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 // 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 676 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
687 MOCK_METHOD1(OnUpdatedToken, void(const std::string&)); // NOLINT | 687 MOCK_METHOD1(OnUpdatedToken, void(const std::string&)); // NOLINT |
688 MOCK_METHOD2(OnEncryptedTypesChanged, | 688 MOCK_METHOD2(OnEncryptedTypesChanged, |
689 void(ModelTypeSet, bool)); // NOLINT | 689 void(ModelTypeSet, bool)); // NOLINT |
690 MOCK_METHOD0(OnEncryptionComplete, void()); // NOLINT | 690 MOCK_METHOD0(OnEncryptionComplete, void()); // NOLINT |
691 MOCK_METHOD1(OnActionableError, | 691 MOCK_METHOD1(OnActionableError, |
692 void(const syncer::SyncProtocolError&)); // NOLINT | 692 void(const syncer::SyncProtocolError&)); // NOLINT |
693 }; | 693 }; |
694 | 694 |
695 class SyncNotifierMock : public syncer::SyncNotifier { | 695 class SyncNotifierMock : public syncer::SyncNotifier { |
696 public: | 696 public: |
697 MOCK_METHOD1(AddObserver, void(syncer::SyncNotifierObserver*)); | 697 MOCK_METHOD2(UpdateRegisteredIds, void(SyncNotifierObserver*, |
698 MOCK_METHOD1(RemoveObserver, void(syncer::SyncNotifierObserver*)); | 698 const ObjectIdSet&)); |
699 MOCK_METHOD1(SetUniqueId, void(const std::string&)); | 699 MOCK_METHOD1(SetUniqueId, void(const std::string&)); |
700 MOCK_METHOD1(SetStateDeprecated, void(const std::string&)); | 700 MOCK_METHOD1(SetStateDeprecated, void(const std::string&)); |
701 MOCK_METHOD2(UpdateCredentials, | 701 MOCK_METHOD2(UpdateCredentials, |
702 void(const std::string&, const std::string&)); | 702 void(const std::string&, const std::string&)); |
703 MOCK_METHOD1(UpdateEnabledTypes, | |
704 void(syncer::ModelTypeSet)); | |
705 MOCK_METHOD1(SendNotification, void(syncer::ModelTypeSet)); | 703 MOCK_METHOD1(SendNotification, void(syncer::ModelTypeSet)); |
706 }; | 704 }; |
707 | 705 |
708 } // namespace | 706 } // namespace |
709 | 707 |
710 class SyncManagerTest : public testing::Test, | 708 class SyncManagerTest : public testing::Test, |
711 public SyncManager::ChangeDelegate { | 709 public SyncManager::ChangeDelegate { |
712 protected: | 710 protected: |
713 enum NigoriStatus { | 711 enum NigoriStatus { |
714 DONT_WRITE_NIGORI, | 712 DONT_WRITE_NIGORI, |
715 WRITE_TO_NIGORI | 713 WRITE_TO_NIGORI |
716 }; | 714 }; |
717 | 715 |
718 enum EncryptionStatus { | 716 enum EncryptionStatus { |
719 UNINITIALIZED, | 717 UNINITIALIZED, |
720 DEFAULT_ENCRYPTION, | 718 DEFAULT_ENCRYPTION, |
721 FULL_ENCRYPTION | 719 FULL_ENCRYPTION |
722 }; | 720 }; |
723 | 721 |
724 SyncManagerTest() | 722 SyncManagerTest() |
725 : sync_notifier_mock_(NULL), | 723 : sync_notifier_mock_(NULL), |
726 sync_manager_("Test sync manager"), | 724 sync_manager_("Test sync manager") {} |
727 sync_notifier_observer_(NULL), | |
728 update_enabled_types_call_count_(0) {} | |
729 | 725 |
730 virtual ~SyncManagerTest() { | 726 virtual ~SyncManagerTest() { |
731 EXPECT_FALSE(sync_notifier_mock_); | 727 EXPECT_FALSE(sync_notifier_mock_); |
732 } | 728 } |
733 | 729 |
734 // Test implementation. | 730 // Test implementation. |
735 void SetUp() { | 731 void SetUp() { |
736 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 732 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
737 | 733 |
738 SyncCredentials credentials; | 734 SyncCredentials credentials; |
739 credentials.email = "foo@bar.com"; | 735 credentials.email = "foo@bar.com"; |
740 credentials.sync_token = "sometoken"; | 736 credentials.sync_token = "sometoken"; |
741 | 737 |
742 sync_notifier_mock_ = new StrictMock<SyncNotifierMock>(); | 738 sync_notifier_mock_ = new StrictMock<SyncNotifierMock>(); |
743 EXPECT_CALL(*sync_notifier_mock_, AddObserver(_)). | |
744 WillOnce(Invoke(this, &SyncManagerTest::SyncNotifierAddObserver)); | |
745 EXPECT_CALL(*sync_notifier_mock_, SetUniqueId(_)); | 739 EXPECT_CALL(*sync_notifier_mock_, SetUniqueId(_)); |
746 EXPECT_CALL(*sync_notifier_mock_, SetStateDeprecated("")); | 740 EXPECT_CALL(*sync_notifier_mock_, SetStateDeprecated("")); |
747 EXPECT_CALL(*sync_notifier_mock_, | 741 EXPECT_CALL(*sync_notifier_mock_, |
748 UpdateCredentials(credentials.email, credentials.sync_token)); | 742 UpdateCredentials(credentials.email, credentials.sync_token)); |
749 EXPECT_CALL(*sync_notifier_mock_, UpdateEnabledTypes(_)). | |
750 WillRepeatedly( | |
751 Invoke(this, &SyncManagerTest::SyncNotifierUpdateEnabledTypes)); | |
752 EXPECT_CALL(*sync_notifier_mock_, RemoveObserver(_)). | |
753 WillOnce(Invoke(this, &SyncManagerTest::SyncNotifierRemoveObserver)); | |
754 | 743 |
755 sync_manager_.AddObserver(&observer_); | 744 sync_manager_.AddObserver(&observer_); |
756 EXPECT_CALL(observer_, OnInitializationComplete(_, _)). | 745 EXPECT_CALL(observer_, OnInitializationComplete(_, _)). |
757 WillOnce(SaveArg<0>(&js_backend_)); | 746 WillOnce(SaveArg<0>(&js_backend_)); |
758 | 747 |
759 EXPECT_FALSE(sync_notifier_observer_); | |
760 EXPECT_FALSE(js_backend_.IsInitialized()); | 748 EXPECT_FALSE(js_backend_.IsInitialized()); |
761 | 749 |
762 std::vector<ModelSafeWorker*> workers; | 750 std::vector<ModelSafeWorker*> workers; |
763 ModelSafeRoutingInfo routing_info; | 751 ModelSafeRoutingInfo routing_info; |
764 GetModelSafeRoutingInfo(&routing_info); | 752 GetModelSafeRoutingInfo(&routing_info); |
765 | 753 |
766 // Takes ownership of |sync_notifier_mock_|. | 754 // Takes ownership of |sync_notifier_mock_|. |
767 sync_manager_.Init(temp_dir_.path(), | 755 sync_manager_.Init(temp_dir_.path(), |
768 WeakHandle<JsEventHandler>(), | 756 WeakHandle<JsEventHandler>(), |
769 "bogus", 0, false, | 757 "bogus", 0, false, |
770 base::MessageLoopProxy::current(), | 758 base::MessageLoopProxy::current(), |
771 scoped_ptr<HttpPostProviderFactory>( | 759 scoped_ptr<HttpPostProviderFactory>( |
772 new TestHttpPostProviderFactory()), | 760 new TestHttpPostProviderFactory()), |
773 routing_info, workers, | 761 routing_info, workers, |
774 &extensions_activity_monitor_, this, | 762 &extensions_activity_monitor_, this, |
775 credentials, | 763 credentials, |
776 scoped_ptr<SyncNotifier>(sync_notifier_mock_), | 764 scoped_ptr<SyncNotifier>(sync_notifier_mock_), |
777 "", | 765 "", |
778 syncer::SyncManager::TEST_IN_MEMORY, | 766 syncer::SyncManager::TEST_IN_MEMORY, |
779 &encryptor_, | 767 &encryptor_, |
780 &handler_, | 768 &handler_, |
781 NULL); | 769 NULL); |
782 | 770 |
783 EXPECT_TRUE(sync_notifier_observer_); | |
784 EXPECT_TRUE(js_backend_.IsInitialized()); | 771 EXPECT_TRUE(js_backend_.IsInitialized()); |
785 | 772 |
786 EXPECT_EQ(0, update_enabled_types_call_count_); | |
787 | |
788 for (ModelSafeRoutingInfo::iterator i = routing_info.begin(); | 773 for (ModelSafeRoutingInfo::iterator i = routing_info.begin(); |
789 i != routing_info.end(); ++i) { | 774 i != routing_info.end(); ++i) { |
790 type_roots_[i->first] = MakeServerNodeForType( | 775 type_roots_[i->first] = MakeServerNodeForType( |
791 sync_manager_.GetUserShare(), i->first); | 776 sync_manager_.GetUserShare(), i->first); |
792 } | 777 } |
793 PumpLoop(); | 778 PumpLoop(); |
794 } | 779 } |
795 | 780 |
796 void TearDown() { | 781 void TearDown() { |
797 sync_manager_.RemoveObserver(&observer_); | 782 sync_manager_.RemoveObserver(&observer_); |
| 783 EXPECT_CALL(*sync_notifier_mock_, |
| 784 UpdateRegisteredIds(_, ObjectIdSet())); |
798 sync_manager_.ShutdownOnSyncThread(); | 785 sync_manager_.ShutdownOnSyncThread(); |
799 sync_notifier_mock_ = NULL; | 786 sync_notifier_mock_ = NULL; |
800 EXPECT_FALSE(sync_notifier_observer_); | |
801 PumpLoop(); | 787 PumpLoop(); |
802 } | 788 } |
803 | 789 |
804 void GetModelSafeRoutingInfo(ModelSafeRoutingInfo* out) { | 790 void GetModelSafeRoutingInfo(ModelSafeRoutingInfo* out) { |
805 (*out)[syncer::NIGORI] = syncer::GROUP_PASSIVE; | 791 (*out)[syncer::NIGORI] = syncer::GROUP_PASSIVE; |
806 (*out)[syncer::BOOKMARKS] = syncer::GROUP_PASSIVE; | 792 (*out)[syncer::BOOKMARKS] = syncer::GROUP_PASSIVE; |
807 (*out)[syncer::THEMES] = syncer::GROUP_PASSIVE; | 793 (*out)[syncer::THEMES] = syncer::GROUP_PASSIVE; |
808 (*out)[syncer::SESSIONS] = syncer::GROUP_PASSIVE; | 794 (*out)[syncer::SESSIONS] = syncer::GROUP_PASSIVE; |
809 (*out)[syncer::PASSWORDS] = syncer::GROUP_PASSIVE; | 795 (*out)[syncer::PASSWORDS] = syncer::GROUP_PASSIVE; |
810 (*out)[syncer::PREFERENCES] = syncer::GROUP_PASSIVE; | 796 (*out)[syncer::PREFERENCES] = syncer::GROUP_PASSIVE; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
851 } | 837 } |
852 return cryptographer->is_ready(); | 838 return cryptographer->is_ready(); |
853 } | 839 } |
854 | 840 |
855 int64 GetIdForDataType(ModelType type) { | 841 int64 GetIdForDataType(ModelType type) { |
856 if (type_roots_.count(type) == 0) | 842 if (type_roots_.count(type) == 0) |
857 return 0; | 843 return 0; |
858 return type_roots_[type]; | 844 return type_roots_[type]; |
859 } | 845 } |
860 | 846 |
861 void SyncNotifierAddObserver( | |
862 syncer::SyncNotifierObserver* sync_notifier_observer) { | |
863 EXPECT_EQ(NULL, sync_notifier_observer_); | |
864 sync_notifier_observer_ = sync_notifier_observer; | |
865 } | |
866 | |
867 void SyncNotifierRemoveObserver( | |
868 syncer::SyncNotifierObserver* sync_notifier_observer) { | |
869 EXPECT_EQ(sync_notifier_observer_, sync_notifier_observer); | |
870 sync_notifier_observer_ = NULL; | |
871 } | |
872 | |
873 void SyncNotifierUpdateEnabledTypes(syncer::ModelTypeSet types) { | |
874 ModelSafeRoutingInfo routes; | |
875 GetModelSafeRoutingInfo(&routes); | |
876 const syncer::ModelTypeSet expected_types = GetRoutingInfoTypes(routes); | |
877 EXPECT_TRUE(types.Equals(expected_types)); | |
878 ++update_enabled_types_call_count_; | |
879 } | |
880 | |
881 void PumpLoop() { | 847 void PumpLoop() { |
882 message_loop_.RunAllPending(); | 848 message_loop_.RunAllPending(); |
883 } | 849 } |
884 | 850 |
885 void SendJsMessage(const std::string& name, const JsArgList& args, | 851 void SendJsMessage(const std::string& name, const JsArgList& args, |
886 const WeakHandle<JsReplyHandler>& reply_handler) { | 852 const WeakHandle<JsReplyHandler>& reply_handler) { |
887 js_backend_.Call(FROM_HERE, &JsBackend::ProcessJsMessage, | 853 js_backend_.Call(FROM_HERE, &JsBackend::ProcessJsMessage, |
888 name, args, reply_handler); | 854 name, args, reply_handler); |
889 PumpLoop(); | 855 PumpLoop(); |
890 } | 856 } |
(...skipping 27 matching lines...) Expand all Loading... |
918 } | 884 } |
919 | 885 |
920 private: | 886 private: |
921 // Needed by |sync_manager_|. | 887 // Needed by |sync_manager_|. |
922 MessageLoop message_loop_; | 888 MessageLoop message_loop_; |
923 // Needed by |sync_manager_|. | 889 // Needed by |sync_manager_|. |
924 ScopedTempDir temp_dir_; | 890 ScopedTempDir temp_dir_; |
925 // Sync Id's for the roots of the enabled datatypes. | 891 // Sync Id's for the roots of the enabled datatypes. |
926 std::map<ModelType, int64> type_roots_; | 892 std::map<ModelType, int64> type_roots_; |
927 FakeExtensionsActivityMonitor extensions_activity_monitor_; | 893 FakeExtensionsActivityMonitor extensions_activity_monitor_; |
928 StrictMock<SyncNotifierMock>* sync_notifier_mock_; | |
929 | 894 |
930 protected: | 895 protected: |
931 FakeEncryptor encryptor_; | 896 FakeEncryptor encryptor_; |
932 TestUnrecoverableErrorHandler handler_; | 897 TestUnrecoverableErrorHandler handler_; |
| 898 StrictMock<SyncNotifierMock>* sync_notifier_mock_; |
933 SyncManagerImpl sync_manager_; | 899 SyncManagerImpl sync_manager_; |
934 WeakHandle<JsBackend> js_backend_; | 900 WeakHandle<JsBackend> js_backend_; |
935 StrictMock<SyncManagerObserverMock> observer_; | 901 StrictMock<SyncManagerObserverMock> observer_; |
936 syncer::SyncNotifierObserver* sync_notifier_observer_; | |
937 int update_enabled_types_call_count_; | |
938 }; | 902 }; |
939 | 903 |
940 TEST_F(SyncManagerTest, UpdateEnabledTypes) { | 904 TEST_F(SyncManagerTest, UpdateEnabledTypes) { |
941 EXPECT_EQ(0, update_enabled_types_call_count_); | |
942 | |
943 ModelSafeRoutingInfo routes; | 905 ModelSafeRoutingInfo routes; |
944 GetModelSafeRoutingInfo(&routes); | 906 GetModelSafeRoutingInfo(&routes); |
945 const syncer::ModelTypeSet enabled_types = GetRoutingInfoTypes(routes); | 907 const syncer::ModelTypeSet enabled_types = GetRoutingInfoTypes(routes); |
946 | 908 |
| 909 EXPECT_CALL(*sync_notifier_mock_, |
| 910 UpdateRegisteredIds(_, ModelTypeSetToObjectIdSet(enabled_types))); |
947 sync_manager_.UpdateEnabledTypes(enabled_types); | 911 sync_manager_.UpdateEnabledTypes(enabled_types); |
948 EXPECT_EQ(1, update_enabled_types_call_count_); | |
949 } | 912 } |
950 | 913 |
951 TEST_F(SyncManagerTest, ProcessJsMessage) { | 914 TEST_F(SyncManagerTest, ProcessJsMessage) { |
952 const JsArgList kNoArgs; | 915 const JsArgList kNoArgs; |
953 | 916 |
954 StrictMock<MockJsReplyHandler> reply_handler; | 917 StrictMock<MockJsReplyHandler> reply_handler; |
955 | 918 |
956 ListValue false_args; | 919 ListValue false_args; |
957 false_args.Append(Value::CreateBooleanValue(false)); | 920 false_args.Append(Value::CreateBooleanValue(false)); |
958 | 921 |
(...skipping 1647 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2606 // Ensure only bookmarks and nigori lost their progress marker. Preferences | 2569 // Ensure only bookmarks and nigori lost their progress marker. Preferences |
2607 // should still have it. | 2570 // should still have it. |
2608 partial_types = | 2571 partial_types = |
2609 sync_manager_.GetTypesWithEmptyProgressMarkerToken(ModelTypeSet::All()); | 2572 sync_manager_.GetTypesWithEmptyProgressMarkerToken(ModelTypeSet::All()); |
2610 EXPECT_TRUE(partial_types.Has(NIGORI)); | 2573 EXPECT_TRUE(partial_types.Has(NIGORI)); |
2611 EXPECT_TRUE(partial_types.Has(BOOKMARKS)); | 2574 EXPECT_TRUE(partial_types.Has(BOOKMARKS)); |
2612 EXPECT_FALSE(partial_types.Has(PREFERENCES)); | 2575 EXPECT_FALSE(partial_types.Has(PREFERENCES)); |
2613 } | 2576 } |
2614 | 2577 |
2615 } // namespace | 2578 } // namespace |
OLD | NEW |