OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 InspectorTest = {}; | 5 InspectorTest = {}; |
6 InspectorTest._dispatchTable = new Map(); | 6 InspectorTest._dispatchTable = new Map(); |
7 InspectorTest._requestId = 0; | 7 InspectorTest._requestId = 0; |
8 InspectorTest._dumpInspectorProtocolMessages = false; | 8 InspectorTest._dumpInspectorProtocolMessages = false; |
9 InspectorTest.eventHandler = {}; | 9 InspectorTest._eventHandler = {}; |
10 | 10 |
11 InspectorTest.startDumpingProtocolMessages = function() | 11 Protocol = new Proxy({}, { |
12 { | 12 get: function(target, agentName, receiver) { |
13 InspectorTest._dumpInspectorProtocolMessages = true; | 13 return new Proxy({}, { |
14 } | 14 get: function(target, methodName, receiver) { |
15 | 15 const eventPattern = /^on(ce)?([A-Z][A-Za-z0-9]+)/; |
16 InspectorTest.sendCommand = function(method, params, handler) | 16 var match = eventPattern.exec(methodName); |
17 { | 17 if (!match) { |
18 var requestId = ++InspectorTest._requestId; | 18 return (args) => InspectorTest._sendCommandPromise(`${agentName}.${met
hodName}`, args || {}); |
19 var messageObject = { "id": requestId, "method": method, "params": params }; | 19 } else { |
20 InspectorTest.sendRawCommand(requestId, JSON.stringify(messageObject), handler
); | 20 var eventName = match[2]; |
21 } | 21 eventName = eventName.charAt(0).toLowerCase() + eventName.slice(1); |
22 | 22 if (match[1]) |
23 InspectorTest.sendRawCommand = function(requestId, command, handler) | 23 return (args) => InspectorTest._waitForEventPromise(`${agentName}.${
eventName}`, args || {}); |
24 { | 24 else |
25 if (InspectorTest._dumpInspectorProtocolMessages) | 25 return (listener) => { InspectorTest._eventHandler[`${agentName}.${e
ventName}`] = listener }; |
26 print("frontend: " + command); | 26 } |
27 InspectorTest._dispatchTable.set(requestId, handler); | 27 } |
28 sendMessageToBackend(command); | 28 }); |
29 } | |
30 | |
31 InspectorTest.sendCommandOrDie = function(command, properties, callback) | |
32 { | |
33 InspectorTest.sendCommand(command, properties, commandCallback); | |
34 function commandCallback(msg) | |
35 { | |
36 if (msg.error) { | |
37 InspectorTest.log("ERROR: " + msg.error.message); | |
38 InspectorTest.completeTest(); | |
39 return; | |
40 } | |
41 if (callback) | |
42 callback(msg.result); | |
43 } | 29 } |
44 } | 30 }); |
45 | |
46 InspectorTest.sendCommandPromise = function(method, params) | |
47 { | |
48 return new Promise(fulfill => InspectorTest.sendCommand(method, params, fulfil
l)); | |
49 } | |
50 | |
51 InspectorTest.waitForEventPromise = function(eventName) | |
52 { | |
53 return new Promise(fulfill => InspectorTest.eventHandler[eventName] = fullfill
AndClearListener.bind(null, fulfill)); | |
54 | |
55 function fullfillAndClearListener(fulfill, result) | |
56 { | |
57 delete InspectorTest.eventHandler[eventName]; | |
58 fulfill(result); | |
59 } | |
60 } | |
61 | |
62 InspectorTest.dispatchMessage = function(messageObject) | |
63 { | |
64 if (InspectorTest._dumpInspectorProtocolMessages) | |
65 print("backend: " + JSON.stringify(messageObject)); | |
66 try { | |
67 var messageId = messageObject["id"]; | |
68 if (typeof messageId === "number") { | |
69 var handler = InspectorTest._dispatchTable.get(messageId); | |
70 if (handler) { | |
71 handler(messageObject); | |
72 InspectorTest._dispatchTable.delete(messageId); | |
73 } | |
74 } else { | |
75 var eventName = messageObject["method"]; | |
76 var eventHandler = InspectorTest.eventHandler[eventName]; | |
77 if (eventHandler) | |
78 eventHandler(messageObject); | |
79 } | |
80 } catch (e) { | |
81 InspectorTest.log("Exception when dispatching message: " + e + "\n" + e.stac
k + "\n message = " + JSON.stringify(messageObject, null, 2)); | |
82 InspectorTest.completeTest(); | |
83 } | |
84 } | |
85 | 31 |
86 InspectorTest.log = print.bind(null); | 32 InspectorTest.log = print.bind(null); |
87 | 33 |
| 34 InspectorTest.logMessage = function(message) |
| 35 { |
| 36 if (message.id) |
| 37 message.id = "<messageId>"; |
| 38 |
| 39 const nonStableFields = new Set(["objectId", "scriptId", "exceptionId", "times
tamp"]); |
| 40 var objects = [ message ]; |
| 41 while (objects.length) { |
| 42 var object = objects.shift(); |
| 43 for (var key in object) { |
| 44 if (nonStableFields.has(key)) |
| 45 object[key] = `<${key}>`; |
| 46 else if (typeof object[key] === "object") |
| 47 objects.push(object[key]); |
| 48 } |
| 49 } |
| 50 |
| 51 InspectorTest.logObject(message); |
| 52 return message; |
| 53 } |
| 54 |
88 InspectorTest.logObject = function(object, title) | 55 InspectorTest.logObject = function(object, title) |
89 { | 56 { |
90 var lines = []; | 57 var lines = []; |
91 | 58 |
92 function dumpValue(value, prefix, prefixWithName) | 59 function dumpValue(value, prefix, prefixWithName) |
93 { | 60 { |
94 if (typeof value === "object" && value !== null) { | 61 if (typeof value === "object" && value !== null) { |
95 if (value instanceof Array) | 62 if (value instanceof Array) |
96 dumpItems(value, prefix, prefixWithName); | 63 dumpItems(value, prefix, prefixWithName); |
97 else | 64 else |
(...skipping 28 matching lines...) Expand all Loading... |
126 lines.push(firstLinePrefix + "["); | 93 lines.push(firstLinePrefix + "["); |
127 for (var i = 0; i < object.length; ++i) | 94 for (var i = 0; i < object.length; ++i) |
128 dumpValue(object[i], " " + prefix, " " + prefix + "[" + i + "] : "); | 95 dumpValue(object[i], " " + prefix, " " + prefix + "[" + i + "] : "); |
129 lines.push(prefix + "]"); | 96 lines.push(prefix + "]"); |
130 } | 97 } |
131 | 98 |
132 dumpValue(object, "", title); | 99 dumpValue(object, "", title); |
133 InspectorTest.log(lines.join("\n")); | 100 InspectorTest.log(lines.join("\n")); |
134 } | 101 } |
135 | 102 |
136 InspectorTest.logMessage = function(message) | |
137 { | |
138 if (message.id) | |
139 message.id = 0; | |
140 | |
141 const nonStableFields = new Set(["objectId", "scriptId", "exceptionId"]); | |
142 var objects = [ message ]; | |
143 while (objects.length) { | |
144 var object = objects.shift(); | |
145 for (var key in object) { | |
146 if (nonStableFields.has(key)) | |
147 object[key] = `<${key}>`; | |
148 else if (typeof object[key] === "object") | |
149 objects.push(object[key]); | |
150 } | |
151 } | |
152 | |
153 InspectorTest.logObject(message); | |
154 return message; | |
155 } | |
156 | |
157 InspectorTest.completeTest = quit.bind(null); | 103 InspectorTest.completeTest = quit.bind(null); |
158 | 104 |
159 InspectorTest.completeTestAfterPendingTimeouts = function() | 105 InspectorTest.completeTestAfterPendingTimeouts = function() |
160 { | 106 { |
161 InspectorTest.sendCommand("Runtime.evaluate", { | 107 InspectorTest.sendCommand("Runtime.evaluate", { |
162 expression: "new Promise(resolve => setTimeout(resolve, 0))", | 108 expression: "new Promise(resolve => setTimeout(resolve, 0))", |
163 awaitPromise: true }, InspectorTest.completeTest); | 109 awaitPromise: true }, InspectorTest.completeTest); |
164 } | 110 } |
165 | 111 |
166 InspectorTest.evaluateInPage = function(string, callback) | 112 InspectorTest.addScript = function(string) |
167 { | 113 { |
168 InspectorTest.sendCommand("Runtime.evaluate", { "expression": string }, functi
on(message) { | 114 return InspectorTest._sendCommandPromise("Runtime.evaluate", { "expression": s
tring }).then(dumpErrorIfNeeded); |
| 115 |
| 116 function dumpErrorIfNeeded(message) |
| 117 { |
169 if (message.error) { | 118 if (message.error) { |
170 InspectorTest.log("Error while executing '" + string + "': " + message.err
or.message); | 119 InspectorTest.log("Error while executing '" + string + "': " + message.err
or.message); |
171 InspectorTest.completeTest(); | 120 InspectorTest.completeTest(); |
172 } | 121 } |
173 else if (callback) | 122 } |
174 callback(message.result.result.value); | |
175 }); | |
176 }; | 123 }; |
177 | 124 |
| 125 InspectorTest.startDumpingProtocolMessages = function() |
| 126 { |
| 127 InspectorTest._dumpInspectorProtocolMessages = true; |
| 128 } |
| 129 |
| 130 InspectorTest.sendRawCommand = function(requestId, command, handler) |
| 131 { |
| 132 if (InspectorTest._dumpInspectorProtocolMessages) |
| 133 print("frontend: " + command); |
| 134 InspectorTest._dispatchTable.set(requestId, handler); |
| 135 sendMessageToBackend(command); |
| 136 } |
| 137 |
178 InspectorTest.checkExpectation = function(fail, name, messageObject) | 138 InspectorTest.checkExpectation = function(fail, name, messageObject) |
179 { | 139 { |
180 if (fail === !!messageObject.error) { | 140 if (fail === !!messageObject.error) { |
181 InspectorTest.log("PASS: " + name); | 141 InspectorTest.log("PASS: " + name); |
182 return true; | 142 return true; |
183 } | 143 } |
184 | 144 |
185 InspectorTest.log("FAIL: " + name + ": " + JSON.stringify(messageObject)); | 145 InspectorTest.log("FAIL: " + name + ": " + JSON.stringify(messageObject)); |
186 InspectorTest.completeTest(); | 146 InspectorTest.completeTest(); |
187 return false; | 147 return false; |
188 } | 148 } |
189 InspectorTest.expectedSuccess = InspectorTest.checkExpectation.bind(null, false)
; | 149 InspectorTest.expectedSuccess = InspectorTest.checkExpectation.bind(null, false)
; |
190 InspectorTest.expectedError = InspectorTest.checkExpectation.bind(null, true); | 150 InspectorTest.expectedError = InspectorTest.checkExpectation.bind(null, true); |
191 | 151 |
192 InspectorTest.runTestSuite = function(testSuite) | 152 InspectorTest.runTestSuite = function(testSuite) |
193 { | 153 { |
194 function nextTest() | 154 function nextTest() |
195 { | 155 { |
196 if (!testSuite.length) { | 156 if (!testSuite.length) { |
197 InspectorTest.completeTest(); | 157 InspectorTest.completeTest(); |
198 return; | 158 return; |
199 } | 159 } |
200 var fun = testSuite.shift(); | 160 var fun = testSuite.shift(); |
201 InspectorTest.log("\nRunning test: " + fun.name); | 161 InspectorTest.log("\nRunning test: " + fun.name); |
202 fun(nextTest); | 162 fun(nextTest); |
203 } | 163 } |
204 nextTest(); | 164 nextTest(); |
205 } | 165 } |
| 166 |
| 167 InspectorTest._sendCommandPromise = function(method, params) |
| 168 { |
| 169 var requestId = ++InspectorTest._requestId; |
| 170 var messageObject = { "id": requestId, "method": method, "params": params }; |
| 171 var fulfillCallback; |
| 172 var promise = new Promise(fulfill => fulfillCallback = fulfill); |
| 173 InspectorTest.sendRawCommand(requestId, JSON.stringify(messageObject), fulfill
Callback); |
| 174 return promise; |
| 175 } |
| 176 |
| 177 InspectorTest._waitForEventPromise = function(eventName) |
| 178 { |
| 179 return new Promise(fulfill => InspectorTest._eventHandler[eventName] = fullfil
lAndClearListener.bind(null, fulfill)); |
| 180 |
| 181 function fullfillAndClearListener(fulfill, result) |
| 182 { |
| 183 delete InspectorTest._eventHandler[eventName]; |
| 184 fulfill(result); |
| 185 } |
| 186 } |
| 187 |
| 188 InspectorTest._dispatchMessage = function(messageObject) |
| 189 { |
| 190 if (InspectorTest._dumpInspectorProtocolMessages) |
| 191 print("backend: " + JSON.stringify(messageObject)); |
| 192 try { |
| 193 var messageId = messageObject["id"]; |
| 194 if (typeof messageId === "number") { |
| 195 var handler = InspectorTest._dispatchTable.get(messageId); |
| 196 if (handler) { |
| 197 handler(messageObject); |
| 198 InspectorTest._dispatchTable.delete(messageId); |
| 199 } |
| 200 } else { |
| 201 var eventName = messageObject["method"]; |
| 202 var eventHandler = InspectorTest._eventHandler[eventName]; |
| 203 if (eventHandler) |
| 204 eventHandler(messageObject); |
| 205 } |
| 206 } catch (e) { |
| 207 InspectorTest.log("Exception when dispatching message: " + e + "\n" + e.stac
k + "\n message = " + JSON.stringify(messageObject, null, 2)); |
| 208 InspectorTest.completeTest(); |
| 209 } |
| 210 } |
OLD | NEW |