Bug 1082133 - browser_*_behavior.js fails when run as a standalone directory. r=mak
authorJoel Maher <jmaher@mozilla.com>
Tue, 11 Nov 2014 11:37:00 +0100
changeset 226712 4e83df89ebb86eff069f45c99ba5323123b269c0
parent 226703 210b1691f0c71a662adccb43d766e335c4135e3e
child 226713 f2ee082ca47284347fe74e29a98090ba562df930
push id60
push usergszorc@mozilla.com
push dateThu, 13 Nov 2014 06:13:11 +0000
reviewersmak
bugs1082133
milestone36.0a1
Bug 1082133 - browser_*_behavior.js fails when run as a standalone directory. r=mak
browser/components/search/test/browser.ini
browser/components/search/test/browser_abouthome_behavior.js
browser/components/search/test/browser_amazon_behavior.js
browser/components/search/test/browser_bing_behavior.js
browser/components/search/test/browser_eBay_behavior.js
browser/components/search/test/browser_google_behavior.js
browser/components/search/test/browser_yahoo_behavior.js
--- a/browser/components/search/test/browser.ini
+++ b/browser/components/search/test/browser.ini
@@ -30,8 +30,10 @@ skip-if = e10s # Bug ?????? - some issue
 [browser_google_behavior.js]
 skip-if = e10s # Bug ?????? - some issue with progress listeners [JavaScript Error: "req.originalURI is null" {file: "chrome://mochitests/content/browser/browser/components/search/test/browser_bing_behavior.js" line: 127}]
 [browser_healthreport.js]
 [browser_private_search_perwindowpb.js]
 skip-if = e10s # Bug ?????? - Test uses load event and checks event.target.
 [browser_yahoo.js]
 [browser_yahoo_behavior.js]
 skip-if = e10s # Bug ?????? - some issue with progress listeners [JavaScript Error: "req.originalURI is null" {file: "chrome://mochitests/content/browser/browser/components/search/test/browser_bing_behavior.js" line: 127}]
