| 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/string_split.h" | 10 #include "base/string_split.h" |
| (...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 return t_url; | 247 return t_url; |
| 248 } | 248 } |
| 249 | 249 |
| 250 void TemplateURLServiceTest::AssertEquals(const TemplateURL& expected, | 250 void TemplateURLServiceTest::AssertEquals(const TemplateURL& expected, |
| 251 const TemplateURL& actual) { | 251 const TemplateURL& actual) { |
| 252 ASSERT_EQ(expected.short_name(), actual.short_name()); | 252 ASSERT_EQ(expected.short_name(), actual.short_name()); |
| 253 ASSERT_EQ(expected.keyword(), actual.keyword()); | 253 ASSERT_EQ(expected.keyword(), actual.keyword()); |
| 254 ASSERT_EQ(expected.url(), actual.url()); | 254 ASSERT_EQ(expected.url(), actual.url()); |
| 255 ASSERT_EQ(expected.suggestions_url(), actual.suggestions_url()); | 255 ASSERT_EQ(expected.suggestions_url(), actual.suggestions_url()); |
| 256 ASSERT_EQ(expected.favicon_url(), actual.favicon_url()); | 256 ASSERT_EQ(expected.favicon_url(), actual.favicon_url()); |
| 257 ASSERT_EQ(expected.alternate_urls(), actual.alternate_urls()); |
| 257 ASSERT_EQ(expected.show_in_default_list(), actual.show_in_default_list()); | 258 ASSERT_EQ(expected.show_in_default_list(), actual.show_in_default_list()); |
| 258 ASSERT_EQ(expected.safe_for_autoreplace(), actual.safe_for_autoreplace()); | 259 ASSERT_EQ(expected.safe_for_autoreplace(), actual.safe_for_autoreplace()); |
| 259 ASSERT_EQ(expected.input_encodings(), actual.input_encodings()); | 260 ASSERT_EQ(expected.input_encodings(), actual.input_encodings()); |
| 260 ASSERT_EQ(expected.id(), actual.id()); | 261 ASSERT_EQ(expected.id(), actual.id()); |
| 261 ASSERT_EQ(expected.date_created(), actual.date_created()); | 262 ASSERT_EQ(expected.date_created(), actual.date_created()); |
| 262 ASSERT_EQ(expected.last_modified(), actual.last_modified()); | 263 ASSERT_EQ(expected.last_modified(), actual.last_modified()); |
| 263 ASSERT_EQ(expected.sync_guid(), actual.sync_guid()); | 264 ASSERT_EQ(expected.sync_guid(), actual.sync_guid()); |
| 265 ASSERT_EQ(expected.search_terms_replacement_key(), |
| 266 actual.search_terms_replacement_key()); |
| 264 } | 267 } |
| 265 | 268 |
| 266 void TemplateURLServiceTest::ExpectSimilar(const TemplateURL* expected, | 269 void TemplateURLServiceTest::ExpectSimilar(const TemplateURL* expected, |
| 267 const TemplateURL* actual) { | 270 const TemplateURL* actual) { |
| 268 ASSERT_TRUE(expected != NULL); | 271 ASSERT_TRUE(expected != NULL); |
| 269 ASSERT_TRUE(actual != NULL); | 272 ASSERT_TRUE(actual != NULL); |
| 270 EXPECT_EQ(expected->short_name(), actual->short_name()); | 273 EXPECT_EQ(expected->short_name(), actual->short_name()); |
| 271 EXPECT_EQ(expected->keyword(), actual->keyword()); | 274 EXPECT_EQ(expected->keyword(), actual->keyword()); |
| 272 EXPECT_EQ(expected->url(), actual->url()); | 275 EXPECT_EQ(expected->url(), actual->url()); |
| 273 EXPECT_EQ(expected->suggestions_url(), actual->suggestions_url()); | 276 EXPECT_EQ(expected->suggestions_url(), actual->suggestions_url()); |
| 274 EXPECT_EQ(expected->favicon_url(), actual->favicon_url()); | 277 EXPECT_EQ(expected->favicon_url(), actual->favicon_url()); |
| 278 EXPECT_EQ(expected->alternate_urls(), actual->alternate_urls()); |
| 275 EXPECT_EQ(expected->show_in_default_list(), actual->show_in_default_list()); | 279 EXPECT_EQ(expected->show_in_default_list(), actual->show_in_default_list()); |
| 276 EXPECT_EQ(expected->safe_for_autoreplace(), actual->safe_for_autoreplace()); | 280 EXPECT_EQ(expected->safe_for_autoreplace(), actual->safe_for_autoreplace()); |
| 277 EXPECT_EQ(expected->input_encodings(), actual->input_encodings()); | 281 EXPECT_EQ(expected->input_encodings(), actual->input_encodings()); |
| 282 EXPECT_EQ(expected->search_terms_replacement_key(), |
| 283 actual->search_terms_replacement_key()); |
| 278 } | 284 } |
| 279 | 285 |
| 280 TemplateURL* TemplateURLServiceTest::CreatePreloadedTemplateURL( | 286 TemplateURL* TemplateURLServiceTest::CreatePreloadedTemplateURL( |
| 281 bool safe_for_autoreplace, | 287 bool safe_for_autoreplace, |
| 282 int prepopulate_id) { | 288 int prepopulate_id) { |
| 283 TemplateURLData data; | 289 TemplateURLData data; |
| 284 data.short_name = ASCIIToUTF16("unittest"); | 290 data.short_name = ASCIIToUTF16("unittest"); |
| 285 data.SetKeyword(ASCIIToUTF16("unittest")); | 291 data.SetKeyword(ASCIIToUTF16("unittest")); |
| 286 data.SetURL("http://www.unittest.com/{searchTerms}"); | 292 data.SetURL("http://www.unittest.com/{searchTerms}"); |
| 287 data.favicon_url = GURL("http://favicon.url"); | 293 data.favicon_url = GURL("http://favicon.url"); |
| (...skipping 1046 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1334 VerifyObserverCount(1); | 1340 VerifyObserverCount(1); |
| 1335 EXPECT_FALSE(model()->is_default_search_managed()); | 1341 EXPECT_FALSE(model()->is_default_search_managed()); |
| 1336 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); | 1342 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); |
| 1337 | 1343 |
| 1338 // Set a managed preference that establishes a default search provider. | 1344 // Set a managed preference that establishes a default search provider. |
| 1339 const char kName[] = "test1"; | 1345 const char kName[] = "test1"; |
| 1340 const char kKeyword[] = "test.com"; | 1346 const char kKeyword[] = "test.com"; |
| 1341 const char kSearchURL[] = "http://test.com/search?t={searchTerms}"; | 1347 const char kSearchURL[] = "http://test.com/search?t={searchTerms}"; |
| 1342 const char kIconURL[] = "http://test.com/icon.jpg"; | 1348 const char kIconURL[] = "http://test.com/icon.jpg"; |
| 1343 const char kEncodings[] = "UTF-16;UTF-32"; | 1349 const char kEncodings[] = "UTF-16;UTF-32"; |
| 1350 const char kAlternateURL[] = "http://test.com/search#t={searchTerms}"; |
| 1351 const char kSearchTermsReplacementKey[] = "espv"; |
| 1344 test_util_.SetManagedDefaultSearchPreferences(true, kName, kKeyword, | 1352 test_util_.SetManagedDefaultSearchPreferences(true, kName, kKeyword, |
| 1345 kSearchURL, std::string(), kIconURL, kEncodings); | 1353 kSearchURL, std::string(), kIconURL, kEncodings, kAlternateURL, |
| 1354 kSearchTermsReplacementKey); |
| 1346 VerifyObserverFired(); | 1355 VerifyObserverFired(); |
| 1347 EXPECT_TRUE(model()->is_default_search_managed()); | 1356 EXPECT_TRUE(model()->is_default_search_managed()); |
| 1348 EXPECT_EQ(initial_count + 2, model()->GetTemplateURLs().size()); | 1357 EXPECT_EQ(initial_count + 2, model()->GetTemplateURLs().size()); |
| 1349 | 1358 |
| 1350 // Verify that the default manager we are getting is the managed one. | 1359 // Verify that the default manager we are getting is the managed one. |
| 1351 TemplateURLData data; | 1360 TemplateURLData data; |
| 1352 data.short_name = ASCIIToUTF16(kName); | 1361 data.short_name = ASCIIToUTF16(kName); |
| 1353 data.SetKeyword(ASCIIToUTF16(kKeyword)); | 1362 data.SetKeyword(ASCIIToUTF16(kKeyword)); |
| 1354 data.SetURL(kSearchURL); | 1363 data.SetURL(kSearchURL); |
| 1355 data.favicon_url = GURL(kIconURL); | 1364 data.favicon_url = GURL(kIconURL); |
| 1356 data.show_in_default_list = true; | 1365 data.show_in_default_list = true; |
| 1357 base::SplitString(kEncodings, ';', &data.input_encodings); | 1366 base::SplitString(kEncodings, ';', &data.input_encodings); |
| 1367 data.alternate_urls.push_back(kAlternateURL); |
| 1368 data.search_terms_replacement_key = kSearchTermsReplacementKey; |
| 1358 Profile* profile = test_util_.profile(); | 1369 Profile* profile = test_util_.profile(); |
| 1359 scoped_ptr<TemplateURL> expected_managed_default1(new TemplateURL(profile, | 1370 scoped_ptr<TemplateURL> expected_managed_default1(new TemplateURL(profile, |
| 1360 data)); | 1371 data)); |
| 1361 const TemplateURL* actual_managed_default = | 1372 const TemplateURL* actual_managed_default = |
| 1362 model()->GetDefaultSearchProvider(); | 1373 model()->GetDefaultSearchProvider(); |
| 1363 ExpectSimilar(expected_managed_default1.get(), actual_managed_default); | 1374 ExpectSimilar(expected_managed_default1.get(), actual_managed_default); |
| 1364 EXPECT_TRUE(actual_managed_default->show_in_default_list()); | 1375 EXPECT_TRUE(actual_managed_default->show_in_default_list()); |
| 1365 | 1376 |
| 1366 // Update the managed preference and check that the model has changed. | 1377 // Update the managed preference and check that the model has changed. |
| 1367 const char kNewName[] = "test2"; | 1378 const char kNewName[] = "test2"; |
| 1368 const char kNewKeyword[] = "other.com"; | 1379 const char kNewKeyword[] = "other.com"; |
| 1369 const char kNewSearchURL[] = "http://other.com/search?t={searchTerms}"; | 1380 const char kNewSearchURL[] = "http://other.com/search?t={searchTerms}"; |
| 1370 const char kNewSuggestURL[] = "http://other.com/suggest?t={searchTerms}"; | 1381 const char kNewSuggestURL[] = "http://other.com/suggest?t={searchTerms}"; |
| 1371 test_util_.SetManagedDefaultSearchPreferences(true, kNewName, kNewKeyword, | 1382 test_util_.SetManagedDefaultSearchPreferences(true, kNewName, kNewKeyword, |
| 1372 kNewSearchURL, kNewSuggestURL, std::string(), std::string()); | 1383 kNewSearchURL, kNewSuggestURL, std::string(), std::string(), |
| 1384 std::string(), std::string()); |
| 1373 VerifyObserverFired(); | 1385 VerifyObserverFired(); |
| 1374 EXPECT_TRUE(model()->is_default_search_managed()); | 1386 EXPECT_TRUE(model()->is_default_search_managed()); |
| 1375 EXPECT_EQ(initial_count + 2, model()->GetTemplateURLs().size()); | 1387 EXPECT_EQ(initial_count + 2, model()->GetTemplateURLs().size()); |
| 1376 | 1388 |
| 1377 // Verify that the default manager we are now getting is the correct one. | 1389 // Verify that the default manager we are now getting is the correct one. |
| 1378 TemplateURLData data2; | 1390 TemplateURLData data2; |
| 1379 data2.short_name = ASCIIToUTF16(kNewName); | 1391 data2.short_name = ASCIIToUTF16(kNewName); |
| 1380 data2.SetKeyword(ASCIIToUTF16(kNewKeyword)); | 1392 data2.SetKeyword(ASCIIToUTF16(kNewKeyword)); |
| 1381 data2.SetURL(kNewSearchURL); | 1393 data2.SetURL(kNewSearchURL); |
| 1382 data2.suggestions_url = kNewSuggestURL; | 1394 data2.suggestions_url = kNewSuggestURL; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 1395 | 1407 |
| 1396 // The default should now be the first URL added | 1408 // The default should now be the first URL added |
| 1397 const TemplateURL* actual_final_managed_default = | 1409 const TemplateURL* actual_final_managed_default = |
| 1398 model()->GetDefaultSearchProvider(); | 1410 model()->GetDefaultSearchProvider(); |
| 1399 ExpectSimilar(model()->GetTemplateURLs()[0], actual_final_managed_default); | 1411 ExpectSimilar(model()->GetTemplateURLs()[0], actual_final_managed_default); |
| 1400 EXPECT_EQ(actual_final_managed_default->show_in_default_list(), true); | 1412 EXPECT_EQ(actual_final_managed_default->show_in_default_list(), true); |
| 1401 | 1413 |
| 1402 // Disable the default search provider through policy. | 1414 // Disable the default search provider through policy. |
| 1403 test_util_.SetManagedDefaultSearchPreferences(false, std::string(), | 1415 test_util_.SetManagedDefaultSearchPreferences(false, std::string(), |
| 1404 std::string(), std::string(), std::string(), std::string(), | 1416 std::string(), std::string(), std::string(), std::string(), |
| 1405 std::string()); | 1417 std::string(), std::string(), std::string()); |
| 1406 VerifyObserverFired(); | 1418 VerifyObserverFired(); |
| 1407 EXPECT_TRUE(model()->is_default_search_managed()); | 1419 EXPECT_TRUE(model()->is_default_search_managed()); |
| 1408 EXPECT_TRUE(NULL == model()->GetDefaultSearchProvider()); | 1420 EXPECT_TRUE(NULL == model()->GetDefaultSearchProvider()); |
| 1409 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); | 1421 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); |
| 1410 | 1422 |
| 1411 // Re-enable it. | 1423 // Re-enable it. |
| 1412 test_util_.SetManagedDefaultSearchPreferences(true, kName, kKeyword, | 1424 test_util_.SetManagedDefaultSearchPreferences(true, kName, kKeyword, |
| 1413 kSearchURL, std::string(), kIconURL, kEncodings); | 1425 kSearchURL, std::string(), kIconURL, kEncodings, kAlternateURL, |
| 1426 kSearchTermsReplacementKey); |
| 1414 VerifyObserverFired(); | 1427 VerifyObserverFired(); |
| 1415 EXPECT_TRUE(model()->is_default_search_managed()); | 1428 EXPECT_TRUE(model()->is_default_search_managed()); |
| 1416 EXPECT_EQ(initial_count + 2, model()->GetTemplateURLs().size()); | 1429 EXPECT_EQ(initial_count + 2, model()->GetTemplateURLs().size()); |
| 1417 | 1430 |
| 1418 // Verify that the default manager we are getting is the managed one. | 1431 // Verify that the default manager we are getting is the managed one. |
| 1419 actual_managed_default = model()->GetDefaultSearchProvider(); | 1432 actual_managed_default = model()->GetDefaultSearchProvider(); |
| 1420 ExpectSimilar(expected_managed_default1.get(), actual_managed_default); | 1433 ExpectSimilar(expected_managed_default1.get(), actual_managed_default); |
| 1421 EXPECT_EQ(actual_managed_default->show_in_default_list(), true); | 1434 EXPECT_EQ(actual_managed_default->show_in_default_list(), true); |
| 1422 | 1435 |
| 1423 // Clear the model and disable the default search provider through policy. | 1436 // Clear the model and disable the default search provider through policy. |
| 1424 // Verify that there is no default search provider after loading the model. | 1437 // Verify that there is no default search provider after loading the model. |
| 1425 // This checks against regressions of http://crbug.com/67180 | 1438 // This checks against regressions of http://crbug.com/67180 |
| 1426 | 1439 |
| 1427 // First, remove the preferences, reset the model, and set a default. | 1440 // First, remove the preferences, reset the model, and set a default. |
| 1428 test_util_.RemoveManagedDefaultSearchPreferences(); | 1441 test_util_.RemoveManagedDefaultSearchPreferences(); |
| 1429 test_util_.ResetModel(true); | 1442 test_util_.ResetModel(true); |
| 1430 TemplateURL* new_default = | 1443 TemplateURL* new_default = |
| 1431 model()->GetTemplateURLForKeyword(ASCIIToUTF16("key1")); | 1444 model()->GetTemplateURLForKeyword(ASCIIToUTF16("key1")); |
| 1432 ASSERT_FALSE(new_default == NULL); | 1445 ASSERT_FALSE(new_default == NULL); |
| 1433 model()->SetDefaultSearchProvider(new_default); | 1446 model()->SetDefaultSearchProvider(new_default); |
| 1434 EXPECT_EQ(new_default, model()->GetDefaultSearchProvider()); | 1447 EXPECT_EQ(new_default, model()->GetDefaultSearchProvider()); |
| 1435 | 1448 |
| 1436 // Now reset the model again but load it after setting the preferences. | 1449 // Now reset the model again but load it after setting the preferences. |
| 1437 test_util_.ResetModel(false); | 1450 test_util_.ResetModel(false); |
| 1438 test_util_.SetManagedDefaultSearchPreferences(false, std::string(), | 1451 test_util_.SetManagedDefaultSearchPreferences(false, std::string(), |
| 1439 std::string(), std::string(), std::string(), std::string(), | 1452 std::string(), std::string(), std::string(), std::string(), |
| 1440 std::string()); | 1453 std::string(), std::string(), std::string()); |
| 1441 test_util_.VerifyLoad(); | 1454 test_util_.VerifyLoad(); |
| 1442 EXPECT_TRUE(model()->is_default_search_managed()); | 1455 EXPECT_TRUE(model()->is_default_search_managed()); |
| 1443 EXPECT_TRUE(model()->GetDefaultSearchProvider() == NULL); | 1456 EXPECT_TRUE(model()->GetDefaultSearchProvider() == NULL); |
| 1444 } | 1457 } |
| 1445 | 1458 |
| 1446 // Test that if we load a TemplateURL with an empty GUID, the load process | 1459 // Test that if we load a TemplateURL with an empty GUID, the load process |
| 1447 // assigns it a newly generated GUID. | 1460 // assigns it a newly generated GUID. |
| 1448 TEST_F(TemplateURLServiceTest, PatchEmptySyncGUID) { | 1461 TEST_F(TemplateURLServiceTest, PatchEmptySyncGUID) { |
| 1449 // Add a new TemplateURL. | 1462 // Add a new TemplateURL. |
| 1450 test_util_.VerifyLoad(); | 1463 test_util_.VerifyLoad(); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1504 EXPECT_EQ(8U, loaded_url->input_encodings().size()); | 1517 EXPECT_EQ(8U, loaded_url->input_encodings().size()); |
| 1505 | 1518 |
| 1506 // Reload the model to verify it was actually saved to the database and the | 1519 // Reload the model to verify it was actually saved to the database and the |
| 1507 // duplicate encodings were removed. | 1520 // duplicate encodings were removed. |
| 1508 test_util_.ResetModel(true); | 1521 test_util_.ResetModel(true); |
| 1509 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); | 1522 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); |
| 1510 loaded_url = model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")); | 1523 loaded_url = model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")); |
| 1511 ASSERT_FALSE(loaded_url == NULL); | 1524 ASSERT_FALSE(loaded_url == NULL); |
| 1512 EXPECT_EQ(4U, loaded_url->input_encodings().size()); | 1525 EXPECT_EQ(4U, loaded_url->input_encodings().size()); |
| 1513 } | 1526 } |
| OLD | NEW |