2011-02-07 Pavel Feldman <pfeldman@chromium.org>
[WebKit-https.git] / LayoutTests / http / tests / inspector / inspector-test2.js
1 var initialize_InspectorTest = function() {
2
3 var results = [];
4 var resultsSynchronized = false;
5
6 InspectorTest.completeTest = function()
7 {
8     InspectorBackend.didEvaluateForTestInFrontend(InspectorTest.completeTestCallId, "");
9 }
10
11 InspectorTest.evaluateInConsole = function(code, callback)
12 {
13     WebInspector.console.visible = true;
14     WebInspector.console.prompt.text = code;
15     var event = document.createEvent("KeyboardEvent");
16     event.initKeyboardEvent("keydown", true, true, null, "Enter", "");
17     WebInspector.console.promptElement.dispatchEvent(event);
18     InspectorTest._addSniffer(WebInspector.ConsoleView.prototype, "addMessage",
19         function(commandResult) {
20             if (callback)
21                 callback(commandResult.toMessageElement().textContent);
22         });
23 }
24
25 InspectorTest.evaluateInPage = function(code, callback)
26 {
27     InspectorBackend.evaluate(code, "console", false, callback || function() {});
28 }
29
30 InspectorTest.evaluateInPageWithTimeout = function(code, callback)
31 {
32     InspectorTest.evaluateInPage("setTimeout(unescape('" + escape(code) + "'))", callback);
33 }
34
35 InspectorTest.addResult = function(text)
36 {
37     results.push(text);
38     if (resultsSynchronized)
39         addResultToPage(text);
40     else {
41         clearResults();
42         for (var i = 0; i < results.length; ++i)
43             addResultToPage(results[i]);
44         resultsSynchronized = true;
45     }
46
47     function clearResults()
48     {
49         InspectorTest.evaluateInPage("Array.prototype.forEach.call(document.body.querySelectorAll('div.output'), function(node) { node.parentNode.removeChild(node); })");
50     }
51
52     function addResultToPage(text)
53     {
54         InspectorTest.evaluateInPage("output(unescape('" + escape(text) + "'))");
55     }
56 }
57
58 InspectorTest.addResults = function(textArray)
59 {
60     if (!textArray)
61         return;
62     for (var i = 0, size = textArray.length; i < size; ++i)
63         InspectorTest.addResult(textArray[i]);
64 }
65
66 InspectorTest.addObject = function(object, nondeterministicProps, prefix, firstLinePrefix)
67 {
68     prefix = prefix || "";
69     firstLinePrefix = firstLinePrefix || prefix;
70     InspectorTest.addResult(firstLinePrefix + "{");
71     for (var prop in object) {
72         if (!object.hasOwnProperty(prop))
73             continue;
74         var prefixWithName = prefix + "    " + prop + " : ";
75         var propValue = object[prop];
76         if (nondeterministicProps && prop in nondeterministicProps)
77             InspectorTest.addResult(prefixWithName + "<" + typeof propValue + ">");
78         else if (propValue === null)
79             InspectorTest.addResult(prefixWithName + "null");
80         else if (typeof propValue === "object")
81             InspectorTest.addObject(propValue, nondeterministicProps, prefix + "    ", prefixWithName);
82         else if (typeof propValue === "string")
83             InspectorTest.addResult(prefixWithName + "\"" + propValue + "\"");
84         else
85             InspectorTest.addResult(prefixWithName + propValue);
86     }
87     InspectorTest.addResult(prefix + "}");
88 }
89
90 InspectorTest.reloadPage = function(callback)
91 {
92     InspectorTest._reloadPageCallback = callback;
93
94     if (WebInspector.panels.network)
95         WebInspector.panels.network._reset();
96     InspectorBackend.reloadPage(false);
97 }
98
99 InspectorTest.reloadPageIfNeeded = function(callback)
100 {
101     if (!InspectorTest._pageWasReloaded) {
102         InspectorTest._pageWasReloaded = true;
103         InspectorTest.reloadPage(callback);
104     } else {
105         if (callback)
106             callback();
107     }
108 }
109
110 InspectorTest.pageReloaded = function()
111 {
112     resultsSynchronized = false;
113     InspectorTest.addResult("Page reloaded.");
114     if (InspectorTest._reloadPageCallback) {
115         var callback = InspectorTest._reloadPageCallback;
116         delete InspectorTest._reloadPageCallback;
117         callback();
118     }
119 }
120
121 InspectorTest.runAfterPendingDispatches = function(callback)
122 {
123     WebInspector.TestController.prototype.runAfterPendingDispatches(callback);
124 }
125
126 InspectorTest.createKeyEvent = function(keyIdentifier)
127 {
128     var evt = document.createEvent("KeyboardEvent");
129     evt.initKeyboardEvent("keydown", true /* can bubble */, true /* can cancel */, null /* view */, keyIdentifier, "");
130     return evt;
131 }
132
133 InspectorTest.findDOMNode = function(root, filter, callback)
134 {
135     var found = false;
136     var pendingCalls = 1;
137
138     if (root)
139         findDOMNode(root);
140     else
141         waitForDocument();
142
143     function waitForDocument()
144     {
145         root = WebInspector.domAgent.document;
146         if (root)
147             findDOMNode(root);
148         else
149             InspectorTest._addSniffer(WebInspector, setDocument, waitForDocument);
150     }
151
152     function findDOMNode(node)
153     {
154         if (filter(node)) {
155             callback(node);
156             found = true;
157         } else
158             WebInspector.domAgent.getChildNodesAsync(node, processChildren);
159
160         --pendingCalls;
161
162         if (!found && !pendingCalls)
163             setTimeout(findDOMNode.bind(null, root), 0);
164
165         function processChildren(children)
166         {
167             pendingCalls += children ? children.length : 0;
168             for (var i = 0; !found && children && i < children.length; ++i)
169                 findDOMNode(children[i]);
170         }
171     }
172 }
173
174 InspectorTest._addSniffer = function(receiver, methodName, override, opt_sticky)
175 {
176     var original = receiver[methodName];
177     if (typeof original !== "function")
178         throw ("Cannot find method to override: " + methodName);
179
180     receiver[methodName] = function(var_args) {
181         try {
182             var result = original.apply(this, arguments);
183         } finally {
184             if (!opt_sticky)
185                 receiver[methodName] = original;
186         }
187         // In case of exception the override won't be called.
188         try {
189             override.apply(this, arguments);
190         } catch (e) {
191             throw ("Exception in overriden method '" + methodName + "': " + e);
192         }
193         return result;
194     };
195 }
196
197 };
198
199 var runTestCallId = 0;
200 var completeTestCallId = 1;
201
202 function runTest()
203 {
204     if (!window.layoutTestController)
205         return;
206
207     layoutTestController.dumpAsText();
208     layoutTestController.waitUntilDone();
209
210     function runTestInFrontend(initializationFunctions, testFunction, completeTestCallId)
211     {
212         if (window.InspectorTest) {
213             InspectorTest.pageReloaded();
214             return;
215         }
216
217         InspectorTest = {};
218         InspectorTest.completeTestCallId = completeTestCallId;
219
220         for (var i = 0; i < initializationFunctions.length; ++i) {
221             try {
222                 initializationFunctions[i]();
223             } catch (e) {
224                 console.error("Exception in test initialization: " + e);
225             }
226         }
227
228         WebInspector.showPanel("elements");
229         testFunction();
230     }
231
232     var initializationFunctions = [];
233     for (var name in window) {
234         if (name.indexOf("initialize_") === 0 && typeof window[name] === "function")
235             initializationFunctions.push(window[name].toString());
236     }
237     var parameters = ["[" + initializationFunctions + "]", test, completeTestCallId];
238     var toEvaluate = "(" + runTestInFrontend + ")(" + parameters.join(", ") + ");";
239     layoutTestController.evaluateInWebInspector(runTestCallId, toEvaluate);
240 }
241
242 function didEvaluateForTestInFrontend(callId)
243 {
244     if (callId !== completeTestCallId)
245         return;
246     layoutTestController.closeWebInspector();
247     setTimeout(function() {
248         layoutTestController.notifyDone();
249     }, 0);
250 }
251
252 function output(text)
253 {
254     var outputElement = document.createElement("div");
255     outputElement.className = "output";
256     outputElement.style.whiteSpace = "pre";
257     outputElement.appendChild(document.createTextNode(text));
258     outputElement.appendChild(document.createElement("br"));
259     document.body.appendChild(outputElement);
260 }