Chromium Code Reviews| 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, 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 * Adds some canned audit failures to the page being tested: | |
| 54 * - A low-contrast (white on white) element | |
| 55 * - An element with a non-existent ARIA role | |
| 56 * - A control without a label | |
| 57 */ | |
| 58 function addAuditFailures() { | |
| 59 // Contrast ratio | |
| 60 var style = document.createElement('style'); | |
| 61 style.innerText = 'p { color: #ffffff }'; | |
| 62 document.head.appendChild(style); | |
| 63 | |
| 64 // Bad ARIA role | |
| 65 var div = document.createElement('div'); | |
| 66 div.setAttribute('role', 'not-a-role'); | |
| 67 document.body.appendChild(div); | |
| 68 | |
| 69 // Unlabelled control | |
| 70 var input = document.createElement('input'); | |
| 71 input.type = 'text'; | |
| 72 document.body.appendChild(input); | |
| 73 } | |
| 74 | |
| 75 /** | |
| 76 * Adds an expectation that console.warn() will be called at least once with | |
| 77 * a string matching '.*accessibility.*'. | |
| 78 */ | |
| 79 function expectReportConsoleWarning() { | |
| 80 function StubConsole() {}; | |
| 81 StubConsole.prototype.warn = function() {}; | |
| 82 StubConsole.prototype.log = function() {}; | |
| 83 StubConsole.prototype.info = function() {}; | |
| 84 StubConsole.prototype.error = function() {}; | |
| 85 var mockConsole = mock(StubConsole); | |
| 86 mockConsole.expects(atLeastOnce()).warn(stringContains('accessibility')); | |
| 87 mockConsole.stubs().log(ANYTHING); | |
| 88 mockConsole.stubs().info(ANYTHING); | |
| 89 mockConsole.stubs().error(ANYTHING); | |
| 90 console = mockConsole.proxy(); | |
| 91 } | |
| 92 | |
| 93 /** | |
| 94 * Creates a mock axs.Audit object. | |
| 95 * @return {object} a mock object with a run() method. | |
| 96 */ | |
| 97 function createMockAudit() { | |
| 98 function StubAudit() {}; | |
| 99 StubAudit.prototype.run = function() {}; | |
| 100 return mock(StubAudit); | |
| 101 } | |
| 102 | |
| 103 /** | |
| 104 * Creates an expectation that the global axs.Audit object will never have its | |
| 105 * run() method called. | |
| 106 */ | |
| 107 function expectAuditWillNotRun() { | |
| 108 var audit = createMockAudit(); | |
| 109 audit.expects(never()).run(); | |
| 110 axs.Audit = audit.proxy(); | |
| 111 } | |
| 112 | |
| 113 /** | |
| 114 * Creates an expectation that the global axs.Audit object will have its run() | |
| 115 * method called |times| times. | |
| 116 * This creates an interstitial mock axs.Audit object with the expectation, and | |
| 117 * delegates to the real axs.Audit object to run the actual audit. | |
| 118 * @param {number} times The number of times the audit is expected to run. | |
| 119 */ | |
| 120 function expectAuditWillRun(times) { | |
| 121 var audit = createMockAudit(); | |
| 122 var realAudit = axs.Audit; | |
| 123 var expectedInvocation = audit.expects(exactly(times)).run(); | |
| 124 var willArgs = []; | |
| 125 for (var i = 0; i < times; i++) | |
| 126 willArgs.push(callFunction(realAudit.run)); | |
| 127 expectedInvocation.will.apply(expectedInvocation, willArgs); | |
| 128 axs.Audit = audit.proxy(); | |
| 129 } | |
| 130 | |
| 131 // Test that an audit failure causes a test failure, if both | |
| 132 // |runAccessibilityChecks| and |accessibilityIssuesAreErrors| are true. | |
| 133 TEST_F('WebUIAccessibilityAuditBrowserTest', 'testWithAuditFailures_shouldFail', | |
|
scr
2013/01/04 19:02:55
s/_shouldFail// and include this.testShouldFail =
| |
| 134 function() { | |
| 135 expectAuditWillRun(1); | |
| 136 addAuditFailures(); | |
| 137 }); | |
| 138 | |
| 139 // Test that the accessibility audit does not run if |runAccessibilityChecks| | |
| 140 // is false. | |
| 141 TEST_F('WebUIAccessibilityAuditBrowserTest', | |
| 142 'testWithAuditFailures_a11yChecksDisabled', | |
| 143 function() { | |
| 144 expectAuditWillNotRun(); | |
| 145 this.disableAccessibilityChecks(); | |
| 146 addAuditFailures(); | |
| 147 }); | |
| 148 | |
| 149 // Tests that the accessibility audit will run but not cause a test failure when | |
| 150 // accessibilityIssuesAreErrors(false) is called in the test function | |
| 151 TEST_F('WebUIAccessibilityAuditBrowserTest', | |
| 152 'testWithAuditFailures_a11yIssuesAreWarnings', | |
| 153 function() { | |
| 154 this.accessibilityIssuesAreErrors = false; | |
| 155 expectAuditWillRun(1); | |
| 156 expectReportConsoleWarning(); | |
| 157 | |
| 158 this.expectedWarnings = 1; | |
| 159 this.expectedErrors = 2; | |
| 160 this.enableAccessibilityChecks(); | |
| 161 addAuditFailures(); | |
| 162 }); | |
| 163 | |
| 164 /** | |
| 165 * Test fixture with |runAccessibilityChecks| set to false. | |
| 166 * @constructor | |
| 167 * @extends {WebUIAccessibilityAuditBrowserTest} | |
| 168 */ | |
| 169 function WebUIAccessibilityAuditBrowserTest_TestsDisabledInFixture() {} | |
| 170 | |
| 171 WebUIAccessibilityAuditBrowserTest_TestsDisabledInFixture.prototype = { | |
| 172 __proto__: WebUIAccessibilityAuditBrowserTest.prototype, | |
| 173 | |
| 174 runAccessibilityChecks: false, | |
| 175 accessibilityIssuesAreErrors: true, | |
| 176 }; | |
| 177 | |
| 178 // Test that the accessibility audit does not run when |runAccessibilityChecks| | |
| 179 // is set to false in the test fixture. | |
| 180 TEST_F('WebUIAccessibilityAuditBrowserTest_TestsDisabledInFixture', | |
| 181 'testWithAuditFailures_a11yChecksNotEnabled', | |
| 182 function() { | |
| 183 expectAuditWillNotRun(); | |
| 184 addAuditFailures(); | |
| 185 }); | |
| 186 | |
| 187 // Test that the accessibility audit does run if the enableAccessibilityChecks() | |
| 188 // method is called in the test function. | |
| 189 TEST_F('WebUIAccessibilityAuditBrowserTest_TestsDisabledInFixture', | |
| 190 'testWithAuditFailures_shouldFail', | |
| 191 function() { | |
| 192 console.log(axs.Audit); | |
| 193 expectAuditWillRun(1); | |
| 194 this.enableAccessibilityChecks(); | |
| 195 addAuditFailures(); | |
| 196 }); | |
| 197 | |
| 198 // Test that the accessibility audit runs when the expectAccessibilityOk() | |
| 199 // method is called. | |
| 200 TEST_F('WebUIAccessibilityAuditBrowserTest_TestsDisabledInFixture', | |
| 201 'testRunningAuditManually_noErrors', | |
| 202 function() { | |
| 203 expectAuditWillRun(1); | |
| 204 expectAccessibilityOk(); | |
| 205 }); | |
| 206 | |
| 207 // Test that calling expectAccessibilityOk() when there are accessibility issues | |
| 208 // on the page causes the test to fail. | |
| 209 TEST_F('WebUIAccessibilityAuditBrowserTest_TestsDisabledInFixture', | |
| 210 'testRunningAuditManually_withErrors_shouldFail', | |
| 211 function() { | |
| 212 expectAuditWillRun(1); | |
| 213 addAuditFailures(); | |
| 214 expectAccessibilityOk(); | |
| 215 }); | |
| 216 | |
| 217 // Test that calling expectAccessibilityOk() multiple times will cause the | |
| 218 // accessibility audit to run multiple times. | |
| 219 TEST_F('WebUIAccessibilityAuditBrowserTest_TestsDisabledInFixture', | |
| 220 'testRunningAuditManuallySeveralTimes', function() { | |
| 221 expectAuditWillRun(2); | |
| 222 expectAccessibilityOk(); | |
| 223 expectAccessibilityOk(); | |
| 224 }); | |
| 225 | |
| 226 /** | |
| 227 * Test fixture with |accessibilityIssuesAreErrors| set to false. | |
| 228 * @constructor | |
| 229 * @extends {WebUIAccessibilityAuditBrowserTest} | |
| 230 */ | |
| 231 function WebUIAccessibilityAuditBrowserTest_IssuesAreWarnings() {} | |
| 232 | |
| 233 WebUIAccessibilityAuditBrowserTest_IssuesAreWarnings.prototype = { | |
| 234 __proto__: WebUIAccessibilityAuditBrowserTest.prototype, | |
| 235 | |
| 236 accessibilityIssuesAreErrors: false, | |
| 237 }; | |
| 238 | |
| 239 // Tests that the accessibility audit will run but not cause a test failure when | |
| 240 // |accessibilityIssuesAreErrors| is false in the test fixture. | |
| 241 TEST_F('WebUIAccessibilityAuditBrowserTest_IssuesAreWarnings', | |
| 242 'testWithAuditFailures', | |
| 243 function() { | |
| 244 expectAuditWillRun(1); | |
| 245 expectReportConsoleWarning(); | |
| 246 this.expectedWarnings = 1; | |
| 247 this.expectedErrors = 2; | |
| 248 | |
| 249 this.enableAccessibilityChecks(); | |
| 250 addAuditFailures(); | |
| 251 }); | |
| 252 | |
| 253 // Tests that the accessibility audit will run and call a test failure when | |
| 254 // accessibilityIssuesAreErrors(true) is called in the test function. | |
| 255 TEST_F('WebUIAccessibilityAuditBrowserTest_IssuesAreWarnings', | |
| 256 'testWithAuditFailuresAndIssuesAreErrors_shouldFail', | |
| 257 function() { | |
| 258 expectAuditWillRun(1); | |
| 259 this.expectedWarnings = 1; | |
| 260 this.expectedErrors = 2; | |
| 261 | |
| 262 this.accessibilityIssuesAreErrors = true; | |
| 263 this.enableAccessibilityChecks(); | |
| 264 | |
| 265 addAuditFailures(); | |
| 266 }); | |
| 267 | |
| 268 // Tests that the accessibility audit will run twice if expectAccessibilityOk() | |
| 269 // is called during the test function and |runAccessibilityChecks| is true in | |
| 270 // the test fixture. | |
| 271 TEST_F('WebUIAccessibilityAuditBrowserTest_IssuesAreWarnings', | |
| 272 'testWithAuditFailuresAndExpectA11yOk', | |
| 273 function() { | |
| 274 expectAuditWillRun(2); | |
| 275 | |
| 276 expectAccessibilityOk(); | |
| 277 | |
| 278 this.expectedWarnings = 1; | |
| 279 this.expectedErrors = 2; | |
| 280 expectReportConsoleWarning(); | |
| 281 | |
| 282 this.enableAccessibilityChecks(); | |
| 283 | |
| 284 addAuditFailures(); | |
| 285 }); | |
| OLD | NEW |