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

Side by Side Diff: chrome/browser/search_engines/default_search_pref_migration_unittest.cc

Issue 268643002: Use the DefaultSearchManager as the exclusive authority on DSE, ignoring Web Data. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix merge errors. Created 6 years, 7 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 | Annotate | Revision Log
OLDNEW
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
54
gab 2014/05/07 20:25:09 nit: rm empty lines.
erikwright (departed) 2014/05/07 22:26:47 Done.
55
40 DefaultSearchPrefMigrationTest::DefaultSearchPrefMigrationTest() { 56 DefaultSearchPrefMigrationTest::DefaultSearchPrefMigrationTest() {
41 } 57 }
42 58
43 void DefaultSearchPrefMigrationTest::SetUp() { 59 void DefaultSearchPrefMigrationTest::SetUp() {
44 test_util_.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()));
45 } 64 }
46 65
47 void DefaultSearchPrefMigrationTest::TearDown() { 66 void DefaultSearchPrefMigrationTest::TearDown() {
48 test_util_.TearDown(); 67 default_search_manager_.reset();
68 profile_.reset();
69 }
70
71 void DefaultSearchPrefMigrationTest::SaveDefaultSearchProviderToLegacyPrefs(
72 const TemplateURL* t_url) {
73 PrefService* prefs = profile()->GetPrefs();
74
75 bool enabled = false;
76 std::string search_url;
77 std::string suggest_url;
78 std::string instant_url;
79 std::string image_url;
80 std::string new_tab_url;
81 std::string search_url_post_params;
82 std::string suggest_url_post_params;
83 std::string instant_url_post_params;
84 std::string image_url_post_params;
85 std::string icon_url;
86 std::string encodings;
87 std::string short_name;
88 std::string keyword;
89 std::string id_string;
90 std::string prepopulate_id;
91 base::ListValue alternate_urls;
92 std::string search_terms_replacement_key;
93 if (t_url) {
94 DCHECK_EQ(TemplateURL::NORMAL, t_url->GetType());
95 enabled = true;
96 search_url = t_url->url();
97 suggest_url = t_url->suggestions_url();
98 instant_url = t_url->instant_url();
99 image_url = t_url->image_url();
100 new_tab_url = t_url->new_tab_url();
101 search_url_post_params = t_url->search_url_post_params();
102 suggest_url_post_params = t_url->suggestions_url_post_params();
103 instant_url_post_params = t_url->instant_url_post_params();
104 image_url_post_params = t_url->image_url_post_params();
105 GURL icon_gurl = t_url->favicon_url();
106 if (!icon_gurl.is_empty())
107 icon_url = icon_gurl.spec();
108 encodings = JoinString(t_url->input_encodings(), ';');
109 short_name = base::UTF16ToUTF8(t_url->short_name());
110 keyword = base::UTF16ToUTF8(t_url->keyword());
111 id_string = base::Int64ToString(t_url->id());
112 prepopulate_id = base::Int64ToString(t_url->prepopulate_id());
113 for (size_t i = 0; i < t_url->alternate_urls().size(); ++i)
114 alternate_urls.AppendString(t_url->alternate_urls()[i]);
115 search_terms_replacement_key = t_url->search_terms_replacement_key();
116 }
117 prefs->SetBoolean(prefs::kDefaultSearchProviderEnabled, enabled);
118 prefs->SetString(prefs::kDefaultSearchProviderSearchURL, search_url);
119 prefs->SetString(prefs::kDefaultSearchProviderSuggestURL, suggest_url);
120 prefs->SetString(prefs::kDefaultSearchProviderInstantURL, instant_url);
121 prefs->SetString(prefs::kDefaultSearchProviderImageURL, image_url);
122 prefs->SetString(prefs::kDefaultSearchProviderNewTabURL, new_tab_url);
123 prefs->SetString(prefs::kDefaultSearchProviderSearchURLPostParams,
124 search_url_post_params);
125 prefs->SetString(prefs::kDefaultSearchProviderSuggestURLPostParams,
126 suggest_url_post_params);
127 prefs->SetString(prefs::kDefaultSearchProviderInstantURLPostParams,
128 instant_url_post_params);
129 prefs->SetString(prefs::kDefaultSearchProviderImageURLPostParams,
130 image_url_post_params);
131 prefs->SetString(prefs::kDefaultSearchProviderIconURL, icon_url);
132 prefs->SetString(prefs::kDefaultSearchProviderEncodings, encodings);
133 prefs->SetString(prefs::kDefaultSearchProviderName, short_name);
134 prefs->SetString(prefs::kDefaultSearchProviderKeyword, keyword);
135 prefs->SetString(prefs::kDefaultSearchProviderID, id_string);
136 prefs->SetString(prefs::kDefaultSearchProviderPrepopulateID, prepopulate_id);
137 prefs->Set(prefs::kDefaultSearchProviderAlternateURLs, alternate_urls);
138 prefs->SetString(prefs::kDefaultSearchProviderSearchTermsReplacementKey,
139 search_terms_replacement_key);
49 } 140 }
50 141
51 scoped_ptr<TemplateURL> DefaultSearchPrefMigrationTest::CreateKeyword( 142 scoped_ptr<TemplateURL> DefaultSearchPrefMigrationTest::CreateKeyword(
52 const std::string& short_name, 143 const std::string& short_name,
53 const std::string& keyword, 144 const std::string& keyword,
54 const std::string& url) { 145 const std::string& url) {
55 TemplateURLData data; 146 TemplateURLData data;
56 data.short_name = base::ASCIIToUTF16(short_name); 147 data.short_name = base::ASCIIToUTF16(short_name);
57 data.SetKeyword(base::ASCIIToUTF16(keyword)); 148 data.SetKeyword(base::ASCIIToUTF16(keyword));
58 data.SetURL(url); 149 data.SetURL(url);
59 scoped_ptr<TemplateURL> t_url(new TemplateURL(test_util_.profile(), data)); 150 scoped_ptr<TemplateURL> t_url(new TemplateURL(profile(), data));
60 return t_url.Pass(); 151 return t_url.Pass();
61 } 152 }
62 153
63 TEST_F(DefaultSearchPrefMigrationTest, MigrateUserSelectedValue) { 154 TEST_F(DefaultSearchPrefMigrationTest, MigrateUserSelectedValue) {
64 scoped_ptr<TemplateURL> t_url( 155 scoped_ptr<TemplateURL> t_url(
65 CreateKeyword("name1", "key1", "http://foo1/{searchTerms}")); 156 CreateKeyword("name1", "key1", "http://foo1/{searchTerms}"));
66 // Store a value in the legacy location. 157 // Store a value in the legacy location.
67 test_util()->model()->SaveDefaultSearchProviderToPrefs( 158 SaveDefaultSearchProviderToLegacyPrefs(t_url.get());
68 t_url.get(), test_util()->profile()->GetPrefs());
69 159
70 // Run the migration. 160 // Run the migration.
71 ConfigureDefaultSearchPrefMigrationToDictionaryValue( 161 ConfigureDefaultSearchPrefMigrationToDictionaryValue(profile()->GetPrefs());
72 test_util()->profile()->GetPrefs());
73 162
74 // Test that it was migrated. 163 // Test that it was migrated.
75 DefaultSearchManager manager(test_util()->profile()->GetPrefs(), 164 DefaultSearchManager::Source source;
76 DefaultSearchManager::ObserverCallback()); 165 const TemplateURLData* modern_default =
77 TemplateURLData* modern_default = manager.GetDefaultSearchEngine(NULL); 166 default_search_manager()->GetDefaultSearchEngine(&source);
78 ASSERT_TRUE(modern_default); 167 ASSERT_TRUE(modern_default);
168 EXPECT_EQ(DefaultSearchManager::FROM_USER, source);
79 EXPECT_EQ(t_url->short_name(), modern_default->short_name); 169 EXPECT_EQ(t_url->short_name(), modern_default->short_name);
80 EXPECT_EQ(t_url->keyword(), modern_default->keyword()); 170 EXPECT_EQ(t_url->keyword(), modern_default->keyword());
81 EXPECT_EQ(t_url->url(), modern_default->url()); 171 EXPECT_EQ(t_url->url(), modern_default->url());
82 } 172 }
83 173
174 TEST_F(DefaultSearchPrefMigrationTest, MigrateOnlyOnce) {
175 scoped_ptr<TemplateURL> t_url(
176 CreateKeyword("name1", "key1", "http://foo1/{searchTerms}"));
177 // Store a value in the legacy location.
178 SaveDefaultSearchProviderToLegacyPrefs(t_url.get());
179
180 // Run the migration.
181 ConfigureDefaultSearchPrefMigrationToDictionaryValue(profile()->GetPrefs());
182
183 // Test that it was migrated.
184 DefaultSearchManager::Source source;
185 const TemplateURLData* modern_default =
186 default_search_manager()->GetDefaultSearchEngine(&source);
187 ASSERT_TRUE(modern_default);
188 EXPECT_EQ(DefaultSearchManager::FROM_USER, source);
189 EXPECT_EQ(t_url->short_name(), modern_default->short_name);
190 EXPECT_EQ(t_url->keyword(), modern_default->keyword());
191 EXPECT_EQ(t_url->url(), modern_default->url());
192 default_search_manager()->ClearUserSelectedDefaultSearchEngine();
193
194 // Run the migration.
195 ConfigureDefaultSearchPrefMigrationToDictionaryValue(profile()->GetPrefs());
196
197 // Test that it was NOT migrated.
198 modern_default = default_search_manager()->GetDefaultSearchEngine(&source);
199 ASSERT_TRUE(modern_default);
200 EXPECT_EQ(DefaultSearchManager::FROM_FALLBACK, source);
201 }
202
84 TEST_F(DefaultSearchPrefMigrationTest, ModernValuePresent) { 203 TEST_F(DefaultSearchPrefMigrationTest, ModernValuePresent) {
85 scoped_ptr<TemplateURL> t_url( 204 scoped_ptr<TemplateURL> t_url(
86 CreateKeyword("name1", "key1", "http://foo1/{searchTerms}")); 205 CreateKeyword("name1", "key1", "http://foo1/{searchTerms}"));
87 scoped_ptr<TemplateURL> t_url2( 206 scoped_ptr<TemplateURL> t_url2(
88 CreateKeyword("name2", "key2", "http://foo2/{searchTerms}")); 207 CreateKeyword("name2", "key2", "http://foo2/{searchTerms}"));
89 // Store a value in the legacy location. 208 // Store a value in the legacy location.
90 test_util()->model()->SaveDefaultSearchProviderToPrefs( 209 SaveDefaultSearchProviderToLegacyPrefs(t_url.get());
91 t_url.get(), test_util()->profile()->GetPrefs());
92 210
93 // Store another value in the modern location. 211 // Store another value in the modern location.
94 DefaultSearchManager(test_util()->profile()->GetPrefs(), 212 default_search_manager()->SetUserSelectedDefaultSearchEngine(t_url2->data());
95 DefaultSearchManager::ObserverCallback())
96 .SetUserSelectedDefaultSearchEngine(t_url2->data());
97 213
98 // Run the migration. 214 // Run the migration.
99 ConfigureDefaultSearchPrefMigrationToDictionaryValue( 215 ConfigureDefaultSearchPrefMigrationToDictionaryValue(profile()->GetPrefs());
100 test_util()->profile()->GetPrefs());
101 216
102 // Test that no migration occurred. The modern value is left intact. 217 // Test that no migration occurred. The modern value is left intact.
103 DefaultSearchManager manager(test_util()->profile()->GetPrefs(), 218 DefaultSearchManager::Source source;
104 DefaultSearchManager::ObserverCallback()); 219 const TemplateURLData* modern_default =
105 TemplateURLData* modern_default = manager.GetDefaultSearchEngine(NULL); 220 default_search_manager()->GetDefaultSearchEngine(&source);
106 ASSERT_TRUE(modern_default); 221 ASSERT_TRUE(modern_default);
222 EXPECT_EQ(DefaultSearchManager::FROM_USER, source);
107 EXPECT_EQ(t_url2->short_name(), modern_default->short_name); 223 EXPECT_EQ(t_url2->short_name(), modern_default->short_name);
108 EXPECT_EQ(t_url2->keyword(), modern_default->keyword()); 224 EXPECT_EQ(t_url2->keyword(), modern_default->keyword());
109 EXPECT_EQ(t_url2->url(), modern_default->url()); 225 EXPECT_EQ(t_url2->url(), modern_default->url());
110 } 226 }
111 227
112 TEST_F(DefaultSearchPrefMigrationTest, 228 TEST_F(DefaultSearchPrefMigrationTest,
113 AutomaticallySelectedValueIsNotMigrated) { 229 AutomaticallySelectedValueIsNotMigrated) {
114 test_util()->VerifyLoad(); 230 DefaultSearchManager::Source source;
115 scoped_ptr<TemplateURLData> legacy_default; 231 TemplateURLData prepopulated_default(
116 bool legacy_is_managed = false; 232 *default_search_manager()->GetDefaultSearchEngine(&source));
117 // The initialization of the TemplateURLService will have stored the 233 EXPECT_EQ(DefaultSearchManager::FROM_FALLBACK, source);
118 // pre-populated DSE in the legacy location in prefs. 234
119 ASSERT_TRUE(TemplateURLService::LoadDefaultSearchProviderFromPrefs( 235 TemplateURL prepopulated_turl(profile(), prepopulated_default);
120 test_util()->profile()->GetPrefs(), &legacy_default, &legacy_is_managed)); 236
121 EXPECT_FALSE(legacy_is_managed); 237 // Store a value in the legacy location.
122 EXPECT_TRUE(legacy_default); 238 SaveDefaultSearchProviderToLegacyPrefs(&prepopulated_turl);
123 EXPECT_GT(legacy_default->prepopulate_id, 0);
124 239
125 // Run the migration. 240 // Run the migration.
126 ConfigureDefaultSearchPrefMigrationToDictionaryValue( 241 ConfigureDefaultSearchPrefMigrationToDictionaryValue(profile()->GetPrefs());
127 test_util()->profile()->GetPrefs());
128 242
129 // Test that the legacy value is not migrated, as it is not user-selected. 243 // Test that the legacy value is not migrated, as it is not user-selected.
130 ASSERT_EQ(DefaultSearchManager::FROM_FALLBACK, 244 default_search_manager()->GetDefaultSearchEngine(&source);
131 DefaultSearchManager(test_util()->profile()->GetPrefs(), 245 EXPECT_EQ(DefaultSearchManager::FROM_FALLBACK, source);
132 DefaultSearchManager::ObserverCallback())
133 .GetDefaultSearchEngineSource());
134 } 246 }
135
136 TEST_F(DefaultSearchPrefMigrationTest, ManagedValueIsNotMigrated) {
gab 2014/05/07 20:25:09 Why is this test removed? Shouldn't this still be
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 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698