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 // This class defines tests that implementations of Invalidator should pass in | 5 // This class defines tests that implementations of Invalidator should pass in |
6 // order to be conformant. Here's how you use it to test your implementation. | 6 // order to be conformant. Here's how you use it to test your implementation. |
7 // | 7 // |
8 // Say your class is called MyInvalidator. Then you need to define a class | 8 // Say your class is called MyInvalidator. Then you need to define a class |
9 // called MyInvalidatorTestDelegate in my_sync_notifier_unittest.cc like this: | 9 // called MyInvalidatorTestDelegate in my_sync_notifier_unittest.cc like this: |
10 // | 10 // |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 // | 74 // |
75 // Easy! | 75 // Easy! |
76 | 76 |
77 #ifndef SYNC_NOTIFIER_INVALIDATOR_TEST_TEMPLATE_H_ | 77 #ifndef SYNC_NOTIFIER_INVALIDATOR_TEST_TEMPLATE_H_ |
78 #define SYNC_NOTIFIER_INVALIDATOR_TEST_TEMPLATE_H_ | 78 #define SYNC_NOTIFIER_INVALIDATOR_TEST_TEMPLATE_H_ |
79 | 79 |
80 #include "base/basictypes.h" | 80 #include "base/basictypes.h" |
81 #include "base/compiler_specific.h" | 81 #include "base/compiler_specific.h" |
82 #include "google/cacheinvalidation/include/types.h" | 82 #include "google/cacheinvalidation/include/types.h" |
83 #include "google/cacheinvalidation/types.pb.h" | 83 #include "google/cacheinvalidation/types.pb.h" |
| 84 #include "sync/internal_api/public/base/invalidation_test_util.h" |
| 85 #include "sync/internal_api/public/base/object_id_invalidation_map_test_util.h" |
84 #include "sync/notifier/fake_invalidation_handler.h" | 86 #include "sync/notifier/fake_invalidation_handler.h" |
85 #include "sync/notifier/fake_invalidation_state_tracker.h" | 87 #include "sync/notifier/fake_invalidation_state_tracker.h" |
86 #include "sync/notifier/invalidator.h" | 88 #include "sync/notifier/invalidator.h" |
87 #include "sync/notifier/object_id_invalidation_map.h" | |
88 #include "sync/notifier/object_id_invalidation_map_test_util.h" | |
89 #include "testing/gtest/include/gtest/gtest.h" | 89 #include "testing/gtest/include/gtest/gtest.h" |
90 | 90 |
91 namespace syncer { | 91 namespace syncer { |
92 | 92 |
93 template <typename InvalidatorTestDelegate> | 93 template <typename InvalidatorTestDelegate> |
94 class InvalidatorTest : public testing::Test { | 94 class InvalidatorTest : public testing::Test { |
95 protected: | 95 protected: |
96 InvalidatorTest() | 96 InvalidatorTest() |
97 : id1(ipc::invalidation::ObjectSource::TEST, "a"), | 97 : id1(ipc::invalidation::ObjectSource::TEST, "a"), |
98 id2(ipc::invalidation::ObjectSource::TEST, "b"), | 98 id2(ipc::invalidation::ObjectSource::TEST, "b"), |
(...skipping 29 matching lines...) Expand all Loading... |
128 // handler, and then unregister the handler, dispatching invalidations in | 128 // handler, and then unregister the handler, dispatching invalidations in |
129 // between. The handler should only see invalidations when its registered and | 129 // between. The handler should only see invalidations when its registered and |
130 // its IDs are registered. | 130 // its IDs are registered. |
131 TYPED_TEST_P(InvalidatorTest, Basic) { | 131 TYPED_TEST_P(InvalidatorTest, Basic) { |
132 Invalidator* const invalidator = this->CreateAndInitializeInvalidator(); | 132 Invalidator* const invalidator = this->CreateAndInitializeInvalidator(); |
133 | 133 |
134 FakeInvalidationHandler handler; | 134 FakeInvalidationHandler handler; |
135 | 135 |
136 invalidator->RegisterHandler(&handler); | 136 invalidator->RegisterHandler(&handler); |
137 | 137 |
138 ObjectIdInvalidationMap states; | 138 ObjectIdInvalidationMap invalidation_map; |
139 states[this->id1].payload = "1"; | 139 invalidation_map.Insert(Invalidation::Init(this->id1, 1, "1")); |
140 states[this->id2].payload = "2"; | 140 invalidation_map.Insert(Invalidation::Init(this->id2, 2, "2")); |
141 states[this->id3].payload = "3"; | 141 invalidation_map.Insert(Invalidation::Init(this->id3, 3, "3")); |
142 | 142 |
143 // Should be ignored since no IDs are registered to |handler|. | 143 // Should be ignored since no IDs are registered to |handler|. |
144 this->delegate_.TriggerOnIncomingInvalidation(states); | 144 this->delegate_.TriggerOnIncomingInvalidation(invalidation_map); |
145 EXPECT_EQ(0, handler.GetInvalidationCount()); | 145 EXPECT_EQ(0, handler.GetInvalidationCount()); |
146 | 146 |
147 ObjectIdSet ids; | 147 ObjectIdSet ids; |
148 ids.insert(this->id1); | 148 ids.insert(this->id1); |
149 ids.insert(this->id2); | 149 ids.insert(this->id2); |
150 invalidator->UpdateRegisteredIds(&handler, ids); | 150 invalidator->UpdateRegisteredIds(&handler, ids); |
151 | 151 |
152 this->delegate_.TriggerOnInvalidatorStateChange(INVALIDATIONS_ENABLED); | 152 this->delegate_.TriggerOnInvalidatorStateChange(INVALIDATIONS_ENABLED); |
153 EXPECT_EQ(INVALIDATIONS_ENABLED, handler.GetInvalidatorState()); | 153 EXPECT_EQ(INVALIDATIONS_ENABLED, handler.GetInvalidatorState()); |
154 | 154 |
155 ObjectIdInvalidationMap expected_states; | 155 ObjectIdInvalidationMap expected_invalidations; |
156 expected_states[this->id1].payload = "1"; | 156 expected_invalidations.Insert(Invalidation::Init(this->id1, 1, "1")); |
157 expected_states[this->id2].payload = "2"; | 157 expected_invalidations.Insert(Invalidation::Init(this->id2, 2, "2")); |
158 | 158 |
159 this->delegate_.TriggerOnIncomingInvalidation(states); | 159 this->delegate_.TriggerOnIncomingInvalidation(invalidation_map); |
160 EXPECT_EQ(1, handler.GetInvalidationCount()); | 160 EXPECT_EQ(1, handler.GetInvalidationCount()); |
161 EXPECT_THAT(expected_states, Eq(handler.GetLastInvalidationMap())); | 161 EXPECT_THAT(expected_invalidations, Eq(handler.GetLastInvalidationMap())); |
162 | 162 |
163 ids.erase(this->id1); | 163 ids.erase(this->id1); |
164 ids.insert(this->id3); | 164 ids.insert(this->id3); |
165 invalidator->UpdateRegisteredIds(&handler, ids); | 165 invalidator->UpdateRegisteredIds(&handler, ids); |
166 | 166 |
167 expected_states.erase(this->id1); | 167 expected_invalidations = ObjectIdInvalidationMap(); |
168 expected_states[this->id3].payload = "3"; | 168 expected_invalidations.Insert(Invalidation::Init(this->id2, 2, "2")); |
| 169 expected_invalidations.Insert(Invalidation::Init(this->id3, 3, "3")); |
169 | 170 |
170 // Removed object IDs should not be notified, newly-added ones should. | 171 // Removed object IDs should not be notified, newly-added ones should. |
171 this->delegate_.TriggerOnIncomingInvalidation(states); | 172 this->delegate_.TriggerOnIncomingInvalidation(invalidation_map); |
172 EXPECT_EQ(2, handler.GetInvalidationCount()); | 173 EXPECT_EQ(2, handler.GetInvalidationCount()); |
173 EXPECT_THAT(expected_states, Eq(handler.GetLastInvalidationMap())); | 174 EXPECT_THAT(expected_invalidations, Eq(handler.GetLastInvalidationMap())); |
174 | 175 |
175 this->delegate_.TriggerOnInvalidatorStateChange(TRANSIENT_INVALIDATION_ERROR); | 176 this->delegate_.TriggerOnInvalidatorStateChange(TRANSIENT_INVALIDATION_ERROR); |
176 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, | 177 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, |
177 handler.GetInvalidatorState()); | 178 handler.GetInvalidatorState()); |
178 | 179 |
179 this->delegate_.TriggerOnInvalidatorStateChange( | 180 this->delegate_.TriggerOnInvalidatorStateChange( |
180 INVALIDATION_CREDENTIALS_REJECTED); | 181 INVALIDATION_CREDENTIALS_REJECTED); |
181 EXPECT_EQ(INVALIDATION_CREDENTIALS_REJECTED, | 182 EXPECT_EQ(INVALIDATION_CREDENTIALS_REJECTED, |
182 handler.GetInvalidatorState()); | 183 handler.GetInvalidatorState()); |
183 | 184 |
184 invalidator->UnregisterHandler(&handler); | 185 invalidator->UnregisterHandler(&handler); |
185 | 186 |
186 // Should be ignored since |handler| isn't registered anymore. | 187 // Should be ignored since |handler| isn't registered anymore. |
187 this->delegate_.TriggerOnIncomingInvalidation(states); | 188 this->delegate_.TriggerOnIncomingInvalidation(invalidation_map); |
188 EXPECT_EQ(2, handler.GetInvalidationCount()); | 189 EXPECT_EQ(2, handler.GetInvalidationCount()); |
189 } | 190 } |
190 | 191 |
191 // Register handlers and some IDs for those handlers, register a handler with | 192 // Register handlers and some IDs for those handlers, register a handler with |
192 // no IDs, and register a handler with some IDs but unregister it. Then, | 193 // no IDs, and register a handler with some IDs but unregister it. Then, |
193 // dispatch some invalidations and invalidations. Handlers that are registered | 194 // dispatch some invalidations and invalidations. Handlers that are registered |
194 // should get invalidations, and the ones that have registered IDs should | 195 // should get invalidations, and the ones that have registered IDs should |
195 // receive invalidations for those IDs. | 196 // receive invalidations for those IDs. |
196 TYPED_TEST_P(InvalidatorTest, MultipleHandlers) { | 197 TYPED_TEST_P(InvalidatorTest, MultipleHandlers) { |
197 Invalidator* const invalidator = this->CreateAndInitializeInvalidator(); | 198 Invalidator* const invalidator = this->CreateAndInitializeInvalidator(); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 | 230 |
230 invalidator->UnregisterHandler(&handler4); | 231 invalidator->UnregisterHandler(&handler4); |
231 | 232 |
232 this->delegate_.TriggerOnInvalidatorStateChange(INVALIDATIONS_ENABLED); | 233 this->delegate_.TriggerOnInvalidatorStateChange(INVALIDATIONS_ENABLED); |
233 EXPECT_EQ(INVALIDATIONS_ENABLED, handler1.GetInvalidatorState()); | 234 EXPECT_EQ(INVALIDATIONS_ENABLED, handler1.GetInvalidatorState()); |
234 EXPECT_EQ(INVALIDATIONS_ENABLED, handler2.GetInvalidatorState()); | 235 EXPECT_EQ(INVALIDATIONS_ENABLED, handler2.GetInvalidatorState()); |
235 EXPECT_EQ(INVALIDATIONS_ENABLED, handler3.GetInvalidatorState()); | 236 EXPECT_EQ(INVALIDATIONS_ENABLED, handler3.GetInvalidatorState()); |
236 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler4.GetInvalidatorState()); | 237 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler4.GetInvalidatorState()); |
237 | 238 |
238 { | 239 { |
239 ObjectIdInvalidationMap states; | 240 ObjectIdInvalidationMap invalidation_map; |
240 states[this->id1].payload = "1"; | 241 invalidation_map.Insert(Invalidation::Init(this->id1, 1, "1")); |
241 states[this->id2].payload = "2"; | 242 invalidation_map.Insert(Invalidation::Init(this->id2, 2, "2")); |
242 states[this->id3].payload = "3"; | 243 invalidation_map.Insert(Invalidation::Init(this->id3, 3, "3")); |
243 states[this->id4].payload = "4"; | 244 invalidation_map.Insert(Invalidation::Init(this->id4, 4, "4")); |
244 this->delegate_.TriggerOnIncomingInvalidation(states); | |
245 | 245 |
246 ObjectIdInvalidationMap expected_states; | 246 this->delegate_.TriggerOnIncomingInvalidation(invalidation_map); |
247 expected_states[this->id1].payload = "1"; | 247 |
248 expected_states[this->id2].payload = "2"; | 248 ObjectIdInvalidationMap expected_invalidations; |
| 249 expected_invalidations.Insert(Invalidation::Init(this->id1, 1, "1")); |
| 250 expected_invalidations.Insert(Invalidation::Init(this->id2, 2, "2")); |
249 | 251 |
250 EXPECT_EQ(1, handler1.GetInvalidationCount()); | 252 EXPECT_EQ(1, handler1.GetInvalidationCount()); |
251 EXPECT_THAT(expected_states, Eq(handler1.GetLastInvalidationMap())); | 253 EXPECT_THAT(expected_invalidations, Eq(handler1.GetLastInvalidationMap())); |
252 | 254 |
253 expected_states.clear(); | 255 expected_invalidations = ObjectIdInvalidationMap(); |
254 expected_states[this->id3].payload = "3"; | 256 expected_invalidations.Insert(Invalidation::Init(this->id3, 3, "3")); |
255 | 257 |
256 EXPECT_EQ(1, handler2.GetInvalidationCount()); | 258 EXPECT_EQ(1, handler2.GetInvalidationCount()); |
257 EXPECT_THAT(expected_states, Eq(handler2.GetLastInvalidationMap())); | 259 EXPECT_THAT(expected_invalidations, Eq(handler2.GetLastInvalidationMap())); |
258 | 260 |
259 EXPECT_EQ(0, handler3.GetInvalidationCount()); | 261 EXPECT_EQ(0, handler3.GetInvalidationCount()); |
260 EXPECT_EQ(0, handler4.GetInvalidationCount()); | 262 EXPECT_EQ(0, handler4.GetInvalidationCount()); |
261 } | 263 } |
262 | 264 |
263 this->delegate_.TriggerOnInvalidatorStateChange(TRANSIENT_INVALIDATION_ERROR); | 265 this->delegate_.TriggerOnInvalidatorStateChange(TRANSIENT_INVALIDATION_ERROR); |
264 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler1.GetInvalidatorState()); | 266 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler1.GetInvalidatorState()); |
265 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler2.GetInvalidatorState()); | 267 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler2.GetInvalidatorState()); |
266 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler3.GetInvalidatorState()); | 268 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler3.GetInvalidatorState()); |
267 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler4.GetInvalidatorState()); | 269 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler4.GetInvalidatorState()); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
299 | 301 |
300 // Unregister the IDs for the first observer. It should not receive any | 302 // Unregister the IDs for the first observer. It should not receive any |
301 // further invalidations. | 303 // further invalidations. |
302 invalidator->UpdateRegisteredIds(&handler1, ObjectIdSet()); | 304 invalidator->UpdateRegisteredIds(&handler1, ObjectIdSet()); |
303 | 305 |
304 this->delegate_.TriggerOnInvalidatorStateChange(INVALIDATIONS_ENABLED); | 306 this->delegate_.TriggerOnInvalidatorStateChange(INVALIDATIONS_ENABLED); |
305 EXPECT_EQ(INVALIDATIONS_ENABLED, handler1.GetInvalidatorState()); | 307 EXPECT_EQ(INVALIDATIONS_ENABLED, handler1.GetInvalidatorState()); |
306 EXPECT_EQ(INVALIDATIONS_ENABLED, handler2.GetInvalidatorState()); | 308 EXPECT_EQ(INVALIDATIONS_ENABLED, handler2.GetInvalidatorState()); |
307 | 309 |
308 { | 310 { |
309 ObjectIdInvalidationMap states; | 311 ObjectIdInvalidationMap invalidation_map; |
310 states[this->id1].payload = "1"; | 312 invalidation_map.Insert(Invalidation::Init(this->id1, 1, "1")); |
311 states[this->id2].payload = "2"; | 313 invalidation_map.Insert(Invalidation::Init(this->id2, 2, "2")); |
312 states[this->id3].payload = "3"; | 314 invalidation_map.Insert(Invalidation::Init(this->id3, 3, "3")); |
313 this->delegate_.TriggerOnIncomingInvalidation(states); | 315 this->delegate_.TriggerOnIncomingInvalidation(invalidation_map); |
314 EXPECT_EQ(0, handler1.GetInvalidationCount()); | 316 EXPECT_EQ(0, handler1.GetInvalidationCount()); |
315 EXPECT_EQ(1, handler2.GetInvalidationCount()); | 317 EXPECT_EQ(1, handler2.GetInvalidationCount()); |
316 } | 318 } |
317 | 319 |
318 this->delegate_.TriggerOnInvalidatorStateChange(TRANSIENT_INVALIDATION_ERROR); | 320 this->delegate_.TriggerOnInvalidatorStateChange(TRANSIENT_INVALIDATION_ERROR); |
319 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler1.GetInvalidatorState()); | 321 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler1.GetInvalidatorState()); |
320 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler2.GetInvalidatorState()); | 322 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler2.GetInvalidatorState()); |
321 | 323 |
322 invalidator->UnregisterHandler(&handler2); | 324 invalidator->UnregisterHandler(&handler2); |
323 invalidator->UnregisterHandler(&handler1); | 325 invalidator->UnregisterHandler(&handler1); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
367 invalidator->UnregisterHandler(&handler); | 369 invalidator->UnregisterHandler(&handler); |
368 } | 370 } |
369 | 371 |
370 REGISTER_TYPED_TEST_CASE_P(InvalidatorTest, | 372 REGISTER_TYPED_TEST_CASE_P(InvalidatorTest, |
371 Basic, MultipleHandlers, EmptySetUnregisters, | 373 Basic, MultipleHandlers, EmptySetUnregisters, |
372 GetInvalidatorStateAlwaysCurrent); | 374 GetInvalidatorStateAlwaysCurrent); |
373 | 375 |
374 } // namespace syncer | 376 } // namespace syncer |
375 | 377 |
376 #endif // SYNC_NOTIFIER_INVALIDATOR_TEST_TEMPLATE_H_ | 378 #endif // SYNC_NOTIFIER_INVALIDATOR_TEST_TEMPLATE_H_ |
OLD | NEW |