1 // Copyright 2014 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 "components/invalidation/fake_invalidation_handler.h"
9 #include "components/invalidation/invalidator_registrar.h"
10 #include "components/invalidation/invalidator_test_template.h"
11 #include "google/cacheinvalidation/types.pb.h"
12 #include "testing/gtest/include/gtest/gtest.h"
18 // We test InvalidatorRegistrar by wrapping it in an Invalidator and
19 // running the usual Invalidator tests.
21 // Thin Invalidator wrapper around InvalidatorRegistrar.
22 class RegistrarInvalidator
: public Invalidator
{
24 RegistrarInvalidator() {}
25 ~RegistrarInvalidator() override
{}
27 InvalidatorRegistrar
* GetRegistrar() {
31 // Invalidator implementation.
32 void RegisterHandler(InvalidationHandler
* handler
) override
{
33 registrar_
.RegisterHandler(handler
);
36 void UpdateRegisteredIds(InvalidationHandler
* handler
,
37 const ObjectIdSet
& ids
) override
{
38 registrar_
.UpdateRegisteredIds(handler
, ids
);
41 void UnregisterHandler(InvalidationHandler
* handler
) override
{
42 registrar_
.UnregisterHandler(handler
);
45 InvalidatorState
GetInvalidatorState() const override
{
46 return registrar_
.GetInvalidatorState();
49 void UpdateCredentials(const std::string
& email
,
50 const std::string
& token
) override
{
54 void RequestDetailedStatus(
55 base::Callback
<void(const base::DictionaryValue
&)> call
) const override
{
60 InvalidatorRegistrar registrar_
;
62 DISALLOW_COPY_AND_ASSIGN(RegistrarInvalidator
);
65 class RegistrarInvalidatorTestDelegate
{
67 RegistrarInvalidatorTestDelegate() {}
69 ~RegistrarInvalidatorTestDelegate() {
73 void CreateInvalidator(
74 const std::string
& invalidator_client_id
,
75 const std::string
& initial_state
,
76 const base::WeakPtr
<InvalidationStateTracker
>&
77 invalidation_state_tracker
) {
78 DCHECK(!invalidator_
.get());
79 invalidator_
.reset(new RegistrarInvalidator());
82 RegistrarInvalidator
* GetInvalidator() {
83 return invalidator_
.get();
86 void DestroyInvalidator() {
90 void WaitForInvalidator() {
94 void TriggerOnInvalidatorStateChange(InvalidatorState state
) {
95 invalidator_
->GetRegistrar()->UpdateInvalidatorState(state
);
98 void TriggerOnIncomingInvalidation(
99 const ObjectIdInvalidationMap
& invalidation_map
) {
100 invalidator_
->GetRegistrar()->DispatchInvalidationsToHandlers(
105 scoped_ptr
<RegistrarInvalidator
> invalidator_
;
108 INSTANTIATE_TYPED_TEST_CASE_P(
109 RegistrarInvalidatorTest
, InvalidatorTest
,
110 RegistrarInvalidatorTestDelegate
);
112 class InvalidatorRegistrarTest
: public testing::Test
{};
114 // Technically the test below can be part of InvalidatorTest, but we
115 // want to keep the number of death tests down.
117 // When we expect a death via CHECK(), we can't match against the
118 // CHECK() message since they are removed in official builds.
120 #if GTEST_HAS_DEATH_TEST
122 // Multiple registrations by different handlers on the same object ID should
124 TEST_F(InvalidatorRegistrarTest
, MultipleRegistration
) {
125 const invalidation::ObjectId
id1(ipc::invalidation::ObjectSource::TEST
, "a");
126 const invalidation::ObjectId
id2(ipc::invalidation::ObjectSource::TEST
, "a");
128 InvalidatorRegistrar registrar
;
130 FakeInvalidationHandler handler1
;
131 registrar
.RegisterHandler(&handler1
);
133 FakeInvalidationHandler handler2
;
134 registrar
.RegisterHandler(&handler2
);
139 registrar
.UpdateRegisteredIds(&handler1
, ids
);
141 registrar
.DetachFromThreadForTest();
142 EXPECT_DEATH({ registrar
.UpdateRegisteredIds(&handler2
, ids
); }, "");
144 registrar
.UnregisterHandler(&handler2
);
145 registrar
.UnregisterHandler(&handler1
);
147 #endif // GTEST_HAS_DEATH_TEST
151 } // namespace syncer