Commit 6f7d0c6f authored by Torsten Grote's avatar Torsten Grote

Do proper Doze white-listing in UI tests

parent 412381f3
...@@ -12,4 +12,4 @@ ...@@ -12,4 +12,4 @@
-keep class junit.** { *; } -keep class junit.** { *; }
-dontwarn junit.** -dontwarn junit.**
-dontwarn org.briarproject.briar.android.BriarTestApplication -dontwarn org.briarproject.briar.android.TestBriarApplication
\ No newline at end of file \ No newline at end of file
...@@ -3,11 +3,11 @@ package org.briarproject.briar.android; ...@@ -3,11 +3,11 @@ package org.briarproject.briar.android;
import org.briarproject.bramble.BrambleCoreModule; import org.briarproject.bramble.BrambleCoreModule;
import org.briarproject.briar.BriarCoreModule; import org.briarproject.briar.BriarCoreModule;
public class BriarTestApplication extends BriarApplicationImpl { public class TestBriarApplication extends BriarApplicationImpl {
@Override @Override
protected AndroidComponent createApplicationComponent() { protected AndroidComponent createApplicationComponent() {
AndroidComponent component = DaggerBriarTestComponent.builder() AndroidComponent component = DaggerTestComponent.builder()
.appModule(new AppModule(this)).build(); .appModule(new AppModule(this)).build();
// We need to load the eager singletons directly after making the // We need to load the eager singletons directly after making the
// dependency graphs // dependency graphs
......
...@@ -17,7 +17,7 @@ import dagger.Component; ...@@ -17,7 +17,7 @@ import dagger.Component;
BrambleAndroidModule.class, BrambleAndroidModule.class,
BrambleCoreModule.class BrambleCoreModule.class
}) })
public interface BriarTestComponent extends AndroidComponent { public interface TestComponent extends AndroidComponent {
void inject(NavDrawerActivityTest test); void inject(NavDrawerActivityTest test);
void inject(SettingsActivityTest test); void inject(SettingsActivityTest test);
......
package org.briarproject.briar.android.navdrawer; package org.briarproject.briar.android.navdrawer;
import android.app.Activity;
import android.support.test.espresso.contrib.DrawerActions; import android.support.test.espresso.contrib.DrawerActions;
import android.support.test.espresso.intent.rule.IntentsTestRule; import android.support.test.espresso.intent.rule.IntentsTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.view.Gravity; import android.view.Gravity;
import org.briarproject.briar.R; 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.settings.SettingsActivity;
import org.briarproject.briar.android.test.ScreenshotTest; import org.briarproject.briar.android.test.ScreenshotTest;
import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith;
import static android.support.test.espresso.Espresso.onView; 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.click;
...@@ -19,11 +21,10 @@ import static android.support.test.espresso.contrib.DrawerMatchers.isClosed; ...@@ -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.Intents.intended;
import static android.support.test.espresso.intent.matcher.IntentMatchers.hasComponent; 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.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.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText; 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 { public class NavDrawerActivityTest extends ScreenshotTest {
@Rule @Rule
...@@ -31,20 +32,19 @@ public class NavDrawerActivityTest extends ScreenshotTest { ...@@ -31,20 +32,19 @@ public class NavDrawerActivityTest extends ScreenshotTest {
new IntentsTestRule<>(NavDrawerActivity.class); new IntentsTestRule<>(NavDrawerActivity.class);
@Override @Override
protected void inject(BriarTestComponent component) { protected void inject(TestComponent component) {
component.inject(this); component.inject(this);
} }
@Before @Override
public void waitForSignIn() { protected Activity getActivity() {
onView(isRoot()) return activityRule.getActivity();
.perform(waitForActivityToResume(activityRule.getActivity()));
} }
@Test @Test
public void openSettings() { public void openSettings() {
onView(withId(R.id.drawer_layout)) onView(withId(R.id.drawer_layout))
.check(matches(isClosed(Gravity.LEFT))) .check(matches(isClosed(Gravity.START)))
.perform(DrawerActions.open()); .perform(DrawerActions.open());
onView(withText(R.string.settings_button)) onView(withText(R.string.settings_button))
.check(matches(isDisplayed())) .check(matches(isDisplayed()))
......
package org.briarproject.briar.android.settings; package org.briarproject.briar.android.settings;
import android.app.Activity;
import android.content.Intent; import android.content.Intent;
import android.support.test.espresso.contrib.DrawerActions; import android.support.test.espresso.contrib.DrawerActions;
import android.support.test.rule.ActivityTestRule; import android.support.test.rule.ActivityTestRule;
...@@ -9,10 +10,9 @@ import android.view.Gravity; ...@@ -9,10 +10,9 @@ import android.view.Gravity;
import junit.framework.AssertionFailedError; import junit.framework.AssertionFailedError;
import org.briarproject.briar.R; 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.navdrawer.NavDrawerActivity;
import org.briarproject.briar.android.test.ScreenshotTest; import org.briarproject.briar.android.test.ScreenshotTest;
import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
...@@ -22,10 +22,8 @@ import static android.support.test.espresso.action.ViewActions.click; ...@@ -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.assertion.ViewAssertions.matches;
import static android.support.test.espresso.contrib.DrawerMatchers.isClosed; 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.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.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText; import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.briarproject.briar.android.test.ViewActions.waitForActivityToResume;
@RunWith(AndroidJUnit4.class) @RunWith(AndroidJUnit4.class)
public class SettingsActivityTest extends ScreenshotTest { public class SettingsActivityTest extends ScreenshotTest {
...@@ -35,14 +33,13 @@ public class SettingsActivityTest extends ScreenshotTest { ...@@ -35,14 +33,13 @@ public class SettingsActivityTest extends ScreenshotTest {
new ActivityTestRule<>(SettingsActivity.class); new ActivityTestRule<>(SettingsActivity.class);
@Override @Override
protected void inject(BriarTestComponent component) { protected void inject(TestComponent component) {
component.inject(this); component.inject(this);
} }
@Before @Override
public void waitForSignIn() { protected Activity getActivity() {
onView(isRoot()) return activityRule.getActivity();
.perform(waitForActivityToResume(activityRule.getActivity()));
} }
@Test @Test
......
...@@ -4,7 +4,7 @@ import android.app.Application; ...@@ -4,7 +4,7 @@ import android.app.Application;
import android.content.Context; import android.content.Context;
import android.support.test.runner.AndroidJUnitRunner; import android.support.test.runner.AndroidJUnitRunner;
import org.briarproject.briar.android.BriarTestApplication; import org.briarproject.briar.android.TestBriarApplication;
public class BriarTestRunner extends AndroidJUnitRunner { public class BriarTestRunner extends AndroidJUnitRunner {
...@@ -13,7 +13,7 @@ public class BriarTestRunner extends AndroidJUnitRunner { ...@@ -13,7 +13,7 @@ public class BriarTestRunner extends AndroidJUnitRunner {
Context context) Context context)
throws InstantiationException, IllegalAccessException, throws InstantiationException, IllegalAccessException,
ClassNotFoundException { ClassNotFoundException {
return super.newApplication(cl, BriarTestApplication.class.getName(), return super.newApplication(cl, TestBriarApplication.class.getName(),
context); context);
} }
......
package org.briarproject.briar.android.test; 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.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.util.Log;
import android.view.Gravity;
import org.briarproject.bramble.api.lifecycle.LifecycleManager; import org.briarproject.bramble.api.lifecycle.LifecycleManager;
import org.briarproject.briar.R; import org.briarproject.briar.R;
import org.briarproject.briar.android.BriarTestApplication; import org.briarproject.briar.android.TestBriarApplication;
import org.briarproject.briar.android.BriarTestComponent; import org.briarproject.briar.android.TestComponent;
import org.junit.Before; import org.junit.Before;
import org.junit.ClassRule; import org.junit.ClassRule;
...@@ -19,15 +20,19 @@ import tools.fastlane.screengrab.Screengrab; ...@@ -19,15 +20,19 @@ import tools.fastlane.screengrab.Screengrab;
import tools.fastlane.screengrab.UiAutomatorScreenshotStrategy; import tools.fastlane.screengrab.UiAutomatorScreenshotStrategy;
import tools.fastlane.screengrab.locale.LocaleTestRule; 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.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click; import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.typeText; import static android.support.test.espresso.action.ViewActions.typeText;
import static android.support.test.espresso.assertion.ViewAssertions.matches; 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.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.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText; import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.LifecycleState.RUNNING; 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; import static tools.fastlane.screengrab.Screengrab.setDefaultScreenshotStrategy;
public abstract class ScreenshotTest { public abstract class ScreenshotTest {
...@@ -35,31 +40,26 @@ public abstract class ScreenshotTest { ...@@ -35,31 +40,26 @@ public abstract class ScreenshotTest {
@ClassRule @ClassRule
public static final LocaleTestRule localeTestRule = new LocaleTestRule(); public static final LocaleTestRule localeTestRule = new LocaleTestRule();
@Before
public void setupScreenshots() {
setDefaultScreenshotStrategy(new UiAutomatorScreenshotStrategy());
}
private static final String USERNAME = "test"; private static final String USERNAME = "test";
private static final String PASSWORD = "123456"; private static final String PASSWORD = "123456";
private final BriarTestApplication app = private final TestBriarApplication app =
(BriarTestApplication) InstrumentationRegistry.getTargetContext() (TestBriarApplication) getTargetContext()
.getApplicationContext(); .getApplicationContext();
@Inject @Inject
LifecycleManager lifecycleManager; 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 @Before
public void signIn() throws Exception { public void signIn() throws Exception {
inject((BriarTestComponent) app.getApplicationComponent()); inject((TestComponent) app.getApplicationComponent());
if (lifecycleManager.getLifecycleState() == RUNNING) return; if (lifecycleManager.getLifecycleState() == RUNNING) return;
try { try {
...@@ -73,9 +73,11 @@ public abstract class ScreenshotTest { ...@@ -73,9 +73,11 @@ public abstract class ScreenshotTest {
// we start from a blank state and have no account, yet // we start from a blank state and have no account, yet
createAccount(); createAccount();
} }
onView(isRoot())
.perform(waitForActivityToResume(getActivity()));
} }
private void createAccount() { private void createAccount() throws Exception {
// TODO use AccountManager to start with fresh account // TODO use AccountManager to start with fresh account
// TODO move this below into a dedicated test for SetupActivity // TODO move this below into a dedicated test for SetupActivity
...@@ -99,19 +101,26 @@ public abstract class ScreenshotTest { ...@@ -99,19 +101,26 @@ public abstract class ScreenshotTest {
onView(withId(R.id.next)) onView(withId(R.id.next))
.check(matches(isDisplayed())) .check(matches(isDisplayed()))
.perform(click()); .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)) onView(withId(R.id.progress))
.check(matches(isDisplayed())); .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) { protected void screenshot(String name) {
try { try {
Screengrab.screenshot(name); Screengrab.screenshot(name);
......
...@@ -21,7 +21,7 @@ import static java.util.concurrent.TimeUnit.SECONDS; ...@@ -21,7 +21,7 @@ import static java.util.concurrent.TimeUnit.SECONDS;
public class ViewActions { 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; private final static long WAIT_MS = 50;
public static ViewAction waitUntilMatches(Matcher<View> viewMatcher) { public static ViewAction waitUntilMatches(Matcher<View> viewMatcher) {
......
...@@ -55,7 +55,6 @@ import static android.text.format.DateUtils.FORMAT_SHOW_DATE; ...@@ -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.MINUTE_IN_MILLIS;
import static android.text.format.DateUtils.WEEK_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.APPLICATION_ID;
import static org.briarproject.briar.BuildConfig.BUILD_TYPE;
import static org.briarproject.briar.android.TestingConstants.EXPIRY_DATE; import static org.briarproject.briar.android.TestingConstants.EXPIRY_DATE;
@MethodsNotNullByDefault @MethodsNotNullByDefault
...@@ -175,7 +174,6 @@ public class UiUtils { ...@@ -175,7 +174,6 @@ public class UiUtils {
public static boolean needsDozeWhitelisting(Context ctx) { public static boolean needsDozeWhitelisting(Context ctx) {
if (SDK_INT < 23) return false; if (SDK_INT < 23) return false;
if (BUILD_TYPE.equals("screenshot")) return false;
PowerManager pm = (PowerManager) ctx.getSystemService(POWER_SERVICE); PowerManager pm = (PowerManager) ctx.getSystemService(POWER_SERVICE);
String packageName = ctx.getPackageName(); String packageName = ctx.getPackageName();
if (pm == null) throw new AssertionError(); if (pm == null) throw new AssertionError();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment