Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/test/base/in_process_browser_test.h" | 5 #include "chrome/test/base/in_process_browser_test.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/debug/stack_trace.h" | 8 #include "base/debug/stack_trace.h" |
| 9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 11 #include "base/mac/scoped_nsautorelease_pool.h" | 11 #include "base/mac/scoped_nsautorelease_pool.h" |
| 12 #include "base/path_service.h" | 12 #include "base/path_service.h" |
| 13 #include "base/string_number_conversions.h" | 13 #include "base/string_number_conversions.h" |
| 14 #include "base/test/test_file_util.h" | 14 #include "base/test/test_file_util.h" |
| 15 #include "chrome/browser/browser_process.h" | 15 #include "chrome/browser/browser_process.h" |
| 16 #include "chrome/browser/browser_shutdown.h" | |
| 17 #include "chrome/browser/intranet_redirect_detector.h" | |
| 18 #include "chrome/browser/io_thread.h" | 16 #include "chrome/browser/io_thread.h" |
| 19 #include "chrome/browser/net/url_request_mock_util.h" | |
| 20 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 21 #include "chrome/browser/profiles/profile_manager.h" | 18 #include "chrome/browser/profiles/profile_manager.h" |
| 22 #include "chrome/browser/tabs/tab_strip_model.h" | 19 #include "chrome/browser/tabs/tab_strip_model.h" |
| 23 #include "chrome/browser/ui/browser.h" | 20 #include "chrome/browser/ui/browser.h" |
| 24 #include "chrome/browser/ui/browser_list.h" | 21 #include "chrome/browser/ui/browser_list.h" |
| 25 #include "chrome/browser/ui/browser_navigator.h" | 22 #include "chrome/browser/ui/browser_navigator.h" |
| 26 #include "chrome/browser/ui/browser_window.h" | 23 #include "chrome/browser/ui/browser_window.h" |
| 27 #include "chrome/common/chrome_constants.h" | 24 #include "chrome/common/chrome_constants.h" |
| 28 #include "chrome/common/chrome_paths.h" | 25 #include "chrome/common/chrome_paths.h" |
| 29 #include "chrome/common/chrome_switches.h" | 26 #include "chrome/common/chrome_switches.h" |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 40 #include "content/renderer/mock_content_renderer_client.h" | 37 #include "content/renderer/mock_content_renderer_client.h" |
| 41 #include "net/base/mock_host_resolver.h" | 38 #include "net/base/mock_host_resolver.h" |
| 42 #include "net/test/test_server.h" | 39 #include "net/test/test_server.h" |
| 43 #include "sandbox/src/dep.h" | 40 #include "sandbox/src/dep.h" |
| 44 | 41 |
| 45 #if defined(OS_MACOSX) | 42 #if defined(OS_MACOSX) |
| 46 #include "base/mac/mac_util.h" | 43 #include "base/mac/mac_util.h" |
| 47 #include "base/system_monitor/system_monitor.h" | 44 #include "base/system_monitor/system_monitor.h" |
| 48 #endif | 45 #endif |
| 49 | 46 |
| 50 #if defined(OS_WIN) | |
| 51 #include "chrome/browser/ui/views/frame/browser_frame_win.h" | |
| 52 #endif | |
| 53 | |
| 54 #if defined(OS_CHROMEOS) | 47 #if defined(OS_CHROMEOS) |
| 55 #include "chrome/browser/chromeos/audio_handler.h" | 48 #include "chrome/browser/chromeos/audio_handler.h" |
| 56 #endif | 49 #endif |
| 57 | 50 |
| 58 extern int BrowserMain(const MainFunctionParams&); | 51 extern int BrowserMain(const MainFunctionParams&); |
| 59 | 52 |
| 60 const char kUnitTestShowWindows[] = "show-windows"; | |
| 61 | |
| 62 // Passed as value of kTestType. | 53 // Passed as value of kTestType. |
| 63 static const char kBrowserTestType[] = "browser"; | 54 static const char kBrowserTestType[] = "browser"; |
| 64 | 55 |
| 65 InProcessBrowserTest::InProcessBrowserTest() | 56 InProcessBrowserTest::InProcessBrowserTest() |
| 66 : browser_(NULL), | 57 : browser_(NULL), |
| 67 show_window_(false), | 58 show_window_(false), |
| 68 dom_automation_enabled_(false), | 59 dom_automation_enabled_(false), |
| 69 tab_closeable_state_watcher_enabled_(false) { | 60 tab_closeable_state_watcher_enabled_(false) { |
| 70 #if defined(OS_MACOSX) | 61 #if defined(OS_MACOSX) |
| 71 base::mac::SetOverrideAmIBundled(true); | 62 base::mac::SetOverrideAmIBundled(true); |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 99 // TODO(phajdan.jr): Extract a smaller test suite so we don't need this. | 90 // TODO(phajdan.jr): Extract a smaller test suite so we don't need this. |
| 100 DCHECK(g_browser_process); | 91 DCHECK(g_browser_process); |
| 101 delete g_browser_process; | 92 delete g_browser_process; |
| 102 g_browser_process = NULL; | 93 g_browser_process = NULL; |
| 103 | 94 |
| 104 // Allow subclasses the opportunity to make changes to the default user data | 95 // Allow subclasses the opportunity to make changes to the default user data |
| 105 // dir before running any tests. | 96 // dir before running any tests. |
| 106 ASSERT_TRUE(SetUpUserDataDirectory()) | 97 ASSERT_TRUE(SetUpUserDataDirectory()) |
| 107 << "Could not set up user data directory."; | 98 << "Could not set up user data directory."; |
| 108 | 99 |
| 109 // Don't delete the resources when BrowserMain returns. Many ui classes | |
| 110 // cache SkBitmaps in a static field so that if we delete the resource | |
| 111 // bundle we'll crash. | |
| 112 browser_shutdown::delete_resources_on_shutdown = false; | |
| 113 | |
| 114 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 100 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 115 // Allow subclasses to change the command line before running any tests. | 101 // Allow subclasses to change the command line before running any tests. |
| 116 SetUpCommandLine(command_line); | 102 SetUpCommandLine(command_line); |
| 117 // Add command line arguments that are used by all InProcessBrowserTests. | 103 // Add command line arguments that are used by all InProcessBrowserTests. |
| 118 PrepareTestCommandLine(command_line); | 104 PrepareTestCommandLine(command_line); |
| 119 | 105 |
| 120 // Single-process mode is not set in BrowserMain, so process it explicitly, | 106 // Single-process mode is not set in BrowserMain, so process it explicitly, |
| 121 // and set up renderer. | 107 // and set up renderer. |
| 122 if (command_line->HasSwitch(switches::kSingleProcess)) { | 108 if (command_line->HasSwitch(switches::kSingleProcess)) { |
| 123 RenderProcessHost::set_run_renderer_in_process(true); | 109 RenderProcessHost::set_run_renderer_in_process(true); |
| 124 single_process_renderer_client_.reset( | 110 single_process_renderer_client_.reset( |
| 125 new content::MockContentRendererClient); | 111 new content::MockContentRendererClient); |
| 126 content::GetContentClient()->set_renderer( | 112 content::GetContentClient()->set_renderer( |
| 127 single_process_renderer_client_.get()); | 113 single_process_renderer_client_.get()); |
| 128 } | 114 } |
| 129 | 115 |
| 130 #if defined(OS_CHROMEOS) | 116 #if defined(OS_CHROMEOS) |
| 131 // Make sure that the log directory exists. | 117 // Make sure that the log directory exists. |
| 132 FilePath log_dir = logging::GetSessionLogFile(*command_line).DirName(); | 118 FilePath log_dir = logging::GetSessionLogFile(*command_line).DirName(); |
| 133 file_util::CreateDirectory(log_dir); | 119 file_util::CreateDirectory(log_dir); |
| 134 #endif // defined(OS_CHROMEOS) | 120 #endif // defined(OS_CHROMEOS) |
| 135 | 121 |
| 136 SandboxInitWrapper sandbox_wrapper; | 122 host_resolver_ = new net::RuleBasedHostResolverProc(NULL); |
| 137 MainFunctionParams params(*command_line, sandbox_wrapper, NULL); | |
| 138 params.ui_task = | |
| 139 NewRunnableMethod(this, &InProcessBrowserTest::RunTestOnMainThreadLoop); | |
| 140 | |
| 141 host_resolver_ = new net::RuleBasedHostResolverProc( | |
| 142 new IntranetRedirectHostResolverProc(NULL)); | |
| 143 | 123 |
| 144 // Something inside the browser does this lookup implicitly. Make it fail | 124 // Something inside the browser does this lookup implicitly. Make it fail |
| 145 // to avoid external dependency. It won't break the tests. | 125 // to avoid external dependency. It won't break the tests. |
| 146 host_resolver_->AddSimulatedFailure("*.google.com"); | 126 host_resolver_->AddSimulatedFailure("*.google.com"); |
| 147 | 127 |
| 148 // See http://en.wikipedia.org/wiki/Web_Proxy_Autodiscovery_Protocol | 128 // See http://en.wikipedia.org/wiki/Web_Proxy_Autodiscovery_Protocol |
| 149 // We don't want the test code to use it. | 129 // We don't want the test code to use it. |
| 150 host_resolver_->AddSimulatedFailure("wpad"); | 130 host_resolver_->AddSimulatedFailure("wpad"); |
| 151 | 131 |
| 152 net::ScopedDefaultHostResolverProc scoped_host_resolver_proc( | 132 net::ScopedDefaultHostResolverProc scoped_host_resolver_proc( |
| 153 host_resolver_.get()); | 133 host_resolver_.get()); |
| 154 | 134 |
| 135 SandboxInitWrapper sandbox_wrapper; | |
| 136 MainFunctionParams params(*command_line, sandbox_wrapper, NULL); | |
| 137 params.ui_task = | |
| 138 NewRunnableMethod(this, &InProcessBrowserTest::RunTestOnMainThreadLoop); | |
| 139 | |
| 155 SetUpInProcessBrowserTestFixture(); | 140 SetUpInProcessBrowserTestFixture(); |
| 156 | |
| 157 BrowserMain(params); | 141 BrowserMain(params); |
| 158 TearDownInProcessBrowserTestFixture(); | 142 TearDownInProcessBrowserTestFixture(); |
| 159 } | 143 } |
| 160 | 144 |
| 161 void InProcessBrowserTest::PrepareTestCommandLine(CommandLine* command_line) { | 145 void InProcessBrowserTest::PrepareTestCommandLine(CommandLine* command_line) { |
| 162 // Propagate commandline settings from test_launcher_utils. | 146 // Propagate commandline settings from test_launcher_utils. |
| 163 test_launcher_utils::PrepareBrowserCommandLineForTests(command_line); | 147 test_launcher_utils::PrepareBrowserCommandLineForTests(command_line); |
| 164 | 148 |
| 165 #if defined(OS_WIN) | |
| 166 // Hide windows on show. | |
| 167 if (!command_line->HasSwitch(kUnitTestShowWindows) && !show_window_) | |
|
sky
2011/09/21 00:12:34
Where are you going to move this code to?
Paweł Hajdan Jr.
2011/09/21 01:01:09
I think this is not needed. Don't windows show by
sky
2011/09/21 03:30:23
I believe they show by default, and this code is t
Paweł Hajdan Jr.
2011/09/21 16:36:37
I have just verified this on Windows. The windows
| |
| 168 BrowserFrameWin::SetShowState(SW_HIDE); | |
| 169 #endif | |
| 170 | |
| 171 if (dom_automation_enabled_) | 149 if (dom_automation_enabled_) |
| 172 command_line->AppendSwitch(switches::kDomAutomationController); | 150 command_line->AppendSwitch(switches::kDomAutomationController); |
| 173 | 151 |
| 174 // This is a Browser test. | 152 // This is a Browser test. |
| 175 command_line->AppendSwitchASCII(switches::kTestType, kBrowserTestType); | 153 command_line->AppendSwitchASCII(switches::kTestType, kBrowserTestType); |
| 176 | 154 |
| 177 #if defined(OS_WIN) | 155 #if defined(OS_WIN) |
| 178 // The Windows sandbox requires that the browser and child processes are the | 156 // The Windows sandbox requires that the browser and child processes are the |
| 179 // same binary. So we launch browser_process.exe which loads chrome.dll | 157 // same binary. So we launch browser_process.exe which loads chrome.dll |
| 180 command_line->AppendSwitchPath(switches::kBrowserSubprocessPath, | 158 command_line->AppendSwitchPath(switches::kBrowserSubprocessPath, |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 214 LOG(ERROR) << "Could not create temporary user data directory \"" | 192 LOG(ERROR) << "Could not create temporary user data directory \"" |
| 215 << temp_user_data_dir_.path().value() << "\"."; | 193 << temp_user_data_dir_.path().value() << "\"."; |
| 216 return false; | 194 return false; |
| 217 } | 195 } |
| 218 } | 196 } |
| 219 return test_launcher_utils::OverrideUserDataDir(user_data_dir); | 197 return test_launcher_utils::OverrideUserDataDir(user_data_dir); |
| 220 } | 198 } |
| 221 | 199 |
| 222 void InProcessBrowserTest::TearDown() { | 200 void InProcessBrowserTest::TearDown() { |
| 223 DCHECK(!g_browser_process); | 201 DCHECK(!g_browser_process); |
| 224 | |
| 225 browser_shutdown::delete_resources_on_shutdown = true; | |
| 226 | |
| 227 #if defined(OS_WIN) | |
| 228 BrowserFrameWin::SetShowState(-1); | |
| 229 #endif | |
| 230 } | 202 } |
| 231 | 203 |
| 232 void InProcessBrowserTest::AddTabAtIndexToBrowser( | 204 void InProcessBrowserTest::AddTabAtIndexToBrowser( |
| 233 Browser* browser, | 205 Browser* browser, |
| 234 int index, | 206 int index, |
| 235 const GURL& url, | 207 const GURL& url, |
| 236 PageTransition::Type transition) { | 208 PageTransition::Type transition) { |
| 237 browser::NavigateParams params(browser, url, transition); | 209 browser::NavigateParams params(browser, url, transition); |
| 238 params.tabstrip_index = index; | 210 params.tabstrip_index = index; |
| 239 params.disposition = NEW_FOREGROUND_TAB; | 211 params.disposition = NEW_FOREGROUND_TAB; |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 303 // executed (as opposed to executed inside a message loop) would autorelease | 275 // 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 | 276 // 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 | 277 // the message loops' pools and causes problems with code relying on |
| 306 // deallocation via an autorelease pool (such as browser window closure and | 278 // deallocation via an autorelease pool (such as browser window closure and |
| 307 // browser shutdown). To avoid this, the following pool is recycled after each | 279 // browser shutdown). To avoid this, the following pool is recycled after each |
| 308 // time code is directly executed. | 280 // time code is directly executed. |
| 309 base::mac::ScopedNSAutoreleasePool pool; | 281 base::mac::ScopedNSAutoreleasePool pool; |
| 310 | 282 |
| 311 // Pump startup related events. | 283 // Pump startup related events. |
| 312 MessageLoopForUI::current()->RunAllPending(); | 284 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 } | |
| 324 pool.Recycle(); | 285 pool.Recycle(); |
| 325 | 286 |
| 326 BrowserThread::PostTask( | 287 browser_ = CreateBrowser(ProfileManager::GetDefaultProfile()); |
| 327 BrowserThread::IO, FROM_HERE, | |
| 328 NewRunnableFunction(chrome_browser_net::SetUrlRequestMocksEnabled, true)); | |
| 329 | |
| 330 browser_ = CreateBrowser(profile); | |
| 331 pool.Recycle(); | 288 pool.Recycle(); |
| 332 | 289 |
| 333 // Pump any pending events that were created as a result of creating a | 290 // Pump any pending events that were created as a result of creating a |
| 334 // browser. | 291 // browser. |
| 335 MessageLoopForUI::current()->RunAllPending(); | 292 MessageLoopForUI::current()->RunAllPending(); |
| 336 | 293 |
| 337 SetUpOnMainThread(); | 294 SetUpOnMainThread(); |
| 338 pool.Recycle(); | 295 pool.Recycle(); |
| 339 | 296 |
| 340 RunTestOnMainThread(); | 297 RunTestOnMainThread(); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 352 return; | 309 return; |
| 353 | 310 |
| 354 // Invoke CloseAllBrowsersAndMayExit on a running message loop. | 311 // Invoke CloseAllBrowsersAndMayExit on a running message loop. |
| 355 // CloseAllBrowsersAndMayExit exits the message loop after everything has been | 312 // CloseAllBrowsersAndMayExit exits the message loop after everything has been |
| 356 // shut down properly. | 313 // shut down properly. |
| 357 MessageLoopForUI::current()->PostTask( | 314 MessageLoopForUI::current()->PostTask( |
| 358 FROM_HERE, | 315 FROM_HERE, |
| 359 NewRunnableFunction(&BrowserList::AttemptExit)); | 316 NewRunnableFunction(&BrowserList::AttemptExit)); |
| 360 ui_test_utils::RunMessageLoop(); | 317 ui_test_utils::RunMessageLoop(); |
| 361 } | 318 } |
| OLD | NEW |