Upstream version 7.36.149.0
[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.eventHandler = {};
35
36 /**
37  * @param {string} method
38  * @param {object} params
39  * @param {function({object} messageObject)=} handler
40  */
41 InspectorTest.sendCommand = function(method, params, handler)
42 {
43     this._dispatchTable[++this._requestId] = handler;
44
45     var messageObject = { "method": method,
46                           "params": params,
47                           "id": this._requestId };
48
49     InspectorFrontendHost.sendMessageToBackend(JSON.stringify(messageObject));
50
51     return this._requestId;
52 }
53
54 InspectorTest.sendCommandOrDie = function(command, properties, callback)
55 {
56     InspectorTest.sendCommand(command, properties || {}, commandCallback);
57     function commandCallback(msg)
58     {
59         if (msg.error) {
60             InspectorTest.log("ERROR: " + msg.error.message);
61             InspectorTest.completeTest();
62             return;
63         }
64         if (callback)
65             callback(msg.result);
66     }
67 }
68
69 InspectorTest.domUndo = function(callback)
70 {
71     InspectorTest.sendCommandOrDie("DOM.undo", {}, callback);
72 }
73
74 InspectorTest.undoAndNext = function(next)
75 {
76     return InspectorTest.domUndo.bind(InspectorTest, next);
77 }
78
79 InspectorTest.runTestSuite = function(testSuite)
80 {
81     function nextTest()
82     {
83         if (!testSuite.length) {
84             InspectorTest.completeTest();
85             return;
86         }
87         var fun = testSuite.shift();
88         InspectorTest.log("\nRunning test: " + fun.name);
89         fun(nextTest);
90     }
91
92     nextTest();
93 }
94
95 /**
96  * @param {function(object)=} callback
97  */
98 InspectorTest.wrapCallback = function(callback)
99 {
100     /**
101      * @param {object} message
102      */
103     function callbackWrapper(message)
104     {
105         if (InspectorTest.completeTestIfError(message))
106             return;
107         if (!callback)
108             return;
109         try {
110             callback(message["result"]);
111         } catch (e) {
112             InspectorTest.log("Exception " + e + " while invoking callback: " + callback);
113             InspectorTest.completeTest();
114         }
115     }
116     return callbackWrapper;
117 }
118
119 /**
120  * @param {string} command
121  * @param {function({object} messageObject)=} handler
122  */
123 InspectorTest.sendRawCommand = function(command, handler)
124 {
125     this._dispatchTable[++this._requestId] = handler;
126     InspectorFrontendHost.sendMessageToBackend(command);
127     return this._requestId;
128 }
129
130 /**
131  * @param {string} message
132  */
133 InspectorFrontendAPI.dispatchMessage = function(message)
134 {
135     var messageObject = JSON.parse(message);
136     var messageId = messageObject["id"];
137     try {
138         if (typeof messageId === "number") {
139             var handler = InspectorTest._dispatchTable[messageId];
140             if (handler && typeof handler === "function")
141                 handler(messageObject);
142         } else {
143             var eventName = messageObject["method"];
144             var eventHandler = InspectorTest.eventHandler[eventName];
145             if (eventHandler)
146                 eventHandler(messageObject);
147         }
148     } catch(e) {
149         InspectorTest.log("Exception when dispatching message: " + e + "\n\n message = " + JSON.stringify(messageObject, null, 2));
150         InspectorTest.completeTest();
151     }
152 }
153
154 /**
155 * Logs message to document.
156 * @param {string} message
157 */
158 InspectorTest.log = function(message)
159 {
160     this.sendCommand("Runtime.evaluate", { "expression": "log(" + JSON.stringify(message) + ")" } );
161 }
162
163 /**
164 * Formats and logs object to document.
165 * @param {Object} object
166 * @param {string=} title
167 */
168 InspectorTest.logObject = function(object, title)
169 {
170     var lines = [];
171
172     function dumpValue(value, prefix, prefixWithName)
173     {
174         if (typeof value === "object" && value !== null) {
175             if (value instanceof Array)
176                 dumpItems(value, prefix, prefixWithName);
177             else
178                 dumpProperties(value, prefix, prefixWithName);
179         } else {
180             lines.push(prefixWithName + String(value).replace(/\n/g, " "));
181         }
182     }
183
184     function dumpProperties(object, prefix, firstLinePrefix)
185     {
186         prefix = prefix || "";
187         firstLinePrefix = firstLinePrefix || prefix;
188         lines.push(firstLinePrefix + "{");
189
190         var propertyNames = Object.keys(object);
191         propertyNames.sort();
192         for (var i = 0; i < propertyNames.length; ++i) {
193             var name = propertyNames[i];
194             if (typeof object.hasOwnProperty === "function" && !object.hasOwnProperty(name))
195                 continue;
196             var prefixWithName = "    " + prefix + name + " : ";
197             dumpValue(object[name], "    " + prefix, prefixWithName);
198         }
199         lines.push(prefix + "}");
200     }
201
202     function dumpItems(object, prefix, firstLinePrefix)
203     {
204         prefix = prefix || "";
205         firstLinePrefix = firstLinePrefix || prefix;
206         lines.push(firstLinePrefix + "[");
207         for (var i = 0; i < object.length; ++i)
208             dumpValue(object[i], "    " + prefix, "    " + prefix + "[" + i + "] : ");
209         lines.push(prefix + "]");
210     }
211
212     dumpValue(object, "", title);
213     InspectorTest.log(lines.join("\n"));
214 }
215
216 /**
217 * Logs message directly to process stdout via alert function (hopefully followed by flush call).
218 * This message should survive process crash or kill by timeout.
219 * @param {string} message
220 */
221 InspectorTest.debugLog = function(message)
222 {
223     this.sendCommand("Runtime.evaluate", { "expression": "debugLog(" + JSON.stringify(message) + ")" } );
224 }
225
226 InspectorTest.completeTest = function()
227 {
228     this.sendCommand("Runtime.evaluate", { "expression": "closeTest();"} );
229 }
230
231 InspectorTest.completeTestIfError = function(messageObject)
232 {
233     if (messageObject.error) {
234         InspectorTest.log(messageObject.error.message);
235         InspectorTest.completeTest();
236         return true;
237     }
238     return false;
239 }
240
241 InspectorTest.checkExpectation = function(fail, name, messageObject)
242 {
243     if (fail === !!messageObject.error) {
244         InspectorTest.log("PASS: " + name);
245         return true;
246     }
247
248     InspectorTest.log("FAIL: " + name + ": " + JSON.stringify(messageObject));
249     InspectorTest.completeTest();
250     return false;
251 }
252 InspectorTest.expectedSuccess = InspectorTest.checkExpectation.bind(null, false);
253 InspectorTest.expectedError = InspectorTest.checkExpectation.bind(null, true);
254
255 InspectorTest.assert = function(condition, message)
256 {
257     if (condition)
258         return;
259     InspectorTest.log("FAIL: assertion failed: " + message);
260     InspectorTest.completeTest();
261 }
262
263 InspectorTest.assertEquals = function(expected, actual, message)
264 {
265     if (expected === actual)
266         return;
267     InspectorTest.assert(false, "expected: `" + expected + "', actual: `" + actual + "'" + (message ? ", " + message : ""));
268 }
269
270 /**
271  * @param {string} scriptName
272  */
273 InspectorTest.importScript = function(scriptName)
274 {
275     var xhr = new XMLHttpRequest();
276     xhr.open("GET", scriptName, false);
277     xhr.send(null);
278     window.eval(xhr.responseText + "\n//@ sourceURL=" + scriptName);
279 }
280
281
282 InspectorTest.eventHandler["Inspector.evaluateForTestInFrontend"] = function(message)
283 {
284     try {
285         eval(message.params.script);
286     } catch (e) {
287         InspectorTest.log("FAIL: exception in evaluateForTestInFrontend: " + e);
288         InspectorTest.completeTest();
289     }
290 };
291
292 function enableInspectorAgent()
293 {
294     InspectorTest.sendCommand("Inspector.enable", { });
295 }
296
297 window.addEventListener("load", enableInspectorAgent, false);
298
299 </script>
300 </head>
301 </html>