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 "components/search_engines/template_url_prepopulate_data.h" | 5 #include "components/search_engines/template_url_prepopulate_data.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <utility> | 10 #include <utility> |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 unique_ids.end()); | 113 unique_ids.end()); |
114 unique_ids.insert(urls[turl_i]->prepopulate_id); | 114 unique_ids.insert(urls[turl_i]->prepopulate_id); |
115 } | 115 } |
116 } | 116 } |
117 } | 117 } |
118 | 118 |
119 // Verifies that default search providers from the preferences file | 119 // Verifies that default search providers from the preferences file |
120 // override the built-in ones. | 120 // override the built-in ones. |
121 TEST_F(TemplateURLPrepopulateDataTest, ProvidersFromPrefs) { | 121 TEST_F(TemplateURLPrepopulateDataTest, ProvidersFromPrefs) { |
122 prefs_.SetUserPref(prefs::kSearchProviderOverridesVersion, | 122 prefs_.SetUserPref(prefs::kSearchProviderOverridesVersion, |
123 new base::Value(1)); | 123 base::MakeUnique<base::Value>(1)); |
124 base::ListValue* overrides = new base::ListValue; | 124 auto overrides = base::MakeUnique<base::ListValue>(); |
125 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue); | 125 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue); |
126 // Set only the minimal required settings for a search provider configuration. | 126 // Set only the minimal required settings for a search provider configuration. |
127 entry->SetString("name", "foo"); | 127 entry->SetString("name", "foo"); |
128 entry->SetString("keyword", "fook"); | 128 entry->SetString("keyword", "fook"); |
129 entry->SetString("search_url", "http://foo.com/s?q={searchTerms}"); | 129 entry->SetString("search_url", "http://foo.com/s?q={searchTerms}"); |
130 entry->SetString("favicon_url", "http://foi.com/favicon.ico"); | 130 entry->SetString("favicon_url", "http://foi.com/favicon.ico"); |
131 entry->SetString("encoding", "UTF-8"); | 131 entry->SetString("encoding", "UTF-8"); |
132 entry->SetInteger("id", 1001); | 132 entry->SetInteger("id", 1001); |
133 overrides->Append(entry->CreateDeepCopy()); | 133 overrides->Append(entry->CreateDeepCopy()); |
134 prefs_.SetUserPref(prefs::kSearchProviderOverrides, overrides); | 134 prefs_.SetUserPref(prefs::kSearchProviderOverrides, std::move(overrides)); |
135 | 135 |
136 int version = TemplateURLPrepopulateData::GetDataVersion(&prefs_); | 136 int version = TemplateURLPrepopulateData::GetDataVersion(&prefs_); |
137 EXPECT_EQ(1, version); | 137 EXPECT_EQ(1, version); |
138 | 138 |
139 size_t default_index; | 139 size_t default_index; |
140 std::vector<std::unique_ptr<TemplateURLData>> t_urls = | 140 std::vector<std::unique_ptr<TemplateURLData>> t_urls = |
141 TemplateURLPrepopulateData::GetPrepopulatedEngines(&prefs_, | 141 TemplateURLPrepopulateData::GetPrepopulatedEngines(&prefs_, |
142 &default_index); | 142 &default_index); |
143 | 143 |
144 ASSERT_EQ(1u, t_urls.size()); | 144 ASSERT_EQ(1u, t_urls.size()); |
(...skipping 11 matching lines...) Expand all Loading... |
156 EXPECT_TRUE(t_urls[0]->date_created.is_null()); | 156 EXPECT_TRUE(t_urls[0]->date_created.is_null()); |
157 EXPECT_TRUE(t_urls[0]->last_modified.is_null()); | 157 EXPECT_TRUE(t_urls[0]->last_modified.is_null()); |
158 | 158 |
159 // Test the optional settings too. | 159 // Test the optional settings too. |
160 entry->SetString("suggest_url", "http://foo.com/suggest?q={searchTerms}"); | 160 entry->SetString("suggest_url", "http://foo.com/suggest?q={searchTerms}"); |
161 entry->SetString("instant_url", "http://foo.com/instant?q={searchTerms}"); | 161 entry->SetString("instant_url", "http://foo.com/instant?q={searchTerms}"); |
162 base::ListValue* alternate_urls = new base::ListValue; | 162 base::ListValue* alternate_urls = new base::ListValue; |
163 alternate_urls->AppendString("http://foo.com/alternate?q={searchTerms}"); | 163 alternate_urls->AppendString("http://foo.com/alternate?q={searchTerms}"); |
164 entry->Set("alternate_urls", alternate_urls); | 164 entry->Set("alternate_urls", alternate_urls); |
165 entry->SetString("search_terms_replacement_key", "espv"); | 165 entry->SetString("search_terms_replacement_key", "espv"); |
166 overrides = new base::ListValue; | 166 overrides = base::MakeUnique<base::ListValue>(); |
167 overrides->Append(entry->CreateDeepCopy()); | 167 overrides->Append(entry->CreateDeepCopy()); |
168 prefs_.SetUserPref(prefs::kSearchProviderOverrides, overrides); | 168 prefs_.SetUserPref(prefs::kSearchProviderOverrides, std::move(overrides)); |
169 | 169 |
170 t_urls = TemplateURLPrepopulateData::GetPrepopulatedEngines( | 170 t_urls = TemplateURLPrepopulateData::GetPrepopulatedEngines( |
171 &prefs_, &default_index); | 171 &prefs_, &default_index); |
172 ASSERT_EQ(1u, t_urls.size()); | 172 ASSERT_EQ(1u, t_urls.size()); |
173 EXPECT_EQ(ASCIIToUTF16("foo"), t_urls[0]->short_name()); | 173 EXPECT_EQ(ASCIIToUTF16("foo"), t_urls[0]->short_name()); |
174 EXPECT_EQ(ASCIIToUTF16("fook"), t_urls[0]->keyword()); | 174 EXPECT_EQ(ASCIIToUTF16("fook"), t_urls[0]->keyword()); |
175 EXPECT_EQ("foo.com", GetHostFromTemplateURLData(*t_urls[0])); | 175 EXPECT_EQ("foo.com", GetHostFromTemplateURLData(*t_urls[0])); |
176 EXPECT_EQ("foi.com", t_urls[0]->favicon_url.host()); | 176 EXPECT_EQ("foi.com", t_urls[0]->favicon_url.host()); |
177 EXPECT_EQ(1u, t_urls[0]->input_encodings.size()); | 177 EXPECT_EQ(1u, t_urls[0]->input_encodings.size()); |
178 EXPECT_EQ(1001, t_urls[0]->prepopulate_id); | 178 EXPECT_EQ(1001, t_urls[0]->prepopulate_id); |
179 EXPECT_EQ("http://foo.com/suggest?q={searchTerms}", | 179 EXPECT_EQ("http://foo.com/suggest?q={searchTerms}", |
180 t_urls[0]->suggestions_url); | 180 t_urls[0]->suggestions_url); |
181 EXPECT_EQ("http://foo.com/instant?q={searchTerms}", | 181 EXPECT_EQ("http://foo.com/instant?q={searchTerms}", |
182 t_urls[0]->instant_url); | 182 t_urls[0]->instant_url); |
183 ASSERT_EQ(1u, t_urls[0]->alternate_urls.size()); | 183 ASSERT_EQ(1u, t_urls[0]->alternate_urls.size()); |
184 EXPECT_EQ("http://foo.com/alternate?q={searchTerms}", | 184 EXPECT_EQ("http://foo.com/alternate?q={searchTerms}", |
185 t_urls[0]->alternate_urls[0]); | 185 t_urls[0]->alternate_urls[0]); |
186 EXPECT_EQ("espv", t_urls[0]->search_terms_replacement_key); | 186 EXPECT_EQ("espv", t_urls[0]->search_terms_replacement_key); |
187 | 187 |
188 // Test that subsequent providers are loaded even if an intermediate | 188 // Test that subsequent providers are loaded even if an intermediate |
189 // provider has an incomplete configuration. | 189 // provider has an incomplete configuration. |
190 overrides = new base::ListValue; | 190 overrides = base::MakeUnique<base::ListValue>(); |
191 overrides->Append(entry->CreateDeepCopy()); | 191 overrides->Append(entry->CreateDeepCopy()); |
192 entry->SetInteger("id", 1002); | 192 entry->SetInteger("id", 1002); |
193 entry->SetString("name", "bar"); | 193 entry->SetString("name", "bar"); |
194 entry->SetString("keyword", "bark"); | 194 entry->SetString("keyword", "bark"); |
195 entry->SetString("encoding", std::string()); | 195 entry->SetString("encoding", std::string()); |
196 overrides->Append(entry->CreateDeepCopy()); | 196 overrides->Append(entry->CreateDeepCopy()); |
197 entry->SetInteger("id", 1003); | 197 entry->SetInteger("id", 1003); |
198 entry->SetString("name", "baz"); | 198 entry->SetString("name", "baz"); |
199 entry->SetString("keyword", "bazk"); | 199 entry->SetString("keyword", "bazk"); |
200 entry->SetString("encoding", "UTF-8"); | 200 entry->SetString("encoding", "UTF-8"); |
201 overrides->Append(entry->CreateDeepCopy()); | 201 overrides->Append(entry->CreateDeepCopy()); |
202 prefs_.SetUserPref(prefs::kSearchProviderOverrides, overrides); | 202 prefs_.SetUserPref(prefs::kSearchProviderOverrides, std::move(overrides)); |
203 | 203 |
204 t_urls = | 204 t_urls = |
205 TemplateURLPrepopulateData::GetPrepopulatedEngines(&prefs_, | 205 TemplateURLPrepopulateData::GetPrepopulatedEngines(&prefs_, |
206 &default_index); | 206 &default_index); |
207 EXPECT_EQ(2u, t_urls.size()); | 207 EXPECT_EQ(2u, t_urls.size()); |
208 } | 208 } |
209 | 209 |
210 TEST_F(TemplateURLPrepopulateDataTest, ClearProvidersFromPrefs) { | 210 TEST_F(TemplateURLPrepopulateDataTest, ClearProvidersFromPrefs) { |
211 prefs_.SetUserPref(prefs::kSearchProviderOverridesVersion, | 211 prefs_.SetUserPref(prefs::kSearchProviderOverridesVersion, |
212 new base::Value(1)); | 212 base::MakeUnique<base::Value>(1)); |
213 base::ListValue* overrides = new base::ListValue; | 213 auto overrides = base::MakeUnique<base::ListValue>(); |
214 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue); | 214 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue); |
215 // Set only the minimal required settings for a search provider configuration. | 215 // Set only the minimal required settings for a search provider configuration. |
216 entry->SetString("name", "foo"); | 216 entry->SetString("name", "foo"); |
217 entry->SetString("keyword", "fook"); | 217 entry->SetString("keyword", "fook"); |
218 entry->SetString("search_url", "http://foo.com/s?q={searchTerms}"); | 218 entry->SetString("search_url", "http://foo.com/s?q={searchTerms}"); |
219 entry->SetString("favicon_url", "http://foi.com/favicon.ico"); | 219 entry->SetString("favicon_url", "http://foi.com/favicon.ico"); |
220 entry->SetString("encoding", "UTF-8"); | 220 entry->SetString("encoding", "UTF-8"); |
221 entry->SetInteger("id", 1001); | 221 entry->SetInteger("id", 1001); |
222 overrides->Append(std::move(entry)); | 222 overrides->Append(std::move(entry)); |
223 prefs_.SetUserPref(prefs::kSearchProviderOverrides, overrides); | 223 prefs_.SetUserPref(prefs::kSearchProviderOverrides, std::move(overrides)); |
224 | 224 |
225 int version = TemplateURLPrepopulateData::GetDataVersion(&prefs_); | 225 int version = TemplateURLPrepopulateData::GetDataVersion(&prefs_); |
226 EXPECT_EQ(1, version); | 226 EXPECT_EQ(1, version); |
227 | 227 |
228 // This call removes the above search engine. | 228 // This call removes the above search engine. |
229 TemplateURLPrepopulateData::ClearPrepopulatedEnginesInPrefs(&prefs_); | 229 TemplateURLPrepopulateData::ClearPrepopulatedEnginesInPrefs(&prefs_); |
230 | 230 |
231 version = TemplateURLPrepopulateData::GetDataVersion(&prefs_); | 231 version = TemplateURLPrepopulateData::GetDataVersion(&prefs_); |
232 EXPECT_EQ(TemplateURLPrepopulateData::kCurrentDataVersion, version); | 232 EXPECT_EQ(TemplateURLPrepopulateData::kCurrentDataVersion, version); |
233 | 233 |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
360 using PrepopulatedEngine = TemplateURLPrepopulateData::PrepopulatedEngine; | 360 using PrepopulatedEngine = TemplateURLPrepopulateData::PrepopulatedEngine; |
361 const std::vector<const PrepopulatedEngine*> all_engines = | 361 const std::vector<const PrepopulatedEngine*> all_engines = |
362 TemplateURLPrepopulateData::GetAllPrepopulatedEngines(); | 362 TemplateURLPrepopulateData::GetAllPrepopulatedEngines(); |
363 for (const PrepopulatedEngine* engine : all_engines) { | 363 for (const PrepopulatedEngine* engine : all_engines) { |
364 std::unique_ptr<TemplateURLData> data = | 364 std::unique_ptr<TemplateURLData> data = |
365 TemplateURLDataFromPrepopulatedEngine(*engine); | 365 TemplateURLDataFromPrepopulatedEngine(*engine); |
366 EXPECT_EQ(engine->type, | 366 EXPECT_EQ(engine->type, |
367 TemplateURL(*data).GetEngineType(SearchTermsData())); | 367 TemplateURL(*data).GetEngineType(SearchTermsData())); |
368 } | 368 } |
369 } | 369 } |
OLD | NEW |