Give names to all utility processes.
[chromium-blink-merge.git] / chrome / browser / search_engines / default_search_pref_migration_unittest.cc
blob55087367766727dcbad6b0f14baa17501bd456da
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 "testing/gtest/include/gtest/gtest.h"
25 #include "url/gurl.h"
27 class DefaultSearchPrefMigrationTest : public testing::Test {
28 public:
29 DefaultSearchPrefMigrationTest();
31 // testing::Test:
32 void SetUp() override;
34 void SaveDefaultSearchProviderToLegacyPrefs(const TemplateURL* t_url);
36 scoped_ptr<TemplateURL> CreateKeyword(const std::string& short_name,
37 const std::string& keyword,
38 const std::string& url);
40 TestingProfile* profile() { return profile_.get(); }
42 DefaultSearchManager* default_search_manager() {
43 return default_search_manager_.get();
46 private:
47 base::ScopedTempDir temp_dir_;
48 scoped_ptr<TestingProfile> profile_;
49 scoped_ptr<DefaultSearchManager> default_search_manager_;
51 DISALLOW_COPY_AND_ASSIGN(DefaultSearchPrefMigrationTest);
54 DefaultSearchPrefMigrationTest::DefaultSearchPrefMigrationTest() {
57 void DefaultSearchPrefMigrationTest::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()));
64 void DefaultSearchPrefMigrationTest::SaveDefaultSearchProviderToLegacyPrefs(
65 const TemplateURL* t_url) {
66 PrefService* prefs = profile()->GetPrefs();
68 bool enabled = false;
69 std::string search_url;
70 std::string suggest_url;
71 std::string instant_url;
72 std::string image_url;
73 std::string new_tab_url;
74 std::string search_url_post_params;
75 std::string suggest_url_post_params;
76 std::string instant_url_post_params;
77 std::string image_url_post_params;
78 std::string icon_url;
79 std::string encodings;
80 std::string short_name;
81 std::string keyword;
82 std::string id_string;
83 std::string prepopulate_id;
84 base::ListValue alternate_urls;
85 std::string search_terms_replacement_key;
86 if (t_url) {
87 DCHECK_EQ(TemplateURL::NORMAL, t_url->GetType());
88 enabled = true;
89 search_url = t_url->url();
90 suggest_url = t_url->suggestions_url();
91 instant_url = t_url->instant_url();
92 image_url = t_url->image_url();
93 new_tab_url = t_url->new_tab_url();
94 search_url_post_params = t_url->search_url_post_params();
95 suggest_url_post_params = t_url->suggestions_url_post_params();
96 instant_url_post_params = t_url->instant_url_post_params();
97 image_url_post_params = t_url->image_url_post_params();
98 GURL icon_gurl = t_url->favicon_url();
99 if (!icon_gurl.is_empty())
100 icon_url = icon_gurl.spec();
101 encodings = JoinString(t_url->input_encodings(), ';');
102 short_name = base::UTF16ToUTF8(t_url->short_name());
103 keyword = base::UTF16ToUTF8(t_url->keyword());
104 id_string = base::Int64ToString(t_url->id());
105 prepopulate_id = base::Int64ToString(t_url->prepopulate_id());
106 for (size_t i = 0; i < t_url->alternate_urls().size(); ++i)
107 alternate_urls.AppendString(t_url->alternate_urls()[i]);
108 search_terms_replacement_key = t_url->search_terms_replacement_key();
110 prefs->SetBoolean(prefs::kDefaultSearchProviderEnabled, enabled);
111 prefs->SetString(prefs::kDefaultSearchProviderSearchURL, search_url);
112 prefs->SetString(prefs::kDefaultSearchProviderSuggestURL, suggest_url);
113 prefs->SetString(prefs::kDefaultSearchProviderInstantURL, instant_url);
114 prefs->SetString(prefs::kDefaultSearchProviderImageURL, image_url);
115 prefs->SetString(prefs::kDefaultSearchProviderNewTabURL, new_tab_url);
116 prefs->SetString(prefs::kDefaultSearchProviderSearchURLPostParams,
117 search_url_post_params);
118 prefs->SetString(prefs::kDefaultSearchProviderSuggestURLPostParams,
119 suggest_url_post_params);
120 prefs->SetString(prefs::kDefaultSearchProviderInstantURLPostParams,
121 instant_url_post_params);
122 prefs->SetString(prefs::kDefaultSearchProviderImageURLPostParams,
123 image_url_post_params);
124 prefs->SetString(prefs::kDefaultSearchProviderIconURL, icon_url);
125 prefs->SetString(prefs::kDefaultSearchProviderEncodings, encodings);
126 prefs->SetString(prefs::kDefaultSearchProviderName, short_name);
127 prefs->SetString(prefs::kDefaultSearchProviderKeyword, keyword);
128 prefs->SetString(prefs::kDefaultSearchProviderID, id_string);
129 prefs->SetString(prefs::kDefaultSearchProviderPrepopulateID, prepopulate_id);
130 prefs->Set(prefs::kDefaultSearchProviderAlternateURLs, alternate_urls);
131 prefs->SetString(prefs::kDefaultSearchProviderSearchTermsReplacementKey,
132 search_terms_replacement_key);
135 scoped_ptr<TemplateURL> DefaultSearchPrefMigrationTest::CreateKeyword(
136 const std::string& short_name,
137 const std::string& keyword,
138 const std::string& url) {
139 TemplateURLData data;
140 data.short_name = base::ASCIIToUTF16(short_name);
141 data.SetKeyword(base::ASCIIToUTF16(keyword));
142 data.SetURL(url);
143 scoped_ptr<TemplateURL> t_url(new TemplateURL(data));
144 return t_url.Pass();
147 TEST_F(DefaultSearchPrefMigrationTest, MigrateUserSelectedValue) {
148 scoped_ptr<TemplateURL> t_url(
149 CreateKeyword("name1", "key1", "http://foo1/{searchTerms}"));
150 // Store a value in the legacy location.
151 SaveDefaultSearchProviderToLegacyPrefs(t_url.get());
153 // Run the migration.
154 ConfigureDefaultSearchPrefMigrationToDictionaryValue(profile()->GetPrefs());
156 // Test that it was migrated.
157 DefaultSearchManager::Source source;
158 const TemplateURLData* modern_default =
159 default_search_manager()->GetDefaultSearchEngine(&source);
160 ASSERT_TRUE(modern_default);
161 EXPECT_EQ(DefaultSearchManager::FROM_USER, source);
162 EXPECT_EQ(t_url->short_name(), modern_default->short_name);
163 EXPECT_EQ(t_url->keyword(), modern_default->keyword());
164 EXPECT_EQ(t_url->url(), modern_default->url());
167 TEST_F(DefaultSearchPrefMigrationTest, MigrateOnlyOnce) {
168 scoped_ptr<TemplateURL> t_url(
169 CreateKeyword("name1", "key1", "http://foo1/{searchTerms}"));
170 // Store a value in the legacy location.
171 SaveDefaultSearchProviderToLegacyPrefs(t_url.get());
173 // Run the migration.
174 ConfigureDefaultSearchPrefMigrationToDictionaryValue(profile()->GetPrefs());
176 // Test that it was migrated.
177 DefaultSearchManager::Source source;
178 const TemplateURLData* modern_default =
179 default_search_manager()->GetDefaultSearchEngine(&source);
180 ASSERT_TRUE(modern_default);
181 EXPECT_EQ(DefaultSearchManager::FROM_USER, source);
182 EXPECT_EQ(t_url->short_name(), modern_default->short_name);
183 EXPECT_EQ(t_url->keyword(), modern_default->keyword());
184 EXPECT_EQ(t_url->url(), modern_default->url());
185 default_search_manager()->ClearUserSelectedDefaultSearchEngine();
187 // Run the migration.
188 ConfigureDefaultSearchPrefMigrationToDictionaryValue(profile()->GetPrefs());
190 // Test that it was NOT migrated.
191 modern_default = default_search_manager()->GetDefaultSearchEngine(&source);
192 ASSERT_TRUE(modern_default);
193 EXPECT_EQ(DefaultSearchManager::FROM_FALLBACK, source);
196 TEST_F(DefaultSearchPrefMigrationTest, ModernValuePresent) {
197 scoped_ptr<TemplateURL> t_url(
198 CreateKeyword("name1", "key1", "http://foo1/{searchTerms}"));
199 scoped_ptr<TemplateURL> t_url2(
200 CreateKeyword("name2", "key2", "http://foo2/{searchTerms}"));
201 // Store a value in the legacy location.
202 SaveDefaultSearchProviderToLegacyPrefs(t_url.get());
204 // Store another value in the modern location.
205 default_search_manager()->SetUserSelectedDefaultSearchEngine(t_url2->data());
207 // Run the migration.
208 ConfigureDefaultSearchPrefMigrationToDictionaryValue(profile()->GetPrefs());
210 // Test that no migration occurred. The modern value is left intact.
211 DefaultSearchManager::Source source;
212 const TemplateURLData* modern_default =
213 default_search_manager()->GetDefaultSearchEngine(&source);
214 ASSERT_TRUE(modern_default);
215 EXPECT_EQ(DefaultSearchManager::FROM_USER, source);
216 EXPECT_EQ(t_url2->short_name(), modern_default->short_name);
217 EXPECT_EQ(t_url2->keyword(), modern_default->keyword());
218 EXPECT_EQ(t_url2->url(), modern_default->url());
221 TEST_F(DefaultSearchPrefMigrationTest,
222 AutomaticallySelectedValueIsNotMigrated) {
223 DefaultSearchManager::Source source;
224 TemplateURLData prepopulated_default(
225 *default_search_manager()->GetDefaultSearchEngine(&source));
226 EXPECT_EQ(DefaultSearchManager::FROM_FALLBACK, source);
228 TemplateURL prepopulated_turl(prepopulated_default);
230 // Store a value in the legacy location.
231 SaveDefaultSearchProviderToLegacyPrefs(&prepopulated_turl);
233 // Run the migration.
234 ConfigureDefaultSearchPrefMigrationToDictionaryValue(profile()->GetPrefs());
236 // Test that the legacy value is not migrated, as it is not user-selected.
237 default_search_manager()->GetDefaultSearchEngine(&source);
238 EXPECT_EQ(DefaultSearchManager::FROM_FALLBACK, source);