Bug 1496848 - Make browser_ContentSearch.js work with process flips. r=adw
authorMike Conley <mconley@mozilla.com>
Fri, 05 Oct 2018 22:29:49 +0000
changeset 439858 9f11d400d1cc
parent 439857 2c3fc67d57b9
child 439859 73dcff37f846
push id34792
push usernerli@mozilla.com
push dateSat, 06 Oct 2018 09:26:47 +0000
treeherdermozilla-central@e2ec1eeb812d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersadw
bugs1496848
milestone64.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1496848 - Make browser_ContentSearch.js work with process flips. r=adw Depends on D7937 Differential Revision: https://phabricator.services.mozilla.com/D7938
browser/modules/test/browser/browser_ContentSearch.js
browser/modules/test/browser/contentSearch.js
--- a/browser/modules/test/browser/browser_ContentSearch.js
+++ b/browser/modules/test/browser/browser_ContentSearch.js
@@ -100,62 +100,52 @@ add_task(async function modifyEngine() {
   msg = await waitForTestMsg(mm, "CurrentState");
   checkMsg(msg, {
     type: "CurrentState",
     data: await currentStateObj(),
   });
 });
 
 add_task(async function search() {
-  let { mm } = await addTab();
+  let { browser } = await addTab();
   let engine = Services.search.currentEngine;
   let data = {
     engineName: engine.name,
     searchString: "ContentSearchTest",
     healthReportKey: "ContentSearchTest",
     searchPurpose: "ContentSearchTest",
   };
   let submissionURL =
     engine.getSubmission(data.searchString, "", data.whence).uri.spec;
-  mm.sendAsyncMessage(TEST_MSG, {
-    type: "Search",
-    data,
-    expectedURL: submissionURL,
-  });
-  let msg = await waitForTestMsg(mm, "loadStopped");
-  Assert.equal(msg.data.url, submissionURL, "Correct search page loaded");
+
+  await performSearch(browser, data, submissionURL);
 });
 
 add_task(async function searchInBackgroundTab() {
   // This test is like search(), but it opens a new tab after starting a search
   // in another.  In other words, it performs a search in a background tab.  The
   // search page should be loaded in the same tab that performed the search, in
   // the background tab.
-  let { mm } = await addTab();
+  let { browser } = await addTab();
   let engine = Services.search.currentEngine;
   let data = {
     engineName: engine.name,
     searchString: "ContentSearchTest",
     healthReportKey: "ContentSearchTest",
     searchPurpose: "ContentSearchTest",
   };
   let submissionURL =
     engine.getSubmission(data.searchString, "", data.whence).uri.spec;
-  mm.sendAsyncMessage(TEST_MSG, {
-    type: "Search",
-    data,
-    expectedURL: submissionURL,
-  });
 
+  let searchPromise = performSearch(browser, data, submissionURL);
   let newTab = BrowserTestUtils.addTab(gBrowser);
   gBrowser.selectedTab = newTab;
   registerCleanupFunction(() => gBrowser.removeTab(newTab));
 
-  let msg = await waitForTestMsg(mm, "loadStopped");
-  Assert.equal(msg.data.url, submissionURL, "Correct search page loaded");
+  await searchPromise;
 });
 
 add_task(async function badImage() {
   let { mm } = await addTab();
   // If the bad image URI caused an exception to be thrown within ContentSearch,
   // then we'll hang waiting for the CurrentState responses triggered by the new
   // engine.  That's what we're testing, and obviously it shouldn't happen.
   let vals = await waitForNewEngine(mm, "contentSearchBadImage.xml", 1);
@@ -258,16 +248,31 @@ add_task(async function GetSuggestions_A
     },
   });
 
   // Finally, clean up by removing the test engine.
   Services.search.removeEngine(engine);
   await waitForTestMsg(mm, "CurrentState");
 });
 
+async function performSearch(browser, data, expectedURL) {
+  let mm = browser.messageManager;
+  let stoppedPromise = BrowserTestUtils.browserStopped(browser, expectedURL);
+  mm.sendAsyncMessage(TEST_MSG, {
+    type: "Search",
+    data,
+    expectedURL,
+  });
+
+  await stoppedPromise;
+  // BrowserTestUtils.browserStopped should ensure this, but let's
+  // be absolutely sure.
+  Assert.equal(browser.currentURI.spec, expectedURL, "Correct search page loaded");
+}
+
 function buffersEqual(actualArrayBuffer, expectedArrayBuffer) {
   let expectedView = new Int8Array(expectedArrayBuffer);
   let actualView = new Int8Array(actualArrayBuffer);
   for (let i = 0; i < expectedView.length; i++) {
     if (actualView[i] != expectedView[i]) {
       return false;
     }
   }
--- a/browser/modules/test/browser/contentSearch.js
+++ b/browser/modules/test/browser/contentSearch.js
@@ -18,63 +18,16 @@ content.addEventListener(SERVICE_EVENT_T
   sendAsyncMessage(TEST_MSG, Cu.waiveXrays(event.detail));
 });
 
 // Forward messages from the test to the in-content service.
 addMessageListener(TEST_MSG, msg => {
   (async function() {
     // If the message is a search, stop the page from loading and then tell the
     // test that it loaded.
-    let loadPromise;
-    if (msg.data.type == "Search") {
-      loadPromise = waitForLoadAndStopIt(msg.data.expectedURL);
-    }
 
     content.dispatchEvent(
       new content.CustomEvent(CLIENT_EVENT_TYPE, {
         detail: Cu.cloneInto(msg.data, content),
       })
     );
-
-    if (msg.data.type == "Search") {
-      let url = await loadPromise;
-
-      sendAsyncMessage(TEST_MSG, {
-        type: "loadStopped",
-        url,
-      });
-    }
   })();
 });
-
-// We need to keep a reference to the listener as webProgress records a weak
-// reference, hence we need to stop garbage collection cleaning it up before the
-// test is completed.
-var webProgressListener;
-
-function waitForLoadAndStopIt(expectedURL) {
-  return new Promise(resolve => {
-    let webProgress = content.docShell.QueryInterface(Ci.nsIInterfaceRequestor)
-                                      .getInterface(Ci.nsIWebProgress);
-    webProgressListener = {
-      onStateChange(webProg, req, flags, status) {
-        if (req instanceof Ci.nsIChannel) {
-          let url = req.originalURI.spec;
-          dump("waitForLoadAndStopIt: onStateChange " + url + "\n");
-          let docStart = Ci.nsIWebProgressListener.STATE_IS_DOCUMENT |
-                         Ci.nsIWebProgressListener.STATE_START;
-          if ((flags & docStart) && webProg.isTopLevel && url == expectedURL) {
-            webProgress.removeProgressListener(webProgressListener);
-            webProgressListener = null;
-            req.cancel(Cr.NS_ERROR_FAILURE);
-            resolve(url);
-          }
-        }
-      },
-      QueryInterface: ChromeUtils.generateQI([
-        Ci.nsIWebProgressListener,
-        Ci.nsISupportsWeakReference,
-      ]),
-    };
-    webProgress.addProgressListener(webProgressListener, Ci.nsIWebProgress.NOTIFY_ALL);
-    dump("waitForLoadAndStopIt: Waiting for URL to load: " + expectedURL + "\n");
-  });
-}