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 function testDispatchQueueIsEmpty() {
18 if (!WebInspector.dispatchQueueIsEmpty()) {
19 // Wait for unprocessed messages.
20 setTimeout(testDispatchQueueIsEmpty, 10);
23 RuntimeAgent.evaluate("didEvaluateForTestInFrontend(" + InspectorTest.completeTestCallId + ", \"\")", "test");
25 testDispatchQueueIsEmpty();
28 InspectorTest.evaluateInConsole = function(code, callback)
30 callback = InspectorTest.safeWrap(callback);
32 WebInspector.consoleView.visible = true;
33 WebInspector.consoleView.prompt.text = code;
34 var event = document.createEvent("KeyboardEvent");
35 event.initKeyboardEvent("keydown", true, true, null, "Enter", "");
36 WebInspector.consoleView.prompt.proxyElement.dispatchEvent(event);
37 InspectorTest.addConsoleSniffer(
38 function(commandResult) {
39 callback(commandResult.toMessageElement().textContent);
43 InspectorTest.evaluateInConsoleAndDump = function(code, callback)
45 callback = InspectorTest.safeWrap(callback);
47 function mycallback(text)
49 InspectorTest.addResult(code + " = " + text);
52 InspectorTest.evaluateInConsole(code, mycallback);
55 InspectorTest.evaluateInPage = function(code, callback)
57 callback = InspectorTest.safeWrap(callback);
59 function mycallback(error, result, wasThrown)
62 callback(WebInspector.RemoteObject.fromPayload(result), wasThrown);
64 RuntimeAgent.evaluate(code, "console", false, mycallback);
67 InspectorTest.evaluateInPageWithTimeout = function(code)
69 InspectorTest.evaluateInPage("setTimeout(unescape('" + escape(code) + "'))");
72 InspectorTest.addResult = function(text)
75 if (resultsSynchronized)
76 addResultToPage(text);
79 for (var i = 0; i < results.length; ++i)
80 addResultToPage(results[i]);
81 resultsSynchronized = true;
84 function clearResults()
86 InspectorTest.evaluateInPage("clearOutput()");
89 function addResultToPage(text)
91 InspectorTest.evaluateInPage("output(unescape('" + escape(text) + "'))");
95 InspectorTest.addResults = function(textArray)
99 for (var i = 0, size = textArray.length; i < size; ++i)
100 InspectorTest.addResult(textArray[i]);
103 function onError(event)
105 window.removeEventListener("error", onError);
106 InspectorTest.addResult("Uncaught exception in inspector front-end: " + event.message + " [" + event.filename + ":" + event.lineno + "]");
107 InspectorTest.completeTest();
110 window.addEventListener("error", onError);
112 InspectorTest.addObject = function(object, nondeterministicProps, prefix, firstLinePrefix)
114 prefix = prefix || "";
115 firstLinePrefix = firstLinePrefix || prefix;
116 InspectorTest.addResult(firstLinePrefix + "{");
117 for (var prop in object) {
118 if (typeof object.hasOwnProperty === "function" && !object.hasOwnProperty(prop))
120 var prefixWithName = " " + prefix + prop + " : ";
121 var propValue = object[prop];
122 if (nondeterministicProps && prop in nondeterministicProps)
123 InspectorTest.addResult(prefixWithName + "<" + typeof propValue + ">");
125 InspectorTest.dump(propValue, nondeterministicProps, " " + prefix, prefixWithName);
127 InspectorTest.addResult(prefix + "}");
130 InspectorTest.addArray = function(array, nondeterministicProps, prefix, firstLinePrefix)
132 prefix = prefix || "";
133 firstLinePrefix = firstLinePrefix || prefix;
134 InspectorTest.addResult(firstLinePrefix + "[");
135 for (var i = 0; i < array.length; ++i)
136 InspectorTest.dump(array[i], nondeterministicProps, prefix + " ");
137 InspectorTest.addResult(prefix + "]");
140 InspectorTest.dump = function(value, nondeterministicProps, prefix, prefixWithName)
142 prefixWithName = prefixWithName || prefix;
143 if (prefixWithName && prefixWithName.length > 80) {
144 InspectorTest.addResult(prefixWithName + "was skipped due to prefix length limit");
148 InspectorTest.addResult(prefixWithName + "null");
149 else if (value instanceof Array)
150 InspectorTest.addArray(value, nondeterministicProps, prefix, prefixWithName);
151 else if (typeof value === "object")
152 InspectorTest.addObject(value, nondeterministicProps, prefix, prefixWithName);
153 else if (typeof value === "string")
154 InspectorTest.addResult(prefixWithName + "\"" + value + "\"");
156 InspectorTest.addResult(prefixWithName + value);
159 InspectorTest.assertGreaterOrEqual = function(expected, actual, message)
161 if (actual < expected)
162 InspectorTest.addResult("FAILED: " + (message ? message + ": " : "") + actual + " < " + expected);
165 InspectorTest.navigate = function(url, callback)
167 InspectorTest._pageLoadedCallback = InspectorTest.safeWrap(callback);
169 if (WebInspector.panels.network)
170 WebInspector.panels.network._reset();
171 InspectorTest.evaluateInConsole("window.location = '" + url + "'");
174 InspectorTest.reloadPage = function(callback)
176 InspectorTest._pageLoadedCallback = InspectorTest.safeWrap(callback);
178 if (WebInspector.panels.network)
179 WebInspector.panels.network._reset();
180 PageAgent.reload(false);
183 InspectorTest.pageLoaded = function()
185 resultsSynchronized = false;
186 InspectorTest.addResult("Page reloaded.");
187 if (InspectorTest._pageLoadedCallback) {
188 var callback = InspectorTest._pageLoadedCallback;
189 delete InspectorTest._pageLoadedCallback;
194 InspectorTest.runWhenPageLoads = function(callback)
196 var oldCallback = InspectorTest._pageLoadedCallback;
197 function chainedCallback()
203 InspectorTest._pageLoadedCallback = InspectorTest.safeWrap(chainedCallback);
206 InspectorTest.runAfterPendingDispatches = function(callback)
208 callback = InspectorTest.safeWrap(callback);
209 InspectorBackend.runAfterPendingDispatches(callback);
212 InspectorTest.createKeyEvent = function(keyIdentifier, ctrlKey, altKey, shiftKey, metaKey)
214 var evt = document.createEvent("KeyboardEvent");
215 evt.initKeyboardEvent("keydown", true /* can bubble */, true /* can cancel */, null /* view */, keyIdentifier, "", ctrlKey, altKey, shiftKey, metaKey);
219 InspectorTest.runTestSuite = function(testSuite)
221 var testSuiteTests = testSuite.slice();
225 if (!testSuiteTests.length) {
226 InspectorTest.completeTest();
229 var nextTest = testSuiteTests.shift();
230 InspectorTest.addResult("");
231 InspectorTest.addResult("Running: " + /function\s([^(]*)/.exec(nextTest)[1]);
232 InspectorTest.safeWrap(nextTest)(runner, runner);
237 InspectorTest.assertEquals = function(expected, found, message)
239 if (expected === found)
244 error = "Failure (" + message + "):";
247 throw new Error(error + " expected <" + expected + "> found <" + found + ">");
250 InspectorTest.assertTrue = function(found, message)
252 InspectorTest.assertEquals(true, !!found, message);
255 InspectorTest.safeWrap = function(func, onexception)
263 return func.apply(wrapThis, arguments);
265 InspectorTest.addResult("Exception while running: " + func + "\n" + (e.stack || e));
267 InspectorTest.safeWrap(onexception)();
269 InspectorTest.completeTest();
275 InspectorTest.addSniffer = function(receiver, methodName, override, opt_sticky)
277 override = InspectorTest.safeWrap(override);
279 var original = receiver[methodName];
280 if (typeof original !== "function")
281 throw ("Cannot find method to override: " + methodName);
283 receiver[methodName] = function(var_args) {
285 var result = original.apply(this, arguments);
288 receiver[methodName] = original;
290 // In case of exception the override won't be called.
292 override.apply(this, arguments);
294 throw ("Exception in overriden method '" + methodName + "': " + e);
300 InspectorTest.addConsoleSniffer = function(override, opt_sticky)
302 InspectorTest.addSniffer(WebInspector.ConsoleView.prototype, "_appendConsoleMessage", override, opt_sticky);
305 InspectorTest.override = function(receiver, methodName, override, opt_sticky)
307 override = InspectorTest.safeWrap(override);
309 var original = receiver[methodName];
310 if (typeof original !== "function")
311 throw ("Cannot find method to override: " + methodName);
313 receiver[methodName] = function(var_args) {
316 var result = override.apply(this, arguments);
318 throw ("Exception in overriden method '" + methodName + "': " + e);
322 receiver[methodName] = original;
330 InspectorTest.textContentWithLineBreaks = function(node)
333 var currentNode = node;
334 while (currentNode = currentNode.traverseNextNode(node)) {
335 if (currentNode.nodeType === Node.TEXT_NODE)
336 buffer += currentNode.nodeValue;
337 else if (currentNode.nodeName === "LI")
339 else if (currentNode.classList.contains("console-message"))
347 var runTestCallId = 0;
348 var completeTestCallId = 1;
349 var frontendReopeningCount = 0;
351 function reopenFrontend()
353 closeFrontend(openFrontendAndIncrement);
356 function closeFrontend(callback)
358 // Do this asynchronously to allow InspectorBackendDispatcher to send response
359 // back to the frontend before it's destroyed.
360 setTimeout(function() {
361 layoutTestController.closeWebInspector();
366 function openFrontendAndIncrement()
368 frontendReopeningCount++;
369 layoutTestController.showWebInspector();
373 function runAfterIframeIsLoaded()
375 if (window.layoutTestController)
376 layoutTestController.waitUntilDone();
379 if (!window.iframeLoaded)
380 setTimeout(step, 100);
384 setTimeout(step, 100);
387 function runTest(enableWatchDogWhileDebugging)
389 if (!window.layoutTestController)
392 layoutTestController.dumpAsText();
393 layoutTestController.waitUntilDone();
395 function runTestInFrontend(initializationFunctions, testFunction, completeTestCallId)
397 if (window.InspectorTest) {
398 InspectorTest.pageLoaded();
403 InspectorTest.completeTestCallId = completeTestCallId;
405 for (var i = 0; i < initializationFunctions.length; ++i) {
407 initializationFunctions[i]();
409 console.error("Exception in test initialization: " + e);
410 InspectorTest.completeTest();
414 WebInspector.showPanel("audits");
418 console.error("Exception during test execution: " + e);
419 InspectorTest.completeTest();
423 var initializationFunctions = [ String(initialize_InspectorTest) ];
424 for (var name in window) {
425 if (name.indexOf("initialize_") === 0 && typeof window[name] === "function" && name !== "initialize_InspectorTest")
426 initializationFunctions.push(window[name].toString());
428 var parameters = ["[" + initializationFunctions + "]", test, completeTestCallId];
429 var toEvaluate = "(" + runTestInFrontend + ")(" + parameters.join(", ") + ");";
430 layoutTestController.evaluateInWebInspector(runTestCallId, toEvaluate);
432 if (enableWatchDogWhileDebugging) {
435 console.log("Internal watchdog triggered at 20 seconds. Test timed out.");
436 closeInspectorAndNotifyDone();
438 window._watchDogTimer = setTimeout(watchDog, 20000);
442 function didEvaluateForTestInFrontend(callId)
444 if (callId !== completeTestCallId)
446 delete window.completeTestCallId;
447 // Close inspector asynchrously to allow caller of this
448 // function send response before backend dispatcher and frontend are destroyed.
449 setTimeout(closeInspectorAndNotifyDone, 0);
452 function closeInspectorAndNotifyDone()
454 if (window._watchDogTimer)
455 clearTimeout(window._watchDogTimer);
457 layoutTestController.closeWebInspector();
458 setTimeout(function() {
459 layoutTestController.notifyDone();
465 function output(text)
467 if (!outputElement) {
468 var intermediate = document.createElement("div");
469 document.body.appendChild(intermediate);
471 var intermediate2 = document.createElement("div");
472 intermediate.appendChild(intermediate2);
474 outputElement = document.createElement("div");
475 outputElement.className = "output";
476 outputElement.style.whiteSpace = "pre";
477 intermediate2.appendChild(outputElement);
479 outputElement.appendChild(document.createTextNode(text));
480 outputElement.appendChild(document.createElement("br"));
483 function clearOutput()
486 outputElement.parentNode.removeChild(outputElement);
487 outputElement = null;