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