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.runAfterPendingDispatches = function(callback)
156 callback = InspectorTest.safeWrap(callback);
157 InspectorBackend.runAfterPendingDispatches(callback);
160 InspectorTest.createKeyEvent = function(keyIdentifier, ctrlKey, altKey, shiftKey, metaKey)
162 var evt = document.createEvent("KeyboardEvent");
163 evt.initKeyboardEvent("keydown", true /* can bubble */, true /* can cancel */, null /* view */, keyIdentifier, "", ctrlKey, altKey, shiftKey, metaKey);
167 InspectorTest.runTestSuite = function(testSuite)
169 var testSuiteTests = testSuite.slice();
173 if (!testSuiteTests.length) {
174 InspectorTest.completeTest();
177 var nextTest = testSuiteTests.shift();
178 InspectorTest.addResult("");
179 InspectorTest.addResult("Running: " + /function\s([^(]*)/.exec(nextTest)[1]);
180 InspectorTest.safeWrap(nextTest)(runner, runner);
185 InspectorTest.assertEquals = function(expected, found, message)
187 if (expected === found)
192 error = "Failure (" + message + "):";
195 throw new Error(error + " expected <" + expected + "> found <" + found + ">");
198 InspectorTest.safeWrap = function(func, onexception)
206 return func.apply(wrapThis, arguments);
208 InspectorTest.addResult("Exception while running: " + func + "\n" + (e.stack || e));
210 InspectorTest.safeWrap(onexception)();
212 InspectorTest.completeTest();
218 InspectorTest.addSniffer = function(receiver, methodName, override, opt_sticky)
220 override = InspectorTest.safeWrap(override);
222 var original = receiver[methodName];
223 if (typeof original !== "function")
224 throw ("Cannot find method to override: " + methodName);
226 receiver[methodName] = function(var_args) {
228 var result = original.apply(this, arguments);
231 receiver[methodName] = original;
233 // In case of exception the override won't be called.
235 override.apply(this, arguments);
237 throw ("Exception in overriden method '" + methodName + "': " + e);
243 InspectorTest.override = function(receiver, methodName, override, opt_sticky)
245 override = InspectorTest.safeWrap(override);
247 var original = receiver[methodName];
248 if (typeof original !== "function")
249 throw ("Cannot find method to override: " + methodName);
251 receiver[methodName] = function(var_args) {
254 var result = override.apply(this, arguments);
256 throw ("Exception in overriden method '" + methodName + "': " + e);
260 receiver[methodName] = original;
268 InspectorTest.textContentWithLineBreaks = function(node)
271 var currentNode = node;
272 while (currentNode = currentNode.traverseNextNode(node)) {
273 if (currentNode.nodeType === Node.TEXT_NODE)
274 buffer += currentNode.nodeValue;
275 else if (currentNode.nodeName === "LI")
277 else if (currentNode.classList.contains("console-message"))
285 var runTestCallId = 0;
286 var completeTestCallId = 1;
288 function runAfterIframeIsLoaded()
290 if (window.layoutTestController)
291 layoutTestController.waitUntilDone();
294 if (!window.iframeLoaded)
295 setTimeout(step, 100);
299 setTimeout(step, 100);
302 function runTest(enableWatchDogWhileDebugging)
304 if (!window.layoutTestController)
307 layoutTestController.dumpAsText();
308 layoutTestController.waitUntilDone();
310 function runTestInFrontend(initializationFunctions, testFunction, completeTestCallId)
312 if (window.InspectorTest) {
313 InspectorTest.pageReloaded();
318 InspectorTest.completeTestCallId = completeTestCallId;
320 for (var i = 0; i < initializationFunctions.length; ++i) {
322 initializationFunctions[i]();
324 console.error("Exception in test initialization: " + e);
325 InspectorTest.completeTest();
329 WebInspector.showPanel("console");
333 console.error("Exception during test execution: " + e);
334 InspectorTest.completeTest();
338 var initializationFunctions = [];
339 for (var name in window) {
340 if (name.indexOf("initialize_") === 0 && typeof window[name] === "function")
341 initializationFunctions.push(window[name].toString());
343 var parameters = ["[" + initializationFunctions + "]", test, completeTestCallId];
344 var toEvaluate = "(" + runTestInFrontend + ")(" + parameters.join(", ") + ");";
345 layoutTestController.evaluateInWebInspector(runTestCallId, toEvaluate);
347 if (enableWatchDogWhileDebugging) {
350 console.log("Internal watchdog triggered at 10 seconds. Test timed out.");
351 closeInspectorAndNotifyDone();
353 window._watchDogTimer = setTimeout(watchDog, 10000);
357 function didEvaluateForTestInFrontend(callId)
359 if (callId !== completeTestCallId)
361 delete window.completeTestCallId;
362 // Close inspector asynchrously to allow caller of this
363 // function send response before backend dispatcher and frontend are destroyed.
364 setTimeout(closeInspectorAndNotifyDone, 0);
367 function closeInspectorAndNotifyDone()
369 if (window._watchDogTimer)
370 clearTimeout(window._watchDogTimer);
372 layoutTestController.closeWebInspector();
373 setTimeout(function() {
374 layoutTestController.notifyDone();
380 function output(text)
382 if (!outputElement) {
383 var intermediate = document.createElement("div");
384 document.body.appendChild(intermediate);
386 var intermediate2 = document.createElement("div");
387 intermediate.appendChild(intermediate2);
389 outputElement = document.createElement("div");
390 outputElement.className = "output";
391 outputElement.style.whiteSpace = "pre";
392 intermediate2.appendChild(outputElement);
394 outputElement.appendChild(document.createTextNode(text));
395 outputElement.appendChild(document.createElement("br"));
398 function clearOutput()
401 outputElement.parentNode.removeChild(outputElement);
402 outputElement = null;