Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(201)

Side by Side Diff: chrome/test/in_process_browser_test.cc

Issue 6596020: Reorganize CommandLine code. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments. Created 9 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/test/in_process_browser_test.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 67
68 const char kUnitTestShowWindows[] = "show-windows"; 68 const char kUnitTestShowWindows[] = "show-windows";
69 69
70 // Passed as value of kTestType. 70 // Passed as value of kTestType.
71 static const char kBrowserTestType[] = "browser"; 71 static const char kBrowserTestType[] = "browser";
72 72
73 InProcessBrowserTest::InProcessBrowserTest() 73 InProcessBrowserTest::InProcessBrowserTest()
74 : browser_(NULL), 74 : browser_(NULL),
75 show_window_(false), 75 show_window_(false),
76 dom_automation_enabled_(false), 76 dom_automation_enabled_(false),
77 tab_closeable_state_watcher_enabled_(false), 77 tab_closeable_state_watcher_enabled_(false) {
78 original_single_process_(false) {
79 #if defined(OS_MACOSX) 78 #if defined(OS_MACOSX)
80 base::mac::SetOverrideAmIBundled(true); 79 base::mac::SetOverrideAmIBundled(true);
81 #endif 80 #endif
82 81
83 // Before we run the browser, we have to hack the path to the exe to match 82 // 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 83 // 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 84 // processes, on Linux at least (failure to do so will cause a browser_test to
86 // be run instead of a renderer). 85 // be run instead of a renderer).
87 FilePath chrome_path; 86 FilePath chrome_path;
88 CHECK(PathService::Get(base::FILE_EXE, &chrome_path)); 87 CHECK(PathService::Get(base::FILE_EXE, &chrome_path));
89 chrome_path = chrome_path.DirName(); 88 chrome_path = chrome_path.DirName();
90 chrome_path = chrome_path.Append(chrome::kBrowserProcessExecutablePath); 89 chrome_path = chrome_path.Append(chrome::kBrowserProcessExecutablePath);
91 CHECK(PathService::Override(base::FILE_EXE, chrome_path)); 90 CHECK(PathService::Override(base::FILE_EXE, chrome_path));
92 91
93 test_server_.reset(new net::TestServer( 92 test_server_.reset(new net::TestServer(
94 net::TestServer::TYPE_HTTP, 93 net::TestServer::TYPE_HTTP,
95 FilePath(FILE_PATH_LITERAL("chrome/test/data")))); 94 FilePath(FILE_PATH_LITERAL("chrome/test/data"))));
96 } 95 }
97 96
98 InProcessBrowserTest::~InProcessBrowserTest() { 97 InProcessBrowserTest::~InProcessBrowserTest() {
99 } 98 }
100 99
101 void InProcessBrowserTest::SetUp() { 100 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. 101 // Create a temporary user data directory if required.
111 ASSERT_TRUE(CreateUserDataDirectory()) 102 ASSERT_TRUE(CreateUserDataDirectory())
112 << "Could not create user data directory."; 103 << "Could not create user data directory.";
113 104
114 // The unit test suite creates a testingbrowser, but we want the real thing. 105 // 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. 106 // Delete the current one. We'll install the testing one in TearDown.
116 delete g_browser_process; 107 delete g_browser_process;
117 g_browser_process = NULL; 108 g_browser_process = NULL;
118 109
119 // Allow subclasses the opportunity to make changes to the default user data 110 // Allow subclasses the opportunity to make changes to the default user data
120 // dir before running any tests. 111 // dir before running any tests.
121 ASSERT_TRUE(SetUpUserDataDirectory()) 112 ASSERT_TRUE(SetUpUserDataDirectory())
122 << "Could not set up user data directory."; 113 << "Could not set up user data directory.";
123 114
124 // Don't delete the resources when BrowserMain returns. Many ui classes 115 // 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 116 // cache SkBitmaps in a static field so that if we delete the resource
126 // bundle we'll crash. 117 // bundle we'll crash.
127 browser_shutdown::delete_resources_on_shutdown = false; 118 browser_shutdown::delete_resources_on_shutdown = false;
128 119
129 // Allow subclasses the opportunity to make changes to the command line before 120 CommandLine* command_line = CommandLine::ForCurrentProcess();
130 // running any tests. 121 // Allow subclasses to change the command line before running any tests.
131 SetUpCommandLine(command_line); 122 SetUpCommandLine(command_line);
132 // Add command line arguments that are used by all InProcessBrowserTests. 123 // Add command line arguments that are used by all InProcessBrowserTests.
133 PrepareTestCommandLine(command_line); 124 PrepareTestCommandLine(command_line);
134 125
135 // Save the single process mode state before it was reset in this test. This 126 // Single-process mode is not set in BrowserMain, so process it explicitly.
136 // state will be recovered in TearDown(). Single-process mode is not set in
137 // BrowserMain so it needs to be processed explicitly.
138 original_single_process_ = RenderProcessHost::run_renderer_in_process();
139 if (command_line->HasSwitch(switches::kSingleProcess)) 127 if (command_line->HasSwitch(switches::kSingleProcess))
140 RenderProcessHost::set_run_renderer_in_process(true); 128 RenderProcessHost::set_run_renderer_in_process(true);
141 129
142 #if defined(OS_CHROMEOS) 130 #if defined(OS_CHROMEOS)
143 // Make sure that the log directory exists. 131 // Make sure that the log directory exists.
144 FilePath log_dir = logging::GetSessionLogFile(*command_line).DirName(); 132 FilePath log_dir = logging::GetSessionLogFile(*command_line).DirName();
145 file_util::CreateDirectory(log_dir); 133 file_util::CreateDirectory(log_dir);
146 #endif // defined(OS_CHROMEOS) 134 #endif // defined(OS_CHROMEOS)
147 135
148 SandboxInitWrapper sandbox_wrapper; 136 SandboxInitWrapper sandbox_wrapper;
(...skipping 14 matching lines...) Expand all
163 151
164 net::ScopedDefaultHostResolverProc scoped_host_resolver_proc( 152 net::ScopedDefaultHostResolverProc scoped_host_resolver_proc(
165 host_resolver_.get()); 153 host_resolver_.get());
166 154
167 SetUpInProcessBrowserTestFixture(); 155 SetUpInProcessBrowserTestFixture();
168 156
169 BrowserMain(params); 157 BrowserMain(params);
170 TearDownInProcessBrowserTestFixture(); 158 TearDownInProcessBrowserTestFixture();
171 } 159 }
172 160
173 void InProcessBrowserTest::PrepareTestCommandLine( 161 void InProcessBrowserTest::PrepareTestCommandLine(CommandLine* command_line) {
174 CommandLine* command_line) {
175 // Propagate commandline settings from test_launcher_utils. 162 // Propagate commandline settings from test_launcher_utils.
176 test_launcher_utils::PrepareBrowserCommandLineForTests(command_line); 163 test_launcher_utils::PrepareBrowserCommandLineForTests(command_line);
177 164
178 #if defined(OS_WIN) 165 #if defined(OS_WIN)
179 // Hide windows on show. 166 // Hide windows on show.
180 if (!command_line->HasSwitch(kUnitTestShowWindows) && !show_window_) 167 if (!command_line->HasSwitch(kUnitTestShowWindows) && !show_window_)
181 BrowserView::SetShowState(SW_HIDE); 168 BrowserView::SetShowState(SW_HIDE);
182 #endif 169 #endif
183 170
184 if (dom_automation_enabled_) 171 if (dom_automation_enabled_)
(...skipping 24 matching lines...) Expand all
209 command_line->AppendSwitchPath(switches::kBrowserSubprocessPath, 196 command_line->AppendSwitchPath(switches::kBrowserSubprocessPath,
210 subprocess_path); 197 subprocess_path);
211 #endif 198 #endif
212 199
213 // If neccessary, disable TabCloseableStateWatcher. 200 // If neccessary, disable TabCloseableStateWatcher.
214 if (!tab_closeable_state_watcher_enabled_) 201 if (!tab_closeable_state_watcher_enabled_)
215 command_line->AppendSwitch(switches::kDisableTabCloseableStateWatcher); 202 command_line->AppendSwitch(switches::kDisableTabCloseableStateWatcher);
216 } 203 }
217 204
218 bool InProcessBrowserTest::CreateUserDataDirectory() { 205 bool InProcessBrowserTest::CreateUserDataDirectory() {
219 CommandLine* command_line = CommandLine::ForCurrentProcessMutable(); 206 CommandLine* command_line = CommandLine::ForCurrentProcess();
220 FilePath user_data_dir = 207 FilePath user_data_dir =
221 command_line->GetSwitchValuePath(switches::kUserDataDir); 208 command_line->GetSwitchValuePath(switches::kUserDataDir);
222 if (user_data_dir.empty()) { 209 if (user_data_dir.empty()) {
223 if (temp_user_data_dir_.CreateUniqueTempDir() && 210 if (temp_user_data_dir_.CreateUniqueTempDir() &&
224 temp_user_data_dir_.IsValid()) { 211 temp_user_data_dir_.IsValid()) {
225 user_data_dir = temp_user_data_dir_.path(); 212 user_data_dir = temp_user_data_dir_.path();
226 } else { 213 } else {
227 LOG(ERROR) << "Could not create temporary user data directory \"" 214 LOG(ERROR) << "Could not create temporary user data directory \""
228 << temp_user_data_dir_.path().value() << "\"."; 215 << temp_user_data_dir_.path().value() << "\".";
229 return false; 216 return false;
230 } 217 }
231 } 218 }
232 return test_launcher_utils::OverrideUserDataDir(user_data_dir); 219 return test_launcher_utils::OverrideUserDataDir(user_data_dir);
233 } 220 }
234 221
235 void InProcessBrowserTest::TearDown() { 222 void InProcessBrowserTest::TearDown() {
236 // Reinstall testing browser process. 223 // Reinstall testing browser process.
237 delete g_browser_process; 224 delete g_browser_process;
238 g_browser_process = new TestingBrowserProcess(); 225 g_browser_process = new TestingBrowserProcess();
239 226
240 browser_shutdown::delete_resources_on_shutdown = true; 227 browser_shutdown::delete_resources_on_shutdown = true;
241 228
242 #if defined(OS_WIN) 229 #if defined(OS_WIN)
243 BrowserView::SetShowState(-1); 230 BrowserView::SetShowState(-1);
244 #endif 231 #endif
245
246 *CommandLine::ForCurrentProcessMutable() = *original_command_line_;
247 RenderProcessHost::set_run_renderer_in_process(original_single_process_);
248 } 232 }
249 233
250 void InProcessBrowserTest::AddTabAtIndexToBrowser( 234 void InProcessBrowserTest::AddTabAtIndexToBrowser(
251 Browser* browser, 235 Browser* browser,
252 int index, 236 int index,
253 const GURL& url, 237 const GURL& url,
254 PageTransition::Type transition) { 238 PageTransition::Type transition) {
255 browser::NavigateParams params(browser, url, transition); 239 browser::NavigateParams params(browser, url, transition);
256 params.tabstrip_index = index; 240 params.tabstrip_index = index;
257 params.disposition = NEW_FOREGROUND_TAB; 241 params.disposition = NEW_FOREGROUND_TAB;
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
352 return; 336 return;
353 337
354 // Invoke CloseAllBrowsersAndExit on a running message loop. 338 // Invoke CloseAllBrowsersAndExit on a running message loop.
355 // CloseAllBrowsersAndExit exits the message loop after everything has been 339 // CloseAllBrowsersAndExit exits the message loop after everything has been
356 // shut down properly. 340 // shut down properly.
357 MessageLoopForUI::current()->PostTask( 341 MessageLoopForUI::current()->PostTask(
358 FROM_HERE, 342 FROM_HERE,
359 NewRunnableFunction(&BrowserList::CloseAllBrowsersAndExit)); 343 NewRunnableFunction(&BrowserList::CloseAllBrowsersAndExit));
360 ui_test_utils::RunMessageLoop(); 344 ui_test_utils::RunMessageLoop();
361 } 345 }
OLDNEW
« no previous file with comments | « chrome/test/in_process_browser_test.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698