[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" |
| 8 | #include "base/file_util.h" |
| 9 | #include "base/path_service.h" |
| 10 | #include "chrome/browser/browser.h" |
| 11 | #include "chrome/browser/browser_process.h" |
| 12 | #include "chrome/browser/browser_shutdown.h" |
| 13 | #include "chrome/browser/profile.h" |
| 14 | #include "chrome/browser/profile_manager.h" |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 15 | #include "chrome/browser/renderer_host/render_process_host.h" |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 16 | #include "chrome/browser/views/frame/browser_view.h" |
| 17 | #include "chrome/common/chrome_constants.h" |
| 18 | #include "chrome/common/chrome_paths.h" |
| 19 | #include "chrome/common/chrome_switches.h" |
| 20 | #include "chrome/common/main_function_params.h" |
| 21 | #include "chrome/test/testing_browser_process.h" |
| 22 | #include "chrome/test/ui_test_utils.h" |
[email protected] | 13324ed | 2009-04-03 05:14:19 | [diff] [blame] | 23 | #include "net/base/host_resolver_unittest.h" |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 24 | #include "sandbox/src/sandbox_factory.h" |
| 25 | #include "sandbox/src/dep.h" |
| 26 | |
| 27 | extern int BrowserMain(const MainFunctionParams&); |
| 28 | |
| 29 | const wchar_t kUnitTestShowWindows[] = L"show-windows"; |
| 30 | |
| 31 | namespace { |
| 32 | |
| 33 | bool DieFileDie(const std::wstring& file, bool recurse) { |
| 34 | if (!file_util::PathExists(file)) |
| 35 | return true; |
| 36 | |
| 37 | // Sometimes Delete fails, so try a few more times. |
| 38 | for (int i = 0; i < 10; ++i) { |
| 39 | if (file_util::Delete(file, recurse)) |
| 40 | return true; |
| 41 | PlatformThread::Sleep(100); |
| 42 | } |
| 43 | return false; |
| 44 | } |
| 45 | |
[email protected] | 22735af6 | 2009-04-07 21:09:58 | [diff] [blame] | 46 | class ShadowingAtExitManager : public base::AtExitManager { |
| 47 | public: |
| 48 | ShadowingAtExitManager() : base::AtExitManager(true) {} |
| 49 | virtual ~ShadowingAtExitManager() {} |
| 50 | |
| 51 | private: |
| 52 | DISALLOW_COPY_AND_ASSIGN(ShadowingAtExitManager); |
| 53 | }; |
| 54 | |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 55 | } // namespace |
| 56 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 57 | InProcessBrowserTest::InProcessBrowserTest() |
| 58 | : browser_(NULL), |
| 59 | show_window_(false), |
[email protected] | 56cdae3 | 2009-03-12 19:58:20 | [diff] [blame] | 60 | dom_automation_enabled_(false), |
| 61 | single_process_(false), |
| 62 | original_single_process_(false) { |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void InProcessBrowserTest::SetUp() { |
[email protected] | 22735af6 | 2009-04-07 21:09:58 | [diff] [blame] | 66 | ShadowingAtExitManager at_exit_manager; |
| 67 | |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 68 | // Cleanup the user data dir. |
| 69 | std::wstring user_data_dir; |
| 70 | PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); |
| 71 | ASSERT_LT(10, static_cast<int>(user_data_dir.size())) << |
| 72 | "The user data directory name passed into this test was too " |
| 73 | "short to delete safely. Please check the user-data-dir " |
| 74 | "argument and try again."; |
| 75 | ASSERT_TRUE(DieFileDie(user_data_dir, true)); |
| 76 | |
| 77 | // The unit test suite creates a testingbrowser, but we want the real thing. |
| 78 | // Delete the current one. We'll install the testing one in TearDown. |
| 79 | delete g_browser_process; |
| 80 | |
| 81 | // Don't delete the resources when BrowserMain returns. Many ui classes |
| 82 | // cache SkBitmaps in a static field so that if we delete the resource |
| 83 | // bundle we'll crash. |
| 84 | browser_shutdown::delete_resources_on_shutdown = false; |
| 85 | |
| 86 | CommandLine* command_line = CommandLine::ForCurrentProcessMutable(); |
[email protected] | 56cdae3 | 2009-03-12 19:58:20 | [diff] [blame] | 87 | original_command_line_.reset(new CommandLine(*command_line)); |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 88 | |
[email protected] | 9665fa6 | 2009-04-13 22:15:29 | [diff] [blame] | 89 | SetUpCommandLine(command_line); |
| 90 | |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 91 | // Hide windows on show. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 92 | if (!command_line->HasSwitch(kUnitTestShowWindows) && !show_window_) |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 93 | BrowserView::SetShowState(SW_HIDE); |
| 94 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 95 | if (dom_automation_enabled_) |
| 96 | command_line->AppendSwitch(switches::kDomAutomationController); |
| 97 | |
[email protected] | 56cdae3 | 2009-03-12 19:58:20 | [diff] [blame] | 98 | if (single_process_) |
| 99 | command_line->AppendSwitch(switches::kSingleProcess); |
| 100 | |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 101 | command_line->AppendSwitchWithValue(switches::kUserDataDir, user_data_dir); |
| 102 | |
| 103 | // For some reason the sandbox wasn't happy running in test mode. These |
| 104 | // tests aren't intended to test the sandbox, so we turn it off. |
| 105 | command_line->AppendSwitch(switches::kNoSandbox); |
| 106 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 107 | // Single-process mode is not set in BrowserMain so it needs to be processed |
| 108 | // explicitlty. |
[email protected] | 56cdae3 | 2009-03-12 19:58:20 | [diff] [blame] | 109 | original_single_process_ = RenderProcessHost::run_renderer_in_process(); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 110 | if (command_line->HasSwitch(switches::kSingleProcess)) |
| 111 | RenderProcessHost::set_run_renderer_in_process(true); |
| 112 | |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 113 | // Explicitly set the path of the exe used for the renderer, otherwise it'll |
| 114 | // try to use unit_test.exe. |
| 115 | std::wstring renderer_path; |
| 116 | PathService::Get(base::FILE_EXE, &renderer_path); |
| 117 | file_util::TrimFilename(&renderer_path); |
| 118 | file_util::AppendToPath(&renderer_path, |
| 119 | chrome::kBrowserProcessExecutableName); |
| 120 | command_line->AppendSwitchWithValue(switches::kRendererPath, renderer_path); |
| 121 | |
| 122 | sandbox::SandboxInterfaceInfo sandbox_info = {0}; |
| 123 | SandboxInitWrapper sandbox_wrapper; |
[email protected] | 7c32108 | 2009-02-09 15:35:47 | [diff] [blame] | 124 | MainFunctionParams params(*command_line, sandbox_wrapper, NULL); |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 125 | params.ui_task = |
| 126 | NewRunnableMethod(this, &InProcessBrowserTest::RunTestOnMainThreadLoop); |
[email protected] | 13324ed | 2009-04-03 05:14:19 | [diff] [blame] | 127 | |
[email protected] | 13324ed | 2009-04-03 05:14:19 | [diff] [blame] | 128 | scoped_refptr<net::RuleBasedHostMapper> host_mapper( |
| 129 | new net::RuleBasedHostMapper()); |
[email protected] | deb27ae | 2009-04-10 02:37:22 | [diff] [blame] | 130 | ConfigureHostMapper(host_mapper.get()); |
[email protected] | 7844fc5 | 2009-04-07 08:58:48 | [diff] [blame] | 131 | net::ScopedHostMapper scoped_host_mapper(host_mapper.get()); |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 132 | BrowserMain(params); |
| 133 | } |
| 134 | |
| 135 | void InProcessBrowserTest::TearDown() { |
| 136 | // Reinstall testing browser process. |
| 137 | delete g_browser_process; |
| 138 | g_browser_process = new TestingBrowserProcess(); |
| 139 | |
| 140 | browser_shutdown::delete_resources_on_shutdown = true; |
| 141 | |
| 142 | BrowserView::SetShowState(-1); |
[email protected] | 56cdae3 | 2009-03-12 19:58:20 | [diff] [blame] | 143 | |
| 144 | *CommandLine::ForCurrentProcessMutable() = *original_command_line_; |
| 145 | RenderProcessHost::set_run_renderer_in_process(original_single_process_); |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | void InProcessBrowserTest::Observe(NotificationType type, |
| 149 | const NotificationSource& source, |
| 150 | const NotificationDetails& details) { |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 151 | if (type == NotificationType::BROWSER_CLOSED) { |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 152 | DCHECK(Source<Browser>(source).ptr() == browser_); |
| 153 | browser_ = NULL; |
| 154 | } else { |
| 155 | NOTREACHED(); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | HTTPTestServer* InProcessBrowserTest::StartHTTPServer() { |
| 160 | // The HTTPServer must run on the IO thread. |
| 161 | DCHECK(!http_server_.get()); |
| 162 | http_server_ = HTTPTestServer::CreateServer( |
| 163 | L"chrome/test/data", |
| 164 | g_browser_process->io_thread()->message_loop()); |
| 165 | return http_server_.get(); |
| 166 | } |
| 167 | |
| 168 | // Creates a browser with a single tab (about:blank), waits for the tab to |
| 169 | // finish loading and shows the browser. |
| 170 | Browser* InProcessBrowserTest::CreateBrowser(Profile* profile) { |
| 171 | Browser* browser = Browser::Create(profile); |
| 172 | |
| 173 | browser->AddTabWithURL( |
[email protected] | 22735af6 | 2009-04-07 21:09:58 | [diff] [blame] | 174 | GURL("about:blank"), GURL(), PageTransition::START_PAGE, true, -1, NULL); |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 175 | |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 176 | // Wait for the page to finish loading. |
| 177 | ui_test_utils::WaitForNavigation( |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame^] | 178 | &browser->GetSelectedTabContents()->controller()); |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 179 | |
| 180 | browser->window()->Show(); |
| 181 | |
| 182 | return browser; |
| 183 | } |
| 184 | |
| 185 | void InProcessBrowserTest::RunTestOnMainThreadLoop() { |
| 186 | // In the long term it would be great if we could use a TestingProfile |
| 187 | // here and only enable services you want tested, but that requires all |
| 188 | // consumers of Profile to handle NULL services. |
| 189 | FilePath user_data_dir; |
| 190 | PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); |
| 191 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 192 | Profile* profile = profile_manager->GetDefaultProfile(user_data_dir); |
| 193 | if (!profile) { |
| 194 | // We should only be able to get here if the profile already exists and |
| 195 | // has been created. |
| 196 | NOTREACHED(); |
| 197 | MessageLoopForUI::current()->Quit(); |
| 198 | return; |
| 199 | } |
| 200 | |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 201 | browser_ = CreateBrowser(profile); |
| 202 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 203 | registrar_.Add(this, |
| 204 | NotificationType::BROWSER_CLOSED, |
| 205 | Source<Browser>(browser_)); |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 206 | |
| 207 | RunTestOnMainThread(); |
| 208 | |
| 209 | if (browser_) |
| 210 | browser_->CloseAllTabs(); |
| 211 | |
| 212 | // Remove all registered notifications, otherwise by the time the |
| 213 | // destructor is run the NotificationService is dead. |
| 214 | registrar_.RemoveAll(); |
| 215 | |
| 216 | // Stop the HTTP server. |
| 217 | http_server_ = NULL; |
| 218 | |
| 219 | MessageLoopForUI::current()->Quit(); |
| 220 | } |
[email protected] | deb27ae | 2009-04-10 02:37:22 | [diff] [blame] | 221 | |
| 222 | void InProcessBrowserTest::ConfigureHostMapper( |
| 223 | net::RuleBasedHostMapper* host_mapper) { |
| 224 | host_mapper->AllowDirectLookup("*.google.com"); |
| 225 | // See http://en.wikipedia.org/wiki/Web_Proxy_Autodiscovery_Protocol |
| 226 | // We don't want the test code to use it. |
| 227 | host_mapper->AddSimulatedFailure("wpad"); |
| 228 | } |