OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "google/cacheinvalidation/v2/types.pb.h" |
| 6 #include "sync/notifier/sync_notifier_helper.h" |
| 7 #include "sync/notifier/mock_sync_notifier_observer.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 |
| 10 using testing::StrictMock; |
| 11 |
| 12 namespace syncer { |
| 13 |
| 14 class SyncNotifierHelperTest : public testing::Test { |
| 15 protected: |
| 16 SyncNotifierHelperTest() |
| 17 : kObjectId1(ipc::invalidation::ObjectSource::TEST, "a"), |
| 18 kObjectId2(ipc::invalidation::ObjectSource::TEST, "b"), |
| 19 kObjectId3(ipc::invalidation::ObjectSource::TEST, "c") { |
| 20 } |
| 21 |
| 22 invalidation::ObjectId kObjectId1; |
| 23 invalidation::ObjectId kObjectId2; |
| 24 invalidation::ObjectId kObjectId3; |
| 25 }; |
| 26 |
| 27 // Basic check that registrations are correctly updated for one handler. |
| 28 TEST_F(SyncNotifierHelperTest, Basic) { |
| 29 SyncNotifierHelper helper; |
| 30 StrictMock<MockSyncNotifierObserver> observer; |
| 31 ObjectIdSet ids; |
| 32 ids.insert(kObjectId1); |
| 33 ids.insert(kObjectId2); |
| 34 helper.UpdateRegisteredIds(&observer, ids); |
| 35 |
| 36 ObjectIdPayloadMap dispatched_payloads; |
| 37 dispatched_payloads[kObjectId1] = "1"; |
| 38 dispatched_payloads[kObjectId2] = "2"; |
| 39 dispatched_payloads[kObjectId3] = "3"; |
| 40 |
| 41 // A object ID with no registration should be ignored. |
| 42 ObjectIdPayloadMap expected_payload1; |
| 43 expected_payload1[kObjectId1] = "1"; |
| 44 expected_payload1[kObjectId2] = "2"; |
| 45 EXPECT_CALL(observer, OnIncomingNotification(expected_payload1, |
| 46 REMOTE_NOTIFICATION)); |
| 47 helper.DispatchInvalidationsToHandlers(dispatched_payloads, |
| 48 REMOTE_NOTIFICATION); |
| 49 |
| 50 // Removed object IDs should not be notified, newly-added ones should. |
| 51 ids.erase(kObjectId1); |
| 52 ids.insert(kObjectId3); |
| 53 helper.UpdateRegisteredIds(&observer, ids); |
| 54 |
| 55 ObjectIdPayloadMap expected_payload2; |
| 56 expected_payload2[kObjectId2] = "2"; |
| 57 expected_payload2[kObjectId3] = "3"; |
| 58 EXPECT_CALL(observer, OnIncomingNotification(expected_payload2, |
| 59 REMOTE_NOTIFICATION)); |
| 60 helper.DispatchInvalidationsToHandlers(dispatched_payloads, |
| 61 REMOTE_NOTIFICATION); |
| 62 } |
| 63 |
| 64 // Tests that we correctly bucket and dispatch invalidations on multiple objects |
| 65 // to the corresponding handlers. |
| 66 TEST_F(SyncNotifierHelperTest, MultipleHandlers) { |
| 67 SyncNotifierHelper helper; |
| 68 StrictMock<MockSyncNotifierObserver> observer; |
| 69 ObjectIdSet ids; |
| 70 ids.insert(kObjectId1); |
| 71 ids.insert(kObjectId2); |
| 72 helper.UpdateRegisteredIds(&observer, ids); |
| 73 StrictMock<MockSyncNotifierObserver> observer2; |
| 74 ObjectIdSet ids2; |
| 75 ids2.insert(kObjectId3); |
| 76 helper.UpdateRegisteredIds(&observer2, ids2); |
| 77 |
| 78 ObjectIdPayloadMap expected_payload1; |
| 79 expected_payload1[kObjectId1] = "1"; |
| 80 expected_payload1[kObjectId2] = "2"; |
| 81 EXPECT_CALL(observer, OnIncomingNotification(expected_payload1, |
| 82 REMOTE_NOTIFICATION)); |
| 83 ObjectIdPayloadMap expected_payload2; |
| 84 expected_payload2[kObjectId3] = "3"; |
| 85 EXPECT_CALL(observer2, OnIncomingNotification(expected_payload2, |
| 86 REMOTE_NOTIFICATION)); |
| 87 |
| 88 ObjectIdPayloadMap dispatched_payloads; |
| 89 dispatched_payloads[kObjectId1] = "1"; |
| 90 dispatched_payloads[kObjectId2] = "2"; |
| 91 dispatched_payloads[kObjectId3] = "3"; |
| 92 helper.DispatchInvalidationsToHandlers(dispatched_payloads, |
| 93 REMOTE_NOTIFICATION); |
| 94 |
| 95 // Also verify that the callbacks for OnNotificationsEnabled/Disabled work. |
| 96 EXPECT_CALL(observer, OnNotificationsEnabled()); |
| 97 EXPECT_CALL(observer2, OnNotificationsEnabled()); |
| 98 EXPECT_CALL(observer, |
| 99 OnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR)); |
| 100 EXPECT_CALL(observer2, |
| 101 OnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR)); |
| 102 helper.EmitOnNotificationsEnabled(); |
| 103 helper.EmitOnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR); |
| 104 } |
| 105 |
| 106 // Multiple registrations by different handlers on the same object ID should |
| 107 // cause a CHECK. |
| 108 TEST_F(SyncNotifierHelperTest, MultipleRegistration) { |
| 109 SyncNotifierHelper helper; |
| 110 StrictMock<MockSyncNotifierObserver> observer; |
| 111 ObjectIdSet ids; |
| 112 ids.insert(kObjectId1); |
| 113 ids.insert(kObjectId2); |
| 114 helper.UpdateRegisteredIds(&observer, ids); |
| 115 |
| 116 StrictMock<MockSyncNotifierObserver> observer2; |
| 117 EXPECT_DEATH({ helper.UpdateRegisteredIds(&observer2, ids); }, |
| 118 "Duplicate registration for .*"); |
| 119 } |
| 120 |
| 121 // Make sure that passing an empty set to UpdateRegisteredIds clears the |
| 122 // corresponding entries for the handler. |
| 123 TEST_F(SyncNotifierHelperTest, EmptySetUnregisters) { |
| 124 SyncNotifierHelper helper; |
| 125 StrictMock<MockSyncNotifierObserver> observer; |
| 126 ObjectIdSet ids; |
| 127 ids.insert(kObjectId1); |
| 128 ids.insert(kObjectId2); |
| 129 helper.UpdateRegisteredIds(&observer, ids); |
| 130 // Control observer. |
| 131 StrictMock<MockSyncNotifierObserver> observer2; |
| 132 ObjectIdSet ids2; |
| 133 ids2.insert(kObjectId3); |
| 134 helper.UpdateRegisteredIds(&observer2, ids2); |
| 135 // Unregister the first observer. It should not receive any further callbacks. |
| 136 helper.UpdateRegisteredIds(&observer, ObjectIdSet()); |
| 137 |
| 138 ObjectIdPayloadMap expected_payload2; |
| 139 expected_payload2[kObjectId3] = "3"; |
| 140 EXPECT_CALL(observer2, OnIncomingNotification(expected_payload2, |
| 141 REMOTE_NOTIFICATION)); |
| 142 EXPECT_CALL(observer2, OnNotificationsEnabled()); |
| 143 EXPECT_CALL(observer2, |
| 144 OnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR)); |
| 145 |
| 146 ObjectIdPayloadMap dispatched_payloads; |
| 147 dispatched_payloads[kObjectId1] = "1"; |
| 148 dispatched_payloads[kObjectId2] = "2"; |
| 149 dispatched_payloads[kObjectId3] = "3"; |
| 150 helper.DispatchInvalidationsToHandlers(dispatched_payloads, |
| 151 REMOTE_NOTIFICATION); |
| 152 helper.EmitOnNotificationsEnabled(); |
| 153 helper.EmitOnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR); |
| 154 } |
| 155 |
| 156 } // namespace syncer |
OLD | NEW |