+[browser_abouthome_behavior.js]
+skip-if = e10s # Bug ???????
new file mode 100644
--- /dev/null
+++ b/browser/components/search/test/browser_abouthome_behavior.js
@@ -0,0 +1,178 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+/*
+ * Test home page search for all plugin URLs
+ */
+
+"use strict";
+
+const BROWSER_SEARCH_PREF      = "browser.search.";
+
+const MOZ_PARAM_LOCALE_REGEX   = /\{moz:locale\}/g;
+const MOZ_PARAM_DIST_ID_REGEX  = /\{moz:distributionID\}/g;
+const MOZ_PARAM_OFFICIAL_REGEX = /\{moz:official\}/g;
+
+// Custom search parameters
+const MOZ_OFFICIAL = Services.appinfo.isOfficialBranding ? "official" : "unofficial";
+
+XPCOMUtils.defineLazyGetter(this, "distributionID", () => {
+  try {
+    return Services.prefs.getCharPref(BROWSER_SEARCH_PREF + "distributionID");
+  } catch (ex) {
+    return Services.appinfo.distributionID;
+  }
+});
+
+XPCOMUtils.defineLazyGetter(this, "GOOGLE_CLIENT", () => {
+  switch (Services.appinfo.defaultUpdateChannel) {
+    case "beta":
+      return "firefox-beta";
+    case "aurora":
+      return "firefox-aurora";
+    case "nightly":
+      return "firefox-nightly";
+    default:
+      return "firefox-a";
+  }
+});
+
+function test() {
+  // Bug 992270: Ignore uncaught about:home exceptions (related to snippets from IndexedDB)
+  ignoreAllUncaughtExceptions(true);
+
+  function replaceUrl(base) {
+    return base.replace(MOZ_PARAM_LOCALE_REGEX, getLocale())
+               .replace(MOZ_PARAM_DIST_ID_REGEX, distributionID)
+               .replace(MOZ_PARAM_OFFICIAL_REGEX, MOZ_OFFICIAL);
+  }
+
+  let gMutationObserver = null;
+
+  function verify_about_home_search(engine_name) {
+    let engine = Services.search.getEngineByName(engine_name);
+    ok(engine, engine_name + " is installed");
+
+    let previouslySelectedEngine = Services.search.currentEngine;
+    Services.search.currentEngine = engine;
+
+    // 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;
+      gMutationObserver = new MutationObserver(function (mutations) {
+        for (let mutation of mutations) {
+          if (mutation.attributeName == "searchEngineName") {
+            // Re-add the listener, and perform a search
+            gBrowser.addProgressListener(listener);
+            gMutationObserver.disconnect()
+            gMutationObserver = null;
+            executeSoon(function() {
+              doc.getElementById("searchText").value = "foo";
+              doc.getElementById("searchSubmit").click();
+            });
+          }
+        }
+      });
+      gMutationObserver.observe(doc.documentElement, { attributes: true });
+    }, true);
+  }
+  waitForExplicitFinish();
+
+  let gCurrTest;
+  let gTests = [
+    {
+      name: "Search with Bing from about:home",
+      searchURL: replaceUrl("http://www.bing.com/search?q=foo&pc=MOZI&form=MOZSPG"),
+      run: function () {
+        verify_about_home_search("Bing");
+      }
+    },
+    {
+      name: "Search with Yahoo from about:home",
+      searchURL: replaceUrl("https://search.yahoo.com/search?p=foo&ei=UTF-8&fr=moz35"),
+      run: function () {
+        verify_about_home_search("Yahoo");
+      }
+    },
+    {
+      name: "Search with eBay from about:home",
+      searchURL: replaceUrl("http://rover.ebay.com/rover/1/711-47294-18009-3/4?mfe=search&mpre=http://www.ebay.com/sch/i.html?_nkw=foo"),
+      run: function () {
+        verify_about_home_search("eBay");
+      }
+    },
+    {
+      name: "Search with Google from about:home",
+      searchURL: replaceUrl("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 + "&channel=np&source=hp"),
+      run: function () {
+        verify_about_home_search("Google");
+      }
+    },
+    {
+      name: "Search with Amazon.com from about:home",
+      searchURL: replaceUrl("http://www.amazon.com/exec/obidos/external-search/?field-keywords=foo&mode=blended&tag=mozilla-20&sourceid=Mozilla-search"),
+      run: function () {
+        verify_about_home_search("Amazon.com");
+      }
+    }
+  ];
+
+  function nextTest() {
+    if (gTests.length) {
+      gCurrTest = gTests.shift();
+      info("Running : " + gCurrTest.name);
+      executeSoon(gCurrTest.run);
+    } else {
+      // Make sure we listen again for uncaught exceptions in the next test or cleanup.
+      executeSoon(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);
+    if (gMutationObserver)
+      gMutationObserver.disconnect();
+  });
+
+  tab.linkedBrowser.addEventListener("load", function load() {
+    tab.linkedBrowser.removeEventListener("load", load, true);
+    gBrowser.addProgressListener(listener);
+    nextTest();
+  }, true);
+}
--- a/browser/components/search/test/browser_amazon_behavior.js
+++ b/browser/components/search/test/browser_amazon_behavior.js
@@ -97,59 +97,20 @@ function test() {
                 win.removeEventListener("ContentSearchService", done);
                 // Let gSearch respond to the event before continuing.
                 executeSoon(() => doSearch(win.document));
               }
             });
           }
         }, true);
       }
-    },
-    {
-      name: "home page search",
-      searchURL: base,
-      run: function () {
-        // Bug 992270: Ignore uncaught about:home exceptions (related to snippets from IndexedDB)
-        ignoreAllUncaughtExceptions(true);
-
-        // 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() {
-    // Make sure we listen again for uncaught exceptions in the next test or cleanup.
-    ignoreAllUncaughtExceptions(false);
-
     if (gTests.length) {
       gCurrTest = gTests.shift();
       info("Running : " + gCurrTest.name);
       executeSoon(gCurrTest.run);
     } else {
       finish();
     }
   }
--- a/browser/components/search/test/browser_bing_behavior.js
+++ b/browser/components/search/test/browser_bing_behavior.js
@@ -97,59 +97,20 @@ function test() {
                 win.removeEventListener("ContentSearchService", done);
                 // Let gSearch respond to the event before continuing.
                 executeSoon(() => doSearch(win.document));
               }
             });
           }
         }, true);
       }
