Bug 1497545 - update inspector-helper attachURL to provide a target; r=ochameau
authoryulia <ystartsev@mozilla.com>
Fri, 12 Oct 2018 13:45:27 +0000
changeset 489851 cb7c1a49aa0f706ee44229a0aa39294761656753
parent 489850 463d82852c31d6709c4c176afd0da2be799d13b5
child 489852 2ed2e01ecdef6ac55541754551b1dc706ec370af
push id247
push userfmarier@mozilla.com
push dateSat, 27 Oct 2018 01:06:44 +0000
reviewersochameau
bugs1497545
milestone64.0a1
Bug 1497545 - update inspector-helper attachURL to provide a target; r=ochameau This is part 1 of bug 1497545, and covers the most difficult case, which is migrating attachURL to something a bit more modern and easier to read. The goal is to make our tests more consistant with our code base now, and keep these tests maintainable. UPDATE: I will split this up, as it is too large to review in one pass Differential Revision: https://phabricator.services.mozilla.com/D8369
devtools/server/tests/mochitest/inspector-helpers.js
--- a/devtools/server/tests/mochitest/inspector-helpers.js
+++ b/devtools/server/tests/mochitest/inspector-helpers.js
@@ -1,17 +1,18 @@
 /* exported attachURL, promiseDone, assertOwnershipTrees, checkMissing, checkAvailable,
    promiseOnce, isSrcChange, isUnretained, isNewRoot, assertSrcChange, assertUnload,
    assertFrameLoad, assertChildList, waitForMutation, addTest, addAsyncTest,
    runNextTest */
 "use strict";
 
 const {require} = ChromeUtils.import("resource://devtools/shared/Loader.jsm", {});
-const {DebuggerClient} = require("devtools/shared/client/debugger-client");
+const {TargetFactory} = require("devtools/client/framework/target");
 const {DebuggerServer} = require("devtools/server/main");
+const {BrowserTestUtils} = require("resource://testing-common/BrowserTestUtils.jsm");
 
 const Services = require("Services");
 const {DocumentWalker: _documentWalker} = require("devtools/server/actors/inspector/document-walker");
 
 // Always log packets when running tests.
 Services.prefs.setBoolPref("devtools.debugger.log", true);
 SimpleTest.registerCleanupFunction(function() {
   Services.prefs.clearUserPref("devtools.debugger.log");
@@ -29,64 +30,63 @@ var gAttachCleanups = [];
 
 SimpleTest.registerCleanupFunction(function() {
   for (const cleanup of gAttachCleanups) {
     cleanup();
   }
 });
 
 /**
+ * Add a new test tab in the browser and load the given url.
+ * @return Promise a promise that resolves to the new target representing
+ *         the page currently opened.
+ */
+
+async function getTargetForSelectedTab(gBrowser) {
+  const selectedTab = gBrowser.selectedTab;
+  await BrowserTestUtils.browserLoaded(selectedTab.linkedBrowser);
+  return TargetFactory.forTab(selectedTab);
+}
+
+/**
  * Open a tab, load the url, wait for it to signal its readiness,
  * find the tab with the debugger server, and call the callback.
  *
  * Returns a function which can be called to close the opened ta
  * and disconnect its debugger client.
  */
-function attachURL(url, callback) {
-  let win = window.open(url, "_blank");
-  let client = null;
+async function attachURL(url) {
+  // Get the current browser window
+  const gBrowser = Services.wm.getMostRecentWindow("navigator:browser").gBrowser;
 
-  const cleanup = () => {
+  // open the url in a new tab, save a reference to the new inner window global object
+  // and wait for it to load. The tests rely on this window object to send a "ready"
+  // event to its opener (the test page). This window reference is used within
+  // the test tab, to reference the webpage being tested against, which is in another
+  // tab.
+  const windowOpened = BrowserTestUtils.waitForNewTab(gBrowser, url);
+  let win = window.open(url, "_blank");
+  await windowOpened;
+
+  const target = await getTargetForSelectedTab(gBrowser);
+  const client = target.client;
+  await target.attach();
+
+  const cleanup = async function() {
     if (client) {
-      client.close();
-      client = null;
+      await client.close();
     }
     if (win) {
       win.close();
       win = null;
     }
   };
+
   gAttachCleanups.push(cleanup);
-
-  window.addEventListener("message", function loadListener(event) {
-    if (event.data === "ready") {
-      client = new DebuggerClient(DebuggerServer.connectPipe());
-      client.connect().then(([applicationType, traits]) => {
-        client.listTabs().then(response => {
-          for (const tab of response.tabs) {
-            if (tab.url === url) {
-              window.removeEventListener("message", loadListener);
-              // eslint-disable-next-line max-nested-callbacks
-              client.attachTarget(tab.actor).then(function() {
-                try {
-                  callback(null, client, tab, win.document);
-                } catch (ex) {
-                  Cu.reportError(ex);
-                  dump(ex);
-                }
-              });
-              break;
-            }
-          }
-        });
-      });
-    }
-  });
-
-  return cleanup;
+  return { client, target, doc: win.document, cleanup };
 }
 
 function promiseOnce(target, event) {
   return new Promise(resolve => {
     target.on(event, (...args) => {
       if (args.length === 1) {
         resolve(args[0]);
       } else {