OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2012 Samsung Electronics. All rights reserved. | |
3 * Copyright (C) 2013 Google Inc. All rights reserved. | |
4 * | |
5 * Redistribution and use in source and binary forms, with or without | |
6 * modification, are permitted provided that the following conditions | |
7 * are met: | |
8 * | |
9 * 1. Redistributions of source code must retain the above copyright | |
10 * notice, this list of conditions and the following disclaimer. | |
11 * 2. Redistributions in binary form must reproduce the above copyright | |
12 * notice, this list of conditions and the following disclaimer in the | |
13 * documentation and/or other materials provided with the distribution. | |
14 * | |
15 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY | |
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
18 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY | |
19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
22 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
25 */ | |
26 InspectorFrontendAPI = {}; | |
27 | |
28 InspectorTest = {}; | |
29 InspectorTest._dispatchTable = []; | |
30 InspectorTest._requestId = -1; | |
31 InspectorTest.eventHandler = {}; | |
32 | |
33 /** | |
34 * @param {string} method | |
35 * @param {object} params | |
36 * @param {function({object} messageObject)=} handler | |
37 */ | |
38 InspectorTest.sendCommand = function(method, params, handler) | |
39 { | |
40 this._dispatchTable[++this._requestId] = handler; | |
41 | |
42 var messageObject = { "method": method, | |
43 "params": params, | |
44 "id": this._requestId }; | |
45 | |
46 InspectorFrontendHost.sendMessageToBackend(JSON.stringify(messageObject)); | |
47 | |
48 return this._requestId; | |
49 } | |
50 | |
51 /** | |
52 * @param {function(object)=} callback | |
53 */ | |
54 InspectorTest.wrapCallback = function(callback) | |
55 { | |
56 /** | |
57 * @param {object} message | |
58 */ | |
59 function callbackWrapper(message) | |
60 { | |
61 if (InspectorTest.completeTestIfError(message)) | |
62 return; | |
63 if (!callback) | |
64 return; | |
65 try { | |
66 callback(message["result"]); | |
67 } catch (e) { | |
68 InspectorTest.log("Exception " + e + " while invoking callback: " +
callback); | |
69 InspectorTest.completeTest(); | |
70 } | |
71 } | |
72 return callbackWrapper; | |
73 } | |
74 | |
75 /** | |
76 * @param {string} command | |
77 * @param {function({object} messageObject)=} handler | |
78 */ | |
79 InspectorTest.sendRawCommand = function(command, handler) | |
80 { | |
81 this._dispatchTable[++this._requestId] = handler; | |
82 InspectorFrontendHost.sendMessageToBackend(command); | |
83 return this._requestId; | |
84 } | |
85 | |
86 /** | |
87 * @param {object} messageObject | |
88 */ | |
89 InspectorFrontendAPI.dispatchMessageAsync = function(messageObject) | |
90 { | |
91 var messageId = messageObject["id"]; | |
92 if (typeof messageId === "number") { | |
93 var handler = InspectorTest._dispatchTable[messageId]; | |
94 if (handler && typeof handler === "function") | |
95 handler(messageObject); | |
96 } else { | |
97 var eventName = messageObject["method"]; | |
98 var eventHandler = InspectorTest.eventHandler[eventName]; | |
99 if (eventHandler) | |
100 eventHandler(messageObject); | |
101 } | |
102 } | |
103 | |
104 /** | |
105 * Logs message to document. | |
106 * @param {string} message | |
107 */ | |
108 InspectorTest.log = function(message) | |
109 { | |
110 this.sendCommand("Runtime.evaluate", { "expression": "log(" + JSON.stringify
(message) + ")" } ); | |
111 } | |
112 | |
113 /** | |
114 * Formats and logs object to document. | |
115 * @param {Object} object | |
116 * @param {string=} title | |
117 */ | |
118 InspectorTest.logObject = function(object, title) | |
119 { | |
120 var lines = []; | |
121 | |
122 function dumpValue(value, prefix, prefixWithName) | |
123 { | |
124 if (typeof value === "object" && value !== null) { | |
125 if (value instanceof Array) | |
126 dumpItems(value, prefix, prefixWithName); | |
127 else | |
128 dumpProperties(value, prefix, prefixWithName); | |
129 } else { | |
130 lines.push(prefixWithName + String(value).replace(/\n/g, " ")); | |
131 } | |
132 } | |
133 | |
134 function dumpProperties(object, prefix, firstLinePrefix) | |
135 { | |
136 prefix = prefix || ""; | |
137 firstLinePrefix = firstLinePrefix || prefix; | |
138 lines.push(firstLinePrefix + "{"); | |
139 | |
140 var propertyNames = Object.keys(object); | |
141 propertyNames.sort(); | |
142 for (var i = 0; i < propertyNames.length; ++i) { | |
143 var name = propertyNames[i]; | |
144 if (typeof object.hasOwnProperty === "function" && !object.hasOwnPro
perty(name)) | |
145 continue; | |
146 var prefixWithName = " " + prefix + name + " : "; | |
147 dumpValue(object[name], " " + prefix, prefixWithName); | |
148 } | |
149 lines.push(prefix + "}"); | |
150 } | |
151 | |
152 function dumpItems(object, prefix, firstLinePrefix) | |
153 { | |
154 prefix = prefix || ""; | |
155 firstLinePrefix = firstLinePrefix || prefix; | |
156 lines.push(firstLinePrefix + "["); | |
157 for (var i = 0; i < object.length; ++i) | |
158 dumpValue(object[i], " " + prefix, " " + prefix + "[" + i + "]
: "); | |
159 lines.push(prefix + "]"); | |
160 } | |
161 | |
162 dumpValue(object, "", title); | |
163 InspectorTest.log(lines.join("\n")); | |
164 } | |
165 | |
166 /** | |
167 * Logs message directly to process stdout via alert function (hopefully followed
by flush call). | |
168 * This message should survive process crash or kill by timeout. | |
169 * @param {string} message | |
170 */ | |
171 InspectorTest.debugLog = function(message) | |
172 { | |
173 this.sendCommand("Runtime.evaluate", { "expression": "debugLog(" + JSON.stri
ngify(message) + ")" } ); | |
174 } | |
175 | |
176 InspectorTest.completeTest = function() | |
177 { | |
178 this.sendCommand("Runtime.evaluate", { "expression": "closeTest();"} ); | |
179 } | |
180 | |
181 InspectorTest.completeTestIfError = function(messageObject) | |
182 { | |
183 if (messageObject.error) { | |
184 InspectorTest.log(messageObject.error.message); | |
185 InspectorTest.completeTest(); | |
186 return true; | |
187 } | |
188 return false; | |
189 } | |
190 | |
191 InspectorTest.checkExpectation = function(fail, name, messageObject) | |
192 { | |
193 if (fail === !!messageObject.error) { | |
194 InspectorTest.log("PASS: " + name); | |
195 return true; | |
196 } | |
197 | |
198 InspectorTest.log("FAIL: " + name + ": " + JSON.stringify(messageObject)); | |
199 InspectorTest.completeTest(); | |
200 return false; | |
201 } | |
202 InspectorTest.expectedSuccess = InspectorTest.checkExpectation.bind(null, false)
; | |
203 InspectorTest.expectedError = InspectorTest.checkExpectation.bind(null, true); | |
204 | |
205 InspectorTest.assert = function(condition, message) | |
206 { | |
207 if (condition) | |
208 return; | |
209 InspectorTest.log("FAIL: assertion failed: " + message); | |
210 InspectorTest.completeTest(); | |
211 } | |
212 | |
213 InspectorTest.assertEquals = function(expected, actual, message) | |
214 { | |
215 if (expected === actual) | |
216 return; | |
217 InspectorTest,assert(false, "expected: `" + expected + "', actual: `" + actu
al + "'" + (message ? ", " + message : "")); | |
218 } | |
219 | |
220 /** | |
221 * @param {string} scriptName | |
222 */ | |
223 InspectorTest.importScript = function(scriptName) | |
224 { | |
225 var xhr = new XMLHttpRequest(); | |
226 xhr.open("GET", scriptName, false); | |
227 xhr.send(null); | |
228 window.eval(xhr.responseText + "\n//@ sourceURL=" + scriptName); | |
229 } | |
230 | |
231 window.addEventListener("message", function(event) { | |
232 try { | |
233 eval(event.data); | |
234 } catch (e) { | |
235 alert(e.stack); | |
236 InspectorTest.completeTest(); | |
237 throw e; | |
238 } | |
239 }); | |
OLD | NEW |