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::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'];" | |
|
Dan Beam
2015/02/10 01:13:38
nit: ] on new line
hcarmona
2015/02/10 02:31:58
Done.
| |
| 142 "var result = axs.Audit.run(config);" | |
| 143 "function getError() {" | |
| 144 " for (var i = 0; i < result.length; ++i) {" | |
| 145 " if (result[i].result == axs.constants.AuditResult.FAIL) {" | |
| 146 " return axs.Audit.createReport(result);" | |
| 147 " }" | |
|
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
| |
| 148 " }" | |
| 149 " 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.
| |
| 150 "}" | |
| 151 "domAutomationController.send(getError());"; | |
| 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) |
| 120 #if defined(OS_MACOSX) | 158 #if defined(OS_MACOSX) |
| 121 , autorelease_pool_(NULL) | 159 , autorelease_pool_(NULL) |
| 122 #endif // OS_MACOSX | 160 #endif // OS_MACOSX |
| 123 { | 161 { |
| 124 #if defined(OS_MACOSX) | 162 #if defined(OS_MACOSX) |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 253 #endif | 291 #endif |
| 254 | 292 |
| 255 // TODO(pkotwicz): Investigate if we can remove this switch. | 293 // TODO(pkotwicz): Investigate if we can remove this switch. |
| 256 if (exit_when_last_browser_closes_) | 294 if (exit_when_last_browser_closes_) |
| 257 command_line->AppendSwitch(switches::kDisableZeroBrowsersOpenForTests); | 295 command_line->AppendSwitch(switches::kDisableZeroBrowsersOpenForTests); |
| 258 | 296 |
| 259 if (open_about_blank_on_browser_launch_ && command_line->GetArgs().empty()) | 297 if (open_about_blank_on_browser_launch_ && command_line->GetArgs().empty()) |
| 260 command_line->AppendArg(url::kAboutBlankURL); | 298 command_line->AppendArg(url::kAboutBlankURL); |
| 261 } | 299 } |
| 262 | 300 |
| 301 bool InProcessBrowserTest::RunAccessibilityChecks(std::string* error_message) { | |
| 302 if (!browser()) { | |
| 303 *error_message = "browser is NULL"; | |
| 304 return false; | |
| 305 } | |
| 306 auto tab_strip = browser()->tab_strip_model(); | |
| 307 if (!tab_strip) { | |
| 308 *error_message = "tab_strip is NULL"; | |
| 309 return false; | |
| 310 } | |
| 311 auto web_contents = tab_strip->GetActiveWebContents(); | |
| 312 if (!web_contents) { | |
| 313 *error_message = "web_contents is NULL"; | |
| 314 return false; | |
| 315 } | |
| 316 auto focused_frame = web_contents->GetFocusedFrame(); | |
| 317 if (!focused_frame) { | |
| 318 *error_message = "focused_frame is NULL"; | |
| 319 return false; | |
| 320 } | |
| 321 | |
| 322 // Load accessibility library. | |
| 323 base::FilePath src_dir; | |
| 324 if (!PathService::Get(base::DIR_SOURCE_ROOT, &src_dir)) { | |
| 325 *error_message = "PathService::Get failed"; | |
| 326 return false; | |
| 327 } | |
| 328 base::FilePath script_path = src_dir.Append(kAXSTesting); | |
| 329 std::string script; | |
| 330 if (!base::ReadFileToString(script_path, &script)) { | |
| 331 *error_message = "Could not read accessibility library"; | |
| 332 return false; | |
| 333 } | |
| 334 if (!content::ExecuteScript(web_contents, script)) { | |
| 335 *error_message = "Failed to load accessibility library"; | |
| 336 return false; | |
| 337 } | |
| 338 | |
| 339 // Run accessibility audit. | |
| 340 if (!content::ExecuteScriptAndExtractString(focused_frame, | |
| 341 kAccessibilityTestString, | |
| 342 error_message)) { | |
| 343 *error_message = "Failed to run accessibility audit"; | |
| 344 return false; | |
| 345 } | |
| 346 | |
| 347 // Test result should be empty if there are no errors. | |
| 348 return error_message->empty(); | |
| 349 } | |
| 350 | |
| 263 bool InProcessBrowserTest::CreateUserDataDirectory() { | 351 bool InProcessBrowserTest::CreateUserDataDirectory() { |
| 264 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 352 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 265 base::FilePath user_data_dir = | 353 base::FilePath user_data_dir = |
| 266 command_line->GetSwitchValuePath(switches::kUserDataDir); | 354 command_line->GetSwitchValuePath(switches::kUserDataDir); |
| 267 if (user_data_dir.empty()) { | 355 if (user_data_dir.empty()) { |
| 268 if (temp_user_data_dir_.CreateUniqueTempDir() && | 356 if (temp_user_data_dir_.CreateUniqueTempDir() && |
| 269 temp_user_data_dir_.IsValid()) { | 357 temp_user_data_dir_.IsValid()) { |
| 270 user_data_dir = temp_user_data_dir_.path(); | 358 user_data_dir = temp_user_data_dir_.path(); |
| 271 } else { | 359 } else { |
| 272 LOG(ERROR) << "Could not create temporary user data directory \"" | 360 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 | 515 // deallocation via an autorelease pool (such as browser window closure and |
| 428 // browser shutdown). To avoid this, the following pool is recycled after each | 516 // browser shutdown). To avoid this, the following pool is recycled after each |
| 429 // time code is directly executed. | 517 // time code is directly executed. |
| 430 autorelease_pool_ = new base::mac::ScopedNSAutoreleasePool; | 518 autorelease_pool_ = new base::mac::ScopedNSAutoreleasePool; |
| 431 #endif | 519 #endif |
| 432 | 520 |
| 433 // Pump any pending events that were created as a result of creating a | 521 // Pump any pending events that were created as a result of creating a |
| 434 // browser. | 522 // browser. |
| 435 content::RunAllPendingInMessageLoop(); | 523 content::RunAllPendingInMessageLoop(); |
| 436 | 524 |
| 525 // run_accessibility_checks_for_test_case_ must be set before calling | |
| 526 // SetUpOnMainThread or RunTestOnMainThread so that one or all tests can | |
| 527 // enable/disable the accessibility audit. | |
| 528 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
| |
| 529 | |
| 437 SetUpOnMainThread(); | 530 SetUpOnMainThread(); |
| 438 #if defined(OS_MACOSX) | 531 #if defined(OS_MACOSX) |
| 439 autorelease_pool_->Recycle(); | 532 autorelease_pool_->Recycle(); |
| 440 #endif | 533 #endif |
| 441 | 534 |
| 442 if (!HasFatalFailure()) | 535 if (!HasFatalFailure()) |
| 443 RunTestOnMainThread(); | 536 RunTestOnMainThread(); |
| 444 #if defined(OS_MACOSX) | 537 #if defined(OS_MACOSX) |
| 445 autorelease_pool_->Recycle(); | 538 autorelease_pool_->Recycle(); |
| 446 #endif | 539 #endif |
| 447 | 540 |
| 541 if (run_accessibility_checks_for_test_case_) { | |
| 542 std::string error_message; | |
| 543 EXPECT_TRUE(RunAccessibilityChecks(&error_message)); | |
| 544 EXPECT_EQ("", error_message); | |
| 545 } | |
| 546 | |
| 448 // Invoke cleanup and quit even if there are failures. This is similar to | 547 // Invoke cleanup and quit even if there are failures. This is similar to |
| 449 // gtest in that it invokes TearDown even if Setup fails. | 548 // gtest in that it invokes TearDown even if Setup fails. |
| 450 TearDownOnMainThread(); | 549 TearDownOnMainThread(); |
| 451 #if defined(OS_MACOSX) | 550 #if defined(OS_MACOSX) |
| 452 autorelease_pool_->Recycle(); | 551 autorelease_pool_->Recycle(); |
| 453 #endif | 552 #endif |
| 454 | 553 |
| 455 // Sometimes tests leave Quit tasks in the MessageLoop (for shame), so let's | 554 // 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. | 555 // 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 | 556 // 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 | 587 // On the Mac, this eventually reaches |
| 489 // -[BrowserWindowController windowWillClose:], which will post a deferred | 588 // -[BrowserWindowController windowWillClose:], which will post a deferred |
| 490 // -autorelease on itself to ultimately destroy the Browser object. The line | 589 // -autorelease on itself to ultimately destroy the Browser object. The line |
| 491 // below is necessary to pump these pending messages to ensure all Browsers | 590 // below is necessary to pump these pending messages to ensure all Browsers |
| 492 // get deleted. | 591 // get deleted. |
| 493 content::RunAllPendingInMessageLoop(); | 592 content::RunAllPendingInMessageLoop(); |
| 494 delete autorelease_pool_; | 593 delete autorelease_pool_; |
| 495 autorelease_pool_ = NULL; | 594 autorelease_pool_ = NULL; |
| 496 #endif | 595 #endif |
| 497 } | 596 } |
| OLD | NEW |