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.
32 InspectorTest
._dispatchTable
= [];
33 InspectorTest
._requestId
= -1;
34 InspectorTest
._dumpInspectorProtocolMessages
= false;
35 InspectorTest
.eventHandler
= {};
37 InspectorTest
.startDumpingProtocolMessages = function()
39 InspectorTest
._dumpInspectorProtocolMessages
= true;
43 * @param {string} method
44 * @param {object} params
45 * @param {function({object} messageObject)=} handler
47 InspectorTest
.sendCommand = function(method
, params
, handler
)
49 this._dispatchTable
[++this._requestId
] = handler
;
51 var messageObject
= { "method": method
,
53 "id": this._requestId
};
55 if (InspectorTest
._dumpInspectorProtocolMessages
)
56 testRunner
.logToStderr("frontend: " + JSON
.stringify(messageObject
));
57 DevToolsHost
.sendMessageToBackend(JSON
.stringify(messageObject
));
59 return this._requestId
;
62 InspectorTest
.sendCommandOrDie = function(command
, properties
, callback
)
64 InspectorTest
.sendCommand(command
, properties
|| {}, commandCallback
);
65 function commandCallback(msg
)
68 InspectorTest
.log("ERROR: " + msg
.error
.message
);
69 InspectorTest
.completeTest();
77 InspectorTest
.domUndo = function(callback
)
79 InspectorTest
.sendCommandOrDie("DOM.undo", {}, callback
);
82 InspectorTest
.undoAndNext = function(next
)
84 return InspectorTest
.domUndo
.bind(InspectorTest
, next
);
87 InspectorTest
.runTestSuite = function(testSuite
)
91 if (!testSuite
.length
) {
92 InspectorTest
.completeTest();
95 var fun
= testSuite
.shift();
96 InspectorTest
.log("\nRunning test: " + fun
.name
);
104 * @param {function(object)=} callback
106 InspectorTest
.wrapCallback = function(callback
)
109 * @param {object} message
111 function callbackWrapper(message
)
113 if (InspectorTest
.completeTestIfError(message
))
118 callback(message
["result"]);
120 InspectorTest
.log("Exception " + e
+ " while invoking callback: " + callback
);
121 InspectorTest
.completeTest();
124 return callbackWrapper
;
128 * @param {string} command
129 * @param {function({object} messageObject)=} handler
131 InspectorTest
.sendRawCommand = function(command
, handler
)
133 this._dispatchTable
[++this._requestId
] = handler
;
134 DevToolsHost
.sendMessageToBackend(command
);
135 return this._requestId
;
139 * @param {string|!Object} messageOrObject
141 DevToolsAPI
.dispatchMessage = function(messageOrObject
)
143 var messageObject
= (typeof messageOrObject
=== "string" ? JSON
.parse(messageOrObject
) : messageOrObject
);
144 if (InspectorTest
._dumpInspectorProtocolMessages
)
145 testRunner
.logToStderr("backend: " + JSON
.stringify(messageObject
));
146 var messageId
= messageObject
["id"];
148 if (typeof messageId
=== "number") {
149 var handler
= InspectorTest
._dispatchTable
[messageId
];
150 if (handler
&& typeof handler
=== "function")
151 handler(messageObject
);
153 var eventName
= messageObject
["method"];
154 var eventHandler
= InspectorTest
.eventHandler
[eventName
];
156 eventHandler(messageObject
);
159 InspectorTest
.log("Exception when dispatching message: " + e
+ "\n" + e
.stack
+ "\n message = " + JSON
.stringify(messageObject
, null, 2));
160 InspectorTest
.completeTest();
165 * Logs message to document.
166 * @param {string} message
168 InspectorTest
.log = function(message
)
170 this.sendCommand("Runtime.evaluate", { "expression": "log(" + JSON
.stringify(message
) + ")" } );
174 * Formats and logs object to document.
175 * @param {Object} object
176 * @param {string=} title
178 InspectorTest
.logObject = function(object
, title
)
182 function dumpValue(value
, prefix
, prefixWithName
)
184 if (typeof value
=== "object" && value
!== null) {
185 if (value
instanceof Array
)
186 dumpItems(value
, prefix
, prefixWithName
);
188 dumpProperties(value
, prefix
, prefixWithName
);
190 lines
.push(prefixWithName
+ String(value
).replace(/\n/g, " "));
194 function dumpProperties(object
, prefix
, firstLinePrefix
)
196 prefix
= prefix
|| "";
197 firstLinePrefix
= firstLinePrefix
|| prefix
;
198 lines
.push(firstLinePrefix
+ "{");
200 var propertyNames
= Object
.keys(object
);
201 propertyNames
.sort();
202 for (var i
= 0; i
< propertyNames
.length
; ++i
) {
203 var name
= propertyNames
[i
];
204 if (!object
.hasOwnProperty(name
))
206 var prefixWithName
= " " + prefix
+ name
+ " : ";
207 dumpValue(object
[name
], " " + prefix
, prefixWithName
);
209 lines
.push(prefix
+ "}");
212 function dumpItems(object
, prefix
, firstLinePrefix
)
214 prefix
= prefix
|| "";
215 firstLinePrefix
= firstLinePrefix
|| prefix
;
216 lines
.push(firstLinePrefix
+ "[");
217 for (var i
= 0; i
< object
.length
; ++i
)
218 dumpValue(object
[i
], " " + prefix
, " " + prefix
+ "[" + i
+ "] : ");
219 lines
.push(prefix
+ "]");
222 dumpValue(object
, "", title
);
223 InspectorTest
.log(lines
.join("\n"));
227 * Logs message directly to process stdout via alert function (hopefully followed by flush call).
228 * This message should survive process crash or kill by timeout.
229 * @param {string} message
231 InspectorTest
.debugLog = function(message
)
233 this.sendCommand("Runtime.evaluate", { "expression": "debugLog(" + JSON
.stringify(message
) + ")" } );
236 InspectorTest
.completeTest = function()
238 this.sendCommand("Runtime.evaluate", { "expression": "closeTest();"} );
242 * Evaluates string in page.
243 * @param {string} message
244 * @param {!function} callback
246 InspectorTest
.evaluateInPage = function(string
, callback
)
248 this.sendCommand("Runtime.evaluate", { "expression": string
}, function(message
) {
250 InspectorTest
.log("Error while executing '" + string
+ "': " + message
.error
.message
);
256 InspectorTest
.completeTestIfError = function(messageObject
)
258 if (messageObject
.error
) {
259 InspectorTest
.log(messageObject
.error
.message
);
260 InspectorTest
.completeTest();
266 InspectorTest
.checkExpectation = function(fail
, name
, messageObject
)
268 if (fail
=== !!messageObject
.error
) {
269 InspectorTest
.log("PASS: " + name
);
273 InspectorTest
.log("FAIL: " + name
+ ": " + JSON
.stringify(messageObject
));
274 InspectorTest
.completeTest();
277 InspectorTest
.expectedSuccess
= InspectorTest
.checkExpectation
.bind(null, false);
278 InspectorTest
.expectedError
= InspectorTest
.checkExpectation
.bind(null, true);
281 * @param {string} scriptName
283 InspectorTest
.importScript = function(scriptName
)
285 var xhr
= new XMLHttpRequest();
286 xhr
.open("GET", scriptName
, false);
288 window
.eval(xhr
.responseText
+ "\n//@ sourceURL=" + scriptName
);
292 InspectorTest
.eventHandler
["Inspector.evaluateForTestInFrontend"] = function(message
)
295 eval(message
.params
.script
);
297 InspectorTest
.log("FAIL: exception in evaluateForTestInFrontend: " + e
);
298 InspectorTest
.completeTest();
302 function enableInspectorAgent()
304 InspectorTest
.sendCommand("Inspector.enable", { });
307 window
.addEventListener("load", enableInspectorAgent
, false);