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 char kAXSTesting[] = "third_party/accessibility-audit/axs_testing.js"; | |
117 // JavaScript snippet to configure and run the accessibility audit. | |
118 const char kAccessibilityTestString[] = | |
119 "var config = new axs.AuditConfiguration();" | |
120 "/* Disable warning about rules that cannot be checked. */" | |
121 "config.showUnsupportedRulesWarning = false;" | |
122 "config.auditRulesToIgnore = [" | |
123 " /*" | |
124 " * The 'elements with meaningful background image' accessibility" | |
125 " * audit (AX_IMAGE_01) does not apply, since Chrome doesn't" | |
126 " * disable background images in high-contrast mode like some" | |
127 " * browsers do." | |
128 " */" | |
129 " 'elementsWithMeaningfulBackgroundImage'," | |
130 " /*" | |
131 " * Most WebUI pages are inside an IFrame, so the 'web page should" | |
132 " * have a title that describes topic or purpose' test (AX_TITLE_01)" | |
133 " * generally does not apply." | |
134 " */" | |
135 " 'pageWithoutTitle'," | |
136 " /*" | |
137 " * Enable when crbug.com/267035 is fixed." | |
138 " * Until then it's just noise." | |
139 " */" | |
140 " 'lowContrastElements'];" | |
141 "var result = axs.Audit.run(config);" | |
142 "for (var i = 0; i < result.length; ++i) {" | |
143 " if (result[i].result == axs.constants.AuditResult.FAIL)" | |
144 " 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
| |
145 "}" | |
146 "domAutomationController.send('');"; | |
147 | |
115 InProcessBrowserTest::InProcessBrowserTest() | 148 InProcessBrowserTest::InProcessBrowserTest() |
116 : browser_(NULL), | 149 : browser_(NULL), |
117 exit_when_last_browser_closes_(true), | 150 exit_when_last_browser_closes_(true), |
118 open_about_blank_on_browser_launch_(true), | 151 open_about_blank_on_browser_launch_(true), |
119 multi_desktop_test_(false) | 152 multi_desktop_test_(false) |
120 #if defined(OS_MACOSX) | 153 #if defined(OS_MACOSX) |
121 , autorelease_pool_(NULL) | 154 , autorelease_pool_(NULL) |
122 #endif // OS_MACOSX | 155 #endif // OS_MACOSX |
123 { | 156 { |
124 #if defined(OS_MACOSX) | 157 #if defined(OS_MACOSX) |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
253 #endif | 286 #endif |
254 | 287 |
255 // TODO(pkotwicz): Investigate if we can remove this switch. | 288 // TODO(pkotwicz): Investigate if we can remove this switch. |
256 if (exit_when_last_browser_closes_) | 289 if (exit_when_last_browser_closes_) |
257 command_line->AppendSwitch(switches::kDisableZeroBrowsersOpenForTests); | 290 command_line->AppendSwitch(switches::kDisableZeroBrowsersOpenForTests); |
258 | 291 |
259 if (open_about_blank_on_browser_launch_ && command_line->GetArgs().empty()) | 292 if (open_about_blank_on_browser_launch_ && command_line->GetArgs().empty()) |
260 command_line->AppendArg(url::kAboutBlankURL); | 293 command_line->AppendArg(url::kAboutBlankURL); |
261 } | 294 } |
262 | 295 |
296 bool InProcessBrowserTest::RunAccessibilityChecks(std::string* error_message) { | |
297 if (!browser()) { | |
298 *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
| |
299 return false; | |
300 } | |
301 auto tab_strip = browser()->tab_strip_model(); | |
302 if (!tab_strip) { | |
303 *error_message = "tab_strip is NULL"; | |
304 return false; | |
305 } | |
306 auto web_contents = tab_strip->GetActiveWebContents(); | |
307 if (!web_contents) { | |
308 *error_message = "web_contents is NULL"; | |
309 return false; | |
310 } | |
311 auto focused_frame = web_contents->GetFocusedFrame(); | |
312 if (!focused_frame) { | |
313 *error_message = "focused_frame is NULL"; | |
314 return false; | |
315 } | |
316 | |
317 // Load accessibility library. | |
318 base::FilePath src_dir; | |
319 if (!PathService::Get(base::DIR_SOURCE_ROOT, &src_dir)) { | |
320 *error_message = "PathService::Get failed"; | |
321 return false; | |
322 } | |
323 base::FilePath script_path = src_dir.Append(kAXSTesting); | |
324 std::string script; | |
325 if (!base::ReadFileToString(script_path, &script)) { | |
326 *error_message = "Could not read " + script_path.value(); | |
327 return false; | |
328 } | |
329 if (!content::ExecuteScript(web_contents, script)) { | |
330 *error_message = "Failed to load accessibility library"; | |
331 return false; | |
332 } | |
333 | |
334 // Run accessibility audit. | |
335 if (!content::ExecuteScriptAndExtractString(focused_frame, | |
336 kAccessibilityTestString, | |
337 error_message)) { | |
338 *error_message = "Failed to run accessibility audit"; | |
339 return false; | |
340 } | |
341 | |
342 // Test result should be empty if there are no errors. | |
343 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.
| |
344 } | |
345 | |
263 bool InProcessBrowserTest::CreateUserDataDirectory() { | 346 bool InProcessBrowserTest::CreateUserDataDirectory() { |
264 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 347 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
265 base::FilePath user_data_dir = | 348 base::FilePath user_data_dir = |
266 command_line->GetSwitchValuePath(switches::kUserDataDir); | 349 command_line->GetSwitchValuePath(switches::kUserDataDir); |
267 if (user_data_dir.empty()) { | 350 if (user_data_dir.empty()) { |
268 if (temp_user_data_dir_.CreateUniqueTempDir() && | 351 if (temp_user_data_dir_.CreateUniqueTempDir() && |
269 temp_user_data_dir_.IsValid()) { | 352 temp_user_data_dir_.IsValid()) { |
270 user_data_dir = temp_user_data_dir_.path(); | 353 user_data_dir = temp_user_data_dir_.path(); |
271 } else { | 354 } else { |
272 LOG(ERROR) << "Could not create temporary user data directory \"" | 355 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 | 510 // deallocation via an autorelease pool (such as browser window closure and |
428 // browser shutdown). To avoid this, the following pool is recycled after each | 511 // browser shutdown). To avoid this, the following pool is recycled after each |
429 // time code is directly executed. | 512 // time code is directly executed. |
430 autorelease_pool_ = new base::mac::ScopedNSAutoreleasePool; | 513 autorelease_pool_ = new base::mac::ScopedNSAutoreleasePool; |
431 #endif | 514 #endif |
432 | 515 |
433 // Pump any pending events that were created as a result of creating a | 516 // Pump any pending events that were created as a result of creating a |
434 // browser. | 517 // browser. |
435 content::RunAllPendingInMessageLoop(); | 518 content::RunAllPendingInMessageLoop(); |
436 | 519 |
520 // run_accessibility_checks_for_test_case_ must be set before calling | |
521 // SetUpOnMainThread or RunTestOnMainThread so that one or all tests can | |
522 // enable/disable the accessibility audit. | |
523 run_accessibility_checks_for_test_case_ = false; | |
524 | |
437 SetUpOnMainThread(); | 525 SetUpOnMainThread(); |
438 #if defined(OS_MACOSX) | 526 #if defined(OS_MACOSX) |
439 autorelease_pool_->Recycle(); | 527 autorelease_pool_->Recycle(); |
440 #endif | 528 #endif |
441 | 529 |
442 if (!HasFatalFailure()) | 530 if (!HasFatalFailure()) |
443 RunTestOnMainThread(); | 531 RunTestOnMainThread(); |
444 #if defined(OS_MACOSX) | 532 #if defined(OS_MACOSX) |
445 autorelease_pool_->Recycle(); | 533 autorelease_pool_->Recycle(); |
446 #endif | 534 #endif |
447 | 535 |
536 if (run_accessibility_checks_for_test_case_) { | |
537 std::string error_message; | |
538 EXPECT_TRUE(RunAccessibilityChecks(&error_message)); | |
539 EXPECT_EQ("", error_message); | |
540 } | |
541 | |
448 // Invoke cleanup and quit even if there are failures. This is similar to | 542 // Invoke cleanup and quit even if there are failures. This is similar to |
449 // gtest in that it invokes TearDown even if Setup fails. | 543 // gtest in that it invokes TearDown even if Setup fails. |
450 TearDownOnMainThread(); | 544 TearDownOnMainThread(); |
451 #if defined(OS_MACOSX) | 545 #if defined(OS_MACOSX) |
452 autorelease_pool_->Recycle(); | 546 autorelease_pool_->Recycle(); |
453 #endif | 547 #endif |
454 | 548 |
455 // Sometimes tests leave Quit tasks in the MessageLoop (for shame), so let's | 549 // 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. | 550 // 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 | 551 // 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 | 582 // On the Mac, this eventually reaches |
489 // -[BrowserWindowController windowWillClose:], which will post a deferred | 583 // -[BrowserWindowController windowWillClose:], which will post a deferred |
490 // -autorelease on itself to ultimately destroy the Browser object. The line | 584 // -autorelease on itself to ultimately destroy the Browser object. The line |
491 // below is necessary to pump these pending messages to ensure all Browsers | 585 // below is necessary to pump these pending messages to ensure all Browsers |
492 // get deleted. | 586 // get deleted. |
493 content::RunAllPendingInMessageLoop(); | 587 content::RunAllPendingInMessageLoop(); |
494 delete autorelease_pool_; | 588 delete autorelease_pool_; |
495 autorelease_pool_ = NULL; | 589 autorelease_pool_ = NULL; |
496 #endif | 590 #endif |
497 } | 591 } |
OLD | NEW |