summaryrefslogtreecommitdiffstats
path: root/devtools/client/framework/test/browser_toolbox_hosts.js
blob: 37738865a93c038a5469977b4ea5300469bbf0a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const {
  gDevToolsBrowser,
} = require("resource://devtools/client/framework/devtools-browser.js");

const { Toolbox } = require("resource://devtools/client/framework/toolbox.js");
const { LEFT, RIGHT, BOTTOM, WINDOW } = Toolbox.HostType;
let toolbox;

// We are opening/close toolboxes many times,
// which introduces long GC pauses between each sub task
// and requires some more time to run in DEBUG builds.
requestLongerTimeout(2);

const URL =
  "data:text/html;charset=utf8,test for opening toolbox in different hosts";

add_task(async function () {
  const win = await BrowserTestUtils.openNewBrowserWindow();
  win.gBrowser.selectedTab = BrowserTestUtils.addTab(win.gBrowser, URL);

  const tab = win.gBrowser.selectedTab;
  toolbox = await gDevTools.showToolboxForTab(tab, {
    toolId: "webconsole",
    hostType: Toolbox.HostType.WINDOW,
  });
  const onToolboxClosed = toolbox.once("destroyed");
  ok(
    gDevToolsBrowser.hasToolboxOpened(win),
    "hasToolboxOpened is true before closing the toolbox"
  );
  await BrowserTestUtils.closeWindow(win);
  ok(
    !gDevToolsBrowser.hasToolboxOpened(win),
    "hasToolboxOpened is false after closing the window"
  );

  info("Wait for toolbox to be destroyed after browser window is closed");
  await onToolboxClosed;
  toolbox = null;
});

add_task(async function runTest() {
  info("Create a test tab and open the toolbox");
  const tab = await addTab(URL);
  toolbox = await gDevTools.showToolboxForTab(tab, { toolId: "webconsole" });

  await runHostTests(gBrowser);
  await toolbox.destroy();

  toolbox = null;
  gBrowser.removeCurrentTab();
});

// We run the same host switching tests in a private window.
// See Bug 1581093 for an example of issue specific to private windows.
add_task(async function runPrivateWindowTest() {
  info("Create a private window + tab and open the toolbox");
  await runHostTestsFromSeparateWindow({
    private: true,
  });
});

// We run the same host switching tests in a non-fission window.
// See Bug 1650963 for an example of issue specific to private windows.
add_task(async function runNonFissionWindowTest() {
  info("Create a non-fission window + tab and open the toolbox");
  await runHostTestsFromSeparateWindow({
    fission: false,
  });
});

async function runHostTestsFromSeparateWindow(options) {
  const win = await BrowserTestUtils.openNewBrowserWindow(options);
  const browser = win.gBrowser;
  browser.selectedTab = BrowserTestUtils.addTab(browser, URL);

  const tab = browser.selectedTab;
  toolbox = await gDevTools.showToolboxForTab(tab, { toolId: "webconsole" });

  await runHostTests(browser);
  await toolbox.destroy();

  toolbox = null;
  await BrowserTestUtils.closeWindow(win);
}

async function runHostTests(browser) {
  await testBottomHost(browser);
  await testLeftHost(browser);
  await testRightHost(browser);
  await testWindowHost(browser);
  await testToolSelect();
  await testDestroy(browser);
  await testRememberHost();
  await testPreviousHost();
}

function testBottomHost(browser) {
  checkHostType(toolbox, BOTTOM);

  // test UI presence
  const panel = browser.getPanel();
  const iframe = panel.querySelector(".devtools-toolbox-bottom-iframe");
  ok(iframe, "toolbox bottom iframe exists");

  checkToolboxLoaded(iframe);
}

