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

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

Issue 15580002: Make use of InvalidationService (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 7 years, 6 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
« no previous file with comments | « sync/internal_api/sync_manager_impl.cc ('k') | sync/internal_api/test/fake_sync_manager.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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_|. 817 // Takes ownership of |fake_invalidator_|.
823 sync_manager_.Init( 818 sync_manager_.Init(
824 temp_dir_.path(), 819 temp_dir_.path(),
825 WeakHandle<JsEventHandler>(), 820 WeakHandle<JsEventHandler>(),
826 "bogus", 821 "bogus",
827 0, 822 0,
828 false, 823 false,
829 scoped_ptr<HttpPostProviderFactory>(new TestHttpPostProviderFactory()), 824 scoped_ptr<HttpPostProviderFactory>(new TestHttpPostProviderFactory()),
830 workers, 825 workers,
831 &extensions_activity_monitor_, 826 &extensions_activity_monitor_,
832 this, 827 this,
833 credentials, 828 credentials,
834 scoped_ptr<Invalidator>(fake_invalidator_),
835 "fake_invalidator_client_id", 829 "fake_invalidator_client_id",
836 std::string(), 830 std::string(),
837 std::string(), // bootstrap tokens 831 std::string(), // bootstrap tokens
838 scoped_ptr<InternalComponentsFactory>(GetFactory()), 832 scoped_ptr<InternalComponentsFactory>(GetFactory()),
839 &encryptor_, 833 &encryptor_,
840 &handler_, 834 &handler_,
841 NULL, 835 NULL,
842 false); 836 false);
843 837
844 sync_manager_.GetEncryptionHandler()->AddObserver(&encryption_observer_); 838 sync_manager_.GetEncryptionHandler()->AddObserver(&encryption_observer_);
845 839
846 EXPECT_TRUE(js_backend_.IsInitialized()); 840 EXPECT_TRUE(js_backend_.IsInitialized());
847 841
848 for (ModelSafeRoutingInfo::iterator i = routing_info.begin(); 842 for (ModelSafeRoutingInfo::iterator i = routing_info.begin();
849 i != routing_info.end(); ++i) { 843 i != routing_info.end(); ++i) {
850 type_roots_[i->first] = MakeServerNodeForType( 844 type_roots_[i->first] = MakeServerNodeForType(
851 sync_manager_.GetUserShare(), i->first); 845 sync_manager_.GetUserShare(), i->first);
852 } 846 }
853 PumpLoop(); 847 PumpLoop();
854
855 EXPECT_TRUE(fake_invalidator_->IsHandlerRegistered(&sync_manager_));
856 } 848 }
857 849
858 void TearDown() { 850 void TearDown() {
859 sync_manager_.RemoveObserver(&manager_observer_); 851 sync_manager_.RemoveObserver(&manager_observer_);
860 sync_manager_.ShutdownOnSyncThread(); 852 sync_manager_.ShutdownOnSyncThread();
861 // We can't assert that |sync_manager_| isn't registered with
862 // |fake_invalidator_| anymore because |fake_invalidator_| is now
863 // destroyed.
864 fake_invalidator_ = NULL;
865 PumpLoop(); 853 PumpLoop();
866 } 854 }
867 855
868 void GetModelSafeRoutingInfo(ModelSafeRoutingInfo* out) { 856 void GetModelSafeRoutingInfo(ModelSafeRoutingInfo* out) {
869 (*out)[NIGORI] = GROUP_PASSIVE; 857 (*out)[NIGORI] = GROUP_PASSIVE;
870 (*out)[DEVICE_INFO] = GROUP_PASSIVE; 858 (*out)[DEVICE_INFO] = GROUP_PASSIVE;
871 (*out)[EXPERIMENTS] = GROUP_PASSIVE; 859 (*out)[EXPERIMENTS] = GROUP_PASSIVE;
872 (*out)[BOOKMARKS] = GROUP_PASSIVE; 860 (*out)[BOOKMARKS] = GROUP_PASSIVE;
873 (*out)[THEMES] = GROUP_PASSIVE; 861 (*out)[THEMES] = GROUP_PASSIVE;
874 (*out)[SESSIONS] = GROUP_PASSIVE; 862 (*out)[SESSIONS] = GROUP_PASSIVE;
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
1020 base::MessageLoop message_loop_; 1008 base::MessageLoop message_loop_;
1021 // Needed by |sync_manager_|. 1009 // Needed by |sync_manager_|.
1022 base::ScopedTempDir temp_dir_; 1010 base::ScopedTempDir temp_dir_;
1023 // Sync Id's for the roots of the enabled datatypes. 1011 // Sync Id's for the roots of the enabled datatypes.
1024 std::map<ModelType, int64> type_roots_; 1012 std::map<ModelType, int64> type_roots_;
1025 FakeExtensionsActivityMonitor extensions_activity_monitor_; 1013 FakeExtensionsActivityMonitor extensions_activity_monitor_;
1026 1014
1027 protected: 1015 protected:
1028 FakeEncryptor encryptor_; 1016 FakeEncryptor encryptor_;
1029 TestUnrecoverableErrorHandler handler_; 1017 TestUnrecoverableErrorHandler handler_;
1030 FakeInvalidator* fake_invalidator_;
1031 SyncManagerImpl sync_manager_; 1018 SyncManagerImpl sync_manager_;
1032 WeakHandle<JsBackend> js_backend_; 1019 WeakHandle<JsBackend> js_backend_;
1033 StrictMock<SyncManagerObserverMock> manager_observer_; 1020 StrictMock<SyncManagerObserverMock> manager_observer_;
1034 StrictMock<SyncEncryptionHandlerObserverMock> encryption_observer_; 1021 StrictMock<SyncEncryptionHandlerObserverMock> encryption_observer_;
1035 InternalComponentsFactory::Switches switches_; 1022 InternalComponentsFactory::Switches switches_;
1036 }; 1023 };
1037 1024
1038 TEST_F(SyncManagerTest, UpdateEnabledTypes) {
1039 ModelSafeRoutingInfo routes;
1040 GetModelSafeRoutingInfo(&routes);
1041 const ModelTypeSet enabled_types = GetRoutingInfoTypes(routes);
1042 sync_manager_.UpdateEnabledTypes(enabled_types);
1043 EXPECT_EQ(ModelTypeSetToObjectIdSet(enabled_types),
1044 fake_invalidator_->GetRegisteredIds(&sync_manager_));
1045 }
1046
1047 TEST_F(SyncManagerTest, RegisterInvalidationHandler) {
1048 FakeInvalidationHandler fake_handler;
1049 sync_manager_.RegisterInvalidationHandler(&fake_handler);
1050 EXPECT_TRUE(fake_invalidator_->IsHandlerRegistered(&fake_handler));
1051
1052 const ObjectIdSet& ids =
1053 ModelTypeSetToObjectIdSet(ModelTypeSet(BOOKMARKS, PREFERENCES));
1054 sync_manager_.UpdateRegisteredInvalidationIds(&fake_handler, ids);
1055 EXPECT_EQ(ids, fake_invalidator_->GetRegisteredIds(&fake_handler));
1056
1057 sync_manager_.UnregisterInvalidationHandler(&fake_handler);
1058 EXPECT_FALSE(fake_invalidator_->IsHandlerRegistered(&fake_handler));
1059 }
1060
1061 TEST_F(SyncManagerTest, ProcessJsMessage) { 1025 TEST_F(SyncManagerTest, ProcessJsMessage) {
1062 const JsArgList kNoArgs; 1026 const JsArgList kNoArgs;
1063 1027
1064 StrictMock<MockJsReplyHandler> reply_handler; 1028 StrictMock<MockJsReplyHandler> reply_handler;
1065 1029
1066 base::ListValue disabled_args; 1030 base::ListValue disabled_args;
1067 disabled_args.Append(new base::StringValue("TRANSIENT_INVALIDATION_ERROR")); 1031 disabled_args.Append(new base::StringValue("TRANSIENT_INVALIDATION_ERROR"));
1068 1032
1069 EXPECT_CALL(reply_handler, 1033 EXPECT_CALL(reply_handler,
1070 HandleJsReply("getNotificationState", 1034 HandleJsReply("getNotificationState",
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after
1355 base::DictionaryValue enabled_details; 1319 base::DictionaryValue enabled_details;
1356 enabled_details.SetString("state", "INVALIDATIONS_ENABLED"); 1320 enabled_details.SetString("state", "INVALIDATIONS_ENABLED");
1357 base::DictionaryValue credentials_rejected_details; 1321 base::DictionaryValue credentials_rejected_details;
1358 credentials_rejected_details.SetString( 1322 credentials_rejected_details.SetString(
1359 "state", "INVALIDATION_CREDENTIALS_REJECTED"); 1323 "state", "INVALIDATION_CREDENTIALS_REJECTED");
1360 base::DictionaryValue transient_error_details; 1324 base::DictionaryValue transient_error_details;
1361 transient_error_details.SetString("state", "TRANSIENT_INVALIDATION_ERROR"); 1325 transient_error_details.SetString("state", "TRANSIENT_INVALIDATION_ERROR");
1362 base::DictionaryValue auth_error_details; 1326 base::DictionaryValue auth_error_details;
1363 auth_error_details.SetString("status", "CONNECTION_AUTH_ERROR"); 1327 auth_error_details.SetString("status", "CONNECTION_AUTH_ERROR");
1364 1328
1365 EXPECT_CALL(manager_observer_,
1366 OnConnectionStatusChange(CONNECTION_AUTH_ERROR));
1367
1368 EXPECT_CALL(
1369 event_handler,
1370 HandleJsEvent("onConnectionStatusChange",
1371 HasDetailsAsDictionary(auth_error_details)));
1372
1373 EXPECT_CALL(event_handler, 1329 EXPECT_CALL(event_handler,
1374 HandleJsEvent("onNotificationStateChange", 1330 HandleJsEvent("onNotificationStateChange",
1375 HasDetailsAsDictionary(enabled_details))); 1331 HasDetailsAsDictionary(enabled_details)));
1376 1332
1377 EXPECT_CALL( 1333 EXPECT_CALL(
1378 event_handler, 1334 event_handler,
1379 HandleJsEvent("onNotificationStateChange", 1335 HandleJsEvent("onNotificationStateChange",
1380 HasDetailsAsDictionary(credentials_rejected_details))) 1336 HasDetailsAsDictionary(credentials_rejected_details)))
1381 .Times(2); 1337 .Times(2);
1382 1338
(...skipping 20 matching lines...) Expand all
1403 SetJsEventHandler(WeakHandle<JsEventHandler>()); 1359 SetJsEventHandler(WeakHandle<JsEventHandler>());
1404 1360
1405 SimulateInvalidatorStateChangeForTest(INVALIDATIONS_ENABLED); 1361 SimulateInvalidatorStateChangeForTest(INVALIDATIONS_ENABLED);
1406 SimulateInvalidatorStateChangeForTest(INVALIDATION_CREDENTIALS_REJECTED); 1362 SimulateInvalidatorStateChangeForTest(INVALIDATION_CREDENTIALS_REJECTED);
1407 SimulateInvalidatorStateChangeForTest(TRANSIENT_INVALIDATION_ERROR); 1363 SimulateInvalidatorStateChangeForTest(TRANSIENT_INVALIDATION_ERROR);
1408 1364
1409 // Should trigger the replies. 1365 // Should trigger the replies.
1410 PumpLoop(); 1366 PumpLoop();
1411 } 1367 }
1412 1368
1413 // Simulate the invalidator's credentials being rejected. That should
1414 // also clear the sync token.
1415 TEST_F(SyncManagerTest, OnInvalidatorStateChangeCredentialsRejected) {
1416 EXPECT_CALL(manager_observer_,
1417 OnConnectionStatusChange(CONNECTION_AUTH_ERROR));
1418
1419 EXPECT_FALSE(sync_manager_.GetHasInvalidAuthTokenForTest());
1420
1421 SimulateInvalidatorStateChangeForTest(INVALIDATION_CREDENTIALS_REJECTED);
1422
1423 EXPECT_TRUE(sync_manager_.GetHasInvalidAuthTokenForTest());
1424
1425 // Should trigger the replies.
1426 PumpLoop();
1427 }
1428
1429 TEST_F(SyncManagerTest, OnIncomingNotification) { 1369 TEST_F(SyncManagerTest, OnIncomingNotification) {
1430 StrictMock<MockJsEventHandler> event_handler; 1370 StrictMock<MockJsEventHandler> event_handler;
1431 1371
1432 const ModelTypeSet empty_model_types; 1372 const ModelTypeSet empty_model_types;
1433 const ModelTypeSet model_types( 1373 const ModelTypeSet model_types(
1434 BOOKMARKS, THEMES); 1374 BOOKMARKS, THEMES);
1435 1375
1436 // Build expected_args to have a single argument with the string 1376 // Build expected_args to have a single argument with the string
1437 // equivalents of model_types. 1377 // equivalents of model_types.
1438 base::DictionaryValue expected_details; 1378 base::DictionaryValue expected_details;
(...skipping 2127 matching lines...) Expand 10 before | Expand all | Expand 10 after
3566 size_t folder_b_pos = 3506 size_t folder_b_pos =
3567 FindChangeInList(folder_b_id, ChangeRecord::ACTION_DELETE); 3507 FindChangeInList(folder_b_id, ChangeRecord::ACTION_DELETE);
3568 size_t child_pos = FindChangeInList(child_id, ChangeRecord::ACTION_DELETE); 3508 size_t child_pos = FindChangeInList(child_id, ChangeRecord::ACTION_DELETE);
3569 3509
3570 // Deletes should appear before updates. 3510 // Deletes should appear before updates.
3571 EXPECT_LT(child_pos, folder_a_pos); 3511 EXPECT_LT(child_pos, folder_a_pos);
3572 EXPECT_LT(folder_b_pos, folder_a_pos); 3512 EXPECT_LT(folder_b_pos, folder_a_pos);
3573 } 3513 }
3574 3514
3575 } // namespace 3515 } // namespace
OLDNEW
« no previous file with comments | « sync/internal_api/sync_manager_impl.cc ('k') | sync/internal_api/test/fake_sync_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698