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 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 |
436 TEST_F(TemplateURLServiceSyncTest, GetAllSyncDataNoManagedEngines) { | 472 TEST_F(TemplateURLServiceSyncTest, GetAllSyncDataNoManagedEngines) { |
437 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com")); | 473 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com")); |
438 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://key2.com")); | 474 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://key2.com")); |
439 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key3"), "http://key3.com", | 475 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key3"), "http://key3.com", |
440 std::string(), 100, false, true)); | 476 std::string(), 100, false, true)); |
441 syncer::SyncDataList all_sync_data = | 477 syncer::SyncDataList all_sync_data = |
442 model()->GetAllSyncData(syncer::SEARCH_ENGINES); | 478 model()->GetAllSyncData(syncer::SEARCH_ENGINES); |
443 | 479 |
444 EXPECT_EQ(2U, all_sync_data.size()); | 480 EXPECT_EQ(2U, all_sync_data.size()); |
445 | 481 |
(...skipping 1253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1699 | 1735 |
1700 // Go unmanaged. Ensure that the DSP changes to the expected pending entry | 1736 // Go unmanaged. Ensure that the DSP changes to the expected pending entry |
1701 // from Sync. | 1737 // from Sync. |
1702 const TemplateURL* expected_default = | 1738 const TemplateURL* expected_default = |
1703 model()->GetTemplateURLForGUID("newdefault"); | 1739 model()->GetTemplateURLForGUID("newdefault"); |
1704 RemoveManagedDefaultSearchPreferences(test_util_a_->profile()); | 1740 RemoveManagedDefaultSearchPreferences(test_util_a_->profile()); |
1705 | 1741 |
1706 EXPECT_EQ(expected_default, model()->GetDefaultSearchProvider()); | 1742 EXPECT_EQ(expected_default, model()->GetDefaultSearchProvider()); |
1707 } | 1743 } |
1708 | 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 = |
| 1763 model()->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 |
1709 TEST_F(TemplateURLServiceSyncTest, SyncMergeDeletesDefault) { | 1814 TEST_F(TemplateURLServiceSyncTest, SyncMergeDeletesDefault) { |
1710 // If the value from Sync is a duplicate of the local default and is newer, it | 1815 // 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. | 1816 // should safely replace the local value and set as the new default. |
1712 TemplateURL* default_turl = model()->Add(CreateTestTemplateURL( | 1817 TemplateURL* default_turl = model()->Add(CreateTestTemplateURL( |
1713 ASCIIToUTF16("key1"), "http://key1.com/{searchTerms}", "whateverguid", | 1818 ASCIIToUTF16("key1"), "http://key1.com/{searchTerms}", "whateverguid", |
1714 10)); | 1819 10)); |
1715 model()->SetUserSelectedDefaultSearchProvider(default_turl); | 1820 model()->SetUserSelectedDefaultSearchProvider(default_turl); |
1716 | 1821 |
1717 syncer::SyncDataList initial_data = CreateInitialSyncData(); | 1822 syncer::SyncDataList initial_data = CreateInitialSyncData(); |
1718 // The key1 entry should be a duplicate of the default. | 1823 // The key1 entry should be a duplicate of the default. |
(...skipping 595 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2314 | 2419 |
2315 TEST_F(TemplateURLServiceSyncTest, NonAsciiKeywordDoesNotCrash) { | 2420 TEST_F(TemplateURLServiceSyncTest, NonAsciiKeywordDoesNotCrash) { |
2316 model()->Add(CreateTestTemplateURL(UTF8ToUTF16("\xf0\xaf\xa6\x8d"), | 2421 model()->Add(CreateTestTemplateURL(UTF8ToUTF16("\xf0\xaf\xa6\x8d"), |
2317 "http://key1.com")); | 2422 "http://key1.com")); |
2318 syncer::SyncDataList initial_data = CreateInitialSyncData(); | 2423 syncer::SyncDataList initial_data = CreateInitialSyncData(); |
2319 | 2424 |
2320 model()->MergeDataAndStartSyncing( | 2425 model()->MergeDataAndStartSyncing( |
2321 syncer::SEARCH_ENGINES, initial_data, PassProcessor(), | 2426 syncer::SEARCH_ENGINES, initial_data, PassProcessor(), |
2322 CreateAndPassSyncErrorFactory()); | 2427 CreateAndPassSyncErrorFactory()); |
2323 } | 2428 } |
OLD | NEW |