Bug 925898: properly pass the "homepage" reason to getSubmission calls for about:home, r=MattN, a=lsblakk
☠☠ backed out by 844d1594df32 ☠ ☠
authorGavin Sharp <gavin@gavinsharp.com>
Fri, 11 Oct 2013 15:00:11 -0700
changeset 155912 4253a66382fa080f2b5b0b902d1bd84c54bb765a
parent 155911 651537e2968a32bfbab112b7c8b32ecb16fb54b8
child 155913 f93851154cf0ff2e232bd6a40aa7d5b2b561e98c
push id4486
push usergsharp@mozilla.com
push dateFri, 25 Oct 2013 14:05:23 +0000
treeherdermozilla-aurora@4253a66382fa [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersMattN, lsblakk
bugs925898
milestone26.0a2
Bug 925898: properly pass the "homepage" reason to getSubmission calls for about:home, r=MattN, a=lsblakk
browser/components/search/test/Makefile.in
browser/components/search/test/browser_google.js
browser/components/search/test/browser_google_behavior.js
browser/modules/AboutHome.jsm
--- a/browser/components/search/test/Makefile.in
+++ b/browser/components/search/test/Makefile.in
@@ -18,13 +18,14 @@ MOCHITEST_BROWSER_FILES = \
   483086-1.xml \
   483086-2.xml \
   test.html \
   $(NULL)
 
 ifdef ENABLE_TESTS
 pp_mochitest_browser_files := \
   browser_google.js \
+  browser_google_behavior.js \
   $(NULL)
 pp_mochitest_browser_files_PATH := $(DEPTH)/_tests/testing/mochitest/browser/$(relativesrcdir)
 pp_mochitest_browser_files_FLAGS := -DMOZ_DISTRIBUTION_ID=$(MOZ_DISTRIBUTION_ID)
 PP_TARGETS += pp_mochitest_browser_files
 endif # ENABLE_TESTS
--- a/browser/components/search/test/browser_google.js
+++ b/browser/components/search/test/browser_google.js
@@ -75,16 +75,18 @@ function test() {
 
   // Test search URLs (including purposes).
   url = engine.getSubmission("foo").uri.spec;
   is(url, base, "Check search URL for 'foo'");
   url = engine.getSubmission("foo", null, "contextmenu").uri.spec;
   is(url, base + "&channel=rcs", "Check context menu search URL for 'foo'");
   url = engine.getSubmission("foo", null, "keyword").uri.spec;
   is(url, base + "&channel=fflb", "Check keyword search URL for 'foo'");
+  url = engine.getSubmission("foo", null, "homepage").uri.spec;
+  is(url, base + "&channel=np&source=hp", "Check homepage search URL for 'foo'");
 
   // Check search suggestion URL.
   url = engine.getSubmission("foo", "application/x-suggestions+json").uri.spec;
   is(url, "https://www.google.com/complete/search?client=firefox&q=foo", "Check search suggestion URL for 'foo'");
 
   // Check all other engine properties.
   const EXPECTED_ENGINE = {
     name: "Google",
new file mode 100644
--- /dev/null
+++ b/browser/components/search/test/browser_google_behavior.js
@@ -0,0 +1,187 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+/*
+ * Test Google search plugin URLs
+ */
+
+"use strict";
+
+const BROWSER_SEARCH_PREF      = "browser.search.";
+
+const MOZ_PARAM_LOCALE         = /\{moz:locale\}/g;
+const MOZ_PARAM_DIST_ID        = /\{moz:distributionID\}/g;
+const MOZ_PARAM_OFFICIAL       = /\{moz:official\}/g;
+
+// Custom search parameters
+#ifdef MOZ_OFFICIAL_BRANDING
+const MOZ_OFFICIAL = "official";
+#else
+const MOZ_OFFICIAL = "unofficial";
+#endif
+
+#if MOZ_UPDATE_CHANNEL == beta
+const GOOGLE_CLIENT = "firefox-beta";
+#elif MOZ_UPDATE_CHANNEL == aurora
+const GOOGLE_CLIENT = "firefox-aurora";
+#elif MOZ_UPDATE_CHANNEL == nightly
+const GOOGLE_CLIENT = "firefox-nightly";
+#else
+const GOOGLE_CLIENT = "firefox-a";
+#endif
+
+#expand const MOZ_DISTRIBUTION_ID = __MOZ_DISTRIBUTION_ID__;
+
+function getLocale() {
+  const localePref = "general.useragent.locale";
+  return getLocalizedPref(localePref, Services.prefs.getCharPref(localePref));
+}
+
+function getLocalizedPref(aPrefName, aDefault) {
+  try {
+    return Services.prefs.getComplexValue(aPrefName, Ci.nsIPrefLocalizedString).data;
+  } catch (ex) {
+    return aDefault;
+  }
+
+  return aDefault;
+}
+
+function test() {
+  let engine = Services.search.getEngineByName("Google");
+  ok(engine, "Google is installed");
+
+  is(Services.search.defaultEngine, engine, "Check that Google is the default search engine");
+
+  let distributionID;
+  try {
+    distributionID = Services.prefs.getCharPref(BROWSER_SEARCH_PREF + "distributionID");
+  } catch (ex) {
+    distributionID = MOZ_DISTRIBUTION_ID;
+  }
+
+  let base = "https://www.google.com/search?q=foo&ie=utf-8&oe=utf-8&aq=t&rls={moz:distributionID}:{moz:locale}:{moz:official}&client=" + GOOGLE_CLIENT;
+  base = base.replace(MOZ_PARAM_LOCALE, getLocale());
+  base = base.replace(MOZ_PARAM_DIST_ID, distributionID);
+  base = base.replace(MOZ_PARAM_OFFICIAL, MOZ_OFFICIAL);
+
+  let url;
+
+  // Test search URLs (including purposes).
+  url = engine.getSubmission("foo").uri.spec;
+  is(url, base, "Check search URL for 'foo'");
+
+  waitForExplicitFinish();
+
+  var gCurrTest;
+  var gTests = [
+    {
+      name: "context menu search",
+      searchURL: base + "&channel=rcs",
+      run: function () {
+        // Simulate a contextmenu search
+        // FIXME: This is a bit "low-level"...
+        BrowserSearch.loadSearch("foo", false, "contextmenu");
+      }
+    },
+    {
+      name: "keyword search",
+      searchURL: base + "&channel=fflb",
+      run: function () {
+        gURLBar.value = "? foo";
+        gURLBar.focus();
+        EventUtils.synthesizeKey("VK_RETURN", {});
+      }
+    },
+    {
+      name: "search bar search",
+      searchURL: base + "&channel=sb",
+      run: function () {
+        let sb = BrowserSearch.searchBar;
+        sb.focus();
+        sb.value = "foo";
+        registerCleanupFunction(function () {
+          sb.value = "";
+        });
+        EventUtils.synthesizeKey("VK_RETURN", {});
+      }
+    },
+    {
+      name: "home page search",
+      searchURL: base + "&channel=np&source=hp",
+      run: function () {
+        // load about:home, but remove the listener first so it doesn't
+        // get in the way
+        gBrowser.removeProgressListener(listener);
+        gBrowser.loadURI("about:home");
+        info("Waiting for about:home load");
+        tab.linkedBrowser.addEventListener("load", function load(event) {
+          if (event.originalTarget != tab.linkedBrowser.contentDocument ||
+              event.target.location.href == "about:blank") {
+            info("skipping spurious load event");
+            return;
+          }
+          tab.linkedBrowser.removeEventListener("load", load, true);
+
+          // Observe page setup
+          let doc = gBrowser.contentDocument;
+          let mutationObserver = new MutationObserver(function (mutations) {
+            for (let mutation of mutations) {
+              if (mutation.attributeName == "searchEngineName") {
+                // Re-add the listener, and perform a search
+                gBrowser.addProgressListener(listener);
+                doc.getElementById("searchText").value = "foo";
+                doc.getElementById("searchSubmit").click();
+              }
+            }
+          });
+          mutationObserver.observe(doc.documentElement, { attributes: true });
+        }, true);
+      }
+    }
+  ];
+
+  function nextTest() {
+    if (gTests.length) {
+      gCurrTest = gTests.shift();
+      info("Running : " + gCurrTest.name);
+      executeSoon(gCurrTest.run);
+    } else {
+      finish();
+    }
+  }
+
+  let tab = gBrowser.selectedTab = gBrowser.addTab();
+
+  let listener = {
+    onStateChange: function onStateChange(webProgress, req, flags, status) {
+      info("onStateChange");
+      // Only care about top-level document starts
+      let docStart = Ci.nsIWebProgressListener.STATE_IS_DOCUMENT |
+                     Ci.nsIWebProgressListener.STATE_START;
+      if (!(flags & docStart) || !webProgress.isTopLevel)
+        return;
+
+      info("received document start");
+
+      ok(req instanceof Ci.nsIChannel, "req is a channel");
+      is(req.originalURI.spec, gCurrTest.searchURL, "search URL was loaded");
+      info("Actual URI: " + req.URI.spec);
+
+      req.cancel(Components.results.NS_ERROR_FAILURE);
+
+      executeSoon(nextTest);
+    }
+  }
+
+  registerCleanupFunction(function () {
+    gBrowser.removeProgressListener(listener);
+    gBrowser.removeTab(tab);
+  });
+
+  tab.linkedBrowser.addEventListener("load", function load() {
+    tab.linkedBrowser.removeEventListener("load", load, true);
+    gBrowser.addProgressListener(listener);
+    nextTest();
+  }, true);
+}
--- a/browser/modules/AboutHome.jsm
+++ b/browser/modules/AboutHome.jsm
@@ -164,17 +164,17 @@ let AboutHome = {
         } catch(ex) {
           Cu.reportError(ex);
           break;
         }
 #ifdef MOZ_SERVICES_HEALTHREPORT
         window.BrowserSearch.recordSearchInHealthReport(data.engineName, "abouthome");
 #endif
         // Trigger a search through nsISearchEngine.getSubmission()
-        let submission = Services.search.currentEngine.getSubmission(data.searchTerms);
+        let submission = Services.search.currentEngine.getSubmission(data.searchTerms, null, "homepage");
         window.loadURI(submission.uri.spec, null, submission.postData);
         break;
     }
   },
 
   // Send all the chrome-privileged data needed by about:home. This
   // gets re-sent when the search engine changes.
   sendAboutHomeData: function(target) {