mobile/android/base/tests/testSearchSuggestions.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/tests/testSearchSuggestions.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,112 @@
     1.4 +package org.mozilla.gecko.tests;
     1.5 +
     1.6 +import java.util.ArrayList;
     1.7 +import java.util.HashMap;
     1.8 +
     1.9 +import org.mozilla.gecko.Actions;
    1.10 +import org.mozilla.gecko.R;
    1.11 +import org.mozilla.gecko.home.BrowserSearch;
    1.12 +import org.mozilla.gecko.home.SuggestClient;
    1.13 +
    1.14 +import android.app.Activity;
    1.15 +import android.support.v4.app.Fragment;
    1.16 +import android.view.View;
    1.17 +import android.view.ViewGroup;
    1.18 +import android.widget.TextView;
    1.19 +
    1.20 +/**
    1.21 + * Test for search suggestions.
    1.22 + * Sends queries from AwesomeBar input and verifies that suggestions match
    1.23 + * expected values.
    1.24 + */
    1.25 +public class testSearchSuggestions extends BaseTest {
    1.26 +    private static final int SUGGESTION_MAX = 3;
    1.27 +    private static final int SUGGESTION_TIMEOUT = 5000;
    1.28 +    private static final String TEST_QUERY = "foo barz";
    1.29 +    private static final String SUGGESTION_TEMPLATE = "/robocop/robocop_suggestions.sjs?query=__searchTerms__";
    1.30 +
    1.31 +    public void testSearchSuggestions() {
    1.32 +        blockForGeckoReady();
    1.33 +
    1.34 +        // Map of expected values. See robocop_suggestions.sjs.
    1.35 +        final HashMap<String, ArrayList<String>> suggestMap = new HashMap<String, ArrayList<String>>();
    1.36 +        buildSuggestMap(suggestMap);
    1.37 +
    1.38 +        focusUrlBar();
    1.39 +
    1.40 +        for (int i = 0; i < TEST_QUERY.length(); i++) {
    1.41 +            Actions.EventExpecter enginesEventExpecter = null;
    1.42 +
    1.43 +            if (i == 0) {
    1.44 +                enginesEventExpecter = mActions.expectGeckoEvent("SearchEngines:Data");
    1.45 +            }
    1.46 +
    1.47 +            mActions.sendKeys(TEST_QUERY.substring(i, i+1));
    1.48 +
    1.49 +            // The BrowserSearch UI only shows up once a non-empty
    1.50 +            // search term is entered
    1.51 +            if (enginesEventExpecter != null) {
    1.52 +                connectSuggestClient(getActivity());
    1.53 +                enginesEventExpecter.blockForEvent();
    1.54 +                enginesEventExpecter.unregisterListener();
    1.55 +                enginesEventExpecter = null;
    1.56 +            }
    1.57 +
    1.58 +            final String query = TEST_QUERY.substring(0, i+1);
    1.59 +            boolean success = waitForTest(new BooleanTest() {
    1.60 +                @Override
    1.61 +                public boolean test() {
    1.62 +                    // get the first suggestion row
    1.63 +                    ViewGroup suggestionGroup = (ViewGroup) getActivity().findViewById(R.id.suggestion_layout);
    1.64 +                    if (suggestionGroup == null)
    1.65 +                        return false;
    1.66 +
    1.67 +                    ArrayList<String> expected = suggestMap.get(query);
    1.68 +                    for (int i = 0; i < expected.size(); i++) {
    1.69 +                        View queryChild = suggestionGroup.getChildAt(i);
    1.70 +                        if (queryChild == null || queryChild.getVisibility() == View.GONE)
    1.71 +                            return false;
    1.72 +
    1.73 +                        String suggestion = ((TextView) queryChild.findViewById(R.id.suggestion_text)).getText().toString();
    1.74 +                        if (!suggestion.equals(expected.get(i)))
    1.75 +                            return false;
    1.76 +                    }
    1.77 +
    1.78 +                    return true;
    1.79 +                }
    1.80 +            }, SUGGESTION_TIMEOUT);
    1.81 +
    1.82 +            mAsserter.is(success, true, "Results for query '" + query + "' matched expected suggestions");
    1.83 +        }
    1.84 +    }
    1.85 +
    1.86 +    private void buildSuggestMap(HashMap<String, ArrayList<String>> suggestMap) {
    1.87 +        // these values assume SUGGESTION_MAX = 3
    1.88 +        suggestMap.put("f",        new ArrayList<String>() {{ add("f"); add("facebook"); add("fandango"); add("frys"); }});
    1.89 +        suggestMap.put("fo",       new ArrayList<String>() {{ add("fo"); add("forever 21"); add("food network"); add("fox news"); }});
    1.90 +        suggestMap.put("foo",      new ArrayList<String>() {{ add("foo"); add("food network"); add("foothill college"); add("foot locker"); }});
    1.91 +        suggestMap.put("foo ",     new ArrayList<String>() {{ add("foo "); add("foo fighters"); add("foo bar"); add("foo bat"); }});
    1.92 +        suggestMap.put("foo b",    new ArrayList<String>() {{ add("foo b"); add("foo bar"); add("foo bat"); add("foo bay"); }});
    1.93 +        suggestMap.put("foo ba",   new ArrayList<String>() {{ add("foo ba"); add("foo bar"); add("foo bat"); add("foo bay"); }});
    1.94 +        suggestMap.put("foo bar",  new ArrayList<String>() {{ add("foo bar"); }});
    1.95 +        suggestMap.put("foo barz", new ArrayList<String>() {{ add("foo barz"); }});
    1.96 +    }
    1.97 +
    1.98 +    private void connectSuggestClient(final Activity activity) {
    1.99 +        waitForTest(new BooleanTest() {
   1.100 +            @Override
   1.101 +            public boolean test() {
   1.102 +                final Fragment browserSearch = getBrowserSearch();
   1.103 +                return (browserSearch != null);
   1.104 +            }
   1.105 +        }, SUGGESTION_TIMEOUT);
   1.106 +
   1.107 +        final BrowserSearch browserSearch = (BrowserSearch) getBrowserSearch();
   1.108 +
   1.109 +        final String suggestTemplate = getAbsoluteRawUrl(SUGGESTION_TEMPLATE);
   1.110 +        final SuggestClient client = new SuggestClient(activity, suggestTemplate,
   1.111 +                SUGGESTION_TIMEOUT);
   1.112 +        browserSearch.setSuggestClient(client);
   1.113 +    }
   1.114 +}
   1.115 +

mercurial