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());
}
Espresso will wait for the main thread to become idle before performing operations. Fragment
and Activity
transitions happen on the main thread, so you do not need to implement any IdlingResource
s. What's happening here is that the transition animation is causing flakiness with Espresso's synchronization of the main thread and testing thread. As mentioned in the Espresso setup instructions, you should disable system animations on the device used for testing.
In general, Thread.sleep
statements are bad practice for testing, and will result in unstable and slow tests. Espresso was designed precisely to address this problem on Android. Checkout Valera Zakharov's explanation of the problem when introducing Espresso at GTAC 2013.