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.
4
5 #include "chrome/browser/search_engines/template_url_service_test_util.h"
6
7 #include "base/bind.h"
8 #include "base/run_loop.h"
9 #include "base/strings/string_split.h"
10 #include "base/threading/thread.h"
11 #include "chrome/browser/search_engines/default_search_manager.h"
12 #include "chrome/browser/search_engines/template_url_service.h"
13 #include "chrome/browser/search_engines/template_url_service_factory.h"
14 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h"
15 #include "chrome/browser/webdata/web_data_service_factory.h"
16 #include "chrome/test/base/testing_pref_service_syncable.h"
17 #include "chrome/test/base/testing_profile.h"
18 #include "components/google/core/browser/google_url_tracker.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20
21 #if defined(OS_CHROMEOS)
22 #include "chrome/browser/google/google_brand_chromeos.h"
23 #endif
24
25 // Trivial subclass of TemplateURLService that records the last invocation of
26 // SetKeywordSearchTermsForURL.
27 class TestingTemplateURLService : public TemplateURLService {
28 public:
Build(content::BrowserContext * profile)29 static KeyedService* Build(content::BrowserContext* profile) {
30 return new TestingTemplateURLService(static_cast<Profile*>(profile));
31 }
32
TestingTemplateURLService(Profile * profile)33 explicit TestingTemplateURLService(Profile* profile)
34 : TemplateURLService(profile) {
35 }
36
GetAndClearSearchTerm()37 base::string16 GetAndClearSearchTerm() {
38 base::string16 search_term;
39 search_term.swap(search_term_);
40 return search_term;
41 }
42
43 protected:
SetKeywordSearchTermsForURL(const TemplateURL * t_url,const GURL & url,const base::string16 & term)44 virtual void SetKeywordSearchTermsForURL(
45 const TemplateURL* t_url,
46 const GURL& url,
47 const base::string16& term) OVERRIDE {
48 search_term_ = term;
49 }
50
51 private:
52 base::string16 search_term_;
53
54 DISALLOW_COPY_AND_ASSIGN(TestingTemplateURLService);
55 };
56
57 // TemplateURLServiceTestUtilBase ---------------------------------------------
58
TemplateURLServiceTestUtilBase()59 TemplateURLServiceTestUtilBase::TemplateURLServiceTestUtilBase()
60 : changed_count_(0) {
61 }
62
~TemplateURLServiceTestUtilBase()63 TemplateURLServiceTestUtilBase::~TemplateURLServiceTestUtilBase() {
64 }
65
CreateTemplateUrlService()66 void TemplateURLServiceTestUtilBase::CreateTemplateUrlService() {
67 profile()->CreateWebDataService();
68
69 TemplateURLService* service = static_cast<TemplateURLService*>(
70 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
71 profile(), TestingTemplateURLService::Build));
72 service->AddObserver(this);
73 }
74
OnTemplateURLServiceChanged()75 void TemplateURLServiceTestUtilBase::OnTemplateURLServiceChanged() {
76 changed_count_++;
77 }
78
GetObserverCount()79 int TemplateURLServiceTestUtilBase::GetObserverCount() {
80 return changed_count_;
81 }
82
ResetObserverCount()83 void TemplateURLServiceTestUtilBase::ResetObserverCount() {
84 changed_count_ = 0;
85 }
86
VerifyLoad()87 void TemplateURLServiceTestUtilBase::VerifyLoad() {
88 ASSERT_FALSE(model()->loaded());
89 model()->Load();
90 base::RunLoop().RunUntilIdle();
91 EXPECT_EQ(1, GetObserverCount());
92 ResetObserverCount();
93 }
94
ChangeModelToLoadState()95 void TemplateURLServiceTestUtilBase::ChangeModelToLoadState() {
96 model()->ChangeToLoadedState();
97 // Initialize the web data service so that the database gets updated with
98 // any changes made.
99
100 model()->service_ = WebDataService::FromBrowserContext(profile());
101 base::RunLoop().RunUntilIdle();
102 }
103
ClearModel()104 void TemplateURLServiceTestUtilBase::ClearModel() {
105 TemplateURLServiceFactory::GetInstance()->SetTestingFactory(
106 profile(), NULL);
107 }
108
ResetModel(bool verify_load)109 void TemplateURLServiceTestUtilBase::ResetModel(bool verify_load) {
110 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
111 profile(), TestingTemplateURLService::Build);
112 model()->AddObserver(this);
113 changed_count_ = 0;
114 if (verify_load)
115 VerifyLoad();
116 }
117
GetAndClearSearchTerm()118 base::string16 TemplateURLServiceTestUtilBase::GetAndClearSearchTerm() {
119 return
120 static_cast<TestingTemplateURLService*>(model())->GetAndClearSearchTerm();
121 }
122
SetGoogleBaseURL(const GURL & base_url) const123 void TemplateURLServiceTestUtilBase::SetGoogleBaseURL(
124 const GURL& base_url) const {
125 DCHECK(base_url.is_valid());
126 UIThreadSearchTermsData data(profile());
127 GURL old_url = GURL(data.GoogleBaseURLValue());
128 UIThreadSearchTermsData::SetGoogleBaseURL(base_url.spec());
129 TemplateURLServiceFactory::GetForProfile(profile())
130 ->OnGoogleURLUpdated(old_url, base_url);
131 }
132
SetManagedDefaultSearchPreferences(bool enabled,const std::string & name,const std::string & keyword,const std::string & search_url,const std::string & suggest_url,const std::string & icon_url,const std::string & encodings,const std::string & alternate_url,const std::string & search_terms_replacement_key)133 void TemplateURLServiceTestUtilBase::SetManagedDefaultSearchPreferences(
134 bool enabled,
135 const std::string& name,
136 const std::string& keyword,
137 const std::string& search_url,
138 const std::string& suggest_url,
139 const std::string& icon_url,
140 const std::string& encodings,
141 const std::string& alternate_url,
142 const std::string& search_terms_replacement_key) {
143 TestingPrefServiceSyncable* pref_service = profile()->GetTestingPrefService();
144 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue);
145 if (!enabled) {
146 value->SetBoolean(DefaultSearchManager::kDisabledByPolicy, true);
147 pref_service->SetManagedPref(
148 DefaultSearchManager::kDefaultSearchProviderDataPrefName,
149 value.release());
150 return;
151 }
152
153 EXPECT_FALSE(keyword.empty());
154 EXPECT_FALSE(search_url.empty());
155 value->Set(DefaultSearchManager::kShortName,
156 base::Value::CreateStringValue(name));
157 value->Set(DefaultSearchManager::kKeyword,
158 base::Value::CreateStringValue(keyword));
159 value->Set(DefaultSearchManager::kURL,
160 base::Value::CreateStringValue(search_url));
161 value->Set(DefaultSearchManager::kSuggestionsURL,
162 base::Value::CreateStringValue(suggest_url));
163 value->Set(DefaultSearchManager::kFaviconURL,
164 base::Value::CreateStringValue(icon_url));
165 value->Set(DefaultSearchManager::kSearchTermsReplacementKey,
166 base::Value::CreateStringValue(search_terms_replacement_key));
167
168 std::vector<std::string> encodings_items;
169 base::SplitString(encodings, ';', &encodings_items);
170 scoped_ptr<base::ListValue> encodings_list(new base::ListValue);
171 for (std::vector<std::string>::const_iterator it = encodings_items.begin();
172 it != encodings_items.end();
173 ++it) {
174 encodings_list->AppendString(*it);
175 }
176 value->Set(DefaultSearchManager::kInputEncodings, encodings_list.release());
177
178 scoped_ptr<base::ListValue> alternate_url_list(new base::ListValue());
179 if (!alternate_url.empty())
180 alternate_url_list->Append(base::Value::CreateStringValue(alternate_url));
181 value->Set(DefaultSearchManager::kAlternateURLs,
182 alternate_url_list.release());
183
184 pref_service->SetManagedPref(
185 DefaultSearchManager::kDefaultSearchProviderDataPrefName,
186 value.release());
187 }
188
RemoveManagedDefaultSearchPreferences()189 void TemplateURLServiceTestUtilBase::RemoveManagedDefaultSearchPreferences() {
190 TestingPrefServiceSyncable* pref_service = profile()->GetTestingPrefService();
191 pref_service->RemoveManagedPref(
192 DefaultSearchManager::kDefaultSearchProviderDataPrefName);
193 }
194
model() const195 TemplateURLService* TemplateURLServiceTestUtilBase::model() const {
196 return TemplateURLServiceFactory::GetForProfile(profile());
197 }
198
199
200 // TemplateURLServiceTestUtil -------------------------------------------------
201
TemplateURLServiceTestUtil()202 TemplateURLServiceTestUtil::TemplateURLServiceTestUtil()
203 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {
204 }
205
~TemplateURLServiceTestUtil()206 TemplateURLServiceTestUtil::~TemplateURLServiceTestUtil() {
207 }
208
SetUp()209 void TemplateURLServiceTestUtil::SetUp() {
210 // Make unique temp directory.
211 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
212 profile_.reset(new TestingProfile(temp_dir_.path()));
213
214 TemplateURLServiceTestUtilBase::CreateTemplateUrlService();
215
216 #if defined(OS_CHROMEOS)
217 google_brand::chromeos::ClearBrandForCurrentSession();
218 #endif
219 }
220
TearDown()221 void TemplateURLServiceTestUtil::TearDown() {
222 profile_.reset();
223
224 UIThreadSearchTermsData::SetGoogleBaseURL(std::string());
225
226 // Flush the message loop to make application verifiers happy.
227 base::RunLoop().RunUntilIdle();
228 }
229
profile() const230 TestingProfile* TemplateURLServiceTestUtil::profile() const {
231 return profile_.get();
232 }
233