1 var initialize_InspectorTest = function() {
4 var resultsSynchronized = false;
6 function consoleOutputHook(messageType)
8 InspectorTest.addResult(messageType + ": " + Array.prototype.slice.call(arguments, 1));
11 console.log = consoleOutputHook.bind(InspectorTest, "log");
12 console.error = consoleOutputHook.bind(InspectorTest, "error");
13 console.info = consoleOutputHook.bind(InspectorTest, "info");
15 InspectorTest.completeTest = function()
17 InspectorAgent.didEvaluateForTestInFrontend(InspectorTest.completeTestCallId, "");
20 InspectorTest.evaluateInConsole = function(code, callback)
22 callback = InspectorTest.safeWrap(callback);
24 WebInspector.console.visible = true;
25 WebInspector.console.prompt.text = code;
26 var event = document.createEvent("KeyboardEvent");
27 event.initKeyboardEvent("keydown", true, true, null, "Enter", "");
28 WebInspector.console.promptElement.dispatchEvent(event);
29 InspectorTest.addSniffer(WebInspector.ConsoleView.prototype, "addMessage",
30 function(commandResult) {
31 callback(commandResult.toMessageElement().textContent);
35 InspectorTest.evaluateInConsoleAndDump = function(code, callback)
37 callback = InspectorTest.safeWrap(callback);
39 function mycallback(text)
41 InspectorTest.addResult(code + " = " + text);
44 InspectorTest.evaluateInConsole(code, mycallback);
47 InspectorTest.evaluateInPage = function(code, callback)
49 callback = InspectorTest.safeWrap(callback);
51 function mycallback(error, result)
54 callback(WebInspector.RemoteObject.fromPayload(result));
56 RuntimeAgent.evaluate(code, "console", false, mycallback);
59 InspectorTest.evaluateInPageWithTimeout = function(code)
61 InspectorTest.evaluateInPage("setTimeout(unescape('" + escape(code) + "'))");
64 InspectorTest.addResult = function(text)
67 if (resultsSynchronized)
68 addResultToPage(text);
71 for (var i = 0; i < results.length; ++i)
72 addResultToPage(results[i]);
73 resultsSynchronized = true;
76 function clearResults()
78 InspectorTest.evaluateInPage("clearOutput()");
81 function addResultToPage(text)
83 InspectorTest.evaluateInPage("output(unescape('" + escape(text) + "'))");
87 InspectorTest.addResults = function(textArray)
91 for (var i = 0, size = textArray.length; i < size; ++i)
92 InspectorTest.addResult(textArray[i]);
95 function onError(event)
97 window.removeEventListener("error", onError);
98 InspectorTest.addResult("Uncaught exception in inspector front-end: " + event.message + " [" + event.filename + ":" + event.lineno + "]");
99 InspectorTest.completeTest();
102 window.addEventListener("error", onError);
104 InspectorTest.addObject = function(object, nondeterministicProps, prefix, firstLinePrefix)
106 prefix = prefix || "";
107 firstLinePrefix = firstLinePrefix || prefix;
108 InspectorTest.addResult(firstLinePrefix + "{");
109 for (var prop in object) {
110 if (typeof object.hasOwnProperty === "function" && !object.hasOwnProperty(prop))
112 var prefixWithName = prefix + " " + prop + " : ";
113 var propValue = object[prop];
114 if (nondeterministicProps && prop in nondeterministicProps)
115 InspectorTest.addResult(prefixWithName + "<" + typeof propValue + ">");
116 else if (propValue === null)
117 InspectorTest.addResult(prefixWithName + "null");
118 else if (typeof propValue === "object")
119 InspectorTest.addObject(propValue, nondeterministicProps, prefix + " ", prefixWithName);
120 else if (typeof propValue === "string")
121 InspectorTest.addResult(prefixWithName + "\"" + propValue + "\"");
123 InspectorTest.addResult(prefixWithName + propValue);
125 InspectorTest.addResult(prefix + "}");
128 InspectorTest.assertGreaterOrEqual = function(expected, actual, message)
130 if (actual < expected)
131 InspectorTest.addResult("FAILED: " + (message ? message + ": " : "") + actual + " < " + expected);
134 InspectorTest.reloadPage = function(callback)
136 InspectorTest._reloadPageCallback = InspectorTest.safeWrap(callback);
138 if (WebInspector.panels.network)
139 WebInspector.panels.network._reset();
140 PageAgent.reloadPage(false);
143 InspectorTest.pageReloaded = function()
145 resultsSynchronized = false;
146 InspectorTest.addResult("Page reloaded.");
147 if (InspectorTest._reloadPageCallback) {
148 var callback = InspectorTest._reloadPageCallback;
149 delete InspectorTest._reloadPageCallback;
154 InspectorTest.runWhenPageLoads = function(callback)
156 var oldCallback = InspectorTest._reloadPageCallback;
157 function chainedCallback()
163 InspectorTest._reloadPageCallback = InspectorTest.safeWrap(chainedCallback);
166 InspectorTest.runAfterPendingDispatches = function(callback)
168 callback = InspectorTest.safeWrap(callback);
169 InspectorBackend.runAfterPendingDispatches(callback);
172 InspectorTest.createKeyEvent = function(keyIdentifier, ctrlKey, altKey, shiftKey, metaKey)
174 var evt = document.createEvent("KeyboardEvent");
175 evt.initKeyboardEvent("keydown", true /* can bubble */, true /* can cancel */, null /* view */, keyIdentifier, "", ctrlKey, altKey, shiftKey, metaKey);
179 InspectorTest.runTestSuite = function(testSuite)
181 var testSuiteTests = testSuite.slice();
185 if (!testSuiteTests.length) {
186 InspectorTest.completeTest();
189 var nextTest = testSuiteTests.shift();
190 InspectorTest.addResult("");
191 InspectorTest.addResult("Running: " + /function\s([^(]*)/.exec(nextTest)[1]);
192 InspectorTest.safeWrap(nextTest)(runner, runner);
197 InspectorTest.assertEquals = function(expected, found, message)
199 if (expected === found)
204 error = "Failure (" + message + "):";
207 throw new Error(error + " expected <" + expected + "> found <" + found + ">");
210 InspectorTest.safeWrap = function(func, onexception)
218 return func.apply(wrapThis, arguments);
220 InspectorTest.addResult("Exception while running: " + func + "\n" + (e.stack || e));
222 InspectorTest.safeWrap(onexception)();
224 InspectorTest.completeTest();
230 InspectorTest.addSniffer = function(receiver, methodName, override, opt_sticky)
232 override = InspectorTest.safeWrap(override);
234 var original = receiver[methodName];
235 if (typeof original !== "function")
236 throw ("Cannot find method to override: " + methodName);
238 receiver[methodName] = function(var_args) {
240 var result = original.apply(this, arguments);
243 receiver[methodName] = original;
245 // In case of exception the override won't be called.
247 override.apply(this, arguments);
249 throw ("Exception in overriden method '" + methodName + "': " + e);
255 InspectorTest.override = function(receiver, methodName, override, opt_sticky)
257 override = InspectorTest.safeWrap(override);
259 var original = receiver[methodName];
260 if (typeof original !== "function")
261 throw ("Cannot find method to override: " + methodName);
263 receiver[methodName] = function(var_args) {
266 var result = override.apply(this, arguments);
268 throw ("Exception in overriden method '" + methodName + "': " + e);
272 receiver[methodName] = original;
280 InspectorTest.textContentWithLineBreaks = function(node)
283 var currentNode = node;
284 while (currentNode = currentNode.traverseNextNode(node)) {
285 if (currentNode.nodeType === Node.TEXT_NODE)
286 buffer += currentNode.nodeValue;
287 else if (currentNode.nodeName === "LI")
289 else if (currentNode.classList.contains("console-message"))
297 var runTestCallId = 0;
298 var completeTestCallId = 1;
300 function runAfterIframeIsLoaded()
302 if (window.layoutTestController)
303 layoutTestController.waitUntilDone();
306 if (!window.iframeLoaded)
307 setTimeout(step, 100);
311 setTimeout(step, 100);
314 function runTest(enableWatchDogWhileDebugging)
316 if (!window.layoutTestController)
319 layoutTestController.dumpAsText();
320 layoutTestController.waitUntilDone();
322 function runTestInFrontend(initializationFunctions, testFunction, completeTestCallId)
324 if (window.InspectorTest) {
325 InspectorTest.pageReloaded();
330 InspectorTest.completeTestCallId = completeTestCallId;
332 for (var i = 0; i < initializationFunctions.length; ++i) {
334 initializationFunctions[i]();
336 console.error("Exception in test initialization: " + e);
337 InspectorTest.completeTest();
341 WebInspector.showPanel("console");
345 console.error("Exception during test execution: " + e);
346 InspectorTest.completeTest();
350 var initializationFunctions = [];
351 for (var name in window) {
352 if (name.indexOf("initialize_") === 0 && typeof window[name] === "function")
353 initializationFunctions.push(window[name].toString());
355 var parameters = ["[" + initializationFunctions + "]", test, completeTestCallId];
356 var toEvaluate = "(" + runTestInFrontend + ")(" + parameters.join(", ") + ");";
357 layoutTestController.evaluateInWebInspector(runTestCallId, toEvaluate);
359 if (enableWatchDogWhileDebugging) {
362 console.log("Internal watchdog triggered at 10 seconds. Test timed out.");
363 closeInspectorAndNotifyDone();
365 window._watchDogTimer = setTimeout(watchDog, 10000);
369 function didEvaluateForTestInFrontend(callId)
371 if (callId !== completeTestCallId)
373 delete window.completeTestCallId;
374 // Close inspector asynchrously to allow caller of this
375 // function send response before backend dispatcher and frontend are destroyed.
376 setTimeout(closeInspectorAndNotifyDone, 0);
379 function closeInspectorAndNotifyDone()
381 if (window._watchDogTimer)
382 clearTimeout(window._watchDogTimer);
384 layoutTestController.closeWebInspector();
385 setTimeout(function() {
386 layoutTestController.notifyDone();
392 function output(text)
394 if (!outputElement) {
395 var intermediate = document.createElement("div");
396 document.body.appendChild(intermediate);
398 var intermediate2 = document.createElement("div");
399 intermediate.appendChild(intermediate2);
401 outputElement = document.createElement("div");
402 outputElement.className = "output";
403 outputElement.style.whiteSpace = "pre";
404 intermediate2.appendChild(outputElement);
406 outputElement.appendChild(document.createTextNode(text));
407 outputElement.appendChild(document.createElement("br"));
410 function clearOutput()
413 outputElement.parentNode.removeChild(outputElement);
414 outputElement = null;