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
3 // found in the LICENSE file.
5 #include "chrome/browser/search_engines/template_url_service_test_util.h"
8 #include "base/run_loop.h"
9 #include "base/threading/thread.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/google/google_url_tracker.h"
12 #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_factory.h"
15 #include "chrome/browser/webdata/web_data_service_factory.h"
16 #include "chrome/common/pref_names.h"
17 #include "chrome/test/base/testing_pref_service_syncable.h"
18 #include "chrome/test/base/testing_profile.h"
19 #include "content/public/browser/notification_service.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 #if defined(OS_CHROMEOS)
23 #include "chrome/browser/google/google_util_chromeos.h"
26 // Trivial subclass of TemplateURLService that records the last invocation of
27 // SetKeywordSearchTermsForURL.
28 class TestingTemplateURLService : public TemplateURLService {
30 static BrowserContextKeyedService* Build(content::BrowserContext* profile) {
31 return new TestingTemplateURLService(static_cast<Profile*>(profile));
34 explicit TestingTemplateURLService(Profile* profile)
35 : TemplateURLService(profile) {
38 string16 GetAndClearSearchTerm() {
40 search_term.swap(search_term_);
45 virtual void SetKeywordSearchTermsForURL(const TemplateURL* t_url,
47 const string16& term) OVERRIDE {
52 string16 search_term_;
54 DISALLOW_COPY_AND_ASSIGN(TestingTemplateURLService);
57 // TemplateURLServiceTestUtilBase ---------------------------------------------
59 TemplateURLServiceTestUtilBase::TemplateURLServiceTestUtilBase()
63 TemplateURLServiceTestUtilBase::~TemplateURLServiceTestUtilBase() {
66 void TemplateURLServiceTestUtilBase::CreateTemplateUrlService() {
67 profile()->CreateWebDataService();
69 TemplateURLService* service = static_cast<TemplateURLService*>(
70 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
71 profile(), TestingTemplateURLService::Build));
72 service->AddObserver(this);
75 void TemplateURLServiceTestUtilBase::OnTemplateURLServiceChanged() {
79 int TemplateURLServiceTestUtilBase::GetObserverCount() {
80 return changed_count_;
83 void TemplateURLServiceTestUtilBase::ResetObserverCount() {
87 void TemplateURLServiceTestUtilBase::VerifyLoad() {
88 ASSERT_FALSE(model()->loaded());
90 base::RunLoop().RunUntilIdle();
91 EXPECT_EQ(1, GetObserverCount());
95 void TemplateURLServiceTestUtilBase::ChangeModelToLoadState() {
96 model()->ChangeToLoadedState();
97 // Initialize the web data service so that the database gets updated with
100 model()->service_ = WebDataService::FromBrowserContext(profile());
101 base::RunLoop().RunUntilIdle();
104 void TemplateURLServiceTestUtilBase::ClearModel() {
105 TemplateURLServiceFactory::GetInstance()->SetTestingFactory(
109 void TemplateURLServiceTestUtilBase::ResetModel(bool verify_load) {
110 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
111 profile(), TestingTemplateURLService::Build);
112 model()->AddObserver(this);
118 string16 TemplateURLServiceTestUtilBase::GetAndClearSearchTerm() {
120 static_cast<TestingTemplateURLService*>(model())->GetAndClearSearchTerm();
123 void TemplateURLServiceTestUtilBase::SetGoogleBaseURL(
124 const GURL& base_url) const {
125 DCHECK(base_url.is_valid());
126 UIThreadSearchTermsData data(profile());
127 GoogleURLTracker::UpdatedDetails urls(GURL(data.GoogleBaseURLValue()),
129 UIThreadSearchTermsData::SetGoogleBaseURL(base_url.spec());
130 content::NotificationService::current()->Notify(
131 chrome::NOTIFICATION_GOOGLE_URL_UPDATED,
132 content::Source<Profile>(profile()),
133 content::Details<GoogleURLTracker::UpdatedDetails>(&urls));
136 void TemplateURLServiceTestUtilBase::SetManagedDefaultSearchPreferences(
138 const std::string& name,
139 const std::string& keyword,
140 const std::string& search_url,
141 const std::string& suggest_url,
142 const std::string& icon_url,
143 const std::string& encodings,
144 const std::string& alternate_url,
145 const std::string& search_terms_replacement_key) {
146 TestingPrefServiceSyncable* pref_service = profile()->GetTestingPrefService();
147 pref_service->SetManagedPref(prefs::kDefaultSearchProviderEnabled,
148 Value::CreateBooleanValue(enabled));
149 pref_service->SetManagedPref(prefs::kDefaultSearchProviderName,
150 Value::CreateStringValue(name));
151 pref_service->SetManagedPref(prefs::kDefaultSearchProviderKeyword,
152 Value::CreateStringValue(keyword));
153 pref_service->SetManagedPref(prefs::kDefaultSearchProviderSearchURL,
154 Value::CreateStringValue(search_url));
155 pref_service->SetManagedPref(prefs::kDefaultSearchProviderSuggestURL,
156 Value::CreateStringValue(suggest_url));
157 pref_service->SetManagedPref(prefs::kDefaultSearchProviderIconURL,
158 Value::CreateStringValue(icon_url));
159 pref_service->SetManagedPref(prefs::kDefaultSearchProviderEncodings,
160 Value::CreateStringValue(encodings));
161 scoped_ptr<base::ListValue> alternate_url_list(new base::ListValue());
162 if (!alternate_url.empty())
163 alternate_url_list->Append(Value::CreateStringValue(alternate_url));
164 pref_service->SetManagedPref(prefs::kDefaultSearchProviderAlternateURLs,
165 alternate_url_list.release());
166 pref_service->SetManagedPref(
167 prefs::kDefaultSearchProviderSearchTermsReplacementKey,
168 Value::CreateStringValue(search_terms_replacement_key));
169 model()->Observe(chrome::NOTIFICATION_DEFAULT_SEARCH_POLICY_CHANGED,
170 content::NotificationService::AllSources(),
171 content::NotificationService::NoDetails());
174 void TemplateURLServiceTestUtilBase::RemoveManagedDefaultSearchPreferences() {
175 TestingPrefServiceSyncable* pref_service = profile()->GetTestingPrefService();
176 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderEnabled);
177 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderName);
178 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderKeyword);
179 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderSearchURL);
180 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderSuggestURL);
181 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderIconURL);
182 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderEncodings);
183 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderAlternateURLs);
184 pref_service->RemoveManagedPref(
185 prefs::kDefaultSearchProviderSearchTermsReplacementKey);
186 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderID);
187 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderPrepopulateID);
188 model()->Observe(chrome::NOTIFICATION_DEFAULT_SEARCH_POLICY_CHANGED,
189 content::NotificationService::AllSources(),
190 content::NotificationService::NoDetails());
193 TemplateURLService* TemplateURLServiceTestUtilBase::model() const {
194 return TemplateURLServiceFactory::GetForProfile(profile());
198 // TemplateURLServiceTestUtil -------------------------------------------------
200 TemplateURLServiceTestUtil::TemplateURLServiceTestUtil()
201 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {
204 TemplateURLServiceTestUtil::~TemplateURLServiceTestUtil() {
207 void TemplateURLServiceTestUtil::SetUp() {
208 // Make unique temp directory.
209 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
210 profile_.reset(new TestingProfile(temp_dir_.path()));
212 TemplateURLServiceTestUtilBase::CreateTemplateUrlService();
214 #if defined(OS_CHROMEOS)
215 google_util::chromeos::ClearBrandForCurrentSession();
219 void TemplateURLServiceTestUtil::TearDown() {
222 UIThreadSearchTermsData::SetGoogleBaseURL(std::string());
224 // Flush the message loop to make application verifiers happy.
225 base::RunLoop().RunUntilIdle();
228 TestingProfile* TemplateURLServiceTestUtil::profile() const {
229 return profile_.get();