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 message directly to process stdout via alert function (hopefully followed by flush call).
79 * This message should survive process crash or kill by timeout.
80 * @param {string} message
82 InspectorTest.debugLog = function(message)
84 this.sendCommand("Runtime.evaluate", { "expression": "debugLog(" + JSON.stringify(message) + ")" } );
87 InspectorTest.completeTest = function()
89 this.sendCommand("Runtime.evaluate", { "expression": "closeTest();"} );
92 InspectorTest.checkForError = function(responseObject)
94 if (responseObject.error) {
95 InspectorTest.log("PROTOCOL ERROR: " + responseObject.error.message);
96 InspectorTest.completeTest();
97 throw "PROTOCOL ERROR";
102 * @param {string} scriptName
104 InspectorTest.importScript = function(scriptName)
106 var xhr = new XMLHttpRequest();
107 xhr.open("GET", scriptName, false);
109 if (xhr.status !== 0 && xhr.status !== 200)
110 throw new Error("Invalid script URL: " + scriptName);
111 var script = "try { " + xhr.responseText + "} catch (e) { alert(" + JSON.stringify("Error in: " + scriptName) + "); throw e; }";
115 InspectorTest.importInspectorScripts = function()
117 // Note: This function overwrites the InspectorFrontendAPI, so there's currently no
118 // way to intercept the messages from the backend.
120 var inspectorScripts = [
125 "InspectorFrontendAPI",
126 "InspectorFrontendHostStub",
127 "InspectorBackendCommands",
133 "FrameResourceManager",
137 "SourceCodeRevision",
140 "ResourceCollection",
146 "ExecutionContextList"
148 for (var i = 0; i < inspectorScripts.length; ++i)
149 InspectorTest.importScript("../../../../../Source/WebInspectorUI/UserInterface/" + inspectorScripts[i] + ".js");
151 // The initialization should be in sync with WebInspector.loaded in Main.js.
152 // FIXME: As soon as we can support all the observers and managers we should remove UI related tasks
153 // from WebInspector.loaded, so that it can be used from the LayoutTests.
155 InspectorBackend.registerPageDispatcher(new WebInspector.PageObserver);
156 InspectorBackend.registerDOMDispatcher(new WebInspector.DOMObserver);
158 WebInspector.frameResourceManager = new WebInspector.FrameResourceManager;
159 WebInspector.domTreeManager = new WebInspector.DOMTreeManager;
161 InspectorFrontendHost.loaded();
165 window.addEventListener("message", function(event) {
170 InspectorTest.completeTest();