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 677 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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_METHOD2(UpdateRegisteredIds, void(SyncNotifierObserver*, | 698 MOCK_METHOD1(AddObserver, void(SyncNotifierObserver*)); |
699 const ObjectIdSet&)); | 699 MOCK_METHOD1(RemoveObserver, void(SyncNotifierObserver*)); |
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)); |
704 MOCK_METHOD1(SendNotification, void(ModelTypeSet)); | 706 MOCK_METHOD1(SendNotification, void(ModelTypeSet)); |
705 }; | 707 }; |
706 | 708 |
707 } // namespace | 709 } // namespace |
708 | 710 |
709 class SyncManagerTest : public testing::Test, | 711 class SyncManagerTest : public testing::Test, |
710 public SyncManager::ChangeDelegate { | 712 public SyncManager::ChangeDelegate { |
711 protected: | 713 protected: |
712 enum NigoriStatus { | 714 enum NigoriStatus { |
713 DONT_WRITE_NIGORI, | 715 DONT_WRITE_NIGORI, |
714 WRITE_TO_NIGORI | 716 WRITE_TO_NIGORI |
715 }; | 717 }; |
716 | 718 |
717 enum EncryptionStatus { | 719 enum EncryptionStatus { |
718 UNINITIALIZED, | 720 UNINITIALIZED, |
719 DEFAULT_ENCRYPTION, | 721 DEFAULT_ENCRYPTION, |
720 FULL_ENCRYPTION | 722 FULL_ENCRYPTION |
721 }; | 723 }; |
722 | 724 |
723 SyncManagerTest() | 725 SyncManagerTest() |
724 : sync_notifier_mock_(NULL), | 726 : sync_notifier_mock_(NULL), |
725 sync_manager_("Test sync manager") {} | 727 sync_manager_("Test sync manager"), |
| 728 sync_notifier_observer_(NULL), |
| 729 update_enabled_types_call_count_(0) {} |
726 | 730 |
727 virtual ~SyncManagerTest() { | 731 virtual ~SyncManagerTest() { |
728 EXPECT_FALSE(sync_notifier_mock_); | 732 EXPECT_FALSE(sync_notifier_mock_); |
729 } | 733 } |
730 | 734 |
731 // Test implementation. | 735 // Test implementation. |
732 void SetUp() { | 736 void SetUp() { |
733 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 737 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
734 | 738 |
735 SyncCredentials credentials; | 739 SyncCredentials credentials; |
736 credentials.email = "foo@bar.com"; | 740 credentials.email = "foo@bar.com"; |
737 credentials.sync_token = "sometoken"; | 741 credentials.sync_token = "sometoken"; |
738 | 742 |
739 sync_notifier_mock_ = new StrictMock<SyncNotifierMock>(); | 743 sync_notifier_mock_ = new StrictMock<SyncNotifierMock>(); |
| 744 EXPECT_CALL(*sync_notifier_mock_, AddObserver(_)). |
| 745 WillOnce(Invoke(this, &SyncManagerTest::SyncNotifierAddObserver)); |
740 EXPECT_CALL(*sync_notifier_mock_, SetUniqueId(_)); | 746 EXPECT_CALL(*sync_notifier_mock_, SetUniqueId(_)); |
741 EXPECT_CALL(*sync_notifier_mock_, SetStateDeprecated("")); | 747 EXPECT_CALL(*sync_notifier_mock_, SetStateDeprecated("")); |
742 EXPECT_CALL(*sync_notifier_mock_, | 748 EXPECT_CALL(*sync_notifier_mock_, |
743 UpdateCredentials(credentials.email, credentials.sync_token)); | 749 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)); |
744 | 755 |
745 sync_manager_.AddObserver(&observer_); | 756 sync_manager_.AddObserver(&observer_); |
746 EXPECT_CALL(observer_, OnInitializationComplete(_, _)). | 757 EXPECT_CALL(observer_, OnInitializationComplete(_, _)). |
747 WillOnce(SaveArg<0>(&js_backend_)); | 758 WillOnce(SaveArg<0>(&js_backend_)); |
748 | 759 |
| 760 EXPECT_FALSE(sync_notifier_observer_); |
749 EXPECT_FALSE(js_backend_.IsInitialized()); | 761 EXPECT_FALSE(js_backend_.IsInitialized()); |
750 | 762 |
751 std::vector<ModelSafeWorker*> workers; | 763 std::vector<ModelSafeWorker*> workers; |
752 ModelSafeRoutingInfo routing_info; | 764 ModelSafeRoutingInfo routing_info; |
753 GetModelSafeRoutingInfo(&routing_info); | 765 GetModelSafeRoutingInfo(&routing_info); |
754 | 766 |
755 // Takes ownership of |sync_notifier_mock_|. | 767 // Takes ownership of |sync_notifier_mock_|. |
756 sync_manager_.Init(temp_dir_.path(), | 768 sync_manager_.Init(temp_dir_.path(), |
757 WeakHandle<JsEventHandler>(), | 769 WeakHandle<JsEventHandler>(), |
758 "bogus", 0, false, | 770 "bogus", 0, false, |
759 base::MessageLoopProxy::current(), | 771 base::MessageLoopProxy::current(), |
760 scoped_ptr<HttpPostProviderFactory>( | 772 scoped_ptr<HttpPostProviderFactory>( |
761 new TestHttpPostProviderFactory()), | 773 new TestHttpPostProviderFactory()), |
762 routing_info, workers, | 774 routing_info, workers, |
763 &extensions_activity_monitor_, this, | 775 &extensions_activity_monitor_, this, |
764 credentials, | 776 credentials, |
765 scoped_ptr<SyncNotifier>(sync_notifier_mock_), | 777 scoped_ptr<SyncNotifier>(sync_notifier_mock_), |
766 "", | 778 "", |
767 scoped_ptr<InternalComponentsFactory>(GetFactory()), | 779 scoped_ptr<InternalComponentsFactory>(GetFactory()), |
768 &encryptor_, | 780 &encryptor_, |
769 &handler_, | 781 &handler_, |
770 NULL); | 782 NULL); |
771 | 783 |
| 784 EXPECT_TRUE(sync_notifier_observer_); |
772 EXPECT_TRUE(js_backend_.IsInitialized()); | 785 EXPECT_TRUE(js_backend_.IsInitialized()); |
773 | 786 |
| 787 EXPECT_EQ(0, update_enabled_types_call_count_); |
| 788 |
774 for (ModelSafeRoutingInfo::iterator i = routing_info.begin(); | 789 for (ModelSafeRoutingInfo::iterator i = routing_info.begin(); |
775 i != routing_info.end(); ++i) { | 790 i != routing_info.end(); ++i) { |
776 type_roots_[i->first] = MakeServerNodeForType( | 791 type_roots_[i->first] = MakeServerNodeForType( |
777 sync_manager_.GetUserShare(), i->first); | 792 sync_manager_.GetUserShare(), i->first); |
778 } | 793 } |
779 PumpLoop(); | 794 PumpLoop(); |
780 } | 795 } |
781 | 796 |
782 void TearDown() { | 797 void TearDown() { |
783 sync_manager_.RemoveObserver(&observer_); | 798 sync_manager_.RemoveObserver(&observer_); |
784 EXPECT_CALL(*sync_notifier_mock_, | |
785 UpdateRegisteredIds(_, ObjectIdSet())); | |
786 sync_manager_.ShutdownOnSyncThread(); | 799 sync_manager_.ShutdownOnSyncThread(); |
787 sync_notifier_mock_ = NULL; | 800 sync_notifier_mock_ = NULL; |
| 801 EXPECT_FALSE(sync_notifier_observer_); |
788 PumpLoop(); | 802 PumpLoop(); |
789 } | 803 } |
790 | 804 |
791 void GetModelSafeRoutingInfo(ModelSafeRoutingInfo* out) { | 805 void GetModelSafeRoutingInfo(ModelSafeRoutingInfo* out) { |
792 (*out)[NIGORI] = GROUP_PASSIVE; | 806 (*out)[NIGORI] = GROUP_PASSIVE; |
793 (*out)[BOOKMARKS] = GROUP_PASSIVE; | 807 (*out)[BOOKMARKS] = GROUP_PASSIVE; |
794 (*out)[THEMES] = GROUP_PASSIVE; | 808 (*out)[THEMES] = GROUP_PASSIVE; |
795 (*out)[SESSIONS] = GROUP_PASSIVE; | 809 (*out)[SESSIONS] = GROUP_PASSIVE; |
796 (*out)[PASSWORDS] = GROUP_PASSIVE; | 810 (*out)[PASSWORDS] = GROUP_PASSIVE; |
797 (*out)[PREFERENCES] = GROUP_PASSIVE; | 811 (*out)[PREFERENCES] = GROUP_PASSIVE; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
838 } | 852 } |
839 return cryptographer->is_ready(); | 853 return cryptographer->is_ready(); |
840 } | 854 } |
841 | 855 |
842 int64 GetIdForDataType(ModelType type) { | 856 int64 GetIdForDataType(ModelType type) { |
843 if (type_roots_.count(type) == 0) | 857 if (type_roots_.count(type) == 0) |
844 return 0; | 858 return 0; |
845 return type_roots_[type]; | 859 return type_roots_[type]; |
846 } | 860 } |
847 | 861 |
| 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 |
848 void PumpLoop() { | 882 void PumpLoop() { |
849 message_loop_.RunAllPending(); | 883 message_loop_.RunAllPending(); |
850 } | 884 } |
851 | 885 |
852 void SendJsMessage(const std::string& name, const JsArgList& args, | 886 void SendJsMessage(const std::string& name, const JsArgList& args, |
853 const WeakHandle<JsReplyHandler>& reply_handler) { | 887 const WeakHandle<JsReplyHandler>& reply_handler) { |
854 js_backend_.Call(FROM_HERE, &JsBackend::ProcessJsMessage, | 888 js_backend_.Call(FROM_HERE, &JsBackend::ProcessJsMessage, |
855 name, args, reply_handler); | 889 name, args, reply_handler); |
856 PumpLoop(); | 890 PumpLoop(); |
857 } | 891 } |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
907 void SimulateDisableNotificationsForTest( | 941 void SimulateDisableNotificationsForTest( |
908 NotificationsDisabledReason reason) { | 942 NotificationsDisabledReason reason) { |
909 DCHECK(sync_manager_.thread_checker_.CalledOnValidThread()); | 943 DCHECK(sync_manager_.thread_checker_.CalledOnValidThread()); |
910 sync_manager_.OnNotificationsDisabled(reason); | 944 sync_manager_.OnNotificationsDisabled(reason); |
911 } | 945 } |
912 | 946 |
913 void TriggerOnIncomingNotificationForTest(ModelTypeSet model_types) { | 947 void TriggerOnIncomingNotificationForTest(ModelTypeSet model_types) { |
914 DCHECK(sync_manager_.thread_checker_.CalledOnValidThread()); | 948 DCHECK(sync_manager_.thread_checker_.CalledOnValidThread()); |
915 ModelTypePayloadMap model_types_with_payloads = | 949 ModelTypePayloadMap model_types_with_payloads = |
916 ModelTypePayloadMapFromEnumSet(model_types, std::string()); | 950 ModelTypePayloadMapFromEnumSet(model_types, std::string()); |
917 sync_manager_.OnIncomingNotification( | 951 sync_manager_.OnIncomingNotification(model_types_with_payloads, |
918 ModelTypePayloadMapToObjectIdPayloadMap(model_types_with_payloads), | 952 REMOTE_NOTIFICATION); |
919 REMOTE_NOTIFICATION); | |
920 } | 953 } |
921 | 954 |
922 private: | 955 private: |
923 // Needed by |sync_manager_|. | 956 // Needed by |sync_manager_|. |
924 MessageLoop message_loop_; | 957 MessageLoop message_loop_; |
925 // Needed by |sync_manager_|. | 958 // Needed by |sync_manager_|. |
926 ScopedTempDir temp_dir_; | 959 ScopedTempDir temp_dir_; |
927 // Sync Id's for the roots of the enabled datatypes. | 960 // Sync Id's for the roots of the enabled datatypes. |
928 std::map<ModelType, int64> type_roots_; | 961 std::map<ModelType, int64> type_roots_; |
929 FakeExtensionsActivityMonitor extensions_activity_monitor_; | 962 FakeExtensionsActivityMonitor extensions_activity_monitor_; |
| 963 StrictMock<SyncNotifierMock>* sync_notifier_mock_; |
930 | 964 |
931 protected: | 965 protected: |
932 FakeEncryptor encryptor_; | 966 FakeEncryptor encryptor_; |
933 TestUnrecoverableErrorHandler handler_; | 967 TestUnrecoverableErrorHandler handler_; |
934 StrictMock<SyncNotifierMock>* sync_notifier_mock_; | |
935 SyncManagerImpl sync_manager_; | 968 SyncManagerImpl sync_manager_; |
936 WeakHandle<JsBackend> js_backend_; | 969 WeakHandle<JsBackend> js_backend_; |
937 StrictMock<SyncManagerObserverMock> observer_; | 970 StrictMock<SyncManagerObserverMock> observer_; |
| 971 SyncNotifierObserver* sync_notifier_observer_; |
| 972 int update_enabled_types_call_count_; |
938 }; | 973 }; |
939 | 974 |
940 TEST_F(SyncManagerTest, UpdateEnabledTypes) { | 975 TEST_F(SyncManagerTest, UpdateEnabledTypes) { |
| 976 EXPECT_EQ(0, update_enabled_types_call_count_); |
| 977 |
941 ModelSafeRoutingInfo routes; | 978 ModelSafeRoutingInfo routes; |
942 GetModelSafeRoutingInfo(&routes); | 979 GetModelSafeRoutingInfo(&routes); |
943 const ModelTypeSet enabled_types = GetRoutingInfoTypes(routes); | 980 const ModelTypeSet enabled_types = GetRoutingInfoTypes(routes); |
944 | 981 |
945 EXPECT_CALL(*sync_notifier_mock_, | |
946 UpdateRegisteredIds(_, ModelTypeSetToObjectIdSet(enabled_types))); | |
947 sync_manager_.UpdateEnabledTypes(enabled_types); | 982 sync_manager_.UpdateEnabledTypes(enabled_types); |
| 983 EXPECT_EQ(1, update_enabled_types_call_count_); |
948 } | 984 } |
949 | 985 |
950 TEST_F(SyncManagerTest, ProcessJsMessage) { | 986 TEST_F(SyncManagerTest, ProcessJsMessage) { |
951 const JsArgList kNoArgs; | 987 const JsArgList kNoArgs; |
952 | 988 |
953 StrictMock<MockJsReplyHandler> reply_handler; | 989 StrictMock<MockJsReplyHandler> reply_handler; |
954 | 990 |
955 ListValue false_args; | 991 ListValue false_args; |
956 false_args.Append(Value::CreateBooleanValue(false)); | 992 false_args.Append(Value::CreateBooleanValue(false)); |
957 | 993 |
(...skipping 1663 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2621 // Ensure only bookmarks and nigori lost their progress marker. Preferences | 2657 // Ensure only bookmarks and nigori lost their progress marker. Preferences |
2622 // should still have it. | 2658 // should still have it. |
2623 partial_types = | 2659 partial_types = |
2624 sync_manager_.GetTypesWithEmptyProgressMarkerToken(ModelTypeSet::All()); | 2660 sync_manager_.GetTypesWithEmptyProgressMarkerToken(ModelTypeSet::All()); |
2625 EXPECT_TRUE(partial_types.Has(NIGORI)); | 2661 EXPECT_TRUE(partial_types.Has(NIGORI)); |
2626 EXPECT_TRUE(partial_types.Has(BOOKMARKS)); | 2662 EXPECT_TRUE(partial_types.Has(BOOKMARKS)); |
2627 EXPECT_FALSE(partial_types.Has(PREFERENCES)); | 2663 EXPECT_FALSE(partial_types.Has(PREFERENCES)); |
2628 } | 2664 } |
2629 | 2665 |
2630 } // namespace | 2666 } // namespace |
OLD | NEW |