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.h" | 8 #include "base/message_loop.h" |
9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
10 #include "base/scoped_temp_dir.h" | 10 #include "base/scoped_temp_dir.h" |
| 11 #include "base/synchronization/waitable_event.h" |
11 #include "base/threading/thread.h" | 12 #include "base/threading/thread.h" |
12 #include "chrome/browser/search_engines/search_terms_data.h" | 13 #include "chrome/browser/search_engines/search_terms_data.h" |
13 #include "chrome/browser/search_engines/template_url_service.h" | 14 #include "chrome/browser/search_engines/template_url_service.h" |
14 #include "chrome/browser/search_engines/template_url_service_factory.h" | 15 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 16 #include "chrome/browser/webdata/web_data_service_factory.h" |
15 #include "chrome/common/chrome_notification_types.h" | 17 #include "chrome/common/chrome_notification_types.h" |
16 #include "chrome/common/pref_names.h" | 18 #include "chrome/common/pref_names.h" |
17 #include "chrome/test/base/testing_pref_service.h" | 19 #include "chrome/test/base/testing_pref_service.h" |
18 #include "chrome/test/base/testing_profile.h" | 20 #include "chrome/test/base/testing_profile.h" |
19 #include "content/public/browser/notification_service.h" | 21 #include "content/public/browser/notification_service.h" |
20 #include "content/test/test_browser_thread.h" | 22 #include "content/test/test_browser_thread.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
22 | 24 |
23 using content::BrowserThread; | 25 using content::BrowserThread; |
24 | 26 |
(...skipping 30 matching lines...) Expand all Loading... |
55 | 57 |
56 void SetUp(); | 58 void SetUp(); |
57 void TearDown(); | 59 void TearDown(); |
58 | 60 |
59 // Starts the I/O thread. This isn't done automatically because not every test | 61 // Starts the I/O thread. This isn't done automatically because not every test |
60 // needs this. | 62 // needs this. |
61 void StartIOThread() { | 63 void StartIOThread() { |
62 io_thread_.StartIOThread(); | 64 io_thread_.StartIOThread(); |
63 } | 65 } |
64 | 66 |
65 virtual WebDataService* GetWebDataService(ServiceAccessType access) { | 67 static scoped_refptr<RefcountedProfileKeyedService> |
66 return service_.get(); | 68 GetWebDataServiceForTemplateURLServiceTestingProfile(Profile* profile); |
67 } | |
68 | 69 |
69 private: | 70 private: |
70 scoped_refptr<WebDataService> service_; | 71 scoped_refptr<WebDataService> service_; |
71 ScopedTempDir temp_dir_; | 72 ScopedTempDir temp_dir_; |
72 content::TestBrowserThread db_thread_; | 73 content::TestBrowserThread db_thread_; |
73 content::TestBrowserThread io_thread_; | 74 content::TestBrowserThread io_thread_; |
74 }; | 75 }; |
75 | 76 |
| 77 |
76 // Trivial subclass of TemplateURLService that records the last invocation of | 78 // Trivial subclass of TemplateURLService that records the last invocation of |
77 // SetKeywordSearchTermsForURL. | 79 // SetKeywordSearchTermsForURL. |
78 class TestingTemplateURLService : public TemplateURLService { | 80 class TestingTemplateURLService : public TemplateURLService { |
79 public: | 81 public: |
80 static ProfileKeyedService* Build(Profile* profile) { | 82 static ProfileKeyedService* Build(Profile* profile) { |
81 return new TestingTemplateURLService(profile); | 83 return new TestingTemplateURLService(profile); |
82 } | 84 } |
83 | 85 |
84 explicit TestingTemplateURLService(Profile* profile) | 86 explicit TestingTemplateURLService(Profile* profile) |
85 : TemplateURLService(profile) { | 87 : TemplateURLService(profile) { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 ResetRequestContext(); | 123 ResetRequestContext(); |
122 | 124 |
123 // Wait for the delete of the request context to happen. | 125 // Wait for the delete of the request context to happen. |
124 if (io_thread_.IsRunning()) | 126 if (io_thread_.IsRunning()) |
125 TemplateURLServiceTestUtil::BlockTillIOThreadProcessesRequests(); | 127 TemplateURLServiceTestUtil::BlockTillIOThreadProcessesRequests(); |
126 | 128 |
127 // The I/O thread must be shutdown before the DB thread. | 129 // The I/O thread must be shutdown before the DB thread. |
128 io_thread_.Stop(); | 130 io_thread_.Stop(); |
129 | 131 |
130 // Clean up the test directory. | 132 // Clean up the test directory. |
131 if (service_.get()) | 133 if (service_.get()) { |
132 service_->Shutdown(); | 134 service_->ShutdownOnUIThread(); |
| 135 service_ = NULL; |
| 136 } |
133 // Note that we must ensure the DB thread is stopped after WDS | 137 // Note that we must ensure the DB thread is stopped after WDS |
134 // shutdown (so it can commit pending transactions) but before | 138 // shutdown (so it can commit pending transactions) but before |
135 // deleting the test profile directory, otherwise we may not be | 139 // deleting the test profile directory, otherwise we may not be |
136 // able to delete it due to an open transaction. | 140 // able to delete it due to an open transaction. |
| 141 // Schedule another task on the DB thread to notify us that it's safe to |
| 142 // carry on with the test. |
| 143 base::WaitableEvent done(false, false); |
| 144 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, |
| 145 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done))); |
| 146 done.Wait(); |
137 db_thread_.Stop(); | 147 db_thread_.Stop(); |
138 } | 148 } |
139 | 149 |
| 150 scoped_refptr<RefcountedProfileKeyedService> |
| 151 TemplateURLServiceTestingProfile:: |
| 152 GetWebDataServiceForTemplateURLServiceTestingProfile(Profile* profile) { |
| 153 TemplateURLServiceTestingProfile* test_profile = |
| 154 reinterpret_cast<TemplateURLServiceTestingProfile*>(profile); |
| 155 return test_profile->service_; |
| 156 } |
| 157 |
140 TemplateURLServiceTestUtil::TemplateURLServiceTestUtil() | 158 TemplateURLServiceTestUtil::TemplateURLServiceTestUtil() |
141 : ui_thread_(BrowserThread::UI, &message_loop_), | 159 : ui_thread_(BrowserThread::UI, &message_loop_), |
142 changed_count_(0) { | 160 changed_count_(0) { |
143 } | 161 } |
144 | 162 |
145 TemplateURLServiceTestUtil::~TemplateURLServiceTestUtil() { | 163 TemplateURLServiceTestUtil::~TemplateURLServiceTestUtil() { |
146 } | 164 } |
147 | 165 |
148 void TemplateURLServiceTestUtil::SetUp() { | 166 void TemplateURLServiceTestUtil::SetUp() { |
149 profile_.reset(new TemplateURLServiceTestingProfile()); | 167 profile_.reset(new TemplateURLServiceTestingProfile()); |
| 168 WebDataServiceFactory::GetInstance()->SetTestingFactory( |
| 169 profile_.get(), TemplateURLServiceTestingProfile:: |
| 170 GetWebDataServiceForTemplateURLServiceTestingProfile); |
| 171 |
150 profile_->SetUp(); | 172 profile_->SetUp(); |
151 TemplateURLService* service = static_cast<TemplateURLService*>( | 173 TemplateURLService* service = static_cast<TemplateURLService*>( |
152 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( | 174 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
153 profile_.get(), TestingTemplateURLService::Build)); | 175 profile_.get(), TestingTemplateURLService::Build)); |
154 service->AddObserver(this); | 176 service->AddObserver(this); |
155 } | 177 } |
156 | 178 |
157 void TemplateURLServiceTestUtil::TearDown() { | 179 void TemplateURLServiceTestUtil::TearDown() { |
158 if (profile_.get()) { | 180 if (profile_.get()) { |
159 profile_->TearDown(); | 181 profile_->TearDown(); |
(...skipping 30 matching lines...) Expand all Loading... |
190 model()->Load(); | 212 model()->Load(); |
191 BlockTillServiceProcessesRequests(); | 213 BlockTillServiceProcessesRequests(); |
192 EXPECT_EQ(1, GetObserverCount()); | 214 EXPECT_EQ(1, GetObserverCount()); |
193 ResetObserverCount(); | 215 ResetObserverCount(); |
194 } | 216 } |
195 | 217 |
196 void TemplateURLServiceTestUtil::ChangeModelToLoadState() { | 218 void TemplateURLServiceTestUtil::ChangeModelToLoadState() { |
197 model()->ChangeToLoadedState(); | 219 model()->ChangeToLoadedState(); |
198 // Initialize the web data service so that the database gets updated with | 220 // Initialize the web data service so that the database gets updated with |
199 // any changes made. | 221 // any changes made. |
200 model()->service_ = profile_->GetWebDataService(Profile::EXPLICIT_ACCESS); | 222 model()->service_ = WebDataServiceFactory::GetForProfile( |
| 223 profile_.get(), Profile::EXPLICIT_ACCESS); |
201 } | 224 } |
202 | 225 |
203 void TemplateURLServiceTestUtil::ClearModel() { | 226 void TemplateURLServiceTestUtil::ClearModel() { |
204 TemplateURLServiceFactory::GetInstance()->SetTestingFactory( | 227 TemplateURLServiceFactory::GetInstance()->SetTestingFactory( |
205 profile_.get(), NULL); | 228 profile_.get(), NULL); |
206 } | 229 } |
207 | 230 |
208 void TemplateURLServiceTestUtil::ResetModel(bool verify_load) { | 231 void TemplateURLServiceTestUtil::ResetModel(bool verify_load) { |
209 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( | 232 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
210 profile_.get(), TestingTemplateURLService::Build); | 233 profile_.get(), TestingTemplateURLService::Build); |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 return profile_.get(); | 303 return profile_.get(); |
281 } | 304 } |
282 | 305 |
283 void TemplateURLServiceTestUtil::StartIOThread() { | 306 void TemplateURLServiceTestUtil::StartIOThread() { |
284 profile_->StartIOThread(); | 307 profile_->StartIOThread(); |
285 } | 308 } |
286 | 309 |
287 void TemplateURLServiceTestUtil::PumpLoop() { | 310 void TemplateURLServiceTestUtil::PumpLoop() { |
288 message_loop_.RunAllPending(); | 311 message_loop_.RunAllPending(); |
289 } | 312 } |
OLD | NEW |