Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(921)

Side by Side Diff: chrome/browser/search_engines/template_url_prepopulate_data_unittest.cc

Issue 382663004: Componentize tests under search_engines (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Include search_engines even if os=="ios" Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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 "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 "grit/generated_resources.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "ui/base/l10n/l10n_util.h"
20
21 using base::ASCIIToUTF16;
22
23 namespace {
24
25 SearchEngineType GetEngineType(const std::string& url) {
26 TemplateURLData data;
27 data.SetURL(url);
28 return TemplateURLPrepopulateData::GetEngineType(TemplateURL(data),
29 SearchTermsData());
30 }
31
32 std::string GetHostFromTemplateURLData(const TemplateURLData& data) {
33 return TemplateURL(data).url_ref().GetHost(SearchTermsData());
34 }
35
36 } // namespace
37
38 class TemplateURLPrepopulateDataTest : public testing::Test {
39 public:
40 virtual void SetUp() OVERRIDE {
41 TemplateURLPrepopulateData::RegisterProfilePrefs(prefs_.registry());
42 }
43
44 protected:
45 user_prefs::TestingPrefServiceSyncable prefs_;
46 };
47
48 // Verifies the set of prepopulate data doesn't contain entries with duplicate
49 // ids.
50 TEST_F(TemplateURLPrepopulateDataTest, UniqueIDs) {
51 const int kCountryIds[] = {
52 'A'<<8|'D', 'A'<<8|'E', 'A'<<8|'F', 'A'<<8|'G', 'A'<<8|'I',
53 'A'<<8|'L', 'A'<<8|'M', 'A'<<8|'N', 'A'<<8|'O', 'A'<<8|'Q',
54 'A'<<8|'R', 'A'<<8|'S', 'A'<<8|'T', 'A'<<8|'U', 'A'<<8|'W',
55 'A'<<8|'X', 'A'<<8|'Z', 'B'<<8|'A', 'B'<<8|'B', 'B'<<8|'D',
56 'B'<<8|'E', 'B'<<8|'F', 'B'<<8|'G', 'B'<<8|'H', 'B'<<8|'I',
57 'B'<<8|'J', 'B'<<8|'M', 'B'<<8|'N', 'B'<<8|'O', 'B'<<8|'R',
58 'B'<<8|'S', 'B'<<8|'T', 'B'<<8|'V', 'B'<<8|'W', 'B'<<8|'Y',
59 'B'<<8|'Z', 'C'<<8|'A', 'C'<<8|'C', 'C'<<8|'D', 'C'<<8|'F',
60 'C'<<8|'G', 'C'<<8|'H', 'C'<<8|'I', 'C'<<8|'K', 'C'<<8|'L',
61 'C'<<8|'M', 'C'<<8|'N', 'C'<<8|'O', 'C'<<8|'R', 'C'<<8|'U',
62 'C'<<8|'V', 'C'<<8|'X', 'C'<<8|'Y', 'C'<<8|'Z', 'D'<<8|'E',
63 'D'<<8|'J', 'D'<<8|'K', 'D'<<8|'M', 'D'<<8|'O', 'D'<<8|'Z',
64 'E'<<8|'C', 'E'<<8|'E', 'E'<<8|'G', 'E'<<8|'R', 'E'<<8|'S',
65 'E'<<8|'T', 'F'<<8|'I', 'F'<<8|'J', 'F'<<8|'K', 'F'<<8|'M',
66 'F'<<8|'O', 'F'<<8|'R', 'G'<<8|'A', 'G'<<8|'B', 'G'<<8|'D',
67 'G'<<8|'E', 'G'<<8|'F', 'G'<<8|'G', 'G'<<8|'H', 'G'<<8|'I',
68 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P'<<8|'A',
86 'P'<<8|'E', 'P'<<8|'F', 'P'<<8|'G', 'P'<<8|'H', 'P'<<8|'K',
87 'P'<<8|'L', 'P'<<8|'M', 'P'<<8|'N', 'P'<<8|'R', 'P'<<8|'S',
88 'P'<<8|'T', 'P'<<8|'W', 'P'<<8|'Y', 'Q'<<8|'A', 'R'<<8|'E',
89 'R'<<8|'O', 'R'<<8|'S', 'R'<<8|'U', 'R'<<8|'W', 'S'<<8|'A',
90 'S'<<8|'B', 'S'<<8|'C', 'S'<<8|'D', 'S'<<8|'E', 'S'<<8|'G',
91 'S'<<8|'H', 'S'<<8|'I', 'S'<<8|'J', 'S'<<8|'K', 'S'<<8|'L',
92 'S'<<8|'M', 'S'<<8|'N', 'S'<<8|'O', 'S'<<8|'R', 'S'<<8|'T',
93 'S'<<8|'V', 'S'<<8|'Y', 'S'<<8|'Z', 'T'<<8|'C', 'T'<<8|'D',
94 'T'<<8|'F', 'T'<<8|'G', 'T'<<8|'H', 'T'<<8|'J', 'T'<<8|'K',
95 'T'<<8|'L', 'T'<<8|'M', 'T'<<8|'N', 'T'<<8|'O', 'T'<<8|'R',
96 'T'<<8|'T', 'T'<<8|'V', 'T'<<8|'W', 'T'<<8|'Z', 'U'<<8|'A',
97 'U'<<8|'G', 'U'<<8|'M', 'U'<<8|'S', 'U'<<8|'Y', 'U'<<8|'Z',
98 'V'<<8|'A', 'V'<<8|'C', 'V'<<8|'E', 'V'<<8|'G', 'V'<<8|'I',
99 'V'<<8|'N', 'V'<<8|'U', 'W'<<8|'F', 'W'<<8|'S', 'Y'<<8|'E',
100 'Y'<<8|'T', 'Z'<<8|'A', 'Z'<<8|'M', 'Z'<<8|'W', -1 };
101
102 for (size_t i = 0; i < arraysize(kCountryIds); ++i) {
103 prefs_.SetInteger(prefs::kCountryIDAtInstall, kCountryIds[i]);
104 size_t default_index;
105 ScopedVector<TemplateURLData> urls =
106 TemplateURLPrepopulateData::GetPrepopulatedEngines(&prefs_,
107 &default_index);
108 std::set<int> unique_ids;
109 for (size_t turl_i = 0; turl_i < urls.size(); ++turl_i) {
110 ASSERT_TRUE(unique_ids.find(urls[turl_i]->prepopulate_id) ==
111 unique_ids.end());
112 unique_ids.insert(urls[turl_i]->prepopulate_id);
113 }
114 }
115 }
116
117 // Verifies that default search providers from the preferences file
118 // override the built-in ones.
119 TEST_F(TemplateURLPrepopulateDataTest, ProvidersFromPrefs) {
120 prefs_.SetUserPref(prefs::kSearchProviderOverridesVersion,
121 base::Value::CreateIntegerValue(1));
122 base::ListValue* overrides = new base::ListValue;
123 scoped_ptr<base::DictionaryValue> entry(new base::DictionaryValue);
124 // Set only the minimal required settings for a search provider configuration.
125 entry->SetString("name", "foo");
126 entry->SetString("keyword", "fook");
127 entry->SetString("search_url", "http://foo.com/s?q={searchTerms}");
128 entry->SetString("favicon_url", "http://foi.com/favicon.ico");
129 entry->SetString("encoding", "UTF-8");
130 entry->SetInteger("id", 1001);
131 overrides->Append(entry->DeepCopy());
132 prefs_.SetUserPref(prefs::kSearchProviderOverrides, overrides);
133
134 int version = TemplateURLPrepopulateData::GetDataVersion(&prefs_);
135 EXPECT_EQ(1, version);
136
137 size_t default_index;
138 ScopedVector<TemplateURLData> t_urls =
139 TemplateURLPrepopulateData::GetPrepopulatedEngines(&prefs_,
140 &default_index);
141
142 ASSERT_EQ(1u, t_urls.size());
143 EXPECT_EQ(ASCIIToUTF16("foo"), t_urls[0]->short_name);
144 EXPECT_EQ(ASCIIToUTF16("fook"), t_urls[0]->keyword());
145 EXPECT_EQ("foo.com", GetHostFromTemplateURLData(*t_urls[0]));
146 EXPECT_EQ("foi.com", t_urls[0]->favicon_url.host());
147 EXPECT_EQ(1u, t_urls[0]->input_encodings.size());
148 EXPECT_EQ(1001, t_urls[0]->prepopulate_id);
149 EXPECT_TRUE(t_urls[0]->suggestions_url.empty());
150 EXPECT_TRUE(t_urls[0]->instant_url.empty());
151 EXPECT_EQ(0u, t_urls[0]->alternate_urls.size());
152 EXPECT_TRUE(t_urls[0]->search_terms_replacement_key.empty());
153
154 // Test the optional settings too.
155 entry->SetString("suggest_url", "http://foo.com/suggest?q={searchTerms}");
156 entry->SetString("instant_url", "http://foo.com/instant?q={searchTerms}");
157 base::ListValue* alternate_urls = new base::ListValue;
158 alternate_urls->AppendString("http://foo.com/alternate?q={searchTerms}");
159 entry->Set("alternate_urls", alternate_urls);
160 entry->SetString("search_terms_replacement_key", "espv");
161 overrides = new base::ListValue;
162 overrides->Append(entry->DeepCopy());
163 prefs_.SetUserPref(prefs::kSearchProviderOverrides, overrides);
164
165 t_urls = TemplateURLPrepopulateData::GetPrepopulatedEngines(
166 &prefs_, &default_index);
167 ASSERT_EQ(1u, t_urls.size());
168 EXPECT_EQ(ASCIIToUTF16("foo"), t_urls[0]->short_name);
169 EXPECT_EQ(ASCIIToUTF16("fook"), t_urls[0]->keyword());
170 EXPECT_EQ("foo.com", GetHostFromTemplateURLData(*t_urls[0]));
171 EXPECT_EQ("foi.com", t_urls[0]->favicon_url.host());
172 EXPECT_EQ(1u, t_urls[0]->input_encodings.size());
173 EXPECT_EQ(1001, t_urls[0]->prepopulate_id);
174 EXPECT_EQ("http://foo.com/suggest?q={searchTerms}",
175 t_urls[0]->suggestions_url);
176 EXPECT_EQ("http://foo.com/instant?q={searchTerms}",
177 t_urls[0]->instant_url);
178 ASSERT_EQ(1u, t_urls[0]->alternate_urls.size());
179 EXPECT_EQ("http://foo.com/alternate?q={searchTerms}",
180 t_urls[0]->alternate_urls[0]);
181 EXPECT_EQ("espv", t_urls[0]->search_terms_replacement_key);
182
183 // Test that subsequent providers are loaded even if an intermediate
184 // provider has an incomplete configuration.
185 overrides = new base::ListValue;
186 overrides->Append(entry->DeepCopy());
187 entry->SetInteger("id", 1002);
188 entry->SetString("name", "bar");
189 entry->SetString("keyword", "bark");
190 entry->SetString("encoding", std::string());
191 overrides->Append(entry->DeepCopy());
192 entry->SetInteger("id", 1003);
193 entry->SetString("name", "baz");
194 entry->SetString("keyword", "bazk");
195 entry->SetString("encoding", "UTF-8");
196 overrides->Append(entry->DeepCopy());
197 prefs_.SetUserPref(prefs::kSearchProviderOverrides, overrides);
198
199 t_urls =
200 TemplateURLPrepopulateData::GetPrepopulatedEngines(&prefs_,
201 &default_index);
202 EXPECT_EQ(2u, t_urls.size());
203 }
204
205 TEST_F(TemplateURLPrepopulateDataTest, ClearProvidersFromPrefs) {
206 prefs_.SetUserPref(prefs::kSearchProviderOverridesVersion,
207 base::Value::CreateIntegerValue(1));
208 base::ListValue* overrides = new base::ListValue;
209 base::DictionaryValue* entry(new base::DictionaryValue);
210 // Set only the minimal required settings for a search provider configuration.
211 entry->SetString("name", "foo");
212 entry->SetString("keyword", "fook");
213 entry->SetString("search_url", "http://foo.com/s?q={searchTerms}");
214 entry->SetString("favicon_url", "http://foi.com/favicon.ico");
215 entry->SetString("encoding", "UTF-8");
216 entry->SetInteger("id", 1001);
217 overrides->Append(entry);
218 prefs_.SetUserPref(prefs::kSearchProviderOverrides, overrides);
219
220 int version = TemplateURLPrepopulateData::GetDataVersion(&prefs_);
221 EXPECT_EQ(1, version);
222
223 // This call removes the above search engine.
224 TemplateURLPrepopulateData::ClearPrepopulatedEnginesInPrefs(&prefs_);
225
226 version = TemplateURLPrepopulateData::GetDataVersion(&prefs_);
227 EXPECT_EQ(TemplateURLPrepopulateData::kCurrentDataVersion, version);
228
229 size_t default_index;
230 ScopedVector<TemplateURLData> t_urls =
231 TemplateURLPrepopulateData::GetPrepopulatedEngines(&prefs_,
232 &default_index);
233 ASSERT_FALSE(t_urls.empty());
234 for (size_t i = 0; i < t_urls.size(); ++i) {
235 EXPECT_NE(ASCIIToUTF16("foo"), t_urls[i]->short_name);
236 EXPECT_NE(ASCIIToUTF16("fook"), t_urls[i]->keyword());
237 EXPECT_NE("foi.com", t_urls[i]->favicon_url.host());
238 EXPECT_NE("foo.com", GetHostFromTemplateURLData(*t_urls[i]));
239 EXPECT_NE(1001, t_urls[i]->prepopulate_id);
240 }
241 // Ensures the default URL is Google and has the optional fields filled.
242 EXPECT_EQ(ASCIIToUTF16("Google"), t_urls[default_index]->short_name);
243 EXPECT_FALSE(t_urls[default_index]->suggestions_url.empty());
244 EXPECT_FALSE(t_urls[default_index]->instant_url.empty());
245 EXPECT_FALSE(t_urls[default_index]->image_url.empty());
246 EXPECT_FALSE(t_urls[default_index]->new_tab_url.empty());
247 EXPECT_FALSE(t_urls[default_index]->contextual_search_url.empty());
248 EXPECT_FALSE(t_urls[default_index]->image_url_post_params.empty());
249 EXPECT_EQ(SEARCH_ENGINE_GOOGLE,
250 TemplateURLPrepopulateData::GetEngineType(
251 TemplateURL(*t_urls[default_index]),
252 SearchTermsData()));
253 }
254
255 // Verifies that built-in search providers are processed correctly.
256 TEST_F(TemplateURLPrepopulateDataTest, ProvidersFromPrepopulated) {
257 // Use United States.
258 prefs_.SetInteger(prefs::kCountryIDAtInstall, 'U'<<8|'S');
259 size_t default_index;
260 ScopedVector<TemplateURLData> t_urls =
261 TemplateURLPrepopulateData::GetPrepopulatedEngines(&prefs_,
262 &default_index);
263
264 // Ensure all the URLs have the required fields populated.
265 ASSERT_FALSE(t_urls.empty());
266 for (size_t i = 0; i < t_urls.size(); ++i) {
267 ASSERT_FALSE(t_urls[i]->short_name.empty());
268 ASSERT_FALSE(t_urls[i]->keyword().empty());
269 ASSERT_FALSE(t_urls[i]->favicon_url.host().empty());
270 ASSERT_FALSE(GetHostFromTemplateURLData(*t_urls[i]).empty());
271 ASSERT_FALSE(t_urls[i]->input_encodings.empty());
272 EXPECT_GT(t_urls[i]->prepopulate_id, 0);
273 }
274
275 // Ensures the default URL is Google and has the optional fields filled.
276 EXPECT_EQ(ASCIIToUTF16("Google"), t_urls[default_index]->short_name);
277 EXPECT_FALSE(t_urls[default_index]->suggestions_url.empty());
278 EXPECT_FALSE(t_urls[default_index]->instant_url.empty());
279 EXPECT_FALSE(t_urls[default_index]->image_url.empty());
280 EXPECT_FALSE(t_urls[default_index]->new_tab_url.empty());
281 EXPECT_FALSE(t_urls[default_index]->contextual_search_url.empty());
282 EXPECT_FALSE(t_urls[default_index]->image_url_post_params.empty());
283 // Expect at least 2 alternate_urls.
284 // This caught a bug with static initialization of arrays, so leave this in.
285 EXPECT_GT(t_urls[default_index]->alternate_urls.size(), 1u);
286 for (size_t i = 0; i < t_urls[default_index]->alternate_urls.size(); ++i)
287 EXPECT_FALSE(t_urls[default_index]->alternate_urls[i].empty());
288 EXPECT_EQ(SEARCH_ENGINE_GOOGLE,
289 TemplateURLPrepopulateData::GetEngineType(
290 TemplateURL(*t_urls[default_index]),
291 SearchTermsData()));
292 EXPECT_FALSE(t_urls[default_index]->search_terms_replacement_key.empty());
293 }
294
295 TEST_F(TemplateURLPrepopulateDataTest, GetEngineTypeBasic) {
296 EXPECT_EQ(SEARCH_ENGINE_OTHER, GetEngineType("http://example.com/"));
297 EXPECT_EQ(SEARCH_ENGINE_ASK, GetEngineType("http://www.ask.com/"));
298 EXPECT_EQ(SEARCH_ENGINE_OTHER, GetEngineType("http://search.atlas.cz/"));
299 EXPECT_EQ(SEARCH_ENGINE_GOOGLE, GetEngineType("http://www.google.com/"));
300 }
301
302 TEST_F(TemplateURLPrepopulateDataTest, GetEngineTypeAdvanced) {
303 // Google URLs in different forms.
304 const char* kGoogleURLs[] = {
305 // Original with google:baseURL:
306 "{google:baseURL}search?q={searchTerms}&{google:RLZ}"
307 "{google:originalQueryForSuggestion}{google:searchFieldtrialParameter}"
308 "sourceid=chrome&ie={inputEncoding}",
309 // Custom with google.com and reordered query params:
310 "http://google.com/search?{google:RLZ}{google:originalQueryForSuggestion}"
311 "{google:searchFieldtrialParameter}"
312 "sourceid=chrome&ie={inputEncoding}&q={searchTerms}",
313 // Custom with a country TLD and almost no query params:
314 "http://www.google.ru/search?q={searchTerms}"
315 };
316 for (size_t i = 0; i < arraysize(kGoogleURLs); ++i) {
317 EXPECT_EQ(SEARCH_ENGINE_GOOGLE, GetEngineType(kGoogleURLs[i]));
318 }
319
320 // Non-Google URLs.
321 const char* kYahooURLs[] = {
322 "http://search.yahoo.com/search?"
323 "ei={inputEncoding}&fr=crmas&p={searchTerms}",
324 "http://search.yahoo.com/search?p={searchTerms}",
325 // Aggressively match types by checking just TLD+1.
326 "http://someothersite.yahoo.com/",
327 };
328 for (size_t i = 0; i < arraysize(kYahooURLs); ++i) {
329 EXPECT_EQ(SEARCH_ENGINE_YAHOO, GetEngineType(kYahooURLs[i]));
330 }
331
332 // URLs for engines not present in country-specific lists.
333 EXPECT_EQ(SEARCH_ENGINE_NIGMA,
334 GetEngineType("http://nigma.ru/?s={searchTerms}&arg1=value1"));
335 // Also test matching against alternate URLs (and TLD+1 matching).
336 EXPECT_EQ(SEARCH_ENGINE_SOFTONIC,
337 GetEngineType("http://test.softonic.com.br/?{searchTerms}"));
338
339 // Search URL for which no prepopulated search provider exists.
340 EXPECT_EQ(SEARCH_ENGINE_OTHER,
341 GetEngineType("http://example.net/search?q={searchTerms}"));
342 EXPECT_EQ(SEARCH_ENGINE_OTHER, GetEngineType("invalid:search:url"));
343
344 // URL that doesn't look Google-related, but matches a Google base URL
345 // specified on the command line.
346 const std::string foo_url("http://www.foo.com/search?q={searchTerms}");
347 EXPECT_EQ(SEARCH_ENGINE_OTHER, GetEngineType(foo_url));
348 CommandLine::ForCurrentProcess()->AppendSwitchASCII(switches::kGoogleBaseURL,
349 "http://www.foo.com/");
350 EXPECT_EQ(SEARCH_ENGINE_GOOGLE, GetEngineType(foo_url));
351 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698