bfd6a990929a29193ad8f6bb3cd2b9b2f03506ea
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / http / tests / inspector-protocol / resources / protocol-test.html
1 <!--
2 Copyright (C) 2012 Samsung Electronics. All rights reserved.
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7
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.
13
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.
24 -->
25 <html>
26 <head>
27 <script>
28
29 InspectorFrontendAPI = {};
30
31 InspectorTest = {};
32 InspectorTest._dispatchTable = [];
33 InspectorTest._requestId = -1;
34 InspectorTest._dumpInspectorProtocolMessages = false;
35 InspectorTest.eventHandler = {};
36
37 InspectorTest.startDumpingProtocolMessages = function()
38 {
39     InspectorTest._dumpInspectorProtocolMessages = true;
40 }
41
42 /**
43  * @param {string} method
44  * @param {object} params
45  * @param {function({object} messageObject)=} handler
46  */
47 InspectorTest.sendCommand = function(method, params, handler)
48 {
49     this._dispatchTable[++this._requestId] = handler;
50
51     var messageObject = { "method": method,
52                           "params": params,
53                           "id": this._requestId };
54
55     if (InspectorTest._dumpInspectorProtocolMessages)
56         testRunner.logToStderr("frontend: " + JSON.stringify(messageObject));
57     InspectorFrontendHost.sendMessageToBackend(JSON.stringify(messageObject));
58
59     return this._requestId;
60 }
61
62 InspectorTest.sendCommandOrDie = function(command, properties, callback)
63 {
64     InspectorTest.sendCommand(command, properties || {}, commandCallback);
65     function commandCallback(msg)
66     {
67         if (msg.error) {
68             InspectorTest.log("ERROR: " + msg.error.message);
69             InspectorTest.completeTest();
70             return;
71         }
72         if (callback)
73             callback(msg.result);
74     }
75 }
76
77 InspectorTest.domUndo = function(callback)
78 {
79     InspectorTest.sendCommandOrDie("DOM.undo", {}, callback);
80 }
81
82 InspectorTest.undoAndNext = function(next)
83 {
84     return InspectorTest.domUndo.bind(InspectorTest, next);
85 }
86
87 InspectorTest.runTestSuite = function(testSuite)
88 {
89     function nextTest()
90     {
91         if (!testSuite.length) {
92             InspectorTest.completeTest();
93             return;
94         }
95         var fun = testSuite.shift();
96         InspectorTest.log("\nRunning test: " + fun.name);
97         fun(nextTest);
98     }
99
100     nextTest();
101 }
102
103 /**
104  * @param {function(object)=} callback
105  */
106 InspectorTest.wrapCallback = function(callback)
107 {
108     /**
109      * @param {object} message
110      */
111     function callbackWrapper(message)
112     {
113         if (InspectorTest.completeTestIfError(message))
114             return;
115         if (!callback)
116             return;
117         try {
118             callback(message["result"]);
119         } catch (e) {
120             InspectorTest.log("Exception " + e + " while invoking callback: " + callback);
121             InspectorTest.completeTest();
122         }
123     }
124     return callbackWrapper;
125 }
126
127 /**
128  * @param {string} command
129  * @param {function({object} messageObject)=} handler
130  */
131 InspectorTest.sendRawCommand = function(command, handler)
132 {
133     this._dispatchTable[++this._requestId] = handler;
134     InspectorFrontendHost.sendMessageToBackend(command);
135     return this._requestId;
136 }
137
138 /**
139  * @param {string|!Object} messageOrObject
140  */
141 InspectorFrontendAPI.dispatchMessage = function(messageOrObject)
142 {
143     if (InspectorTest._dumpInspectorProtocolMessages)
144         testRunner.logToStderr("backend: " + (typeof messageOrObject === "string" ? messageOrObject : JSON.stringify(messageOrObject)));
145     var messageObject = (typeof messageOrObject === "string" ? JSON.parse(messageOrObject) : messageOrObject);
146     var messageId = messageObject["id"];
147     try {
148         if (typeof messageId === "number") {
149             var handler = InspectorTest._dispatchTable[messageId];
150             if (handler && typeof handler === "function")
151                 handler(messageObject);
152         } else {
153             var eventName = messageObject["method"];
154             var eventHandler = InspectorTest.eventHandler[eventName];
155             if (eventHandler)
156                 eventHandler(messageObject);
157         }
158     } catch(e) {
159         InspectorTest.log("Exception when dispatching message: " + e + "\n" + e.stack + "\n message = " + JSON.stringify(messageObject, null, 2));
160         InspectorTest.completeTest();
161     }
162 }
163
164 /**
165 * Logs message to document.
166 * @param {string} message
167 */
168 InspectorTest.log = function(message)
169 {
170     this.sendCommand("Runtime.evaluate", { "expression": "log(" + JSON.stringify(message) + ")" } );
171 }
172
173 /**
174 * Formats and logs object to document.
175 * @param {Object} object
176 * @param {string=} title
177 */
178 InspectorTest.logObject = function(object, title)
179 {
180     var lines = [];
181
182     function dumpValue(value, prefix, prefixWithName)
183     {
184         if (typeof value === "object" && value !== null) {
185             if (value instanceof Array)
186                 dumpItems(value, prefix, prefixWithName);
187             else
188                 dumpProperties(value, prefix, prefixWithName);
189         } else {
190             lines.push(prefixWithName + String(value).replace(/\n/g, " "));
191         }
192     }
193
194     function dumpProperties(object, prefix, firstLinePrefix)
195     {
196         prefix = prefix || "";
197         firstLinePrefix = firstLinePrefix || prefix;
198         lines.push(firstLinePrefix + "{");
199
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 (typeof object.hasOwnProperty === "function" && !object.hasOwnProperty(name))
205                 continue;
206             var prefixWithName = "    " + prefix + name + " : ";
207             dumpValue(object[name], "    " + prefix, prefixWithName);
208         }
209         lines.push(prefix + "}");
210     }
211
212     function dumpItems(object, prefix, firstLinePrefix)
213     {
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 + "]");
220     }
221
222     dumpValue(object, "", title);
223     InspectorTest.log(lines.join("\n"));
224 }
225
226 /**
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
230 */
231 InspectorTest.debugLog = function(message)
232 {
233     this.sendCommand("Runtime.evaluate", { "expression": "debugLog(" + JSON.stringify(message) + ")" } );
234 }
235
236 InspectorTest.completeTest = function()
237 {
238     this.sendCommand("Runtime.evaluate", { "expression": "closeTest();"} );
239 }
240
241 /**
242  * Evaluates string in page.
243  * @param {string} message
244  * @param {!function} callback
245  */
246 InspectorTest.evaluateInPage = function(string, callback)
247 {
248     this.sendCommand("Runtime.evaluate", { "expression": string }, function(message) {
249         if (message.error)
250             InspectorTest.log("Error while executing '" + string + "': " + message.error.message);
251         else
252             callback();
253     });
254 };
255
256 InspectorTest.completeTestIfError = function(messageObject)
257 {
258     if (messageObject.error) {
259         InspectorTest.log(messageObject.error.message);
260         InspectorTest.completeTest();
261         return true;
262     }
263     return false;
264 }
265
266 InspectorTest.checkExpectation = function(fail, name, messageObject)
267 {
268     if (fail === !!messageObject.error) {
269         InspectorTest.log("PASS: " + name);
270         return true;
271     }
272
273     InspectorTest.log("FAIL: " + name + ": " + JSON.stringify(messageObject));
274     InspectorTest.completeTest();
275     return false;
276 }
277 InspectorTest.expectedSuccess = InspectorTest.checkExpectation.bind(null, false);
278 InspectorTest.expectedError = InspectorTest.checkExpectation.bind(null, true);
279
280 InspectorTest.assert = function(condition, message)
281 {
282     if (condition)
283         return;
284     InspectorTest.log("FAIL: assertion failed: " + message);
285     InspectorTest.completeTest();
286 }
287
288 InspectorTest.assertEquals = function(expected, actual, message)
289 {
290     if (expected === actual)
291         return;
292     InspectorTest.assert(false, "expected: `" + expected + "', actual: `" + actual + "'" + (message ? ", " + message : ""));
293 }
294
295 /**
296  * @param {string} scriptName
297  */
298 InspectorTest.importScript = function(scriptName)
299 {
300     var xhr = new XMLHttpRequest();
301     xhr.open("GET", scriptName, false);
302     xhr.send(null);
303     window.eval(xhr.responseText + "\n//@ sourceURL=" + scriptName);
304 }
305
306
307 InspectorTest.eventHandler["Inspector.evaluateForTestInFrontend"] = function(message)
308 {
309     try {
310         eval(message.params.script);
311     } catch (e) {
312         InspectorTest.log("FAIL: exception in evaluateForTestInFrontend: " + e);
313         InspectorTest.completeTest();
314     }
315 };
316
317 function enableInspectorAgent()
318 {
319     InspectorTest.sendCommand("Inspector.enable", { });
320 }
321
322 window.addEventListener("load", enableInspectorAgent, false);
323
324 </script>
325 </head>
326 </html>