-    },
-    {
-      name: "home page search",
-      searchURL: base + "&form=MOZSPG",
-      run: function () {
-        // Bug 992270: Ignore uncaught about:home exceptions (related to snippets from IndexedDB)
-        ignoreAllUncaughtExceptions(true);
-
-        // 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() {
-    // Make sure we listen again for uncaught exceptions in the next test or cleanup.
-    ignoreAllUncaughtExceptions(false);
-
     if (gTests.length) {
       gCurrTest = gTests.shift();
       info("Running : " + gCurrTest.name);
       executeSoon(gCurrTest.run);
     } else {
       finish();
     }
   }
--- a/browser/components/search/test/browser_eBay_behavior.js
+++ b/browser/components/search/test/browser_eBay_behavior.js
@@ -97,59 +97,20 @@ function test() {
                 win.removeEventListener("ContentSearchService", done);
                 // Let gSearch respond to the event before continuing.
                 executeSoon(() => doSearch(win.document));
               }
             });
           }
         }, true);
       }
-    },
-    {
-      name: "home page search",
-      searchURL: base,
-      run: function () {
-        // Bug 992270: Ignore uncaught about:home exceptions (related to snippets from IndexedDB)
-        ignoreAllUncaughtExceptions(true);
-
-        // 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() {
-    // Make sure we listen again for uncaught exceptions in the next test or cleanup.
-    ignoreAllUncaughtExceptions(false);
-
     if (gTests.length) {
       gCurrTest = gTests.shift();
       info("Running : " + gCurrTest.name);
       executeSoon(gCurrTest.run);
     } else {
       finish();
     }
   }
--- a/browser/components/search/test/browser_google_behavior.js
+++ b/browser/components/search/test/browser_google_behavior.js
@@ -133,59 +133,20 @@ function test() {
                 win.removeEventListener("ContentSearchService", done);
                 // Let gSearch respond to the event before continuing.
                 executeSoon(() => doSearch(win.document));
               }
             });
           }
         }, true);
       }
-    },
-    {
-      name: "home page search",
-      searchURL: base + "&channel=np&source=hp",
-      run: function () {
-        // Bug 992270: Ignore uncaught about:home exceptions (related to snippets from IndexedDB)
-        ignoreAllUncaughtExceptions(true);
-
-        // 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() {
-    // Make sure we listen again for uncaught exceptions in the next test or cleanup.
-    ignoreAllUncaughtExceptions(false);
-
     if (gTests.length) {
       gCurrTest = gTests.shift();
       info("Running : " + gCurrTest.name);
       executeSoon(gCurrTest.run);
     } else {
       finish();
     }
   }
--- a/browser/components/search/test/browser_yahoo_behavior.js
+++ b/browser/components/search/test/browser_yahoo_behavior.js
@@ -97,59 +97,20 @@ function test() {
                 win.removeEventListener("ContentSearchService", done);
                 // Let gSearch respond to the event before continuing.
                 executeSoon(() => doSearch(win.document));
               }
             });
           }
         }, true);
       }
-    },
-    {
-      name: "home page search",
-      searchURL: base,
-      run: function () {
-        // Bug 992270: Ignore uncaught about:home exceptions (related to snippets from IndexedDB)
-        ignoreAllUncaughtExceptions(true);
-
-        // 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() {
-    // Make sure we listen again for uncaught exceptions in the next test or cleanup.
-    ignoreAllUncaughtExceptions(false);
-
     if (gTests.length) {
       gCurrTest = gTests.shift();
       info("Running : " + gCurrTest.name);
       executeSoon(gCurrTest.run);
     } else {
       finish();
     }
   }