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); |
| 143 InspectorTest.completeTest(); |
| 144 } |
| 145 else if (callback) |
| 146 callback(message.result.result.value); |
| 147 }); |
| 148 }; |
| 149 |
| 150 InspectorTest.checkExpectation = function(fail, name, messageObject) |
| 151 { |
| 152 if (fail === !!messageObject.error) { |
| 153 InspectorTest.log("PASS: " + name); |
| 154 return true; |
| 155 } |
| 156 |
| 157 InspectorTest.log("FAIL: " + name + ": " + JSON.stringify(messageObject)); |
| 158 InspectorTest.completeTest(); |
| 159 return false; |
| 160 } |
| 161 InspectorTest.expectedSuccess = InspectorTest.checkExpectation.bind(null, false)
; |
| 162 InspectorTest.expectedError = InspectorTest.checkExpectation.bind(null, true); |
| 163 |
| 164 InspectorTest.runTestSuite = function(testSuite) |
| 165 { |
| 166 function nextTest() |
| 167 { |
| 168 if (!testSuite.length) { |
| 169 InspectorTest.completeTest(); |
| 170 return; |
| 171 } |
| 172 var fun = testSuite.shift(); |
| 173 InspectorTest.log("\nRunning test: " + fun.name); |
| 174 fun(nextTest); |
| 175 } |
| 176 nextTest(); |
| 177 } |
OLD | NEW |