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

Side by Side Diff: chrome/browser/sync/internal_api/syncapi_unittest.cc

Issue 7926001: [Sync] Move change-related methods out of SyncManager::Observer (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 // Unit tests for the SyncApi. Note that a lot of the underlying 5 // Unit tests for the SyncApi. Note that a lot of the underlying
6 // functionality is provided by the Syncable layer, which has its own 6 // functionality is provided by the Syncable layer, which has its own
7 // unit tests. We'll test SyncApi specific things in this harness. 7 // unit tests. We'll test SyncApi specific things in this harness.
8 8
9 #include <cstddef> 9 #include <cstddef>
10 #include <map> 10 #include <map>
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 using browser_sync::MockJsReplyHandler; 65 using browser_sync::MockJsReplyHandler;
66 using browser_sync::ModelSafeRoutingInfo; 66 using browser_sync::ModelSafeRoutingInfo;
67 using browser_sync::ModelSafeWorker; 67 using browser_sync::ModelSafeWorker;
68 using browser_sync::ModelSafeWorkerRegistrar; 68 using browser_sync::ModelSafeWorkerRegistrar;
69 using browser_sync::sessions::SyncSessionSnapshot; 69 using browser_sync::sessions::SyncSessionSnapshot;
70 using browser_sync::WeakHandle; 70 using browser_sync::WeakHandle;
71 using syncable::GetAllRealModelTypes; 71 using syncable::GetAllRealModelTypes;
72 using syncable::kEncryptedString; 72 using syncable::kEncryptedString;
73 using syncable::ModelType; 73 using syncable::ModelType;
74 using syncable::ModelTypeSet; 74 using syncable::ModelTypeSet;
75 using test::ExpectDictDictionaryValue;
76 using test::ExpectDictStringValue; 75 using test::ExpectDictStringValue;
77 using testing::_; 76 using testing::_;
78 using testing::AnyNumber; 77 using testing::AnyNumber;
79 using testing::AtLeast; 78 using testing::AtLeast;
80 using testing::InSequence; 79 using testing::InSequence;
81 using testing::Invoke; 80 using testing::Invoke;
82 using testing::SaveArg; 81 using testing::SaveArg;
83 using testing::StrictMock; 82 using testing::StrictMock;
84 83
85 namespace sync_api { 84 namespace sync_api {
(...skipping 464 matching lines...) Expand 10 before | Expand all | Expand 10 after
550 scoped_ptr<DictionaryValue> details(node.GetDetailsAsValue()); 549 scoped_ptr<DictionaryValue> details(node.GetDetailsAsValue());
551 if (details.get()) { 550 if (details.get()) {
552 CheckNodeValue(node, *details, true); 551 CheckNodeValue(node, *details, true);
553 } else { 552 } else {
554 ADD_FAILURE(); 553 ADD_FAILURE();
555 } 554 }
556 } 555 }
557 556
558 namespace { 557 namespace {
559 558
560 void ExpectChangeRecordActionValue(ChangeRecord::Action
561 expected_value,
562 const DictionaryValue& value,
563 const std::string& key) {
564 std::string str_value;
565 EXPECT_TRUE(value.GetString(key, &str_value));
566 switch (expected_value) {
567 case ChangeRecord::ACTION_ADD:
568 EXPECT_EQ("Add", str_value);
569 break;
570 case ChangeRecord::ACTION_UPDATE:
571 EXPECT_EQ("Update", str_value);
572 break;
573 case ChangeRecord::ACTION_DELETE:
574 EXPECT_EQ("Delete", str_value);
575 break;
576 default:
577 NOTREACHED();
578 break;
579 }
580 }
581
582 void CheckNonDeleteChangeRecordValue(const ChangeRecord& record,
583 const DictionaryValue& value,
584 BaseTransaction* trans) {
585 EXPECT_NE(ChangeRecord::ACTION_DELETE, record.action);
586 ExpectChangeRecordActionValue(record.action, value, "action");
587 {
588 ReadNode node(trans);
589 EXPECT_TRUE(node.InitByIdLookup(record.id));
590 scoped_ptr<DictionaryValue> expected_details(node.GetDetailsAsValue());
591 ExpectDictDictionaryValue(*expected_details, value, "node");
592 }
593 }
594
595 void CheckDeleteChangeRecordValue(const ChangeRecord& record,
596 const DictionaryValue& value) {
597 EXPECT_EQ(ChangeRecord::ACTION_DELETE, record.action);
598 ExpectChangeRecordActionValue(record.action, value, "action");
599 DictionaryValue* node_value = NULL;
600 EXPECT_TRUE(value.GetDictionary("node", &node_value));
601 if (node_value) {
602 ExpectInt64Value(record.id, *node_value, "id");
603 scoped_ptr<DictionaryValue> expected_specifics_value(
604 browser_sync::EntitySpecificsToValue(record.specifics));
605 ExpectDictDictionaryValue(*expected_specifics_value,
606 *node_value, "specifics");
607 scoped_ptr<DictionaryValue> expected_extra_value;
608 if (record.extra.get()) {
609 expected_extra_value.reset(record.extra->ToValue());
610 }
611 Value* extra_value = NULL;
612 EXPECT_EQ(record.extra.get() != NULL,
613 node_value->Get("extra", &extra_value));
614 EXPECT_TRUE(Value::Equals(extra_value, expected_extra_value.get()));
615 }
616 }
617
618 class MockExtraChangeRecordData
619 : public ExtraPasswordChangeRecordData {
620 public:
621 MOCK_CONST_METHOD0(ToValue, DictionaryValue*());
622 };
623
624 } // namespace
625
626 TEST_F(SyncApiTest, ChangeRecordToValue) {
627 int64 child_id = MakeNode(test_user_share_.user_share(),
628 syncable::BOOKMARKS, "testtag");
629 sync_pb::EntitySpecifics child_specifics;
630 {
631 ReadTransaction trans(FROM_HERE, test_user_share_.user_share());
632 ReadNode node(&trans);
633 EXPECT_TRUE(node.InitByIdLookup(child_id));
634 child_specifics = node.GetEntry()->Get(syncable::SPECIFICS);
635 }
636
637 // Add
638 {
639 ReadTransaction trans(FROM_HERE, test_user_share_.user_share());
640 ChangeRecord record;
641 record.action = ChangeRecord::ACTION_ADD;
642 record.id = 1;
643 record.specifics = child_specifics;
644 record.extra.reset(new StrictMock<MockExtraChangeRecordData>());
645 scoped_ptr<DictionaryValue> value(record.ToValue(&trans));
646 CheckNonDeleteChangeRecordValue(record, *value, &trans);
647 }
648
649 // Update
650 {
651 ReadTransaction trans(FROM_HERE, test_user_share_.user_share());
652 ChangeRecord record;
653 record.action = ChangeRecord::ACTION_UPDATE;
654 record.id = child_id;
655 record.specifics = child_specifics;
656 record.extra.reset(new StrictMock<MockExtraChangeRecordData>());
657 scoped_ptr<DictionaryValue> value(record.ToValue(&trans));
658 CheckNonDeleteChangeRecordValue(record, *value, &trans);
659 }
660
661 // Delete (no extra)
662 {
663 ReadTransaction trans(FROM_HERE, test_user_share_.user_share());
664 ChangeRecord record;
665 record.action = ChangeRecord::ACTION_DELETE;
666 record.id = child_id + 1;
667 record.specifics = child_specifics;
668 scoped_ptr<DictionaryValue> value(record.ToValue(&trans));
669 CheckDeleteChangeRecordValue(record, *value);
670 }
671
672 // Delete (with extra)
673 {
674 ReadTransaction trans(FROM_HERE, test_user_share_.user_share());
675 ChangeRecord record;
676 record.action = ChangeRecord::ACTION_DELETE;
677 record.id = child_id + 1;
678 record.specifics = child_specifics;
679
680 DictionaryValue extra_value;
681 extra_value.SetString("foo", "bar");
682 scoped_ptr<StrictMock<MockExtraChangeRecordData> > extra(
683 new StrictMock<MockExtraChangeRecordData>());
684 EXPECT_CALL(*extra, ToValue()).Times(2).WillRepeatedly(
685 Invoke(&extra_value, &DictionaryValue::DeepCopy));
686
687 record.extra.reset(extra.release());
688 scoped_ptr<DictionaryValue> value(record.ToValue(&trans));
689 CheckDeleteChangeRecordValue(record, *value);
690 }
691 }
692
693 namespace {
694
695 class TestHttpPostProviderInterface : public HttpPostProviderInterface { 559 class TestHttpPostProviderInterface : public HttpPostProviderInterface {
696 public: 560 public:
697 virtual ~TestHttpPostProviderInterface() {} 561 virtual ~TestHttpPostProviderInterface() {}
698 562
699 virtual void SetUserAgent(const char* user_agent) OVERRIDE {} 563 virtual void SetUserAgent(const char* user_agent) OVERRIDE {}
700 virtual void SetExtraRequestHeaders(const char* headers) OVERRIDE {} 564 virtual void SetExtraRequestHeaders(const char* headers) OVERRIDE {}
701 virtual void SetURL(const char* url, int port) OVERRIDE {} 565 virtual void SetURL(const char* url, int port) OVERRIDE {}
702 virtual void SetPostPayload(const char* content_type, 566 virtual void SetPostPayload(const char* content_type,
703 int content_length, 567 int content_length,
704 const char* content) OVERRIDE {} 568 const char* content) OVERRIDE {}
(...skipping 20 matching lines...) Expand all
725 virtual HttpPostProviderInterface* Create() OVERRIDE { 589 virtual HttpPostProviderInterface* Create() OVERRIDE {
726 return new TestHttpPostProviderInterface(); 590 return new TestHttpPostProviderInterface();
727 } 591 }
728 virtual void Destroy(HttpPostProviderInterface* http) OVERRIDE { 592 virtual void Destroy(HttpPostProviderInterface* http) OVERRIDE {
729 delete http; 593 delete http;
730 } 594 }
731 }; 595 };
732 596
733 class SyncManagerObserverMock : public SyncManager::Observer { 597 class SyncManagerObserverMock : public SyncManager::Observer {
734 public: 598 public:
735 MOCK_METHOD3(OnChangesApplied,
736 void(ModelType,
737 const BaseTransaction*,
738 const ImmutableChangeRecordList&)); // NOLINT
739 MOCK_METHOD1(OnChangesComplete, void(ModelType)); // NOLINT
740 MOCK_METHOD1(OnSyncCycleCompleted, 599 MOCK_METHOD1(OnSyncCycleCompleted,
741 void(const SyncSessionSnapshot*)); // NOLINT 600 void(const SyncSessionSnapshot*)); // NOLINT
742 MOCK_METHOD2(OnInitializationComplete, 601 MOCK_METHOD2(OnInitializationComplete,
743 void(const WeakHandle<JsBackend>&, bool)); // NOLINT 602 void(const WeakHandle<JsBackend>&, bool)); // NOLINT
744 MOCK_METHOD1(OnAuthError, void(const GoogleServiceAuthError&)); // NOLINT 603 MOCK_METHOD1(OnAuthError, void(const GoogleServiceAuthError&)); // NOLINT
745 MOCK_METHOD1(OnPassphraseRequired, 604 MOCK_METHOD1(OnPassphraseRequired,
746 void(sync_api::PassphraseRequiredReason)); // NOLINT 605 void(sync_api::PassphraseRequiredReason)); // NOLINT
747 MOCK_METHOD1(OnPassphraseAccepted, void(const std::string&)); // NOLINT 606 MOCK_METHOD1(OnPassphraseAccepted, void(const std::string&)); // NOLINT
748 MOCK_METHOD0(OnStopSyncingPermanently, void()); // NOLINT 607 MOCK_METHOD0(OnStopSyncingPermanently, void()); // NOLINT
749 MOCK_METHOD1(OnUpdatedToken, void(const std::string&)); // NOLINT 608 MOCK_METHOD1(OnUpdatedToken, void(const std::string&)); // NOLINT
(...skipping 12 matching lines...) Expand all
762 MOCK_METHOD1(SetUniqueId, void(const std::string&)); 621 MOCK_METHOD1(SetUniqueId, void(const std::string&));
763 MOCK_METHOD1(SetState, void(const std::string&)); 622 MOCK_METHOD1(SetState, void(const std::string&));
764 MOCK_METHOD2(UpdateCredentials, 623 MOCK_METHOD2(UpdateCredentials,
765 void(const std::string&, const std::string&)); 624 void(const std::string&, const std::string&));
766 MOCK_METHOD1(UpdateEnabledTypes, 625 MOCK_METHOD1(UpdateEnabledTypes,
767 void(const syncable::ModelTypeSet&)); 626 void(const syncable::ModelTypeSet&));
768 MOCK_METHOD1(SendNotification, void(const syncable::ModelTypeSet&)); 627 MOCK_METHOD1(SendNotification, void(const syncable::ModelTypeSet&));
769 }; 628 };
770 629
771 class SyncManagerTest : public testing::Test, 630 class SyncManagerTest : public testing::Test,
772 public ModelSafeWorkerRegistrar { 631 public ModelSafeWorkerRegistrar,
632 public SyncManager::ChangeDelegate {
773 protected: 633 protected:
774 SyncManagerTest() 634 SyncManagerTest()
775 : ui_thread_(BrowserThread::UI, &ui_loop_), 635 : ui_thread_(BrowserThread::UI, &ui_loop_),
776 sync_notifier_mock_(NULL), 636 sync_notifier_mock_(NULL),
777 sync_manager_("Test sync manager"), 637 sync_manager_("Test sync manager"),
778 sync_notifier_observer_(NULL), 638 sync_notifier_observer_(NULL),
779 update_enabled_types_call_count_(0) {} 639 update_enabled_types_call_count_(0) {}
780 640
781 virtual ~SyncManagerTest() { 641 virtual ~SyncManagerTest() {
782 EXPECT_FALSE(sync_notifier_mock_); 642 EXPECT_FALSE(sync_notifier_mock_);
(...skipping 25 matching lines...) Expand all
808 EXPECT_CALL(observer_, OnInitializationComplete(_, _)). 668 EXPECT_CALL(observer_, OnInitializationComplete(_, _)).
809 WillOnce(SaveArg<0>(&js_backend_)); 669 WillOnce(SaveArg<0>(&js_backend_));
810 670
811 EXPECT_FALSE(sync_notifier_observer_); 671 EXPECT_FALSE(sync_notifier_observer_);
812 EXPECT_FALSE(js_backend_.IsInitialized()); 672 EXPECT_FALSE(js_backend_.IsInitialized());
813 673
814 // Takes ownership of |sync_notifier_mock_|. 674 // Takes ownership of |sync_notifier_mock_|.
815 sync_manager_.Init(temp_dir_.path(), 675 sync_manager_.Init(temp_dir_.path(),
816 WeakHandle<JsEventHandler>(), 676 WeakHandle<JsEventHandler>(),
817 "bogus", 0, false, 677 "bogus", 0, false,
818 new TestHttpPostProviderFactory(), this, "bogus", 678 new TestHttpPostProviderFactory(), this, this, "bogus",
819 credentials, sync_notifier_mock_, "", 679 credentials, sync_notifier_mock_, "",
820 true /* setup_for_test_mode */); 680 true /* setup_for_test_mode */);
821 681
822 EXPECT_TRUE(sync_notifier_observer_); 682 EXPECT_TRUE(sync_notifier_observer_);
823 EXPECT_TRUE(js_backend_.IsInitialized()); 683 EXPECT_TRUE(js_backend_.IsInitialized());
824 684
825 EXPECT_EQ(1, update_enabled_types_call_count_); 685 EXPECT_EQ(1, update_enabled_types_call_count_);
826 686
827 ModelSafeRoutingInfo routes; 687 ModelSafeRoutingInfo routes;
828 GetModelSafeRoutingInfo(&routes); 688 GetModelSafeRoutingInfo(&routes);
829 for (ModelSafeRoutingInfo::iterator i = routes.begin(); i != routes.end(); 689 for (ModelSafeRoutingInfo::iterator i = routes.begin(); i != routes.end();
830 ++i) { 690 ++i) {
831 EXPECT_CALL(observer_, OnChangesApplied(i->first, _, _))
832 .RetiresOnSaturation();
833 EXPECT_CALL(observer_, OnChangesComplete(i->first))
834 .RetiresOnSaturation();
835 type_roots_[i->first] = MakeServerNodeForType( 691 type_roots_[i->first] = MakeServerNodeForType(
836 sync_manager_.GetUserShare(), i->first); 692 sync_manager_.GetUserShare(), i->first);
837 } 693 }
838 PumpLoop(); 694 PumpLoop();
839 } 695 }
840 696
841 void TearDown() { 697 void TearDown() {
842 sync_manager_.RemoveObserver(&observer_); 698 sync_manager_.RemoveObserver(&observer_);
843 sync_manager_.Shutdown(); 699 sync_manager_.Shutdown();
844 sync_notifier_mock_ = NULL; 700 sync_notifier_mock_ = NULL;
845 EXPECT_FALSE(sync_notifier_observer_); 701 EXPECT_FALSE(sync_notifier_observer_);
846 PumpLoop(); 702 PumpLoop();
847 } 703 }
848 704
849 // ModelSafeWorkerRegistrar implementation. 705 // ModelSafeWorkerRegistrar implementation.
850 virtual void GetWorkers(std::vector<ModelSafeWorker*>* out) { 706 virtual void GetWorkers(std::vector<ModelSafeWorker*>* out) OVERRIDE {
851 NOTIMPLEMENTED(); 707 NOTIMPLEMENTED();
852 out->clear(); 708 out->clear();
853 } 709 }
854 virtual void GetModelSafeRoutingInfo(ModelSafeRoutingInfo* out) { 710 virtual void GetModelSafeRoutingInfo(ModelSafeRoutingInfo* out) OVERRIDE {
855 (*out)[syncable::NIGORI] = browser_sync::GROUP_PASSIVE; 711 (*out)[syncable::NIGORI] = browser_sync::GROUP_PASSIVE;
856 (*out)[syncable::BOOKMARKS] = browser_sync::GROUP_PASSIVE; 712 (*out)[syncable::BOOKMARKS] = browser_sync::GROUP_PASSIVE;
857 (*out)[syncable::THEMES] = browser_sync::GROUP_PASSIVE; 713 (*out)[syncable::THEMES] = browser_sync::GROUP_PASSIVE;
858 (*out)[syncable::SESSIONS] = browser_sync::GROUP_PASSIVE; 714 (*out)[syncable::SESSIONS] = browser_sync::GROUP_PASSIVE;
859 (*out)[syncable::PASSWORDS] = browser_sync::GROUP_PASSIVE; 715 (*out)[syncable::PASSWORDS] = browser_sync::GROUP_PASSIVE;
860 } 716 }
861 717
718 virtual void OnChangesApplied(
719 syncable::ModelType model_type,
720 const BaseTransaction* trans,
721 const ImmutableChangeRecordList& changes) OVERRIDE {}
722
723 virtual void OnChangesComplete(syncable::ModelType model_type) OVERRIDE {}
724
862 // Helper methods. 725 // Helper methods.
863 bool SetUpEncryption() { 726 bool SetUpEncryption() {
864 // Mock the Mac Keychain service. The real Keychain can block on user input. 727 // Mock the Mac Keychain service. The real Keychain can block on user input.
865 #if defined(OS_MACOSX) 728 #if defined(OS_MACOSX)
866 Encryptor::UseMockKeychain(true); 729 Encryptor::UseMockKeychain(true);
867 #endif 730 #endif
868 731
869 // We need to create the nigori node as if it were an applied server update. 732 // We need to create the nigori node as if it were an applied server update.
870 UserShare* share = sync_manager_.GetUserShare(); 733 UserShare* share = sync_manager_.GetUserShare();
871 int64 nigori_id = GetIdForDataType(syncable::NIGORI); 734 int64 nigori_id = GetIdForDataType(syncable::NIGORI);
(...skipping 677 matching lines...) Expand 10 before | Expand all | Expand 10 after
1549 ReadNode node(&trans); 1412 ReadNode node(&trans);
1550 EXPECT_TRUE(node.InitByIdLookup(node1)); 1413 EXPECT_TRUE(node.InitByIdLookup(node1));
1551 EXPECT_EQ(syncable::BOOKMARKS, node.GetModelType()); 1414 EXPECT_EQ(syncable::BOOKMARKS, node.GetModelType());
1552 EXPECT_EQ(title, node.GetTitle()); 1415 EXPECT_EQ(title, node.GetTitle());
1553 EXPECT_EQ(title, node.GetBookmarkSpecifics().title()); 1416 EXPECT_EQ(title, node.GetBookmarkSpecifics().title());
1554 EXPECT_EQ(url, node.GetBookmarkSpecifics().url()); 1417 EXPECT_EQ(url, node.GetBookmarkSpecifics().url());
1555 } 1418 }
1556 } 1419 }
1557 1420
1558 } // namespace browser_sync 1421 } // namespace browser_sync
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698