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