| 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 typedef testing::Test TemplateURLPrepopulateDataTest; | 23 typedef testing::Test TemplateURLPrepopulateDataTest; |
| 23 | 24 |
| 24 const int kCountryIds[] = { | 25 const int kCountryIds[] = { |
| 25 'A'<<8|'D', 'A'<<8|'E', 'A'<<8|'F', 'A'<<8|'G', 'A'<<8|'I', | 26 'A'<<8|'D', 'A'<<8|'E', 'A'<<8|'F', 'A'<<8|'G', 'A'<<8|'I', |
| 26 'A'<<8|'L', 'A'<<8|'M', 'A'<<8|'N', 'A'<<8|'O', 'A'<<8|'Q', | 27 'A'<<8|'L', 'A'<<8|'M', 'A'<<8|'N', 'A'<<8|'O', 'A'<<8|'Q', |
| 27 'A'<<8|'R', 'A'<<8|'S', 'A'<<8|'T', 'A'<<8|'U', 'A'<<8|'W', | 28 'A'<<8|'R', 'A'<<8|'S', 'A'<<8|'T', 'A'<<8|'U', 'A'<<8|'W', |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 'T'<<8|'L', 'T'<<8|'M', 'T'<<8|'N', 'T'<<8|'O', 'T'<<8|'R', | 69 'T'<<8|'L', 'T'<<8|'M', 'T'<<8|'N', 'T'<<8|'O', 'T'<<8|'R', |
| 69 'T'<<8|'T', 'T'<<8|'V', 'T'<<8|'W', 'T'<<8|'Z', 'U'<<8|'A', | 70 'T'<<8|'T', 'T'<<8|'V', 'T'<<8|'W', 'T'<<8|'Z', 'U'<<8|'A', |
| 70 'U'<<8|'G', 'U'<<8|'M', 'U'<<8|'S', 'U'<<8|'Y', 'U'<<8|'Z', | 71 'U'<<8|'G', 'U'<<8|'M', 'U'<<8|'S', 'U'<<8|'Y', 'U'<<8|'Z', |
| 71 'V'<<8|'A', 'V'<<8|'C', 'V'<<8|'E', 'V'<<8|'G', 'V'<<8|'I', | 72 'V'<<8|'A', 'V'<<8|'C', 'V'<<8|'E', 'V'<<8|'G', 'V'<<8|'I', |
| 72 'V'<<8|'N', 'V'<<8|'U', 'W'<<8|'F', 'W'<<8|'S', 'Y'<<8|'E', | 73 'V'<<8|'N', 'V'<<8|'U', 'W'<<8|'F', 'W'<<8|'S', 'Y'<<8|'E', |
| 73 'Y'<<8|'T', 'Z'<<8|'A', 'Z'<<8|'M', 'Z'<<8|'W', -1 }; | 74 'Y'<<8|'T', 'Z'<<8|'A', 'Z'<<8|'M', 'Z'<<8|'W', -1 }; |
| 74 | 75 |
| 75 // Verifies the set of prepopulate data doesn't contain entries with duplicate | 76 // Verifies the set of prepopulate data doesn't contain entries with duplicate |
| 76 // ids. | 77 // ids. |
| 77 TEST(TemplateURLPrepopulateDataTest, UniqueIDs) { | 78 TEST(TemplateURLPrepopulateDataTest, UniqueIDs) { |
| 79 content::TestBrowserThreadBundle thread_bundle; |
| 78 TestingProfile profile; | 80 TestingProfile profile; |
| 79 for (size_t i = 0; i < arraysize(kCountryIds); ++i) { | 81 for (size_t i = 0; i < arraysize(kCountryIds); ++i) { |
| 80 profile.GetPrefs()->SetInteger(prefs::kCountryIDAtInstall, kCountryIds[i]); | 82 profile.GetPrefs()->SetInteger(prefs::kCountryIDAtInstall, kCountryIds[i]); |
| 81 ScopedVector<TemplateURL> urls; | 83 ScopedVector<TemplateURL> urls; |
| 82 size_t default_index; | 84 size_t default_index; |
| 83 TemplateURLPrepopulateData::GetPrepopulatedEngines(&profile, &urls.get(), | 85 TemplateURLPrepopulateData::GetPrepopulatedEngines(&profile, &urls.get(), |
| 84 &default_index); | 86 &default_index); |
| 85 std::set<int> unique_ids; | 87 std::set<int> unique_ids; |
| 86 for (size_t turl_i = 0; turl_i < urls.size(); ++turl_i) { | 88 for (size_t turl_i = 0; turl_i < urls.size(); ++turl_i) { |
| 87 ASSERT_TRUE(unique_ids.find(urls[turl_i]->prepopulate_id()) == | 89 ASSERT_TRUE(unique_ids.find(urls[turl_i]->prepopulate_id()) == |
| 88 unique_ids.end()); | 90 unique_ids.end()); |
| 89 unique_ids.insert(urls[turl_i]->prepopulate_id()); | 91 unique_ids.insert(urls[turl_i]->prepopulate_id()); |
| 90 } | 92 } |
| 91 } | 93 } |
| 92 } | 94 } |
| 93 | 95 |
| 94 // Verifies that default search providers from the preferences file | 96 // Verifies that default search providers from the preferences file |
| 95 // override the built-in ones. | 97 // override the built-in ones. |
| 96 TEST(TemplateURLPrepopulateDataTest, ProvidersFromPrefs) { | 98 TEST(TemplateURLPrepopulateDataTest, ProvidersFromPrefs) { |
| 99 content::TestBrowserThreadBundle thread_bundle; |
| 97 TestingProfile profile; | 100 TestingProfile profile; |
| 98 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService(); | 101 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService(); |
| 99 prefs->SetUserPref(prefs::kSearchProviderOverridesVersion, | 102 prefs->SetUserPref(prefs::kSearchProviderOverridesVersion, |
| 100 Value::CreateIntegerValue(1)); | 103 Value::CreateIntegerValue(1)); |
| 101 ListValue* overrides = new ListValue; | 104 ListValue* overrides = new ListValue; |
| 102 scoped_ptr<DictionaryValue> entry(new DictionaryValue); | 105 scoped_ptr<DictionaryValue> entry(new DictionaryValue); |
| 103 // Set only the minimal required settings for a search provider configuration. | 106 // Set only the minimal required settings for a search provider configuration. |
| 104 entry->SetString("name", "foo"); | 107 entry->SetString("name", "foo"); |
| 105 entry->SetString("keyword", "fook"); | 108 entry->SetString("keyword", "fook"); |
| 106 entry->SetString("search_url", "http://foo.com/s?q={searchTerms}"); | 109 entry->SetString("search_url", "http://foo.com/s?q={searchTerms}"); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 overrides->Append(entry->DeepCopy()); | 179 overrides->Append(entry->DeepCopy()); |
| 177 prefs->SetUserPref(prefs::kSearchProviderOverrides, overrides); | 180 prefs->SetUserPref(prefs::kSearchProviderOverrides, overrides); |
| 178 | 181 |
| 179 t_urls.clear(); | 182 t_urls.clear(); |
| 180 TemplateURLPrepopulateData::GetPrepopulatedEngines(&profile, &t_urls.get(), | 183 TemplateURLPrepopulateData::GetPrepopulatedEngines(&profile, &t_urls.get(), |
| 181 &default_index); | 184 &default_index); |
| 182 EXPECT_EQ(2u, t_urls.size()); | 185 EXPECT_EQ(2u, t_urls.size()); |
| 183 } | 186 } |
| 184 | 187 |
| 185 TEST(TemplateURLPrepopulateDataTest, ClearProvidersFromPrefs) { | 188 TEST(TemplateURLPrepopulateDataTest, ClearProvidersFromPrefs) { |
| 189 content::TestBrowserThreadBundle thread_bundle; |
| 186 TestingProfile profile; | 190 TestingProfile profile; |
| 187 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService(); | 191 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService(); |
| 188 prefs->SetUserPref(prefs::kSearchProviderOverridesVersion, | 192 prefs->SetUserPref(prefs::kSearchProviderOverridesVersion, |
| 189 Value::CreateIntegerValue(1)); | 193 Value::CreateIntegerValue(1)); |
| 190 ListValue* overrides = new ListValue; | 194 ListValue* overrides = new ListValue; |
| 191 DictionaryValue* entry(new DictionaryValue); | 195 DictionaryValue* entry(new DictionaryValue); |
| 192 // Set only the minimal required settings for a search provider configuration. | 196 // Set only the minimal required settings for a search provider configuration. |
| 193 entry->SetString("name", "foo"); | 197 entry->SetString("name", "foo"); |
| 194 entry->SetString("keyword", "fook"); | 198 entry->SetString("keyword", "fook"); |
| 195 entry->SetString("search_url", "http://foo.com/s?q={searchTerms}"); | 199 entry->SetString("search_url", "http://foo.com/s?q={searchTerms}"); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 EXPECT_FALSE(t_urls[default_index]->image_url_post_params().empty()); | 232 EXPECT_FALSE(t_urls[default_index]->image_url_post_params().empty()); |
| 229 EXPECT_EQ(SEARCH_ENGINE_GOOGLE, | 233 EXPECT_EQ(SEARCH_ENGINE_GOOGLE, |
| 230 TemplateURLPrepopulateData::GetEngineType(t_urls[default_index]->url())); | 234 TemplateURLPrepopulateData::GetEngineType(t_urls[default_index]->url())); |
| 231 } | 235 } |
| 232 | 236 |
| 233 // Verifies that built-in search providers are processed correctly. | 237 // Verifies that built-in search providers are processed correctly. |
| 234 TEST(TemplateURLPrepopulateDataTest, ProvidersFromPrepopulated) { | 238 TEST(TemplateURLPrepopulateDataTest, ProvidersFromPrepopulated) { |
| 235 // Use United States. | 239 // Use United States. |
| 236 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 240 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 237 switches::kCountry, "US"); | 241 switches::kCountry, "US"); |
| 242 content::TestBrowserThreadBundle thread_bundle; |
| 238 TestingProfile profile; | 243 TestingProfile profile; |
| 239 ScopedVector<TemplateURL> t_urls; | 244 ScopedVector<TemplateURL> t_urls; |
| 240 size_t default_index; | 245 size_t default_index; |
| 241 TemplateURLPrepopulateData::GetPrepopulatedEngines(&profile, &t_urls.get(), | 246 TemplateURLPrepopulateData::GetPrepopulatedEngines(&profile, &t_urls.get(), |
| 242 &default_index); | 247 &default_index); |
| 243 | 248 |
| 244 // Ensure all the URLs have the required fields populated. | 249 // Ensure all the URLs have the required fields populated. |
| 245 ASSERT_FALSE(t_urls.empty()); | 250 ASSERT_FALSE(t_urls.empty()); |
| 246 for (size_t i = 0; i < t_urls.size(); ++i) { | 251 for (size_t i = 0; i < t_urls.size(); ++i) { |
| 247 ASSERT_FALSE(t_urls[i]->short_name().empty()); | 252 ASSERT_FALSE(t_urls[i]->short_name().empty()); |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 355 | 360 |
| 356 TEST(TemplateURLPrepopulateDataTest, GetLogoURLInvalid) { | 361 TEST(TemplateURLPrepopulateDataTest, GetLogoURLInvalid) { |
| 357 TemplateURLData data; | 362 TemplateURLData data; |
| 358 data.SetURL("http://invalid:search:url/"); | 363 data.SetURL("http://invalid:search:url/"); |
| 359 TemplateURL turl(NULL, data); | 364 TemplateURL turl(NULL, data); |
| 360 GURL logo_url = TemplateURLPrepopulateData::GetLogoURL( | 365 GURL logo_url = TemplateURLPrepopulateData::GetLogoURL( |
| 361 turl, TemplateURLPrepopulateData::LOGO_100_PERCENT); | 366 turl, TemplateURLPrepopulateData::LOGO_100_PERCENT); |
| 362 | 367 |
| 363 EXPECT_TRUE(logo_url.is_empty()); | 368 EXPECT_TRUE(logo_url.is_empty()); |
| 364 } | 369 } |
| OLD | NEW |