Bug 1497348 - Remove callbacks and use async await in browser_toolbox_window_reload_target.js; r=yzen
authorPatrick Brosset <pbrosset@mozilla.com>
Thu, 18 Oct 2018 07:56:14 +0000
changeset 490237 d3dc7831efa89c4a464ba2aeb4252ce984185cfb
parent 490236 32670a4721d4dac1a14315b16db5847a20749027
child 490238 125e5170ef3a92d25ad95cf14df1cc564a4e0a52
push id247
push userfmarier@mozilla.com
push dateSat, 27 Oct 2018 01:06:44 +0000
reviewersyzen
bugs1497348
milestone64.0a1
Bug 1497348 - Remove callbacks and use async await in browser_toolbox_window_reload_target.js; r=yzen Differential Revision: https://phabricator.services.mozilla.com/D8974
devtools/client/framework/test/browser_toolbox_window_reload_target.js
--- a/devtools/client/framework/test/browser_toolbox_window_reload_target.js
+++ b/devtools/client/framework/test/browser_toolbox_window_reload_target.js
@@ -1,98 +1,101 @@
 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
 /* vim: set ft=javascript ts=2 et sw=2 tw=80: */
 /* Any copyright is dedicated to the Public Domain.
  * http://creativecommons.org/publicdomain/zero/1.0/ */
 
+// Test that pressing various page reload keyboard shortcuts always works when devtools
+// has focus, no matter if it's undocked or docked, and whatever the tool selected (this
+// is to avoid tools from overriding the page reload shortcuts).
+// This test also serves as a safety net checking that tools just don't explode when the
+// page is reloaded.
+// It is therefore quite long to run.
+
 requestLongerTimeout(10);
 
 const TEST_URL = "data:text/html;charset=utf-8," +
                  "<html><head><title>Test reload</title></head>" +
                  "<body><h1>Testing reload from devtools</h1></body></html>";
 
-var {Toolbox} = require("devtools/client/framework/toolbox");
-
-const {LocalizationHelper} = require("devtools/shared/l10n");
+const { Toolbox } = require("devtools/client/framework/toolbox");
+const { LocalizationHelper } = require("devtools/shared/l10n");
 const L10N = new LocalizationHelper("devtools/client/locales/toolbox.properties");
 
-var target, toolbox, description, reloadsSent, toolIDs;
+// Track how many page reloads we've sent to the page.
+var reloadsSent = 0;
 
