blob: 8c37bc57a1c43139aa18b1fae0a1705cb441c5e7 [file] [log] [blame]
[email protected]c4ff4952010-01-08 19:12:471// Copyright (c) 2010 The Chromium Authors. All rights reserved.
[email protected]d4515eb2009-01-30 00:40:432// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/test/in_process_browser_test.h"
6
7#include "base/command_line.h"
[email protected]d8412052009-04-21 22:01:018#include "base/file_path.h"
[email protected]d4515eb2009-01-30 00:40:439#include "base/file_util.h"
10#include "base/path_service.h"
[email protected]304a3172010-05-04 05:38:4411#include "base/scoped_nsautorelease_pool.h"
[email protected]fb895c62009-10-09 18:20:3012#include "base/test/test_file_util.h"
[email protected]d4515eb2009-01-30 00:40:4313#include "chrome/browser/browser.h"
[email protected]b6e38ef2009-06-16 00:43:2314#include "chrome/browser/browser_list.h"
[email protected]d4515eb2009-01-30 00:40:4315#include "chrome/browser/browser_process.h"
16#include "chrome/browser/browser_shutdown.h"
[email protected]108c2a12009-06-05 22:18:0917#include "chrome/browser/browser_window.h"
[email protected]6fad2632009-11-02 05:59:3718#include "chrome/browser/chrome_thread.h"
[email protected]c4ff4952010-01-08 19:12:4719#include "chrome/browser/intranet_redirect_detector.h"
[email protected]0ac83682010-01-22 17:46:2720#include "chrome/browser/io_thread.h"
[email protected]cec4a272009-07-31 21:55:0321#include "chrome/browser/net/url_request_mock_util.h"
[email protected]d4515eb2009-01-30 00:40:4322#include "chrome/browser/profile.h"
23#include "chrome/browser/profile_manager.h"
[email protected]8bcdec92009-02-25 16:15:1824#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]5c238752009-06-13 10:29:0725#include "chrome/browser/tab_contents/tab_contents.h"
[email protected]108c2a12009-06-05 22:18:0926#if defined(OS_WIN)
[email protected]d4515eb2009-01-30 00:40:4327#include "chrome/browser/views/frame/browser_view.h"
[email protected]108c2a12009-06-05 22:18:0928#endif
[email protected]d4515eb2009-01-30 00:40:4329#include "chrome/common/chrome_constants.h"
30#include "chrome/common/chrome_paths.h"
31#include "chrome/common/chrome_switches.h"
32#include "chrome/common/main_function_params.h"
[email protected]ad1f9bd2009-07-30 20:23:1533#include "chrome/common/notification_registrar.h"
34#include "chrome/common/notification_type.h"
[email protected]e0d481582009-09-15 21:06:2535#include "chrome/common/url_constants.h"
[email protected]d4515eb2009-01-30 00:40:4336#include "chrome/test/testing_browser_process.h"
37#include "chrome/test/ui_test_utils.h"
[email protected]c4ff4952010-01-08 19:12:4738#include "net/base/mock_host_resolver.h"
[email protected]3985ba82010-07-29 21:44:1239#include "net/test/test_server.h"
[email protected]d4515eb2009-01-30 00:40:4340#include "sandbox/src/dep.h"
41
[email protected]8ecd3aad2009-11-04 08:32:2242#if defined(OS_LINUX)
43#include "base/singleton.h"
44#include "chrome/browser/renderer_host/render_sandbox_host_linux.h"
45#include "chrome/browser/zygote_host_linux.h"
46
47namespace {
48
49// A helper class to do Linux-only initialization only once per process.
50class LinuxHostInit {
51 public:
52 LinuxHostInit() {
[email protected]576a4ca2009-11-05 01:41:0953 RenderSandboxHostLinux* shost = Singleton<RenderSandboxHostLinux>::get();
[email protected]8ecd3aad2009-11-04 08:32:2254 shost->Init("");
[email protected]576a4ca2009-11-05 01:41:0955 ZygoteHost* zhost = Singleton<ZygoteHost>::get();
[email protected]8ecd3aad2009-11-04 08:32:2256 zhost->Init("");
57 }
58 ~LinuxHostInit() {}
59};
60
61} // namespace
62#endif
63
[email protected]d4515eb2009-01-30 00:40:4364extern int BrowserMain(const MainFunctionParams&);
65
66const wchar_t kUnitTestShowWindows[] = L"show-windows";
67
[email protected]ddf8a4b02010-03-22 23:08:3068// Passed as value of kTestType.
69static const char kBrowserTestType[] = "browser";
70
[email protected]4ff446f2009-07-10 18:29:3971// Default delay for the time-out at which we stop the
72// inner-message loop the first time.
[email protected]b5f95102009-07-01 19:53:5973const int kInitialTimeoutInMS = 30000;
74
75// Delay for sub-sequent time-outs once the initial time-out happened.
76const int kSubsequentTimeoutInMS = 5000;
77
[email protected]8bcdec92009-02-25 16:15:1878InProcessBrowserTest::InProcessBrowserTest()
79 : browser_(NULL),
80 show_window_(false),
[email protected]56cdae32009-03-12 19:58:2081 dom_automation_enabled_(false),
[email protected]0b4d3382010-07-14 16:13:0482 tab_closeable_state_watcher_enabled_(false),
[email protected]4ff446f2009-07-10 18:29:3983 original_single_process_(false),
84 initial_timeout_(kInitialTimeoutInMS) {
[email protected]d4515eb2009-01-30 00:40:4385}
86
[email protected]c4ff4952010-01-08 19:12:4787InProcessBrowserTest::~InProcessBrowserTest() {
88}
89
[email protected]d4515eb2009-01-30 00:40:4390void InProcessBrowserTest::SetUp() {
91 // Cleanup the user data dir.
[email protected]23cc9a12009-07-30 21:27:0192 FilePath user_data_dir;
[email protected]d4515eb2009-01-30 00:40:4393 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
[email protected]23cc9a12009-07-30 21:27:0194 ASSERT_LT(10, static_cast<int>(user_data_dir.value().size())) <<
[email protected]d4515eb2009-01-30 00:40:4395 "The user data directory name passed into this test was too "
96 "short to delete safely. Please check the user-data-dir "
97 "argument and try again.";
[email protected]298883bc2010-04-30 14:50:5898 ASSERT_TRUE(file_util::DieFileDie(user_data_dir, true));
[email protected]d4515eb2009-01-30 00:40:4399
[email protected]4a44bc32010-05-28 22:22:44100 // Recreate the user data dir. (PathService::Get guarantees that the directory
101 // exists if it returns true, but it only actually checks on the first call,
102 // the rest are cached. Thus we need to recreate it ourselves to not break
103 // the PathService guarantee.)
104 ASSERT_TRUE(file_util::CreateDirectory(user_data_dir));
105
[email protected]d4515eb2009-01-30 00:40:43106 // The unit test suite creates a testingbrowser, but we want the real thing.
107 // Delete the current one. We'll install the testing one in TearDown.
108 delete g_browser_process;
[email protected]298883bc2010-04-30 14:50:58109 g_browser_process = NULL;
110
111 SetUpUserDataDirectory();
[email protected]d4515eb2009-01-30 00:40:43112
113 // Don't delete the resources when BrowserMain returns. Many ui classes
114 // cache SkBitmaps in a static field so that if we delete the resource
115 // bundle we'll crash.
116 browser_shutdown::delete_resources_on_shutdown = false;
117
[email protected]986088a62010-05-13 18:59:20118 // Remember the command line. Normally this doesn't matter, because the test
119 // harness creates a new process for each test, but when the test harness is
120 // running in single process mode, we can't let one test's command-line
121 // changes (e.g. enabling DOM automation) affect other tests.
[email protected]d4515eb2009-01-30 00:40:43122 CommandLine* command_line = CommandLine::ForCurrentProcessMutable();
[email protected]56cdae32009-03-12 19:58:20123 original_command_line_.reset(new CommandLine(*command_line));
[email protected]d4515eb2009-01-30 00:40:43124
[email protected]9665fa62009-04-13 22:15:29125 SetUpCommandLine(command_line);
126
[email protected]108c2a12009-06-05 22:18:09127#if defined(OS_WIN)
[email protected]d4515eb2009-01-30 00:40:43128 // Hide windows on show.
[email protected]8bcdec92009-02-25 16:15:18129 if (!command_line->HasSwitch(kUnitTestShowWindows) && !show_window_)
[email protected]d4515eb2009-01-30 00:40:43130 BrowserView::SetShowState(SW_HIDE);
[email protected]108c2a12009-06-05 22:18:09131#endif
[email protected]d4515eb2009-01-30 00:40:43132
[email protected]8bcdec92009-02-25 16:15:18133 if (dom_automation_enabled_)
134 command_line->AppendSwitch(switches::kDomAutomationController);
135
[email protected]cdbc1842009-08-26 02:56:58136 // Turn off tip loading for tests; see http://crbug.com/17725
137 command_line->AppendSwitch(switches::kDisableWebResources);
[email protected]0a519262009-07-13 18:14:08138
[email protected]4f08c83f2010-07-29 23:02:34139 command_line->AppendSwitchPath(switches::kUserDataDir, user_data_dir);
[email protected]d4515eb2009-01-30 00:40:43140
[email protected]e01aba812009-07-09 18:31:24141 // Don't show the first run ui.
142 command_line->AppendSwitch(switches::kNoFirstRun);
143
[email protected]ddf8a4b02010-03-22 23:08:30144 // This is a Browser test.
[email protected]05076ba22010-07-30 05:59:57145 command_line->AppendSwitchASCII(switches::kTestType, kBrowserTestType);
[email protected]ddf8a4b02010-03-22 23:08:30146
[email protected]8bcdec92009-02-25 16:15:18147 // Single-process mode is not set in BrowserMain so it needs to be processed
[email protected]298883bc2010-04-30 14:50:58148 // explicitly.
[email protected]56cdae32009-03-12 19:58:20149 original_single_process_ = RenderProcessHost::run_renderer_in_process();
[email protected]8bcdec92009-02-25 16:15:18150 if (command_line->HasSwitch(switches::kSingleProcess))
151 RenderProcessHost::set_run_renderer_in_process(true);
152
[email protected]87bf9742010-06-09 20:31:41153#if defined(OS_WIN)
154 // The Windows sandbox requires that the browser and child processes are the
155 // same binary. So we launch browser_process.exe which loads chrome.dll
[email protected]4f08c83f2010-07-29 23:02:34156 command_line->AppendSwitchPath(switches::kBrowserSubprocessPath,
157 command_line->GetProgram());
[email protected]87bf9742010-06-09 20:31:41158#else
159 // Explicitly set the path of the binary used for child processes, otherwise
160 // they'll try to use browser_tests which doesn't contain ChromeMain.
[email protected]6cea14d2009-09-10 18:19:18161 FilePath subprocess_path;
[email protected]7f74a4e2009-04-30 17:00:24162 PathService::Get(base::FILE_EXE, &subprocess_path);
[email protected]6cea14d2009-09-10 18:19:18163 subprocess_path = subprocess_path.DirName();
164 subprocess_path = subprocess_path.AppendASCII(WideToASCII(
165 chrome::kBrowserProcessExecutablePath));
[email protected]4f08c83f2010-07-29 23:02:34166 command_line->AppendSwitchPath(switches::kBrowserSubprocessPath,
167 subprocess_path);
[email protected]87bf9742010-06-09 20:31:41168#endif
[email protected]d4515eb2009-01-30 00:40:43169
[email protected]9c73efa2009-07-08 00:18:36170 // Enable warning level logging so that we can see when bad stuff happens.
171 command_line->AppendSwitch(switches::kEnableLogging);
[email protected]0c605fe32010-07-30 06:00:38172 command_line->AppendSwitchASCII(switches::kLoggingLevel, "1"); // warning
[email protected]9c73efa2009-07-08 00:18:36173
[email protected]0b4d3382010-07-14 16:13:04174 // If ncecessary, disable TabCloseableStateWatcher.
175 if (!tab_closeable_state_watcher_enabled_)
176 command_line->AppendSwitch(switches::kDisableTabCloseableStateWatcher);
177
[email protected]d4515eb2009-01-30 00:40:43178 SandboxInitWrapper sandbox_wrapper;
[email protected]7c321082009-02-09 15:35:47179 MainFunctionParams params(*command_line, sandbox_wrapper, NULL);
[email protected]d4515eb2009-01-30 00:40:43180 params.ui_task =
181 NewRunnableMethod(this, &InProcessBrowserTest::RunTestOnMainThreadLoop);
[email protected]13324ed2009-04-03 05:14:19182
[email protected]c4ff4952010-01-08 19:12:47183 host_resolver_ = new net::RuleBasedHostResolverProc(
184 new IntranetRedirectHostResolverProc(NULL));
[email protected]c7ad50f2009-09-11 06:28:15185
186 // Something inside the browser does this lookup implicitly. Make it fail
187 // to avoid external dependency. It won't break the tests.
188 host_resolver_->AddSimulatedFailure("*.google.com");
189
190 // See http://en.wikipedia.org/wiki/Web_Proxy_Autodiscovery_Protocol
191 // We don't want the test code to use it.
192 host_resolver_->AddSimulatedFailure("wpad");
193
[email protected]b59ff372009-07-15 22:04:32194 net::ScopedDefaultHostResolverProc scoped_host_resolver_proc(
[email protected]c7ad50f2009-09-11 06:28:15195 host_resolver_.get());
[email protected]5c5de8c2009-09-23 17:11:26196
197 SetUpInProcessBrowserTestFixture();
[email protected]60a782e2010-02-24 22:41:35198
199 // Before we run the browser, we have to hack the path to the exe to match
200 // what it would be if Chrome was running, because it is used to fork renderer
201 // processes, on Linux at least (failure to do so will cause a browser_test to
202 // be run instead of a renderer).
203 FilePath chrome_path;
204 CHECK(PathService::Get(base::FILE_EXE, &chrome_path));
205 chrome_path = chrome_path.DirName();
206#if defined(OS_WIN)
207 chrome_path = chrome_path.Append(chrome::kBrowserProcessExecutablePath);
208#elif defined(OS_POSIX)
209 chrome_path = chrome_path.Append(
210 WideToASCII(chrome::kBrowserProcessExecutablePath));
211#endif
212 CHECK(PathService::Override(base::FILE_EXE, chrome_path));
213
214#if defined(OS_LINUX)
215 // Initialize the RenderSandbox and Zygote hosts. Apparently they get used
216 // for InProcessBrowserTest, and this is not the normal browser startup path.
217 Singleton<LinuxHostInit>::get();
218#endif
219
[email protected]d4515eb2009-01-30 00:40:43220 BrowserMain(params);
[email protected]5c5de8c2009-09-23 17:11:26221 TearDownInProcessBrowserTestFixture();
[email protected]d4515eb2009-01-30 00:40:43222}
223
224void InProcessBrowserTest::TearDown() {
225 // Reinstall testing browser process.
226 delete g_browser_process;
227 g_browser_process = new TestingBrowserProcess();
228
229 browser_shutdown::delete_resources_on_shutdown = true;
230
[email protected]eb1bd832010-05-18 20:39:58231#if defined(OS_WIN)
[email protected]d4515eb2009-01-30 00:40:43232 BrowserView::SetShowState(-1);
[email protected]108c2a12009-06-05 22:18:09233#endif
[email protected]56cdae32009-03-12 19:58:20234
235 *CommandLine::ForCurrentProcessMutable() = *original_command_line_;
236 RenderProcessHost::set_run_renderer_in_process(original_single_process_);
[email protected]d4515eb2009-01-30 00:40:43237}
238
[email protected]3985ba82010-07-29 21:44:12239net::HTTPTestServer* InProcessBrowserTest::StartHTTPServer() {
[email protected]d4515eb2009-01-30 00:40:43240 DCHECK(!http_server_.get());
[email protected]3985ba82010-07-29 21:44:12241 http_server_ = net::HTTPTestServer::CreateServer(
[email protected]e70c6a82010-07-23 20:38:56242 L"chrome/test/data");
[email protected]d4515eb2009-01-30 00:40:43243 return http_server_.get();
244}
245
246// Creates a browser with a single tab (about:blank), waits for the tab to
247// finish loading and shows the browser.
248Browser* InProcessBrowserTest::CreateBrowser(Profile* profile) {
249 Browser* browser = Browser::Create(profile);
250
[email protected]e0d481582009-09-15 21:06:25251 browser->AddTabWithURL(GURL(chrome::kAboutBlankURL), GURL(),
[email protected]715af7e2010-04-29 01:55:38252 PageTransition::START_PAGE, -1,
[email protected]4a1665442010-06-28 16:09:39253 TabStripModel::ADD_SELECTED, NULL, std::string());
[email protected]f0a51fb52009-03-05 12:46:38254
[email protected]d4515eb2009-01-30 00:40:43255 // Wait for the page to finish loading.
256 ui_test_utils::WaitForNavigation(
[email protected]ce3fa3c2009-04-20 19:55:57257 &browser->GetSelectedTabContents()->controller());
[email protected]d4515eb2009-01-30 00:40:43258
259 browser->window()->Show();
260
261 return browser;
262}
263
[email protected]64f19312010-04-13 22:30:01264void InProcessBrowserTest::RunTestOnMainThreadLoop() {
[email protected]304a3172010-05-04 05:38:44265 // On Mac, without the following autorelease pool, code which is directly
266 // executed (as opposed to executed inside a message loop) would autorelease
267 // objects into a higher-level pool. This pool is not recycled in-sync with
268 // the message loops' pools and causes problems with code relying on
269 // deallocation via an autorelease pool (such as browser window closure and
270 // browser shutdown). To avoid this, the following pool is recycled after each
271 // time code is directly executed.
272 base::ScopedNSAutoreleasePool pool;
273
[email protected]64f19312010-04-13 22:30:01274 // Pump startup related events.
275 MessageLoopForUI::current()->RunAllPending();
276
277 // In the long term it would be great if we could use a TestingProfile
278 // here and only enable services you want tested, but that requires all
279 // consumers of Profile to handle NULL services.
280 Profile* profile = ProfileManager::GetDefaultProfile();
281 if (!profile) {
282 // We should only be able to get here if the profile already exists and
283 // has been created.
284 NOTREACHED();
285 return;
286 }
[email protected]304a3172010-05-04 05:38:44287 pool.Recycle();
[email protected]64f19312010-04-13 22:30:01288
289 ChromeThread::PostTask(
290 ChromeThread::IO, FROM_HERE,
291 NewRunnableFunction(chrome_browser_net::SetUrlRequestMocksEnabled, true));
292
293 browser_ = CreateBrowser(profile);
[email protected]304a3172010-05-04 05:38:44294 pool.Recycle();
[email protected]64f19312010-04-13 22:30:01295
296 // Start the timeout timer to prevent hangs.
297 MessageLoopForUI::current()->PostDelayedTask(FROM_HERE,
298 NewRunnableMethod(this, &InProcessBrowserTest::TimedOut),
299 initial_timeout_);
300
301 // Pump any pending events that were created as a result of creating a
302 // browser.
303 MessageLoopForUI::current()->RunAllPending();
304
305 RunTestOnMainThread();
[email protected]304a3172010-05-04 05:38:44306 pool.Recycle();
[email protected]64f19312010-04-13 22:30:01307
308 CleanUpOnMainThread();
[email protected]304a3172010-05-04 05:38:44309 pool.Recycle();
[email protected]64f19312010-04-13 22:30:01310
311 QuitBrowsers();
[email protected]304a3172010-05-04 05:38:44312 pool.Recycle();
[email protected]64f19312010-04-13 22:30:01313
314 // Stop the HTTP server.
315 http_server_ = NULL;
316}
317
318void InProcessBrowserTest::QuitBrowsers() {
319 if (BrowserList::size() == 0)
320 return;
321
322 // Invoke CloseAllBrowsersAndExit on a running message loop.
323 // CloseAllBrowsersAndExit exits the message loop after everything has been
324 // shut down properly.
325 MessageLoopForUI::current()->PostTask(
326 FROM_HERE,
327 NewRunnableFunction(&BrowserList::CloseAllBrowsersAndExit));
328 ui_test_utils::RunMessageLoop();
329}
[email protected]b5f95102009-07-01 19:53:59330
[email protected]5c00ca82010-04-08 21:30:42331void InProcessBrowserTest::TimedOut() {
[email protected]94ab2ad2009-10-15 18:18:51332 std::string error_message = "Test timed out. Each test runs for a max of ";
[email protected]b9777872010-07-30 18:26:16333 error_message += IntToString(initial_timeout_);
[email protected]94ab2ad2009-10-15 18:18:51334 error_message += " ms (kInitialTimeoutInMS).";
335
336 GTEST_NONFATAL_FAILURE_(error_message.c_str());
[email protected]b5f95102009-07-01 19:53:59337
[email protected]b5f95102009-07-01 19:53:59338 MessageLoopForUI::current()->Quit();
339}
[email protected]4ff446f2009-07-10 18:29:39340
341void InProcessBrowserTest::SetInitialTimeoutInMS(int timeout_value) {
342 DCHECK_GT(timeout_value, 0);
343 initial_timeout_ = timeout_value;
344}