OLD | NEW |
| (Empty) |
1 <html> | |
2 <head> | |
3 <script type="text/javascript" src="../../http/tests/inspector-protocol/inspecto
r-protocol-test.js"></script> | |
4 <script> | |
5 | |
6 var worker; | |
7 var onMessageCallbacks = {}; | |
8 | |
9 function startWorker() | |
10 { | |
11 var callback; | |
12 var promise = new Promise((fulfill) => callback = fulfill); | |
13 worker = new Worker("../resources/worker-console-worker.js"); | |
14 worker.onmessage = function(event) { | |
15 worker.onmessage = onMessageFromWorker; | |
16 callback(); | |
17 }; | |
18 return promise; | |
19 } | |
20 | |
21 function logInWorkerFromPage(message, callback) | |
22 { | |
23 onMessageCallbacks[message] = callback; | |
24 worker.postMessage(message); | |
25 } | |
26 | |
27 function onMessageFromWorker(event) | |
28 { | |
29 var callback = onMessageCallbacks[event.data]; | |
30 delete onMessageCallbacks[event.data]; | |
31 if (callback) | |
32 callback(); | |
33 } | |
34 | |
35 function stopWorker() | |
36 { | |
37 worker.terminate(); | |
38 worker = null; | |
39 } | |
40 | |
41 function test() | |
42 { | |
43 var workerEventHandler = {}; | |
44 InspectorTest.eventHandler["Target.attachedToTarget"] = onWorkerCreated; | |
45 InspectorTest.eventHandler["Target.receivedMessageFromTarget"] = onWorkerMes
sage; | |
46 workerEventHandler["Runtime.consoleAPICalled"] = onConsoleAPICalledFromWorke
r; | |
47 | |
48 var workerId; | |
49 | |
50 function onWorkerCreated(payload) | |
51 { | |
52 InspectorTest.log("Worker.created"); | |
53 workerId = payload.params.targetInfo.targetId; | |
54 } | |
55 | |
56 var requestId = 0; | |
57 var dispatchTable = []; | |
58 | |
59 function sendCommandToWorker(method, params, callback) | |
60 { | |
61 dispatchTable[++requestId] = callback; | |
62 var messageObject = { | |
63 "method": method, | |
64 "params": params, | |
65 "id": requestId | |
66 }; | |
67 InspectorTest.sendCommandOrDie("Target.sendMessageToTarget", { | |
68 targetId: workerId, | |
69 message: JSON.stringify(messageObject) | |
70 }); | |
71 } | |
72 | |
73 function onWorkerMessage(payload) | |
74 { | |
75 if (payload.params.targetId !== workerId) | |
76 InspectorTest.log("targetId mismatch"); | |
77 var messageObject = JSON.parse(payload.params.message); | |
78 var messageId = messageObject["id"]; | |
79 if (typeof messageId === "number") { | |
80 var handler = dispatchTable[messageId]; | |
81 dispatchTable[messageId] = null; | |
82 if (handler && typeof handler === "function") | |
83 handler(messageObject); | |
84 } else { | |
85 var eventName = messageObject["method"]; | |
86 var eventHandler = workerEventHandler[eventName]; | |
87 if (eventHandler) | |
88 eventHandler(messageObject); | |
89 } | |
90 } | |
91 | |
92 function logInWorker(message, next) | |
93 { | |
94 InspectorTest.log("Logging in worker: " + message); | |
95 InspectorTest.eventHandler["Log.entryAdded"] = onLogEntry; | |
96 InspectorTest.evaluateInPage("logInWorkerFromPage(\"" + message + "\")")
; | |
97 | |
98 function onLogEntry(payload) | |
99 { | |
100 InspectorTest.log("Got log message from page: " + payload.params.ent
ry.text); | |
101 delete InspectorTest.eventHandler["Log.entryAdded"]; | |
102 next(); | |
103 } | |
104 } | |
105 | |
106 var gotMessages = []; | |
107 var waitingForMessage; | |
108 var waitingForMessageCallback; | |
109 | |
110 function onConsoleAPICalledFromWorker(payload) | |
111 { | |
112 var message = payload.params.args[0].value; | |
113 InspectorTest.log("Got console API call from worker: " + message); | |
114 gotMessages.push(message); | |
115 if (message === waitingForMessage) | |
116 waitingForMessageCallback(); | |
117 } | |
118 | |
119 function waitForMessage(message, next) | |
120 { | |
121 if (gotMessages.indexOf(message) !== -1) { | |
122 next(); | |
123 return; | |
124 } | |
125 waitingForMessage = message; | |
126 waitingForMessageCallback = next; | |
127 } | |
128 | |
129 var steps = [ | |
130 function listenToConsole(next) | |
131 { | |
132 InspectorTest.sendCommandOrDie("Log.enable", {}, next); | |
133 }, | |
134 | |
135 function start0(next) | |
136 { | |
137 InspectorTest.log("Starting worker"); | |
138 InspectorTest.evaluateInPageAsync("startWorker()").then(next); | |
139 }, | |
140 | |
141 function log0(next) | |
142 { | |
143 logInWorker("message0", next); | |
144 }, | |
145 | |
146 function stop0(next) | |
147 { | |
148 InspectorTest.log("Stopping worker"); | |
149 InspectorTest.evaluateInPage("stopWorker()", next); | |
150 }, | |
151 | |
152 function start1(next) | |
153 { | |
154 InspectorTest.log("Starting worker"); | |
155 InspectorTest.evaluateInPageAsync("startWorker()").then(next); | |
156 }, | |
157 | |
158 function log1(next) | |
159 { | |
160 logInWorker("message1", next); | |
161 }, | |
162 | |
163 function enable1(next) | |
164 { | |
165 InspectorTest.log("Starting autoattach"); | |
166 InspectorTest.sendCommandOrDie("Target.setAutoAttach", {autoAttach:
true, waitForDebuggerOnStart: false}, next); | |
167 }, | |
168 | |
169 function consoleEnable1(next) | |
170 { | |
171 InspectorTest.log("Sending Runtime.enable to worker"); | |
172 waitForMessage("message1", next); | |
173 sendCommandToWorker("Runtime.enable", {}); | |
174 }, | |
175 | |
176 function log2(next) | |
177 { | |
178 logInWorker("message2", next); | |
179 }, | |
180 | |
181 function waitForMessage2(next) | |
182 { | |
183 waitForMessage("message2", next); | |
184 }, | |
185 | |
186 function throw1(next) | |
187 { | |
188 logInWorker("throw1", next); | |
189 }, | |
190 | |
191 function disable1(next) | |
192 { | |
193 InspectorTest.log("Stopping autoattach"); | |
194 InspectorTest.sendCommandOrDie("Target.setAutoAttach", {autoAttach:
false, waitForDebuggerOnStart: false}, next); | |
195 }, | |
196 | |
197 function log3(next) | |
198 { | |
199 logInWorker("message3", next); | |
200 }, | |
201 | |
202 function stop1(next) | |
203 { | |
204 InspectorTest.log("Stopping worker"); | |
205 InspectorTest.evaluateInPage("stopWorker()", next); | |
206 }, | |
207 | |
208 | |
209 function enable2(next) | |
210 { | |
211 InspectorTest.log("Starting autoattach"); | |
212 InspectorTest.sendCommandOrDie("Target.setAutoAttach", {autoAttach:
true, waitForDebuggerOnStart: false}, next); | |
213 }, | |
214 | |
215 function start2(next) | |
216 { | |
217 InspectorTest.log("Starting worker"); | |
218 InspectorTest.evaluateInPageAsync("startWorker()").then(next); | |
219 }, | |
220 | |
221 function log4(next) | |
222 { | |
223 logInWorker("message4", next); | |
224 }, | |
225 | |
226 function consoleEnable2(next) | |
227 { | |
228 InspectorTest.log("Sending Runtime.enable to worker"); | |
229 waitForMessage("message4", next); | |
230 sendCommandToWorker("Runtime.enable", {}); | |
231 }, | |
232 | |
233 function log5(next) | |
234 { | |
235 logInWorker("message5", next); | |
236 }, | |
237 | |
238 function waitForMessage5(next) | |
239 { | |
240 waitForMessage("message5", next); | |
241 }, | |
242 | |
243 function stop2(next) | |
244 { | |
245 InspectorTest.log("Stopping worker"); | |
246 InspectorTest.evaluateInPage("stopWorker()", next); | |
247 }, | |
248 | |
249 function start3(next) | |
250 { | |
251 InspectorTest.log("Starting worker"); | |
252 InspectorTest.evaluateInPageAsync("startWorker()").then(next); | |
253 }, | |
254 | |
255 function log6(next) | |
256 { | |
257 logInWorker("message6", next); | |
258 }, | |
259 | |
260 function stop3(next) | |
261 { | |
262 InspectorTest.log("Stopping worker"); | |
263 InspectorTest.evaluateInPage("stopWorker()", next); | |
264 }, | |
265 | |
266 function disable2(next) | |
267 { | |
268 InspectorTest.log("Stopping autoattach"); | |
269 InspectorTest.sendCommandOrDie("Target.setAutoAttach", {autoAttach:
false, waitForDebuggerOnStart: false}, next); | |
270 } | |
271 ]; | |
272 | |
273 function runNextStep() | |
274 { | |
275 if (!steps.length) { | |
276 InspectorTest.completeTest(); | |
277 return; | |
278 } | |
279 var nextStep = steps.shift(); | |
280 InspectorTest.safeWrap(nextStep)(runNextStep); | |
281 } | |
282 | |
283 runNextStep(); | |
284 } | |
285 </script> | |
286 </head> | |
287 <body onload="runTest()"> | |
288 </body> | |
289 </html> | |
OLD | NEW |