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