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 | |
| 331 * fail. Cleared when results are reported from runTest() or asyncTestDone(). | |
| 332 * @type {Array.<Error>} | |
| 333 **/ | |
| 334 var errors = []; | |
| 335 | |
| 336 /** | |
| 337 * Notifies the running browser test that any async javascript is complete. | |
| 338 * @param {Array.<boolean, string>=} result When passed, this is used for the | |
| 339 * asyncTestResult message. | |
| 340 **/ | |
| 341 function asyncTestDone(result) { | |
| 342 if (!asyncTestIsDone) { | |
| 343 asyncTestIsDone = true; | |
| 344 chrome.send('asyncTestResult', result ? result : testResult()); | |
| 345 errors.splice(0, errors.length); | |
| 346 } | |
| 347 } | |
| 348 | |
| 349 /** | |
| 350 * Returns [success, message] & clears |errors|. | |
| 351 * @return {Array.<boolean, string>} | |
| 352 **/ | |
| 353 function testResult() { | |
| 354 var result = [true, '']; | |
| 355 if (errors.length) { | |
| 356 var message = ''; | |
| 357 for (var i = 0; i < errors.length; ++i) { | |
| 358 message += 'Failed: ' + currentTestFunction + '(' + | |
| 359 currentTestArguments.map(JSON.stringify) + | |
| 360 ')\n' + errors[i].stack; | |
| 361 } | |
| 362 result = [false, message]; | |
| 363 } | |
| 364 return result; | |
| 365 } | |
| 366 | |
| 312 // Asserts. | 367 // Asserts. |
| 313 // Use the following assertions to verify a condition within a test. | 368 // Use the following assertions to verify a condition within a test. |
| 314 // If assertion fails, the C++ backend will be immediately notified. | 369 // If assertion fails, the C++ backend will be immediately notified. |
|
mmenke
2011/08/10 17:23:40
This is not true for async tests.
Sheridan Rawlins
2011/08/11 01:58:44
Done.
| |
| 315 // If assertion passes, no notification will be sent to the C++ backend. | 370 // If assertion passes, no notification will be sent to the C++ backend. |
| 316 | 371 |
| 317 /** | 372 /** |
| 318 * When |test| !== true, aborts the current test. | 373 * When |test| !== true, aborts the current test. |
| 319 * @param {boolean} test The predicate to check against |expected|. | 374 * @param {boolean} test The predicate to check against |expected|. |
| 320 * @param {string=} message The message to include in the Error thrown. | 375 * @param {string=} message The message to include in the Error thrown. |
| 321 * @throws {Error} upon failure. | 376 * @throws {Error} upon failure. |
| 322 **/ | 377 **/ |
| 323 function assertTrue(test, message) { | 378 function assertTrue(test, message) { |
| 324 helper.registerCall(); | 379 helper.registerCall(); |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 438 * Always aborts the current test. | 493 * Always aborts the current test. |
| 439 * @param {string=} message The message to include in the Error thrown. | 494 * @param {string=} message The message to include in the Error thrown. |
| 440 * @throws {Error} always. | 495 * @throws {Error} always. |
| 441 **/ | 496 **/ |
| 442 function assertNotReached(message) { | 497 function assertNotReached(message) { |
| 443 helper.registerCall(); | 498 helper.registerCall(); |
| 444 throw new Error(helper.getCallMessage(message)); | 499 throw new Error(helper.getCallMessage(message)); |
| 445 } | 500 } |
| 446 | 501 |
| 447 /** | 502 /** |
| 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 | 503 * 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 | 504 * failure. The new function stuffs any errors into the |errors| array for |
| 456 * checking by runTest. This allows tests to continue running other checks, | 505 * checking by runTest. This allows tests to continue running other checks, |
| 457 * while failing the overal test if any errors occurrred. | 506 * while failing the overal test if any errors occurrred. |
|
mmenke
2011/08/10 17:23:40
nit: overall
Sheridan Rawlins
2011/08/11 01:58:44
Done.
| |
| 458 * @param {Function} assertFunc The function which may throw an Error. | 507 * @param {Function} assertFunc The function which may throw an Error. |
| 459 * @return {Function} A function that applies its arguments to |assertFunc|. | 508 * @return {Function} A function that applies its arguments to |assertFunc|. |
| 460 * @see errors | 509 * @see errors |
| 461 * @see runTest | 510 * @see runTest |
| 462 **/ | 511 **/ |
| 463 function createExpect(assertFunc) { | 512 function createExpect(assertFunc) { |
| 464 var expectFunc = function() { | 513 var expectFunc = function() { |
| 465 try { | 514 try { |
| 466 assertFunc.apply(null, arguments); | 515 assertFunc.apply(null, arguments); |
| 467 } catch (e) { | 516 } catch (e) { |
| 468 errors.push(e); | 517 errors.push(e); |
| 469 } | 518 } |
| 470 }; | 519 }; |
| 471 expectFunc.isExpect = true; | 520 expectFunc.isExpect = true; |
| 472 expectFunc.expectName = assertFunc.name.replace(/^assert/, 'expect'); | 521 expectFunc.expectName = assertFunc.name.replace(/^assert/, 'expect'); |
| 473 return expectFunc; | 522 return expectFunc; |
| 474 } | 523 } |
| 475 | 524 |
| 476 /** | 525 /** |
| 477 * This is the starting point for tests run by WebUIBrowserTest. If an error | 526 * This is the starting point for tests run by WebUIBrowserTest. If an error |
| 478 * occurs, it reports a failure and a message created by joining individual | 527 * occurs, it reports a failure and a message created by joining individual |
| 479 * error messages. | 528 * error messages. The |errors| is cleared before returning the results. |
| 480 * @param {string} testFunction The function name to call. | 529 * @param {string} testFunction The function name to call. |
| 481 * @param {Array} testArguments The arguments to call |testFunction| with. | 530 * @param {Array} testArguments The arguments to call |testFunction| with. |
| 482 * @return {Array.<boolean, string>} [test-succeeded, message-if-failed] | 531 * @return {Array.<boolean, string>} [test-succeeded, message-if-failed] |
| 483 * @see errors | 532 * @see errors |
| 484 * @see runTestFunction | 533 * @see runTestFunction |
| 485 **/ | 534 **/ |
| 486 function runTest(testFunction, testArguments) { | 535 function runTest(testFunction, testArguments) { |
| 487 // Avoid eval() if at all possible, since it will not work on pages | 536 // Avoid eval() if at all possible, since it will not work on pages |
| 488 // that have enabled content-security-policy. | 537 // that have enabled content-security-policy. |
| 489 var testBody = this[testFunction]; // global object -- not a method. | 538 var testBody = this[testFunction]; // global object -- not a method. |
| 490 if (typeof testBody === "undefined") | 539 var testName = testFunction; |
| 540 if (typeof testBody === "undefined") { | |
| 491 testBody = eval(testFunction); | 541 testBody = eval(testFunction); |
| 542 testName = testBody.toString(); | |
| 543 } | |
| 492 if (testBody != RUN_TEST_F) { | 544 if (testBody != RUN_TEST_F) { |
| 493 var testName = | |
| 494 testFunction.name ? testFunction.name : testBody.toString(); | |
| 495 console.log('Running test ' + testName); | 545 console.log('Running test ' + testName); |
| 496 } | 546 } |
| 497 return runTestFunction(testFunction, testBody, testArguments); | 547 var result = runTestFunction(testFunction, testBody, testArguments); |
| 548 errors.splice(0, errors.length); | |
| 549 return result; | |
| 498 } | 550 } |
| 499 | 551 |
| 500 /** | 552 /** |
| 501 * This is the guts of WebUIBrowserTest. It clears |errors|, runs the | 553 * This is the guts of WebUIBrowserTest. It runs the test surrounded by an |
| 502 * test surrounded by an expect to catch Errors. If |errors| is | 554 * expect to catch Errors. If |errors| is non-empty, it reports a failure and |
| 503 * non-empty, it reports a failure and a message by joining |errors|. | 555 * a message by joining |errors|. Consumers can use this to use assert/expect |
| 504 * Consumers can use this to use assert/expect functions asynchronously, | 556 * functions asynchronously, but are then responsible for reporting errors to |
| 505 * but are then responsible for reporting errors to the browser themselves. | 557 * the browser themselves. |
| 506 * @param {string} testFunction The function name to report on failure. | 558 * @param {string} testFunction The function name to report on failure. |
| 507 * @param {Function} testBody The function to call. | 559 * @param {Function} testBody The function to call. |
| 508 * @param {Array} testArguments The arguments to call |testBody| with. | 560 * @param {Array} testArguments The arguments to call |testBody| with. |
| 509 * @return {Array.<boolean, string>} [test-succeeded, message-if-failed] | 561 * @return {Array.<boolean, string>} [test-succeeded, message-if-failed] |
| 510 * @see errors | |
| 511 * @see createExpect | 562 * @see createExpect |
|
mmenke
2011/08/10 17:23:40
nit: @see asyncTestDone?
Sheridan Rawlins
2011/08/11 01:58:44
Done.
| |
| 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 |