| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 | 5 |
| 6 /** | 6 /** |
| 7 * @fileoverview This file contains small testing framework along with the | 7 * @fileoverview This file contains small testing framework along with the |
| 8 * test suite for the frontend. These tests are a part of the continues build | 8 * test suite for the frontend. These tests are a part of the continues build |
| 9 * and are executed by the devtools_sanity_unittest.cc as a part of the | 9 * and are executed by the devtools_sanity_unittest.cc as a part of the |
| 10 * Interactive UI Test suite. | 10 * Interactive UI Test suite. |
| (...skipping 442 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 453 TestSuite.prototype.testSetBreakpoint = function() { | 453 TestSuite.prototype.testSetBreakpoint = function() { |
| 454 var parsedDebuggerTestPageHtml = false; | 454 var parsedDebuggerTestPageHtml = false; |
| 455 var parsedDebuggerTestJs = false; | 455 var parsedDebuggerTestJs = false; |
| 456 | 456 |
| 457 this.showPanel('scripts'); | 457 this.showPanel('scripts'); |
| 458 | 458 |
| 459 var scriptUrl = null; | 459 var scriptUrl = null; |
| 460 var breakpointLine = 12; | 460 var breakpointLine = 12; |
| 461 | 461 |
| 462 var test = this; | 462 var test = this; |
| 463 var orig = devtools.DebuggerAgent.prototype.handleScriptsResponse_; | |
| 464 this.addSniffer(devtools.DebuggerAgent.prototype, 'handleScriptsResponse_', | 463 this.addSniffer(devtools.DebuggerAgent.prototype, 'handleScriptsResponse_', |
| 465 function(msg) { | 464 function(msg) { |
| 466 var scriptSelect = document.getElementById('scripts-files'); | 465 var scriptSelect = document.getElementById('scripts-files'); |
| 467 var options = scriptSelect.options; | 466 var options = scriptSelect.options; |
| 468 | 467 |
| 469 // There should be console API source (see | 468 // There should be console API source (see |
| 470 // InjectedScript._ensureCommandLineAPIInstalled) and the page script. | 469 // InjectedScript._ensureCommandLineAPIInstalled) and the page script. |
| 471 test.assertEquals(2, options.length, 'Unexpected number of scripts.'); | 470 test.assertEquals(2, options.length, 'Unexpected number of scripts(' + |
| 471 test.optionsToString_(options) + ')'); |
| 472 | 472 |
| 473 // Select page's script if it's not current option. | 473 test.showMainPageScriptSource_( |
| 474 var scriptResource; | 474 'debugger_test_page.html', |
| 475 if (options[scriptSelect.selectedIndex].text == | 475 function(view, url) { |
| 476 'debugger_test_page.html') { | 476 view._addBreakpoint(breakpointLine); |
| 477 scriptResource = | 477 // Force v8 execution. |
| 478 options[scriptSelect.selectedIndex].representedObject; | 478 RemoteToolsAgent.ExecuteVoidJavaScript(); |
| 479 } else { | 479 test.waitForSetBreakpointResponse_(url, breakpointLine, |
| 480 var pageScriptIndex = (1 - scriptSelect.selectedIndex); | 480 function() { |
| 481 test.assertEquals('debugger_test_page.html', | 481 test.releaseControl(); |
| 482 options[pageScriptIndex].text); | 482 }); |
| 483 scriptResource = options[pageScriptIndex].representedObject; | 483 }); |
| 484 // Current panel is 'Scripts'. | |
| 485 WebInspector.currentPanel._showScriptOrResource(scriptResource); | |
| 486 } | |
| 487 | |
| 488 test.assertTrue(scriptResource instanceof WebInspector.Resource, | |
| 489 'Unexpected resource class.'); | |
| 490 test.assertTrue(!!scriptResource.url, 'Resource URL is null.'); | |
| 491 test.assertTrue( | |
| 492 scriptResource.url.search(/debugger_test_page.html$/) != -1, | |
| 493 'Main HTML resource should be selected.'); | |
| 494 | |
| 495 // Store for access from setbreakpoint handler. | |
| 496 scriptUrl = scriptResource.url; | |
| 497 | |
| 498 var scriptsPanel = WebInspector.panels.scripts; | |
| 499 | |
| 500 var view = scriptsPanel.visibleView; | |
| 501 test.assertTrue(view instanceof WebInspector.SourceView); | |
| 502 | |
| 503 if (!view.sourceFrame._isContentLoaded()) { | |
| 504 test.addSniffer(view, '_sourceFrameSetupFinished', function(event) { | |
| 505 view._addBreakpoint(breakpointLine); | |
| 506 // Force v8 execution. | |
| 507 RemoteToolsAgent.ExecuteVoidJavaScript(); | |
| 508 }); | |
| 509 } else { | |
| 510 view._addBreakpoint(breakpointLine); | |
| 511 // Force v8 execution. | |
| 512 RemoteToolsAgent.ExecuteVoidJavaScript(); | |
| 513 } | |
| 514 }); | 484 }); |
| 515 | 485 |
| 516 this.addSniffer( | 486 |
| 487 this.takeControl(); |
| 488 }; |
| 489 |
| 490 |
| 491 /** |
| 492 * Serializes options collection to string. |
| 493 * @param {HTMLOptionsCollection} options |
| 494 * @return {string} |
| 495 */ |
| 496 TestSuite.prototype.optionsToString_ = function(options) { |
| 497 var names = []; |
| 498 for (var i = 0; i < options.length; i++) { |
| 499 names.push('"' + options[i].text + '"'); |
| 500 } |
| 501 return names.join(','); |
| 502 }; |
| 503 |
| 504 |
| 505 /** |
| 506 * Ensures that main HTML resource is selected in Scripts panel and that its |
| 507 * source frame is setup. Invokes the callback when the condition is satisfied. |
| 508 * @param {HTMLOptionsCollection} options |
| 509 * @param {function(WebInspector.SourceView,string)} callback |
| 510 */ |
| 511 TestSuite.prototype.showMainPageScriptSource_ = function(scriptName, callback) { |
| 512 var test = this; |
| 513 |
| 514 var scriptSelect = document.getElementById('scripts-files'); |
| 515 var options = scriptSelect.options; |
| 516 |
| 517 // There should be console API source (see |
| 518 // InjectedScript._ensureCommandLineAPIInstalled) and the page script. |
| 519 test.assertEquals(2, options.length, |
| 520 'Unexpected number of scripts(' + test.optionsToString_(options) + ')'); |
| 521 |
| 522 // Select page's script if it's not current option. |
| 523 if (options[scriptSelect.selectedIndex].text !== scriptName) { |
| 524 var pageScriptIndex = -1; |
| 525 for (var i = 0; i < options.length; i++) { |
| 526 if (options[i].text === scriptName) { |
| 527 pageScriptIndex = i; |
| 528 break; |
| 529 } |
| 530 } |
| 531 test.assertTrue(-1 !== pageScriptIndex, |
| 532 'Script with url ' + scriptName + ' not found among ' + |
| 533 test.optionsToString_(options)); |
| 534 // Current panel is 'Scripts'. |
| 535 WebInspector.currentPanel._showScriptOrResource(scriptResource); |
| 536 test.assertEquals(pageScriptIndex, scriptSelect.selectedIndex, |
| 537 'Unexpected selected option index.'); |
| 538 } |
| 539 var scriptResource = options[scriptSelect.selectedIndex].representedObject; |
| 540 |
| 541 test.assertTrue(scriptResource instanceof WebInspector.Resource, |
| 542 'Unexpected resource class.'); |
| 543 test.assertTrue(!!scriptResource.url, 'Resource URL is null.'); |
| 544 test.assertTrue( |
| 545 scriptResource.url.search(scriptName + '$') != -1, |
| 546 'Main HTML resource should be selected.'); |
| 547 |
| 548 var scriptsPanel = WebInspector.panels.scripts; |
| 549 |
| 550 var view = scriptsPanel.visibleView; |
| 551 test.assertTrue(view instanceof WebInspector.SourceView); |
| 552 |
| 553 if (!view.sourceFrame._isContentLoaded()) { |
| 554 test.addSniffer(view, '_sourceFrameSetupFinished', function(event) { |
| 555 callback(view, scriptResource.url); |
| 556 }); |
| 557 } else { |
| 558 callback(view, scriptResource.url); |
| 559 } |
| 560 }; |
| 561 |
| 562 |
| 563 /* |
| 564 * Evaluates the code in the console as if user typed it manually and invokes |
| 565 * the callback when the result message is received and added to the console. |
| 566 * @param {string} code |
| 567 * @param {function(string)} callback |
| 568 */ |
| 569 TestSuite.prototype.evaluateInConsole_ = function(code, callback) { |
| 570 WebInspector.console.visible = true; |
| 571 WebInspector.console.prompt.text = code; |
| 572 WebInspector.console.promptElement.handleKeyEvent( |
| 573 new TestSuite.KeyEvent('Enter')); |
| 574 |
| 575 this.addSniffer(WebInspector.ConsoleView.prototype, 'addMessage', |
| 576 function(commandResult) { |
| 577 callback(commandResult.toMessageElement().textContent); |
| 578 }); |
| 579 }; |
| 580 |
| 581 |
| 582 /* |
| 583 * Waits for 'setbreakpoint' response, checks that corresponding breakpoint |
| 584 * was successfully set and invokes the callback if it was. |
| 585 * @param {string} scriptUrl |
| 586 * @param {number} breakpointLine |
| 587 * @param {function()} callback |
| 588 */ |
| 589 TestSuite.prototype.waitForSetBreakpointResponse_ = function(scriptUrl, |
| 590 breakpointLine, |
| 591 callback) { |
| 592 var test = this; |
| 593 test.addSniffer( |
| 517 devtools.DebuggerAgent.prototype, | 594 devtools.DebuggerAgent.prototype, |
| 518 'handleSetBreakpointResponse_', | 595 'handleSetBreakpointResponse_', |
| 519 function(msg) { | 596 function(msg) { |
| 520 var bps = this.urlToBreakpoints_[scriptUrl]; | 597 var bps = this.urlToBreakpoints_[scriptUrl]; |
| 521 test.assertTrue(!!bps, 'No breakpoints for line ' + breakpointLine); | 598 test.assertTrue(!!bps, 'No breakpoints for line ' + breakpointLine); |
| 522 var line = devtools.DebuggerAgent.webkitToV8LineNumber_(breakpointLine); | 599 var line = devtools.DebuggerAgent.webkitToV8LineNumber_(breakpointLine); |
| 523 test.assertTrue(!!bps[line].getV8Id(), | 600 test.assertTrue(!!bps[line].getV8Id(), |
| 524 'Breakpoint id was not assigned.'); | 601 'Breakpoint id was not assigned.'); |
| 525 test.releaseControl(); | 602 callback(); |
| 526 }); | 603 }); |
| 604 }; |
| 605 |
| 606 |
| 607 /** |
| 608 * Tests eval on call frame. |
| 609 */ |
| 610 TestSuite.prototype.testEvalOnCallFrame = function() { |
| 611 this.showPanel('scripts'); |
| 612 |
| 613 var breakpointLine = 16; |
| 614 |
| 615 var test = this; |
| 616 this.addSniffer(devtools.DebuggerAgent.prototype, 'handleScriptsResponse_', |
| 617 function(msg) { |
| 618 test.showMainPageScriptSource_( |
| 619 'debugger_test_page.html', |
| 620 function(view, url) { |
| 621 view._addBreakpoint(breakpointLine); |
| 622 // Force v8 execution. |
| 623 RemoteToolsAgent.ExecuteVoidJavaScript(); |
| 624 test.waitForSetBreakpointResponse_(url, breakpointLine, |
| 625 setBreakpointCallback); |
| 626 }); |
| 627 }); |
| 628 |
| 629 function setBreakpointCallback() { |
| 630 // Since breakpoints are ignored in evals' calculate() function is |
| 631 // execute after zero-timeout so that the breakpoint is hit. |
| 632 test.evaluateInConsole_( |
| 633 'setTimeout("calculate(123)" , 0)', |
| 634 function(resultText) { |
| 635 test.assertTrue(!isNaN(resultText), |
| 636 'Failed to get timer id: ' + resultText); |
| 637 waitForBreakpointHit(); |
| 638 }); |
| 639 } |
| 640 |
| 641 function waitForBreakpointHit() { |
| 642 test.addSniffer( |
| 643 devtools.DebuggerAgent.prototype, |
| 644 'handleBacktraceResponse_', |
| 645 function(msg) { |
| 646 test.assertEquals(2, this.callFrames_.length, |
| 647 'Unexpected stack depth on the breakpoint. ' + |
| 648 JSON.stringify(msg)); |
| 649 test.assertEquals('calculate', this.callFrames_[0].functionName, |
| 650 'Unexpected top frame function.'); |
| 651 // Evaluate 'e+1' where 'e' is an argument of 'calculate' function. |
| 652 test.evaluateInConsole_( |
| 653 'e+1', |
| 654 function(resultText) { |
| 655 test.assertEquals('124', resultText, 'Unexpected "e+1" value.'); |
| 656 test.releaseControl(); |
| 657 }); |
| 658 }); |
| 659 } |
| 527 | 660 |
| 528 this.takeControl(); | 661 this.takeControl(); |
| 529 }; | 662 }; |
| 530 | 663 |
| 531 | 664 |
| 532 /** | 665 /** |
| 533 * Tests 'Pause' button will pause debugger when a snippet is evaluated. | 666 * Tests 'Pause' button will pause debugger when a snippet is evaluated. |
| 534 */ | 667 */ |
| 535 TestSuite.prototype.testPauseInEval = function() { | 668 TestSuite.prototype.testPauseInEval = function() { |
| 536 this.showPanel('scripts'); | 669 this.showPanel('scripts'); |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 658 initEval(inputs[inputIndex++]); | 791 initEval(inputs[inputIndex++]); |
| 659 } | 792 } |
| 660 }, true); | 793 }, true); |
| 661 | 794 |
| 662 initEval(inputs[inputIndex++]); | 795 initEval(inputs[inputIndex++]); |
| 663 this.takeControl(); | 796 this.takeControl(); |
| 664 }; | 797 }; |
| 665 | 798 |
| 666 | 799 |
| 667 /** | 800 /** |
| 668 * Tests eval on call frame. | |
| 669 */ | |
| 670 TestSuite.prototype.testEvalCallFrame = function() { | |
| 671 }; | |
| 672 | |
| 673 | |
| 674 /** | |
| 675 * Test runner for the test suite. | 801 * Test runner for the test suite. |
| 676 */ | 802 */ |
| 677 var uiTests = {}; | 803 var uiTests = {}; |
| 678 | 804 |
| 679 | 805 |
| 680 /** | 806 /** |
| 681 * Run each test from the test suit on a fresh instance of the suite. | 807 * Run each test from the test suit on a fresh instance of the suite. |
| 682 */ | 808 */ |
| 683 uiTests.runAllTests = function() { | 809 uiTests.runAllTests = function() { |
| 684 // For debugging purposes. | 810 // For debugging purposes. |
| 685 for (var name in TestSuite.prototype) { | 811 for (var name in TestSuite.prototype) { |
| 686 if (name.substring(0, 4) == 'test' && | 812 if (name.substring(0, 4) == 'test' && |
| 687 typeof TestSuite.prototype[name] == 'function') { | 813 typeof TestSuite.prototype[name] == 'function') { |
| 688 uiTests.runTest(name); | 814 uiTests.runTest(name); |
| 689 } | 815 } |
| 690 } | 816 } |
| 691 }; | 817 }; |
| 692 | 818 |
| 693 | 819 |
| 694 /** | 820 /** |
| 695 * Run specified test on a fresh instance of the test suite. | 821 * Run specified test on a fresh instance of the test suite. |
| 696 * @param {string} name Name of a test method from TestSuite class. | 822 * @param {string} name Name of a test method from TestSuite class. |
| 697 */ | 823 */ |
| 698 uiTests.runTest = function(name) { | 824 uiTests.runTest = function(name) { |
| 699 new TestSuite().runTest(name); | 825 new TestSuite().runTest(name); |
| 700 }; | 826 }; |
| 701 | 827 |
| 702 | 828 |
| 703 } | 829 } |
| OLD | NEW |