blob: 547aff0713df512daeb6c980cbe0922a9ac92c13 [file] [log] [blame]
[email protected]69c579e2010-04-23 20:01:001// Copyright (c) 2010 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
5#include "chrome/test/testing_profile.h"
6
[email protected]a42b5592009-09-03 16:52:237#include "build/build_config.h"
[email protected]e3e43d92010-02-26 22:02:388#include "base/command_line.h"
[email protected]83a7d2eb2010-05-03 21:46:199#include "base/message_loop_proxy.h"
[email protected]e83326f2010-07-31 17:29:2510#include "base/string_number_conversions.h"
[email protected]c10da4b02010-03-25 14:38:3211#include "chrome/common/url_constants.h"
[email protected]a9afddb2009-02-12 17:49:4212#include "chrome/browser/bookmarks/bookmark_model.h"
[email protected]156ed27b2009-11-23 18:31:2513#include "chrome/browser/dom_ui/ntp_resource_cache.h"
[email protected]075ae732009-02-11 23:58:3114#include "chrome/browser/history/history_backend.h"
[email protected]8e4c29612010-07-14 01:24:4515#include "chrome/browser/net/gaia/token_service.h"
[email protected]8cb5d5b2010-02-09 11:36:1616#include "chrome/browser/sessions/session_service.h"
[email protected]4772b072010-03-30 17:45:4617#include "chrome/browser/sync/profile_sync_service_mock.h"
[email protected]a9afddb2009-02-12 17:49:4218#include "chrome/common/chrome_constants.h"
[email protected]68d2a05f2010-05-07 21:39:5519#include "chrome/common/net/url_request_context_getter.h"
[email protected]d2879af2010-02-08 16:02:5620#include "chrome/common/notification_service.h"
[email protected]812b3a32010-01-08 05:36:0421#include "net/url_request/url_request_context.h"
[email protected]4772b072010-03-30 17:45:4622#include "testing/gmock/include/gmock/gmock.h"
[email protected]2f351cb2009-11-09 23:43:3423#include "webkit/database/database_tracker.h"
[email protected]4bcac782009-02-10 02:48:2724
[email protected]a42b5592009-09-03 16:52:2325#if defined(OS_LINUX) && !defined(TOOLKIT_VIEWS)
26#include "chrome/browser/gtk/gtk_theme_provider.h"
27#endif
28
[email protected]e1acf6f2008-10-27 20:43:3329using base::Time;
[email protected]3c887412010-04-19 20:30:2330using testing::NiceMock;
[email protected]4772b072010-03-30 17:45:4631using testing::Return;
[email protected]e1acf6f2008-10-27 20:43:3332
[email protected]d364c652008-08-29 19:46:5633namespace {
34
[email protected]0bfc29a2009-04-27 16:15:4435// Task used to make sure history has finished processing a request. Intended
36// for use with BlockUntilHistoryProcessesPendingRequests.
37
38class QuittingHistoryDBTask : public HistoryDBTask {
39 public:
40 QuittingHistoryDBTask() {}
41
42 virtual bool RunOnDBThread(history::HistoryBackend* backend,
43 history::HistoryDatabase* db) {
44 return true;
45 }
46
47 virtual void DoneRunOnMainThread() {
48 MessageLoop::current()->Quit();
49 }
50
51 private:
[email protected]7991a232009-11-06 01:55:4852 ~QuittingHistoryDBTask() {}
53
[email protected]0bfc29a2009-04-27 16:15:4454 DISALLOW_COPY_AND_ASSIGN(QuittingHistoryDBTask);
55};
56
[email protected]d8e41ed2008-09-11 15:22:3257// BookmarkLoadObserver is used when blocking until the BookmarkModel
58// finishes loading. As soon as the BookmarkModel finishes loading the message
59// loop is quit.
60class BookmarkLoadObserver : public BookmarkModelObserver {
[email protected]d364c652008-08-29 19:46:5661 public:
62 BookmarkLoadObserver() {}
[email protected]d8e41ed2008-09-11 15:22:3263 virtual void Loaded(BookmarkModel* model) {
[email protected]d364c652008-08-29 19:46:5664 MessageLoop::current()->Quit();
65 }
66
[email protected]d8e41ed2008-09-11 15:22:3267 virtual void BookmarkNodeMoved(BookmarkModel* model,
[email protected]b3c33d462009-06-26 22:29:2068 const BookmarkNode* old_parent,
[email protected]d364c652008-08-29 19:46:5669 int old_index,
[email protected]b3c33d462009-06-26 22:29:2070 const BookmarkNode* new_parent,
[email protected]d364c652008-08-29 19:46:5671 int new_index) {}
[email protected]d8e41ed2008-09-11 15:22:3272 virtual void BookmarkNodeAdded(BookmarkModel* model,
[email protected]b3c33d462009-06-26 22:29:2073 const BookmarkNode* parent,
[email protected]d364c652008-08-29 19:46:5674 int index) {}
[email protected]d8e41ed2008-09-11 15:22:3275 virtual void BookmarkNodeRemoved(BookmarkModel* model,
[email protected]b3c33d462009-06-26 22:29:2076 const BookmarkNode* parent,
[email protected]66965022009-07-15 17:20:0177 int old_index,
78 const BookmarkNode* node) {}
[email protected]d8e41ed2008-09-11 15:22:3279 virtual void BookmarkNodeChanged(BookmarkModel* model,
[email protected]b3c33d462009-06-26 22:29:2080 const BookmarkNode* node) {}
[email protected]58b359d2009-02-27 22:05:0881 virtual void BookmarkNodeChildrenReordered(BookmarkModel* model,
[email protected]b3c33d462009-06-26 22:29:2082 const BookmarkNode* node) {}
[email protected]d8e41ed2008-09-11 15:22:3283 virtual void BookmarkNodeFavIconLoaded(BookmarkModel* model,
[email protected]b3c33d462009-06-26 22:29:2084 const BookmarkNode* node) {}
[email protected]d364c652008-08-29 19:46:5685
86 private:
87 DISALLOW_COPY_AND_ASSIGN(BookmarkLoadObserver);
88};
89
[email protected]812b3a32010-01-08 05:36:0490// This context is used to assist testing the CookieMonster by providing a
91// valid CookieStore. This can probably be expanded to test other aspects of
92// the context as well.
93class TestURLRequestContext : public URLRequestContext {
94 public:
95 TestURLRequestContext() {
[email protected]0f7066e2010-03-25 08:31:4796 cookie_store_ = new net::CookieMonster(NULL, NULL);
[email protected]812b3a32010-01-08 05:36:0497 }
98};
99
100// Used to return a dummy context (normally the context is on the IO thread).
101// The one here can be run on the main test thread. Note that this can lead to
102// a leak if your test does not have a ChromeThread::IO in it because
103// URLRequestContextGetter is defined as a ReferenceCounted object with a
[email protected]83a7d2eb2010-05-03 21:46:19104// special trait that deletes it on the IO thread.
[email protected]812b3a32010-01-08 05:36:04105class TestURLRequestContextGetter : public URLRequestContextGetter {
106 public:
107 virtual URLRequestContext* GetURLRequestContext() {
108 if (!context_)
109 context_ = new TestURLRequestContext();
110 return context_.get();
111 }
[email protected]656475d2010-05-06 18:34:24112 virtual scoped_refptr<base::MessageLoopProxy> GetIOMessageLoopProxy() {
[email protected]83a7d2eb2010-05-03 21:46:19113 return ChromeThread::GetMessageLoopProxyForThread(ChromeThread::IO);
114 }
[email protected]812b3a32010-01-08 05:36:04115
116 private:
117 scoped_refptr<URLRequestContext> context_;
118};
119
[email protected]c10da4b02010-03-25 14:38:32120class TestExtensionURLRequestContext : public URLRequestContext {
121 public:
122 TestExtensionURLRequestContext() {
123 net::CookieMonster* cookie_monster = new net::CookieMonster(NULL, NULL);
124 const char* schemes[] = {chrome::kExtensionScheme};
125 cookie_monster->SetCookieableSchemes(schemes, 1);
126 cookie_store_ = cookie_monster;
127 }
128};
129
130class TestExtensionURLRequestContextGetter : public URLRequestContextGetter {
131 public:
132 virtual URLRequestContext* GetURLRequestContext() {
133 if (!context_)
134 context_ = new TestExtensionURLRequestContext();
135 return context_.get();
136 }
[email protected]656475d2010-05-06 18:34:24137 virtual scoped_refptr<base::MessageLoopProxy> GetIOMessageLoopProxy() {
[email protected]83a7d2eb2010-05-03 21:46:19138 return ChromeThread::GetMessageLoopProxyForThread(ChromeThread::IO);
139 }
[email protected]c10da4b02010-03-25 14:38:32140
141 private:
142 scoped_refptr<URLRequestContext> context_;
143};
144
[email protected]d364c652008-08-29 19:46:56145} // namespace
146
[email protected]f25387b2008-08-21 15:20:33147TestingProfile::TestingProfile()
[email protected]ea6f76572008-12-18 00:09:55148 : start_time_(Time::Now()),
[email protected]a42b5592009-09-03 16:52:23149 created_theme_provider_(false),
[email protected]ea6f76572008-12-18 00:09:55150 has_history_service_(false),
151 off_the_record_(false),
152 last_session_exited_cleanly_(true) {
[email protected]f25387b2008-08-21 15:20:33153 PathService::Get(base::DIR_TEMP, &path_);
[email protected]f7011fcb2009-01-28 21:54:32154 path_ = path_.Append(FILE_PATH_LITERAL("TestingProfilePath"));
[email protected]f25387b2008-08-21 15:20:33155 file_util::Delete(path_, true);
156 file_util::CreateDirectory(path_);
initial.commit09911bf2008-07-26 23:55:29157}
158
[email protected]848cd05e2008-09-19 18:33:48159TestingProfile::TestingProfile(int count)
[email protected]ea6f76572008-12-18 00:09:55160 : start_time_(Time::Now()),
[email protected]a6caa7e2009-09-25 21:03:51161 created_theme_provider_(false),
[email protected]ea6f76572008-12-18 00:09:55162 has_history_service_(false),
163 off_the_record_(false),
164 last_session_exited_cleanly_(true) {
[email protected]848cd05e2008-09-19 18:33:48165 PathService::Get(base::DIR_TEMP, &path_);
[email protected]f7011fcb2009-01-28 21:54:32166 path_ = path_.Append(FILE_PATH_LITERAL("TestingProfilePath"));
[email protected]e83326f2010-07-31 17:29:25167 path_ = path_.AppendASCII(base::IntToString(count));
[email protected]848cd05e2008-09-19 18:33:48168 file_util::Delete(path_, true);
169 file_util::CreateDirectory(path_);
170}
171
[email protected]f25387b2008-08-21 15:20:33172TestingProfile::~TestingProfile() {
[email protected]d2879af2010-02-08 16:02:56173 NotificationService::current()->Notify(
174 NotificationType::PROFILE_DESTROYED,
175 Source<Profile>(this),
176 NotificationService::NoDetails());
[email protected]f25387b2008-08-21 15:20:33177 DestroyHistoryService();
[email protected]d2879af2010-02-08 16:02:56178 // FaviconService depends on HistoryServce so destroying it later.
179 DestroyFaviconService();
[email protected]2609bc12010-01-24 08:32:55180 DestroyWebDataService();
[email protected]f25387b2008-08-21 15:20:33181 file_util::Delete(path_, true);
182}
initial.commit09911bf2008-07-26 23:55:29183
[email protected]d2879af2010-02-08 16:02:56184void TestingProfile::CreateFaviconService() {
185 favicon_service_ = NULL;
186 favicon_service_ = new FaviconService(this);
187}
188
[email protected]d486a0852009-11-02 21:40:00189void TestingProfile::CreateHistoryService(bool delete_file, bool no_db) {
[email protected]f25387b2008-08-21 15:20:33190 if (history_service_.get())
191 history_service_->Cleanup();
192
193 history_service_ = NULL;
194
195 if (delete_file) {
[email protected]f7011fcb2009-01-28 21:54:32196 FilePath path = GetPath();
197 path = path.Append(chrome::kHistoryFilename);
[email protected]f25387b2008-08-21 15:20:33198 file_util::Delete(path, false);
199 }
[email protected]90ef13132008-08-27 03:27:46200 history_service_ = new HistoryService(this);
[email protected]d486a0852009-11-02 21:40:00201 history_service_->Init(GetPath(), bookmark_bar_model_.get(), no_db);
initial.commit09911bf2008-07-26 23:55:29202}
203
[email protected]d2879af2010-02-08 16:02:56204void TestingProfile::DestroyFaviconService() {
205 if (!favicon_service_.get())
206 return;
207 favicon_service_ = NULL;
208}
209
initial.commit09911bf2008-07-26 23:55:29210void TestingProfile::DestroyHistoryService() {
211 if (!history_service_.get())
212 return;
213
214 history_service_->NotifyRenderProcessHostDestruction(0);
215 history_service_->SetOnBackendDestroyTask(new MessageLoop::QuitTask);
216 history_service_->Cleanup();
217 history_service_ = NULL;
218
219 // Wait for the backend class to terminate before deleting the files and
220 // moving to the next test. Note: if this never terminates, somebody is
221 // probably leaking a reference to the history backend, so it never calls
222 // our destroy task.
223 MessageLoop::current()->Run();
224
initial.commit09911bf2008-07-26 23:55:29225 // Make sure we don't have any event pending that could disrupt the next
226 // test.
227 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask);
228 MessageLoop::current()->Run();
229}
[email protected]4d0cd7ce2008-08-11 16:40:57230
[email protected]d8e41ed2008-09-11 15:22:32231void TestingProfile::CreateBookmarkModel(bool delete_file) {
[email protected]90ef13132008-08-27 03:27:46232 // Nuke the model first, that way we're sure it's done writing to disk.
233 bookmark_bar_model_.reset(NULL);
234
235 if (delete_file) {
[email protected]f7011fcb2009-01-28 21:54:32236 FilePath path = GetPath();
237 path = path.Append(chrome::kBookmarksFileName);
[email protected]90ef13132008-08-27 03:27:46238 file_util::Delete(path, false);
239 }
[email protected]d8e41ed2008-09-11 15:22:32240 bookmark_bar_model_.reset(new BookmarkModel(this));
[email protected]90ef13132008-08-27 03:27:46241 if (history_service_.get()) {
242 history_service_->history_backend_->bookmark_service_ =
243 bookmark_bar_model_.get();
244 history_service_->history_backend_->expirer_.bookmark_service_ =
245 bookmark_bar_model_.get();
246 }
247 bookmark_bar_model_->Load();
[email protected]4d0cd7ce2008-08-11 16:40:57248}
[email protected]d2c017a2008-08-13 21:51:45249
[email protected]69c579e2010-04-23 20:01:00250void TestingProfile::CreateAutocompleteClassifier() {
251 autocomplete_classifier_.reset(new AutocompleteClassifier(this));
252}
253
[email protected]2609bc12010-01-24 08:32:55254void TestingProfile::CreateWebDataService(bool delete_file) {
255 if (web_data_service_.get())
256 web_data_service_->Shutdown();
257
258 if (delete_file) {
259 FilePath path = GetPath();
260 path = path.Append(chrome::kWebDataFilename);
261 file_util::Delete(path, false);
262 }
263
264 web_data_service_ = new WebDataService;
265 if (web_data_service_.get())
266 web_data_service_->Init(GetPath());
267}
268
[email protected]d364c652008-08-29 19:46:56269void TestingProfile::BlockUntilBookmarkModelLoaded() {
270 DCHECK(bookmark_bar_model_.get());
271 if (bookmark_bar_model_->IsLoaded())
272 return;
273 BookmarkLoadObserver observer;
274 bookmark_bar_model_->AddObserver(&observer);
275 MessageLoop::current()->Run();
276 bookmark_bar_model_->RemoveObserver(&observer);
277 DCHECK(bookmark_bar_model_->IsLoaded());
278}
279
[email protected]d2c017a2008-08-13 21:51:45280void TestingProfile::CreateTemplateURLModel() {
281 template_url_model_.reset(new TemplateURLModel(this));
282}
[email protected]0bfc29a2009-04-27 16:15:44283
[email protected]a42b5592009-09-03 16:52:23284void TestingProfile::UseThemeProvider(BrowserThemeProvider* theme_provider) {
285 theme_provider->Init(this);
286 created_theme_provider_ = true;
[email protected]761962c2009-09-25 00:18:15287 theme_provider_.reset(theme_provider);
[email protected]a42b5592009-09-03 16:52:23288}
289
[email protected]2f351cb2009-11-09 23:43:34290webkit_database::DatabaseTracker* TestingProfile::GetDatabaseTracker() {
291 if (!db_tracker_)
[email protected]fe615f32010-06-13 09:08:41292 db_tracker_ = new webkit_database::DatabaseTracker(GetPath(), false);
[email protected]2f351cb2009-11-09 23:43:34293 return db_tracker_;
294}
295
[email protected]a42b5592009-09-03 16:52:23296void TestingProfile::InitThemes() {
297 if (!created_theme_provider_) {
298#if defined(OS_LINUX) && !defined(TOOLKIT_VIEWS)
[email protected]761962c2009-09-25 00:18:15299 theme_provider_.reset(new GtkThemeProvider);
[email protected]a42b5592009-09-03 16:52:23300#else
[email protected]761962c2009-09-25 00:18:15301 theme_provider_.reset(new BrowserThemeProvider);
[email protected]a42b5592009-09-03 16:52:23302#endif
[email protected]761962c2009-09-25 00:18:15303 theme_provider_->Init(this);
[email protected]a42b5592009-09-03 16:52:23304 created_theme_provider_ = true;
[email protected]a42b5592009-09-03 16:52:23305 }
[email protected]76688082009-05-16 07:04:42306}
307
[email protected]812b3a32010-01-08 05:36:04308URLRequestContextGetter* TestingProfile::GetRequestContext() {
309 return request_context_.get();
310}
311
312void TestingProfile::CreateRequestContext() {
313 if (!request_context_)
314 request_context_ = new TestURLRequestContextGetter();
315}
316
[email protected]c10da4b02010-03-25 14:38:32317URLRequestContextGetter* TestingProfile::GetRequestContextForExtensions() {
318 if (!extensions_request_context_)
319 extensions_request_context_ = new TestExtensionURLRequestContextGetter();
320 return extensions_request_context_.get();
321}
322
[email protected]8cb5d5b2010-02-09 11:36:16323void TestingProfile::set_session_service(SessionService* session_service) {
324 session_service_ = session_service;
325}
326
[email protected]156ed27b2009-11-23 18:31:25327NTPResourceCache* TestingProfile::GetNTPResourceCache() {
328 if (!ntp_resource_cache_.get())
329 ntp_resource_cache_.reset(new NTPResourceCache(this));
330 return ntp_resource_cache_.get();
331}
332
[email protected]0bfc29a2009-04-27 16:15:44333void TestingProfile::BlockUntilHistoryProcessesPendingRequests() {
334 DCHECK(history_service_.get());
335 DCHECK(MessageLoop::current());
336
337 CancelableRequestConsumer consumer;
338 history_service_->ScheduleDBTask(new QuittingHistoryDBTask(), &consumer);
339 MessageLoop::current()->Run();
340}
[email protected]345a8b72009-09-29 09:11:44341
[email protected]8e4c29612010-07-14 01:24:45342TokenService* TestingProfile::GetTokenService() {
343 if (!token_service_.get()) {
344 token_service_.reset(new TokenService());
345 }
346 return token_service_.get();
347}
348
[email protected]345a8b72009-09-29 09:11:44349ProfileSyncService* TestingProfile::GetProfileSyncService() {
[email protected]4772b072010-03-30 17:45:46350 if (!profile_sync_service_.get()) {
[email protected]3c887412010-04-19 20:30:23351 // Use a NiceMock here since we are really using the mock as a
352 // fake. Test cases that want to set expectations on a
353 // ProfileSyncService should use the ProfileMock and have this
354 // method return their own mock instance.
355 profile_sync_service_.reset(new NiceMock<ProfileSyncServiceMock>());
[email protected]4772b072010-03-30 17:45:46356 }
[email protected]345a8b72009-09-29 09:11:44357 return profile_sync_service_.get();
[email protected]345a8b72009-09-29 09:11:44358}
[email protected]2609bc12010-01-24 08:32:55359
360void TestingProfile::DestroyWebDataService() {
361 if (!web_data_service_.get())
362 return;
363
364 web_data_service_->Shutdown();
365}