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