Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 * @fileoverview Library providing basic test framework functionality. | 6 * @fileoverview Library providing basic test framework functionality. |
| 7 **/ | 7 **/ |
| 8 | 8 |
| 9 /** | 9 /** |
| 10 * Namespace for |Test|. | 10 * Namespace for |Test|. |
| 11 * @type {Object} | 11 * @type {Object} |
| 12 **/ | 12 **/ |
| 13 var testing = {}; | 13 var testing = {}; |
| 14 | 14 |
| 15 /** | 15 /** |
| 16 * Hold the currentTestCase across between PreLoad and Run. | 16 * Hold the currentTestCase across between PreLoad and Run. |
| 17 * @type {TestCase} | 17 * @type {TestCase} |
| 18 **/ | 18 **/ |
| 19 var currentTestCase = null; | 19 var currentTestCase = null; |
| 20 | 20 |
| 21 /** | |
| 22 * @type {?string} The string representation of the currently running test | |
| 23 * function. | |
| 24 */ | |
| 25 var currentTestFunction = null; | |
| 26 | |
| 27 /** | |
| 28 * @type {?Array} The arguments of the currently running test. | |
| 29 */ | |
| 30 var currentTestArguments = null; | |
| 31 | |
| 21 (function() { | 32 (function() { |
| 22 // Provide global objects for generation case. | 33 // Provide global objects for generation case. |
| 23 if (this['window'] === undefined) | 34 if (this['window'] === undefined) |
| 24 this['window'] = this; | 35 this['window'] = this; |
| 25 if (this['chrome'] === undefined) { | 36 if (this['chrome'] === undefined) { |
| 26 this['chrome'] = { | 37 this['chrome'] = { |
| 27 send: function() {}, | 38 send: function() {}, |
| 28 }; | 39 }; |
| 29 } | 40 } |
| 30 if (this['console'] === undefined) { | 41 if (this['console'] === undefined) { |
| (...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 302 return callMessage; | 313 return callMessage; |
| 303 }, | 314 }, |
| 304 }; | 315 }; |
| 305 | 316 |
| 306 /** | 317 /** |
| 307 * Help register calls for better error reporting. | 318 * Help register calls for better error reporting. |
| 308 * @type {CallHelper} | 319 * @type {CallHelper} |
| 309 **/ | 320 **/ |
| 310 var helper = new CallHelper(); | 321 var helper = new CallHelper(); |
| 311 | 322 |
| 323 /** | |
| 324 * true when asyncTestDone has been called. | |
| 325 * @type {boolean} | |
| 326 */ | |
| 327 var asyncTestIsDone = false; | |
| 328 | |
| 329 /** | |
| 330 * Holds the errors, if any, caught by expects so that the test case can fail. | |
|
mmenke
2011/08/08 16:38:25
nit: Add "Cleared when errors are reported to the
Sheridan Rawlins
2011/08/10 01:58:53
Done.
| |
| 331 * @type {Array.<Error>} | |
| 332 **/ | |
| 333 var errors = []; | |
| 334 | |
| 335 /** | |
| 336 * Notifies the running browser test that any async javascript is complete. | |
| 337 * @param {Array.<boolean, string>=} result When passed, this is used for the | |
| 338 * asyncTestResult message. | |
| 339 **/ | |
| 340 function asyncTestDone(result) { | |
| 341 if (!asyncTestIsDone) { | |
| 342 asyncTestIsDone = true; | |
| 343 chrome.send('asyncTestResult', result ? result : testResult()); | |
| 344 errors.splice(0, errors.length); | |
| 345 } | |
| 346 } | |
| 347 | |
| 348 /** | |
| 349 * Returns [success, message] & clears |errors|. | |
| 350 * @return {Array.<boolean, string>} | |
| 351 **/ | |
| 352 function testResult() { | |
| 353 var result = [true, '']; | |
| 354 if (errors.length) { | |
| 355 var message = ''; | |
| 356 for (var i = 0; i < errors.length; ++i) { | |
| 357 message += 'Failed: ' + currentTestFunction + '(' + | |
| 358 currentTestArguments.map(JSON.stringify) + | |
| 359 ')\n' + errors[i].stack; | |
| 360 } | |
| 361 result = [false, message]; | |
| 362 } | |
| 363 return result; | |
| 364 } | |
| 365 | |
| 312 // Asserts. | 366 // Asserts. |
| 313 // Use the following assertions to verify a condition within a test. | 367 // Use the following assertions to verify a condition within a test. |
| 314 // If assertion fails, the C++ backend will be immediately notified. | 368 // If assertion fails, the C++ backend will be immediately notified. |
| 315 // If assertion passes, no notification will be sent to the C++ backend. | 369 // If assertion passes, no notification will be sent to the C++ backend. |
| 316 | 370 |
| 317 /** | 371 /** |
| 318 * When |test| !== true, aborts the current test. | 372 * When |test| !== true, aborts the current test. |
| 319 * @param {boolean} test The predicate to check against |expected|. | 373 * @param {boolean} test The predicate to check against |expected|. |
| 320 * @param {string=} message The message to include in the Error thrown. | 374 * @param {string=} message The message to include in the Error thrown. |
| 321 * @throws {Error} upon failure. | 375 * @throws {Error} upon failure. |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 438 * Always aborts the current test. | 492 * Always aborts the current test. |
| 439 * @param {string=} message The message to include in the Error thrown. | 493 * @param {string=} message The message to include in the Error thrown. |
| 440 * @throws {Error} always. | 494 * @throws {Error} always. |
| 441 **/ | 495 **/ |
| 442 function assertNotReached(message) { | 496 function assertNotReached(message) { |
| 443 helper.registerCall(); | 497 helper.registerCall(); |
| 444 throw new Error(helper.getCallMessage(message)); | 498 throw new Error(helper.getCallMessage(message)); |
| 445 } | 499 } |
| 446 | 500 |
| 447 /** | 501 /** |
| 448 * Holds the errors, if any, caught by expects so that the test case can fail. | |
| 449 * @type {Array.<Error>} | |
| 450 **/ | |
| 451 var errors = []; | |
| 452 | |
| 453 /** | |
| 454 * Creates a function based upon a function that thows an exception on | 502 * Creates a function based upon a function that thows an exception on |
| 455 * failure. The new function stuffs any errors into the |errors| array for | 503 * failure. The new function stuffs any errors into the |errors| array for |
| 456 * checking by runTest. This allows tests to continue running other checks, | 504 * checking by runTest. This allows tests to continue running other checks, |
| 457 * while failing the overal test if any errors occurrred. | 505 * while failing the overal test if any errors occurrred. |
| 458 * @param {Function} assertFunc The function which may throw an Error. | 506 * @param {Function} assertFunc The function which may throw an Error. |
| 459 * @return {Function} A function that applies its arguments to |assertFunc|. | 507 * @return {Function} A function that applies its arguments to |assertFunc|. |
| 460 * @see errors | 508 * @see errors |
| 461 * @see runTest | 509 * @see runTest |
| 462 **/ | 510 **/ |
| 463 function createExpect(assertFunc) { | 511 function createExpect(assertFunc) { |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 480 * @param {string} testFunction The function name to call. | 528 * @param {string} testFunction The function name to call. |
| 481 * @param {Array} testArguments The arguments to call |testFunction| with. | 529 * @param {Array} testArguments The arguments to call |testFunction| with. |
| 482 * @return {Array.<boolean, string>} [test-succeeded, message-if-failed] | 530 * @return {Array.<boolean, string>} [test-succeeded, message-if-failed] |
| 483 * @see errors | 531 * @see errors |
| 484 * @see runTestFunction | 532 * @see runTestFunction |
| 485 **/ | 533 **/ |
| 486 function runTest(testFunction, testArguments) { | 534 function runTest(testFunction, testArguments) { |
| 487 // Avoid eval() if at all possible, since it will not work on pages | 535 // Avoid eval() if at all possible, since it will not work on pages |
| 488 // that have enabled content-security-policy. | 536 // that have enabled content-security-policy. |
| 489 var testBody = this[testFunction]; // global object -- not a method. | 537 var testBody = this[testFunction]; // global object -- not a method. |
| 490 if (typeof testBody === "undefined") | 538 var testName = testFunction; |
| 539 if (typeof testBody === "undefined") { | |
| 491 testBody = eval(testFunction); | 540 testBody = eval(testFunction); |
| 541 testName = testBody.toString(); | |
| 542 } | |
| 492 if (testBody != RUN_TEST_F) { | 543 if (testBody != RUN_TEST_F) { |
| 493 var testName = | |
| 494 testFunction.name ? testFunction.name : testBody.toString(); | |
| 495 console.log('Running test ' + testName); | 544 console.log('Running test ' + testName); |
| 496 } | 545 } |
| 497 return runTestFunction(testFunction, testBody, testArguments); | 546 var result = runTestFunction(testFunction, testBody, testArguments); |
| 547 errors.splice(0, errors.length); | |
| 548 return result; | |
| 498 } | 549 } |
| 499 | 550 |
| 500 /** | 551 /** |
| 501 * This is the guts of WebUIBrowserTest. It clears |errors|, runs the | 552 * This is the guts of WebUIBrowserTest. It clears |errors|, runs the |
|
mmenke
2011/08/08 16:38:25
nit: It no longer clears errors.
Sheridan Rawlins
2011/08/10 01:58:53
Done.
| |
| 502 * test surrounded by an expect to catch Errors. If |errors| is | 553 * test surrounded by an expect to catch Errors. If |errors| is |
| 503 * non-empty, it reports a failure and a message by joining |errors|. | 554 * non-empty, it reports a failure and a message by joining |errors|. |
| 504 * Consumers can use this to use assert/expect functions asynchronously, | 555 * Consumers can use this to use assert/expect functions asynchronously, |
| 505 * but are then responsible for reporting errors to the browser themselves. | 556 * but are then responsible for reporting errors to the browser themselves. |
| 506 * @param {string} testFunction The function name to report on failure. | 557 * @param {string} testFunction The function name to report on failure. |
| 507 * @param {Function} testBody The function to call. | 558 * @param {Function} testBody The function to call. |
| 508 * @param {Array} testArguments The arguments to call |testBody| with. | 559 * @param {Array} testArguments The arguments to call |testBody| with. |
| 509 * @return {Array.<boolean, string>} [test-succeeded, message-if-failed] | 560 * @return {Array.<boolean, string>} [test-succeeded, message-if-failed] |
| 510 * @see errors | 561 * @see errors |
|
mmenke
2011/08/08 16:38:25
nit: This should probably be moved back up to run
Sheridan Rawlins
2011/08/10 01:58:53
Done.
| |
| 511 * @see createExpect | 562 * @see createExpect |
| 512 **/ | 563 **/ |
| 513 function runTestFunction(testFunction, testBody, testArguments) { | 564 function runTestFunction(testFunction, testBody, testArguments) { |
| 514 errors.splice(0, errors.length); | 565 currentTestFunction = testFunction; |
| 566 currentTestArguments = testArguments; | |
| 515 createExpect(testBody).apply(null, testArguments); | 567 createExpect(testBody).apply(null, testArguments); |
| 516 | 568 return testResult(); |
| 517 var result = [true]; | |
| 518 if (errors.length) { | |
| 519 var message = ''; | |
| 520 for (var i = 0; i < errors.length; ++i) { | |
| 521 message += 'Failed: ' + testFunction + '(' + | |
| 522 testArguments.map(JSON.stringify) + | |
| 523 ')\n' + errors[i].stack; | |
| 524 } | |
| 525 errors.splice(0, errors.length); | |
| 526 result = [false, message]; | |
| 527 } | |
| 528 return result; | |
| 529 } | 569 } |
| 530 | 570 |
| 531 /** | 571 /** |
| 532 * Creates a new test case for the given |testFixture| and |testName|. Assumes | 572 * Creates a new test case for the given |testFixture| and |testName|. Assumes |
| 533 * |testFixture| describes a globally available subclass of type Test. | 573 * |testFixture| describes a globally available subclass of type Test. |
| 534 * @param {string} testFixture The fixture for this test case. | 574 * @param {string} testFixture The fixture for this test case. |
| 535 * @param {string} testName The name for this test case. | 575 * @param {string} testName The name for this test case. |
| 536 * @return {TestCase} A newly created TestCase. | 576 * @return {TestCase} A newly created TestCase. |
| 537 **/ | 577 **/ |
| 538 function createTestCase(testFixture, testName) { | 578 function createTestCase(testFixture, testName) { |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 703 * @return {string} description of the matcher for this argument. | 743 * @return {string} description of the matcher for this argument. |
| 704 **/ | 744 **/ |
| 705 describe: function() { | 745 describe: function() { |
| 706 return 'SaveArguments(' + | 746 return 'SaveArguments(' + |
| 707 this.realMatcher_.describe.call(this.realMatcher_) + ')'; | 747 this.realMatcher_.describe.call(this.realMatcher_) + ')'; |
| 708 }, | 748 }, |
| 709 }; | 749 }; |
| 710 | 750 |
| 711 // Exports. | 751 // Exports. |
| 712 testing.Test = Test; | 752 testing.Test = Test; |
| 753 window.asyncTestDone = asyncTestDone; | |
| 713 window.assertTrue = assertTrue; | 754 window.assertTrue = assertTrue; |
| 714 window.assertFalse = assertFalse; | 755 window.assertFalse = assertFalse; |
| 715 window.assertGE = assertGE; | 756 window.assertGE = assertGE; |
| 716 window.assertGT = assertGT; | 757 window.assertGT = assertGT; |
| 717 window.assertEquals = assertEquals; | 758 window.assertEquals = assertEquals; |
| 718 window.assertLE = assertLE; | 759 window.assertLE = assertLE; |
| 719 window.assertLT = assertLT; | 760 window.assertLT = assertLT; |
| 720 window.assertNotEquals = assertNotEquals; | 761 window.assertNotEquals = assertNotEquals; |
| 721 window.assertNotReached = assertNotReached; | 762 window.assertNotReached = assertNotReached; |
| 722 window.callFunction = callFunction; | 763 window.callFunction = callFunction; |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 735 window.runTest = runTest; | 776 window.runTest = runTest; |
| 736 window.runTestFunction = runTestFunction; | 777 window.runTestFunction = runTestFunction; |
| 737 window.SaveArgumentsMatcher = SaveArgumentsMatcher; | 778 window.SaveArgumentsMatcher = SaveArgumentsMatcher; |
| 738 window.TEST = TEST; | 779 window.TEST = TEST; |
| 739 window.TEST_F = TEST_F; | 780 window.TEST_F = TEST_F; |
| 740 window.GEN = GEN; | 781 window.GEN = GEN; |
| 741 | 782 |
| 742 // Import the Mock4JS helpers. | 783 // Import the Mock4JS helpers. |
| 743 Mock4JS.addMockSupport(window); | 784 Mock4JS.addMockSupport(window); |
| 744 })(); | 785 })(); |
| OLD | NEW |