blob: 9f34ca30673335b746c3e78e905f665f968d1219 [file] [log] [blame]
[email protected]e7e46732012-01-05 11:45:551// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]a4ff9eae2011-08-01 19:58:165#include "chrome/test/base/testing_profile.h"
initial.commit09911bf2008-07-26 23:55:296
[email protected]a42b5592009-09-03 16:52:237#include "build/build_config.h"
[email protected]dbbad7a2010-08-13 18:18:368
9#include "base/base_paths.h"
[email protected]e3e43d92010-02-26 22:02:3810#include "base/command_line.h"
[email protected]dbbad7a2010-08-13 18:18:3611#include "base/file_util.h"
[email protected]83a7d2eb2010-05-03 21:46:1912#include "base/message_loop_proxy.h"
[email protected]dbbad7a2010-08-13 18:18:3613#include "base/path_service.h"
[email protected]8e937c1e2012-06-28 22:57:3014#include "base/run_loop.h"
[email protected]e83326f2010-07-31 17:29:2515#include "base/string_number_conversions.h"
[email protected]dbbad7a2010-08-13 18:18:3616#include "chrome/browser/autocomplete/autocomplete_classifier.h"
[email protected]a9afddb2009-02-12 17:49:4217#include "chrome/browser/bookmarks/bookmark_model.h"
[email protected]f61f4782012-06-08 21:54:2118#include "chrome/browser/bookmarks/bookmark_model_factory.h"
[email protected]1459fb62011-05-25 19:03:2719#include "chrome/browser/browser_process.h"
[email protected]9d01a6a2010-11-30 12:03:3320#include "chrome/browser/content_settings/host_content_settings_map.h"
[email protected]2f69b382011-02-19 00:34:2521#include "chrome/browser/extensions/extension_service.h"
[email protected]19eb80152011-02-26 00:28:4322#include "chrome/browser/extensions/extension_special_storage_policy.h"
[email protected]31d8f5f22012-04-02 15:22:0823#include "chrome/browser/extensions/extension_system.h"
24#include "chrome/browser/extensions/extension_system_factory.h"
25#include "chrome/browser/extensions/test_extension_system.h"
[email protected]ee2ed42c2011-04-28 22:19:1426#include "chrome/browser/favicon/favicon_service.h"
[email protected]f3d2b312012-08-23 22:27:5927#include "chrome/browser/favicon/favicon_service_factory.h"
[email protected]c476e632011-06-23 11:18:0428#include "chrome/browser/geolocation/chrome_geolocation_permission_context.h"
[email protected]dbbad7a2010-08-13 18:18:3629#include "chrome/browser/history/history.h"
[email protected]075ae732009-02-11 23:58:3130#include "chrome/browser/history/history_backend.h"
[email protected]f61f4782012-06-08 21:54:2131#include "chrome/browser/history/history_service_factory.h"
[email protected]572f40c9b2012-07-06 20:35:3332#include "chrome/browser/history/shortcuts_backend.h"
33#include "chrome/browser/history/shortcuts_backend_factory.h"
[email protected]dbbad7a2010-08-13 18:18:3634#include "chrome/browser/history/top_sites.h"
[email protected]6f96cbcb2011-11-04 02:26:0735#include "chrome/browser/net/proxy_service_factory.h"
[email protected]dbbad7a2010-08-13 18:18:3636#include "chrome/browser/notifications/desktop_notification_service.h"
[email protected]dcb72d52011-04-13 12:36:5337#include "chrome/browser/notifications/desktop_notification_service_factory.h"
[email protected]5c4c89f2012-08-07 21:09:5938#include "chrome/browser/policy/user_cloud_policy_manager.h"
[email protected]37858e52010-08-26 00:22:0239#include "chrome/browser/prefs/browser_prefs.h"
[email protected]9a8c4022011-01-25 14:25:3340#include "chrome/browser/prefs/testing_pref_store.h"
[email protected]4850a7f2011-03-08 23:36:5941#include "chrome/browser/prerender/prerender_manager.h"
[email protected]5cdcd152011-03-31 22:48:5242#include "chrome/browser/profiles/profile_dependency_manager.h"
[email protected]ee78d8a2012-03-15 16:53:4843#include "chrome/browser/protector/protector_service_factory.h"
[email protected]16bd088b2012-04-03 08:12:1244#include "chrome/browser/search_engines/template_url_fetcher_factory.h"
[email protected]c52b2892012-03-07 11:01:0245#include "chrome/browser/speech/chrome_speech_recognition_preferences.h"
[email protected]810ffba2012-06-12 01:07:4846#include "chrome/browser/webdata/web_data_service.h"
[email protected]d07edd42012-05-14 23:49:4647#include "chrome/browser/webdata/web_data_service_factory.h"
[email protected]a9afddb2009-02-12 17:49:4248#include "chrome/common/chrome_constants.h"
[email protected]432115822011-07-10 15:52:2749#include "chrome/common/chrome_notification_types.h"
[email protected]9e33d7e2011-09-30 16:43:5450#include "chrome/common/chrome_switches.h"
[email protected]dbbad7a2010-08-13 18:18:3651#include "chrome/common/url_constants.h"
[email protected]7bc60682011-07-29 20:55:5952#include "chrome/test/base/bookmark_load_observer.h"
[email protected]18a15ca82012-08-30 00:28:1653#include "chrome/test/base/history_index_restore_observer.h"
[email protected]8ad3636e2011-08-01 22:31:4054#include "chrome/test/base/testing_pref_service.h"
[email protected]af44e7fb2011-07-29 18:32:3255#include "chrome/test/base/ui_test_utils.h"
[email protected]c38831a12011-10-28 12:44:4956#include "content/public/browser/browser_thread.h"
[email protected]ad50def52011-10-19 23:17:0757#include "content/public/browser/notification_service.h"
[email protected]fb29e6cf2012-07-12 21:27:2058#include "content/public/browser/render_process_host.h"
[email protected]08a932d52012-06-03 21:42:1259#include "content/public/test/mock_resource_context.h"
[email protected]a7fe9112012-07-20 02:34:4560#include "content/public/test/test_utils.h"
[email protected]aa84a7e2012-03-15 21:29:0661#include "net/cookies/cookie_monster.h"
[email protected]812b3a32010-01-08 05:36:0462#include "net/url_request/url_request_context.h"
[email protected]abe2c032011-03-31 18:49:3463#include "net/url_request/url_request_context_getter.h"
[email protected]d2db0292011-01-26 20:23:4464#include "net/url_request/url_request_test_util.h"
[email protected]4772b072010-03-30 17:45:4665#include "testing/gmock/include/gmock/gmock.h"
[email protected]248ce192011-02-10 15:26:3466
[email protected]21d3a882012-05-31 14:41:5567#if defined(ENABLE_CONFIGURATION_POLICY)
68#include "chrome/browser/policy/policy_service_impl.h"
69#else
70#include "chrome/browser/policy/policy_service_stub.h"
71#endif // defined(ENABLE_CONFIGURATION_POLICY)
72
[email protected]6f96cbcb2011-11-04 02:26:0773#if defined(OS_CHROMEOS)
74#include "chrome/browser/chromeos/proxy_config_service_impl.h"
75#endif // defined(OS_CHROMEOS)
76
[email protected]e1acf6f2008-10-27 20:43:3377using base::Time;
[email protected]631bb742011-11-02 11:29:3978using content::BrowserThread;
[email protected]b441a8492012-06-06 14:55:5779using content::DownloadManagerDelegate;
[email protected]3c887412010-04-19 20:30:2380using testing::NiceMock;
[email protected]4772b072010-03-30 17:45:4681using testing::Return;
[email protected]e1acf6f2008-10-27 20:43:3382
[email protected]d364c652008-08-29 19:46:5683namespace {
84
[email protected]0bfc29a2009-04-27 16:15:4485// Task used to make sure history has finished processing a request. Intended
86// for use with BlockUntilHistoryProcessesPendingRequests.
87
88class QuittingHistoryDBTask : public HistoryDBTask {
89 public:
90 QuittingHistoryDBTask() {}
91
92 virtual bool RunOnDBThread(history::HistoryBackend* backend,
93 history::HistoryDatabase* db) {
94 return true;
95 }
96
97 virtual void DoneRunOnMainThread() {
98 MessageLoop::current()->Quit();
99 }
100
101 private:
[email protected]7991a232009-11-06 01:55:48102 ~QuittingHistoryDBTask() {}
103
[email protected]0bfc29a2009-04-27 16:15:44104 DISALLOW_COPY_AND_ASSIGN(QuittingHistoryDBTask);
105};
106
[email protected]aeb53f02011-01-15 00:21:34107class TestExtensionURLRequestContext : public net::URLRequestContext {
[email protected]c10da4b02010-03-25 14:38:32108 public:
109 TestExtensionURLRequestContext() {
110 net::CookieMonster* cookie_monster = new net::CookieMonster(NULL, NULL);
111 const char* schemes[] = {chrome::kExtensionScheme};
112 cookie_monster->SetCookieableSchemes(schemes, 1);
[email protected]f6c21cb2011-02-16 19:45:41113 set_cookie_store(cookie_monster);
[email protected]c10da4b02010-03-25 14:38:32114 }
[email protected]7b2720b2012-04-25 16:59:11115
[email protected]7b2720b2012-04-25 16:59:11116 virtual ~TestExtensionURLRequestContext() {}
[email protected]c10da4b02010-03-25 14:38:32117};
118
[email protected]abe2c032011-03-31 18:49:34119class TestExtensionURLRequestContextGetter
120 : public net::URLRequestContextGetter {
[email protected]c10da4b02010-03-25 14:38:32121 public:
[email protected]aeb53f02011-01-15 00:21:34122 virtual net::URLRequestContext* GetURLRequestContext() {
[email protected]ef2bf422012-05-11 03:27:09123 if (!context_.get())
124 context_.reset(new TestExtensionURLRequestContext());
[email protected]c10da4b02010-03-25 14:38:32125 return context_.get();
126 }
[email protected]4969b0122012-06-16 01:58:28127 virtual scoped_refptr<base::SingleThreadTaskRunner>
128 GetNetworkTaskRunner() const OVERRIDE {
[email protected]0c7d74f2010-10-11 11:55:26129 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO);
[email protected]83a7d2eb2010-05-03 21:46:19130 }
[email protected]c10da4b02010-03-25 14:38:32131
[email protected]13ed17f82012-04-06 02:27:18132 protected:
133 virtual ~TestExtensionURLRequestContextGetter() {}
134
[email protected]c10da4b02010-03-25 14:38:32135 private:
[email protected]ef2bf422012-05-11 03:27:09136 scoped_ptr<net::URLRequestContext> context_;
[email protected]c10da4b02010-03-25 14:38:32137};
138
[email protected]abc77242012-03-16 04:32:31139ProfileKeyedService* CreateTestDesktopNotificationService(Profile* profile) {
[email protected]29cebbd2012-06-06 21:43:42140#if defined(ENABLE_NOTIFICATIONS)
[email protected]dcb72d52011-04-13 12:36:53141 return new DesktopNotificationService(profile, NULL);
[email protected]29cebbd2012-06-06 21:43:42142#else
143 return NULL;
144#endif
[email protected]dcb72d52011-04-13 12:36:53145}
146
[email protected]d364c652008-08-29 19:46:56147} // namespace
148
[email protected]89dbb1772012-07-17 13:47:25149// static
150#if defined(OS_CHROMEOS)
151// Must be kept in sync with
152// ChromeBrowserMainPartsChromeos::PreEarlyInitialization.
153const char TestingProfile::kTestUserProfileDir[] = "test-user";
154#else
155const char TestingProfile::kTestUserProfileDir[] = "Default";
156#endif
157
[email protected]f25387b2008-08-21 15:20:33158TestingProfile::TestingProfile()
[email protected]ea6f76572008-12-18 00:09:55159 : start_time_(Time::Now()),
[email protected]2fb7dc982010-09-29 12:24:28160 testing_prefs_(NULL),
[email protected]5bb2f522011-03-25 19:04:44161 incognito_(false),
[email protected]5cdcd152011-03-31 22:48:52162 last_session_exited_cleanly_(true),
[email protected]537c1082011-12-02 02:37:17163 profile_dependency_manager_(ProfileDependencyManager::GetInstance()),
164 delegate_(NULL) {
[email protected]5c4c89f2012-08-07 21:09:59165 CreateTempProfileDir();
[email protected]49a25632011-08-31 17:03:48166 profile_path_ = temp_dir_.path();
167
168 Init();
[email protected]537c1082011-12-02 02:37:17169 FinishInit();
[email protected]49a25632011-08-31 17:03:48170}
171
172TestingProfile::TestingProfile(const FilePath& path)
173 : start_time_(Time::Now()),
174 testing_prefs_(NULL),
175 incognito_(false),
176 last_session_exited_cleanly_(true),
177 profile_path_(path),
[email protected]537c1082011-12-02 02:37:17178 profile_dependency_manager_(ProfileDependencyManager::GetInstance()),
179 delegate_(NULL) {
[email protected]49a25632011-08-31 17:03:48180 Init();
[email protected]537c1082011-12-02 02:37:17181 FinishInit();
182}
183
184TestingProfile::TestingProfile(const FilePath& path,
185 Delegate* delegate)
186 : start_time_(Time::Now()),
187 testing_prefs_(NULL),
188 incognito_(false),
189 last_session_exited_cleanly_(true),
190 profile_path_(path),
191 profile_dependency_manager_(ProfileDependencyManager::GetInstance()),
192 delegate_(delegate) {
193 Init();
194 if (delegate_) {
195 MessageLoop::current()->PostTask(FROM_HERE,
196 base::Bind(&TestingProfile::FinishInit,
197 base::Unretained(this)));
198 } else {
199 FinishInit();
200 }
[email protected]49a25632011-08-31 17:03:48201}
202
[email protected]5c4c89f2012-08-07 21:09:59203TestingProfile::TestingProfile(
204 const FilePath& path,
205 Delegate* delegate,
206 scoped_refptr<ExtensionSpecialStoragePolicy> extension_policy,
207 scoped_ptr<PrefService> prefs,
208 scoped_ptr<policy::UserCloudPolicyManager> user_cloud_policy_manager)
209 : start_time_(Time::Now()),
210 prefs_(prefs.release()),
211 testing_prefs_(NULL),
212 incognito_(false),
213 last_session_exited_cleanly_(true),
214 extension_special_storage_policy_(extension_policy),
215 user_cloud_policy_manager_(user_cloud_policy_manager.release()),
216 profile_path_(path),
217 profile_dependency_manager_(ProfileDependencyManager::GetInstance()),
218 delegate_(delegate) {
219
220 // If no profile path was supplied, create one.
221 if (profile_path_.empty()) {
222 CreateTempProfileDir();
223 profile_path_ = temp_dir_.path();
224 }
225
226 Init();
227 // If caller supplied a delegate, delay the FinishInit invocation until other
228 // tasks have run.
229 // TODO(atwilson): See if this is still required once we convert the current
230 // users of the constructor that takes a Delegate* param.
231 if (delegate_) {
232 MessageLoop::current()->PostTask(FROM_HERE,
233 base::Bind(&TestingProfile::FinishInit,
234 base::Unretained(this)));
235 } else {
236 FinishInit();
237 }
238}
239
240void TestingProfile::CreateTempProfileDir() {
241 if (!temp_dir_.CreateUniqueTempDir()) {
242 LOG(ERROR) << "Failed to create unique temporary directory.";
243
244 // Fallback logic in case we fail to create unique temporary directory.
245 FilePath system_tmp_dir;
246 bool success = PathService::Get(base::DIR_TEMP, &system_tmp_dir);
247
248 // We're severly screwed if we can't get the system temporary
249 // directory. Die now to avoid writing to the filesystem root
250 // or other bad places.
251 CHECK(success);
252
253 FilePath fallback_dir(system_tmp_dir.AppendASCII("TestingProfilePath"));
254 file_util::Delete(fallback_dir, true);
255 file_util::CreateDirectory(fallback_dir);
256 if (!temp_dir_.Set(fallback_dir)) {
257 // That shouldn't happen, but if it does, try to recover.
258 LOG(ERROR) << "Failed to use a fallback temporary directory.";
259
260 // We're screwed if this fails, see CHECK above.
261 CHECK(temp_dir_.Set(system_tmp_dir));
262 }
263 }
264}
265
[email protected]49a25632011-08-31 17:03:48266void TestingProfile::Init() {
[email protected]d41e6572012-05-04 20:49:42267 if (!file_util::PathExists(profile_path_))
268 file_util::CreateDirectory(profile_path_);
269
[email protected]bd306722012-07-11 20:43:59270 extensions::ExtensionSystemFactory::GetInstance()->SetTestingFactory(
271 this, extensions::TestExtensionSystem::Build);
[email protected]31d8f5f22012-04-02 15:22:08272
[email protected]49a25632011-08-31 17:03:48273 profile_dependency_manager_->CreateProfileServices(this, true);
274
[email protected]5fd2e842012-03-01 00:29:11275#if defined(ENABLE_NOTIFICATIONS)
[email protected]dcb72d52011-04-13 12:36:53276 // Install profile keyed service factory hooks for dummy/test services
[email protected]7722653b2011-06-03 17:28:22277 DesktopNotificationServiceFactory::GetInstance()->SetTestingFactory(
278 this, CreateTestDesktopNotificationService);
[email protected]5fd2e842012-03-01 00:29:11279#endif
[email protected]537c1082011-12-02 02:37:17280}
[email protected]12463cd2011-07-19 09:40:20281
[email protected]537c1082011-12-02 02:37:17282void TestingProfile::FinishInit() {
[email protected]080adba92011-12-07 19:00:40283 DCHECK(content::NotificationService::current());
[email protected]ad50def52011-10-19 23:17:07284 content::NotificationService::current()->Notify(
[email protected]12463cd2011-07-19 09:40:20285 chrome::NOTIFICATION_PROFILE_CREATED,
[email protected]6c2381d2011-10-19 02:52:53286 content::Source<Profile>(static_cast<Profile*>(this)),
[email protected]ad50def52011-10-19 23:17:07287 content::NotificationService::NoDetails());
[email protected]537c1082011-12-02 02:37:17288
289 if (delegate_)
[email protected]00b33f802012-03-13 07:46:03290 delegate_->OnProfileCreated(this, true, false);
[email protected]ab23dbe2010-08-12 02:10:46291}
292
[email protected]f25387b2008-08-21 15:20:33293TestingProfile::~TestingProfile() {
[email protected]a7c4c032012-07-27 00:36:32294 MaybeSendDestroyedNotification();
[email protected]5cdcd152011-03-31 22:48:52295
296 profile_dependency_manager_->DestroyProfileServices(this);
297
[email protected]35552dc52011-07-12 09:04:38298 if (host_content_settings_map_)
299 host_content_settings_map_->ShutdownOnUIThread();
300
[email protected]809cc4d2010-10-27 15:22:54301 DestroyTopSites();
[email protected]cdba46992011-06-07 11:51:39302
[email protected]cc5bfd42010-11-24 14:44:02303 if (pref_proxy_config_tracker_.get())
304 pref_proxy_config_tracker_->DetachFromPrefService();
[email protected]f25387b2008-08-21 15:20:33305}
initial.commit09911bf2008-07-26 23:55:29306
[email protected]f3d2b312012-08-23 22:27:59307static ProfileKeyedService* BuildFaviconService(Profile* profile) {
308 return new FaviconService(
309 HistoryServiceFactory::GetForProfileWithoutCreating(profile));
310}
311
[email protected]d2879af2010-02-08 16:02:56312void TestingProfile::CreateFaviconService() {
[email protected]f3d2b312012-08-23 22:27:59313 // It is up to the caller to create the history service if one is needed.
314 FaviconServiceFactory::GetInstance()->SetTestingFactory(
315 this, BuildFaviconService);
[email protected]d2879af2010-02-08 16:02:56316}
317
[email protected]f61f4782012-06-08 21:54:21318static scoped_refptr<RefcountedProfileKeyedService> BuildHistoryService(
319 Profile* profile) {
320 return new HistoryService(profile);
321}
322
[email protected]d486a0852009-11-02 21:40:00323void TestingProfile::CreateHistoryService(bool delete_file, bool no_db) {
[email protected]809cc4d2010-10-27 15:22:54324 DestroyHistoryService();
[email protected]f25387b2008-08-21 15:20:33325 if (delete_file) {
[email protected]f7011fcb2009-01-28 21:54:32326 FilePath path = GetPath();
327 path = path.Append(chrome::kHistoryFilename);
[email protected]f25387b2008-08-21 15:20:33328 file_util::Delete(path, false);
329 }
[email protected]7019bb6d2012-08-23 09:09:48330 // This will create and init the history service.
[email protected]f61f4782012-06-08 21:54:21331 HistoryService* history_service = static_cast<HistoryService*>(
332 HistoryServiceFactory::GetInstance()->SetTestingFactoryAndUse(
333 this, BuildHistoryService).get());
[email protected]7019bb6d2012-08-23 09:09:48334 if (!history_service->Init(this->GetPath(),
335 BookmarkModelFactory::GetForProfile(this),
336 no_db)) {
[email protected]f61f4782012-06-08 21:54:21337 HistoryServiceFactory::GetInstance()->SetTestingFactoryAndUse(this, NULL);
[email protected]7019bb6d2012-08-23 09:09:48338 }
initial.commit09911bf2008-07-26 23:55:29339}
340
341void TestingProfile::DestroyHistoryService() {
[email protected]f61f4782012-06-08 21:54:21342 scoped_refptr<HistoryService> history_service =
[email protected]6c537042012-07-13 00:59:02343 HistoryServiceFactory::GetForProfileWithoutCreating(this);
[email protected]f61f4782012-06-08 21:54:21344 if (!history_service.get())
initial.commit09911bf2008-07-26 23:55:29345 return;
346
[email protected]f61f4782012-06-08 21:54:21347 history_service->NotifyRenderProcessHostDestruction(0);
348 history_service->SetOnBackendDestroyTask(MessageLoop::QuitClosure());
349 history_service->Cleanup();
350 HistoryServiceFactory::ShutdownForProfile(this);
initial.commit09911bf2008-07-26 23:55:29351
352 // Wait for the backend class to terminate before deleting the files and
353 // moving to the next test. Note: if this never terminates, somebody is
354 // probably leaking a reference to the history backend, so it never calls
355 // our destroy task.
356 MessageLoop::current()->Run();
357
initial.commit09911bf2008-07-26 23:55:29358 // Make sure we don't have any event pending that could disrupt the next
359 // test.
[email protected]1cb1a242011-12-10 18:36:51360 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
initial.commit09911bf2008-07-26 23:55:29361 MessageLoop::current()->Run();
362}
[email protected]4d0cd7ce2008-08-11 16:40:57363
[email protected]809cc4d2010-10-27 15:22:54364void TestingProfile::CreateTopSites() {
365 DestroyTopSites();
366 top_sites_ = new history::TopSites(this);
[email protected]73c47932010-12-06 18:13:43367 FilePath file_name = GetPath().Append(chrome::kTopSitesFilename);
[email protected]809cc4d2010-10-27 15:22:54368 top_sites_->Init(file_name);
369}
370
371void TestingProfile::DestroyTopSites() {
372 if (top_sites_.get()) {
373 top_sites_->Shutdown();
374 top_sites_ = NULL;
375 // TopSites::Shutdown schedules some tasks (from TopSitesBackend) that need
376 // to be run to properly shutdown. Run all pending tasks now. This is
377 // normally handled by browser_process shutdown.
378 if (MessageLoop::current())
379 MessageLoop::current()->RunAllPending();
380 }
381}
382
[email protected]f61f4782012-06-08 21:54:21383static ProfileKeyedService* BuildBookmarkModel(Profile* profile) {
384 BookmarkModel* bookmark_model = new BookmarkModel(profile);
385 bookmark_model->Load();
386 return bookmark_model;
387}
388
389
[email protected]d8e41ed2008-09-11 15:22:32390void TestingProfile::CreateBookmarkModel(bool delete_file) {
[email protected]90ef13132008-08-27 03:27:46391
392 if (delete_file) {
[email protected]f7011fcb2009-01-28 21:54:32393 FilePath path = GetPath();
394 path = path.Append(chrome::kBookmarksFileName);
[email protected]90ef13132008-08-27 03:27:46395 file_util::Delete(path, false);
396 }
[email protected]f61f4782012-06-08 21:54:21397 // This will create a bookmark model.
398 BookmarkModel* bookmark_service =
399 static_cast<BookmarkModel*>(
400 BookmarkModelFactory::GetInstance()->SetTestingFactoryAndUse(
401 this, BuildBookmarkModel));
402
403 HistoryService* history_service =
[email protected]6c537042012-07-13 00:59:02404 HistoryServiceFactory::GetForProfileWithoutCreating(this).get();
[email protected]f61f4782012-06-08 21:54:21405 if (history_service) {
406 history_service->history_backend_->bookmark_service_ =
407 bookmark_service;
408 history_service->history_backend_->expirer_.bookmark_service_ =
409 bookmark_service;
[email protected]90ef13132008-08-27 03:27:46410 }
[email protected]4d0cd7ce2008-08-11 16:40:57411}
[email protected]d2c017a2008-08-13 21:51:45412
[email protected]d07edd42012-05-14 23:49:46413static scoped_refptr<RefcountedProfileKeyedService> BuildWebDataService(
414 Profile* profile) {
415 WebDataService* web_data_service = new WebDataService();
416 if (web_data_service)
417 web_data_service->Init(profile->GetPath());
[email protected]a5c65292012-05-25 00:50:01418 return web_data_service;
[email protected]d07edd42012-05-14 23:49:46419}
[email protected]2609bc12010-01-24 08:32:55420
[email protected]d07edd42012-05-14 23:49:46421void TestingProfile::CreateWebDataService() {
[email protected]a5c65292012-05-25 00:50:01422 WebDataServiceFactory::GetInstance()->SetTestingFactory(
[email protected]d07edd42012-05-14 23:49:46423 this, BuildWebDataService);
[email protected]2609bc12010-01-24 08:32:55424}
425
[email protected]d364c652008-08-29 19:46:56426void TestingProfile::BlockUntilBookmarkModelLoaded() {
[email protected]d7f7f752012-08-08 02:02:55427 // Only get the bookmark model if it actually exists since the caller of the
428 // test should explicitly call CreateBookmarkModel to build it.
429 BookmarkModel* bookmark_model =
430 BookmarkModelFactory::GetForProfileIfExists(this);
431 DCHECK(bookmark_model);
432 if (bookmark_model->IsLoaded())
[email protected]d364c652008-08-29 19:46:56433 return;
[email protected]8e937c1e2012-06-28 22:57:30434 base::RunLoop run_loop;
[email protected]78089f02012-07-19 06:11:28435 BookmarkLoadObserver observer(content::GetQuitTaskForRunLoop(&run_loop));
[email protected]d7f7f752012-08-08 02:02:55436 bookmark_model->AddObserver(&observer);
[email protected]8e937c1e2012-06-28 22:57:30437 run_loop.Run();
[email protected]d7f7f752012-08-08 02:02:55438 bookmark_model->RemoveObserver(&observer);
439 DCHECK(bookmark_model->IsLoaded());
[email protected]d364c652008-08-29 19:46:56440}
441
[email protected]18a15ca82012-08-30 00:28:16442void TestingProfile::BlockUntilHistoryIndexIsRefreshed() {
443 // Only get the history service if it actually exists since the caller of the
444 // test should explicitly call CreateHistoryService to build it.
445 HistoryService* history_service =
446 HistoryServiceFactory::GetForProfileWithoutCreating(this);
447 DCHECK(history_service);
448 history::InMemoryURLIndex* index = history_service->InMemoryIndex();
449 if (!index || index->restored())
450 return;
451 base::RunLoop run_loop;
452 HistoryIndexRestoreObserver observer(
453 content::GetQuitTaskForRunLoop(&run_loop));
454 index->set_restore_cache_observer(&observer);
455 run_loop.Run();
456 index->set_restore_cache_observer(NULL);
457 DCHECK(index->restored());
458}
459
[email protected]120abf132011-09-27 21:38:06460// TODO(phajdan.jr): Doesn't this hang if Top Sites are already loaded?
[email protected]809cc4d2010-10-27 15:22:54461void TestingProfile::BlockUntilTopSitesLoaded() {
[email protected]a7fe9112012-07-20 02:34:45462 content::WindowedNotificationObserver top_sites_loaded_observer(
[email protected]120abf132011-09-27 21:38:06463 chrome::NOTIFICATION_TOP_SITES_LOADED,
[email protected]ad50def52011-10-19 23:17:07464 content::NotificationService::AllSources());
[email protected]d7f7f752012-08-08 02:02:55465 if (!HistoryServiceFactory::GetForProfile(this, Profile::EXPLICIT_ACCESS))
[email protected]809cc4d2010-10-27 15:22:54466 GetTopSites()->HistoryLoaded();
[email protected]120abf132011-09-27 21:38:06467 top_sites_loaded_observer.Wait();
[email protected]809cc4d2010-10-27 15:22:54468}
469
[email protected]f9dec9482010-08-20 20:42:19470FilePath TestingProfile::GetPath() {
[email protected]49a25632011-08-31 17:03:48471 return profile_path_;
[email protected]f9dec9482010-08-20 20:42:19472}
473
[email protected]dbbad7a2010-08-13 18:18:36474TestingPrefService* TestingProfile::GetTestingPrefService() {
[email protected]2fb7dc982010-09-29 12:24:28475 if (!prefs_.get())
476 CreateTestingPrefService();
477 DCHECK(testing_prefs_);
478 return testing_prefs_;
[email protected]dbbad7a2010-08-13 18:18:36479}
480
[email protected]8e5c89a2011-06-07 18:13:33481TestingProfile* TestingProfile::AsTestingProfile() {
482 return this;
483}
484
[email protected]29d70252011-04-28 02:16:58485std::string TestingProfile::GetProfileName() {
486 return std::string("testing_profile");
487}
488
[email protected]27d6e852012-03-02 21:31:32489bool TestingProfile::IsOffTheRecord() const {
[email protected]5bb2f522011-03-25 19:04:44490 return incognito_;
[email protected]fadc607b62011-02-07 17:55:50491}
492
[email protected]93dba942011-02-23 22:45:01493void TestingProfile::SetOffTheRecordProfile(Profile* profile) {
[email protected]5bb2f522011-03-25 19:04:44494 incognito_profile_.reset(profile);
[email protected]93dba942011-02-23 22:45:01495}
496
[email protected]fadc607b62011-02-07 17:55:50497Profile* TestingProfile::GetOffTheRecordProfile() {
[email protected]5bb2f522011-03-25 19:04:44498 return incognito_profile_.get();
[email protected]fadc607b62011-02-07 17:55:50499}
500
[email protected]d4f5d1162011-11-30 01:41:52501GAIAInfoUpdateService* TestingProfile::GetGAIAInfoUpdateService() {
502 return NULL;
503}
504
[email protected]fadc607b62011-02-07 17:55:50505bool TestingProfile::HasOffTheRecordProfile() {
[email protected]5bb2f522011-03-25 19:04:44506 return incognito_profile_.get() != NULL;
[email protected]fadc607b62011-02-07 17:55:50507}
508
509Profile* TestingProfile::GetOriginalProfile() {
510 return this;
511}
512
[email protected]fadc607b62011-02-07 17:55:50513VisitedLinkMaster* TestingProfile::GetVisitedLinkMaster() {
514 return NULL;
515}
516
[email protected]eaa7dd182010-12-14 11:09:00517ExtensionService* TestingProfile::GetExtensionService() {
[email protected]bd306722012-07-11 20:43:59518 return extensions::ExtensionSystem::Get(this)->extension_service();
[email protected]2fb7dc982010-09-29 12:24:28519}
520
[email protected]20f97c92012-07-13 23:12:37521extensions::UserScriptMaster* TestingProfile::GetUserScriptMaster() {
[email protected]bd306722012-07-11 20:43:59522 return extensions::ExtensionSystem::Get(this)->user_script_master();
[email protected]fadc607b62011-02-07 17:55:50523}
524
525ExtensionProcessManager* TestingProfile::GetExtensionProcessManager() {
[email protected]bd306722012-07-11 20:43:59526 return extensions::ExtensionSystem::Get(this)->process_manager();
[email protected]fadc607b62011-02-07 17:55:50527}
528
[email protected]5a38dfd2012-07-23 23:22:10529extensions::EventRouter* TestingProfile::GetExtensionEventRouter() {
[email protected]bd306722012-07-11 20:43:59530 return extensions::ExtensionSystem::Get(this)->event_router();
[email protected]fadc607b62011-02-07 17:55:50531}
532
[email protected]2261e7b72011-07-12 13:43:55533void TestingProfile::SetExtensionSpecialStoragePolicy(
534 ExtensionSpecialStoragePolicy* extension_special_storage_policy) {
535 extension_special_storage_policy_ = extension_special_storage_policy;
536}
537
[email protected]19eb80152011-02-26 00:28:43538ExtensionSpecialStoragePolicy*
539TestingProfile::GetExtensionSpecialStoragePolicy() {
[email protected]cdba46992011-06-07 11:51:39540 if (!extension_special_storage_policy_.get())
[email protected]7c5048c2011-08-19 17:07:05541 extension_special_storage_policy_ = new ExtensionSpecialStoragePolicy(NULL);
[email protected]cdba46992011-06-07 11:51:39542 return extension_special_storage_policy_.get();
[email protected]19eb80152011-02-26 00:28:43543}
544
[email protected]dbbad7a2010-08-13 18:18:36545net::CookieMonster* TestingProfile::GetCookieMonster() {
546 if (!GetRequestContext())
547 return NULL;
[email protected]277ec262011-03-30 21:09:40548 return GetRequestContext()->GetURLRequestContext()->cookie_store()->
549 GetCookieMonster();
[email protected]dbbad7a2010-08-13 18:18:36550}
551
[email protected]5c4c89f2012-08-07 21:09:59552policy::UserCloudPolicyManager* TestingProfile::GetUserCloudPolicyManager() {
553 return user_cloud_policy_manager_.get();
554}
555
[email protected]21d3a882012-05-31 14:41:55556policy::PolicyService* TestingProfile::GetPolicyService() {
557 if (!policy_service_.get()) {
558#if defined(ENABLE_CONFIGURATION_POLICY)
559 policy::PolicyServiceImpl::Providers providers;
560 policy_service_.reset(new policy::PolicyServiceImpl(providers));
561#else
562 policy_service_.reset(new policy::PolicyServiceStub());
563#endif
564 }
565 return policy_service_.get();
566}
567
[email protected]2fb7dc982010-09-29 12:24:28568void TestingProfile::SetPrefService(PrefService* prefs) {
[email protected]ddcaa412012-03-30 19:57:29569#if defined(ENABLE_PROTECTOR_SERVICE)
[email protected]ee78d8a2012-03-15 16:53:48570 // ProtectorService binds itself very closely to the PrefService at the moment
571 // of Profile creation and watches pref changes to update their backup.
572 // For tests that replace the PrefService after TestingProfile creation,
573 // ProtectorService is disabled to prevent further invalid memory accesses.
574 protector::ProtectorServiceFactory::GetInstance()->
575 SetTestingFactory(this, NULL);
[email protected]ddcaa412012-03-30 19:57:29576#endif
[email protected]2fb7dc982010-09-29 12:24:28577 prefs_.reset(prefs);
578}
579
580void TestingProfile::CreateTestingPrefService() {
581 DCHECK(!prefs_.get());
582 testing_prefs_ = new TestingPrefService();
583 prefs_.reset(testing_prefs_);
584 Profile::RegisterUserPrefs(prefs_.get());
[email protected]572f40c9b2012-07-06 20:35:33585 chrome::RegisterUserPrefs(prefs_.get());
[email protected]2fb7dc982010-09-29 12:24:28586}
587
[email protected]dbbad7a2010-08-13 18:18:36588PrefService* TestingProfile::GetPrefs() {
589 if (!prefs_.get()) {
[email protected]2fb7dc982010-09-29 12:24:28590 CreateTestingPrefService();
[email protected]dbbad7a2010-08-13 18:18:36591 }
592 return prefs_.get();
593}
594
595history::TopSites* TestingProfile::GetTopSites() {
[email protected]809cc4d2010-10-27 15:22:54596 return top_sites_.get();
[email protected]dbbad7a2010-08-13 18:18:36597}
598
[email protected]fadc607b62011-02-07 17:55:50599history::TopSites* TestingProfile::GetTopSitesWithoutCreating() {
600 return top_sites_.get();
601}
602
[email protected]b441a8492012-06-06 14:55:57603DownloadManagerDelegate* TestingProfile::GetDownloadManagerDelegate() {
[email protected]fadc607b62011-02-07 17:55:50604 return NULL;
605}
606
[email protected]abe2c032011-03-31 18:49:34607net::URLRequestContextGetter* TestingProfile::GetRequestContext() {
[email protected]812b3a32010-01-08 05:36:04608 return request_context_.get();
609}
610
[email protected]da5683db2011-04-23 17:12:21611net::URLRequestContextGetter* TestingProfile::GetRequestContextForRenderProcess(
612 int renderer_child_id) {
[email protected]31d8f5f22012-04-02 15:22:08613 ExtensionService* extension_service =
[email protected]bd306722012-07-11 20:43:59614 extensions::ExtensionSystem::Get(this)->extension_service();
[email protected]31d8f5f22012-04-02 15:22:08615 if (extension_service) {
[email protected]1c321ee2012-05-21 03:02:34616 const extensions::Extension* installed_app = extension_service->
[email protected]da5683db2011-04-23 17:12:21617 GetInstalledAppForRenderer(renderer_child_id);
618 if (installed_app != NULL && installed_app->is_storage_isolated())
[email protected]10eb28162012-09-18 03:04:09619 return GetRequestContextForStoragePartition(installed_app->id());
[email protected]da5683db2011-04-23 17:12:21620 }
[email protected]d9696672011-03-15 22:45:09621
[email protected]fb29e6cf2012-07-12 21:27:20622 content::RenderProcessHost* rph = content::RenderProcessHost::FromID(
623 renderer_child_id);
624 if (rph && rph->IsGuest()) {
625 // For guest processes (used by the browser tag), we need to isolate the
626 // storage.
627 // TODO(nasko): Until we have proper storage partitions, create a
628 // non-persistent context using the RPH's id.
629 std::string id("guest-");
630 id.append(base::IntToString(renderer_child_id));
[email protected]10eb28162012-09-18 03:04:09631 return GetRequestContextForStoragePartition(id);
[email protected]fb29e6cf2012-07-12 21:27:20632 }
633
[email protected]d9696672011-03-15 22:45:09634 return GetRequestContext();
635}
636
[email protected]812b3a32010-01-08 05:36:04637void TestingProfile::CreateRequestContext() {
638 if (!request_context_)
[email protected]44738602012-03-02 22:56:01639 request_context_ =
640 new TestURLRequestContextGetter(
641 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
[email protected]812b3a32010-01-08 05:36:04642}
643
[email protected]7c893202010-10-07 20:18:02644void TestingProfile::ResetRequestContext() {
[email protected]3931fd82012-03-12 21:18:48645 // Any objects holding live URLFetchers should be deleted before the request
646 // context is shut down.
[email protected]16bd088b2012-04-03 08:12:12647 TemplateURLFetcherFactory::ShutdownForProfile(this);
[email protected]3931fd82012-03-12 21:18:48648
[email protected]7c893202010-10-07 20:18:02649 request_context_ = NULL;
650}
651
[email protected]10705a7b2012-08-21 19:07:08652net::URLRequestContextGetter* TestingProfile::GetMediaRequestContext() {
653 return NULL;
654}
655
656net::URLRequestContextGetter*
657TestingProfile::GetMediaRequestContextForRenderProcess(
658 int renderer_child_id) {
[email protected]fadc607b62011-02-07 17:55:50659 return NULL;
660}
661
[email protected]10eb28162012-09-18 03:04:09662net::URLRequestContextGetter*
663TestingProfile::GetMediaRequestContextForStoragePartition(
664 const std::string& partition_id) {
665 return NULL;
666}
667
[email protected]abe2c032011-03-31 18:49:34668net::URLRequestContextGetter* TestingProfile::GetRequestContextForExtensions() {
[email protected]c10da4b02010-03-25 14:38:32669 if (!extensions_request_context_)
[email protected]00dea9582012-05-09 15:20:05670 extensions_request_context_ = new TestExtensionURLRequestContextGetter();
[email protected]c10da4b02010-03-25 14:38:32671 return extensions_request_context_.get();
672}
673
[email protected]fadc607b62011-02-07 17:55:50674net::SSLConfigService* TestingProfile::GetSSLConfigService() {
675 return NULL;
676}
677
[email protected]10eb28162012-09-18 03:04:09678net::URLRequestContextGetter*
679TestingProfile::GetRequestContextForStoragePartition(
680 const std::string& partition_id) {
681 // We don't test storage partitions here yet, so returning the same dummy
[email protected]d9696672011-03-15 22:45:09682 // context is sufficient for now.
683 return GetRequestContext();
684}
685
[email protected]df02aca2012-02-09 21:03:20686content::ResourceContext* TestingProfile::GetResourceContext() {
[email protected]314c3e22012-02-21 03:57:42687 if (!resource_context_.get())
688 resource_context_.reset(new content::MockResourceContext());
689 return resource_context_.get();
[email protected]e89b77d2011-04-15 18:58:10690}
691
[email protected]dbbad7a2010-08-13 18:18:36692HostContentSettingsMap* TestingProfile::GetHostContentSettingsMap() {
[email protected]35552dc52011-07-12 09:04:38693 if (!host_content_settings_map_.get()) {
[email protected]646c29cb62012-06-18 14:31:05694 host_content_settings_map_ = new HostContentSettingsMap(GetPrefs(), false);
[email protected]701550ad2012-09-06 23:43:29695#if defined(ENABLE_EXTENSIONS)
[email protected]646c29cb62012-06-18 14:31:05696 ExtensionService* extension_service = GetExtensionService();
697 if (extension_service)
698 host_content_settings_map_->RegisterExtensionService(extension_service);
[email protected]701550ad2012-09-06 23:43:29699#endif
[email protected]35552dc52011-07-12 09:04:38700 }
[email protected]dbbad7a2010-08-13 18:18:36701 return host_content_settings_map_.get();
702}
703
[email protected]810ddc52012-01-24 01:00:35704content::GeolocationPermissionContext*
[email protected]dbbad7a2010-08-13 18:18:36705TestingProfile::GetGeolocationPermissionContext() {
706 if (!geolocation_permission_context_.get()) {
707 geolocation_permission_context_ =
[email protected]c476e632011-06-23 11:18:04708 new ChromeGeolocationPermissionContext(this);
[email protected]dbbad7a2010-08-13 18:18:36709 }
710 return geolocation_permission_context_.get();
711}
712
[email protected]c52b2892012-03-07 11:01:02713content::SpeechRecognitionPreferences*
714 TestingProfile::GetSpeechRecognitionPreferences() {
[email protected]3297132a2012-02-21 23:27:59715#if defined(ENABLE_INPUT_SPEECH)
[email protected]750e9572012-06-06 15:21:24716 return ChromeSpeechRecognitionPreferences::GetForProfile(this);
[email protected]3297132a2012-02-21 23:27:59717#else
718 return NULL;
719#endif
[email protected]8238dd62011-09-29 15:13:01720}
721
[email protected]fadc607b62011-02-07 17:55:50722std::wstring TestingProfile::GetName() {
723 return std::wstring();
724}
725
726std::wstring TestingProfile::GetID() {
727 return id_;
728}
729
730void TestingProfile::SetID(const std::wstring& id) {
731 id_ = id;
732}
733
734bool TestingProfile::DidLastSessionExitCleanly() {
735 return last_session_exited_cleanly_;
736}
737
[email protected]fadc607b62011-02-07 17:55:50738bool TestingProfile::IsSameProfile(Profile *p) {
739 return this == p;
740}
741
742base::Time TestingProfile::GetStartTime() const {
743 return start_time_;
744}
745
[email protected]a6d36cc2011-02-23 00:39:48746ProtocolHandlerRegistry* TestingProfile::GetProtocolHandlerRegistry() {
[email protected]ab563ff2012-07-21 00:26:32747 return NULL;
[email protected]a6d36cc2011-02-23 00:39:48748}
749
[email protected]fadc607b62011-02-07 17:55:50750FilePath TestingProfile::last_selected_directory() {
751 return last_selected_directory_;
752}
753
754void TestingProfile::set_last_selected_directory(const FilePath& path) {
755 last_selected_directory_ = path;
756}
757
[email protected]cc5bfd42010-11-24 14:44:02758PrefProxyConfigTracker* TestingProfile::GetProxyConfigTracker() {
[email protected]6f96cbcb2011-11-04 02:26:07759 if (!pref_proxy_config_tracker_.get()) {
760 pref_proxy_config_tracker_.reset(
761 ProxyServiceFactory::CreatePrefProxyConfigTracker(GetPrefs()));
762 }
763 return pref_proxy_config_tracker_.get();
[email protected]cc5bfd42010-11-24 14:44:02764}
765
[email protected]0bfc29a2009-04-27 16:15:44766void TestingProfile::BlockUntilHistoryProcessesPendingRequests() {
[email protected]f61f4782012-06-08 21:54:21767 scoped_refptr<HistoryService> history_service =
[email protected]6c537042012-07-13 00:59:02768 HistoryServiceFactory::GetForProfile(this, Profile::EXPLICIT_ACCESS);
[email protected]f61f4782012-06-08 21:54:21769 DCHECK(history_service.get());
[email protected]0bfc29a2009-04-27 16:15:44770 DCHECK(MessageLoop::current());
771
772 CancelableRequestConsumer consumer;
[email protected]f61f4782012-06-08 21:54:21773 history_service->ScheduleDBTask(new QuittingHistoryDBTask(), &consumer);
[email protected]0bfc29a2009-04-27 16:15:44774 MessageLoop::current()->Run();
775}
[email protected]345a8b72009-09-29 09:11:44776
[email protected]8382d8c2011-09-15 03:43:17777chrome_browser_net::Predictor* TestingProfile::GetNetworkPredictor() {
778 return NULL;
779}
780
[email protected]db96a882011-10-09 02:01:54781void TestingProfile::ClearNetworkingHistorySince(base::Time time) {
[email protected]02896a82011-09-21 18:54:32782 NOTIMPLEMENTED();
783}
784
[email protected]671f9c62011-10-28 19:22:07785GURL TestingProfile::GetHomePage() {
786 return GURL(chrome::kChromeUINewTabURL);
787}
788
[email protected]fadc607b62011-02-07 17:55:50789PrefService* TestingProfile::GetOffTheRecordPrefs() {
790 return NULL;
791}
792
[email protected]36fb2c7c2011-04-04 15:49:08793quota::SpecialStoragePolicy* TestingProfile::GetSpecialStoragePolicy() {
794 return GetExtensionSpecialStoragePolicy();
795}
796
[email protected]1bee6ed2012-03-14 06:46:36797bool TestingProfile::WasCreatedByVersionOrLater(const std::string& version) {
798 return true;
799}
[email protected]ef92e172012-04-25 19:40:41800
801base::Callback<ChromeURLDataManagerBackend*(void)>
802 TestingProfile::GetChromeURLDataManagerBackendGetter() const {
803 return base::Callback<ChromeURLDataManagerBackend*(void)>();
804}
[email protected]5c4c89f2012-08-07 21:09:59805
806TestingProfile::Builder::Builder()
807 : build_called_(false),
808 delegate_(NULL) {
809}
810
811TestingProfile::Builder::~Builder() {
812}
813
814void TestingProfile::Builder::SetPath(const FilePath& path) {
815 path_ = path;
816}
817
818void TestingProfile::Builder::SetDelegate(Delegate* delegate) {
819 delegate_ = delegate;
820}
821
822void TestingProfile::Builder::SetExtensionSpecialStoragePolicy(
823 scoped_refptr<ExtensionSpecialStoragePolicy> policy) {
824 extension_policy_ = policy;
825}
826
827void TestingProfile::Builder::SetPrefService(scoped_ptr<PrefService> prefs) {
828 pref_service_ = prefs.Pass();
829}
830
831void TestingProfile::Builder::SetUserCloudPolicyManager(
832 scoped_ptr<policy::UserCloudPolicyManager> manager) {
833 user_cloud_policy_manager_ = manager.Pass();
834}
835
836scoped_ptr<TestingProfile> TestingProfile::Builder::Build() {
837 DCHECK(!build_called_);
838 build_called_ = true;
839 return scoped_ptr<TestingProfile>(new TestingProfile(
840 path_,
841 delegate_,
842 extension_policy_,
843 pref_service_.Pass(),
844 user_cloud_policy_manager_.Pass()));
845}