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 = []; |
| 7 InspectorTest._requestId = -1; |
| 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 this._dispatchTable[++this._requestId] = handler; |
| 19 var messageObject = { "id": this._requestId, "method": method, "params": par
ams }; |
| 20 if (InspectorTest._dumpInspectorProtocolMessages) |
| 21 print("frontend: " + JSON.stringify(messageObject)); |
| 22 sendMessageToBackend(JSON.stringify(messageObject)); |
| 23 return this._requestId; |
| 24 } |
| 25 |
| 26 InspectorTest.sendCommandOrDie = function(command, properties, callback) |
| 27 { |
| 28 InspectorTest.sendCommand(command, properties || {}, commandCallback); |
| 29 function commandCallback(msg) |
| 30 { |
| 31 if (msg.error) { |
| 32 InspectorTest.log("ERROR: " + msg.error.message); |
| 33 InspectorTest.completeTest(); |
| 34 return; |
| 35 } |
| 36 if (callback) |
| 37 callback(msg.result); |
| 38 } |
| 39 } |
| 40 |
| 41 InspectorTest.sendCommandPromise = function(method, params) |
| 42 { |
| 43 var callback; |
| 44 var promise = new Promise(fulfill => callback = fulfill); |
| 45 InspectorTest.sendCommand(method, params, callback); |
| 46 return promise; |
| 47 } |
| 48 |
| 49 InspectorTest.waitForEventPromise = function(eventName) |
| 50 { |
| 51 return new Promise(fulfill => InspectorTest.eventHandler[eventName] = fullfi
llAndClearListener.bind(null, fulfill)); |
| 52 |
| 53 function fullfillAndClearListener(fulfill, result) |
| 54 { |
| 55 fulfill(result); |
| 56 delete InspectorTest.eventHandler[eventName]; |
| 57 } |
| 58 } |
| 59 |
| 60 InspectorTest.runTestSuite = function(testSuite) |
| 61 { |
| 62 function nextTest() |
| 63 { |
| 64 if (!testSuite.length) { |
| 65 InspectorTest.completeTest(); |
| 66 return; |
| 67 } |
| 68 var fun = testSuite.shift(); |
| 69 InspectorTest.log("\nRunning test: " + fun.name); |
| 70 fun(nextTest); |
| 71 } |
| 72 |
| 73 nextTest(); |
| 74 } |
| 75 |
| 76 /** |
| 77 * @param {string} command |
| 78 * @param {function({object} messageObject)=} handler |
| 79 */ |
| 80 InspectorTest.sendRawCommand = function(command, handler) |
| 81 { |
| 82 this._dispatchTable[++this._requestId] = handler; |
| 83 sendMessageToBackend(command); |
| 84 return this._requestId; |
| 85 } |
| 86 |
| 87 /** |
| 88 * @param {string|!Object} messageOrObject |
| 89 */ |
| 90 dispatchMessage = function(messageOrObject) |
| 91 { |
| 92 var messageObject = (typeof messageOrObject === "string" ? JSON.parse(messag
eOrObject) : messageOrObject); |
| 93 if (InspectorTest._dumpInspectorProtocolMessages) |
| 94 print("backend: " + JSON.stringify(messageObject)); |
| 95 var messageId = messageObject["id"]; |
| 96 try { |
| 97 if (typeof messageId === "number") { |
| 98 var handler = InspectorTest._dispatchTable[messageId]; |
| 99 if (handler && typeof handler === "function") { |
| 100 handler(messageObject); |
| 101 } |
| 102 } else { |
| 103 var eventName = messageObject["method"]; |
| 104 var eventHandler = InspectorTest.eventHandler[eventName]; |
| 105 if (eventHandler) { |
| 106 eventHandler(messageObject); |
| 107 } |
| 108 } |
| 109 } catch(e) { |
| 110 InspectorTest.log("Exception when dispatching message: " + e + "\n" + e.
stack + "\n message = " + JSON.stringify(messageObject, null, 2)); |
| 111 InspectorTest.completeTest(); |
| 112 } |
| 113 } |
| 114 |
| 115 /** |
| 116 * Logs message to document. |
| 117 * @param {string} message |
| 118 */ |
| 119 InspectorTest.log = function(message) |
| 120 { |
| 121 print(message); |
| 122 } |
| 123 |
| 124 /** |
| 125 * Formats and logs object. |
| 126 * @param {Object} object |
| 127 * @param {string=} title |
| 128 */ |
| 129 InspectorTest.logObject = function(object, title) |
| 130 { |
| 131 var lines = []; |
| 132 |
| 133 function dumpValue(value, prefix, prefixWithName) |
| 134 { |
| 135 if (typeof value === "object" && value !== null) { |
| 136 if (value instanceof Array) |
| 137 dumpItems(value, prefix, prefixWithName); |
| 138 else |
| 139 dumpProperties(value, prefix, prefixWithName); |
| 140 } else { |
| 141 lines.push(prefixWithName + String(value).replace(/\n/g, " ")); |
| 142 } |
| 143 } |
| 144 |
| 145 function dumpProperties(object, prefix, firstLinePrefix) |
| 146 { |
| 147 prefix = prefix || ""; |
| 148 firstLinePrefix = firstLinePrefix || prefix; |
| 149 lines.push(firstLinePrefix + "{"); |
| 150 |
| 151 var propertyNames = Object.keys(object); |
| 152 propertyNames.sort(); |
| 153 for (var i = 0; i < propertyNames.length; ++i) { |
| 154 var name = propertyNames[i]; |
| 155 if (!object.hasOwnProperty(name)) |
| 156 continue; |
| 157 var prefixWithName = " " + prefix + name + " : "; |
| 158 dumpValue(object[name], " " + prefix, prefixWithName); |
| 159 } |
| 160 lines.push(prefix + "}"); |
| 161 } |
| 162 |
| 163 function dumpItems(object, prefix, firstLinePrefix) |
| 164 { |
| 165 prefix = prefix || ""; |
| 166 firstLinePrefix = firstLinePrefix || prefix; |
| 167 lines.push(firstLinePrefix + "["); |
| 168 for (var i = 0; i < object.length; ++i) |
| 169 dumpValue(object[i], " " + prefix, " " + prefix + "[" + i + "]
: "); |
| 170 lines.push(prefix + "]"); |
| 171 } |
| 172 |
| 173 dumpValue(object, "", title); |
| 174 InspectorTest.log(lines.join("\n")); |
| 175 } |
| 176 |
| 177 InspectorTest.completeTest = function() |
| 178 { |
| 179 quit(); |
| 180 } |
| 181 |
| 182 /** |
| 183 * Evaluates string in page. |
| 184 * @param {string} message |
| 185 * @param {!function} callback |
| 186 */ |
| 187 InspectorTest.evaluateInPage = function(string, callback) |
| 188 { |
| 189 this.sendCommand("Runtime.evaluate", { "expression": string }, function(mess
age) { |
| 190 if (message.error) |
| 191 InspectorTest.log("Error while executing '" + string + "': " + messa
ge.error.message); |
| 192 else if (callback) |
| 193 callback(message.result.result.value); |
| 194 }); |
| 195 }; |
| 196 |
| 197 InspectorTest.checkExpectation = function(fail, name, messageObject) |
| 198 { |
| 199 if (fail === !!messageObject.error) { |
| 200 InspectorTest.log("PASS: " + name); |
| 201 return true; |
| 202 } |
| 203 |
| 204 InspectorTest.log("FAIL: " + name + ": " + JSON.stringify(messageObject)); |
| 205 InspectorTest.completeTest(); |
| 206 return false; |
| 207 } |
| 208 InspectorTest.expectedSuccess = InspectorTest.checkExpectation.bind(null, false)
; |
| 209 InspectorTest.expectedError = InspectorTest.checkExpectation.bind(null, true); |
OLD | NEW |