Search in sources :

Example 1 with UiCollection

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);
}
Also used : UiCollection(android.support.test.uiautomator.UiCollection) SearchEngine(org.mozilla.focus.search.SearchEngine) UiObject(android.support.test.uiautomator.UiObject) Random(java.util.Random) UiSelector(android.support.test.uiautomator.UiSelector) SessionLoadedIdlingResource(org.mozilla.focus.helper.SessionLoadedIdlingResource) Intent(android.content.Intent) InputMethodManager(android.view.inputmethod.InputMethodManager) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

Intent (android.content.Intent)1 UiCollection (android.support.test.uiautomator.UiCollection)1 UiObject (android.support.test.uiautomator.UiObject)1 UiSelector (android.support.test.uiautomator.UiSelector)1 InputMethodManager (android.view.inputmethod.InputMethodManager)1 Random (java.util.Random)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1 SessionLoadedIdlingResource (org.mozilla.focus.helper.SessionLoadedIdlingResource)1 SearchEngine (org.mozilla.focus.search.SearchEngine)1