Bug 1436665 - Fix WebExtensions devtools.network tests; r=ochameau,rpl
☠☠ backed out by 00d4c3be380c ☠ ☠
authorJan Odvarko <odvarko@gmail.com>
Fri, 13 Apr 2018 15:56:07 +0200
changeset 467544 09030f59fbeae4360be8c46d7e85ff32e9c00201
parent 467543 513c72b05382c41171f49f81b0a799a6cfc132ad
child 467545 0fa5f7cfee1da60eca676b977deb7ad65dc28082
push id9165
push userasasaki@mozilla.com
push dateThu, 26 Apr 2018 21:04:54 +0000
treeherdermozilla-beta@064c3804de2e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersochameau, rpl
bugs1436665
milestone61.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 1436665 - Fix WebExtensions devtools.network tests; r=ochameau,rpl MozReview-Commit-ID: KAwcvb18JO1
browser/components/extensions/test/browser/browser_ext_devtools_network.js
devtools/client/netmonitor/src/connector/firefox-connector.js
--- a/browser/components/extensions/test/browser/browser_ext_devtools_network.js
+++ b/browser/components/extensions/test/browser/browser_ext_devtools_network.js
@@ -71,25 +71,21 @@ function devtools_page() {
     // Get response content using returned promise
     request.getContent().then(([content, encoding]) => {
       browser.test.sendMessage("onRequestFinished-promiseResolved",
                                [content, encoding]);
     });
 
     browser.devtools.network.onRequestFinished.removeListener(requestFinishedListener);
   };
-  browser.devtools.network.onRequestFinished.addListener(requestFinishedListener);
-}
 
