blob: 9fc2544a2277c362dcb815d52c6e465ebc201a2a [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),
[email protected]95409e12010-08-17 20:07:1181 test_server_(net::TestServer::TYPE_HTTP,
82 FilePath(FILE_PATH_LITERAL("chrome/test/data"))),
[email protected]8bcdec92009-02-25 16:15:1883 show_window_(false),
[email protected]56cdae32009-03-12 19:58:2084 dom_automation_enabled_(false),
[email protected]0b4d3382010-07-14 16:13:0485 tab_closeable_state_watcher_enabled_(false),
[email protected]4ff446f2009-07-10 18:29:3986 original_single_process_(false),
87 initial_timeout_(kInitialTimeoutInMS) {
[email protected]d4515eb2009-01-30 00:40:4388}
89
[email protected]c4ff4952010-01-08 19:12:4790InProcessBrowserTest::~InProcessBrowserTest() {
91}
92
[email protected]d4515eb2009-01-30 00:40:4393void InProcessBrowserTest::SetUp() {
94 // Cleanup the user data dir.
[email protected]23cc9a12009-07-30 21:27:0195 FilePath user_data_dir;
[email protected]d4515eb2009-01-30 00:40:4396 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
[email protected]23cc9a12009-07-30 21:27:0197 ASSERT_LT(10, static_cast<int>(user_data_dir.value().size())) <<
[email protected]d4515eb2009-01-30 00:40:4398 "The user data directory name passed into this test was too "
99 "short to delete safely. Please check the user-data-dir "
100 "argument and try again.";
[email protected]298883bc2010-04-30 14:50:58101 ASSERT_TRUE(file_util::DieFileDie(user_data_dir, true));
[email protected]d4515eb2009-01-30 00:40:43102
[email protected]4a44bc32010-05-28 22:22:44103 // Recreate the user data dir. (PathService::Get guarantees that the directory
104 // exists if it returns true, but it only actually checks on the first call,
105 // the rest are cached. Thus we need to recreate it ourselves to not break
106 // the PathService guarantee.)
107 ASSERT_TRUE(file_util::CreateDirectory(user_data_dir));
108
[email protected]d4515eb2009-01-30 00:40:43109 // The unit test suite creates a testingbrowser, but we want the real thing.
110 // Delete the current one. We'll install the testing one in TearDown.
111 delete g_browser_process;
[email protected]298883bc2010-04-30 14:50:58112 g_browser_process = NULL;
113
114 SetUpUserDataDirectory();
[email protected]d4515eb2009-01-30 00:40:43115
116 // Don't delete the resources when BrowserMain returns. Many ui classes
117 // cache SkBitmaps in a static field so that if we delete the resource
118 // bundle we'll crash.
119 browser_shutdown::delete_resources_on_shutdown = false;
120
[email protected]986088a62010-05-13 18:59:20121 // Remember the command line. Normally this doesn't matter, because the test
122 // harness creates a new process for each test, but when the test harness is
123 // running in single process mode, we can't let one test's command-line
124 // changes (e.g. enabling DOM automation) affect other tests.
[email protected]d4515eb2009-01-30 00:40:43125 CommandLine* command_line = CommandLine::ForCurrentProcessMutable();
[email protected]56cdae32009-03-12 19:58:20126 original_command_line_.reset(new CommandLine(*command_line));
[email protected]d4515eb2009-01-30 00:40:43127
[email protected]9665fa62009-04-13 22:15:29128 SetUpCommandLine(command_line);
129
[email protected]108c2a12009-06-05 22:18:09130#if defined(OS_WIN)
[email protected]d4515eb2009-01-30 00:40:43131 // Hide windows on show.
[email protected]8bcdec92009-02-25 16:15:18132 if (!command_line->HasSwitch(kUnitTestShowWindows) && !show_window_)
[email protected]d4515eb2009-01-30 00:40:43133 BrowserView::SetShowState(SW_HIDE);
[email protected]108c2a12009-06-05 22:18:09134#endif
[email protected]d4515eb2009-01-30 00:40:43135
[email protected]8bcdec92009-02-25 16:15:18136 if (dom_automation_enabled_)
137 command_line->AppendSwitch(switches::kDomAutomationController);
138
[email protected]cdbc1842009-08-26 02:56:58139 // Turn off tip loading for tests; see http://crbug.com/17725
140 command_line->AppendSwitch(switches::kDisableWebResources);
[email protected]0a519262009-07-13 18:14:08141
[email protected]4f08c83f2010-07-29 23:02:34142 command_line->AppendSwitchPath(switches::kUserDataDir, user_data_dir);
[email protected]d4515eb2009-01-30 00:40:43143
[email protected]e01aba812009-07-09 18:31:24144 // Don't show the first run ui.
145 command_line->AppendSwitch(switches::kNoFirstRun);
146
[email protected]ddf8a4b02010-03-22 23:08:30147 // This is a Browser test.
[email protected]05076ba22010-07-30 05:59:57148 command_line->AppendSwitchASCII(switches::kTestType, kBrowserTestType);
[email protected]ddf8a4b02010-03-22 23:08:30149
[email protected]8bcdec92009-02-25 16:15:18150 // Single-process mode is not set in BrowserMain so it needs to be processed
[email protected]298883bc2010-04-30 14:50:58151 // explicitly.
[email protected]56cdae32009-03-12 19:58:20152 original_single_process_ = RenderProcessHost::run_renderer_in_process();
[email protected]8bcdec92009-02-25 16:15:18153 if (command_line->HasSwitch(switches::kSingleProcess))
154 RenderProcessHost::set_run_renderer_in_process(true);
155
[email protected]87bf9742010-06-09 20:31:41156#if defined(OS_WIN)
157 // The Windows sandbox requires that the browser and child processes are the
158 // same binary. So we launch browser_process.exe which loads chrome.dll
[email protected]4f08c83f2010-07-29 23:02:34159 command_line->AppendSwitchPath(switches::kBrowserSubprocessPath,
160 command_line->GetProgram());
[email protected]87bf9742010-06-09 20:31:41161#else
162 // Explicitly set the path of the binary used for child processes, otherwise
163 // they'll try to use browser_tests which doesn't contain ChromeMain.
[email protected]6cea14d2009-09-10 18:19:18164 FilePath subprocess_path;
[email protected]7f74a4e2009-04-30 17:00:24165 PathService::Get(base::FILE_EXE, &subprocess_path);
[email protected]6cea14d2009-09-10 18:19:18166 subprocess_path = subprocess_path.DirName();
167 subprocess_path = subprocess_path.AppendASCII(WideToASCII(
168 chrome::kBrowserProcessExecutablePath));
[email protected]4f08c83f2010-07-29 23:02:34169 command_line->AppendSwitchPath(switches::kBrowserSubprocessPath,
170 subprocess_path);
[email protected]87bf9742010-06-09 20:31:41171#endif
[email protected]d4515eb2009-01-30 00:40:43172
[email protected]9c73efa2009-07-08 00:18:36173 // Enable warning level logging so that we can see when bad stuff happens.
174 command_line->AppendSwitch(switches::kEnableLogging);
[email protected]0c605fe32010-07-30 06:00:38175 command_line->AppendSwitchASCII(switches::kLoggingLevel, "1"); // warning
[email protected]9c73efa2009-07-08 00:18:36176
[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]d4515eb2009-01-30 00:40:43242// Creates a browser with a single tab (about:blank), waits for the tab to
243// finish loading and shows the browser.
244Browser* InProcessBrowserTest::CreateBrowser(Profile* profile) {
245 Browser* browser = Browser::Create(profile);
246
[email protected]e0d481582009-09-15 21:06:25247 browser->AddTabWithURL(GURL(chrome::kAboutBlankURL), GURL(),
[email protected]715af7e2010-04-29 01:55:38248 PageTransition::START_PAGE, -1,
[email protected]b283a7532010-08-12 21:24:59249 TabStripModel::ADD_SELECTED, NULL, std::string(),
250 &browser);
[email protected]f0a51fb52009-03-05 12:46:38251
[email protected]d4515eb2009-01-30 00:40:43252 // Wait for the page to finish loading.
253 ui_test_utils::WaitForNavigation(
[email protected]ce3fa3c2009-04-20 19:55:57254 &browser->GetSelectedTabContents()->controller());
[email protected]d4515eb2009-01-30 00:40:43255
256 browser->window()->Show();
257
258 return browser;
259}
260
[email protected]64f19312010-04-13 22:30:01261void InProcessBrowserTest::RunTestOnMainThreadLoop() {
[email protected]304a3172010-05-04 05:38:44262 // On Mac, without the following autorelease pool, code which is directly
263 // executed (as opposed to executed inside a message loop) would autorelease
264 // objects into a higher-level pool. This pool is not recycled in-sync with
265 // the message loops' pools and causes problems with code relying on
266 // deallocation via an autorelease pool (such as browser window closure and
267 // browser shutdown). To avoid this, the following pool is recycled after each
268 // time code is directly executed.
269 base::ScopedNSAutoreleasePool pool;
270
[email protected]64f19312010-04-13 22:30:01271 // Pump startup related events.
272 MessageLoopForUI::current()->RunAllPending();
273
274 // In the long term it would be great if we could use a TestingProfile
275 // here and only enable services you want tested, but that requires all
276 // consumers of Profile to handle NULL services.
277 Profile* profile = ProfileManager::GetDefaultProfile();
278 if (!profile) {
279 // We should only be able to get here if the profile already exists and
280 // has been created.
281 NOTREACHED();
282 return;
283 }
[email protected]304a3172010-05-04 05:38:44284 pool.Recycle();
[email protected]64f19312010-04-13 22:30:01285
286 ChromeThread::PostTask(
287 ChromeThread::IO, FROM_HERE,
288 NewRunnableFunction(chrome_browser_net::SetUrlRequestMocksEnabled, true));
289
290 browser_ = CreateBrowser(profile);
[email protected]304a3172010-05-04 05:38:44291 pool.Recycle();
[email protected]64f19312010-04-13 22:30:01292
293 // Start the timeout timer to prevent hangs.
294 MessageLoopForUI::current()->PostDelayedTask(FROM_HERE,
295 NewRunnableMethod(this, &InProcessBrowserTest::TimedOut),
296 initial_timeout_);
297
298 // Pump any pending events that were created as a result of creating a
299 // browser.
300 MessageLoopForUI::current()->RunAllPending();
301
302 RunTestOnMainThread();
[email protected]304a3172010-05-04 05:38:44303 pool.Recycle();
[email protected]64f19312010-04-13 22:30:01304
305 CleanUpOnMainThread();
[email protected]304a3172010-05-04 05:38:44306 pool.Recycle();
[email protected]64f19312010-04-13 22:30:01307
308 QuitBrowsers();
[email protected]304a3172010-05-04 05:38:44309 pool.Recycle();
[email protected]64f19312010-04-13 22:30:01310}
311
312void InProcessBrowserTest::QuitBrowsers() {
313 if (BrowserList::size() == 0)
314 return;
315
316 // Invoke CloseAllBrowsersAndExit on a running message loop.
317 // CloseAllBrowsersAndExit exits the message loop after everything has been
318 // shut down properly.
319 MessageLoopForUI::current()->PostTask(
320 FROM_HERE,
321 NewRunnableFunction(&BrowserList::CloseAllBrowsersAndExit));
322 ui_test_utils::RunMessageLoop();
323}
[email protected]b5f95102009-07-01 19:53:59324
[email protected]5c00ca82010-04-08 21:30:42325void InProcessBrowserTest::TimedOut() {
[email protected]94ab2ad2009-10-15 18:18:51326 std::string error_message = "Test timed out. Each test runs for a max of ";
[email protected]528c56d2010-07-30 19:28:44327 error_message += base::IntToString(initial_timeout_);
[email protected]94ab2ad2009-10-15 18:18:51328 error_message += " ms (kInitialTimeoutInMS).";
329
[email protected]73ed7bb2010-08-10 20:50:39330 GTEST_NONFATAL_FAILURE_(error_message.c_str());
[email protected]2fb73f52010-08-10 19:25:31331
[email protected]73ed7bb2010-08-10 20:50:39332 MessageLoopForUI::current()->Quit();
[email protected]b5f95102009-07-01 19:53:59333}
[email protected]4ff446f2009-07-10 18:29:39334
335void InProcessBrowserTest::SetInitialTimeoutInMS(int timeout_value) {
336 DCHECK_GT(timeout_value, 0);
337 initial_timeout_ = timeout_value;
338}