Bug 896560 - Update testSearchSuggestions test for new about:home (r=margaret)
authorLucas Rocha <lucasr@mozilla.com>
Wed, 14 Aug 2013 22:53:34 +0100
changeset 151514 d9ff3e3468ee56808fc82626adcd8d3f993fb3be
parent 151513 1a6e28e051e0e3fb9a508e02de18922afd4e364c
child 151515 be075a38db6eef8b77680115cbe8c8ca52edea4f
push idunknown
push userunknown
push dateunknown
reviewersmargaret
bugs896560
milestone26.0a1
Bug 896560 - Update testSearchSuggestions test for new about:home (r=margaret)
mobile/android/base/tests/BaseTest.java.in
mobile/android/base/tests/robocop.ini
mobile/android/base/tests/testSearchSuggestions.java.in
--- a/mobile/android/base/tests/BaseTest.java.in
+++ b/mobile/android/base/tests/BaseTest.java.in
@@ -11,16 +11,19 @@ import android.content.ContentValues;
 import android.content.ContentUris;
 import android.content.Intent;
 import android.content.pm.ActivityInfo;
 import android.content.res.AssetManager;
 import android.database.Cursor;
 import android.net.Uri;
 import android.os.Build;
 import android.os.SystemClock;
+import android.support.v4.app.Fragment;
+import android.support.v4.app.FragmentActivity;
+import android.support.v4.app.FragmentManager;
 import android.test.ActivityInstrumentationTestCase2;
 import android.util.DisplayMetrics;
 import android.view.inputmethod.InputMethodManager;
 import android.view.KeyEvent;
 import android.view.View;
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.ListAdapter;
@@ -217,16 +220,21 @@ abstract class BaseTest extends Activity
         // Send the keys for the URL we want to enter
         mActions.sendKeys(url);
 
         // Get the URL text from the URL bar EditText view
         String urlBarText = mDriver.findElement(mActivity, URL_EDIT_TEXT_ID).getText();
         mAsserter.is(url, urlBarText, "URL typed properly");
     }
 
+    protected final Fragment getBrowserSearch() {
+        final FragmentManager fm = ((FragmentActivity) getActivity()).getSupportFragmentManager();
+        return fm.findFragmentByTag("browser_search");
+    }
+
     protected final void hitEnterAndWait() {
         Actions.EventExpecter contentEventExpecter = mActions.expectGeckoEvent("DOMContentLoaded");
         mActions.sendSpecialKey(Actions.SpecialKey.ENTER);
         // wait for screen to load
         contentEventExpecter.blockForEvent();
         contentEventExpecter.unregisterListener();
     }
 
--- a/mobile/android/base/tests/robocop.ini
+++ b/mobile/android/base/tests/robocop.ini
@@ -13,17 +13,17 @@
 # [testOverscroll] # see bug 851861
 # [testAxisLocking] # see bug 851861
 [testAboutPage]
 # [testWebContentContextMenu] # see bug 862493
 [testPasswordProvider]
 # [testPasswordEncrypt] # see bug 824067
 [testFormHistory]
 [testBrowserProvider]
-# [testSearchSuggestions] # disabled on fig - bug 880060
+[testSearchSuggestions]
 [testSharedPreferences]
 # [testThumbnails] # see bug 813107
 [testAddonManager]
 # [testHistory] # disabled on fig - bug 880060
 [testVkbOverlap]
 [testDoorHanger]
 [testTabHistory]
 [testShareLink]
--- a/mobile/android/base/tests/testSearchSuggestions.java.in
+++ b/mobile/android/base/tests/testSearchSuggestions.java.in
@@ -1,14 +1,15 @@
 #filter substitution
 package @ANDROID_PACKAGE_NAME@.tests;
 
 import @ANDROID_PACKAGE_NAME@.*;
 import android.app.Activity;
 import android.content.Context;
+import android.support.v4.app.Fragment;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.TextView;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 import java.lang.RuntimeException;
 import java.util.ArrayList;
@@ -35,25 +36,36 @@ public class testSearchSuggestions exten
 
         // Map of expected values. See robocop_suggestions.sjs.
         final HashMap<String, ArrayList<String>> suggestMap = new HashMap<String, ArrayList<String>>();
         buildSuggestMap(suggestMap);
 
         final int suggestionLayoutId = mDriver.findElement(getActivity(), "suggestion_layout").getId();
         final int suggestionTextId = mDriver.findElement(getActivity(), "suggestion_text").getId();
 
-        Actions.EventExpecter enginesEventExpecter = mActions.expectGeckoEvent("SearchEngines:Data");
         focusUrlBar();
