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/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" |
| 17 #include "chrome/common/chrome_constants.h" |
18 #include "chrome/common/chrome_notification_types.h" | 18 #include "chrome/common/chrome_notification_types.h" |
19 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
20 #include "chrome/test/automation/value_conversion_util.h" | 20 #include "chrome/test/automation/value_conversion_util.h" |
21 #include "chrome/test/base/testing_pref_service_syncable.h" | 21 #include "chrome/test/base/testing_pref_service_syncable.h" |
22 #include "chrome/test/base/testing_profile.h" | 22 #include "chrome/test/base/testing_profile.h" |
23 #include "content/public/browser/notification_service.h" | 23 #include "content/public/browser/notification_service.h" |
24 #include "content/public/test/test_browser_thread.h" | 24 #include "content/public/test/test_browser_thread.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
26 | 26 |
27 | 27 |
(...skipping 18 matching lines...) Expand all Loading... |
46 static void WaitForThreadToProcessRequests(BrowserThread::ID identifier) { | 46 static void WaitForThreadToProcessRequests(BrowserThread::ID identifier) { |
47 // Schedule a task on the thread that is processed after all | 47 // Schedule a task on the thread that is processed after all |
48 // pending requests on the thread. | 48 // pending requests on the thread. |
49 BrowserThread::PostTask(identifier, FROM_HERE, | 49 BrowserThread::PostTask(identifier, FROM_HERE, |
50 base::Bind(&QuitCallback, MessageLoop::current())); | 50 base::Bind(&QuitCallback, MessageLoop::current())); |
51 MessageLoop::current()->Run(); | 51 MessageLoop::current()->Run(); |
52 } | 52 } |
53 | 53 |
54 } // namespace | 54 } // namespace |
55 | 55 |
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 | 56 // Trivial subclass of TemplateURLService that records the last invocation of |
86 // SetKeywordSearchTermsForURL. | 57 // SetKeywordSearchTermsForURL. |
87 class TestingTemplateURLService : public TemplateURLService { | 58 class TestingTemplateURLService : public TemplateURLService { |
88 public: | 59 public: |
89 static ProfileKeyedService* Build(Profile* profile) { | 60 static ProfileKeyedService* Build(Profile* profile) { |
90 return new TestingTemplateURLService(profile); | 61 return new TestingTemplateURLService(profile); |
91 } | 62 } |
92 | 63 |
93 explicit TestingTemplateURLService(Profile* profile) | 64 explicit TestingTemplateURLService(Profile* profile) |
94 : TemplateURLService(profile) { | 65 : TemplateURLService(profile) { |
(...skipping 11 matching lines...) Expand all Loading... |
106 const string16& term) OVERRIDE { | 77 const string16& term) OVERRIDE { |
107 search_term_ = term; | 78 search_term_ = term; |
108 } | 79 } |
109 | 80 |
110 private: | 81 private: |
111 string16 search_term_; | 82 string16 search_term_; |
112 | 83 |
113 DISALLOW_COPY_AND_ASSIGN(TestingTemplateURLService); | 84 DISALLOW_COPY_AND_ASSIGN(TestingTemplateURLService); |
114 }; | 85 }; |
115 | 86 |
116 void TemplateURLServiceTestingProfile::SetUp() { | |
117 db_thread_.Start(); | |
118 | |
119 // Make unique temp directory. | |
120 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
121 | |
122 base::FilePath path = temp_dir_.path().AppendASCII("TestDataService.db"); | |
123 service_ = new WebDataService; | |
124 service_->Init(path); | |
125 } | |
126 | |
127 void TemplateURLServiceTestingProfile::TearDown() { | |
128 // Clear the request context so it will get deleted. This should be done | |
129 // before shutting down the I/O thread to avoid memory leaks. | |
130 ResetRequestContext(); | |
131 | |
132 // Wait for the delete of the request context to happen. | |
133 if (io_thread_.IsRunning()) | |
134 TemplateURLServiceTestUtil::BlockTillIOThreadProcessesRequests(); | |
135 | |
136 // The I/O thread must be shutdown before the DB thread. | |
137 io_thread_.Stop(); | |
138 | |
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 | |
145 // shutdown (so it can commit pending transactions) but before | |
146 // deleting the test profile directory, otherwise we may not be | |
147 // 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 | |
149 // carry on with the test. | |
150 base::WaitableEvent done(false, false); | |
151 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, | |
152 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done))); | |
153 done.Wait(); | |
154 db_thread_.Stop(); | |
155 } | |
156 | |
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() | 87 TemplateURLServiceTestUtil::TemplateURLServiceTestUtil() |
166 : ui_thread_(BrowserThread::UI, &message_loop_), | 88 : ui_thread_(BrowserThread::UI, &message_loop_), |
| 89 db_thread_(BrowserThread::DB), |
| 90 io_thread_(BrowserThread::IO), |
167 changed_count_(0) { | 91 changed_count_(0) { |
168 } | 92 } |
169 | 93 |
170 TemplateURLServiceTestUtil::~TemplateURLServiceTestUtil() { | 94 TemplateURLServiceTestUtil::~TemplateURLServiceTestUtil() { |
171 } | 95 } |
172 | 96 |
173 void TemplateURLServiceTestUtil::SetUp() { | 97 void TemplateURLServiceTestUtil::SetUp() { |
174 profile_.reset(new TemplateURLServiceTestingProfile()); | 98 ASSERT_TRUE(db_thread_.Start()); |
175 WebDataServiceFactory::GetInstance()->SetTestingFactory( | 99 // Make unique temp directory. |
176 profile_.get(), TemplateURLServiceTestingProfile:: | 100 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
177 GetWebDataServiceForTemplateURLServiceTestingProfile); | |
178 | 101 |
179 profile_->SetUp(); | 102 profile_.reset(new TestingProfile(temp_dir_.path())); |
| 103 profile_->CreateWebDataService(); |
| 104 |
180 TemplateURLService* service = static_cast<TemplateURLService*>( | 105 TemplateURLService* service = static_cast<TemplateURLService*>( |
181 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( | 106 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
182 profile_.get(), TestingTemplateURLService::Build)); | 107 profile_.get(), TestingTemplateURLService::Build)); |
183 service->AddObserver(this); | 108 service->AddObserver(this); |
184 | 109 |
185 #if defined(OS_CHROMEOS) | 110 #if defined(OS_CHROMEOS) |
186 google_util::chromeos::ClearBrandForCurrentSession(); | 111 google_util::chromeos::ClearBrandForCurrentSession(); |
187 #endif | 112 #endif |
188 } | 113 } |
189 | 114 |
190 void TemplateURLServiceTestUtil::TearDown() { | 115 void TemplateURLServiceTestUtil::TearDown() { |
| 116 // Clear the request context so it will get deleted. This should be done |
| 117 // before shutting down the I/O thread to avoid memory leaks. |
| 118 profile_->ResetRequestContext(); |
| 119 |
| 120 // Wait for the delete of the request context to happen. |
| 121 if (io_thread_.IsRunning()) |
| 122 TemplateURLServiceTestUtil::BlockTillIOThreadProcessesRequests(); |
| 123 |
| 124 // The I/O thread must be shutdown before the DB thread. |
| 125 io_thread_.Stop(); |
| 126 |
191 if (profile_.get()) { | 127 if (profile_.get()) { |
192 profile_->TearDown(); | |
193 profile_.reset(); | 128 profile_.reset(); |
194 } | 129 } |
| 130 base::WaitableEvent done(false, false); |
| 131 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, |
| 132 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done))); |
| 133 done.Wait(); |
| 134 db_thread_.Stop(); |
195 UIThreadSearchTermsData::SetGoogleBaseURL(std::string()); | 135 UIThreadSearchTermsData::SetGoogleBaseURL(std::string()); |
196 | 136 |
197 // Flush the message loop to make application verifiers happy. | 137 // Flush the message loop to make application verifiers happy. |
198 message_loop_.RunUntilIdle(); | 138 message_loop_.RunUntilIdle(); |
199 } | 139 } |
200 | 140 |
201 void TemplateURLServiceTestUtil::OnTemplateURLServiceChanged() { | 141 void TemplateURLServiceTestUtil::OnTemplateURLServiceChanged() { |
202 changed_count_++; | 142 changed_count_++; |
203 } | 143 } |
204 | 144 |
(...skipping 18 matching lines...) Expand all Loading... |
223 model()->Load(); | 163 model()->Load(); |
224 BlockTillServiceProcessesRequests(); | 164 BlockTillServiceProcessesRequests(); |
225 EXPECT_EQ(1, GetObserverCount()); | 165 EXPECT_EQ(1, GetObserverCount()); |
226 ResetObserverCount(); | 166 ResetObserverCount(); |
227 } | 167 } |
228 | 168 |
229 void TemplateURLServiceTestUtil::ChangeModelToLoadState() { | 169 void TemplateURLServiceTestUtil::ChangeModelToLoadState() { |
230 model()->ChangeToLoadedState(); | 170 model()->ChangeToLoadedState(); |
231 // Initialize the web data service so that the database gets updated with | 171 // Initialize the web data service so that the database gets updated with |
232 // any changes made. | 172 // any changes made. |
233 | 173 model()->service_ = WebDataService::FromBrowserContext(profile_.get()); |
234 model()->service_ = WebDataServiceFactory::GetForProfile( | 174 BlockTillServiceProcessesRequests(); |
235 profile_.get(), Profile::EXPLICIT_ACCESS); | |
236 } | 175 } |
237 | 176 |
238 void TemplateURLServiceTestUtil::ClearModel() { | 177 void TemplateURLServiceTestUtil::ClearModel() { |
239 TemplateURLServiceFactory::GetInstance()->SetTestingFactory( | 178 TemplateURLServiceFactory::GetInstance()->SetTestingFactory( |
240 profile_.get(), NULL); | 179 profile_.get(), NULL); |
241 } | 180 } |
242 | 181 |
243 void TemplateURLServiceTestUtil::ResetModel(bool verify_load) { | 182 void TemplateURLServiceTestUtil::ResetModel(bool verify_load) { |
244 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( | 183 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
245 profile_.get(), TestingTemplateURLService::Build); | 184 profile_.get(), TestingTemplateURLService::Build); |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 | 262 |
324 TemplateURLService* TemplateURLServiceTestUtil::model() const { | 263 TemplateURLService* TemplateURLServiceTestUtil::model() const { |
325 return TemplateURLServiceFactory::GetForProfile(profile_.get()); | 264 return TemplateURLServiceFactory::GetForProfile(profile_.get()); |
326 } | 265 } |
327 | 266 |
328 TestingProfile* TemplateURLServiceTestUtil::profile() const { | 267 TestingProfile* TemplateURLServiceTestUtil::profile() const { |
329 return profile_.get(); | 268 return profile_.get(); |
330 } | 269 } |
331 | 270 |
332 void TemplateURLServiceTestUtil::StartIOThread() { | 271 void TemplateURLServiceTestUtil::StartIOThread() { |
333 profile_->StartIOThread(); | 272 io_thread_.StartIOThread(); |
334 } | 273 } |
335 | 274 |
336 void TemplateURLServiceTestUtil::PumpLoop() { | 275 void TemplateURLServiceTestUtil::PumpLoop() { |
337 message_loop_.RunUntilIdle(); | 276 message_loop_.RunUntilIdle(); |
338 } | 277 } |
OLD | NEW |