OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 */ | 115 */ |
116 testShouldFail: false, | 116 testShouldFail: false, |
117 | 117 |
118 /** | 118 /** |
119 * Extra libraries to add before loading this test file. | 119 * Extra libraries to add before loading this test file. |
120 * @type {Array.<string>} | 120 * @type {Array.<string>} |
121 */ | 121 */ |
122 extraLibraries: [], | 122 extraLibraries: [], |
123 | 123 |
124 /** | 124 /** |
| 125 * FIXME(aboxhall) |
| 126 */ |
| 127 runA11yChecks : true, |
| 128 |
| 129 /** |
| 130 * FIXME(aboxhall) |
| 131 * Off by default to begin with; as we add the ability to suppress false |
| 132 * positives, we will transition this to true. |
| 133 */ |
| 134 a11yIssuesAreErrors: false, |
| 135 |
| 136 /** |
125 * Create a new class to handle |messageNames|, assign it to | 137 * Create a new class to handle |messageNames|, assign it to |
126 * |this.mockHandler|, register its messages and return it. | 138 * |this.mockHandler|, register its messages and return it. |
127 * @return {Mock} Mock handler class assigned to |this.mockHandler|. | 139 * @return {Mock} Mock handler class assigned to |this.mockHandler|. |
128 */ | 140 */ |
129 makeAndRegisterMockHandler: function(messageNames) { | 141 makeAndRegisterMockHandler: function(messageNames) { |
130 var MockClass = makeMockClass(messageNames); | 142 var MockClass = makeMockClass(messageNames); |
131 this.mockHandler = mock(MockClass); | 143 this.mockHandler = mock(MockClass); |
132 registerMockMessageCallbacks(this.mockHandler, MockClass); | 144 registerMockMessageCallbacks(this.mockHandler, MockClass); |
133 return this.mockHandler; | 145 return this.mockHandler; |
134 }, | 146 }, |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 * This class is not exported and is available to hold the state of the | 239 * This class is not exported and is available to hold the state of the |
228 * |currentTestCase| throughout preload and test run. | 240 * |currentTestCase| throughout preload and test run. |
229 * @param {string} name The name of the test case. | 241 * @param {string} name The name of the test case. |
230 * @param {Test} fixture The fixture object for this test case. | 242 * @param {Test} fixture The fixture object for this test case. |
231 * @param {Function} body The code to run for the test. | 243 * @param {Function} body The code to run for the test. |
232 * @constructor | 244 * @constructor |
233 */ | 245 */ |
234 function TestCase(name, fixture, body) { | 246 function TestCase(name, fixture, body) { |
235 this.name = name; | 247 this.name = name; |
236 this.fixture = fixture; | 248 this.fixture = fixture; |
| 249 this.runA11yChecks = fixture.runA11yChecks; |
| 250 this.a11yIssuesAreErrors = fixture.a11yIssuesAreErrors; |
237 this.body = body; | 251 this.body = body; |
238 } | 252 } |
239 | 253 |
240 TestCase.prototype = { | 254 TestCase.prototype = { |
241 /** | 255 /** |
242 * The name of this test. | 256 * The name of this test. |
243 * @type {string} | 257 * @type {string} |
244 */ | 258 */ |
245 name: null, | 259 name: null, |
246 | 260 |
247 /** | 261 /** |
248 * The test fixture to set |this| to when running the test |body|. | 262 * The test fixture to set |this| to when running the test |body|. |
249 * @type {testing.Test} | 263 * @type {testing.Test} |
250 */ | 264 */ |
251 fixture: null, | 265 fixture: null, |
252 | 266 |
253 /** | 267 /** |
254 * The test body to execute in runTest(). | 268 * The test body to execute in runTest(). |
255 * @type {Function} | 269 * @type {Function} |
256 */ | 270 */ |
257 body: null, | 271 body: null, |
258 | 272 |
259 /** | 273 /** |
260 * True when the test fixture will run the test later. | 274 * True when the test fixture will run the test later. |
261 * @type {boolean} | 275 * @type {boolean} |
262 * @private | 276 * @private |
263 */ | 277 */ |
264 deferred_: false, | 278 deferred_: false, |
265 | 279 |
| 280 runA11yChecks: true, |
| 281 |
| 282 /** |
| 283 * FIXME(aboxhall) |
| 284 */ |
| 285 a11yIssuesAreErrors: true, |
| 286 |
266 /** | 287 /** |
267 * Called at preload time, proxies to the fixture. | 288 * Called at preload time, proxies to the fixture. |
268 * @type {Function} | 289 * @type {Function} |
269 */ | 290 */ |
270 preLoad: function(name) { | 291 preLoad: function(name) { |
271 if (this.fixture) | 292 if (this.fixture) |
272 this.fixture.preLoad(); | 293 this.fixture.preLoad(); |
273 }, | 294 }, |
274 | 295 |
275 /** | 296 /** |
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
578 var testIsDone = false; | 599 var testIsDone = false; |
579 | 600 |
580 /** | 601 /** |
581 * Holds the errors, if any, caught by expects so that the test case can | 602 * Holds the errors, if any, caught by expects so that the test case can |
582 * fail. Cleared when results are reported from runTest() or testDone(). | 603 * fail. Cleared when results are reported from runTest() or testDone(). |
583 * @type {Array.<Error>} | 604 * @type {Array.<Error>} |
584 */ | 605 */ |
585 var errors = []; | 606 var errors = []; |
586 | 607 |
587 /** | 608 /** |
| 609 * Hold the accessibility warnings and errors, respectively. |
| 610 * Like errors, cleared when results are reported. |
| 611 */ |
| 612 var a11y_warnings = []; |
| 613 var a11y_errors = []; |
| 614 |
| 615 /** |
588 * URL to dummy WebUI page for testing framework. | 616 * URL to dummy WebUI page for testing framework. |
589 * @type {string} | 617 * @type {string} |
590 */ | 618 */ |
591 var DUMMY_URL = 'chrome://DummyURL'; | 619 var DUMMY_URL = 'chrome://DummyURL'; |
592 | 620 |
593 /** | 621 /** |
594 * Resets test state by clearing |errors| and |testIsDone| flags. | 622 * Resets test state by clearing |errors| and |testIsDone| flags. |
595 */ | 623 */ |
596 function resetTestState() { | 624 function resetTestState() { |
597 errors.splice(0, errors.length); | 625 errors.splice(0, errors.length); |
598 testIsDone = false; | 626 testIsDone = false; |
599 } | 627 } |
600 | 628 |
601 /** | 629 /** |
602 * Notifies the running browser test of the test results. Clears |errors|. | 630 * Notifies the running browser test of the test results. Clears |errors|. |
603 * @param {Array.<boolean, string>=} result When passed, this is used for the | 631 * @param {Array.<boolean, string>=} result When passed, this is used for the |
604 * testResult message. | 632 * testResult message. |
605 */ | 633 */ |
606 function testDone(result) { | 634 function testDone(result) { |
607 if (!testIsDone) { | 635 if (!testIsDone) { |
608 testIsDone = true; | 636 testIsDone = true; |
609 if (currentTestCase) { | 637 if (currentTestCase) { |
| 638 if (currentTestCase.runA11yChecks && |
| 639 !runAccessibilityAudit_(a11y_errors, a11y_warnings)) { |
| 640 if (currentTestCase.a11yIssuesAreErrors) { |
| 641 if (!result) |
| 642 result = testResult(); |
| 643 result = [false, accessibilityErrorsToMessage(a11y_errors, |
| 644 a11y_warnings, |
| 645 result[1])]; |
| 646 } else { /* a11yIssuesAreErrors */ |
| 647 console.warn(accessibilityErrorsToMessage(a11y_errors, |
| 648 a11y_warnings)); |
| 649 } |
| 650 } |
| 651 |
610 try { | 652 try { |
611 currentTestCase.tearDown(); | 653 currentTestCase.tearDown(); |
612 } catch (e) { | 654 } catch (e) { |
613 // Caught an exception in tearDown; Register the error and recreate | 655 // Caught an exception in tearDown; Register the error and recreate |
614 // the result if it is passed in. | 656 // the result if it is passed in. |
615 errors.push(e); | 657 errors.push(e); |
616 if (result) | 658 if (result) |
617 result = [false, errorsToMessage([e], result[1])]; | 659 result = [false, errorsToMessage([e], result[1])]; |
618 } | 660 } |
619 currentTestCase = null; | 661 currentTestCase = null; |
620 } | 662 } |
621 chrome.send('testResult', result ? result : testResult()); | 663 if (!result) |
| 664 result = testResult(); |
| 665 chrome.send('testResult', result); |
622 errors.splice(0, errors.length); | 666 errors.splice(0, errors.length); |
623 } else { | 667 } else { |
624 console.warn('testIsDone already'); | 668 console.warn('testIsDone already'); |
625 } | 669 } |
626 } | 670 } |
627 | 671 |
628 /** | 672 /** |
629 * Converts each Error in |errors| to a suitable message, adding them to | 673 * Converts each Error in |errors| to a suitable message, adding them to |
630 * |message|, and returns the message string. | 674 * |message|, and returns the message string. |
631 * @param {Array.<Error>} errors Array of errors to add to |message|. | 675 * @param {Array.<Error>} errors Array of errors to add to |message|. |
(...skipping 13 matching lines...) Expand all Loading... |
645 return message; | 689 return message; |
646 } | 690 } |
647 | 691 |
648 /** | 692 /** |
649 * Returns [success, message] & clears |errors|. | 693 * Returns [success, message] & clears |errors|. |
650 * @param {boolean} errorsOk When true, errors are ok. | 694 * @param {boolean} errorsOk When true, errors are ok. |
651 * @return {Array.<boolean, string>} | 695 * @return {Array.<boolean, string>} |
652 */ | 696 */ |
653 function testResult(errorsOk) { | 697 function testResult(errorsOk) { |
654 var result = [true, '']; | 698 var result = [true, '']; |
655 if (errors.length) { | 699 if (errors.length) |
656 result = [!!errorsOk, errorsToMessage(errors)]; | 700 result = [!!errorsOk, errorsToMessage(errors)]; |
657 } | 701 |
658 return result; | 702 return result; |
659 } | 703 } |
660 | 704 |
661 // Asserts. | 705 // Asserts. |
662 // Use the following assertions to verify a condition within a test. | 706 // Use the following assertions to verify a condition within a test. |
663 // If assertion fails, throw an Error with information pertinent to the test. | 707 // If assertion fails, throw an Error with information pertinent to the test. |
664 | 708 |
665 /** | 709 /** |
666 * When |test| !== true, aborts the current test. | 710 * When |test| !== true, aborts the current test. |
667 * @param {boolean} test The predicate to check against |expected|. | 711 * @param {boolean} test The predicate to check against |expected|. |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
785 /** | 829 /** |
786 * Always aborts the current test. | 830 * Always aborts the current test. |
787 * @param {string=} message The message to include in the Error thrown. | 831 * @param {string=} message The message to include in the Error thrown. |
788 * @throws {Error} always. | 832 * @throws {Error} always. |
789 */ | 833 */ |
790 function assertNotReached(message) { | 834 function assertNotReached(message) { |
791 helper.registerCall(); | 835 helper.registerCall(); |
792 throw new Error(helper.getCallMessage(message)); | 836 throw new Error(helper.getCallMessage(message)); |
793 } | 837 } |
794 | 838 |
| 839 function disableA11yChecks() { |
| 840 if (!currentTestCase) |
| 841 return; |
| 842 currentTestCase.runA11yChecks = false; |
| 843 } |
| 844 |
| 845 function enableA11yChecks() { |
| 846 if (!currentTestCase) |
| 847 return; |
| 848 currentTestCase.runA11yChecks = true; |
| 849 } |
| 850 |
| 851 function a11yIssuesAreErrors(flag) { |
| 852 if (!currentTestCase) |
| 853 return; |
| 854 currentTestCase.a11yIssuesAreErrors = flag; |
| 855 } |
| 856 |
| 857 /** |
| 858 * FIXME(aboxhall) documentation |
| 859 */ |
| 860 function accessibilityErrorsToMessage(a11y_errors, a11y_warnings, message) { |
| 861 if (message) |
| 862 message += '\n\n'; |
| 863 else |
| 864 message = '\n'; |
| 865 |
| 866 message += 'An accessibility audit found '; |
| 867 |
| 868 if (a11y_errors.length > 0) { |
| 869 message += a11y_errors.length + |
| 870 (a11y_errors.length == 1 ? ' error ' : ' errors '); |
| 871 if (a11y_warnings.length > 0) |
| 872 message += 'and '; |
| 873 } |
| 874 if (a11y_warnings.length > 0) { |
| 875 message += a11y_warnings.length + |
| 876 (a11y_warnings.length == 1 ? ' warning ' : ' warnings '); |
| 877 } |
| 878 message += 'on this page.\n'; |
| 879 if (!currentTestCase.a11yIssuesAreErrors) { |
| 880 message += 'These are currently for information only, but will become ' + |
| 881 'errors at a later date.\n' |
| 882 } |
| 883 message += 'For more information, please see ' + |
| 884 'https://sites.google.com/a/chromium.org/dev/webui-accessibility-audit'; |
| 885 |
| 886 for (var i = 0; i < a11y_errors.length; i++) |
| 887 message += '\n\n' + a11y_errors[i]; |
| 888 |
| 889 for (var i = 0; i < a11y_warnings.length; i++) |
| 890 message += '\n\n' + a11y_warnings[i]; |
| 891 return message; |
| 892 } |
| 893 |
| 894 /** |
| 895 * FIXME(aboxhall): documentation |
| 896 */ |
| 897 function accessibilityErrorMessage(rule, result) { |
| 898 if (rule.severity == axs.constants.Severity.Severe) |
| 899 var message = 'Error: ' |
| 900 else |
| 901 var message = 'Warning: ' |
| 902 message += rule.name + ' failed on the following ' + |
| 903 (result.elements.length == 1 ? 'element' : 'elements'); |
| 904 |
| 905 if (result.elements.length == 1) |
| 906 message += ':' |
| 907 else |
| 908 message += ' (1 - ' + Math.min(5, result.elements.length) + |
| 909 ' of ' + result.elements.length + '):'; |
| 910 |
| 911 for (var i = 0; i < Math.min(result.elements.length, 5); i++) |
| 912 message += '\n' + axs.utils.getQuerySelectorText(result.elements[i]); |
| 913 return message; |
| 914 } |
| 915 |
| 916 /** |
| 917 * FIXME(aboxhall) documentation |
| 918 */ |
| 919 function assertAccessibilityOk() { |
| 920 helper.registerCall(); |
| 921 var a11y_errors = []; |
| 922 var a11y_warnings = []; |
| 923 if (!runAccessibilityAudit_(a11y_errors, a11y_warnings)) |
| 924 throw new Error(accessibilityErrorsToMessage(a11y_errors, |
| 925 a11y_warnings)); |
| 926 } |
| 927 |
| 928 /** |
| 929 * Run an accessibility audit on the current page state. |
| 930 * FIXME(aboxhall) documentation |
| 931 * @type {Function} |
| 932 * @return {boolean} Whether there were any errors or warnings |
| 933 * @private |
| 934 */ |
| 935 function runAccessibilityAudit_(a11yErrors, a11yWarnings) { |
| 936 for (var auditRuleName in axs.AuditRule.specs) { |
| 937 var auditRule = axs.AuditRules.getRule(auditRuleName); |
| 938 if (!auditRule) |
| 939 continue; // Shouldn't happen, but fail silently if it does. |
| 940 |
| 941 if (auditRule.disabled || auditRule.requiresConsoleAPI) |
| 942 continue; |
| 943 |
| 944 var result = auditRule.run(); |
| 945 if (result.result == axs.constants.AuditResult.FAIL) { |
| 946 // TODO(aboxhall): more useful error messages (sadly non-trivial) |
| 947 if (auditRule.severity == axs.constants.Severity.Severe) |
| 948 a11yErrors.push(accessibilityErrorMessage(auditRule, result)); |
| 949 else |
| 950 a11yWarnings.push(accessibilityErrorMessage(auditRule, result)); |
| 951 } |
| 952 } |
| 953 // TODO(aboxhall): have strict (no errors or warnings) vs non-strict |
| 954 // (warnings ok) |
| 955 // TODO(aboxhall): some kind of info logging for warnings only?? |
| 956 return (a11yErrors.length == 0 && a11yWarnings.length == 0); |
| 957 } |
| 958 |
795 /** | 959 /** |
796 * Creates a function based upon a function that thows an exception on | 960 * Creates a function based upon a function that thows an exception on |
797 * failure. The new function stuffs any errors into the |errors| array for | 961 * failure. The new function stuffs any errors into the |errors| array for |
798 * checking by runTest. This allows tests to continue running other checks, | 962 * checking by runTest. This allows tests to continue running other checks, |
799 * while failing the overall test if any errors occurrred. | 963 * while failing the overall test if any errors occurrred. |
800 * @param {Function} assertFunc The function which may throw an Error. | 964 * @param {Function} assertFunc The function which may throw an Error. |
801 * @return {function(...*):bool} A function that applies its arguments to | 965 * @return {function(...*):bool} A function that applies its arguments to |
802 * |assertFunc| and returns true if |assertFunc| passes. | 966 * |assertFunc| and returns true if |assertFunc| passes. |
803 * @see errors | 967 * @see errors |
804 * @see runTestFunction | 968 * @see runTestFunction |
(...skipping 556 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1361 * @return {RunAllAction} Action for use in will. | 1525 * @return {RunAllAction} Action for use in will. |
1362 */ | 1526 */ |
1363 function runAllActionsAsync(whenTestDone) { | 1527 function runAllActionsAsync(whenTestDone) { |
1364 return new RunAllAction(true, whenTestDone, | 1528 return new RunAllAction(true, whenTestDone, |
1365 Array.prototype.slice.call(arguments, 1)); | 1529 Array.prototype.slice.call(arguments, 1)); |
1366 } | 1530 } |
1367 | 1531 |
1368 // Exports. | 1532 // Exports. |
1369 testing.Test = Test; | 1533 testing.Test = Test; |
1370 exports.testDone = testDone; | 1534 exports.testDone = testDone; |
| 1535 exports.a11yIssuesAreErrors = a11yIssuesAreErrors; |
1371 exports.assertTrue = assertTrue; | 1536 exports.assertTrue = assertTrue; |
1372 exports.assertFalse = assertFalse; | 1537 exports.assertFalse = assertFalse; |
1373 exports.assertGE = assertGE; | 1538 exports.assertGE = assertGE; |
1374 exports.assertGT = assertGT; | 1539 exports.assertGT = assertGT; |
1375 exports.assertEquals = assertEquals; | 1540 exports.assertEquals = assertEquals; |
1376 exports.assertLE = assertLE; | 1541 exports.assertLE = assertLE; |
1377 exports.assertLT = assertLT; | 1542 exports.assertLT = assertLT; |
1378 exports.assertNotEquals = assertNotEquals; | 1543 exports.assertNotEquals = assertNotEquals; |
1379 exports.assertNotReached = assertNotReached; | 1544 exports.assertNotReached = assertNotReached; |
| 1545 exports.assertAccessibilityOk = assertAccessibilityOk; |
1380 exports.callFunction = callFunction; | 1546 exports.callFunction = callFunction; |
1381 exports.callFunctionWithSavedArgs = callFunctionWithSavedArgs; | 1547 exports.callFunctionWithSavedArgs = callFunctionWithSavedArgs; |
1382 exports.callGlobalWithSavedArgs = callGlobalWithSavedArgs; | 1548 exports.callGlobalWithSavedArgs = callGlobalWithSavedArgs; |
| 1549 exports.disableA11yChecks = disableA11yChecks; |
| 1550 exports.enableA11yChecks = enableA11yChecks; |
1383 exports.expectTrue = createExpect(assertTrue); | 1551 exports.expectTrue = createExpect(assertTrue); |
1384 exports.expectFalse = createExpect(assertFalse); | 1552 exports.expectFalse = createExpect(assertFalse); |
1385 exports.expectGE = createExpect(assertGE); | 1553 exports.expectGE = createExpect(assertGE); |
1386 exports.expectGT = createExpect(assertGT); | 1554 exports.expectGT = createExpect(assertGT); |
1387 exports.expectEquals = createExpect(assertEquals); | 1555 exports.expectEquals = createExpect(assertEquals); |
1388 exports.expectLE = createExpect(assertLE); | 1556 exports.expectLE = createExpect(assertLE); |
1389 exports.expectLT = createExpect(assertLT); | 1557 exports.expectLT = createExpect(assertLT); |
1390 exports.expectNotEquals = createExpect(assertNotEquals); | 1558 exports.expectNotEquals = createExpect(assertNotEquals); |
1391 exports.expectNotReached = createExpect(assertNotReached); | 1559 exports.expectNotReached = createExpect(assertNotReached); |
| 1560 exports.expectAccessibilityOk = createExpect(assertAccessibilityOk); |
1392 exports.preloadJavascriptLibraries = preloadJavascriptLibraries; | 1561 exports.preloadJavascriptLibraries = preloadJavascriptLibraries; |
1393 exports.registerMessageCallback = registerMessageCallback; | 1562 exports.registerMessageCallback = registerMessageCallback; |
1394 exports.registerMockGlobals = registerMockGlobals; | 1563 exports.registerMockGlobals = registerMockGlobals; |
1395 exports.registerMockMessageCallbacks = registerMockMessageCallbacks; | 1564 exports.registerMockMessageCallbacks = registerMockMessageCallbacks; |
1396 exports.resetTestState = resetTestState; | 1565 exports.resetTestState = resetTestState; |
| 1566 exports.runAccessibilityAudit_ = runAccessibilityAudit_; |
1397 exports.runAllActions = runAllActions; | 1567 exports.runAllActions = runAllActions; |
1398 exports.runAllActionsAsync = runAllActionsAsync; | 1568 exports.runAllActionsAsync = runAllActionsAsync; |
1399 exports.runTest = runTest; | 1569 exports.runTest = runTest; |
1400 exports.runTestFunction = runTestFunction; | 1570 exports.runTestFunction = runTestFunction; |
1401 exports.SaveMockArguments = SaveMockArguments; | 1571 exports.SaveMockArguments = SaveMockArguments; |
1402 exports.DUMMY_URL = DUMMY_URL; | 1572 exports.DUMMY_URL = DUMMY_URL; |
1403 exports.TEST = TEST; | 1573 exports.TEST = TEST; |
1404 exports.TEST_F = TEST_F; | 1574 exports.TEST_F = TEST_F; |
| 1575 exports.RUNTIME_TEST_F = TEST_F; |
1405 exports.GEN = GEN; | 1576 exports.GEN = GEN; |
1406 exports.GEN_INCLUDE = GEN_INCLUDE; | 1577 exports.GEN_INCLUDE = GEN_INCLUDE; |
1407 exports.WhenTestDone = WhenTestDone; | 1578 exports.WhenTestDone = WhenTestDone; |
1408 | 1579 |
1409 // Import the Mock4JS helpers. | 1580 // Import the Mock4JS helpers. |
1410 Mock4JS.addMockSupport(exports); | 1581 Mock4JS.addMockSupport(exports); |
1411 })(this); | 1582 })(this); |
OLD | NEW |