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

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

Issue 9749012: [Sync] Have SyncableService's take ownership of their SyncChangeProcessor. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Comment Created 8 years, 9 months 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 | Annotate | Revision Log
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 "base/memory/scoped_ptr.h" 5 #include "base/memory/scoped_ptr.h"
6 #include "base/string_util.h" 6 #include "base/string_util.h"
7 #include "base/time.h" 7 #include "base/time.h"
8 #include "base/utf_string_conversions.h" 8 #include "base/utf_string_conversions.h"
9 #include "chrome/browser/search_engines/template_url.h" 9 #include "chrome/browser/search_engines/template_url.h"
10 #include "chrome/browser/search_engines/template_url_service.h" 10 #include "chrome/browser/search_engines/template_url_service.h"
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 void set_erroneous(bool erroneous) { erroneous_ = erroneous; } 120 void set_erroneous(bool erroneous) { erroneous_ = erroneous; }
121 121
122 private: 122 private:
123 // Track the changes received in ProcessSyncChanges. 123 // Track the changes received in ProcessSyncChanges.
124 std::map<std::string, SyncChange> change_map_; 124 std::map<std::string, SyncChange> change_map_;
125 bool erroneous_; 125 bool erroneous_;
126 126
127 DISALLOW_COPY_AND_ASSIGN(TestChangeProcessor); 127 DISALLOW_COPY_AND_ASSIGN(TestChangeProcessor);
128 }; 128 };
129 129
130 class SyncChangeProcessorDelegate : public SyncChangeProcessor {
131 public:
132 explicit SyncChangeProcessorDelegate(SyncChangeProcessor* recipient)
133 : recipient_(recipient) {
134 DCHECK(recipient_);
135 }
136 virtual ~SyncChangeProcessorDelegate() {}
137
138 // SyncChangeProcessor implementation.
139 virtual SyncError ProcessSyncChanges(
140 const tracked_objects::Location& from_here,
141 const SyncChangeList& change_list) OVERRIDE {
142 return recipient_->ProcessSyncChanges(from_here, change_list);
143 }
144
145 private:
146 // The recipient of all sync changes.
147 SyncChangeProcessor* recipient_;
148 };
sky 2012/03/22 21:45:30 DISALLOW_COPY_AND_ASSIGN
Nicolas Zea 2012/03/22 23:05:29 Done.
149
130 class TemplateURLServiceSyncTest : public testing::Test { 150 class TemplateURLServiceSyncTest : public testing::Test {
131 public: 151 public:
132 typedef TemplateURLService::SyncDataMap SyncDataMap; 152 typedef TemplateURLService::SyncDataMap SyncDataMap;
133 153
134 TemplateURLServiceSyncTest() {} 154 TemplateURLServiceSyncTest()
155 : sync_processor_(new TestChangeProcessor),
156 sync_processor_delegate_(new SyncChangeProcessorDelegate(
157 sync_processor_.get())) {}
135 158
136 virtual void SetUp() { 159 virtual void SetUp() {
137 profile_a_.reset(new TestingProfile); 160 profile_a_.reset(new TestingProfile);
138 TemplateURLServiceFactory::GetInstance()->RegisterUserPrefsOnProfile( 161 TemplateURLServiceFactory::GetInstance()->RegisterUserPrefsOnProfile(
139 profile_a_.get()); 162 profile_a_.get());
140 model_a_.reset(new TemplateURLService(profile_a_.get())); 163 model_a_.reset(new TemplateURLService(profile_a_.get()));
141 model_a_->Load(); 164 model_a_->Load();
142 profile_b_.reset(new TestingProfile); 165 profile_b_.reset(new TestingProfile);
143 TemplateURLServiceFactory::GetInstance()->RegisterUserPrefsOnProfile( 166 TemplateURLServiceFactory::GetInstance()->RegisterUserPrefsOnProfile(
144 profile_b_.get()); 167 profile_b_.get());
145 model_b_.reset(new TemplateURLService(profile_b_.get())); 168 model_b_.reset(new TemplateURLService(profile_b_.get()));
146 model_b_->Load(); 169 model_b_->Load();
147 } 170 }
148 171
149 virtual void TearDown() { } 172 virtual void TearDown() { }
150 173
151 TemplateURLService* model() { return model_a_.get(); } 174 TemplateURLService* model() { return model_a_.get(); }
152 // For readability, we redefine an accessor for Model A for use in tests that 175 // For readability, we redefine an accessor for Model A for use in tests that
153 // involve syncing two models. 176 // involve syncing two models.
154 TemplateURLService* model_a() { return model_a_.get(); } 177 TemplateURLService* model_a() { return model_a_.get(); }
155 TemplateURLService* model_b() { return model_b_.get(); } 178 TemplateURLService* model_b() { return model_b_.get(); }
156 TestChangeProcessor* processor() { return &processor_; } 179 TestChangeProcessor* processor() { return sync_processor_.get(); }
180 scoped_ptr<SyncChangeProcessor> PassProcessor() {
181 return sync_processor_delegate_.PassAs<SyncChangeProcessor>();
182 }
157 183
158 // Create a TemplateURL with some test values. The caller owns the returned 184 // Create a TemplateURL with some test values. The caller owns the returned
159 // TemplateURL*. 185 // TemplateURL*.
160 TemplateURL* CreateTestTemplateURL(const string16& keyword, 186 TemplateURL* CreateTestTemplateURL(const string16& keyword,
161 const std::string& url) const { 187 const std::string& url) const {
162 return CreateTestTemplateURL(keyword, url, std::string()); 188 return CreateTestTemplateURL(keyword, url, std::string());
163 } 189 }
164 190
165 TemplateURL* CreateTestTemplateURL(const string16& keyword, 191 TemplateURL* CreateTestTemplateURL(const string16& keyword,
166 const std::string& url, 192 const std::string& url,
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
268 } 294 }
269 295
270 protected: 296 protected:
271 // We keep two TemplateURLServices to test syncing between them. 297 // We keep two TemplateURLServices to test syncing between them.
272 scoped_ptr<TestingProfile> profile_a_; 298 scoped_ptr<TestingProfile> profile_a_;
273 scoped_ptr<TemplateURLService> model_a_; 299 scoped_ptr<TemplateURLService> model_a_;
274 scoped_ptr<TestingProfile> profile_b_; 300 scoped_ptr<TestingProfile> profile_b_;
275 scoped_ptr<TemplateURLService> model_b_; 301 scoped_ptr<TemplateURLService> model_b_;
276 302
277 // Our dummy ChangeProcessor used to inspect changes pushed to Sync. 303 // Our dummy ChangeProcessor used to inspect changes pushed to Sync.
278 TestChangeProcessor processor_; 304 scoped_ptr<TestChangeProcessor> sync_processor_;
305 scoped_ptr<SyncChangeProcessorDelegate> sync_processor_delegate_;
279 306
280 DISALLOW_COPY_AND_ASSIGN(TemplateURLServiceSyncTest); 307 DISALLOW_COPY_AND_ASSIGN(TemplateURLServiceSyncTest);
281 }; 308 };
282 309
283 } // namespace 310 } // namespace
284 311
285 TEST_F(TemplateURLServiceSyncTest, SerializeDeserialize) { 312 TEST_F(TemplateURLServiceSyncTest, SerializeDeserialize) {
286 // Create a TemplateURL and convert it into a sync specific type. 313 // Create a TemplateURL and convert it into a sync specific type.
287 scoped_ptr<TemplateURL> turl(CreateTestTemplateURL(ASCIIToUTF16("unittest"), 314 scoped_ptr<TemplateURL> turl(CreateTestTemplateURL(ASCIIToUTF16("unittest"),
288 "http://www.unittest.com/")); 315 "http://www.unittest.com/"));
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
514 "http://key1.com", std::string(), 8999); 541 "http://key1.com", std::string(), 8999);
515 model()->MergeSyncAndLocalURLDuplicates(sync_turl2, sync_turl, &changes); 542 model()->MergeSyncAndLocalURLDuplicates(sync_turl2, sync_turl, &changes);
516 result = model()->GetTemplateURLForKeyword(ASCIIToUTF16("key1")); 543 result = model()->GetTemplateURLForKeyword(ASCIIToUTF16("key1"));
517 ASSERT_TRUE(result); 544 ASSERT_TRUE(result);
518 EXPECT_EQ(9001, result->last_modified().ToTimeT()); 545 EXPECT_EQ(9001, result->last_modified().ToTimeT());
519 EXPECT_EQ(1U, changes.size()); 546 EXPECT_EQ(1U, changes.size());
520 } 547 }
521 548
522 TEST_F(TemplateURLServiceSyncTest, StartSyncEmpty) { 549 TEST_F(TemplateURLServiceSyncTest, StartSyncEmpty) {
523 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, SyncDataList(), 550 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, SyncDataList(),
524 processor()); 551 PassProcessor());
525 552
526 EXPECT_EQ(0U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size()); 553 EXPECT_EQ(0U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
527 EXPECT_EQ(0, processor()->change_list_size()); 554 EXPECT_EQ(0, processor()->change_list_size());
528 } 555 }
529 556
530 TEST_F(TemplateURLServiceSyncTest, MergeIntoEmpty) { 557 TEST_F(TemplateURLServiceSyncTest, MergeIntoEmpty) {
531 SyncDataList initial_data = CreateInitialSyncData(); 558 SyncDataList initial_data = CreateInitialSyncData();
532 559
533 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, initial_data, 560 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, initial_data,
534 processor()); 561 PassProcessor());
535 562
536 EXPECT_EQ(3U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size()); 563 EXPECT_EQ(3U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
537 // We expect the model to have accepted all of the initial sync data. Search 564 // We expect the model to have accepted all of the initial sync data. Search
538 // through the model using the GUIDs to ensure that they're present. 565 // through the model using the GUIDs to ensure that they're present.
539 for (SyncDataList::const_iterator iter = initial_data.begin(); 566 for (SyncDataList::const_iterator iter = initial_data.begin();
540 iter != initial_data.end(); ++iter) { 567 iter != initial_data.end(); ++iter) {
541 std::string guid = GetGUID(*iter); 568 std::string guid = GetGUID(*iter);
542 EXPECT_TRUE(model()->GetTemplateURLForGUID(guid)); 569 EXPECT_TRUE(model()->GetTemplateURLForGUID(guid));
543 } 570 }
544 571
545 EXPECT_EQ(0, processor()->change_list_size()); 572 EXPECT_EQ(0, processor()->change_list_size());
546 } 573 }
547 574
548 TEST_F(TemplateURLServiceSyncTest, MergeInAllNewData) { 575 TEST_F(TemplateURLServiceSyncTest, MergeInAllNewData) {
549 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("google.com"), 576 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("google.com"),
550 "http://google.com", "abc")); 577 "http://google.com", "abc"));
551 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("yahoo.com"), 578 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("yahoo.com"),
552 "http://yahoo.com", "def")); 579 "http://yahoo.com", "def"));
553 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("bing.com"), 580 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("bing.com"),
554 "http://bing.com", "xyz")); 581 "http://bing.com", "xyz"));
555 SyncDataList initial_data = CreateInitialSyncData(); 582 SyncDataList initial_data = CreateInitialSyncData();
556 583
557 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, initial_data, 584 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, initial_data,
558 processor()); 585 PassProcessor());
559 586
560 EXPECT_EQ(6U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size()); 587 EXPECT_EQ(6U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
561 // We expect the model to have accepted all of the initial sync data. Search 588 // We expect the model to have accepted all of the initial sync data. Search
562 // through the model using the GUIDs to ensure that they're present. 589 // through the model using the GUIDs to ensure that they're present.
563 for (SyncDataList::const_iterator iter = initial_data.begin(); 590 for (SyncDataList::const_iterator iter = initial_data.begin();
564 iter != initial_data.end(); ++iter) { 591 iter != initial_data.end(); ++iter) {
565 std::string guid = GetGUID(*iter); 592 std::string guid = GetGUID(*iter);
566 EXPECT_TRUE(model()->GetTemplateURLForGUID(guid)); 593 EXPECT_TRUE(model()->GetTemplateURLForGUID(guid));
567 } 594 }
568 // All the original TemplateURLs should also remain in the model. 595 // All the original TemplateURLs should also remain in the model.
(...skipping 12 matching lines...) Expand all
581 // been no changes since the last time we synced. Even the last_modified 608 // been no changes since the last time we synced. Even the last_modified
582 // timestamps are the same. 609 // timestamps are the same.
583 SyncDataList initial_data = CreateInitialSyncData(); 610 SyncDataList initial_data = CreateInitialSyncData();
584 for (SyncDataList::const_iterator iter = initial_data.begin(); 611 for (SyncDataList::const_iterator iter = initial_data.begin();
585 iter != initial_data.end(); ++iter) { 612 iter != initial_data.end(); ++iter) {
586 TemplateURL* converted = Deserialize(*iter); 613 TemplateURL* converted = Deserialize(*iter);
587 model()->Add(converted); 614 model()->Add(converted);
588 } 615 }
589 616
590 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, initial_data, 617 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, initial_data,
591 processor()); 618 PassProcessor());
592 619
593 EXPECT_EQ(3U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size()); 620 EXPECT_EQ(3U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
594 for (SyncDataList::const_iterator iter = initial_data.begin(); 621 for (SyncDataList::const_iterator iter = initial_data.begin();
595 iter != initial_data.end(); ++iter) { 622 iter != initial_data.end(); ++iter) {
596 std::string guid = GetGUID(*iter); 623 std::string guid = GetGUID(*iter);
597 EXPECT_TRUE(model()->GetTemplateURLForGUID(guid)); 624 EXPECT_TRUE(model()->GetTemplateURLForGUID(guid));
598 } 625 }
599 EXPECT_EQ(0, processor()->change_list_size()); 626 EXPECT_EQ(0, processor()->change_list_size());
600 } 627 }
601 628
(...skipping 12 matching lines...) Expand all
614 ASCIIToUTF16("google.com"), "http://google.ca", "abc", 9999)); 641 ASCIIToUTF16("google.com"), "http://google.ca", "abc", 9999));
615 initial_data.push_back( 642 initial_data.push_back(
616 TemplateURLService::CreateSyncDataFromTemplateURL(*turl1_newer)); 643 TemplateURLService::CreateSyncDataFromTemplateURL(*turl1_newer));
617 644
618 scoped_ptr<TemplateURL> turl2_older(CreateTestTemplateURL( 645 scoped_ptr<TemplateURL> turl2_older(CreateTestTemplateURL(
619 ASCIIToUTF16("bing.com"), "http://bing.ca", "xyz", 8888)); 646 ASCIIToUTF16("bing.com"), "http://bing.ca", "xyz", 8888));
620 initial_data.push_back( 647 initial_data.push_back(
621 TemplateURLService::CreateSyncDataFromTemplateURL(*turl2_older)); 648 TemplateURLService::CreateSyncDataFromTemplateURL(*turl2_older));
622 649
623 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, initial_data, 650 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, initial_data,
624 processor()); 651 PassProcessor());
625 652
626 // Both were local updates, so we expect the same count. 653 // Both were local updates, so we expect the same count.
627 EXPECT_EQ(2U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size()); 654 EXPECT_EQ(2U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
628 655
629 // Check that the first replaced the initial Google TemplateURL. 656 // Check that the first replaced the initial Google TemplateURL.
630 EXPECT_EQ(turl1, model()->GetTemplateURLForGUID("abc")); 657 EXPECT_EQ(turl1, model()->GetTemplateURLForGUID("abc"));
631 EXPECT_EQ("http://google.ca", turl1->url()->url()); 658 EXPECT_EQ("http://google.ca", turl1->url()->url());
632 659
633 // Check that the second produced an upstream update to the Bing TemplateURL. 660 // Check that the second produced an upstream update to the Bing TemplateURL.
634 EXPECT_EQ(1, processor()->change_list_size()); 661 EXPECT_EQ(1, processor()->change_list_size());
(...skipping 12 matching lines...) Expand all
647 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com", 674 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com",
648 "aaa", 100)); // dupe 675 "aaa", 100)); // dupe
649 676
650 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key2"), 677 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key2"),
651 "http://expected.com", "bbb", 100)); // keyword conflict 678 "http://expected.com", "bbb", 100)); // keyword conflict
652 679
653 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("unique"), 680 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("unique"),
654 "http://unique.com", "ccc")); // add 681 "http://unique.com", "ccc")); // add
655 682
656 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, initial_data, 683 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, initial_data,
657 processor()); 684 PassProcessor());
658 685
659 // The dupe results in a merge. The other two should be added to the model. 686 // The dupe results in a merge. The other two should be added to the model.
660 EXPECT_EQ(5U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size()); 687 EXPECT_EQ(5U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
661 688
662 // The key1 duplicate results in the local copy winning. Ensure that Sync's 689 // The key1 duplicate results in the local copy winning. Ensure that Sync's
663 // copy was not added, and the local copy is pushed upstream to Sync as an 690 // copy was not added, and the local copy is pushed upstream to Sync as an
664 // update. The local copy should have received the sync data's GUID. 691 // update. The local copy should have received the sync data's GUID.
665 EXPECT_TRUE(model()->GetTemplateURLForGUID("key1")); 692 EXPECT_TRUE(model()->GetTemplateURLForGUID("key1"));
666 // Check changes for the UPDATE. 693 // Check changes for the UPDATE.
667 ASSERT_TRUE(processor()->ContainsGUID("key1")); 694 ASSERT_TRUE(processor()->ContainsGUID("key1"));
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
707 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com", 734 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com",
708 "aaa", 10)); // dupe 735 "aaa", 10)); // dupe
709 736
710 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key2"), 737 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key2"),
711 "http://expected.com", "bbb", 10)); // keyword conflict 738 "http://expected.com", "bbb", 10)); // keyword conflict
712 739
713 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("unique"), 740 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("unique"),
714 "http://unique.com", "ccc", 10)); // add 741 "http://unique.com", "ccc", 10)); // add
715 742
716 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, initial_data, 743 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, initial_data,
717 processor()); 744 PassProcessor());
718 745
719 // The dupe results in a merge. The other two should be added to the model. 746 // The dupe results in a merge. The other two should be added to the model.
720 EXPECT_EQ(5U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size()); 747 EXPECT_EQ(5U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
721 748
722 // The key1 duplicate results in Sync's copy winning. Ensure that Sync's 749 // The key1 duplicate results in Sync's copy winning. Ensure that Sync's
723 // copy replaced the local copy. 750 // copy replaced the local copy.
724 EXPECT_TRUE(model()->GetTemplateURLForGUID("key1")); 751 EXPECT_TRUE(model()->GetTemplateURLForGUID("key1"));
725 EXPECT_FALSE(model()->GetTemplateURLForGUID("aaa")); 752 EXPECT_FALSE(model()->GetTemplateURLForGUID("aaa"));
726 753
727 // The key2 keyword conflict results in Sync's copy winning, so ensure it 754 // The key2 keyword conflict results in Sync's copy winning, so ensure it
(...skipping 18 matching lines...) Expand all
746 EXPECT_EQ(SyncChange::ACTION_ADD, 773 EXPECT_EQ(SyncChange::ACTION_ADD,
747 processor()->GetChangeByGUID("bbb").change_type()); 774 processor()->GetChangeByGUID("bbb").change_type());
748 ASSERT_TRUE(processor()->ContainsGUID("ccc")); 775 ASSERT_TRUE(processor()->ContainsGUID("ccc"));
749 EXPECT_EQ(SyncChange::ACTION_ADD, 776 EXPECT_EQ(SyncChange::ACTION_ADD,
750 processor()->GetChangeByGUID("ccc").change_type()); 777 processor()->GetChangeByGUID("ccc").change_type());
751 } 778 }
752 779
753 TEST_F(TemplateURLServiceSyncTest, ProcessChangesEmptyModel) { 780 TEST_F(TemplateURLServiceSyncTest, ProcessChangesEmptyModel) {
754 // We initially have no data. 781 // We initially have no data.
755 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, SyncDataList(), 782 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, SyncDataList(),
756 processor()); 783 PassProcessor());
757 784
758 // Set up a bunch of ADDs. 785 // Set up a bunch of ADDs.
759 SyncChangeList changes; 786 SyncChangeList changes;
760 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_ADD, 787 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_ADD,
761 CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com", "key1"))); 788 CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com", "key1")));
762 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_ADD, 789 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_ADD,
763 CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://key2.com", "key2"))); 790 CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://key2.com", "key2")));
764 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_ADD, 791 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_ADD,
765 CreateTestTemplateURL(ASCIIToUTF16("key3"), "http://key3.com", "key3"))); 792 CreateTestTemplateURL(ASCIIToUTF16("key3"), "http://key3.com", "key3")));
766 793
767 model()->ProcessSyncChanges(FROM_HERE, changes); 794 model()->ProcessSyncChanges(FROM_HERE, changes);
768 795
769 EXPECT_EQ(3U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size()); 796 EXPECT_EQ(3U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
770 EXPECT_EQ(0, processor()->change_list_size()); 797 EXPECT_EQ(0, processor()->change_list_size());
771 EXPECT_TRUE(model()->GetTemplateURLForGUID("key1")); 798 EXPECT_TRUE(model()->GetTemplateURLForGUID("key1"));
772 EXPECT_TRUE(model()->GetTemplateURLForGUID("key2")); 799 EXPECT_TRUE(model()->GetTemplateURLForGUID("key2"));
773 EXPECT_TRUE(model()->GetTemplateURLForGUID("key3")); 800 EXPECT_TRUE(model()->GetTemplateURLForGUID("key3"));
774 } 801 }
775 802
776 TEST_F(TemplateURLServiceSyncTest, ProcessChangesNoConflicts) { 803 TEST_F(TemplateURLServiceSyncTest, ProcessChangesNoConflicts) {
777 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, 804 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES,
778 CreateInitialSyncData(), processor()); 805 CreateInitialSyncData(), PassProcessor());
779 806
780 // Process different types of changes, without conflicts. 807 // Process different types of changes, without conflicts.
781 SyncChangeList changes; 808 SyncChangeList changes;
782 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_ADD, 809 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_ADD,
783 CreateTestTemplateURL(ASCIIToUTF16("key4"), "http://key4.com", "key4"))); 810 CreateTestTemplateURL(ASCIIToUTF16("key4"), "http://key4.com", "key4")));
784 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_UPDATE, 811 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_UPDATE,
785 CreateTestTemplateURL(ASCIIToUTF16("newkeyword"), "http://new.com", 812 CreateTestTemplateURL(ASCIIToUTF16("newkeyword"), "http://new.com",
786 "key2"))); 813 "key2")));
787 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_DELETE, 814 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_DELETE,
788 CreateTestTemplateURL(ASCIIToUTF16("key3"), "http://key3.com", "key3"))); 815 CreateTestTemplateURL(ASCIIToUTF16("key3"), "http://key3.com", "key3")));
789 816
790 model()->ProcessSyncChanges(FROM_HERE, changes); 817 model()->ProcessSyncChanges(FROM_HERE, changes);
791 818
792 // Add one, remove one, update one, so the number shouldn't change. 819 // Add one, remove one, update one, so the number shouldn't change.
793 EXPECT_EQ(3U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size()); 820 EXPECT_EQ(3U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
794 EXPECT_EQ(0, processor()->change_list_size()); 821 EXPECT_EQ(0, processor()->change_list_size());
795 EXPECT_TRUE(model()->GetTemplateURLForGUID("key1")); 822 EXPECT_TRUE(model()->GetTemplateURLForGUID("key1"));
796 EXPECT_TRUE(model()->GetTemplateURLForGUID("key2")); 823 EXPECT_TRUE(model()->GetTemplateURLForGUID("key2"));
797 const TemplateURL* turl = model()->GetTemplateURLForGUID("key2"); 824 const TemplateURL* turl = model()->GetTemplateURLForGUID("key2");
798 EXPECT_TRUE(turl); 825 EXPECT_TRUE(turl);
799 EXPECT_EQ(ASCIIToUTF16("newkeyword"), turl->keyword()); 826 EXPECT_EQ(ASCIIToUTF16("newkeyword"), turl->keyword());
800 EXPECT_EQ("http://new.com", turl->url()->url()); 827 EXPECT_EQ("http://new.com", turl->url()->url());
801 EXPECT_FALSE(model()->GetTemplateURLForGUID("key3")); 828 EXPECT_FALSE(model()->GetTemplateURLForGUID("key3"));
802 EXPECT_TRUE(model()->GetTemplateURLForGUID("key4")); 829 EXPECT_TRUE(model()->GetTemplateURLForGUID("key4"));
803 } 830 }
804 831
805 TEST_F(TemplateURLServiceSyncTest, ProcessChangesWithConflictsSyncWins) { 832 TEST_F(TemplateURLServiceSyncTest, ProcessChangesWithConflictsSyncWins) {
806 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, 833 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES,
807 CreateInitialSyncData(), processor()); 834 CreateInitialSyncData(), PassProcessor());
808 835
809 // Process different types of changes, with conflicts. Note that all this data 836 // Process different types of changes, with conflicts. Note that all this data
810 // has a newer timestamp, so Sync will win in these scenarios. 837 // has a newer timestamp, so Sync will win in these scenarios.
811 SyncChangeList changes; 838 SyncChangeList changes;
812 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_ADD, 839 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_ADD,
813 CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://new.com", "aaa"))); 840 CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://new.com", "aaa")));
814 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_UPDATE, 841 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_UPDATE,
815 CreateTestTemplateURL(ASCIIToUTF16("key3"), "http://key3.com", "key1"))); 842 CreateTestTemplateURL(ASCIIToUTF16("key3"), "http://key3.com", "key1")));
816 843
817 model()->ProcessSyncChanges(FROM_HERE, changes); 844 model()->ProcessSyncChanges(FROM_HERE, changes);
(...skipping 15 matching lines...) Expand all
833 EXPECT_TRUE(model()->GetTemplateURLForGUID("key1")); 860 EXPECT_TRUE(model()->GetTemplateURLForGUID("key1"));
834 EXPECT_EQ(model()->GetTemplateURLForGUID("key1"), 861 EXPECT_EQ(model()->GetTemplateURLForGUID("key1"),
835 model()->GetTemplateURLForKeyword(ASCIIToUTF16("key3"))); 862 model()->GetTemplateURLForKeyword(ASCIIToUTF16("key3")));
836 EXPECT_TRUE(model()->GetTemplateURLForGUID("key3")); 863 EXPECT_TRUE(model()->GetTemplateURLForGUID("key3"));
837 EXPECT_EQ(model()->GetTemplateURLForGUID("key3"), 864 EXPECT_EQ(model()->GetTemplateURLForGUID("key3"),
838 model()->GetTemplateURLForKeyword(ASCIIToUTF16("key3.com"))); 865 model()->GetTemplateURLForKeyword(ASCIIToUTF16("key3.com")));
839 } 866 }
840 867
841 TEST_F(TemplateURLServiceSyncTest, ProcessChangesWithConflictsLocalWins) { 868 TEST_F(TemplateURLServiceSyncTest, ProcessChangesWithConflictsLocalWins) {
842 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, 869 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES,
843 CreateInitialSyncData(), processor()); 870 CreateInitialSyncData(), PassProcessor());
844 871
845 // Process different types of changes, with conflicts. Note that all this data 872 // Process different types of changes, with conflicts. Note that all this data
846 // has an older timestamp, so the local data will win in these scenarios. 873 // has an older timestamp, so the local data will win in these scenarios.
847 SyncChangeList changes; 874 SyncChangeList changes;
848 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_ADD, 875 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_ADD,
849 CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://new.com", "aaa", 876 CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://new.com", "aaa",
850 10))); 877 10)));
851 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_UPDATE, 878 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_UPDATE,
852 CreateTestTemplateURL(ASCIIToUTF16("key3"), "http://key3.com", "key1", 879 CreateTestTemplateURL(ASCIIToUTF16("key3"), "http://key3.com", "key1",
853 10))); 880 10)));
(...skipping 26 matching lines...) Expand all
880 processor()->GetChangeByGUID("aaa").change_type()); 907 processor()->GetChangeByGUID("aaa").change_type());
881 ASSERT_TRUE(processor()->ContainsGUID("key1")); 908 ASSERT_TRUE(processor()->ContainsGUID("key1"));
882 EXPECT_EQ(SyncChange::ACTION_UPDATE, 909 EXPECT_EQ(SyncChange::ACTION_UPDATE,
883 processor()->GetChangeByGUID("key1").change_type()); 910 processor()->GetChangeByGUID("key1").change_type());
884 } 911 }
885 912
886 TEST_F(TemplateURLServiceSyncTest, ProcessTemplateURLChange) { 913 TEST_F(TemplateURLServiceSyncTest, ProcessTemplateURLChange) {
887 // Ensure that ProcessTemplateURLChange is called and pushes the correct 914 // Ensure that ProcessTemplateURLChange is called and pushes the correct
888 // changes to Sync whenever local changes are made to TemplateURLs. 915 // changes to Sync whenever local changes are made to TemplateURLs.
889 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, 916 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES,
890 CreateInitialSyncData(), processor()); 917 CreateInitialSyncData(), PassProcessor());
891 918
892 // Add a new search engine. 919 // Add a new search engine.
893 TemplateURL* new_turl = 920 TemplateURL* new_turl =
894 CreateTestTemplateURL(ASCIIToUTF16("baidu"), "http://baidu.cn", "new"); 921 CreateTestTemplateURL(ASCIIToUTF16("baidu"), "http://baidu.cn", "new");
895 model()->Add(new_turl); 922 model()->Add(new_turl);
896 EXPECT_EQ(1, processor()->change_list_size()); 923 EXPECT_EQ(1, processor()->change_list_size());
897 ASSERT_TRUE(processor()->ContainsGUID("new")); 924 ASSERT_TRUE(processor()->ContainsGUID("new"));
898 SyncChange change = processor()->GetChangeByGUID("new"); 925 SyncChange change = processor()->GetChangeByGUID("new");
899 EXPECT_EQ(SyncChange::ACTION_ADD, change.change_type()); 926 EXPECT_EQ(SyncChange::ACTION_ADD, change.change_type());
900 EXPECT_EQ("baidu", GetKeyword(change.sync_data())); 927 EXPECT_EQ("baidu", GetKeyword(change.sync_data()));
(...skipping 14 matching lines...) Expand all
915 model()->Remove(existing_turl); 942 model()->Remove(existing_turl);
916 EXPECT_EQ(1, processor()->change_list_size()); 943 EXPECT_EQ(1, processor()->change_list_size());
917 ASSERT_TRUE(processor()->ContainsGUID("key2")); 944 ASSERT_TRUE(processor()->ContainsGUID("key2"));
918 change = processor()->GetChangeByGUID("key2"); 945 change = processor()->GetChangeByGUID("key2");
919 EXPECT_EQ(SyncChange::ACTION_DELETE, change.change_type()); 946 EXPECT_EQ(SyncChange::ACTION_DELETE, change.change_type());
920 } 947 }
921 948
922 TEST_F(TemplateURLServiceSyncTest, MergeTwoClientsBasic) { 949 TEST_F(TemplateURLServiceSyncTest, MergeTwoClientsBasic) {
923 // Start off B with some empty data. 950 // Start off B with some empty data.
924 model_b()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, 951 model_b()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES,
925 CreateInitialSyncData(), processor()); 952 CreateInitialSyncData(), PassProcessor());
926 953
927 // Merge A and B. All of B's data should transfer over to A, which initially 954 // Merge A and B. All of B's data should transfer over to A, which initially
928 // has no data. 955 // has no data.
956 scoped_ptr<SyncChangeProcessorDelegate> delegate_b(
957 new SyncChangeProcessorDelegate(model_b()));
929 model_a()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, 958 model_a()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES,
930 model_b()->GetAllSyncData(syncable::SEARCH_ENGINES), model_b()); 959 model_b()->GetAllSyncData(syncable::SEARCH_ENGINES),
960 delegate_b.PassAs<SyncChangeProcessor>());
931 961
932 // They should be consistent. 962 // They should be consistent.
933 AssertEquals(model_a()->GetAllSyncData(syncable::SEARCH_ENGINES), 963 AssertEquals(model_a()->GetAllSyncData(syncable::SEARCH_ENGINES),
934 model_b()->GetAllSyncData(syncable::SEARCH_ENGINES)); 964 model_b()->GetAllSyncData(syncable::SEARCH_ENGINES));
935 } 965 }
936 966
937 TEST_F(TemplateURLServiceSyncTest, MergeTwoClientsDupesAndConflicts) { 967 TEST_F(TemplateURLServiceSyncTest, MergeTwoClientsDupesAndConflicts) {
938 // Start off B with some empty data. 968 // Start off B with some empty data.
939 model_b()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, 969 model_b()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES,
940 CreateInitialSyncData(), processor()); 970 CreateInitialSyncData(), PassProcessor());
941 971
942 // Set up A so we have some interesting duplicates and conflicts. 972 // Set up A so we have some interesting duplicates and conflicts.
943 model_a()->Add(CreateTestTemplateURL(ASCIIToUTF16("key4"), "http://key4.com", 973 model_a()->Add(CreateTestTemplateURL(ASCIIToUTF16("key4"), "http://key4.com",
944 "key4")); // Added 974 "key4")); // Added
945 model_a()->Add(CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://key2.com", 975 model_a()->Add(CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://key2.com",
946 "key2")); // Merge - Copy of key2. 976 "key2")); // Merge - Copy of key2.
947 model_a()->Add(CreateTestTemplateURL(ASCIIToUTF16("key3"), "http://key3.com", 977 model_a()->Add(CreateTestTemplateURL(ASCIIToUTF16("key3"), "http://key3.com",
948 "key5", 10)); // Merge - Dupe of key3. 978 "key5", 10)); // Merge - Dupe of key3.
949 model_a()->Add(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key6.com", 979 model_a()->Add(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key6.com",
950 "key6", 10)); // Conflict with key1 980 "key6", 10)); // Conflict with key1
951 981
952 // Merge A and B. 982 // Merge A and B.
983 scoped_ptr<SyncChangeProcessorDelegate> delegate_b(
984 new SyncChangeProcessorDelegate(model_b()));
953 model_a()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, 985 model_a()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES,
954 model_b()->GetAllSyncData(syncable::SEARCH_ENGINES), model_b()); 986 model_b()->GetAllSyncData(syncable::SEARCH_ENGINES),
987 delegate_b.PassAs<SyncChangeProcessor>());
955 988
956 // They should be consistent. 989 // They should be consistent.
957 AssertEquals(model_a()->GetAllSyncData(syncable::SEARCH_ENGINES), 990 AssertEquals(model_a()->GetAllSyncData(syncable::SEARCH_ENGINES),
958 model_b()->GetAllSyncData(syncable::SEARCH_ENGINES)); 991 model_b()->GetAllSyncData(syncable::SEARCH_ENGINES));
959 } 992 }
960 993
961 TEST_F(TemplateURLServiceSyncTest, StopSyncing) { 994 TEST_F(TemplateURLServiceSyncTest, StopSyncing) {
962 SyncError error = model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, 995 SyncError error = model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES,
963 CreateInitialSyncData(), processor()); 996 CreateInitialSyncData(), PassProcessor());
964 ASSERT_FALSE(error.IsSet()); 997 ASSERT_FALSE(error.IsSet());
965 model()->StopSyncing(syncable::SEARCH_ENGINES); 998 model()->StopSyncing(syncable::SEARCH_ENGINES);
966 999
967 SyncChangeList changes; 1000 SyncChangeList changes;
968 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_UPDATE, 1001 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_UPDATE,
969 CreateTestTemplateURL(ASCIIToUTF16("newkeyword"), "http://new.com", 1002 CreateTestTemplateURL(ASCIIToUTF16("newkeyword"), "http://new.com",
970 "key2"))); 1003 "key2")));
971 error = model()->ProcessSyncChanges(FROM_HERE, changes); 1004 error = model()->ProcessSyncChanges(FROM_HERE, changes);
972 EXPECT_TRUE(error.IsSet()); 1005 EXPECT_TRUE(error.IsSet());
973 1006
974 // Ensure that the sync changes were not accepted. 1007 // Ensure that the sync changes were not accepted.
975 EXPECT_TRUE(model()->GetTemplateURLForGUID("key2")); 1008 EXPECT_TRUE(model()->GetTemplateURLForGUID("key2"));
976 EXPECT_FALSE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("newkeyword"))); 1009 EXPECT_FALSE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("newkeyword")));
977 } 1010 }
978 1011
979 TEST_F(TemplateURLServiceSyncTest, SyncErrorOnInitialSync) { 1012 TEST_F(TemplateURLServiceSyncTest, SyncErrorOnInitialSync) {
980 processor()->set_erroneous(true); 1013 processor()->set_erroneous(true);
981 SyncError error = model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, 1014 SyncError error = model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES,
982 CreateInitialSyncData(), processor()); 1015 CreateInitialSyncData(), PassProcessor());
983 EXPECT_TRUE(error.IsSet()); 1016 EXPECT_TRUE(error.IsSet());
984 1017
985 // Ensure that if the initial merge was erroneous, then subsequence attempts 1018 // Ensure that if the initial merge was erroneous, then subsequence attempts
986 // to push data into the local model are rejected, since the model was never 1019 // to push data into the local model are rejected, since the model was never
987 // successfully associated with Sync in the first place. 1020 // successfully associated with Sync in the first place.
988 SyncChangeList changes; 1021 SyncChangeList changes;
989 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_UPDATE, 1022 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_UPDATE,
990 CreateTestTemplateURL(ASCIIToUTF16("newkeyword"), "http://new.com", 1023 CreateTestTemplateURL(ASCIIToUTF16("newkeyword"), "http://new.com",
991 "key2"))); 1024 "key2")));
992 processor()->set_erroneous(false); 1025 processor()->set_erroneous(false);
993 error = model()->ProcessSyncChanges(FROM_HERE, changes); 1026 error = model()->ProcessSyncChanges(FROM_HERE, changes);
994 EXPECT_TRUE(error.IsSet()); 1027 EXPECT_TRUE(error.IsSet());
995 1028
996 // Ensure that the sync changes were not accepted. 1029 // Ensure that the sync changes were not accepted.
997 EXPECT_TRUE(model()->GetTemplateURLForGUID("key2")); 1030 EXPECT_TRUE(model()->GetTemplateURLForGUID("key2"));
998 EXPECT_FALSE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("newkeyword"))); 1031 EXPECT_FALSE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("newkeyword")));
999 } 1032 }
1000 1033
1001 TEST_F(TemplateURLServiceSyncTest, SyncErrorOnLaterSync) { 1034 TEST_F(TemplateURLServiceSyncTest, SyncErrorOnLaterSync) {
1002 // Ensure that if the SyncProcessor succeeds in the initial merge, but fails 1035 // Ensure that if the SyncProcessor succeeds in the initial merge, but fails
1003 // in future ProcessSyncChanges, we still return an error. 1036 // in future ProcessSyncChanges, we still return an error.
1004 SyncError error = model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, 1037 SyncError error = model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES,
1005 CreateInitialSyncData(), processor()); 1038 CreateInitialSyncData(), PassProcessor());
1006 ASSERT_FALSE(error.IsSet()); 1039 ASSERT_FALSE(error.IsSet());
1007 1040
1008 SyncChangeList changes; 1041 SyncChangeList changes;
1009 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_UPDATE, 1042 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_UPDATE,
1010 CreateTestTemplateURL(ASCIIToUTF16("newkeyword"), "http://new.com", 1043 CreateTestTemplateURL(ASCIIToUTF16("newkeyword"), "http://new.com",
1011 "key2"))); 1044 "key2")));
1012 processor()->set_erroneous(true); 1045 processor()->set_erroneous(true);
1013 error = model()->ProcessSyncChanges(FROM_HERE, changes); 1046 error = model()->ProcessSyncChanges(FROM_HERE, changes);
1014 EXPECT_TRUE(error.IsSet()); 1047 EXPECT_TRUE(error.IsSet());
1015 } 1048 }
1016 1049
1017 TEST_F(TemplateURLServiceSyncTest, MergeTwiceWithSameSyncData) { 1050 TEST_F(TemplateURLServiceSyncTest, MergeTwiceWithSameSyncData) {
1018 // Ensure that a second merge with the same data as the first does not 1051 // Ensure that a second merge with the same data as the first does not
1019 // actually update the local data. 1052 // actually update the local data.
1020 SyncDataList initial_data; 1053 SyncDataList initial_data;
1021 initial_data.push_back(CreateInitialSyncData()[0]); 1054 initial_data.push_back(CreateInitialSyncData()[0]);
1022 1055
1023 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com", 1056 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com",
1024 "key1", 10)); // earlier 1057 "key1", 10)); // earlier
1025 1058
1026 SyncError error = model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, 1059 SyncError error = model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES,
1027 initial_data, processor()); 1060 initial_data, PassProcessor());
1028 ASSERT_FALSE(error.IsSet()); 1061 ASSERT_FALSE(error.IsSet());
1029 1062
1030 // We should have updated the original TemplateURL with Sync's version. 1063 // We should have updated the original TemplateURL with Sync's version.
1031 // Keep a copy of it so we can compare it after we re-merge. 1064 // Keep a copy of it so we can compare it after we re-merge.
1032 ASSERT_TRUE(model()->GetTemplateURLForGUID("key1")); 1065 ASSERT_TRUE(model()->GetTemplateURLForGUID("key1"));
1033 TemplateURL updated_turl(*model()->GetTemplateURLForGUID("key1")); 1066 TemplateURL updated_turl(*model()->GetTemplateURLForGUID("key1"));
1034 EXPECT_EQ(Time::FromTimeT(90), updated_turl.last_modified()); 1067 EXPECT_EQ(Time::FromTimeT(90), updated_turl.last_modified());
1035 1068
1036 // Modify a single field of the initial data. This should not be updated in 1069 // Modify a single field of the initial data. This should not be updated in
1037 // the second merge, as the last_modified timestamp remains the same. 1070 // the second merge, as the last_modified timestamp remains the same.
1038 scoped_ptr<TemplateURL> temp_turl(Deserialize(initial_data[0])); 1071 scoped_ptr<TemplateURL> temp_turl(Deserialize(initial_data[0]));
1039 temp_turl->set_short_name(ASCIIToUTF16("SomethingDifferent")); 1072 temp_turl->set_short_name(ASCIIToUTF16("SomethingDifferent"));
1040 initial_data.clear(); 1073 initial_data.clear();
1041 initial_data.push_back( 1074 initial_data.push_back(
1042 TemplateURLService::CreateSyncDataFromTemplateURL(*temp_turl)); 1075 TemplateURLService::CreateSyncDataFromTemplateURL(*temp_turl));
1043 1076
1044 // Remerge the data again. This simulates shutting down and syncing again 1077 // Remerge the data again. This simulates shutting down and syncing again
1045 // at a different time, but the cloud data has not changed. 1078 // at a different time, but the cloud data has not changed.
1046 model()->StopSyncing(syncable::SEARCH_ENGINES); 1079 model()->StopSyncing(syncable::SEARCH_ENGINES);
1080 sync_processor_delegate_.reset(new SyncChangeProcessorDelegate(
1081 sync_processor_.get()));
1047 error = model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, 1082 error = model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES,
1048 initial_data, processor()); 1083 initial_data, PassProcessor());
1049 ASSERT_FALSE(error.IsSet()); 1084 ASSERT_FALSE(error.IsSet());
1050 1085
1051 // Check that the TemplateURL was not modified. 1086 // Check that the TemplateURL was not modified.
1052 const TemplateURL* reupdated_turl = model()->GetTemplateURLForGUID("key1"); 1087 const TemplateURL* reupdated_turl = model()->GetTemplateURLForGUID("key1");
1053 ASSERT_TRUE(reupdated_turl); 1088 ASSERT_TRUE(reupdated_turl);
1054 AssertEquals(updated_turl, *reupdated_turl); 1089 AssertEquals(updated_turl, *reupdated_turl);
1055 } 1090 }
1056 1091
1057 TEST_F(TemplateURLServiceSyncTest, SyncedDefaultGUIDArrivesFirst) { 1092 TEST_F(TemplateURLServiceSyncTest, SyncedDefaultGUIDArrivesFirst) {
1058 SyncDataList initial_data = CreateInitialSyncData(); 1093 SyncDataList initial_data = CreateInitialSyncData();
1059 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, initial_data, 1094 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, initial_data,
1060 processor()); 1095 PassProcessor());
1061 model()->SetDefaultSearchProvider(model()->GetTemplateURLForGUID("key2")); 1096 model()->SetDefaultSearchProvider(model()->GetTemplateURLForGUID("key2"));
1062 1097
1063 EXPECT_EQ(3U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size()); 1098 EXPECT_EQ(3U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
1064 const TemplateURL* default_search = model()->GetDefaultSearchProvider(); 1099 const TemplateURL* default_search = model()->GetDefaultSearchProvider();
1065 ASSERT_TRUE(default_search); 1100 ASSERT_TRUE(default_search);
1066 1101
1067 // Change kSyncedDefaultSearchProviderGUID to a GUID that does not exist in 1102 // Change kSyncedDefaultSearchProviderGUID to a GUID that does not exist in
1068 // the model yet. Ensure that the default has not changed in any way. 1103 // the model yet. Ensure that the default has not changed in any way.
1069 profile_a_->GetTestingPrefService()->SetString( 1104 profile_a_->GetTestingPrefService()->SetString(
1070 prefs::kSyncedDefaultSearchProviderGUID, "newdefault"); 1105 prefs::kSyncedDefaultSearchProviderGUID, "newdefault");
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
1111 // change our default since we're not quite syncing yet. 1146 // change our default since we're not quite syncing yet.
1112 profile_a_->GetTestingPrefService()->SetString( 1147 profile_a_->GetTestingPrefService()->SetString(
1113 prefs::kSyncedDefaultSearchProviderGUID, "key2"); 1148 prefs::kSyncedDefaultSearchProviderGUID, "key2");
1114 1149
1115 EXPECT_EQ(default_search, model()->GetDefaultSearchProvider()); 1150 EXPECT_EQ(default_search, model()->GetDefaultSearchProvider());
1116 1151
1117 // Now sync the initial data, which will include the search engine entry 1152 // Now sync the initial data, which will include the search engine entry
1118 // destined to become the new default. 1153 // destined to become the new default.
1119 SyncDataList initial_data = CreateInitialSyncData(); 1154 SyncDataList initial_data = CreateInitialSyncData();
1120 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, initial_data, 1155 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, initial_data,
1121 processor()); 1156 PassProcessor());
1122 1157
1123 // Ensure that the new default has been set. 1158 // Ensure that the new default has been set.
1124 EXPECT_EQ(4U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size()); 1159 EXPECT_EQ(4U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
1125 ASSERT_NE(default_search, model()->GetDefaultSearchProvider()); 1160 ASSERT_NE(default_search, model()->GetDefaultSearchProvider());
1126 ASSERT_EQ("key2", model()->GetDefaultSearchProvider()->sync_guid()); 1161 ASSERT_EQ("key2", model()->GetDefaultSearchProvider()->sync_guid());
1127 } 1162 }
1128 1163
1129 TEST_F(TemplateURLServiceSyncTest, SyncedDefaultAlreadySetOnStartup) { 1164 TEST_F(TemplateURLServiceSyncTest, SyncedDefaultAlreadySetOnStartup) {
1130 // Start with the default set to something in the model before we start 1165 // Start with the default set to something in the model before we start
1131 // syncing. 1166 // syncing.
1132 const char kGUID[] = "initdefault"; 1167 const char kGUID[] = "initdefault";
1133 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("what"), "http://thewhat.com", 1168 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("what"), "http://thewhat.com",
1134 kGUID)); 1169 kGUID));
1135 model()->SetDefaultSearchProvider(model()->GetTemplateURLForGUID(kGUID)); 1170 model()->SetDefaultSearchProvider(model()->GetTemplateURLForGUID(kGUID));
1136 1171
1137 const TemplateURL* default_search = model()->GetDefaultSearchProvider(); 1172 const TemplateURL* default_search = model()->GetDefaultSearchProvider();
1138 ASSERT_TRUE(default_search); 1173 ASSERT_TRUE(default_search);
1139 1174
1140 // Set kSyncedDefaultSearchProviderGUID to the current default. 1175 // Set kSyncedDefaultSearchProviderGUID to the current default.
1141 profile_a_->GetTestingPrefService()->SetString( 1176 profile_a_->GetTestingPrefService()->SetString(
1142 prefs::kSyncedDefaultSearchProviderGUID, kGUID); 1177 prefs::kSyncedDefaultSearchProviderGUID, kGUID);
1143 1178
1144 EXPECT_EQ(default_search, model()->GetDefaultSearchProvider()); 1179 EXPECT_EQ(default_search, model()->GetDefaultSearchProvider());
1145 1180
1146 // Now sync the initial data. 1181 // Now sync the initial data.
1147 SyncDataList initial_data = CreateInitialSyncData(); 1182 SyncDataList initial_data = CreateInitialSyncData();
1148 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, initial_data, 1183 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, initial_data,
1149 processor()); 1184 PassProcessor());
1150 1185
1151 // Ensure that the new entries were added and the default has not changed. 1186 // Ensure that the new entries were added and the default has not changed.
1152 EXPECT_EQ(4U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size()); 1187 EXPECT_EQ(4U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
1153 ASSERT_EQ(default_search, model()->GetDefaultSearchProvider()); 1188 ASSERT_EQ(default_search, model()->GetDefaultSearchProvider());
1154 } 1189 }
1155 1190
1156 TEST_F(TemplateURLServiceSyncTest, SyncWithManagedDefaultSearch) { 1191 TEST_F(TemplateURLServiceSyncTest, SyncWithManagedDefaultSearch) {
1157 // First start off with a few entries and make sure we can set an unmanaged 1192 // First start off with a few entries and make sure we can set an unmanaged
1158 // default search provider. 1193 // default search provider.
1159 SyncDataList initial_data = CreateInitialSyncData(); 1194 SyncDataList initial_data = CreateInitialSyncData();
1160 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, initial_data, 1195 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, initial_data,
1161 processor()); 1196 PassProcessor());
1162 model()->SetDefaultSearchProvider(model()->GetTemplateURLForGUID("key2")); 1197 model()->SetDefaultSearchProvider(model()->GetTemplateURLForGUID("key2"));
1163 1198
1164 EXPECT_EQ(3U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size()); 1199 EXPECT_EQ(3U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
1165 ASSERT_FALSE(model()->is_default_search_managed()); 1200 ASSERT_FALSE(model()->is_default_search_managed());
1166 ASSERT_TRUE(model()->GetDefaultSearchProvider()); 1201 ASSERT_TRUE(model()->GetDefaultSearchProvider());
1167 1202
1168 // Change the default search provider to a managed one. 1203 // Change the default search provider to a managed one.
1169 const char kName[] = "manageddefault"; 1204 const char kName[] = "manageddefault";
1170 const char kSearchURL[] = "http://manageddefault.com/search?t={searchTerms}"; 1205 const char kSearchURL[] = "http://manageddefault.com/search?t={searchTerms}";
1171 const char kIconURL[] = "http://manageddefault.com/icon.jpg"; 1206 const char kIconURL[] = "http://manageddefault.com/icon.jpg";
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
1207 TEST_F(TemplateURLServiceSyncTest, SyncMergeDeletesDefault) { 1242 TEST_F(TemplateURLServiceSyncTest, SyncMergeDeletesDefault) {
1208 // If the value from Sync is a duplicate of the local default and is newer, it 1243 // If the value from Sync is a duplicate of the local default and is newer, it
1209 // should safely replace the local value and set as the new default. 1244 // should safely replace the local value and set as the new default.
1210 TemplateURL* default_turl = CreateTestTemplateURL(ASCIIToUTF16("key1"), 1245 TemplateURL* default_turl = CreateTestTemplateURL(ASCIIToUTF16("key1"),
1211 "http://key1.com", "whateverguid", 10); 1246 "http://key1.com", "whateverguid", 10);
1212 model()->Add(default_turl); 1247 model()->Add(default_turl);
1213 model()->SetDefaultSearchProvider(default_turl); 1248 model()->SetDefaultSearchProvider(default_turl);
1214 1249
1215 // The key1 entry should be a duplicate of the default. 1250 // The key1 entry should be a duplicate of the default.
1216 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, 1251 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES,
1217 CreateInitialSyncData(), processor()); 1252 CreateInitialSyncData(), PassProcessor());
1218 1253
1219 EXPECT_EQ(3U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size()); 1254 EXPECT_EQ(3U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
1220 EXPECT_FALSE(model()->GetTemplateURLForGUID("whateverguid")); 1255 EXPECT_FALSE(model()->GetTemplateURLForGUID("whateverguid"));
1221 EXPECT_EQ(model()->GetDefaultSearchProvider(), 1256 EXPECT_EQ(model()->GetDefaultSearchProvider(),
1222 model()->GetTemplateURLForGUID("key1")); 1257 model()->GetTemplateURLForGUID("key1"));
1223 } 1258 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698