blob: 5d7e2be371585757dae9f9376a42e306543f086b [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]528c56d2010-07-30 19:28:4412#include "base/string_number_conversions.h"
[email protected]fb895c62009-10-09 18:20:3013#include "base/test/test_file_util.h"
[email protected]d4515eb2009-01-30 00:40:4314#include "chrome/browser/browser.h"
[email protected]b6e38ef2009-06-16 00:43:2315#include "chrome/browser/browser_list.h"
[email protected]d4515eb2009-01-30 00:40:4316#include "chrome/browser/browser_process.h"
17#include "chrome/browser/browser_shutdown.h"
[email protected]108c2a12009-06-05 22:18:0918#include "chrome/browser/browser_window.h"
[email protected]6fad2632009-11-02 05:59:3719#include "chrome/browser/chrome_thread.h"
[email protected]c4ff4952010-01-08 19:12:4720#include "chrome/browser/intranet_redirect_detector.h"
[email protected]0ac83682010-01-22 17:46:2721#include "chrome/browser/io_thread.h"
[email protected]cec4a272009-07-31 21:55:0322#include "chrome/browser/net/url_request_mock_util.h"
[email protected]d4515eb2009-01-30 00:40:4323#include "chrome/browser/profile.h"
24#include "chrome/browser/profile_manager.h"
[email protected]8bcdec92009-02-25 16:15:1825#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]5c238752009-06-13 10:29:0726#include "chrome/browser/tab_contents/tab_contents.h"
[email protected]108c2a12009-06-05 22:18:0927#if defined(OS_WIN)
[email protected]d4515eb2009-01-30 00:40:4328#include "chrome/browser/views/frame/browser_view.h"
[email protected]108c2a12009-06-05 22:18:0929#endif
[email protected]d4515eb2009-01-30 00:40:4330#include "chrome/common/chrome_constants.h"
31#include "chrome/common/chrome_paths.h"
32#include "chrome/common/chrome_switches.h"
33#include "chrome/common/main_function_params.h"
[email protected]ad1f9bd2009-07-30 20:23:1534#include "chrome/common/notification_registrar.h"
35#include "chrome/common/notification_type.h"
[email protected]e0d481582009-09-15 21:06:2536#include "chrome/common/url_constants.h"
[email protected]d4515eb2009-01-30 00:40:4337#include "chrome/test/testing_browser_process.h"
38#include "chrome/test/ui_test_utils.h"
[email protected]c4ff4952010-01-08 19:12:4739#include "net/base/mock_host_resolver.h"
[email protected]3985ba82010-07-29 21:44:1240#include "net/test/test_server.h"
[email protected]d4515eb2009-01-30 00:40:4341#include "sandbox/src/dep.h"
42
[email protected]8ecd3aad2009-11-04 08:32:2243#if defined(OS_LINUX)
44#include "base/singleton.h"
45#include "chrome/browser/renderer_host/render_sandbox_host_linux.h"
46#include "chrome/browser/zygote_host_linux.h"
47
48namespace {
49
50// A helper class to do Linux-only initialization only once per process.
51class LinuxHostInit {
52 public:
53 LinuxHostInit() {
[email protected]576a4ca2009-11-05 01:41:0954 RenderSandboxHostLinux* shost = Singleton<RenderSandboxHostLinux>::get();
[email protected]8ecd3aad2009-11-04 08:32:2255 shost->Init("");
[email protected]576a4ca2009-11-05 01:41:0956 ZygoteHost* zhost = Singleton<ZygoteHost>::get();
[email protected]8ecd3aad2009-11-04 08:32:2257 zhost->Init("");
58 }
59 ~LinuxHostInit() {}
60};
61
62} // namespace
63#endif
64
[email protected]d4515eb2009-01-30 00:40:4365extern int BrowserMain(const MainFunctionParams&);
66
67const wchar_t kUnitTestShowWindows[] = L"show-windows";
68
[email protected]ddf8a4b02010-03-22 23:08:3069// Passed as value of kTestType.
70static const char kBrowserTestType[] = "browser";
71
[email protected]4ff446f2009-07-10 18:29:3972// Default delay for the time-out at which we stop the
73// inner-message loop the first time.
[email protected]b5f95102009-07-01 19:53:5974const int kInitialTimeoutInMS = 30000;
75
76// Delay for sub-sequent time-outs once the initial time-out happened.
77const int kSubsequentTimeoutInMS = 5000;
78
[email protected]8bcdec92009-02-25 16:15:1879InProcessBrowserTest::InProcessBrowserTest()
80 : browser_(NULL),
81 show_window_(false),
[email protected]56cdae32009-03-12 19:58:2082 dom_automation_enabled_(false),
[email protected]0b4d3382010-07-14 16:13:0483 tab_closeable_state_watcher_enabled_(false),
[email protected]4ff446f2009-07-10 18:29:3984 original_single_process_(false),
85 initial_timeout_(kInitialTimeoutInMS) {
[email protected]d4515eb2009-01-30 00:40:4386}
87
[email protected]c4ff4952010-01-08 19:12:4788InProcessBrowserTest::~InProcessBrowserTest() {
89}
90
[email protected]d4515eb2009-01-30 00:40:4391void InProcessBrowserTest::SetUp() {
92 // Cleanup the user data dir.
[email protected]23cc9a12009-07-30 21:27:0193 FilePath user_data_dir;
[email protected]d4515eb2009-01-30 00:40:4394 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
[email protected]23cc9a12009-07-30 21:27:0195 ASSERT_LT(10, static_cast<int>(user_data_dir.value().size())) <<
[email protected]d4515eb2009-01-30 00:40:4396 "The user data directory name passed into this test was too "
97 "short to delete safely. Please check the user-data-dir "
98 "argument and try again.";
[email protected]298883bc2010-04-30 14:50:5899 ASSERT_TRUE(file_util::DieFileDie(user_data_dir, true));
[email protected]d4515eb2009-01-30 00:40:43100
[email protected]4a44bc32010-05-28 22:22:44101 // Recreate the user data dir. (PathService::Get guarantees that the directory
102 // exists if it returns true, but it only actually checks on the first call,
103 // the rest are cached. Thus we need to recreate it ourselves to not break
104 // the PathService guarantee.)
105 ASSERT_TRUE(file_util::CreateDirectory(user_data_dir));
106
[email protected]d4515eb2009-01-30 00:40:43107 // The unit test suite creates a testingbrowser, but we want the real thing.
108 // Delete the current one. We'll install the testing one in TearDown.
109 delete g_browser_process;
[email protected]298883bc2010-04-30 14:50:58110 g_browser_process = NULL;
111
112 SetUpUserDataDirectory();
[email protected]d4515eb2009-01-30 00:40:43113
114 // Don't delete the resources when BrowserMain returns. Many ui classes
115 // cache SkBitmaps in a static field so that if we delete the resource
116 // bundle we'll crash.
117 browser_shutdown::delete_resources_on_shutdown = false;
118
[email protected]986088a62010-05-13 18:59:20119 // Remember the command line. Normally this doesn't matter, because the test
120 // harness creates a new process for each test, but when the test harness is
121 // running in single process mode, we can't let one test's command-line
122 // changes (e.g. enabling DOM automation) affect other tests.
[email protected]d4515eb2009-01-30 00:40:43123 CommandLine* command_line = CommandLine::ForCurrentProcessMutable();
[email protected]56cdae32009-03-12 19:58:20124 original_command_line_.reset(new CommandLine(*command_line));
[email protected]d4515eb2009-01-30 00:40:43125
[email protected]9665fa62009-04-13 22:15:29126 SetUpCommandLine(command_line);
127
[email protected]108c2a12009-06-05 22:18:09128#if defined(OS_WIN)
[email protected]d4515eb2009-01-30 00:40:43129 // Hide windows on show.
[email protected]8bcdec92009-02-25 16:15:18130 if (!command_line->HasSwitch(kUnitTestShowWindows) && !show_window_)
[email protected]d4515eb2009-01-30 00:40:43131 BrowserView::SetShowState(SW_HIDE);
[email protected]108c2a12009-06-05 22:18:09132#endif
[email protected]d4515eb2009-01-30 00:40:43133
[email protected]8bcdec92009-02-25 16:15:18134 if (dom_automation_enabled_)
135 command_line->AppendSwitch(switches::kDomAutomationController);
136
[email protected]cdbc1842009-08-26 02:56:58137 // Turn off tip loading for tests; see http://crbug.com/17725
138 command_line->AppendSwitch(switches::kDisableWebResources);
[email protected]0a519262009-07-13 18:14:08139
[email protected]4f08c83f2010-07-29 23:02:34140 command_line->AppendSwitchPath(switches::kUserDataDir, user_data_dir);
[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.
[email protected]05076ba22010-07-30 05:59:57146 command_line->AppendSwitchASCII(switches::kTestType, kBrowserTestType);
[email protected]ddf8a4b02010-03-22 23:08:30147
[email protected]8bcdec92009-02-25 16:15:18148 // Single-process mode is not set in BrowserMain so it needs to be processed
[email protected]298883bc2010-04-30 14:50:58149 // explicitly.
[email protected]56cdae32009-03-12 19:58:20150 original_single_process_ = RenderProcessHost::run_renderer_in_process();
[email protected]8bcdec92009-02-25 16:15:18151 if (command_line->HasSwitch(switches::kSingleProcess))
152 RenderProcessHost::set_run_renderer_in_process(true);
153
[email protected]87bf9742010-06-09 20:31:41154#if defined(OS_WIN)
155 // The Windows sandbox requires that the browser and child processes are the
156 // same binary. So we launch browser_process.exe which loads chrome.dll
[email protected]4f08c83f2010-07-29 23:02:34157 command_line->AppendSwitchPath(switches::kBrowserSubprocessPath,
158 command_line->GetProgram());
[email protected]87bf9742010-06-09 20:31:41159#else
160 // Explicitly set the path of the binary used for child processes, otherwise
161 // they'll try to use browser_tests which doesn't contain ChromeMain.
[email protected]6cea14d2009-09-10 18:19:18162 FilePath subprocess_path;
[email protected]7f74a4e2009-04-30 17:00:24163 PathService::Get(base::FILE_EXE, &subprocess_path);
[email protected]6cea14d2009-09-10 18:19:18164 subprocess_path = subprocess_path.DirName();
165 subprocess_path = subprocess_path.AppendASCII(WideToASCII(
166 chrome::kBrowserProcessExecutablePath));
[email protected]4f08c83f2010-07-29 23:02:34167 command_line->AppendSwitchPath(switches::kBrowserSubprocessPath,
168 subprocess_path);
[email protected]87bf9742010-06-09 20:31:41169#endif
[email protected]d4515eb2009-01-30 00:40:43170
[email protected]9c73efa2009-07-08 00:18:36171 // Enable warning level logging so that we can see when bad stuff happens.
172 command_line->AppendSwitch(switches::kEnableLogging);
[email protected]0c605fe32010-07-30 06:00:38173 command_line->AppendSwitchASCII(switches::kLoggingLevel, "1"); // warning
[email protected]9c73efa2009-07-08 00:18:36174
[email protected]0b4d3382010-07-14 16:13:04175 // If ncecessary, disable TabCloseableStateWatcher.
176 if (!tab_closeable_state_watcher_enabled_)
177 command_line->AppendSwitch(switches::kDisableTabCloseableStateWatcher);
178
[email protected]d4515eb2009-01-30 00:40:43179 SandboxInitWrapper sandbox_wrapper;
[email protected]7c321082009-02-09 15:35:47180 MainFunctionParams params(*command_line, sandbox_wrapper, NULL);
[email protected]d4515eb2009-01-30 00:40:43181 params.ui_task =
182 NewRunnableMethod(this, &InProcessBrowserTest::RunTestOnMainThreadLoop);
[email protected]13324ed2009-04-03 05:14:19183
[email protected]c4ff4952010-01-08 19:12:47184 host_resolver_ = new net::RuleBasedHostResolverProc(
185 new IntranetRedirectHostResolverProc(NULL));
[email protected]c7ad50f2009-09-11 06:28:15186
187 // Something inside the browser does this lookup implicitly. Make it fail
188 // to avoid external dependency. It won't break the tests.
189 host_resolver_->AddSimulatedFailure("*.google.com");
190
191 // See http://en.wikipedia.org/wiki/Web_Proxy_Autodiscovery_Protocol
192 // We don't want the test code to use it.
193 host_resolver_->AddSimulatedFailure("wpad");
194
[email protected]b59ff372009-07-15 22:04:32195 net::ScopedDefaultHostResolverProc scoped_host_resolver_proc(
[email protected]c7ad50f2009-09-11 06:28:15196 host_resolver_.get());
[email protected]5c5de8c2009-09-23 17:11:26197
198 SetUpInProcessBrowserTestFixture();
[email protected]60a782e2010-02-24 22:41:35199
200 // Before we run the browser, we have to hack the path to the exe to match
201 // what it would be if Chrome was running, because it is used to fork renderer
202 // processes, on Linux at least (failure to do so will cause a browser_test to
203 // be run instead of a renderer).
204 FilePath chrome_path;
205 CHECK(PathService::Get(base::FILE_EXE, &chrome_path));
206 chrome_path = chrome_path.DirName();
207#if defined(OS_WIN)
208 chrome_path = chrome_path.Append(chrome::kBrowserProcessExecutablePath);
209#elif defined(OS_POSIX)
210 chrome_path = chrome_path.Append(
211 WideToASCII(chrome::kBrowserProcessExecutablePath));
212#endif
213 CHECK(PathService::Override(base::FILE_EXE, chrome_path));
214
215#if defined(OS_LINUX)
216 // Initialize the RenderSandbox and Zygote hosts. Apparently they get used
217 // for InProcessBrowserTest, and this is not the normal browser startup path.
218 Singleton<LinuxHostInit>::get();
219#endif
220
[email protected]d4515eb2009-01-30 00:40:43221 BrowserMain(params);
[email protected]5c5de8c2009-09-23 17:11:26222 TearDownInProcessBrowserTestFixture();
[email protected]d4515eb2009-01-30 00:40:43223}
224
225void InProcessBrowserTest::TearDown() {
226 // Reinstall testing browser process.
227 delete g_browser_process;
228 g_browser_process = new TestingBrowserProcess();
229
230 browser_shutdown::delete_resources_on_shutdown = true;
231
[email protected]eb1bd832010-05-18 20:39:58232#if defined(OS_WIN)
[email protected]d4515eb2009-01-30 00:40:43233 BrowserView::SetShowState(-1);
[email protected]108c2a12009-06-05 22:18:09234#endif
[email protected]56cdae32009-03-12 19:58:20235
236 *CommandLine::ForCurrentProcessMutable() = *original_command_line_;
237 RenderProcessHost::set_run_renderer_in_process(original_single_process_);
[email protected]d4515eb2009-01-30 00:40:43238}
239
[email protected]3985ba82010-07-29 21:44:12240net::HTTPTestServer* InProcessBrowserTest::StartHTTPServer() {
[email protected]d4515eb2009-01-30 00:40:43241 DCHECK(!http_server_.get());
[email protected]3985ba82010-07-29 21:44:12242 http_server_ = net::HTTPTestServer::CreateServer(
[email protected]e70c6a82010-07-23 20:38:56243 L"chrome/test/data");
[email protected]d4515eb2009-01-30 00:40:43244 return http_server_.get();
245}
246
247// Creates a browser with a single tab (about:blank), waits for the tab to
248// finish loading and shows the browser.
249Browser* InProcessBrowserTest::CreateBrowser(Profile* profile) {
250 Browser* browser = Browser::Create(profile);
251
[email protected]e0d481582009-09-15 21:06:25252 browser->AddTabWithURL(GURL(chrome::kAboutBlankURL), GURL(),
[email protected]715af7e2010-04-29 01:55:38253 PageTransition::START_PAGE, -1,
[email protected]4a1665442010-06-28 16:09:39254 TabStripModel::ADD_SELECTED, NULL, std::string());
[email protected]f0a51fb52009-03-05 12:46:38255
[email protected]d4515eb2009-01-30 00:40:43256 // Wait for the page to finish loading.
257 ui_test_utils::WaitForNavigation(
[email protected]ce3fa3c2009-04-20 19:55:57258 &browser->GetSelectedTabContents()->controller());
[email protected]d4515eb2009-01-30 00:40:43259
260 browser->window()->Show();
261
262 return browser;
263}
264
[email protected]64f19312010-04-13 22:30:01265void InProcessBrowserTest::RunTestOnMainThreadLoop() {
[email protected]304a3172010-05-04 05:38:44266 // On Mac, without the following autorelease pool, code which is directly
267 // executed (as opposed to executed inside a message loop) would autorelease
268 // objects into a higher-level pool. This pool is not recycled in-sync with
269 // the message loops' pools and causes problems with code relying on
270 // deallocation via an autorelease pool (such as browser window closure and
271 // browser shutdown). To avoid this, the following pool is recycled after each
272 // time code is directly executed.
273 base::ScopedNSAutoreleasePool pool;
274
[email protected]64f19312010-04-13 22:30:01275 // Pump startup related events.
276 MessageLoopForUI::current()->RunAllPending();
277
278 // In the long term it would be great if we could use a TestingProfile
279 // here and only enable services you want tested, but that requires all
280 // consumers of Profile to handle NULL services.
281 Profile* profile = ProfileManager::GetDefaultProfile();
282 if (!profile) {
283 // We should only be able to get here if the profile already exists and
284 // has been created.
285 NOTREACHED();
286 return;
287 }
[email protected]304a3172010-05-04 05:38:44288 pool.Recycle();
[email protected]64f19312010-04-13 22:30:01289
290 ChromeThread::PostTask(
291 ChromeThread::IO, FROM_HERE,
292 NewRunnableFunction(chrome_browser_net::SetUrlRequestMocksEnabled, true));
293
294 browser_ = CreateBrowser(profile);
[email protected]304a3172010-05-04 05:38:44295 pool.Recycle();
[email protected]64f19312010-04-13 22:30:01296
297 // Start the timeout timer to prevent hangs.
298 MessageLoopForUI::current()->PostDelayedTask(FROM_HERE,
299 NewRunnableMethod(this, &InProcessBrowserTest::TimedOut),
300 initial_timeout_);
301
302 // Pump any pending events that were created as a result of creating a
303 // browser.
304 MessageLoopForUI::current()->RunAllPending();
305
306 RunTestOnMainThread();
[email protected]304a3172010-05-04 05:38:44307 pool.Recycle();
[email protected]64f19312010-04-13 22:30:01308
309 CleanUpOnMainThread();
[email protected]304a3172010-05-04 05:38:44310 pool.Recycle();
[email protected]64f19312010-04-13 22:30:01311
312 QuitBrowsers();
[email protected]304a3172010-05-04 05:38:44313 pool.Recycle();
[email protected]64f19312010-04-13 22:30:01314
315 // Stop the HTTP server.
316 http_server_ = NULL;
317}
318
319void InProcessBrowserTest::QuitBrowsers() {
320 if (BrowserList::size() == 0)
321 return;
322
323 // Invoke CloseAllBrowsersAndExit on a running message loop.
324 // CloseAllBrowsersAndExit exits the message loop after everything has been
325 // shut down properly.
326 MessageLoopForUI::current()->PostTask(
327 FROM_HERE,
328 NewRunnableFunction(&BrowserList::CloseAllBrowsersAndExit));
329 ui_test_utils::RunMessageLoop();
330}
[email protected]b5f95102009-07-01 19:53:59331
[email protected]5c00ca82010-04-08 21:30:42332void InProcessBrowserTest::TimedOut() {
[email protected]94ab2ad2009-10-15 18:18:51333 std::string error_message = "Test timed out. Each test runs for a max of ";
[email protected]528c56d2010-07-30 19:28:44334 error_message += base::IntToString(initial_timeout_);
[email protected]94ab2ad2009-10-15 18:18:51335 error_message += " ms (kInitialTimeoutInMS).";
336
337 GTEST_NONFATAL_FAILURE_(error_message.c_str());
[email protected]b5f95102009-07-01 19:53:59338
[email protected]b5f95102009-07-01 19:53:59339 MessageLoopForUI::current()->Quit();
340}
[email protected]4ff446f2009-07-10 18:29:39341
342void InProcessBrowserTest::SetInitialTimeoutInMS(int timeout_value) {
343 DCHECK_GT(timeout_value, 0);
344 initial_timeout_ = timeout_value;
345}