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/mock_sync_notifier_observer.h" | |
7 #include "sync/notifier/sync_notifier_registrar.h" | |
8 #include "testing/gtest/include/gtest/gtest.h" | |
9 | |
10 namespace syncer { | |
11 | |
12 namespace { | |
13 | |
14 using testing::InSequence; | |
15 using testing::Mock; | |
16 using testing::StrictMock; | |
17 | |
18 class SyncNotifierRegistrarTest : public testing::Test { | |
19 protected: | |
20 SyncNotifierRegistrarTest() | |
21 : kObjectId1(ipc::invalidation::ObjectSource::TEST, "a"), | |
22 kObjectId2(ipc::invalidation::ObjectSource::TEST, "b"), | |
23 kObjectId3(ipc::invalidation::ObjectSource::TEST, "c"), | |
24 kObjectId4(ipc::invalidation::ObjectSource::TEST, "d") { | |
25 } | |
26 | |
27 const invalidation::ObjectId kObjectId1; | |
28 const invalidation::ObjectId kObjectId2; | |
29 const invalidation::ObjectId kObjectId3; | |
30 const invalidation::ObjectId kObjectId4; | |
31 }; | |
32 | |
33 // Register a handler, register some IDs for that handler, and then unregister | |
34 // the handler, dispatching invalidations in between. The handler should only | |
35 // see invalidations when its registered and its IDs are registered. | |
36 TEST_F(SyncNotifierRegistrarTest, Basic) { | |
37 StrictMock<MockSyncNotifierObserver> handler; | |
38 | |
39 SyncNotifierRegistrar registrar; | |
40 | |
41 registrar.RegisterHandler(&handler); | |
42 | |
43 ObjectIdPayloadMap payloads; | |
44 payloads[kObjectId1] = "1"; | |
45 payloads[kObjectId2] = "2"; | |
46 payloads[kObjectId3] = "3"; | |
47 | |
48 // Should be ignored since no IDs are registered to |handler|. | |
49 registrar.DispatchInvalidationsToHandlers(payloads, REMOTE_NOTIFICATION); | |
50 | |
51 Mock::VerifyAndClearExpectations(&handler); | |
52 | |
53 ObjectIdSet ids; | |
54 ids.insert(kObjectId1); | |
55 ids.insert(kObjectId2); | |
56 registrar.UpdateRegisteredIds(&handler, ids); | |
57 | |
58 { | |
59 ObjectIdPayloadMap expected_payloads; | |
60 expected_payloads[kObjectId1] = "1"; | |
61 expected_payloads[kObjectId2] = "2"; | |
62 EXPECT_CALL(handler, OnIncomingNotification(expected_payloads, | |
63 REMOTE_NOTIFICATION)); | |
64 } | |
65 | |
66 registrar.DispatchInvalidationsToHandlers(payloads, REMOTE_NOTIFICATION); | |
67 | |
68 Mock::VerifyAndClearExpectations(&handler); | |
69 | |
70 ids.erase(kObjectId1); | |
71 ids.insert(kObjectId3); | |
72 registrar.UpdateRegisteredIds(&handler, ids); | |
73 | |
74 { | |
75 ObjectIdPayloadMap expected_payloads; | |
76 expected_payloads[kObjectId2] = "2"; | |
77 expected_payloads[kObjectId3] = "3"; | |
78 EXPECT_CALL(handler, OnIncomingNotification(expected_payloads, | |
79 REMOTE_NOTIFICATION)); | |
80 } | |
81 | |
82 // Removed object IDs should not be notified, newly-added ones should. | |
83 registrar.DispatchInvalidationsToHandlers(payloads, REMOTE_NOTIFICATION); | |
84 | |
85 Mock::VerifyAndClearExpectations(&handler); | |
86 | |
87 registrar.UnregisterHandler(&handler); | |
88 | |
89 // Should be ignored since |handler| isn't registered anymore. | |
90 registrar.DispatchInvalidationsToHandlers(payloads, REMOTE_NOTIFICATION); | |
91 } | |
92 | |
93 // Register handlers and some IDs for those handlers, register a handler with | |
94 // no IDs, and register a handler with some IDs but unregister it. Then, | |
95 // dispatch some notifications and invalidations. Handlers that are registered | |
96 // should get notifications, and the ones that have registered IDs should | |
97 // receive invalidations for those IDs. | |
98 TEST_F(SyncNotifierRegistrarTest, MultipleHandlers) { | |
99 StrictMock<MockSyncNotifierObserver> handler1; | |
100 EXPECT_CALL(handler1, OnNotificationsEnabled()); | |
101 { | |
102 ObjectIdPayloadMap expected_payloads; | |
103 expected_payloads[kObjectId1] = "1"; | |
104 expected_payloads[kObjectId2] = "2"; | |
105 EXPECT_CALL(handler1, OnIncomingNotification(expected_payloads, | |
106 REMOTE_NOTIFICATION)); | |
107 } | |
108 EXPECT_CALL(handler1, | |
109 OnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR)); | |
110 | |
111 StrictMock<MockSyncNotifierObserver> handler2; | |
112 EXPECT_CALL(handler2, OnNotificationsEnabled()); | |
113 { | |
114 ObjectIdPayloadMap expected_payloads; | |
115 expected_payloads[kObjectId3] = "3"; | |
116 EXPECT_CALL(handler2, OnIncomingNotification(expected_payloads, | |
117 REMOTE_NOTIFICATION)); | |
118 } | |
119 EXPECT_CALL(handler2, | |
120 OnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR)); | |
121 | |
122 StrictMock<MockSyncNotifierObserver> handler3; | |
123 EXPECT_CALL(handler3, OnNotificationsEnabled()); | |
124 EXPECT_CALL(handler3, | |
125 OnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR)); | |
126 | |
127 StrictMock<MockSyncNotifierObserver> handler4; | |
128 | |
129 SyncNotifierRegistrar registrar; | |
130 | |
131 registrar.RegisterHandler(&handler1); | |
132 registrar.RegisterHandler(&handler2); | |
133 registrar.RegisterHandler(&handler3); | |
134 registrar.RegisterHandler(&handler4); | |
135 | |
136 { | |
137 ObjectIdSet ids; | |
138 ids.insert(kObjectId1); | |
139 ids.insert(kObjectId2); | |
140 registrar.UpdateRegisteredIds(&handler1, ids); | |
141 } | |
142 | |
143 { | |
144 ObjectIdSet ids; | |
145 ids.insert(kObjectId3); | |
146 registrar.UpdateRegisteredIds(&handler2, ids); | |
147 } | |
148 | |
149 // Don't register any IDs for handler3. | |
150 | |
151 { | |
152 ObjectIdSet ids; | |
153 ids.insert(kObjectId4); | |
154 registrar.UpdateRegisteredIds(&handler4, ids); | |
155 } | |
156 | |
157 registrar.UnregisterHandler(&handler4); | |
158 | |
159 registrar.EmitOnNotificationsEnabled(); | |
160 { | |
161 ObjectIdPayloadMap payloads; | |
162 payloads[kObjectId1] = "1"; | |
163 payloads[kObjectId2] = "2"; | |
164 payloads[kObjectId3] = "3"; | |
165 payloads[kObjectId4] = "4"; | |
166 registrar.DispatchInvalidationsToHandlers(payloads, REMOTE_NOTIFICATION); | |
167 } | |
168 registrar.EmitOnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR); | |
169 } | |
170 | |
171 // Multiple registrations by different handlers on the same object ID should | |
172 // cause a CHECK. | |
173 TEST_F(SyncNotifierRegistrarTest, MultipleRegistration) { | |
174 SyncNotifierRegistrar registrar; | |
175 | |
176 StrictMock<MockSyncNotifierObserver> handler1; | |
177 registrar.RegisterHandler(&handler1); | |
178 | |
179 MockSyncNotifierObserver handler2; | |
180 registrar.RegisterHandler(&handler2); | |
181 | |
182 ObjectIdSet ids; | |
183 ids.insert(kObjectId1); | |
184 ids.insert(kObjectId2); | |
185 registrar.UpdateRegisteredIds(&handler1, ids); | |
186 | |
187 registrar.DetachFromThreadForTest(); | |
188 EXPECT_DEATH({ registrar.UpdateRegisteredIds(&handler2, ids); }, | |
189 "Duplicate registration: .*"); | |
190 } | |
191 | |
192 // Make sure that passing an empty set to UpdateRegisteredIds clears the | |
193 // corresponding entries for the handler. | |
194 TEST_F(SyncNotifierRegistrarTest, EmptySetUnregisters) { | |
195 StrictMock<MockSyncNotifierObserver> handler1; | |
196 EXPECT_CALL(handler1, OnNotificationsEnabled()); | |
197 EXPECT_CALL(handler1, | |
198 OnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR)); | |
199 | |
200 // Control observer. | |
201 StrictMock<MockSyncNotifierObserver> handler2; | |
202 EXPECT_CALL(handler2, OnNotificationsEnabled()); | |
203 { | |
204 ObjectIdPayloadMap expected_payloads; | |
205 expected_payloads[kObjectId3] = "3"; | |
206 EXPECT_CALL(handler2, OnIncomingNotification(expected_payloads, | |
207 REMOTE_NOTIFICATION)); | |
208 } | |
209 EXPECT_CALL(handler2, | |
210 OnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR)); | |
211 | |
212 SyncNotifierRegistrar registrar; | |
213 | |
214 registrar.RegisterHandler(&handler1); | |
215 registrar.RegisterHandler(&handler2); | |
216 | |
217 { | |
218 ObjectIdSet ids; | |
219 ids.insert(kObjectId1); | |
220 ids.insert(kObjectId2); | |
221 registrar.UpdateRegisteredIds(&handler1, ids); | |
222 } | |
223 | |
224 { | |
225 ObjectIdSet ids; | |
226 ids.insert(kObjectId3); | |
227 registrar.UpdateRegisteredIds(&handler2, ids); | |
228 } | |
229 | |
230 // Unregister the IDs for the first observer. It should not receive any | |
231 // further invalidations. | |
232 registrar.UpdateRegisteredIds(&handler1, ObjectIdSet()); | |
233 | |
234 registrar.EmitOnNotificationsEnabled(); | |
235 { | |
236 ObjectIdPayloadMap payloads; | |
237 payloads[kObjectId1] = "1"; | |
238 payloads[kObjectId2] = "2"; | |
239 payloads[kObjectId3] = "3"; | |
240 registrar.DispatchInvalidationsToHandlers(payloads, | |
241 REMOTE_NOTIFICATION); | |
242 } | |
243 registrar.EmitOnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR); | |
244 } | |
245 | |
246 } // namespace | |
247 | |
248 } // namespace syncer | |
OLD | NEW |