1 var initialize_InspectorTest = function() {
4 var resultsSynchronized = false;
6 InspectorTest.completeTest = function()
8 InspectorAgent.didEvaluateForTestInFrontend(InspectorTest.completeTestCallId, "");
11 InspectorTest.evaluateInConsole = function(code, callback)
13 callback = InspectorTest.safeWrap(callback);
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);
26 InspectorTest.evaluateInConsoleAndDump = function(code, callback)
28 callback = InspectorTest.safeWrap(callback);
30 function mycallback(text)
32 InspectorTest.addResult(code + " = " + text);
35 InspectorTest.evaluateInConsole(code, mycallback);
38 InspectorTest.evaluateInPage = function(code, callback)
40 callback = InspectorTest.safeWrap(callback);
42 function mycallback(result)
44 callback(WebInspector.RemoteObject.fromPayload(result));
46 RuntimeAgent.evaluate(code, "console", false, mycallback);
49 InspectorTest.evaluateInPageWithTimeout = function(code)
51 InspectorTest.evaluateInPage("setTimeout(unescape('" + escape(code) + "'))");
54 InspectorTest.addResult = function(text)
57 if (resultsSynchronized)
58 addResultToPage(text);
61 for (var i = 0; i < results.length; ++i)
62 addResultToPage(results[i]);
63 resultsSynchronized = true;
66 function clearResults()
68 InspectorTest.evaluateInPage("clearOutput()");
71 function addResultToPage(text)
73 InspectorTest.evaluateInPage("output(unescape('" + escape(text) + "'))");
77 console.error = InspectorTest.addResult;
79 InspectorTest.addResults = function(textArray)
83 for (var i = 0, size = textArray.length; i < size; ++i)
84 InspectorTest.addResult(textArray[i]);
87 InspectorTest.addObject = function(object, nondeterministicProps, prefix, firstLinePrefix)
89 prefix = prefix || "";
90 firstLinePrefix = firstLinePrefix || prefix;
91 InspectorTest.addResult(firstLinePrefix + "{");
92 for (var prop in object) {
93 if (typeof object.hasOwnProperty === "function" && !object.hasOwnProperty(prop))
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 + "\"");
106 InspectorTest.addResult(prefixWithName + propValue);
108 InspectorTest.addResult(prefix + "}");
111 InspectorTest.assertGreaterOrEqual = function(expected, actual, message)
113 if (actual < expected)
114 InspectorTest.addResult("FAILED: " + (message ? message + ": " : "") + actual + " < " + expected);
117 InspectorTest.reloadPage = function(callback)
119 InspectorTest._reloadPageCallback = InspectorTest.safeWrap(callback);
121 if (WebInspector.panels.network)
122 WebInspector.panels.network._reset();
123 InspectorAgent.reloadPage(false);
126 InspectorTest.pageReloaded = function()
128 resultsSynchronized = false;
129 InspectorTest.addResult("Page reloaded.");
130 if (InspectorTest._reloadPageCallback) {
131 var callback = InspectorTest._reloadPageCallback;
132 delete InspectorTest._reloadPageCallback;
137 InspectorTest.runAfterPendingDispatches = function(callback)
139 callback = InspectorTest.safeWrap(callback);
140 InspectorBackend.runAfterPendingDispatches(callback);
143 InspectorTest.createKeyEvent = function(keyIdentifier)
145 var evt = document.createEvent("KeyboardEvent");
146 evt.initKeyboardEvent("keydown", true /* can bubble */, true /* can cancel */, null /* view */, keyIdentifier, "");
150 InspectorTest.runTestSuite = function(testSuite)
152 var testSuiteTests = testSuite.slice();
156 if (!testSuiteTests.length) {
157 InspectorTest.completeTest();
160 var nextTest = testSuiteTests.shift();
161 InspectorTest.addResult("");
162 InspectorTest.addResult("Running: " + /function\s([^(]*)/.exec(nextTest)[1]);
163 InspectorTest.safeWrap(nextTest)(runner, runner);
168 InspectorTest.assertEquals = function(expected, found, message)
170 if (expected === found)
175 error = "Failure (" + message + "):";
178 throw new Error(error + " expected <" + expected + "> found <" + found + ">");
181 InspectorTest.safeWrap = function(func, onexception)
189 return func.apply(wrapThis, arguments);
191 InspectorTest.addResult("Exception while running: " + func + "\n" + (e.stack || e));
193 InspectorTest.safeWrap(onexception)();
195 InspectorTest.completeTest();
201 InspectorTest.addSniffer = function(receiver, methodName, override, opt_sticky)
203 override = InspectorTest.safeWrap(override);
205 var original = receiver[methodName];
206 if (typeof original !== "function")
207 throw ("Cannot find method to override: " + methodName);
209 receiver[methodName] = function(var_args) {
211 var result = original.apply(this, arguments);
214 receiver[methodName] = original;
216 // In case of exception the override won't be called.
218 override.apply(this, arguments);
220 throw ("Exception in overriden method '" + methodName + "': " + e);
228 var runTestCallId = 0;
229 var completeTestCallId = 1;
231 function runAfterIframeIsLoaded()
233 if (window.layoutTestController)
234 layoutTestController.waitUntilDone();
237 if (!window.iframeLoaded)
238 setTimeout(step, 100);
242 setTimeout(step, 100);
245 function runTest(enableWatchDogWhileDebugging)
247 if (!window.layoutTestController)
250 layoutTestController.dumpAsText();
251 layoutTestController.waitUntilDone();
253 function runTestInFrontend(initializationFunctions, testFunction, completeTestCallId)
255 if (window.InspectorTest) {
256 InspectorTest.pageReloaded();
261 InspectorTest.completeTestCallId = completeTestCallId;
263 for (var i = 0; i < initializationFunctions.length; ++i) {
265 initializationFunctions[i]();
267 console.error("Exception in test initialization: " + e);
268 InspectorTest.completeTest();
272 WebInspector.showPanel("elements");
276 console.error("Exception during test execution: " + e);
277 InspectorTest.completeTest();
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());
286 var parameters = ["[" + initializationFunctions + "]", test, completeTestCallId];
287 var toEvaluate = "(" + runTestInFrontend + ")(" + parameters.join(", ") + ");";
288 layoutTestController.evaluateInWebInspector(runTestCallId, toEvaluate);
290 if (enableWatchDogWhileDebugging) {
293 console.log("Internal watchdog triggered at 10 seconds. Test timed out.");
294 closeInspectorAndNotifyDone();
296 window._watchDogTimer = setTimeout(watchDog, 10000);
300 function didEvaluateForTestInFrontend(callId)
302 if (callId !== completeTestCallId)
304 delete window.completeTestCallId;
305 closeInspectorAndNotifyDone();
308 function closeInspectorAndNotifyDone()
310 if (window._watchDogTimer)
311 clearTimeout(window._watchDogTimer);
313 layoutTestController.closeWebInspector();
314 setTimeout(function() {
315 layoutTestController.notifyDone();
321 function output(text)
323 if (!outputElement) {
324 outputElement = document.createElement("div");
325 outputElement.className = "output";
326 outputElement.style.whiteSpace = "pre";
327 document.body.appendChild(outputElement);
329 outputElement.appendChild(document.createTextNode(text));
330 outputElement.appendChild(document.createElement("br"));
333 function clearOutput()
336 outputElement.parentNode.removeChild(outputElement);
337 outputElement = null;