Chromium Code Reviews| 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..128f80b2dc8f9059ac8a6cbb7057960fb78e76a3 100644 |
| --- a/chrome/test/base/in_process_browser_test.cc |
| +++ b/chrome/test/base/in_process_browser_test.cc |
| @@ -112,6 +112,39 @@ void SingleDesktopTestObserver::OnBrowserAdded(Browser* browser) { |
| } // namespace |
| +// Library used for testing accessibility. |
| +const char kAXSTesting[] = "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'];" |
| + "var result = axs.Audit.run(config);" |
| + "for (var i = 0; i < result.length; ++i) {" |
| + " if (result[i].result == axs.constants.AuditResult.FAIL)" |
| + " domAutomationController.send(axs.Audit.createReport(result));" |
|
Jay Civelli
2015/02/07 01:11:12
Don't you need to return in that case?
hcarmona
2015/02/07 02:22:12
Yes, now I'm wondering why this didn't fail on my
|
| + "}" |
| + "domAutomationController.send('');"; |
| + |
| InProcessBrowserTest::InProcessBrowserTest() |
| : browser_(NULL), |
| exit_when_last_browser_closes_(true), |
| @@ -260,6 +293,56 @@ void InProcessBrowserTest::PrepareTestCommandLine( |
| command_line->AppendArg(url::kAboutBlankURL); |
| } |
| +bool InProcessBrowserTest::RunAccessibilityChecks(std::string* error_message) { |
| + if (!browser()) { |
| + *error_message = "browser is NULL"; |
|
Jay Civelli
2015/02/07 01:11:12
Do we want to support |error_message| potentially
hcarmona
2015/02/07 02:22:12
The error the audit returns is very descriptive ab
|
| + 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 " + script_path.value(); |
| + 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->compare(""); |
|
Jay Civelli
2015/02/07 01:11:12
Shouldn't we just return true? If someone called t
hcarmona
2015/02/07 02:22:12
We can't return true because running the audit pop
Jay Civelli
2015/02/07 02:36:35
Oh, right.
Nit: you could do return !error_message
hcarmona
2015/02/09 22:55:32
Done.
|
| +} |
| + |
| bool InProcessBrowserTest::CreateUserDataDirectory() { |
| base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| base::FilePath user_data_dir = |
| @@ -434,6 +517,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; |
| + |
| SetUpOnMainThread(); |
| #if defined(OS_MACOSX) |
| autorelease_pool_->Recycle(); |
| @@ -445,6 +533,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(); |