Bug 1188692 - Attempt to reduce intermittent failures in browser_ContentSearch.js by initialising listeners earlier. r=adw, a=test-only
authorMark Banner <standard8@mozilla.com>
Wed, 21 Dec 2016 11:50:25 +0000
changeset 353512 0cc9fa6f5389f5d4be6a40f22b3594954a8206b1
parent 353511 4c681a167a5e3ce19021604e1b3661d2418b4966
child 353513 39fd680b2e51c6a586c9f438eba53ee55387b61e
push id6795
push userjlund@mozilla.com
push dateMon, 23 Jan 2017 14:19:46 +0000
treeherdermozilla-esr52@76101b503191 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersadw, test-only
bugs1188692
milestone52.0a2
Bug 1188692 - Attempt to reduce intermittent failures in browser_ContentSearch.js by initialising listeners earlier. r=adw, a=test-only 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: function() {
-        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: function (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
@@ -6,16 +6,34 @@ const TEST_MSG = "ContentSearchTest";
 const CONTENT_SEARCH_MSG = "ContentSearch";
 const TEST_CONTENT_SCRIPT_BASENAME = "contentSearch.js";
 
 // This timeout is absurdly high to avoid random failures like bug 1087120.
 // That bug was reported when the timeout was 5 seconds, so let's try 10.
 const SUGGESTIONS_TIMEOUT = 10000;
 
 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: 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: function (webProg, req, flags, status) {