2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * @fileoverview This file contains small testing framework along with the
34 * test suite for the frontend. These tests are a part of the continues build
35 * and are executed by the devtools_sanity_unittest.cc as a part of the
36 * Interactive UI Test suite.
37 * FIXME: change field naming style to use trailing underscore.
40 if (window.domAutomationController) {
42 var ___interactiveUiTestsMode = true;
45 * Test suite for interactive UI tests.
48 TestSuite = function()
50 this.controlTaken_ = false;
56 * Reports test failure.
57 * @param {string} message Failure description.
59 TestSuite.prototype.fail = function(message)
61 if (this.controlTaken_)
62 this.reportFailure_(message);
69 * Equals assertion tests that expected === actual.
70 * @param {Object} expected Expected object.
71 * @param {Object} actual Actual object.
72 * @param {string} opt_message User message to print if the test fails.
74 TestSuite.prototype.assertEquals = function(expected, actual, opt_message)
76 if (expected !== actual) {
77 var message = "Expected: '" + expected + "', but was '" + actual + "'";
79 message = opt_message + "(" + message + ")";
85 * True assertion tests that value == true.
86 * @param {Object} value Actual object.
87 * @param {string} opt_message User message to print if the test fails.
89 TestSuite.prototype.assertTrue = function(value, opt_message)
91 this.assertEquals(true, !!value, opt_message);
96 * Contains assertion tests that string contains substring.
97 * @param {string} string Outer.
98 * @param {string} substring Inner.
100 TestSuite.prototype.assertContains = function(string, substring)
102 if (string.indexOf(substring) === -1)
103 this.fail("Expected to: '" + string + "' to contain '" + substring + "'");
108 * Takes control over execution.
110 TestSuite.prototype.takeControl = function()
112 this.controlTaken_ = true;
113 // Set up guard timer.
115 this.timerId_ = setTimeout(function() {
116 self.reportFailure_("Timeout exceeded: 20 sec");
122 * Releases control over execution.
124 TestSuite.prototype.releaseControl = function()
126 if (this.timerId_ !== -1) {
127 clearTimeout(this.timerId_);
135 * Async tests use this one to report that they are completed.
137 TestSuite.prototype.reportOk_ = function()
139 window.domAutomationController.send("[OK]");
144 * Async tests use this one to report failures.
146 TestSuite.prototype.reportFailure_ = function(error)
148 if (this.timerId_ !== -1) {
149 clearTimeout(this.timerId_);
152 window.domAutomationController.send("[FAILED] " + error);
157 * Runs all global functions starting with "test" as unit tests.
159 TestSuite.prototype.runTest = function(testName)
163 if (!this.controlTaken_)
166 this.reportFailure_(e);
172 * @param {string} panelName Name of the panel to show.
174 TestSuite.prototype.showPanel = function(panelName)
176 // Open Scripts panel.
177 var toolbar = document.getElementById("toolbar");
178 var button = toolbar.getElementsByClassName(panelName)[0];
180 this.assertEquals(WebInspector.panels[panelName], WebInspector.inspectorView.currentPanel());
185 * Overrides the method with specified name until it's called first time.
186 * @param {Object} receiver An object whose method to override.
187 * @param {string} methodName Name of the method to override.
188 * @param {Function} override A function that should be called right after the
189 * overriden method returns.
190 * @param {boolean} opt_sticky Whether restore original method after first run
193 TestSuite.prototype.addSniffer = function(receiver, methodName, override, opt_sticky)
195 var orig = receiver[methodName];
196 if (typeof orig !== "function")
197 this.fail("Cannot find method to override: " + methodName);
199 receiver[methodName] = function(var_args) {
201 var result = orig.apply(this, arguments);
204 receiver[methodName] = orig;
206 // In case of exception the override won't be called.
208 override.apply(this, arguments);
210 test.fail("Exception in overriden method '" + methodName + "': " + e);
217 TestSuite.prototype.testEnableResourcesTab = function()
219 // FIXME once reference is removed downstream.
222 TestSuite.prototype.testCompletionOnPause = function()
224 // FIXME once reference is removed downstream.
231 * Tests that scripts tab can be open and populated with inspected scripts.
233 TestSuite.prototype.testShowScriptsTab = function()
235 this.showPanel("scripts");
237 // There should be at least main page script.
238 this._waitUntilScriptsAreParsed(["debugger_test_page.html"],
240 test.releaseControl();
242 // Wait until all scripts are added to the debugger.
248 * Tests that scripts tab is populated with inspected scripts even if it
249 * hadn't been shown by the moment inspected paged refreshed.
250 * @see http://crbug.com/26312
252 TestSuite.prototype.testScriptsTabIsPopulatedOnInspectedPageRefresh = function()
255 this.assertEquals(WebInspector.panels.elements, WebInspector.inspectorView.currentPanel(), "Elements panel should be current one.");
257 WebInspector.debuggerPresentationModel.addEventListener(WebInspector.DebuggerPresentationModel.Events.DebuggerReset, waitUntilScriptIsParsed);
259 // Reload inspected page. It will reset the debugger agent.
260 test.evaluateInConsole_("window.location.reload(true);", function(resultText) {});
262 function waitUntilScriptIsParsed()
264 WebInspector.debuggerPresentationModel.removeEventListener(WebInspector.DebuggerPresentationModel.Events.DebuggerReset, waitUntilScriptIsParsed);
265 test.showPanel("scripts");
266 test._waitUntilScriptsAreParsed(["debugger_test_page.html"],
268 test.releaseControl();
272 // Wait until all scripts are added to the debugger.
278 * Tests that scripts list contains content scripts.
280 TestSuite.prototype.testContentScriptIsPresent = function()
282 this.showPanel("scripts");
285 test._waitUntilScriptsAreParsed(
286 ["page_with_content_script.html", "simple_content_script.js"],
288 test.releaseControl();
291 // Wait until all scripts are added to the debugger.
297 * Tests that scripts are not duplicaed on Scripts tab switch.
299 TestSuite.prototype.testNoScriptDuplicatesOnPanelSwitch = function()
303 // There should be two scripts: one for the main page and another
304 // one which is source of console API(see
305 // InjectedScript._ensureCommandLineAPIInstalled).
306 var expectedScriptsCount = 2;
307 var parsedScripts = [];
309 this.showPanel("scripts");
312 function switchToElementsTab() {
313 test.showPanel("elements");
314 setTimeout(switchToScriptsTab, 0);
317 function switchToScriptsTab() {
318 test.showPanel("scripts");
319 setTimeout(checkScriptsPanel, 0);
322 function checkScriptsPanel() {
323 test.assertTrue(!!WebInspector.panels.scripts.visibleView, "No visible script view.");
324 test.assertTrue(test._scriptsAreParsed(["debugger_test_page.html"]), "Some scripts are missing.");
326 test.releaseControl();
329 function checkNoDuplicates() {
330 var scriptSelect = document.getElementById("scripts-files");
331 var options = scriptSelect.options;
332 for (var i = 0; i < options.length; i++) {
333 var scriptName = options[i].text;
334 for (var j = i + 1; j < options.length; j++)
335 test.assertTrue(scriptName !== options[j].text, "Found script duplicates: " + test.optionsToString_(options));
339 test._waitUntilScriptsAreParsed(
340 ["debugger_test_page.html"],
343 setTimeout(switchToElementsTab, 0);
347 // Wait until all scripts are added to the debugger.
352 // Tests that debugger works correctly if pause event occurs when DevTools
353 // frontend is being loaded.
354 TestSuite.prototype.testPauseWhenLoadingDevTools = function()
356 this.showPanel("scripts");
358 // Script execution can already be paused.
359 if (WebInspector.debuggerModel.debuggerPausedDetails)
362 this._waitForScriptPause(this.releaseControl.bind(this));
367 // Tests that pressing "Pause" will pause script execution if the script
368 // is already running.
369 TestSuite.prototype.testPauseWhenScriptIsRunning = function()
371 this.showPanel("scripts");
373 this.evaluateInConsole_(
374 'setTimeout("handleClick()" , 0)',
375 didEvaluateInConsole.bind(this));
377 function didEvaluateInConsole(resultText) {
378 this.assertTrue(!isNaN(resultText), "Failed to get timer id: " + resultText);
379 // Wait for some time to make sure that inspected page is running the
381 setTimeout(testScriptPause.bind(this), 300);
384 function testScriptPause() {
385 // The script should be in infinite loop. Click "Pause" button to
386 // pause it and wait for the result.
387 WebInspector.panels.scripts.pauseButton.click();
389 this._waitForScriptPause(this.releaseControl.bind(this));
397 * Tests network size.
399 TestSuite.prototype.testNetworkSize = function()
403 function finishResource(resource, finishTime)
405 test.assertEquals(219, resource.transferSize, "Incorrect total encoded data length");
406 test.assertEquals(25, resource.resourceSize, "Incorrect total data length");
407 test.releaseControl();
410 this.addSniffer(WebInspector.NetworkDispatcher.prototype, "_finishResource", finishResource);
412 // Reload inspected page to sniff network events
413 test.evaluateInConsole_("window.location.reload(true);", function(resultText) {});
420 * Tests network sync size.
422 TestSuite.prototype.testNetworkSyncSize = function()
426 function finishResource(resource, finishTime)
428 test.assertEquals(219, resource.transferSize, "Incorrect total encoded data length");
429 test.assertEquals(25, resource.resourceSize, "Incorrect total data length");
430 test.releaseControl();
433 this.addSniffer(WebInspector.NetworkDispatcher.prototype, "_finishResource", finishResource);
435 // Send synchronous XHR to sniff network events
436 test.evaluateInConsole_("var xhr = new XMLHttpRequest(); xhr.open(\"GET\", \"chunked\", false); xhr.send(null);", function() {});
443 * Tests network raw headers text.
445 TestSuite.prototype.testNetworkRawHeadersText = function()
449 function finishResource(resource, finishTime)
451 if (!resource.responseHeadersText)
452 test.fail("Failure: resource does not have response headers text");
453 test.assertEquals(164, resource.responseHeadersText.length, "Incorrect response headers text length");
454 test.releaseControl();
457 this.addSniffer(WebInspector.NetworkDispatcher.prototype, "_finishResource", finishResource);
459 // Reload inspected page to sniff network events
460 test.evaluateInConsole_("window.location.reload(true);", function(resultText) {});
467 * Tests network timing.
469 TestSuite.prototype.testNetworkTiming = function()
473 function finishResource(resource, finishTime)
475 // Setting relaxed expectations to reduce flakiness.
476 // Server sends headers after 100ms, then sends data during another 100ms.
477 // We expect these times to be measured at least as 70ms.
478 test.assertTrue(resource.timing.receiveHeadersEnd - resource.timing.connectStart >= 70,
479 "Time between receiveHeadersEnd and connectStart should be >=70ms, but was " +
480 "receiveHeadersEnd=" + resource.timing.receiveHeadersEnd + ", connectStart=" + resource.timing.connectStart + ".");
481 test.assertTrue(resource.responseReceivedTime - resource.startTime >= 0.07,
482 "Time between responseReceivedTime and startTime should be >=0.07s, but was " +
483 "responseReceivedTime=" + resource.responseReceivedTime + ", startTime=" + resource.startTime + ".");
484 test.assertTrue(resource.endTime - resource.startTime >= 0.14,
485 "Time between endTime and startTime should be >=0.14s, but was " +
486 "endtime=" + resource.endTime + ", startTime=" + resource.startTime + ".");
488 test.releaseControl();
491 this.addSniffer(WebInspector.NetworkDispatcher.prototype, "_finishResource", finishResource);
493 // Reload inspected page to sniff network events
494 test.evaluateInConsole_("window.location.reload(true);", function(resultText) {});
500 TestSuite.prototype.testConsoleOnNavigateBack = function()
502 if (WebInspector.console.messages.length === 1)
503 firstConsoleMessageReceived.call(this);
505 WebInspector.console.addEventListener(WebInspector.ConsoleModel.Events.MessageAdded, firstConsoleMessageReceived, this);
507 function firstConsoleMessageReceived() {
508 this.evaluateInConsole_("clickLink();", didClickLink.bind(this));
511 function didClickLink() {
512 // Check that there are no new messages(command is not a message).
513 this.assertEquals(1, WebInspector.console.messages.length);
514 this.assertEquals(1, WebInspector.console.messages[0].totalRepeatCount);
515 this.evaluateInConsole_("history.back();", didNavigateBack.bind(this));
518 function didNavigateBack()
520 // Make sure navigation completed and possible console messages were pushed.
521 this.evaluateInConsole_("void 0;", didCompleteNavigation.bind(this));
524 function didCompleteNavigation() {
525 this.assertEquals(1, WebInspector.console.messages.length);
526 this.assertEquals(1, WebInspector.console.messages[0].totalRepeatCount);
527 this.releaseControl();
534 TestSuite.prototype.testReattachAfterCrash = function()
536 this.evaluateInConsole_("1+1;", this.releaseControl.bind(this));
541 TestSuite.prototype.testSharedWorker = function()
543 function didEvaluateInConsole(resultText) {
544 this.assertEquals("2011", resultText);
545 this.releaseControl();
547 this.evaluateInConsole_("globalVar", didEvaluateInConsole.bind(this));
552 TestSuite.prototype.testPauseInSharedWorkerInitialization = function()
554 if (WebInspector.debuggerModel.debuggerPausedDetails)
556 this._waitForScriptPause(this.releaseControl.bind(this));
562 * Serializes options collection to string.
563 * @param {HTMLOptionsCollection} options
566 TestSuite.prototype.optionsToString_ = function(options)
569 for (var i = 0; i < options.length; i++)
570 names.push('"' + options[i].text + '"');
571 return names.join(",");
576 * Ensures that main HTML resource is selected in Scripts panel and that its
577 * source frame is setup. Invokes the callback when the condition is satisfied.
578 * @param {HTMLOptionsCollection} options
579 * @param {function(WebInspector.SourceView,string)} callback
581 TestSuite.prototype.showMainPageScriptSource_ = function(scriptName, callback)
585 var scriptSelect = document.getElementById("scripts-files");
586 var options = scriptSelect.options;
588 test.assertTrue(options.length, "Scripts list is empty");
590 // Select page's script if it's not current option.
592 if (options[scriptSelect.selectedIndex].text === scriptName)
593 scriptResource = options[scriptSelect.selectedIndex].representedObject;
595 var pageScriptIndex = -1;
596 for (var i = 0; i < options.length; i++) {
597 if (options[i].text === scriptName) {
602 test.assertTrue(-1 !== pageScriptIndex, "Script with url " + scriptName + " not found among " + test.optionsToString_(options));
603 scriptResource = options[pageScriptIndex].representedObject;
605 // Current panel is "Scripts".
606 WebInspector.inspectorView.currentPanel()._showScriptOrResource(scriptResource);
607 test.assertEquals(pageScriptIndex, scriptSelect.selectedIndex, "Unexpected selected option index.");
610 test.assertTrue(scriptResource instanceof WebInspector.Resource,
611 "Unexpected resource class.");
612 test.assertTrue(!!scriptResource.url, "Resource URL is null.");
613 test.assertTrue(scriptResource.url.search(scriptName + "$") !== -1, "Main HTML resource should be selected.");
615 var scriptsPanel = WebInspector.panels.scripts;
617 var view = scriptsPanel.visibleView;
618 test.assertTrue(view instanceof WebInspector.SourceView);
620 if (!view.sourceFrame._loaded) {
621 test.addSniffer(view, "_sourceFrameSetupFinished", function(event) {
622 callback(view, scriptResource.url);
625 callback(view, scriptResource.url);
630 * Evaluates the code in the console as if user typed it manually and invokes
631 * the callback when the result message is received and added to the console.
632 * @param {string} code
633 * @param {function(string)} callback
635 TestSuite.prototype.evaluateInConsole_ = function(code, callback)
637 WebInspector.showConsole();
638 WebInspector.consoleView.prompt.text = code;
639 WebInspector.consoleView.promptElement.dispatchEvent(TestSuite.createKeyEvent("Enter"));
641 this.addSniffer(WebInspector.ConsoleView.prototype, "_appendConsoleMessage",
642 function(commandResult) {
643 callback(commandResult.toMessageElement().textContent);
649 * Checks that all expected scripts are present in the scripts list
650 * in the Scripts panel.
651 * @param {Array.<string>} expected Regular expressions describing
652 * expected script names.
653 * @return {boolean} Whether all the scripts are in "scripts-files" select
656 TestSuite.prototype._scriptsAreParsed = function(expected)
658 var scriptSelect = document.getElementById("scripts-files");
659 var options = scriptSelect.options;
661 // Check that at least all the expected scripts are present.
662 var missing = expected.slice(0);
663 for (var i = 0 ; i < options.length; i++) {
664 for (var j = 0; j < missing.length; j++) {
665 if (options[i].text.search(missing[j]) !== -1) {
666 missing.splice(j, 1);
671 return missing.length === 0;
676 * Waits for script pause, checks expectations, and invokes the callback.
677 * @param {function():void} callback
679 TestSuite.prototype._waitForScriptPause = function(callback)
681 function pauseListener(event) {
682 WebInspector.debuggerModel.removeEventListener(WebInspector.DebuggerModel.Events.DebuggerPaused, pauseListener, this);
685 WebInspector.debuggerModel.addEventListener(WebInspector.DebuggerModel.Events.DebuggerPaused, pauseListener, this);
690 * Waits until all the scripts are parsed and asynchronously executes the code
691 * in the inspected page.
693 TestSuite.prototype._executeCodeWhenScriptsAreParsed = function(code, expectedScripts)
697 function executeFunctionInInspectedPage() {
698 // Since breakpoints are ignored in evals' calculate() function is
699 // execute after zero-timeout so that the breakpoint is hit.
700 test.evaluateInConsole_(
701 'setTimeout("' + code + '" , 0)',
702 function(resultText) {
703 test.assertTrue(!isNaN(resultText), "Failed to get timer id: " + resultText + ". Code: " + code);
707 test._waitUntilScriptsAreParsed(expectedScripts, executeFunctionInInspectedPage);
712 * Waits until all the scripts are parsed and invokes the callback.
714 TestSuite.prototype._waitUntilScriptsAreParsed = function(expectedScripts, callback)
718 function waitForAllScripts() {
719 if (test._scriptsAreParsed(expectedScripts))
722 test.addSniffer(WebInspector.panels.scripts, "_addOptionToFilesSelect", waitForAllScripts);
730 * Key event with given key identifier.
732 TestSuite.createKeyEvent = function(keyIdentifier)
734 var evt = document.createEvent("KeyboardEvent");
735 evt.initKeyboardEvent("keydown", true /* can bubble */, true /* can cancel */, null /* view */, keyIdentifier, "");
741 * Test runner for the test suite.
747 * Run each test from the test suit on a fresh instance of the suite.
749 uiTests.runAllTests = function()
751 // For debugging purposes.
752 for (var name in TestSuite.prototype) {
753 if (name.substring(0, 4) === "test" && typeof TestSuite.prototype[name] === "function")
754 uiTests.runTest(name);
760 * Run specified test on a fresh instance of the test suite.
761 * @param {string} name Name of a test method from TestSuite class.
763 uiTests.runTest = function(name)
765 if (uiTests._populatedInterface)
766 new TestSuite().runTest(name);
768 uiTests._pendingTestName = name;
775 uiTests._populatedInterface = true;
776 var name = uiTests._pendingTestName;
777 delete uiTests._pendingTestName;
779 new TestSuite().runTest(name);
782 var oldShowElementsPanel = WebInspector.showElementsPanel;
783 WebInspector.showElementsPanel = function()
785 oldShowElementsPanel.call(this);
789 var oldShowPanel = WebInspector.showPanel;
790 WebInspector.showPanel = function(name)
792 oldShowPanel.call(this, name);