[email protected] | c4ff495 | 2010-01-08 19:12:47 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 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" |
[email protected] | 304a317 | 2010-05-04 05:38:44 | [diff] [blame] | 11 | #include "base/scoped_nsautorelease_pool.h" |
[email protected] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame^] | 12 | #include "base/string_number_conversions.h" |
[email protected] | fb895c6 | 2009-10-09 18:20:30 | [diff] [blame] | 13 | #include "base/test/test_file_util.h" |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 14 | #include "chrome/browser/browser.h" |
[email protected] | b6e38ef | 2009-06-16 00:43:23 | [diff] [blame] | 15 | #include "chrome/browser/browser_list.h" |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 16 | #include "chrome/browser/browser_process.h" |
| 17 | #include "chrome/browser/browser_shutdown.h" |
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 18 | #include "chrome/browser/browser_window.h" |
[email protected] | 6fad263 | 2009-11-02 05:59:37 | [diff] [blame] | 19 | #include "chrome/browser/chrome_thread.h" |
[email protected] | c4ff495 | 2010-01-08 19:12:47 | [diff] [blame] | 20 | #include "chrome/browser/intranet_redirect_detector.h" |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 21 | #include "chrome/browser/io_thread.h" |
[email protected] | cec4a27 | 2009-07-31 21:55:03 | [diff] [blame] | 22 | #include "chrome/browser/net/url_request_mock_util.h" |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 23 | #include "chrome/browser/profile.h" |
| 24 | #include "chrome/browser/profile_manager.h" |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 25 | #include "chrome/browser/renderer_host/render_process_host.h" |
[email protected] | 5c23875 | 2009-06-13 10:29:07 | [diff] [blame] | 26 | #include "chrome/browser/tab_contents/tab_contents.h" |
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 27 | #if defined(OS_WIN) |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 28 | #include "chrome/browser/views/frame/browser_view.h" |
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 29 | #endif |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 30 | #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] | ad1f9bd | 2009-07-30 20:23:15 | [diff] [blame] | 34 | #include "chrome/common/notification_registrar.h" |
| 35 | #include "chrome/common/notification_type.h" |
[email protected] | e0d48158 | 2009-09-15 21:06:25 | [diff] [blame] | 36 | #include "chrome/common/url_constants.h" |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 37 | #include "chrome/test/testing_browser_process.h" |
| 38 | #include "chrome/test/ui_test_utils.h" |
[email protected] | c4ff495 | 2010-01-08 19:12:47 | [diff] [blame] | 39 | #include "net/base/mock_host_resolver.h" |
[email protected] | 3985ba8 | 2010-07-29 21:44:12 | [diff] [blame] | 40 | #include "net/test/test_server.h" |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 41 | #include "sandbox/src/dep.h" |
| 42 | |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 43 | #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 | |
| 48 | namespace { |
| 49 | |
| 50 | // A helper class to do Linux-only initialization only once per process. |
| 51 | class LinuxHostInit { |
| 52 | public: |
| 53 | LinuxHostInit() { |
[email protected] | 576a4ca | 2009-11-05 01:41:09 | [diff] [blame] | 54 | RenderSandboxHostLinux* shost = Singleton<RenderSandboxHostLinux>::get(); |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 55 | shost->Init(""); |
[email protected] | 576a4ca | 2009-11-05 01:41:09 | [diff] [blame] | 56 | ZygoteHost* zhost = Singleton<ZygoteHost>::get(); |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 57 | zhost->Init(""); |
| 58 | } |
| 59 | ~LinuxHostInit() {} |
| 60 | }; |
| 61 | |
| 62 | } // namespace |
| 63 | #endif |
| 64 | |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 65 | extern int BrowserMain(const MainFunctionParams&); |
| 66 | |
| 67 | const wchar_t kUnitTestShowWindows[] = L"show-windows"; |
| 68 | |
[email protected] | ddf8a4b0 | 2010-03-22 23:08:30 | [diff] [blame] | 69 | // Passed as value of kTestType. |
| 70 | static const char kBrowserTestType[] = "browser"; |
| 71 | |
[email protected] | 4ff446f | 2009-07-10 18:29:39 | [diff] [blame] | 72 | // Default delay for the time-out at which we stop the |
| 73 | // inner-message loop the first time. |
[email protected] | b5f9510 | 2009-07-01 19:53:59 | [diff] [blame] | 74 | const int kInitialTimeoutInMS = 30000; |
| 75 | |
| 76 | // Delay for sub-sequent time-outs once the initial time-out happened. |
| 77 | const int kSubsequentTimeoutInMS = 5000; |
| 78 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 79 | InProcessBrowserTest::InProcessBrowserTest() |
| 80 | : browser_(NULL), |
| 81 | show_window_(false), |
[email protected] | 56cdae3 | 2009-03-12 19:58:20 | [diff] [blame] | 82 | dom_automation_enabled_(false), |
[email protected] | 0b4d338 | 2010-07-14 16:13:04 | [diff] [blame] | 83 | tab_closeable_state_watcher_enabled_(false), |
[email protected] | 4ff446f | 2009-07-10 18:29:39 | [diff] [blame] | 84 | original_single_process_(false), |
| 85 | initial_timeout_(kInitialTimeoutInMS) { |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 86 | } |
| 87 | |
[email protected] | c4ff495 | 2010-01-08 19:12:47 | [diff] [blame] | 88 | InProcessBrowserTest::~InProcessBrowserTest() { |
| 89 | } |
| 90 | |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 91 | void InProcessBrowserTest::SetUp() { |
| 92 | // Cleanup the user data dir. |
[email protected] | 23cc9a1 | 2009-07-30 21:27:01 | [diff] [blame] | 93 | FilePath user_data_dir; |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 94 | PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); |
[email protected] | 23cc9a1 | 2009-07-30 21:27:01 | [diff] [blame] | 95 | ASSERT_LT(10, static_cast<int>(user_data_dir.value().size())) << |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 96 | "The user data directory name passed into this test was too " |
| 97 | "short to delete safely. Please check the user-data-dir " |
| 98 | "argument and try again."; |
[email protected] | 298883bc | 2010-04-30 14:50:58 | [diff] [blame] | 99 | ASSERT_TRUE(file_util::DieFileDie(user_data_dir, true)); |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 100 | |
[email protected] | 4a44bc3 | 2010-05-28 22:22:44 | [diff] [blame] | 101 | // Recreate the user data dir. (PathService::Get guarantees that the directory |
| 102 | // exists if it returns true, but it only actually checks on the first call, |
| 103 | // the rest are cached. Thus we need to recreate it ourselves to not break |
| 104 | // the PathService guarantee.) |
| 105 | ASSERT_TRUE(file_util::CreateDirectory(user_data_dir)); |
| 106 | |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 107 | // The unit test suite creates a testingbrowser, but we want the real thing. |
| 108 | // Delete the current one. We'll install the testing one in TearDown. |
| 109 | delete g_browser_process; |
[email protected] | 298883bc | 2010-04-30 14:50:58 | [diff] [blame] | 110 | g_browser_process = NULL; |
| 111 | |
| 112 | SetUpUserDataDirectory(); |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 113 | |
| 114 | // Don't delete the resources when BrowserMain returns. Many ui classes |
| 115 | // cache SkBitmaps in a static field so that if we delete the resource |
| 116 | // bundle we'll crash. |
| 117 | browser_shutdown::delete_resources_on_shutdown = false; |
| 118 | |
[email protected] | 986088a6 | 2010-05-13 18:59:20 | [diff] [blame] | 119 | // Remember the command line. Normally this doesn't matter, because the test |
| 120 | // harness creates a new process for each test, but when the test harness is |
| 121 | // running in single process mode, we can't let one test's command-line |
| 122 | // changes (e.g. enabling DOM automation) affect other tests. |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 123 | CommandLine* command_line = CommandLine::ForCurrentProcessMutable(); |
[email protected] | 56cdae3 | 2009-03-12 19:58:20 | [diff] [blame] | 124 | original_command_line_.reset(new CommandLine(*command_line)); |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 125 | |
[email protected] | 9665fa6 | 2009-04-13 22:15:29 | [diff] [blame] | 126 | SetUpCommandLine(command_line); |
| 127 | |
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 128 | #if defined(OS_WIN) |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 129 | // Hide windows on show. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 130 | if (!command_line->HasSwitch(kUnitTestShowWindows) && !show_window_) |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 131 | BrowserView::SetShowState(SW_HIDE); |
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 132 | #endif |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 133 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 134 | if (dom_automation_enabled_) |
| 135 | command_line->AppendSwitch(switches::kDomAutomationController); |
| 136 | |
[email protected] | cdbc184 | 2009-08-26 02:56:58 | [diff] [blame] | 137 | // Turn off tip loading for tests; see http://crbug.com/17725 |
| 138 | command_line->AppendSwitch(switches::kDisableWebResources); |
[email protected] | 0a51926 | 2009-07-13 18:14:08 | [diff] [blame] | 139 | |
[email protected] | 4f08c83f | 2010-07-29 23:02:34 | [diff] [blame] | 140 | command_line->AppendSwitchPath(switches::kUserDataDir, user_data_dir); |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 141 | |
[email protected] | e01aba81 | 2009-07-09 18:31:24 | [diff] [blame] | 142 | // Don't show the first run ui. |
| 143 | command_line->AppendSwitch(switches::kNoFirstRun); |
| 144 | |
[email protected] | ddf8a4b0 | 2010-03-22 23:08:30 | [diff] [blame] | 145 | // This is a Browser test. |
[email protected] | 05076ba2 | 2010-07-30 05:59:57 | [diff] [blame] | 146 | command_line->AppendSwitchASCII(switches::kTestType, kBrowserTestType); |
[email protected] | ddf8a4b0 | 2010-03-22 23:08:30 | [diff] [blame] | 147 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 148 | // Single-process mode is not set in BrowserMain so it needs to be processed |
[email protected] | 298883bc | 2010-04-30 14:50:58 | [diff] [blame] | 149 | // explicitly. |
[email protected] | 56cdae3 | 2009-03-12 19:58:20 | [diff] [blame] | 150 | original_single_process_ = RenderProcessHost::run_renderer_in_process(); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 151 | if (command_line->HasSwitch(switches::kSingleProcess)) |
| 152 | RenderProcessHost::set_run_renderer_in_process(true); |
| 153 | |
[email protected] | 87bf974 | 2010-06-09 20:31:41 | [diff] [blame] | 154 | #if defined(OS_WIN) |
| 155 | // The Windows sandbox requires that the browser and child processes are the |
| 156 | // same binary. So we launch browser_process.exe which loads chrome.dll |
[email protected] | 4f08c83f | 2010-07-29 23:02:34 | [diff] [blame] | 157 | command_line->AppendSwitchPath(switches::kBrowserSubprocessPath, |
| 158 | command_line->GetProgram()); |
[email protected] | 87bf974 | 2010-06-09 20:31:41 | [diff] [blame] | 159 | #else |
| 160 | // Explicitly set the path of the binary used for child processes, otherwise |
| 161 | // they'll try to use browser_tests which doesn't contain ChromeMain. |
[email protected] | 6cea14d | 2009-09-10 18:19:18 | [diff] [blame] | 162 | FilePath subprocess_path; |
[email protected] | 7f74a4e | 2009-04-30 17:00:24 | [diff] [blame] | 163 | PathService::Get(base::FILE_EXE, &subprocess_path); |
[email protected] | 6cea14d | 2009-09-10 18:19:18 | [diff] [blame] | 164 | subprocess_path = subprocess_path.DirName(); |
| 165 | subprocess_path = subprocess_path.AppendASCII(WideToASCII( |
| 166 | chrome::kBrowserProcessExecutablePath)); |
[email protected] | 4f08c83f | 2010-07-29 23:02:34 | [diff] [blame] | 167 | command_line->AppendSwitchPath(switches::kBrowserSubprocessPath, |
| 168 | subprocess_path); |
[email protected] | 87bf974 | 2010-06-09 20:31:41 | [diff] [blame] | 169 | #endif |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 170 | |
[email protected] | 9c73efa | 2009-07-08 00:18:36 | [diff] [blame] | 171 | // Enable warning level logging so that we can see when bad stuff happens. |
| 172 | command_line->AppendSwitch(switches::kEnableLogging); |
[email protected] | 0c605fe3 | 2010-07-30 06:00:38 | [diff] [blame] | 173 | command_line->AppendSwitchASCII(switches::kLoggingLevel, "1"); // warning |
[email protected] | 9c73efa | 2009-07-08 00:18:36 | [diff] [blame] | 174 | |
[email protected] | 0b4d338 | 2010-07-14 16:13:04 | [diff] [blame] | 175 | // If ncecessary, disable TabCloseableStateWatcher. |
| 176 | if (!tab_closeable_state_watcher_enabled_) |
| 177 | command_line->AppendSwitch(switches::kDisableTabCloseableStateWatcher); |
| 178 | |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 179 | SandboxInitWrapper sandbox_wrapper; |
[email protected] | 7c32108 | 2009-02-09 15:35:47 | [diff] [blame] | 180 | MainFunctionParams params(*command_line, sandbox_wrapper, NULL); |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 181 | params.ui_task = |
| 182 | NewRunnableMethod(this, &InProcessBrowserTest::RunTestOnMainThreadLoop); |
[email protected] | 13324ed | 2009-04-03 05:14:19 | [diff] [blame] | 183 | |
[email protected] | c4ff495 | 2010-01-08 19:12:47 | [diff] [blame] | 184 | host_resolver_ = new net::RuleBasedHostResolverProc( |
| 185 | new IntranetRedirectHostResolverProc(NULL)); |
[email protected] | c7ad50f | 2009-09-11 06:28:15 | [diff] [blame] | 186 | |
| 187 | // Something inside the browser does this lookup implicitly. Make it fail |
| 188 | // to avoid external dependency. It won't break the tests. |
| 189 | host_resolver_->AddSimulatedFailure("*.google.com"); |
| 190 | |
| 191 | // See http://en.wikipedia.org/wiki/Web_Proxy_Autodiscovery_Protocol |
| 192 | // We don't want the test code to use it. |
| 193 | host_resolver_->AddSimulatedFailure("wpad"); |
| 194 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 195 | net::ScopedDefaultHostResolverProc scoped_host_resolver_proc( |
[email protected] | c7ad50f | 2009-09-11 06:28:15 | [diff] [blame] | 196 | host_resolver_.get()); |
[email protected] | 5c5de8c | 2009-09-23 17:11:26 | [diff] [blame] | 197 | |
| 198 | SetUpInProcessBrowserTestFixture(); |
[email protected] | 60a782e | 2010-02-24 22:41:35 | [diff] [blame] | 199 | |
| 200 | // Before we run the browser, we have to hack the path to the exe to match |
| 201 | // what it would be if Chrome was running, because it is used to fork renderer |
| 202 | // processes, on Linux at least (failure to do so will cause a browser_test to |
| 203 | // be run instead of a renderer). |
| 204 | FilePath chrome_path; |
| 205 | CHECK(PathService::Get(base::FILE_EXE, &chrome_path)); |
| 206 | chrome_path = chrome_path.DirName(); |
| 207 | #if defined(OS_WIN) |
| 208 | chrome_path = chrome_path.Append(chrome::kBrowserProcessExecutablePath); |
| 209 | #elif defined(OS_POSIX) |
| 210 | chrome_path = chrome_path.Append( |
| 211 | WideToASCII(chrome::kBrowserProcessExecutablePath)); |
| 212 | #endif |
| 213 | CHECK(PathService::Override(base::FILE_EXE, chrome_path)); |
| 214 | |
| 215 | #if defined(OS_LINUX) |
| 216 | // Initialize the RenderSandbox and Zygote hosts. Apparently they get used |
| 217 | // for InProcessBrowserTest, and this is not the normal browser startup path. |
| 218 | Singleton<LinuxHostInit>::get(); |
| 219 | #endif |
| 220 | |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 221 | BrowserMain(params); |
[email protected] | 5c5de8c | 2009-09-23 17:11:26 | [diff] [blame] | 222 | TearDownInProcessBrowserTestFixture(); |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | void InProcessBrowserTest::TearDown() { |
| 226 | // Reinstall testing browser process. |
| 227 | delete g_browser_process; |
| 228 | g_browser_process = new TestingBrowserProcess(); |
| 229 | |
| 230 | browser_shutdown::delete_resources_on_shutdown = true; |
| 231 | |
[email protected] | eb1bd83 | 2010-05-18 20:39:58 | [diff] [blame] | 232 | #if defined(OS_WIN) |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 233 | BrowserView::SetShowState(-1); |
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 234 | #endif |
[email protected] | 56cdae3 | 2009-03-12 19:58:20 | [diff] [blame] | 235 | |
| 236 | *CommandLine::ForCurrentProcessMutable() = *original_command_line_; |
| 237 | RenderProcessHost::set_run_renderer_in_process(original_single_process_); |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 238 | } |
| 239 | |
[email protected] | 3985ba8 | 2010-07-29 21:44:12 | [diff] [blame] | 240 | net::HTTPTestServer* InProcessBrowserTest::StartHTTPServer() { |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 241 | DCHECK(!http_server_.get()); |
[email protected] | 3985ba8 | 2010-07-29 21:44:12 | [diff] [blame] | 242 | http_server_ = net::HTTPTestServer::CreateServer( |
[email protected] | e70c6a8 | 2010-07-23 20:38:56 | [diff] [blame] | 243 | L"chrome/test/data"); |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 244 | return http_server_.get(); |
| 245 | } |
| 246 | |
| 247 | // Creates a browser with a single tab (about:blank), waits for the tab to |
| 248 | // finish loading and shows the browser. |
| 249 | Browser* InProcessBrowserTest::CreateBrowser(Profile* profile) { |
| 250 | Browser* browser = Browser::Create(profile); |
| 251 | |
[email protected] | e0d48158 | 2009-09-15 21:06:25 | [diff] [blame] | 252 | browser->AddTabWithURL(GURL(chrome::kAboutBlankURL), GURL(), |
[email protected] | 715af7e | 2010-04-29 01:55:38 | [diff] [blame] | 253 | PageTransition::START_PAGE, -1, |
[email protected] | 4a166544 | 2010-06-28 16:09:39 | [diff] [blame] | 254 | TabStripModel::ADD_SELECTED, NULL, std::string()); |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 255 | |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 256 | // Wait for the page to finish loading. |
| 257 | ui_test_utils::WaitForNavigation( |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 258 | &browser->GetSelectedTabContents()->controller()); |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 259 | |
| 260 | browser->window()->Show(); |
| 261 | |
| 262 | return browser; |
| 263 | } |
| 264 | |
[email protected] | 64f1931 | 2010-04-13 22:30:01 | [diff] [blame] | 265 | void InProcessBrowserTest::RunTestOnMainThreadLoop() { |
[email protected] | 304a317 | 2010-05-04 05:38:44 | [diff] [blame] | 266 | // On Mac, without the following autorelease pool, code which is directly |
| 267 | // executed (as opposed to executed inside a message loop) would autorelease |
| 268 | // objects into a higher-level pool. This pool is not recycled in-sync with |
| 269 | // the message loops' pools and causes problems with code relying on |
| 270 | // deallocation via an autorelease pool (such as browser window closure and |
| 271 | // browser shutdown). To avoid this, the following pool is recycled after each |
| 272 | // time code is directly executed. |
| 273 | base::ScopedNSAutoreleasePool pool; |
| 274 | |
[email protected] | 64f1931 | 2010-04-13 22:30:01 | [diff] [blame] | 275 | // Pump startup related events. |
| 276 | MessageLoopForUI::current()->RunAllPending(); |
| 277 | |
| 278 | // In the long term it would be great if we could use a TestingProfile |
| 279 | // here and only enable services you want tested, but that requires all |
| 280 | // consumers of Profile to handle NULL services. |
| 281 | Profile* profile = ProfileManager::GetDefaultProfile(); |
| 282 | if (!profile) { |
| 283 | // We should only be able to get here if the profile already exists and |
| 284 | // has been created. |
| 285 | NOTREACHED(); |
| 286 | return; |
| 287 | } |
[email protected] | 304a317 | 2010-05-04 05:38:44 | [diff] [blame] | 288 | pool.Recycle(); |
[email protected] | 64f1931 | 2010-04-13 22:30:01 | [diff] [blame] | 289 | |
| 290 | ChromeThread::PostTask( |
| 291 | ChromeThread::IO, FROM_HERE, |
| 292 | NewRunnableFunction(chrome_browser_net::SetUrlRequestMocksEnabled, true)); |
| 293 | |
| 294 | browser_ = CreateBrowser(profile); |
[email protected] | 304a317 | 2010-05-04 05:38:44 | [diff] [blame] | 295 | pool.Recycle(); |
[email protected] | 64f1931 | 2010-04-13 22:30:01 | [diff] [blame] | 296 | |
| 297 | // Start the timeout timer to prevent hangs. |
| 298 | MessageLoopForUI::current()->PostDelayedTask(FROM_HERE, |
| 299 | NewRunnableMethod(this, &InProcessBrowserTest::TimedOut), |
| 300 | initial_timeout_); |
| 301 | |
| 302 | // Pump any pending events that were created as a result of creating a |
| 303 | // browser. |
| 304 | MessageLoopForUI::current()->RunAllPending(); |
| 305 | |
| 306 | RunTestOnMainThread(); |
[email protected] | 304a317 | 2010-05-04 05:38:44 | [diff] [blame] | 307 | pool.Recycle(); |
[email protected] | 64f1931 | 2010-04-13 22:30:01 | [diff] [blame] | 308 | |
| 309 | CleanUpOnMainThread(); |
[email protected] | 304a317 | 2010-05-04 05:38:44 | [diff] [blame] | 310 | pool.Recycle(); |
[email protected] | 64f1931 | 2010-04-13 22:30:01 | [diff] [blame] | 311 | |
| 312 | QuitBrowsers(); |
[email protected] | 304a317 | 2010-05-04 05:38:44 | [diff] [blame] | 313 | pool.Recycle(); |
[email protected] | 64f1931 | 2010-04-13 22:30:01 | [diff] [blame] | 314 | |
| 315 | // Stop the HTTP server. |
| 316 | http_server_ = NULL; |
| 317 | } |
| 318 | |
| 319 | void InProcessBrowserTest::QuitBrowsers() { |
| 320 | if (BrowserList::size() == 0) |
| 321 | return; |
| 322 | |
| 323 | // Invoke CloseAllBrowsersAndExit on a running message loop. |
| 324 | // CloseAllBrowsersAndExit exits the message loop after everything has been |
| 325 | // shut down properly. |
| 326 | MessageLoopForUI::current()->PostTask( |
| 327 | FROM_HERE, |
| 328 | NewRunnableFunction(&BrowserList::CloseAllBrowsersAndExit)); |
| 329 | ui_test_utils::RunMessageLoop(); |
| 330 | } |
[email protected] | b5f9510 | 2009-07-01 19:53:59 | [diff] [blame] | 331 | |
[email protected] | 5c00ca8 | 2010-04-08 21:30:42 | [diff] [blame] | 332 | void InProcessBrowserTest::TimedOut() { |
[email protected] | 94ab2ad | 2009-10-15 18:18:51 | [diff] [blame] | 333 | std::string error_message = "Test timed out. Each test runs for a max of "; |
[email protected] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame^] | 334 | error_message += base::IntToString(initial_timeout_); |
[email protected] | 94ab2ad | 2009-10-15 18:18:51 | [diff] [blame] | 335 | error_message += " ms (kInitialTimeoutInMS)."; |
| 336 | |
| 337 | GTEST_NONFATAL_FAILURE_(error_message.c_str()); |
[email protected] | b5f9510 | 2009-07-01 19:53:59 | [diff] [blame] | 338 | |
[email protected] | b5f9510 | 2009-07-01 19:53:59 | [diff] [blame] | 339 | MessageLoopForUI::current()->Quit(); |
| 340 | } |
[email protected] | 4ff446f | 2009-07-10 18:29:39 | [diff] [blame] | 341 | |
| 342 | void InProcessBrowserTest::SetInitialTimeoutInMS(int timeout_value) { |
| 343 | DCHECK_GT(timeout_value, 0); |
| 344 | initial_timeout_ = timeout_value; |
| 345 | } |