| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/search_engines/default_search_pref_migration.h" | 5 #include "chrome/browser/search_engines/default_search_pref_migration.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/files/scoped_temp_dir.h" |
| 11 #include "base/logging.h" |
| 10 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/prefs/pref_service.h" |
| 14 #include "base/run_loop.h" |
| 11 #include "base/strings/string16.h" | 15 #include "base/strings/string16.h" |
| 16 #include "base/strings/string_number_conversions.h" |
| 17 #include "base/strings/string_util.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
| 13 #include "chrome/browser/search_engines/template_url.h" | 19 #include "chrome/browser/search_engines/template_url.h" |
| 14 #include "chrome/browser/search_engines/template_url_service.h" | 20 #include "chrome/browser/search_engines/template_url_service.h" |
| 15 #include "chrome/browser/search_engines/template_url_service_test_util.h" | 21 #include "chrome/common/pref_names.h" |
| 16 #include "chrome/test/base/testing_pref_service_syncable.h" | 22 #include "chrome/test/base/testing_pref_service_syncable.h" |
| 17 #include "chrome/test/base/testing_profile.h" | 23 #include "chrome/test/base/testing_profile.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 25 #include "url/gurl.h" |
| 19 | 26 |
| 20 class DefaultSearchPrefMigrationTest : public testing::Test { | 27 class DefaultSearchPrefMigrationTest : public testing::Test { |
| 21 public: | 28 public: |
| 22 DefaultSearchPrefMigrationTest(); | 29 DefaultSearchPrefMigrationTest(); |
| 23 | 30 |
| 24 // testing::Test: | 31 // testing::Test: |
| 25 virtual void SetUp() OVERRIDE; | 32 virtual void SetUp() OVERRIDE; |
| 26 virtual void TearDown() OVERRIDE; | 33 virtual void TearDown() OVERRIDE; |
| 27 | 34 |
| 35 void SaveDefaultSearchProviderToLegacyPrefs(const TemplateURL* t_url); |
| 36 |
| 28 scoped_ptr<TemplateURL> CreateKeyword(const std::string& short_name, | 37 scoped_ptr<TemplateURL> CreateKeyword(const std::string& short_name, |
| 29 const std::string& keyword, | 38 const std::string& keyword, |
| 30 const std::string& url); | 39 const std::string& url); |
| 31 | 40 |
| 32 TemplateURLServiceTestUtil* test_util() { return &test_util_; } | 41 const TemplateURLData* GetModernDefaultSearchEngine( |
| 42 DefaultSearchManager::Source* source); |
| 43 |
| 44 void SetModernUserSelectedDefaultSearchEngine(const TemplateURLData& data); |
| 45 |
| 46 TestingProfile* profile() { return profile_.get(); } |
| 33 | 47 |
| 34 private: | 48 private: |
| 35 TemplateURLServiceTestUtil test_util_; | 49 base::ScopedTempDir temp_dir_; |
| 50 scoped_ptr<TestingProfile> profile_; |
| 36 | 51 |
| 37 DISALLOW_COPY_AND_ASSIGN(DefaultSearchPrefMigrationTest); | 52 DISALLOW_COPY_AND_ASSIGN(DefaultSearchPrefMigrationTest); |
| 38 }; | 53 }; |
| 39 | 54 |
| 55 |
| 56 |
| 40 DefaultSearchPrefMigrationTest::DefaultSearchPrefMigrationTest() { | 57 DefaultSearchPrefMigrationTest::DefaultSearchPrefMigrationTest() { |
| 41 } | 58 } |
| 42 | 59 |
| 43 void DefaultSearchPrefMigrationTest::SetUp() { | 60 void DefaultSearchPrefMigrationTest::SetUp() { |
| 44 test_util_.SetUp(); | 61 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 62 profile_.reset(new TestingProfile(temp_dir_.path())); |
| 45 } | 63 } |
| 46 | 64 |
| 47 void DefaultSearchPrefMigrationTest::TearDown() { | 65 void DefaultSearchPrefMigrationTest::TearDown() { |
| 48 test_util_.TearDown(); | 66 profile_.reset(); |
| 67 |
| 68 // Flush the message loop to make application verifiers happy. |
| 69 base::RunLoop().RunUntilIdle(); |
| 70 } |
| 71 |
| 72 void DefaultSearchPrefMigrationTest::SaveDefaultSearchProviderToLegacyPrefs( |
| 73 const TemplateURL* t_url) { |
| 74 PrefService* prefs = profile()->GetPrefs(); |
| 75 |
| 76 bool enabled = false; |
| 77 std::string search_url; |
| 78 std::string suggest_url; |
| 79 std::string instant_url; |
| 80 std::string image_url; |
| 81 std::string new_tab_url; |
| 82 std::string search_url_post_params; |
| 83 std::string suggest_url_post_params; |
| 84 std::string instant_url_post_params; |
| 85 std::string image_url_post_params; |
| 86 std::string icon_url; |
| 87 std::string encodings; |
| 88 std::string short_name; |
| 89 std::string keyword; |
| 90 std::string id_string; |
| 91 std::string prepopulate_id; |
| 92 base::ListValue alternate_urls; |
| 93 std::string search_terms_replacement_key; |
| 94 if (t_url) { |
| 95 DCHECK_EQ(TemplateURL::NORMAL, t_url->GetType()); |
| 96 enabled = true; |
| 97 search_url = t_url->url(); |
| 98 suggest_url = t_url->suggestions_url(); |
| 99 instant_url = t_url->instant_url(); |
| 100 image_url = t_url->image_url(); |
| 101 new_tab_url = t_url->new_tab_url(); |
| 102 search_url_post_params = t_url->search_url_post_params(); |
| 103 suggest_url_post_params = t_url->suggestions_url_post_params(); |
| 104 instant_url_post_params = t_url->instant_url_post_params(); |
| 105 image_url_post_params = t_url->image_url_post_params(); |
| 106 GURL icon_gurl = t_url->favicon_url(); |
| 107 if (!icon_gurl.is_empty()) |
| 108 icon_url = icon_gurl.spec(); |
| 109 encodings = JoinString(t_url->input_encodings(), ';'); |
| 110 short_name = base::UTF16ToUTF8(t_url->short_name()); |
| 111 keyword = base::UTF16ToUTF8(t_url->keyword()); |
| 112 id_string = base::Int64ToString(t_url->id()); |
| 113 prepopulate_id = base::Int64ToString(t_url->prepopulate_id()); |
| 114 for (size_t i = 0; i < t_url->alternate_urls().size(); ++i) |
| 115 alternate_urls.AppendString(t_url->alternate_urls()[i]); |
| 116 search_terms_replacement_key = t_url->search_terms_replacement_key(); |
| 117 } |
| 118 prefs->SetBoolean(prefs::kDefaultSearchProviderEnabled, enabled); |
| 119 prefs->SetString(prefs::kDefaultSearchProviderSearchURL, search_url); |
| 120 prefs->SetString(prefs::kDefaultSearchProviderSuggestURL, suggest_url); |
| 121 prefs->SetString(prefs::kDefaultSearchProviderInstantURL, instant_url); |
| 122 prefs->SetString(prefs::kDefaultSearchProviderImageURL, image_url); |
| 123 prefs->SetString(prefs::kDefaultSearchProviderNewTabURL, new_tab_url); |
| 124 prefs->SetString(prefs::kDefaultSearchProviderSearchURLPostParams, |
| 125 search_url_post_params); |
| 126 prefs->SetString(prefs::kDefaultSearchProviderSuggestURLPostParams, |
| 127 suggest_url_post_params); |
| 128 prefs->SetString(prefs::kDefaultSearchProviderInstantURLPostParams, |
| 129 instant_url_post_params); |
| 130 prefs->SetString(prefs::kDefaultSearchProviderImageURLPostParams, |
| 131 image_url_post_params); |
| 132 prefs->SetString(prefs::kDefaultSearchProviderIconURL, icon_url); |
| 133 prefs->SetString(prefs::kDefaultSearchProviderEncodings, encodings); |
| 134 prefs->SetString(prefs::kDefaultSearchProviderName, short_name); |
| 135 prefs->SetString(prefs::kDefaultSearchProviderKeyword, keyword); |
| 136 prefs->SetString(prefs::kDefaultSearchProviderID, id_string); |
| 137 prefs->SetString(prefs::kDefaultSearchProviderPrepopulateID, prepopulate_id); |
| 138 prefs->Set(prefs::kDefaultSearchProviderAlternateURLs, alternate_urls); |
| 139 prefs->SetString(prefs::kDefaultSearchProviderSearchTermsReplacementKey, |
| 140 search_terms_replacement_key); |
| 49 } | 141 } |
| 50 | 142 |
| 51 scoped_ptr<TemplateURL> DefaultSearchPrefMigrationTest::CreateKeyword( | 143 scoped_ptr<TemplateURL> DefaultSearchPrefMigrationTest::CreateKeyword( |
| 52 const std::string& short_name, | 144 const std::string& short_name, |
| 53 const std::string& keyword, | 145 const std::string& keyword, |
| 54 const std::string& url) { | 146 const std::string& url) { |
| 55 TemplateURLData data; | 147 TemplateURLData data; |
| 56 data.short_name = base::ASCIIToUTF16(short_name); | 148 data.short_name = base::ASCIIToUTF16(short_name); |
| 57 data.SetKeyword(base::ASCIIToUTF16(keyword)); | 149 data.SetKeyword(base::ASCIIToUTF16(keyword)); |
| 58 data.SetURL(url); | 150 data.SetURL(url); |
| 59 scoped_ptr<TemplateURL> t_url(new TemplateURL(test_util_.profile(), data)); | 151 scoped_ptr<TemplateURL> t_url(new TemplateURL(profile(), data)); |
| 60 return t_url.Pass(); | 152 return t_url.Pass(); |
| 61 } | 153 } |
| 62 | 154 |
| 155 void DefaultSearchPrefMigrationTest::SetModernUserSelectedDefaultSearchEngine( |
| 156 const TemplateURLData& data) { |
| 157 DefaultSearchManager(profile()->GetPrefs(), |
| 158 DefaultSearchManager::ObserverCallback()) |
| 159 .SetUserSelectedDefaultSearchEngine(data); |
| 160 } |
| 161 |
| 162 const TemplateURLData* |
| 163 DefaultSearchPrefMigrationTest::GetModernDefaultSearchEngine( |
| 164 DefaultSearchManager::Source* source) { |
| 165 return DefaultSearchManager(profile()->GetPrefs(), |
| 166 DefaultSearchManager::ObserverCallback()) |
| 167 .GetDefaultSearchEngine(source); |
| 168 } |
| 169 |
| 63 TEST_F(DefaultSearchPrefMigrationTest, MigrateUserSelectedValue) { | 170 TEST_F(DefaultSearchPrefMigrationTest, MigrateUserSelectedValue) { |
| 64 scoped_ptr<TemplateURL> t_url( | 171 scoped_ptr<TemplateURL> t_url( |
| 65 CreateKeyword("name1", "key1", "http://foo1/{searchTerms}")); | 172 CreateKeyword("name1", "key1", "http://foo1/{searchTerms}")); |
| 66 // Store a value in the legacy location. | 173 // Store a value in the legacy location. |
| 67 TemplateURLService::SaveDefaultSearchProviderToPrefs( | 174 SaveDefaultSearchProviderToLegacyPrefs(t_url.get()); |
| 68 t_url.get(), test_util()->profile()->GetPrefs()); | |
| 69 | 175 |
| 70 // Run the migration. | 176 // Run the migration. |
| 71 ConfigureDefaultSearchPrefMigrationToDictionaryValue( | 177 ConfigureDefaultSearchPrefMigrationToDictionaryValue(profile()->GetPrefs()); |
| 72 test_util()->profile()->GetPrefs()); | |
| 73 | 178 |
| 74 // Test that it was migrated. | 179 // Test that it was migrated. |
| 75 TemplateURLData modern_default; | 180 DefaultSearchManager::Source source; |
| 76 ASSERT_TRUE(DefaultSearchManager(test_util()->profile()->GetPrefs()) | 181 const TemplateURLData* modern_default = GetModernDefaultSearchEngine(&source); |
| 77 .GetDefaultSearchEngine(&modern_default)); | 182 ASSERT_TRUE(modern_default); |
| 78 EXPECT_EQ(t_url->short_name(), modern_default.short_name); | 183 EXPECT_EQ(DefaultSearchManager::FROM_USER, source); |
| 79 EXPECT_EQ(t_url->keyword(), modern_default.keyword()); | 184 EXPECT_EQ(t_url->short_name(), modern_default->short_name); |
| 80 EXPECT_EQ(t_url->url(), modern_default.url()); | 185 EXPECT_EQ(t_url->keyword(), modern_default->keyword()); |
| 186 EXPECT_EQ(t_url->url(), modern_default->url()); |
| 81 } | 187 } |
| 82 | 188 |
| 83 TEST_F(DefaultSearchPrefMigrationTest, ModernValuePresent) { | 189 TEST_F(DefaultSearchPrefMigrationTest, ModernValuePresent) { |
| 84 scoped_ptr<TemplateURL> t_url( | 190 scoped_ptr<TemplateURL> t_url( |
| 85 CreateKeyword("name1", "key1", "http://foo1/{searchTerms}")); | 191 CreateKeyword("name1", "key1", "http://foo1/{searchTerms}")); |
| 86 scoped_ptr<TemplateURL> t_url2( | 192 scoped_ptr<TemplateURL> t_url2( |
| 87 CreateKeyword("name2", "key2", "http://foo2/{searchTerms}")); | 193 CreateKeyword("name2", "key2", "http://foo2/{searchTerms}")); |
| 88 // Store a value in the legacy location. | 194 // Store a value in the legacy location. |
| 89 TemplateURLService::SaveDefaultSearchProviderToPrefs( | 195 SaveDefaultSearchProviderToLegacyPrefs(t_url.get()); |
| 90 t_url.get(), test_util()->profile()->GetPrefs()); | |
| 91 | 196 |
| 92 // Store another value in the modern location. | 197 // Store another value in the modern location. |
| 93 DefaultSearchManager(test_util()->profile()->GetPrefs()) | 198 SetModernUserSelectedDefaultSearchEngine(t_url2->data()); |
| 94 .SetUserSelectedDefaultSearchEngine(t_url2->data()); | |
| 95 | 199 |
| 96 // Run the migration. | 200 // Run the migration. |
| 97 ConfigureDefaultSearchPrefMigrationToDictionaryValue( | 201 ConfigureDefaultSearchPrefMigrationToDictionaryValue(profile()->GetPrefs()); |
| 98 test_util()->profile()->GetPrefs()); | |
| 99 | 202 |
| 100 // Test that no migration occurred. The modern value is left intact. | 203 // Test that no migration occurred. The modern value is left intact. |
| 101 TemplateURLData modern_default; | 204 DefaultSearchManager::Source source; |
| 102 ASSERT_TRUE(DefaultSearchManager(test_util()->profile()->GetPrefs()) | 205 const TemplateURLData* modern_default = GetModernDefaultSearchEngine(&source); |
| 103 .GetDefaultSearchEngine(&modern_default)); | 206 EXPECT_EQ(DefaultSearchManager::FROM_USER, source); |
| 104 EXPECT_EQ(t_url2->short_name(), modern_default.short_name); | 207 EXPECT_EQ(t_url2->short_name(), modern_default->short_name); |
| 105 EXPECT_EQ(t_url2->keyword(), modern_default.keyword()); | 208 EXPECT_EQ(t_url2->keyword(), modern_default->keyword()); |
| 106 EXPECT_EQ(t_url2->url(), modern_default.url()); | 209 EXPECT_EQ(t_url2->url(), modern_default->url()); |
| 107 } | 210 } |
| 108 | 211 |
| 109 TEST_F(DefaultSearchPrefMigrationTest, | 212 TEST_F(DefaultSearchPrefMigrationTest, |
| 110 AutomaticallySelectedValueIsNotMigrated) { | 213 AutomaticallySelectedValueIsNotMigrated) { |
| 111 test_util()->VerifyLoad(); | 214 DefaultSearchManager::Source source; |
| 112 scoped_ptr<TemplateURLData> legacy_default; | 215 TemplateURLData prepopulated_default(*GetModernDefaultSearchEngine(&source)); |
| 113 bool legacy_is_managed = false; | 216 EXPECT_EQ(DefaultSearchManager::FROM_FALLBACK, source); |
| 114 // The initialization of the TemplateURLService will have stored the | 217 |
| 115 // pre-populated DSE in the legacy location in prefs. | 218 TemplateURL prepopulated_turl(profile(), prepopulated_default); |
| 116 ASSERT_TRUE(TemplateURLService::LoadDefaultSearchProviderFromPrefs( | 219 |
| 117 test_util()->profile()->GetPrefs(), &legacy_default, &legacy_is_managed)); | 220 // Store a value in the legacy location. |
| 118 EXPECT_FALSE(legacy_is_managed); | 221 SaveDefaultSearchProviderToLegacyPrefs(&prepopulated_turl); |
| 119 EXPECT_TRUE(legacy_default); | |
| 120 EXPECT_GT(legacy_default->prepopulate_id, 0); | |
| 121 | 222 |
| 122 // Run the migration. | 223 // Run the migration. |
| 123 ConfigureDefaultSearchPrefMigrationToDictionaryValue( | 224 ConfigureDefaultSearchPrefMigrationToDictionaryValue(profile()->GetPrefs()); |
| 124 test_util()->profile()->GetPrefs()); | |
| 125 | 225 |
| 126 // Test that the legacy value is not migrated, as it is not user-selected. | 226 // Test that the legacy value is not migrated, as it is not user-selected. |
| 127 TemplateURLData modern_default; | 227 GetModernDefaultSearchEngine(&source); |
| 128 ASSERT_FALSE(DefaultSearchManager(test_util()->profile()->GetPrefs()) | 228 EXPECT_EQ(DefaultSearchManager::FROM_FALLBACK, source); |
| 129 .GetDefaultSearchEngine(&modern_default)); | |
| 130 } | 229 } |
| 131 | |
| 132 TEST_F(DefaultSearchPrefMigrationTest, ManagedValueIsNotMigrated) { | |
| 133 // Set a managed preference that establishes a default search provider. | |
| 134 const char kName[] = "test1"; | |
| 135 const char kKeyword[] = "test.com"; | |
| 136 const char kSearchURL[] = "http://test.com/search?t={searchTerms}"; | |
| 137 const char kIconURL[] = "http://test.com/icon.jpg"; | |
| 138 const char kEncodings[] = "UTF-16;UTF-32"; | |
| 139 const char kAlternateURL[] = "http://test.com/search#t={searchTerms}"; | |
| 140 const char kSearchTermsReplacementKey[] = "espv"; | |
| 141 | |
| 142 // This method only updates the legacy location for managed DSEs. So it will | |
| 143 // not cause DefaultSearchManager to report a value. | |
| 144 test_util()->SetManagedDefaultSearchPreferences(true, | |
| 145 kName, | |
| 146 kKeyword, | |
| 147 kSearchURL, | |
| 148 std::string(), | |
| 149 kIconURL, | |
| 150 kEncodings, | |
| 151 kAlternateURL, | |
| 152 kSearchTermsReplacementKey); | |
| 153 test_util()->VerifyLoad(); | |
| 154 | |
| 155 // Verify that the policy value is correctly installed. | |
| 156 scoped_ptr<TemplateURLData> legacy_default; | |
| 157 bool legacy_is_managed = false; | |
| 158 ASSERT_TRUE(TemplateURLService::LoadDefaultSearchProviderFromPrefs( | |
| 159 test_util()->profile()->GetPrefs(), &legacy_default, &legacy_is_managed)); | |
| 160 EXPECT_TRUE(legacy_is_managed); | |
| 161 EXPECT_TRUE(legacy_default); | |
| 162 | |
| 163 // Run the migration. | |
| 164 ConfigureDefaultSearchPrefMigrationToDictionaryValue( | |
| 165 test_util()->profile()->GetPrefs()); | |
| 166 | |
| 167 // Test that the policy-defined value is not migrated. | |
| 168 TemplateURLData modern_default; | |
| 169 ASSERT_FALSE(DefaultSearchManager(test_util()->profile()->GetPrefs()) | |
| 170 .GetDefaultSearchEngine(&modern_default)); | |
| 171 } | |
| OLD | NEW |