OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/test/base/in_process_browser_test.h" | 5 #include "chrome/test/base/in_process_browser_test.h" |
6 | 6 |
7 #include "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 BrowserList::RemoveObserver(this); | 105 BrowserList::RemoveObserver(this); |
106 } | 106 } |
107 | 107 |
108 void SingleDesktopTestObserver::OnBrowserAdded(Browser* browser) { | 108 void SingleDesktopTestObserver::OnBrowserAdded(Browser* browser) { |
109 CHECK(CalledOnValidThread()); | 109 CHECK(CalledOnValidThread()); |
110 CHECK_EQ(browser->host_desktop_type(), allowed_desktop_); | 110 CHECK_EQ(browser->host_desktop_type(), allowed_desktop_); |
111 } | 111 } |
112 | 112 |
113 } // namespace | 113 } // namespace |
114 | 114 |
| 115 // Library used for testing accessibility. |
| 116 const base::FilePath::CharType kAXSTesting[] = |
| 117 FILE_PATH_LITERAL("third_party/accessibility-audit/axs_testing.js"); |
| 118 // JavaScript snippet to configure and run the accessibility audit. |
| 119 const char kAccessibilityTestString[] = |
| 120 "var config = new axs.AuditConfiguration();" |
| 121 "/* Disable warning about rules that cannot be checked. */" |
| 122 "config.showUnsupportedRulesWarning = false;" |
| 123 "config.auditRulesToIgnore = [" |
| 124 " /*" |
| 125 " * The 'elements with meaningful background image' accessibility" |
| 126 " * audit (AX_IMAGE_01) does not apply, since Chrome doesn't" |
| 127 " * disable background images in high-contrast mode like some" |
| 128 " * browsers do." |
| 129 " */" |
| 130 " 'elementsWithMeaningfulBackgroundImage'," |
| 131 " /*" |
| 132 " * Most WebUI pages are inside an IFrame, so the 'web page should" |
| 133 " * have a title that describes topic or purpose' test (AX_TITLE_01)" |
| 134 " * generally does not apply." |
| 135 " */" |
| 136 " 'pageWithoutTitle'," |
| 137 " /*" |
| 138 " * Enable when crbug.com/267035 is fixed." |
| 139 " * Until then it's just noise." |
| 140 " */" |
| 141 " 'lowContrastElements'" |
| 142 "];" |
| 143 "var result = axs.Audit.run(config);" |
| 144 "var error = '';" |
| 145 "for (var i = 0; i < result.length; ++i) {" |
| 146 " if (result[i].result == axs.constants.AuditResult.FAIL) {" |
| 147 " error = axs.Audit.createReport(result);" |
| 148 " break;" |
| 149 " }" |
| 150 "}" |
| 151 "domAutomationController.send(error);"; |
| 152 |
115 InProcessBrowserTest::InProcessBrowserTest() | 153 InProcessBrowserTest::InProcessBrowserTest() |
116 : browser_(NULL), | 154 : browser_(NULL), |
117 exit_when_last_browser_closes_(true), | 155 exit_when_last_browser_closes_(true), |
118 open_about_blank_on_browser_launch_(true), | 156 open_about_blank_on_browser_launch_(true), |
119 multi_desktop_test_(false) | 157 multi_desktop_test_(false), |
| 158 run_accessibility_checks_for_test_case_(false) |
120 #if defined(OS_MACOSX) | 159 #if defined(OS_MACOSX) |
121 , autorelease_pool_(NULL) | 160 , autorelease_pool_(NULL) |
122 #endif // OS_MACOSX | 161 #endif // OS_MACOSX |
123 { | 162 { |
124 #if defined(OS_MACOSX) | 163 #if defined(OS_MACOSX) |
125 // TODO(phajdan.jr): Make browser_tests self-contained on Mac, remove this. | 164 // TODO(phajdan.jr): Make browser_tests self-contained on Mac, remove this. |
126 // Before we run the browser, we have to hack the path to the exe to match | 165 // Before we run the browser, we have to hack the path to the exe to match |
127 // what it would be if Chrome was running, because it is used to fork renderer | 166 // what it would be if Chrome was running, because it is used to fork renderer |
128 // processes, on Linux at least (failure to do so will cause a browser_test to | 167 // processes, on Linux at least (failure to do so will cause a browser_test to |
129 // be run instead of a renderer). | 168 // be run instead of a renderer). |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 #endif | 292 #endif |
254 | 293 |
255 // TODO(pkotwicz): Investigate if we can remove this switch. | 294 // TODO(pkotwicz): Investigate if we can remove this switch. |
256 if (exit_when_last_browser_closes_) | 295 if (exit_when_last_browser_closes_) |
257 command_line->AppendSwitch(switches::kDisableZeroBrowsersOpenForTests); | 296 command_line->AppendSwitch(switches::kDisableZeroBrowsersOpenForTests); |
258 | 297 |
259 if (open_about_blank_on_browser_launch_ && command_line->GetArgs().empty()) | 298 if (open_about_blank_on_browser_launch_ && command_line->GetArgs().empty()) |
260 command_line->AppendArg(url::kAboutBlankURL); | 299 command_line->AppendArg(url::kAboutBlankURL); |
261 } | 300 } |
262 | 301 |
| 302 bool InProcessBrowserTest::RunAccessibilityChecks(std::string* error_message) { |
| 303 if (!browser()) { |
| 304 *error_message = "browser is NULL"; |
| 305 return false; |
| 306 } |
| 307 auto tab_strip = browser()->tab_strip_model(); |
| 308 if (!tab_strip) { |
| 309 *error_message = "tab_strip is NULL"; |
| 310 return false; |
| 311 } |
| 312 auto web_contents = tab_strip->GetActiveWebContents(); |
| 313 if (!web_contents) { |
| 314 *error_message = "web_contents is NULL"; |
| 315 return false; |
| 316 } |
| 317 auto focused_frame = web_contents->GetFocusedFrame(); |
| 318 if (!focused_frame) { |
| 319 *error_message = "focused_frame is NULL"; |
| 320 return false; |
| 321 } |
| 322 |
| 323 // Load accessibility library. |
| 324 base::FilePath src_dir; |
| 325 if (!PathService::Get(base::DIR_SOURCE_ROOT, &src_dir)) { |
| 326 *error_message = "PathService::Get failed"; |
| 327 return false; |
| 328 } |
| 329 base::FilePath script_path = src_dir.Append(kAXSTesting); |
| 330 std::string script; |
| 331 if (!base::ReadFileToString(script_path, &script)) { |
| 332 *error_message = "Could not read accessibility library"; |
| 333 return false; |
| 334 } |
| 335 if (!content::ExecuteScript(web_contents, script)) { |
| 336 *error_message = "Failed to load accessibility library"; |
| 337 return false; |
| 338 } |
| 339 |
| 340 // Run accessibility audit. |
| 341 if (!content::ExecuteScriptAndExtractString(focused_frame, |
| 342 kAccessibilityTestString, |
| 343 error_message)) { |
| 344 *error_message = "Failed to run accessibility audit"; |
| 345 return false; |
| 346 } |
| 347 |
| 348 // Test result should be empty if there are no errors. |
| 349 return error_message->empty(); |
| 350 } |
| 351 |
263 bool InProcessBrowserTest::CreateUserDataDirectory() { | 352 bool InProcessBrowserTest::CreateUserDataDirectory() { |
264 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 353 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
265 base::FilePath user_data_dir = | 354 base::FilePath user_data_dir = |
266 command_line->GetSwitchValuePath(switches::kUserDataDir); | 355 command_line->GetSwitchValuePath(switches::kUserDataDir); |
267 if (user_data_dir.empty()) { | 356 if (user_data_dir.empty()) { |
268 if (temp_user_data_dir_.CreateUniqueTempDir() && | 357 if (temp_user_data_dir_.CreateUniqueTempDir() && |
269 temp_user_data_dir_.IsValid()) { | 358 temp_user_data_dir_.IsValid()) { |
270 user_data_dir = temp_user_data_dir_.path(); | 359 user_data_dir = temp_user_data_dir_.path(); |
271 } else { | 360 } else { |
272 LOG(ERROR) << "Could not create temporary user data directory \"" | 361 LOG(ERROR) << "Could not create temporary user data directory \"" |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
427 // deallocation via an autorelease pool (such as browser window closure and | 516 // deallocation via an autorelease pool (such as browser window closure and |
428 // browser shutdown). To avoid this, the following pool is recycled after each | 517 // browser shutdown). To avoid this, the following pool is recycled after each |
429 // time code is directly executed. | 518 // time code is directly executed. |
430 autorelease_pool_ = new base::mac::ScopedNSAutoreleasePool; | 519 autorelease_pool_ = new base::mac::ScopedNSAutoreleasePool; |
431 #endif | 520 #endif |
432 | 521 |
433 // Pump any pending events that were created as a result of creating a | 522 // Pump any pending events that were created as a result of creating a |
434 // browser. | 523 // browser. |
435 content::RunAllPendingInMessageLoop(); | 524 content::RunAllPendingInMessageLoop(); |
436 | 525 |
| 526 // run_accessibility_checks_for_test_case_ must be set before calling |
| 527 // SetUpOnMainThread or RunTestOnMainThread so that one or all tests can |
| 528 // enable/disable the accessibility audit. |
| 529 run_accessibility_checks_for_test_case_ = false; |
| 530 |
437 SetUpOnMainThread(); | 531 SetUpOnMainThread(); |
438 #if defined(OS_MACOSX) | 532 #if defined(OS_MACOSX) |
439 autorelease_pool_->Recycle(); | 533 autorelease_pool_->Recycle(); |
440 #endif | 534 #endif |
441 | 535 |
442 if (!HasFatalFailure()) | 536 if (!HasFatalFailure()) |
443 RunTestOnMainThread(); | 537 RunTestOnMainThread(); |
444 #if defined(OS_MACOSX) | 538 #if defined(OS_MACOSX) |
445 autorelease_pool_->Recycle(); | 539 autorelease_pool_->Recycle(); |
446 #endif | 540 #endif |
447 | 541 |
| 542 if (run_accessibility_checks_for_test_case_) { |
| 543 std::string error_message; |
| 544 EXPECT_TRUE(RunAccessibilityChecks(&error_message)); |
| 545 EXPECT_EQ("", error_message); |
| 546 } |
| 547 |
448 // Invoke cleanup and quit even if there are failures. This is similar to | 548 // Invoke cleanup and quit even if there are failures. This is similar to |
449 // gtest in that it invokes TearDown even if Setup fails. | 549 // gtest in that it invokes TearDown even if Setup fails. |
450 TearDownOnMainThread(); | 550 TearDownOnMainThread(); |
451 #if defined(OS_MACOSX) | 551 #if defined(OS_MACOSX) |
452 autorelease_pool_->Recycle(); | 552 autorelease_pool_->Recycle(); |
453 #endif | 553 #endif |
454 | 554 |
455 // Sometimes tests leave Quit tasks in the MessageLoop (for shame), so let's | 555 // Sometimes tests leave Quit tasks in the MessageLoop (for shame), so let's |
456 // run all pending messages here to avoid preempting the QuitBrowsers tasks. | 556 // run all pending messages here to avoid preempting the QuitBrowsers tasks. |
457 // TODO(jbates) Once crbug.com/134753 is fixed, this can be removed because it | 557 // TODO(jbates) Once crbug.com/134753 is fixed, this can be removed because it |
(...skipping 30 matching lines...) Expand all Loading... |
488 // On the Mac, this eventually reaches | 588 // On the Mac, this eventually reaches |
489 // -[BrowserWindowController windowWillClose:], which will post a deferred | 589 // -[BrowserWindowController windowWillClose:], which will post a deferred |
490 // -autorelease on itself to ultimately destroy the Browser object. The line | 590 // -autorelease on itself to ultimately destroy the Browser object. The line |
491 // below is necessary to pump these pending messages to ensure all Browsers | 591 // below is necessary to pump these pending messages to ensure all Browsers |
492 // get deleted. | 592 // get deleted. |
493 content::RunAllPendingInMessageLoop(); | 593 content::RunAllPendingInMessageLoop(); |
494 delete autorelease_pool_; | 594 delete autorelease_pool_; |
495 autorelease_pool_ = NULL; | 595 autorelease_pool_ = NULL; |
496 #endif | 596 #endif |
497 } | 597 } |
OLD | NEW |