use of android.support.test.uiautomator.UiCollection in project Rocket by mozilla-tw.
the class SearchSuggestionTest method clickSearchSuggestion_browseByDefaultSearchEngine.
@Test
@Ignore
public void clickSearchSuggestion_browseByDefaultSearchEngine() throws UiObjectNotFoundException, UnsupportedEncodingException {
activityTestRule.launchActivity(new Intent());
final SessionLoadedIdlingResource loadingIdlingResource = new SessionLoadedIdlingResource(activityTestRule.getActivity());
// Click search field
onView(withId(R.id.home_fragment_fake_input)).perform(click());
onView(withId(R.id.url_edit)).check(matches(isDisplayed()));
// Check if the soft keyboard is shown
final InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
Assert.assertTrue(inputMethodManager.isAcceptingText());
onView(withId(R.id.url_edit)).perform(typeText("rocket"));
onView(withId(R.id.search_suggestion)).check(matches(isDisplayed()));
// Check if the suggestion count is shown at most 5
final UiCollection suggestionCollection = new UiCollection(new UiSelector().resourceId(getResourceId("search_suggestion")));
Assert.assertTrue(suggestionCollection.exists());
final int suggestionCount = suggestionCollection.getChildCount(new UiSelector().resourceId(getResourceId("suggestion_item")));
Assert.assertTrue(suggestionCount <= 5);
// Pick a suggestion
final UiObject suggestionItem = suggestionCollection.getChild(new UiSelector().index(new Random().nextInt(suggestionCount)));
final String suggestionText = suggestionItem.getText();
suggestionItem.click();
// Get default search engine
final SearchEngine defaultSearchEngine = SearchEngineManager.getInstance().getDefaultSearchEngine(context);
// After page loading completes
IdlingRegistry.getInstance().register(loadingIdlingResource);
onView(withId(R.id.display_url)).check(matches(isDisplayed()));
// Check if the search result is using default search engine
final String[] searchEngine = defaultSearchEngine.getName().toLowerCase().split(" ");
onView(withId(R.id.display_url)).check(matches(withText(containsString(searchEngine[0]))));
// Check if the search result is matched the suggestion we picked
onView(withId(R.id.display_url)).check(matches(DecodedTextMatcher.withText(containsString(searchEngine[0]))));
IdlingRegistry.getInstance().unregister(loadingIdlingResource);
}
Aggregations