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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
/* eslint no-unused-vars: [2, {"vars": "local", "args": "none"}] */
"use strict";
/* import-globals-from ../../debugger/test/mochitest/helpers.js */
/* import-globals-from ../../debugger/test/mochitest/helpers/context.js */
Services.scriptloader.loadSubScript(
"chrome://mochitests/content/browser/devtools/client/debugger/test/mochitest/helpers.js",
this
);
var { DevToolsServer } = require("devtools/server/devtools-server");
var { DevToolsClient } = require("devtools/client/devtools-client");
var { Toolbox } = require("devtools/client/framework/toolbox");
function createWorkerInTab(tab, url) {
info("Creating worker with url '" + url + "' in tab.");
return SpecialPowers.spawn(tab.linkedBrowser, [url], urlChild => {
return new Promise(resolve => {
const worker = new content.Worker(urlChild);
worker.addEventListener(
"message",
function() {
if (!content.workers) {
content.workers = [];
}
content.workers[urlChild] = worker;
resolve();
},
{ once: true }
);
});
});
}
function terminateWorkerInTab(tab, url) {
info("Terminating worker with url '" + url + "' in tab.");
return SpecialPowers.spawn(tab.linkedBrowser, [url], urlChild => {
content.workers[urlChild].terminate();
delete content.workers[urlChild];
});
}
function postMessageToWorkerInTab(tab, url, message) {
info("Posting message to worker with url '" + url + "' in tab.");
return SpecialPowers.spawn(
tab.linkedBrowser,
[url, message],
(urlChild, messageChild) => {
return new Promise(function(resolve) {
const worker = content.workers[urlChild];
worker.postMessage(messageChild);
worker.addEventListener(
"message",
function() {
resolve();
},
{ once: true }
);
});
}
);
}
function evalInTab(tab, string) {
info("Evalling string in tab.");
return SpecialPowers.spawn(tab.linkedBrowser, [string], stringChild => {
return content.eval(stringChild);
});
}
function connect(client) {
info("Connecting client.");
return client.connect();
}
function close(client) {
info("Waiting for client to close.\n");
return client.close();
}
function listTabs(client) {
info("Listing tabs.");
return client.mainRoot.listTabs();
}
function findTab(tabs, url) {
info("Finding tab with url '" + url + "'.");
for (const tab of tabs) {
if (tab.url === url) {
return tab;
}
}
return null;
}
function listWorkers(targetFront) {
info("Listing workers.");
return targetFront.listWorkers();
}
function findWorker(workers, url) {
info("Finding worker with url '" + url + "'.");
for (const worker of workers) {
if (worker.url === url) {
return worker;
}
}
return null;
}
function waitForWorkerListChanged(targetFront) {
info("Waiting for worker list to change.");
return targetFront.once("workerListChanged");
}
async function waitForWorkerClose(workerDescriptorFront) {
info("Waiting for worker to close.");
await workerDescriptorFront.once("close");
info("Worker did close.");
}
// Return a promise with a reference to webconsole, opening the split
// console if necessary. This cleans up the split console pref so
// it won't pollute other tests.
async function getSplitConsole(toolbox, win) {
if (!win) {
win = toolbox.win;
}
if (!toolbox.splitConsole) {
EventUtils.synthesizeKey("VK_ESCAPE", {}, win);
}
await toolbox.getPanelWhenReady("webconsole");
ok(toolbox.splitConsole, "Split console is shown.");
return toolbox.getPanel("webconsole");
}
function executeAndWaitForMessage(
webconsole,
expression,
expectedTextContent,
className = "result"
) {
const { ui } = webconsole.hud;
return new Promise(resolve => {
const onNewMessages = messages => {
for (const message of messages) {
if (
message.node.classList.contains(className) &&
message.node.textContent.includes(expectedTextContent)
) {
ui.off("new-messages", onNewMessages);
resolve(message.node);
}
}
};
ui.on("new-messages", onNewMessages);
ui.wrapper.dispatchEvaluateExpression(expression);
});
}
async function initWorkerDebugger(TAB_URL, WORKER_URL) {
const tab = await addTab(TAB_URL);
const target = await TargetFactory.forTab(tab);
await target.attach();
const { client } = target;
await createWorkerInTab(tab, WORKER_URL);
const { workers } = await listWorkers(target);
const workerDescriptorFront = findWorker(workers, WORKER_URL);
const toolbox = await gDevTools.showToolbox(
workerDescriptorFront,
"jsdebugger",
Toolbox.HostType.WINDOW
);
const debuggerPanel = toolbox.getCurrentPanel();
const gDebugger = debuggerPanel.panelWin;
const context = createDebuggerContext(toolbox);
return {
...context,
client,
tab,
target,
workerDescriptorFront,
toolbox,
gDebugger,
};
}
// Override addTab/removeTab as defined by shared-head, since these have
// an extra window parameter and add a frame script
this.addTab = function addTab(url, win) {
info("Adding tab: " + url);
return new Promise(resolve => {
const targetWindow = win || window;
const targetBrowser = targetWindow.gBrowser;
targetWindow.focus();
const tab = (targetBrowser.selectedTab = BrowserTestUtils.addTab(
targetBrowser,
url
));
const linkedBrowser = tab.linkedBrowser;
BrowserTestUtils.browserLoaded(linkedBrowser).then(function() {
info("Tab added and finished loading: " + url);
resolve(tab);
});
});
};
this.removeTab = function removeTab(tab, win) {
info("Removing tab.");
return new Promise(resolve => {
const targetWindow = win || window;
const targetBrowser = targetWindow.gBrowser;
const tabContainer = targetBrowser.tabContainer;
tabContainer.addEventListener(
"TabClose",
function() {
info("Tab removed and finished closing.");
resolve();
},
{ once: true }
);
targetBrowser.removeTab(tab);
});
};
function pushPrefs(...aPrefs) {
return new Promise(resolve => {
SpecialPowers.pushPrefEnv({ set: aPrefs }, resolve);
});
}
function popPrefs() {
return new Promise(resolve => {
SpecialPowers.popPrefEnv(resolve);
});
}
|