-function waitForRequestAdded(toolbox) {
-  return new Promise(resolve => {
-    let netPanel = toolbox.getPanel("netmonitor");
-    netPanel.panelWin.api.once("NetMonitor:RequestAdded", () => {
-      resolve();
-    });
+  browser.test.onMessage.addListener(msg => {
+    if (msg === "addOnRequestFinishedListener") {
+      browser.devtools.network.onRequestFinished.addListener(requestFinishedListener);
+    }
   });
 }
 
 let extData = {
   background,
   manifest: {
     permissions: ["tabs", "http://mochi.test/", "http://example.com/"],
     devtools_page: "devtools_page.html",
@@ -103,16 +99,36 @@ let extData = {
         </head>
         <body>
         </body>
       </html>`,
     "devtools_page.js": devtools_page,
   },
 };
 
+function waitForRequestAdded(toolbox) {
+  return new Promise(async resolve => {
+    let netPanel = await toolbox.getNetMonitorAPI();
+    netPanel.once("NetMonitor:RequestAdded", () => {
+      resolve();
+    });
+  });
+}
+
+async function navigateToolboxTarget(extension, toolbox) {
+  extension.sendMessage("navigate");
+
+  // Wait till the navigation is complete.
+  await Promise.all([
+    extension.awaitMessage("tabUpdated"),
+    extension.awaitMessage("onNavigatedFired"),
+    waitForRequestAdded(toolbox),
+  ]);
+}
+
 /**
  * Test for `chrome.devtools.network.onNavigate()` API
  */
 add_task(async function test_devtools_network_on_navigated() {
   let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, "http://mochi.test:8888/");
   let extension = ExtensionTestUtils.loadExtension(extData);
 
   await extension.startup();
@@ -157,52 +173,42 @@ add_task(async function test_devtools_ne
   await extension.awaitMessage("ready");
 
   let target = gDevTools.getTargetForTab(tab);
 
   // Open the Toolbox
   let toolbox = await gDevTools.showToolbox(target, "webconsole");
   info("Developer toolbox opened.");
 
-  // Get HAR, it should be empty since the Net panel wasn't selected.
+  // Get HAR, it should be empty since no data collected yet.
   const getHAREmptyPromise = extension.awaitMessage("getHAR-result");
   extension.sendMessage("getHAR");
   const getHAREmptyResult = await getHAREmptyPromise;
   is(getHAREmptyResult.entries.length, 0, "HAR log should be empty");
 
-  // Select the Net panel.
-  await toolbox.selectTool("netmonitor");
-
-  // Get HAR again, it should be empty because the Panel is selected
-  // but no data collected yet.
-  const getHAREmptyPromiseWithPanel = extension.awaitMessage("getHAR-result");
-  extension.sendMessage("getHAR");
-  const emptyResultWithPanel = await getHAREmptyPromiseWithPanel;
-  is(emptyResultWithPanel.entries.length, 0, "HAR log should be empty");
-
   // Reload the page to collect some HTTP requests.
-  extension.sendMessage("navigate");
-
-  // Wait till the navigation is complete and request
-  // added into the net panel.
-  await Promise.all([
-    extension.awaitMessage("tabUpdated"),
-    extension.awaitMessage("onNavigatedFired"),
-    extension.awaitMessage("onRequestFinished"),
-    extension.awaitMessage("onRequestFinished-callbackExecuted"),
-    extension.awaitMessage("onRequestFinished-promiseResolved"),
-    waitForRequestAdded(toolbox),
-  ]);
+  await navigateToolboxTarget(extension, toolbox);
 
   // Get HAR, it should not be empty now.
   const getHARPromise = extension.awaitMessage("getHAR-result");
   extension.sendMessage("getHAR");
   const getHARResult = await getHARPromise;
   is(getHARResult.entries.length, 1, "HAR log should not be empty");
 
+  // Select the Net panel and reload page again.
+  await toolbox.selectTool("netmonitor");
+  await navigateToolboxTarget(extension, toolbox);
+
+  // Get HAR again, it should not be empty even if
+  // the Network panel is selected now.
+  const getHAREmptyPromiseWithPanel = extension.awaitMessage("getHAR-result");
+  extension.sendMessage("getHAR");
+  const emptyResultWithPanel = await getHAREmptyPromiseWithPanel;
+  is(emptyResultWithPanel.entries.length, 1, "HAR log should not be empty");
+
   // Shutdown
   await gDevTools.closeToolbox(target);
 
   await target.destroy();
 
   await extension.unload();
 
   BrowserTestUtils.removeTab(tab);
@@ -212,45 +218,42 @@ add_task(async function test_devtools_ne
  * Test for `chrome.devtools.network.onRequestFinished()` API
  */
 add_task(async function test_devtools_network_on_request_finished() {
   let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, "http://mochi.test:8888/");
   let extension = ExtensionTestUtils.loadExtension(extData);
 
   await extension.startup();
   await extension.awaitMessage("ready");
-
   let target = gDevTools.getTargetForTab(tab);
 
   // Open the Toolbox
-  let toolbox = await gDevTools.showToolbox(target, "netmonitor");
+  let toolbox = await gDevTools.showToolbox(target, "webconsole");
   info("Developer toolbox opened.");
 
-  // Reload and wait for onRequestFinished event.
-  extension.sendMessage("navigate");
+  // Wait the extension to subscribe the onRequestFinished listener.
+  await extension.sendMessage("addOnRequestFinishedListener");
 
-  await Promise.all([
-    extension.awaitMessage("tabUpdated"),
-    extension.awaitMessage("onNavigatedFired"),
-    waitForRequestAdded(toolbox),
-  ]);
+  // Reload the page
+  await navigateToolboxTarget(extension, toolbox);
 
+  info("Wait for an onRequestFinished event");
   await extension.awaitMessage("onRequestFinished");
 
   // Wait for response content being fetched.
+  info("Wait for request.getBody results");
   let [callbackRes, promiseRes] = await Promise.all([
     extension.awaitMessage("onRequestFinished-callbackExecuted"),
     extension.awaitMessage("onRequestFinished-promiseResolved"),
   ]);
 
   ok(callbackRes[0].startsWith("<html>"),
      "The expected content has been retrieved.");
   is(callbackRes[1], "text/html; charset=utf-8",
      "The expected content has been retrieved.");
-
   is(promiseRes[0], callbackRes[0],
      "The resolved value is equal to the one received in the callback API mode");
   is(promiseRes[1], callbackRes[1],
      "The resolved value is equal to the one received in the callback API mode");
 
   // Shutdown
   await gDevTools.closeToolbox(target);
 
--- a/devtools/client/netmonitor/src/connector/firefox-connector.js
+++ b/devtools/client/netmonitor/src/connector/firefox-connector.js
@@ -80,17 +80,18 @@ class FirefoxConnector {
   async disconnect() {
     if (this.actions) {
       this.actions.batchReset();
     }
 
     await this.removeListeners();
 
     if (this.tabTarget) {
-      this.tabTarget.off("will-navigate");
+      this.tabTarget.off("will-navigate", this.willNavigate);
+      this.tabTarget.off("navigate", this.navigate);
       this.tabTarget = null;
     }
 
     this.webConsoleClient = null;
     this.dataProvider = null;
   }
 
   async pause() {
@@ -171,24 +172,28 @@ class FirefoxConnector {
     if (this.dataProvider.isPayloadQueueEmpty()) {
       this.onReloaded();
       return;
     }
     let listener = () => {
       if (this.dataProvider && !this.dataProvider.isPayloadQueueEmpty()) {
         return;
       }
-      this.owner.off(EVENTS.PAYLOAD_READY, listener);
+      if (this.owner) {
+        this.owner.off(EVENTS.PAYLOAD_READY, listener);
+      }
       // Netmonitor may already be destroyed,
       // so do not try to notify the listeners
       if (this.dataProvider) {
         this.onReloaded();
       }
     };
-    this.owner.on(EVENTS.PAYLOAD_READY, listener);
+    if (this.owner) {
+      this.owner.on(EVENTS.PAYLOAD_READY, listener);
+    }
   }
 
   onReloaded() {
     let panel = this.toolbox.getPanel("netmonitor");
     if (panel) {
       panel.emit("reloaded");
     }
   }