Chromium Code Reviews| 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 syncer::SyncDataList all_sync_data = | 423 syncer::SyncDataList all_sync_data = |
| 423 model()->GetAllSyncData(syncer::SEARCH_ENGINES); | 424 model()->GetAllSyncData(syncer::SEARCH_ENGINES); |
| 424 | 425 |
| 425 EXPECT_EQ(2U, all_sync_data.size()); | 426 EXPECT_EQ(2U, all_sync_data.size()); |
| 426 | 427 |
| 427 for (syncer::SyncDataList::const_iterator iter = all_sync_data.begin(); | 428 for (syncer::SyncDataList::const_iterator iter = all_sync_data.begin(); |
| 428 iter != all_sync_data.end(); ++iter) { | 429 iter != all_sync_data.end(); ++iter) { |
| 429 std::string guid = GetGUID(*iter); | 430 std::string guid = GetGUID(*iter); |
| 430 const TemplateURL* service_turl = model()->GetTemplateURLForGUID(guid); | 431 const TemplateURL* service_turl = model()->GetTemplateURLForGUID(guid); |
| 431 std::unique_ptr<TemplateURL> deserialized(Deserialize(*iter)); | 432 std::unique_ptr<TemplateURL> deserialized(Deserialize(*iter)); |
| 432 AssertEquals(*service_turl, *deserialized); | 433 AssertEquals(*service_turl, *deserialized); |
| 433 } | 434 } |
| 434 } | 435 } |
| 435 | 436 |
| 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 a 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 | |
| 458 EXPECT_EQ(2U, all_sync_data.size()); | |
| 459 | |
| 460 for (syncer::SyncDataList::const_iterator iter = all_sync_data.begin(); | |
| 461 iter != all_sync_data.end(); ++iter) { | |
| 462 std::string guid = GetGUID(*iter); | |
| 463 const TemplateURL* service_turl = model()->GetTemplateURLForGUID(guid); | |
| 464 std::unique_ptr<TemplateURL> deserialized(Deserialize(*iter)); | |
|
Peter Kasting
2016/12/22 20:49:03
Nit: Use = instead of (), for consistency with lin
Alexander Yashkin
2016/12/23 19:44:08
Done
| |
| 465 AssertEquals(*service_turl, *deserialized); | |
| 466 EXPECT_NE(TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION, | |
| 467 deserialized->type()); | |
| 468 EXPECT_NE(ext_turl->keyword(), deserialized->keyword()); | |
| 469 EXPECT_NE(ext_turl->short_name(), deserialized->short_name()); | |
| 470 EXPECT_NE(ext_turl->url(), deserialized->url()); | |
| 471 } | |
| 472 } | |
| 473 | |
| 436 TEST_F(TemplateURLServiceSyncTest, GetAllSyncDataNoManagedEngines) { | 474 TEST_F(TemplateURLServiceSyncTest, GetAllSyncDataNoManagedEngines) { |
| 437 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com")); | 475 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com")); |
| 438 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://key2.com")); | 476 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://key2.com")); |
| 439 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key3"), "http://key3.com", | 477 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key3"), "http://key3.com", |
| 440 std::string(), 100, false, true)); | 478 std::string(), 100, false, true)); |
| 441 syncer::SyncDataList all_sync_data = | 479 syncer::SyncDataList all_sync_data = |
| 442 model()->GetAllSyncData(syncer::SEARCH_ENGINES); | 480 model()->GetAllSyncData(syncer::SEARCH_ENGINES); |
| 443 | 481 |
| 444 EXPECT_EQ(2U, all_sync_data.size()); | 482 EXPECT_EQ(2U, all_sync_data.size()); |
| 445 | 483 |
| (...skipping 1253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1699 | 1737 |
| 1700 // Go unmanaged. Ensure that the DSP changes to the expected pending entry | 1738 // Go unmanaged. Ensure that the DSP changes to the expected pending entry |
| 1701 // from Sync. | 1739 // from Sync. |
| 1702 const TemplateURL* expected_default = | 1740 const TemplateURL* expected_default = |
| 1703 model()->GetTemplateURLForGUID("newdefault"); | 1741 model()->GetTemplateURLForGUID("newdefault"); |
| 1704 RemoveManagedDefaultSearchPreferences(test_util_a_->profile()); | 1742 RemoveManagedDefaultSearchPreferences(test_util_a_->profile()); |
| 1705 | 1743 |
| 1706 EXPECT_EQ(expected_default, model()->GetDefaultSearchProvider()); | 1744 EXPECT_EQ(expected_default, model()->GetDefaultSearchProvider()); |
| 1707 } | 1745 } |
| 1708 | 1746 |
| 1747 TEST_F(TemplateURLServiceSyncTest, SyncWithExtensionDefaultSearch) { | |
| 1748 // First start off with a few entries and make sure we can set an extension | |
| 1749 // default search provider. | |
| 1750 syncer::SyncDataList initial_data = CreateInitialSyncData(); | |
| 1751 model()->MergeDataAndStartSyncing(syncer::SEARCH_ENGINES, initial_data, | |
| 1752 PassProcessor(), | |
| 1753 CreateAndPassSyncErrorFactory()); | |
| 1754 model()->SetUserSelectedDefaultSearchProvider( | |
| 1755 model()->GetTemplateURLForGUID("key2")); | |
| 1756 | |
| 1757 // Expect one change because of user default engine change. | |
| 1758 EXPECT_EQ(1U, processor()->change_list_size()); | |
| 1759 ASSERT_TRUE(processor()->contains_guid("key2")); | |
| 1760 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, | |
| 1761 processor()->change_for_guid("key2").change_type()); | |
| 1762 | |
| 1763 EXPECT_EQ(3U, model()->GetAllSyncData(syncer::SEARCH_ENGINES).size()); | |
| 1764 ASSERT_TRUE(model()->GetDefaultSearchProvider()); | |
| 1765 | |
| 1766 // Change the default search provider to a extension one. | |
| 1767 std::unique_ptr<TemplateURLData> extension = | |
| 1768 GenerateDummyTemplateURLData("extensiondefault"); | |
| 1769 auto ext_dse = base::MakeUnique<TemplateURL>( | |
| 1770 *extension, TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION); | |
| 1771 auto ext_info = base::MakeUnique<TemplateURL::AssociatedExtensionInfo>("ext"); | |
| 1772 ext_info->wants_to_be_default_engine = true; | |
| 1773 test_util_a_->AddExtensionControlledTURL(std::move(ext_dse), | |
| 1774 std::move(ext_info)); | |
| 1775 | |
| 1776 const TemplateURL* dsp_turl = model()->GetDefaultSearchProvider(); | |
| 1777 EXPECT_TRUE(model()->IsExtensionControlledDefaultSearch()); | |
| 1778 | |
| 1779 // Expect still one change because of user default engine change. | |
| 1780 EXPECT_EQ(1U, processor()->change_list_size()); | |
| 1781 // Check that added extension engine did not change number of engines | |
| 1782 // in sync processor. | |
| 1783 EXPECT_EQ(3U, model()->GetAllSyncData(syncer::SEARCH_ENGINES).size()); | |
| 1784 | |
| 1785 // Add a new entry from Sync. It should still sync in despite the default | |
| 1786 // being extension controlled. | |
| 1787 syncer::SyncChangeList changes; | |
| 1788 changes.push_back(CreateTestSyncChange( | |
| 1789 syncer::SyncChange::ACTION_ADD, | |
| 1790 CreateTestTemplateURL(ASCIIToUTF16("newkeyword"), | |
| 1791 "http://new.com/{searchTerms}", "newdefault"))); | |
| 1792 model()->ProcessSyncChanges(FROM_HERE, changes); | |
| 1793 | |
| 1794 EXPECT_EQ(4U, model()->GetAllSyncData(syncer::SEARCH_ENGINES).size()); | |
| 1795 | |
| 1796 // Change kSyncedDefaultSearchProviderGUID to point to the new entry and | |
| 1797 // ensure that the DSP remains extension controlled. | |
| 1798 profile_a()->GetTestingPrefService()->SetString( | |
| 1799 prefs::kSyncedDefaultSearchProviderGUID, "newdefault"); | |
| 1800 | |
| 1801 EXPECT_EQ(dsp_turl, model()->GetDefaultSearchProvider()); | |
| 1802 EXPECT_TRUE(model()->IsExtensionControlledDefaultSearch()); | |
| 1803 | |
| 1804 // Remove extension DSE. Ensure that the DSP changes to the expected pending | |
| 1805 // entry from Sync. | |
| 1806 const TemplateURL* expected_default = | |
| 1807 model()->GetTemplateURLForGUID("newdefault"); | |
| 1808 test_util_a_->RemoveExtensionDefaultSearchFromPrefs("ext"); | |
| 1809 | |
| 1810 EXPECT_EQ(expected_default, model()->GetDefaultSearchProvider()); | |
| 1811 } | |
| 1812 | |
| 1709 TEST_F(TemplateURLServiceSyncTest, SyncMergeDeletesDefault) { | 1813 TEST_F(TemplateURLServiceSyncTest, SyncMergeDeletesDefault) { |
| 1710 // If the value from Sync is a duplicate of the local default and is newer, it | 1814 // If the value from Sync is a duplicate of the local default and is newer, it |
| 1711 // should safely replace the local value and set as the new default. | 1815 // should safely replace the local value and set as the new default. |
| 1712 TemplateURL* default_turl = model()->Add(CreateTestTemplateURL( | 1816 TemplateURL* default_turl = model()->Add(CreateTestTemplateURL( |
| 1713 ASCIIToUTF16("key1"), "http://key1.com/{searchTerms}", "whateverguid", | 1817 ASCIIToUTF16("key1"), "http://key1.com/{searchTerms}", "whateverguid", |
| 1714 10)); | 1818 10)); |
| 1715 model()->SetUserSelectedDefaultSearchProvider(default_turl); | 1819 model()->SetUserSelectedDefaultSearchProvider(default_turl); |
| 1716 | 1820 |
| 1717 syncer::SyncDataList initial_data = CreateInitialSyncData(); | 1821 syncer::SyncDataList initial_data = CreateInitialSyncData(); |
| 1718 // The key1 entry should be a duplicate of the default. | 1822 // The key1 entry should be a duplicate of the default. |
| (...skipping 596 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2315 | 2419 |
| 2316 TEST_F(TemplateURLServiceSyncTest, NonAsciiKeywordDoesNotCrash) { | 2420 TEST_F(TemplateURLServiceSyncTest, NonAsciiKeywordDoesNotCrash) { |
| 2317 model()->Add(CreateTestTemplateURL(UTF8ToUTF16("\xf0\xaf\xa6\x8d"), | 2421 model()->Add(CreateTestTemplateURL(UTF8ToUTF16("\xf0\xaf\xa6\x8d"), |
| 2318 "http://key1.com")); | 2422 "http://key1.com")); |
| 2319 syncer::SyncDataList initial_data = CreateInitialSyncData(); | 2423 syncer::SyncDataList initial_data = CreateInitialSyncData(); |
| 2320 | 2424 |
| 2321 model()->MergeDataAndStartSyncing( | 2425 model()->MergeDataAndStartSyncing( |
| 2322 syncer::SEARCH_ENGINES, initial_data, PassProcessor(), | 2426 syncer::SEARCH_ENGINES, initial_data, PassProcessor(), |
| 2323 CreateAndPassSyncErrorFactory()); | 2427 CreateAndPassSyncErrorFactory()); |
| 2324 } | 2428 } |
| OLD | NEW |