| 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" |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 if (state == INVALIDATIONS_ENABLED) { | 62 if (state == INVALIDATIONS_ENABLED) { |
| 63 fake_push_client_->EnableNotifications(); | 63 fake_push_client_->EnableNotifications(); |
| 64 } else { | 64 } else { |
| 65 fake_push_client_->DisableNotifications(ToNotifierReasonForTest(state)); | 65 fake_push_client_->DisableNotifications(ToNotifierReasonForTest(state)); |
| 66 } | 66 } |
| 67 } | 67 } |
| 68 | 68 |
| 69 void TriggerOnIncomingInvalidation( | 69 void TriggerOnIncomingInvalidation( |
| 70 const ObjectIdInvalidationMap& invalidation_map) { | 70 const ObjectIdInvalidationMap& invalidation_map) { |
| 71 const P2PNotificationData notification_data( | 71 const P2PNotificationData notification_data( |
| 72 "", NOTIFY_ALL, invalidation_map); | 72 std::string(), NOTIFY_ALL, invalidation_map); |
| 73 notifier::Notification notification; | 73 notifier::Notification notification; |
| 74 notification.channel = kSyncP2PNotificationChannel; | 74 notification.channel = kSyncP2PNotificationChannel; |
| 75 notification.data = notification_data.ToString(); | 75 notification.data = notification_data.ToString(); |
| 76 fake_push_client_->SimulateIncomingNotification(notification); | 76 fake_push_client_->SimulateIncomingNotification(notification); |
| 77 } | 77 } |
| 78 | 78 |
| 79 private: | 79 private: |
| 80 // Owned by |invalidator_|. | 80 // Owned by |invalidator_|. |
| 81 notifier::FakePushClient* fake_push_client_; | 81 notifier::FakePushClient* fake_push_client_; |
| 82 scoped_ptr<P2PInvalidator> invalidator_; | 82 scoped_ptr<P2PInvalidator> invalidator_; |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 EXPECT_TRUE(notification_data.IsTargeted("sender")); | 153 EXPECT_TRUE(notification_data.IsTargeted("sender")); |
| 154 EXPECT_TRUE(notification_data.IsTargeted("other1")); | 154 EXPECT_TRUE(notification_data.IsTargeted("other1")); |
| 155 EXPECT_TRUE(notification_data.IsTargeted("other2")); | 155 EXPECT_TRUE(notification_data.IsTargeted("other2")); |
| 156 } | 156 } |
| 157 } | 157 } |
| 158 | 158 |
| 159 // Make sure the P2PNotificationData <-> string conversions work for a | 159 // Make sure the P2PNotificationData <-> string conversions work for a |
| 160 // default-constructed P2PNotificationData. | 160 // default-constructed P2PNotificationData. |
| 161 TEST_F(P2PInvalidatorTest, P2PNotificationDataDefault) { | 161 TEST_F(P2PInvalidatorTest, P2PNotificationDataDefault) { |
| 162 const P2PNotificationData notification_data; | 162 const P2PNotificationData notification_data; |
| 163 EXPECT_TRUE(notification_data.IsTargeted("")); | 163 EXPECT_TRUE(notification_data.IsTargeted(std::string())); |
| 164 EXPECT_FALSE(notification_data.IsTargeted("other1")); | 164 EXPECT_FALSE(notification_data.IsTargeted("other1")); |
| 165 EXPECT_FALSE(notification_data.IsTargeted("other2")); | 165 EXPECT_FALSE(notification_data.IsTargeted("other2")); |
| 166 EXPECT_TRUE(notification_data.GetIdInvalidationMap().empty()); | 166 EXPECT_TRUE(notification_data.GetIdInvalidationMap().empty()); |
| 167 const std::string& notification_data_str = notification_data.ToString(); | 167 const std::string& notification_data_str = notification_data.ToString(); |
| 168 EXPECT_EQ( | 168 EXPECT_EQ( |
| 169 "{\"idInvalidationMap\":[],\"notificationType\":\"notifySelf\"," | 169 "{\"idInvalidationMap\":[],\"notificationType\":\"notifySelf\"," |
| 170 "\"senderId\":\"\"}", notification_data_str); | 170 "\"senderId\":\"\"}", notification_data_str); |
| 171 | 171 |
| 172 P2PNotificationData notification_data_parsed; | 172 P2PNotificationData notification_data_parsed; |
| 173 EXPECT_TRUE(notification_data_parsed.ResetFromString(notification_data_str)); | 173 EXPECT_TRUE(notification_data_parsed.ResetFromString(notification_data_str)); |
| 174 EXPECT_TRUE(notification_data.Equals(notification_data_parsed)); | 174 EXPECT_TRUE(notification_data.Equals(notification_data_parsed)); |
| 175 } | 175 } |
| 176 | 176 |
| 177 // Make sure the P2PNotificationData <-> string conversions work for a | 177 // Make sure the P2PNotificationData <-> string conversions work for a |
| 178 // non-default-constructed P2PNotificationData. | 178 // non-default-constructed P2PNotificationData. |
| 179 TEST_F(P2PInvalidatorTest, P2PNotificationDataNonDefault) { | 179 TEST_F(P2PInvalidatorTest, P2PNotificationDataNonDefault) { |
| 180 const ObjectIdInvalidationMap& invalidation_map = | 180 const ObjectIdInvalidationMap& invalidation_map = |
| 181 ObjectIdSetToInvalidationMap( | 181 ObjectIdSetToInvalidationMap( |
| 182 ModelTypeSetToObjectIdSet(ModelTypeSet(BOOKMARKS, THEMES)), ""); | 182 ModelTypeSetToObjectIdSet(ModelTypeSet(BOOKMARKS, THEMES)), |
| 183 std::string()); |
| 183 const P2PNotificationData notification_data( | 184 const P2PNotificationData notification_data( |
| 184 "sender", NOTIFY_ALL, invalidation_map); | 185 "sender", NOTIFY_ALL, invalidation_map); |
| 185 EXPECT_TRUE(notification_data.IsTargeted("sender")); | 186 EXPECT_TRUE(notification_data.IsTargeted("sender")); |
| 186 EXPECT_TRUE(notification_data.IsTargeted("other1")); | 187 EXPECT_TRUE(notification_data.IsTargeted("other1")); |
| 187 EXPECT_TRUE(notification_data.IsTargeted("other2")); | 188 EXPECT_TRUE(notification_data.IsTargeted("other2")); |
| 188 EXPECT_THAT(invalidation_map, | 189 EXPECT_THAT(invalidation_map, |
| 189 Eq(notification_data.GetIdInvalidationMap())); | 190 Eq(notification_data.GetIdInvalidationMap())); |
| 190 const std::string& notification_data_str = notification_data.ToString(); | 191 const std::string& notification_data_str = notification_data.ToString(); |
| 191 EXPECT_EQ( | 192 EXPECT_EQ( |
| 192 "{\"idInvalidationMap\":[" | 193 "{\"idInvalidationMap\":[" |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 EXPECT_THAT( | 241 EXPECT_THAT( |
| 241 ModelTypeInvalidationMapToObjectIdInvalidationMap( | 242 ModelTypeInvalidationMapToObjectIdInvalidationMap( |
| 242 MakeInvalidationMap(enabled_types)), | 243 MakeInvalidationMap(enabled_types)), |
| 243 Eq(fake_handler_.GetLastInvalidationMap())); | 244 Eq(fake_handler_.GetLastInvalidationMap())); |
| 244 | 245 |
| 245 // Sent with target NOTIFY_OTHERS so should not be propagated to | 246 // Sent with target NOTIFY_OTHERS so should not be propagated to |
| 246 // |fake_handler_|. | 247 // |fake_handler_|. |
| 247 { | 248 { |
| 248 const ObjectIdInvalidationMap& invalidation_map = | 249 const ObjectIdInvalidationMap& invalidation_map = |
| 249 ObjectIdSetToInvalidationMap( | 250 ObjectIdSetToInvalidationMap( |
| 250 ModelTypeSetToObjectIdSet(ModelTypeSet(THEMES, APPS)), ""); | 251 ModelTypeSetToObjectIdSet(ModelTypeSet(THEMES, APPS)), |
| 252 std::string()); |
| 251 invalidator->SendInvalidation(invalidation_map); | 253 invalidator->SendInvalidation(invalidation_map); |
| 252 } | 254 } |
| 253 | 255 |
| 254 ReflectSentNotifications(); | 256 ReflectSentNotifications(); |
| 255 EXPECT_EQ(1, fake_handler_.GetInvalidationCount()); | 257 EXPECT_EQ(1, fake_handler_.GetInvalidationCount()); |
| 256 } | 258 } |
| 257 | 259 |
| 258 // Set up the P2PInvalidator and send out notifications with various | 260 // Set up the P2PInvalidator and send out notifications with various |
| 259 // target settings. The notifications received by the observer should | 261 // target settings. The notifications received by the observer should |
| 260 // be consistent with the target settings. | 262 // be consistent with the target settings. |
| 261 TEST_F(P2PInvalidatorTest, SendNotificationData) { | 263 TEST_F(P2PInvalidatorTest, SendNotificationData) { |
| 262 const ModelTypeSet enabled_types(BOOKMARKS, PREFERENCES, THEMES); | 264 const ModelTypeSet enabled_types(BOOKMARKS, PREFERENCES, THEMES); |
| 263 const ModelTypeSet changed_types(THEMES, APPS); | 265 const ModelTypeSet changed_types(THEMES, APPS); |
| 264 const ModelTypeSet expected_types(THEMES); | 266 const ModelTypeSet expected_types(THEMES); |
| 265 | 267 |
| 266 const ObjectIdInvalidationMap& invalidation_map = | 268 const ObjectIdInvalidationMap& invalidation_map = |
| 267 ObjectIdSetToInvalidationMap( | 269 ObjectIdSetToInvalidationMap(ModelTypeSetToObjectIdSet(changed_types), |
| 268 ModelTypeSetToObjectIdSet(changed_types), ""); | 270 std::string()); |
| 269 | 271 |
| 270 P2PInvalidator* const invalidator = delegate_.GetInvalidator(); | 272 P2PInvalidator* const invalidator = delegate_.GetInvalidator(); |
| 271 notifier::FakePushClient* const push_client = delegate_.GetPushClient(); | 273 notifier::FakePushClient* const push_client = delegate_.GetPushClient(); |
| 272 | 274 |
| 273 invalidator->UpdateRegisteredIds(&fake_handler_, | 275 invalidator->UpdateRegisteredIds(&fake_handler_, |
| 274 ModelTypeSetToObjectIdSet(enabled_types)); | 276 ModelTypeSetToObjectIdSet(enabled_types)); |
| 275 | 277 |
| 276 invalidator->UpdateCredentials("foo@bar.com", "fake_token"); | 278 invalidator->UpdateCredentials("foo@bar.com", "fake_token"); |
| 277 | 279 |
| 278 ReflectSentNotifications(); | 280 ReflectSentNotifications(); |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 354 EXPECT_EQ(5, fake_handler_.GetInvalidationCount()); | 356 EXPECT_EQ(5, fake_handler_.GetInvalidationCount()); |
| 355 } | 357 } |
| 356 | 358 |
| 357 INSTANTIATE_TYPED_TEST_CASE_P( | 359 INSTANTIATE_TYPED_TEST_CASE_P( |
| 358 P2PInvalidatorTest, InvalidatorTest, | 360 P2PInvalidatorTest, InvalidatorTest, |
| 359 P2PInvalidatorTestDelegate); | 361 P2PInvalidatorTestDelegate); |
| 360 | 362 |
| 361 } // namespace | 363 } // namespace |
| 362 | 364 |
| 363 } // namespace syncer | 365 } // namespace syncer |
| OLD | NEW |