Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(477)

Side by Side Diff: chrome/browser/search_engines/template_url_service_sync_unittest.cc

Issue 2479113002: Make extensions DSE persistent in browser prefs (Closed)
Patch Set: Updated after review, round 4 Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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
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 one change because of user default engine change.
1761 EXPECT_EQ(1U, processor()->change_list_size());
1762
1763 EXPECT_EQ(3U, model()->GetAllSyncData(syncer::SEARCH_ENGINES).size());
1764 ASSERT_FALSE(model()->is_default_search_managed());
vasilii 2016/12/15 13:41:57 Seems irrelevant.
Alexander Yashkin 2016/12/15 15:47:10 Copy paste remains. Deleted.
1765 ASSERT_TRUE(model()->GetDefaultSearchProvider());
1766
1767 // Change the default search provider to a 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 // Expect still one change because of user default engine change.
1781 EXPECT_EQ(1U, processor()->change_list_size());
1782 ASSERT_TRUE(processor()->contains_guid("key2"));
1783 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE,
1784 processor()->change_for_guid("key2").change_type());
vasilii 2016/12/15 13:41:57 Those checks should go up to the place where you a
Alexander Yashkin 2016/12/15 15:47:10 Done
1785 // Check that added extension engine did not change number of engines
1786 // in sync processor.
1787 EXPECT_EQ(3U, model()->GetAllSyncData(syncer::SEARCH_ENGINES).size());
1788
1789 // Add a new entry from Sync. It should still sync in despite the default
1790 // being extension controlled.
1791 syncer::SyncChangeList changes;
1792 changes.push_back(CreateTestSyncChange(
1793 syncer::SyncChange::ACTION_ADD,
1794 CreateTestTemplateURL(ASCIIToUTF16("newkeyword"),
1795 "http://new.com/{searchTerms}", "newdefault")));
1796 model()->ProcessSyncChanges(FROM_HERE, changes);
1797
1798 EXPECT_EQ(4U, model()->GetAllSyncData(syncer::SEARCH_ENGINES).size());
1799
1800 // Change kSyncedDefaultSearchProviderGUID to point to the new entry and
vasilii 2016/12/15 13:41:57 The code below has nothing to do with Sync. I'd re
Alexander Yashkin 2016/12/15 15:47:10 I think that this code checks that while extension
1801 // ensure that the DSP remains extension controlled.
1802 profile_a()->GetTestingPrefService()->SetString(
1803 prefs::kSyncedDefaultSearchProviderGUID, "newdefault");
1804
1805 EXPECT_EQ(dsp_turl, model()->GetDefaultSearchProvider());
1806 EXPECT_TRUE(model()->IsExtensionControlledDefaultSearch());
1807
1808 // Remove extension DSE. Ensure that the DSP changes to the expected pending
1809 // entry from Sync.
1810 const TemplateURL* expected_default =
1811 model()->GetTemplateURLForGUID("newdefault");
1812 test_util_a_->RemoveExtensionDefaultSearchFromPrefs("ext");
1813
1814 EXPECT_EQ(expected_default, model()->GetDefaultSearchProvider());
1815 }
1816
1709 TEST_F(TemplateURLServiceSyncTest, SyncMergeDeletesDefault) { 1817 TEST_F(TemplateURLServiceSyncTest, SyncMergeDeletesDefault) {
1710 // If the value from Sync is a duplicate of the local default and is newer, it 1818 // 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. 1819 // should safely replace the local value and set as the new default.
1712 TemplateURL* default_turl = model()->Add(CreateTestTemplateURL( 1820 TemplateURL* default_turl = model()->Add(CreateTestTemplateURL(
1713 ASCIIToUTF16("key1"), "http://key1.com/{searchTerms}", "whateverguid", 1821 ASCIIToUTF16("key1"), "http://key1.com/{searchTerms}", "whateverguid",
1714 10)); 1822 10));
1715 model()->SetUserSelectedDefaultSearchProvider(default_turl); 1823 model()->SetUserSelectedDefaultSearchProvider(default_turl);
1716 1824
1717 syncer::SyncDataList initial_data = CreateInitialSyncData(); 1825 syncer::SyncDataList initial_data = CreateInitialSyncData();
1718 // The key1 entry should be a duplicate of the default. 1826 // The key1 entry should be a duplicate of the default.
(...skipping 596 matching lines...) Expand 10 before | Expand all | Expand 10 after
2315 2423
2316 TEST_F(TemplateURLServiceSyncTest, NonAsciiKeywordDoesNotCrash) { 2424 TEST_F(TemplateURLServiceSyncTest, NonAsciiKeywordDoesNotCrash) {
2317 model()->Add(CreateTestTemplateURL(UTF8ToUTF16("\xf0\xaf\xa6\x8d"), 2425 model()->Add(CreateTestTemplateURL(UTF8ToUTF16("\xf0\xaf\xa6\x8d"),
2318 "http://key1.com")); 2426 "http://key1.com"));
2319 syncer::SyncDataList initial_data = CreateInitialSyncData(); 2427 syncer::SyncDataList initial_data = CreateInitialSyncData();
2320 2428
2321 model()->MergeDataAndStartSyncing( 2429 model()->MergeDataAndStartSyncing(
2322 syncer::SEARCH_ENGINES, initial_data, PassProcessor(), 2430 syncer::SEARCH_ENGINES, initial_data, PassProcessor(),
2323 CreateAndPassSyncErrorFactory()); 2431 CreateAndPassSyncErrorFactory());
2324 } 2432 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698