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

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

Issue 190663012: Run ContentMain in a browser_test's browser process. This removes duplication of code in the browse… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: try to fix android by restoring old path just for it Created 6 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/auto_reset.h" 7 #include "base/auto_reset.h"
8 #include "base/basictypes.h" 8 #include "base/basictypes.h"
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 68
69 #if !defined(OS_ANDROID) && !defined(OS_IOS) 69 #if !defined(OS_ANDROID) && !defined(OS_IOS)
70 #include "components/storage_monitor/test_storage_monitor.h" 70 #include "components/storage_monitor/test_storage_monitor.h"
71 #endif 71 #endif
72 72
73 namespace { 73 namespace {
74 74
75 // Passed as value of kTestType. 75 // Passed as value of kTestType.
76 const char kBrowserTestType[] = "browser"; 76 const char kBrowserTestType[] = "browser";
77 77
78 // Used when running in single-process mode.
79 base::LazyInstance<ChromeContentRendererClient>::Leaky
80 g_chrome_content_renderer_client = LAZY_INSTANCE_INITIALIZER;
81
82 // A BrowserListObserver that makes sure that all browsers created are on the 78 // A BrowserListObserver that makes sure that all browsers created are on the
83 // |allowed_desktop_|. 79 // |allowed_desktop_|.
84 class SingleDesktopTestObserver : public chrome::BrowserListObserver, 80 class SingleDesktopTestObserver : public chrome::BrowserListObserver,
85 public base::NonThreadSafe { 81 public base::NonThreadSafe {
86 public: 82 public:
87 explicit SingleDesktopTestObserver(chrome::HostDesktopType allowed_desktop); 83 explicit SingleDesktopTestObserver(chrome::HostDesktopType allowed_desktop);
88 virtual ~SingleDesktopTestObserver(); 84 virtual ~SingleDesktopTestObserver();
89 85
90 // chrome::BrowserListObserver: 86 // chrome::BrowserListObserver:
91 virtual void OnBrowserAdded(Browser* browser) OVERRIDE; 87 virtual void OnBrowserAdded(Browser* browser) OVERRIDE;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 // Before we run the browser, we have to hack the path to the exe to match 122 // Before we run the browser, we have to hack the path to the exe to match
127 // what it would be if Chrome was running, because it is used to fork renderer 123 // what it would be if Chrome was running, because it is used to fork renderer
128 // processes, on Linux at least (failure to do so will cause a browser_test to 124 // processes, on Linux at least (failure to do so will cause a browser_test to
129 // be run instead of a renderer). 125 // be run instead of a renderer).
130 base::FilePath chrome_path; 126 base::FilePath chrome_path;
131 CHECK(PathService::Get(base::FILE_EXE, &chrome_path)); 127 CHECK(PathService::Get(base::FILE_EXE, &chrome_path));
132 chrome_path = chrome_path.DirName(); 128 chrome_path = chrome_path.DirName();
133 chrome_path = chrome_path.Append(chrome::kBrowserProcessExecutablePath); 129 chrome_path = chrome_path.Append(chrome::kBrowserProcessExecutablePath);
134 CHECK(PathService::Override(base::FILE_EXE, chrome_path)); 130 CHECK(PathService::Override(base::FILE_EXE, chrome_path));
135 #endif // defined(OS_MACOSX) 131 #endif // defined(OS_MACOSX)
132
136 CreateTestServer(base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))); 133 CreateTestServer(base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
137 base::FilePath src_dir; 134 base::FilePath src_dir;
138 CHECK(PathService::Get(base::DIR_SOURCE_ROOT, &src_dir)); 135 CHECK(PathService::Get(base::DIR_SOURCE_ROOT, &src_dir));
139 embedded_test_server()->ServeFilesFromDirectory( 136 base::FilePath test_data_dir = src_dir.AppendASCII("chrome/test/data");
140 src_dir.AppendASCII("chrome/test/data")); 137 embedded_test_server()->ServeFilesFromDirectory(test_data_dir);
138
139 // chrome::DIR_TEST_DATA isn't going to be setup until after we call
140 // ContentMain. However that is after tests' constructors or SetUp methods,
141 // which sometimes need it. So just override it.
142 CHECK(PathService::Override(chrome::DIR_TEST_DATA, test_data_dir));
141 } 143 }
142 144
143 InProcessBrowserTest::~InProcessBrowserTest() { 145 InProcessBrowserTest::~InProcessBrowserTest() {
144 } 146 }
145 147
146 void InProcessBrowserTest::SetUp() { 148 void InProcessBrowserTest::SetUp() {
147 // Browser tests will create their own g_browser_process later. 149 // Browser tests will create their own g_browser_process later.
148 DCHECK(!g_browser_process); 150 DCHECK(!g_browser_process);
149 151
150 CommandLine* command_line = CommandLine::ForCurrentProcess(); 152 CommandLine* command_line = CommandLine::ForCurrentProcess();
151 // Allow subclasses to change the command line before running any tests. 153 // Allow subclasses to change the command line before running any tests.
152 SetUpCommandLine(command_line); 154 SetUpCommandLine(command_line);
153 // Add command line arguments that are used by all InProcessBrowserTests. 155 // Add command line arguments that are used by all InProcessBrowserTests.
154 PrepareTestCommandLine(command_line); 156 PrepareTestCommandLine(command_line);
155 157
156 // Create a temporary user data directory if required. 158 // Create a temporary user data directory if required.
157 ASSERT_TRUE(CreateUserDataDirectory()) 159 ASSERT_TRUE(CreateUserDataDirectory())
158 << "Could not create user data directory."; 160 << "Could not create user data directory.";
159 161
160 // Allow subclasses the opportunity to make changes to the default user data 162 // Allow subclasses the opportunity to make changes to the default user data
161 // dir before running any tests. 163 // dir before running any tests.
162 ASSERT_TRUE(SetUpUserDataDirectory()) 164 ASSERT_TRUE(SetUpUserDataDirectory())
163 << "Could not set up user data directory."; 165 << "Could not set up user data directory.";
164 166
165 // Single-process mode is not set in BrowserMain, so process it explicitly,
166 // and set up renderer.
167 if (command_line->HasSwitch(switches::kSingleProcess)) {
168 content::SetRendererClientForTesting(
169 g_chrome_content_renderer_client.Pointer());
170 }
171
172 #if defined(OS_CHROMEOS) 167 #if defined(OS_CHROMEOS)
173 // Make sure that the log directory exists. 168 // Make sure that the log directory exists.
174 base::FilePath log_dir = logging::GetSessionLogFile(*command_line).DirName(); 169 base::FilePath log_dir = logging::GetSessionLogFile(*command_line).DirName();
175 base::CreateDirectory(log_dir); 170 base::CreateDirectory(log_dir);
176 #endif // defined(OS_CHROMEOS) 171 #endif // defined(OS_CHROMEOS)
177 172
178 #if defined(OS_MACOSX) 173 #if defined(OS_MACOSX)
179 // On Mac, without the following autorelease pool, code which is directly
180 // executed (as opposed to executed inside a message loop) would autorelease
181 // objects into a higher-level pool. This pool is not recycled in-sync with
182 // the message loops' pools and causes problems with code relying on
183 // deallocation via an autorelease pool (such as browser window closure and
184 // browser shutdown). To avoid this, the following pool is recycled after each
185 // time code is directly executed.
186 autorelease_pool_ = new base::mac::ScopedNSAutoreleasePool;
187
188 // Always use the MockKeychain if OS encription is used (which is when 174 // Always use the MockKeychain if OS encription is used (which is when
189 // anything sensitive gets stored, including Cookies). Without this, 175 // anything sensitive gets stored, including Cookies). Without this,
190 // many tests will hang waiting for a user to approve KeyChain access. 176 // many tests will hang waiting for a user to approve KeyChain access.
191 OSCrypt::UseMockKeychain(true); 177 OSCrypt::UseMockKeychain(true);
192 #endif 178 #endif
193 179
194 #if defined(ENABLE_CAPTIVE_PORTAL_DETECTION) 180 #if defined(ENABLE_CAPTIVE_PORTAL_DETECTION)
195 captive_portal::CaptivePortalService::set_state_for_testing( 181 captive_portal::CaptivePortalService::set_state_for_testing(
196 captive_portal::CaptivePortalService::DISABLED_FOR_TESTING); 182 captive_portal::CaptivePortalService::DISABLED_FOR_TESTING);
197 #endif 183 #endif
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
375 new_command_line.AppendSwitchNative((*iter).first, (*iter).second); 361 new_command_line.AppendSwitchNative((*iter).first, (*iter).second);
376 } 362 }
377 return new_command_line; 363 return new_command_line;
378 } 364 }
379 #endif 365 #endif
380 366
381 void InProcessBrowserTest::RunTestOnMainThreadLoop() { 367 void InProcessBrowserTest::RunTestOnMainThreadLoop() {
382 // Pump startup related events. 368 // Pump startup related events.
383 content::RunAllPendingInMessageLoop(); 369 content::RunAllPendingInMessageLoop();
384 370
385 #if defined(OS_MACOSX)
386 autorelease_pool_->Recycle();
387 #endif
388
389 chrome::HostDesktopType active_desktop = chrome::GetActiveDesktop(); 371 chrome::HostDesktopType active_desktop = chrome::GetActiveDesktop();
390 // Self-adds/removes itself from the BrowserList observers. 372 // Self-adds/removes itself from the BrowserList observers.
391 scoped_ptr<SingleDesktopTestObserver> single_desktop_test_observer; 373 scoped_ptr<SingleDesktopTestObserver> single_desktop_test_observer;
392 if (!multi_desktop_test_) { 374 if (!multi_desktop_test_) {
393 single_desktop_test_observer.reset( 375 single_desktop_test_observer.reset(
394 new SingleDesktopTestObserver(active_desktop)); 376 new SingleDesktopTestObserver(active_desktop));
395 } 377 }
396 378
397 const BrowserList* active_browser_list = 379 const BrowserList* active_browser_list =
398 BrowserList::GetInstance(active_desktop); 380 BrowserList::GetInstance(active_desktop);
399 if (!active_browser_list->empty()) { 381 if (!active_browser_list->empty()) {
400 browser_ = active_browser_list->get(0); 382 browser_ = active_browser_list->get(0);
401 #if defined(USE_ASH) 383 #if defined(USE_ASH)
402 // There are cases where windows get created maximized by default. 384 // There are cases where windows get created maximized by default.
403 if (browser_->window()->IsMaximized()) 385 if (browser_->window()->IsMaximized())
404 browser_->window()->Restore(); 386 browser_->window()->Restore();
405 #endif 387 #endif
406 content::WaitForLoadStop( 388 content::WaitForLoadStop(
407 browser_->tab_strip_model()->GetActiveWebContents()); 389 browser_->tab_strip_model()->GetActiveWebContents());
408 } 390 }
409 391
410 #if !defined(OS_ANDROID) && !defined(OS_IOS) 392 #if !defined(OS_ANDROID) && !defined(OS_IOS)
411 // Do not use the real StorageMonitor for tests, which introduces another 393 // Do not use the real StorageMonitor for tests, which introduces another
412 // source of variability and potential slowness. 394 // source of variability and potential slowness.
413 ASSERT_TRUE(storage_monitor::TestStorageMonitor::CreateForBrowserTests()); 395 ASSERT_TRUE(storage_monitor::TestStorageMonitor::CreateForBrowserTests());
414 #endif 396 #endif
415 397
398 #if defined(OS_MACOSX)
399 // On Mac, without the following autorelease pool, code which is directly
400 // executed (as opposed to executed inside a message loop) would autorelease
401 // objects into a higher-level pool. This pool is not recycled in-sync with
402 // the message loops' pools and causes problems with code relying on
403 // deallocation via an autorelease pool (such as browser window closure and
404 // browser shutdown). To avoid this, the following pool is recycled after each
405 // time code is directly executed.
406 autorelease_pool_ = new base::mac::ScopedNSAutoreleasePool;
407 #endif
408
416 // Pump any pending events that were created as a result of creating a 409 // Pump any pending events that were created as a result of creating a
417 // browser. 410 // browser.
418 content::RunAllPendingInMessageLoop(); 411 content::RunAllPendingInMessageLoop();
419 412
420 SetUpOnMainThread(); 413 SetUpOnMainThread();
421 #if defined(OS_MACOSX) 414 #if defined(OS_MACOSX)
422 autorelease_pool_->Recycle(); 415 autorelease_pool_->Recycle();
423 #endif 416 #endif
424 417
425 if (!HasFatalFailure()) 418 if (!HasFatalFailure())
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
471 // On the Mac, this eventually reaches 464 // On the Mac, this eventually reaches
472 // -[BrowserWindowController windowWillClose:], which will post a deferred 465 // -[BrowserWindowController windowWillClose:], which will post a deferred
473 // -autorelease on itself to ultimately destroy the Browser object. The line 466 // -autorelease on itself to ultimately destroy the Browser object. The line
474 // below is necessary to pump these pending messages to ensure all Browsers 467 // below is necessary to pump these pending messages to ensure all Browsers
475 // get deleted. 468 // get deleted.
476 content::RunAllPendingInMessageLoop(); 469 content::RunAllPendingInMessageLoop();
477 delete autorelease_pool_; 470 delete autorelease_pool_;
478 autorelease_pool_ = NULL; 471 autorelease_pool_ = NULL;
479 #endif 472 #endif
480 } 473 }
OLDNEW
« no previous file with comments | « chrome/test/base/chrome_unit_test_suite.cc ('k') | chrome/test/base/interactive_ui_tests_main.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698