-function test() {
-  addTab(TEST_URL).then(async () => {
-    target = await TargetFactory.forTab(gBrowser.selectedTab);
+add_task(async function() {
+  await addTab(TEST_URL);
+  const target = await TargetFactory.forTab(gBrowser.selectedTab);
+  // Load the frame-script-utils into the child.
+  loadFrameScriptUtils();
 
-    toolIDs = gDevTools.getToolDefinitionArray()
-                .filter(def => def.isTargetSupported(target))
-                .map(def => def.id);
-    gDevTools.showToolbox(target, toolIDs[0], Toolbox.HostType.BOTTOM)
-             .then(startReloadTest);
-  });
-}
+  info("Getting the entire list of tools supported in this tab");
+  const toolIDs = gDevTools.getToolDefinitionArray()
+                           .filter(def => def.isTargetSupported(target))
+                           .map(def => def.id);
 
-function startReloadTest(aToolbox) {
-  loadFrameScriptUtils(); // causes frame-script-utils to be loaded into the child.
-  toolbox = aToolbox;
+  info("Display the toolbox, docked at the bottom, with the first tool selected");
+  const toolbox = await gDevTools.showToolbox(target, toolIDs[0],
+    Toolbox.HostType.BOTTOM);
 
-  reloadsSent = 0;
-  let reloads = 0;
-  const reloadCounter = (msg) => {
-    reloads++;
-    info("Detected reload #" + reloads);
-    is(reloads, reloadsSent, "Reloaded from devtools window once and only for " + description + "");
+  info("Listen to page reloads to check that they are indeed sent by the toolbox");
+  let reloadDetected = 0;
+  const reloadCounter = msg => {
+    reloadDetected++;
+    info("Detected reload #" + reloadDetected);
+    is(reloadDetected, reloadsSent, "Detected the right number of reloads in the page");
   };
   gBrowser.selectedBrowser.messageManager.addMessageListener("devtools:test:load", reloadCounter);
 
-  testAllTheTools("docked", () => {
-    const origHostType = toolbox.hostType;
-    toolbox.switchHost(Toolbox.HostType.WINDOW).then(() => {
-      toolbox.win.focus();
-      testAllTheTools("undocked", () => {
-        toolbox.switchHost(origHostType).then(() => {
-          gBrowser.selectedBrowser.messageManager.removeMessageListener("devtools:test:load", reloadCounter);
-          // If we finish too early, the inspector breaks promises:
-          toolbox.getPanel("inspector").once("new-root", finishUp);
-        });
-      });
-    });
-  }, toolIDs.length - 1 /* only test 1 tool in docked mode, to cut down test time */);
+  info("Start testing with the toolbox docked");
+  // Note that we actually only test 1 tool in docked mode, to cut down on test time.
+  await testOneTool(toolbox, toolIDs[toolIDs.length - 1]);
+
+  info("Switch to undocked mode");
+  await toolbox.switchHost(Toolbox.HostType.WINDOW);
+  toolbox.win.focus();
+
+  info("Now test with the toolbox undocked");
+  for (const toolID of toolIDs) {
+    await testOneTool(toolbox, toolID);
+  }
+
+  info("Switch back to docked mode");
+  await toolbox.switchHost(Toolbox.HostType.BOTTOM);
+
+  gBrowser.selectedBrowser.messageManager.removeMessageListener("devtools:test:load", reloadCounter);
+
+  // If we finish too early, the inspector breaks promises.
+  await toolbox.getPanel("inspector").once("new-root");
+
+  await toolbox.destroy();
+  gBrowser.removeCurrentTab();
+});
+
+async function testOneTool(toolbox, toolID) {
+  info(`Select tool ${toolID}`);
+  await toolbox.selectTool(toolID);
+
+  await testReload("toolbox.reload.key", toolbox, toolID);
+  await testReload("toolbox.reload2.key", toolbox, toolID);
+  await testReload("toolbox.forceReload.key", toolbox, toolID);
+  await testReload("toolbox.forceReload2.key", toolbox, toolID);
 }
 
-function testAllTheTools(docked, callback, toolNum = 0) {
-  if (toolNum >= toolIDs.length) {
-    return callback();
-  }
-  toolbox.selectTool(toolIDs[toolNum]).then(() => {
-    testReload("toolbox.reload.key", docked, toolIDs[toolNum], () => {
-      testReload("toolbox.reload2.key", docked, toolIDs[toolNum], () => {
-        testReload("toolbox.forceReload.key", docked, toolIDs[toolNum], () => {
-          testReload("toolbox.forceReload2.key", docked, toolIDs[toolNum], () => {
-            testAllTheTools(docked, callback, toolNum + 1);
-          });
-        });
-      });
-    });
+function testReload(shortcut, toolbox, toolID) {
+  info(`Reload with ${shortcut}`);
+
+  const mm = gBrowser.selectedBrowser.messageManager;
+
+  return new Promise(resolve => {
+    const complete = () => {
+      mm.removeMessageListener("devtools:test:load", complete);
+      resolve();
+    };
+    mm.addMessageListener("devtools:test:load", complete);
+
+    toolbox.win.focus();
+    synthesizeKeyShortcut(L10N.getStr(shortcut), toolbox.win);
+    reloadsSent++;
   });
 }
-
-function testReload(shortcut, docked, toolID, callback) {
-  const complete = () => {
-    gBrowser.selectedBrowser.messageManager.removeMessageListener("devtools:test:load", complete);
-    return callback();
-  };
-  gBrowser.selectedBrowser.messageManager.addMessageListener("devtools:test:load", complete);
-
-  description = docked + " devtools with tool " + toolID + ", shortcut #" + shortcut;
-  info("Testing reload in " + description);
-  toolbox.win.focus();
-  synthesizeKeyShortcut(L10N.getStr(shortcut), toolbox.win);
-  reloadsSent++;
-}
-
-function finishUp() {
-  toolbox.destroy().then(() => {
-    gBrowser.removeCurrentTab();
-
-    target = toolbox = description = reloadsSent = toolIDs = null;
-
-    finish();
-  });
-}