Chromium Code Reviews| 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 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.
| |
| 117 FILE_PATH_LITERAL("third_party/accessibility-audit/axs_testing.js")); | |
| 118 // static | |
| 119 const std::string InProcessBrowserTest::kExpectedAccessibilityResults = ""; | |
| 120 // JavaScript snippet to configure and run the accessibility audit. | |
| 121 const std::string kAccessibilityTestString = | |
| 122 "var config = new axs.AuditConfiguration();" | |
| 123 "/* Disable warning about rules that cannot be checked. */" | |
| 124 "config.showUnsupportedRulesWarning = false;" | |
| 125 "config.auditRulesToIgnore = [" | |
| 126 " /* " | |
| 127 " * The 'elements with meaningful background image' accessibility" | |
| 128 " * audit (AX_IMAGE_01) does not apply, since Chrome doesn't" | |
| 129 " * disable background images in high-contrast mode like some" | |
| 130 " * browsers do." | |
| 131 " */" | |
| 132 " 'elementsWithMeaningfulBackgroundImage'," | |
| 133 " /* " | |
| 134 " * Most WebUI pages are inside an IFrame, so the 'web page should" | |
| 135 " * have a title that describes topic or purpose' test (AX_TITLE_01)" | |
| 136 " * generally does not apply." | |
| 137 " */" | |
| 138 " 'pageWithoutTitle'," | |
| 139 " /* " | |
| 140 " * Enable when crbug.com/267035 is fixed." | |
| 141 " * Until then it's just noise." | |
| 142 " */" | |
| 143 " 'lowContrastElements'];" | |
| 144 "var result = axs.Audit.run(config);" | |
| 145 "var pass = true;" | |
| 146 "for (var i = 0; i < result.length; ++i) {" | |
| 147 " if (result[i].result == axs.constants.AuditResult.FAIL) {" | |
| 148 " 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.
| |
| 149 " break;" | |
| 150 " }" | |
| 151 "}" | |
| 152 "if (pass) {" | |
| 153 " domAutomationController.send('');" | |
| 154 "}" | |
| 155 "else {" | |
| 156 " domAutomationController.send(axs.Audit.createReport(result));" | |
| 157 "}"; | |
| 158 | |
| 115 InProcessBrowserTest::InProcessBrowserTest() | 159 InProcessBrowserTest::InProcessBrowserTest() |
| 116 : browser_(NULL), | 160 : browser_(NULL), |
| 117 exit_when_last_browser_closes_(true), | 161 exit_when_last_browser_closes_(true), |
| 118 open_about_blank_on_browser_launch_(true), | 162 open_about_blank_on_browser_launch_(true), |
| 119 multi_desktop_test_(false) | 163 multi_desktop_test_(false), |
| 164 run_accessibility_checks_(false) | |
| 120 #if defined(OS_MACOSX) | 165 #if defined(OS_MACOSX) |
| 121 , autorelease_pool_(NULL) | 166 , autorelease_pool_(NULL) |
| 122 #endif // OS_MACOSX | 167 #endif // OS_MACOSX |
| 123 { | 168 { |
| 124 #if defined(OS_MACOSX) | 169 #if defined(OS_MACOSX) |
| 125 // TODO(phajdan.jr): Make browser_tests self-contained on Mac, remove this. | 170 // 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 | 171 // 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 | 172 // 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 | 173 // processes, on Linux at least (failure to do so will cause a browser_test to |
| 129 // be run instead of a renderer). | 174 // be run instead of a renderer). |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 253 #endif | 298 #endif |
| 254 | 299 |
| 255 // TODO(pkotwicz): Investigate if we can remove this switch. | 300 // TODO(pkotwicz): Investigate if we can remove this switch. |
| 256 if (exit_when_last_browser_closes_) | 301 if (exit_when_last_browser_closes_) |
| 257 command_line->AppendSwitch(switches::kDisableZeroBrowsersOpenForTests); | 302 command_line->AppendSwitch(switches::kDisableZeroBrowsersOpenForTests); |
| 258 | 303 |
| 259 if (open_about_blank_on_browser_launch_ && command_line->GetArgs().empty()) | 304 if (open_about_blank_on_browser_launch_ && command_line->GetArgs().empty()) |
| 260 command_line->AppendArg(url::kAboutBlankURL); | 305 command_line->AppendArg(url::kAboutBlankURL); |
| 261 } | 306 } |
| 262 | 307 |
| 308 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.
| |
| 309 ASSERT_TRUE(browser()); | |
| 310 auto tab_strip = browser()->tab_strip_model(); | |
| 311 ASSERT_TRUE(tab_strip); | |
| 312 auto web_contents = tab_strip->GetActiveWebContents(); | |
| 313 ASSERT_TRUE(web_contents); | |
| 314 auto focused_frame = web_contents->GetFocusedFrame(); | |
| 315 ASSERT_TRUE(focused_frame); | |
| 316 | |
| 317 ASSERT_TRUE(LoadAccessibilityLibrary(web_contents)); | |
| 318 | |
| 319 // Run test. | |
| 320 ASSERT_TRUE(content::ExecuteScriptAndExtractString(focused_frame, | |
| 321 kAccessibilityTestString, | |
| 322 test_result)); | |
| 323 } | |
| 324 | |
| 325 bool InProcessBrowserTest::LoadAccessibilityLibrary( | |
| 326 content::WebContents* web_contents) { | |
| 327 // Path is relative to the source root. | |
| 328 base::FilePath src_dir; | |
| 329 if (!PathService::Get(base::DIR_SOURCE_ROOT, &src_dir)) | |
| 330 return false; | |
| 331 | |
| 332 // Get full path to the library. | |
| 333 base::FilePath script_path = src_dir.Append(kAXSTesting); | |
| 334 | |
| 335 std::string script; | |
| 336 if (!base::ReadFileToString(script_path, &script)) { | |
| 337 LOG(ERROR) << "Failed to load script " << script_path.value(); | |
| 338 return false; | |
| 339 } | |
| 340 | |
| 341 return content::ExecuteScript(web_contents, script); | |
| 342 } | |
| 343 | |
| 263 bool InProcessBrowserTest::CreateUserDataDirectory() { | 344 bool InProcessBrowserTest::CreateUserDataDirectory() { |
| 264 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 345 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 265 base::FilePath user_data_dir = | 346 base::FilePath user_data_dir = |
| 266 command_line->GetSwitchValuePath(switches::kUserDataDir); | 347 command_line->GetSwitchValuePath(switches::kUserDataDir); |
| 267 if (user_data_dir.empty()) { | 348 if (user_data_dir.empty()) { |
| 268 if (temp_user_data_dir_.CreateUniqueTempDir() && | 349 if (temp_user_data_dir_.CreateUniqueTempDir() && |
| 269 temp_user_data_dir_.IsValid()) { | 350 temp_user_data_dir_.IsValid()) { |
| 270 user_data_dir = temp_user_data_dir_.path(); | 351 user_data_dir = temp_user_data_dir_.path(); |
| 271 } else { | 352 } else { |
| 272 LOG(ERROR) << "Could not create temporary user data directory \"" | 353 LOG(ERROR) << "Could not create temporary user data directory \"" |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 358 content::NOTIFICATION_LOAD_STOP, | 439 content::NOTIFICATION_LOAD_STOP, |
| 359 content::NotificationService::AllSources()); | 440 content::NotificationService::AllSources()); |
| 360 chrome::AddSelectedTabWithURL(browser, | 441 chrome::AddSelectedTabWithURL(browser, |
| 361 GURL(url::kAboutBlankURL), | 442 GURL(url::kAboutBlankURL), |
| 362 ui::PAGE_TRANSITION_AUTO_TOPLEVEL); | 443 ui::PAGE_TRANSITION_AUTO_TOPLEVEL); |
| 363 observer.Wait(); | 444 observer.Wait(); |
| 364 | 445 |
| 365 browser->window()->Show(); | 446 browser->window()->Show(); |
| 366 } | 447 } |
| 367 | 448 |
| 449 void InProcessBrowserTest::DisableAccessibilityChecks() { | |
| 450 run_accessibility_checks_for_test_case_ = false; | |
| 451 } | |
| 452 | |
| 453 void InProcessBrowserTest::EnableAccessibilityChecks() { | |
| 454 run_accessibility_checks_for_test_case_ = true; | |
| 455 } | |
| 456 | |
| 368 #if !defined(OS_MACOSX) | 457 #if !defined(OS_MACOSX) |
| 369 base::CommandLine InProcessBrowserTest::GetCommandLineForRelaunch() { | 458 base::CommandLine InProcessBrowserTest::GetCommandLineForRelaunch() { |
| 370 base::CommandLine new_command_line( | 459 base::CommandLine new_command_line( |
| 371 base::CommandLine::ForCurrentProcess()->GetProgram()); | 460 base::CommandLine::ForCurrentProcess()->GetProgram()); |
| 372 base::CommandLine::SwitchMap switches = | 461 base::CommandLine::SwitchMap switches = |
| 373 base::CommandLine::ForCurrentProcess()->GetSwitches(); | 462 base::CommandLine::ForCurrentProcess()->GetSwitches(); |
| 374 switches.erase(switches::kUserDataDir); | 463 switches.erase(switches::kUserDataDir); |
| 375 switches.erase(content::kSingleProcessTestsFlag); | 464 switches.erase(content::kSingleProcessTestsFlag); |
| 376 switches.erase(switches::kSingleProcess); | 465 switches.erase(switches::kSingleProcess); |
| 377 new_command_line.AppendSwitch(content::kLaunchAsBrowser); | 466 new_command_line.AppendSwitch(content::kLaunchAsBrowser); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 |
| 437 SetUpOnMainThread(); | 526 SetUpOnMainThread(); |
| 438 #if defined(OS_MACOSX) | 527 #if defined(OS_MACOSX) |
| 439 autorelease_pool_->Recycle(); | 528 autorelease_pool_->Recycle(); |
| 440 #endif | 529 #endif |
| 441 | 530 |
| 531 // run_accessibility_checks_for_test_case_ must be set BEFORE calling | |
| 532 // RunTestOnMainThread because a test has the option to override the decision | |
| 533 // to run accessibility tests in the test body. | |
| 534 run_accessibility_checks_for_test_case_ = run_accessibility_checks_; | |
| 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 // Accessibility audit will be run if either: | |
| 543 // - The current test called EnableAccessibilityChecks | |
| 544 // - The current test did NOT call DisableAccessibilityChecks and | |
| 545 // run_accessibility_checks_ is true. | |
| 546 if (run_accessibility_checks_for_test_case_) { | |
| 547 std::string test_result; | |
| 548 RunAccessibilityChecks(&test_result); | |
| 549 EXPECT_EQ(kExpectedAccessibilityResults, test_result); | |
| 550 } | |
| 551 | |
| 448 // Invoke cleanup and quit even if there are failures. This is similar to | 552 // Invoke cleanup and quit even if there are failures. This is similar to |
| 449 // gtest in that it invokes TearDown even if Setup fails. | 553 // gtest in that it invokes TearDown even if Setup fails. |
| 450 TearDownOnMainThread(); | 554 TearDownOnMainThread(); |
| 451 #if defined(OS_MACOSX) | 555 #if defined(OS_MACOSX) |
| 452 autorelease_pool_->Recycle(); | 556 autorelease_pool_->Recycle(); |
| 453 #endif | 557 #endif |
| 454 | 558 |
| 455 // Sometimes tests leave Quit tasks in the MessageLoop (for shame), so let's | 559 // 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. | 560 // 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 | 561 // 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 | 592 // On the Mac, this eventually reaches |
| 489 // -[BrowserWindowController windowWillClose:], which will post a deferred | 593 // -[BrowserWindowController windowWillClose:], which will post a deferred |
| 490 // -autorelease on itself to ultimately destroy the Browser object. The line | 594 // -autorelease on itself to ultimately destroy the Browser object. The line |
| 491 // below is necessary to pump these pending messages to ensure all Browsers | 595 // below is necessary to pump these pending messages to ensure all Browsers |
| 492 // get deleted. | 596 // get deleted. |
| 493 content::RunAllPendingInMessageLoop(); | 597 content::RunAllPendingInMessageLoop(); |
| 494 delete autorelease_pool_; | 598 delete autorelease_pool_; |
| 495 autorelease_pool_ = NULL; | 599 autorelease_pool_ = NULL; |
| 496 #endif | 600 #endif |
| 497 } | 601 } |
| OLD | NEW |