Created
September 11, 2017 19:28
-
-
Save truedem/0da31f7481a16de075665581bc93275e to your computer and use it in GitHub Desktop.
Espresso IdlingResource for RxJava tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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")))); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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