Bug 1568157 - Part 4: Replace `toolbox.walker` with the contextual WalkerFront. r...
[gecko.git] / devtools / client / framework / test / browser_toolbox_window_reload_target.js
blobb5f37ee596f5fbf4d66c964b884f88c314492045
1 /* Any copyright is dedicated to the Public Domain.
2  * http://creativecommons.org/publicdomain/zero/1.0/ */
4 // Test that pressing various page reload keyboard shortcuts always works when devtools
5 // has focus, no matter if it's undocked or docked, and whatever the tool selected (this
6 // is to avoid tools from overriding the page reload shortcuts).
7 // This test also serves as a safety net checking that tools just don't explode when the
8 // page is reloaded.
9 // It is therefore quite long to run.
11 requestLongerTimeout(10);
13 const TEST_URL =
14   "data:text/html;charset=utf-8," +
15   "<html><head><title>Test reload</title></head>" +
16   "<body><h1>Testing reload from devtools</h1></body></html>";
18 const { Toolbox } = require("devtools/client/framework/toolbox");
19 const { LocalizationHelper } = require("devtools/shared/l10n");
20 const L10N = new LocalizationHelper(
21   "devtools/client/locales/toolbox.properties"
24 // Track how many page reloads we've sent to the page.
25 var reloadsSent = 0;
27 add_task(async function() {
28   await addTab(TEST_URL);
29   const target = await TargetFactory.forTab(gBrowser.selectedTab);
30   // Load the frame-script-utils into the child.
31   loadFrameScriptUtils();
33   info("Getting the entire list of tools supported in this tab");
34   const toolIDs = gDevTools
35     .getToolDefinitionArray()
36     .filter(def => def.isTargetSupported(target))
37     .map(def => def.id);
39   info(
40     "Display the toolbox, docked at the bottom, with the first tool selected"
41   );
42   const toolbox = await gDevTools.showToolbox(
43     target,
44     toolIDs[0],
45     Toolbox.HostType.BOTTOM
46   );
48   info(
49     "Listen to page reloads to check that they are indeed sent by the toolbox"
50   );
51   let reloadDetected = 0;
52   const reloadCounter = msg => {
53     reloadDetected++;
54     info("Detected reload #" + reloadDetected);
55     is(
56       reloadDetected,
57       reloadsSent,
58       "Detected the right number of reloads in the page"
59     );
60   };
61   gBrowser.selectedBrowser.messageManager.addMessageListener(
62     "devtools:test:load",
63     reloadCounter
64   );
66   info("Start testing with the toolbox docked");
67   // Note that we actually only test 1 tool in docked mode, to cut down on test time.
68   await testOneTool(toolbox, toolIDs[toolIDs.length - 1]);
70   info("Switch to undocked mode");
71   await toolbox.switchHost(Toolbox.HostType.WINDOW);
72   toolbox.win.focus();
74   info("Now test with the toolbox undocked");
75   for (const toolID of toolIDs) {
76     await testOneTool(toolbox, toolID);
77   }
79   info("Switch back to docked mode");
80   await toolbox.switchHost(Toolbox.HostType.BOTTOM);
82   gBrowser.selectedBrowser.messageManager.removeMessageListener(
83     "devtools:test:load",
84     reloadCounter
85   );
87   await toolbox.destroy();
88   gBrowser.removeCurrentTab();
89 });
91 async function testOneTool(toolbox, toolID) {
92   info(`Select tool ${toolID}`);
93   await toolbox.selectTool(toolID);
95   await testReload("toolbox.reload.key", toolbox);
96   await testReload("toolbox.reload2.key", toolbox);
97   await testReload("toolbox.forceReload.key", toolbox);
98   await testReload("toolbox.forceReload2.key", toolbox);
101 async function testReload(shortcut, toolbox) {
102   info(`Reload with ${shortcut}`);
104   const mm = gBrowser.selectedBrowser.messageManager;
105   const walker = (await toolbox.target.getFront("inspector")).walker;
107   return new Promise(resolve => {
108     const observer = {
109       _isDocumentUnloaded: false,
110       _isNewRooted: false,
111       onMutation(mutations) {
112         for (const { type } of mutations) {
113           if (type === "documentUnload") {
114             this._isDocumentUnloaded = true;
115           } else if (type === "newRoot") {
116             this._isNewRooted = true;
117           }
118         }
119       },
120       isReady() {
121         return this._isDocumentUnloaded && this._isNewRooted;
122       },
123     };
125     observer.onMutation = observer.onMutation.bind(observer);
126     walker.on("mutations", observer.onMutation);
128     // If we have a jsdebugger panel, wait for it to complete its reload
129     const jsdebugger = toolbox.getPanel("jsdebugger");
130     let onReloaded = Promise.resolve;
131     if (jsdebugger) {
132       onReloaded = jsdebugger.once("reloaded");
133     }
135     const complete = async () => {
136       mm.removeMessageListener("devtools:test:load", complete);
137       // Wait for the documentUnload and newRoot were fired.
138       await waitUntil(() => observer.isReady());
139       walker.off("mutations", observer.onMutation);
140       await onReloaded;
141       resolve();
142     };
144     mm.addMessageListener("devtools:test:load", complete);
146     toolbox.win.focus();
147     synthesizeKeyShortcut(L10N.getStr(shortcut), toolbox.win);
148     reloadsSent++;
149   });