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 "google/cacheinvalidation/types.pb.h" | |
6 #include "sync/notifier/fake_sync_notifier_observer.h" | |
7 #include "sync/notifier/object_id_state_map_test_util.h" | |
8 #include "sync/notifier/sync_notifier_registrar.h" | |
9 #include "testing/gtest/include/gtest/gtest.h" | |
10 | |
11 namespace syncer { | |
12 | |
13 namespace { | |
14 | |
15 class SyncNotifierRegistrarTest : public testing::Test { | |
16 protected: | |
17 SyncNotifierRegistrarTest() | |
18 : kObjectId1(ipc::invalidation::ObjectSource::TEST, "a"), | |
19 kObjectId2(ipc::invalidation::ObjectSource::TEST, "b"), | |
20 kObjectId3(ipc::invalidation::ObjectSource::TEST, "c"), | |
21 kObjectId4(ipc::invalidation::ObjectSource::TEST, "d") { | |
22 } | |
23 | |
24 const invalidation::ObjectId kObjectId1; | |
25 const invalidation::ObjectId kObjectId2; | |
26 const invalidation::ObjectId kObjectId3; | |
27 const invalidation::ObjectId kObjectId4; | |
28 }; | |
29 | |
30 // Register a handler, register some IDs for that handler, and then unregister | |
31 // the handler, dispatching invalidations in between. The handler should only | |
32 // see invalidations when its registered and its IDs are registered. | |
33 TEST_F(SyncNotifierRegistrarTest, Basic) { | |
34 FakeSyncNotifierObserver handler; | |
35 | |
36 SyncNotifierRegistrar registrar; | |
37 | |
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(SyncNotifierRegistrarTest, MultipleHandlers) { | |
93 FakeSyncNotifierObserver handler1; | |
94 FakeSyncNotifierObserver handler2; | |
95 FakeSyncNotifierObserver handler3; | |
96 FakeSyncNotifierObserver handler4; | |
97 | |
98 SyncNotifierRegistrar 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(SyncNotifierRegistrarTest, MultipleRegistration) { | |
183 SyncNotifierRegistrar registrar; | |
184 | |
185 FakeSyncNotifierObserver handler1; | |
186 registrar.RegisterHandler(&handler1); | |
187 | |
188 FakeSyncNotifierObserver handler2; | |
189 registrar.RegisterHandler(&handler2); | |
190 | |
191 ObjectIdSet ids; | |
192 ids.insert(kObjectId1); | |
193 ids.insert(kObjectId2); | |
194 registrar.UpdateRegisteredIds(&handler1, ids); | |
195 | |
196 registrar.DetachFromThreadForTest(); | |
197 // We expect a death via CHECK(). We can't match against the CHECK() message | |
198 // though since they are removed in official builds. | |
199 EXPECT_DEATH({ registrar.UpdateRegisteredIds(&handler2, ids); }, ""); | |
200 } | |
201 | |
202 // Make sure that passing an empty set to UpdateRegisteredIds clears the | |
203 // corresponding entries for the handler. | |
204 TEST_F(SyncNotifierRegistrarTest, EmptySetUnregisters) { | |
205 FakeSyncNotifierObserver handler1; | |
206 | |
207 // Control observer. | |
208 FakeSyncNotifierObserver handler2; | |
209 | |
210 SyncNotifierRegistrar 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 | |
257 | |
258 } // namespace syncer | |
OLD | NEW |