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

Side by Side Diff: chrome/browser/search_engines/template_url_service_test_util.cc

Issue 12871006: Second try at splitting WebDataService (minus ownership changes) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merge to head for commit. Created 7 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/browser/search_engines/template_url_service_test_util.h" 5 #include "chrome/browser/search_engines/template_url_service_test_util.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "base/message_loop.h" 8 #include "base/message_loop.h"
10 #include "base/path_service.h" 9 #include "base/path_service.h"
11 #include "base/synchronization/waitable_event.h" 10 #include "base/synchronization/waitable_event.h"
12 #include "base/threading/thread.h" 11 #include "base/threading/thread.h"
13 #include "chrome/browser/google/google_url_tracker.h" 12 #include "chrome/browser/google/google_url_tracker.h"
14 #include "chrome/browser/search_engines/search_terms_data.h" 13 #include "chrome/browser/search_engines/search_terms_data.h"
15 #include "chrome/browser/search_engines/template_url_service.h" 14 #include "chrome/browser/search_engines/template_url_service.h"
16 #include "chrome/browser/search_engines/template_url_service_factory.h" 15 #include "chrome/browser/search_engines/template_url_service_factory.h"
17 #include "chrome/browser/webdata/web_data_service_factory.h" 16 #include "chrome/browser/webdata/web_data_service_factory.h"
18 #include "chrome/common/chrome_notification_types.h" 17 #include "chrome/common/chrome_notification_types.h"
(...skipping 27 matching lines...) Expand all
46 static void WaitForThreadToProcessRequests(BrowserThread::ID identifier) { 45 static void WaitForThreadToProcessRequests(BrowserThread::ID identifier) {
47 // Schedule a task on the thread that is processed after all 46 // Schedule a task on the thread that is processed after all
48 // pending requests on the thread. 47 // pending requests on the thread.
49 BrowserThread::PostTask(identifier, FROM_HERE, 48 BrowserThread::PostTask(identifier, FROM_HERE,
50 base::Bind(&QuitCallback, MessageLoop::current())); 49 base::Bind(&QuitCallback, MessageLoop::current()));
51 MessageLoop::current()->Run(); 50 MessageLoop::current()->Run();
52 } 51 }
53 52
54 } // namespace 53 } // namespace
55 54
56 // Subclass the TestingProfile so that it can return a WebDataService.
57 class TemplateURLServiceTestingProfile : public TestingProfile {
58 public:
59 TemplateURLServiceTestingProfile()
60 : TestingProfile(),
61 db_thread_(BrowserThread::DB),
62 io_thread_(BrowserThread::IO) {
63 }
64
65 void SetUp();
66 void TearDown();
67
68 // Starts the I/O thread. This isn't done automatically because not every test
69 // needs this.
70 void StartIOThread() {
71 io_thread_.StartIOThread();
72 }
73
74 static scoped_refptr<RefcountedProfileKeyedService>
75 GetWebDataServiceForTemplateURLServiceTestingProfile(Profile* profile);
76
77 private:
78 scoped_refptr<WebDataService> service_;
79 base::ScopedTempDir temp_dir_;
80 content::TestBrowserThread db_thread_;
81 content::TestBrowserThread io_thread_;
82 };
83
84
85 // Trivial subclass of TemplateURLService that records the last invocation of 55 // Trivial subclass of TemplateURLService that records the last invocation of
86 // SetKeywordSearchTermsForURL. 56 // SetKeywordSearchTermsForURL.
87 class TestingTemplateURLService : public TemplateURLService { 57 class TestingTemplateURLService : public TemplateURLService {
88 public: 58 public:
89 static ProfileKeyedService* Build(Profile* profile) { 59 static ProfileKeyedService* Build(Profile* profile) {
90 return new TestingTemplateURLService(profile); 60 return new TestingTemplateURLService(profile);
91 } 61 }
92 62
93 explicit TestingTemplateURLService(Profile* profile) 63 explicit TestingTemplateURLService(Profile* profile)
94 : TemplateURLService(profile) { 64 : TemplateURLService(profile) {
(...skipping 11 matching lines...) Expand all
106 const string16& term) OVERRIDE { 76 const string16& term) OVERRIDE {
107 search_term_ = term; 77 search_term_ = term;
108 } 78 }
109 79
110 private: 80 private:
111 string16 search_term_; 81 string16 search_term_;
112 82
113 DISALLOW_COPY_AND_ASSIGN(TestingTemplateURLService); 83 DISALLOW_COPY_AND_ASSIGN(TestingTemplateURLService);
114 }; 84 };
115 85
116 void TemplateURLServiceTestingProfile::SetUp() { 86 TemplateURLServiceTestUtil::TemplateURLServiceTestUtil()
117 db_thread_.Start(); 87 : ui_thread_(BrowserThread::UI, &message_loop_),
88 db_thread_(BrowserThread::DB),
89 io_thread_(BrowserThread::IO),
90 changed_count_(0) {
91 }
118 92
93 TemplateURLServiceTestUtil::~TemplateURLServiceTestUtil() {
94 }
95
96 void TemplateURLServiceTestUtil::SetUp() {
119 // Make unique temp directory. 97 // Make unique temp directory.
120 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 98 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
99 profile_.reset(new TestingProfile(temp_dir_.path()));
100 db_thread_.Start();
101 profile_->CreateWebDataService();
121 102
122 base::FilePath path = temp_dir_.path().AppendASCII("TestDataService.db"); 103 TemplateURLService* service = static_cast<TemplateURLService*>(
123 service_ = new WebDataService; 104 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
124 ASSERT_TRUE(service_->InitWithPath(path)); 105 profile_.get(), TestingTemplateURLService::Build));
106 service->AddObserver(this);
107
108 #if defined(OS_CHROMEOS)
109 google_util::chromeos::ClearBrandForCurrentSession();
110 #endif
125 } 111 }
126 112
127 void TemplateURLServiceTestingProfile::TearDown() { 113 void TemplateURLServiceTestUtil::TearDown() {
128 // Clear the request context so it will get deleted. This should be done 114 if (profile_.get()) {
129 // before shutting down the I/O thread to avoid memory leaks. 115 // Clear the request context so it will get deleted. This should be done
130 ResetRequestContext(); 116 // before shutting down the I/O thread to avoid memory leaks.
117 profile_->ResetRequestContext();
118 profile_.reset();
119 }
131 120
132 // Wait for the delete of the request context to happen. 121 // Wait for the delete of the request context to happen.
133 if (io_thread_.IsRunning()) 122 if (io_thread_.IsRunning())
134 TemplateURLServiceTestUtil::BlockTillIOThreadProcessesRequests(); 123 TemplateURLServiceTestUtil::BlockTillIOThreadProcessesRequests();
135 124
136 // The I/O thread must be shutdown before the DB thread. 125 // The I/O thread must be shutdown before the DB thread.
137 io_thread_.Stop(); 126 io_thread_.Stop();
138 127
139 // Clean up the test directory.
140 if (service_.get()) {
141 service_->ShutdownOnUIThread();
142 service_ = NULL;
143 }
144 // Note that we must ensure the DB thread is stopped after WDS 128 // Note that we must ensure the DB thread is stopped after WDS
145 // shutdown (so it can commit pending transactions) but before 129 // shutdown (so it can commit pending transactions) but before
146 // deleting the test profile directory, otherwise we may not be 130 // deleting the test profile directory, otherwise we may not be
147 // able to delete it due to an open transaction. 131 // able to delete it due to an open transaction.
148 // Schedule another task on the DB thread to notify us that it's safe to 132 // Schedule another task on the DB thread to notify us that it's safe to
149 // carry on with the test. 133 // carry on with the test.
150 base::WaitableEvent done(false, false); 134 base::WaitableEvent done(false, false);
151 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, 135 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
152 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done))); 136 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done)));
153 done.Wait(); 137 done.Wait();
138 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
139 MessageLoop::current()->Run();
154 db_thread_.Stop(); 140 db_thread_.Stop();
155 }
156 141
157 scoped_refptr<RefcountedProfileKeyedService>
158 TemplateURLServiceTestingProfile::
159 GetWebDataServiceForTemplateURLServiceTestingProfile(Profile* profile) {
160 TemplateURLServiceTestingProfile* test_profile =
161 reinterpret_cast<TemplateURLServiceTestingProfile*>(profile);
162 return test_profile->service_;
163 }
164
165 TemplateURLServiceTestUtil::TemplateURLServiceTestUtil()
166 : ui_thread_(BrowserThread::UI, &message_loop_),
167 changed_count_(0) {
168 }
169
170 TemplateURLServiceTestUtil::~TemplateURLServiceTestUtil() {
171 }
172
173 void TemplateURLServiceTestUtil::SetUp() {
174 profile_.reset(new TemplateURLServiceTestingProfile());
175 WebDataServiceFactory::GetInstance()->SetTestingFactory(
176 profile_.get(), TemplateURLServiceTestingProfile::
177 GetWebDataServiceForTemplateURLServiceTestingProfile);
178
179 profile_->SetUp();
180 TemplateURLService* service = static_cast<TemplateURLService*>(
181 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
182 profile_.get(), TestingTemplateURLService::Build));
183 service->AddObserver(this);
184
185 #if defined(OS_CHROMEOS)
186 google_util::chromeos::ClearBrandForCurrentSession();
187 #endif
188 }
189
190 void TemplateURLServiceTestUtil::TearDown() {
191 if (profile_.get()) {
192 profile_->TearDown();
193 profile_.reset();
194 }
195 UIThreadSearchTermsData::SetGoogleBaseURL(std::string()); 142 UIThreadSearchTermsData::SetGoogleBaseURL(std::string());
196 143
197 // Flush the message loop to make application verifiers happy. 144 // Flush the message loop to make application verifiers happy.
198 message_loop_.RunUntilIdle(); 145 message_loop_.RunUntilIdle();
199 } 146 }
200 147
201 void TemplateURLServiceTestUtil::OnTemplateURLServiceChanged() { 148 void TemplateURLServiceTestUtil::OnTemplateURLServiceChanged() {
202 changed_count_++; 149 changed_count_++;
203 } 150 }
204 151
(...skipping 21 matching lines...) Expand all
226 ResetObserverCount(); 173 ResetObserverCount();
227 } 174 }
228 175
229 void TemplateURLServiceTestUtil::ChangeModelToLoadState() { 176 void TemplateURLServiceTestUtil::ChangeModelToLoadState() {
230 model()->ChangeToLoadedState(); 177 model()->ChangeToLoadedState();
231 // Initialize the web data service so that the database gets updated with 178 // Initialize the web data service so that the database gets updated with
232 // any changes made. 179 // any changes made.
233 180
234 model()->service_ = WebDataServiceFactory::GetForProfile( 181 model()->service_ = WebDataServiceFactory::GetForProfile(
235 profile_.get(), Profile::EXPLICIT_ACCESS); 182 profile_.get(), Profile::EXPLICIT_ACCESS);
183 BlockTillServiceProcessesRequests();
236 } 184 }
237 185
238 void TemplateURLServiceTestUtil::ClearModel() { 186 void TemplateURLServiceTestUtil::ClearModel() {
239 TemplateURLServiceFactory::GetInstance()->SetTestingFactory( 187 TemplateURLServiceFactory::GetInstance()->SetTestingFactory(
240 profile_.get(), NULL); 188 profile_.get(), NULL);
241 } 189 }
242 190
243 void TemplateURLServiceTestUtil::ResetModel(bool verify_load) { 191 void TemplateURLServiceTestUtil::ResetModel(bool verify_load) {
244 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( 192 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
245 profile_.get(), TestingTemplateURLService::Build); 193 profile_.get(), TestingTemplateURLService::Build);
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
323 271
324 TemplateURLService* TemplateURLServiceTestUtil::model() const { 272 TemplateURLService* TemplateURLServiceTestUtil::model() const {
325 return TemplateURLServiceFactory::GetForProfile(profile_.get()); 273 return TemplateURLServiceFactory::GetForProfile(profile_.get());
326 } 274 }
327 275
328 TestingProfile* TemplateURLServiceTestUtil::profile() const { 276 TestingProfile* TemplateURLServiceTestUtil::profile() const {
329 return profile_.get(); 277 return profile_.get();
330 } 278 }
331 279
332 void TemplateURLServiceTestUtil::StartIOThread() { 280 void TemplateURLServiceTestUtil::StartIOThread() {
333 profile_->StartIOThread(); 281 io_thread_.StartIOThread();
334 } 282 }
335 283
336 void TemplateURLServiceTestUtil::PumpLoop() { 284 void TemplateURLServiceTestUtil::PumpLoop() {
337 message_loop_.RunUntilIdle(); 285 message_loop_.RunUntilIdle();
338 } 286 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698