OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 InspectorTest = {}; | |
6 InspectorTest._dispatchTable = new Map(); | |
7 InspectorTest._requestId = 0; | |
8 InspectorTest._dumpInspectorProtocolMessages = false; | |
9 InspectorTest.eventHandler = {}; | |
10 | |
11 InspectorTest.startDumpingProtocolMessages = function() | |
12 { | |
13 InspectorTest._dumpInspectorProtocolMessages = true; | |
14 } | |
15 | |
16 InspectorTest.sendCommand = function(method, params, handler) | |
17 { | |
18 var requestId = ++InspectorTest._requestId; | |
19 var messageObject = { "id": requestId, "method": method, "params": params }; | |
20 InspectorTest.sendRawCommand(requestId, JSON.stringify(messageObject), handler ); | |
21 } | |
22 | |
23 InspectorTest.sendRawCommand = function(requestId, command, handler) | |
24 { | |
25 if (InspectorTest._dumpInspectorProtocolMessages) | |
26 print("frontend: " + command); | |
27 InspectorTest._dispatchTable.set(requestId, handler); | |
28 sendMessageToBackend(command); | |
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 } | |
44 } | |
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 | |
86 InspectorTest.log = print.bind(null); | |
87 | |
88 InspectorTest.logObject = function(object, title) | |
89 { | |
90 var lines = []; | |
91 | |
92 function dumpValue(value, prefix, prefixWithName) | |
93 { | |
94 if (typeof value === "object" && value !== null) { | |
95 if (value instanceof Array) | |
96 dumpItems(value, prefix, prefixWithName); | |
97 else | |
98 dumpProperties(value, prefix, prefixWithName); | |
99 } else { | |
100 lines.push(prefixWithName + String(value).replace(/\n/g, " ")); | |
101 } | |
102 } | |
103 | |
104 function dumpProperties(object, prefix, firstLinePrefix) | |
105 { | |
106 prefix = prefix || ""; | |
107 firstLinePrefix = firstLinePrefix || prefix; | |
108 lines.push(firstLinePrefix + "{"); | |
109 | |
110 var propertyNames = Object.keys(object); | |
111 propertyNames.sort(); | |
112 for (var i = 0; i < propertyNames.length; ++i) { | |
113 var name = propertyNames[i]; | |
114 if (!object.hasOwnProperty(name)) | |
115 continue; | |
116 var prefixWithName = " " + prefix + name + " : "; | |
117 dumpValue(object[name], " " + prefix, prefixWithName); | |
118 } | |
119 lines.push(prefix + "}"); | |
120 } | |
121 | |
122 function dumpItems(object, prefix, firstLinePrefix) | |
123 { | |
124 prefix = prefix || ""; | |
125 firstLinePrefix = firstLinePrefix || prefix; | |
126 lines.push(firstLinePrefix + "["); | |
127 for (var i = 0; i < object.length; ++i) | |
128 dumpValue(object[i], " " + prefix, " " + prefix + "[" + i + "] : "); | |
129 lines.push(prefix + "]"); | |
130 } | |
131 | |
132 dumpValue(object, "", title); | |
133 InspectorTest.log(lines.join("\n")); | |
134 } | |
135 | |
136 InspectorTest.completeTest = quit.bind(null); | |
137 | |
138 InspectorTest.evaluateInPage = function(string, callback) | |
139 { | |
140 InspectorTest.sendCommand("Runtime.evaluate", { "expression": string }, functi on(message) { | |
141 if (message.error) | |
142 InspectorTest.log("Error while executing '" + string + "': " + message.err or.message); | |
dgozman
2016/09/30 00:44:52
Let's also completeTest.
kozy
2016/09/30 01:12:41
Done.
| |
143 else if (callback) | |
144 callback(message.result.result.value); | |
145 }); | |
146 }; | |
147 | |
148 InspectorTest.checkExpectation = function(fail, name, messageObject) | |
149 { | |
150 if (fail === !!messageObject.error) { | |
151 InspectorTest.log("PASS: " + name); | |
152 return true; | |
153 } | |
154 | |
155 InspectorTest.log("FAIL: " + name + ": " + JSON.stringify(messageObject)); | |
156 InspectorTest.completeTest(); | |
157 return false; | |
158 } | |
159 InspectorTest.expectedSuccess = InspectorTest.checkExpectation.bind(null, false) ; | |
160 InspectorTest.expectedError = InspectorTest.checkExpectation.bind(null, true); | |
161 | |
162 InspectorTest.runTestSuite = function(testSuite) | |
163 { | |
164 function nextTest() | |
165 { | |
166 if (!testSuite.length) { | |
167 InspectorTest.completeTest(); | |
168 return; | |
169 } | |
170 var fun = testSuite.shift(); | |
171 InspectorTest.log("\nRunning test: " + fun.name); | |
172 fun(nextTest); | |
173 } | |
174 nextTest(); | |
175 } | |
OLD | NEW |