Skip to content

Instantly share code, notes, and snippets.

@truedem
Created September 11, 2017 19:28
Show Gist options
  • Save truedem/0da31f7481a16de075665581bc93275e to your computer and use it in GitHub Desktop.
Save truedem/0da31f7481a16de075665581bc93275e to your computer and use it in GitHub Desktop.
Espresso IdlingResource for RxJava tests
@After
public void release() {
Espresso.unregisterIdlingResources();
}
@Test
public void buttonShouldUpdateText() {
// may put following 3 lines into @BeforeEach annotaded method as well
rxEspressoScheduleHandler = new RxEspressoScheduleHandler();
RxJavaPlugins.setScheduleHandler(rxEspressoScheduleHandler);
Espresso.registerIdlingResources(rxEspressoScheduleHandler.getIdlingResource());
onView(withId(R.id.button)).perform(click());
// this action happens after delay - for example after network call through RxJava Observable
onView(withId(R.id.resultView)).check(matches(withText(containsString("Result"))));
}
import android.support.test.espresso.IdlingResource;
import android.support.test.espresso.idling.CountingIdlingResource;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;
public class RxEspressoScheduleHandler implements Function<Runnable, Runnable> {
private final CountingIdlingResource countingIdlingResource = new CountingIdlingResource("rxJava");
@Override
public Runnable apply(@NonNull final Runnable runnable) throws Exception {
return new Runnable() {
@Override
public void run() {
countingIdlingResource.increment();
try {
runnable.run();
} finally {
countingIdlingResource.decrement();
}
}
};
}
public IdlingResource getIdlingResource() {
return countingIdlingResource;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment