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..f9256aed5b96d10451461cc93eb3b2e655b252bb 100644 |
| --- a/chrome/test/base/in_process_browser_test.cc |
| +++ b/chrome/test/base/in_process_browser_test.cc |
| @@ -112,11 +112,56 @@ void SingleDesktopTestObserver::OnBrowserAdded(Browser* browser) { |
| } // namespace |
| +// Library used for testing accessibility. |
| +const base::FilePath kAXSTesting( |
|
Jay Civelli
2015/02/05 23:29:09
Style guide recommends to use const char[] as cons
hcarmona
2015/02/06 22:50:53
Done.
|
| + FILE_PATH_LITERAL("third_party/accessibility-audit/axs_testing.js")); |
| +// static |
| +const std::string InProcessBrowserTest::kExpectedAccessibilityResults = ""; |
| +// JavaScript snippet to configure and run the accessibility audit. |
| +const std::string 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);" |
| + "var pass = true;" |
| + "for (var i = 0; i < result.length; ++i) {" |
| + " if (result[i].result == axs.constants.AuditResult.FAIL) {" |
| + " pass = false;" |
|
Jay Civelli
2015/02/05 23:29:09
The code would be shorter if you simply called dom
hcarmona
2015/02/06 22:50:53
Done.
|
| + " break;" |
| + " }" |
| + "}" |
| + "if (pass) {" |
| + " domAutomationController.send('');" |
| + "}" |
| + "else {" |
| + " domAutomationController.send(axs.Audit.createReport(result));" |
| + "}"; |
| + |
| InProcessBrowserTest::InProcessBrowserTest() |
| : browser_(NULL), |
| exit_when_last_browser_closes_(true), |
| open_about_blank_on_browser_launch_(true), |
| - multi_desktop_test_(false) |
| + multi_desktop_test_(false), |
| + run_accessibility_checks_(false) |
| #if defined(OS_MACOSX) |
| , autorelease_pool_(NULL) |
| #endif // OS_MACOSX |
| @@ -260,6 +305,42 @@ void InProcessBrowserTest::PrepareTestCommandLine( |
| command_line->AppendArg(url::kAboutBlankURL); |
| } |
| +void InProcessBrowserTest::RunAccessibilityChecks(std::string *test_result) { |
|
Jay Civelli
2015/02/05 23:29:09
std::string*
hcarmona
2015/02/06 22:50:53
Done.
|
| + ASSERT_TRUE(browser()); |
| + auto tab_strip = browser()->tab_strip_model(); |
| + ASSERT_TRUE(tab_strip); |
| + auto web_contents = tab_strip->GetActiveWebContents(); |
| + ASSERT_TRUE(web_contents); |
| + auto focused_frame = web_contents->GetFocusedFrame(); |
| + ASSERT_TRUE(focused_frame); |
| + |
| + ASSERT_TRUE(LoadAccessibilityLibrary(web_contents)); |
| + |
| + // Run test. |
| + ASSERT_TRUE(content::ExecuteScriptAndExtractString(focused_frame, |
| + kAccessibilityTestString, |
| + test_result)); |
| +} |
| + |
| +bool InProcessBrowserTest::LoadAccessibilityLibrary( |
| + content::WebContents* web_contents) { |
| + // Path is relative to the source root. |
| + base::FilePath src_dir; |
| + if (!PathService::Get(base::DIR_SOURCE_ROOT, &src_dir)) |
| + return false; |
| + |
| + // Get full path to the library. |
| + base::FilePath script_path = src_dir.Append(kAXSTesting); |
| + |
| + std::string script; |
| + if (!base::ReadFileToString(script_path, &script)) { |
| + LOG(ERROR) << "Failed to load script " << script_path.value(); |
| + return false; |
| + } |
| + |
| + return content::ExecuteScript(web_contents, script); |
| +} |
| + |
| bool InProcessBrowserTest::CreateUserDataDirectory() { |
| base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| base::FilePath user_data_dir = |
| @@ -365,6 +446,14 @@ void InProcessBrowserTest::AddBlankTabAndShow(Browser* browser) { |
| browser->window()->Show(); |
| } |
| +void InProcessBrowserTest::DisableAccessibilityChecks() { |
| + run_accessibility_checks_for_test_case_ = false; |
| +} |
| + |
| +void InProcessBrowserTest::EnableAccessibilityChecks() { |
| + run_accessibility_checks_for_test_case_ = true; |
| +} |
| + |
| #if !defined(OS_MACOSX) |
| base::CommandLine InProcessBrowserTest::GetCommandLineForRelaunch() { |
| base::CommandLine new_command_line( |
| @@ -439,12 +528,27 @@ void InProcessBrowserTest::RunTestOnMainThreadLoop() { |
| autorelease_pool_->Recycle(); |
| #endif |
| + // run_accessibility_checks_for_test_case_ must be set BEFORE calling |
| + // RunTestOnMainThread because a test has the option to override the decision |
| + // to run accessibility tests in the test body. |
| + run_accessibility_checks_for_test_case_ = run_accessibility_checks_; |
| + |
| if (!HasFatalFailure()) |
| RunTestOnMainThread(); |
| #if defined(OS_MACOSX) |
| autorelease_pool_->Recycle(); |
| #endif |
| + // Accessibility audit will be run if either: |
| + // - The current test called EnableAccessibilityChecks |
| + // - The current test did NOT call DisableAccessibilityChecks and |
| + // run_accessibility_checks_ is true. |
| + if (run_accessibility_checks_for_test_case_) { |
| + std::string test_result; |
| + RunAccessibilityChecks(&test_result); |
| + EXPECT_EQ(kExpectedAccessibilityResults, test_result); |
| + } |
| + |
| // Invoke cleanup and quit even if there are failures. This is similar to |
| // gtest in that it invokes TearDown even if Setup fails. |
| TearDownOnMainThread(); |