From d1db225ffb24d17de5ac373d3f6413295f8aadf2 Mon Sep 17 00:00:00 2001 From: Matthieu <24-artectrex@users.noreply.shinice.net> Date: Thu, 20 May 2021 13:39:11 +0200 Subject: [PATCH] a bit more reliability for tests --- .../java/org/pixeldroid/app/DrawerMenuTest.kt | 5 +++++ .../org/pixeldroid/app/testUtility/CustomMatchers.kt | 9 ++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/app/src/androidTest/java/org/pixeldroid/app/DrawerMenuTest.kt b/app/src/androidTest/java/org/pixeldroid/app/DrawerMenuTest.kt index 5d763c04..81175963 100644 --- a/app/src/androidTest/java/org/pixeldroid/app/DrawerMenuTest.kt +++ b/app/src/androidTest/java/org/pixeldroid/app/DrawerMenuTest.kt @@ -12,6 +12,7 @@ import androidx.test.espresso.matcher.ViewMatchers.* import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation import androidx.test.uiautomator.UiDevice +import org.hamcrest.Matchers.allOf import org.pixeldroid.app.testUtility.* import org.pixeldroid.app.utils.db.AppDatabase import org.junit.After @@ -107,8 +108,10 @@ class DrawerMenuTest { // Start the screen of your activity. onView(withText(R.string.menu_account)).perform(click()) // Check that profile activity was opened. + waitForView(R.id.editButton) onView(withId(R.id.editButton)).check(matches(isDisplayed())) val followersText = context.resources.getQuantityString(R.plurals.nb_followers, 2, 2) + waitForView(R.id.nbFollowingTextView, allOf(withId(R.id.nbFollowersTextView), withText(followersText))) onView(withText(followersText)).perform(click()) waitForView(R.id.account_entry_avatar) @@ -120,8 +123,10 @@ class DrawerMenuTest { // Start the screen of your activity. onView(withText(R.string.menu_account)).perform(click()) // Check that profile activity was opened. + waitForView(R.id.editButton) onView(withId(R.id.editButton)).check(matches(isDisplayed())) val followingText = context.resources.getQuantityString(R.plurals.nb_following, 3, 3) + waitForView(R.id.nbFollowingTextView, allOf(withId(R.id.nbFollowingTextView), withText(followingText))) onView(withText(followingText)).perform(click()) waitForView(R.id.account_entry_avatar) diff --git a/app/src/androidTest/java/org/pixeldroid/app/testUtility/CustomMatchers.kt b/app/src/androidTest/java/org/pixeldroid/app/testUtility/CustomMatchers.kt index fddf11d6..5c09d096 100644 --- a/app/src/androidTest/java/org/pixeldroid/app/testUtility/CustomMatchers.kt +++ b/app/src/androidTest/java/org/pixeldroid/app/testUtility/CustomMatchers.kt @@ -66,15 +66,15 @@ fun ViewInteraction.isDisplayed(): Boolean { * Doesn't work if the root changes (since it operates on the root!) * @param viewId The id of the view to wait for. */ -fun waitForView(viewId: Int) { - Espresso.onView(isRoot()).perform(waitForViewViewAction(viewId)) +fun waitForView(viewId: Int, viewMatcher: Matcher = withId(viewId)) { + Espresso.onView(isRoot()).perform(waitForViewViewAction(viewId, viewMatcher)) } /** * This ViewAction tells espresso to wait till a certain view is found in the view hierarchy. * @param viewId The id of the view to wait for. */ -private fun waitForViewViewAction(viewId: Int): ViewAction { +private fun waitForViewViewAction(viewId: Int, viewMatcher: Matcher): ViewAction { // The maximum time which espresso will wait for the view to show up (in milliseconds) val timeOut = 5000 return object : ViewAction { @@ -90,7 +90,6 @@ private fun waitForViewViewAction(viewId: Int): ViewAction { uiController.loopMainThreadUntilIdle() val startTime = System.currentTimeMillis() val endTime = startTime + timeOut - val viewMatcher = withId(viewId) do { // Iterate through all views on the screen and see if the view we are looking for is there already @@ -103,7 +102,7 @@ private fun waitForViewViewAction(viewId: Int): ViewAction { // Loops the main thread for a specified period of time. // Control may not return immediately, instead it'll return after the provided delay has passed and the queue is in an idle state again. uiController.loopMainThreadForAtLeast(100) - } while (System.currentTimeMillis() < endTime) // in case of a timeout we throw an exception -> test fails + } while (System.currentTimeMillis() < endTime) // in case of a timeout we throw an exception - test fails throw PerformException.Builder() .withCause(TimeoutException()) .withActionDescription(this.description)