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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 // // observers of the Invalidator implementation with the given | 50 // // observers of the Invalidator implementation with the given |
51 // // parameters. | 51 // // parameters. |
52 // void TriggerOnInvalidatorStateChange(InvalidatorState state) { | 52 // void TriggerOnInvalidatorStateChange(InvalidatorState state) { |
53 // ... | 53 // ... |
54 // } | 54 // } |
55 // | 55 // |
56 // // Should cause OnIncomingInvalidation() to be called on all | 56 // // Should cause OnIncomingInvalidation() to be called on all |
57 // // observers of the Invalidator implementation with the given | 57 // // observers of the Invalidator implementation with the given |
58 // // parameters. | 58 // // parameters. |
59 // void TriggerOnIncomingInvalidation( | 59 // void TriggerOnIncomingInvalidation( |
60 // const ObjectIdInvalidationMap& invalidation_map, | 60 // const ObjectIdInvalidationMap& invalidation_map) { |
61 // IncomingInvalidationSource source) { | |
62 // ... | 61 // ... |
63 // } | 62 // } |
64 // }; | 63 // }; |
65 // | 64 // |
66 // The InvalidatorTest test harness will have a member variable of | 65 // The InvalidatorTest test harness will have a member variable of |
67 // this delegate type and will call its functions in the various | 66 // this delegate type and will call its functions in the various |
68 // tests. | 67 // tests. |
69 // | 68 // |
70 // Then you simply #include this file as well as gtest.h and add the | 69 // Then you simply #include this file as well as gtest.h and add the |
71 // following statement to my_sync_notifier_unittest.cc: | 70 // following statement to my_sync_notifier_unittest.cc: |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 FakeInvalidationHandler handler; | 134 FakeInvalidationHandler handler; |
136 | 135 |
137 invalidator->RegisterHandler(&handler); | 136 invalidator->RegisterHandler(&handler); |
138 | 137 |
139 ObjectIdInvalidationMap states; | 138 ObjectIdInvalidationMap states; |
140 states[this->id1].payload = "1"; | 139 states[this->id1].payload = "1"; |
141 states[this->id2].payload = "2"; | 140 states[this->id2].payload = "2"; |
142 states[this->id3].payload = "3"; | 141 states[this->id3].payload = "3"; |
143 | 142 |
144 // Should be ignored since no IDs are registered to |handler|. | 143 // Should be ignored since no IDs are registered to |handler|. |
145 this->delegate_.TriggerOnIncomingInvalidation(states, REMOTE_INVALIDATION); | 144 this->delegate_.TriggerOnIncomingInvalidation(states); |
146 EXPECT_EQ(0, handler.GetInvalidationCount()); | 145 EXPECT_EQ(0, handler.GetInvalidationCount()); |
147 | 146 |
148 ObjectIdSet ids; | 147 ObjectIdSet ids; |
149 ids.insert(this->id1); | 148 ids.insert(this->id1); |
150 ids.insert(this->id2); | 149 ids.insert(this->id2); |
151 invalidator->UpdateRegisteredIds(&handler, ids); | 150 invalidator->UpdateRegisteredIds(&handler, ids); |
152 | 151 |
153 this->delegate_.TriggerOnInvalidatorStateChange(INVALIDATIONS_ENABLED); | 152 this->delegate_.TriggerOnInvalidatorStateChange(INVALIDATIONS_ENABLED); |
154 EXPECT_EQ(INVALIDATIONS_ENABLED, handler.GetInvalidatorState()); | 153 EXPECT_EQ(INVALIDATIONS_ENABLED, handler.GetInvalidatorState()); |
155 | 154 |
156 ObjectIdInvalidationMap expected_states; | 155 ObjectIdInvalidationMap expected_states; |
157 expected_states[this->id1].payload = "1"; | 156 expected_states[this->id1].payload = "1"; |
158 expected_states[this->id2].payload = "2"; | 157 expected_states[this->id2].payload = "2"; |
159 | 158 |
160 this->delegate_.TriggerOnIncomingInvalidation(states, REMOTE_INVALIDATION); | 159 this->delegate_.TriggerOnIncomingInvalidation(states); |
161 EXPECT_EQ(1, handler.GetInvalidationCount()); | 160 EXPECT_EQ(1, handler.GetInvalidationCount()); |
162 EXPECT_THAT(expected_states, Eq(handler.GetLastInvalidationMap())); | 161 EXPECT_THAT(expected_states, Eq(handler.GetLastInvalidationMap())); |
163 EXPECT_EQ(REMOTE_INVALIDATION, handler.GetLastInvalidationSource()); | |
164 | 162 |
165 ids.erase(this->id1); | 163 ids.erase(this->id1); |
166 ids.insert(this->id3); | 164 ids.insert(this->id3); |
167 invalidator->UpdateRegisteredIds(&handler, ids); | 165 invalidator->UpdateRegisteredIds(&handler, ids); |
168 | 166 |
169 expected_states.erase(this->id1); | 167 expected_states.erase(this->id1); |
170 expected_states[this->id3].payload = "3"; | 168 expected_states[this->id3].payload = "3"; |
171 | 169 |
172 // Removed object IDs should not be notified, newly-added ones should. | 170 // Removed object IDs should not be notified, newly-added ones should. |
173 this->delegate_.TriggerOnIncomingInvalidation(states, REMOTE_INVALIDATION); | 171 this->delegate_.TriggerOnIncomingInvalidation(states); |
174 EXPECT_EQ(2, handler.GetInvalidationCount()); | 172 EXPECT_EQ(2, handler.GetInvalidationCount()); |
175 EXPECT_THAT(expected_states, Eq(handler.GetLastInvalidationMap())); | 173 EXPECT_THAT(expected_states, Eq(handler.GetLastInvalidationMap())); |
176 EXPECT_EQ(REMOTE_INVALIDATION, handler.GetLastInvalidationSource()); | |
177 | 174 |
178 this->delegate_.TriggerOnInvalidatorStateChange(TRANSIENT_INVALIDATION_ERROR); | 175 this->delegate_.TriggerOnInvalidatorStateChange(TRANSIENT_INVALIDATION_ERROR); |
179 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, | 176 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, |
180 handler.GetInvalidatorState()); | 177 handler.GetInvalidatorState()); |
181 | 178 |
182 this->delegate_.TriggerOnInvalidatorStateChange( | 179 this->delegate_.TriggerOnInvalidatorStateChange( |
183 INVALIDATION_CREDENTIALS_REJECTED); | 180 INVALIDATION_CREDENTIALS_REJECTED); |
184 EXPECT_EQ(INVALIDATION_CREDENTIALS_REJECTED, | 181 EXPECT_EQ(INVALIDATION_CREDENTIALS_REJECTED, |
185 handler.GetInvalidatorState()); | 182 handler.GetInvalidatorState()); |
186 | 183 |
187 invalidator->UnregisterHandler(&handler); | 184 invalidator->UnregisterHandler(&handler); |
188 | 185 |
189 // Should be ignored since |handler| isn't registered anymore. | 186 // Should be ignored since |handler| isn't registered anymore. |
190 this->delegate_.TriggerOnIncomingInvalidation(states, REMOTE_INVALIDATION); | 187 this->delegate_.TriggerOnIncomingInvalidation(states); |
191 EXPECT_EQ(2, handler.GetInvalidationCount()); | 188 EXPECT_EQ(2, handler.GetInvalidationCount()); |
192 } | 189 } |
193 | 190 |
194 // Register handlers and some IDs for those handlers, register a handler with | 191 // Register handlers and some IDs for those handlers, register a handler with |
195 // no IDs, and register a handler with some IDs but unregister it. Then, | 192 // no IDs, and register a handler with some IDs but unregister it. Then, |
196 // dispatch some invalidations and invalidations. Handlers that are registered | 193 // dispatch some invalidations and invalidations. Handlers that are registered |
197 // should get invalidations, and the ones that have registered IDs should | 194 // should get invalidations, and the ones that have registered IDs should |
198 // receive invalidations for those IDs. | 195 // receive invalidations for those IDs. |
199 TYPED_TEST_P(InvalidatorTest, MultipleHandlers) { | 196 TYPED_TEST_P(InvalidatorTest, MultipleHandlers) { |
200 Invalidator* const invalidator = this->CreateAndInitializeInvalidator(); | 197 Invalidator* const invalidator = this->CreateAndInitializeInvalidator(); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 EXPECT_EQ(INVALIDATIONS_ENABLED, handler2.GetInvalidatorState()); | 234 EXPECT_EQ(INVALIDATIONS_ENABLED, handler2.GetInvalidatorState()); |
238 EXPECT_EQ(INVALIDATIONS_ENABLED, handler3.GetInvalidatorState()); | 235 EXPECT_EQ(INVALIDATIONS_ENABLED, handler3.GetInvalidatorState()); |
239 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler4.GetInvalidatorState()); | 236 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler4.GetInvalidatorState()); |
240 | 237 |
241 { | 238 { |
242 ObjectIdInvalidationMap states; | 239 ObjectIdInvalidationMap states; |
243 states[this->id1].payload = "1"; | 240 states[this->id1].payload = "1"; |
244 states[this->id2].payload = "2"; | 241 states[this->id2].payload = "2"; |
245 states[this->id3].payload = "3"; | 242 states[this->id3].payload = "3"; |
246 states[this->id4].payload = "4"; | 243 states[this->id4].payload = "4"; |
247 this->delegate_.TriggerOnIncomingInvalidation(states, REMOTE_INVALIDATION); | 244 this->delegate_.TriggerOnIncomingInvalidation(states); |
248 | 245 |
249 ObjectIdInvalidationMap expected_states; | 246 ObjectIdInvalidationMap expected_states; |
250 expected_states[this->id1].payload = "1"; | 247 expected_states[this->id1].payload = "1"; |
251 expected_states[this->id2].payload = "2"; | 248 expected_states[this->id2].payload = "2"; |
252 | 249 |
253 EXPECT_EQ(1, handler1.GetInvalidationCount()); | 250 EXPECT_EQ(1, handler1.GetInvalidationCount()); |
254 EXPECT_THAT(expected_states, Eq(handler1.GetLastInvalidationMap())); | 251 EXPECT_THAT(expected_states, Eq(handler1.GetLastInvalidationMap())); |
255 EXPECT_EQ(REMOTE_INVALIDATION, handler1.GetLastInvalidationSource()); | |
256 | 252 |
257 expected_states.clear(); | 253 expected_states.clear(); |
258 expected_states[this->id3].payload = "3"; | 254 expected_states[this->id3].payload = "3"; |
259 | 255 |
260 EXPECT_EQ(1, handler2.GetInvalidationCount()); | 256 EXPECT_EQ(1, handler2.GetInvalidationCount()); |
261 EXPECT_THAT(expected_states, Eq(handler2.GetLastInvalidationMap())); | 257 EXPECT_THAT(expected_states, Eq(handler2.GetLastInvalidationMap())); |
262 EXPECT_EQ(REMOTE_INVALIDATION, handler2.GetLastInvalidationSource()); | |
263 | 258 |
264 EXPECT_EQ(0, handler3.GetInvalidationCount()); | 259 EXPECT_EQ(0, handler3.GetInvalidationCount()); |
265 EXPECT_EQ(0, handler4.GetInvalidationCount()); | 260 EXPECT_EQ(0, handler4.GetInvalidationCount()); |
266 } | 261 } |
267 | 262 |
268 this->delegate_.TriggerOnInvalidatorStateChange(TRANSIENT_INVALIDATION_ERROR); | 263 this->delegate_.TriggerOnInvalidatorStateChange(TRANSIENT_INVALIDATION_ERROR); |
269 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler1.GetInvalidatorState()); | 264 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler1.GetInvalidatorState()); |
270 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler2.GetInvalidatorState()); | 265 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler2.GetInvalidatorState()); |
271 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler3.GetInvalidatorState()); | 266 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler3.GetInvalidatorState()); |
272 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler4.GetInvalidatorState()); | 267 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler4.GetInvalidatorState()); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
308 | 303 |
309 this->delegate_.TriggerOnInvalidatorStateChange(INVALIDATIONS_ENABLED); | 304 this->delegate_.TriggerOnInvalidatorStateChange(INVALIDATIONS_ENABLED); |
310 EXPECT_EQ(INVALIDATIONS_ENABLED, handler1.GetInvalidatorState()); | 305 EXPECT_EQ(INVALIDATIONS_ENABLED, handler1.GetInvalidatorState()); |
311 EXPECT_EQ(INVALIDATIONS_ENABLED, handler2.GetInvalidatorState()); | 306 EXPECT_EQ(INVALIDATIONS_ENABLED, handler2.GetInvalidatorState()); |
312 | 307 |
313 { | 308 { |
314 ObjectIdInvalidationMap states; | 309 ObjectIdInvalidationMap states; |
315 states[this->id1].payload = "1"; | 310 states[this->id1].payload = "1"; |
316 states[this->id2].payload = "2"; | 311 states[this->id2].payload = "2"; |
317 states[this->id3].payload = "3"; | 312 states[this->id3].payload = "3"; |
318 this->delegate_.TriggerOnIncomingInvalidation(states, REMOTE_INVALIDATION); | 313 this->delegate_.TriggerOnIncomingInvalidation(states); |
319 EXPECT_EQ(0, handler1.GetInvalidationCount()); | 314 EXPECT_EQ(0, handler1.GetInvalidationCount()); |
320 EXPECT_EQ(1, handler2.GetInvalidationCount()); | 315 EXPECT_EQ(1, handler2.GetInvalidationCount()); |
321 } | 316 } |
322 | 317 |
323 this->delegate_.TriggerOnInvalidatorStateChange(TRANSIENT_INVALIDATION_ERROR); | 318 this->delegate_.TriggerOnInvalidatorStateChange(TRANSIENT_INVALIDATION_ERROR); |
324 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler1.GetInvalidatorState()); | 319 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler1.GetInvalidatorState()); |
325 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler2.GetInvalidatorState()); | 320 EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler2.GetInvalidatorState()); |
326 | 321 |
327 invalidator->UnregisterHandler(&handler2); | 322 invalidator->UnregisterHandler(&handler2); |
328 invalidator->UnregisterHandler(&handler1); | 323 invalidator->UnregisterHandler(&handler1); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
372 invalidator->UnregisterHandler(&handler); | 367 invalidator->UnregisterHandler(&handler); |
373 } | 368 } |
374 | 369 |
375 REGISTER_TYPED_TEST_CASE_P(InvalidatorTest, | 370 REGISTER_TYPED_TEST_CASE_P(InvalidatorTest, |
376 Basic, MultipleHandlers, EmptySetUnregisters, | 371 Basic, MultipleHandlers, EmptySetUnregisters, |
377 GetInvalidatorStateAlwaysCurrent); | 372 GetInvalidatorStateAlwaysCurrent); |
378 | 373 |
379 } // namespace syncer | 374 } // namespace syncer |
380 | 375 |
381 #endif // SYNC_NOTIFIER_INVALIDATOR_TEST_TEMPLATE_H_ | 376 #endif // SYNC_NOTIFIER_INVALIDATOR_TEST_TEMPLATE_H_ |
OLD | NEW |