I have an activity that hosts a fragment F1. Upon a button click, F1 is replaced by another fragment F2. When the back button is pressed, the app returns from F2 to F1 via an exit transition animation.
My Espresso test case looks roughly like the following:
@Test
public void pressingBackRestorePreviousFragment() {
// we are in F1 and about to switch to F2
onView(withId(R.id.the_button)).perform(click());
// we are now in F2, pressing back should "return" to F1
Espresso.pressBack();
onView(withText("A specific text in F1")).check(matches(isDisplayed());
}
When the test case runs in step-by-step debug mode, the above test passes. But in normal run mode, it fails. Only when I inserted Thread.sleep(___)
before onView(withText(__))
the test would pass.
I believe a better technique is to replace Thread.sleep() with Espresso IdlingResource
, but I am not sure how to incorporate it with the view animation thread. Ideally, I'd like to rewrite the above test case to
@Test
public void pressingBackRestorePreviousFragment() {
// we are in F1 and about to switch to F2
onView(withId(R.id.the_button)).perform(click());
// we are now in F2, pressing back should "return" to F1
Espresso.pressBack();
onView(isRoot()).perform(waitForAnimCompletion());
onView(withText("A specific text in F1")).check(matches(isDisplayed());
}