| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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 28 matching lines...) Expand all Loading... |
| 39 #include "sync/internal_api/public/write_transaction.h" | 39 #include "sync/internal_api/public/write_transaction.h" |
| 40 #include "sync/internal_api/sync_encryption_handler_impl.h" | 40 #include "sync/internal_api/sync_encryption_handler_impl.h" |
| 41 #include "sync/internal_api/sync_manager_impl.h" | 41 #include "sync/internal_api/sync_manager_impl.h" |
| 42 #include "sync/internal_api/syncapi_internal.h" | 42 #include "sync/internal_api/syncapi_internal.h" |
| 43 #include "sync/js/js_arg_list.h" | 43 #include "sync/js/js_arg_list.h" |
| 44 #include "sync/js/js_backend.h" | 44 #include "sync/js/js_backend.h" |
| 45 #include "sync/js/js_event_handler.h" | 45 #include "sync/js/js_event_handler.h" |
| 46 #include "sync/js/js_reply_handler.h" | 46 #include "sync/js/js_reply_handler.h" |
| 47 #include "sync/js/js_test_util.h" | 47 #include "sync/js/js_test_util.h" |
| 48 #include "sync/notifier/fake_invalidation_handler.h" | 48 #include "sync/notifier/fake_invalidation_handler.h" |
| 49 #include "sync/notifier/fake_invalidator.h" | |
| 50 #include "sync/notifier/invalidation_handler.h" | 49 #include "sync/notifier/invalidation_handler.h" |
| 51 #include "sync/notifier/invalidator.h" | 50 #include "sync/notifier/invalidator.h" |
| 52 #include "sync/protocol/bookmark_specifics.pb.h" | 51 #include "sync/protocol/bookmark_specifics.pb.h" |
| 53 #include "sync/protocol/encryption.pb.h" | 52 #include "sync/protocol/encryption.pb.h" |
| 54 #include "sync/protocol/extension_specifics.pb.h" | 53 #include "sync/protocol/extension_specifics.pb.h" |
| 55 #include "sync/protocol/password_specifics.pb.h" | 54 #include "sync/protocol/password_specifics.pb.h" |
| 56 #include "sync/protocol/preference_specifics.pb.h" | 55 #include "sync/protocol/preference_specifics.pb.h" |
| 57 #include "sync/protocol/proto_value_conversions.h" | 56 #include "sync/protocol/proto_value_conversions.h" |
| 58 #include "sync/protocol/sync.pb.h" | 57 #include "sync/protocol/sync.pb.h" |
| 59 #include "sync/sessions/sync_session.h" | 58 #include "sync/sessions/sync_session.h" |
| (...skipping 722 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 782 WRITE_TO_NIGORI | 781 WRITE_TO_NIGORI |
| 783 }; | 782 }; |
| 784 | 783 |
| 785 enum EncryptionStatus { | 784 enum EncryptionStatus { |
| 786 UNINITIALIZED, | 785 UNINITIALIZED, |
| 787 DEFAULT_ENCRYPTION, | 786 DEFAULT_ENCRYPTION, |
| 788 FULL_ENCRYPTION | 787 FULL_ENCRYPTION |
| 789 }; | 788 }; |
| 790 | 789 |
| 791 SyncManagerTest() | 790 SyncManagerTest() |
| 792 : fake_invalidator_(NULL), | 791 : sync_manager_("Test sync manager") { |
| 793 sync_manager_("Test sync manager") { | |
| 794 switches_.encryption_method = | 792 switches_.encryption_method = |
| 795 InternalComponentsFactory::ENCRYPTION_KEYSTORE; | 793 InternalComponentsFactory::ENCRYPTION_KEYSTORE; |
| 796 } | 794 } |
| 797 | 795 |
| 798 virtual ~SyncManagerTest() { | 796 virtual ~SyncManagerTest() { |
| 799 EXPECT_FALSE(fake_invalidator_); | |
| 800 } | 797 } |
| 801 | 798 |
| 802 // Test implementation. | 799 // Test implementation. |
| 803 void SetUp() { | 800 void SetUp() { |
| 804 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 801 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 805 | 802 |
| 806 SyncCredentials credentials; | 803 SyncCredentials credentials; |
| 807 credentials.email = "foo@bar.com"; | 804 credentials.email = "foo@bar.com"; |
| 808 credentials.sync_token = "sometoken"; | 805 credentials.sync_token = "sometoken"; |
| 809 | 806 |
| 810 fake_invalidator_ = new FakeInvalidator(); | |
| 811 | |
| 812 sync_manager_.AddObserver(&manager_observer_); | 807 sync_manager_.AddObserver(&manager_observer_); |
| 813 EXPECT_CALL(manager_observer_, OnInitializationComplete(_, _, _, _)). | 808 EXPECT_CALL(manager_observer_, OnInitializationComplete(_, _, _, _)). |
| 814 WillOnce(SaveArg<0>(&js_backend_)); | 809 WillOnce(SaveArg<0>(&js_backend_)); |
| 815 | 810 |
| 816 EXPECT_FALSE(js_backend_.IsInitialized()); | 811 EXPECT_FALSE(js_backend_.IsInitialized()); |
| 817 | 812 |
| 818 std::vector<ModelSafeWorker*> workers; | 813 std::vector<ModelSafeWorker*> workers; |
| 819 ModelSafeRoutingInfo routing_info; | 814 ModelSafeRoutingInfo routing_info; |
| 820 GetModelSafeRoutingInfo(&routing_info); | 815 GetModelSafeRoutingInfo(&routing_info); |
| 821 | 816 |
| 822 // Takes ownership of |fake_invalidator_|. | |
| 823 sync_manager_.Init(temp_dir_.path(), | 817 sync_manager_.Init(temp_dir_.path(), |
| 824 WeakHandle<JsEventHandler>(), | 818 WeakHandle<JsEventHandler>(), |
| 825 "bogus", 0, false, | 819 "bogus", 0, false, |
| 826 scoped_ptr<HttpPostProviderFactory>( | 820 scoped_ptr<HttpPostProviderFactory>( |
| 827 new TestHttpPostProviderFactory()), | 821 new TestHttpPostProviderFactory()), |
| 828 workers, &extensions_activity_monitor_, this, | 822 workers, &extensions_activity_monitor_, this, |
| 829 credentials, | 823 credentials, |
| 830 scoped_ptr<Invalidator>(fake_invalidator_), | |
| 831 "fake_invalidator_client_id", | 824 "fake_invalidator_client_id", |
| 832 "", "", // bootstrap tokens | 825 "", "", // bootstrap tokens |
| 833 scoped_ptr<InternalComponentsFactory>(GetFactory()), | 826 scoped_ptr<InternalComponentsFactory>(GetFactory()), |
| 834 &encryptor_, | 827 &encryptor_, |
| 835 &handler_, | 828 &handler_, |
| 836 NULL); | 829 NULL); |
| 837 | 830 |
| 838 sync_manager_.GetEncryptionHandler()->AddObserver(&encryption_observer_); | 831 sync_manager_.GetEncryptionHandler()->AddObserver(&encryption_observer_); |
| 839 | 832 |
| 840 EXPECT_TRUE(js_backend_.IsInitialized()); | 833 EXPECT_TRUE(js_backend_.IsInitialized()); |
| 841 | 834 |
| 842 for (ModelSafeRoutingInfo::iterator i = routing_info.begin(); | 835 for (ModelSafeRoutingInfo::iterator i = routing_info.begin(); |
| 843 i != routing_info.end(); ++i) { | 836 i != routing_info.end(); ++i) { |
| 844 type_roots_[i->first] = MakeServerNodeForType( | 837 type_roots_[i->first] = MakeServerNodeForType( |
| 845 sync_manager_.GetUserShare(), i->first); | 838 sync_manager_.GetUserShare(), i->first); |
| 846 } | 839 } |
| 847 PumpLoop(); | 840 PumpLoop(); |
| 848 | |
| 849 EXPECT_TRUE(fake_invalidator_->IsHandlerRegistered(&sync_manager_)); | |
| 850 } | 841 } |
| 851 | 842 |
| 852 void TearDown() { | 843 void TearDown() { |
| 853 sync_manager_.RemoveObserver(&manager_observer_); | 844 sync_manager_.RemoveObserver(&manager_observer_); |
| 854 sync_manager_.ShutdownOnSyncThread(); | 845 sync_manager_.ShutdownOnSyncThread(); |
| 855 // We can't assert that |sync_manager_| isn't registered with | |
| 856 // |fake_invalidator_| anymore because |fake_invalidator_| is now | |
| 857 // destroyed. | |
| 858 fake_invalidator_ = NULL; | |
| 859 PumpLoop(); | 846 PumpLoop(); |
| 860 } | 847 } |
| 861 | 848 |
| 862 void GetModelSafeRoutingInfo(ModelSafeRoutingInfo* out) { | 849 void GetModelSafeRoutingInfo(ModelSafeRoutingInfo* out) { |
| 863 (*out)[NIGORI] = GROUP_PASSIVE; | 850 (*out)[NIGORI] = GROUP_PASSIVE; |
| 864 (*out)[DEVICE_INFO] = GROUP_PASSIVE; | 851 (*out)[DEVICE_INFO] = GROUP_PASSIVE; |
| 865 (*out)[EXPERIMENTS] = GROUP_PASSIVE; | 852 (*out)[EXPERIMENTS] = GROUP_PASSIVE; |
| 866 (*out)[BOOKMARKS] = GROUP_PASSIVE; | 853 (*out)[BOOKMARKS] = GROUP_PASSIVE; |
| 867 (*out)[THEMES] = GROUP_PASSIVE; | 854 (*out)[THEMES] = GROUP_PASSIVE; |
| 868 (*out)[SESSIONS] = GROUP_PASSIVE; | 855 (*out)[SESSIONS] = GROUP_PASSIVE; |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 974 return GetEncryptedTypesWithTrans(&trans); | 961 return GetEncryptedTypesWithTrans(&trans); |
| 975 } | 962 } |
| 976 | 963 |
| 977 ModelTypeSet GetEncryptedTypesWithTrans(BaseTransaction* trans) { | 964 ModelTypeSet GetEncryptedTypesWithTrans(BaseTransaction* trans) { |
| 978 return trans->GetDirectory()->GetNigoriHandler()-> | 965 return trans->GetDirectory()->GetNigoriHandler()-> |
| 979 GetEncryptedTypes(trans->GetWrappedTrans()); | 966 GetEncryptedTypes(trans->GetWrappedTrans()); |
| 980 } | 967 } |
| 981 | 968 |
| 982 void SimulateInvalidatorStateChangeForTest(InvalidatorState state) { | 969 void SimulateInvalidatorStateChangeForTest(InvalidatorState state) { |
| 983 DCHECK(sync_manager_.thread_checker_.CalledOnValidThread()); | 970 DCHECK(sync_manager_.thread_checker_.CalledOnValidThread()); |
| 984 sync_manager_.OnInvalidatorStateChange(state); | 971 sync_manager_.UpdateInvalidatorState(state); |
| 985 } | 972 } |
| 986 | 973 |
| 987 void TriggerOnIncomingNotificationForTest(ModelTypeSet model_types) { | 974 void TriggerOnIncomingNotificationForTest(ModelTypeSet model_types) { |
| 988 DCHECK(sync_manager_.thread_checker_.CalledOnValidThread()); | 975 DCHECK(sync_manager_.thread_checker_.CalledOnValidThread()); |
| 989 ModelTypeInvalidationMap invalidation_map = | 976 ModelTypeInvalidationMap invalidation_map = |
| 990 ModelTypeSetToInvalidationMap(model_types, std::string()); | 977 ModelTypeSetToInvalidationMap(model_types, std::string()); |
| 991 sync_manager_.OnIncomingInvalidation( | 978 sync_manager_.Invalidate( |
| 992 ModelTypeInvalidationMapToObjectIdInvalidationMap( | 979 ModelTypeInvalidationMapToObjectIdInvalidationMap( |
| 993 invalidation_map)); | 980 invalidation_map)); |
| 994 } | 981 } |
| 995 | 982 |
| 996 void SetProgressMarkerForType(ModelType type, bool set) { | 983 void SetProgressMarkerForType(ModelType type, bool set) { |
| 997 if (set) { | 984 if (set) { |
| 998 sync_pb::DataTypeProgressMarker marker; | 985 sync_pb::DataTypeProgressMarker marker; |
| 999 marker.set_token("token"); | 986 marker.set_token("token"); |
| 1000 marker.set_data_type_id(GetSpecificsFieldNumberFromModelType(type)); | 987 marker.set_data_type_id(GetSpecificsFieldNumberFromModelType(type)); |
| 1001 sync_manager_.directory()->SetDownloadProgress(type, marker); | 988 sync_manager_.directory()->SetDownloadProgress(type, marker); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 1014 MessageLoop message_loop_; | 1001 MessageLoop message_loop_; |
| 1015 // Needed by |sync_manager_|. | 1002 // Needed by |sync_manager_|. |
| 1016 base::ScopedTempDir temp_dir_; | 1003 base::ScopedTempDir temp_dir_; |
| 1017 // Sync Id's for the roots of the enabled datatypes. | 1004 // Sync Id's for the roots of the enabled datatypes. |
| 1018 std::map<ModelType, int64> type_roots_; | 1005 std::map<ModelType, int64> type_roots_; |
| 1019 FakeExtensionsActivityMonitor extensions_activity_monitor_; | 1006 FakeExtensionsActivityMonitor extensions_activity_monitor_; |
| 1020 | 1007 |
| 1021 protected: | 1008 protected: |
| 1022 FakeEncryptor encryptor_; | 1009 FakeEncryptor encryptor_; |
| 1023 TestUnrecoverableErrorHandler handler_; | 1010 TestUnrecoverableErrorHandler handler_; |
| 1024 FakeInvalidator* fake_invalidator_; | |
| 1025 SyncManagerImpl sync_manager_; | 1011 SyncManagerImpl sync_manager_; |
| 1026 WeakHandle<JsBackend> js_backend_; | 1012 WeakHandle<JsBackend> js_backend_; |
| 1027 StrictMock<SyncManagerObserverMock> manager_observer_; | 1013 StrictMock<SyncManagerObserverMock> manager_observer_; |
| 1028 StrictMock<SyncEncryptionHandlerObserverMock> encryption_observer_; | 1014 StrictMock<SyncEncryptionHandlerObserverMock> encryption_observer_; |
| 1029 InternalComponentsFactory::Switches switches_; | 1015 InternalComponentsFactory::Switches switches_; |
| 1030 }; | 1016 }; |
| 1031 | 1017 |
| 1032 TEST_F(SyncManagerTest, UpdateEnabledTypes) { | |
| 1033 ModelSafeRoutingInfo routes; | |
| 1034 GetModelSafeRoutingInfo(&routes); | |
| 1035 const ModelTypeSet enabled_types = GetRoutingInfoTypes(routes); | |
| 1036 sync_manager_.UpdateEnabledTypes(enabled_types); | |
| 1037 EXPECT_EQ(ModelTypeSetToObjectIdSet(enabled_types), | |
| 1038 fake_invalidator_->GetRegisteredIds(&sync_manager_)); | |
| 1039 } | |
| 1040 | |
| 1041 TEST_F(SyncManagerTest, RegisterInvalidationHandler) { | |
| 1042 FakeInvalidationHandler fake_handler; | |
| 1043 sync_manager_.RegisterInvalidationHandler(&fake_handler); | |
| 1044 EXPECT_TRUE(fake_invalidator_->IsHandlerRegistered(&fake_handler)); | |
| 1045 | |
| 1046 const ObjectIdSet& ids = | |
| 1047 ModelTypeSetToObjectIdSet(ModelTypeSet(BOOKMARKS, PREFERENCES)); | |
| 1048 sync_manager_.UpdateRegisteredInvalidationIds(&fake_handler, ids); | |
| 1049 EXPECT_EQ(ids, fake_invalidator_->GetRegisteredIds(&fake_handler)); | |
| 1050 | |
| 1051 sync_manager_.UnregisterInvalidationHandler(&fake_handler); | |
| 1052 EXPECT_FALSE(fake_invalidator_->IsHandlerRegistered(&fake_handler)); | |
| 1053 } | |
| 1054 | |
| 1055 TEST_F(SyncManagerTest, ProcessJsMessage) { | 1018 TEST_F(SyncManagerTest, ProcessJsMessage) { |
| 1056 const JsArgList kNoArgs; | 1019 const JsArgList kNoArgs; |
| 1057 | 1020 |
| 1058 StrictMock<MockJsReplyHandler> reply_handler; | 1021 StrictMock<MockJsReplyHandler> reply_handler; |
| 1059 | 1022 |
| 1060 base::ListValue disabled_args; | 1023 base::ListValue disabled_args; |
| 1061 disabled_args.Append(new base::StringValue("TRANSIENT_INVALIDATION_ERROR")); | 1024 disabled_args.Append(new base::StringValue("TRANSIENT_INVALIDATION_ERROR")); |
| 1062 | 1025 |
| 1063 EXPECT_CALL(reply_handler, | 1026 EXPECT_CALL(reply_handler, |
| 1064 HandleJsReply("getNotificationState", | 1027 HandleJsReply("getNotificationState", |
| (...skipping 2298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3363 size_t folder_b_pos = | 3326 size_t folder_b_pos = |
| 3364 FindChangeInList(folder_b_id, ChangeRecord::ACTION_DELETE); | 3327 FindChangeInList(folder_b_id, ChangeRecord::ACTION_DELETE); |
| 3365 size_t child_pos = FindChangeInList(child_id, ChangeRecord::ACTION_DELETE); | 3328 size_t child_pos = FindChangeInList(child_id, ChangeRecord::ACTION_DELETE); |
| 3366 | 3329 |
| 3367 // Deletes should appear before updates. | 3330 // Deletes should appear before updates. |
| 3368 EXPECT_LT(child_pos, folder_a_pos); | 3331 EXPECT_LT(child_pos, folder_a_pos); |
| 3369 EXPECT_LT(folder_b_pos, folder_a_pos); | 3332 EXPECT_LT(folder_b_pos, folder_a_pos); |
| 3370 } | 3333 } |
| 3371 | 3334 |
| 3372 } // namespace | 3335 } // namespace |
| OLD | NEW |