2 Copyright (C) 2012 Samsung Electronics. All rights reserved.
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
8 1. Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
14 THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 InspectorFrontendAPI = {};
32 InspectorTest._dispatchTable = [];
33 InspectorTest._requestId = -1;
34 InspectorTest.eventHandler = {};
37 * @param {string} method
38 * @param {object} params
39 * @param {function({object} messageObject)=} handler
41 InspectorTest.sendCommand = function(method, params, handler)
43 this._dispatchTable[++this._requestId] = handler;
45 var messageObject = { "method": method,
47 "id": this._requestId };
49 InspectorFrontendHost.sendMessageToBackend(JSON.stringify(messageObject));
51 return this._requestId;
54 InspectorTest.sendCommandOrDie = function(command, properties, callback)
56 InspectorTest.sendCommand(command, properties || {}, commandCallback);
57 function commandCallback(msg)
60 InspectorTest.log("ERROR: " + msg.error.message);
61 InspectorTest.completeTest();
69 InspectorTest.domUndo = function(callback)
71 InspectorTest.sendCommandOrDie("DOM.undo", {}, callback);
74 InspectorTest.undoAndNext = function(next)
76 return InspectorTest.domUndo.bind(InspectorTest, next);
79 InspectorTest.runTestSuite = function(testSuite)
83 if (!testSuite.length) {
84 InspectorTest.completeTest();
87 var fun = testSuite.shift();
88 InspectorTest.log("\nRunning test: " + fun.name);
96 * @param {function(object)=} callback
98 InspectorTest.wrapCallback = function(callback)
101 * @param {object} message
103 function callbackWrapper(message)
105 if (InspectorTest.completeTestIfError(message))
110 callback(message["result"]);
112 InspectorTest.log("Exception " + e + " while invoking callback: " + callback);
113 InspectorTest.completeTest();
116 return callbackWrapper;
120 * @param {string} command
121 * @param {function({object} messageObject)=} handler
123 InspectorTest.sendRawCommand = function(command, handler)
125 this._dispatchTable[++this._requestId] = handler;
126 InspectorFrontendHost.sendMessageToBackend(command);
127 return this._requestId;
131 * @param {string} message
133 InspectorFrontendAPI.dispatchMessage = function(message)
135 var messageObject = JSON.parse(message);
136 var messageId = messageObject["id"];
138 if (typeof messageId === "number") {
139 var handler = InspectorTest._dispatchTable[messageId];
140 if (handler && typeof handler === "function")
141 handler(messageObject);
143 var eventName = messageObject["method"];
144 var eventHandler = InspectorTest.eventHandler[eventName];
146 eventHandler(messageObject);
149 InspectorTest.log("Exception when dispatching message: " + e + "\n\n message = " + JSON.stringify(messageObject, null, 2));
150 InspectorTest.completeTest();
155 * Logs message to document.
156 * @param {string} message
158 InspectorTest.log = function(message)
160 this.sendCommand("Runtime.evaluate", { "expression": "log(" + JSON.stringify(message) + ")" } );
164 * Formats and logs object to document.
165 * @param {Object} object
166 * @param {string=} title
168 InspectorTest.logObject = function(object, title)
172 function dumpValue(value, prefix, prefixWithName)
174 if (typeof value === "object" && value !== null) {
175 if (value instanceof Array)
176 dumpItems(value, prefix, prefixWithName);
178 dumpProperties(value, prefix, prefixWithName);
180 lines.push(prefixWithName + String(value).replace(/\n/g, " "));
184 function dumpProperties(object, prefix, firstLinePrefix)
186 prefix = prefix || "";
187 firstLinePrefix = firstLinePrefix || prefix;
188 lines.push(firstLinePrefix + "{");
190 var propertyNames = Object.keys(object);
191 propertyNames.sort();
192 for (var i = 0; i < propertyNames.length; ++i) {
193 var name = propertyNames[i];
194 if (typeof object.hasOwnProperty === "function" && !object.hasOwnProperty(name))
196 var prefixWithName = " " + prefix + name + " : ";
197 dumpValue(object[name], " " + prefix, prefixWithName);
199 lines.push(prefix + "}");
202 function dumpItems(object, prefix, firstLinePrefix)
204 prefix = prefix || "";
205 firstLinePrefix = firstLinePrefix || prefix;
206 lines.push(firstLinePrefix + "[");
207 for (var i = 0; i < object.length; ++i)
208 dumpValue(object[i], " " + prefix, " " + prefix + "[" + i + "] : ");
209 lines.push(prefix + "]");
212 dumpValue(object, "", title);
213 InspectorTest.log(lines.join("\n"));
217 * Logs message directly to process stdout via alert function (hopefully followed by flush call).
218 * This message should survive process crash or kill by timeout.
219 * @param {string} message
221 InspectorTest.debugLog = function(message)
223 this.sendCommand("Runtime.evaluate", { "expression": "debugLog(" + JSON.stringify(message) + ")" } );
226 InspectorTest.completeTest = function()
228 this.sendCommand("Runtime.evaluate", { "expression": "closeTest();"} );
231 InspectorTest.completeTestIfError = function(messageObject)
233 if (messageObject.error) {
234 InspectorTest.log(messageObject.error.message);
235 InspectorTest.completeTest();
241 InspectorTest.checkExpectation = function(fail, name, messageObject)
243 if (fail === !!messageObject.error) {
244 InspectorTest.log("PASS: " + name);
248 InspectorTest.log("FAIL: " + name + ": " + JSON.stringify(messageObject));
249 InspectorTest.completeTest();
252 InspectorTest.expectedSuccess = InspectorTest.checkExpectation.bind(null, false);
253 InspectorTest.expectedError = InspectorTest.checkExpectation.bind(null, true);
255 InspectorTest.assert = function(condition, message)
259 InspectorTest.log("FAIL: assertion failed: " + message);
260 InspectorTest.completeTest();
263 InspectorTest.assertEquals = function(expected, actual, message)
265 if (expected === actual)
267 InspectorTest.assert(false, "expected: `" + expected + "', actual: `" + actual + "'" + (message ? ", " + message : ""));
271 * @param {string} scriptName
273 InspectorTest.importScript = function(scriptName)
275 var xhr = new XMLHttpRequest();
276 xhr.open("GET", scriptName, false);
278 window.eval(xhr.responseText + "\n//@ sourceURL=" + scriptName);
282 InspectorTest.eventHandler["Inspector.evaluateForTestInFrontend"] = function(message)
285 eval(message.params.script);
287 InspectorTest.log("FAIL: exception in evaluateForTestInFrontend: " + e);
288 InspectorTest.completeTest();
292 function enableInspectorAgent()
294 InspectorTest.sendCommand("Inspector.enable", { });
297 window.addEventListener("load", enableInspectorAgent, false);