1 // Copyright 2014 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 "base/command_line.h"
6 #include "base/files/scoped_temp_dir.h"
7 #include "base/memory/scoped_vector.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "components/google/core/browser/google_switches.h"
10 #include "components/pref_registry/testing_pref_service_syncable.h"
11 #include "components/search_engines/prepopulated_engines.h"
12 #include "components/search_engines/search_engines_pref_names.h"
13 #include "components/search_engines/search_terms_data.h"
14 #include "components/search_engines/template_url.h"
15 #include "components/search_engines/template_url_prepopulate_data.h"
16 #include "components/search_engines/template_url_service.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 using base::ASCIIToUTF16
;
23 SearchEngineType
GetEngineType(const std::string
& url
) {
26 return TemplateURLPrepopulateData::GetEngineType(TemplateURL(data
),
30 std::string
GetHostFromTemplateURLData(const TemplateURLData
& data
) {
31 return TemplateURL(data
).url_ref().GetHost(SearchTermsData());
36 class TemplateURLPrepopulateDataTest
: public testing::Test
{
38 void SetUp() override
{
39 TemplateURLPrepopulateData::RegisterProfilePrefs(prefs_
.registry());
43 user_prefs::TestingPrefServiceSyncable prefs_
;
46 // Verifies the set of prepopulate data doesn't contain entries with duplicate
48 TEST_F(TemplateURLPrepopulateDataTest
, UniqueIDs
) {
49 const int kCountryIds
[] = {
50 'A'<<8|'D', 'A'<<8|'E', 'A'<<8|'F', 'A'<<8|'G', 'A'<<8|'I',
51 'A'<<8|'L', 'A'<<8|'M', 'A'<<8|'N', 'A'<<8|'O', 'A'<<8|'Q',
52 'A'<<8|'R', 'A'<<8|'S', 'A'<<8|'T', 'A'<<8|'U', 'A'<<8|'W',
53 'A'<<8|'X', 'A'<<8|'Z', 'B'<<8|'A', 'B'<<8|'B', 'B'<<8|'D',
54 'B'<<8|'E', 'B'<<8|'F', 'B'<<8|'G', 'B'<<8|'H', 'B'<<8|'I',
55 'B'<<8|'J', 'B'<<8|'M', 'B'<<8|'N', 'B'<<8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'Y', 'S'<<8|'Z', 'T'<<8|'C', 'T'<<8|'D',
92 'T'<<8|'F', 'T'<<8|'G', 'T'<<8|'H', 'T'<<8|'J', 'T'<<8|'K',
93 'T'<<8|'L', 'T'<<8|'M', 'T'<<8|'N', 'T'<<8|'O', 'T'<<8|'R',
94 'T'<<8|'T', 'T'<<8|'V', 'T'<<8|'W', 'T'<<8|'Z', 'U'<<8|'A',
95 'U'<<8|'G', 'U'<<8|'M', 'U'<<8|'S', 'U'<<8|'Y', 'U'<<8|'Z',
96 'V'<<8|'A', 'V'<<8|'C', 'V'<<8|'E', 'V'<<8|'G', 'V'<<8|'I',
97 'V'<<8|'N', 'V'<<8|'U', 'W'<<8|'F', 'W'<<8|'S', 'Y'<<8|'E',
98 'Y'<<8|'T', 'Z'<<8|'A', 'Z'<<8|'M', 'Z'<<8|'W', -1 };
100 for (size_t i
= 0; i
< arraysize(kCountryIds
); ++i
) {
101 prefs_
.SetInteger(prefs::kCountryIDAtInstall
, kCountryIds
[i
]);
102 size_t default_index
;
103 ScopedVector
<TemplateURLData
> urls
=
104 TemplateURLPrepopulateData::GetPrepopulatedEngines(&prefs_
,
106 std::set
<int> unique_ids
;
107 for (size_t turl_i
= 0; turl_i
< urls
.size(); ++turl_i
) {
108 ASSERT_TRUE(unique_ids
.find(urls
[turl_i
]->prepopulate_id
) ==
110 unique_ids
.insert(urls
[turl_i
]->prepopulate_id
);
115 // Verifies that default search providers from the preferences file
116 // override the built-in ones.
117 TEST_F(TemplateURLPrepopulateDataTest
, ProvidersFromPrefs
) {
118 prefs_
.SetUserPref(prefs::kSearchProviderOverridesVersion
,
119 new base::FundamentalValue(1));
120 base::ListValue
* overrides
= new base::ListValue
;
121 scoped_ptr
<base::DictionaryValue
> entry(new base::DictionaryValue
);
122 // Set only the minimal required settings for a search provider configuration.
123 entry
->SetString("name", "foo");
124 entry
->SetString("keyword", "fook");
125 entry
->SetString("search_url", "http://foo.com/s?q={searchTerms}");
126 entry
->SetString("favicon_url", "http://foi.com/favicon.ico");
127 entry
->SetString("encoding", "UTF-8");
128 entry
->SetInteger("id", 1001);
129 overrides
->Append(entry
->DeepCopy());
130 prefs_
.SetUserPref(prefs::kSearchProviderOverrides
, overrides
);
132 int version
= TemplateURLPrepopulateData::GetDataVersion(&prefs_
);
133 EXPECT_EQ(1, version
);
135 size_t default_index
;
136 ScopedVector
<TemplateURLData
> t_urls
=
137 TemplateURLPrepopulateData::GetPrepopulatedEngines(&prefs_
,
140 ASSERT_EQ(1u, t_urls
.size());
141 EXPECT_EQ(ASCIIToUTF16("foo"), t_urls
[0]->short_name());
142 EXPECT_EQ(ASCIIToUTF16("fook"), t_urls
[0]->keyword());
143 EXPECT_EQ("foo.com", GetHostFromTemplateURLData(*t_urls
[0]));
144 EXPECT_EQ("foi.com", t_urls
[0]->favicon_url
.host());
145 EXPECT_EQ(1u, t_urls
[0]->input_encodings
.size());
146 EXPECT_EQ(1001, t_urls
[0]->prepopulate_id
);
147 EXPECT_TRUE(t_urls
[0]->suggestions_url
.empty());
148 EXPECT_TRUE(t_urls
[0]->instant_url
.empty());
149 EXPECT_EQ(0u, t_urls
[0]->alternate_urls
.size());
150 EXPECT_TRUE(t_urls
[0]->search_terms_replacement_key
.empty());
152 // Test the optional settings too.
153 entry
->SetString("suggest_url", "http://foo.com/suggest?q={searchTerms}");
154 entry
->SetString("instant_url", "http://foo.com/instant?q={searchTerms}");
155 base::ListValue
* alternate_urls
= new base::ListValue
;
156 alternate_urls
->AppendString("http://foo.com/alternate?q={searchTerms}");
157 entry
->Set("alternate_urls", alternate_urls
);
158 entry
->SetString("search_terms_replacement_key", "espv");
159 overrides
= new base::ListValue
;
160 overrides
->Append(entry
->DeepCopy());
161 prefs_
.SetUserPref(prefs::kSearchProviderOverrides
, overrides
);
163 t_urls
= TemplateURLPrepopulateData::GetPrepopulatedEngines(
164 &prefs_
, &default_index
);
165 ASSERT_EQ(1u, t_urls
.size());
166 EXPECT_EQ(ASCIIToUTF16("foo"), t_urls
[0]->short_name());
167 EXPECT_EQ(ASCIIToUTF16("fook"), t_urls
[0]->keyword());
168 EXPECT_EQ("foo.com", GetHostFromTemplateURLData(*t_urls
[0]));
169 EXPECT_EQ("foi.com", t_urls
[0]->favicon_url
.host());
170 EXPECT_EQ(1u, t_urls
[0]->input_encodings
.size());
171 EXPECT_EQ(1001, t_urls
[0]->prepopulate_id
);
172 EXPECT_EQ("http://foo.com/suggest?q={searchTerms}",
173 t_urls
[0]->suggestions_url
);
174 EXPECT_EQ("http://foo.com/instant?q={searchTerms}",
175 t_urls
[0]->instant_url
);
176 ASSERT_EQ(1u, t_urls
[0]->alternate_urls
.size());
177 EXPECT_EQ("http://foo.com/alternate?q={searchTerms}",
178 t_urls
[0]->alternate_urls
[0]);
179 EXPECT_EQ("espv", t_urls
[0]->search_terms_replacement_key
);
181 // Test that subsequent providers are loaded even if an intermediate
182 // provider has an incomplete configuration.
183 overrides
= new base::ListValue
;
184 overrides
->Append(entry
->DeepCopy());
185 entry
->SetInteger("id", 1002);
186 entry
->SetString("name", "bar");
187 entry
->SetString("keyword", "bark");
188 entry
->SetString("encoding", std::string());
189 overrides
->Append(entry
->DeepCopy());
190 entry
->SetInteger("id", 1003);
191 entry
->SetString("name", "baz");
192 entry
->SetString("keyword", "bazk");
193 entry
->SetString("encoding", "UTF-8");
194 overrides
->Append(entry
->DeepCopy());
195 prefs_
.SetUserPref(prefs::kSearchProviderOverrides
, overrides
);
198 TemplateURLPrepopulateData::GetPrepopulatedEngines(&prefs_
,
200 EXPECT_EQ(2u, t_urls
.size());
203 TEST_F(TemplateURLPrepopulateDataTest
, ClearProvidersFromPrefs
) {
204 prefs_
.SetUserPref(prefs::kSearchProviderOverridesVersion
,
205 new base::FundamentalValue(1));
206 base::ListValue
* overrides
= new base::ListValue
;
207 base::DictionaryValue
* entry(new base::DictionaryValue
);
208 // Set only the minimal required settings for a search provider configuration.
209 entry
->SetString("name", "foo");
210 entry
->SetString("keyword", "fook");
211 entry
->SetString("search_url", "http://foo.com/s?q={searchTerms}");
212 entry
->SetString("favicon_url", "http://foi.com/favicon.ico");
213 entry
->SetString("encoding", "UTF-8");
214 entry
->SetInteger("id", 1001);
215 overrides
->Append(entry
);
216 prefs_
.SetUserPref(prefs::kSearchProviderOverrides
, overrides
);
218 int version
= TemplateURLPrepopulateData::GetDataVersion(&prefs_
);
219 EXPECT_EQ(1, version
);
221 // This call removes the above search engine.
222 TemplateURLPrepopulateData::ClearPrepopulatedEnginesInPrefs(&prefs_
);
224 version
= TemplateURLPrepopulateData::GetDataVersion(&prefs_
);
225 EXPECT_EQ(TemplateURLPrepopulateData::kCurrentDataVersion
, version
);
227 size_t default_index
;
228 ScopedVector
<TemplateURLData
> t_urls
=
229 TemplateURLPrepopulateData::GetPrepopulatedEngines(&prefs_
,
231 ASSERT_FALSE(t_urls
.empty());
232 for (size_t i
= 0; i
< t_urls
.size(); ++i
) {
233 EXPECT_NE(ASCIIToUTF16("foo"), t_urls
[i
]->short_name());
234 EXPECT_NE(ASCIIToUTF16("fook"), t_urls
[i
]->keyword());
235 EXPECT_NE("foi.com", t_urls
[i
]->favicon_url
.host());
236 EXPECT_NE("foo.com", GetHostFromTemplateURLData(*t_urls
[i
]));
237 EXPECT_NE(1001, t_urls
[i
]->prepopulate_id
);
239 // Ensures the default URL is Google and has the optional fields filled.
240 EXPECT_EQ(ASCIIToUTF16("Google"), t_urls
[default_index
]->short_name());
241 EXPECT_FALSE(t_urls
[default_index
]->suggestions_url
.empty());
242 EXPECT_FALSE(t_urls
[default_index
]->instant_url
.empty());
243 EXPECT_FALSE(t_urls
[default_index
]->image_url
.empty());
244 EXPECT_FALSE(t_urls
[default_index
]->new_tab_url
.empty());
245 EXPECT_FALSE(t_urls
[default_index
]->contextual_search_url
.empty());
246 EXPECT_FALSE(t_urls
[default_index
]->image_url_post_params
.empty());
247 EXPECT_EQ(SEARCH_ENGINE_GOOGLE
,
248 TemplateURLPrepopulateData::GetEngineType(
249 TemplateURL(*t_urls
[default_index
]),
253 // Verifies that built-in search providers are processed correctly.
254 TEST_F(TemplateURLPrepopulateDataTest
, ProvidersFromPrepopulated
) {
255 // Use United States.
256 prefs_
.SetInteger(prefs::kCountryIDAtInstall
, 'U'<<8|'S');
257 size_t default_index
;
258 ScopedVector
<TemplateURLData
> t_urls
=
259 TemplateURLPrepopulateData::GetPrepopulatedEngines(&prefs_
,
262 // Ensure all the URLs have the required fields populated.
263 ASSERT_FALSE(t_urls
.empty());
264 for (size_t i
= 0; i
< t_urls
.size(); ++i
) {
265 ASSERT_FALSE(t_urls
[i
]->short_name().empty());
266 ASSERT_FALSE(t_urls
[i
]->keyword().empty());
267 ASSERT_FALSE(t_urls
[i
]->favicon_url
.host().empty());
268 ASSERT_FALSE(GetHostFromTemplateURLData(*t_urls
[i
]).empty());
269 ASSERT_FALSE(t_urls
[i
]->input_encodings
.empty());
270 EXPECT_GT(t_urls
[i
]->prepopulate_id
, 0);
273 // Ensures the default URL is Google and has the optional fields filled.
274 EXPECT_EQ(ASCIIToUTF16("Google"), t_urls
[default_index
]->short_name());
275 EXPECT_FALSE(t_urls
[default_index
]->suggestions_url
.empty());
276 EXPECT_FALSE(t_urls
[default_index
]->instant_url
.empty());
277 EXPECT_FALSE(t_urls
[default_index
]->image_url
.empty());
278 EXPECT_FALSE(t_urls
[default_index
]->new_tab_url
.empty());
279 EXPECT_FALSE(t_urls
[default_index
]->contextual_search_url
.empty());
280 EXPECT_FALSE(t_urls
[default_index
]->image_url_post_params
.empty());
281 // Expect at least 2 alternate_urls.
282 // This caught a bug with static initialization of arrays, so leave this in.
283 EXPECT_GT(t_urls
[default_index
]->alternate_urls
.size(), 1u);
284 for (size_t i
= 0; i
< t_urls
[default_index
]->alternate_urls
.size(); ++i
)
285 EXPECT_FALSE(t_urls
[default_index
]->alternate_urls
[i
].empty());
286 EXPECT_EQ(SEARCH_ENGINE_GOOGLE
,
287 TemplateURLPrepopulateData::GetEngineType(
288 TemplateURL(*t_urls
[default_index
]),
290 EXPECT_FALSE(t_urls
[default_index
]->search_terms_replacement_key
.empty());
293 TEST_F(TemplateURLPrepopulateDataTest
, GetEngineTypeBasic
) {
294 EXPECT_EQ(SEARCH_ENGINE_OTHER
, GetEngineType("http://example.com/"));
295 EXPECT_EQ(SEARCH_ENGINE_ASK
, GetEngineType("http://www.ask.com/"));
296 EXPECT_EQ(SEARCH_ENGINE_OTHER
, GetEngineType("http://search.atlas.cz/"));
297 EXPECT_EQ(SEARCH_ENGINE_GOOGLE
, GetEngineType("http://www.google.com/"));
300 TEST_F(TemplateURLPrepopulateDataTest
, GetEngineTypeAdvanced
) {
301 // Google URLs in different forms.
302 const char* kGoogleURLs
[] = {
303 // Original with google:baseURL:
304 "{google:baseURL}search?q={searchTerms}&{google:RLZ}"
305 "{google:originalQueryForSuggestion}{google:searchFieldtrialParameter}"
306 "sourceid=chrome&ie={inputEncoding}",
307 // Custom with google.com and reordered query params:
308 "http://google.com/search?{google:RLZ}{google:originalQueryForSuggestion}"
309 "{google:searchFieldtrialParameter}"
310 "sourceid=chrome&ie={inputEncoding}&q={searchTerms}",
311 // Custom with a country TLD and almost no query params:
312 "http://www.google.ru/search?q={searchTerms}"
314 for (size_t i
= 0; i
< arraysize(kGoogleURLs
); ++i
) {
315 EXPECT_EQ(SEARCH_ENGINE_GOOGLE
, GetEngineType(kGoogleURLs
[i
]));
319 const char* kYahooURLs
[] = {
320 "http://search.yahoo.com/search?"
321 "ei={inputEncoding}&fr=crmas&p={searchTerms}",
322 "http://search.yahoo.com/search?p={searchTerms}",
323 // Aggressively match types by checking just TLD+1.
324 "http://someothersite.yahoo.com/",
326 for (size_t i
= 0; i
< arraysize(kYahooURLs
); ++i
) {
327 EXPECT_EQ(SEARCH_ENGINE_YAHOO
, GetEngineType(kYahooURLs
[i
]));
330 // URLs for engines not present in country-specific lists.
331 EXPECT_EQ(SEARCH_ENGINE_NIGMA
,
332 GetEngineType("http://nigma.ru/?s={searchTerms}&arg1=value1"));
333 // Also test matching against alternate URLs (and TLD+1 matching).
334 EXPECT_EQ(SEARCH_ENGINE_SOFTONIC
,
335 GetEngineType("http://test.softonic.com.br/?{searchTerms}"));
337 // Search URL for which no prepopulated search provider exists.
338 EXPECT_EQ(SEARCH_ENGINE_OTHER
,
339 GetEngineType("http://example.net/search?q={searchTerms}"));
340 EXPECT_EQ(SEARCH_ENGINE_OTHER
, GetEngineType("invalid:search:url"));
342 // URL that doesn't look Google-related, but matches a Google base URL
343 // specified on the command line.
344 const std::string
foo_url("http://www.foo.com/search?q={searchTerms}");
345 EXPECT_EQ(SEARCH_ENGINE_OTHER
, GetEngineType(foo_url
));
346 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
347 switches::kGoogleBaseURL
, "http://www.foo.com/");
348 EXPECT_EQ(SEARCH_ENGINE_GOOGLE
, GetEngineType(foo_url
));