[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 1 | // 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] | d841205 | 2009-04-21 22:01:01 | [diff] [blame] | 8 | #include "base/file_path.h" |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 9 | #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] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 14 | #include "chrome/browser/browser_window.h" |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 15 | #include "chrome/browser/profile.h" |
| 16 | #include "chrome/browser/profile_manager.h" |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 17 | #include "chrome/browser/renderer_host/render_process_host.h" |
[email protected] | 5c23875 | 2009-06-13 10:29:07 | [diff] [blame^] | 18 | #include "chrome/browser/tab_contents/tab_contents.h" |
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 19 | #if defined(OS_WIN) |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 20 | #include "chrome/browser/views/frame/browser_view.h" |
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 21 | #endif |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 22 | #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] | 13324ed | 2009-04-03 05:14:19 | [diff] [blame] | 28 | #include "net/base/host_resolver_unittest.h" |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 29 | #include "sandbox/src/dep.h" |
| 30 | |
| 31 | extern int BrowserMain(const MainFunctionParams&); |
| 32 | |
| 33 | const wchar_t kUnitTestShowWindows[] = L"show-windows"; |
| 34 | |
| 35 | namespace { |
| 36 | |
| 37 | bool 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] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 52 | InProcessBrowserTest::InProcessBrowserTest() |
| 53 | : browser_(NULL), |
| 54 | show_window_(false), |
[email protected] | 56cdae3 | 2009-03-12 19:58:20 | [diff] [blame] | 55 | dom_automation_enabled_(false), |
| 56 | single_process_(false), |
| 57 | original_single_process_(false) { |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void 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] | 56cdae3 | 2009-03-12 19:58:20 | [diff] [blame] | 80 | original_command_line_.reset(new CommandLine(*command_line)); |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 81 | |
[email protected] | 9665fa6 | 2009-04-13 22:15:29 | [diff] [blame] | 82 | SetUpCommandLine(command_line); |
| 83 | |
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 84 | #if defined(OS_WIN) |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 85 | // Hide windows on show. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 86 | if (!command_line->HasSwitch(kUnitTestShowWindows) && !show_window_) |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 87 | BrowserView::SetShowState(SW_HIDE); |
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 88 | #endif |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 89 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 90 | if (dom_automation_enabled_) |
| 91 | command_line->AppendSwitch(switches::kDomAutomationController); |
| 92 | |
[email protected] | 56cdae3 | 2009-03-12 19:58:20 | [diff] [blame] | 93 | if (single_process_) |
| 94 | command_line->AppendSwitch(switches::kSingleProcess); |
| 95 | |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 96 | 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] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 102 | // Single-process mode is not set in BrowserMain so it needs to be processed |
| 103 | // explicitlty. |
[email protected] | 56cdae3 | 2009-03-12 19:58:20 | [diff] [blame] | 104 | original_single_process_ = RenderProcessHost::run_renderer_in_process(); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 105 | if (command_line->HasSwitch(switches::kSingleProcess)) |
| 106 | RenderProcessHost::set_run_renderer_in_process(true); |
| 107 | |
[email protected] | 7f74a4e | 2009-04-30 17:00:24 | [diff] [blame] | 108 | // 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] | 075a725 | 2009-04-23 18:07:51 | [diff] [blame] | 115 | chrome::kBrowserProcessExecutablePath); |
[email protected] | 7f74a4e | 2009-04-30 17:00:24 | [diff] [blame] | 116 | command_line->AppendSwitchWithValue(switches::kBrowserSubprocessPath, |
| 117 | subprocess_path); |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 118 | |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 119 | SandboxInitWrapper sandbox_wrapper; |
[email protected] | 7c32108 | 2009-02-09 15:35:47 | [diff] [blame] | 120 | MainFunctionParams params(*command_line, sandbox_wrapper, NULL); |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 121 | params.ui_task = |
| 122 | NewRunnableMethod(this, &InProcessBrowserTest::RunTestOnMainThreadLoop); |
[email protected] | 13324ed | 2009-04-03 05:14:19 | [diff] [blame] | 123 | |
[email protected] | 13324ed | 2009-04-03 05:14:19 | [diff] [blame] | 124 | scoped_refptr<net::RuleBasedHostMapper> host_mapper( |
| 125 | new net::RuleBasedHostMapper()); |
[email protected] | deb27ae | 2009-04-10 02:37:22 | [diff] [blame] | 126 | ConfigureHostMapper(host_mapper.get()); |
[email protected] | 7844fc5 | 2009-04-07 08:58:48 | [diff] [blame] | 127 | net::ScopedHostMapper scoped_host_mapper(host_mapper.get()); |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 128 | BrowserMain(params); |
| 129 | } |
| 130 | |
| 131 | void 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] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 138 | #if defined(WIN) |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 139 | BrowserView::SetShowState(-1); |
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 140 | #endif |
[email protected] | 56cdae3 | 2009-03-12 19:58:20 | [diff] [blame] | 141 | |
| 142 | *CommandLine::ForCurrentProcessMutable() = *original_command_line_; |
| 143 | RenderProcessHost::set_run_renderer_in_process(original_single_process_); |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | void InProcessBrowserTest::Observe(NotificationType type, |
| 147 | const NotificationSource& source, |
| 148 | const NotificationDetails& details) { |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 149 | if (type == NotificationType::BROWSER_CLOSED) { |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 150 | DCHECK(Source<Browser>(source).ptr() == browser_); |
| 151 | browser_ = NULL; |
| 152 | } else { |
| 153 | NOTREACHED(); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | HTTPTestServer* 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. |
| 168 | Browser* InProcessBrowserTest::CreateBrowser(Profile* profile) { |
| 169 | Browser* browser = Browser::Create(profile); |
| 170 | |
| 171 | browser->AddTabWithURL( |
[email protected] | 5a4940be | 2009-05-06 06:44:39 | [diff] [blame] | 172 | GURL("about:blank"), GURL(), PageTransition::START_PAGE, true, -1, false, |
| 173 | NULL); |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 174 | |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 175 | // Wait for the page to finish loading. |
| 176 | ui_test_utils::WaitForNavigation( |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 177 | &browser->GetSelectedTabContents()->controller()); |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 178 | |
| 179 | browser->window()->Show(); |
| 180 | |
| 181 | return browser; |
| 182 | } |
| 183 | |
| 184 | void 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] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 200 | browser_ = CreateBrowser(profile); |
| 201 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 202 | registrar_.Add(this, |
| 203 | NotificationType::BROWSER_CLOSED, |
| 204 | Source<Browser>(browser_)); |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 205 | |
| 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] | deb27ae | 2009-04-10 02:37:22 | [diff] [blame] | 220 | |
| 221 | void 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 | } |