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
9 // It is therefore quite long to run.
11 requestLongerTimeout(10);
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.
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))
40 "Display the toolbox, docked at the bottom, with the first tool selected"
42 const toolbox = await gDevTools.showToolbox(
45 Toolbox.HostType.BOTTOM
49 "Listen to page reloads to check that they are indeed sent by the toolbox"
51 let reloadDetected = 0;
52 const reloadCounter = msg => {
54 info("Detected reload #" + reloadDetected);
58 "Detected the right number of reloads in the page"
61 gBrowser.selectedBrowser.messageManager.addMessageListener(
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);
74 info("Now test with the toolbox undocked");
75 for (const toolID of toolIDs) {
76 await testOneTool(toolbox, toolID);
79 info("Switch back to docked mode");
80 await toolbox.switchHost(Toolbox.HostType.BOTTOM);
82 gBrowser.selectedBrowser.messageManager.removeMessageListener(
87 await toolbox.destroy();
88 gBrowser.removeCurrentTab();
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 => {
109 _isDocumentUnloaded: 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;
121 return this._isDocumentUnloaded && this._isNewRooted;
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;
132 onReloaded = jsdebugger.once("reloaded");
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);
144 mm.addMessageListener("devtools:test:load", complete);
147 synthesizeKeyShortcut(L10N.getStr(shortcut), toolbox.win);