OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/bind_helpers.h" | 6 #include "base/bind_helpers.h" |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "base/memory/scoped_vector.h" | 9 #include "base/memory/scoped_vector.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
180 Time last_modified); | 180 Time last_modified); |
181 | 181 |
182 // Verifies the two TemplateURLs are equal. | 182 // Verifies the two TemplateURLs are equal. |
183 void AssertEquals(const TemplateURL& expected, const TemplateURL& actual); | 183 void AssertEquals(const TemplateURL& expected, const TemplateURL& actual); |
184 | 184 |
185 // Create an URL that appears to have been prepopulated, but won't be in the | 185 // Create an URL that appears to have been prepopulated, but won't be in the |
186 // current data. The caller owns the returned TemplateURL*. | 186 // current data. The caller owns the returned TemplateURL*. |
187 TemplateURL* CreatePreloadedTemplateURL(bool safe_for_autoreplace, | 187 TemplateURL* CreatePreloadedTemplateURL(bool safe_for_autoreplace, |
188 int prepopulate_id); | 188 int prepopulate_id); |
189 | 189 |
190 // Creates a TemplateURL with the same prepopulated id as a real prepopulated | |
191 // item. The input number determines which prepopulated item. The caller is | |
192 // responsible for owning the returned TemplateURL*. | |
193 TemplateURL* CreateReplaceablePreloadedTemplateURL( | |
194 bool safe_for_autoreplace, | |
195 size_t index_offset_from_default, | |
196 base::string16* prepopulated_display_url); | |
197 | |
198 // Verifies the behavior of when a preloaded url later gets changed. | |
199 // Since the input is the offset from the default, when one passes in | |
200 // 0, it tests the default. Passing in a number > 0 will verify what | |
201 // happens when a preloaded url that is not the default gets updated. | |
202 void TestLoadUpdatingPreloadedURL(size_t index_offset_from_default); | |
203 | |
204 // Helper methods to make calling TemplateURLServiceTestUtil methods less | 190 // Helper methods to make calling TemplateURLServiceTestUtil methods less |
205 // visually noisy in the test code. | 191 // visually noisy in the test code. |
206 void VerifyObserverCount(int expected_changed_count); | 192 void VerifyObserverCount(int expected_changed_count); |
207 void VerifyObserverFired(); | 193 void VerifyObserverFired(); |
208 TemplateURLService* model() { return test_util_.model(); } | 194 TemplateURLService* model() { return test_util_.model(); } |
209 | 195 |
210 protected: | 196 protected: |
211 TemplateURLServiceTestUtil test_util_; | 197 TemplateURLServiceTestUtil test_util_; |
212 | 198 |
213 void TestGenerateSearchURL(SearchTermsData* search_terms_data) { | 199 void TestGenerateSearchURL(SearchTermsData* search_terms_data) { |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
299 data.favicon_url = GURL("http://favicon.url"); | 285 data.favicon_url = GURL("http://favicon.url"); |
300 data.show_in_default_list = true; | 286 data.show_in_default_list = true; |
301 data.safe_for_autoreplace = safe_for_autoreplace; | 287 data.safe_for_autoreplace = safe_for_autoreplace; |
302 data.input_encodings.push_back("UTF-8"); | 288 data.input_encodings.push_back("UTF-8"); |
303 data.date_created = Time::FromTimeT(100); | 289 data.date_created = Time::FromTimeT(100); |
304 data.last_modified = Time::FromTimeT(100); | 290 data.last_modified = Time::FromTimeT(100); |
305 data.prepopulate_id = prepopulate_id; | 291 data.prepopulate_id = prepopulate_id; |
306 return new TemplateURL(test_util_.profile(), data); | 292 return new TemplateURL(test_util_.profile(), data); |
307 } | 293 } |
308 | 294 |
309 TemplateURL* TemplateURLServiceTest::CreateReplaceablePreloadedTemplateURL( | |
310 bool safe_for_autoreplace, | |
311 size_t index_offset_from_default, | |
312 base::string16* prepopulated_display_url) { | |
313 size_t default_search_provider_index = 0; | |
314 ScopedVector<TemplateURLData> prepopulated_urls = | |
315 TemplateURLPrepopulateData::GetPrepopulatedEngines( | |
316 test_util_.profile()->GetPrefs(), &default_search_provider_index); | |
317 EXPECT_LT(index_offset_from_default, prepopulated_urls.size()); | |
318 size_t prepopulated_index = (default_search_provider_index + | |
319 index_offset_from_default) % prepopulated_urls.size(); | |
320 TemplateURL* t_url = CreatePreloadedTemplateURL(safe_for_autoreplace, | |
321 prepopulated_urls[prepopulated_index]->prepopulate_id); | |
322 *prepopulated_display_url = | |
323 TemplateURL(NULL, *prepopulated_urls[prepopulated_index]).url_ref(). | |
324 DisplayURL(); | |
325 return t_url; | |
326 } | |
327 | |
328 void TemplateURLServiceTest::TestLoadUpdatingPreloadedURL( | |
329 size_t index_offset_from_default) { | |
330 base::string16 prepopulated_url; | |
331 TemplateURL* t_url = CreateReplaceablePreloadedTemplateURL(false, | |
332 index_offset_from_default, &prepopulated_url); | |
333 | |
334 base::string16 original_url = t_url->url_ref().DisplayURL(); | |
335 std::string original_guid = t_url->sync_guid(); | |
336 EXPECT_NE(prepopulated_url, original_url); | |
337 | |
338 // Then add it to the model and save it all. | |
339 test_util_.ChangeModelToLoadState(); | |
340 model()->Add(t_url); | |
341 const TemplateURL* keyword_url = | |
342 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")); | |
343 ASSERT_TRUE(keyword_url != NULL); | |
344 EXPECT_EQ(t_url, keyword_url); | |
345 EXPECT_EQ(original_url, keyword_url->url_ref().DisplayURL()); | |
346 base::RunLoop().RunUntilIdle(); | |
347 | |
348 // Now reload the model and verify that the merge updates the url, and | |
349 // preserves the sync GUID. | |
350 test_util_.ResetModel(true); | |
351 keyword_url = model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")); | |
352 ASSERT_TRUE(keyword_url != NULL); | |
353 EXPECT_EQ(prepopulated_url, keyword_url->url_ref().DisplayURL()); | |
354 EXPECT_EQ(original_guid, keyword_url->sync_guid()); | |
355 | |
356 // Wait for any saves to finish. | |
357 base::RunLoop().RunUntilIdle(); | |
358 | |
359 // Reload the model to verify that change was saved correctly. | |
360 test_util_.ResetModel(true); | |
361 keyword_url = model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")); | |
362 ASSERT_TRUE(keyword_url != NULL); | |
363 EXPECT_EQ(prepopulated_url, keyword_url->url_ref().DisplayURL()); | |
364 EXPECT_EQ(original_guid, keyword_url->sync_guid()); | |
365 } | |
366 | |
367 void TemplateURLServiceTest::VerifyObserverCount(int expected_changed_count) { | 295 void TemplateURLServiceTest::VerifyObserverCount(int expected_changed_count) { |
368 EXPECT_EQ(expected_changed_count, test_util_.GetObserverCount()); | 296 EXPECT_EQ(expected_changed_count, test_util_.GetObserverCount()); |
369 test_util_.ResetObserverCount(); | 297 test_util_.ResetObserverCount(); |
370 } | 298 } |
371 | 299 |
372 void TemplateURLServiceTest::VerifyObserverFired() { | 300 void TemplateURLServiceTest::VerifyObserverFired() { |
373 EXPECT_LE(1, test_util_.GetObserverCount()); | 301 EXPECT_LE(1, test_util_.GetObserverCount()); |
374 test_util_.ResetObserverCount(); | 302 test_util_.ResetObserverCount(); |
375 } | 303 } |
376 | 304 |
(...skipping 846 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1223 base::RunLoop().RunUntilIdle(); | 1151 base::RunLoop().RunUntilIdle(); |
1224 | 1152 |
1225 // Reload the model and check that the default search provider | 1153 // Reload the model and check that the default search provider |
1226 // was properly saved. | 1154 // was properly saved. |
1227 test_util_.ResetModel(true); | 1155 test_util_.ResetModel(true); |
1228 default_search = model()->GetDefaultSearchProvider(); | 1156 default_search = model()->GetDefaultSearchProvider(); |
1229 ASSERT_TRUE(default_search != NULL); | 1157 ASSERT_TRUE(default_search != NULL); |
1230 AssertEquals(*cloned_url, *default_search); | 1158 AssertEquals(*cloned_url, *default_search); |
1231 } | 1159 } |
1232 | 1160 |
1233 TEST_F(TemplateURLServiceTest, FindNewDefaultSearchProvider) { | |
1234 // Ensure that if our service is initially empty, we don't initial have a | |
1235 // valid new DSP. | |
1236 EXPECT_FALSE(model()->FindNewDefaultSearchProvider()); | |
1237 | |
1238 // Add a few entries with searchTerms, but ensure only the last one is in the | |
1239 // default list. | |
1240 AddKeywordWithDate("name1", "key1", "http://foo1/{searchTerms}", | |
1241 "http://sugg1", std::string(), "http://icon1", true, | |
1242 "UTF-8;UTF-16", Time(), Time()); | |
1243 AddKeywordWithDate("name2", "key2", "http://foo2/{searchTerms}", | |
1244 "http://sugg2", std::string(), "http://icon1", true, | |
1245 "UTF-8;UTF-16", Time(), Time()); | |
1246 AddKeywordWithDate("name3", "key3", "http://foo1/{searchTerms}", | |
1247 "http://sugg3", std::string(), "http://icon3", true, | |
1248 "UTF-8;UTF-16", Time(), Time()); | |
1249 TemplateURLData data; | |
1250 data.short_name = ASCIIToUTF16("valid"); | |
1251 data.SetKeyword(ASCIIToUTF16("validkeyword")); | |
1252 data.SetURL("http://valid/{searchTerms}"); | |
1253 data.favicon_url = GURL("http://validicon"); | |
1254 data.show_in_default_list = true; | |
1255 TemplateURL* valid_turl(new TemplateURL(test_util_.profile(), data)); | |
1256 model()->Add(valid_turl); | |
1257 EXPECT_EQ(4U, model()->GetTemplateURLs().size()); | |
1258 | |
1259 // Request a new DSP from the service and only expect the valid one. | |
1260 TemplateURL* new_default = model()->FindNewDefaultSearchProvider(); | |
1261 ASSERT_TRUE(new_default); | |
1262 EXPECT_EQ(valid_turl, new_default); | |
1263 | |
1264 // Remove the default we received and ensure that the service returns NULL. | |
1265 model()->Remove(new_default); | |
1266 EXPECT_FALSE(model()->FindNewDefaultSearchProvider()); | |
1267 } | |
1268 | |
1269 // Make sure that the load routine doesn't delete | 1161 // Make sure that the load routine doesn't delete |
1270 // prepopulated engines that no longer exist in the prepopulate data if | 1162 // prepopulated engines that no longer exist in the prepopulate data if |
1271 // it is the default search provider. | 1163 // it is the default search provider. |
1272 TEST_F(TemplateURLServiceTest, LoadRetainsDefaultProvider) { | 1164 TEST_F(TemplateURLServiceTest, LoadRetainsDefaultProvider) { |
1273 // Set the default search provider to a preloaded template url which | 1165 // Set the default search provider to a preloaded template url which |
1274 // is not in the current set of preloaded template urls and save | 1166 // is not in the current set of preloaded template urls and save |
1275 // the result. | 1167 // the result. |
1276 TemplateURL* t_url = CreatePreloadedTemplateURL(true, 999999); | 1168 TemplateURL* t_url = CreatePreloadedTemplateURL(true, 999999); |
1277 test_util_.ChangeModelToLoadState(); | 1169 test_util_.ChangeModelToLoadState(); |
1278 model()->Add(t_url); | 1170 model()->Add(t_url); |
(...skipping 25 matching lines...) Expand all Loading... | |
1304 test_util_.ResetModel(true); | 1196 test_util_.ResetModel(true); |
1305 { | 1197 { |
1306 const TemplateURL* keyword_url = | 1198 const TemplateURL* keyword_url = |
1307 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")); | 1199 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")); |
1308 ASSERT_TRUE(keyword_url != NULL); | 1200 ASSERT_TRUE(keyword_url != NULL); |
1309 AssertEquals(*cloned_url, *keyword_url); | 1201 AssertEquals(*cloned_url, *keyword_url); |
1310 ASSERT_EQ(keyword_url, model()->GetDefaultSearchProvider()); | 1202 ASSERT_EQ(keyword_url, model()->GetDefaultSearchProvider()); |
1311 } | 1203 } |
1312 } | 1204 } |
1313 | 1205 |
1314 // Make sure that the load routine updates the url of a preexisting | |
1315 // default search engine provider and that the result is saved correctly. | |
1316 TEST_F(TemplateURLServiceTest, LoadUpdatesDefaultSearchURL) { | |
1317 TestLoadUpdatingPreloadedURL(0); | |
1318 } | |
1319 | |
1320 // Make sure that the load routine updates the url of a preexisting | |
1321 // non-default search engine provider and that the result is saved correctly. | |
1322 TEST_F(TemplateURLServiceTest, LoadUpdatesSearchURL) { | |
1323 TestLoadUpdatingPreloadedURL(1); | |
1324 } | |
1325 | |
1326 // Make sure that the load routine sets a default search provider if it was | 1206 // Make sure that the load routine sets a default search provider if it was |
1327 // missing and not managed. | 1207 // missing and not managed. |
1328 TEST_F(TemplateURLServiceTest, LoadEnsuresDefaultSearchProviderExists) { | 1208 TEST_F(TemplateURLServiceTest, LoadEnsuresDefaultSearchProviderExists) { |
1329 // Force the model to load and make sure we have a default search provider. | 1209 // Force the model to load and make sure we have a default search provider. |
1330 test_util_.VerifyLoad(); | 1210 test_util_.VerifyLoad(); |
1331 TemplateURL* old_default = model()->GetDefaultSearchProvider(); | 1211 TemplateURL* old_default = model()->GetDefaultSearchProvider(); |
1332 EXPECT_TRUE(old_default); | 1212 ASSERT_TRUE(old_default); |
Peter Kasting
2014/05/07 23:38:29
This can still be EXPECT_TRUE, we don't deref it.
erikwright (departed)
2014/05/08 12:46:24
Done.
| |
1333 | 1213 |
1334 // Now remove it. | |
1335 model()->SetUserSelectedDefaultSearchProvider(NULL); | |
1336 model()->Remove(old_default); | |
1337 base::RunLoop().RunUntilIdle(); | |
1338 | |
1339 EXPECT_FALSE(model()->GetDefaultSearchProvider()); | |
1340 | |
1341 // Reset the model and load it. There should be a default search provider. | |
1342 test_util_.ResetModel(true); | |
1343 | |
1344 ASSERT_TRUE(model()->GetDefaultSearchProvider()); | |
1345 EXPECT_TRUE(model()->GetDefaultSearchProvider()->SupportsReplacement()); | 1214 EXPECT_TRUE(model()->GetDefaultSearchProvider()->SupportsReplacement()); |
1346 | 1215 |
1347 // Make default search provider unusable (no search terms). | 1216 // Make default search provider unusable (no search terms). |
1348 model()->ResetTemplateURL(model()->GetDefaultSearchProvider(), | 1217 model()->ResetTemplateURL(model()->GetDefaultSearchProvider(), |
1349 ASCIIToUTF16("test"), ASCIIToUTF16("test"), | 1218 ASCIIToUTF16("test"), ASCIIToUTF16("test"), |
1350 "http://example.com/"); | 1219 "http://example.com/"); |
1351 base::RunLoop().RunUntilIdle(); | 1220 base::RunLoop().RunUntilIdle(); |
1352 | 1221 |
1353 // Reset the model and load it. There should be a usable default search | 1222 // Reset the model and load it. There should be a usable default search |
1354 // provider. | 1223 // provider. |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1452 actual_managed_default = model()->GetDefaultSearchProvider(); | 1321 actual_managed_default = model()->GetDefaultSearchProvider(); |
1453 ExpectSimilar(expected_managed_default2.get(), actual_managed_default); | 1322 ExpectSimilar(expected_managed_default2.get(), actual_managed_default); |
1454 EXPECT_EQ(actual_managed_default->show_in_default_list(), true); | 1323 EXPECT_EQ(actual_managed_default->show_in_default_list(), true); |
1455 | 1324 |
1456 // Remove all the managed prefs and check that we are no longer managed. | 1325 // Remove all the managed prefs and check that we are no longer managed. |
1457 test_util_.RemoveManagedDefaultSearchPreferences(); | 1326 test_util_.RemoveManagedDefaultSearchPreferences(); |
1458 VerifyObserverFired(); | 1327 VerifyObserverFired(); |
1459 EXPECT_FALSE(model()->is_default_search_managed()); | 1328 EXPECT_FALSE(model()->is_default_search_managed()); |
1460 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); | 1329 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); |
1461 | 1330 |
1462 // The default should now be the first URL added | 1331 // The default should now be the user preference. |
1463 const TemplateURL* actual_final_managed_default = | 1332 const TemplateURL* actual_final_managed_default = |
1464 model()->GetDefaultSearchProvider(); | 1333 model()->GetDefaultSearchProvider(); |
1465 ExpectSimilar(model()->GetTemplateURLs()[0], actual_final_managed_default); | 1334 ExpectSimilar(regular_default, actual_final_managed_default); |
1466 EXPECT_EQ(actual_final_managed_default->show_in_default_list(), true); | 1335 EXPECT_EQ(actual_final_managed_default->show_in_default_list(), true); |
1467 | 1336 |
1468 // Disable the default search provider through policy. | 1337 // Disable the default search provider through policy. |
1469 test_util_.SetManagedDefaultSearchPreferences(false, std::string(), | 1338 test_util_.SetManagedDefaultSearchPreferences(false, std::string(), |
1470 std::string(), std::string(), std::string(), std::string(), | 1339 std::string(), std::string(), std::string(), std::string(), |
1471 std::string(), std::string(), std::string()); | 1340 std::string(), std::string(), std::string()); |
1472 VerifyObserverFired(); | 1341 VerifyObserverFired(); |
1473 EXPECT_TRUE(model()->is_default_search_managed()); | 1342 EXPECT_TRUE(model()->is_default_search_managed()); |
1474 EXPECT_TRUE(NULL == model()->GetDefaultSearchProvider()); | 1343 EXPECT_TRUE(NULL == model()->GetDefaultSearchProvider()); |
1475 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); | 1344 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1679 scoped_ptr<AssociatedExtensionInfo> extension_info( | 1548 scoped_ptr<AssociatedExtensionInfo> extension_info( |
1680 new AssociatedExtensionInfo); | 1549 new AssociatedExtensionInfo); |
1681 extension_info->wants_to_be_default_engine = true; | 1550 extension_info->wants_to_be_default_engine = true; |
1682 extension_info->extension_id = "ext1"; | 1551 extension_info->extension_id = "ext1"; |
1683 model()->AddExtensionControlledTURL(ext_dse, extension_info.Pass()); | 1552 model()->AddExtensionControlledTURL(ext_dse, extension_info.Pass()); |
1684 EXPECT_EQ(ext_dse, model()->GetTemplateURLForKeyword(ASCIIToUTF16("ext1"))); | 1553 EXPECT_EQ(ext_dse, model()->GetTemplateURLForKeyword(ASCIIToUTF16("ext1"))); |
1685 EXPECT_TRUE(model()->is_default_search_managed()); | 1554 EXPECT_TRUE(model()->is_default_search_managed()); |
1686 actual_managed_default = model()->GetDefaultSearchProvider(); | 1555 actual_managed_default = model()->GetDefaultSearchProvider(); |
1687 ExpectSimilar(expected_managed_default.get(), actual_managed_default); | 1556 ExpectSimilar(expected_managed_default.get(), actual_managed_default); |
1688 } | 1557 } |
OLD | NEW |