Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
|
James Hawkins
2013/01/08 18:38:07
Don't add copyrights.
aboxhall
2013/01/08 19:35:50
Done.
| |
| 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, this.getAccessibilityErrors().length); | |
| 44 if (this.expectedWarnings != null) { | |
| 45 expectEquals(this.expectedWarnings, | |
| 46 this.getAccessibilityWarnings().length); | |
| 47 } | |
| 48 testing.Test.prototype.tearDown.call(this); | |
| 49 } | |
| 50 }; | |
| 51 | |
| 52 /** | |
| 53 * Test fixture for tests that are expected to fail. | |
| 54 */ | |
| 55 function WebUIAccessibilityAuditBrowserTest_ShouldFail() {} | |
| 56 | |
| 57 WebUIAccessibilityAuditBrowserTest_ShouldFail.prototype = { | |
| 58 __proto__: WebUIAccessibilityAuditBrowserTest.prototype, | |
| 59 | |
| 60 testShouldFail: true | |
| 61 }; | |
| 62 | |
| 63 /** | |
| 64 * Adds some canned audit failures to the page being tested: | |
| 65 * - A low-contrast (white on white) element | |
| 66 * - An element with a non-existent ARIA role | |
| 67 * - A control without a label | |
| 68 */ | |
| 69 function addAuditFailures() { | |
| 70 // Contrast ratio | |
| 71 var style = document.createElement('style'); | |
| 72 style.innerText = 'p { color: #ffffff }'; | |
| 73 document.head.appendChild(style); | |
| 74 | |
| 75 // Bad ARIA role | |
| 76 var div = document.createElement('div'); | |
| 77 div.setAttribute('role', 'not-a-role'); | |
| 78 document.body.appendChild(div); | |
| 79 | |
| 80 // Unlabelled control | |
| 81 var input = document.createElement('input'); | |
| 82 input.type = 'text'; | |
| 83 document.body.appendChild(input); | |
| 84 } | |
| 85 | |
| 86 /** | |
| 87 * Adds an expectation that console.warn() will be called at least once with | |
| 88 * a string matching '.*accessibility.*'. | |
| 89 */ | |
| 90 function expectReportConsoleWarning() { | |
| 91 function StubConsole() {}; | |
| 92 StubConsole.prototype.warn = function() {}; | |
| 93 StubConsole.prototype.log = function() {}; | |
| 94 StubConsole.prototype.info = function() {}; | |
| 95 StubConsole.prototype.error = function() {}; | |
| 96 var mockConsole = mock(StubConsole); | |
| 97 mockConsole.expects(atLeastOnce()).warn(stringContains('accessibility')); | |
| 98 mockConsole.stubs().log(ANYTHING); | |
| 99 mockConsole.stubs().info(ANYTHING); | |
| 100 mockConsole.stubs().error(ANYTHING); | |
| 101 console = mockConsole.proxy(); | |
| 102 } | |
| 103 | |
| 104 /** | |
| 105 * Creates a mock axs.Audit object. | |
| 106 * @return {object} a mock object with a run() method. | |
| 107 */ | |
| 108 function createMockAudit() { | |
| 109 function StubAudit() {}; | |
| 110 StubAudit.prototype.run = function() {}; | |
| 111 return mock(StubAudit); | |
| 112 } | |
| 113 | |
| 114 /** | |
| 115 * Creates an expectation that the global axs.Audit object will never have its | |
| 116 * run() method called. | |
| 117 */ | |
| 118 function expectAuditWillNotRun() { | |
| 119 var audit = createMockAudit(); | |
| 120 audit.expects(never()).run(); | |
| 121 axs.Audit = audit.proxy(); | |
| 122 } | |
| 123 | |
| 124 /** | |
| 125 * Creates an expectation that the global axs.Audit object will have its run() | |
| 126 * method called |times| times. | |
| 127 * This creates an interstitial mock axs.Audit object with the expectation, and | |
| 128 * delegates to the real axs.Audit object to run the actual audit. | |
| 129 * @param {number} times The number of times the audit is expected to run. | |
| 130 */ | |
| 131 function expectAuditWillRun(times) { | |
| 132 var audit = createMockAudit(); | |
| 133 var realAudit = axs.Audit; | |
| 134 var expectedInvocation = audit.expects(exactly(times)).run(); | |
| 135 var willArgs = []; | |
| 136 for (var i = 0; i < times; i++) | |
| 137 willArgs.push(callFunction(realAudit.run)); | |
| 138 expectedInvocation.will.apply(expectedInvocation, willArgs); | |
| 139 axs.Audit = audit.proxy(); | |
| 140 } | |
| 141 | |
| 142 // Test that an audit failure causes a test failure, if both | |
| 143 // |runAccessibilityChecks| and |accessibilityIssuesAreErrors| are true. | |
| 144 TEST_F('WebUIAccessibilityAuditBrowserTest_ShouldFail', 'testWithAuditFailures', | |
| 145 function() { | |
| 146 expectAuditWillRun(1); | |
| 147 addAuditFailures(); | |
| 148 }); | |
| 149 | |
| 150 // Test that the accessibility audit does not run if |runAccessibilityChecks| | |
| 151 // is false. | |
| 152 TEST_F('WebUIAccessibilityAuditBrowserTest', | |
| 153 'testWithAuditFailures_a11yChecksDisabled', | |
| 154 function() { | |
| 155 expectAuditWillNotRun(); | |
| 156 this.disableAccessibilityChecks(); | |
| 157 addAuditFailures(); | |
| 158 }); | |
| 159 | |
| 160 // Tests that the accessibility audit will run but not cause a test failure when | |
| 161 // accessibilityIssuesAreErrors(false) is called in the test function | |
| 162 TEST_F('WebUIAccessibilityAuditBrowserTest', | |
| 163 'testWithAuditFailures_a11yIssuesAreWarnings', | |
| 164 function() { | |
| 165 this.accessibilityIssuesAreErrors = false; | |
| 166 expectAuditWillRun(1); | |
| 167 expectReportConsoleWarning(); | |
| 168 | |
| 169 this.expectedWarnings = 1; | |
| 170 this.expectedErrors = 2; | |
| 171 this.enableAccessibilityChecks(); | |
| 172 addAuditFailures(); | |
| 173 }); | |
| 174 | |
| 175 /** | |
| 176 * Test fixture with |runAccessibilityChecks| set to false. | |
| 177 * @constructor | |
| 178 * @extends {WebUIAccessibilityAuditBrowserTest} | |
| 179 */ | |
| 180 function WebUIAccessibilityAuditBrowserTest_TestsDisabledInFixture() {} | |
| 181 | |
| 182 WebUIAccessibilityAuditBrowserTest_TestsDisabledInFixture.prototype = { | |
| 183 __proto__: WebUIAccessibilityAuditBrowserTest.prototype, | |
| 184 | |
| 185 runAccessibilityChecks: false, | |
| 186 accessibilityIssuesAreErrors: true, | |
| 187 }; | |
| 188 | |
| 189 /** | |
| 190 * Test fixture with |runAccessibilityChecks| set to false for tests that should | |
| 191 * fail. | |
| 192 * @constructor | |
| 193 * @extends {WebUIAccessibilityAuditBrowserTest_TestsDisabledInFixture} | |
| 194 */ | |
| 195 function WebUIAccessibilityAuditBrowserTest_TestsDisabledInFixture_ShouldFail() | |
| 196 {} | |
| 197 | |
| 198 WebUIAccessibilityAuditBrowserTest_TestsDisabledInFixture_ShouldFail.prototype = | |
| 199 { | |
| 200 __proto__: | |
| 201 WebUIAccessibilityAuditBrowserTest_TestsDisabledInFixture.prototype, | |
| 202 | |
| 203 testShouldFail: true | |
| 204 }; | |
| 205 | |
| 206 | |
| 207 | |
| 208 // Test that the accessibility audit does not run when |runAccessibilityChecks| | |
| 209 // is set to false in the test fixture. | |
| 210 TEST_F('WebUIAccessibilityAuditBrowserTest_TestsDisabledInFixture', | |
| 211 'testWithAuditFailures_a11yChecksNotEnabled', | |
| 212 function() { | |
| 213 expectAuditWillNotRun(); | |
| 214 addAuditFailures(); | |
| 215 }); | |
| 216 | |
| 217 // Test that the accessibility audit does run if the enableAccessibilityChecks() | |
| 218 // method is called in the test function. | |
| 219 TEST_F('WebUIAccessibilityAuditBrowserTest_TestsDisabledInFixture_ShouldFail', | |
| 220 'testWithAuditFailures', | |
| 221 function() { | |
| 222 console.log(axs.Audit); | |
| 223 expectAuditWillRun(1); | |
| 224 this.enableAccessibilityChecks(); | |
| 225 addAuditFailures(); | |
| 226 }); | |
| 227 | |
| 228 // Test that the accessibility audit runs when the expectAccessibilityOk() | |
| 229 // method is called. | |
| 230 TEST_F('WebUIAccessibilityAuditBrowserTest_TestsDisabledInFixture', | |
| 231 'testRunningAuditManually_noErrors', | |
| 232 function() { | |
| 233 expectAuditWillRun(1); | |
| 234 expectAccessibilityOk(); | |
| 235 }); | |
| 236 | |
| 237 // Test that calling expectAccessibilityOk() when there are accessibility issues | |
| 238 // on the page causes the test to fail. | |
| 239 TEST_F('WebUIAccessibilityAuditBrowserTest_TestsDisabledInFixture_ShouldFail', | |
| 240 'testRunningAuditManually_withErrors', | |
| 241 function() { | |
| 242 expectAuditWillRun(1); | |
| 243 addAuditFailures(); | |
| 244 expectAccessibilityOk(); | |
| 245 }); | |
| 246 | |
| 247 // Test that calling expectAccessibilityOk() multiple times will cause the | |
| 248 // accessibility audit to run multiple times. | |
| 249 TEST_F('WebUIAccessibilityAuditBrowserTest_TestsDisabledInFixture', | |
| 250 'testRunningAuditManuallySeveralTimes', function() { | |
| 251 expectAuditWillRun(2); | |
| 252 expectAccessibilityOk(); | |
| 253 expectAccessibilityOk(); | |
| 254 }); | |
| 255 | |
| 256 /** | |
| 257 * Test fixture with |accessibilityIssuesAreErrors| set to false. | |
|
James Hawkins
2013/01/08 18:38:07
nit: You use 'Tests' elsewhere, which is what shou
aboxhall
2013/01/08 19:35:50
Done (not here though, because this is a test fixt
| |
| 258 * @constructor | |
| 259 * @extends {WebUIAccessibilityAuditBrowserTest} | |
| 260 */ | |
| 261 function WebUIAccessibilityAuditBrowserTest_IssuesAreWarnings() {} | |
| 262 | |
| 263 WebUIAccessibilityAuditBrowserTest_IssuesAreWarnings.prototype = { | |
| 264 __proto__: WebUIAccessibilityAuditBrowserTest.prototype, | |
| 265 | |
| 266 accessibilityIssuesAreErrors: false, | |
| 267 }; | |
| 268 | |
| 269 /** | |
| 270 * Test fixture with |accessibilityIssuesAreErrors| set to false for tests that | |
| 271 * should fail | |
| 272 * @constructor | |
| 273 * @extends {WebUIAccessibilityAuditBrowserTest_IssuesAreWarnings} | |
| 274 */ | |
| 275 function WebUIAccessibilityAuditBrowserTest_IssuesAreWarnings_ShouldFail() {} | |
| 276 | |
| 277 WebUIAccessibilityAuditBrowserTest_IssuesAreWarnings_ShouldFail.prototype = { | |
| 278 __proto__: WebUIAccessibilityAuditBrowserTest_IssuesAreWarnings.prototype, | |
| 279 | |
| 280 testShouldFail: true | |
| 281 }; | |
| 282 | |
| 283 | |
| 284 // Tests that the accessibility audit will run but not cause a test failure when | |
| 285 // |accessibilityIssuesAreErrors| is false in the test fixture. | |
| 286 TEST_F('WebUIAccessibilityAuditBrowserTest_IssuesAreWarnings', | |
| 287 'testWithAuditFailures', | |
| 288 function() { | |
| 289 expectAuditWillRun(1); | |
| 290 expectReportConsoleWarning(); | |
| 291 this.expectedWarnings = 1; | |
| 292 this.expectedErrors = 2; | |
| 293 | |
| 294 this.enableAccessibilityChecks(); | |
| 295 addAuditFailures(); | |
| 296 }); | |
| 297 | |
| 298 // Tests that the accessibility audit will run and call a test failure when | |
| 299 // accessibilityIssuesAreErrors(true) is called in the test function. | |
| 300 TEST_F('WebUIAccessibilityAuditBrowserTest_IssuesAreWarnings_ShouldFail', | |
| 301 'testWithAuditFailuresAndIssuesAreErrors', | |
| 302 function() { | |
| 303 expectAuditWillRun(1); | |
| 304 this.expectedWarnings = 1; | |
| 305 this.expectedErrors = 2; | |
| 306 | |
| 307 this.accessibilityIssuesAreErrors = true; | |
| 308 this.enableAccessibilityChecks(); | |
| 309 | |
| 310 addAuditFailures(); | |
| 311 }); | |
| 312 | |
| 313 // Tests that the accessibility audit will run twice if expectAccessibilityOk() | |
| 314 // is called during the test function and |runAccessibilityChecks| is true in | |
| 315 // the test fixture. | |
| 316 TEST_F('WebUIAccessibilityAuditBrowserTest_IssuesAreWarnings', | |
| 317 'testWithAuditFailuresAndExpectA11yOk', | |
| 318 function() { | |
| 319 expectAuditWillRun(2); | |
| 320 | |
| 321 expectAccessibilityOk(); | |
| 322 | |
| 323 this.expectedWarnings = 1; | |
| 324 this.expectedErrors = 2; | |
| 325 expectReportConsoleWarning(); | |
| 326 | |
| 327 this.enableAccessibilityChecks(); | |
| 328 | |
| 329 addAuditFailures(); | |
| 330 }); | |
| OLD | NEW |