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
3 // found in the LICENSE file.
5 #include "chrome/test/base/in_process_browser_test.h"
7 #include "base/auto_reset.h"
8 #include "base/basictypes.h"
10 #include "base/command_line.h"
11 #include "base/files/file_path.h"
12 #include "base/files/file_util.h"
13 #include "base/lazy_instance.h"
14 #include "base/path_service.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "base/test/test_file_util.h"
17 #include "base/threading/non_thread_safe.h"
18 #include "chrome/browser/browser_process.h"
19 #include "chrome/browser/lifetime/application_lifetime.h"
20 #include "chrome/browser/net/net_error_tab_helper.h"
21 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/browser/profiles/profile_manager.h"
23 #include "chrome/browser/ui/browser.h"
24 #include "chrome/browser/ui/browser_finder.h"
25 #include "chrome/browser/ui/browser_list.h"
26 #include "chrome/browser/ui/browser_list_observer.h"
27 #include "chrome/browser/ui/browser_navigator.h"
28 #include "chrome/browser/ui/browser_tabstrip.h"
29 #include "chrome/browser/ui/browser_window.h"
30 #include "chrome/browser/ui/host_desktop.h"
31 #include "chrome/browser/ui/tabs/tab_strip_model.h"
32 #include "chrome/common/chrome_constants.h"
33 #include "chrome/common/chrome_paths.h"
34 #include "chrome/common/chrome_switches.h"
35 #include "chrome/common/logging_chrome.h"
36 #include "chrome/common/url_constants.h"
37 #include "chrome/renderer/chrome_content_renderer_client.h"
38 #include "chrome/test/base/chrome_test_suite.h"
39 #include "chrome/test/base/test_launcher_utils.h"
40 #include "chrome/test/base/test_switches.h"
41 #include "chrome/test/base/testing_browser_process.h"
42 #include "components/google/core/browser/google_util.h"
43 #include "components/os_crypt/os_crypt.h"
44 #include "content/public/browser/notification_service.h"
45 #include "content/public/browser/notification_types.h"
46 #include "content/public/test/browser_test_utils.h"
47 #include "content/public/test/test_launcher.h"
48 #include "content/public/test/test_navigation_observer.h"
49 #include "net/test/embedded_test_server/embedded_test_server.h"
50 #include "net/test/spawned_test_server/spawned_test_server.h"
52 #if defined(OS_MACOSX)
53 #include "base/mac/scoped_nsautorelease_pool.h"
57 #include "base/win/scoped_com_initializer.h"
58 #include "base/win/windows_version.h"
59 #include "ui/base/win/atl_module.h"
60 #include "win8/test/metro_registration_helper.h"
61 #include "win8/test/test_registrar_constants.h"
64 #if defined(ENABLE_CAPTIVE_PORTAL_DETECTION)
65 #include "chrome/browser/captive_portal/captive_portal_service.h"
68 #if !defined(OS_ANDROID) && !defined(OS_IOS)
69 #include "components/storage_monitor/test_storage_monitor.h"
72 #if defined(OS_CHROMEOS)
73 #include "chrome/browser/chromeos/input_method/input_method_configuration.h"
78 // Passed as value of kTestType.
79 const char kBrowserTestType
[] = "browser";
81 // A BrowserListObserver that makes sure that all browsers created are on the
82 // |allowed_desktop_|.
83 class SingleDesktopTestObserver
: public chrome::BrowserListObserver
,
84 public base::NonThreadSafe
{
86 explicit SingleDesktopTestObserver(chrome::HostDesktopType allowed_desktop
);
87 ~SingleDesktopTestObserver() override
;
89 // chrome::BrowserListObserver:
90 void OnBrowserAdded(Browser
* browser
) override
;
93 chrome::HostDesktopType allowed_desktop_
;
95 DISALLOW_COPY_AND_ASSIGN(SingleDesktopTestObserver
);
98 SingleDesktopTestObserver::SingleDesktopTestObserver(
99 chrome::HostDesktopType allowed_desktop
)
100 : allowed_desktop_(allowed_desktop
) {
101 BrowserList::AddObserver(this);
104 SingleDesktopTestObserver::~SingleDesktopTestObserver() {
105 BrowserList::RemoveObserver(this);
108 void SingleDesktopTestObserver::OnBrowserAdded(Browser
* browser
) {
109 CHECK(CalledOnValidThread());
110 CHECK_EQ(browser
->host_desktop_type(), allowed_desktop_
);
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 = ["
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"
130 " 'elementsWithMeaningfulBackgroundImage',"
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."
136 " 'pageWithoutTitle',"
138 " * Enable when crbug.com/267035 is fixed."
139 " * Until then it's just noise."
141 " 'lowContrastElements'"
143 "var result = axs.Audit.run(config);"
145 "for (var i = 0; i < result.length; ++i) {"
146 " if (result[i].result == axs.constants.AuditResult.FAIL) {"
147 " error = axs.Audit.createReport(result);"
151 "domAutomationController.send(error);";
153 InProcessBrowserTest::InProcessBrowserTest()
155 exit_when_last_browser_closes_(true),
156 open_about_blank_on_browser_launch_(true),
157 multi_desktop_test_(false),
158 run_accessibility_checks_for_test_case_(false)
159 #if defined(OS_MACOSX)
160 , autorelease_pool_(NULL
)
163 #if defined(OS_MACOSX)
164 // TODO(phajdan.jr): Make browser_tests self-contained on Mac, remove this.
165 // Before we run the browser, we have to hack the path to the exe to match
166 // what it would be if Chrome was running, because it is used to fork renderer
167 // processes, on Linux at least (failure to do so will cause a browser_test to
168 // be run instead of a renderer).
169 base::FilePath chrome_path
;
170 CHECK(PathService::Get(base::FILE_EXE
, &chrome_path
));
171 chrome_path
= chrome_path
.DirName();
172 chrome_path
= chrome_path
.Append(chrome::kBrowserProcessExecutablePath
);
173 CHECK(PathService::Override(base::FILE_EXE
, chrome_path
));
174 #endif // defined(OS_MACOSX)
176 CreateTestServer(base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
177 base::FilePath src_dir
;
178 CHECK(PathService::Get(base::DIR_SOURCE_ROOT
, &src_dir
));
179 base::FilePath test_data_dir
= src_dir
.AppendASCII("chrome/test/data");
180 embedded_test_server()->ServeFilesFromDirectory(test_data_dir
);
182 // chrome::DIR_TEST_DATA isn't going to be setup until after we call
183 // ContentMain. However that is after tests' constructors or SetUp methods,
184 // which sometimes need it. So just override it.
185 CHECK(PathService::Override(chrome::DIR_TEST_DATA
, test_data_dir
));
188 InProcessBrowserTest::~InProcessBrowserTest() {
191 void InProcessBrowserTest::SetUp() {
192 // Browser tests will create their own g_browser_process later.
193 DCHECK(!g_browser_process
);
195 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
197 // Auto-reload breaks many browser tests, which assume error pages won't be
198 // reloaded out from under them. Tests that expect or desire this behavior can
199 // append switches::kEnableOfflineAutoReload, which will override the disable
201 command_line
->AppendSwitch(switches::kDisableOfflineAutoReload
);
203 // Allow subclasses to change the command line before running any tests.
204 SetUpCommandLine(command_line
);
205 // Add command line arguments that are used by all InProcessBrowserTests.
206 PrepareTestCommandLine(command_line
);
208 // Create a temporary user data directory if required.
209 ASSERT_TRUE(CreateUserDataDirectory())
210 << "Could not create user data directory.";
212 // Allow subclasses the opportunity to make changes to the default user data
213 // dir before running any tests.
214 ASSERT_TRUE(SetUpUserDataDirectory())
215 << "Could not set up user data directory.";
217 #if defined(OS_CHROMEOS)
218 // Make sure that the log directory exists.
219 base::FilePath log_dir
= logging::GetSessionLogFile(*command_line
).DirName();
220 base::CreateDirectory(log_dir
);
221 // Disable IME extension loading to avoid many browser tests failures.
222 chromeos::input_method::DisableExtensionLoading();
223 #endif // defined(OS_CHROMEOS)
225 #if defined(OS_MACOSX)
226 // Always use the MockKeychain if OS encription is used (which is when
227 // anything sensitive gets stored, including Cookies). Without this,
228 // many tests will hang waiting for a user to approve KeyChain access.
229 OSCrypt::UseMockKeychain(true);
232 #if defined(ENABLE_CAPTIVE_PORTAL_DETECTION)
233 CaptivePortalService::set_state_for_testing(
234 CaptivePortalService::DISABLED_FOR_TESTING
);
237 chrome_browser_net::NetErrorTabHelper::set_state_for_testing(
238 chrome_browser_net::NetErrorTabHelper::TESTING_FORCE_DISABLED
);
240 google_util::SetMockLinkDoctorBaseURLForTesting();
243 base::win::Version version
= base::win::GetVersion();
244 // Although Ash officially is only supported for users on Win7+, we still run
245 // ash_unittests on Vista builders, so we still need to initialize COM.
246 if (version
>= base::win::VERSION_VISTA
&&
247 base::CommandLine::ForCurrentProcess()->HasSwitch(
248 switches::kAshBrowserTests
)) {
249 com_initializer_
.reset(new base::win::ScopedCOMInitializer());
250 ui::win::CreateATLModuleIfNeeded();
251 if (version
>= base::win::VERSION_WIN8
)
252 ASSERT_TRUE(win8::MakeTestDefaultBrowserSynchronously());
256 BrowserTestBase::SetUp();
259 void InProcessBrowserTest::PrepareTestCommandLine(
260 base::CommandLine
* command_line
) {
261 // Propagate commandline settings from test_launcher_utils.
262 test_launcher_utils::PrepareBrowserCommandLineForTests(command_line
);
264 // This is a Browser test.
265 command_line
->AppendSwitchASCII(switches::kTestType
, kBrowserTestType
);
268 if (command_line
->HasSwitch(switches::kAshBrowserTests
)) {
269 command_line
->AppendSwitchNative(switches::kViewerLaunchViaAppId
,
270 win8::test::kDefaultTestAppUserModelId
);
271 // Ash already launches with a single browser opened, add kSilentLaunch to
272 // make sure StartupBrowserCreator doesn't attempt to launch a browser on
273 // the native desktop on startup.
274 command_line
->AppendSwitch(switches::kSilentLaunch
);
278 #if defined(OS_MACOSX)
279 // Explicitly set the path of the binary used for child processes, otherwise
280 // they'll try to use browser_tests which doesn't contain ChromeMain.
281 base::FilePath subprocess_path
;
282 PathService::Get(base::FILE_EXE
, &subprocess_path
);
283 // Recreate the real environment, run the helper within the app bundle.
284 subprocess_path
= subprocess_path
.DirName().DirName();
285 DCHECK_EQ(subprocess_path
.BaseName().value(), "Contents");
287 subprocess_path
.Append("Versions").Append(chrome::kChromeVersion
);
289 subprocess_path
.Append(chrome::kHelperProcessExecutablePath
);
290 command_line
->AppendSwitchPath(switches::kBrowserSubprocessPath
,
294 // TODO(pkotwicz): Investigate if we can remove this switch.
295 if (exit_when_last_browser_closes_
)
296 command_line
->AppendSwitch(switches::kDisableZeroBrowsersOpenForTests
);
298 if (open_about_blank_on_browser_launch_
&& command_line
->GetArgs().empty())
299 command_line
->AppendArg(url::kAboutBlankURL
);
302 bool InProcessBrowserTest::RunAccessibilityChecks(std::string
* error_message
) {
304 *error_message
= "browser is NULL";
307 auto tab_strip
= browser()->tab_strip_model();
309 *error_message
= "tab_strip is NULL";
312 auto web_contents
= tab_strip
->GetActiveWebContents();
314 *error_message
= "web_contents is NULL";
317 auto focused_frame
= web_contents
->GetFocusedFrame();
318 if (!focused_frame
) {
319 *error_message
= "focused_frame is NULL";
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";
329 base::FilePath script_path
= src_dir
.Append(kAXSTesting
);
331 if (!base::ReadFileToString(script_path
, &script
)) {
332 *error_message
= "Could not read accessibility library";
335 if (!content::ExecuteScript(web_contents
, script
)) {
336 *error_message
= "Failed to load accessibility library";
340 // Run accessibility audit.
341 if (!content::ExecuteScriptAndExtractString(focused_frame
,
342 kAccessibilityTestString
,
344 *error_message
= "Failed to run accessibility audit";
348 // Test result should be empty if there are no errors.
349 return error_message
->empty();
352 bool InProcessBrowserTest::CreateUserDataDirectory() {
353 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
354 base::FilePath user_data_dir
=
355 command_line
->GetSwitchValuePath(switches::kUserDataDir
);
356 if (user_data_dir
.empty()) {
357 if (temp_user_data_dir_
.CreateUniqueTempDir() &&
358 temp_user_data_dir_
.IsValid()) {
359 user_data_dir
= temp_user_data_dir_
.path();
361 LOG(ERROR
) << "Could not create temporary user data directory \""
362 << temp_user_data_dir_
.path().value() << "\".";
366 return test_launcher_utils::OverrideUserDataDir(user_data_dir
);
369 void InProcessBrowserTest::TearDown() {
370 DCHECK(!g_browser_process
);
372 com_initializer_
.reset();
374 BrowserTestBase::TearDown();
377 // TODO(alexmos): This function should expose success of the underlying
378 // navigation to tests, which should make sure navigations succeed when
379 // appropriate. See https://crbug.com/425335
380 void InProcessBrowserTest::AddTabAtIndexToBrowser(
384 ui::PageTransition transition
,
385 bool check_navigation_success
) {
386 chrome::NavigateParams
params(browser
, url
, transition
);
387 params
.tabstrip_index
= index
;
388 params
.disposition
= NEW_FOREGROUND_TAB
;
389 chrome::Navigate(¶ms
);
391 if (check_navigation_success
)
392 content::WaitForLoadStop(params
.target_contents
);
394 content::WaitForLoadStopWithoutSuccessCheck(params
.target_contents
);
397 void InProcessBrowserTest::AddTabAtIndex(
400 ui::PageTransition transition
) {
401 AddTabAtIndexToBrowser(browser(), index
, url
, transition
, true);
404 bool InProcessBrowserTest::SetUpUserDataDirectory() {
408 // Creates a browser with a single tab (about:blank), waits for the tab to
409 // finish loading and shows the browser.
410 Browser
* InProcessBrowserTest::CreateBrowser(Profile
* profile
) {
411 Browser
* browser
= new Browser(
412 Browser::CreateParams(profile
, chrome::GetActiveDesktop()));
413 AddBlankTabAndShow(browser
);
417 Browser
* InProcessBrowserTest::CreateIncognitoBrowser() {
418 // Create a new browser with using the incognito profile.
419 Browser
* incognito
= new Browser(
420 Browser::CreateParams(browser()->profile()->GetOffTheRecordProfile(),
421 chrome::GetActiveDesktop()));
422 AddBlankTabAndShow(incognito
);
426 Browser
* InProcessBrowserTest::CreateBrowserForPopup(Profile
* profile
) {
428 new Browser(Browser::CreateParams(Browser::TYPE_POPUP
, profile
,
429 chrome::GetActiveDesktop()));
430 AddBlankTabAndShow(browser
);
434 Browser
* InProcessBrowserTest::CreateBrowserForApp(
435 const std::string
& app_name
,
437 Browser
* browser
= new Browser(
438 Browser::CreateParams::CreateForApp(
439 app_name
, false /* trusted_source */, gfx::Rect(), profile
,
440 chrome::GetActiveDesktop()));
441 AddBlankTabAndShow(browser
);
445 void InProcessBrowserTest::AddBlankTabAndShow(Browser
* browser
) {
446 content::WindowedNotificationObserver
observer(
447 content::NOTIFICATION_LOAD_STOP
,
448 content::NotificationService::AllSources());
449 chrome::AddSelectedTabWithURL(browser
,
450 GURL(url::kAboutBlankURL
),
451 ui::PAGE_TRANSITION_AUTO_TOPLEVEL
);
454 browser
->window()->Show();
457 #if !defined(OS_MACOSX)
458 base::CommandLine
InProcessBrowserTest::GetCommandLineForRelaunch() {
459 base::CommandLine
new_command_line(
460 base::CommandLine::ForCurrentProcess()->GetProgram());
461 base::CommandLine::SwitchMap switches
=
462 base::CommandLine::ForCurrentProcess()->GetSwitches();
463 switches
.erase(switches::kUserDataDir
);
464 switches
.erase(content::kSingleProcessTestsFlag
);
465 switches
.erase(switches::kSingleProcess
);
466 new_command_line
.AppendSwitch(content::kLaunchAsBrowser
);
468 base::FilePath user_data_dir
;
469 PathService::Get(chrome::DIR_USER_DATA
, &user_data_dir
);
470 new_command_line
.AppendSwitchPath(switches::kUserDataDir
, user_data_dir
);
472 for (base::CommandLine::SwitchMap::const_iterator iter
= switches
.begin();
473 iter
!= switches
.end(); ++iter
) {
474 new_command_line
.AppendSwitchNative((*iter
).first
, (*iter
).second
);
476 return new_command_line
;
480 void InProcessBrowserTest::RunTestOnMainThreadLoop() {
481 // Pump startup related events.
482 content::RunAllPendingInMessageLoop();
484 chrome::HostDesktopType active_desktop
= chrome::GetActiveDesktop();
485 // Self-adds/removes itself from the BrowserList observers.
486 scoped_ptr
<SingleDesktopTestObserver
> single_desktop_test_observer
;
487 if (!multi_desktop_test_
) {
488 single_desktop_test_observer
.reset(
489 new SingleDesktopTestObserver(active_desktop
));
492 const BrowserList
* active_browser_list
=
493 BrowserList::GetInstance(active_desktop
);
494 if (!active_browser_list
->empty()) {
495 browser_
= active_browser_list
->get(0);
497 // There are cases where windows get created maximized by default.
498 if (browser_
->window()->IsMaximized())
499 browser_
->window()->Restore();
501 content::WaitForLoadStop(
502 browser_
->tab_strip_model()->GetActiveWebContents());
505 #if !defined(OS_ANDROID) && !defined(OS_IOS)
506 // Do not use the real StorageMonitor for tests, which introduces another
507 // source of variability and potential slowness.
508 ASSERT_TRUE(storage_monitor::TestStorageMonitor::CreateForBrowserTests());
511 #if defined(OS_MACOSX)
512 // On Mac, without the following autorelease pool, code which is directly
513 // executed (as opposed to executed inside a message loop) would autorelease
514 // objects into a higher-level pool. This pool is not recycled in-sync with
515 // the message loops' pools and causes problems with code relying on
516 // deallocation via an autorelease pool (such as browser window closure and
517 // browser shutdown). To avoid this, the following pool is recycled after each
518 // time code is directly executed.
519 autorelease_pool_
= new base::mac::ScopedNSAutoreleasePool
;
522 // Pump any pending events that were created as a result of creating a
524 content::RunAllPendingInMessageLoop();
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;
532 #if defined(OS_MACOSX)
533 autorelease_pool_
->Recycle();
536 if (!HasFatalFailure())
537 RunTestOnMainThread();
538 #if defined(OS_MACOSX)
539 autorelease_pool_
->Recycle();
542 if (run_accessibility_checks_for_test_case_
) {
543 std::string error_message
;
544 EXPECT_TRUE(RunAccessibilityChecks(&error_message
));
545 EXPECT_EQ("", error_message
);
548 // Invoke cleanup and quit even if there are failures. This is similar to
549 // gtest in that it invokes TearDown even if Setup fails.
550 TearDownOnMainThread();
551 #if defined(OS_MACOSX)
552 autorelease_pool_
->Recycle();
555 // Sometimes tests leave Quit tasks in the MessageLoop (for shame), so let's
556 // run all pending messages here to avoid preempting the QuitBrowsers tasks.
557 // TODO(jbates) Once crbug.com/134753 is fixed, this can be removed because it
558 // will not be possible to post Quit tasks.
559 content::RunAllPendingInMessageLoop();
562 // All BrowserLists should be empty at this point.
563 for (chrome::HostDesktopType t
= chrome::HOST_DESKTOP_TYPE_FIRST
;
564 t
< chrome::HOST_DESKTOP_TYPE_COUNT
;
565 t
= static_cast<chrome::HostDesktopType
>(t
+ 1)) {
566 CHECK(BrowserList::GetInstance(t
)->empty()) << t
;
570 void InProcessBrowserTest::QuitBrowsers() {
571 if (chrome::GetTotalBrowserCount() == 0) {
572 chrome::NotifyAppTerminating();
576 // Invoke AttemptExit on a running message loop.
577 // AttemptExit exits the message loop after everything has been
578 // shut down properly.
579 base::MessageLoopForUI::current()->PostTask(FROM_HERE
,
580 base::Bind(&chrome::AttemptExit
));
581 content::RunMessageLoop();
583 #if defined(OS_MACOSX)
584 // chrome::AttemptExit() will attempt to close all browsers by deleting
585 // their tab contents. The last tab contents being removed triggers closing of
586 // the browser window.
588 // On the Mac, this eventually reaches
589 // -[BrowserWindowController windowWillClose:], which will post a deferred
590 // -autorelease on itself to ultimately destroy the Browser object. The line
591 // below is necessary to pump these pending messages to ensure all Browsers
593 content::RunAllPendingInMessageLoop();
594 delete autorelease_pool_
;
595 autorelease_pool_
= NULL
;