Skip to content

Commit

Permalink
Added StorageMigrationCompletedBannerTest
Browse files Browse the repository at this point in the history
  • Loading branch information
grzesiek2010 committed Feb 26, 2020
1 parent a5c897d commit 12853f6
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.odk.collect.android.storage;

import androidx.test.rule.ActivityTestRule;

import org.junit.Rule;
import org.junit.Test;
import org.odk.collect.android.activities.MainMenuActivity;
import org.odk.collect.android.support.StorageMigrationCompletedRule;
import org.odk.collect.android.support.pages.MainMenuPage;

public class StorageMigrationCompletedBannerTest {

@Rule
public ActivityTestRule<MainMenuActivity> main = new ActivityTestRule<>(MainMenuActivity.class);

@Rule
public StorageMigrationCompletedRule storageMigrationCompletedRule = new StorageMigrationCompletedRule();

@Test
public void when_storageMigrationCompleted_should_bannerBeVisibleAndPersistScreenRotation() {
new MainMenuPage(main)
.assertStorageMigrationCompletedBannerIsDisplayed()
.rotateToLandscape(new MainMenuPage(main))
.assertStorageMigrationCompletedBannerIsDisplayed()
.rotateToPortrait(new MainMenuPage(main))
.assertStorageMigrationCompletedBannerIsDisplayed()
.clickDismissButton()
.assertStorageMigrationCompletedBannerIsNotDisplayed()
.rotateToLandscape(new MainMenuPage(main))
.assertStorageMigrationCompletedBannerIsNotDisplayed()
.rotateToPortrait(new MainMenuPage(main))
.assertStorageMigrationCompletedBannerIsNotDisplayed();
}

@Test
public void when_storageMigrationCompleted_should_bannerBeVisibleAndDismissForEverIfAUserClicksDismissButton() {
new MainMenuPage(main)
.assertStorageMigrationCompletedBannerIsDisplayed()
.clickDismissButton()
.assertStorageMigrationCompletedBannerIsNotDisplayed()
.rotateToLandscape(new MainMenuPage(main))
.assertStorageMigrationCompletedBannerIsNotDisplayed()
.rotateToPortrait(new MainMenuPage(main))
.assertStorageMigrationCompletedBannerIsNotDisplayed()
.reopenApp()
.assertStorageMigrationCompletedBannerIsNotDisplayed();
}

@Test
public void when_storageMigrationCompleted_should_bannerBeVisibleAndDismissAfterReopeningApp() {
new MainMenuPage(main)
.assertStorageMigrationCompletedBannerIsDisplayed()
.reopenApp()
.assertStorageMigrationCompletedBannerIsNotDisplayed();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.odk.collect.android.support;

import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.odk.collect.android.storage.StorageStateProvider;
import org.odk.collect.android.storage.migration.StorageMigrationRepository;
import org.odk.collect.android.storage.migration.StorageMigrationResult;

import javax.inject.Singleton;

import dagger.Provides;

import static org.odk.collect.android.support.CollectHelpers.overrideAppDependencyModule;

public class StorageMigrationCompletedRule implements TestRule {

@Override
public Statement apply(Statement base, Description description) {
return new StorageMigrationCompletedStatement(base);
}

private class StorageMigrationCompletedStatement extends Statement {

private final Statement base;

StorageMigrationCompletedStatement(Statement base) {
this.base = base;
}

@Override
public void evaluate() throws Throwable {
overrideAppDependencyModule(new AppDependencyModule());
new StorageStateProvider().enableUsingScopedStorage();
base.evaluate();
}
}

private static class AppDependencyModule extends org.odk.collect.android.injection.config.AppDependencyModule {

@Provides
@Singleton
public StorageMigrationRepository providesStorageMigrationRepository() {
StorageMigrationRepository storageMigrationRepository = new StorageMigrationRepository();
storageMigrationRepository.setResult(StorageMigrationResult.SUCCESS);
return storageMigrationRepository;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
import org.odk.collect.android.support.ActivityHelpers;

import androidx.test.espresso.Espresso;
import androidx.test.espresso.NoActivityResumedException;
import androidx.test.rule.ActivityTestRule;

import timber.log.Timber;

import static androidx.test.espresso.Espresso.onData;
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
Expand All @@ -15,6 +18,7 @@
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.core.IsNot.not;

public class MainMenuPage extends Page<MainMenuPage> {

Expand Down Expand Up @@ -86,5 +90,31 @@ public MainMenuPage assertNumberOfFinalizedForms(int number) {
}
return this;
}

public MainMenuPage assertStorageMigrationCompletedBannerIsDisplayed() {
onView(withText(R.string.storage_migration_completed)).check(matches(isDisplayed()));
onView(withText(R.string.scoped_storage_dismiss)).check(matches(isDisplayed()));
return this;
}

public MainMenuPage assertStorageMigrationCompletedBannerIsNotDisplayed() {
onView(withId(R.id.storageMigrationBanner)).check(matches(not(isDisplayed())));
return this;
}

public MainMenuPage clickDismissButton() {
onView(withId(R.id.storageMigrationBannerDismissButton)).perform(click());
return this;
}

public MainMenuPage reopenApp() {
try {
Espresso.pressBack();
} catch (NoActivityResumedException e) {
Timber.i(e);
}
rule.launchActivity(null);
return this;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public ActivityAvailability providesActivityAvailability(Context context) {

@Provides
@Singleton
StorageMigrationRepository providesStorageMigrationRepository() {
public StorageMigrationRepository providesStorageMigrationRepository() {
return new StorageMigrationRepository();
}

Expand Down

0 comments on commit 12853f6

Please sign in to comment.