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

Do proper Doze white-listing in UI tests

parent 412381f3
......@@ -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
......@@ -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
......
......@@ -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);
......
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()))
......
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
......
......@@ -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);
}
......
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);
......
......@@ -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) {
......
......@@ -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();
......
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