blob: 0cbb39db90a5e1cf79214aa8ae90c1d7e69c6086 [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]23cc9a12009-07-30 21:27:01139 command_line->AppendSwitchWithValue(switches::kUserDataDir,
140 user_data_dir.ToWStringHack());
[email protected]d4515eb2009-01-30 00:40:43141
[email protected]e01aba812009-07-09 18:31:24142 // Don't show the first run ui.
143 command_line->AppendSwitch(switches::kNoFirstRun);
144
[email protected]ddf8a4b02010-03-22 23:08:30145 // This is a Browser test.
146 command_line->AppendSwitchWithValue(switches::kTestType,
147 ASCIIToWide(kBrowserTestType));
148
[email protected]8bcdec92009-02-25 16:15:18149 // Single-process mode is not set in BrowserMain so it needs to be processed
[email protected]298883bc2010-04-30 14:50:58150 // explicitly.
[email protected]56cdae32009-03-12 19:58:20151 original_single_process_ = RenderProcessHost::run_renderer_in_process();
[email protected]8bcdec92009-02-25 16:15:18152 if (command_line->HasSwitch(switches::kSingleProcess))
153 RenderProcessHost::set_run_renderer_in_process(true);
154
[email protected]87bf9742010-06-09 20:31:41155#if defined(OS_WIN)
156 // The Windows sandbox requires that the browser and child processes are the
157 // same binary. So we launch browser_process.exe which loads chrome.dll
158 command_line->AppendSwitchWithValue(switches::kBrowserSubprocessPath,
159 command_line->GetProgram().value());
160#else
161 // Explicitly set the path of the binary used for child processes, otherwise
162 // they'll try to use browser_tests which doesn't contain ChromeMain.
[email protected]6cea14d2009-09-10 18:19:18163 FilePath subprocess_path;
[email protected]7f74a4e2009-04-30 17:00:24164 PathService::Get(base::FILE_EXE, &subprocess_path);
[email protected]6cea14d2009-09-10 18:19:18165 subprocess_path = subprocess_path.DirName();
166 subprocess_path = subprocess_path.AppendASCII(WideToASCII(
167 chrome::kBrowserProcessExecutablePath));
[email protected]7f74a4e2009-04-30 17:00:24168 command_line->AppendSwitchWithValue(switches::kBrowserSubprocessPath,
[email protected]6cea14d2009-09-10 18:19:18169 subprocess_path.ToWStringHack());
[email protected]87bf9742010-06-09 20:31:41170#endif
[email protected]d4515eb2009-01-30 00:40:43171
[email protected]9c73efa2009-07-08 00:18:36172 // Enable warning level logging so that we can see when bad stuff happens.
173 command_line->AppendSwitch(switches::kEnableLogging);
174 command_line->AppendSwitchWithValue(switches::kLoggingLevel,
175 IntToWString(1)); // warning
176
[email protected]0b4d3382010-07-14 16:13:04177 // If ncecessary, disable TabCloseableStateWatcher.
178 if (!tab_closeable_state_watcher_enabled_)
179 command_line->AppendSwitch(switches::kDisableTabCloseableStateWatcher);
180
[email protected]d4515eb2009-01-30 00:40:43181 SandboxInitWrapper sandbox_wrapper;
[email protected]7c321082009-02-09 15:35:47182 MainFunctionParams params(*command_line, sandbox_wrapper, NULL);
[email protected]d4515eb2009-01-30 00:40:43183 params.ui_task =
184 NewRunnableMethod(this, &InProcessBrowserTest::RunTestOnMainThreadLoop);
[email protected]13324ed2009-04-03 05:14:19185
[email protected]c4ff4952010-01-08 19:12:47186 host_resolver_ = new net::RuleBasedHostResolverProc(
187 new IntranetRedirectHostResolverProc(NULL));
[email protected]c7ad50f2009-09-11 06:28:15188
189 // Something inside the browser does this lookup implicitly. Make it fail
190 // to avoid external dependency. It won't break the tests.
191 host_resolver_->AddSimulatedFailure("*.google.com");
192
193 // See http://en.wikipedia.org/wiki/Web_Proxy_Autodiscovery_Protocol
194 // We don't want the test code to use it.
195 host_resolver_->AddSimulatedFailure("wpad");
196
[email protected]b59ff372009-07-15 22:04:32197 net::ScopedDefaultHostResolverProc scoped_host_resolver_proc(
[email protected]c7ad50f2009-09-11 06:28:15198 host_resolver_.get());
[email protected]5c5de8c2009-09-23 17:11:26199
200 SetUpInProcessBrowserTestFixture();
[email protected]60a782e2010-02-24 22:41:35201
202 // Before we run the browser, we have to hack the path to the exe to match
203 // what it would be if Chrome was running, because it is used to fork renderer
204 // processes, on Linux at least (failure to do so will cause a browser_test to
205 // be run instead of a renderer).
206 FilePath chrome_path;
207 CHECK(PathService::Get(base::FILE_EXE, &chrome_path));
208 chrome_path = chrome_path.DirName();
209#if defined(OS_WIN)
210 chrome_path = chrome_path.Append(chrome::kBrowserProcessExecutablePath);
211#elif defined(OS_POSIX)
212 chrome_path = chrome_path.Append(
213 WideToASCII(chrome::kBrowserProcessExecutablePath));
214#endif
215 CHECK(PathService::Override(base::FILE_EXE, chrome_path));
216
217#if defined(OS_LINUX)
218 // Initialize the RenderSandbox and Zygote hosts. Apparently they get used
219 // for InProcessBrowserTest, and this is not the normal browser startup path.
220 Singleton<LinuxHostInit>::get();
221#endif
222
[email protected]d4515eb2009-01-30 00:40:43223 BrowserMain(params);
[email protected]5c5de8c2009-09-23 17:11:26224 TearDownInProcessBrowserTestFixture();
[email protected]d4515eb2009-01-30 00:40:43225}
226
227void InProcessBrowserTest::TearDown() {
228 // Reinstall testing browser process.
229 delete g_browser_process;
230 g_browser_process = new TestingBrowserProcess();
231
232 browser_shutdown::delete_resources_on_shutdown = true;
233
[email protected]eb1bd832010-05-18 20:39:58234#if defined(OS_WIN)
[email protected]d4515eb2009-01-30 00:40:43235 BrowserView::SetShowState(-1);
[email protected]108c2a12009-06-05 22:18:09236#endif
[email protected]56cdae32009-03-12 19:58:20237
238 *CommandLine::ForCurrentProcessMutable() = *original_command_line_;
239 RenderProcessHost::set_run_renderer_in_process(original_single_process_);
[email protected]d4515eb2009-01-30 00:40:43240}
241
[email protected]3985ba82010-07-29 21:44:12242net::HTTPTestServer* InProcessBrowserTest::StartHTTPServer() {
[email protected]d4515eb2009-01-30 00:40:43243 DCHECK(!http_server_.get());
[email protected]3985ba82010-07-29 21:44:12244 http_server_ = net::HTTPTestServer::CreateServer(
[email protected]e70c6a82010-07-23 20:38:56245 L"chrome/test/data");
[email protected]d4515eb2009-01-30 00:40:43246 return http_server_.get();
247}
248
249// Creates a browser with a single tab (about:blank), waits for the tab to
250// finish loading and shows the browser.
251Browser* InProcessBrowserTest::CreateBrowser(Profile* profile) {
252 Browser* browser = Browser::Create(profile);
253
[email protected]e0d481582009-09-15 21:06:25254 browser->AddTabWithURL(GURL(chrome::kAboutBlankURL), GURL(),
[email protected]715af7e2010-04-29 01:55:38255 PageTransition::START_PAGE, -1,
[email protected]4a1665442010-06-28 16:09:39256 TabStripModel::ADD_SELECTED, NULL, std::string());
[email protected]f0a51fb52009-03-05 12:46:38257
[email protected]d4515eb2009-01-30 00:40:43258 // Wait for the page to finish loading.
259 ui_test_utils::WaitForNavigation(
[email protected]ce3fa3c2009-04-20 19:55:57260 &browser->GetSelectedTabContents()->controller());
[email protected]d4515eb2009-01-30 00:40:43261
262 browser->window()->Show();
263
264 return browser;
265}
266
[email protected]64f19312010-04-13 22:30:01267void InProcessBrowserTest::RunTestOnMainThreadLoop() {
[email protected]304a3172010-05-04 05:38:44268 // On Mac, without the following autorelease pool, code which is directly
269 // executed (as opposed to executed inside a message loop) would autorelease
270 // objects into a higher-level pool. This pool is not recycled in-sync with
271 // the message loops' pools and causes problems with code relying on
272 // deallocation via an autorelease pool (such as browser window closure and
273 // browser shutdown). To avoid this, the following pool is recycled after each
274 // time code is directly executed.
275 base::ScopedNSAutoreleasePool pool;
276
[email protected]64f19312010-04-13 22:30:01277 // Pump startup related events.
278 MessageLoopForUI::current()->RunAllPending();
279
280 // In the long term it would be great if we could use a TestingProfile
281 // here and only enable services you want tested, but that requires all
282 // consumers of Profile to handle NULL services.
283 Profile* profile = ProfileManager::GetDefaultProfile();
284 if (!profile) {
285 // We should only be able to get here if the profile already exists and
286 // has been created.
287 NOTREACHED();
288 return;
289 }
[email protected]304a3172010-05-04 05:38:44290 pool.Recycle();
[email protected]64f19312010-04-13 22:30:01291
292 ChromeThread::PostTask(
293 ChromeThread::IO, FROM_HERE,
294 NewRunnableFunction(chrome_browser_net::SetUrlRequestMocksEnabled, true));
295
296 browser_ = CreateBrowser(profile);
[email protected]304a3172010-05-04 05:38:44297 pool.Recycle();
[email protected]64f19312010-04-13 22:30:01298
299 // Start the timeout timer to prevent hangs.
300 MessageLoopForUI::current()->PostDelayedTask(FROM_HERE,
301 NewRunnableMethod(this, &InProcessBrowserTest::TimedOut),
302 initial_timeout_);
303
304 // Pump any pending events that were created as a result of creating a
305 // browser.
306 MessageLoopForUI::current()->RunAllPending();
307
308 RunTestOnMainThread();
[email protected]304a3172010-05-04 05:38:44309 pool.Recycle();
[email protected]64f19312010-04-13 22:30:01310
311 CleanUpOnMainThread();
[email protected]304a3172010-05-04 05:38:44312 pool.Recycle();
[email protected]64f19312010-04-13 22:30:01313
314 QuitBrowsers();
[email protected]304a3172010-05-04 05:38:44315 pool.Recycle();
[email protected]64f19312010-04-13 22:30:01316
317 // Stop the HTTP server.
318 http_server_ = NULL;
319}
320
321void InProcessBrowserTest::QuitBrowsers() {
322 if (BrowserList::size() == 0)
323 return;
324
325 // Invoke CloseAllBrowsersAndExit on a running message loop.
326 // CloseAllBrowsersAndExit exits the message loop after everything has been
327 // shut down properly.
328 MessageLoopForUI::current()->PostTask(
329 FROM_HERE,
330 NewRunnableFunction(&BrowserList::CloseAllBrowsersAndExit));
331 ui_test_utils::RunMessageLoop();
332}
[email protected]b5f95102009-07-01 19:53:59333
[email protected]5c00ca82010-04-08 21:30:42334void InProcessBrowserTest::TimedOut() {
[email protected]94ab2ad2009-10-15 18:18:51335 std::string error_message = "Test timed out. Each test runs for a max of ";
336 error_message += IntToString(kInitialTimeoutInMS);
337 error_message += " ms (kInitialTimeoutInMS).";
338
339 GTEST_NONFATAL_FAILURE_(error_message.c_str());
[email protected]b5f95102009-07-01 19:53:59340
[email protected]b5f95102009-07-01 19:53:59341 MessageLoopForUI::current()->Quit();
342}
[email protected]4ff446f2009-07-10 18:29:39343
344void InProcessBrowserTest::SetInitialTimeoutInMS(int timeout_value) {
345 DCHECK_GT(timeout_value, 0);
346 initial_timeout_ = timeout_value;
347}