Index: chrome/test/base/in_process_browser_test.cc |
diff --git a/chrome/test/base/in_process_browser_test.cc b/chrome/test/base/in_process_browser_test.cc |
index 5b01038eb80b7beadf7ddb349b1ddfaca63c81aa..330537c024915ac9610e7e0dd62d51da1ca2976b 100644 |
--- a/chrome/test/base/in_process_browser_test.cc |
+++ b/chrome/test/base/in_process_browser_test.cc |
@@ -112,6 +112,44 @@ void SingleDesktopTestObserver::OnBrowserAdded(Browser* browser) { |
} // namespace |
+// Library used for testing accessibility. |
+const base::FilePath::CharType kAXSTesting[] = |
+ FILE_PATH_LITERAL("third_party/accessibility-audit/axs_testing.js"); |
+// JavaScript snippet to configure and run the accessibility audit. |
+const char kAccessibilityTestString[] = |
+ "var config = new axs.AuditConfiguration();" |
+ "/* Disable warning about rules that cannot be checked. */" |
+ "config.showUnsupportedRulesWarning = false;" |
+ "config.auditRulesToIgnore = [" |
+ " /*" |
+ " * The 'elements with meaningful background image' accessibility" |
+ " * audit (AX_IMAGE_01) does not apply, since Chrome doesn't" |
+ " * disable background images in high-contrast mode like some" |
+ " * browsers do." |
+ " */" |
+ " 'elementsWithMeaningfulBackgroundImage'," |
+ " /*" |
+ " * Most WebUI pages are inside an IFrame, so the 'web page should" |
+ " * have a title that describes topic or purpose' test (AX_TITLE_01)" |
+ " * generally does not apply." |
+ " */" |
+ " 'pageWithoutTitle'," |
+ " /*" |
+ " * Enable when crbug.com/267035 is fixed." |
+ " * Until then it's just noise." |
+ " */" |
+ " 'lowContrastElements'];" |
Dan Beam
2015/02/10 01:13:38
nit: ] on new line
hcarmona
2015/02/10 02:31:58
Done.
|
+ "var result = axs.Audit.run(config);" |
+ "function getError() {" |
+ " for (var i = 0; i < result.length; ++i) {" |
+ " if (result[i].result == axs.constants.AuditResult.FAIL) {" |
+ " return axs.Audit.createReport(result);" |
+ " }" |
Dan Beam
2015/02/10 01:13:38
nit: no curlies
hcarmona
2015/02/10 02:31:58
Curlies stay b/c of adding break
|
+ " }" |
+ " return '';" |
Dan Beam
2015/02/10 01:13:38
nit: I think this could just be a for + break;
hcarmona
2015/02/10 02:31:58
Done.
|
+ "}" |
+ "domAutomationController.send(getError());"; |
+ |
InProcessBrowserTest::InProcessBrowserTest() |
: browser_(NULL), |
exit_when_last_browser_closes_(true), |
@@ -260,6 +298,56 @@ void InProcessBrowserTest::PrepareTestCommandLine( |
command_line->AppendArg(url::kAboutBlankURL); |
} |
+bool InProcessBrowserTest::RunAccessibilityChecks(std::string* error_message) { |
+ if (!browser()) { |
+ *error_message = "browser is NULL"; |
+ return false; |
+ } |
+ auto tab_strip = browser()->tab_strip_model(); |
+ if (!tab_strip) { |
+ *error_message = "tab_strip is NULL"; |
+ return false; |
+ } |
+ auto web_contents = tab_strip->GetActiveWebContents(); |
+ if (!web_contents) { |
+ *error_message = "web_contents is NULL"; |
+ return false; |
+ } |
+ auto focused_frame = web_contents->GetFocusedFrame(); |
+ if (!focused_frame) { |
+ *error_message = "focused_frame is NULL"; |
+ return false; |
+ } |
+ |
+ // Load accessibility library. |
+ base::FilePath src_dir; |
+ if (!PathService::Get(base::DIR_SOURCE_ROOT, &src_dir)) { |
+ *error_message = "PathService::Get failed"; |
+ return false; |
+ } |
+ base::FilePath script_path = src_dir.Append(kAXSTesting); |
+ std::string script; |
+ if (!base::ReadFileToString(script_path, &script)) { |
+ *error_message = "Could not read accessibility library"; |
+ return false; |
+ } |
+ if (!content::ExecuteScript(web_contents, script)) { |
+ *error_message = "Failed to load accessibility library"; |
+ return false; |
+ } |
+ |
+ // Run accessibility audit. |
+ if (!content::ExecuteScriptAndExtractString(focused_frame, |
+ kAccessibilityTestString, |
+ error_message)) { |
+ *error_message = "Failed to run accessibility audit"; |
+ return false; |
+ } |
+ |
+ // Test result should be empty if there are no errors. |
+ return error_message->empty(); |
+} |
+ |
bool InProcessBrowserTest::CreateUserDataDirectory() { |
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
base::FilePath user_data_dir = |
@@ -434,6 +522,11 @@ void InProcessBrowserTest::RunTestOnMainThreadLoop() { |
// browser. |
content::RunAllPendingInMessageLoop(); |
+ // run_accessibility_checks_for_test_case_ must be set before calling |
+ // SetUpOnMainThread or RunTestOnMainThread so that one or all tests can |
+ // enable/disable the accessibility audit. |
+ run_accessibility_checks_for_test_case_ = false; |
Dan Beam
2015/02/10 01:13:38
why not put this in the initializer list? coverit
hcarmona
2015/02/10 02:31:58
Initialized to make coverity happy, but also kept
|
+ |
SetUpOnMainThread(); |
#if defined(OS_MACOSX) |
autorelease_pool_->Recycle(); |
@@ -445,6 +538,12 @@ void InProcessBrowserTest::RunTestOnMainThreadLoop() { |
autorelease_pool_->Recycle(); |
#endif |
+ if (run_accessibility_checks_for_test_case_) { |
+ std::string error_message; |
+ EXPECT_TRUE(RunAccessibilityChecks(&error_message)); |
+ EXPECT_EQ("", error_message); |
+ } |
+ |
// Invoke cleanup and quit even if there are failures. This is similar to |
// gtest in that it invokes TearDown even if Setup fails. |
TearDownOnMainThread(); |