michael@0: package org.mozilla.gecko.tests; michael@0: michael@0: import java.util.ArrayList; michael@0: import java.util.HashMap; michael@0: michael@0: import org.mozilla.gecko.Actions; michael@0: import org.mozilla.gecko.R; michael@0: import org.mozilla.gecko.home.BrowserSearch; michael@0: import org.mozilla.gecko.home.SuggestClient; michael@0: michael@0: import android.app.Activity; michael@0: import android.support.v4.app.Fragment; michael@0: import android.view.View; michael@0: import android.view.ViewGroup; michael@0: import android.widget.TextView; michael@0: michael@0: /** michael@0: * Test for search suggestions. michael@0: * Sends queries from AwesomeBar input and verifies that suggestions match michael@0: * expected values. michael@0: */ michael@0: public class testSearchSuggestions extends BaseTest { michael@0: private static final int SUGGESTION_MAX = 3; michael@0: private static final int SUGGESTION_TIMEOUT = 5000; michael@0: private static final String TEST_QUERY = "foo barz"; michael@0: private static final String SUGGESTION_TEMPLATE = "/robocop/robocop_suggestions.sjs?query=__searchTerms__"; michael@0: michael@0: public void testSearchSuggestions() { michael@0: blockForGeckoReady(); michael@0: michael@0: // Map of expected values. See robocop_suggestions.sjs. michael@0: final HashMap> suggestMap = new HashMap>(); michael@0: buildSuggestMap(suggestMap); michael@0: michael@0: focusUrlBar(); michael@0: michael@0: for (int i = 0; i < TEST_QUERY.length(); i++) { michael@0: Actions.EventExpecter enginesEventExpecter = null; michael@0: michael@0: if (i == 0) { michael@0: enginesEventExpecter = mActions.expectGeckoEvent("SearchEngines:Data"); michael@0: } michael@0: michael@0: mActions.sendKeys(TEST_QUERY.substring(i, i+1)); michael@0: michael@0: // The BrowserSearch UI only shows up once a non-empty michael@0: // search term is entered michael@0: if (enginesEventExpecter != null) { michael@0: connectSuggestClient(getActivity()); michael@0: enginesEventExpecter.blockForEvent(); michael@0: enginesEventExpecter.unregisterListener(); michael@0: enginesEventExpecter = null; michael@0: } michael@0: michael@0: final String query = TEST_QUERY.substring(0, i+1); michael@0: boolean success = waitForTest(new BooleanTest() { michael@0: @Override michael@0: public boolean test() { michael@0: // get the first suggestion row michael@0: ViewGroup suggestionGroup = (ViewGroup) getActivity().findViewById(R.id.suggestion_layout); michael@0: if (suggestionGroup == null) michael@0: return false; michael@0: michael@0: ArrayList expected = suggestMap.get(query); michael@0: for (int i = 0; i < expected.size(); i++) { michael@0: View queryChild = suggestionGroup.getChildAt(i); michael@0: if (queryChild == null || queryChild.getVisibility() == View.GONE) michael@0: return false; michael@0: michael@0: String suggestion = ((TextView) queryChild.findViewById(R.id.suggestion_text)).getText().toString(); michael@0: if (!suggestion.equals(expected.get(i))) michael@0: return false; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: }, SUGGESTION_TIMEOUT); michael@0: michael@0: mAsserter.is(success, true, "Results for query '" + query + "' matched expected suggestions"); michael@0: } michael@0: } michael@0: michael@0: private void buildSuggestMap(HashMap> suggestMap) { michael@0: // these values assume SUGGESTION_MAX = 3 michael@0: suggestMap.put("f", new ArrayList() {{ add("f"); add("facebook"); add("fandango"); add("frys"); }}); michael@0: suggestMap.put("fo", new ArrayList() {{ add("fo"); add("forever 21"); add("food network"); add("fox news"); }}); michael@0: suggestMap.put("foo", new ArrayList() {{ add("foo"); add("food network"); add("foothill college"); add("foot locker"); }}); michael@0: suggestMap.put("foo ", new ArrayList() {{ add("foo "); add("foo fighters"); add("foo bar"); add("foo bat"); }}); michael@0: suggestMap.put("foo b", new ArrayList() {{ add("foo b"); add("foo bar"); add("foo bat"); add("foo bay"); }}); michael@0: suggestMap.put("foo ba", new ArrayList() {{ add("foo ba"); add("foo bar"); add("foo bat"); add("foo bay"); }}); michael@0: suggestMap.put("foo bar", new ArrayList() {{ add("foo bar"); }}); michael@0: suggestMap.put("foo barz", new ArrayList() {{ add("foo barz"); }}); michael@0: } michael@0: michael@0: private void connectSuggestClient(final Activity activity) { michael@0: waitForTest(new BooleanTest() { michael@0: @Override michael@0: public boolean test() { michael@0: final Fragment browserSearch = getBrowserSearch(); michael@0: return (browserSearch != null); michael@0: } michael@0: }, SUGGESTION_TIMEOUT); michael@0: michael@0: final BrowserSearch browserSearch = (BrowserSearch) getBrowserSearch(); michael@0: michael@0: final String suggestTemplate = getAbsoluteRawUrl(SUGGESTION_TEMPLATE); michael@0: final SuggestClient client = new SuggestClient(activity, suggestTemplate, michael@0: SUGGESTION_TIMEOUT); michael@0: browserSearch.setSuggestClient(client); michael@0: } michael@0: } michael@0: