Componentize HistoryURLProvider/ScoredHistoryMatch.
[chromium-blink-merge.git] / chrome / browser / search_engines / default_search_pref_migration_unittest.cc
blobd78010d1644f32088f428365f6178031073451c7
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 "chrome/browser/search_engines/default_search_pref_migration.h"
7 #include <string>
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/prefs/pref_service.h"
14 #include "base/strings/string16.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "base/strings/string_util.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "chrome/common/pref_names.h"
19 #include "chrome/test/base/testing_pref_service_syncable.h"
20 #include "chrome/test/base/testing_profile.h"
21 #include "components/search_engines/search_engines_pref_names.h"
22 #include "components/search_engines/template_url.h"
23 #include "components/search_engines/template_url_service.h"
24 #include "content/public/test/test_browser_thread_bundle.h"
25 #include "testing/gtest/include/gtest/gtest.h"
26 #include "url/gurl.h"
28 class DefaultSearchPrefMigrationTest : public testing::Test {
29 public:
30 DefaultSearchPrefMigrationTest();
32 // testing::Test:
33 void SetUp() override;
35 void SaveDefaultSearchProviderToLegacyPrefs(const TemplateURL* t_url);
37 scoped_ptr<TemplateURL> CreateKeyword(const std::string& short_name,
38 const std::string& keyword,
39 const std::string& url);
41 TestingProfile* profile() { return profile_.get(); }
43 DefaultSearchManager* default_search_manager() {
44 return default_search_manager_.get();
47 private:
48 content::TestBrowserThreadBundle thread_bundle_;
49 base::ScopedTempDir temp_dir_;
50 scoped_ptr<TestingProfile> profile_;
51 scoped_ptr<DefaultSearchManager> default_search_manager_;
53 DISALLOW_COPY_AND_ASSIGN(DefaultSearchPrefMigrationTest);
56 DefaultSearchPrefMigrationTest::DefaultSearchPrefMigrationTest() {
59 void DefaultSearchPrefMigrationTest::SetUp() {
60 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
61 profile_.reset(new TestingProfile(temp_dir_.path()));
62 default_search_manager_.reset(new DefaultSearchManager(
63 profile_->GetPrefs(), DefaultSearchManager::ObserverCallback()));
66 void DefaultSearchPrefMigrationTest::SaveDefaultSearchProviderToLegacyPrefs(
67 const TemplateURL* t_url) {
68 PrefService* prefs = profile()->GetPrefs();
70 bool enabled = false;
71 std::string search_url;
72 std::string suggest_url;
73 std::string instant_url;
74 std::string image_url;
75 std::string new_tab_url;
76 std::string search_url_post_params;
77 std::string suggest_url_post_params;
78 std::string instant_url_post_params;
79 std::string image_url_post_params;
80 std::string icon_url;
81 std::string encodings;
82 std::string short_name;
83 std::string keyword;
84 std::string id_string;
85 std::string prepopulate_id;
86 base::ListValue alternate_urls;
87 std::string search_terms_replacement_key;
88 if (t_url) {
89 DCHECK_EQ(TemplateURL::NORMAL, t_url->GetType());
90 enabled = true;
91 search_url = t_url->url();
92 suggest_url = t_url->suggestions_url();
93 instant_url = t_url->instant_url();
94 image_url = t_url->image_url();
95 new_tab_url = t_url->new_tab_url();
96 search_url_post_params = t_url->search_url_post_params();
97 suggest_url_post_params = t_url->suggestions_url_post_params();
98 instant_url_post_params = t_url->instant_url_post_params();
99 image_url_post_params = t_url->image_url_post_params();
100 GURL icon_gurl = t_url->favicon_url();
101 if (!icon_gurl.is_empty())
102 icon_url = icon_gurl.spec();
103 encodings = JoinString(t_url->input_encodings(), ';');
104 short_name = base::UTF16ToUTF8(t_url->short_name());
105 keyword = base::UTF16ToUTF8(t_url->keyword());
106 id_string = base::Int64ToString(t_url->id());
107 prepopulate_id = base::Int64ToString(t_url->prepopulate_id());
108 for (size_t i = 0; i < t_url->alternate_urls().size(); ++i)
109 alternate_urls.AppendString(t_url->alternate_urls()[i]);
110 search_terms_replacement_key = t_url->search_terms_replacement_key();
112 prefs->SetBoolean(prefs::kDefaultSearchProviderEnabled, enabled);
113 prefs->SetString(prefs::kDefaultSearchProviderSearchURL, search_url);
114 prefs->SetString(prefs::kDefaultSearchProviderSuggestURL, suggest_url);
115 prefs->SetString(prefs::kDefaultSearchProviderInstantURL, instant_url);
116 prefs->SetString(prefs::kDefaultSearchProviderImageURL, image_url);
117 prefs->SetString(prefs::kDefaultSearchProviderNewTabURL, new_tab_url);
118 prefs->SetString(prefs::kDefaultSearchProviderSearchURLPostParams,
119 search_url_post_params);
120 prefs->SetString(prefs::kDefaultSearchProviderSuggestURLPostParams,
121 suggest_url_post_params);
122 prefs->SetString(prefs::kDefaultSearchProviderInstantURLPostParams,
123 instant_url_post_params);
124 prefs->SetString(prefs::kDefaultSearchProviderImageURLPostParams,
125 image_url_post_params);
126 prefs->SetString(prefs::kDefaultSearchProviderIconURL, icon_url);
127 prefs->SetString(prefs::kDefaultSearchProviderEncodings, encodings);
128 prefs->SetString(prefs::kDefaultSearchProviderName, short_name);
129 prefs->SetString(prefs::kDefaultSearchProviderKeyword, keyword);
130 prefs->SetString(prefs::kDefaultSearchProviderID, id_string);
131 prefs->SetString(prefs::kDefaultSearchProviderPrepopulateID, prepopulate_id);
132 prefs->Set(prefs::kDefaultSearchProviderAlternateURLs, alternate_urls);
133 prefs->SetString(prefs::kDefaultSearchProviderSearchTermsReplacementKey,
134 search_terms_replacement_key);
137 scoped_ptr<TemplateURL> DefaultSearchPrefMigrationTest::CreateKeyword(
138 const std::string& short_name,
139 const std::string& keyword,
140 const std::string& url) {
141 TemplateURLData data;
142 data.SetShortName(base::ASCIIToUTF16(short_name));
143 data.SetKeyword(base::ASCIIToUTF16(keyword));
144 data.SetURL(url);
145 scoped_ptr<TemplateURL> t_url(new TemplateURL(data));
146 return t_url.Pass();
149 TEST_F(DefaultSearchPrefMigrationTest, MigrateUserSelectedValue) {
150 scoped_ptr<TemplateURL> t_url(
151 CreateKeyword("name1", "key1", "http://foo1/{searchTerms}"));
152 // Store a value in the legacy location.
153 SaveDefaultSearchProviderToLegacyPrefs(t_url.get());
155 // Run the migration.
156 ConfigureDefaultSearchPrefMigrationToDictionaryValue(profile()->GetPrefs());
158 // Test that it was migrated.
159 DefaultSearchManager::Source source;
160 const TemplateURLData* modern_default =
161 default_search_manager()->GetDefaultSearchEngine(&source);
162 ASSERT_TRUE(modern_default);
163 EXPECT_EQ(DefaultSearchManager::FROM_USER, source);
164 EXPECT_EQ(t_url->short_name(), modern_default->short_name());
165 EXPECT_EQ(t_url->keyword(), modern_default->keyword());
166 EXPECT_EQ(t_url->url(), modern_default->url());
169 TEST_F(DefaultSearchPrefMigrationTest, MigrateOnlyOnce) {
170 scoped_ptr<TemplateURL> t_url(
171 CreateKeyword("name1", "key1", "http://foo1/{searchTerms}"));
172 // Store a value in the legacy location.
173 SaveDefaultSearchProviderToLegacyPrefs(t_url.get());
175 // Run the migration.
176 ConfigureDefaultSearchPrefMigrationToDictionaryValue(profile()->GetPrefs());
178 // Test that it was migrated.
179 DefaultSearchManager::Source source;
180 const TemplateURLData* modern_default =
181 default_search_manager()->GetDefaultSearchEngine(&source);
182 ASSERT_TRUE(modern_default);
183 EXPECT_EQ(DefaultSearchManager::FROM_USER, source);
184 EXPECT_EQ(t_url->short_name(), modern_default->short_name());
185 EXPECT_EQ(t_url->keyword(), modern_default->keyword());
186 EXPECT_EQ(t_url->url(), modern_default->url());
187 default_search_manager()->ClearUserSelectedDefaultSearchEngine();
189 // Run the migration.
190 ConfigureDefaultSearchPrefMigrationToDictionaryValue(profile()->GetPrefs());
192 // Test that it was NOT migrated.
193 modern_default = default_search_manager()->GetDefaultSearchEngine(&source);
194 ASSERT_TRUE(modern_default);
195 EXPECT_EQ(DefaultSearchManager::FROM_FALLBACK, source);
198 TEST_F(DefaultSearchPrefMigrationTest, ModernValuePresent) {
199 scoped_ptr<TemplateURL> t_url(
200 CreateKeyword("name1", "key1", "http://foo1/{searchTerms}"));
201 scoped_ptr<TemplateURL> t_url2(
202 CreateKeyword("name2", "key2", "http://foo2/{searchTerms}"));
203 // Store a value in the legacy location.
204 SaveDefaultSearchProviderToLegacyPrefs(t_url.get());
206 // Store another value in the modern location.
207 default_search_manager()->SetUserSelectedDefaultSearchEngine(t_url2->data());
209 // Run the migration.
210 ConfigureDefaultSearchPrefMigrationToDictionaryValue(profile()->GetPrefs());
212 // Test that no migration occurred. The modern value is left intact.
213 DefaultSearchManager::Source source;
214 const TemplateURLData* modern_default =
215 default_search_manager()->GetDefaultSearchEngine(&source);
216 ASSERT_TRUE(modern_default);
217 EXPECT_EQ(DefaultSearchManager::FROM_USER, source);
218 EXPECT_EQ(t_url2->short_name(), modern_default->short_name());
219 EXPECT_EQ(t_url2->keyword(), modern_default->keyword());
220 EXPECT_EQ(t_url2->url(), modern_default->url());
223 TEST_F(DefaultSearchPrefMigrationTest,
224 AutomaticallySelectedValueIsNotMigrated) {
225 DefaultSearchManager::Source source;
226 TemplateURLData prepopulated_default(
227 *default_search_manager()->GetDefaultSearchEngine(&source));
228 EXPECT_EQ(DefaultSearchManager::FROM_FALLBACK, source);
230 TemplateURL prepopulated_turl(prepopulated_default);
232 // Store a value in the legacy location.
233 SaveDefaultSearchProviderToLegacyPrefs(&prepopulated_turl);
235 // Run the migration.
236 ConfigureDefaultSearchPrefMigrationToDictionaryValue(profile()->GetPrefs());
238 // Test that the legacy value is not migrated, as it is not user-selected.
239 default_search_manager()->GetDefaultSearchEngine(&source);
240 EXPECT_EQ(DefaultSearchManager::FROM_FALLBACK, source);