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

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: Restore DSP origin change metric. Restore some naming and ordering. 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"
12 #include "base/strings/utf_string_conversions.h" 15 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/search_engines/template_url.h" 16 #include "chrome/browser/search_engines/template_url.h"
14 #include "chrome/browser/search_engines/template_url_service.h" 17 #include "chrome/browser/search_engines/template_url_service.h"
15 #include "chrome/browser/search_engines/template_url_service_test_util.h"
16 #include "chrome/test/base/testing_pref_service_syncable.h" 18 #include "chrome/test/base/testing_pref_service_syncable.h"
17 #include "chrome/test/base/testing_profile.h" 19 #include "chrome/test/base/testing_profile.h"
18 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
19 21
20 class DefaultSearchPrefMigrationTest : public testing::Test { 22 class DefaultSearchPrefMigrationTest : public testing::Test {
21 public: 23 public:
22 DefaultSearchPrefMigrationTest(); 24 DefaultSearchPrefMigrationTest();
23 25
24 // testing::Test: 26 // testing::Test:
25 virtual void SetUp() OVERRIDE; 27 virtual void SetUp() OVERRIDE;
26 virtual void TearDown() OVERRIDE; 28 virtual void TearDown() OVERRIDE;
27 29
28 scoped_ptr<TemplateURL> CreateKeyword(const std::string& short_name, 30 scoped_ptr<TemplateURL> CreateKeyword(const std::string& short_name,
29 const std::string& keyword, 31 const std::string& keyword,
30 const std::string& url); 32 const std::string& url);
31 33
32 TemplateURLServiceTestUtil* test_util() { return &test_util_; } 34 TestingProfile* profile() { return profile_.get(); }
35
36 DefaultSearchManager* default_search_manager() {
37 return default_search_manager_.get();
38 }
33 39
34 private: 40 private:
35 TemplateURLServiceTestUtil test_util_; 41 base::ScopedTempDir temp_dir_;
42 scoped_ptr<TestingProfile> profile_;
43 scoped_ptr<DefaultSearchManager> default_search_manager_;
36 44
37 DISALLOW_COPY_AND_ASSIGN(DefaultSearchPrefMigrationTest); 45 DISALLOW_COPY_AND_ASSIGN(DefaultSearchPrefMigrationTest);
38 }; 46 };
39 47
40 DefaultSearchPrefMigrationTest::DefaultSearchPrefMigrationTest() { 48 DefaultSearchPrefMigrationTest::DefaultSearchPrefMigrationTest() {
41 } 49 }
42 50
43 void DefaultSearchPrefMigrationTest::SetUp() { 51 void DefaultSearchPrefMigrationTest::SetUp() {
44 test_util_.SetUp(); 52 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
53 profile_.reset(new TestingProfile(temp_dir_.path()));
54 default_search_manager_.reset(new DefaultSearchManager(
55 profile_->GetPrefs(), DefaultSearchManager::ObserverCallback()));
45 } 56 }
46 57
47 void DefaultSearchPrefMigrationTest::TearDown() { 58 void DefaultSearchPrefMigrationTest::TearDown() {
48 test_util_.TearDown(); 59 default_search_manager_.reset();
Peter Kasting 2014/05/08 22:46:46 Nit: Comment why we need to explicitly delete thes
60 profile_.reset();
49 } 61 }
50 62
51 scoped_ptr<TemplateURL> DefaultSearchPrefMigrationTest::CreateKeyword( 63 scoped_ptr<TemplateURL> DefaultSearchPrefMigrationTest::CreateKeyword(
52 const std::string& short_name, 64 const std::string& short_name,
53 const std::string& keyword, 65 const std::string& keyword,
54 const std::string& url) { 66 const std::string& url) {
55 TemplateURLData data; 67 TemplateURLData data;
56 data.short_name = base::ASCIIToUTF16(short_name); 68 data.short_name = base::ASCIIToUTF16(short_name);
57 data.SetKeyword(base::ASCIIToUTF16(keyword)); 69 data.SetKeyword(base::ASCIIToUTF16(keyword));
58 data.SetURL(url); 70 data.SetURL(url);
59 scoped_ptr<TemplateURL> t_url(new TemplateURL(test_util_.profile(), data)); 71 scoped_ptr<TemplateURL> t_url(new TemplateURL(profile(), data));
60 return t_url.Pass(); 72 return t_url.Pass();
61 } 73 }
62 74
63 TEST_F(DefaultSearchPrefMigrationTest, MigrateUserSelectedValue) { 75 TEST_F(DefaultSearchPrefMigrationTest, MigrateUserSelectedValue) {
64 scoped_ptr<TemplateURL> t_url( 76 scoped_ptr<TemplateURL> t_url(
65 CreateKeyword("name1", "key1", "http://foo1/{searchTerms}")); 77 CreateKeyword("name1", "key1", "http://foo1/{searchTerms}"));
66 // Store a value in the legacy location. 78 // Store a value in the legacy location.
67 test_util()->model()->SaveDefaultSearchProviderToPrefs( 79 TemplateURLService::SaveDefaultSearchProviderToPrefs(t_url.get(),
68 t_url.get(), test_util()->profile()->GetPrefs()); 80 profile()->GetPrefs());
69 81
70 // Run the migration. 82 // Run the migration.
71 ConfigureDefaultSearchPrefMigrationToDictionaryValue( 83 ConfigureDefaultSearchPrefMigrationToDictionaryValue(profile()->GetPrefs());
72 test_util()->profile()->GetPrefs());
73 84
74 // Test that it was migrated. 85 // Test that it was migrated.
75 DefaultSearchManager manager(test_util()->profile()->GetPrefs(), 86 DefaultSearchManager::Source source;
76 DefaultSearchManager::ObserverCallback()); 87 const TemplateURLData* modern_default =
77 TemplateURLData* modern_default = manager.GetDefaultSearchEngine(NULL); 88 default_search_manager()->GetDefaultSearchEngine(&source);
78 ASSERT_TRUE(modern_default); 89 ASSERT_TRUE(modern_default);
90 EXPECT_EQ(DefaultSearchManager::FROM_USER, source);
79 EXPECT_EQ(t_url->short_name(), modern_default->short_name); 91 EXPECT_EQ(t_url->short_name(), modern_default->short_name);
80 EXPECT_EQ(t_url->keyword(), modern_default->keyword()); 92 EXPECT_EQ(t_url->keyword(), modern_default->keyword());
81 EXPECT_EQ(t_url->url(), modern_default->url()); 93 EXPECT_EQ(t_url->url(), modern_default->url());
82 } 94 }
83 95
96 TEST_F(DefaultSearchPrefMigrationTest, MigrateOnlyOnce) {
97 scoped_ptr<TemplateURL> t_url(
98 CreateKeyword("name1", "key1", "http://foo1/{searchTerms}"));
99 // Store a value in the legacy location.
100 TemplateURLService::SaveDefaultSearchProviderToPrefs(t_url.get(),
101 profile()->GetPrefs());
102
103 // Run the migration.
104 ConfigureDefaultSearchPrefMigrationToDictionaryValue(profile()->GetPrefs());
105
106 // Test that it was migrated.
107 DefaultSearchManager::Source source;
108 const TemplateURLData* modern_default =
109 default_search_manager()->GetDefaultSearchEngine(&source);
110 ASSERT_TRUE(modern_default);
111 EXPECT_EQ(DefaultSearchManager::FROM_USER, source);
112 EXPECT_EQ(t_url->short_name(), modern_default->short_name);
113 EXPECT_EQ(t_url->keyword(), modern_default->keyword());
114 EXPECT_EQ(t_url->url(), modern_default->url());
115 default_search_manager()->ClearUserSelectedDefaultSearchEngine();
116
117 // Run the migration.
118 ConfigureDefaultSearchPrefMigrationToDictionaryValue(profile()->GetPrefs());
119
120 // Test that it was NOT migrated.
121 modern_default = default_search_manager()->GetDefaultSearchEngine(&source);
122 ASSERT_TRUE(modern_default);
123 EXPECT_EQ(DefaultSearchManager::FROM_FALLBACK, source);
124 }
125
84 TEST_F(DefaultSearchPrefMigrationTest, ModernValuePresent) { 126 TEST_F(DefaultSearchPrefMigrationTest, ModernValuePresent) {
85 scoped_ptr<TemplateURL> t_url( 127 scoped_ptr<TemplateURL> t_url(
86 CreateKeyword("name1", "key1", "http://foo1/{searchTerms}")); 128 CreateKeyword("name1", "key1", "http://foo1/{searchTerms}"));
87 scoped_ptr<TemplateURL> t_url2( 129 scoped_ptr<TemplateURL> t_url2(
88 CreateKeyword("name2", "key2", "http://foo2/{searchTerms}")); 130 CreateKeyword("name2", "key2", "http://foo2/{searchTerms}"));
89 // Store a value in the legacy location. 131 // Store a value in the legacy location.
90 test_util()->model()->SaveDefaultSearchProviderToPrefs( 132 TemplateURLService::SaveDefaultSearchProviderToPrefs(t_url.get(),
91 t_url.get(), test_util()->profile()->GetPrefs()); 133 profile()->GetPrefs());
92 134
93 // Store another value in the modern location. 135 // Store another value in the modern location.
94 DefaultSearchManager(test_util()->profile()->GetPrefs(), 136 default_search_manager()->SetUserSelectedDefaultSearchEngine(t_url2->data());
95 DefaultSearchManager::ObserverCallback())
96 .SetUserSelectedDefaultSearchEngine(t_url2->data());
97 137
98 // Run the migration. 138 // Run the migration.
99 ConfigureDefaultSearchPrefMigrationToDictionaryValue( 139 ConfigureDefaultSearchPrefMigrationToDictionaryValue(profile()->GetPrefs());
100 test_util()->profile()->GetPrefs());
101 140
102 // Test that no migration occurred. The modern value is left intact. 141 // Test that no migration occurred. The modern value is left intact.
103 DefaultSearchManager manager(test_util()->profile()->GetPrefs(), 142 DefaultSearchManager::Source source;
104 DefaultSearchManager::ObserverCallback()); 143 const TemplateURLData* modern_default =
105 TemplateURLData* modern_default = manager.GetDefaultSearchEngine(NULL); 144 default_search_manager()->GetDefaultSearchEngine(&source);
106 ASSERT_TRUE(modern_default); 145 ASSERT_TRUE(modern_default);
146 EXPECT_EQ(DefaultSearchManager::FROM_USER, source);
107 EXPECT_EQ(t_url2->short_name(), modern_default->short_name); 147 EXPECT_EQ(t_url2->short_name(), modern_default->short_name);
108 EXPECT_EQ(t_url2->keyword(), modern_default->keyword()); 148 EXPECT_EQ(t_url2->keyword(), modern_default->keyword());
109 EXPECT_EQ(t_url2->url(), modern_default->url()); 149 EXPECT_EQ(t_url2->url(), modern_default->url());
110 } 150 }
111 151
112 TEST_F(DefaultSearchPrefMigrationTest, 152 TEST_F(DefaultSearchPrefMigrationTest,
113 AutomaticallySelectedValueIsNotMigrated) { 153 AutomaticallySelectedValueIsNotMigrated) {
114 test_util()->VerifyLoad(); 154 DefaultSearchManager::Source source;
115 scoped_ptr<TemplateURLData> legacy_default; 155 TemplateURLData prepopulated_default(
116 bool legacy_is_managed = false; 156 *default_search_manager()->GetDefaultSearchEngine(&source));
117 // The initialization of the TemplateURLService will have stored the 157 EXPECT_EQ(DefaultSearchManager::FROM_FALLBACK, source);
118 // pre-populated DSE in the legacy location in prefs. 158
119 ASSERT_TRUE(TemplateURLService::LoadDefaultSearchProviderFromPrefs( 159 TemplateURL prepopulated_turl(profile(), prepopulated_default);
120 test_util()->profile()->GetPrefs(), &legacy_default, &legacy_is_managed)); 160
121 EXPECT_FALSE(legacy_is_managed); 161 // Store a value in the legacy location.
122 EXPECT_TRUE(legacy_default); 162 TemplateURLService::SaveDefaultSearchProviderToPrefs(&prepopulated_turl,
123 EXPECT_GT(legacy_default->prepopulate_id, 0); 163 profile()->GetPrefs());
124 164
125 // Run the migration. 165 // Run the migration.
126 ConfigureDefaultSearchPrefMigrationToDictionaryValue( 166 ConfigureDefaultSearchPrefMigrationToDictionaryValue(profile()->GetPrefs());
127 test_util()->profile()->GetPrefs());
128 167
129 // Test that the legacy value is not migrated, as it is not user-selected. 168 // Test that the legacy value is not migrated, as it is not user-selected.
130 ASSERT_EQ(DefaultSearchManager::FROM_FALLBACK, 169 default_search_manager()->GetDefaultSearchEngine(&source);
131 DefaultSearchManager(test_util()->profile()->GetPrefs(), 170 EXPECT_EQ(DefaultSearchManager::FROM_FALLBACK, source);
132 DefaultSearchManager::ObserverCallback())
133 .GetDefaultSearchEngineSource());
134 } 171 }
135
136 TEST_F(DefaultSearchPrefMigrationTest, ManagedValueIsNotMigrated) {
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