blob: 0815941d73970426e12dea4ea7b9678227d4e572 [file] [log] [blame]
[email protected]d4515eb2009-01-30 00:40:431// Copyright (c) 2006-2008 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.
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]23cc9a12009-07-30 21:27:0111#include "base/test_file_util.h"
[email protected]d4515eb2009-01-30 00:40:4312#include "chrome/browser/browser.h"
[email protected]b6e38ef2009-06-16 00:43:2313#include "chrome/browser/browser_list.h"
[email protected]d4515eb2009-01-30 00:40:4314#include "chrome/browser/browser_process.h"
15#include "chrome/browser/browser_shutdown.h"
[email protected]108c2a12009-06-05 22:18:0916#include "chrome/browser/browser_window.h"
[email protected]cec4a272009-07-31 21:55:0317#include "chrome/browser/net/url_request_mock_util.h"
[email protected]d4515eb2009-01-30 00:40:4318#include "chrome/browser/profile.h"
19#include "chrome/browser/profile_manager.h"
[email protected]8bcdec92009-02-25 16:15:1820#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]5c238752009-06-13 10:29:0721#include "chrome/browser/tab_contents/tab_contents.h"
[email protected]108c2a12009-06-05 22:18:0922#if defined(OS_WIN)
[email protected]d4515eb2009-01-30 00:40:4323#include "chrome/browser/views/frame/browser_view.h"
[email protected]108c2a12009-06-05 22:18:0924#endif
[email protected]d4515eb2009-01-30 00:40:4325#include "chrome/common/chrome_constants.h"
26#include "chrome/common/chrome_paths.h"
27#include "chrome/common/chrome_switches.h"
28#include "chrome/common/main_function_params.h"
[email protected]ad1f9bd2009-07-30 20:23:1529#include "chrome/common/notification_registrar.h"
30#include "chrome/common/notification_type.h"
[email protected]d4515eb2009-01-30 00:40:4331#include "chrome/test/testing_browser_process.h"
32#include "chrome/test/ui_test_utils.h"
[email protected]b59ff372009-07-15 22:04:3233#include "net/base/mock_host_resolver.h"
[email protected]d4515eb2009-01-30 00:40:4334#include "sandbox/src/dep.h"
35
36extern int BrowserMain(const MainFunctionParams&);
37
38const wchar_t kUnitTestShowWindows[] = L"show-windows";
39
[email protected]4ff446f2009-07-10 18:29:3940// Default delay for the time-out at which we stop the
41// inner-message loop the first time.
[email protected]b5f95102009-07-01 19:53:5942const int kInitialTimeoutInMS = 30000;
43
44// Delay for sub-sequent time-outs once the initial time-out happened.
45const int kSubsequentTimeoutInMS = 5000;
46
[email protected]8bcdec92009-02-25 16:15:1847InProcessBrowserTest::InProcessBrowserTest()
48 : browser_(NULL),
49 show_window_(false),
[email protected]56cdae32009-03-12 19:58:2050 dom_automation_enabled_(false),
51 single_process_(false),
[email protected]4ff446f2009-07-10 18:29:3952 original_single_process_(false),
53 initial_timeout_(kInitialTimeoutInMS) {
[email protected]d4515eb2009-01-30 00:40:4354}
55
56void InProcessBrowserTest::SetUp() {
57 // Cleanup the user data dir.
[email protected]23cc9a12009-07-30 21:27:0158 FilePath user_data_dir;
[email protected]d4515eb2009-01-30 00:40:4359 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
[email protected]23cc9a12009-07-30 21:27:0160 ASSERT_LT(10, static_cast<int>(user_data_dir.value().size())) <<
[email protected]d4515eb2009-01-30 00:40:4361 "The user data directory name passed into this test was too "
62 "short to delete safely. Please check the user-data-dir "
63 "argument and try again.";
[email protected]23cc9a12009-07-30 21:27:0164 ASSERT_TRUE(file_util::DieFileDie(user_data_dir, true));
[email protected]d4515eb2009-01-30 00:40:4365
66 // The unit test suite creates a testingbrowser, but we want the real thing.
67 // Delete the current one. We'll install the testing one in TearDown.
68 delete g_browser_process;
69
70 // Don't delete the resources when BrowserMain returns. Many ui classes
71 // cache SkBitmaps in a static field so that if we delete the resource
72 // bundle we'll crash.
73 browser_shutdown::delete_resources_on_shutdown = false;
74
75 CommandLine* command_line = CommandLine::ForCurrentProcessMutable();
[email protected]56cdae32009-03-12 19:58:2076 original_command_line_.reset(new CommandLine(*command_line));
[email protected]d4515eb2009-01-30 00:40:4377
[email protected]9665fa62009-04-13 22:15:2978 SetUpCommandLine(command_line);
79
[email protected]108c2a12009-06-05 22:18:0980#if defined(OS_WIN)
[email protected]d4515eb2009-01-30 00:40:4381 // Hide windows on show.
[email protected]8bcdec92009-02-25 16:15:1882 if (!command_line->HasSwitch(kUnitTestShowWindows) && !show_window_)
[email protected]d4515eb2009-01-30 00:40:4383 BrowserView::SetShowState(SW_HIDE);
[email protected]108c2a12009-06-05 22:18:0984#endif
[email protected]d4515eb2009-01-30 00:40:4385
[email protected]8bcdec92009-02-25 16:15:1886 if (dom_automation_enabled_)
87 command_line->AppendSwitch(switches::kDomAutomationController);
88
[email protected]56cdae32009-03-12 19:58:2089 if (single_process_)
90 command_line->AppendSwitch(switches::kSingleProcess);
91
[email protected]1cf38fa2009-07-25 06:54:4392 // TODO(arv): Reenable once kEnableWebResources is changed back to
93 // kDisableWebResources
94 // http://crbug.com/17725
95 // command_line->AppendSwitch(switches::kEnableWebResources);
[email protected]0a519262009-07-13 18:14:0896
[email protected]23cc9a12009-07-30 21:27:0197 command_line->AppendSwitchWithValue(switches::kUserDataDir,
98 user_data_dir.ToWStringHack());
[email protected]d4515eb2009-01-30 00:40:4399
100 // For some reason the sandbox wasn't happy running in test mode. These
101 // tests aren't intended to test the sandbox, so we turn it off.
102 command_line->AppendSwitch(switches::kNoSandbox);
103
[email protected]e01aba812009-07-09 18:31:24104 // Don't show the first run ui.
105 command_line->AppendSwitch(switches::kNoFirstRun);
106
[email protected]8bcdec92009-02-25 16:15:18107 // Single-process mode is not set in BrowserMain so it needs to be processed
108 // explicitlty.
[email protected]56cdae32009-03-12 19:58:20109 original_single_process_ = RenderProcessHost::run_renderer_in_process();
[email protected]8bcdec92009-02-25 16:15:18110 if (command_line->HasSwitch(switches::kSingleProcess))
111 RenderProcessHost::set_run_renderer_in_process(true);
112
[email protected]7f74a4e2009-04-30 17:00:24113 // Explicitly set the path of the exe used for the renderer and plugin,
114 // otherwise they'll try to use unit_test.exe.
115 std::wstring subprocess_path;
116 PathService::Get(base::FILE_EXE, &subprocess_path);
117 FilePath fp_subprocess_path = FilePath::FromWStringHack(subprocess_path);
118 subprocess_path = fp_subprocess_path.DirName().ToWStringHack();
119 file_util::AppendToPath(&subprocess_path,
[email protected]075a7252009-04-23 18:07:51120 chrome::kBrowserProcessExecutablePath);
[email protected]7f74a4e2009-04-30 17:00:24121 command_line->AppendSwitchWithValue(switches::kBrowserSubprocessPath,
122 subprocess_path);
[email protected]d4515eb2009-01-30 00:40:43123
[email protected]9c73efa2009-07-08 00:18:36124 // Enable warning level logging so that we can see when bad stuff happens.
125 command_line->AppendSwitch(switches::kEnableLogging);
126 command_line->AppendSwitchWithValue(switches::kLoggingLevel,
127 IntToWString(1)); // warning
128
[email protected]d4515eb2009-01-30 00:40:43129 SandboxInitWrapper sandbox_wrapper;
[email protected]7c321082009-02-09 15:35:47130 MainFunctionParams params(*command_line, sandbox_wrapper, NULL);
[email protected]d4515eb2009-01-30 00:40:43131 params.ui_task =
132 NewRunnableMethod(this, &InProcessBrowserTest::RunTestOnMainThreadLoop);
[email protected]13324ed2009-04-03 05:14:19133
[email protected]b59ff372009-07-15 22:04:32134 scoped_refptr<net::RuleBasedHostResolverProc> host_resolver_proc(
135 new net::RuleBasedHostResolverProc(NULL));
136 ConfigureHostResolverProc(host_resolver_proc);
137 net::ScopedDefaultHostResolverProc scoped_host_resolver_proc(
138 host_resolver_proc);
[email protected]d4515eb2009-01-30 00:40:43139 BrowserMain(params);
140}
141
142void InProcessBrowserTest::TearDown() {
143 // Reinstall testing browser process.
144 delete g_browser_process;
145 g_browser_process = new TestingBrowserProcess();
146
147 browser_shutdown::delete_resources_on_shutdown = true;
148
[email protected]108c2a12009-06-05 22:18:09149#if defined(WIN)
[email protected]d4515eb2009-01-30 00:40:43150 BrowserView::SetShowState(-1);
[email protected]108c2a12009-06-05 22:18:09151#endif
[email protected]56cdae32009-03-12 19:58:20152
153 *CommandLine::ForCurrentProcessMutable() = *original_command_line_;
154 RenderProcessHost::set_run_renderer_in_process(original_single_process_);
[email protected]d4515eb2009-01-30 00:40:43155}
156
[email protected]d4515eb2009-01-30 00:40:43157HTTPTestServer* InProcessBrowserTest::StartHTTPServer() {
158 // The HTTPServer must run on the IO thread.
159 DCHECK(!http_server_.get());
160 http_server_ = HTTPTestServer::CreateServer(
161 L"chrome/test/data",
162 g_browser_process->io_thread()->message_loop());
163 return http_server_.get();
164}
165
166// Creates a browser with a single tab (about:blank), waits for the tab to
167// finish loading and shows the browser.
168Browser* InProcessBrowserTest::CreateBrowser(Profile* profile) {
169 Browser* browser = Browser::Create(profile);
170
171 browser->AddTabWithURL(
[email protected]5a4940be2009-05-06 06:44:39172 GURL("about:blank"), GURL(), PageTransition::START_PAGE, true, -1, false,
173 NULL);
[email protected]f0a51fb52009-03-05 12:46:38174
[email protected]d4515eb2009-01-30 00:40:43175 // Wait for the page to finish loading.
176 ui_test_utils::WaitForNavigation(
[email protected]ce3fa3c2009-04-20 19:55:57177 &browser->GetSelectedTabContents()->controller());
[email protected]d4515eb2009-01-30 00:40:43178
179 browser->window()->Show();
180
181 return browser;
182}
183
184void InProcessBrowserTest::RunTestOnMainThreadLoop() {
185 // In the long term it would be great if we could use a TestingProfile
186 // here and only enable services you want tested, but that requires all
187 // consumers of Profile to handle NULL services.
188 FilePath user_data_dir;
189 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
190 ProfileManager* profile_manager = g_browser_process->profile_manager();
191 Profile* profile = profile_manager->GetDefaultProfile(user_data_dir);
192 if (!profile) {
193 // We should only be able to get here if the profile already exists and
194 // has been created.
195 NOTREACHED();
196 MessageLoopForUI::current()->Quit();
197 return;
198 }
199
[email protected]eca6a4f2009-06-25 17:29:09200
201 // Before we run the browser, we have to hack the path to the exe to match
202 // what it would be if Chrome was running, because it is used to fork renderer
203 // processes, on Linux at least (failure to do so will cause a browser_test to
204 // be run instead of a renderer).
205 FilePath chrome_path;
206 CHECK(PathService::Get(base::FILE_EXE, &chrome_path));
207 chrome_path = chrome_path.DirName();
208#if defined(OS_WIN)
209 chrome_path = chrome_path.Append(chrome::kBrowserProcessExecutablePath);
210#elif defined(OS_POSIX)
211 chrome_path = chrome_path.Append(
212 WideToASCII(chrome::kBrowserProcessExecutablePath));
213#endif
214 CHECK(PathService::Override(base::FILE_EXE, chrome_path));
215
[email protected]cec4a272009-07-31 21:55:03216 g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE,
217 NewRunnableFunction(chrome_browser_net::SetUrlRequestMocksEnabled, true));
218
[email protected]d4515eb2009-01-30 00:40:43219 browser_ = CreateBrowser(profile);
220
[email protected]b5f95102009-07-01 19:53:59221 // Start the timeout timer to prevent hangs.
222 MessageLoopForUI::current()->PostDelayedTask(FROM_HERE,
223 NewRunnableMethod(this, &InProcessBrowserTest::TimedOut),
[email protected]4ff446f2009-07-10 18:29:39224 initial_timeout_);
[email protected]b5f95102009-07-01 19:53:59225
[email protected]d4515eb2009-01-30 00:40:43226 RunTestOnMainThread();
[email protected]17c4f3c2009-07-04 16:36:25227 CleanUpOnMainThread();
[email protected]d4515eb2009-01-30 00:40:43228
[email protected]cc661e52009-07-27 19:18:41229 // Close all browser windows. This might not happen immediately, since some
230 // may need to wait for beforeunload and unload handlers to fire in a tab.
231 // When all windows are closed, the last window will call Quit().
[email protected]e6152c22009-06-23 23:33:27232 BrowserList::const_iterator browser = BrowserList::begin();
233 for (; browser != BrowserList::end(); ++browser)
[email protected]cc661e52009-07-27 19:18:41234 (*browser)->CloseWindow();
[email protected]d4515eb2009-01-30 00:40:43235
236 // Stop the HTTP server.
237 http_server_ = NULL;
[email protected]d4515eb2009-01-30 00:40:43238}
[email protected]deb27ae2009-04-10 02:37:22239
[email protected]b59ff372009-07-15 22:04:32240void InProcessBrowserTest::ConfigureHostResolverProc(
241 net::RuleBasedHostResolverProc* host_resolver_proc) {
242 host_resolver_proc->AllowDirectLookup("*.google.com");
[email protected]deb27ae2009-04-10 02:37:22243 // See http://en.wikipedia.org/wiki/Web_Proxy_Autodiscovery_Protocol
244 // We don't want the test code to use it.
[email protected]b59ff372009-07-15 22:04:32245 host_resolver_proc->AddSimulatedFailure("wpad");
[email protected]deb27ae2009-04-10 02:37:22246}
[email protected]b5f95102009-07-01 19:53:59247
248void InProcessBrowserTest::TimedOut() {
249 DCHECK(MessageLoopForUI::current()->IsNested());
250
251 GTEST_NONFATAL_FAILURE_("Timed-out");
252
253 // Start the timeout timer to prevent hangs.
254 MessageLoopForUI::current()->PostDelayedTask(FROM_HERE,
255 NewRunnableMethod(this, &InProcessBrowserTest::TimedOut),
256 kSubsequentTimeoutInMS);
257
258 MessageLoopForUI::current()->Quit();
259}
[email protected]4ff446f2009-07-10 18:29:39260
261void InProcessBrowserTest::SetInitialTimeoutInMS(int timeout_value) {
262 DCHECK_GT(timeout_value, 0);
263 initial_timeout_ = timeout_value;
264}