Skip to content

Commit

Permalink
Apply a retry rule in NoteFragmentTest
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
amberin committed Apr 8, 2024
1 parent f9252d3 commit eb96f43
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<MainActivity>

@Rule
@JvmField
val mRetryTestRule = RetryTestRule()

@Before
@Throws(Exception::class)
override fun setUp() {
Expand Down Expand Up @@ -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"))))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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<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 eb96f43

Please sign in to comment.