async function testLeftHost(browser) {
  await toolbox.switchHost(LEFT);
  checkHostType(toolbox, LEFT);

  // test UI presence
  const panel = browser.getPanel();
  const bottom = panel.querySelector(".devtools-toolbox-bottom-iframe");
  ok(!bottom, "toolbox bottom iframe doesn't exist");

  const iframe = panel.querySelector(".devtools-toolbox-side-iframe");
  ok(iframe, "toolbox side iframe exists");

  checkToolboxLoaded(iframe);
}

async function testRightHost(browser) {
  await toolbox.switchHost(RIGHT);
  checkHostType(toolbox, RIGHT);

  // test UI presence
  const panel = browser.getPanel();
  const bottom = panel.querySelector(".devtools-toolbox-bottom-iframe");
  ok(!bottom, "toolbox bottom iframe doesn't exist");

  const iframe = panel.querySelector(".devtools-toolbox-side-iframe");
  ok(iframe, "toolbox side iframe exists");

  checkToolboxLoaded(iframe);
}

async function testWindowHost(browser) {
  await toolbox.switchHost(WINDOW);
  checkHostType(toolbox, WINDOW);

  const panel = browser.getPanel();
  const sidebar = panel.querySelector(".devtools-toolbox-side-iframe");
  ok(!sidebar, "toolbox sidebar iframe doesn't exist");

  const win = Services.wm.getMostRecentWindow("devtools:toolbox");
  ok(win, "toolbox separate window exists");

  const iframe = win.document.querySelector(".devtools-toolbox-window-iframe");
  checkToolboxLoaded(iframe);
}

async function testToolSelect() {
  // make sure we can load a tool after switching hosts
  await toolbox.selectTool("inspector");
}

async function testDestroy(browser) {
  await toolbox.destroy();
  toolbox = await gDevTools.showToolboxForTab(browser.selectedTab);
}

function testRememberHost() {
  // last host was the window - make sure it's the same when re-opening
  is(toolbox.hostType, WINDOW, "host remembered");

  const win = Services.wm.getMostRecentWindow("devtools:toolbox");
  ok(win, "toolbox separate window exists");
}

async function testPreviousHost() {
  // last host was the window - make sure it's the same when re-opening
  is(toolbox.hostType, WINDOW, "host remembered");

  info("Switching to left");
  await toolbox.switchHost(LEFT);
  checkHostType(toolbox, LEFT, WINDOW);

  info("Switching to right");
  await toolbox.switchHost(RIGHT);
  checkHostType(toolbox, RIGHT, LEFT);

  info("Switching to bottom");
  await toolbox.switchHost(BOTTOM);
  checkHostType(toolbox, BOTTOM, RIGHT);

  info("Switching from bottom to right");
  await toolbox.switchToPreviousHost();
  checkHostType(toolbox, RIGHT, BOTTOM);

  info("Switching from right to bottom");
  await toolbox.switchToPreviousHost();
  checkHostType(toolbox, BOTTOM, RIGHT);

  info("Switching to window");
  await toolbox.switchHost(WINDOW);
  checkHostType(toolbox, WINDOW, BOTTOM);

  info("Switching from window to bottom");
  await toolbox.switchToPreviousHost();
  checkHostType(toolbox, BOTTOM, WINDOW);

  info("Forcing the previous host to match the current (bottom)");
  Services.prefs.setCharPref("devtools.toolbox.previousHost", BOTTOM);

  info("Switching from bottom to right (since previous=current=bottom");
  await toolbox.switchToPreviousHost();
  checkHostType(toolbox, RIGHT, BOTTOM);

  info("Forcing the previous host to match the current (right)");
  Services.prefs.setCharPref("devtools.toolbox.previousHost", RIGHT);
  info("Switching from right to bottom (since previous=current=side");
  await toolbox.switchToPreviousHost();
  checkHostType(toolbox, BOTTOM, RIGHT);
}

function checkToolboxLoaded(iframe) {
  const tabs = iframe.contentDocument.querySelector(".toolbox-tabs");
  ok(tabs, "toolbox UI has been loaded into iframe");
}