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.
25 InspectorFrontendAPI = {};
28 InspectorTest._dispatchTable = [];
29 InspectorTest._requestId = -1;
30 InspectorTest.eventHandler = {};
33 * @param {string} method
34 * @param {object} params
35 * @param {function({object} messageObject)=} handler
37 InspectorTest.sendCommand = function(method, params, handler)
39 this._dispatchTable[++this._requestId] = handler;
41 var messageObject = { "method": method,
43 "id": this._requestId };
45 InspectorFrontendHost.sendMessageToBackend(JSON.stringify(messageObject));
47 return this._requestId;
51 * @param {object} messageObject
53 InspectorFrontendAPI.dispatchMessageAsync = function(messageObject)
55 var messageId = messageObject["id"];
56 if (typeof messageId === "number") {
57 var handler = InspectorTest._dispatchTable[messageId];
58 if (handler && typeof handler === "function")
59 handler(messageObject);
61 var eventName = messageObject["method"];
62 var eventHandler = InspectorTest.eventHandler[eventName];
64 eventHandler(messageObject);
69 * Logs message to document.
70 * @param {string} message
72 InspectorTest.log = function(message)
74 this.sendCommand("Runtime.evaluate", { "expression": "log(" + JSON.stringify(message) + ")" } );
78 * Logs an assert message to document.
79 * @param {boolean} condition
80 * @param {string} message
82 InspectorTest.assert = function(condition, message)
84 var status = condition ? "PASS" : "FAIL";
85 this.sendCommand("Runtime.evaluate", { "expression": "log(" + JSON.stringify(status + ": " + message) + ")" } );
89 * Logs message directly to process stdout via alert function (hopefully followed by flush call).
90 * This message should survive process crash or kill by timeout.
91 * @param {string} message
93 InspectorTest.debugLog = function(message)
95 this.sendCommand("Runtime.evaluate", { "expression": "debugLog(" + JSON.stringify(message) + ")" } );
98 InspectorTest.completeTest = function()
100 this.sendCommand("Runtime.evaluate", { "expression": "closeTest();"} );
103 InspectorTest.checkForError = function(responseObject)
105 if (responseObject.error) {
106 InspectorTest.log("PROTOCOL ERROR: " + responseObject.error.message);
107 InspectorTest.completeTest();
108 throw "PROTOCOL ERROR";
113 * @param {string} scriptName
115 InspectorTest.importScript = function(scriptName)
117 var xhr = new XMLHttpRequest();
118 xhr.open("GET", scriptName, false);
120 if (xhr.status !== 0 && xhr.status !== 200)
121 throw new Error("Invalid script URL: " + scriptName);
122 var script = "try { " + xhr.responseText + "} catch (e) { alert(" + JSON.stringify("Error in: " + scriptName) + "); throw e; }";
126 InspectorTest.importInspectorScripts = function()
128 // Note: This function overwrites the InspectorFrontendAPI, so there's currently no
129 // way to intercept the messages from the backend.
131 var inspectorScripts = [
136 "InspectorFrontendAPI",
137 "InspectorFrontendHostStub",
138 "InspectorBackendCommands",
145 "FrameResourceManager",
149 "SourceCodeRevision",
152 "ResourceCollection",
158 "ExecutionContextList",
162 for (var i = 0; i < inspectorScripts.length; ++i)
163 InspectorTest.importScript("../../../../../Source/WebInspectorUI/UserInterface/" + inspectorScripts[i] + ".js");
165 // The initialization should be in sync with WebInspector.loaded in Main.js.
166 // FIXME: As soon as we can support all the observers and managers we should remove UI related tasks
167 // from WebInspector.loaded, so that it can be used from the LayoutTests.
169 InspectorBackend.registerPageDispatcher(new WebInspector.PageObserver);
170 InspectorBackend.registerDOMDispatcher(new WebInspector.DOMObserver);
171 InspectorBackend.registerCSSDispatcher(new WebInspector.CSSObserver);
173 WebInspector.frameResourceManager = new WebInspector.FrameResourceManager;
174 WebInspector.domTreeManager = new WebInspector.DOMTreeManager;
175 WebInspector.cssStyleManager = new WebInspector.CSSStyleManager;
177 InspectorFrontendHost.loaded();
181 window.addEventListener("message", function(event) {
186 InspectorTest.completeTest();