| 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 24 matching lines...) Expand all Loading... |
| 35 */ | 35 */ |
| 36 var currentTestArguments = []; | 36 var currentTestArguments = []; |
| 37 | 37 |
| 38 /** | 38 /** |
| 39 * This class will be exported as testing.Test, and is provided to hold the | 39 * This class will be exported as testing.Test, and is provided to hold the |
| 40 * fixture's configuration and callback methods for the various phases of | 40 * fixture's configuration and callback methods for the various phases of |
| 41 * invoking a test. It is called "Test" rather than TestFixture to roughly | 41 * invoking a test. It is called "Test" rather than TestFixture to roughly |
| 42 * mimic the gtest's class names. | 42 * mimic the gtest's class names. |
| 43 * @constructor | 43 * @constructor |
| 44 */ | 44 */ |
| 45 function Test() {} | 45 function Test() {}; |
| 46 | 46 |
| 47 Test.prototype = { | 47 Test.prototype = { |
| 48 /** | 48 /** |
| 49 * The name of the test. | 49 * The name of the test. |
| 50 */ | 50 */ |
| 51 name: null, | 51 name: null, |
| 52 | 52 |
| 53 /** | 53 /** |
| 54 * When set to a string value representing a url, generate BrowsePreload | 54 * When set to a string value representing a url, generate BrowsePreload |
| 55 * call, which will browse to the url and call fixture.preLoad of the | 55 * call, which will browse to the url and call fixture.preLoad of the |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 * Whether to run the accessibility checks. | 125 * Whether to run the accessibility checks. |
| 126 * @type {boolean} | 126 * @type {boolean} |
| 127 */ | 127 */ |
| 128 runAccessibilityChecks : true, | 128 runAccessibilityChecks: true, |
| 129 |
| 130 /** |
| 131 * Configuration for the accessibility audit. |
| 132 * @type {axs.AuditConfiguration} |
| 133 */ |
| 134 accessibilityAuditConfig: new axs.AuditConfiguration(), |
| 129 | 135 |
| 130 /** | 136 /** |
| 131 * Whether to treat accessibility issues (errors or warnings) as test | 137 * Whether to treat accessibility issues (errors or warnings) as test |
| 132 * failures. If true, any accessibility issues will cause the test to fail. | 138 * failures. If true, any accessibility issues will cause the test to fail. |
| 133 * If false, accessibility issues will cause a console.warn. | 139 * If false, accessibility issues will cause a console.warn. |
| 134 * Off by default to begin with; as we add the ability to suppress false | 140 * Off by default to begin with; as we add the ability to suppress false |
| 135 * positives, we will transition this to true. | 141 * positives, we will transition this to true. |
| 136 * @type {boolean} | 142 * @type {boolean} |
| 137 */ | 143 */ |
| 138 accessibilityIssuesAreErrors: false, | 144 accessibilityIssuesAreErrors: false, |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 }, | 244 }, |
| 239 | 245 |
| 240 /** | 246 /** |
| 241 * Called to run the accessibility audit from the perspective of this | 247 * Called to run the accessibility audit from the perspective of this |
| 242 * fixture. | 248 * fixture. |
| 243 */ | 249 */ |
| 244 runAccessibilityAudit: function() { | 250 runAccessibilityAudit: function() { |
| 245 if (!this.runAccessibilityChecks || typeof document === 'undefined') | 251 if (!this.runAccessibilityChecks || typeof document === 'undefined') |
| 246 return; | 252 return; |
| 247 | 253 |
| 248 if (!runAccessibilityAudit(this.a11yErrors_, this.a11yWarnings_)) { | 254 if (!runAccessibilityAudit(this.a11yErrors_, this.a11yWarnings_, |
| 255 this.accessibilityAuditConfig)) { |
| 249 var report = accessibilityAuditReport(this.a11yErrors_, | 256 var report = accessibilityAuditReport(this.a11yErrors_, |
| 250 this.a11yWarnings_); | 257 this.a11yWarnings_); |
| 251 if (this.accessibilityIssuesAreErrors) | 258 if (this.accessibilityIssuesAreErrors) |
| 252 throw new Error(report); | 259 throw new Error(report); |
| 253 else | 260 else |
| 254 console.warn(report); | 261 console.warn(report); |
| 255 } | 262 } |
| 256 }, | 263 }, |
| 257 | 264 |
| 258 /** | 265 /** |
| (...skipping 617 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 876 * @throws {Error} always. | 883 * @throws {Error} always. |
| 877 */ | 884 */ |
| 878 function assertNotReached(message) { | 885 function assertNotReached(message) { |
| 879 helper.registerCall(); | 886 helper.registerCall(); |
| 880 throw new Error(helper.getCallMessage(message)); | 887 throw new Error(helper.getCallMessage(message)); |
| 881 } | 888 } |
| 882 | 889 |
| 883 /** | 890 /** |
| 884 * Run an accessibility audit on the current page state. | 891 * Run an accessibility audit on the current page state. |
| 885 * @type {Function} | 892 * @type {Function} |
| 893 * @param {Array} a11yErrors |
| 894 * @param {Array} a11yWarnings |
| 895 * @param {axs.AuditConfigutarion=} opt_config |
| 886 * @return {boolean} Whether there were any errors or warnings | 896 * @return {boolean} Whether there were any errors or warnings |
| 887 * @private | 897 * @private |
| 888 */ | 898 */ |
| 889 function runAccessibilityAudit(a11yErrors, a11yWarnings) { | 899 function runAccessibilityAudit(a11yErrors, a11yWarnings, opt_config) { |
| 890 var auditResults = axs.Audit.run(); | 900 var auditResults = axs.Audit.run(opt_config); |
| 891 for (var i = 0; i < auditResults.length; i++) { | 901 for (var i = 0; i < auditResults.length; i++) { |
| 892 var auditResult = auditResults[i]; | 902 var auditResult = auditResults[i]; |
| 893 if (auditResult.result == axs.constants.AuditResult.FAIL) { | 903 if (auditResult.result == axs.constants.AuditResult.FAIL) { |
| 894 var auditRule = auditResult.rule; | 904 var auditRule = auditResult.rule; |
| 895 // TODO(aboxhall): more useful error messages (sadly non-trivial) | 905 // TODO(aboxhall): more useful error messages (sadly non-trivial) |
| 896 if (auditRule.severity == axs.constants.Severity.Severe) | 906 if (auditRule.severity == axs.constants.Severity.Severe) |
| 897 a11yErrors.push(accessibilityErrorMessage(auditRule, auditResult)); | 907 a11yErrors.push(accessibilityErrorMessage(auditRule, auditResult)); |
| 898 else | 908 else |
| 899 a11yWarnings.push(accessibilityErrorMessage(auditRule, auditResult)); | 909 a11yWarnings.push(accessibilityErrorMessage(auditRule, auditResult)); |
| 900 } | 910 } |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 968 } | 978 } |
| 969 | 979 |
| 970 var maxElements = Math.min(result.elements.length, 5); | 980 var maxElements = Math.min(result.elements.length, 5); |
| 971 for (var i = 0; i < maxElements; i++) | 981 for (var i = 0; i < maxElements; i++) |
| 972 message += '\n' + axs.utils.getQuerySelectorText(result.elements[i]); | 982 message += '\n' + axs.utils.getQuerySelectorText(result.elements[i]); |
| 973 return message; | 983 return message; |
| 974 } | 984 } |
| 975 | 985 |
| 976 /** | 986 /** |
| 977 * Asserts that the current page state passes the accessibility audit. | 987 * Asserts that the current page state passes the accessibility audit. |
| 988 * @param {Array=} opt_errors Array to fill with errors, if desired. |
| 989 * @param {Array=} opt_warnings Array to fill with warnings, if desired. |
| 978 */ | 990 */ |
| 979 function assertAccessibilityOk() { | 991 function assertAccessibilityOk(opt_errors, opt_warnings) { |
| 980 helper.registerCall(); | 992 helper.registerCall(); |
| 981 var a11yErrors = []; | 993 var a11yErrors = opt_errors || []; |
| 982 var a11yWarnings = []; | 994 var a11yWarnings = opt_warnings || []; |
| 983 if (!runAccessibilityAudit(a11yErrors, a11yWarnings)) | 995 var auditConfig = currentTestCase.fixture.accessibilityAuditConfig; |
| 996 if (!runAccessibilityAudit(a11yErrors, a11yWarnings, auditConfig)) |
| 984 throw new Error(accessibilityAuditReport(a11yErrors, a11yWarnings)); | 997 throw new Error(accessibilityAuditReport(a11yErrors, a11yWarnings)); |
| 985 } | 998 } |
| 986 | 999 |
| 987 /** | 1000 /** |
| 988 * Creates a function based upon a function that thows an exception on | 1001 * Creates a function based upon a function that thows an exception on |
| 989 * failure. The new function stuffs any errors into the |errors| array for | 1002 * failure. The new function stuffs any errors into the |errors| array for |
| 990 * checking by runTest. This allows tests to continue running other checks, | 1003 * checking by runTest. This allows tests to continue running other checks, |
| 991 * while failing the overall test if any errors occurrred. | 1004 * while failing the overall test if any errors occurrred. |
| 992 * @param {Function} assertFunc The function which may throw an Error. | 1005 * @param {Function} assertFunc The function which may throw an Error. |
| 993 * @return {function(...*):bool} A function that applies its arguments to | 1006 * @return {function(...*):bool} A function that applies its arguments to |
| (...skipping 604 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1598 exports.TEST = TEST; | 1611 exports.TEST = TEST; |
| 1599 exports.TEST_F = TEST_F; | 1612 exports.TEST_F = TEST_F; |
| 1600 exports.RUNTIME_TEST_F = TEST_F; | 1613 exports.RUNTIME_TEST_F = TEST_F; |
| 1601 exports.GEN = GEN; | 1614 exports.GEN = GEN; |
| 1602 exports.GEN_INCLUDE = GEN_INCLUDE; | 1615 exports.GEN_INCLUDE = GEN_INCLUDE; |
| 1603 exports.WhenTestDone = WhenTestDone; | 1616 exports.WhenTestDone = WhenTestDone; |
| 1604 | 1617 |
| 1605 // Import the Mock4JS helpers. | 1618 // Import the Mock4JS helpers. |
| 1606 Mock4JS.addMockSupport(exports); | 1619 Mock4JS.addMockSupport(exports); |
| 1607 })(this); | 1620 })(this); |
| OLD | NEW |