Skip to content

Commit

Permalink
Wait for recyclerView to appear
Browse files Browse the repository at this point in the history
This is needed for NotEventsTest.agenda_MultipleWithTimes(), among
others.
  • Loading branch information
amberin committed Apr 8, 2024
1 parent 749de74 commit 71bb66d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,6 @@ class NoteEventsTest : OrgzlyTest() {

onNotesInAgenda().check(matches(recyclerViewItemCount(10)))

SystemClock.sleep(2000)

// Today: deadline
onItemInAgenda(1, R.id.item_head_scheduled_text).check(matches(not(isDisplayed())))
onItemInAgenda(1, R.id.item_head_deadline_text).check(matches(isDisplayed()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ 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));
Expand Down Expand Up @@ -454,4 +455,50 @@ 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<View> 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<View> 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();
}
};
}
}

0 comments on commit 71bb66d

Please sign in to comment.