OLD | NEW |
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/message_loop/message_loop.h" | 8 #include "base/run_loop.h" |
9 #include "base/path_service.h" | |
10 #include "base/synchronization/waitable_event.h" | |
11 #include "base/threading/thread.h" | 9 #include "base/threading/thread.h" |
12 #include "chrome/browser/chrome_notification_types.h" | 10 #include "chrome/browser/chrome_notification_types.h" |
13 #include "chrome/browser/google/google_url_tracker.h" | 11 #include "chrome/browser/google/google_url_tracker.h" |
14 #include "chrome/browser/search_engines/search_terms_data.h" | 12 #include "chrome/browser/search_engines/search_terms_data.h" |
15 #include "chrome/browser/search_engines/template_url_service.h" | 13 #include "chrome/browser/search_engines/template_url_service.h" |
16 #include "chrome/browser/search_engines/template_url_service_factory.h" | 14 #include "chrome/browser/search_engines/template_url_service_factory.h" |
17 #include "chrome/browser/webdata/web_data_service_factory.h" | 15 #include "chrome/browser/webdata/web_data_service_factory.h" |
18 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
19 #include "chrome/test/automation/value_conversion_util.h" | 17 #include "chrome/test/automation/value_conversion_util.h" |
20 #include "chrome/test/base/testing_pref_service_syncable.h" | 18 #include "chrome/test/base/testing_pref_service_syncable.h" |
21 #include "chrome/test/base/testing_profile.h" | 19 #include "chrome/test/base/testing_profile.h" |
22 #include "content/public/browser/notification_service.h" | 20 #include "content/public/browser/notification_service.h" |
23 #include "content/public/test/test_browser_thread.h" | |
24 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
25 | 22 |
26 | |
27 #if defined(OS_CHROMEOS) | 23 #if defined(OS_CHROMEOS) |
28 #include "chrome/browser/google/google_util_chromeos.h" | 24 #include "chrome/browser/google/google_util_chromeos.h" |
29 #endif | 25 #endif |
30 | 26 |
31 using content::BrowserThread; | |
32 | |
33 namespace { | |
34 | |
35 // A callback used to coordinate when the database has finished processing | |
36 // requests. See note in BlockTillServiceProcessesRequests for details. | |
37 // | |
38 // Schedules a QuitClosure on the message loop it was created with. | |
39 void QuitCallback(base::MessageLoop* message_loop) { | |
40 message_loop->PostTask(FROM_HERE, base::MessageLoop::QuitClosure()); | |
41 } | |
42 | |
43 // Blocks the caller until thread has finished servicing all pending | |
44 // requests. | |
45 static void WaitForThreadToProcessRequests(BrowserThread::ID identifier) { | |
46 // Schedule a task on the thread that is processed after all | |
47 // pending requests on the thread. | |
48 BrowserThread::PostTask( | |
49 identifier, | |
50 FROM_HERE, | |
51 base::Bind(&QuitCallback, base::MessageLoop::current())); | |
52 base::MessageLoop::current()->Run(); | |
53 } | |
54 | |
55 } // namespace | |
56 | |
57 | |
58 // TestingTemplateURLService -------------------------------------------------- | |
59 | |
60 // Trivial subclass of TemplateURLService that records the last invocation of | 27 // Trivial subclass of TemplateURLService that records the last invocation of |
61 // SetKeywordSearchTermsForURL. | 28 // SetKeywordSearchTermsForURL. |
62 class TestingTemplateURLService : public TemplateURLService { | 29 class TestingTemplateURLService : public TemplateURLService { |
63 public: | 30 public: |
64 static BrowserContextKeyedService* Build(content::BrowserContext* profile) { | 31 static BrowserContextKeyedService* Build(content::BrowserContext* profile) { |
65 return new TestingTemplateURLService(static_cast<Profile*>(profile)); | 32 return new TestingTemplateURLService(static_cast<Profile*>(profile)); |
66 } | 33 } |
67 | 34 |
68 explicit TestingTemplateURLService(Profile* profile) | 35 explicit TestingTemplateURLService(Profile* profile) |
69 : TemplateURLService(profile) { | 36 : TemplateURLService(profile) { |
(...skipping 11 matching lines...) Expand all Loading... |
81 const string16& term) OVERRIDE { | 48 const string16& term) OVERRIDE { |
82 search_term_ = term; | 49 search_term_ = term; |
83 } | 50 } |
84 | 51 |
85 private: | 52 private: |
86 string16 search_term_; | 53 string16 search_term_; |
87 | 54 |
88 DISALLOW_COPY_AND_ASSIGN(TestingTemplateURLService); | 55 DISALLOW_COPY_AND_ASSIGN(TestingTemplateURLService); |
89 }; | 56 }; |
90 | 57 |
91 | |
92 // TemplateURLServiceTestUtilBase --------------------------------------------- | 58 // TemplateURLServiceTestUtilBase --------------------------------------------- |
93 | 59 |
94 TemplateURLServiceTestUtilBase::TemplateURLServiceTestUtilBase() | 60 TemplateURLServiceTestUtilBase::TemplateURLServiceTestUtilBase() |
95 : changed_count_(0) { | 61 : changed_count_(0) { |
96 } | 62 } |
97 | 63 |
98 TemplateURLServiceTestUtilBase::~TemplateURLServiceTestUtilBase() {} | 64 TemplateURLServiceTestUtilBase::~TemplateURLServiceTestUtilBase() { |
| 65 } |
99 | 66 |
100 void TemplateURLServiceTestUtilBase::CreateTemplateUrlService() { | 67 void TemplateURLServiceTestUtilBase::CreateTemplateUrlService() { |
101 profile()->CreateWebDataService(); | 68 profile()->CreateWebDataService(); |
102 | 69 |
103 TemplateURLService* service = static_cast<TemplateURLService*>( | 70 TemplateURLService* service = static_cast<TemplateURLService*>( |
104 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( | 71 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
105 profile(), TestingTemplateURLService::Build)); | 72 profile(), TestingTemplateURLService::Build)); |
106 service->AddObserver(this); | 73 service->AddObserver(this); |
107 } | 74 } |
108 | 75 |
109 void TemplateURLServiceTestUtilBase::OnTemplateURLServiceChanged() { | 76 void TemplateURLServiceTestUtilBase::OnTemplateURLServiceChanged() { |
110 changed_count_++; | 77 changed_count_++; |
111 } | 78 } |
112 | 79 |
113 int TemplateURLServiceTestUtilBase::GetObserverCount() { | 80 int TemplateURLServiceTestUtilBase::GetObserverCount() { |
114 return changed_count_; | 81 return changed_count_; |
115 } | 82 } |
116 | 83 |
117 void TemplateURLServiceTestUtilBase::ResetObserverCount() { | 84 void TemplateURLServiceTestUtilBase::ResetObserverCount() { |
118 changed_count_ = 0; | 85 changed_count_ = 0; |
119 } | 86 } |
120 | 87 |
121 void TemplateURLServiceTestUtilBase::BlockTillServiceProcessesRequests() { | |
122 WaitForThreadToProcessRequests(BrowserThread::DB); | |
123 } | |
124 | |
125 void TemplateURLServiceTestUtilBase::BlockTillIOThreadProcessesRequests() { | |
126 WaitForThreadToProcessRequests(BrowserThread::IO); | |
127 } | |
128 | |
129 void TemplateURLServiceTestUtilBase::VerifyLoad() { | 88 void TemplateURLServiceTestUtilBase::VerifyLoad() { |
130 ASSERT_FALSE(model()->loaded()); | 89 ASSERT_FALSE(model()->loaded()); |
131 model()->Load(); | 90 model()->Load(); |
132 BlockTillServiceProcessesRequests(); | 91 base::RunLoop().RunUntilIdle(); |
133 EXPECT_EQ(1, GetObserverCount()); | 92 EXPECT_EQ(1, GetObserverCount()); |
134 ResetObserverCount(); | 93 ResetObserverCount(); |
135 } | 94 } |
136 | 95 |
137 void TemplateURLServiceTestUtilBase::ChangeModelToLoadState() { | 96 void TemplateURLServiceTestUtilBase::ChangeModelToLoadState() { |
138 model()->ChangeToLoadedState(); | 97 model()->ChangeToLoadedState(); |
139 // Initialize the web data service so that the database gets updated with | 98 // Initialize the web data service so that the database gets updated with |
140 // any changes made. | 99 // any changes made. |
141 | 100 |
142 model()->service_ = WebDataService::FromBrowserContext(profile()); | 101 model()->service_ = WebDataService::FromBrowserContext(profile()); |
143 BlockTillServiceProcessesRequests(); | 102 base::RunLoop().RunUntilIdle(); |
144 } | 103 } |
145 | 104 |
146 void TemplateURLServiceTestUtilBase::ClearModel() { | 105 void TemplateURLServiceTestUtilBase::ClearModel() { |
147 TemplateURLServiceFactory::GetInstance()->SetTestingFactory( | 106 TemplateURLServiceFactory::GetInstance()->SetTestingFactory( |
148 profile(), NULL); | 107 profile(), NULL); |
149 } | 108 } |
150 | 109 |
151 void TemplateURLServiceTestUtilBase::ResetModel(bool verify_load) { | 110 void TemplateURLServiceTestUtilBase::ResetModel(bool verify_load) { |
152 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( | 111 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
153 profile(), TestingTemplateURLService::Build); | 112 profile(), TestingTemplateURLService::Build); |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 } | 190 } |
232 | 191 |
233 TemplateURLService* TemplateURLServiceTestUtilBase::model() const { | 192 TemplateURLService* TemplateURLServiceTestUtilBase::model() const { |
234 return TemplateURLServiceFactory::GetForProfile(profile()); | 193 return TemplateURLServiceFactory::GetForProfile(profile()); |
235 } | 194 } |
236 | 195 |
237 | 196 |
238 // TemplateURLServiceTestUtil ------------------------------------------------- | 197 // TemplateURLServiceTestUtil ------------------------------------------------- |
239 | 198 |
240 TemplateURLServiceTestUtil::TemplateURLServiceTestUtil() | 199 TemplateURLServiceTestUtil::TemplateURLServiceTestUtil() |
241 : ui_thread_(BrowserThread::UI, &message_loop_), | 200 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { |
242 db_thread_(BrowserThread::DB), | |
243 io_thread_(BrowserThread::IO) { | |
244 } | 201 } |
245 | 202 |
246 TemplateURLServiceTestUtil::~TemplateURLServiceTestUtil() { | 203 TemplateURLServiceTestUtil::~TemplateURLServiceTestUtil() { |
247 } | 204 } |
248 | 205 |
249 void TemplateURLServiceTestUtil::SetUp() { | 206 void TemplateURLServiceTestUtil::SetUp() { |
250 // Make unique temp directory. | 207 // Make unique temp directory. |
251 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 208 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
252 profile_.reset(new TestingProfile(temp_dir_.path())); | 209 profile_.reset(new TestingProfile(temp_dir_.path())); |
253 db_thread_.Start(); | |
254 | 210 |
255 TemplateURLServiceTestUtilBase::CreateTemplateUrlService(); | 211 TemplateURLServiceTestUtilBase::CreateTemplateUrlService(); |
256 | 212 |
257 #if defined(OS_CHROMEOS) | 213 #if defined(OS_CHROMEOS) |
258 google_util::chromeos::ClearBrandForCurrentSession(); | 214 google_util::chromeos::ClearBrandForCurrentSession(); |
259 #endif | 215 #endif |
260 } | 216 } |
261 | 217 |
262 void TemplateURLServiceTestUtil::TearDown() { | 218 void TemplateURLServiceTestUtil::TearDown() { |
263 if (profile_.get()) { | 219 profile_.reset(); |
264 // Clear the request context so it will get deleted. This should be done | |
265 // before shutting down the I/O thread to avoid memory leaks. | |
266 profile_->ResetRequestContext(); | |
267 profile_.reset(); | |
268 } | |
269 | |
270 // Wait for the delete of the request context to happen. | |
271 if (io_thread_.IsRunning()) | |
272 TemplateURLServiceTestUtilBase::BlockTillIOThreadProcessesRequests(); | |
273 | |
274 // The I/O thread must be shutdown before the DB thread. | |
275 io_thread_.Stop(); | |
276 | |
277 // Note that we must ensure the DB thread is stopped after WDS | |
278 // shutdown (so it can commit pending transactions) but before | |
279 // deleting the test profile directory, otherwise we may not be | |
280 // able to delete it due to an open transaction. | |
281 // Schedule another task on the DB thread to notify us that it's safe to | |
282 // carry on with the test. | |
283 base::WaitableEvent done(false, false); | |
284 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, | |
285 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done))); | |
286 done.Wait(); | |
287 base::MessageLoop::current()->PostTask(FROM_HERE, | |
288 base::MessageLoop::QuitClosure()); | |
289 base::MessageLoop::current()->Run(); | |
290 db_thread_.Stop(); | |
291 | 220 |
292 UIThreadSearchTermsData::SetGoogleBaseURL(std::string()); | 221 UIThreadSearchTermsData::SetGoogleBaseURL(std::string()); |
293 | 222 |
294 // Flush the message loop to make application verifiers happy. | 223 // Flush the message loop to make application verifiers happy. |
295 message_loop_.RunUntilIdle(); | 224 base::RunLoop().RunUntilIdle(); |
296 } | 225 } |
297 | 226 |
298 TestingProfile* TemplateURLServiceTestUtil::profile() const { | 227 TestingProfile* TemplateURLServiceTestUtil::profile() const { |
299 return profile_.get(); | 228 return profile_.get(); |
300 } | 229 } |
301 | |
302 void TemplateURLServiceTestUtil::StartIOThread() { | |
303 io_thread_.StartIOThread(); | |
304 } | |
305 | |
306 void TemplateURLServiceTestUtil::PumpLoop() { | |
307 message_loop_.RunUntilIdle(); | |
308 } | |
OLD | NEW |