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 #include "sync/notifier/p2p_invalidator.h" | 5 #include "sync/notifier/p2p_invalidator.h" |
6 | 6 |
7 #include <cstddef> | 7 #include <cstddef> |
8 | 8 |
9 #include "jingle/notifier/listener/fake_push_client.h" | 9 #include "jingle/notifier/listener/fake_push_client.h" |
10 #include "sync/internal_api/public/base/model_type.h" | 10 #include "sync/internal_api/public/base/model_type.h" |
11 #include "sync/notifier/fake_invalidation_handler.h" | 11 #include "sync/notifier/fake_invalidation_handler.h" |
12 #include "sync/notifier/invalidator_test_template.h" | 12 #include "sync/notifier/invalidator_test_template.h" |
13 #include "sync/notifier/object_id_invalidation_map_test_util.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
15 | 14 |
16 namespace syncer { | 15 namespace syncer { |
17 | 16 |
18 namespace { | 17 namespace { |
19 | 18 |
20 class P2PInvalidatorTestDelegate { | 19 class P2PInvalidatorTestDelegate { |
21 public: | 20 public: |
22 P2PInvalidatorTestDelegate() : fake_push_client_(NULL) {} | 21 P2PInvalidatorTestDelegate() : fake_push_client_(NULL) {} |
23 | 22 |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 delegate_.GetInvalidator()->RegisterHandler(&fake_handler_); | 90 delegate_.GetInvalidator()->RegisterHandler(&fake_handler_); |
92 } | 91 } |
93 | 92 |
94 virtual ~P2PInvalidatorTest() { | 93 virtual ~P2PInvalidatorTest() { |
95 delegate_.GetInvalidator()->UnregisterHandler(&fake_handler_); | 94 delegate_.GetInvalidator()->UnregisterHandler(&fake_handler_); |
96 } | 95 } |
97 | 96 |
98 ObjectIdInvalidationMap MakeInvalidationMap(ModelTypeSet types) { | 97 ObjectIdInvalidationMap MakeInvalidationMap(ModelTypeSet types) { |
99 ObjectIdInvalidationMap invalidations; | 98 ObjectIdInvalidationMap invalidations; |
100 ObjectIdSet ids = ModelTypeSetToObjectIdSet(types); | 99 ObjectIdSet ids = ModelTypeSetToObjectIdSet(types); |
101 return ObjectIdSetToInvalidationMap(ids, | 100 return ObjectIdInvalidationMap::InvalidateAll(ids); |
102 Invalidation::kUnknownVersion, | |
103 std::string()); | |
104 } | 101 } |
105 | 102 |
106 // Simulate receiving all the notifications we sent out since last | 103 // Simulate receiving all the notifications we sent out since last |
107 // time this was called. | 104 // time this was called. |
108 void ReflectSentNotifications() { | 105 void ReflectSentNotifications() { |
109 const std::vector<notifier::Notification>& sent_notifications = | 106 const std::vector<notifier::Notification>& sent_notifications = |
110 delegate_.GetPushClient()->sent_notifications(); | 107 delegate_.GetPushClient()->sent_notifications(); |
111 for(size_t i = next_sent_notification_to_reflect_; | 108 for(size_t i = next_sent_notification_to_reflect_; |
112 i < sent_notifications.size(); ++i) { | 109 i < sent_notifications.size(); ++i) { |
113 delegate_.GetInvalidator()->OnIncomingNotification(sent_notifications[i]); | 110 delegate_.GetInvalidator()->OnIncomingNotification(sent_notifications[i]); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 } | 156 } |
160 } | 157 } |
161 | 158 |
162 // Make sure the P2PNotificationData <-> string conversions work for a | 159 // Make sure the P2PNotificationData <-> string conversions work for a |
163 // default-constructed P2PNotificationData. | 160 // default-constructed P2PNotificationData. |
164 TEST_F(P2PInvalidatorTest, P2PNotificationDataDefault) { | 161 TEST_F(P2PInvalidatorTest, P2PNotificationDataDefault) { |
165 const P2PNotificationData notification_data; | 162 const P2PNotificationData notification_data; |
166 EXPECT_TRUE(notification_data.IsTargeted(std::string())); | 163 EXPECT_TRUE(notification_data.IsTargeted(std::string())); |
167 EXPECT_FALSE(notification_data.IsTargeted("other1")); | 164 EXPECT_FALSE(notification_data.IsTargeted("other1")); |
168 EXPECT_FALSE(notification_data.IsTargeted("other2")); | 165 EXPECT_FALSE(notification_data.IsTargeted("other2")); |
169 EXPECT_TRUE(notification_data.GetIdInvalidationMap().empty()); | 166 EXPECT_TRUE(notification_data.GetIdInvalidationMap().Empty()); |
170 const std::string& notification_data_str = notification_data.ToString(); | 167 const std::string& notification_data_str = notification_data.ToString(); |
171 EXPECT_EQ( | 168 EXPECT_EQ( |
172 "{\"idInvalidationMap\":[],\"notificationType\":\"notifySelf\"," | 169 "{\"invalidations\":[],\"notificationType\":\"notifySelf\"," |
173 "\"senderId\":\"\"}", notification_data_str); | 170 "\"senderId\":\"\"}", notification_data_str); |
174 | 171 |
175 P2PNotificationData notification_data_parsed; | 172 P2PNotificationData notification_data_parsed; |
176 EXPECT_TRUE(notification_data_parsed.ResetFromString(notification_data_str)); | 173 EXPECT_TRUE(notification_data_parsed.ResetFromString(notification_data_str)); |
177 EXPECT_TRUE(notification_data.Equals(notification_data_parsed)); | 174 EXPECT_TRUE(notification_data.Equals(notification_data_parsed)); |
178 } | 175 } |
179 | 176 |
180 // Make sure the P2PNotificationData <-> string conversions work for a | 177 // Make sure the P2PNotificationData <-> string conversions work for a |
181 // non-default-constructed P2PNotificationData. | 178 // non-default-constructed P2PNotificationData. |
182 TEST_F(P2PInvalidatorTest, P2PNotificationDataNonDefault) { | 179 TEST_F(P2PInvalidatorTest, P2PNotificationDataNonDefault) { |
183 const ObjectIdInvalidationMap& invalidation_map = | 180 ObjectIdInvalidationMap invalidation_map = |
184 ObjectIdSetToInvalidationMap( | 181 MakeInvalidationMap(ModelTypeSet(BOOKMARKS, THEMES)); |
185 ModelTypeSetToObjectIdSet(ModelTypeSet(BOOKMARKS, THEMES)), | 182 const P2PNotificationData notification_data("sender", |
186 Invalidation::kUnknownVersion, | 183 NOTIFY_ALL, |
187 std::string()); | 184 invalidation_map); |
188 const P2PNotificationData notification_data( | |
189 "sender", NOTIFY_ALL, invalidation_map); | |
190 EXPECT_TRUE(notification_data.IsTargeted("sender")); | 185 EXPECT_TRUE(notification_data.IsTargeted("sender")); |
191 EXPECT_TRUE(notification_data.IsTargeted("other1")); | 186 EXPECT_TRUE(notification_data.IsTargeted("other1")); |
192 EXPECT_TRUE(notification_data.IsTargeted("other2")); | 187 EXPECT_TRUE(notification_data.IsTargeted("other2")); |
193 EXPECT_THAT(invalidation_map, | 188 EXPECT_EQ(invalidation_map, notification_data.GetIdInvalidationMap()); |
194 Eq(notification_data.GetIdInvalidationMap())); | |
195 const std::string& notification_data_str = notification_data.ToString(); | 189 const std::string& notification_data_str = notification_data.ToString(); |
196 EXPECT_EQ( | 190 EXPECT_EQ( |
197 "{\"idInvalidationMap\":[" | 191 "{\"invalidations\":[" |
198 "{\"objectId\":{\"name\":\"BOOKMARK\",\"source\":1004}," | 192 "{\"isUnknownVersion\":true," |
199 "\"state\":{\"ackHandle\":{\"state\":\"\",\"timestamp\":\"0\"}," | 193 "\"objectId\":{\"name\":\"BOOKMARK\",\"source\":1004}}," |
200 "\"payload\":\"\",\"version\":\"-1\"}}," | 194 "{\"isUnknownVersion\":true," |
201 "{\"objectId\":{\"name\":\"THEME\",\"source\":1004}," | 195 "\"objectId\":{\"name\":\"THEME\",\"source\":1004}}" |
202 "\"state\":{\"ackHandle\":{\"state\":\"\",\"timestamp\":\"0\"}," | |
203 "\"payload\":\"\",\"version\":\"-1\"}}" | |
204 "],\"notificationType\":\"notifyAll\"," | 196 "],\"notificationType\":\"notifyAll\"," |
205 "\"senderId\":\"sender\"}", notification_data_str); | 197 "\"senderId\":\"sender\"}", notification_data_str); |
206 | 198 |
207 P2PNotificationData notification_data_parsed; | 199 P2PNotificationData notification_data_parsed; |
208 EXPECT_TRUE(notification_data_parsed.ResetFromString(notification_data_str)); | 200 EXPECT_TRUE(notification_data_parsed.ResetFromString(notification_data_str)); |
209 EXPECT_TRUE(notification_data.Equals(notification_data_parsed)); | 201 EXPECT_TRUE(notification_data.Equals(notification_data_parsed)); |
210 } | 202 } |
211 | 203 |
212 // Set up the P2PInvalidator, simulate a successful connection, and send | 204 // Set up the P2PInvalidator, simulate a successful connection, and send |
213 // a notification with the default target (NOTIFY_OTHERS). The | 205 // a notification with the default target (NOTIFY_OTHERS). The |
(...skipping 27 matching lines...) Expand all Loading... |
241 EXPECT_EQ(INVALIDATIONS_ENABLED, fake_handler_.GetInvalidatorState()); | 233 EXPECT_EQ(INVALIDATIONS_ENABLED, fake_handler_.GetInvalidatorState()); |
242 | 234 |
243 ReflectSentNotifications(); | 235 ReflectSentNotifications(); |
244 EXPECT_EQ(1, fake_handler_.GetInvalidationCount()); | 236 EXPECT_EQ(1, fake_handler_.GetInvalidationCount()); |
245 EXPECT_THAT( | 237 EXPECT_THAT( |
246 MakeInvalidationMap(enabled_types), | 238 MakeInvalidationMap(enabled_types), |
247 Eq(fake_handler_.GetLastInvalidationMap())); | 239 Eq(fake_handler_.GetLastInvalidationMap())); |
248 | 240 |
249 // Sent with target NOTIFY_OTHERS so should not be propagated to | 241 // Sent with target NOTIFY_OTHERS so should not be propagated to |
250 // |fake_handler_|. | 242 // |fake_handler_|. |
251 { | 243 invalidator->SendInvalidation( |
252 const ObjectIdInvalidationMap& invalidation_map = | 244 ModelTypeSetToObjectIdSet(ModelTypeSet(THEMES, APPS))); |
253 ObjectIdSetToInvalidationMap( | |
254 ModelTypeSetToObjectIdSet(ModelTypeSet(THEMES, APPS)), | |
255 Invalidation::kUnknownVersion, | |
256 std::string()); | |
257 invalidator->SendInvalidation(invalidation_map); | |
258 } | |
259 | 245 |
260 ReflectSentNotifications(); | 246 ReflectSentNotifications(); |
261 EXPECT_EQ(1, fake_handler_.GetInvalidationCount()); | 247 EXPECT_EQ(1, fake_handler_.GetInvalidationCount()); |
262 } | 248 } |
263 | 249 |
264 // Set up the P2PInvalidator and send out notifications with various | 250 // Set up the P2PInvalidator and send out notifications with various |
265 // target settings. The notifications received by the observer should | 251 // target settings. The notifications received by the observer should |
266 // be consistent with the target settings. | 252 // be consistent with the target settings. |
267 TEST_F(P2PInvalidatorTest, SendNotificationData) { | 253 TEST_F(P2PInvalidatorTest, SendNotificationData) { |
268 const ModelTypeSet enabled_types(BOOKMARKS, PREFERENCES, THEMES); | 254 const ModelTypeSet enabled_types(BOOKMARKS, PREFERENCES, THEMES); |
269 const ModelTypeSet changed_types(THEMES, APPS); | 255 const ModelTypeSet changed_types(THEMES, APPS); |
270 const ModelTypeSet expected_types(THEMES); | 256 const ModelTypeSet expected_types(THEMES); |
271 | 257 |
272 const ObjectIdInvalidationMap& invalidation_map = | 258 const ObjectIdInvalidationMap& invalidation_map = |
273 ObjectIdSetToInvalidationMap(ModelTypeSetToObjectIdSet(changed_types), | 259 MakeInvalidationMap(changed_types); |
274 Invalidation::kUnknownVersion, | |
275 std::string()); | |
276 | 260 |
277 P2PInvalidator* const invalidator = delegate_.GetInvalidator(); | 261 P2PInvalidator* const invalidator = delegate_.GetInvalidator(); |
278 notifier::FakePushClient* const push_client = delegate_.GetPushClient(); | 262 notifier::FakePushClient* const push_client = delegate_.GetPushClient(); |
279 | 263 |
280 invalidator->UpdateRegisteredIds(&fake_handler_, | 264 invalidator->UpdateRegisteredIds(&fake_handler_, |
281 ModelTypeSetToObjectIdSet(enabled_types)); | 265 ModelTypeSetToObjectIdSet(enabled_types)); |
282 | 266 |
283 invalidator->UpdateCredentials("foo@bar.com", "fake_token"); | 267 invalidator->UpdateCredentials("foo@bar.com", "fake_token"); |
284 | 268 |
285 ReflectSentNotifications(); | 269 ReflectSentNotifications(); |
286 push_client->EnableNotifications(); | 270 push_client->EnableNotifications(); |
287 EXPECT_EQ(INVALIDATIONS_ENABLED, fake_handler_.GetInvalidatorState()); | 271 EXPECT_EQ(INVALIDATIONS_ENABLED, fake_handler_.GetInvalidatorState()); |
288 | 272 |
289 ReflectSentNotifications(); | 273 ReflectSentNotifications(); |
290 EXPECT_EQ(1, fake_handler_.GetInvalidationCount()); | 274 EXPECT_EQ(1, fake_handler_.GetInvalidationCount()); |
291 EXPECT_THAT(MakeInvalidationMap(enabled_types), | 275 EXPECT_EQ(ModelTypeSetToObjectIdSet(enabled_types), |
292 Eq(fake_handler_.GetLastInvalidationMap())); | 276 fake_handler_.GetLastInvalidationMap().GetObjectIds()); |
293 | 277 |
294 // Should be dropped. | 278 // Should be dropped. |
295 invalidator->SendNotificationDataForTest(P2PNotificationData()); | 279 invalidator->SendNotificationDataForTest(P2PNotificationData()); |
296 ReflectSentNotifications(); | 280 ReflectSentNotifications(); |
297 EXPECT_EQ(1, fake_handler_.GetInvalidationCount()); | 281 EXPECT_EQ(1, fake_handler_.GetInvalidationCount()); |
298 | 282 |
299 const ObjectIdInvalidationMap& expected_ids = | 283 const ObjectIdSet& expected_ids = ModelTypeSetToObjectIdSet(expected_types); |
300 MakeInvalidationMap(expected_types); | |
301 | 284 |
302 // Should be propagated. | 285 // Should be propagated. |
303 invalidator->SendNotificationDataForTest( | 286 invalidator->SendNotificationDataForTest( |
304 P2PNotificationData("sender", NOTIFY_SELF, invalidation_map)); | 287 P2PNotificationData("sender", NOTIFY_SELF, invalidation_map)); |
305 ReflectSentNotifications(); | 288 ReflectSentNotifications(); |
306 EXPECT_EQ(2, fake_handler_.GetInvalidationCount()); | 289 EXPECT_EQ(2, fake_handler_.GetInvalidationCount()); |
307 EXPECT_THAT(expected_ids, Eq(fake_handler_.GetLastInvalidationMap())); | 290 EXPECT_EQ(expected_ids, |
| 291 fake_handler_.GetLastInvalidationMap().GetObjectIds()); |
308 | 292 |
309 // Should be dropped. | 293 // Should be dropped. |
310 invalidator->SendNotificationDataForTest( | 294 invalidator->SendNotificationDataForTest( |
311 P2PNotificationData("sender2", NOTIFY_SELF, invalidation_map)); | 295 P2PNotificationData("sender2", NOTIFY_SELF, invalidation_map)); |
312 ReflectSentNotifications(); | 296 ReflectSentNotifications(); |
313 EXPECT_EQ(2, fake_handler_.GetInvalidationCount()); | 297 EXPECT_EQ(2, fake_handler_.GetInvalidationCount()); |
314 | 298 |
315 // Should be dropped. | 299 // Should be dropped. |
316 invalidator->SendNotificationDataForTest( | 300 invalidator->SendNotificationDataForTest( |
317 P2PNotificationData("sender", NOTIFY_SELF, ObjectIdInvalidationMap())); | 301 P2PNotificationData("sender", NOTIFY_SELF, ObjectIdInvalidationMap())); |
318 ReflectSentNotifications(); | 302 ReflectSentNotifications(); |
319 EXPECT_EQ(2, fake_handler_.GetInvalidationCount()); | 303 EXPECT_EQ(2, fake_handler_.GetInvalidationCount()); |
320 | 304 |
321 // Should be dropped. | 305 // Should be dropped. |
322 invalidator->SendNotificationDataForTest( | 306 invalidator->SendNotificationDataForTest( |
323 P2PNotificationData("sender", NOTIFY_OTHERS, invalidation_map)); | 307 P2PNotificationData("sender", NOTIFY_OTHERS, invalidation_map)); |
324 ReflectSentNotifications(); | 308 ReflectSentNotifications(); |
325 EXPECT_EQ(2, fake_handler_.GetInvalidationCount()); | 309 EXPECT_EQ(2, fake_handler_.GetInvalidationCount()); |
326 | 310 |
327 // Should be propagated. | 311 // Should be propagated. |
328 invalidator->SendNotificationDataForTest( | 312 invalidator->SendNotificationDataForTest( |
329 P2PNotificationData("sender2", NOTIFY_OTHERS, invalidation_map)); | 313 P2PNotificationData("sender2", NOTIFY_OTHERS, invalidation_map)); |
330 ReflectSentNotifications(); | 314 ReflectSentNotifications(); |
331 EXPECT_EQ(3, fake_handler_.GetInvalidationCount()); | 315 EXPECT_EQ(3, fake_handler_.GetInvalidationCount()); |
332 EXPECT_THAT(expected_ids, Eq(fake_handler_.GetLastInvalidationMap())); | 316 EXPECT_EQ(expected_ids, |
| 317 fake_handler_.GetLastInvalidationMap().GetObjectIds()); |
333 | 318 |
334 // Should be dropped. | 319 // Should be dropped. |
335 invalidator->SendNotificationDataForTest( | 320 invalidator->SendNotificationDataForTest( |
336 P2PNotificationData("sender2", NOTIFY_OTHERS, ObjectIdInvalidationMap())); | 321 P2PNotificationData("sender2", NOTIFY_OTHERS, ObjectIdInvalidationMap())); |
337 ReflectSentNotifications(); | 322 ReflectSentNotifications(); |
338 EXPECT_EQ(3, fake_handler_.GetInvalidationCount()); | 323 EXPECT_EQ(3, fake_handler_.GetInvalidationCount()); |
339 | 324 |
340 // Should be propagated. | 325 // Should be propagated. |
341 invalidator->SendNotificationDataForTest( | 326 invalidator->SendNotificationDataForTest( |
342 P2PNotificationData("sender", NOTIFY_ALL, invalidation_map)); | 327 P2PNotificationData("sender", NOTIFY_ALL, invalidation_map)); |
343 ReflectSentNotifications(); | 328 ReflectSentNotifications(); |
344 EXPECT_EQ(4, fake_handler_.GetInvalidationCount()); | 329 EXPECT_EQ(4, fake_handler_.GetInvalidationCount()); |
345 EXPECT_THAT(expected_ids, Eq(fake_handler_.GetLastInvalidationMap())); | 330 EXPECT_EQ(expected_ids, |
| 331 fake_handler_.GetLastInvalidationMap().GetObjectIds()); |
346 | 332 |
347 // Should be propagated. | 333 // Should be propagated. |
348 invalidator->SendNotificationDataForTest( | 334 invalidator->SendNotificationDataForTest( |
349 P2PNotificationData("sender2", NOTIFY_ALL, invalidation_map)); | 335 P2PNotificationData("sender2", NOTIFY_ALL, invalidation_map)); |
350 ReflectSentNotifications(); | 336 ReflectSentNotifications(); |
351 EXPECT_EQ(5, fake_handler_.GetInvalidationCount()); | 337 EXPECT_EQ(5, fake_handler_.GetInvalidationCount()); |
352 EXPECT_THAT(expected_ids, Eq(fake_handler_.GetLastInvalidationMap())); | 338 EXPECT_EQ(expected_ids, |
| 339 fake_handler_.GetLastInvalidationMap().GetObjectIds()); |
353 | 340 |
354 // Should be dropped. | 341 // Should be dropped. |
355 invalidator->SendNotificationDataForTest( | 342 invalidator->SendNotificationDataForTest( |
356 P2PNotificationData("sender2", NOTIFY_ALL, ObjectIdInvalidationMap())); | 343 P2PNotificationData("sender2", NOTIFY_ALL, ObjectIdInvalidationMap())); |
357 ReflectSentNotifications(); | 344 ReflectSentNotifications(); |
358 EXPECT_EQ(5, fake_handler_.GetInvalidationCount()); | 345 EXPECT_EQ(5, fake_handler_.GetInvalidationCount()); |
359 } | 346 } |
360 | 347 |
361 INSTANTIATE_TYPED_TEST_CASE_P( | 348 INSTANTIATE_TYPED_TEST_CASE_P( |
362 P2PInvalidatorTest, InvalidatorTest, | 349 P2PInvalidatorTest, InvalidatorTest, |
363 P2PInvalidatorTestDelegate); | 350 P2PInvalidatorTestDelegate); |
364 | 351 |
365 } // namespace | 352 } // namespace |
366 | 353 |
367 } // namespace syncer | 354 } // namespace syncer |
OLD | NEW |