Infobar material design refresh: bg color
[chromium-blink-merge.git] / chrome / test / base / testing_browser_process.cc
bloba7b2b9df43534f36f9acc0ae03019c1516e19adb
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
3 // found in the LICENSE file.
5 #include "chrome/test/base/testing_browser_process.h"
7 #include "base/prefs/pref_service.h"
8 #include "base/strings/string_util.h"
9 #include "base/time/default_tick_clock.h"
10 #include "build/build_config.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/browser_process_impl.h"
13 #include "chrome/browser/printing/print_job_manager.h"
14 #include "chrome/browser/profiles/profile_manager.h"
15 #include "chrome/test/base/testing_browser_process_platform_part.h"
16 #include "components/network_time/network_time_tracker.h"
17 #include "content/public/browser/notification_service.h"
18 #include "net/url_request/url_request_context_getter.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "ui/message_center/message_center.h"
22 #if !defined(OS_IOS)
23 #include "chrome/browser/notifications/notification_ui_manager.h"
24 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
25 #endif
27 #if defined(ENABLE_BACKGROUND)
28 #include "chrome/browser/background/background_mode_manager.h"
29 #endif
31 #if defined(ENABLE_CONFIGURATION_POLICY)
32 #include "components/policy/core/browser/browser_policy_connector.h"
33 #else
34 #include "components/policy/core/common/policy_service_stub.h"
35 #endif // defined(ENABLE_CONFIGURATION_POLICY)
37 #if defined(ENABLE_EXTENSIONS)
38 #include "chrome/browser/extensions/chrome_extensions_browser_client.h"
39 #include "chrome/browser/media_galleries/media_file_system_registry.h"
40 #include "chrome/browser/ui/apps/chrome_app_window_client.h"
41 #include "components/storage_monitor/storage_monitor.h"
42 #include "components/storage_monitor/test_storage_monitor.h"
43 #endif
45 #if defined(ENABLE_PRINT_PREVIEW)
46 #include "chrome/browser/printing/background_printing_manager.h"
47 #include "chrome/browser/printing/print_preview_dialog_controller.h"
48 #endif
50 // static
51 TestingBrowserProcess* TestingBrowserProcess::GetGlobal() {
52 return static_cast<TestingBrowserProcess*>(g_browser_process);
55 // static
56 void TestingBrowserProcess::CreateInstance() {
57 DCHECK(!g_browser_process);
58 g_browser_process = new TestingBrowserProcess;
61 // static
62 void TestingBrowserProcess::DeleteInstance() {
63 // g_browser_process must be null during its own destruction.
64 BrowserProcess* browser_process = g_browser_process;
65 g_browser_process = nullptr;
66 delete browser_process;
69 TestingBrowserProcess::TestingBrowserProcess()
70 : notification_service_(content::NotificationService::Create()),
71 module_ref_count_(0),
72 app_locale_("en"),
73 local_state_(nullptr),
74 io_thread_(nullptr),
75 system_request_context_(nullptr),
76 platform_part_(new TestingBrowserProcessPlatformPart()) {
77 #if defined(ENABLE_EXTENSIONS)
78 extensions_browser_client_.reset(
79 new extensions::ChromeExtensionsBrowserClient);
80 extensions::AppWindowClient::Set(ChromeAppWindowClient::GetInstance());
81 extensions::ExtensionsBrowserClient::Set(extensions_browser_client_.get());
82 #endif
85 TestingBrowserProcess::~TestingBrowserProcess() {
86 EXPECT_FALSE(local_state_);
87 #if defined(ENABLE_CONFIGURATION_POLICY)
88 ShutdownBrowserPolicyConnector();
89 #endif
90 #if defined(ENABLE_EXTENSIONS)
91 extensions::ExtensionsBrowserClient::Set(nullptr);
92 #endif
94 // Destructors for some objects owned by TestingBrowserProcess will use
95 // g_browser_process if it is not null, so it must be null before proceeding.
96 DCHECK_EQ(static_cast<BrowserProcess*>(nullptr), g_browser_process);
99 void TestingBrowserProcess::ResourceDispatcherHostCreated() {
102 void TestingBrowserProcess::EndSession() {
105 MetricsServicesManager* TestingBrowserProcess::GetMetricsServicesManager() {
106 return nullptr;
109 metrics::MetricsService* TestingBrowserProcess::metrics_service() {
110 return nullptr;
113 rappor::RapporService* TestingBrowserProcess::rappor_service() {
114 return nullptr;
117 IOThread* TestingBrowserProcess::io_thread() {
118 return io_thread_;
121 WatchDogThread* TestingBrowserProcess::watchdog_thread() {
122 return nullptr;
125 ProfileManager* TestingBrowserProcess::profile_manager() {
126 #if defined(OS_IOS)
127 NOTIMPLEMENTED();
128 return nullptr;
129 #else
130 return profile_manager_.get();
131 #endif
134 void TestingBrowserProcess::SetProfileManager(ProfileManager* profile_manager) {
135 #if !defined(OS_IOS)
136 // NotificationUIManager can contain references to elements in the current
137 // ProfileManager (for example, the MessageCenterSettingsController maintains
138 // a pointer to the ProfileInfoCache). So when we change the ProfileManager
139 // (typically during test shutdown) make sure to reset any objects that might
140 // maintain references to it. See SetLocalState() for a description of a
141 // similar situation.
142 notification_ui_manager_.reset();
143 profile_manager_.reset(profile_manager);
144 #endif
147 PrefService* TestingBrowserProcess::local_state() {
148 return local_state_;
151 chrome_variations::VariationsService*
152 TestingBrowserProcess::variations_service() {
153 return nullptr;
156 web_resource::PromoResourceService*
157 TestingBrowserProcess::promo_resource_service() {
158 return nullptr;
161 policy::BrowserPolicyConnector*
162 TestingBrowserProcess::browser_policy_connector() {
163 #if defined(ENABLE_CONFIGURATION_POLICY)
164 if (!browser_policy_connector_) {
165 EXPECT_FALSE(created_browser_policy_connector_);
166 created_browser_policy_connector_ = true;
167 browser_policy_connector_ = platform_part_->CreateBrowserPolicyConnector();
169 return browser_policy_connector_.get();
170 #else
171 return nullptr;
172 #endif
175 policy::PolicyService* TestingBrowserProcess::policy_service() {
176 #if defined(OS_IOS)
177 NOTIMPLEMENTED();
178 return nullptr;
179 #elif defined(ENABLE_CONFIGURATION_POLICY)
180 return browser_policy_connector()->GetPolicyService();
181 #else
182 if (!policy_service_)
183 policy_service_.reset(new policy::PolicyServiceStub());
184 return policy_service_.get();
185 #endif
188 IconManager* TestingBrowserProcess::icon_manager() {
189 return nullptr;
192 GLStringManager* TestingBrowserProcess::gl_string_manager() {
193 return nullptr;
196 GpuModeManager* TestingBrowserProcess::gpu_mode_manager() {
197 return nullptr;
200 BackgroundModeManager* TestingBrowserProcess::background_mode_manager() {
201 return nullptr;
204 void TestingBrowserProcess::set_background_mode_manager_for_test(
205 scoped_ptr<BackgroundModeManager> manager) {
206 NOTREACHED();
209 StatusTray* TestingBrowserProcess::status_tray() {
210 return nullptr;
213 SafeBrowsingService* TestingBrowserProcess::safe_browsing_service() {
214 #if defined(OS_IOS)
215 NOTIMPLEMENTED();
216 return nullptr;
217 #else
218 return sb_service_.get();
219 #endif
222 safe_browsing::ClientSideDetectionService*
223 TestingBrowserProcess::safe_browsing_detection_service() {
224 return nullptr;
227 net::URLRequestContextGetter* TestingBrowserProcess::system_request_context() {
228 return system_request_context_;
231 BrowserProcessPlatformPart* TestingBrowserProcess::platform_part() {
232 return platform_part_.get();
235 extensions::EventRouterForwarder*
236 TestingBrowserProcess::extension_event_router_forwarder() {
237 return nullptr;
240 NotificationUIManager* TestingBrowserProcess::notification_ui_manager() {
241 #if defined(ENABLE_NOTIFICATIONS)
242 if (!notification_ui_manager_.get())
243 notification_ui_manager_.reset(
244 NotificationUIManager::Create(local_state()));
245 return notification_ui_manager_.get();
246 #else
247 NOTIMPLEMENTED();
248 return nullptr;
249 #endif
252 message_center::MessageCenter* TestingBrowserProcess::message_center() {
253 return message_center::MessageCenter::Get();
256 IntranetRedirectDetector* TestingBrowserProcess::intranet_redirect_detector() {
257 return nullptr;
259 void TestingBrowserProcess::CreateDevToolsHttpProtocolHandler(
260 chrome::HostDesktopType host_desktop_type,
261 const std::string& ip,
262 uint16 port) {
265 unsigned int TestingBrowserProcess::AddRefModule() {
266 return ++module_ref_count_;
269 unsigned int TestingBrowserProcess::ReleaseModule() {
270 DCHECK_GT(module_ref_count_, 0U);
271 return --module_ref_count_;
274 bool TestingBrowserProcess::IsShuttingDown() {
275 return false;
278 printing::PrintJobManager* TestingBrowserProcess::print_job_manager() {
279 #if defined(ENABLE_PRINTING)
280 if (!print_job_manager_.get())
281 print_job_manager_.reset(new printing::PrintJobManager());
282 return print_job_manager_.get();
283 #else
284 NOTIMPLEMENTED();
285 return nullptr;
286 #endif
289 printing::PrintPreviewDialogController*
290 TestingBrowserProcess::print_preview_dialog_controller() {
291 #if defined(ENABLE_PRINT_PREVIEW)
292 if (!print_preview_dialog_controller_.get())
293 print_preview_dialog_controller_ =
294 new printing::PrintPreviewDialogController();
295 return print_preview_dialog_controller_.get();
296 #else
297 NOTIMPLEMENTED();
298 return nullptr;
299 #endif
302 printing::BackgroundPrintingManager*
303 TestingBrowserProcess::background_printing_manager() {
304 #if defined(ENABLE_PRINT_PREVIEW)
305 if (!background_printing_manager_.get()) {
306 background_printing_manager_.reset(
307 new printing::BackgroundPrintingManager());
309 return background_printing_manager_.get();
310 #else
311 NOTIMPLEMENTED();
312 return nullptr;
313 #endif
316 const std::string& TestingBrowserProcess::GetApplicationLocale() {
317 return app_locale_;
320 void TestingBrowserProcess::SetApplicationLocale(
321 const std::string& app_locale) {
322 app_locale_ = app_locale;
325 DownloadStatusUpdater* TestingBrowserProcess::download_status_updater() {
326 return nullptr;
329 DownloadRequestLimiter* TestingBrowserProcess::download_request_limiter() {
330 return nullptr;
333 ChromeNetLog* TestingBrowserProcess::net_log() {
334 return nullptr;
337 component_updater::ComponentUpdateService*
338 TestingBrowserProcess::component_updater() {
339 return nullptr;
342 CRLSetFetcher* TestingBrowserProcess::crl_set_fetcher() {
343 return nullptr;
346 component_updater::PnaclComponentInstaller*
347 TestingBrowserProcess::pnacl_component_installer() {
348 return nullptr;
351 component_updater::SupervisedUserWhitelistInstaller*
352 TestingBrowserProcess::supervised_user_whitelist_installer() {
353 return nullptr;
356 MediaFileSystemRegistry* TestingBrowserProcess::media_file_system_registry() {
357 #if defined(OS_IOS) || defined(OS_ANDROID)
358 NOTIMPLEMENTED();
359 return nullptr;
360 #else
361 if (!media_file_system_registry_)
362 media_file_system_registry_.reset(new MediaFileSystemRegistry());
363 return media_file_system_registry_.get();
364 #endif
367 bool TestingBrowserProcess::created_local_state() const {
368 return (local_state_ != nullptr);
371 #if defined(ENABLE_WEBRTC)
372 WebRtcLogUploader* TestingBrowserProcess::webrtc_log_uploader() {
373 return nullptr;
375 #endif
377 network_time::NetworkTimeTracker*
378 TestingBrowserProcess::network_time_tracker() {
379 if (!network_time_tracker_) {
380 DCHECK(local_state_);
381 network_time_tracker_.reset(new network_time::NetworkTimeTracker(
382 scoped_ptr<base::TickClock>(new base::DefaultTickClock()),
383 local_state_));
385 return network_time_tracker_.get();
388 gcm::GCMDriver* TestingBrowserProcess::gcm_driver() {
389 return nullptr;
392 memory::OomPriorityManager* TestingBrowserProcess::GetOomPriorityManager() {
393 return nullptr;
396 ShellIntegration::DefaultWebClientState
397 TestingBrowserProcess::CachedDefaultWebClientState() {
398 return ShellIntegration::UNKNOWN_DEFAULT;
400 void TestingBrowserProcess::SetSystemRequestContext(
401 net::URLRequestContextGetter* context_getter) {
402 system_request_context_ = context_getter;
405 void TestingBrowserProcess::SetNotificationUIManager(
406 scoped_ptr<NotificationUIManager> notification_ui_manager) {
407 notification_ui_manager_.swap(notification_ui_manager);
410 void TestingBrowserProcess::SetLocalState(PrefService* local_state) {
411 if (!local_state) {
412 // The local_state_ PrefService is owned outside of TestingBrowserProcess,
413 // but some of the members of TestingBrowserProcess hold references to it
414 // (for example, via PrefNotifier members). But given our test
415 // infrastructure which tears down individual tests before freeing the
416 // TestingBrowserProcess, there's not a good way to make local_state outlive
417 // these dependencies. As a workaround, whenever local_state_ is cleared
418 // (assumedly as part of exiting the test and freeing TestingBrowserProcess)
419 // any components owned by TestingBrowserProcess that depend on local_state
420 // are also freed.
421 network_time_tracker_.reset();
422 #if !defined(OS_IOS)
423 notification_ui_manager_.reset();
424 #endif
425 #if defined(ENABLE_CONFIGURATION_POLICY)
426 ShutdownBrowserPolicyConnector();
427 created_browser_policy_connector_ = false;
428 #endif
430 local_state_ = local_state;
433 void TestingBrowserProcess::SetIOThread(IOThread* io_thread) {
434 io_thread_ = io_thread;
437 void TestingBrowserProcess::ShutdownBrowserPolicyConnector() {
438 #if defined(ENABLE_CONFIGURATION_POLICY)
439 if (browser_policy_connector_)
440 browser_policy_connector_->Shutdown();
441 browser_policy_connector_.reset();
442 #else
443 CHECK(false);
444 #endif
447 void TestingBrowserProcess::SetSafeBrowsingService(
448 SafeBrowsingService* sb_service) {
449 #if defined(OS_IOS)
450 NOTIMPLEMENTED();
451 #else
452 sb_service_ = sb_service;
453 #endif
456 ///////////////////////////////////////////////////////////////////////////////
458 TestingBrowserProcessInitializer::TestingBrowserProcessInitializer() {
459 TestingBrowserProcess::CreateInstance();
462 TestingBrowserProcessInitializer::~TestingBrowserProcessInitializer() {
463 TestingBrowserProcess::DeleteInstance();