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