From eb96f43f2ebd84574ed066f32b8a47771b47da10 Mon Sep 17 00:00:00 2001 From: Victor Andreasson Date: Tue, 9 Apr 2024 00:41:10 +0200 Subject: [PATCH] Apply a retry rule in NoteFragmentTest Because testContentLineCountUpdatedOnNoteUpdate() is just... too damn flaky, and I cannot understand where the problem is. It just seems that, sometimes, onRecyclerViewItem() will not work, and waiting for the right ID doesn't help. --- .../android/espresso/NoteFragmentTest.kt | 7 +++ .../android/espresso/util/EspressoUtils.java | 49 +------------------ 2 files changed, 9 insertions(+), 47 deletions(-) diff --git a/app/src/androidTest/java/com/orgzly/android/espresso/NoteFragmentTest.kt b/app/src/androidTest/java/com/orgzly/android/espresso/NoteFragmentTest.kt index 2fc127da2..8a0d73b20 100644 --- a/app/src/androidTest/java/com/orgzly/android/espresso/NoteFragmentTest.kt +++ b/app/src/androidTest/java/com/orgzly/android/espresso/NoteFragmentTest.kt @@ -15,16 +15,22 @@ import androidx.test.espresso.matcher.RootMatchers.isDialog import androidx.test.espresso.matcher.ViewMatchers.* import com.orgzly.R import com.orgzly.android.OrgzlyTest +import com.orgzly.android.RetryTestRule import com.orgzly.android.espresso.util.EspressoUtils.* import com.orgzly.android.ui.main.MainActivity import org.hamcrest.Matchers.* import org.junit.After import org.junit.Before +import org.junit.Rule import org.junit.Test class NoteFragmentTest : OrgzlyTest() { private lateinit var scenario: ActivityScenario + @Rule + @JvmField + val mRetryTestRule = RetryTestRule() + @Before @Throws(Exception::class) override fun setUp() { @@ -492,6 +498,7 @@ class NoteFragmentTest : OrgzlyTest() { onView(withId(R.id.content)).perform(click()) onView(withId(R.id.content_edit)).perform(typeTextIntoFocusedView("a\nb\nc")) onView(withId(R.id.done)).perform(click()) // Note done + SystemClock.sleep(500) onNoteInBook(1, R.id.item_head_fold_button).perform(click()) onNoteInBook(1, R.id.item_head_title_view).check(matches(withText(endsWith("3")))) } diff --git a/app/src/androidTest/java/com/orgzly/android/espresso/util/EspressoUtils.java b/app/src/androidTest/java/com/orgzly/android/espresso/util/EspressoUtils.java index 66e0b46db..51c4909a7 100644 --- a/app/src/androidTest/java/com/orgzly/android/espresso/util/EspressoUtils.java +++ b/app/src/androidTest/java/com/orgzly/android/espresso/util/EspressoUtils.java @@ -64,6 +64,8 @@ * - replaceText() is preferred over typeText() as it is much faster. */ public class EspressoUtils { + + public static ViewInteraction onListView() { return onView(allOf(isAssignableFrom(ListView.class), isDisplayed())); } @@ -185,7 +187,6 @@ public static ViewInteraction onSavedSearch(int position) { } public static ViewInteraction onRecyclerViewItem(@IdRes int recyclerView, int position, @IdRes int childView) { - onView(isRoot()).perform(waitId(recyclerView, 5000)); onView(withId(recyclerView)).perform(RecyclerViewActions.scrollToPosition(position)); return onView(new EspressoRecyclerViewMatcher(recyclerView) .atPositionOnView(position, childView)); @@ -453,50 +454,4 @@ public void perform(UiController uiController, View view) { public static ViewAction scroll() { return new NestedScrollViewExtension(); } - - /** - * Perform action of waiting for a specific view id. Copied from https://stackoverflow.com/a/49814995. - * @param viewId The id of the view to wait for. - * @param millis The timeout of until when to wait for. - */ - public static ViewAction waitId(final int viewId, final long millis) { - return new ViewAction() { - @Override - public Matcher getConstraints() { - return isRoot(); - } - - @Override - public String getDescription() { - return "wait for a specific view with id <" + viewId + "> during " + millis + " millis."; - } - - @Override - public void perform(final UiController uiController, final View view) { - uiController.loopMainThreadUntilIdle(); - final long startTime = System.currentTimeMillis(); - final long endTime = startTime + millis; - final Matcher viewMatcher = withId(viewId); - - do { - for (View child : TreeIterables.breadthFirstViewTraversal(view)) { - // found view with required ID - if (viewMatcher.matches(child)) { - return; - } - } - - uiController.loopMainThreadForAtLeast(50); - } - while (System.currentTimeMillis() < endTime); - - // timeout happens - throw new PerformException.Builder() - .withActionDescription(this.getDescription()) - .withViewDescription(HumanReadables.describe(view)) - .withCause(new TimeoutException()) - .build(); - } - }; - } } \ No newline at end of file