* Note that in order to scroll the list you shouldn't use {@link ViewActions#scrollTo()} as * {@link Espresso#onData(org.hamcrest.Matcher)} handles scrolling.
* Rules are interceptors which are executed for each test method and will run before * any of your setup code in the {@link Before @Before} method. *
* {@link ActivityTestRule} will create and launch of the activity for you and also expose * the activity under test. To get a reference to the activity you can use * the {@link ActivityTestRule#getActivity()} method. */ @Rule public ActivityTestRule mActivityRule = new ActivityTestRule<>( LongListActivity.class); /** * Test that the list is long enough for this sample, the last item shouldn't appear. */ @Test public void lastItem_NotDisplayed() { // Last item should not exist if the list wasn't scrolled down. onView(withText(LAST_ITEM_ID)).check(doesNotExist()); } /** * Check that the item is created. onData() takes care of scrolling. */ @Test public void list_Scrolls() { onRow(LAST_ITEM_ID).check(matches(isCompletelyDisplayed())); } /** * Clicks on a row and checks that the activity detected the click. */ @Test public void row_Click() { // Click on one of the rows. onRow(TEXT_ITEM_30).onChildView(withId(R.id.rowContentTextView)).perform(click()); // Check that the activity detected the click on the first column. onView(ViewMatchers.withId(R.id.selection_row_value)) .check(matches(withText(TEXT_ITEM_30_SELECTED))); } /** * Checks that a toggle button is checked after clicking on it. */ @Test public void toggle_Click() { // Click on a toggle button. onRow(TEXT_ITEM_30).onChildView(withId(R.id.rowToggleButton)).perform(click()); // Check that the toggle button is checked. onRow(TEXT_ITEM_30).onChildView(withId(R.id.rowToggleButton)).check(matches(isChecked())); } /** * Make sure that clicking on the toggle button doesn't trigger a click on the row. */ @Test public void toggle_ClickDoesntPropagate() { // Click on one of the rows. onRow(TEXT_ITEM_30).onChildView(withId(R.id.rowContentTextView)).perform(click()); // Click on the toggle button, in a different row. onRow(TEXT_ITEM_60).onChildView(withId(R.id.rowToggleButton)).perform(click()); // Check that the activity didn't detect the click on the first column. onView(ViewMatchers.withId(R.id.selection_row_value)) .check(matches(withText(TEXT_ITEM_30_SELECTED))); } /** * Uses {@link Espresso#onData(org.hamcrest.Matcher)} to get a reference to a specific row. * * Note: A custom matcher can be used to match the content and have more readable code. * See the Custom Matcher Sample. * * * @param str the content of the field * @return a {@link DataInteraction} referencing the row */ private static DataInteraction onRow(String str) { return onData(hasEntry(equalTo(LongListActivity.ROW_TEXT), is(str))); } }
* Note: A custom matcher can be used to match the content and have more readable code. * See the Custom Matcher Sample. *