Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(845)

Side by Side Diff: components/search_engines/default_search_pref_migration_unittest.cc

Issue 2598033004: Remove old default search preferences. (Closed)
Patch Set: Fixed after review, round 2 Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "components/search_engines/default_search_pref_migration.h"
6
7 #include <stddef.h>
8
9 #include <string>
10
11 #include "base/compiler_specific.h"
12 #include "base/files/scoped_temp_dir.h"
13 #include "base/logging.h"
14 #include "base/macros.h"
15 #include "base/strings/string16.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/string_util.h"
18 #include "base/strings/utf_string_conversions.h"
19 #include "components/search_engines/default_search_manager.h"
20 #include "components/search_engines/search_engines_pref_names.h"
21 #include "components/search_engines/template_url.h"
22 #include "components/search_engines/template_url_prepopulate_data.h"
23 #include "components/search_engines/template_url_service.h"
24 #include "components/sync_preferences/testing_pref_service_syncable.h"
25 #include "testing/gtest/include/gtest/gtest.h"
26 #include "url/gurl.h"
27
28 class DefaultSearchPrefMigrationTest : public testing::Test {
29 public:
30 DefaultSearchPrefMigrationTest();
31
32 void SaveDefaultSearchProviderToLegacyPrefs(const TemplateURL* t_url);
33
34 std::unique_ptr<TemplateURL> CreateKeyword(const std::string& short_name,
35 const std::string& keyword,
36 const std::string& url);
37
38 DefaultSearchManager* default_search_manager() {
39 return default_search_manager_.get();
40 }
41
42 PrefService* pref_service() { return &prefs_; }
43
44 private:
45 sync_preferences::TestingPrefServiceSyncable prefs_;
46 std::unique_ptr<DefaultSearchManager> default_search_manager_;
47
48 DISALLOW_COPY_AND_ASSIGN(DefaultSearchPrefMigrationTest);
49 };
50
51 DefaultSearchPrefMigrationTest::DefaultSearchPrefMigrationTest() {
52 DefaultSearchManager::RegisterProfilePrefs(prefs_.registry());
53 TemplateURLPrepopulateData::RegisterProfilePrefs(prefs_.registry());
54 TemplateURLService::RegisterProfilePrefs(prefs_.registry());
55 default_search_manager_.reset(new DefaultSearchManager(
56 pref_service(), DefaultSearchManager::ObserverCallback()));
57 }
58
59 void DefaultSearchPrefMigrationTest::SaveDefaultSearchProviderToLegacyPrefs(
60 const TemplateURL* t_url) {
61 PrefService* prefs = pref_service();
62
63 bool enabled = false;
64 std::string search_url;
65 std::string suggest_url;
66 std::string instant_url;
67 std::string image_url;
68 std::string new_tab_url;
69 std::string search_url_post_params;
70 std::string suggest_url_post_params;
71 std::string instant_url_post_params;
72 std::string image_url_post_params;
73 std::string icon_url;
74 std::string encodings;
75 std::string short_name;
76 std::string keyword;
77 std::string id_string;
78 std::string prepopulate_id;
79 base::ListValue alternate_urls;
80 std::string search_terms_replacement_key;
81 if (t_url) {
82 DCHECK_EQ(TemplateURL::NORMAL, t_url->type());
83 enabled = true;
84 search_url = t_url->url();
85 suggest_url = t_url->suggestions_url();
86 instant_url = t_url->instant_url();
87 image_url = t_url->image_url();
88 new_tab_url = t_url->new_tab_url();
89 search_url_post_params = t_url->search_url_post_params();
90 suggest_url_post_params = t_url->suggestions_url_post_params();
91 instant_url_post_params = t_url->instant_url_post_params();
92 image_url_post_params = t_url->image_url_post_params();
93 GURL icon_gurl = t_url->favicon_url();
94 if (!icon_gurl.is_empty())
95 icon_url = icon_gurl.spec();
96 encodings = base::JoinString(t_url->input_encodings(), ";");
97 short_name = base::UTF16ToUTF8(t_url->short_name());
98 keyword = base::UTF16ToUTF8(t_url->keyword());
99 id_string = base::Int64ToString(t_url->id());
100 prepopulate_id = base::Int64ToString(t_url->prepopulate_id());
101 for (size_t i = 0; i < t_url->alternate_urls().size(); ++i)
102 alternate_urls.AppendString(t_url->alternate_urls()[i]);
103 search_terms_replacement_key = t_url->search_terms_replacement_key();
104 }
105 prefs->SetBoolean(prefs::kDefaultSearchProviderEnabled, enabled);
106 prefs->SetString(prefs::kDefaultSearchProviderSearchURL, search_url);
107 prefs->SetString(prefs::kDefaultSearchProviderSuggestURL, suggest_url);
108 prefs->SetString(prefs::kDefaultSearchProviderInstantURL, instant_url);
109 prefs->SetString(prefs::kDefaultSearchProviderImageURL, image_url);
110 prefs->SetString(prefs::kDefaultSearchProviderNewTabURL, new_tab_url);
111 prefs->SetString(prefs::kDefaultSearchProviderSearchURLPostParams,
112 search_url_post_params);
113 prefs->SetString(prefs::kDefaultSearchProviderSuggestURLPostParams,
114 suggest_url_post_params);
115 prefs->SetString(prefs::kDefaultSearchProviderInstantURLPostParams,
116 instant_url_post_params);
117 prefs->SetString(prefs::kDefaultSearchProviderImageURLPostParams,
118 image_url_post_params);
119 prefs->SetString(prefs::kDefaultSearchProviderIconURL, icon_url);
120 prefs->SetString(prefs::kDefaultSearchProviderEncodings, encodings);
121 prefs->SetString(prefs::kDefaultSearchProviderName, short_name);
122 prefs->SetString(prefs::kDefaultSearchProviderKeyword, keyword);
123 prefs->SetString(prefs::kDefaultSearchProviderID, id_string);
124 prefs->SetString(prefs::kDefaultSearchProviderPrepopulateID, prepopulate_id);
125 prefs->Set(prefs::kDefaultSearchProviderAlternateURLs, alternate_urls);
126 prefs->SetString(prefs::kDefaultSearchProviderSearchTermsReplacementKey,
127 search_terms_replacement_key);
128 }
129
130 std::unique_ptr<TemplateURL> DefaultSearchPrefMigrationTest::CreateKeyword(
131 const std::string& short_name,
132 const std::string& keyword,
133 const std::string& url) {
134 TemplateURLData data;
135 data.SetShortName(base::ASCIIToUTF16(short_name));
136 data.SetKeyword(base::ASCIIToUTF16(keyword));
137 data.SetURL(url);
138 std::unique_ptr<TemplateURL> t_url(new TemplateURL(data));
139 return t_url;
140 }
141
142 TEST_F(DefaultSearchPrefMigrationTest, MigrateUserSelectedValue) {
143 std::unique_ptr<TemplateURL> t_url(
144 CreateKeyword("name1", "key1", "http://foo1/{searchTerms}"));
145 // Store a value in the legacy location.
146 SaveDefaultSearchProviderToLegacyPrefs(t_url.get());
147
148 // Run the migration.
149 ConfigureDefaultSearchPrefMigrationToDictionaryValue(pref_service());
150
151 // Test that it was migrated.
152 DefaultSearchManager::Source source;
153 const TemplateURLData* modern_default =
154 default_search_manager()->GetDefaultSearchEngine(&source);
155 ASSERT_TRUE(modern_default);
156 EXPECT_EQ(DefaultSearchManager::FROM_USER, source);
157 EXPECT_EQ(t_url->short_name(), modern_default->short_name());
158 EXPECT_EQ(t_url->keyword(), modern_default->keyword());
159 EXPECT_EQ(t_url->url(), modern_default->url());
160 }
161
162 TEST_F(DefaultSearchPrefMigrationTest, MigrateOnlyOnce) {
163 std::unique_ptr<TemplateURL> t_url(
164 CreateKeyword("name1", "key1", "http://foo1/{searchTerms}"));
165 // Store a value in the legacy location.
166 SaveDefaultSearchProviderToLegacyPrefs(t_url.get());
167
168 // Run the migration.
169 ConfigureDefaultSearchPrefMigrationToDictionaryValue(pref_service());
170
171 // Test that it was migrated.
172 DefaultSearchManager::Source source;
173 const TemplateURLData* modern_default =
174 default_search_manager()->GetDefaultSearchEngine(&source);
175 ASSERT_TRUE(modern_default);
176 EXPECT_EQ(DefaultSearchManager::FROM_USER, source);
177 EXPECT_EQ(t_url->short_name(), modern_default->short_name());
178 EXPECT_EQ(t_url->keyword(), modern_default->keyword());
179 EXPECT_EQ(t_url->url(), modern_default->url());
180 default_search_manager()->ClearUserSelectedDefaultSearchEngine();
181
182 // Run the migration.
183 ConfigureDefaultSearchPrefMigrationToDictionaryValue(pref_service());
184
185 // Test that it was NOT migrated.
186 modern_default = default_search_manager()->GetDefaultSearchEngine(&source);
187 ASSERT_TRUE(modern_default);
188 EXPECT_EQ(DefaultSearchManager::FROM_FALLBACK, source);
189 }
190
191 TEST_F(DefaultSearchPrefMigrationTest, ModernValuePresent) {
192 std::unique_ptr<TemplateURL> t_url(
193 CreateKeyword("name1", "key1", "http://foo1/{searchTerms}"));
194 std::unique_ptr<TemplateURL> t_url2(
195 CreateKeyword("name2", "key2", "http://foo2/{searchTerms}"));
196 // Store a value in the legacy location.
197 SaveDefaultSearchProviderToLegacyPrefs(t_url.get());
198
199 // Store another value in the modern location.
200 default_search_manager()->SetUserSelectedDefaultSearchEngine(t_url2->data());
201
202 // Run the migration.
203 ConfigureDefaultSearchPrefMigrationToDictionaryValue(pref_service());
204
205 // Test that no migration occurred. The modern value is left intact.
206 DefaultSearchManager::Source source;
207 const TemplateURLData* modern_default =
208 default_search_manager()->GetDefaultSearchEngine(&source);
209 ASSERT_TRUE(modern_default);
210 EXPECT_EQ(DefaultSearchManager::FROM_USER, source);
211 EXPECT_EQ(t_url2->short_name(), modern_default->short_name());
212 EXPECT_EQ(t_url2->keyword(), modern_default->keyword());
213 EXPECT_EQ(t_url2->url(), modern_default->url());
214 }
215
216 TEST_F(DefaultSearchPrefMigrationTest,
217 AutomaticallySelectedValueIsNotMigrated) {
218 DefaultSearchManager::Source source;
219 TemplateURLData prepopulated_default(
220 *default_search_manager()->GetDefaultSearchEngine(&source));
221 EXPECT_EQ(DefaultSearchManager::FROM_FALLBACK, source);
222
223 TemplateURL prepopulated_turl(prepopulated_default);
224
225 // Store a value in the legacy location.
226 SaveDefaultSearchProviderToLegacyPrefs(&prepopulated_turl);
227
228 // Run the migration.
229 ConfigureDefaultSearchPrefMigrationToDictionaryValue(pref_service());
230
231 // Test that the legacy value is not migrated, as it is not user-selected.
232 default_search_manager()->GetDefaultSearchEngine(&source);
233 EXPECT_EQ(DefaultSearchManager::FROM_FALLBACK, source);
234 }
OLDNEW
« no previous file with comments | « components/search_engines/default_search_pref_migration.cc ('k') | components/search_engines/search_engines_pref_names.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698