blob: d79a02e440618906248d186a78ec78d5210cda66 [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"
11#include "chrome/browser/browser.h"
12#include "chrome/browser/browser_process.h"
13#include "chrome/browser/browser_shutdown.h"
[email protected]108c2a12009-06-05 22:18:0914#include "chrome/browser/browser_window.h"
[email protected]d4515eb2009-01-30 00:40:4315#include "chrome/browser/profile.h"
16#include "chrome/browser/profile_manager.h"
[email protected]8bcdec92009-02-25 16:15:1817#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]5c238752009-06-13 10:29:0718#include "chrome/browser/tab_contents/tab_contents.h"
[email protected]108c2a12009-06-05 22:18:0919#if defined(OS_WIN)
[email protected]d4515eb2009-01-30 00:40:4320#include "chrome/browser/views/frame/browser_view.h"
[email protected]108c2a12009-06-05 22:18:0921#endif
[email protected]d4515eb2009-01-30 00:40:4322#include "chrome/common/chrome_constants.h"
23#include "chrome/common/chrome_paths.h"
24#include "chrome/common/chrome_switches.h"
25#include "chrome/common/main_function_params.h"
26#include "chrome/test/testing_browser_process.h"
27#include "chrome/test/ui_test_utils.h"
[email protected]13324ed2009-04-03 05:14:1928#include "net/base/host_resolver_unittest.h"
[email protected]d4515eb2009-01-30 00:40:4329#include "sandbox/src/dep.h"
30
31extern int BrowserMain(const MainFunctionParams&);
32
33const wchar_t kUnitTestShowWindows[] = L"show-windows";
34
35namespace {
36
37bool DieFileDie(const std::wstring& file, bool recurse) {
38 if (!file_util::PathExists(file))
39 return true;
40
41 // Sometimes Delete fails, so try a few more times.
42 for (int i = 0; i < 10; ++i) {
43 if (file_util::Delete(file, recurse))
44 return true;
45 PlatformThread::Sleep(100);
46 }
47 return false;
48}
49
50} // namespace
51
[email protected]8bcdec92009-02-25 16:15:1852InProcessBrowserTest::InProcessBrowserTest()
53 : browser_(NULL),
54 show_window_(false),
[email protected]56cdae32009-03-12 19:58:2055 dom_automation_enabled_(false),
56 single_process_(false),
57 original_single_process_(false) {
[email protected]d4515eb2009-01-30 00:40:4358}
59
60void InProcessBrowserTest::SetUp() {
61 // Cleanup the user data dir.
62 std::wstring user_data_dir;
63 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
64 ASSERT_LT(10, static_cast<int>(user_data_dir.size())) <<
65 "The user data directory name passed into this test was too "
66 "short to delete safely. Please check the user-data-dir "
67 "argument and try again.";
68 ASSERT_TRUE(DieFileDie(user_data_dir, true));
69
70 // The unit test suite creates a testingbrowser, but we want the real thing.
71 // Delete the current one. We'll install the testing one in TearDown.
72 delete g_browser_process;
73
74 // Don't delete the resources when BrowserMain returns. Many ui classes
75 // cache SkBitmaps in a static field so that if we delete the resource
76 // bundle we'll crash.
77 browser_shutdown::delete_resources_on_shutdown = false;
78
79 CommandLine* command_line = CommandLine::ForCurrentProcessMutable();
[email protected]56cdae32009-03-12 19:58:2080 original_command_line_.reset(new CommandLine(*command_line));
[email protected]d4515eb2009-01-30 00:40:4381
[email protected]9665fa62009-04-13 22:15:2982 SetUpCommandLine(command_line);
83
[email protected]108c2a12009-06-05 22:18:0984#if defined(OS_WIN)
[email protected]d4515eb2009-01-30 00:40:4385 // Hide windows on show.
[email protected]8bcdec92009-02-25 16:15:1886 if (!command_line->HasSwitch(kUnitTestShowWindows) && !show_window_)
[email protected]d4515eb2009-01-30 00:40:4387 BrowserView::SetShowState(SW_HIDE);
[email protected]108c2a12009-06-05 22:18:0988#endif
[email protected]d4515eb2009-01-30 00:40:4389
[email protected]8bcdec92009-02-25 16:15:1890 if (dom_automation_enabled_)
91 command_line->AppendSwitch(switches::kDomAutomationController);
92
[email protected]56cdae32009-03-12 19:58:2093 if (single_process_)
94 command_line->AppendSwitch(switches::kSingleProcess);
95
[email protected]d4515eb2009-01-30 00:40:4396 command_line->AppendSwitchWithValue(switches::kUserDataDir, user_data_dir);
97
98 // For some reason the sandbox wasn't happy running in test mode. These
99 // tests aren't intended to test the sandbox, so we turn it off.
100 command_line->AppendSwitch(switches::kNoSandbox);
101
[email protected]8bcdec92009-02-25 16:15:18102 // Single-process mode is not set in BrowserMain so it needs to be processed
103 // explicitlty.
[email protected]56cdae32009-03-12 19:58:20104 original_single_process_ = RenderProcessHost::run_renderer_in_process();
[email protected]8bcdec92009-02-25 16:15:18105 if (command_line->HasSwitch(switches::kSingleProcess))
106 RenderProcessHost::set_run_renderer_in_process(true);
107
[email protected]7f74a4e2009-04-30 17:00:24108 // Explicitly set the path of the exe used for the renderer and plugin,
109 // otherwise they'll try to use unit_test.exe.
110 std::wstring subprocess_path;
111 PathService::Get(base::FILE_EXE, &subprocess_path);
112 FilePath fp_subprocess_path = FilePath::FromWStringHack(subprocess_path);
113 subprocess_path = fp_subprocess_path.DirName().ToWStringHack();
114 file_util::AppendToPath(&subprocess_path,
[email protected]075a7252009-04-23 18:07:51115 chrome::kBrowserProcessExecutablePath);
[email protected]7f74a4e2009-04-30 17:00:24116 command_line->AppendSwitchWithValue(switches::kBrowserSubprocessPath,
117 subprocess_path);
[email protected]d4515eb2009-01-30 00:40:43118
[email protected]d4515eb2009-01-30 00:40:43119 SandboxInitWrapper sandbox_wrapper;
[email protected]7c321082009-02-09 15:35:47120 MainFunctionParams params(*command_line, sandbox_wrapper, NULL);
[email protected]d4515eb2009-01-30 00:40:43121 params.ui_task =
122 NewRunnableMethod(this, &InProcessBrowserTest::RunTestOnMainThreadLoop);
[email protected]13324ed2009-04-03 05:14:19123
[email protected]13324ed2009-04-03 05:14:19124 scoped_refptr<net::RuleBasedHostMapper> host_mapper(
125 new net::RuleBasedHostMapper());
[email protected]deb27ae2009-04-10 02:37:22126 ConfigureHostMapper(host_mapper.get());
[email protected]7844fc52009-04-07 08:58:48127 net::ScopedHostMapper scoped_host_mapper(host_mapper.get());
[email protected]d4515eb2009-01-30 00:40:43128 BrowserMain(params);
129}
130
131void InProcessBrowserTest::TearDown() {
132 // Reinstall testing browser process.
133 delete g_browser_process;
134 g_browser_process = new TestingBrowserProcess();
135
136 browser_shutdown::delete_resources_on_shutdown = true;
137
[email protected]108c2a12009-06-05 22:18:09138#if defined(WIN)
[email protected]d4515eb2009-01-30 00:40:43139 BrowserView::SetShowState(-1);
[email protected]108c2a12009-06-05 22:18:09140#endif
[email protected]56cdae32009-03-12 19:58:20141
142 *CommandLine::ForCurrentProcessMutable() = *original_command_line_;
143 RenderProcessHost::set_run_renderer_in_process(original_single_process_);
[email protected]d4515eb2009-01-30 00:40:43144}
145
146void InProcessBrowserTest::Observe(NotificationType type,
147 const NotificationSource& source,
148 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:56149 if (type == NotificationType::BROWSER_CLOSED) {
[email protected]d4515eb2009-01-30 00:40:43150 DCHECK(Source<Browser>(source).ptr() == browser_);
151 browser_ = NULL;
152 } else {
153 NOTREACHED();
154 }
155}
156
157HTTPTestServer* 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]d4515eb2009-01-30 00:40:43200 browser_ = CreateBrowser(profile);
201
[email protected]bfd04a62009-02-01 18:16:56202 registrar_.Add(this,
203 NotificationType::BROWSER_CLOSED,
204 Source<Browser>(browser_));
[email protected]d4515eb2009-01-30 00:40:43205
206 RunTestOnMainThread();
207
208 if (browser_)
209 browser_->CloseAllTabs();
210
211 // Remove all registered notifications, otherwise by the time the
212 // destructor is run the NotificationService is dead.
213 registrar_.RemoveAll();
214
215 // Stop the HTTP server.
216 http_server_ = NULL;
217
218 MessageLoopForUI::current()->Quit();
219}
[email protected]deb27ae2009-04-10 02:37:22220
221void InProcessBrowserTest::ConfigureHostMapper(
222 net::RuleBasedHostMapper* host_mapper) {
223 host_mapper->AllowDirectLookup("*.google.com");
224 // See http://en.wikipedia.org/wiki/Web_Proxy_Autodiscovery_Protocol
225 // We don't want the test code to use it.
226 host_mapper->AddSimulatedFailure("wpad");
227}