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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/test/base/js2gtest.js ('k') | chrome/test/data/webui/test_api.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/test/data/webui/accessibility_audit_browsertest.js
diff --git a/chrome/test/data/webui/accessibility_audit_browsertest.js b/chrome/test/data/webui/accessibility_audit_browsertest.js
new file mode 100644
index 0000000000000000000000000000000000000000..6283757e633d754f91cde839853f0800acac46ed
--- /dev/null
+++ b/chrome/test/data/webui/accessibility_audit_browsertest.js
@@ -0,0 +1,250 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+function WebUIAccessibilityAuditBrowserTest() {}
+
+/**
+ * @fileoverview Tests to ensure that the accessibility audit and mechanisms
+ * to enable/disable it work as expected.
scr 2012/12/18 23:13:44 Indent 4 spaces http://google-styleguide.googlecod
+ * @author aboxhall@google.com (Alice Boxhall)
+ * @see test_api.js
+ */
+WebUIAccessibilityAuditBrowserTest.prototype = {
+ __proto__: testing.Test.prototype,
+
+ browsePreload: 'chrome://terms',
+
+ runAccessibilityChecks: true,
+ accessibilityIssuesAreErrors: true,
+
+ /**
+ * Number of expected accessibility errors, if it should be checked, otherwise
+ * null. Note: 'a11y' is short for 'accessibility'
+ * @type {?number}
+ */
+ expectedWarnings: null,
+
+ /**
+ * Number of expected accessibility warnings, if it should be checked,
+ * otherwise null.
+ * @type {?number}
+ */
+ expectedErrors: null,
+
+ tearDown: function() {
+ if (this.expectedErrors != null)
+ expectEquals(this.expectedErrors, getAccessibilityErrors().length);
+ if (this.expectedWarnings != null)
+ expectEquals(this.expectedWarnings, getAccessibilityWarnings().length);
+ testing.Test.prototype.tearDown.call(this);
+ }
+};
+
+/**
+ * Adds some canned audit failures to the page being tested:
+ * - A low-contrast (white on white) element
+ * - An element with a non-existent ARIA role
+ * - A control without a label
+ */
+function addAuditFailures() {
+ // Contrast ratio
+ var style = document.createElement('style');
+ style.innerText = 'p { color: #ffffff }';
+ document.head.appendChild(style);
+
+ // Bad ARIA role
+ var div = document.createElement('div');
+ div.setAttribute('role', 'not-a-role');
+ document.body.appendChild(div);
+
+ // Unlabelled control
+ var input = document.createElement('input');
+ input.type = 'text';
+ document.body.appendChild(input);
+}
+
+/**
+ * Creates a mock axs.Audit object.
+ * @return {object} a mock object with a run() method.
+ */
+function createMockAudit() {
+ function StubAudit() {};
+ StubAudit.prototype.run = function() {};
+ return mock(StubAudit);
+}
+
+/**
+ * Creates an expectation that the global axs.Audit object will never have its
+ * run() method called.
+ */
+function expectAuditWillNotRun() {
+ var audit = createMockAudit();
+ audit.expects(never()).run();
+ axs.Audit = audit.proxy();
+}
+
+/**
+ * Creates an expectation that the global axs.Audit object will have its run()
+ * method called |times| times.
+ * This creates an interstitial mock axs.Audit object with the expectation, and
+ * delegates to the real axs.Audit object to run the actual audit.
+ * @param {number} times The number of times the audit is expected to run.
+ */
+function expectAuditWillRun(times) {
+ var audit = createMockAudit();
+ var realAudit = axs.Audit;
+ var expectedInvocation = audit.expects(exactly(times)).run();
+ var willArgs = [];
+ for (var i = 0; i < times; i++)
+ willArgs.push(callFunction(realAudit.run));
+ ExpectedInvocation.prototype.will.apply(expectedInvocation, willArgs);
+ axs.Audit = audit.proxy();
+}
+
+// Test that an audit failure causes a test failure, if both
+// |runAccessibilityChecks| and |accessibilityIssuesAreErrors| are true.
+TEST_F('WebUIAccessibilityAuditBrowserTest', 'testWithAuditFailures_shouldFail',
+ function() {
+ expectAuditWillRun(1);
+ addAuditFailures();
+});
+
+// Test that the accessibility audit does not run if |runAccessibilityChecks|
+// is false.
+TEST_F('WebUIAccessibilityAuditBrowserTest',
+ 'testWithAuditFailures_a11yChecksDisabled',
+ function() {
+ expectAuditWillNotRun();
+ disableAccessibilityChecks();
+ addAuditFailures();
+});
+
+// Tests that the accessibility audit will run but not cause a test failure when
+// accessibilityIssuesAreErrors(false) is called in the test function
+TEST_F('WebUIAccessibilityAuditBrowserTest',
+ 'testWithAuditFailures_a11yIssuesAreWarnings',
+ function() {
+ accessibilityIssuesAreErrors(false);
+ expectAuditWillRun(1);
+ this.expectedWarnings = 1;
+ this.expectedErrors = 2;
+ enableAccessibilityChecks();
+ addAuditFailures();
+});
+
+/**
+ * Test fixture with |runAccessibilityChecks| set to false.
+ */
+function WebUIAccessibilityAuditBrowserTest_TestsDisabledInFixture() {}
+
+WebUIAccessibilityAuditBrowserTest_TestsDisabledInFixture.prototype = {
+ __proto__: WebUIAccessibilityAuditBrowserTest.prototype,
+
+ runAccessibilityChecks: false,
+ accessibilityIssuesAreErrors: true,
+};
+
+// Test that the accessibility audit does not run when |runAccessibilityChecks|
+// is set to false in the test fixture.
+TEST_F('WebUIAccessibilityAuditBrowserTest_TestsDisabledInFixture',
+ 'testWithAuditFailures_a11yChecksNotEnabled',
+ function() {
+ expectAuditWillNotRun();
+ addAuditFailures();
+});
+
+// Test that the accessibility audit does run if the enableAccessibilityChecks()
+// method is called in the test function.
+TEST_F('WebUIAccessibilityAuditBrowserTest_TestsDisabledInFixture',
+ 'testWithAuditFailures_shouldFail',
+ function() {
+ expectAuditWillRun(1);
+ enableAccessibilityChecks();
+ addAuditFailures();
+});
+
+// Test that the accessibility audit runs when the expectAccessibilityOk()
+// method is called.
+TEST_F('WebUIAccessibilityAuditBrowserTest_TestsDisabledInFixture',
+ 'testRunningAuditManually_noErrors',
+ function() {
+ expectAuditWillRun(1);
+ expectAccessibilityOk();
+});
+
+// Test that calling expectAccessibilityOk() when there are accessibility issues
+// on the page causes the test to fail.
+TEST_F('WebUIAccessibilityAuditBrowserTest_TestsDisabledInFixture',
+ 'testRunningAuditManually_withErrors_shouldFail',
+ function() {
+ expectAuditWillRun(1);
+ addAuditFailures();
+ expectAccessibilityOk();
+});
+
+// Test that calling expectAccessibilityOk() multiple times will cause the
+// accessibility audit to run multiple times.
+TEST_F('WebUIAccessibilityAuditBrowserTest_TestsDisabledInFixture',
+ 'testRunningAuditManuallySeveralTimes', function() {
+ expectAuditWillRun(2);
+ expectAccessibilityOk();
+ expectAccessibilityOk();
+});
+
+/**
+ * Test fixture with |accessibilityIssuesAreErrors| set to false.
+ */
+function WebUIAccessibilityAuditBrowserTest_IssuesAreWarnings() {}
+
+WebUIAccessibilityAuditBrowserTest_IssuesAreWarnings.prototype = {
+ __proto__: WebUIAccessibilityAuditBrowserTest.prototype,
+
+ accessibilityIssuesAreErrors: false,
+};
+
+// Tests that the accessibility audit will run but not cause a test failure when
+// |accessibilityIssuesAreErrors| is false in the test fixture.
+TEST_F('WebUIAccessibilityAuditBrowserTest_IssuesAreWarnings',
+ 'testWithAuditFailures',
+ function() {
+ expectAuditWillRun(1);
+ this.expectedWarnings = 1;
+ this.expectedErrors = 2;
+
+ enableAccessibilityChecks();
+ addAuditFailures();
+});
+
+// Tests that the accessibility audit will run and call a test failure when
+// accessibilityIssuesAreErrors(true) is called in the test function.
+TEST_F('WebUIAccessibilityAuditBrowserTest_IssuesAreWarnings',
+ 'testWithAuditFailuresAndIssuesAreErrors_shouldFail',
+ function() {
+ expectAuditWillRun(1);
+ this.expectedWarnings = 1;
+ this.expectedErrors = 2;
+
+ accessibilityIssuesAreErrors(true)
+ enableAccessibilityChecks();
+
+ addAuditFailures();
+});
+
+// Tests that the accessibility audit will run twice if expectAccessibilityOk()
+// is called during the test function and |runAccessibilityChecks| is true in
+// the test fixture.
+TEST_F('WebUIAccessibilityAuditBrowserTest_IssuesAreWarnings',
+ 'testWithAuditFailuresAndExpectA11yOk',
+ function() {
+ expectAuditWillRun(2);
+
+ expectAccessibilityOk();
+
+ this.expectedWarnings = 1;
+ this.expectedErrors = 2;
+
+ enableAccessibilityChecks();
+
+ addAuditFailures();
+});
« 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