From a12166c13bbdf18ab410387d8e03a42d6e6c2079 Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Mon, 22 Mar 2021 14:39:51 -0300 Subject: [PATCH] Use Android Test Orchestrator to have a clean state for each test fixes flaky/broken espresso tests --- .gitlab-ci.yml | 1 + briar-android/build.gradle | 5 +++- .../briarproject/briar/android/UiTest.java | 2 +- .../briar/android/PromoVideoTest.java | 1 - .../briar/android/ScreenshotTest.java | 5 ++++ .../briar/android/SetupDataTest.java | 26 +++++-------------- .../SettingsActivityScreenshotTest.java | 5 ++-- briar-android/witness.gradle | 2 ++ 8 files changed, 22 insertions(+), 25 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 76ecc8a15..c979798b8 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -40,6 +40,7 @@ android test: paths: - kernel.log - logcat.txt + - briar-android/build/reports/androidTests/connected/flavors/* expire_in: 3 days when: on_failure when: manual diff --git a/briar-android/build.gradle b/briar-android/build.gradle index 475fdc569..b6a253871 100644 --- a/briar-android/build.gradle +++ b/briar-android/build.gradle @@ -37,7 +37,7 @@ android { buildConfigField "Long", "BuildTimestamp", "${getStdout(['git', 'log', '-n', '1', '--format=%ct'], now)}000L" testInstrumentationRunner 'org.briarproject.briar.android.BriarTestRunner' - testInstrumentationRunnerArguments disableAnalytics: 'true' + testInstrumentationRunnerArguments disableAnalytics: 'true', clearPackageData: 'true' } buildTypes { @@ -80,6 +80,7 @@ android { } testOptions { + execution 'ANDROIDX_TEST_ORCHESTRATOR' unitTests { includeAndroidResources = true } @@ -148,6 +149,8 @@ dependencies { androidTestImplementation "androidx.test.espresso:espresso-core:$espressoVersion" androidTestImplementation "androidx.test.espresso:espresso-contrib:$espressoVersion" androidTestImplementation "androidx.test.espresso:espresso-intents:$espressoVersion" + androidTestImplementation 'androidx.test:runner:1.3.0' + androidTestUtil 'androidx.test:orchestrator:1.3.0' androidTestAnnotationProcessor "com.google.dagger:dagger-compiler:2.24" androidTestCompileOnly 'javax.annotation:jsr250-api:1.0' androidTestImplementation 'junit:junit:4.13.1' diff --git a/briar-android/src/androidTest/java/org/briarproject/briar/android/UiTest.java b/briar-android/src/androidTest/java/org/briarproject/briar/android/UiTest.java index af75f3bbf..030b68dc9 100644 --- a/briar-android/src/androidTest/java/org/briarproject/briar/android/UiTest.java +++ b/briar-android/src/androidTest/java/org/briarproject/briar/android/UiTest.java @@ -52,7 +52,7 @@ public abstract class UiTest { @Override protected void beforeActivityLaunched() { super.beforeActivityLaunched(); - accountManager.deleteAccount(); + // Android Test Orchestrator already clears existing accounts accountManager.createAccount(USERNAME, PASSWORD); Intent serviceIntent = new Intent(getApplicationContext(), BriarService.class); diff --git a/briar-android/src/androidTestScreenshot/java/org/briarproject/briar/android/PromoVideoTest.java b/briar-android/src/androidTestScreenshot/java/org/briarproject/briar/android/PromoVideoTest.java index f19be7487..87b69cb4a 100644 --- a/briar-android/src/androidTestScreenshot/java/org/briarproject/briar/android/PromoVideoTest.java +++ b/briar-android/src/androidTestScreenshot/java/org/briarproject/briar/android/PromoVideoTest.java @@ -66,7 +66,6 @@ public class PromoVideoTest extends ScreenshotTest { @Override protected void inject(BriarUiTestComponent component) { component.inject(this); - accountManager.deleteAccount(); } @Test diff --git a/briar-android/src/androidTestScreenshot/java/org/briarproject/briar/android/ScreenshotTest.java b/briar-android/src/androidTestScreenshot/java/org/briarproject/briar/android/ScreenshotTest.java index 84fef39b2..61c0a5bbd 100644 --- a/briar-android/src/androidTestScreenshot/java/org/briarproject/briar/android/ScreenshotTest.java +++ b/briar-android/src/androidTestScreenshot/java/org/briarproject/briar/android/ScreenshotTest.java @@ -10,6 +10,7 @@ import org.junit.ClassRule; import javax.inject.Inject; +import androidx.test.ext.junit.rules.ActivityScenarioRule; import tools.fastlane.screengrab.FalconScreenshotStrategy; import tools.fastlane.screengrab.Screengrab; import tools.fastlane.screengrab.locale.LocaleTestRule; @@ -26,6 +27,10 @@ public abstract class ScreenshotTest extends UiTest { @Inject protected Clock clock; + protected void screenshot(String name, ActivityScenarioRule rule) { + rule.getScenario().onActivity(activity -> screenshot(name, activity)); + } + protected void screenshot(String name, Activity activity) { try { Screengrab.screenshot(name, new FalconScreenshotStrategy(activity)); diff --git a/briar-android/src/androidTestScreenshot/java/org/briarproject/briar/android/SetupDataTest.java b/briar-android/src/androidTestScreenshot/java/org/briarproject/briar/android/SetupDataTest.java index 04e5a854e..4faaf99f1 100644 --- a/briar-android/src/androidTestScreenshot/java/org/briarproject/briar/android/SetupDataTest.java +++ b/briar-android/src/androidTestScreenshot/java/org/briarproject/briar/android/SetupDataTest.java @@ -10,7 +10,7 @@ import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; -import androidx.test.espresso.intent.rule.IntentsTestRule; +import androidx.test.ext.junit.rules.ActivityScenarioRule; import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.uiautomator.UiDevice; import androidx.test.uiautomator.UiObject; @@ -22,26 +22,22 @@ import static androidx.test.espresso.action.ViewActions.click; import static androidx.test.espresso.action.ViewActions.typeText; import static androidx.test.espresso.assertion.ViewAssertions.matches; import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed; +import static androidx.test.espresso.matcher.ViewMatchers.isEnabled; import static androidx.test.espresso.matcher.ViewMatchers.withId; import static androidx.test.espresso.matcher.ViewMatchers.withText; import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation; import static org.briarproject.bramble.api.plugin.LanTcpConstants.ID; import static org.briarproject.briar.android.ViewActions.waitUntilMatches; import static org.briarproject.briar.android.util.UiUtils.needsDozeWhitelisting; +import static org.hamcrest.Matchers.allOf; import static org.junit.Assert.assertTrue; @RunWith(AndroidJUnit4.class) public class SetupDataTest extends ScreenshotTest { @Rule - public IntentsTestRule testRule = - new IntentsTestRule(SetupActivity.class) { - @Override - protected void beforeActivityLaunched() { - super.beforeActivityLaunched(); - accountManager.deleteAccount(); - } - }; + public ActivityScenarioRule testRule = + new ActivityScenarioRule<>(SetupActivity.class); @Override protected void inject(BriarUiTestComponent component) { @@ -59,7 +55,7 @@ public class SetupDataTest extends ScreenshotTest { onView(withId(R.id.nickname_entry)) .perform(waitUntilMatches(withText(USERNAME))); - screenshot("manual_create_account", testRule.getActivity()); + screenshot("manual_create_account", testRule); onView(withId(R.id.next)) .check(matches(isDisplayed())) @@ -73,7 +69,7 @@ public class SetupDataTest extends ScreenshotTest { .check(matches(isDisplayed())) .perform(typeText(PASSWORD)); onView(withId(R.id.next)) - .check(matches(isDisplayed())) + .check(matches(allOf(isDisplayed(), isEnabled()))) .perform(click()); // White-list Doze if needed @@ -94,14 +90,6 @@ public class SetupDataTest extends ScreenshotTest { lifecycleManager.waitForStartup(); assertTrue(accountManager.hasDatabaseKey()); createTestData(); - - // close expiry warning - onView(withId(R.id.expiryWarning)) - .perform(waitUntilMatches(isDisplayed())); - onView(withId(R.id.expiryWarningClose)) - .check(matches(isDisplayed())); - onView(withId(R.id.expiryWarningClose)) - .perform(click()); } private void createTestData() { diff --git a/briar-android/src/androidTestScreenshot/java/org/briarproject/briar/android/settings/SettingsActivityScreenshotTest.java b/briar-android/src/androidTestScreenshot/java/org/briarproject/briar/android/settings/SettingsActivityScreenshotTest.java index c99250ad8..c396a835a 100644 --- a/briar-android/src/androidTestScreenshot/java/org/briarproject/briar/android/settings/SettingsActivityScreenshotTest.java +++ b/briar-android/src/androidTestScreenshot/java/org/briarproject/briar/android/settings/SettingsActivityScreenshotTest.java @@ -14,7 +14,6 @@ import org.junit.runner.RunWith; import androidx.recyclerview.widget.RecyclerView; import androidx.test.espresso.contrib.DrawerActions; import androidx.test.ext.junit.runners.AndroidJUnit4; -import androidx.test.rule.ActivityTestRule; import static androidx.test.core.app.ApplicationProvider.getApplicationContext; import static androidx.test.espresso.Espresso.onView; @@ -38,8 +37,8 @@ import static org.junit.Assume.assumeTrue; public class SettingsActivityScreenshotTest extends ScreenshotTest { @Rule - public ActivityTestRule testRule = - new ActivityTestRule<>(SettingsActivity.class); + public CleanAccountTestRule testRule = + new CleanAccountTestRule<>(SettingsActivity.class); @Override protected void inject(BriarUiTestComponent component) { diff --git a/briar-android/witness.gradle b/briar-android/witness.gradle index e3be69cbc..6b11c5cdc 100644 --- a/briar-android/witness.gradle +++ b/briar-android/witness.gradle @@ -51,9 +51,11 @@ dependencyVerification { 'androidx.test.espresso:espresso-idling-resource:3.3.0:espresso-idling-resource-3.3.0.aar:29519b112731f289cc6e2f9b2eccc5ea72c754b04272bb93370f45d7e170a7c6', 'androidx.test.espresso:espresso-intents:3.3.0:espresso-intents-3.3.0.aar:5b6cd6aadce78edc705d93c1e81ace3b59be97128aca0e88fd9c5c176aa9bf10', 'androidx.test.ext:junit:1.1.2:junit-1.1.2.aar:6c6ab120c640bf16fcaae69cb83c144d0ed6b6298562be0ac35e37ed969c0409', + 'androidx.test.services:test-services:1.3.0:test-services-1.3.0.apk:1b88faab6864baf25c5d0b92a610c283c159a566e7a56c03307117fa1b542993', 'androidx.test.uiautomator:uiautomator:2.2.0:uiautomator-2.2.0.aar:2838e9d961dbffefbbd229a2bd4f6f82ac4fb2462975862a9e75e9ed325a3197', 'androidx.test:core:1.3.0:core-1.3.0.aar:86549cae8c5b848f817e2c716e174c7dab61caf0b4df9848680eeb753089a337', 'androidx.test:monitor:1.3.0:monitor-1.3.0.aar:f73a31306a783e63150c60c49e140dc38da39a1b7947690f4b73387b5ebad77e', + 'androidx.test:orchestrator:1.3.0:orchestrator-1.3.0.apk:676f808d08a3d05050eae30c3b7d92ce5cef1e00a54d68355bb7e7d4b72366fe', 'androidx.test:rules:1.3.0:rules-1.3.0.aar:c1753946c498b0d5d7cf341cfed661f66915c4c9deb4ed10462a08ae33b2429a', 'androidx.test:runner:1.3.0:runner-1.3.0.aar:61d13f5a9fcbbd73ba18fa84e1d6a0111c6e1c665a89b418126966e61fffd93b', 'androidx.tracing:tracing:1.0.0:tracing-1.0.0.aar:07b8b6139665b884a162eccf97891ca50f7f56831233bf25168ae04f7b568612',