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