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.
5 #include "base/basictypes.h"
6 #include "base/compiler_specific.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "google/cacheinvalidation/types.pb.h"
9 #include "sync/notifier/fake_invalidation_handler.h"
10 #include "sync/notifier/invalidator_registrar.h"
11 #include "sync/notifier/invalidator_test_template.h"
12 #include "sync/notifier/object_id_invalidation_map_test_util.h"
13 #include "testing/gtest/include/gtest/gtest.h"
19 // We test InvalidatorRegistrar by wrapping it in an Invalidator and
20 // running the usual Invalidator tests.
22 // Thin Invalidator wrapper around InvalidatorRegistrar.
23 class RegistrarInvalidator
: public Invalidator
{
25 RegistrarInvalidator() {}
26 virtual ~RegistrarInvalidator() {}
28 InvalidatorRegistrar
* GetRegistrar() {
32 // Invalidator implementation.
33 virtual void RegisterHandler(InvalidationHandler
* handler
) OVERRIDE
{
34 registrar_
.RegisterHandler(handler
);
37 virtual void UpdateRegisteredIds(InvalidationHandler
* handler
,
38 const ObjectIdSet
& ids
) OVERRIDE
{
39 registrar_
.UpdateRegisteredIds(handler
, ids
);
42 virtual void UnregisterHandler(InvalidationHandler
* handler
) OVERRIDE
{
43 registrar_
.UnregisterHandler(handler
);
46 virtual void Acknowledge(const invalidation::ObjectId
& id
,
47 const AckHandle
& ack_handle
) OVERRIDE
{
51 virtual InvalidatorState
GetInvalidatorState() const OVERRIDE
{
52 return registrar_
.GetInvalidatorState();
55 virtual void UpdateCredentials(
56 const std::string
& email
, const std::string
& token
) OVERRIDE
{
61 InvalidatorRegistrar registrar_
;
63 DISALLOW_COPY_AND_ASSIGN(RegistrarInvalidator
);
66 class RegistrarInvalidatorTestDelegate
{
68 RegistrarInvalidatorTestDelegate() {}
70 ~RegistrarInvalidatorTestDelegate() {
74 void CreateInvalidator(
75 const std::string
& invalidator_client_id
,
76 const std::string
& initial_state
,
77 const base::WeakPtr
<InvalidationStateTracker
>&
78 invalidation_state_tracker
) {
79 DCHECK(!invalidator_
.get());
80 invalidator_
.reset(new RegistrarInvalidator());
83 RegistrarInvalidator
* GetInvalidator() {
84 return invalidator_
.get();
87 void DestroyInvalidator() {
91 void WaitForInvalidator() {
95 void TriggerOnInvalidatorStateChange(InvalidatorState state
) {
96 invalidator_
->GetRegistrar()->UpdateInvalidatorState(state
);
99 void TriggerOnIncomingInvalidation(
100 const ObjectIdInvalidationMap
& invalidation_map
) {
101 invalidator_
->GetRegistrar()->DispatchInvalidationsToHandlers(
106 scoped_ptr
<RegistrarInvalidator
> invalidator_
;
109 INSTANTIATE_TYPED_TEST_CASE_P(
110 RegistrarInvalidatorTest
, InvalidatorTest
,
111 RegistrarInvalidatorTestDelegate
);
113 class InvalidatorRegistrarTest
: public testing::Test
{};
115 // Technically the tests below can be part of InvalidatorTest, but we
116 // want to keep the number of death tests down.
118 // When we expect a death via CHECK(), we can't match against the
119 // CHECK() message since they are removed in official builds.
121 #if GTEST_HAS_DEATH_TEST
122 // Having registered handlers on destruction should cause a CHECK.
123 TEST_F(InvalidatorRegistrarTest
, RegisteredHandlerOnDestruction
) {
124 scoped_ptr
<InvalidatorRegistrar
> registrar(new InvalidatorRegistrar());
125 FakeInvalidationHandler handler
;
127 registrar
->RegisterHandler(&handler
);
129 EXPECT_DEATH({ registrar
.reset(); }, "");
131 ASSERT_TRUE(registrar
.get());
132 registrar
->UnregisterHandler(&handler
);
135 // Multiple registrations by different handlers on the same object ID should
137 TEST_F(InvalidatorRegistrarTest
, MultipleRegistration
) {
138 const invalidation::ObjectId
id1(ipc::invalidation::ObjectSource::TEST
, "a");
139 const invalidation::ObjectId
id2(ipc::invalidation::ObjectSource::TEST
, "a");
141 InvalidatorRegistrar registrar
;
143 FakeInvalidationHandler handler1
;
144 registrar
.RegisterHandler(&handler1
);
146 FakeInvalidationHandler handler2
;
147 registrar
.RegisterHandler(&handler2
);
152 registrar
.UpdateRegisteredIds(&handler1
, ids
);
154 registrar
.DetachFromThreadForTest();
155 EXPECT_DEATH({ registrar
.UpdateRegisteredIds(&handler2
, ids
); }, "");
157 registrar
.UnregisterHandler(&handler2
);
158 registrar
.UnregisterHandler(&handler1
);
160 #endif // GTEST_HAS_DEATH_TEST
164 } // namespace syncer