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 <stddef.h> | 5 #include <stddef.h> |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
12 #include "base/memory/scoped_vector.h" | 12 #include "base/memory/scoped_vector.h" |
13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
17 #include "chrome/browser/search_engines/template_url_service_test_util.h" | 17 #include "chrome/browser/search_engines/template_url_service_test_util.h" |
18 #include "chrome/test/base/testing_profile.h" | 18 #include "chrome/test/base/testing_profile.h" |
19 #include "components/search_engines/search_engines_pref_names.h" | 19 #include "components/search_engines/search_engines_pref_names.h" |
| 20 #include "components/search_engines/search_engines_test_util.h" |
20 #include "components/search_engines/search_terms_data.h" | 21 #include "components/search_engines/search_terms_data.h" |
21 #include "components/search_engines/template_url.h" | 22 #include "components/search_engines/template_url.h" |
22 #include "components/search_engines/template_url_prepopulate_data.h" | 23 #include "components/search_engines/template_url_prepopulate_data.h" |
23 #include "components/search_engines/template_url_service.h" | 24 #include "components/search_engines/template_url_service.h" |
24 #include "components/search_engines/template_url_service_client.h" | 25 #include "components/search_engines/template_url_service_client.h" |
25 #include "components/sync/model/sync_change_processor_wrapper_for_test.h" | 26 #include "components/sync/model/sync_change_processor_wrapper_for_test.h" |
26 #include "components/sync/model/sync_error_factory.h" | 27 #include "components/sync/model/sync_error_factory.h" |
27 #include "components/sync/model/sync_error_factory_mock.h" | 28 #include "components/sync/model/sync_error_factory_mock.h" |
28 #include "components/sync/protocol/search_engine_specifics.pb.h" | 29 #include "components/sync/protocol/search_engine_specifics.pb.h" |
29 #include "components/sync/protocol/sync.pb.h" | 30 #include "components/sync/protocol/sync.pb.h" |
(...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
406 | 407 |
407 for (syncer::SyncDataList::const_iterator iter = all_sync_data.begin(); | 408 for (syncer::SyncDataList::const_iterator iter = all_sync_data.begin(); |
408 iter != all_sync_data.end(); ++iter) { | 409 iter != all_sync_data.end(); ++iter) { |
409 std::string guid = GetGUID(*iter); | 410 std::string guid = GetGUID(*iter); |
410 const TemplateURL* service_turl = model()->GetTemplateURLForGUID(guid); | 411 const TemplateURL* service_turl = model()->GetTemplateURLForGUID(guid); |
411 std::unique_ptr<TemplateURL> deserialized(Deserialize(*iter)); | 412 std::unique_ptr<TemplateURL> deserialized(Deserialize(*iter)); |
412 AssertEquals(*service_turl, *deserialized); | 413 AssertEquals(*service_turl, *deserialized); |
413 } | 414 } |
414 } | 415 } |
415 | 416 |
416 TEST_F(TemplateURLServiceSyncTest, GetAllSyncDataWithExtension) { | 417 TEST_F(TemplateURLServiceSyncTest, GetAllSyncDataWithOmniboxExtension) { |
417 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com")); | 418 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com")); |
418 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://key2.com")); | 419 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://key2.com")); |
419 std::string fake_id("blahblahblah"); | 420 std::string fake_id("blahblahblah"); |
420 std::string fake_url = std::string(kOmniboxScheme) + "://" + fake_id; | 421 std::string fake_url = std::string(kOmniboxScheme) + "://" + fake_id; |
421 model()->RegisterOmniboxKeyword(fake_id, "unittest", "key3", fake_url, | 422 model()->RegisterOmniboxKeyword(fake_id, "unittest", "key3", fake_url, |
422 Time()); | 423 Time()); |
423 syncer::SyncDataList all_sync_data = | 424 syncer::SyncDataList all_sync_data = |
424 model()->GetAllSyncData(syncer::SEARCH_ENGINES); | 425 model()->GetAllSyncData(syncer::SEARCH_ENGINES); |
425 | 426 |
426 EXPECT_EQ(2U, all_sync_data.size()); | 427 EXPECT_EQ(2U, all_sync_data.size()); |
427 | 428 |
428 for (syncer::SyncDataList::const_iterator iter = all_sync_data.begin(); | 429 for (syncer::SyncDataList::const_iterator iter = all_sync_data.begin(); |
429 iter != all_sync_data.end(); ++iter) { | 430 iter != all_sync_data.end(); ++iter) { |
430 std::string guid = GetGUID(*iter); | 431 std::string guid = GetGUID(*iter); |
431 const TemplateURL* service_turl = model()->GetTemplateURLForGUID(guid); | 432 const TemplateURL* service_turl = model()->GetTemplateURLForGUID(guid); |
432 std::unique_ptr<TemplateURL> deserialized(Deserialize(*iter)); | 433 std::unique_ptr<TemplateURL> deserialized(Deserialize(*iter)); |
433 AssertEquals(*service_turl, *deserialized); | 434 AssertEquals(*service_turl, *deserialized); |
434 } | 435 } |
435 } | 436 } |
436 | 437 |
| 438 TEST_F(TemplateURLServiceSyncTest, GetAllSyncDataWithSearchOverrideExtension) { |
| 439 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com")); |
| 440 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://key2.com")); |
| 441 |
| 442 // Change default search provider to an extension one. |
| 443 std::unique_ptr<TemplateURLData> extension = |
| 444 GenerateDummyTemplateURLData("extension"); |
| 445 auto ext_dse = base::MakeUnique<TemplateURL>( |
| 446 *extension, TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION); |
| 447 auto ext_info = base::MakeUnique<TemplateURL::AssociatedExtensionInfo>("ext"); |
| 448 ext_info->wants_to_be_default_engine = true; |
| 449 test_util_a_->AddExtensionControlledTURL(std::move(ext_dse), |
| 450 std::move(ext_info)); |
| 451 |
| 452 const TemplateURL* ext_turl = model()->GetDefaultSearchProvider(); |
| 453 EXPECT_TRUE(model()->IsExtensionControlledDefaultSearch()); |
| 454 |
| 455 // Extension default search must not be synced across browsers. |
| 456 syncer::SyncDataList all_sync_data = |
| 457 model()->GetAllSyncData(syncer::SEARCH_ENGINES); |
| 458 EXPECT_EQ(2U, all_sync_data.size()); |
| 459 |
| 460 for (auto sync_data : all_sync_data) { |
| 461 std::string guid = GetGUID(sync_data); |
| 462 const TemplateURL* service_turl = model()->GetTemplateURLForGUID(guid); |
| 463 std::unique_ptr<TemplateURL> deserialized = Deserialize(sync_data); |
| 464 AssertEquals(*service_turl, *deserialized); |
| 465 EXPECT_NE(TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION, |
| 466 deserialized->type()); |
| 467 EXPECT_NE(ext_turl->keyword(), deserialized->keyword()); |
| 468 EXPECT_NE(ext_turl->short_name(), deserialized->short_name()); |
| 469 EXPECT_NE(ext_turl->url(), deserialized->url()); |
| 470 } |
| 471 } |
| 472 |
437 TEST_F(TemplateURLServiceSyncTest, GetAllSyncDataNoManagedEngines) { | 473 TEST_F(TemplateURLServiceSyncTest, GetAllSyncDataNoManagedEngines) { |
438 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com")); | 474 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com")); |
439 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://key2.com")); | 475 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://key2.com")); |
440 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key3"), "http://key3.com", | 476 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key3"), "http://key3.com", |
441 std::string(), 100, false, true)); | 477 std::string(), 100, false, true)); |
442 syncer::SyncDataList all_sync_data = | 478 syncer::SyncDataList all_sync_data = |
443 model()->GetAllSyncData(syncer::SEARCH_ENGINES); | 479 model()->GetAllSyncData(syncer::SEARCH_ENGINES); |
444 | 480 |
445 EXPECT_EQ(2U, all_sync_data.size()); | 481 EXPECT_EQ(2U, all_sync_data.size()); |
446 | 482 |
(...skipping 1247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1694 | 1730 |
1695 // Go unmanaged. Ensure that the DSP changes to the expected pending entry | 1731 // Go unmanaged. Ensure that the DSP changes to the expected pending entry |
1696 // from Sync. | 1732 // from Sync. |
1697 const TemplateURL* expected_default = | 1733 const TemplateURL* expected_default = |
1698 model()->GetTemplateURLForGUID("newdefault"); | 1734 model()->GetTemplateURLForGUID("newdefault"); |
1699 RemoveManagedDefaultSearchPreferences(test_util_a_->profile()); | 1735 RemoveManagedDefaultSearchPreferences(test_util_a_->profile()); |
1700 | 1736 |
1701 EXPECT_EQ(expected_default, model()->GetDefaultSearchProvider()); | 1737 EXPECT_EQ(expected_default, model()->GetDefaultSearchProvider()); |
1702 } | 1738 } |
1703 | 1739 |
| 1740 TEST_F(TemplateURLServiceSyncTest, SyncWithExtensionDefaultSearch) { |
| 1741 // First start off with a few entries and make sure we can set an extension |
| 1742 // default search provider. |
| 1743 syncer::SyncDataList initial_data = CreateInitialSyncData(); |
| 1744 model()->MergeDataAndStartSyncing(syncer::SEARCH_ENGINES, initial_data, |
| 1745 PassProcessor(), |
| 1746 CreateAndPassSyncErrorFactory()); |
| 1747 model()->SetUserSelectedDefaultSearchProvider( |
| 1748 model()->GetTemplateURLForGUID("key2")); |
| 1749 |
| 1750 // Expect one change because of user default engine change. |
| 1751 const size_t pending_changes = processor()->change_list_size(); |
| 1752 EXPECT_EQ(1U, pending_changes); |
| 1753 ASSERT_TRUE(processor()->contains_guid("key2")); |
| 1754 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, |
| 1755 processor()->change_for_guid("key2").change_type()); |
| 1756 |
| 1757 const size_t sync_engines_count = |
| 1758 model()->GetAllSyncData(syncer::SEARCH_ENGINES).size(); |
| 1759 EXPECT_EQ(3U, sync_engines_count); |
| 1760 ASSERT_TRUE(model()->GetDefaultSearchProvider()); |
| 1761 |
| 1762 // Change the default search provider to an extension one. |
| 1763 std::unique_ptr<TemplateURLData> extension = |
| 1764 GenerateDummyTemplateURLData("extensiondefault"); |
| 1765 auto ext_dse = base::MakeUnique<TemplateURL>( |
| 1766 *extension, TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION); |
| 1767 auto ext_info = base::MakeUnique<TemplateURL::AssociatedExtensionInfo>("ext"); |
| 1768 ext_info->wants_to_be_default_engine = true; |
| 1769 test_util_a_->AddExtensionControlledTURL(std::move(ext_dse), |
| 1770 std::move(ext_info)); |
| 1771 |
| 1772 const TemplateURL* dsp_turl = model()->GetDefaultSearchProvider(); |
| 1773 EXPECT_TRUE(model()->IsExtensionControlledDefaultSearch()); |
| 1774 |
| 1775 // Extension-related changes to the DSE should not be synced as search engine |
| 1776 // changes. |
| 1777 EXPECT_EQ(pending_changes, processor()->change_list_size()); |
| 1778 EXPECT_EQ(sync_engines_count, |
| 1779 model()->GetAllSyncData(syncer::SEARCH_ENGINES).size()); |
| 1780 |
| 1781 // Add a new entry from Sync. It should still sync in despite the default |
| 1782 // being extension controlled. |
| 1783 syncer::SyncChangeList changes; |
| 1784 changes.push_back(CreateTestSyncChange( |
| 1785 syncer::SyncChange::ACTION_ADD, |
| 1786 CreateTestTemplateURL(ASCIIToUTF16("newkeyword"), |
| 1787 "http://new.com/{searchTerms}", "newdefault"))); |
| 1788 model()->ProcessSyncChanges(FROM_HERE, changes); |
| 1789 |
| 1790 EXPECT_EQ(4U, model()->GetAllSyncData(syncer::SEARCH_ENGINES).size()); |
| 1791 |
| 1792 // Change kSyncedDefaultSearchProviderGUID to point to the new entry and |
| 1793 // ensure that the DSP remains extension controlled. |
| 1794 profile_a()->GetTestingPrefService()->SetString( |
| 1795 prefs::kSyncedDefaultSearchProviderGUID, "newdefault"); |
| 1796 |
| 1797 EXPECT_EQ(dsp_turl, model()->GetDefaultSearchProvider()); |
| 1798 EXPECT_TRUE(model()->IsExtensionControlledDefaultSearch()); |
| 1799 |
| 1800 // Remove extension DSE. Ensure that the DSP changes to the expected pending |
| 1801 // entry from Sync. |
| 1802 const TemplateURL* expected_default = |
| 1803 model()->GetTemplateURLForGUID("newdefault"); |
| 1804 test_util_a_->RemoveExtensionControlledTURL("ext"); |
| 1805 |
| 1806 EXPECT_EQ(expected_default, model()->GetDefaultSearchProvider()); |
| 1807 } |
| 1808 |
1704 TEST_F(TemplateURLServiceSyncTest, SyncMergeDeletesDefault) { | 1809 TEST_F(TemplateURLServiceSyncTest, SyncMergeDeletesDefault) { |
1705 // If the value from Sync is a duplicate of the local default and is newer, it | 1810 // If the value from Sync is a duplicate of the local default and is newer, it |
1706 // should safely replace the local value and set as the new default. | 1811 // should safely replace the local value and set as the new default. |
1707 TemplateURL* default_turl = model()->Add(CreateTestTemplateURL( | 1812 TemplateURL* default_turl = model()->Add(CreateTestTemplateURL( |
1708 ASCIIToUTF16("key1"), "http://key1.com/{searchTerms}", "whateverguid", | 1813 ASCIIToUTF16("key1"), "http://key1.com/{searchTerms}", "whateverguid", |
1709 10)); | 1814 10)); |
1710 model()->SetUserSelectedDefaultSearchProvider(default_turl); | 1815 model()->SetUserSelectedDefaultSearchProvider(default_turl); |
1711 | 1816 |
1712 syncer::SyncDataList initial_data = CreateInitialSyncData(); | 1817 syncer::SyncDataList initial_data = CreateInitialSyncData(); |
1713 // The key1 entry should be a duplicate of the default. | 1818 // The key1 entry should be a duplicate of the default. |
(...skipping 595 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2309 | 2414 |
2310 TEST_F(TemplateURLServiceSyncTest, NonAsciiKeywordDoesNotCrash) { | 2415 TEST_F(TemplateURLServiceSyncTest, NonAsciiKeywordDoesNotCrash) { |
2311 model()->Add(CreateTestTemplateURL(UTF8ToUTF16("\xf0\xaf\xa6\x8d"), | 2416 model()->Add(CreateTestTemplateURL(UTF8ToUTF16("\xf0\xaf\xa6\x8d"), |
2312 "http://key1.com")); | 2417 "http://key1.com")); |
2313 syncer::SyncDataList initial_data = CreateInitialSyncData(); | 2418 syncer::SyncDataList initial_data = CreateInitialSyncData(); |
2314 | 2419 |
2315 model()->MergeDataAndStartSyncing( | 2420 model()->MergeDataAndStartSyncing( |
2316 syncer::SEARCH_ENGINES, initial_data, PassProcessor(), | 2421 syncer::SEARCH_ENGINES, initial_data, PassProcessor(), |
2317 CreateAndPassSyncErrorFactory()); | 2422 CreateAndPassSyncErrorFactory()); |
2318 } | 2423 } |
OLD | NEW |