This library includes components which facilitate testing Views in isolation and running Espresso tests with Google TalkBack enabled.
- it allows you to inflate layouts/instantiate Views in their own Activity
- adds automatic toggling of TalkBack before/after each test to assert custom behavior
The library is split into two artifacts, available on JCenter.
coreincludes most of this library's classes and functions. It should be included as anandroidTestCompiledependency, since you'll only use it for instrumentation tests.extrasinclude two Activity components that are used bycore. It's not enough to useandroidTestCompilefor this because that will include it as part of the test APK that instruments your app; they must be part of the app under test. UsedebugCompileor<yourEspressoFlavor>Compileto include this dependency.
androidTestCompile 'com.novoda:espresso-support:<latest-version>'
debugCompile 'com.novoda:espresso-support-extras:<latest-version>'
See demo/build.gradle for examples.
Use the ViewTestRule, passing in a layout resource. It'll inflate the resource into the ViewActivity with MATCH_PARENT for both dimensions. You can use rule.getView() to obtain an instance of the View and it'll be typed to the class you specified.
@Rule
public ViewTestRule<MovieItemView> rule = new ViewTestRule<>(R.layout.test_movie_item_view);You can write BDD style tests here, highlighting the expected behavior for your custom views, using a mixture of Espresso ViewActions and Mockito verifies:
@Test
public void givenViewIsUpdatedWithDifferentMovie_whenClicking_thenListenerDoesNotGetFiredForOriginalMovie() {
givenMovieItemViewIsBoundTo(EDWARD_SCISSORHANDS);
givenMovieItemViewIsBoundTo(NOT_EDWARD_SCISSORHANDS);
onView(withId(R.id.movie_item_button_play)).perform(click());
verify(movieItemListener, never()).onClickPlay(eq(EDWARD_SCISSORHANDS));
}
private void givenMovieItemViewIsBoundTo(final Movie movie) {
viewTestRule.runOnUiThread(new ViewTestRule.Runner<MovieItemView>() {
@Override
public void run(MovieItemView view) {
view.bind(movie);
}
});
}Often, our apps will behave differently when TalkBack is enabled to offer a more streamlined experience for users of screen readers.
Use either TalkBackTestRule wrapped around a ViewTestRule/ActivityTestRule to start/stop TalkBack before and after each test.
private ViewTestRule<MovieItemView> viewTestRule = new ViewTestRule<>(R.layout.test_movie_item_view);
@Rule
public RuleChain chain = RuleChain.outerRule(new TalkBackTestRule()).around(viewTestRule);TalkBack will be enabled before each test is run and disabled after each test finishes.
WRITE_SECURE_SETTINGS permission being set for the app under test.
You can run the demo tests with the following command:
./gradlew demo:installDebug;\
adb shell pm grant com.novoda.movies android.permission.WRITE_SECURE_SETTINGS;\
adb shell am start -a com.novoda.espresso.DISABLE_ALL_SERVICES;\
./gradlew demo:cAT;- First the app is installed
- The
WRITE_SECURE_SETTINGSpermission is set for the app (com.novoda.movies- replace this with your app's package name) - Disable all accessibility services initially
- Run all the connected Android tests
Here are a list of useful links:
- We always welcome people to contribute new features or bug fixes, here is how
- If you have a problem check the Issues Page first to see if we are working on it
