[email protected] | 9e790bd | 2011-01-10 23:48:54 | [diff] [blame] | 1 | // Copyright (c) 2011 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" |
[email protected] | c2818d4 | 2010-10-18 02:47:39 | [diff] [blame] | 10 | #include "base/mac/scoped_nsautorelease_pool.h" |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 11 | #include "base/path_service.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] | b6e38ef | 2009-06-16 00:43:23 | [diff] [blame] | 14 | #include "chrome/browser/browser_list.h" |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 15 | #include "chrome/browser/browser_process.h" |
| 16 | #include "chrome/browser/browser_shutdown.h" |
[email protected] | 8281e48e | 2010-10-12 18:54:49 | [diff] [blame] | 17 | #include "chrome/browser/browser_thread.h" |
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 18 | #include "chrome/browser/browser_window.h" |
[email protected] | c4ff495 | 2010-01-08 19:12:47 | [diff] [blame] | 19 | #include "chrome/browser/intranet_redirect_detector.h" |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 20 | #include "chrome/browser/io_thread.h" |
[email protected] | cec4a27 | 2009-07-31 21:55:03 | [diff] [blame] | 21 | #include "chrome/browser/net/url_request_mock_util.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 22 | #include "chrome/browser/profiles/profile.h" |
| 23 | #include "chrome/browser/profiles/profile_manager.h" |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 24 | #include "chrome/browser/renderer_host/render_process_host.h" |
[email protected] | 5c23875 | 2009-06-13 10:29:07 | [diff] [blame] | 25 | #include "chrome/browser/tab_contents/tab_contents.h" |
[email protected] | c848d3d9 | 2010-09-16 21:57:45 | [diff] [blame] | 26 | #include "chrome/browser/tabs/tab_strip_model.h" |
[email protected] | f46be6e | 2010-11-16 03:52:32 | [diff] [blame] | 27 | #include "chrome/browser/ui/browser.h" |
[email protected] | 339d6dd | 2010-11-12 00:41:58 | [diff] [blame] | 28 | #include "chrome/browser/ui/browser_navigator.h" |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 29 | #include "chrome/common/chrome_constants.h" |
| 30 | #include "chrome/common/chrome_paths.h" |
| 31 | #include "chrome/common/chrome_switches.h" |
[email protected] | 0750b144 | 2010-11-02 21:59:37 | [diff] [blame] | 32 | #include "chrome/common/logging_chrome.h" |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 33 | #include "chrome/common/main_function_params.h" |
[email protected] | ad1f9bd | 2009-07-30 20:23:15 | [diff] [blame] | 34 | #include "chrome/common/notification_type.h" |
[email protected] | e0d48158 | 2009-09-15 21:06:25 | [diff] [blame] | 35 | #include "chrome/common/url_constants.h" |
[email protected] | 470f7a5 | 2010-09-23 20:20:38 | [diff] [blame] | 36 | #include "chrome/test/test_launcher_utils.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] | 7fac888 | 2010-11-15 19:52:52 | [diff] [blame] | 43 | #if defined(OS_MACOSX) |
[email protected] | 0378bf4 | 2011-01-01 18:20:14 | [diff] [blame] | 44 | #include "base/mac/mac_util.h" |
[email protected] | 7fac888 | 2010-11-15 19:52:52 | [diff] [blame] | 45 | #endif |
| 46 | |
| 47 | #if defined(OS_WIN) |
[email protected] | 9e790bd | 2011-01-10 23:48:54 | [diff] [blame] | 48 | #include "chrome/browser/ui/views/frame/browser_view.h" |
[email protected] | 7fac888 | 2010-11-15 19:52:52 | [diff] [blame] | 49 | #endif |
| 50 | |
[email protected] | e1c10f18 | 2010-09-30 08:53:06 | [diff] [blame] | 51 | namespace { |
| 52 | |
| 53 | void InitializeBrowser(Browser* browser) { |
[email protected] | cecc93a | 2010-10-05 15:58:55 | [diff] [blame] | 54 | browser->AddSelectedTabWithURL(GURL(chrome::kAboutBlankURL), |
| 55 | PageTransition::START_PAGE); |
[email protected] | e1c10f18 | 2010-09-30 08:53:06 | [diff] [blame] | 56 | |
| 57 | // Wait for the page to finish loading. |
| 58 | ui_test_utils::WaitForNavigation( |
| 59 | &browser->GetSelectedTabContents()->controller()); |
| 60 | |
| 61 | browser->window()->Show(); |
| 62 | } |
| 63 | |
| 64 | } // namespace |
| 65 | |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 66 | extern int BrowserMain(const MainFunctionParams&); |
| 67 | |
[email protected] | 711a353 | 2010-12-08 22:18:37 | [diff] [blame] | 68 | const char kUnitTestShowWindows[] = "show-windows"; |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 69 | |
[email protected] | ddf8a4b0 | 2010-03-22 23:08:30 | [diff] [blame] | 70 | // Passed as value of kTestType. |
| 71 | static const char kBrowserTestType[] = "browser"; |
| 72 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 73 | InProcessBrowserTest::InProcessBrowserTest() |
| 74 | : browser_(NULL), |
| 75 | show_window_(false), |
[email protected] | 56cdae3 | 2009-03-12 19:58:20 | [diff] [blame] | 76 | dom_automation_enabled_(false), |
[email protected] | 0b4d338 | 2010-07-14 16:13:04 | [diff] [blame] | 77 | tab_closeable_state_watcher_enabled_(false), |
[email protected] | 4b614a6 | 2010-10-06 15:33:10 | [diff] [blame] | 78 | original_single_process_(false) { |
[email protected] | 7fac888 | 2010-11-15 19:52:52 | [diff] [blame] | 79 | #if defined(OS_MACOSX) |
[email protected] | 0378bf4 | 2011-01-01 18:20:14 | [diff] [blame] | 80 | base::mac::SetOverrideAmIBundled(true); |
[email protected] | 7fac888 | 2010-11-15 19:52:52 | [diff] [blame] | 81 | #endif |
| 82 | |
| 83 | // Before we run the browser, we have to hack the path to the exe to match |
| 84 | // what it would be if Chrome was running, because it is used to fork renderer |
| 85 | // processes, on Linux at least (failure to do so will cause a browser_test to |
| 86 | // be run instead of a renderer). |
| 87 | FilePath chrome_path; |
| 88 | CHECK(PathService::Get(base::FILE_EXE, &chrome_path)); |
| 89 | chrome_path = chrome_path.DirName(); |
[email protected] | 7fac888 | 2010-11-15 19:52:52 | [diff] [blame] | 90 | chrome_path = chrome_path.Append(chrome::kBrowserProcessExecutablePath); |
[email protected] | 7fac888 | 2010-11-15 19:52:52 | [diff] [blame] | 91 | CHECK(PathService::Override(base::FILE_EXE, chrome_path)); |
| 92 | |
| 93 | test_server_.reset(new net::TestServer( |
| 94 | net::TestServer::TYPE_HTTP, |
| 95 | FilePath(FILE_PATH_LITERAL("chrome/test/data")))); |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 96 | } |
| 97 | |
[email protected] | c4ff495 | 2010-01-08 19:12:47 | [diff] [blame] | 98 | InProcessBrowserTest::~InProcessBrowserTest() { |
| 99 | } |
| 100 | |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 101 | void InProcessBrowserTest::SetUp() { |
[email protected] | 470f7a5 | 2010-09-23 20:20:38 | [diff] [blame] | 102 | // Remember the command line. Normally this doesn't matter, because the test |
| 103 | // harness creates a new process for each test, but when the test harness is |
| 104 | // running in single process mode, we can't let one test's command-line |
| 105 | // changes (e.g. enabling DOM automation) affect other tests. |
| 106 | // TODO(phajdan.jr): This save/restore logic is unnecessary. Remove it. |
| 107 | CommandLine* command_line = CommandLine::ForCurrentProcessMutable(); |
| 108 | original_command_line_.reset(new CommandLine(*command_line)); |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 109 | |
[email protected] | fc82ccf0 | 2010-10-15 18:12:47 | [diff] [blame] | 110 | // Create a temporary user data directory if required. |
| 111 | ASSERT_TRUE(CreateUserDataDirectory()) |
| 112 | << "Could not create user data directory."; |
[email protected] | 4a44bc3 | 2010-05-28 22:22:44 | [diff] [blame] | 113 | |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 114 | // The unit test suite creates a testingbrowser, but we want the real thing. |
| 115 | // Delete the current one. We'll install the testing one in TearDown. |
| 116 | delete g_browser_process; |
[email protected] | 298883bc | 2010-04-30 14:50:58 | [diff] [blame] | 117 | g_browser_process = NULL; |
| 118 | |
[email protected] | 470f7a5 | 2010-09-23 20:20:38 | [diff] [blame] | 119 | // Allow subclasses the opportunity to make changes to the default user data |
| 120 | // dir before running any tests. |
[email protected] | fc82ccf0 | 2010-10-15 18:12:47 | [diff] [blame] | 121 | ASSERT_TRUE(SetUpUserDataDirectory()) |
| 122 | << "Could not set up user data directory."; |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 123 | |
| 124 | // Don't delete the resources when BrowserMain returns. Many ui classes |
| 125 | // cache SkBitmaps in a static field so that if we delete the resource |
| 126 | // bundle we'll crash. |
| 127 | browser_shutdown::delete_resources_on_shutdown = false; |
| 128 | |
[email protected] | 470f7a5 | 2010-09-23 20:20:38 | [diff] [blame] | 129 | // Allow subclasses the opportunity to make changes to the command line before |
| 130 | // running any tests. |
[email protected] | 9665fa6 | 2009-04-13 22:15:29 | [diff] [blame] | 131 | SetUpCommandLine(command_line); |
[email protected] | 03c79d50 | 2010-11-16 00:38:26 | [diff] [blame] | 132 | // Add command line arguments that are used by all InProcessBrowserTests. |
| 133 | PrepareTestCommandLine(command_line); |
[email protected] | 9665fa6 | 2009-04-13 22:15:29 | [diff] [blame] | 134 | |
[email protected] | 03c79d50 | 2010-11-16 00:38:26 | [diff] [blame] | 135 | // Save the single process mode state before it was reset in this test. This |
| 136 | // state will be recovered in TearDown(). Single-process mode is not set in |
| 137 | // BrowserMain so it needs to be processed explicitly. |
[email protected] | 56cdae3 | 2009-03-12 19:58:20 | [diff] [blame] | 138 | original_single_process_ = RenderProcessHost::run_renderer_in_process(); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 139 | if (command_line->HasSwitch(switches::kSingleProcess)) |
| 140 | RenderProcessHost::set_run_renderer_in_process(true); |
| 141 | |
[email protected] | 02dbca0b | 2010-09-17 07:44:10 | [diff] [blame] | 142 | #if defined(OS_CHROMEOS) |
[email protected] | 0750b144 | 2010-11-02 21:59:37 | [diff] [blame] | 143 | // Make sure that the log directory exists. |
| 144 | FilePath log_dir = logging::GetSessionLogFile(*command_line).DirName(); |
| 145 | file_util::CreateDirectory(log_dir); |
[email protected] | 02dbca0b | 2010-09-17 07:44:10 | [diff] [blame] | 146 | #endif // defined(OS_CHROMEOS) |
| 147 | |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 148 | SandboxInitWrapper sandbox_wrapper; |
[email protected] | 7c32108 | 2009-02-09 15:35:47 | [diff] [blame] | 149 | MainFunctionParams params(*command_line, sandbox_wrapper, NULL); |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 150 | params.ui_task = |
| 151 | NewRunnableMethod(this, &InProcessBrowserTest::RunTestOnMainThreadLoop); |
[email protected] | 13324ed | 2009-04-03 05:14:19 | [diff] [blame] | 152 | |
[email protected] | c4ff495 | 2010-01-08 19:12:47 | [diff] [blame] | 153 | host_resolver_ = new net::RuleBasedHostResolverProc( |
| 154 | new IntranetRedirectHostResolverProc(NULL)); |
[email protected] | c7ad50f | 2009-09-11 06:28:15 | [diff] [blame] | 155 | |
| 156 | // Something inside the browser does this lookup implicitly. Make it fail |
| 157 | // to avoid external dependency. It won't break the tests. |
| 158 | host_resolver_->AddSimulatedFailure("*.google.com"); |
| 159 | |
| 160 | // See http://en.wikipedia.org/wiki/Web_Proxy_Autodiscovery_Protocol |
| 161 | // We don't want the test code to use it. |
| 162 | host_resolver_->AddSimulatedFailure("wpad"); |
| 163 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 164 | net::ScopedDefaultHostResolverProc scoped_host_resolver_proc( |
[email protected] | c7ad50f | 2009-09-11 06:28:15 | [diff] [blame] | 165 | host_resolver_.get()); |
[email protected] | 5c5de8c | 2009-09-23 17:11:26 | [diff] [blame] | 166 | |
| 167 | SetUpInProcessBrowserTestFixture(); |
[email protected] | 60a782e | 2010-02-24 22:41:35 | [diff] [blame] | 168 | |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 169 | BrowserMain(params); |
[email protected] | 5c5de8c | 2009-09-23 17:11:26 | [diff] [blame] | 170 | TearDownInProcessBrowserTestFixture(); |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 171 | } |
| 172 | |
[email protected] | 03c79d50 | 2010-11-16 00:38:26 | [diff] [blame] | 173 | void InProcessBrowserTest::PrepareTestCommandLine( |
| 174 | CommandLine* command_line) { |
| 175 | // Propagate commandline settings from test_launcher_utils. |
| 176 | test_launcher_utils::PrepareBrowserCommandLineForTests(command_line); |
| 177 | |
| 178 | #if defined(OS_WIN) |
| 179 | // Hide windows on show. |
| 180 | if (!command_line->HasSwitch(kUnitTestShowWindows) && !show_window_) |
| 181 | BrowserView::SetShowState(SW_HIDE); |
| 182 | #endif |
| 183 | |
| 184 | if (dom_automation_enabled_) |
| 185 | command_line->AppendSwitch(switches::kDomAutomationController); |
| 186 | |
| 187 | // This is a Browser test. |
| 188 | command_line->AppendSwitchASCII(switches::kTestType, kBrowserTestType); |
| 189 | |
| 190 | #if defined(OS_WIN) |
| 191 | // The Windows sandbox requires that the browser and child processes are the |
| 192 | // same binary. So we launch browser_process.exe which loads chrome.dll |
| 193 | command_line->AppendSwitchPath(switches::kBrowserSubprocessPath, |
| 194 | command_line->GetProgram()); |
| 195 | #else |
| 196 | // Explicitly set the path of the binary used for child processes, otherwise |
| 197 | // they'll try to use browser_tests which doesn't contain ChromeMain. |
| 198 | FilePath subprocess_path; |
| 199 | PathService::Get(base::FILE_EXE, &subprocess_path); |
| 200 | #if defined(OS_MACOSX) |
| 201 | // Recreate the real environment, run the helper within the app bundle. |
| 202 | subprocess_path = subprocess_path.DirName().DirName(); |
| 203 | DCHECK_EQ(subprocess_path.BaseName().value(), "Contents"); |
| 204 | subprocess_path = |
| 205 | subprocess_path.Append("Versions").Append(chrome::kChromeVersion); |
| 206 | subprocess_path = |
| 207 | subprocess_path.Append(chrome::kHelperProcessExecutablePath); |
| 208 | #endif |
| 209 | command_line->AppendSwitchPath(switches::kBrowserSubprocessPath, |
| 210 | subprocess_path); |
| 211 | #endif |
| 212 | |
[email protected] | 7c8b2067 | 2011-02-11 21:18:01 | [diff] [blame] | 213 | // If neccessary, disable TabCloseableStateWatcher. |
[email protected] | 03c79d50 | 2010-11-16 00:38:26 | [diff] [blame] | 214 | if (!tab_closeable_state_watcher_enabled_) |
| 215 | command_line->AppendSwitch(switches::kDisableTabCloseableStateWatcher); |
| 216 | } |
| 217 | |
[email protected] | fc82ccf0 | 2010-10-15 18:12:47 | [diff] [blame] | 218 | bool InProcessBrowserTest::CreateUserDataDirectory() { |
| 219 | CommandLine* command_line = CommandLine::ForCurrentProcessMutable(); |
| 220 | FilePath user_data_dir = |
| 221 | command_line->GetSwitchValuePath(switches::kUserDataDir); |
| 222 | if (user_data_dir.empty()) { |
| 223 | if (temp_user_data_dir_.CreateUniqueTempDir() && |
| 224 | temp_user_data_dir_.IsValid()) { |
| 225 | user_data_dir = temp_user_data_dir_.path(); |
| 226 | } else { |
| 227 | LOG(ERROR) << "Could not create temporary user data directory \"" |
| 228 | << temp_user_data_dir_.path().value() << "\"."; |
| 229 | return false; |
| 230 | } |
| 231 | } |
| 232 | return test_launcher_utils::OverrideUserDataDir(user_data_dir); |
| 233 | } |
| 234 | |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 235 | void InProcessBrowserTest::TearDown() { |
| 236 | // Reinstall testing browser process. |
| 237 | delete g_browser_process; |
| 238 | g_browser_process = new TestingBrowserProcess(); |
| 239 | |
| 240 | browser_shutdown::delete_resources_on_shutdown = true; |
| 241 | |
[email protected] | eb1bd83 | 2010-05-18 20:39:58 | [diff] [blame] | 242 | #if defined(OS_WIN) |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 243 | BrowserView::SetShowState(-1); |
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 244 | #endif |
[email protected] | 56cdae3 | 2009-03-12 19:58:20 | [diff] [blame] | 245 | |
| 246 | *CommandLine::ForCurrentProcessMutable() = *original_command_line_; |
| 247 | RenderProcessHost::set_run_renderer_in_process(original_single_process_); |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 248 | } |
| 249 | |
[email protected] | 616381f0 | 2010-11-02 15:15:33 | [diff] [blame] | 250 | void InProcessBrowserTest::AddTabAtIndexToBrowser( |
| 251 | Browser* browser, |
| 252 | int index, |
| 253 | const GURL& url, |
| 254 | PageTransition::Type transition) { |
| 255 | browser::NavigateParams params(browser, url, transition); |
| 256 | params.tabstrip_index = index; |
| 257 | params.disposition = NEW_FOREGROUND_TAB; |
| 258 | browser::Navigate(¶ms); |
| 259 | } |
| 260 | |
| 261 | void InProcessBrowserTest::AddTabAtIndex( |
| 262 | int index, |
| 263 | const GURL& url, |
| 264 | PageTransition::Type transition) { |
| 265 | AddTabAtIndexToBrowser(browser(), index, url, transition); |
| 266 | } |
| 267 | |
[email protected] | fadc607b6 | 2011-02-07 17:55:50 | [diff] [blame] | 268 | bool InProcessBrowserTest::SetUpUserDataDirectory() { |
| 269 | return true; |
| 270 | } |
| 271 | |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 272 | // Creates a browser with a single tab (about:blank), waits for the tab to |
| 273 | // finish loading and shows the browser. |
| 274 | Browser* InProcessBrowserTest::CreateBrowser(Profile* profile) { |
| 275 | Browser* browser = Browser::Create(profile); |
[email protected] | e1c10f18 | 2010-09-30 08:53:06 | [diff] [blame] | 276 | InitializeBrowser(browser); |
| 277 | return browser; |
| 278 | } |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 279 | |
[email protected] | 46f979d | 2011-01-10 17:08:44 | [diff] [blame] | 280 | Browser* InProcessBrowserTest::CreateIncognitoBrowser() { |
| 281 | // Create a new browser with using the incognito profile. |
| 282 | Browser* incognito = |
| 283 | Browser::Create(browser()->profile()->GetOffTheRecordProfile()); |
| 284 | InitializeBrowser(incognito); |
| 285 | return incognito; |
| 286 | } |
| 287 | |
[email protected] | e1c10f18 | 2010-09-30 08:53:06 | [diff] [blame] | 288 | Browser* InProcessBrowserTest::CreateBrowserForPopup(Profile* profile) { |
[email protected] | 1b74d212 | 2010-10-06 16:49:16 | [diff] [blame] | 289 | Browser* browser = Browser::CreateForType(Browser::TYPE_POPUP, profile); |
[email protected] | e1c10f18 | 2010-09-30 08:53:06 | [diff] [blame] | 290 | InitializeBrowser(browser); |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 291 | return browser; |
| 292 | } |
| 293 | |
[email protected] | 64f1931 | 2010-04-13 22:30:01 | [diff] [blame] | 294 | void InProcessBrowserTest::RunTestOnMainThreadLoop() { |
[email protected] | 00d0ac2 | 2010-10-05 09:30:00 | [diff] [blame] | 295 | #if defined(OS_POSIX) |
| 296 | // Restore default signal handler for SIGTERM, so when the out-of-process |
| 297 | // test runner tries to terminate us, we don't catch it and possibly make it |
| 298 | // look like a success (http://crbug.com/57578). |
| 299 | signal(SIGTERM, SIG_DFL); |
| 300 | #endif // defined(OS_POSIX) |
| 301 | |
[email protected] | 304a317 | 2010-05-04 05:38:44 | [diff] [blame] | 302 | // On Mac, without the following autorelease pool, code which is directly |
| 303 | // executed (as opposed to executed inside a message loop) would autorelease |
| 304 | // objects into a higher-level pool. This pool is not recycled in-sync with |
| 305 | // the message loops' pools and causes problems with code relying on |
| 306 | // deallocation via an autorelease pool (such as browser window closure and |
| 307 | // browser shutdown). To avoid this, the following pool is recycled after each |
| 308 | // time code is directly executed. |
[email protected] | c2818d4 | 2010-10-18 02:47:39 | [diff] [blame] | 309 | base::mac::ScopedNSAutoreleasePool pool; |
[email protected] | 304a317 | 2010-05-04 05:38:44 | [diff] [blame] | 310 | |
[email protected] | 64f1931 | 2010-04-13 22:30:01 | [diff] [blame] | 311 | // Pump startup related events. |
| 312 | MessageLoopForUI::current()->RunAllPending(); |
| 313 | |
| 314 | // In the long term it would be great if we could use a TestingProfile |
| 315 | // here and only enable services you want tested, but that requires all |
| 316 | // consumers of Profile to handle NULL services. |
| 317 | Profile* profile = ProfileManager::GetDefaultProfile(); |
| 318 | if (!profile) { |
| 319 | // We should only be able to get here if the profile already exists and |
| 320 | // has been created. |
| 321 | NOTREACHED(); |
| 322 | return; |
| 323 | } |
[email protected] | 304a317 | 2010-05-04 05:38:44 | [diff] [blame] | 324 | pool.Recycle(); |
[email protected] | 64f1931 | 2010-04-13 22:30:01 | [diff] [blame] | 325 | |
[email protected] | 0c7d74f | 2010-10-11 11:55:26 | [diff] [blame] | 326 | BrowserThread::PostTask( |
| 327 | BrowserThread::IO, FROM_HERE, |
[email protected] | 64f1931 | 2010-04-13 22:30:01 | [diff] [blame] | 328 | NewRunnableFunction(chrome_browser_net::SetUrlRequestMocksEnabled, true)); |
| 329 | |
| 330 | browser_ = CreateBrowser(profile); |
[email protected] | 304a317 | 2010-05-04 05:38:44 | [diff] [blame] | 331 | pool.Recycle(); |
[email protected] | 64f1931 | 2010-04-13 22:30:01 | [diff] [blame] | 332 | |
[email protected] | 64f1931 | 2010-04-13 22:30:01 | [diff] [blame] | 333 | // Pump any pending events that were created as a result of creating a |
| 334 | // browser. |
| 335 | MessageLoopForUI::current()->RunAllPending(); |
| 336 | |
[email protected] | 28dabab7 | 2011-01-05 03:31:26 | [diff] [blame] | 337 | SetUpOnMainThread(); |
| 338 | pool.Recycle(); |
| 339 | |
[email protected] | 64f1931 | 2010-04-13 22:30:01 | [diff] [blame] | 340 | RunTestOnMainThread(); |
[email protected] | 304a317 | 2010-05-04 05:38:44 | [diff] [blame] | 341 | pool.Recycle(); |
[email protected] | 64f1931 | 2010-04-13 22:30:01 | [diff] [blame] | 342 | |
| 343 | CleanUpOnMainThread(); |
[email protected] | 304a317 | 2010-05-04 05:38:44 | [diff] [blame] | 344 | pool.Recycle(); |
[email protected] | 64f1931 | 2010-04-13 22:30:01 | [diff] [blame] | 345 | |
| 346 | QuitBrowsers(); |
[email protected] | 304a317 | 2010-05-04 05:38:44 | [diff] [blame] | 347 | pool.Recycle(); |
[email protected] | 64f1931 | 2010-04-13 22:30:01 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | void InProcessBrowserTest::QuitBrowsers() { |
| 351 | if (BrowserList::size() == 0) |
| 352 | return; |
| 353 | |
| 354 | // Invoke CloseAllBrowsersAndExit on a running message loop. |
| 355 | // CloseAllBrowsersAndExit exits the message loop after everything has been |
| 356 | // shut down properly. |
| 357 | MessageLoopForUI::current()->PostTask( |
| 358 | FROM_HERE, |
| 359 | NewRunnableFunction(&BrowserList::CloseAllBrowsersAndExit)); |
| 360 | ui_test_utils::RunMessageLoop(); |
| 361 | } |