| 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 * Notifies the running browser test that any async javascript is complete. |
| 331 * @param {Array.<boolean, string>=} result When passed, this is used for the |
| 332 * asyncTestResult message. |
| 333 **/ |
| 334 function asyncTestDone(result) { |
| 335 if (!asyncTestIsDone) { |
| 336 asyncTestIsDone = true; |
| 337 chrome.send('asyncTestResult', result ? result : testResult()); |
| 338 } |
| 339 } |
| 340 |
| 341 /** |
| 342 * Returns [success, message] & clears |errors|. |
| 343 * @return {Array.<boolean, string>} |
| 344 **/ |
| 345 function testResult() { |
| 346 var result = [true, '']; |
| 347 if (errors.length) { |
| 348 var message = ''; |
| 349 for (var i = 0; i < errors.length; ++i) { |
| 350 message += 'Failed: ' + currentTestFunction + '(' + |
| 351 currentTestArguments.map(JSON.stringify) + |
| 352 ')\n' + errors[i].stack; |
| 353 } |
| 354 errors.splice(0, errors.length); |
| 355 result = [false, message]; |
| 356 } |
| 357 return result; |
| 358 } |
| 359 |
| 312 // Asserts. | 360 // Asserts. |
| 313 // Use the following assertions to verify a condition within a test. | 361 // Use the following assertions to verify a condition within a test. |
| 314 // If assertion fails, the C++ backend will be immediately notified. | 362 // If assertion fails, the C++ backend will be immediately notified. |
| 315 // If assertion passes, no notification will be sent to the C++ backend. | 363 // If assertion passes, no notification will be sent to the C++ backend. |
| 316 | 364 |
| 317 /** | 365 /** |
| 318 * When |test| !== true, aborts the current test. | 366 * When |test| !== true, aborts the current test. |
| 319 * @param {boolean} test The predicate to check against |expected|. | 367 * @param {boolean} test The predicate to check against |expected|. |
| 320 * @param {string=} message The message to include in the Error thrown. | 368 * @param {string=} message The message to include in the Error thrown. |
| 321 * @throws {Error} upon failure. | 369 * @throws {Error} upon failure. |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 505 * but are then responsible for reporting errors to the browser themselves. | 553 * but are then responsible for reporting errors to the browser themselves. |
| 506 * @param {string} testFunction The function name to report on failure. | 554 * @param {string} testFunction The function name to report on failure. |
| 507 * @param {Function} testBody The function to call. | 555 * @param {Function} testBody The function to call. |
| 508 * @param {Array} testArguments The arguments to call |testBody| with. | 556 * @param {Array} testArguments The arguments to call |testBody| with. |
| 509 * @return {Array.<boolean, string>} [test-succeeded, message-if-failed] | 557 * @return {Array.<boolean, string>} [test-succeeded, message-if-failed] |
| 510 * @see errors | 558 * @see errors |
| 511 * @see createExpect | 559 * @see createExpect |
| 512 **/ | 560 **/ |
| 513 function runTestFunction(testFunction, testBody, testArguments) { | 561 function runTestFunction(testFunction, testBody, testArguments) { |
| 514 errors.splice(0, errors.length); | 562 errors.splice(0, errors.length); |
| 563 currentTestFunction = testFunction; |
| 564 currentTestArguments = testArguments; |
| 515 createExpect(testBody).apply(null, testArguments); | 565 createExpect(testBody).apply(null, testArguments); |
| 516 | 566 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 } | 567 } |
| 530 | 568 |
| 531 /** | 569 /** |
| 532 * Creates a new test case for the given |testFixture| and |testName|. Assumes | 570 * Creates a new test case for the given |testFixture| and |testName|. Assumes |
| 533 * |testFixture| describes a globally available subclass of type Test. | 571 * |testFixture| describes a globally available subclass of type Test. |
| 534 * @param {string} testFixture The fixture for this test case. | 572 * @param {string} testFixture The fixture for this test case. |
| 535 * @param {string} testName The name for this test case. | 573 * @param {string} testName The name for this test case. |
| 536 * @return {TestCase} A newly created TestCase. | 574 * @return {TestCase} A newly created TestCase. |
| 537 **/ | 575 **/ |
| 538 function createTestCase(testFixture, testName) { | 576 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. | 741 * @return {string} description of the matcher for this argument. |
| 704 **/ | 742 **/ |
| 705 describe: function() { | 743 describe: function() { |
| 706 return 'SaveArguments(' + | 744 return 'SaveArguments(' + |
| 707 this.realMatcher_.describe.call(this.realMatcher_) + ')'; | 745 this.realMatcher_.describe.call(this.realMatcher_) + ')'; |
| 708 }, | 746 }, |
| 709 }; | 747 }; |
| 710 | 748 |
| 711 // Exports. | 749 // Exports. |
| 712 testing.Test = Test; | 750 testing.Test = Test; |
| 751 window.asyncTestDone = asyncTestDone; |
| 713 window.assertTrue = assertTrue; | 752 window.assertTrue = assertTrue; |
| 714 window.assertFalse = assertFalse; | 753 window.assertFalse = assertFalse; |
| 715 window.assertGE = assertGE; | 754 window.assertGE = assertGE; |
| 716 window.assertGT = assertGT; | 755 window.assertGT = assertGT; |
| 717 window.assertEquals = assertEquals; | 756 window.assertEquals = assertEquals; |
| 718 window.assertLE = assertLE; | 757 window.assertLE = assertLE; |
| 719 window.assertLT = assertLT; | 758 window.assertLT = assertLT; |
| 720 window.assertNotEquals = assertNotEquals; | 759 window.assertNotEquals = assertNotEquals; |
| 721 window.assertNotReached = assertNotReached; | 760 window.assertNotReached = assertNotReached; |
| 722 window.callFunction = callFunction; | 761 window.callFunction = callFunction; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 735 window.runTest = runTest; | 774 window.runTest = runTest; |
| 736 window.runTestFunction = runTestFunction; | 775 window.runTestFunction = runTestFunction; |
| 737 window.SaveArgumentsMatcher = SaveArgumentsMatcher; | 776 window.SaveArgumentsMatcher = SaveArgumentsMatcher; |
| 738 window.TEST = TEST; | 777 window.TEST = TEST; |
| 739 window.TEST_F = TEST_F; | 778 window.TEST_F = TEST_F; |
| 740 window.GEN = GEN; | 779 window.GEN = GEN; |
| 741 | 780 |
| 742 // Import the Mock4JS helpers. | 781 // Import the Mock4JS helpers. |
| 743 Mock4JS.addMockSupport(window); | 782 Mock4JS.addMockSupport(window); |
| 744 })(); | 783 })(); |
| OLD | NEW |