Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(477)

Side by Side Diff: chrome/browser/invalidation/invalidation_service_test_template.h

Issue 15580002: Make use of InvalidationService (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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 InvalidationFrontend should 5 // This class defines tests that implementations of InvalidationService should
6 // pass in order to be conformant. Here's how you use it to test your 6 // pass in order to be conformant. Here's how you use it to test your
7 // implementation. 7 // implementation.
8 // 8 //
9 // Say your class is called MyInvalidationFrontend. Then you need to define a 9 // Say your class is called MyInvalidationService. Then you need to define a
10 // class called MyInvalidationFrontendTestDelegate in 10 // class called MyInvalidationServiceTestDelegate in
11 // my_invalidation_frontend_unittest.cc like this: 11 // my_invalidation_frontend_unittest.cc like this:
12 // 12 //
13 // class MyInvalidationFrontendTestDelegate { 13 // class MyInvalidationServiceTestDelegate {
14 // public: 14 // public:
15 // MyInvalidationFrontendTestDelegate() ... 15 // MyInvalidationServiceTestDelegate() ...
16 // 16 //
17 // ~MyInvalidationFrontendTestDelegate() { 17 // ~MyInvalidationServiceTestDelegate() {
18 // // DestroyInvalidator() may not be explicitly called by tests. 18 // // DestroyInvalidator() may not be explicitly called by tests.
19 // DestroyInvalidator(); 19 // DestroyInvalidator();
20 // } 20 // }
21 // 21 //
22 // // Create the InvalidationFrontend implementation with the given params. 22 // // Create the InvalidationService implementation with the given params.
23 // void CreateInvalidationFrontend() { 23 // void CreateInvalidationService() {
24 // ... 24 // ...
25 // } 25 // }
26 // 26 //
27 // // Should return the InvalidationFrontend implementation. Only called 27 // // Should return the InvalidationService implementation. Only called
28 // // after CreateInvalidator and before DestroyInvalidator. 28 // // after CreateInvalidator and before DestroyInvalidator.
29 // MyInvalidationFrontend* GetInvalidationFrontend() { 29 // MyInvalidationService* GetInvalidationService() {
30 // ... 30 // ...
31 // } 31 // }
32 // 32 //
33 // // Destroy the InvalidationFrontend implementation. 33 // // Destroy the InvalidationService implementation.
34 // void DestroyInvalidationFrontend() { 34 // void DestroyInvalidationService() {
35 // ... 35 // ...
36 // } 36 // }
37 // 37 //
38 // // The Trigger* functions below should block until the effects of 38 // // The Trigger* functions below should block until the effects of
39 // // the call are visible on the current thread. 39 // // the call are visible on the current thread.
40 // 40 //
41 // // Should cause OnInvalidatorStateChange() to be called on all 41 // // Should cause OnInvalidatorStateChange() to be called on all
42 // // observers of the InvalidationFrontend implementation with the given 42 // // observers of the InvalidationService implementation with the given
43 // // parameters. 43 // // parameters.
44 // void TriggerOnInvalidatorStateChange(InvalidatorState state) { 44 // void TriggerOnInvalidatorStateChange(InvalidatorState state) {
45 // ... 45 // ...
46 // } 46 // }
47 // 47 //
48 // // Should cause OnIncomingInvalidation() to be called on all 48 // // Should cause OnIncomingInvalidation() to be called on all
49 // // observers of the InvalidationFrontend implementation with the given 49 // // observers of the InvalidationService implementation with the given
50 // // parameters. 50 // // parameters.
51 // void TriggerOnIncomingInvalidation( 51 // void TriggerOnIncomingInvalidation(
52 // const ObjectIdInvalidationMap& invalidation_map) { 52 // const ObjectIdInvalidationMap& invalidation_map) {
53 // ... 53 // ...
54 // } 54 // }
55 // }; 55 // };
56 // 56 //
57 // The InvalidationFrontendTest test harness will have a member variable of 57 // The InvalidationServiceTest test harness will have a member variable of
58 // this delegate type and will call its functions in the various 58 // this delegate type and will call its functions in the various
59 // tests. 59 // tests.
60 // 60 //
61 // Then you simply #include this file as well as gtest.h and add the 61 // Then you simply #include this file as well as gtest.h and add the
62 // following statement to my_sync_notifier_unittest.cc: 62 // following statement to my_sync_notifier_unittest.cc:
63 // 63 //
64 // INSTANTIATE_TYPED_TEST_CASE_P( 64 // INSTANTIATE_TYPED_TEST_CASE_P(
65 // MyInvalidationFrontend, 65 // MyInvalidationService,
66 // InvalidationFrontendTest, 66 // InvalidationServiceTest,
67 // MyInvalidatorTestDelegate); 67 // MyInvalidatorTestDelegate);
68 // 68 //
69 // Easy! 69 // Easy!
70 70
71 #ifndef CHROME_BROWSER_INVALIDATION_INVALIDATION_FRONTEND_TEST_TEMPLATE_H_ 71 #ifndef CHROME_BROWSER_INVALIDATION_INVALIDATION_SERVICE_TEST_TEMPLATE_H_
72 #define CHROME_BROWSER_INVALIDATION_INVALIDATION_FRONTEND_TEST_TEMPLATE_H_ 72 #define CHROME_BROWSER_INVALIDATION_INVALIDATION_SERVICE_TEST_TEMPLATE_H_
73 73
74 #include "base/basictypes.h" 74 #include "base/basictypes.h"
75 #include "base/compiler_specific.h" 75 #include "base/compiler_specific.h"
76 #include "chrome/browser/invalidation/invalidation_frontend.h" 76 #include "chrome/browser/invalidation/invalidation_service.h"
77 #include "google/cacheinvalidation/include/types.h" 77 #include "google/cacheinvalidation/include/types.h"
78 #include "google/cacheinvalidation/types.pb.h" 78 #include "google/cacheinvalidation/types.pb.h"
79 #include "sync/notifier/fake_invalidation_handler.h" 79 #include "sync/notifier/fake_invalidation_handler.h"
80 #include "sync/notifier/object_id_invalidation_map.h" 80 #include "sync/notifier/object_id_invalidation_map.h"
81 #include "sync/notifier/object_id_invalidation_map_test_util.h" 81 #include "sync/notifier/object_id_invalidation_map_test_util.h"
82 #include "testing/gtest/include/gtest/gtest.h" 82 #include "testing/gtest/include/gtest/gtest.h"
83 83
84 template <typename InvalidatorTestDelegate> 84 template <typename InvalidatorTestDelegate>
85 class InvalidationFrontendTest : public testing::Test { 85 class InvalidationServiceTest : public testing::Test {
86 protected: 86 protected:
87 // Note: The IDs defined below must be valid. Otherwise they won't make it 87 // Note: The IDs defined below must be valid. Otherwise they won't make it
88 // through the round-trip to ModelTypeInvalidationMap and back that the 88 // through the round-trip to ModelTypeInvalidationMap and back that the
89 // AndroidInvalidation test requires. 89 // AndroidInvalidation test requires.
90 InvalidationFrontendTest() 90 InvalidationServiceTest()
91 : id1(ipc::invalidation::ObjectSource::CHROME_SYNC, "BOOKMARK"), 91 : id1(ipc::invalidation::ObjectSource::CHROME_SYNC, "BOOKMARK"),
92 id2(ipc::invalidation::ObjectSource::CHROME_SYNC, "PREFERENCE"), 92 id2(ipc::invalidation::ObjectSource::CHROME_SYNC, "PREFERENCE"),
93 id3(ipc::invalidation::ObjectSource::CHROME_SYNC, "AUTOFILL"), 93 id3(ipc::invalidation::ObjectSource::CHROME_SYNC, "AUTOFILL"),
94 id4(ipc::invalidation::ObjectSource::CHROME_SYNC, "EXPERIMENTS") { 94 id4(ipc::invalidation::ObjectSource::CHROME_SYNC, "EXPERIMENTS") {
95 } 95 }
96 96
97 invalidation::InvalidationFrontend* 97 invalidation::InvalidationService*
98 CreateAndInitializeInvalidationFrontend() { 98 CreateAndInitializeInvalidationService() {
99 this->delegate_.CreateInvalidationFrontend(); 99 this->delegate_.CreateInvalidationService();
100 return this->delegate_.GetInvalidationFrontend(); 100 return this->delegate_.GetInvalidationService();
101 } 101 }
102 102
103 InvalidatorTestDelegate delegate_; 103 InvalidatorTestDelegate delegate_;
104 104
105 const invalidation::ObjectId id1; 105 const invalidation::ObjectId id1;
106 const invalidation::ObjectId id2; 106 const invalidation::ObjectId id2;
107 const invalidation::ObjectId id3; 107 const invalidation::ObjectId id3;
108 const invalidation::ObjectId id4; 108 const invalidation::ObjectId id4;
109 }; 109 };
110 110
111 TYPED_TEST_CASE_P(InvalidationFrontendTest); 111 TYPED_TEST_CASE_P(InvalidationServiceTest);
112 112
113 // Initialize the invalidator, register a handler, register some IDs for that 113 // Initialize the invalidator, register a handler, register some IDs for that
114 // handler, and then unregister the handler, dispatching invalidations in 114 // handler, and then unregister the handler, dispatching invalidations in
115 // between. The handler should only see invalidations when its registered and 115 // between. The handler should only see invalidations when its registered and
116 // its IDs are registered. 116 // its IDs are registered.
117 TYPED_TEST_P(InvalidationFrontendTest, Basic) { 117 TYPED_TEST_P(InvalidationServiceTest, Basic) {
118 invalidation::InvalidationFrontend* const invalidator = 118 invalidation::InvalidationService* const invalidator =
119 this->CreateAndInitializeInvalidationFrontend(); 119 this->CreateAndInitializeInvalidationService();
120 120
121 syncer::FakeInvalidationHandler handler; 121 syncer::FakeInvalidationHandler handler;
122 122
123 invalidator->RegisterInvalidationHandler(&handler); 123 invalidator->RegisterInvalidationHandler(&handler);
124 124
125 syncer::ObjectIdInvalidationMap states; 125 syncer::ObjectIdInvalidationMap states;
126 states[this->id1].payload = "1"; 126 states[this->id1].payload = "1";
127 states[this->id2].payload = "2"; 127 states[this->id2].payload = "2";
128 states[this->id3].payload = "3"; 128 states[this->id3].payload = "3";
129 129
(...skipping 29 matching lines...) Expand all
159 this->delegate_.TriggerOnIncomingInvalidation(states); 159 this->delegate_.TriggerOnIncomingInvalidation(states);
160 EXPECT_EQ(2, handler.GetInvalidationCount()); 160 EXPECT_EQ(2, handler.GetInvalidationCount());
161 EXPECT_THAT(expected_states, Eq(handler.GetLastInvalidationMap())); 161 EXPECT_THAT(expected_states, Eq(handler.GetLastInvalidationMap()));
162 162
163 this->delegate_.TriggerOnInvalidatorStateChange( 163 this->delegate_.TriggerOnInvalidatorStateChange(
164 syncer::TRANSIENT_INVALIDATION_ERROR); 164 syncer::TRANSIENT_INVALIDATION_ERROR);
165 EXPECT_EQ(syncer::TRANSIENT_INVALIDATION_ERROR, 165 EXPECT_EQ(syncer::TRANSIENT_INVALIDATION_ERROR,
166 handler.GetInvalidatorState()); 166 handler.GetInvalidatorState());
167 167
168 this->delegate_.TriggerOnInvalidatorStateChange( 168 this->delegate_.TriggerOnInvalidatorStateChange(
169 syncer::INVALIDATION_CREDENTIALS_REJECTED); 169 syncer::INVALIDATIONS_ENABLED);
170 EXPECT_EQ(syncer::INVALIDATION_CREDENTIALS_REJECTED, 170 EXPECT_EQ(syncer::INVALIDATIONS_ENABLED,
171 handler.GetInvalidatorState()); 171 handler.GetInvalidatorState());
172 172
173 invalidator->UnregisterInvalidationHandler(&handler); 173 invalidator->UnregisterInvalidationHandler(&handler);
174 174
175 // Should be ignored since |handler| isn't registered anymore. 175 // Should be ignored since |handler| isn't registered anymore.
176 this->delegate_.TriggerOnIncomingInvalidation(states); 176 this->delegate_.TriggerOnIncomingInvalidation(states);
177 EXPECT_EQ(2, handler.GetInvalidationCount()); 177 EXPECT_EQ(2, handler.GetInvalidationCount());
178 } 178 }
179 179
180 // Register handlers and some IDs for those handlers, register a handler with 180 // Register handlers and some IDs for those handlers, register a handler with
181 // no IDs, and register a handler with some IDs but unregister it. Then, 181 // no IDs, and register a handler with some IDs but unregister it. Then,
182 // dispatch some invalidations and invalidations. Handlers that are registered 182 // dispatch some invalidations and invalidations. Handlers that are registered
183 // should get invalidations, and the ones that have registered IDs should 183 // should get invalidations, and the ones that have registered IDs should
184 // receive invalidations for those IDs. 184 // receive invalidations for those IDs.
185 TYPED_TEST_P(InvalidationFrontendTest, MultipleHandlers) { 185 TYPED_TEST_P(InvalidationServiceTest, MultipleHandlers) {
186 invalidation::InvalidationFrontend* const invalidator = 186 invalidation::InvalidationService* const invalidator =
187 this->CreateAndInitializeInvalidationFrontend(); 187 this->CreateAndInitializeInvalidationService();
188 188
189 syncer::FakeInvalidationHandler handler1; 189 syncer::FakeInvalidationHandler handler1;
190 syncer::FakeInvalidationHandler handler2; 190 syncer::FakeInvalidationHandler handler2;
191 syncer::FakeInvalidationHandler handler3; 191 syncer::FakeInvalidationHandler handler3;
192 syncer::FakeInvalidationHandler handler4; 192 syncer::FakeInvalidationHandler handler4;
193 193
194 invalidator->RegisterInvalidationHandler(&handler1); 194 invalidator->RegisterInvalidationHandler(&handler1);
195 invalidator->RegisterInvalidationHandler(&handler2); 195 invalidator->RegisterInvalidationHandler(&handler2);
196 invalidator->RegisterInvalidationHandler(&handler3); 196 invalidator->RegisterInvalidationHandler(&handler3);
197 invalidator->RegisterInvalidationHandler(&handler4); 197 invalidator->RegisterInvalidationHandler(&handler4);
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
263 EXPECT_EQ(syncer::TRANSIENT_INVALIDATION_ERROR, 263 EXPECT_EQ(syncer::TRANSIENT_INVALIDATION_ERROR,
264 handler4.GetInvalidatorState()); 264 handler4.GetInvalidatorState());
265 265
266 invalidator->UnregisterInvalidationHandler(&handler3); 266 invalidator->UnregisterInvalidationHandler(&handler3);
267 invalidator->UnregisterInvalidationHandler(&handler2); 267 invalidator->UnregisterInvalidationHandler(&handler2);
268 invalidator->UnregisterInvalidationHandler(&handler1); 268 invalidator->UnregisterInvalidationHandler(&handler1);
269 } 269 }
270 270
271 // Make sure that passing an empty set to UpdateRegisteredInvalidationIds clears 271 // Make sure that passing an empty set to UpdateRegisteredInvalidationIds clears
272 // the corresponding entries for the handler. 272 // the corresponding entries for the handler.
273 TYPED_TEST_P(InvalidationFrontendTest, EmptySetUnregisters) { 273 TYPED_TEST_P(InvalidationServiceTest, EmptySetUnregisters) {
274 invalidation::InvalidationFrontend* const invalidator = 274 invalidation::InvalidationService* const invalidator =
275 this->CreateAndInitializeInvalidationFrontend(); 275 this->CreateAndInitializeInvalidationService();
276 276
277 syncer::FakeInvalidationHandler handler1; 277 syncer::FakeInvalidationHandler handler1;
278 278
279 // Control observer. 279 // Control observer.
280 syncer::FakeInvalidationHandler handler2; 280 syncer::FakeInvalidationHandler handler2;
281 281
282 invalidator->RegisterInvalidationHandler(&handler1); 282 invalidator->RegisterInvalidationHandler(&handler1);
283 invalidator->RegisterInvalidationHandler(&handler2); 283 invalidator->RegisterInvalidationHandler(&handler2);
284 284
285 { 285 {
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
322 EXPECT_EQ(syncer::TRANSIENT_INVALIDATION_ERROR, 322 EXPECT_EQ(syncer::TRANSIENT_INVALIDATION_ERROR,
323 handler2.GetInvalidatorState()); 323 handler2.GetInvalidatorState());
324 324
325 invalidator->UnregisterInvalidationHandler(&handler2); 325 invalidator->UnregisterInvalidationHandler(&handler2);
326 invalidator->UnregisterInvalidationHandler(&handler1); 326 invalidator->UnregisterInvalidationHandler(&handler1);
327 } 327 }
328 328
329 namespace internal { 329 namespace internal {
330 330
331 // A FakeInvalidationHandler that is "bound" to a specific 331 // A FakeInvalidationHandler that is "bound" to a specific
332 // InvalidationFrontend. This is for cross-referencing state information with 332 // InvalidationService. This is for cross-referencing state information with
333 // the bound InvalidationFrontend. 333 // the bound InvalidationService.
334 class BoundFakeInvalidationHandler : public syncer::FakeInvalidationHandler { 334 class BoundFakeInvalidationHandler : public syncer::FakeInvalidationHandler {
335 public: 335 public:
336 explicit BoundFakeInvalidationHandler( 336 explicit BoundFakeInvalidationHandler(
337 const invalidation::InvalidationFrontend& invalidator); 337 const invalidation::InvalidationService& invalidator);
338 virtual ~BoundFakeInvalidationHandler(); 338 virtual ~BoundFakeInvalidationHandler();
339 339
340 // Returns the last return value of GetInvalidatorState() on the 340 // Returns the last return value of GetInvalidatorState() on the
341 // bound invalidator from the last time the invalidator state 341 // bound invalidator from the last time the invalidator state
342 // changed. 342 // changed.
343 syncer::InvalidatorState GetLastRetrievedState() const; 343 syncer::InvalidatorState GetLastRetrievedState() const;
344 344
345 // InvalidationHandler implementation. 345 // InvalidationHandler implementation.
346 virtual void OnInvalidatorStateChange( 346 virtual void OnInvalidatorStateChange(
347 syncer::InvalidatorState state) OVERRIDE; 347 syncer::InvalidatorState state) OVERRIDE;
348 348
349 private: 349 private:
350 const invalidation::InvalidationFrontend& invalidator_; 350 const invalidation::InvalidationService& invalidator_;
351 syncer::InvalidatorState last_retrieved_state_; 351 syncer::InvalidatorState last_retrieved_state_;
352 352
353 DISALLOW_COPY_AND_ASSIGN(BoundFakeInvalidationHandler); 353 DISALLOW_COPY_AND_ASSIGN(BoundFakeInvalidationHandler);
354 }; 354 };
355 355
356 } // namespace internal 356 } // namespace internal
357 357
358 TYPED_TEST_P(InvalidationFrontendTest, GetInvalidatorStateAlwaysCurrent) { 358 TYPED_TEST_P(InvalidationServiceTest, GetInvalidatorStateAlwaysCurrent) {
359 invalidation::InvalidationFrontend* const invalidator = 359 invalidation::InvalidationService* const invalidator =
360 this->CreateAndInitializeInvalidationFrontend(); 360 this->CreateAndInitializeInvalidationService();
361 361
362 internal::BoundFakeInvalidationHandler handler(*invalidator); 362 internal::BoundFakeInvalidationHandler handler(*invalidator);
363 invalidator->RegisterInvalidationHandler(&handler); 363 invalidator->RegisterInvalidationHandler(&handler);
364 364
365 this->delegate_.TriggerOnInvalidatorStateChange( 365 this->delegate_.TriggerOnInvalidatorStateChange(
366 syncer::INVALIDATIONS_ENABLED); 366 syncer::INVALIDATIONS_ENABLED);
367 EXPECT_EQ(syncer::INVALIDATIONS_ENABLED, handler.GetInvalidatorState()); 367 EXPECT_EQ(syncer::INVALIDATIONS_ENABLED, handler.GetInvalidatorState());
368 EXPECT_EQ(syncer::INVALIDATIONS_ENABLED, handler.GetLastRetrievedState()); 368 EXPECT_EQ(syncer::INVALIDATIONS_ENABLED, handler.GetLastRetrievedState());
369 369
370 this->delegate_.TriggerOnInvalidatorStateChange( 370 this->delegate_.TriggerOnInvalidatorStateChange(
371 syncer::TRANSIENT_INVALIDATION_ERROR); 371 syncer::TRANSIENT_INVALIDATION_ERROR);
372 EXPECT_EQ(syncer::TRANSIENT_INVALIDATION_ERROR, 372 EXPECT_EQ(syncer::TRANSIENT_INVALIDATION_ERROR,
373 handler.GetInvalidatorState()); 373 handler.GetInvalidatorState());
374 EXPECT_EQ(syncer::TRANSIENT_INVALIDATION_ERROR, 374 EXPECT_EQ(syncer::TRANSIENT_INVALIDATION_ERROR,
375 handler.GetLastRetrievedState()); 375 handler.GetLastRetrievedState());
376 376
377 invalidator->UnregisterInvalidationHandler(&handler); 377 invalidator->UnregisterInvalidationHandler(&handler);
378 } 378 }
379 379
380 REGISTER_TYPED_TEST_CASE_P(InvalidationFrontendTest, 380 REGISTER_TYPED_TEST_CASE_P(InvalidationServiceTest,
381 Basic, MultipleHandlers, EmptySetUnregisters, 381 Basic, MultipleHandlers, EmptySetUnregisters,
382 GetInvalidatorStateAlwaysCurrent); 382 GetInvalidatorStateAlwaysCurrent);
383 383
384 #endif // CHROME_BROWSER_INVALIDATION_INVALIDATION_FRONTEND_TEST_TEMPLATE_H_ 384 #endif // CHROME_BROWSER_INVALIDATION_INVALIDATION_SERVICE_TEST_TEMPLATE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698