From 6f7d0c6f776ae3a782d00e16e309343ac6356e65 Mon Sep 17 00:00:00 2001 From: Torsten Grote <t@grobox.de> Date: Fri, 27 Jul 2018 18:21:37 -0300 Subject: [PATCH] Do proper Doze white-listing in UI tests --- briar-android/proguard-test.txt | 2 +- ...ication.java => TestBriarApplication.java} | 4 +- ...rTestComponent.java => TestComponent.java} | 2 +- .../navdrawer/NavDrawerActivityTest.java | 20 +++--- .../settings/SettingsActivityTest.java | 15 ++-- .../briar/android/test/BriarTestRunner.java | 4 +- .../briar/android/test/ScreenshotTest.java | 71 +++++++++++-------- .../briar/android/test/ViewActions.java | 2 +- .../briar/android/util/UiUtils.java | 2 - 9 files changed, 63 insertions(+), 59 deletions(-) rename briar-android/src/androidTest/java/org/briarproject/briar/android/{BriarTestApplication.java => TestBriarApplication.java} (80%) rename briar-android/src/androidTest/java/org/briarproject/briar/android/{BriarTestComponent.java => TestComponent.java} (90%) diff --git a/briar-android/proguard-test.txt b/briar-android/proguard-test.txt index 7007db0c24..652f46e58c 100644 --- a/briar-android/proguard-test.txt +++ b/briar-android/proguard-test.txt @@ -12,4 +12,4 @@ -keep class junit.** { *; } -dontwarn junit.** --dontwarn org.briarproject.briar.android.BriarTestApplication \ No newline at end of file +-dontwarn org.briarproject.briar.android.TestBriarApplication \ No newline at end of file diff --git a/briar-android/src/androidTest/java/org/briarproject/briar/android/BriarTestApplication.java b/briar-android/src/androidTest/java/org/briarproject/briar/android/TestBriarApplication.java similarity index 80% rename from briar-android/src/androidTest/java/org/briarproject/briar/android/BriarTestApplication.java rename to briar-android/src/androidTest/java/org/briarproject/briar/android/TestBriarApplication.java index 8bdd5b63a6..eb6d86057f 100644 --- a/briar-android/src/androidTest/java/org/briarproject/briar/android/BriarTestApplication.java +++ b/briar-android/src/androidTest/java/org/briarproject/briar/android/TestBriarApplication.java @@ -3,11 +3,11 @@ package org.briarproject.briar.android; import org.briarproject.bramble.BrambleCoreModule; import org.briarproject.briar.BriarCoreModule; -public class BriarTestApplication extends BriarApplicationImpl { +public class TestBriarApplication extends BriarApplicationImpl { @Override protected AndroidComponent createApplicationComponent() { - AndroidComponent component = DaggerBriarTestComponent.builder() + AndroidComponent component = DaggerTestComponent.builder() .appModule(new AppModule(this)).build(); // We need to load the eager singletons directly after making the // dependency graphs diff --git a/briar-android/src/androidTest/java/org/briarproject/briar/android/BriarTestComponent.java b/briar-android/src/androidTest/java/org/briarproject/briar/android/TestComponent.java similarity index 90% rename from briar-android/src/androidTest/java/org/briarproject/briar/android/BriarTestComponent.java rename to briar-android/src/androidTest/java/org/briarproject/briar/android/TestComponent.java index 33166b8c4b..a42980f101 100644 --- a/briar-android/src/androidTest/java/org/briarproject/briar/android/BriarTestComponent.java +++ b/briar-android/src/androidTest/java/org/briarproject/briar/android/TestComponent.java @@ -17,7 +17,7 @@ import dagger.Component; BrambleAndroidModule.class, BrambleCoreModule.class }) -public interface BriarTestComponent extends AndroidComponent { +public interface TestComponent extends AndroidComponent { void inject(NavDrawerActivityTest test); void inject(SettingsActivityTest test); diff --git a/briar-android/src/androidTest/java/org/briarproject/briar/android/navdrawer/NavDrawerActivityTest.java b/briar-android/src/androidTest/java/org/briarproject/briar/android/navdrawer/NavDrawerActivityTest.java index b290b2be27..4f8b03574e 100644 --- a/briar-android/src/androidTest/java/org/briarproject/briar/android/navdrawer/NavDrawerActivityTest.java +++ b/briar-android/src/androidTest/java/org/briarproject/briar/android/navdrawer/NavDrawerActivityTest.java @@ -1,16 +1,18 @@ package org.briarproject.briar.android.navdrawer; +import android.app.Activity; import android.support.test.espresso.contrib.DrawerActions; import android.support.test.espresso.intent.rule.IntentsTestRule; +import android.support.test.runner.AndroidJUnit4; import android.view.Gravity; import org.briarproject.briar.R; -import org.briarproject.briar.android.BriarTestComponent; +import org.briarproject.briar.android.TestComponent; import org.briarproject.briar.android.settings.SettingsActivity; import org.briarproject.briar.android.test.ScreenshotTest; -import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import org.junit.runner.RunWith; import static android.support.test.espresso.Espresso.onView; import static android.support.test.espresso.action.ViewActions.click; @@ -19,11 +21,10 @@ import static android.support.test.espresso.contrib.DrawerMatchers.isClosed; import static android.support.test.espresso.intent.Intents.intended; import static android.support.test.espresso.intent.matcher.IntentMatchers.hasComponent; import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; -import static android.support.test.espresso.matcher.ViewMatchers.isRoot; import static android.support.test.espresso.matcher.ViewMatchers.withId; import static android.support.test.espresso.matcher.ViewMatchers.withText; -import static org.briarproject.briar.android.test.ViewActions.waitForActivityToResume; +@RunWith(AndroidJUnit4.class) public class NavDrawerActivityTest extends ScreenshotTest { @Rule @@ -31,20 +32,19 @@ public class NavDrawerActivityTest extends ScreenshotTest { new IntentsTestRule<>(NavDrawerActivity.class); @Override - protected void inject(BriarTestComponent component) { + protected void inject(TestComponent component) { component.inject(this); } - @Before - public void waitForSignIn() { - onView(isRoot()) - .perform(waitForActivityToResume(activityRule.getActivity())); + @Override + protected Activity getActivity() { + return activityRule.getActivity(); } @Test public void openSettings() { onView(withId(R.id.drawer_layout)) - .check(matches(isClosed(Gravity.LEFT))) + .check(matches(isClosed(Gravity.START))) .perform(DrawerActions.open()); onView(withText(R.string.settings_button)) .check(matches(isDisplayed())) diff --git a/briar-android/src/androidTest/java/org/briarproject/briar/android/settings/SettingsActivityTest.java b/briar-android/src/androidTest/java/org/briarproject/briar/android/settings/SettingsActivityTest.java index fd3dd61b7d..5817d732f2 100644 --- a/briar-android/src/androidTest/java/org/briarproject/briar/android/settings/SettingsActivityTest.java +++ b/briar-android/src/androidTest/java/org/briarproject/briar/android/settings/SettingsActivityTest.java @@ -1,5 +1,6 @@ package org.briarproject.briar.android.settings; +import android.app.Activity; import android.content.Intent; import android.support.test.espresso.contrib.DrawerActions; import android.support.test.rule.ActivityTestRule; @@ -9,10 +10,9 @@ import android.view.Gravity; import junit.framework.AssertionFailedError; import org.briarproject.briar.R; -import org.briarproject.briar.android.BriarTestComponent; +import org.briarproject.briar.android.TestComponent; import org.briarproject.briar.android.navdrawer.NavDrawerActivity; import org.briarproject.briar.android.test.ScreenshotTest; -import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; @@ -22,10 +22,8 @@ import static android.support.test.espresso.action.ViewActions.click; import static android.support.test.espresso.assertion.ViewAssertions.matches; import static android.support.test.espresso.contrib.DrawerMatchers.isClosed; import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; -import static android.support.test.espresso.matcher.ViewMatchers.isRoot; import static android.support.test.espresso.matcher.ViewMatchers.withId; import static android.support.test.espresso.matcher.ViewMatchers.withText; -import static org.briarproject.briar.android.test.ViewActions.waitForActivityToResume; @RunWith(AndroidJUnit4.class) public class SettingsActivityTest extends ScreenshotTest { @@ -35,14 +33,13 @@ public class SettingsActivityTest extends ScreenshotTest { new ActivityTestRule<>(SettingsActivity.class); @Override - protected void inject(BriarTestComponent component) { + protected void inject(TestComponent component) { component.inject(this); } - @Before - public void waitForSignIn() { - onView(isRoot()) - .perform(waitForActivityToResume(activityRule.getActivity())); + @Override + protected Activity getActivity() { + return activityRule.getActivity(); } @Test diff --git a/briar-android/src/androidTest/java/org/briarproject/briar/android/test/BriarTestRunner.java b/briar-android/src/androidTest/java/org/briarproject/briar/android/test/BriarTestRunner.java index cefb68766f..655582242e 100644 --- a/briar-android/src/androidTest/java/org/briarproject/briar/android/test/BriarTestRunner.java +++ b/briar-android/src/androidTest/java/org/briarproject/briar/android/test/BriarTestRunner.java @@ -4,7 +4,7 @@ import android.app.Application; import android.content.Context; import android.support.test.runner.AndroidJUnitRunner; -import org.briarproject.briar.android.BriarTestApplication; +import org.briarproject.briar.android.TestBriarApplication; public class BriarTestRunner extends AndroidJUnitRunner { @@ -13,7 +13,7 @@ public class BriarTestRunner extends AndroidJUnitRunner { Context context) throws InstantiationException, IllegalAccessException, ClassNotFoundException { - return super.newApplication(cl, BriarTestApplication.class.getName(), + return super.newApplication(cl, TestBriarApplication.class.getName(), context); } diff --git a/briar-android/src/androidTest/java/org/briarproject/briar/android/test/ScreenshotTest.java b/briar-android/src/androidTest/java/org/briarproject/briar/android/test/ScreenshotTest.java index 4b73dc0cc8..ef11475a9c 100644 --- a/briar-android/src/androidTest/java/org/briarproject/briar/android/test/ScreenshotTest.java +++ b/briar-android/src/androidTest/java/org/briarproject/briar/android/test/ScreenshotTest.java @@ -1,15 +1,16 @@ package org.briarproject.briar.android.test; -import android.support.test.InstrumentationRegistry; +import android.app.Activity; import android.support.test.espresso.NoMatchingViewException; -import android.support.test.espresso.contrib.DrawerActions; +import android.support.test.uiautomator.UiDevice; +import android.support.test.uiautomator.UiObject; +import android.support.test.uiautomator.UiSelector; import android.util.Log; -import android.view.Gravity; import org.briarproject.bramble.api.lifecycle.LifecycleManager; import org.briarproject.briar.R; -import org.briarproject.briar.android.BriarTestApplication; -import org.briarproject.briar.android.BriarTestComponent; +import org.briarproject.briar.android.TestBriarApplication; +import org.briarproject.briar.android.TestComponent; import org.junit.Before; import org.junit.ClassRule; @@ -19,15 +20,19 @@ import tools.fastlane.screengrab.Screengrab; import tools.fastlane.screengrab.UiAutomatorScreenshotStrategy; import tools.fastlane.screengrab.locale.LocaleTestRule; +import static android.support.test.InstrumentationRegistry.getInstrumentation; +import static android.support.test.InstrumentationRegistry.getTargetContext; import static android.support.test.espresso.Espresso.onView; import static android.support.test.espresso.action.ViewActions.click; import static android.support.test.espresso.action.ViewActions.typeText; import static android.support.test.espresso.assertion.ViewAssertions.matches; -import static android.support.test.espresso.contrib.DrawerMatchers.isClosed; import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; +import static android.support.test.espresso.matcher.ViewMatchers.isRoot; import static android.support.test.espresso.matcher.ViewMatchers.withId; import static android.support.test.espresso.matcher.ViewMatchers.withText; import static org.briarproject.bramble.api.lifecycle.LifecycleManager.LifecycleState.RUNNING; +import static org.briarproject.briar.android.test.ViewActions.waitForActivityToResume; +import static org.briarproject.briar.android.util.UiUtils.needsDozeWhitelisting; import static tools.fastlane.screengrab.Screengrab.setDefaultScreenshotStrategy; public abstract class ScreenshotTest { @@ -35,31 +40,26 @@ public abstract class ScreenshotTest { @ClassRule public static final LocaleTestRule localeTestRule = new LocaleTestRule(); - @Before - public void setupScreenshots() { - setDefaultScreenshotStrategy(new UiAutomatorScreenshotStrategy()); - } - private static final String USERNAME = "test"; private static final String PASSWORD = "123456"; - private final BriarTestApplication app = - (BriarTestApplication) InstrumentationRegistry.getTargetContext() + private final TestBriarApplication app = + (TestBriarApplication) getTargetContext() .getApplicationContext(); @Inject LifecycleManager lifecycleManager; - protected abstract void inject(BriarTestComponent component); + protected abstract void inject(TestComponent component); + protected abstract Activity getActivity(); + + @Before + public void setupScreenshots() { + setDefaultScreenshotStrategy(new UiAutomatorScreenshotStrategy()); + } - /** - * Signs the user in. - * - * Note that you need to wait for your UI to show up after this. - * See {@link ViewActions#waitForActivityToResume} for one way to do it. - */ @Before public void signIn() throws Exception { - inject((BriarTestComponent) app.getApplicationComponent()); + inject((TestComponent) app.getApplicationComponent()); if (lifecycleManager.getLifecycleState() == RUNNING) return; try { @@ -73,9 +73,11 @@ public abstract class ScreenshotTest { // we start from a blank state and have no account, yet createAccount(); } + onView(isRoot()) + .perform(waitForActivityToResume(getActivity())); } - private void createAccount() { + private void createAccount() throws Exception { // TODO use AccountManager to start with fresh account // TODO move this below into a dedicated test for SetupActivity @@ -99,19 +101,26 @@ public abstract class ScreenshotTest { onView(withId(R.id.next)) .check(matches(isDisplayed())) .perform(click()); + + // White-list Doze if needed + if (needsDozeWhitelisting(getTargetContext())) { + onView(withText(R.string.setup_doze_button)) + .check(matches(isDisplayed())) + .perform(click()); + UiDevice device = UiDevice.getInstance(getInstrumentation()); + UiObject allowButton = device.findObject( + new UiSelector().className("android.widget.Button") + .index(1)); + allowButton.click(); + onView(withId(R.id.next)) + .check(matches(isDisplayed())) + .perform(click()); + } + onView(withId(R.id.progress)) .check(matches(isDisplayed())); } - protected void signOut() { - onView(withId(R.id.drawer_layout)) - .check(matches(isClosed(Gravity.LEFT))) - .perform(DrawerActions.open()); - onView(withText(R.string.sign_out_button)) - .check(matches(isDisplayed())) - .perform(click()); - } - protected void screenshot(String name) { try { Screengrab.screenshot(name); diff --git a/briar-android/src/androidTest/java/org/briarproject/briar/android/test/ViewActions.java b/briar-android/src/androidTest/java/org/briarproject/briar/android/test/ViewActions.java index 806124d1b6..1dc07c8c78 100644 --- a/briar-android/src/androidTest/java/org/briarproject/briar/android/test/ViewActions.java +++ b/briar-android/src/androidTest/java/org/briarproject/briar/android/test/ViewActions.java @@ -21,7 +21,7 @@ import static java.util.concurrent.TimeUnit.SECONDS; public class ViewActions { - private final static long TIMEOUT_MS = SECONDS.toMillis(5); + private final static long TIMEOUT_MS = SECONDS.toMillis(10); private final static long WAIT_MS = 50; public static ViewAction waitUntilMatches(Matcher<View> viewMatcher) { diff --git a/briar-android/src/main/java/org/briarproject/briar/android/util/UiUtils.java b/briar-android/src/main/java/org/briarproject/briar/android/util/UiUtils.java index 2485f81c95..ee30d8d37f 100644 --- a/briar-android/src/main/java/org/briarproject/briar/android/util/UiUtils.java +++ b/briar-android/src/main/java/org/briarproject/briar/android/util/UiUtils.java @@ -55,7 +55,6 @@ import static android.text.format.DateUtils.FORMAT_SHOW_DATE; import static android.text.format.DateUtils.MINUTE_IN_MILLIS; import static android.text.format.DateUtils.WEEK_IN_MILLIS; import static org.briarproject.briar.BuildConfig.APPLICATION_ID; -import static org.briarproject.briar.BuildConfig.BUILD_TYPE; import static org.briarproject.briar.android.TestingConstants.EXPIRY_DATE; @MethodsNotNullByDefault @@ -175,7 +174,6 @@ public class UiUtils { public static boolean needsDozeWhitelisting(Context ctx) { if (SDK_INT < 23) return false; - if (BUILD_TYPE.equals("screenshot")) return false; PowerManager pm = (PowerManager) ctx.getSystemService(POWER_SERVICE); String packageName = ctx.getPackageName(); if (pm == null) throw new AssertionError(); -- GitLab