Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | package org.mozilla.gecko.tests; |
michael@0 | 2 | |
michael@0 | 3 | import java.util.ArrayList; |
michael@0 | 4 | import java.util.HashMap; |
michael@0 | 5 | |
michael@0 | 6 | import org.mozilla.gecko.Actions; |
michael@0 | 7 | import org.mozilla.gecko.R; |
michael@0 | 8 | import org.mozilla.gecko.home.BrowserSearch; |
michael@0 | 9 | import org.mozilla.gecko.home.SuggestClient; |
michael@0 | 10 | |
michael@0 | 11 | import android.app.Activity; |
michael@0 | 12 | import android.support.v4.app.Fragment; |
michael@0 | 13 | import android.view.View; |
michael@0 | 14 | import android.view.ViewGroup; |
michael@0 | 15 | import android.widget.TextView; |
michael@0 | 16 | |
michael@0 | 17 | /** |
michael@0 | 18 | * Test for search suggestions. |
michael@0 | 19 | * Sends queries from AwesomeBar input and verifies that suggestions match |
michael@0 | 20 | * expected values. |
michael@0 | 21 | */ |
michael@0 | 22 | public class testSearchSuggestions extends BaseTest { |
michael@0 | 23 | private static final int SUGGESTION_MAX = 3; |
michael@0 | 24 | private static final int SUGGESTION_TIMEOUT = 5000; |
michael@0 | 25 | private static final String TEST_QUERY = "foo barz"; |
michael@0 | 26 | private static final String SUGGESTION_TEMPLATE = "/robocop/robocop_suggestions.sjs?query=__searchTerms__"; |
michael@0 | 27 | |
michael@0 | 28 | public void testSearchSuggestions() { |
michael@0 | 29 | blockForGeckoReady(); |
michael@0 | 30 | |
michael@0 | 31 | // Map of expected values. See robocop_suggestions.sjs. |
michael@0 | 32 | final HashMap<String, ArrayList<String>> suggestMap = new HashMap<String, ArrayList<String>>(); |
michael@0 | 33 | buildSuggestMap(suggestMap); |
michael@0 | 34 | |
michael@0 | 35 | focusUrlBar(); |
michael@0 | 36 | |
michael@0 | 37 | for (int i = 0; i < TEST_QUERY.length(); i++) { |
michael@0 | 38 | Actions.EventExpecter enginesEventExpecter = null; |
michael@0 | 39 | |
michael@0 | 40 | if (i == 0) { |
michael@0 | 41 | enginesEventExpecter = mActions.expectGeckoEvent("SearchEngines:Data"); |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | mActions.sendKeys(TEST_QUERY.substring(i, i+1)); |
michael@0 | 45 | |
michael@0 | 46 | // The BrowserSearch UI only shows up once a non-empty |
michael@0 | 47 | // search term is entered |
michael@0 | 48 | if (enginesEventExpecter != null) { |
michael@0 | 49 | connectSuggestClient(getActivity()); |
michael@0 | 50 | enginesEventExpecter.blockForEvent(); |
michael@0 | 51 | enginesEventExpecter.unregisterListener(); |
michael@0 | 52 | enginesEventExpecter = null; |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | final String query = TEST_QUERY.substring(0, i+1); |
michael@0 | 56 | boolean success = waitForTest(new BooleanTest() { |
michael@0 | 57 | @Override |
michael@0 | 58 | public boolean test() { |
michael@0 | 59 | // get the first suggestion row |
michael@0 | 60 | ViewGroup suggestionGroup = (ViewGroup) getActivity().findViewById(R.id.suggestion_layout); |
michael@0 | 61 | if (suggestionGroup == null) |
michael@0 | 62 | return false; |
michael@0 | 63 | |
michael@0 | 64 | ArrayList<String> expected = suggestMap.get(query); |
michael@0 | 65 | for (int i = 0; i < expected.size(); i++) { |
michael@0 | 66 | View queryChild = suggestionGroup.getChildAt(i); |
michael@0 | 67 | if (queryChild == null || queryChild.getVisibility() == View.GONE) |
michael@0 | 68 | return false; |
michael@0 | 69 | |
michael@0 | 70 | String suggestion = ((TextView) queryChild.findViewById(R.id.suggestion_text)).getText().toString(); |
michael@0 | 71 | if (!suggestion.equals(expected.get(i))) |
michael@0 | 72 | return false; |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | return true; |
michael@0 | 76 | } |
michael@0 | 77 | }, SUGGESTION_TIMEOUT); |
michael@0 | 78 | |
michael@0 | 79 | mAsserter.is(success, true, "Results for query '" + query + "' matched expected suggestions"); |
michael@0 | 80 | } |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | private void buildSuggestMap(HashMap<String, ArrayList<String>> suggestMap) { |
michael@0 | 84 | // these values assume SUGGESTION_MAX = 3 |
michael@0 | 85 | suggestMap.put("f", new ArrayList<String>() {{ add("f"); add("facebook"); add("fandango"); add("frys"); }}); |
michael@0 | 86 | suggestMap.put("fo", new ArrayList<String>() {{ add("fo"); add("forever 21"); add("food network"); add("fox news"); }}); |
michael@0 | 87 | suggestMap.put("foo", new ArrayList<String>() {{ add("foo"); add("food network"); add("foothill college"); add("foot locker"); }}); |
michael@0 | 88 | suggestMap.put("foo ", new ArrayList<String>() {{ add("foo "); add("foo fighters"); add("foo bar"); add("foo bat"); }}); |
michael@0 | 89 | suggestMap.put("foo b", new ArrayList<String>() {{ add("foo b"); add("foo bar"); add("foo bat"); add("foo bay"); }}); |
michael@0 | 90 | suggestMap.put("foo ba", new ArrayList<String>() {{ add("foo ba"); add("foo bar"); add("foo bat"); add("foo bay"); }}); |
michael@0 | 91 | suggestMap.put("foo bar", new ArrayList<String>() {{ add("foo bar"); }}); |
michael@0 | 92 | suggestMap.put("foo barz", new ArrayList<String>() {{ add("foo barz"); }}); |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | private void connectSuggestClient(final Activity activity) { |
michael@0 | 96 | waitForTest(new BooleanTest() { |
michael@0 | 97 | @Override |
michael@0 | 98 | public boolean test() { |
michael@0 | 99 | final Fragment browserSearch = getBrowserSearch(); |
michael@0 | 100 | return (browserSearch != null); |
michael@0 | 101 | } |
michael@0 | 102 | }, SUGGESTION_TIMEOUT); |
michael@0 | 103 | |
michael@0 | 104 | final BrowserSearch browserSearch = (BrowserSearch) getBrowserSearch(); |
michael@0 | 105 | |
michael@0 | 106 | final String suggestTemplate = getAbsoluteRawUrl(SUGGESTION_TEMPLATE); |
michael@0 | 107 | final SuggestClient client = new SuggestClient(activity, suggestTemplate, |
michael@0 | 108 | SUGGESTION_TIMEOUT); |
michael@0 | 109 | browserSearch.setSuggestClient(client); |
michael@0 | 110 | } |
michael@0 | 111 | } |
michael@0 | 112 |