| 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/in_process_browser_test.h" | 5 #include "chrome/test/in_process_browser_test.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/mac/scoped_nsautorelease_pool.h" | 10 #include "base/mac/scoped_nsautorelease_pool.h" |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 | 92 |
| 93 test_server_.reset(new net::TestServer( | 93 test_server_.reset(new net::TestServer( |
| 94 net::TestServer::TYPE_HTTP, | 94 net::TestServer::TYPE_HTTP, |
| 95 FilePath(FILE_PATH_LITERAL("chrome/test/data")))); | 95 FilePath(FILE_PATH_LITERAL("chrome/test/data")))); |
| 96 } | 96 } |
| 97 | 97 |
| 98 InProcessBrowserTest::~InProcessBrowserTest() { | 98 InProcessBrowserTest::~InProcessBrowserTest() { |
| 99 } | 99 } |
| 100 | 100 |
| 101 void InProcessBrowserTest::SetUp() { | 101 void InProcessBrowserTest::SetUp() { |
| 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)); | |
| 109 | |
| 110 // Create a temporary user data directory if required. | 102 // Create a temporary user data directory if required. |
| 111 ASSERT_TRUE(CreateUserDataDirectory()) | 103 ASSERT_TRUE(CreateUserDataDirectory()) |
| 112 << "Could not create user data directory."; | 104 << "Could not create user data directory."; |
| 113 | 105 |
| 114 // The unit test suite creates a testingbrowser, but we want the real thing. | 106 // 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. | 107 // Delete the current one. We'll install the testing one in TearDown. |
| 116 delete g_browser_process; | 108 delete g_browser_process; |
| 117 g_browser_process = NULL; | 109 g_browser_process = NULL; |
| 118 | 110 |
| 119 // Allow subclasses the opportunity to make changes to the default user data | 111 // Allow subclasses the opportunity to make changes to the default user data |
| 120 // dir before running any tests. | 112 // dir before running any tests. |
| 121 ASSERT_TRUE(SetUpUserDataDirectory()) | 113 ASSERT_TRUE(SetUpUserDataDirectory()) |
| 122 << "Could not set up user data directory."; | 114 << "Could not set up user data directory."; |
| 123 | 115 |
| 124 // Don't delete the resources when BrowserMain returns. Many ui classes | 116 // 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 | 117 // cache SkBitmaps in a static field so that if we delete the resource |
| 126 // bundle we'll crash. | 118 // bundle we'll crash. |
| 127 browser_shutdown::delete_resources_on_shutdown = false; | 119 browser_shutdown::delete_resources_on_shutdown = false; |
| 128 | 120 |
| 121 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 129 // Allow subclasses the opportunity to make changes to the command line before | 122 // Allow subclasses the opportunity to make changes to the command line before |
| 130 // running any tests. | 123 // running any tests. |
| 131 SetUpCommandLine(command_line); | 124 SetUpCommandLine(command_line); |
| 132 // Add command line arguments that are used by all InProcessBrowserTests. | 125 // Add command line arguments that are used by all InProcessBrowserTests. |
| 133 PrepareTestCommandLine(command_line); | 126 PrepareTestCommandLine(command_line); |
| 134 | 127 |
| 135 // Save the single process mode state before it was reset in this test. This | 128 // 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 | 129 // state will be recovered in TearDown(). Single-process mode is not set in |
| 137 // BrowserMain so it needs to be processed explicitly. | 130 // BrowserMain so it needs to be processed explicitly. |
| 138 original_single_process_ = RenderProcessHost::run_renderer_in_process(); | 131 original_single_process_ = RenderProcessHost::run_renderer_in_process(); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 command_line->AppendSwitchPath(switches::kBrowserSubprocessPath, | 202 command_line->AppendSwitchPath(switches::kBrowserSubprocessPath, |
| 210 subprocess_path); | 203 subprocess_path); |
| 211 #endif | 204 #endif |
| 212 | 205 |
| 213 // If neccessary, disable TabCloseableStateWatcher. | 206 // If neccessary, disable TabCloseableStateWatcher. |
| 214 if (!tab_closeable_state_watcher_enabled_) | 207 if (!tab_closeable_state_watcher_enabled_) |
| 215 command_line->AppendSwitch(switches::kDisableTabCloseableStateWatcher); | 208 command_line->AppendSwitch(switches::kDisableTabCloseableStateWatcher); |
| 216 } | 209 } |
| 217 | 210 |
| 218 bool InProcessBrowserTest::CreateUserDataDirectory() { | 211 bool InProcessBrowserTest::CreateUserDataDirectory() { |
| 219 CommandLine* command_line = CommandLine::ForCurrentProcessMutable(); | 212 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 220 FilePath user_data_dir = | 213 FilePath user_data_dir = |
| 221 command_line->GetSwitchValuePath(switches::kUserDataDir); | 214 command_line->GetSwitchValuePath(switches::kUserDataDir); |
| 222 if (user_data_dir.empty()) { | 215 if (user_data_dir.empty()) { |
| 223 if (temp_user_data_dir_.CreateUniqueTempDir() && | 216 if (temp_user_data_dir_.CreateUniqueTempDir() && |
| 224 temp_user_data_dir_.IsValid()) { | 217 temp_user_data_dir_.IsValid()) { |
| 225 user_data_dir = temp_user_data_dir_.path(); | 218 user_data_dir = temp_user_data_dir_.path(); |
| 226 } else { | 219 } else { |
| 227 LOG(ERROR) << "Could not create temporary user data directory \"" | 220 LOG(ERROR) << "Could not create temporary user data directory \"" |
| 228 << temp_user_data_dir_.path().value() << "\"."; | 221 << temp_user_data_dir_.path().value() << "\"."; |
| 229 return false; | 222 return false; |
| 230 } | 223 } |
| 231 } | 224 } |
| 232 return test_launcher_utils::OverrideUserDataDir(user_data_dir); | 225 return test_launcher_utils::OverrideUserDataDir(user_data_dir); |
| 233 } | 226 } |
| 234 | 227 |
| 235 void InProcessBrowserTest::TearDown() { | 228 void InProcessBrowserTest::TearDown() { |
| 236 // Reinstall testing browser process. | 229 // Reinstall testing browser process. |
| 237 delete g_browser_process; | 230 delete g_browser_process; |
| 238 g_browser_process = new TestingBrowserProcess(); | 231 g_browser_process = new TestingBrowserProcess(); |
| 239 | 232 |
| 240 browser_shutdown::delete_resources_on_shutdown = true; | 233 browser_shutdown::delete_resources_on_shutdown = true; |
| 241 | 234 |
| 242 #if defined(OS_WIN) | 235 #if defined(OS_WIN) |
| 243 BrowserView::SetShowState(-1); | 236 BrowserView::SetShowState(-1); |
| 244 #endif | 237 #endif |
| 245 | 238 |
| 246 *CommandLine::ForCurrentProcessMutable() = *original_command_line_; | |
| 247 RenderProcessHost::set_run_renderer_in_process(original_single_process_); | 239 RenderProcessHost::set_run_renderer_in_process(original_single_process_); |
| 248 } | 240 } |
| 249 | 241 |
| 250 void InProcessBrowserTest::AddTabAtIndexToBrowser( | 242 void InProcessBrowserTest::AddTabAtIndexToBrowser( |
| 251 Browser* browser, | 243 Browser* browser, |
| 252 int index, | 244 int index, |
| 253 const GURL& url, | 245 const GURL& url, |
| 254 PageTransition::Type transition) { | 246 PageTransition::Type transition) { |
| 255 browser::NavigateParams params(browser, url, transition); | 247 browser::NavigateParams params(browser, url, transition); |
| 256 params.tabstrip_index = index; | 248 params.tabstrip_index = index; |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 352 return; | 344 return; |
| 353 | 345 |
| 354 // Invoke CloseAllBrowsersAndExit on a running message loop. | 346 // Invoke CloseAllBrowsersAndExit on a running message loop. |
| 355 // CloseAllBrowsersAndExit exits the message loop after everything has been | 347 // CloseAllBrowsersAndExit exits the message loop after everything has been |
| 356 // shut down properly. | 348 // shut down properly. |
| 357 MessageLoopForUI::current()->PostTask( | 349 MessageLoopForUI::current()->PostTask( |
| 358 FROM_HERE, | 350 FROM_HERE, |
| 359 NewRunnableFunction(&BrowserList::CloseAllBrowsersAndExit)); | 351 NewRunnableFunction(&BrowserList::CloseAllBrowsersAndExit)); |
| 360 ui_test_utils::RunMessageLoop(); | 352 ui_test_utils::RunMessageLoop(); |
| 361 } | 353 } |
| OLD | NEW |