| 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_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") {} |
| 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 Loading... |
| 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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 941 void SimulateDisableNotificationsForTest( | 907 void SimulateDisableNotificationsForTest( |
| 942 NotificationsDisabledReason reason) { | 908 NotificationsDisabledReason reason) { |
| 943 DCHECK(sync_manager_.thread_checker_.CalledOnValidThread()); | 909 DCHECK(sync_manager_.thread_checker_.CalledOnValidThread()); |
| 944 sync_manager_.OnNotificationsDisabled(reason); | 910 sync_manager_.OnNotificationsDisabled(reason); |
| 945 } | 911 } |
| 946 | 912 |
| 947 void TriggerOnIncomingNotificationForTest(ModelTypeSet model_types) { | 913 void TriggerOnIncomingNotificationForTest(ModelTypeSet model_types) { |
| 948 DCHECK(sync_manager_.thread_checker_.CalledOnValidThread()); | 914 DCHECK(sync_manager_.thread_checker_.CalledOnValidThread()); |
| 949 ModelTypePayloadMap model_types_with_payloads = | 915 ModelTypePayloadMap model_types_with_payloads = |
| 950 ModelTypePayloadMapFromEnumSet(model_types, std::string()); | 916 ModelTypePayloadMapFromEnumSet(model_types, std::string()); |
| 951 sync_manager_.OnIncomingNotification(model_types_with_payloads, | 917 sync_manager_.OnIncomingNotification( |
| 952 REMOTE_NOTIFICATION); | 918 ModelTypePayloadMapToObjectIdPayloadMap(model_types_with_payloads), |
| 919 REMOTE_NOTIFICATION); |
| 953 } | 920 } |
| 954 | 921 |
| 955 private: | 922 private: |
| 956 // Needed by |sync_manager_|. | 923 // Needed by |sync_manager_|. |
| 957 MessageLoop message_loop_; | 924 MessageLoop message_loop_; |
| 958 // Needed by |sync_manager_|. | 925 // Needed by |sync_manager_|. |
| 959 ScopedTempDir temp_dir_; | 926 ScopedTempDir temp_dir_; |
| 960 // Sync Id's for the roots of the enabled datatypes. | 927 // Sync Id's for the roots of the enabled datatypes. |
| 961 std::map<ModelType, int64> type_roots_; | 928 std::map<ModelType, int64> type_roots_; |
| 962 FakeExtensionsActivityMonitor extensions_activity_monitor_; | 929 FakeExtensionsActivityMonitor extensions_activity_monitor_; |
| 963 StrictMock<SyncNotifierMock>* sync_notifier_mock_; | |
| 964 | 930 |
| 965 protected: | 931 protected: |
| 966 FakeEncryptor encryptor_; | 932 FakeEncryptor encryptor_; |
| 967 TestUnrecoverableErrorHandler handler_; | 933 TestUnrecoverableErrorHandler handler_; |
| 934 StrictMock<SyncNotifierMock>* sync_notifier_mock_; |
| 968 SyncManagerImpl sync_manager_; | 935 SyncManagerImpl sync_manager_; |
| 969 WeakHandle<JsBackend> js_backend_; | 936 WeakHandle<JsBackend> js_backend_; |
| 970 StrictMock<SyncManagerObserverMock> observer_; | 937 StrictMock<SyncManagerObserverMock> observer_; |
| 971 SyncNotifierObserver* sync_notifier_observer_; | |
| 972 int update_enabled_types_call_count_; | |
| 973 }; | 938 }; |
| 974 | 939 |
| 975 TEST_F(SyncManagerTest, UpdateEnabledTypes) { | 940 TEST_F(SyncManagerTest, UpdateEnabledTypes) { |
| 976 EXPECT_EQ(0, update_enabled_types_call_count_); | |
| 977 | |
| 978 ModelSafeRoutingInfo routes; | 941 ModelSafeRoutingInfo routes; |
| 979 GetModelSafeRoutingInfo(&routes); | 942 GetModelSafeRoutingInfo(&routes); |
| 980 const ModelTypeSet enabled_types = GetRoutingInfoTypes(routes); | 943 const ModelTypeSet enabled_types = GetRoutingInfoTypes(routes); |
| 981 | 944 |
| 945 EXPECT_CALL(*sync_notifier_mock_, |
| 946 UpdateRegisteredIds(_, ModelTypeSetToObjectIdSet(enabled_types))); |
| 982 sync_manager_.UpdateEnabledTypes(enabled_types); | 947 sync_manager_.UpdateEnabledTypes(enabled_types); |
| 983 EXPECT_EQ(1, update_enabled_types_call_count_); | |
| 984 } | 948 } |
| 985 | 949 |
| 986 TEST_F(SyncManagerTest, ProcessJsMessage) { | 950 TEST_F(SyncManagerTest, ProcessJsMessage) { |
| 987 const JsArgList kNoArgs; | 951 const JsArgList kNoArgs; |
| 988 | 952 |
| 989 StrictMock<MockJsReplyHandler> reply_handler; | 953 StrictMock<MockJsReplyHandler> reply_handler; |
| 990 | 954 |
| 991 ListValue false_args; | 955 ListValue false_args; |
| 992 false_args.Append(Value::CreateBooleanValue(false)); | 956 false_args.Append(Value::CreateBooleanValue(false)); |
| 993 | 957 |
| (...skipping 1663 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2657 // Ensure only bookmarks and nigori lost their progress marker. Preferences | 2621 // Ensure only bookmarks and nigori lost their progress marker. Preferences |
| 2658 // should still have it. | 2622 // should still have it. |
| 2659 partial_types = | 2623 partial_types = |
| 2660 sync_manager_.GetTypesWithEmptyProgressMarkerToken(ModelTypeSet::All()); | 2624 sync_manager_.GetTypesWithEmptyProgressMarkerToken(ModelTypeSet::All()); |
| 2661 EXPECT_TRUE(partial_types.Has(NIGORI)); | 2625 EXPECT_TRUE(partial_types.Has(NIGORI)); |
| 2662 EXPECT_TRUE(partial_types.Has(BOOKMARKS)); | 2626 EXPECT_TRUE(partial_types.Has(BOOKMARKS)); |
| 2663 EXPECT_FALSE(partial_types.Has(PREFERENCES)); | 2627 EXPECT_FALSE(partial_types.Has(PREFERENCES)); |
| 2664 } | 2628 } |
| 2665 | 2629 |
| 2666 } // namespace | 2630 } // namespace |
| OLD | NEW |