* 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<>( SuggestActivity.class); private SuggestActivity mActivity = null; @Before public void setActivity() { mActivity = mActivityRule.getActivity(); } @Test public void autoCompleteTextView_twoSuggestions() { // Type "So" to trigger two suggestions. onView(withId(R.id.auto_complete_text_view)) .perform(typeText("So"), closeSoftKeyboard()); // Check that both suggestions are displayed. onView(withText("South China Sea")) .inRoot(withDecorView(not(is(mActivity.getWindow().getDecorView())))) .check(matches(isDisplayed())); onView(withText("Southern Ocean")) .inRoot(withDecorView(not(is(mActivity.getWindow().getDecorView())))) .check(matches(isDisplayed())); } @Test public void autoCompleteTextView_oneSuggestion() { // Type "South" to trigger one suggestion. onView(withId(R.id.auto_complete_text_view)) .perform(typeTextIntoFocusedView("South "), closeSoftKeyboard()); // Should be displayed onView(withText("South China Sea")) .inRoot(withDecorView(not(is(mActivity.getWindow().getDecorView())))) .check(matches(isDisplayed())); // Should not be displayed. onView(withText("Southern Ocean")) .inRoot(withDecorView(not(is(mActivity.getWindow().getDecorView())))) .check(doesNotExist()); } @Test public void autoCompleteTextView_clickAndCheck() { // Type text into the text view onView(withId(R.id.auto_complete_text_view)) .perform(typeTextIntoFocusedView("South "), closeSoftKeyboard()); // Tap on a suggestion. onView(withText("South China Sea")) .inRoot(withDecorView(not(is(mActivity.getWindow().getDecorView())))) .perform(click()); // By clicking on the auto complete term, the text should be filled in. onView(withId(R.id.auto_complete_text_view)) .check(matches(withText("South China Sea"))); } @Test public void autoCompleteTextView_onDataClickAndCheck() { // NB: The autocompletion box is implemented with a ListView, so the preferred way // to interact with it is onData(). We can use inRoot here too! onView(withId(R.id.auto_complete_text_view)) .perform(typeText("S"), closeSoftKeyboard()); // This is useful because some of the completions may not be part of the View Hierarchy // unless you scroll around the list. onData(allOf(instanceOf(String.class), is("Baltic Sea"))) .inRoot(withDecorView(not(is(mActivity.getWindow().getDecorView())))) .perform(click()); // The text should be filled in. onView(withId(R.id.auto_complete_text_view)) .check(matches(withText("Baltic Sea"))); } }