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

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

Issue 12851008: Create a common base class for all the webdatas (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase on trunk and comments 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_),
118 88 db_thread_(BrowserThread::DB),
119 // Make unique temp directory. 89 io_thread_(BrowserThread::IO),
120 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 90 changed_count_(0) {
121
122 base::FilePath path = temp_dir_.path().AppendASCII("TestDataService.db");
123 service_ = new WebDataService;
124 ASSERT_TRUE(service_->InitWithPath(path));
125 } 91 }
126 92
127 void TemplateURLServiceTestingProfile::TearDown() { 93 TemplateURLServiceTestUtil::~TemplateURLServiceTestUtil() {
128 // Clear the request context so it will get deleted. This should be done 94 }
129 // before shutting down the I/O thread to avoid memory leaks. 95
130 ResetRequestContext(); 96 void TemplateURLServiceTestUtil::SetUp() {
97 profile_.reset(new TestingProfile());
98 db_thread_.Start();
99 profile_->CreateWebDataService();
100
101 TemplateURLService* service = static_cast<TemplateURLService*>(
102 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
103 profile_.get(), TestingTemplateURLService::Build));
104 service->AddObserver(this);
105
106 #if defined(OS_CHROMEOS)
107 google_util::chromeos::ClearBrandForCurrentSession();
108 #endif
109 }
110
111 void TemplateURLServiceTestUtil::TearDown() {
112 if (profile_.get()) {
113 // Clear the request context so it will get deleted. This should be done
114 // before shutting down the I/O thread to avoid memory leaks.
115 profile_->ResetRequestContext();
116 profile_.reset();
117 }
131 118
132 // Wait for the delete of the request context to happen. 119 // Wait for the delete of the request context to happen.
133 if (io_thread_.IsRunning()) 120 if (io_thread_.IsRunning())
134 TemplateURLServiceTestUtil::BlockTillIOThreadProcessesRequests(); 121 TemplateURLServiceTestUtil::BlockTillIOThreadProcessesRequests();
135 122
136 // The I/O thread must be shutdown before the DB thread. 123 // The I/O thread must be shutdown before the DB thread.
137 io_thread_.Stop(); 124 io_thread_.Stop();
138 125
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 126 // Note that we must ensure the DB thread is stopped after WDS
145 // shutdown (so it can commit pending transactions) but before 127 // shutdown (so it can commit pending transactions) but before
146 // deleting the test profile directory, otherwise we may not be 128 // deleting the test profile directory, otherwise we may not be
147 // able to delete it due to an open transaction. 129 // 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 130 // Schedule another task on the DB thread to notify us that it's safe to
149 // carry on with the test. 131 // carry on with the test.
150 base::WaitableEvent done(false, false); 132 base::WaitableEvent done(false, false);
151 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, 133 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
152 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done))); 134 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done)));
153 done.Wait(); 135 done.Wait();
136 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
137 MessageLoop::current()->Run();
154 db_thread_.Stop(); 138 db_thread_.Stop();
155 }
156 139
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()); 140 UIThreadSearchTermsData::SetGoogleBaseURL(std::string());
196 141
197 // Flush the message loop to make application verifiers happy. 142 // Flush the message loop to make application verifiers happy.
198 message_loop_.RunUntilIdle(); 143 message_loop_.RunUntilIdle();
199 } 144 }
200 145
201 void TemplateURLServiceTestUtil::OnTemplateURLServiceChanged() { 146 void TemplateURLServiceTestUtil::OnTemplateURLServiceChanged() {
202 changed_count_++; 147 changed_count_++;
203 } 148 }
204 149
(...skipping 21 matching lines...) Expand all
226 ResetObserverCount(); 171 ResetObserverCount();
227 } 172 }
228 173
229 void TemplateURLServiceTestUtil::ChangeModelToLoadState() { 174 void TemplateURLServiceTestUtil::ChangeModelToLoadState() {
230 model()->ChangeToLoadedState(); 175 model()->ChangeToLoadedState();
231 // Initialize the web data service so that the database gets updated with 176 // Initialize the web data service so that the database gets updated with
232 // any changes made. 177 // any changes made.
233 178
234 model()->service_ = WebDataServiceFactory::GetForProfile( 179 model()->service_ = WebDataServiceFactory::GetForProfile(
235 profile_.get(), Profile::EXPLICIT_ACCESS); 180 profile_.get(), Profile::EXPLICIT_ACCESS);
181 BlockTillServiceProcessesRequests();
236 } 182 }
237 183
238 void TemplateURLServiceTestUtil::ClearModel() { 184 void TemplateURLServiceTestUtil::ClearModel() {
239 TemplateURLServiceFactory::GetInstance()->SetTestingFactory( 185 TemplateURLServiceFactory::GetInstance()->SetTestingFactory(
240 profile_.get(), NULL); 186 profile_.get(), NULL);
241 } 187 }
242 188
243 void TemplateURLServiceTestUtil::ResetModel(bool verify_load) { 189 void TemplateURLServiceTestUtil::ResetModel(bool verify_load) {
244 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( 190 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
245 profile_.get(), TestingTemplateURLService::Build); 191 profile_.get(), TestingTemplateURLService::Build);
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
323 269
324 TemplateURLService* TemplateURLServiceTestUtil::model() const { 270 TemplateURLService* TemplateURLServiceTestUtil::model() const {
325 return TemplateURLServiceFactory::GetForProfile(profile_.get()); 271 return TemplateURLServiceFactory::GetForProfile(profile_.get());
326 } 272 }
327 273
328 TestingProfile* TemplateURLServiceTestUtil::profile() const { 274 TestingProfile* TemplateURLServiceTestUtil::profile() const {
329 return profile_.get(); 275 return profile_.get();
330 } 276 }
331 277
332 void TemplateURLServiceTestUtil::StartIOThread() { 278 void TemplateURLServiceTestUtil::StartIOThread() {
333 profile_->StartIOThread(); 279 io_thread_.StartIOThread();
334 } 280 }
335 281
336 void TemplateURLServiceTestUtil::PumpLoop() { 282 void TemplateURLServiceTestUtil::PumpLoop() {
337 message_loop_.RunUntilIdle(); 283 message_loop_.RunUntilIdle();
338 } 284 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698