2011-03-02 MORITA Hajime <morrita@google.com>
[WebKit-https.git] / LayoutTests / http / tests / inspector / inspector-test.js
1 var initialize_InspectorTest = function() {
2
3 var results = [];
4 var resultsSynchronized = false;
5
6 InspectorTest.completeTest = function()
7 {
8     InspectorAgent.didEvaluateForTestInFrontend(InspectorTest.completeTestCallId, "");
9 }
10
11 InspectorTest.evaluateInConsole = function(code, callback)
12 {
13     callback = InspectorTest.safeWrap(callback);
14
15     WebInspector.console.visible = true;
16     WebInspector.console.prompt.text = code;
17     var event = document.createEvent("KeyboardEvent");
18     event.initKeyboardEvent("keydown", true, true, null, "Enter", "");
19     WebInspector.console.promptElement.dispatchEvent(event);
20     InspectorTest.addSniffer(WebInspector.ConsoleView.prototype, "addMessage",
21         function(commandResult) {
22             callback(commandResult.toMessageElement().textContent);
23         });
24 }
25
26 InspectorTest.evaluateInConsoleAndDump = function(code, callback)
27 {
28     callback = InspectorTest.safeWrap(callback);
29
30     function mycallback(text)
31     {
32         InspectorTest.addResult(code + " = " + text);
33         callback(text);
34     }
35     InspectorTest.evaluateInConsole(code, mycallback);
36 }
37
38 InspectorTest.evaluateInPage = function(code, callback)
39 {
40     callback = InspectorTest.safeWrap(callback);
41
42     function mycallback(result)
43     {
44         callback(WebInspector.RemoteObject.fromPayload(result));
45     }
46     RuntimeAgent.evaluate(code, "console", false, mycallback);
47 }
48
49 InspectorTest.evaluateInPageWithTimeout = function(code)
50 {
51     InspectorTest.evaluateInPage("setTimeout(unescape('" + escape(code) + "'))");
52 }
53
54 InspectorTest.addResult = function(text)
55 {
56     results.push(text);
57     if (resultsSynchronized)
58         addResultToPage(text);
59     else {
60         clearResults();
61         for (var i = 0; i < results.length; ++i)
62             addResultToPage(results[i]);
63         resultsSynchronized = true;
64     }
65
66     function clearResults()
67     {
68         InspectorTest.evaluateInPage("clearOutput()");
69     }
70
71     function addResultToPage(text)
72     {
73         InspectorTest.evaluateInPage("output(unescape('" + escape(text) + "'))");
74     }
75 }
76
77 console.error = InspectorTest.addResult;
78
79 InspectorTest.addResults = function(textArray)
80 {
81     if (!textArray)
82         return;
83     for (var i = 0, size = textArray.length; i < size; ++i)
84         InspectorTest.addResult(textArray[i]);
85 }
86
87 InspectorTest.addObject = function(object, nondeterministicProps, prefix, firstLinePrefix)
88 {
89     prefix = prefix || "";
90     firstLinePrefix = firstLinePrefix || prefix;
91     InspectorTest.addResult(firstLinePrefix + "{");
92     for (var prop in object) {
93         if (!object.hasOwnProperty(prop))
94             continue;
95         var prefixWithName = prefix + "    " + prop + " : ";
96         var propValue = object[prop];
97         if (nondeterministicProps && prop in nondeterministicProps)
98             InspectorTest.addResult(prefixWithName + "<" + typeof propValue + ">");
99         else if (propValue === null)
100             InspectorTest.addResult(prefixWithName + "null");
101         else if (typeof propValue === "object")
102             InspectorTest.addObject(propValue, nondeterministicProps, prefix + "    ", prefixWithName);
103         else if (typeof propValue === "string")
104             InspectorTest.addResult(prefixWithName + "\"" + propValue + "\"");
105         else
106             InspectorTest.addResult(prefixWithName + propValue);
107     }
108     InspectorTest.addResult(prefix + "}");
109 }
110
111 InspectorTest.assertGreaterOrEqual = function(expected, actual, message)
112 {
113     if (actual < expected)
114         InspectorTest.addResult("FAILED: " + (message ? message + ": " : "") + actual + " < " + expected);
115 }
116
117 InspectorTest.reloadPage = function(callback)
118 {
119     InspectorTest._reloadPageCallback = InspectorTest.safeWrap(callback);
120
121     if (WebInspector.panels.network)
122         WebInspector.panels.network._reset();
123     InspectorAgent.reloadPage(false);
124 }
125
126 InspectorTest.pageReloaded = function()
127 {
128     resultsSynchronized = false;
129     InspectorTest.addResult("Page reloaded.");
130     if (InspectorTest._reloadPageCallback) {
131         var callback = InspectorTest._reloadPageCallback;
132         delete InspectorTest._reloadPageCallback;
133         callback();
134     }
135 }
136
137 InspectorTest.runAfterPendingDispatches = function(callback)
138 {
139     callback = InspectorTest.safeWrap(callback);
140     InspectorBackend.runAfterPendingDispatches(callback);
141 }
142
143 InspectorTest.createKeyEvent = function(keyIdentifier)
144 {
145     var evt = document.createEvent("KeyboardEvent");
146     evt.initKeyboardEvent("keydown", true /* can bubble */, true /* can cancel */, null /* view */, keyIdentifier, "");
147     return evt;
148 }
149
150 InspectorTest.runTestSuite = function(testSuite)
151 {
152     var testSuiteTests = testSuite.slice();
153
154     function runner()
155     {
156         if (!testSuiteTests.length) {
157             InspectorTest.completeTest();
158             return;
159         }
160         var nextTest = testSuiteTests.shift();
161         InspectorTest.addResult("");
162         InspectorTest.addResult("Running: " + /function\s([^(]*)/.exec(nextTest)[1]);
163         InspectorTest.safeWrap(nextTest)(runner, runner);
164     }
165     runner();
166 }
167
168 InspectorTest.assertEquals = function(expected, found, message)
169 {
170     if (expected === found)
171         return;
172
173     var error;
174     if (message)
175         error = "Failure (" + message + "):";
176     else
177         error = "Failure:";
178     throw new Error(error + " expected <" + expected + "> found <" + found + ">");
179 }
180
181 InspectorTest.safeWrap = function(func, onexception)
182 {
183     function result()
184     {
185         if (!func)
186             return;
187         var wrapThis = this;
188         try {
189             return func.apply(wrapThis, arguments);
190         } catch(e) {
191             InspectorTest.addResult("Exception while running: " + func + "\n" + (e.stack || e));
192             if (onexception)
193                 InspectorTest.safeWrap(onexception)();
194             else
195                 InspectorTest.completeTest();
196         }
197     }
198     return result;
199 }
200
201 InspectorTest.addSniffer = function(receiver, methodName, override, opt_sticky)
202 {
203     override = InspectorTest.safeWrap(override);
204
205     var original = receiver[methodName];
206     if (typeof original !== "function")
207         throw ("Cannot find method to override: " + methodName);
208
209     receiver[methodName] = function(var_args) {
210         try {
211             var result = original.apply(this, arguments);
212         } finally {
213             if (!opt_sticky)
214                 receiver[methodName] = original;
215         }
216         // In case of exception the override won't be called.
217         try {
218             override.apply(this, arguments);
219         } catch (e) {
220             throw ("Exception in overriden method '" + methodName + "': " + e);
221         }
222         return result;
223     };
224 }
225
226 };
227
228 var runTestCallId = 0;
229 var completeTestCallId = 1;
230
231 function runAfterIframeIsLoaded()
232 {
233     if (window.layoutTestController)
234         layoutTestController.waitUntilDone();
235     function step()
236     {
237         if (!window.iframeLoaded)
238             setTimeout(step, 100);
239         else
240             runTest();
241     }
242     setTimeout(step, 100);
243 }
244
245 function runTest(enableWatchDogWhileDebugging)
246 {
247     if (!window.layoutTestController)
248         return;
249
250     layoutTestController.dumpAsText();
251     layoutTestController.waitUntilDone();
252
253     function runTestInFrontend(initializationFunctions, testFunction, completeTestCallId)
254     {
255         if (window.InspectorTest) {
256             InspectorTest.pageReloaded();
257             return;
258         }
259
260         InspectorTest = {};
261         InspectorTest.completeTestCallId = completeTestCallId;
262
263         for (var i = 0; i < initializationFunctions.length; ++i) {
264             try {
265                 initializationFunctions[i]();
266             } catch (e) {
267                 console.error("Exception in test initialization: " + e);
268                 InspectorTest.completeTest();
269             }
270         }
271
272         WebInspector.showPanel("elements");
273         try {
274             testFunction();
275         } catch (e) {
276             console.error("Exception during test execution: " + e);
277             InspectorTest.completeTest();
278         }
279     }
280
281     var initializationFunctions = [];
282     for (var name in window) {
283         if (name.indexOf("initialize_") === 0 && typeof window[name] === "function")
284             initializationFunctions.push(window[name].toString());
285     }
286     var parameters = ["[" + initializationFunctions + "]", test, completeTestCallId];
287     var toEvaluate = "(" + runTestInFrontend + ")(" + parameters.join(", ") + ");";
288     layoutTestController.evaluateInWebInspector(runTestCallId, toEvaluate);
289
290     if (enableWatchDogWhileDebugging) {
291         function watchDog()
292         {
293             console.log("Internal watchdog triggered at 10 seconds. Test timed out.");
294             closeInspectorAndNotifyDone();
295         }
296         window._watchDogTimer = setTimeout(watchDog, 10000);
297     }
298 }
299
300 function didEvaluateForTestInFrontend(callId)
301 {
302     if (callId !== completeTestCallId)
303         return;
304     delete window.completeTestCallId;
305     closeInspectorAndNotifyDone();
306 }
307
308 function closeInspectorAndNotifyDone()
309 {
310     if (window._watchDogTimer)
311         clearTimeout(window._watchDogTimer);
312
313     layoutTestController.closeWebInspector();
314     setTimeout(function() {
315         layoutTestController.notifyDone();
316     }, 0);
317 }
318
319 var outputElement;
320
321 function output(text)
322 {
323     if (!outputElement) {
324         outputElement = document.createElement("div");
325         outputElement.className = "output";
326         outputElement.style.whiteSpace = "pre";
327         document.body.appendChild(outputElement);
328     }
329     outputElement.appendChild(document.createTextNode(text));
330     outputElement.appendChild(document.createElement("br"));
331 }
332
333 function clearOutput()
334 {
335     if (outputElement) {
336         outputElement.parentNode.removeChild(outputElement);
337         outputElement = null;
338     }
339 }