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

Side by Side Diff: sync/notifier/invalidator_registrar_unittest.cc

Issue 10874096: [Sync] Rework Invalidator-related unit tests (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix compile Created 8 years, 3 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 | Annotate | Revision Log
OLDNEW
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 #include "base/compiler_specific.h"
6 #include "base/memory/scoped_ptr.h"
5 #include "google/cacheinvalidation/types.pb.h" 7 #include "google/cacheinvalidation/types.pb.h"
6 #include "sync/notifier/fake_invalidation_handler.h" 8 #include "sync/notifier/fake_invalidation_handler.h"
7 #include "sync/notifier/invalidator_registrar.h" 9 #include "sync/notifier/invalidator_registrar.h"
10 #include "sync/notifier/invalidator_test_template.h"
8 #include "sync/notifier/object_id_state_map_test_util.h" 11 #include "sync/notifier/object_id_state_map_test_util.h"
9 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
10 13
11 namespace syncer { 14 namespace syncer {
12 15
13 namespace { 16 namespace {
14 17
15 class InvalidatorRegistrarTest : public testing::Test { 18 // We test InvalidatorRegistrar by wrapping it in an Invalidator and
16 protected: 19 // running the usual Invalidator tests.
17 InvalidatorRegistrarTest() 20
18 : kObjectId1(ipc::invalidation::ObjectSource::TEST, "a"), 21 // Thin Invalidator wrapper around InvalidatorRegistrar.
19 kObjectId2(ipc::invalidation::ObjectSource::TEST, "b"), 22 class RegistrarInvalidator : public Invalidator {
20 kObjectId3(ipc::invalidation::ObjectSource::TEST, "c"), 23 public:
21 kObjectId4(ipc::invalidation::ObjectSource::TEST, "d") { 24 virtual ~RegistrarInvalidator() {}
25
26 InvalidatorRegistrar* GetRegistrar() {
27 return &registrar_;
22 } 28 }
23 29
24 const invalidation::ObjectId kObjectId1; 30 // Invalidator implementation.
25 const invalidation::ObjectId kObjectId2; 31 virtual void RegisterHandler(InvalidationHandler* handler) OVERRIDE {
26 const invalidation::ObjectId kObjectId3; 32 registrar_.RegisterHandler(handler);
27 const invalidation::ObjectId kObjectId4; 33 }
34
35 virtual void UpdateRegisteredIds(InvalidationHandler* handler,
36 const ObjectIdSet& ids) OVERRIDE {
37 registrar_.UpdateRegisteredIds(handler, ids);
38 }
39 virtual void UnregisterHandler(InvalidationHandler* handler) OVERRIDE {
Nicolas Zea 2012/08/29 23:41:05 newline before/after method?
akalin 2012/08/30 22:20:38 Done.
40 registrar_.UnregisterHandler(handler);
41 }
42 virtual void SetUniqueId(const std::string& unique_id) OVERRIDE {
43 // Do nothing.
44 }
45
46 virtual void SetStateDeprecated(const std::string& state) OVERRIDE {
47 // Do nothing.
48 }
49
50 virtual void UpdateCredentials(
51 const std::string& email, const std::string& token) OVERRIDE {
52 // Do nothing.
53 }
54
55 virtual void SendNotification(
56 const ObjectIdStateMap& id_state_map) OVERRIDE {
57 // Do nothing.
58 }
59
60 private:
61 InvalidatorRegistrar registrar_;
28 }; 62 };
29 63
30 // Register a handler, register some IDs for that handler, and then unregister 64 class RegistrarInvalidatorTestDelegate {
31 // the handler, dispatching invalidations in between. The handler should only 65 public:
32 // see invalidations when its registered and its IDs are registered. 66 RegistrarInvalidatorTestDelegate() {}
33 TEST_F(InvalidatorRegistrarTest, Basic) { 67
34 FakeInvalidationHandler handler; 68 ~RegistrarInvalidatorTestDelegate() {
69 DestroyInvalidator();
70 }
71
72 void CreateInvalidator(
73 const std::string& initial_state,
74 const base::WeakPtr<InvalidationStateTracker>&
75 invalidation_state_tracker) {
76 DCHECK(!invalidator_.get());
77 invalidator_.reset(new RegistrarInvalidator());
78 }
79
80 RegistrarInvalidator* GetInvalidator() {
81 return invalidator_.get();
82 }
83
84 void DestroyInvalidator() {
85 invalidator_.reset();
86 }
87
88 void WaitForInvalidator() {
89 // Do nothing.
90 }
91
92 void TriggerOnNotificationsEnabled() {
93 invalidator_->GetRegistrar()->EmitOnNotificationsEnabled();
94 }
95
96 void TriggerOnIncomingNotification(const ObjectIdStateMap& id_state_map,
97 IncomingNotificationSource source) {
98 invalidator_->GetRegistrar()->DispatchInvalidationsToHandlers(
99 id_state_map, source);
100 }
101
102 void TriggerOnNotificationsDisabled(NotificationsDisabledReason reason) {
103 invalidator_->GetRegistrar()->EmitOnNotificationsDisabled(reason);
104 }
105
106 static bool InvalidatorHandlesDeprecatedState() {
107 return false;
108 }
109
110 private:
111 scoped_ptr<RegistrarInvalidator> invalidator_;
112 };
113
114 INSTANTIATE_TYPED_TEST_CASE_P(
115 RegistrarInvalidatorTest, InvalidatorTest,
116 RegistrarInvalidatorTestDelegate);
117
118 class InvalidatorRegistrarTest : public testing::Test {};
119
120 // Multiple registrations by different handlers on the same object ID should
121 // cause a CHECK.
122 //
123 // Technically this can be part of InvalidatorTest, but we want to keep the
124 // number of death tests down.
125 TEST_F(InvalidatorRegistrarTest, MultipleRegistration) {
126 const invalidation::ObjectId id1(ipc::invalidation::ObjectSource::TEST, "a");
127 const invalidation::ObjectId id2(ipc::invalidation::ObjectSource::TEST, "a");
35 128
36 InvalidatorRegistrar registrar; 129 InvalidatorRegistrar registrar;
37 130
38 registrar.RegisterHandler(&handler);
39
40 ObjectIdStateMap states;
41 states[kObjectId1].payload = "1";
42 states[kObjectId2].payload = "2";
43 states[kObjectId3].payload = "3";
44
45 // Should be ignored since no IDs are registered to |handler|.
46 registrar.DispatchInvalidationsToHandlers(states, REMOTE_NOTIFICATION);
47 EXPECT_EQ(0, handler.GetNotificationCount());
48
49 ObjectIdSet ids;
50 ids.insert(kObjectId1);
51 ids.insert(kObjectId2);
52 registrar.UpdateRegisteredIds(&handler, ids);
53
54 ObjectIdStateMap expected_states;
55 expected_states[kObjectId1].payload = "1";
56 expected_states[kObjectId2].payload = "2";
57
58 registrar.DispatchInvalidationsToHandlers(states, REMOTE_NOTIFICATION);
59 EXPECT_EQ(1, handler.GetNotificationCount());
60 EXPECT_THAT(
61 expected_states,
62 Eq(handler.GetLastNotificationIdStateMap()));
63 EXPECT_EQ(REMOTE_NOTIFICATION, handler.GetLastNotificationSource());
64
65 ids.erase(kObjectId1);
66 ids.insert(kObjectId3);
67 registrar.UpdateRegisteredIds(&handler, ids);
68
69 expected_states.erase(kObjectId1);
70 expected_states[kObjectId3].payload = "3";
71
72 // Removed object IDs should not be notified, newly-added ones should.
73 registrar.DispatchInvalidationsToHandlers(states, REMOTE_NOTIFICATION);
74 EXPECT_EQ(2, handler.GetNotificationCount());
75 EXPECT_THAT(
76 expected_states,
77 Eq(handler.GetLastNotificationIdStateMap()));
78 EXPECT_EQ(REMOTE_NOTIFICATION, handler.GetLastNotificationSource());
79
80 registrar.UnregisterHandler(&handler);
81
82 // Should be ignored since |handler| isn't registered anymore.
83 registrar.DispatchInvalidationsToHandlers(states, REMOTE_NOTIFICATION);
84 EXPECT_EQ(2, handler.GetNotificationCount());
85 }
86
87 // Register handlers and some IDs for those handlers, register a handler with
88 // no IDs, and register a handler with some IDs but unregister it. Then,
89 // dispatch some notifications and invalidations. Handlers that are registered
90 // should get notifications, and the ones that have registered IDs should
91 // receive invalidations for those IDs.
92 TEST_F(InvalidatorRegistrarTest, MultipleHandlers) {
93 FakeInvalidationHandler handler1; 131 FakeInvalidationHandler handler1;
94 FakeInvalidationHandler handler2;
95 FakeInvalidationHandler handler3;
96 FakeInvalidationHandler handler4;
97
98 InvalidatorRegistrar registrar;
99
100 registrar.RegisterHandler(&handler1);
101 registrar.RegisterHandler(&handler2);
102 registrar.RegisterHandler(&handler3);
103 registrar.RegisterHandler(&handler4);
104
105 {
106 ObjectIdSet ids;
107 ids.insert(kObjectId1);
108 ids.insert(kObjectId2);
109 registrar.UpdateRegisteredIds(&handler1, ids);
110 }
111
112 {
113 ObjectIdSet ids;
114 ids.insert(kObjectId3);
115 registrar.UpdateRegisteredIds(&handler2, ids);
116 }
117
118 // Don't register any IDs for handler3.
119
120 {
121 ObjectIdSet ids;
122 ids.insert(kObjectId4);
123 registrar.UpdateRegisteredIds(&handler4, ids);
124 }
125
126 registrar.UnregisterHandler(&handler4);
127
128 registrar.EmitOnNotificationsEnabled();
129 EXPECT_EQ(NO_NOTIFICATION_ERROR,
130 handler1.GetNotificationsDisabledReason());
131 EXPECT_EQ(NO_NOTIFICATION_ERROR,
132 handler2.GetNotificationsDisabledReason());
133 EXPECT_EQ(NO_NOTIFICATION_ERROR,
134 handler3.GetNotificationsDisabledReason());
135 EXPECT_EQ(TRANSIENT_NOTIFICATION_ERROR,
136 handler4.GetNotificationsDisabledReason());
137
138 {
139 ObjectIdStateMap states;
140 states[kObjectId1].payload = "1";
141 states[kObjectId2].payload = "2";
142 states[kObjectId3].payload = "3";
143 states[kObjectId4].payload = "4";
144 registrar.DispatchInvalidationsToHandlers(states, REMOTE_NOTIFICATION);
145
146 ObjectIdStateMap expected_states;
147 expected_states[kObjectId1].payload = "1";
148 expected_states[kObjectId2].payload = "2";
149
150 EXPECT_EQ(1, handler1.GetNotificationCount());
151 EXPECT_THAT(
152 expected_states,
153 Eq(handler1.GetLastNotificationIdStateMap()));
154 EXPECT_EQ(REMOTE_NOTIFICATION, handler1.GetLastNotificationSource());
155
156 expected_states.clear();
157 expected_states[kObjectId3].payload = "3";
158
159 EXPECT_EQ(1, handler2.GetNotificationCount());
160 EXPECT_THAT(
161 expected_states,
162 Eq(handler2.GetLastNotificationIdStateMap()));
163 EXPECT_EQ(REMOTE_NOTIFICATION, handler2.GetLastNotificationSource());
164
165 EXPECT_EQ(0, handler3.GetNotificationCount());
166 EXPECT_EQ(0, handler4.GetNotificationCount());
167 }
168
169 registrar.EmitOnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR);
170 EXPECT_EQ(TRANSIENT_NOTIFICATION_ERROR,
171 handler1.GetNotificationsDisabledReason());
172 EXPECT_EQ(TRANSIENT_NOTIFICATION_ERROR,
173 handler2.GetNotificationsDisabledReason());
174 EXPECT_EQ(TRANSIENT_NOTIFICATION_ERROR,
175 handler3.GetNotificationsDisabledReason());
176 EXPECT_EQ(TRANSIENT_NOTIFICATION_ERROR,
177 handler4.GetNotificationsDisabledReason());
178 }
179
180 // Multiple registrations by different handlers on the same object ID should
181 // cause a CHECK.
182 TEST_F(InvalidatorRegistrarTest, MultipleRegistration) {
183 InvalidatorRegistrar registrar;
184
185 FakeInvalidationHandler handler1;
186 registrar.RegisterHandler(&handler1); 132 registrar.RegisterHandler(&handler1);
187 133
188 FakeInvalidationHandler handler2; 134 FakeInvalidationHandler handler2;
189 registrar.RegisterHandler(&handler2); 135 registrar.RegisterHandler(&handler2);
190 136
191 ObjectIdSet ids; 137 ObjectIdSet ids;
192 ids.insert(kObjectId1); 138 ids.insert(id1);
193 ids.insert(kObjectId2); 139 ids.insert(id2);
194 registrar.UpdateRegisteredIds(&handler1, ids); 140 registrar.UpdateRegisteredIds(&handler1, ids);
195 141
196 registrar.DetachFromThreadForTest(); 142 registrar.DetachFromThreadForTest();
197 // We expect a death via CHECK(). We can't match against the CHECK() message 143 // We expect a death via CHECK(). We can't match against the CHECK() message
198 // though since they are removed in official builds. 144 // though since they are removed in official builds.
199 EXPECT_DEATH({ registrar.UpdateRegisteredIds(&handler2, ids); }, ""); 145 EXPECT_DEATH({ registrar.UpdateRegisteredIds(&handler2, ids); }, "");
200 } 146 }
201 147
202 // Make sure that passing an empty set to UpdateRegisteredIds clears the
203 // corresponding entries for the handler.
204 TEST_F(InvalidatorRegistrarTest, EmptySetUnregisters) {
205 FakeInvalidationHandler handler1;
206
207 // Control observer.
208 FakeInvalidationHandler handler2;
209
210 InvalidatorRegistrar registrar;
211
212 registrar.RegisterHandler(&handler1);
213 registrar.RegisterHandler(&handler2);
214
215 {
216 ObjectIdSet ids;
217 ids.insert(kObjectId1);
218 ids.insert(kObjectId2);
219 registrar.UpdateRegisteredIds(&handler1, ids);
220 }
221
222 {
223 ObjectIdSet ids;
224 ids.insert(kObjectId3);
225 registrar.UpdateRegisteredIds(&handler2, ids);
226 }
227
228 // Unregister the IDs for the first observer. It should not receive any
229 // further invalidations.
230 registrar.UpdateRegisteredIds(&handler1, ObjectIdSet());
231
232 registrar.EmitOnNotificationsEnabled();
233 EXPECT_EQ(NO_NOTIFICATION_ERROR,
234 handler1.GetNotificationsDisabledReason());
235 EXPECT_EQ(NO_NOTIFICATION_ERROR,
236 handler2.GetNotificationsDisabledReason());
237
238 {
239 ObjectIdStateMap states;
240 states[kObjectId1].payload = "1";
241 states[kObjectId2].payload = "2";
242 states[kObjectId3].payload = "3";
243 registrar.DispatchInvalidationsToHandlers(states,
244 REMOTE_NOTIFICATION);
245 EXPECT_EQ(0, handler1.GetNotificationCount());
246 EXPECT_EQ(1, handler2.GetNotificationCount());
247 }
248
249 registrar.EmitOnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR);
250 EXPECT_EQ(TRANSIENT_NOTIFICATION_ERROR,
251 handler1.GetNotificationsDisabledReason());
252 EXPECT_EQ(TRANSIENT_NOTIFICATION_ERROR,
253 handler2.GetNotificationsDisabledReason());
254 }
255
256 } // namespace 148 } // namespace
257 149
258 } // namespace syncer 150 } // namespace syncer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698