Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(354)

Side by Side Diff: chrome/test/data/webui/accessibility_audit_browsertest.js

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

Powered by Google App Engine
This is Rietveld 408576698