-        enginesEventExpecter.blockForEvent();
-        enginesEventExpecter.unregisterListener();
-        connectSuggestClient(getActivity());
 
         for (int i = 0; i < TEST_QUERY.length(); i++) {
+            Actions.EventExpecter enginesEventExpecter = null;
+
+            if (i == 0) {
+                enginesEventExpecter = mActions.expectGeckoEvent("SearchEngines:Data");
+            }
+
             mActions.sendKeys(TEST_QUERY.substring(i, i+1));
 
+            // The BrowserSearch UI only shows up once a non-empty
+            // search term is entered
+            if (enginesEventExpecter != null) {
+                connectSuggestClient(getActivity());
+                enginesEventExpecter.blockForEvent();
+                enginesEventExpecter.unregisterListener();
+                enginesEventExpecter = null;
+            }
+
             final String query = TEST_QUERY.substring(0, i+1);
             boolean success = waitForTest(new BooleanTest() {
                 @Override
                 public boolean test() {
                     // get the first suggestion row
                     ViewGroup suggestionGroup = (ViewGroup) getActivity().findViewById(suggestionLayoutId);
                     if (suggestionGroup == null)
                         return false;
@@ -67,16 +79,17 @@ public class testSearchSuggestions exten
                         String suggestion = ((TextView) queryChild.findViewById(suggestionTextId)).getText().toString();
                         if (!suggestion.equals(expected.get(i)))
                             return false;
                     }
 
                     return true;
                 }
             }, SUGGESTION_TIMEOUT);
+
             mAsserter.is(success, true, "Results for query '" + query + "' matched expected suggestions");
         }
     }
 
     private void buildSuggestMap(HashMap<String, ArrayList<String>> suggestMap) {
         // these values assume SUGGESTION_MAX = 3
         suggestMap.put("f",        new ArrayList<String>() {{ add("f"); add("facebook"); add("fandango"); add("frys"); }});
         suggestMap.put("fo",       new ArrayList<String>() {{ add("fo"); add("forever 21"); add("food network"); add("fox news"); }});
@@ -84,50 +97,43 @@ public class testSearchSuggestions exten
         suggestMap.put("foo ",     new ArrayList<String>() {{ add("foo "); add("foo fighters"); add("foo bar"); add("foo bat"); }});
         suggestMap.put("foo b",    new ArrayList<String>() {{ add("foo b"); add("foo bar"); add("foo bat"); add("foo bay"); }});
         suggestMap.put("foo ba",   new ArrayList<String>() {{ add("foo ba"); add("foo bar"); add("foo bat"); add("foo bay"); }});
         suggestMap.put("foo bar",  new ArrayList<String>() {{ add("foo bar"); }});
         suggestMap.put("foo barz", new ArrayList<String>() {{ add("foo barz"); }});
     }
 
     private void connectSuggestClient(final Activity activity) {
-        final int awesomeBarTabsId = mDriver.findElement(getActivity(), "awesomebar_tabs").getId();
-
         try {
             // create a SuggestClient that uses robocop_suggestions.sjs
             ClassLoader classLoader = getActivity().getApplicationContext().getClassLoader();
-            Class suggestClass = classLoader.loadClass("org.mozilla.gecko.SuggestClient");
+            Class suggestClass = classLoader.loadClass("org.mozilla.gecko.home.SuggestClient");
             Constructor suggestConstructor = suggestClass.getConstructor(
                     new Class[] { Context.class, String.class, int.class, int.class });
             String suggestTemplate = getAbsoluteRawUrl(SUGGESTION_TEMPLATE);
             Object client = suggestConstructor.newInstance(activity, suggestTemplate, SUGGESTION_TIMEOUT, SUGGESTION_MAX);
 
             // enable offline HTTP requests for testing
             final Field checkNetworkField = suggestClass.getDeclaredField("mCheckNetwork");
             checkNetworkField.setAccessible(true);
             checkNetworkField.setBoolean(client, false);
 
             // replace mSuggestClient with test client
-            final Class awesomeBarTabsClass = classLoader.loadClass("org.mozilla.gecko.AwesomeBarTabs");
-            final Method getAllPagesTabMethod = awesomeBarTabsClass.getMethod("getAllPagesTab");
-            final View awesomeBarTabs = (View) activity.findViewById(awesomeBarTabsId);
-            final Class allPagesTabClass = classLoader.loadClass("org.mozilla.gecko.AllPagesTab");
-            final Field suggestClientField = allPagesTabClass.getDeclaredField("mSuggestClient");
-            final Object allPagesTab = getAllPagesTabMethod.invoke(awesomeBarTabs);
+            final Class browserSearchClass = classLoader.loadClass("org.mozilla.gecko.home.BrowserSearch");
+            final Field suggestClientField = browserSearchClass.getDeclaredField("mSuggestClient");
             suggestClientField.setAccessible(true);
+
             waitForTest(new BooleanTest() {
                 @Override
                 public boolean test() {
-                    // wait for mSuggestClient to be set before we replace it
-                    try {
-                        return suggestClientField.get(allPagesTab) != null;
-                    } catch (IllegalAccessException e) {
-                        return false;
-                    }
+                    final Fragment browserSearch = getBrowserSearch();
+                    return (browserSearch != null);
                 }
             }, SUGGESTION_TIMEOUT);
-            suggestClientField.set(allPagesTab, client);
+
+            final Fragment browserSearch = getBrowserSearch();
+            suggestClientField.set(browserSearch, client);
         } catch (Exception e) {
            throw new RuntimeException("Error setting SuggestClient", e);
         }
     }
 }