| 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 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 | 158 |
| 158 void Shutdown() override {} | 159 void Shutdown() override {} |
| 159 void SetOwner(TemplateURLService* owner) override {} | 160 void SetOwner(TemplateURLService* owner) override {} |
| 160 void DeleteAllSearchTermsForKeyword(TemplateURLID id) override {} | 161 void DeleteAllSearchTermsForKeyword(TemplateURLID id) override {} |
| 161 void SetKeywordSearchTermsForURL( | 162 void SetKeywordSearchTermsForURL( |
| 162 const GURL& url, | 163 const GURL& url, |
| 163 TemplateURLID id, | 164 TemplateURLID id, |
| 164 const base::string16& term) override {} | 165 const base::string16& term) override {} |
| 165 void AddKeywordGeneratedVisit(const GURL& url) override {} | 166 void AddKeywordGeneratedVisit(const GURL& url) override {} |
| 166 bool IsOmniboxExtensionURL(const std::string& url) override { return false; } | 167 bool IsOmniboxExtensionURL(const std::string& url) override { return false; } |
| 168 std::string GetExtensionControllingDSEPref() override { |
| 169 return std::string(); |
| 170 } |
| 167 }; | 171 }; |
| 168 | 172 |
| 169 } // namespace | 173 } // namespace |
| 170 | 174 |
| 171 | 175 |
| 172 // TemplateURLServiceSyncTest ------------------------------------------------- | 176 // TemplateURLServiceSyncTest ------------------------------------------------- |
| 173 | 177 |
| 174 class TemplateURLServiceSyncTest : public testing::Test { | 178 class TemplateURLServiceSyncTest : public testing::Test { |
| 175 public: | 179 public: |
| 176 typedef TemplateURLService::SyncDataMap SyncDataMap; | 180 typedef TemplateURLService::SyncDataMap SyncDataMap; |
| (...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 406 | 410 |
| 407 for (syncer::SyncDataList::const_iterator iter = all_sync_data.begin(); | 411 for (syncer::SyncDataList::const_iterator iter = all_sync_data.begin(); |
| 408 iter != all_sync_data.end(); ++iter) { | 412 iter != all_sync_data.end(); ++iter) { |
| 409 std::string guid = GetGUID(*iter); | 413 std::string guid = GetGUID(*iter); |
| 410 const TemplateURL* service_turl = model()->GetTemplateURLForGUID(guid); | 414 const TemplateURL* service_turl = model()->GetTemplateURLForGUID(guid); |
| 411 std::unique_ptr<TemplateURL> deserialized(Deserialize(*iter)); | 415 std::unique_ptr<TemplateURL> deserialized(Deserialize(*iter)); |
| 412 AssertEquals(*service_turl, *deserialized); | 416 AssertEquals(*service_turl, *deserialized); |
| 413 } | 417 } |
| 414 } | 418 } |
| 415 | 419 |
| 416 TEST_F(TemplateURLServiceSyncTest, GetAllSyncDataWithExtension) { | 420 TEST_F(TemplateURLServiceSyncTest, GetAllSyncDataWithOmniboxExtension) { |
| 417 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com")); | 421 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com")); |
| 418 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://key2.com")); | 422 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://key2.com")); |
| 419 std::string fake_id("blahblahblah"); | 423 std::string fake_id("blahblahblah"); |
| 420 std::string fake_url = std::string(kOmniboxScheme) + "://" + fake_id; | 424 std::string fake_url = std::string(kOmniboxScheme) + "://" + fake_id; |
| 421 model()->RegisterOmniboxKeyword(fake_id, "unittest", "key3", fake_url); | 425 model()->RegisterOmniboxKeyword(fake_id, "unittest", "key3", fake_url); |
| 422 syncer::SyncDataList all_sync_data = | 426 syncer::SyncDataList all_sync_data = |
| 423 model()->GetAllSyncData(syncer::SEARCH_ENGINES); | 427 model()->GetAllSyncData(syncer::SEARCH_ENGINES); |
| 424 | 428 |
| 425 EXPECT_EQ(2U, all_sync_data.size()); | 429 EXPECT_EQ(2U, all_sync_data.size()); |
| 426 | 430 |
| 427 for (syncer::SyncDataList::const_iterator iter = all_sync_data.begin(); | 431 for (syncer::SyncDataList::const_iterator iter = all_sync_data.begin(); |
| 428 iter != all_sync_data.end(); ++iter) { | 432 iter != all_sync_data.end(); ++iter) { |
| 429 std::string guid = GetGUID(*iter); | 433 std::string guid = GetGUID(*iter); |
| 430 const TemplateURL* service_turl = model()->GetTemplateURLForGUID(guid); | 434 const TemplateURL* service_turl = model()->GetTemplateURLForGUID(guid); |
| 431 std::unique_ptr<TemplateURL> deserialized(Deserialize(*iter)); | 435 std::unique_ptr<TemplateURL> deserialized(Deserialize(*iter)); |
| 432 AssertEquals(*service_turl, *deserialized); | 436 AssertEquals(*service_turl, *deserialized); |
| 433 } | 437 } |
| 434 } | 438 } |
| 435 | 439 |
| 440 TEST_F(TemplateURLServiceSyncTest, GetAllSyncDataWithSearchOverrideExtension) { |
| 441 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com")); |
| 442 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://key2.com")); |
| 443 |
| 444 // Change default search provider to a extension one. |
| 445 std::unique_ptr<TemplateURLData> extension = |
| 446 GenerateDummyTemplateURLData("extension"); |
| 447 auto ext_dse = base::MakeUnique<TemplateURL>( |
| 448 *extension, TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION); |
| 449 auto ext_info = base::MakeUnique<TemplateURL::AssociatedExtensionInfo>("ext"); |
| 450 ext_info->wants_to_be_default_engine = true; |
| 451 test_util_a_->AddExtensionControlledTURL(std::move(ext_dse), |
| 452 std::move(ext_info)); |
| 453 |
| 454 const TemplateURL* ext_turl = model()->GetDefaultSearchProvider(); |
| 455 EXPECT_TRUE(model()->IsExtensionControlledDefaultSearch()); |
| 456 |
| 457 // Extension default search must not be synced across browsers. |
| 458 syncer::SyncDataList all_sync_data = |
| 459 model()->GetAllSyncData(syncer::SEARCH_ENGINES); |
| 460 |
| 461 EXPECT_EQ(2U, all_sync_data.size()); |
| 462 |
| 463 for (syncer::SyncDataList::const_iterator iter = all_sync_data.begin(); |
| 464 iter != all_sync_data.end(); ++iter) { |
| 465 std::string guid = GetGUID(*iter); |
| 466 const TemplateURL* service_turl = model()->GetTemplateURLForGUID(guid); |
| 467 std::unique_ptr<TemplateURL> deserialized(Deserialize(*iter)); |
| 468 AssertEquals(*service_turl, *deserialized); |
| 469 EXPECT_NE(TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION, |
| 470 deserialized->type()); |
| 471 EXPECT_NE(ext_turl->keyword(), deserialized->keyword()); |
| 472 EXPECT_NE(ext_turl->short_name(), deserialized->short_name()); |
| 473 EXPECT_NE(ext_turl->url(), deserialized->url()); |
| 474 } |
| 475 } |
| 476 |
| 436 TEST_F(TemplateURLServiceSyncTest, GetAllSyncDataNoManagedEngines) { | 477 TEST_F(TemplateURLServiceSyncTest, GetAllSyncDataNoManagedEngines) { |
| 437 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com")); | 478 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com")); |
| 438 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://key2.com")); | 479 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://key2.com")); |
| 439 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key3"), "http://key3.com", | 480 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key3"), "http://key3.com", |
| 440 std::string(), 100, false, true)); | 481 std::string(), 100, false, true)); |
| 441 syncer::SyncDataList all_sync_data = | 482 syncer::SyncDataList all_sync_data = |
| 442 model()->GetAllSyncData(syncer::SEARCH_ENGINES); | 483 model()->GetAllSyncData(syncer::SEARCH_ENGINES); |
| 443 | 484 |
| 444 EXPECT_EQ(2U, all_sync_data.size()); | 485 EXPECT_EQ(2U, all_sync_data.size()); |
| 445 | 486 |
| (...skipping 1253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1699 | 1740 |
| 1700 // Go unmanaged. Ensure that the DSP changes to the expected pending entry | 1741 // Go unmanaged. Ensure that the DSP changes to the expected pending entry |
| 1701 // from Sync. | 1742 // from Sync. |
| 1702 const TemplateURL* expected_default = | 1743 const TemplateURL* expected_default = |
| 1703 model()->GetTemplateURLForGUID("newdefault"); | 1744 model()->GetTemplateURLForGUID("newdefault"); |
| 1704 RemoveManagedDefaultSearchPreferences(test_util_a_->profile()); | 1745 RemoveManagedDefaultSearchPreferences(test_util_a_->profile()); |
| 1705 | 1746 |
| 1706 EXPECT_EQ(expected_default, model()->GetDefaultSearchProvider()); | 1747 EXPECT_EQ(expected_default, model()->GetDefaultSearchProvider()); |
| 1707 } | 1748 } |
| 1708 | 1749 |
| 1750 TEST_F(TemplateURLServiceSyncTest, SyncWithExtensionDefaultSearch) { |
| 1751 // First start off with a few entries and make sure we can set an extension |
| 1752 // default search provider. |
| 1753 syncer::SyncDataList initial_data = CreateInitialSyncData(); |
| 1754 model()->MergeDataAndStartSyncing(syncer::SEARCH_ENGINES, initial_data, |
| 1755 PassProcessor(), |
| 1756 CreateAndPassSyncErrorFactory()); |
| 1757 model()->SetUserSelectedDefaultSearchProvider( |
| 1758 model()->GetTemplateURLForGUID("key2")); |
| 1759 |
| 1760 EXPECT_EQ(3U, model()->GetAllSyncData(syncer::SEARCH_ENGINES).size()); |
| 1761 ASSERT_FALSE(model()->is_default_search_managed()); |
| 1762 ASSERT_TRUE(model()->GetDefaultSearchProvider()); |
| 1763 |
| 1764 // Change the default search provider to a extension one. |
| 1765 std::unique_ptr<TemplateURLData> extension = |
| 1766 GenerateDummyTemplateURLData("extensiondefault"); |
| 1767 auto ext_dse = base::MakeUnique<TemplateURL>( |
| 1768 *extension, TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION); |
| 1769 auto ext_info = base::MakeUnique<TemplateURL::AssociatedExtensionInfo>("ext"); |
| 1770 ext_info->wants_to_be_default_engine = true; |
| 1771 test_util_a_->AddExtensionControlledTURL(std::move(ext_dse), |
| 1772 std::move(ext_info)); |
| 1773 |
| 1774 const TemplateURL* dsp_turl = model()->GetDefaultSearchProvider(); |
| 1775 EXPECT_TRUE(model()->IsExtensionControlledDefaultSearch()); |
| 1776 |
| 1777 // Add a new entry from Sync. It should still sync in despite the default |
| 1778 // being extension controlled. |
| 1779 syncer::SyncChangeList changes; |
| 1780 changes.push_back(CreateTestSyncChange( |
| 1781 syncer::SyncChange::ACTION_ADD, |
| 1782 CreateTestTemplateURL(ASCIIToUTF16("newkeyword"), |
| 1783 "http://new.com/{searchTerms}", "newdefault"))); |
| 1784 model()->ProcessSyncChanges(FROM_HERE, changes); |
| 1785 |
| 1786 EXPECT_EQ(4U, model()->GetAllSyncData(syncer::SEARCH_ENGINES).size()); |
| 1787 |
| 1788 // Change kSyncedDefaultSearchProviderGUID to point to the new entry and |
| 1789 // ensure that the DSP remains extension controlled. |
| 1790 profile_a()->GetTestingPrefService()->SetString( |
| 1791 prefs::kSyncedDefaultSearchProviderGUID, "newdefault"); |
| 1792 |
| 1793 EXPECT_EQ(dsp_turl, model()->GetDefaultSearchProvider()); |
| 1794 EXPECT_TRUE(model()->IsExtensionControlledDefaultSearch()); |
| 1795 |
| 1796 // Remove extension DSE. Ensure that the DSP changes to the expected pending |
| 1797 // entry from Sync. |
| 1798 const TemplateURL* expected_default = |
| 1799 model()->GetTemplateURLForGUID("newdefault"); |
| 1800 test_util_a_->RemoveExtensionDefaultSearchFromPrefs("ext"); |
| 1801 |
| 1802 EXPECT_EQ(expected_default, model()->GetDefaultSearchProvider()); |
| 1803 } |
| 1804 |
| 1709 TEST_F(TemplateURLServiceSyncTest, SyncMergeDeletesDefault) { | 1805 TEST_F(TemplateURLServiceSyncTest, SyncMergeDeletesDefault) { |
| 1710 // If the value from Sync is a duplicate of the local default and is newer, it | 1806 // 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. | 1807 // should safely replace the local value and set as the new default. |
| 1712 TemplateURL* default_turl = model()->Add(CreateTestTemplateURL( | 1808 TemplateURL* default_turl = model()->Add(CreateTestTemplateURL( |
| 1713 ASCIIToUTF16("key1"), "http://key1.com/{searchTerms}", "whateverguid", | 1809 ASCIIToUTF16("key1"), "http://key1.com/{searchTerms}", "whateverguid", |
| 1714 10)); | 1810 10)); |
| 1715 model()->SetUserSelectedDefaultSearchProvider(default_turl); | 1811 model()->SetUserSelectedDefaultSearchProvider(default_turl); |
| 1716 | 1812 |
| 1717 syncer::SyncDataList initial_data = CreateInitialSyncData(); | 1813 syncer::SyncDataList initial_data = CreateInitialSyncData(); |
| 1718 // The key1 entry should be a duplicate of the default. | 1814 // The key1 entry should be a duplicate of the default. |
| (...skipping 596 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2315 | 2411 |
| 2316 TEST_F(TemplateURLServiceSyncTest, NonAsciiKeywordDoesNotCrash) { | 2412 TEST_F(TemplateURLServiceSyncTest, NonAsciiKeywordDoesNotCrash) { |
| 2317 model()->Add(CreateTestTemplateURL(UTF8ToUTF16("\xf0\xaf\xa6\x8d"), | 2413 model()->Add(CreateTestTemplateURL(UTF8ToUTF16("\xf0\xaf\xa6\x8d"), |
| 2318 "http://key1.com")); | 2414 "http://key1.com")); |
| 2319 syncer::SyncDataList initial_data = CreateInitialSyncData(); | 2415 syncer::SyncDataList initial_data = CreateInitialSyncData(); |
| 2320 | 2416 |
| 2321 model()->MergeDataAndStartSyncing( | 2417 model()->MergeDataAndStartSyncing( |
| 2322 syncer::SEARCH_ENGINES, initial_data, PassProcessor(), | 2418 syncer::SEARCH_ENGINES, initial_data, PassProcessor(), |
| 2323 CreateAndPassSyncErrorFactory()); | 2419 CreateAndPassSyncErrorFactory()); |
| 2324 } | 2420 } |
| OLD | NEW |