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 "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 "testing/gtest/include/gtest/gtest.h" | |
13 | |
14 namespace syncer { | |
15 | |
16 namespace { | |
17 | |
18 // We test InvalidatorRegistrar by wrapping it in an Invalidator and | |
19 // running the usual Invalidator tests. | |
20 | |
21 // Thin Invalidator wrapper around InvalidatorRegistrar. | |
22 class RegistrarInvalidator : public Invalidator { | |
23 public: | |
24 RegistrarInvalidator() {} | |
25 virtual ~RegistrarInvalidator() {} | |
26 | |
27 InvalidatorRegistrar* GetRegistrar() { | |
28 return ®istrar_; | |
29 } | |
30 | |
31 // Invalidator implementation. | |
32 virtual void RegisterHandler(InvalidationHandler* handler) OVERRIDE { | |
33 registrar_.RegisterHandler(handler); | |
34 } | |
35 | |
36 virtual void UpdateRegisteredIds(InvalidationHandler* handler, | |
37 const ObjectIdSet& ids) OVERRIDE { | |
38 registrar_.UpdateRegisteredIds(handler, ids); | |
39 } | |
40 | |
41 virtual void UnregisterHandler(InvalidationHandler* handler) OVERRIDE { | |
42 registrar_.UnregisterHandler(handler); | |
43 } | |
44 | |
45 virtual InvalidatorState GetInvalidatorState() const OVERRIDE { | |
46 return registrar_.GetInvalidatorState(); | |
47 } | |
48 | |
49 virtual void UpdateCredentials( | |
50 const std::string& email, const std::string& token) OVERRIDE { | |
51 // Do nothing. | |
52 } | |
53 | |
54 virtual void RequestDetailedStatus( | |
55 base::Callback<void(const base::DictionaryValue&)> call) const OVERRIDE { | |
56 // Do nothing. | |
57 } | |
58 | |
59 private: | |
60 InvalidatorRegistrar registrar_; | |
61 | |
62 DISALLOW_COPY_AND_ASSIGN(RegistrarInvalidator); | |
63 }; | |
64 | |
65 class RegistrarInvalidatorTestDelegate { | |
66 public: | |
67 RegistrarInvalidatorTestDelegate() {} | |
68 | |
69 ~RegistrarInvalidatorTestDelegate() { | |
70 DestroyInvalidator(); | |
71 } | |
72 | |
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()); | |
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 TriggerOnInvalidatorStateChange(InvalidatorState state) { | |
95 invalidator_->GetRegistrar()->UpdateInvalidatorState(state); | |
96 } | |
97 | |
98 void TriggerOnIncomingInvalidation( | |
99 const ObjectIdInvalidationMap& invalidation_map) { | |
100 invalidator_->GetRegistrar()->DispatchInvalidationsToHandlers( | |
101 invalidation_map); | |
102 } | |
103 | |
104 private: | |
105 scoped_ptr<RegistrarInvalidator> invalidator_; | |
106 }; | |
107 | |
108 INSTANTIATE_TYPED_TEST_CASE_P( | |
109 RegistrarInvalidatorTest, InvalidatorTest, | |
110 RegistrarInvalidatorTestDelegate); | |
111 | |
112 class InvalidatorRegistrarTest : public testing::Test {}; | |
113 | |
114 // Technically the tests below can be part of InvalidatorTest, but we | |
115 // want to keep the number of death tests down. | |
116 | |
117 // When we expect a death via CHECK(), we can't match against the | |
118 // CHECK() message since they are removed in official builds. | |
119 | |
120 #if GTEST_HAS_DEATH_TEST | |
121 // Having registered handlers on destruction should cause a CHECK. | |
122 TEST_F(InvalidatorRegistrarTest, RegisteredHandlerOnDestruction) { | |
123 scoped_ptr<InvalidatorRegistrar> registrar(new InvalidatorRegistrar()); | |
124 FakeInvalidationHandler handler; | |
125 | |
126 registrar->RegisterHandler(&handler); | |
127 | |
128 EXPECT_DEATH({ registrar.reset(); }, ""); | |
129 | |
130 ASSERT_TRUE(registrar.get()); | |
131 registrar->UnregisterHandler(&handler); | |
132 } | |
133 | |
134 // Multiple registrations by different handlers on the same object ID should | |
135 // cause a CHECK. | |
136 TEST_F(InvalidatorRegistrarTest, MultipleRegistration) { | |
137 const invalidation::ObjectId id1(ipc::invalidation::ObjectSource::TEST, "a"); | |
138 const invalidation::ObjectId id2(ipc::invalidation::ObjectSource::TEST, "a"); | |
139 | |
140 InvalidatorRegistrar registrar; | |
141 | |
142 FakeInvalidationHandler handler1; | |
143 registrar.RegisterHandler(&handler1); | |
144 | |
145 FakeInvalidationHandler handler2; | |
146 registrar.RegisterHandler(&handler2); | |
147 | |
148 ObjectIdSet ids; | |
149 ids.insert(id1); | |
150 ids.insert(id2); | |
151 registrar.UpdateRegisteredIds(&handler1, ids); | |
152 | |
153 registrar.DetachFromThreadForTest(); | |
154 EXPECT_DEATH({ registrar.UpdateRegisteredIds(&handler2, ids); }, ""); | |
155 | |
156 registrar.UnregisterHandler(&handler2); | |
157 registrar.UnregisterHandler(&handler1); | |
158 } | |
159 #endif // GTEST_HAS_DEATH_TEST | |
160 | |
161 } // namespace | |
162 | |
163 } // namespace syncer | |
OLD | NEW |