| 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 #include "base/compiler_specific.h" |
| 6 #include "base/memory/scoped_ptr.h" |
| 5 #include "google/cacheinvalidation/types.pb.h" | 7 #include "google/cacheinvalidation/types.pb.h" |
| 6 #include "sync/notifier/fake_invalidation_handler.h" | 8 #include "sync/notifier/fake_invalidation_handler.h" |
| 7 #include "sync/notifier/invalidator_registrar.h" | 9 #include "sync/notifier/invalidator_registrar.h" |
| 10 #include "sync/notifier/invalidator_test_template.h" |
| 8 #include "sync/notifier/object_id_state_map_test_util.h" | 11 #include "sync/notifier/object_id_state_map_test_util.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 13 |
| 11 namespace syncer { | 14 namespace syncer { |
| 12 | 15 |
| 13 namespace { | 16 namespace { |
| 14 | 17 |
| 15 class InvalidatorRegistrarTest : public testing::Test { | 18 // We test InvalidatorRegistrar by wrapping it in an Invalidator and |
| 16 protected: | 19 // running the usual Invalidator tests. |
| 17 InvalidatorRegistrarTest() | 20 |
| 18 : kObjectId1(ipc::invalidation::ObjectSource::TEST, "a"), | 21 // Thin Invalidator wrapper around InvalidatorRegistrar. |
| 19 kObjectId2(ipc::invalidation::ObjectSource::TEST, "b"), | 22 class RegistrarInvalidator : public Invalidator { |
| 20 kObjectId3(ipc::invalidation::ObjectSource::TEST, "c"), | 23 public: |
| 21 kObjectId4(ipc::invalidation::ObjectSource::TEST, "d") { | 24 virtual ~RegistrarInvalidator() {} |
| 25 |
| 26 InvalidatorRegistrar* GetRegistrar() { |
| 27 return ®istrar_; |
| 22 } | 28 } |
| 23 | 29 |
| 24 const invalidation::ObjectId kObjectId1; | 30 // Invalidator implementation. |
| 25 const invalidation::ObjectId kObjectId2; | 31 virtual void RegisterHandler(InvalidationHandler* handler) OVERRIDE { |
| 26 const invalidation::ObjectId kObjectId3; | 32 registrar_.RegisterHandler(handler); |
| 27 const invalidation::ObjectId kObjectId4; | 33 } |
| 34 |
| 35 virtual void UpdateRegisteredIds(InvalidationHandler* handler, |
| 36 const ObjectIdSet& ids) OVERRIDE { |
| 37 registrar_.UpdateRegisteredIds(handler, ids); |
| 38 } |
| 39 |
| 40 virtual void UnregisterHandler(InvalidationHandler* handler) OVERRIDE { |
| 41 registrar_.UnregisterHandler(handler); |
| 42 } |
| 43 |
| 44 virtual void SetUniqueId(const std::string& unique_id) OVERRIDE { |
| 45 // Do nothing. |
| 46 } |
| 47 |
| 48 virtual void SetStateDeprecated(const std::string& state) OVERRIDE { |
| 49 // Do nothing. |
| 50 } |
| 51 |
| 52 virtual void UpdateCredentials( |
| 53 const std::string& email, const std::string& token) OVERRIDE { |
| 54 // Do nothing. |
| 55 } |
| 56 |
| 57 virtual void SendNotification( |
| 58 const ObjectIdStateMap& id_state_map) OVERRIDE { |
| 59 // Do nothing. |
| 60 } |
| 61 |
| 62 private: |
| 63 InvalidatorRegistrar registrar_; |
| 28 }; | 64 }; |
| 29 | 65 |
| 30 // Register a handler, register some IDs for that handler, and then unregister | 66 class RegistrarInvalidatorTestDelegate { |
| 31 // the handler, dispatching invalidations in between. The handler should only | 67 public: |
| 32 // see invalidations when its registered and its IDs are registered. | 68 RegistrarInvalidatorTestDelegate() {} |
| 33 TEST_F(InvalidatorRegistrarTest, Basic) { | 69 |
| 34 FakeInvalidationHandler handler; | 70 ~RegistrarInvalidatorTestDelegate() { |
| 71 DestroyInvalidator(); |
| 72 } |
| 73 |
| 74 void CreateInvalidator( |
| 75 const std::string& initial_state, |
| 76 const base::WeakPtr<InvalidationStateTracker>& |
| 77 invalidation_state_tracker) { |
| 78 DCHECK(!invalidator_.get()); |
| 79 invalidator_.reset(new RegistrarInvalidator()); |
| 80 } |
| 81 |
| 82 RegistrarInvalidator* GetInvalidator() { |
| 83 return invalidator_.get(); |
| 84 } |
| 85 |
| 86 void DestroyInvalidator() { |
| 87 invalidator_.reset(); |
| 88 } |
| 89 |
| 90 void WaitForInvalidator() { |
| 91 // Do nothing. |
| 92 } |
| 93 |
| 94 void TriggerOnNotificationsEnabled() { |
| 95 invalidator_->GetRegistrar()->EmitOnNotificationsEnabled(); |
| 96 } |
| 97 |
| 98 void TriggerOnIncomingNotification(const ObjectIdStateMap& id_state_map, |
| 99 IncomingNotificationSource source) { |
| 100 invalidator_->GetRegistrar()->DispatchInvalidationsToHandlers( |
| 101 id_state_map, source); |
| 102 } |
| 103 |
| 104 void TriggerOnNotificationsDisabled(NotificationsDisabledReason reason) { |
| 105 invalidator_->GetRegistrar()->EmitOnNotificationsDisabled(reason); |
| 106 } |
| 107 |
| 108 static bool InvalidatorHandlesDeprecatedState() { |
| 109 return false; |
| 110 } |
| 111 |
| 112 private: |
| 113 scoped_ptr<RegistrarInvalidator> invalidator_; |
| 114 }; |
| 115 |
| 116 INSTANTIATE_TYPED_TEST_CASE_P( |
| 117 RegistrarInvalidatorTest, InvalidatorTest, |
| 118 RegistrarInvalidatorTestDelegate); |
| 119 |
| 120 class InvalidatorRegistrarTest : public testing::Test {}; |
| 121 |
| 122 // Multiple registrations by different handlers on the same object ID should |
| 123 // cause a CHECK. |
| 124 // |
| 125 // Technically this can be part of InvalidatorTest, but we want to keep the |
| 126 // number of death tests down. |
| 127 TEST_F(InvalidatorRegistrarTest, MultipleRegistration) { |
| 128 const invalidation::ObjectId id1(ipc::invalidation::ObjectSource::TEST, "a"); |
| 129 const invalidation::ObjectId id2(ipc::invalidation::ObjectSource::TEST, "a"); |
| 35 | 130 |
| 36 InvalidatorRegistrar registrar; | 131 InvalidatorRegistrar registrar; |
| 37 | 132 |
| 38 registrar.RegisterHandler(&handler); | |
| 39 | |
| 40 ObjectIdStateMap states; | |
| 41 states[kObjectId1].payload = "1"; | |
| 42 states[kObjectId2].payload = "2"; | |
| 43 states[kObjectId3].payload = "3"; | |
| 44 | |
| 45 // Should be ignored since no IDs are registered to |handler|. | |
| 46 registrar.DispatchInvalidationsToHandlers(states, REMOTE_NOTIFICATION); | |
| 47 EXPECT_EQ(0, handler.GetNotificationCount()); | |
| 48 | |
| 49 ObjectIdSet ids; | |
| 50 ids.insert(kObjectId1); | |
| 51 ids.insert(kObjectId2); | |
| 52 registrar.UpdateRegisteredIds(&handler, ids); | |
| 53 | |
| 54 ObjectIdStateMap expected_states; | |
| 55 expected_states[kObjectId1].payload = "1"; | |
| 56 expected_states[kObjectId2].payload = "2"; | |
| 57 | |
| 58 registrar.DispatchInvalidationsToHandlers(states, REMOTE_NOTIFICATION); | |
| 59 EXPECT_EQ(1, handler.GetNotificationCount()); | |
| 60 EXPECT_THAT( | |
| 61 expected_states, | |
| 62 Eq(handler.GetLastNotificationIdStateMap())); | |
| 63 EXPECT_EQ(REMOTE_NOTIFICATION, handler.GetLastNotificationSource()); | |
| 64 | |
| 65 ids.erase(kObjectId1); | |
| 66 ids.insert(kObjectId3); | |
| 67 registrar.UpdateRegisteredIds(&handler, ids); | |
| 68 | |
| 69 expected_states.erase(kObjectId1); | |
| 70 expected_states[kObjectId3].payload = "3"; | |
| 71 | |
| 72 // Removed object IDs should not be notified, newly-added ones should. | |
| 73 registrar.DispatchInvalidationsToHandlers(states, REMOTE_NOTIFICATION); | |
| 74 EXPECT_EQ(2, handler.GetNotificationCount()); | |
| 75 EXPECT_THAT( | |
| 76 expected_states, | |
| 77 Eq(handler.GetLastNotificationIdStateMap())); | |
| 78 EXPECT_EQ(REMOTE_NOTIFICATION, handler.GetLastNotificationSource()); | |
| 79 | |
| 80 registrar.UnregisterHandler(&handler); | |
| 81 | |
| 82 // Should be ignored since |handler| isn't registered anymore. | |
| 83 registrar.DispatchInvalidationsToHandlers(states, REMOTE_NOTIFICATION); | |
| 84 EXPECT_EQ(2, handler.GetNotificationCount()); | |
| 85 } | |
| 86 | |
| 87 // Register handlers and some IDs for those handlers, register a handler with | |
| 88 // no IDs, and register a handler with some IDs but unregister it. Then, | |
| 89 // dispatch some notifications and invalidations. Handlers that are registered | |
| 90 // should get notifications, and the ones that have registered IDs should | |
| 91 // receive invalidations for those IDs. | |
| 92 TEST_F(InvalidatorRegistrarTest, MultipleHandlers) { | |
| 93 FakeInvalidationHandler handler1; | 133 FakeInvalidationHandler handler1; |
| 94 FakeInvalidationHandler handler2; | |
| 95 FakeInvalidationHandler handler3; | |
| 96 FakeInvalidationHandler handler4; | |
| 97 | |
| 98 InvalidatorRegistrar registrar; | |
| 99 | |
| 100 registrar.RegisterHandler(&handler1); | |
| 101 registrar.RegisterHandler(&handler2); | |
| 102 registrar.RegisterHandler(&handler3); | |
| 103 registrar.RegisterHandler(&handler4); | |
| 104 | |
| 105 { | |
| 106 ObjectIdSet ids; | |
| 107 ids.insert(kObjectId1); | |
| 108 ids.insert(kObjectId2); | |
| 109 registrar.UpdateRegisteredIds(&handler1, ids); | |
| 110 } | |
| 111 | |
| 112 { | |
| 113 ObjectIdSet ids; | |
| 114 ids.insert(kObjectId3); | |
| 115 registrar.UpdateRegisteredIds(&handler2, ids); | |
| 116 } | |
| 117 | |
| 118 // Don't register any IDs for handler3. | |
| 119 | |
| 120 { | |
| 121 ObjectIdSet ids; | |
| 122 ids.insert(kObjectId4); | |
| 123 registrar.UpdateRegisteredIds(&handler4, ids); | |
| 124 } | |
| 125 | |
| 126 registrar.UnregisterHandler(&handler4); | |
| 127 | |
| 128 registrar.EmitOnNotificationsEnabled(); | |
| 129 EXPECT_EQ(NO_NOTIFICATION_ERROR, | |
| 130 handler1.GetNotificationsDisabledReason()); | |
| 131 EXPECT_EQ(NO_NOTIFICATION_ERROR, | |
| 132 handler2.GetNotificationsDisabledReason()); | |
| 133 EXPECT_EQ(NO_NOTIFICATION_ERROR, | |
| 134 handler3.GetNotificationsDisabledReason()); | |
| 135 EXPECT_EQ(TRANSIENT_NOTIFICATION_ERROR, | |
| 136 handler4.GetNotificationsDisabledReason()); | |
| 137 | |
| 138 { | |
| 139 ObjectIdStateMap states; | |
| 140 states[kObjectId1].payload = "1"; | |
| 141 states[kObjectId2].payload = "2"; | |
| 142 states[kObjectId3].payload = "3"; | |
| 143 states[kObjectId4].payload = "4"; | |
| 144 registrar.DispatchInvalidationsToHandlers(states, REMOTE_NOTIFICATION); | |
| 145 | |
| 146 ObjectIdStateMap expected_states; | |
| 147 expected_states[kObjectId1].payload = "1"; | |
| 148 expected_states[kObjectId2].payload = "2"; | |
| 149 | |
| 150 EXPECT_EQ(1, handler1.GetNotificationCount()); | |
| 151 EXPECT_THAT( | |
| 152 expected_states, | |
| 153 Eq(handler1.GetLastNotificationIdStateMap())); | |
| 154 EXPECT_EQ(REMOTE_NOTIFICATION, handler1.GetLastNotificationSource()); | |
| 155 | |
| 156 expected_states.clear(); | |
| 157 expected_states[kObjectId3].payload = "3"; | |
| 158 | |
| 159 EXPECT_EQ(1, handler2.GetNotificationCount()); | |
| 160 EXPECT_THAT( | |
| 161 expected_states, | |
| 162 Eq(handler2.GetLastNotificationIdStateMap())); | |
| 163 EXPECT_EQ(REMOTE_NOTIFICATION, handler2.GetLastNotificationSource()); | |
| 164 | |
| 165 EXPECT_EQ(0, handler3.GetNotificationCount()); | |
| 166 EXPECT_EQ(0, handler4.GetNotificationCount()); | |
| 167 } | |
| 168 | |
| 169 registrar.EmitOnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR); | |
| 170 EXPECT_EQ(TRANSIENT_NOTIFICATION_ERROR, | |
| 171 handler1.GetNotificationsDisabledReason()); | |
| 172 EXPECT_EQ(TRANSIENT_NOTIFICATION_ERROR, | |
| 173 handler2.GetNotificationsDisabledReason()); | |
| 174 EXPECT_EQ(TRANSIENT_NOTIFICATION_ERROR, | |
| 175 handler3.GetNotificationsDisabledReason()); | |
| 176 EXPECT_EQ(TRANSIENT_NOTIFICATION_ERROR, | |
| 177 handler4.GetNotificationsDisabledReason()); | |
| 178 } | |
| 179 | |
| 180 // Multiple registrations by different handlers on the same object ID should | |
| 181 // cause a CHECK. | |
| 182 TEST_F(InvalidatorRegistrarTest, MultipleRegistration) { | |
| 183 InvalidatorRegistrar registrar; | |
| 184 | |
| 185 FakeInvalidationHandler handler1; | |
| 186 registrar.RegisterHandler(&handler1); | 134 registrar.RegisterHandler(&handler1); |
| 187 | 135 |
| 188 FakeInvalidationHandler handler2; | 136 FakeInvalidationHandler handler2; |
| 189 registrar.RegisterHandler(&handler2); | 137 registrar.RegisterHandler(&handler2); |
| 190 | 138 |
| 191 ObjectIdSet ids; | 139 ObjectIdSet ids; |
| 192 ids.insert(kObjectId1); | 140 ids.insert(id1); |
| 193 ids.insert(kObjectId2); | 141 ids.insert(id2); |
| 194 registrar.UpdateRegisteredIds(&handler1, ids); | 142 registrar.UpdateRegisteredIds(&handler1, ids); |
| 195 | 143 |
| 196 registrar.DetachFromThreadForTest(); | 144 registrar.DetachFromThreadForTest(); |
| 197 // We expect a death via CHECK(). We can't match against the CHECK() message | 145 // We expect a death via CHECK(). We can't match against the CHECK() message |
| 198 // though since they are removed in official builds. | 146 // though since they are removed in official builds. |
| 199 EXPECT_DEATH({ registrar.UpdateRegisteredIds(&handler2, ids); }, ""); | 147 EXPECT_DEATH({ registrar.UpdateRegisteredIds(&handler2, ids); }, ""); |
| 200 } | 148 } |
| 201 | 149 |
| 202 // Make sure that passing an empty set to UpdateRegisteredIds clears the | |
| 203 // corresponding entries for the handler. | |
| 204 TEST_F(InvalidatorRegistrarTest, EmptySetUnregisters) { | |
| 205 FakeInvalidationHandler handler1; | |
| 206 | |
| 207 // Control observer. | |
| 208 FakeInvalidationHandler handler2; | |
| 209 | |
| 210 InvalidatorRegistrar registrar; | |
| 211 | |
| 212 registrar.RegisterHandler(&handler1); | |
| 213 registrar.RegisterHandler(&handler2); | |
| 214 | |
| 215 { | |
| 216 ObjectIdSet ids; | |
| 217 ids.insert(kObjectId1); | |
| 218 ids.insert(kObjectId2); | |
| 219 registrar.UpdateRegisteredIds(&handler1, ids); | |
| 220 } | |
| 221 | |
| 222 { | |
| 223 ObjectIdSet ids; | |
| 224 ids.insert(kObjectId3); | |
| 225 registrar.UpdateRegisteredIds(&handler2, ids); | |
| 226 } | |
| 227 | |
| 228 // Unregister the IDs for the first observer. It should not receive any | |
| 229 // further invalidations. | |
| 230 registrar.UpdateRegisteredIds(&handler1, ObjectIdSet()); | |
| 231 | |
| 232 registrar.EmitOnNotificationsEnabled(); | |
| 233 EXPECT_EQ(NO_NOTIFICATION_ERROR, | |
| 234 handler1.GetNotificationsDisabledReason()); | |
| 235 EXPECT_EQ(NO_NOTIFICATION_ERROR, | |
| 236 handler2.GetNotificationsDisabledReason()); | |
| 237 | |
| 238 { | |
| 239 ObjectIdStateMap states; | |
| 240 states[kObjectId1].payload = "1"; | |
| 241 states[kObjectId2].payload = "2"; | |
| 242 states[kObjectId3].payload = "3"; | |
| 243 registrar.DispatchInvalidationsToHandlers(states, | |
| 244 REMOTE_NOTIFICATION); | |
| 245 EXPECT_EQ(0, handler1.GetNotificationCount()); | |
| 246 EXPECT_EQ(1, handler2.GetNotificationCount()); | |
| 247 } | |
| 248 | |
| 249 registrar.EmitOnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR); | |
| 250 EXPECT_EQ(TRANSIENT_NOTIFICATION_ERROR, | |
| 251 handler1.GetNotificationsDisabledReason()); | |
| 252 EXPECT_EQ(TRANSIENT_NOTIFICATION_ERROR, | |
| 253 handler2.GetNotificationsDisabledReason()); | |
| 254 } | |
| 255 | |
| 256 } // namespace | 150 } // namespace |
| 257 | 151 |
| 258 } // namespace syncer | 152 } // namespace syncer |
| OLD | NEW |