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

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

Issue 10702074: Refactor sync-specific parts out of SyncNotifier/SyncNotifierObserver (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: FOR_THE_HORDE Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // 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 677 matching lines...) Expand 10 before | Expand all | Expand 10 after
688 MOCK_METHOD1(OnUpdatedToken, void(const std::string&)); // NOLINT 688 MOCK_METHOD1(OnUpdatedToken, void(const std::string&)); // NOLINT
689 MOCK_METHOD2(OnEncryptedTypesChanged, 689 MOCK_METHOD2(OnEncryptedTypesChanged,
690 void(ModelTypeSet, bool)); // NOLINT 690 void(ModelTypeSet, bool)); // NOLINT
691 MOCK_METHOD0(OnEncryptionComplete, void()); // NOLINT 691 MOCK_METHOD0(OnEncryptionComplete, void()); // NOLINT
692 MOCK_METHOD1(OnActionableError, 692 MOCK_METHOD1(OnActionableError,
693 void(const SyncProtocolError&)); // NOLINT 693 void(const SyncProtocolError&)); // NOLINT
694 }; 694 };
695 695
696 class SyncNotifierMock : public SyncNotifier { 696 class SyncNotifierMock : public SyncNotifier {
697 public: 697 public:
698 MOCK_METHOD1(AddObserver, void(SyncNotifierObserver*)); 698 MOCK_METHOD2(UpdateRegisteredIds, void(SyncNotifierObserver*,
699 MOCK_METHOD1(RemoveObserver, void(SyncNotifierObserver*)); 699 const ObjectIdSet&));
700 MOCK_METHOD1(SetUniqueId, void(const std::string&)); 700 MOCK_METHOD1(SetUniqueId, void(const std::string&));
701 MOCK_METHOD1(SetStateDeprecated, void(const std::string&)); 701 MOCK_METHOD1(SetStateDeprecated, void(const std::string&));
702 MOCK_METHOD2(UpdateCredentials, 702 MOCK_METHOD2(UpdateCredentials,
703 void(const std::string&, const std::string&)); 703 void(const std::string&, const std::string&));
704 MOCK_METHOD1(UpdateEnabledTypes,
705 void(ModelTypeSet));
706 MOCK_METHOD1(SendNotification, void(ModelTypeSet)); 704 MOCK_METHOD1(SendNotification, void(ModelTypeSet));
707 }; 705 };
708 706
709 } // namespace 707 } // namespace
710 708
711 class SyncManagerTest : public testing::Test, 709 class SyncManagerTest : public testing::Test,
712 public SyncManager::ChangeDelegate { 710 public SyncManager::ChangeDelegate {
713 protected: 711 protected:
714 enum NigoriStatus { 712 enum NigoriStatus {
715 DONT_WRITE_NIGORI, 713 DONT_WRITE_NIGORI,
716 WRITE_TO_NIGORI 714 WRITE_TO_NIGORI
717 }; 715 };
718 716
719 enum EncryptionStatus { 717 enum EncryptionStatus {
720 UNINITIALIZED, 718 UNINITIALIZED,
721 DEFAULT_ENCRYPTION, 719 DEFAULT_ENCRYPTION,
722 FULL_ENCRYPTION 720 FULL_ENCRYPTION
723 }; 721 };
724 722
725 SyncManagerTest() 723 SyncManagerTest()
726 : sync_notifier_mock_(NULL), 724 : sync_notifier_mock_(NULL),
727 sync_manager_("Test sync manager"), 725 sync_manager_("Test sync manager") {}
akalin 2012/07/21 01:09:47 init sync_notifier_mock_ to NULL
dcheng 2012/07/21 14:06:53 Err... it is on line 724? =) (I didn't add it, I j
728 sync_notifier_observer_(NULL),
729 update_enabled_types_call_count_(0) {}
730 726
731 virtual ~SyncManagerTest() { 727 virtual ~SyncManagerTest() {
732 EXPECT_FALSE(sync_notifier_mock_); 728 EXPECT_FALSE(sync_notifier_mock_);
733 } 729 }
734 730
735 // Test implementation. 731 // Test implementation.
736 void SetUp() { 732 void SetUp() {
737 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 733 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
738 734
739 SyncCredentials credentials; 735 SyncCredentials credentials;
740 credentials.email = "foo@bar.com"; 736 credentials.email = "foo@bar.com";
741 credentials.sync_token = "sometoken"; 737 credentials.sync_token = "sometoken";
742 738
743 sync_notifier_mock_ = new StrictMock<SyncNotifierMock>(); 739 sync_notifier_mock_ = new StrictMock<SyncNotifierMock>();
744 EXPECT_CALL(*sync_notifier_mock_, AddObserver(_)).
745 WillOnce(Invoke(this, &SyncManagerTest::SyncNotifierAddObserver));
746 EXPECT_CALL(*sync_notifier_mock_, SetUniqueId(_)); 740 EXPECT_CALL(*sync_notifier_mock_, SetUniqueId(_));
747 EXPECT_CALL(*sync_notifier_mock_, SetStateDeprecated("")); 741 EXPECT_CALL(*sync_notifier_mock_, SetStateDeprecated(""));
748 EXPECT_CALL(*sync_notifier_mock_, 742 EXPECT_CALL(*sync_notifier_mock_,
749 UpdateCredentials(credentials.email, credentials.sync_token)); 743 UpdateCredentials(credentials.email, credentials.sync_token));
750 EXPECT_CALL(*sync_notifier_mock_, UpdateEnabledTypes(_)).
751 WillRepeatedly(
752 Invoke(this, &SyncManagerTest::SyncNotifierUpdateEnabledTypes));
753 EXPECT_CALL(*sync_notifier_mock_, RemoveObserver(_)).
754 WillOnce(Invoke(this, &SyncManagerTest::SyncNotifierRemoveObserver));
755 744
756 sync_manager_.AddObserver(&observer_); 745 sync_manager_.AddObserver(&observer_);
757 EXPECT_CALL(observer_, OnInitializationComplete(_, _)). 746 EXPECT_CALL(observer_, OnInitializationComplete(_, _)).
758 WillOnce(SaveArg<0>(&js_backend_)); 747 WillOnce(SaveArg<0>(&js_backend_));
759 748
760 EXPECT_FALSE(sync_notifier_observer_);
761 EXPECT_FALSE(js_backend_.IsInitialized()); 749 EXPECT_FALSE(js_backend_.IsInitialized());
762 750
763 std::vector<ModelSafeWorker*> workers; 751 std::vector<ModelSafeWorker*> workers;
764 ModelSafeRoutingInfo routing_info; 752 ModelSafeRoutingInfo routing_info;
765 GetModelSafeRoutingInfo(&routing_info); 753 GetModelSafeRoutingInfo(&routing_info);
766 754
767 // Takes ownership of |sync_notifier_mock_|. 755 // Takes ownership of |sync_notifier_mock_|.
768 sync_manager_.Init(temp_dir_.path(), 756 sync_manager_.Init(temp_dir_.path(),
769 WeakHandle<JsEventHandler>(), 757 WeakHandle<JsEventHandler>(),
770 "bogus", 0, false, 758 "bogus", 0, false,
771 base::MessageLoopProxy::current(), 759 base::MessageLoopProxy::current(),
772 scoped_ptr<HttpPostProviderFactory>( 760 scoped_ptr<HttpPostProviderFactory>(
773 new TestHttpPostProviderFactory()), 761 new TestHttpPostProviderFactory()),
774 routing_info, workers, 762 routing_info, workers,
775 &extensions_activity_monitor_, this, 763 &extensions_activity_monitor_, this,
776 credentials, 764 credentials,
777 scoped_ptr<SyncNotifier>(sync_notifier_mock_), 765 scoped_ptr<SyncNotifier>(sync_notifier_mock_),
778 "", 766 "",
779 scoped_ptr<InternalComponentsFactory>(GetFactory()), 767 scoped_ptr<InternalComponentsFactory>(GetFactory()),
780 &encryptor_, 768 &encryptor_,
781 &handler_, 769 &handler_,
782 NULL); 770 NULL);
783 771
784 EXPECT_TRUE(sync_notifier_observer_);
785 EXPECT_TRUE(js_backend_.IsInitialized()); 772 EXPECT_TRUE(js_backend_.IsInitialized());
786 773
787 EXPECT_EQ(0, update_enabled_types_call_count_);
788
789 for (ModelSafeRoutingInfo::iterator i = routing_info.begin(); 774 for (ModelSafeRoutingInfo::iterator i = routing_info.begin();
790 i != routing_info.end(); ++i) { 775 i != routing_info.end(); ++i) {
791 type_roots_[i->first] = MakeServerNodeForType( 776 type_roots_[i->first] = MakeServerNodeForType(
792 sync_manager_.GetUserShare(), i->first); 777 sync_manager_.GetUserShare(), i->first);
793 } 778 }
794 PumpLoop(); 779 PumpLoop();
795 } 780 }
796 781
797 void TearDown() { 782 void TearDown() {
798 sync_manager_.RemoveObserver(&observer_); 783 sync_manager_.RemoveObserver(&observer_);
784 EXPECT_CALL(*sync_notifier_mock_,
785 UpdateRegisteredIds(_, ObjectIdSet()));
799 sync_manager_.ShutdownOnSyncThread(); 786 sync_manager_.ShutdownOnSyncThread();
800 sync_notifier_mock_ = NULL; 787 sync_notifier_mock_ = NULL;
801 EXPECT_FALSE(sync_notifier_observer_);
802 PumpLoop(); 788 PumpLoop();
803 } 789 }
804 790
805 void GetModelSafeRoutingInfo(ModelSafeRoutingInfo* out) { 791 void GetModelSafeRoutingInfo(ModelSafeRoutingInfo* out) {
806 (*out)[NIGORI] = GROUP_PASSIVE; 792 (*out)[NIGORI] = GROUP_PASSIVE;
807 (*out)[BOOKMARKS] = GROUP_PASSIVE; 793 (*out)[BOOKMARKS] = GROUP_PASSIVE;
808 (*out)[THEMES] = GROUP_PASSIVE; 794 (*out)[THEMES] = GROUP_PASSIVE;
809 (*out)[SESSIONS] = GROUP_PASSIVE; 795 (*out)[SESSIONS] = GROUP_PASSIVE;
810 (*out)[PASSWORDS] = GROUP_PASSIVE; 796 (*out)[PASSWORDS] = GROUP_PASSIVE;
811 (*out)[PREFERENCES] = GROUP_PASSIVE; 797 (*out)[PREFERENCES] = GROUP_PASSIVE;
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
852 } 838 }
853 return cryptographer->is_ready(); 839 return cryptographer->is_ready();
854 } 840 }
855 841
856 int64 GetIdForDataType(ModelType type) { 842 int64 GetIdForDataType(ModelType type) {
857 if (type_roots_.count(type) == 0) 843 if (type_roots_.count(type) == 0)
858 return 0; 844 return 0;
859 return type_roots_[type]; 845 return type_roots_[type];
860 } 846 }
861 847
862 void SyncNotifierAddObserver(
863 SyncNotifierObserver* sync_notifier_observer) {
864 EXPECT_EQ(NULL, sync_notifier_observer_);
865 sync_notifier_observer_ = sync_notifier_observer;
866 }
867
868 void SyncNotifierRemoveObserver(
869 SyncNotifierObserver* sync_notifier_observer) {
870 EXPECT_EQ(sync_notifier_observer_, sync_notifier_observer);
871 sync_notifier_observer_ = NULL;
872 }
873
874 void SyncNotifierUpdateEnabledTypes(ModelTypeSet types) {
875 ModelSafeRoutingInfo routes;
876 GetModelSafeRoutingInfo(&routes);
877 const ModelTypeSet expected_types = GetRoutingInfoTypes(routes);
878 EXPECT_TRUE(types.Equals(expected_types));
879 ++update_enabled_types_call_count_;
880 }
881
882 void PumpLoop() { 848 void PumpLoop() {
883 message_loop_.RunAllPending(); 849 message_loop_.RunAllPending();
884 } 850 }
885 851
886 void SendJsMessage(const std::string& name, const JsArgList& args, 852 void SendJsMessage(const std::string& name, const JsArgList& args,
887 const WeakHandle<JsReplyHandler>& reply_handler) { 853 const WeakHandle<JsReplyHandler>& reply_handler) {
888 js_backend_.Call(FROM_HERE, &JsBackend::ProcessJsMessage, 854 js_backend_.Call(FROM_HERE, &JsBackend::ProcessJsMessage,
889 name, args, reply_handler); 855 name, args, reply_handler);
890 PumpLoop(); 856 PumpLoop();
891 } 857 }
(...skipping 28 matching lines...) Expand all
920 } 886 }
921 887
922 private: 888 private:
923 // Needed by |sync_manager_|. 889 // Needed by |sync_manager_|.
924 MessageLoop message_loop_; 890 MessageLoop message_loop_;
925 // Needed by |sync_manager_|. 891 // Needed by |sync_manager_|.
926 ScopedTempDir temp_dir_; 892 ScopedTempDir temp_dir_;
927 // Sync Id's for the roots of the enabled datatypes. 893 // Sync Id's for the roots of the enabled datatypes.
928 std::map<ModelType, int64> type_roots_; 894 std::map<ModelType, int64> type_roots_;
929 FakeExtensionsActivityMonitor extensions_activity_monitor_; 895 FakeExtensionsActivityMonitor extensions_activity_monitor_;
930 StrictMock<SyncNotifierMock>* sync_notifier_mock_;
931 896
932 protected: 897 protected:
933 FakeEncryptor encryptor_; 898 FakeEncryptor encryptor_;
934 TestUnrecoverableErrorHandler handler_; 899 TestUnrecoverableErrorHandler handler_;
900 StrictMock<SyncNotifierMock>* sync_notifier_mock_;
935 SyncManagerImpl sync_manager_; 901 SyncManagerImpl sync_manager_;
936 WeakHandle<JsBackend> js_backend_; 902 WeakHandle<JsBackend> js_backend_;
937 StrictMock<SyncManagerObserverMock> observer_; 903 StrictMock<SyncManagerObserverMock> observer_;
938 SyncNotifierObserver* sync_notifier_observer_;
939 int update_enabled_types_call_count_;
940 }; 904 };
941 905
942 TEST_F(SyncManagerTest, UpdateEnabledTypes) { 906 TEST_F(SyncManagerTest, UpdateEnabledTypes) {
943 EXPECT_EQ(0, update_enabled_types_call_count_);
944
945 ModelSafeRoutingInfo routes; 907 ModelSafeRoutingInfo routes;
946 GetModelSafeRoutingInfo(&routes); 908 GetModelSafeRoutingInfo(&routes);
947 const ModelTypeSet enabled_types = GetRoutingInfoTypes(routes); 909 const ModelTypeSet enabled_types = GetRoutingInfoTypes(routes);
948 910
911 EXPECT_CALL(*sync_notifier_mock_,
912 UpdateRegisteredIds(_, ModelTypeSetToObjectIdSet(enabled_types)));
949 sync_manager_.UpdateEnabledTypes(enabled_types); 913 sync_manager_.UpdateEnabledTypes(enabled_types);
950 EXPECT_EQ(1, update_enabled_types_call_count_);
951 } 914 }
952 915
953 TEST_F(SyncManagerTest, ProcessJsMessage) { 916 TEST_F(SyncManagerTest, ProcessJsMessage) {
954 const JsArgList kNoArgs; 917 const JsArgList kNoArgs;
955 918
956 StrictMock<MockJsReplyHandler> reply_handler; 919 StrictMock<MockJsReplyHandler> reply_handler;
957 920
958 ListValue false_args; 921 ListValue false_args;
959 false_args.Append(Value::CreateBooleanValue(false)); 922 false_args.Append(Value::CreateBooleanValue(false));
960 923
(...skipping 1667 matching lines...) Expand 10 before | Expand all | Expand 10 after
2628 // Ensure only bookmarks and nigori lost their progress marker. Preferences 2591 // Ensure only bookmarks and nigori lost their progress marker. Preferences
2629 // should still have it. 2592 // should still have it.
2630 partial_types = 2593 partial_types =
2631 sync_manager_.GetTypesWithEmptyProgressMarkerToken(ModelTypeSet::All()); 2594 sync_manager_.GetTypesWithEmptyProgressMarkerToken(ModelTypeSet::All());
2632 EXPECT_TRUE(partial_types.Has(NIGORI)); 2595 EXPECT_TRUE(partial_types.Has(NIGORI));
2633 EXPECT_TRUE(partial_types.Has(BOOKMARKS)); 2596 EXPECT_TRUE(partial_types.Has(BOOKMARKS));
2634 EXPECT_FALSE(partial_types.Has(PREFERENCES)); 2597 EXPECT_FALSE(partial_types.Has(PREFERENCES));
2635 } 2598 }
2636 2599
2637 } // namespace 2600 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698