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