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.
5 #include "components/search_engines/default_search_pref_migration.h"
9 #include "base/compiler_specific.h"
10 #include "base/files/scoped_temp_dir.h"
11 #include "base/logging.h"
12 #include "base/macros.h"
13 #include "base/strings/string16.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "components/pref_registry/testing_pref_service_syncable.h"
18 #include "components/search_engines/default_search_manager.h"
19 #include "components/search_engines/search_engines_pref_names.h"
20 #include "components/search_engines/template_url.h"
21 #include "components/search_engines/template_url_prepopulate_data.h"
22 #include "components/search_engines/template_url_service.h"
23 #include "testing/gtest/include/gtest/gtest.h"
26 class DefaultSearchPrefMigrationTest
: public testing::Test
{
28 DefaultSearchPrefMigrationTest();
30 void SaveDefaultSearchProviderToLegacyPrefs(const TemplateURL
* t_url
);
32 scoped_ptr
<TemplateURL
> CreateKeyword(const std::string
& short_name
,
33 const std::string
& keyword
,
34 const std::string
& url
);
36 DefaultSearchManager
* default_search_manager() {
37 return default_search_manager_
.get();
40 PrefService
* pref_service() { return &prefs_
; }
43 user_prefs::TestingPrefServiceSyncable prefs_
;
44 scoped_ptr
<DefaultSearchManager
> default_search_manager_
;
46 DISALLOW_COPY_AND_ASSIGN(DefaultSearchPrefMigrationTest
);
49 DefaultSearchPrefMigrationTest::DefaultSearchPrefMigrationTest() {
50 DefaultSearchManager::RegisterProfilePrefs(prefs_
.registry());
51 TemplateURLPrepopulateData::RegisterProfilePrefs(prefs_
.registry());
52 TemplateURLService::RegisterProfilePrefs(prefs_
.registry());
53 default_search_manager_
.reset(new DefaultSearchManager(
54 pref_service(), DefaultSearchManager::ObserverCallback()));
57 void DefaultSearchPrefMigrationTest::SaveDefaultSearchProviderToLegacyPrefs(
58 const TemplateURL
* t_url
) {
59 PrefService
* prefs
= pref_service();
62 std::string search_url
;
63 std::string suggest_url
;
64 std::string instant_url
;
65 std::string image_url
;
66 std::string new_tab_url
;
67 std::string search_url_post_params
;
68 std::string suggest_url_post_params
;
69 std::string instant_url_post_params
;
70 std::string image_url_post_params
;
72 std::string encodings
;
73 std::string short_name
;
75 std::string id_string
;
76 std::string prepopulate_id
;
77 base::ListValue alternate_urls
;
78 std::string search_terms_replacement_key
;
80 DCHECK_EQ(TemplateURL::NORMAL
, t_url
->GetType());
82 search_url
= t_url
->url();
83 suggest_url
= t_url
->suggestions_url();
84 instant_url
= t_url
->instant_url();
85 image_url
= t_url
->image_url();
86 new_tab_url
= t_url
->new_tab_url();
87 search_url_post_params
= t_url
->search_url_post_params();
88 suggest_url_post_params
= t_url
->suggestions_url_post_params();
89 instant_url_post_params
= t_url
->instant_url_post_params();
90 image_url_post_params
= t_url
->image_url_post_params();
91 GURL icon_gurl
= t_url
->favicon_url();
92 if (!icon_gurl
.is_empty())
93 icon_url
= icon_gurl
.spec();
94 encodings
= base::JoinString(t_url
->input_encodings(), ";");
95 short_name
= base::UTF16ToUTF8(t_url
->short_name());
96 keyword
= base::UTF16ToUTF8(t_url
->keyword());
97 id_string
= base::Int64ToString(t_url
->id());
98 prepopulate_id
= base::Int64ToString(t_url
->prepopulate_id());
99 for (size_t i
= 0; i
< t_url
->alternate_urls().size(); ++i
)
100 alternate_urls
.AppendString(t_url
->alternate_urls()[i
]);
101 search_terms_replacement_key
= t_url
->search_terms_replacement_key();
103 prefs
->SetBoolean(prefs::kDefaultSearchProviderEnabled
, enabled
);
104 prefs
->SetString(prefs::kDefaultSearchProviderSearchURL
, search_url
);
105 prefs
->SetString(prefs::kDefaultSearchProviderSuggestURL
, suggest_url
);
106 prefs
->SetString(prefs::kDefaultSearchProviderInstantURL
, instant_url
);
107 prefs
->SetString(prefs::kDefaultSearchProviderImageURL
, image_url
);
108 prefs
->SetString(prefs::kDefaultSearchProviderNewTabURL
, new_tab_url
);
109 prefs
->SetString(prefs::kDefaultSearchProviderSearchURLPostParams
,
110 search_url_post_params
);
111 prefs
->SetString(prefs::kDefaultSearchProviderSuggestURLPostParams
,
112 suggest_url_post_params
);
113 prefs
->SetString(prefs::kDefaultSearchProviderInstantURLPostParams
,
114 instant_url_post_params
);
115 prefs
->SetString(prefs::kDefaultSearchProviderImageURLPostParams
,
116 image_url_post_params
);
117 prefs
->SetString(prefs::kDefaultSearchProviderIconURL
, icon_url
);
118 prefs
->SetString(prefs::kDefaultSearchProviderEncodings
, encodings
);
119 prefs
->SetString(prefs::kDefaultSearchProviderName
, short_name
);
120 prefs
->SetString(prefs::kDefaultSearchProviderKeyword
, keyword
);
121 prefs
->SetString(prefs::kDefaultSearchProviderID
, id_string
);
122 prefs
->SetString(prefs::kDefaultSearchProviderPrepopulateID
, prepopulate_id
);
123 prefs
->Set(prefs::kDefaultSearchProviderAlternateURLs
, alternate_urls
);
124 prefs
->SetString(prefs::kDefaultSearchProviderSearchTermsReplacementKey
,
125 search_terms_replacement_key
);
128 scoped_ptr
<TemplateURL
> DefaultSearchPrefMigrationTest::CreateKeyword(
129 const std::string
& short_name
,
130 const std::string
& keyword
,
131 const std::string
& url
) {
132 TemplateURLData data
;
133 data
.SetShortName(base::ASCIIToUTF16(short_name
));
134 data
.SetKeyword(base::ASCIIToUTF16(keyword
));
136 scoped_ptr
<TemplateURL
> t_url(new TemplateURL(data
));
140 TEST_F(DefaultSearchPrefMigrationTest
, MigrateUserSelectedValue
) {
141 scoped_ptr
<TemplateURL
> t_url(
142 CreateKeyword("name1", "key1", "http://foo1/{searchTerms}"));
143 // Store a value in the legacy location.
144 SaveDefaultSearchProviderToLegacyPrefs(t_url
.get());
146 // Run the migration.
147 ConfigureDefaultSearchPrefMigrationToDictionaryValue(pref_service());
149 // Test that it was migrated.
150 DefaultSearchManager::Source source
;
151 const TemplateURLData
* modern_default
=
152 default_search_manager()->GetDefaultSearchEngine(&source
);
153 ASSERT_TRUE(modern_default
);
154 EXPECT_EQ(DefaultSearchManager::FROM_USER
, source
);
155 EXPECT_EQ(t_url
->short_name(), modern_default
->short_name());
156 EXPECT_EQ(t_url
->keyword(), modern_default
->keyword());
157 EXPECT_EQ(t_url
->url(), modern_default
->url());
160 TEST_F(DefaultSearchPrefMigrationTest
, MigrateOnlyOnce
) {
161 scoped_ptr
<TemplateURL
> t_url(
162 CreateKeyword("name1", "key1", "http://foo1/{searchTerms}"));
163 // Store a value in the legacy location.
164 SaveDefaultSearchProviderToLegacyPrefs(t_url
.get());
166 // Run the migration.
167 ConfigureDefaultSearchPrefMigrationToDictionaryValue(pref_service());
169 // Test that it was migrated.
170 DefaultSearchManager::Source source
;
171 const TemplateURLData
* modern_default
=
172 default_search_manager()->GetDefaultSearchEngine(&source
);
173 ASSERT_TRUE(modern_default
);
174 EXPECT_EQ(DefaultSearchManager::FROM_USER
, source
);
175 EXPECT_EQ(t_url
->short_name(), modern_default
->short_name());
176 EXPECT_EQ(t_url
->keyword(), modern_default
->keyword());
177 EXPECT_EQ(t_url
->url(), modern_default
->url());
178 default_search_manager()->ClearUserSelectedDefaultSearchEngine();
180 // Run the migration.
181 ConfigureDefaultSearchPrefMigrationToDictionaryValue(pref_service());
183 // Test that it was NOT migrated.
184 modern_default
= default_search_manager()->GetDefaultSearchEngine(&source
);
185 ASSERT_TRUE(modern_default
);
186 EXPECT_EQ(DefaultSearchManager::FROM_FALLBACK
, source
);
189 TEST_F(DefaultSearchPrefMigrationTest
, ModernValuePresent
) {
190 scoped_ptr
<TemplateURL
> t_url(
191 CreateKeyword("name1", "key1", "http://foo1/{searchTerms}"));
192 scoped_ptr
<TemplateURL
> t_url2(
193 CreateKeyword("name2", "key2", "http://foo2/{searchTerms}"));
194 // Store a value in the legacy location.
195 SaveDefaultSearchProviderToLegacyPrefs(t_url
.get());
197 // Store another value in the modern location.
198 default_search_manager()->SetUserSelectedDefaultSearchEngine(t_url2
->data());
200 // Run the migration.
201 ConfigureDefaultSearchPrefMigrationToDictionaryValue(pref_service());
203 // Test that no migration occurred. The modern value is left intact.
204 DefaultSearchManager::Source source
;
205 const TemplateURLData
* modern_default
=
206 default_search_manager()->GetDefaultSearchEngine(&source
);
207 ASSERT_TRUE(modern_default
);
208 EXPECT_EQ(DefaultSearchManager::FROM_USER
, source
);
209 EXPECT_EQ(t_url2
->short_name(), modern_default
->short_name());
210 EXPECT_EQ(t_url2
->keyword(), modern_default
->keyword());
211 EXPECT_EQ(t_url2
->url(), modern_default
->url());
214 TEST_F(DefaultSearchPrefMigrationTest
,
215 AutomaticallySelectedValueIsNotMigrated
) {
216 DefaultSearchManager::Source source
;
217 TemplateURLData
prepopulated_default(
218 *default_search_manager()->GetDefaultSearchEngine(&source
));
219 EXPECT_EQ(DefaultSearchManager::FROM_FALLBACK
, source
);
221 TemplateURL
prepopulated_turl(prepopulated_default
);
223 // Store a value in the legacy location.
224 SaveDefaultSearchProviderToLegacyPrefs(&prepopulated_turl
);
226 // Run the migration.
227 ConfigureDefaultSearchPrefMigrationToDictionaryValue(pref_service());
229 // Test that the legacy value is not migrated, as it is not user-selected.
230 default_search_manager()->GetDefaultSearchEngine(&source
);
231 EXPECT_EQ(DefaultSearchManager::FROM_FALLBACK
, source
);