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