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|. |
(...skipping 456 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
467 } catch (e) { | 467 } catch (e) { |
468 errors.push(e); | 468 errors.push(e); |
469 } | 469 } |
470 }; | 470 }; |
471 expectFunc.isExpect = true; | 471 expectFunc.isExpect = true; |
472 expectFunc.expectName = assertFunc.name.replace(/^assert/, 'expect'); | 472 expectFunc.expectName = assertFunc.name.replace(/^assert/, 'expect'); |
473 return expectFunc; | 473 return expectFunc; |
474 } | 474 } |
475 | 475 |
476 /** | 476 /** |
477 * This is the starting point for tests run by WebUIBrowserTest. It clears | 477 * This is the starting point for tests run by WebUIBrowserTest. If an error |
478 * |errors|, runs the test surrounded by an expect to catch Errors. If | 478 * occurs, it reports a failure and a message created by joining individual |
479 * |errors| is non-empty, it reports a failure and a message by joining | 479 * error messages. |
480 * |errors|. | |
481 * @param {string} testFunction The function name to call. | 480 * @param {string} testFunction The function name to call. |
482 * @param {Array} testArguments The arguments to call |testFunction| with. | 481 * @param {Array} testArguments The arguments to call |testFunction| with. |
483 * @return {Array.<boolean, string>} [test-succeeded, message-if-failed] | 482 * @return {Array.<boolean, string>} [test-succeeded, message-if-failed] |
484 * @see errors | 483 * @see errors |
485 * @see createExpect | 484 * @see runTestFunction |
486 **/ | 485 **/ |
487 function runTest(testFunction, testArguments) { | 486 function runTest(testFunction, testArguments) { |
488 errors.splice(0, errors.length); | |
489 // Avoid eval() if at all possible, since it will not work on pages | 487 // Avoid eval() if at all possible, since it will not work on pages |
490 // that have enabled content-security-policy. | 488 // that have enabled content-security-policy. |
491 var testBody = this[testFunction]; // global object -- not a method. | 489 var testBody = this[testFunction]; // global object -- not a method. |
492 if (typeof testBody === "undefined") | 490 if (typeof testBody === "undefined") |
493 testBody = eval(testFunction); | 491 testBody = eval(testFunction); |
494 if (testBody != RUN_TEST_F) { | 492 if (testBody != RUN_TEST_F) { |
495 var testName = | 493 var testName = |
496 testFunction.name ? testFunction.name : testBody.toString(); | 494 testFunction.name ? testFunction.name : testBody.toString(); |
497 console.log('Running test ' + testName); | 495 console.log('Running test ' + testName); |
498 } | 496 } |
| 497 return runTestFunction(testFunction, testBody, testArguments); |
| 498 } |
| 499 |
| 500 /** |
| 501 * This is the guts of WebUIBrowserTest. It clears |errors|, runs the |
| 502 * test surrounded by an expect to catch Errors. If |errors| is |
| 503 * non-empty, it reports a failure and a message by joining |errors|. |
| 504 * Consumers can use this to use assert/expect functions asynchronously, |
| 505 * but are then responsible for reporting errors to the browser themselves. |
| 506 * @param {string} testFunction The function name to report on failure. |
| 507 * @param {Function} testBody The function to call. |
| 508 * @param {Array} testArguments The arguments to call |testBody| with. |
| 509 * @return {Array.<boolean, string>} [test-succeeded, message-if-failed] |
| 510 * @see errors |
| 511 * @see createExpect |
| 512 **/ |
| 513 function runTestFunction(testFunction, testBody, testArguments) { |
| 514 errors.splice(0, errors.length); |
499 createExpect(testBody).apply(null, testArguments); | 515 createExpect(testBody).apply(null, testArguments); |
500 | 516 |
501 var result = [true]; | 517 var result = [true]; |
502 if (errors.length) { | 518 if (errors.length) { |
503 var message = ''; | 519 var message = ''; |
504 for (var i = 0; i < errors.length; ++i) { | 520 for (var i = 0; i < errors.length; ++i) { |
505 message += 'Failed: ' + testFunction + '(' + | 521 message += 'Failed: ' + testFunction + '(' + |
506 testArguments.map(JSON.stringify) + | 522 testArguments.map(JSON.stringify) + |
507 ')\n' + errors[i].stack; | 523 ')\n' + errors[i].stack; |
508 } | 524 } |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
710 window.expectGT = createExpect(assertGT); | 726 window.expectGT = createExpect(assertGT); |
711 window.expectEquals = createExpect(assertEquals); | 727 window.expectEquals = createExpect(assertEquals); |
712 window.expectLE = createExpect(assertLE); | 728 window.expectLE = createExpect(assertLE); |
713 window.expectLT = createExpect(assertLT); | 729 window.expectLT = createExpect(assertLT); |
714 window.expectNotEquals = createExpect(assertNotEquals); | 730 window.expectNotEquals = createExpect(assertNotEquals); |
715 window.expectNotReached = createExpect(assertNotReached); | 731 window.expectNotReached = createExpect(assertNotReached); |
716 window.preloadJavascriptLibraries = preloadJavascriptLibraries; | 732 window.preloadJavascriptLibraries = preloadJavascriptLibraries; |
717 window.registerMessageCallback = registerMessageCallback; | 733 window.registerMessageCallback = registerMessageCallback; |
718 window.registerMockMessageCallbacks = registerMockMessageCallbacks; | 734 window.registerMockMessageCallbacks = registerMockMessageCallbacks; |
719 window.runTest = runTest; | 735 window.runTest = runTest; |
| 736 window.runTestFunction = runTestFunction; |
720 window.SaveArgumentsMatcher = SaveArgumentsMatcher; | 737 window.SaveArgumentsMatcher = SaveArgumentsMatcher; |
721 window.TEST = TEST; | 738 window.TEST = TEST; |
722 window.TEST_F = TEST_F; | 739 window.TEST_F = TEST_F; |
723 window.GEN = GEN; | 740 window.GEN = GEN; |
724 | 741 |
725 // Import the Mock4JS helpers. | 742 // Import the Mock4JS helpers. |
726 Mock4JS.addMockSupport(window); | 743 Mock4JS.addMockSupport(window); |
727 })(); | 744 })(); |
OLD | NEW |