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 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
441 **/ | 489 **/ |
442 function assertNotReached(message) { | 490 function assertNotReached(message) { |
443 helper.registerCall(); | 491 helper.registerCall(); |
444 throw new Error(helper.getCallMessage(message)); | 492 throw new Error(helper.getCallMessage(message)); |
445 } | 493 } |
446 | 494 |
447 /** | 495 /** |
448 * Holds the errors, if any, caught by expects so that the test case can fail. | 496 * Holds the errors, if any, caught by expects so that the test case can fail. |
449 * @type {Array.<Error>} | 497 * @type {Array.<Error>} |
450 **/ | 498 **/ |
451 var errors = []; | 499 var errors = []; |
mmenke
2011/08/05 16:08:28
This should go above "function testResult()"
Sheridan Rawlins
2011/08/05 16:59:56
Actually after responding to your comment below, m
| |
452 | 500 |
453 /** | 501 /** |
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 |
(...skipping 18 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 return runTestFunction(testFunction, testBody, testArguments); |
498 } | 547 } |
499 | 548 |
500 /** | 549 /** |
501 * This is the guts of WebUIBrowserTest. It clears |errors|, runs the | 550 * This is the guts of WebUIBrowserTest. It clears |errors|, runs the |
502 * test surrounded by an expect to catch Errors. If |errors| is | 551 * test surrounded by an expect to catch Errors. If |errors| is |
503 * non-empty, it reports a failure and a message by joining |errors|. | 552 * non-empty, it reports a failure and a message by joining |errors|. |
504 * Consumers can use this to use assert/expect functions asynchronously, | 553 * Consumers can use this to use assert/expect functions asynchronously, |
505 * but are then responsible for reporting errors to the browser themselves. | 554 * but are then responsible for reporting errors to the browser themselves. |
506 * @param {string} testFunction The function name to report on failure. | 555 * @param {string} testFunction The function name to report on failure. |
507 * @param {Function} testBody The function to call. | 556 * @param {Function} testBody The function to call. |
508 * @param {Array} testArguments The arguments to call |testBody| with. | 557 * @param {Array} testArguments The arguments to call |testBody| with. |
509 * @return {Array.<boolean, string>} [test-succeeded, message-if-failed] | 558 * @return {Array.<boolean, string>} [test-succeeded, message-if-failed] |
510 * @see errors | 559 * @see errors |
511 * @see createExpect | 560 * @see createExpect |
512 **/ | 561 **/ |
513 function runTestFunction(testFunction, testBody, testArguments) { | 562 function runTestFunction(testFunction, testBody, testArguments) { |
514 errors.splice(0, errors.length); | 563 errors.splice(0, errors.length); |
mmenke
2011/08/05 16:08:28
I'm still not sure about calling this here. Seems
Sheridan Rawlins
2011/08/05 16:59:56
Wow, I just had the insight that this allows the c
| |
564 currentTestFunction = testFunction; | |
565 currentTestArguments = testArguments; | |
515 createExpect(testBody).apply(null, testArguments); | 566 createExpect(testBody).apply(null, testArguments); |
516 | 567 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 } | 568 } |
530 | 569 |
531 /** | 570 /** |
532 * Creates a new test case for the given |testFixture| and |testName|. Assumes | 571 * Creates a new test case for the given |testFixture| and |testName|. Assumes |
533 * |testFixture| describes a globally available subclass of type Test. | 572 * |testFixture| describes a globally available subclass of type Test. |
534 * @param {string} testFixture The fixture for this test case. | 573 * @param {string} testFixture The fixture for this test case. |
535 * @param {string} testName The name for this test case. | 574 * @param {string} testName The name for this test case. |
536 * @return {TestCase} A newly created TestCase. | 575 * @return {TestCase} A newly created TestCase. |
537 **/ | 576 **/ |
538 function createTestCase(testFixture, testName) { | 577 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. | 742 * @return {string} description of the matcher for this argument. |
704 **/ | 743 **/ |
705 describe: function() { | 744 describe: function() { |
706 return 'SaveArguments(' + | 745 return 'SaveArguments(' + |
707 this.realMatcher_.describe.call(this.realMatcher_) + ')'; | 746 this.realMatcher_.describe.call(this.realMatcher_) + ')'; |
708 }, | 747 }, |
709 }; | 748 }; |
710 | 749 |
711 // Exports. | 750 // Exports. |
712 testing.Test = Test; | 751 testing.Test = Test; |
752 window.asyncTestDone = asyncTestDone; | |
713 window.assertTrue = assertTrue; | 753 window.assertTrue = assertTrue; |
714 window.assertFalse = assertFalse; | 754 window.assertFalse = assertFalse; |
715 window.assertGE = assertGE; | 755 window.assertGE = assertGE; |
716 window.assertGT = assertGT; | 756 window.assertGT = assertGT; |
717 window.assertEquals = assertEquals; | 757 window.assertEquals = assertEquals; |
718 window.assertLE = assertLE; | 758 window.assertLE = assertLE; |
719 window.assertLT = assertLT; | 759 window.assertLT = assertLT; |
720 window.assertNotEquals = assertNotEquals; | 760 window.assertNotEquals = assertNotEquals; |
721 window.assertNotReached = assertNotReached; | 761 window.assertNotReached = assertNotReached; |
722 window.callFunction = callFunction; | 762 window.callFunction = callFunction; |
(...skipping 12 matching lines...) Expand all Loading... | |
735 window.runTest = runTest; | 775 window.runTest = runTest; |
736 window.runTestFunction = runTestFunction; | 776 window.runTestFunction = runTestFunction; |
737 window.SaveArgumentsMatcher = SaveArgumentsMatcher; | 777 window.SaveArgumentsMatcher = SaveArgumentsMatcher; |
738 window.TEST = TEST; | 778 window.TEST = TEST; |
739 window.TEST_F = TEST_F; | 779 window.TEST_F = TEST_F; |
740 window.GEN = GEN; | 780 window.GEN = GEN; |
741 | 781 |
742 // Import the Mock4JS helpers. | 782 // Import the Mock4JS helpers. |
743 Mock4JS.addMockSupport(window); | 783 Mock4JS.addMockSupport(window); |
744 })(); | 784 })(); |
OLD | NEW |