2 * Copyright (C) 2012 Samsung Electronics. All rights reserved.
3 * Copyright (C) 2014 Apple Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 InspectorFrontendAPI = {};
29 InspectorTest._dispatchTable = [];
30 InspectorTest._requestId = -1;
31 InspectorTest.eventHandler = {};
34 * @param {string} method
35 * @param {object} params
36 * @param {function({object} messageObject)=} handler
38 InspectorTest.sendCommand = function(method, params, handler)
40 this._dispatchTable[++this._requestId] = handler;
42 var messageObject = { "method": method,
44 "id": this._requestId };
46 InspectorFrontendHost.sendMessageToBackend(JSON.stringify(messageObject));
48 return this._requestId;
52 * @param {object} messageObject
54 InspectorFrontendAPI.dispatchMessageAsync = function(messageObject)
56 var messageId = messageObject["id"];
57 if (typeof messageId === "number") {
58 var handler = InspectorTest._dispatchTable[messageId];
59 if (handler && typeof handler === "function")
60 handler(messageObject);
62 var eventName = messageObject["method"];
63 var eventHandler = InspectorTest.eventHandler[eventName];
65 eventHandler(messageObject);
66 else if (InspectorTest.defaultEventHandler)
67 InspectorTest.defaultEventHandler(messageObject);
72 * Registers an event handler for messages coming from the InspectorBackend.
73 * If multiple callbacks are registered for the same event, it will chain the execution.
74 * @param {string} event name
75 * @param {function} handler to be executed
76 * @param {boolean} execute the handler before all other handlers
78 InspectorTest.addEventListener = function(eventName, callback, capture)
80 if (!InspectorTest.eventHandler[eventName]) {
81 InspectorTest.eventHandler[eventName] = callback;
84 var firstHandler = InspectorTest.eventHandler[eventName];
85 var secondHandler = callback;
87 // Swap firstHandler with the new callback, so that we execute the callback first.
88 [firstHandler, secondHandler] = [secondHandler, firstHandler];
90 InspectorTest.eventHandler[eventName] = function(messageObject)
92 firstHandler(messageObject);
93 secondHandler(messageObject);
98 * Logs message to document.
99 * @param {string} message
101 InspectorTest.log = function(message)
103 this.sendCommand("Runtime.evaluate", { "expression": "log(" + JSON.stringify(message) + ")" } );
107 * Logs an assert message to document.
108 * @param {boolean} condition
109 * @param {string} message
111 InspectorTest.assert = function(condition, message)
113 var status = condition ? "PASS" : "FAIL";
114 this.sendCommand("Runtime.evaluate", { "expression": "log(" + JSON.stringify(status + ": " + message) + ")" } );
118 * Logs message directly to process stdout via alert function (hopefully followed by flush call).
119 * This message should survive process crash or kill by timeout.
120 * @param {string} message
122 InspectorTest.debugLog = function(message)
124 this.sendCommand("Runtime.evaluate", { "expression": "debugLog(" + JSON.stringify(message) + ")" } );
127 InspectorTest.completeTest = function()
129 this.sendCommand("Runtime.evaluate", { "expression": "closeTest();"} );
132 InspectorTest.checkForError = function(responseObject)
134 if (responseObject.error) {
135 InspectorTest.log("PROTOCOL ERROR: " + JSON.stringify(responseObject.error));
136 InspectorTest.completeTest();
137 throw "PROTOCOL ERROR";
142 * @param {string} scriptName
144 InspectorTest.importScript = function(scriptName)
146 var xhr = new XMLHttpRequest();
147 xhr.open("GET", scriptName, false);
149 if (xhr.status !== 0 && xhr.status !== 200)
150 throw new Error("Invalid script URL: " + scriptName);
151 var script = "try { " + xhr.responseText + "} catch (e) { alert(" + JSON.stringify("Error in: " + scriptName) + "); throw e; }";
155 InspectorTest.initializeInspectorModels = function()
157 // Catch any errors and finish the test early.
158 console.error = window.onerror = function()
160 InspectorTest.log(Array.prototype.join.call(arguments, ', '));
161 InspectorTest.completeTest();
164 console.assert = function(assertion, message)
168 InspectorTest.completeTest();
169 InspectorTest.log("ASSERT:" + message);
172 // Note: This function overwrites the InspectorFrontendAPI, so there's currently no
173 // way to intercept the messages from the backend.
175 var inspectorScripts = [
180 "InspectorFrontendAPI",
181 "InspectorFrontendHostStub",
182 "InspectorJSBackendCommands",
183 "InspectorWebBackendCommands",
191 "FrameResourceManager",
195 "SourceCodeRevision",
198 "ResourceCollection",
205 "ExecutionContextList",
212 // This corresponds to loading the scripts in Main.hml.
213 for (var i = 0; i < inspectorScripts.length; ++i)
214 InspectorTest.importScript("../../../../../Source/WebInspectorUI/UserInterface/" + inspectorScripts[i] + ".js");
216 // The initialization should be in sync with WebInspector.loaded in Main.js.
217 // FIXME: As soon as we can support all the observers and managers we should remove UI related tasks
218 // from WebInspector.loaded, so that it can be used from the LayoutTests.
220 InspectorFrontendHost.loaded();
223 InspectorAgent.enable();
225 InspectorBackend.registerInspectorDispatcher(new WebInspector.InspectorObserver);
226 InspectorBackend.registerPageDispatcher(new WebInspector.PageObserver);
227 InspectorBackend.registerDOMDispatcher(new WebInspector.DOMObserver);
228 InspectorBackend.registerCSSDispatcher(new WebInspector.CSSObserver);
229 if (InspectorBackend.registerRuntimeDispatcher)
230 InspectorBackend.registerRuntimeDispatcher(new WebInspector.RuntimeObserver);
232 WebInspector.frameResourceManager = new WebInspector.FrameResourceManager;
233 WebInspector.domTreeManager = new WebInspector.DOMTreeManager;
234 WebInspector.cssStyleManager = new WebInspector.CSSStyleManager;
235 WebInspector.runtimeManager = new WebInspector.RuntimeManager;
239 window.addEventListener("message", function(event) {
244 InspectorTest.completeTest();