OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * @fileoverview Tests to ensure that the accessibility audit and mechanisms |
| 7 * to enable/disable it work as expected. |
| 8 * @author aboxhall@google.com (Alice Boxhall) |
| 9 * @see test_api.js |
| 10 */ |
| 11 |
| 12 /** |
| 13 * Test fixture for accessibility audit test. |
| 14 * @constructor |
| 15 * @extends testing.Test |
| 16 */ |
| 17 function WebUIAccessibilityAuditBrowserTest() {} |
| 18 |
| 19 WebUIAccessibilityAuditBrowserTest.prototype = { |
| 20 __proto__: testing.Test.prototype, |
| 21 |
| 22 browsePreload: 'chrome://terms', |
| 23 |
| 24 runAccessibilityChecks: true, |
| 25 accessibilityIssuesAreErrors: true, |
| 26 |
| 27 /** |
| 28 * Number of expected accessibility errors, if it should be checked, otherwise |
| 29 * null. Note: 'a11y' is short for 'accessibility' |
| 30 * @type {?number} |
| 31 */ |
| 32 expectedWarnings: null, |
| 33 |
| 34 /** |
| 35 * Number of expected accessibility warnings, if it should be checked, |
| 36 * otherwise null. |
| 37 * @type {?number} |
| 38 */ |
| 39 expectedErrors: null, |
| 40 |
| 41 tearDown: function() { |
| 42 if (this.expectedErrors != null) |
| 43 expectEquals(this.expectedErrors, getAccessibilityErrors().length); |
| 44 if (this.expectedWarnings != null) |
| 45 expectEquals(this.expectedWarnings, getAccessibilityWarnings().length); |
| 46 testing.Test.prototype.tearDown.call(this); |
| 47 } |
| 48 }; |
| 49 |
| 50 /** |
| 51 * Adds some canned audit failures to the page being tested: |
| 52 * - A low-contrast (white on white) element |
| 53 * - An element with a non-existent ARIA role |
| 54 * - A control without a label |
| 55 */ |
| 56 function addAuditFailures() { |
| 57 // Contrast ratio |
| 58 var style = document.createElement('style'); |
| 59 style.innerText = 'p { color: #ffffff }'; |
| 60 document.head.appendChild(style); |
| 61 |
| 62 // Bad ARIA role |
| 63 var div = document.createElement('div'); |
| 64 div.setAttribute('role', 'not-a-role'); |
| 65 document.body.appendChild(div); |
| 66 |
| 67 // Unlabelled control |
| 68 var input = document.createElement('input'); |
| 69 input.type = 'text'; |
| 70 document.body.appendChild(input); |
| 71 } |
| 72 |
| 73 /** |
| 74 * Adds an expectation that console.warn() will be called at least once with |
| 75 * a string matching '.*accessibility.*'. |
| 76 */ |
| 77 function expectReportConsoleWarning() { |
| 78 function StubConsole() {}; |
| 79 StubConsole.prototype.warn = function() {}; |
| 80 StubConsole.prototype.log = function() {}; |
| 81 StubConsole.prototype.error = function() {}; |
| 82 var mockConsole = mock(StubConsole); |
| 83 mockConsole.expects(atLeastOnce()).warn(stringContains('accessibility')); |
| 84 mockConsole.stubs().log(ANYTHING); |
| 85 mockConsole.stubs().error(ANYTHING); |
| 86 console = mockConsole.proxy(); |
| 87 } |
| 88 |
| 89 /** |
| 90 * Creates a mock axs.Audit object. |
| 91 * @return {object} a mock object with a run() method. |
| 92 */ |
| 93 function createMockAudit() { |
| 94 function StubAudit() {}; |
| 95 StubAudit.prototype.run = function() {}; |
| 96 return mock(StubAudit); |
| 97 } |
| 98 |
| 99 /** |
| 100 * Creates an expectation that the global axs.Audit object will never have its |
| 101 * run() method called. |
| 102 */ |
| 103 function expectAuditWillNotRun() { |
| 104 var audit = createMockAudit(); |
| 105 audit.expects(never()).run(); |
| 106 axs.Audit = audit.proxy(); |
| 107 } |
| 108 |
| 109 /** |
| 110 * Creates an expectation that the global axs.Audit object will have its run() |
| 111 * method called |times| times. |
| 112 * This creates an interstitial mock axs.Audit object with the expectation, and |
| 113 * delegates to the real axs.Audit object to run the actual audit. |
| 114 * @param {number} times The number of times the audit is expected to run. |
| 115 */ |
| 116 function expectAuditWillRun(times) { |
| 117 var audit = createMockAudit(); |
| 118 var realAudit = axs.Audit; |
| 119 var expectedInvocation = audit.expects(exactly(times)).run(); |
| 120 var willArgs = []; |
| 121 for (var i = 0; i < times; i++) |
| 122 willArgs.push(callFunction(realAudit.run)); |
| 123 expectedInvocation.will.apply(expectedInvocation, willArgs); |
| 124 axs.Audit = audit.proxy(); |
| 125 } |
| 126 |
| 127 // Test that an audit failure causes a test failure, if both |
| 128 // |runAccessibilityChecks| and |accessibilityIssuesAreErrors| are true. |
| 129 TEST_F('WebUIAccessibilityAuditBrowserTest', 'testWithAuditFailures_shouldFail', |
| 130 function() { |
| 131 expectAuditWillRun(1); |
| 132 addAuditFailures(); |
| 133 }); |
| 134 |
| 135 // Test that the accessibility audit does not run if |runAccessibilityChecks| |
| 136 // is false. |
| 137 TEST_F('WebUIAccessibilityAuditBrowserTest', |
| 138 'testWithAuditFailures_a11yChecksDisabled', |
| 139 function() { |
| 140 expectAuditWillNotRun(); |
| 141 disableAccessibilityChecks(); |
| 142 addAuditFailures(); |
| 143 }); |
| 144 |
| 145 // Tests that the accessibility audit will run but not cause a test failure when |
| 146 // accessibilityIssuesAreErrors(false) is called in the test function |
| 147 TEST_F('WebUIAccessibilityAuditBrowserTest', |
| 148 'testWithAuditFailures_a11yIssuesAreWarnings', |
| 149 function() { |
| 150 accessibilityIssuesAreErrors(false); |
| 151 expectAuditWillRun(1); |
| 152 expectReportConsoleWarning(); |
| 153 |
| 154 this.expectedWarnings = 1; |
| 155 this.expectedErrors = 2; |
| 156 enableAccessibilityChecks(); |
| 157 addAuditFailures(); |
| 158 }); |
| 159 |
| 160 /** |
| 161 * Test fixture with |runAccessibilityChecks| set to false. |
| 162 * @constructor |
| 163 * @extends {WebUIAccessibilityAuditBrowserTest} |
| 164 */ |
| 165 function WebUIAccessibilityAuditBrowserTest_TestsDisabledInFixture() {} |
| 166 |
| 167 WebUIAccessibilityAuditBrowserTest_TestsDisabledInFixture.prototype = { |
| 168 __proto__: WebUIAccessibilityAuditBrowserTest.prototype, |
| 169 |
| 170 runAccessibilityChecks: false, |
| 171 accessibilityIssuesAreErrors: true, |
| 172 }; |
| 173 |
| 174 // Test that the accessibility audit does not run when |runAccessibilityChecks| |
| 175 // is set to false in the test fixture. |
| 176 TEST_F('WebUIAccessibilityAuditBrowserTest_TestsDisabledInFixture', |
| 177 'testWithAuditFailures_a11yChecksNotEnabled', |
| 178 function() { |
| 179 expectAuditWillNotRun(); |
| 180 addAuditFailures(); |
| 181 }); |
| 182 |
| 183 // Test that the accessibility audit does run if the enableAccessibilityChecks() |
| 184 // method is called in the test function. |
| 185 TEST_F('WebUIAccessibilityAuditBrowserTest_TestsDisabledInFixture', |
| 186 'testWithAuditFailures_shouldFail', |
| 187 function() { |
| 188 expectAuditWillRun(1); |
| 189 enableAccessibilityChecks(); |
| 190 addAuditFailures(); |
| 191 }); |
| 192 |
| 193 // Test that the accessibility audit runs when the expectAccessibilityOk() |
| 194 // method is called. |
| 195 TEST_F('WebUIAccessibilityAuditBrowserTest_TestsDisabledInFixture', |
| 196 'testRunningAuditManually_noErrors', |
| 197 function() { |
| 198 expectAuditWillRun(1); |
| 199 expectAccessibilityOk(); |
| 200 }); |
| 201 |
| 202 // Test that calling expectAccessibilityOk() when there are accessibility issues |
| 203 // on the page causes the test to fail. |
| 204 TEST_F('WebUIAccessibilityAuditBrowserTest_TestsDisabledInFixture', |
| 205 'testRunningAuditManually_withErrors_shouldFail', |
| 206 function() { |
| 207 expectAuditWillRun(1); |
| 208 addAuditFailures(); |
| 209 expectAccessibilityOk(); |
| 210 }); |
| 211 |
| 212 // Test that calling expectAccessibilityOk() multiple times will cause the |
| 213 // accessibility audit to run multiple times. |
| 214 TEST_F('WebUIAccessibilityAuditBrowserTest_TestsDisabledInFixture', |
| 215 'testRunningAuditManuallySeveralTimes', function() { |
| 216 expectAuditWillRun(2); |
| 217 expectAccessibilityOk(); |
| 218 expectAccessibilityOk(); |
| 219 }); |
| 220 |
| 221 /** |
| 222 * Test fixture with |accessibilityIssuesAreErrors| set to false. |
| 223 * @constructor |
| 224 * @extends {WebUIAccessibilityAuditBrowserTest} |
| 225 */ |
| 226 function WebUIAccessibilityAuditBrowserTest_IssuesAreWarnings() {} |
| 227 |
| 228 WebUIAccessibilityAuditBrowserTest_IssuesAreWarnings.prototype = { |
| 229 __proto__: WebUIAccessibilityAuditBrowserTest.prototype, |
| 230 |
| 231 accessibilityIssuesAreErrors: false, |
| 232 }; |
| 233 |
| 234 // Tests that the accessibility audit will run but not cause a test failure when |
| 235 // |accessibilityIssuesAreErrors| is false in the test fixture. |
| 236 TEST_F('WebUIAccessibilityAuditBrowserTest_IssuesAreWarnings', |
| 237 'testWithAuditFailures', |
| 238 function() { |
| 239 expectAuditWillRun(1); |
| 240 expectReportConsoleWarning(); |
| 241 this.expectedWarnings = 1; |
| 242 this.expectedErrors = 2; |
| 243 |
| 244 enableAccessibilityChecks(); |
| 245 addAuditFailures(); |
| 246 }); |
| 247 |
| 248 // Tests that the accessibility audit will run and call a test failure when |
| 249 // accessibilityIssuesAreErrors(true) is called in the test function. |
| 250 TEST_F('WebUIAccessibilityAuditBrowserTest_IssuesAreWarnings', |
| 251 'testWithAuditFailuresAndIssuesAreErrors_shouldFail', |
| 252 function() { |
| 253 expectAuditWillRun(1); |
| 254 this.expectedWarnings = 1; |
| 255 this.expectedErrors = 2; |
| 256 |
| 257 accessibilityIssuesAreErrors(true) |
| 258 enableAccessibilityChecks(); |
| 259 |
| 260 addAuditFailures(); |
| 261 }); |
| 262 |
| 263 // Tests that the accessibility audit will run twice if expectAccessibilityOk() |
| 264 // is called during the test function and |runAccessibilityChecks| is true in |
| 265 // the test fixture. |
| 266 TEST_F('WebUIAccessibilityAuditBrowserTest_IssuesAreWarnings', |
| 267 'testWithAuditFailuresAndExpectA11yOk', |
| 268 function() { |
| 269 expectAuditWillRun(2); |
| 270 |
| 271 expectAccessibilityOk(); |
| 272 |
| 273 this.expectedWarnings = 1; |
| 274 this.expectedErrors = 2; |
| 275 expectReportConsoleWarning(); |
| 276 |
| 277 enableAccessibilityChecks(); |
| 278 |
| 279 addAuditFailures(); |
| 280 }); |
OLD | NEW |