diff options
Diffstat (limited to 'sync/notifier/invalidator_registrar_unittest.cc')
-rw-r--r-- | sync/notifier/invalidator_registrar_unittest.cc | 294 |
1 files changed, 94 insertions, 200 deletions
diff --git a/sync/notifier/invalidator_registrar_unittest.cc b/sync/notifier/invalidator_registrar_unittest.cc index f79a720..90ac22d 100644 --- a/sync/notifier/invalidator_registrar_unittest.cc +++ b/sync/notifier/invalidator_registrar_unittest.cc @@ -2,9 +2,12 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include "base/compiler_specific.h" +#include "base/memory/scoped_ptr.h" #include "google/cacheinvalidation/types.pb.h" #include "sync/notifier/fake_invalidation_handler.h" #include "sync/notifier/invalidator_registrar.h" +#include "sync/notifier/invalidator_test_template.h" #include "sync/notifier/object_id_state_map_test_util.h" #include "testing/gtest/include/gtest/gtest.h" @@ -12,174 +15,119 @@ namespace syncer { namespace { -class InvalidatorRegistrarTest : public testing::Test { - protected: - InvalidatorRegistrarTest() - : kObjectId1(ipc::invalidation::ObjectSource::TEST, "a"), - kObjectId2(ipc::invalidation::ObjectSource::TEST, "b"), - kObjectId3(ipc::invalidation::ObjectSource::TEST, "c"), - kObjectId4(ipc::invalidation::ObjectSource::TEST, "d") { +// We test InvalidatorRegistrar by wrapping it in an Invalidator and +// running the usual Invalidator tests. + +// Thin Invalidator wrapper around InvalidatorRegistrar. +class RegistrarInvalidator : public Invalidator { + public: + virtual ~RegistrarInvalidator() {} + + InvalidatorRegistrar* GetRegistrar() { + return ®istrar_; } - const invalidation::ObjectId kObjectId1; - const invalidation::ObjectId kObjectId2; - const invalidation::ObjectId kObjectId3; - const invalidation::ObjectId kObjectId4; -}; + // Invalidator implementation. + virtual void RegisterHandler(InvalidationHandler* handler) OVERRIDE { + registrar_.RegisterHandler(handler); + } -// Register a handler, register some IDs for that handler, and then unregister -// the handler, dispatching invalidations in between. The handler should only -// see invalidations when its registered and its IDs are registered. -TEST_F(InvalidatorRegistrarTest, Basic) { - FakeInvalidationHandler handler; + virtual void UpdateRegisteredIds(InvalidationHandler* handler, + const ObjectIdSet& ids) OVERRIDE { + registrar_.UpdateRegisteredIds(handler, ids); + } - InvalidatorRegistrar registrar; + virtual void UnregisterHandler(InvalidationHandler* handler) OVERRIDE { + registrar_.UnregisterHandler(handler); + } - registrar.RegisterHandler(&handler); + virtual void SetUniqueId(const std::string& unique_id) OVERRIDE { + // Do nothing. + } - ObjectIdStateMap states; - states[kObjectId1].payload = "1"; - states[kObjectId2].payload = "2"; - states[kObjectId3].payload = "3"; + virtual void SetStateDeprecated(const std::string& state) OVERRIDE { + // Do nothing. + } - // Should be ignored since no IDs are registered to |handler|. - registrar.DispatchInvalidationsToHandlers(states, REMOTE_NOTIFICATION); - EXPECT_EQ(0, handler.GetNotificationCount()); + virtual void UpdateCredentials( + const std::string& email, const std::string& token) OVERRIDE { + // Do nothing. + } - ObjectIdSet ids; - ids.insert(kObjectId1); - ids.insert(kObjectId2); - registrar.UpdateRegisteredIds(&handler, ids); - - ObjectIdStateMap expected_states; - expected_states[kObjectId1].payload = "1"; - expected_states[kObjectId2].payload = "2"; - - registrar.DispatchInvalidationsToHandlers(states, REMOTE_NOTIFICATION); - EXPECT_EQ(1, handler.GetNotificationCount()); - EXPECT_THAT( - expected_states, - Eq(handler.GetLastNotificationIdStateMap())); - EXPECT_EQ(REMOTE_NOTIFICATION, handler.GetLastNotificationSource()); - - ids.erase(kObjectId1); - ids.insert(kObjectId3); - registrar.UpdateRegisteredIds(&handler, ids); - - expected_states.erase(kObjectId1); - expected_states[kObjectId3].payload = "3"; - - // Removed object IDs should not be notified, newly-added ones should. - registrar.DispatchInvalidationsToHandlers(states, REMOTE_NOTIFICATION); - EXPECT_EQ(2, handler.GetNotificationCount()); - EXPECT_THAT( - expected_states, - Eq(handler.GetLastNotificationIdStateMap())); - EXPECT_EQ(REMOTE_NOTIFICATION, handler.GetLastNotificationSource()); - - registrar.UnregisterHandler(&handler); - - // Should be ignored since |handler| isn't registered anymore. - registrar.DispatchInvalidationsToHandlers(states, REMOTE_NOTIFICATION); - EXPECT_EQ(2, handler.GetNotificationCount()); -} + virtual void SendNotification( + const ObjectIdStateMap& id_state_map) OVERRIDE { + // Do nothing. + } -// Register handlers and some IDs for those handlers, register a handler with -// no IDs, and register a handler with some IDs but unregister it. Then, -// dispatch some notifications and invalidations. Handlers that are registered -// should get notifications, and the ones that have registered IDs should -// receive invalidations for those IDs. -TEST_F(InvalidatorRegistrarTest, MultipleHandlers) { - FakeInvalidationHandler handler1; - FakeInvalidationHandler handler2; - FakeInvalidationHandler handler3; - FakeInvalidationHandler handler4; + private: + InvalidatorRegistrar registrar_; +}; - InvalidatorRegistrar registrar; +class RegistrarInvalidatorTestDelegate { + public: + RegistrarInvalidatorTestDelegate() {} - registrar.RegisterHandler(&handler1); - registrar.RegisterHandler(&handler2); - registrar.RegisterHandler(&handler3); - registrar.RegisterHandler(&handler4); - - { - ObjectIdSet ids; - ids.insert(kObjectId1); - ids.insert(kObjectId2); - registrar.UpdateRegisteredIds(&handler1, ids); + ~RegistrarInvalidatorTestDelegate() { + DestroyInvalidator(); } - { - ObjectIdSet ids; - ids.insert(kObjectId3); - registrar.UpdateRegisteredIds(&handler2, ids); + void CreateInvalidator( + const std::string& initial_state, + const base::WeakPtr<InvalidationStateTracker>& + invalidation_state_tracker) { + DCHECK(!invalidator_.get()); + invalidator_.reset(new RegistrarInvalidator()); } - // Don't register any IDs for handler3. + RegistrarInvalidator* GetInvalidator() { + return invalidator_.get(); + } - { - ObjectIdSet ids; - ids.insert(kObjectId4); - registrar.UpdateRegisteredIds(&handler4, ids); + void DestroyInvalidator() { + invalidator_.reset(); } - registrar.UnregisterHandler(&handler4); - - registrar.EmitOnNotificationsEnabled(); - EXPECT_EQ(NO_NOTIFICATION_ERROR, - handler1.GetNotificationsDisabledReason()); - EXPECT_EQ(NO_NOTIFICATION_ERROR, - handler2.GetNotificationsDisabledReason()); - EXPECT_EQ(NO_NOTIFICATION_ERROR, - handler3.GetNotificationsDisabledReason()); - EXPECT_EQ(TRANSIENT_NOTIFICATION_ERROR, - handler4.GetNotificationsDisabledReason()); - - { - ObjectIdStateMap states; - states[kObjectId1].payload = "1"; - states[kObjectId2].payload = "2"; - states[kObjectId3].payload = "3"; - states[kObjectId4].payload = "4"; - registrar.DispatchInvalidationsToHandlers(states, REMOTE_NOTIFICATION); - - ObjectIdStateMap expected_states; - expected_states[kObjectId1].payload = "1"; - expected_states[kObjectId2].payload = "2"; - - EXPECT_EQ(1, handler1.GetNotificationCount()); - EXPECT_THAT( - expected_states, - Eq(handler1.GetLastNotificationIdStateMap())); - EXPECT_EQ(REMOTE_NOTIFICATION, handler1.GetLastNotificationSource()); - - expected_states.clear(); - expected_states[kObjectId3].payload = "3"; - - EXPECT_EQ(1, handler2.GetNotificationCount()); - EXPECT_THAT( - expected_states, - Eq(handler2.GetLastNotificationIdStateMap())); - EXPECT_EQ(REMOTE_NOTIFICATION, handler2.GetLastNotificationSource()); - - EXPECT_EQ(0, handler3.GetNotificationCount()); - EXPECT_EQ(0, handler4.GetNotificationCount()); + void WaitForInvalidator() { + // Do nothing. } - registrar.EmitOnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR); - EXPECT_EQ(TRANSIENT_NOTIFICATION_ERROR, - handler1.GetNotificationsDisabledReason()); - EXPECT_EQ(TRANSIENT_NOTIFICATION_ERROR, - handler2.GetNotificationsDisabledReason()); - EXPECT_EQ(TRANSIENT_NOTIFICATION_ERROR, - handler3.GetNotificationsDisabledReason()); - EXPECT_EQ(TRANSIENT_NOTIFICATION_ERROR, - handler4.GetNotificationsDisabledReason()); -} + void TriggerOnNotificationsEnabled() { + invalidator_->GetRegistrar()->EmitOnNotificationsEnabled(); + } + + void TriggerOnIncomingNotification(const ObjectIdStateMap& id_state_map, + IncomingNotificationSource source) { + invalidator_->GetRegistrar()->DispatchInvalidationsToHandlers( + id_state_map, source); + } + + void TriggerOnNotificationsDisabled(NotificationsDisabledReason reason) { + invalidator_->GetRegistrar()->EmitOnNotificationsDisabled(reason); + } + + static bool InvalidatorHandlesDeprecatedState() { + return false; + } + + private: + scoped_ptr<RegistrarInvalidator> invalidator_; +}; + +INSTANTIATE_TYPED_TEST_CASE_P( + RegistrarInvalidatorTest, InvalidatorTest, + RegistrarInvalidatorTestDelegate); + +class InvalidatorRegistrarTest : public testing::Test {}; // Multiple registrations by different handlers on the same object ID should // cause a CHECK. +// +// Technically this can be part of InvalidatorTest, but we want to keep the +// number of death tests down. TEST_F(InvalidatorRegistrarTest, MultipleRegistration) { + const invalidation::ObjectId id1(ipc::invalidation::ObjectSource::TEST, "a"); + const invalidation::ObjectId id2(ipc::invalidation::ObjectSource::TEST, "a"); + InvalidatorRegistrar registrar; FakeInvalidationHandler handler1; @@ -189,8 +137,8 @@ TEST_F(InvalidatorRegistrarTest, MultipleRegistration) { registrar.RegisterHandler(&handler2); ObjectIdSet ids; - ids.insert(kObjectId1); - ids.insert(kObjectId2); + ids.insert(id1); + ids.insert(id2); registrar.UpdateRegisteredIds(&handler1, ids); registrar.DetachFromThreadForTest(); @@ -199,60 +147,6 @@ TEST_F(InvalidatorRegistrarTest, MultipleRegistration) { EXPECT_DEATH({ registrar.UpdateRegisteredIds(&handler2, ids); }, ""); } -// Make sure that passing an empty set to UpdateRegisteredIds clears the -// corresponding entries for the handler. -TEST_F(InvalidatorRegistrarTest, EmptySetUnregisters) { - FakeInvalidationHandler handler1; - - // Control observer. - FakeInvalidationHandler handler2; - - InvalidatorRegistrar registrar; - - registrar.RegisterHandler(&handler1); - registrar.RegisterHandler(&handler2); - - { - ObjectIdSet ids; - ids.insert(kObjectId1); - ids.insert(kObjectId2); - registrar.UpdateRegisteredIds(&handler1, ids); - } - - { - ObjectIdSet ids; - ids.insert(kObjectId3); - registrar.UpdateRegisteredIds(&handler2, ids); - } - - // Unregister the IDs for the first observer. It should not receive any - // further invalidations. - registrar.UpdateRegisteredIds(&handler1, ObjectIdSet()); - - registrar.EmitOnNotificationsEnabled(); - EXPECT_EQ(NO_NOTIFICATION_ERROR, - handler1.GetNotificationsDisabledReason()); - EXPECT_EQ(NO_NOTIFICATION_ERROR, - handler2.GetNotificationsDisabledReason()); - - { - ObjectIdStateMap states; - states[kObjectId1].payload = "1"; - states[kObjectId2].payload = "2"; - states[kObjectId3].payload = "3"; - registrar.DispatchInvalidationsToHandlers(states, - REMOTE_NOTIFICATION); - EXPECT_EQ(0, handler1.GetNotificationCount()); - EXPECT_EQ(1, handler2.GetNotificationCount()); - } - - registrar.EmitOnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR); - EXPECT_EQ(TRANSIENT_NOTIFICATION_ERROR, - handler1.GetNotificationsDisabledReason()); - EXPECT_EQ(TRANSIENT_NOTIFICATION_ERROR, - handler2.GetNotificationsDisabledReason()); -} - } // namespace } // namespace syncer |