Bug 1188692 - Attempt to reduce intermittent failures in browser_ContentSearch.js by initialising listeners earlier. r?adw draft
authorMark Banner <standard8@mozilla.com>
Wed, 21 Dec 2016 11:50:25 +0000
changeset 458541 1224b618bdda8746f5fdae8e2fb69443cbdf2862
parent 457899 8f3b24109e3412b36f97277e31ad66856dc609d6
child 541661 acb81065d2a4ac9755ea1b9c8cfbe2dc6e5615d2
push id40970
push userbmo:standard8@mozilla.com
push dateTue, 10 Jan 2017 12:53:12 +0000
reviewersadw
bugs1188692
milestone53.0a1
Bug 1188692 - Attempt to reduce intermittent failures in browser_ContentSearch.js by initialising listeners earlier. r?adw MozReview-Commit-ID: 89Ew3Kr0viC
browser/components/search/test/head.js
browser/modules/test/browser.ini
browser/modules/test/browser_ContentSearch.js
browser/modules/test/contentSearch.js
--- a/browser/components/search/test/head.js
+++ b/browser/components/search/test/head.js
@@ -46,25 +46,39 @@ function promiseEvent(aTarget, aEventNam
     }
 
     return true;
   }
 
   return BrowserTestUtils.waitForEvent(aTarget, aEventName, false, cancelEvent);
 }
 
+/**
+ * Adds a new search engine to the search service and confirms it completes.
+ *
+ * @param {String} basename  The file to load that contains the search engine
+ *                           details.
+ * @param {Object} [options] Options for the test:
+ *   - {String} [iconURL]       The icon to use for the search engine.
+ *   - {Boolean} [setAsCurrent] Whether to set the new engine to be the
+ *                              current engine or not.
+ *   - {String} [testPath]      Used to override the current test path if this
+ *                              file is used from a different directory.
+ * @returns {Promise} The promise is resolved once the engine is added, or
+ *                    rejected if the addition failed.
+ */
 function promiseNewEngine(basename, options = {}) {
   return new Promise((resolve, reject) => {
     // Default the setAsCurrent option to true.
     let setAsCurrent =
       options.setAsCurrent == undefined ? true : options.setAsCurrent;
     info("Waiting for engine to be added: " + basename);
     Services.search.init({
       onInitComplete() {
-        let url = getRootDirectory(gTestPath) + basename;
+        let url = getRootDirectory(options.testPath || gTestPath) + basename;
         let current = Services.search.currentEngine;
         Services.search.addEngine(url, null, options.iconURL || "", false, {
           onSuccess(engine) {
             info("Search engine added: " + basename);
             if (setAsCurrent) {
               Services.search.currentEngine = engine;
             }
             registerCleanupFunction(() => {
--- a/browser/modules/test/browser.ini
+++ b/browser/modules/test/browser.ini
@@ -9,16 +9,18 @@ support-files =
 [browser_CaptivePortalWatcher.js]
 skip-if = os == "win" # Bug 1313894
 [browser_ContentSearch.js]
 support-files =
   contentSearch.js
   contentSearchBadImage.xml
   contentSearchSuggestions.sjs
   contentSearchSuggestions.xml
+  !/browser/components/search/test/head.js
+  !/browser/components/search/test/testEngine.xml
 [browser_NetworkPrioritizer.js]
 [browser_PermissionUI.js]
 [browser_ProcessHangNotifications.js]
 skip-if = !e10s
 [browser_SelfSupportBackend.js]
 support-files =
   ../../components/uitour/test/uitour.html
   ../../components/uitour/UITour-lib.js
--- a/browser/modules/test/browser_ContentSearch.js
+++ b/browser/modules/test/browser_ContentSearch.js
@@ -2,16 +2,34 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 const TEST_MSG = "ContentSearchTest";
 const CONTENT_SEARCH_MSG = "ContentSearch";
 const TEST_CONTENT_SCRIPT_BASENAME = "contentSearch.js";
 
 var gMsgMan;
+/* eslint no-undef:"error" */
+/* import-globals-from ../../components/search/test/head.js */
+Services.scriptloader.loadSubScript(
+  "chrome://mochitests/content/browser/browser/components/search/test/head.js",
+  this);
+
+let originalEngine = Services.search.currentEngine;
+
+add_task(function* setup() {
+  yield promiseNewEngine("testEngine.xml", {
+    setAsCurrent: true,
+    testPath: "chrome://mochitests/content/browser/browser/components/search/test/",
+  });
+
+  registerCleanupFunction(() => {
+    Services.search.currentEngine = originalEngine;
+  });
+});
 
 add_task(function* GetState() {
   yield addTab();
   gMsgMan.sendAsyncMessage(TEST_MSG, {
     type: "GetState",
   });
   let msg = yield waitForTestMsg("State");
   checkMsg(msg, {
--- a/browser/modules/test/contentSearch.js
+++ b/browser/modules/test/contentSearch.js
@@ -13,32 +13,32 @@ content.addEventListener(SERVICE_EVENT_T
   // up with an XrayWrapper to it here, which will screw us up when trying to
   // serialize the object in sendAsyncMessage. Waive Xrays for the benefit of
   // the test machinery.
   sendAsyncMessage(TEST_MSG, Components.utils.waiveXrays(event.detail));
 });
 
 // Forward messages from the test to the in-content service.
 addMessageListener(TEST_MSG, msg => {
-  content.dispatchEvent(
-    new content.CustomEvent(CLIENT_EVENT_TYPE, {
-      detail: msg.data,
-    })
-  );
-
   // If the message is a search, stop the page from loading and then tell the
   // test that it loaded.
   if (msg.data.type == "Search") {
     waitForLoadAndStopIt(msg.data.expectedURL, url => {
       sendAsyncMessage(TEST_MSG, {
         type: "loadStopped",
         url,
       });
     });
   }
+
+  content.dispatchEvent(
+    new content.CustomEvent(CLIENT_EVENT_TYPE, {
+      detail: msg.data,
+    })
+  );
 });
 
 function waitForLoadAndStopIt(expectedURL, callback) {
   let Ci = Components.interfaces;
   let webProgress = docShell.QueryInterface(Ci.nsIInterfaceRequestor)
                             .getInterface(Ci.nsIWebProgress);
   let listener = {
     onStateChange(webProg, req, flags, status) {