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 "base/command_line.h" | 5 #include "base/command_line.h" |
6 #include "base/files/scoped_temp_dir.h" | 6 #include "base/files/scoped_temp_dir.h" |
7 #include "base/memory/scoped_vector.h" | 7 #include "base/memory/scoped_vector.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "chrome/browser/search_engines/prepopulated_engines.h" | 9 #include "chrome/browser/search_engines/prepopulated_engines.h" |
10 #include "chrome/browser/search_engines/search_terms_data.h" | 10 #include "chrome/browser/search_engines/search_terms_data.h" |
11 #include "chrome/browser/search_engines/template_url.h" | 11 #include "chrome/browser/search_engines/template_url.h" |
12 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" | 12 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" |
13 #include "chrome/browser/search_engines/template_url_service.h" | 13 #include "chrome/browser/search_engines/template_url_service.h" |
14 #include "chrome/common/chrome_switches.h" | 14 #include "chrome/common/chrome_switches.h" |
15 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
16 #include "chrome/test/base/testing_pref_service_syncable.h" | 16 #include "chrome/test/base/testing_pref_service_syncable.h" |
17 #include "chrome/test/base/testing_profile.h" | 17 #include "chrome/test/base/testing_profile.h" |
18 #include "content/public/test/test_browser_thread_bundle.h" | |
19 #include "grit/generated_resources.h" | 18 #include "grit/generated_resources.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
21 #include "ui/base/l10n/l10n_util.h" | 20 #include "ui/base/l10n/l10n_util.h" |
22 | 21 |
23 namespace { | 22 namespace { |
24 SearchEngineType GetEngineType(const std::string& url) { | 23 SearchEngineType GetEngineType(const std::string& url) { |
25 TemplateURLData data; | 24 TemplateURLData data; |
26 data.SetURL(url); | 25 data.SetURL(url); |
27 return TemplateURLPrepopulateData::GetEngineType(TemplateURL(NULL, data)); | 26 return TemplateURLPrepopulateData::GetEngineType(TemplateURL(NULL, data)); |
28 } | 27 } |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 'S'<<8|'M', 'S'<<8|'N', 'S'<<8|'O', 'S'<<8|'R', 'S'<<8|'T', | 76 'S'<<8|'M', 'S'<<8|'N', 'S'<<8|'O', 'S'<<8|'R', 'S'<<8|'T', |
78 'S'<<8|'V', 'S'<<8|'Y', 'S'<<8|'Z', 'T'<<8|'C', 'T'<<8|'D', | 77 'S'<<8|'V', 'S'<<8|'Y', 'S'<<8|'Z', 'T'<<8|'C', 'T'<<8|'D', |
79 'T'<<8|'F', 'T'<<8|'G', 'T'<<8|'H', 'T'<<8|'J', 'T'<<8|'K', | 78 'T'<<8|'F', 'T'<<8|'G', 'T'<<8|'H', 'T'<<8|'J', 'T'<<8|'K', |
80 'T'<<8|'L', 'T'<<8|'M', 'T'<<8|'N', 'T'<<8|'O', 'T'<<8|'R', | 79 'T'<<8|'L', 'T'<<8|'M', 'T'<<8|'N', 'T'<<8|'O', 'T'<<8|'R', |
81 'T'<<8|'T', 'T'<<8|'V', 'T'<<8|'W', 'T'<<8|'Z', 'U'<<8|'A', | 80 'T'<<8|'T', 'T'<<8|'V', 'T'<<8|'W', 'T'<<8|'Z', 'U'<<8|'A', |
82 'U'<<8|'G', 'U'<<8|'M', 'U'<<8|'S', 'U'<<8|'Y', 'U'<<8|'Z', | 81 'U'<<8|'G', 'U'<<8|'M', 'U'<<8|'S', 'U'<<8|'Y', 'U'<<8|'Z', |
83 'V'<<8|'A', 'V'<<8|'C', 'V'<<8|'E', 'V'<<8|'G', 'V'<<8|'I', | 82 'V'<<8|'A', 'V'<<8|'C', 'V'<<8|'E', 'V'<<8|'G', 'V'<<8|'I', |
84 'V'<<8|'N', 'V'<<8|'U', 'W'<<8|'F', 'W'<<8|'S', 'Y'<<8|'E', | 83 'V'<<8|'N', 'V'<<8|'U', 'W'<<8|'F', 'W'<<8|'S', 'Y'<<8|'E', |
85 'Y'<<8|'T', 'Z'<<8|'A', 'Z'<<8|'M', 'Z'<<8|'W', -1 }; | 84 'Y'<<8|'T', 'Z'<<8|'A', 'Z'<<8|'M', 'Z'<<8|'W', -1 }; |
86 | 85 |
87 content::TestBrowserThreadBundle thread_bundle; | |
88 TestingProfile profile; | 86 TestingProfile profile; |
89 for (size_t i = 0; i < arraysize(kCountryIds); ++i) { | 87 for (size_t i = 0; i < arraysize(kCountryIds); ++i) { |
90 profile.GetPrefs()->SetInteger(prefs::kCountryIDAtInstall, kCountryIds[i]); | 88 profile.GetPrefs()->SetInteger(prefs::kCountryIDAtInstall, kCountryIds[i]); |
91 ScopedVector<TemplateURL> urls; | 89 ScopedVector<TemplateURL> urls; |
92 size_t default_index; | 90 size_t default_index; |
93 TemplateURLPrepopulateData::GetPrepopulatedEngines(&profile, &urls.get(), | 91 TemplateURLPrepopulateData::GetPrepopulatedEngines(&profile, &urls.get(), |
94 &default_index); | 92 &default_index); |
95 std::set<int> unique_ids; | 93 std::set<int> unique_ids; |
96 for (size_t turl_i = 0; turl_i < urls.size(); ++turl_i) { | 94 for (size_t turl_i = 0; turl_i < urls.size(); ++turl_i) { |
97 ASSERT_TRUE(unique_ids.find(urls[turl_i]->prepopulate_id()) == | 95 ASSERT_TRUE(unique_ids.find(urls[turl_i]->prepopulate_id()) == |
98 unique_ids.end()); | 96 unique_ids.end()); |
99 unique_ids.insert(urls[turl_i]->prepopulate_id()); | 97 unique_ids.insert(urls[turl_i]->prepopulate_id()); |
100 } | 98 } |
101 } | 99 } |
102 } | 100 } |
103 | 101 |
104 // Verifies that default search providers from the preferences file | 102 // Verifies that default search providers from the preferences file |
105 // override the built-in ones. | 103 // override the built-in ones. |
106 TEST(TemplateURLPrepopulateDataTest, ProvidersFromPrefs) { | 104 TEST(TemplateURLPrepopulateDataTest, ProvidersFromPrefs) { |
107 content::TestBrowserThreadBundle thread_bundle; | |
108 TestingProfile profile; | 105 TestingProfile profile; |
109 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService(); | 106 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService(); |
110 prefs->SetUserPref(prefs::kSearchProviderOverridesVersion, | 107 prefs->SetUserPref(prefs::kSearchProviderOverridesVersion, |
111 Value::CreateIntegerValue(1)); | 108 Value::CreateIntegerValue(1)); |
112 ListValue* overrides = new ListValue; | 109 ListValue* overrides = new ListValue; |
113 scoped_ptr<DictionaryValue> entry(new DictionaryValue); | 110 scoped_ptr<DictionaryValue> entry(new DictionaryValue); |
114 // Set only the minimal required settings for a search provider configuration. | 111 // Set only the minimal required settings for a search provider configuration. |
115 entry->SetString("name", "foo"); | 112 entry->SetString("name", "foo"); |
116 entry->SetString("keyword", "fook"); | 113 entry->SetString("keyword", "fook"); |
117 entry->SetString("search_url", "http://foo.com/s?q={searchTerms}"); | 114 entry->SetString("search_url", "http://foo.com/s?q={searchTerms}"); |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 overrides->Append(entry->DeepCopy()); | 184 overrides->Append(entry->DeepCopy()); |
188 prefs->SetUserPref(prefs::kSearchProviderOverrides, overrides); | 185 prefs->SetUserPref(prefs::kSearchProviderOverrides, overrides); |
189 | 186 |
190 t_urls.clear(); | 187 t_urls.clear(); |
191 TemplateURLPrepopulateData::GetPrepopulatedEngines(&profile, &t_urls.get(), | 188 TemplateURLPrepopulateData::GetPrepopulatedEngines(&profile, &t_urls.get(), |
192 &default_index); | 189 &default_index); |
193 EXPECT_EQ(2u, t_urls.size()); | 190 EXPECT_EQ(2u, t_urls.size()); |
194 } | 191 } |
195 | 192 |
196 TEST(TemplateURLPrepopulateDataTest, ClearProvidersFromPrefs) { | 193 TEST(TemplateURLPrepopulateDataTest, ClearProvidersFromPrefs) { |
197 content::TestBrowserThreadBundle thread_bundle; | |
198 TestingProfile profile; | 194 TestingProfile profile; |
199 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService(); | 195 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService(); |
200 prefs->SetUserPref(prefs::kSearchProviderOverridesVersion, | 196 prefs->SetUserPref(prefs::kSearchProviderOverridesVersion, |
201 Value::CreateIntegerValue(1)); | 197 Value::CreateIntegerValue(1)); |
202 ListValue* overrides = new ListValue; | 198 ListValue* overrides = new ListValue; |
203 DictionaryValue* entry(new DictionaryValue); | 199 DictionaryValue* entry(new DictionaryValue); |
204 // Set only the minimal required settings for a search provider configuration. | 200 // Set only the minimal required settings for a search provider configuration. |
205 entry->SetString("name", "foo"); | 201 entry->SetString("name", "foo"); |
206 entry->SetString("keyword", "fook"); | 202 entry->SetString("keyword", "fook"); |
207 entry->SetString("search_url", "http://foo.com/s?q={searchTerms}"); | 203 entry->SetString("search_url", "http://foo.com/s?q={searchTerms}"); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 EXPECT_FALSE(t_urls[default_index]->image_url_post_params().empty()); | 237 EXPECT_FALSE(t_urls[default_index]->image_url_post_params().empty()); |
242 EXPECT_EQ(SEARCH_ENGINE_GOOGLE, | 238 EXPECT_EQ(SEARCH_ENGINE_GOOGLE, |
243 TemplateURLPrepopulateData::GetEngineType(*t_urls[default_index])); | 239 TemplateURLPrepopulateData::GetEngineType(*t_urls[default_index])); |
244 } | 240 } |
245 | 241 |
246 // Verifies that built-in search providers are processed correctly. | 242 // Verifies that built-in search providers are processed correctly. |
247 TEST(TemplateURLPrepopulateDataTest, ProvidersFromPrepopulated) { | 243 TEST(TemplateURLPrepopulateDataTest, ProvidersFromPrepopulated) { |
248 // Use United States. | 244 // Use United States. |
249 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 245 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
250 switches::kCountry, "US"); | 246 switches::kCountry, "US"); |
251 content::TestBrowserThreadBundle thread_bundle; | |
252 TestingProfile profile; | 247 TestingProfile profile; |
253 ScopedVector<TemplateURL> t_urls; | 248 ScopedVector<TemplateURL> t_urls; |
254 size_t default_index; | 249 size_t default_index; |
255 TemplateURLPrepopulateData::GetPrepopulatedEngines(&profile, &t_urls.get(), | 250 TemplateURLPrepopulateData::GetPrepopulatedEngines(&profile, &t_urls.get(), |
256 &default_index); | 251 &default_index); |
257 | 252 |
258 // Ensure all the URLs have the required fields populated. | 253 // Ensure all the URLs have the required fields populated. |
259 ASSERT_FALSE(t_urls.empty()); | 254 ASSERT_FALSE(t_urls.empty()); |
260 for (size_t i = 0; i < t_urls.size(); ++i) { | 255 for (size_t i = 0; i < t_urls.size(); ++i) { |
261 ASSERT_FALSE(t_urls[i]->short_name().empty()); | 256 ASSERT_FALSE(t_urls[i]->short_name().empty()); |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
367 | 362 |
368 TEST(TemplateURLPrepopulateDataTest, GetLogoURLInvalid) { | 363 TEST(TemplateURLPrepopulateDataTest, GetLogoURLInvalid) { |
369 TemplateURLData data; | 364 TemplateURLData data; |
370 data.SetURL("http://invalid:search:url/"); | 365 data.SetURL("http://invalid:search:url/"); |
371 TemplateURL turl(NULL, data); | 366 TemplateURL turl(NULL, data); |
372 GURL logo_url = TemplateURLPrepopulateData::GetLogoURL( | 367 GURL logo_url = TemplateURLPrepopulateData::GetLogoURL( |
373 turl, TemplateURLPrepopulateData::LOGO_100_PERCENT); | 368 turl, TemplateURLPrepopulateData::LOGO_100_PERCENT); |
374 | 369 |
375 EXPECT_TRUE(logo_url.is_empty()); | 370 EXPECT_TRUE(logo_url.is_empty()); |
376 } | 371 } |
OLD | NEW |