| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/invalidation/p2p_invalidator.h" | 5 #include "components/invalidation/p2p_invalidator.h" |
| 6 | 6 |
| 7 #include <cstddef> | 7 #include <cstddef> |
| 8 | 8 |
| 9 #include "components/invalidation/fake_invalidation_handler.h" | 9 #include "components/invalidation/fake_invalidation_handler.h" |
| 10 #include "components/invalidation/invalidator_test_template.h" | 10 #include "components/invalidation/invalidator_test_template.h" |
| 11 #include "components/invalidation/notifier_reason_util.h" | 11 #include "components/invalidation/notifier_reason_util.h" |
| 12 #include "jingle/notifier/listener/fake_push_client.h" | 12 #include "jingle/notifier/listener/fake_push_client.h" |
| 13 #include "sync/internal_api/public/base/invalidator_state.h" | 13 #include "sync/internal_api/public/base/invalidator_state.h" |
| 14 #include "sync/internal_api/public/base/model_type.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 15 |
| 17 namespace syncer { | 16 namespace syncer { |
| 18 | 17 |
| 19 namespace { | 18 namespace { |
| 20 | 19 |
| 21 class P2PInvalidatorTestDelegate { | 20 class P2PInvalidatorTestDelegate { |
| 22 public: | 21 public: |
| 23 P2PInvalidatorTestDelegate() : fake_push_client_(NULL) {} | 22 P2PInvalidatorTestDelegate() : fake_push_client_(NULL) {} |
| 24 | 23 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 private: | 78 private: |
| 80 // Owned by |invalidator_|. | 79 // Owned by |invalidator_|. |
| 81 notifier::FakePushClient* fake_push_client_; | 80 notifier::FakePushClient* fake_push_client_; |
| 82 scoped_ptr<P2PInvalidator> invalidator_; | 81 scoped_ptr<P2PInvalidator> invalidator_; |
| 83 }; | 82 }; |
| 84 | 83 |
| 85 class P2PInvalidatorTest : public testing::Test { | 84 class P2PInvalidatorTest : public testing::Test { |
| 86 protected: | 85 protected: |
| 87 P2PInvalidatorTest() | 86 P2PInvalidatorTest() |
| 88 : next_sent_notification_to_reflect_(0) { | 87 : next_sent_notification_to_reflect_(0) { |
| 88 default_enabled_ids_.insert(invalidation::ObjectId(10, "A")); |
| 89 default_enabled_ids_.insert(invalidation::ObjectId(10, "B")); |
| 89 delegate_.CreateInvalidator("sender", | 90 delegate_.CreateInvalidator("sender", |
| 90 "fake_state", | 91 "fake_state", |
| 91 base::WeakPtr<InvalidationStateTracker>()); | 92 base::WeakPtr<InvalidationStateTracker>()); |
| 92 delegate_.GetInvalidator()->RegisterHandler(&fake_handler_); | 93 delegate_.GetInvalidator()->RegisterHandler(&fake_handler_); |
| 93 } | 94 } |
| 94 | 95 |
| 95 virtual ~P2PInvalidatorTest() { | 96 virtual ~P2PInvalidatorTest() { |
| 96 delegate_.GetInvalidator()->UnregisterHandler(&fake_handler_); | 97 delegate_.GetInvalidator()->UnregisterHandler(&fake_handler_); |
| 97 } | 98 } |
| 98 | 99 |
| 99 ObjectIdInvalidationMap MakeInvalidationMap(ModelTypeSet types) { | 100 ObjectIdInvalidationMap MakeInvalidationMap(ObjectIdSet ids) { |
| 100 ObjectIdInvalidationMap invalidations; | |
| 101 ObjectIdSet ids = ModelTypeSetToObjectIdSet(types); | |
| 102 return ObjectIdInvalidationMap::InvalidateAll(ids); | 101 return ObjectIdInvalidationMap::InvalidateAll(ids); |
| 103 } | 102 } |
| 104 | 103 |
| 105 // Simulate receiving all the notifications we sent out since last | 104 // Simulate receiving all the notifications we sent out since last |
| 106 // time this was called. | 105 // time this was called. |
| 107 void ReflectSentNotifications() { | 106 void ReflectSentNotifications() { |
| 108 const std::vector<notifier::Notification>& sent_notifications = | 107 const std::vector<notifier::Notification>& sent_notifications = |
| 109 delegate_.GetPushClient()->sent_notifications(); | 108 delegate_.GetPushClient()->sent_notifications(); |
| 110 for(size_t i = next_sent_notification_to_reflect_; | 109 for(size_t i = next_sent_notification_to_reflect_; |
| 111 i < sent_notifications.size(); ++i) { | 110 i < sent_notifications.size(); ++i) { |
| 112 delegate_.GetInvalidator()->OnIncomingNotification(sent_notifications[i]); | 111 delegate_.GetInvalidator()->OnIncomingNotification(sent_notifications[i]); |
| 113 } | 112 } |
| 114 next_sent_notification_to_reflect_ = sent_notifications.size(); | 113 next_sent_notification_to_reflect_ = sent_notifications.size(); |
| 115 } | 114 } |
| 116 | 115 |
| 116 ObjectIdSet default_enabled_ids_; |
| 117 |
| 117 FakeInvalidationHandler fake_handler_; | 118 FakeInvalidationHandler fake_handler_; |
| 118 P2PInvalidatorTestDelegate delegate_; | 119 P2PInvalidatorTestDelegate delegate_; |
| 119 | 120 |
| 120 private: | 121 private: |
| 121 size_t next_sent_notification_to_reflect_; | 122 size_t next_sent_notification_to_reflect_; |
| 122 }; | 123 }; |
| 123 | 124 |
| 124 // Make sure the P2PNotificationTarget <-> string conversions work. | 125 // Make sure the P2PNotificationTarget <-> string conversions work. |
| 125 TEST_F(P2PInvalidatorTest, P2PNotificationTarget) { | 126 TEST_F(P2PInvalidatorTest, P2PNotificationTarget) { |
| 126 for (int i = FIRST_NOTIFICATION_TARGET; | 127 for (int i = FIRST_NOTIFICATION_TARGET; |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 | 174 |
| 174 P2PNotificationData notification_data_parsed; | 175 P2PNotificationData notification_data_parsed; |
| 175 EXPECT_TRUE(notification_data_parsed.ResetFromString(notification_data_str)); | 176 EXPECT_TRUE(notification_data_parsed.ResetFromString(notification_data_str)); |
| 176 EXPECT_TRUE(notification_data.Equals(notification_data_parsed)); | 177 EXPECT_TRUE(notification_data.Equals(notification_data_parsed)); |
| 177 } | 178 } |
| 178 | 179 |
| 179 // Make sure the P2PNotificationData <-> string conversions work for a | 180 // Make sure the P2PNotificationData <-> string conversions work for a |
| 180 // non-default-constructed P2PNotificationData. | 181 // non-default-constructed P2PNotificationData. |
| 181 TEST_F(P2PInvalidatorTest, P2PNotificationDataNonDefault) { | 182 TEST_F(P2PInvalidatorTest, P2PNotificationDataNonDefault) { |
| 182 ObjectIdInvalidationMap invalidation_map = | 183 ObjectIdInvalidationMap invalidation_map = |
| 183 ObjectIdInvalidationMap::InvalidateAll( | 184 ObjectIdInvalidationMap::InvalidateAll(default_enabled_ids_); |
| 184 ModelTypeSetToObjectIdSet(ModelTypeSet(BOOKMARKS, THEMES))); | |
| 185 const P2PNotificationData notification_data("sender", | 185 const P2PNotificationData notification_data("sender", |
| 186 NOTIFY_ALL, | 186 NOTIFY_ALL, |
| 187 invalidation_map); | 187 invalidation_map); |
| 188 EXPECT_TRUE(notification_data.IsTargeted("sender")); | 188 EXPECT_TRUE(notification_data.IsTargeted("sender")); |
| 189 EXPECT_TRUE(notification_data.IsTargeted("other1")); | 189 EXPECT_TRUE(notification_data.IsTargeted("other1")); |
| 190 EXPECT_TRUE(notification_data.IsTargeted("other2")); | 190 EXPECT_TRUE(notification_data.IsTargeted("other2")); |
| 191 EXPECT_EQ(invalidation_map, notification_data.GetIdInvalidationMap()); | 191 EXPECT_EQ(invalidation_map, notification_data.GetIdInvalidationMap()); |
| 192 const std::string& notification_data_str = notification_data.ToString(); | 192 const std::string& notification_data_str = notification_data.ToString(); |
| 193 EXPECT_EQ( | 193 EXPECT_EQ( |
| 194 "{\"invalidations\":[" | 194 "{\"invalidations\":[" |
| 195 "{\"isUnknownVersion\":true," | 195 "{\"isUnknownVersion\":true," |
| 196 "\"objectId\":{\"name\":\"BOOKMARK\",\"source\":1004}}," | 196 "\"objectId\":{\"name\":\"A\",\"source\":10}}," |
| 197 "{\"isUnknownVersion\":true," | 197 "{\"isUnknownVersion\":true," |
| 198 "\"objectId\":{\"name\":\"THEME\",\"source\":1004}}" | 198 "\"objectId\":{\"name\":\"B\",\"source\":10}}" |
| 199 "],\"notificationType\":\"notifyAll\"," | 199 "],\"notificationType\":\"notifyAll\"," |
| 200 "\"senderId\":\"sender\"}", notification_data_str); | 200 "\"senderId\":\"sender\"}", |
| 201 notification_data_str); |
| 201 | 202 |
| 202 P2PNotificationData notification_data_parsed; | 203 P2PNotificationData notification_data_parsed; |
| 203 EXPECT_TRUE(notification_data_parsed.ResetFromString(notification_data_str)); | 204 EXPECT_TRUE(notification_data_parsed.ResetFromString(notification_data_str)); |
| 204 EXPECT_TRUE(notification_data.Equals(notification_data_parsed)); | 205 EXPECT_TRUE(notification_data.Equals(notification_data_parsed)); |
| 205 } | 206 } |
| 206 | 207 |
| 207 // Set up the P2PInvalidator, simulate a successful connection, and send | 208 // Set up the P2PInvalidator, simulate a successful connection, and send |
| 208 // a notification with the default target (NOTIFY_OTHERS). The | 209 // a notification with the default target (NOTIFY_OTHERS). The |
| 209 // observer should receive only a notification from the call to | 210 // observer should receive only a notification from the call to |
| 210 // UpdateEnabledTypes(). | 211 // UpdateEnabledTypes(). |
| 211 TEST_F(P2PInvalidatorTest, NotificationsBasic) { | 212 TEST_F(P2PInvalidatorTest, NotificationsBasic) { |
| 212 const ModelTypeSet enabled_types(BOOKMARKS, PREFERENCES); | |
| 213 | |
| 214 P2PInvalidator* const invalidator = delegate_.GetInvalidator(); | 213 P2PInvalidator* const invalidator = delegate_.GetInvalidator(); |
| 215 notifier::FakePushClient* const push_client = delegate_.GetPushClient(); | 214 notifier::FakePushClient* const push_client = delegate_.GetPushClient(); |
| 216 | 215 |
| 217 invalidator->UpdateRegisteredIds(&fake_handler_, | 216 invalidator->UpdateRegisteredIds(&fake_handler_, default_enabled_ids_); |
| 218 ModelTypeSetToObjectIdSet(enabled_types)); | |
| 219 | 217 |
| 220 const char kEmail[] = "foo@bar.com"; | 218 const char kEmail[] = "foo@bar.com"; |
| 221 const char kToken[] = "token"; | 219 const char kToken[] = "token"; |
| 222 invalidator->UpdateCredentials(kEmail, kToken); | 220 invalidator->UpdateCredentials(kEmail, kToken); |
| 223 { | 221 { |
| 224 notifier::Subscription expected_subscription; | 222 notifier::Subscription expected_subscription; |
| 225 expected_subscription.channel = kSyncP2PNotificationChannel; | 223 expected_subscription.channel = kSyncP2PNotificationChannel; |
| 226 expected_subscription.from = kEmail; | 224 expected_subscription.from = kEmail; |
| 227 EXPECT_TRUE(notifier::SubscriptionListsEqual( | 225 EXPECT_TRUE(notifier::SubscriptionListsEqual( |
| 228 push_client->subscriptions(), | 226 push_client->subscriptions(), |
| 229 notifier::SubscriptionList(1, expected_subscription))); | 227 notifier::SubscriptionList(1, expected_subscription))); |
| 230 } | 228 } |
| 231 EXPECT_EQ(kEmail, push_client->email()); | 229 EXPECT_EQ(kEmail, push_client->email()); |
| 232 EXPECT_EQ(kToken, push_client->token()); | 230 EXPECT_EQ(kToken, push_client->token()); |
| 233 | 231 |
| 234 ReflectSentNotifications(); | 232 ReflectSentNotifications(); |
| 235 push_client->EnableNotifications(); | 233 push_client->EnableNotifications(); |
| 236 EXPECT_EQ(INVALIDATIONS_ENABLED, fake_handler_.GetInvalidatorState()); | 234 EXPECT_EQ(INVALIDATIONS_ENABLED, fake_handler_.GetInvalidatorState()); |
| 237 | 235 |
| 238 ReflectSentNotifications(); | 236 ReflectSentNotifications(); |
| 239 EXPECT_EQ(1, fake_handler_.GetInvalidationCount()); | 237 EXPECT_EQ(1, fake_handler_.GetInvalidationCount()); |
| 240 EXPECT_THAT( | 238 EXPECT_THAT(MakeInvalidationMap(default_enabled_ids_), |
| 241 MakeInvalidationMap(enabled_types), | 239 Eq(fake_handler_.GetLastInvalidationMap())); |
| 242 Eq(fake_handler_.GetLastInvalidationMap())); | |
| 243 | 240 |
| 244 // Sent with target NOTIFY_OTHERS so should not be propagated to | 241 // Sent with target NOTIFY_OTHERS so should not be propagated to |
| 245 // |fake_handler_|. | 242 // |fake_handler_|. |
| 246 invalidator->SendInvalidation( | 243 invalidator->SendInvalidation(default_enabled_ids_); |
| 247 ModelTypeSetToObjectIdSet(ModelTypeSet(THEMES, APPS))); | |
| 248 | 244 |
| 249 ReflectSentNotifications(); | 245 ReflectSentNotifications(); |
| 250 EXPECT_EQ(1, fake_handler_.GetInvalidationCount()); | 246 EXPECT_EQ(1, fake_handler_.GetInvalidationCount()); |
| 251 } | 247 } |
| 252 | 248 |
| 253 // Set up the P2PInvalidator and send out notifications with various | 249 // Set up the P2PInvalidator and send out notifications with various |
| 254 // target settings. The notifications received by the observer should | 250 // target settings. The notifications received by the observer should |
| 255 // be consistent with the target settings. | 251 // be consistent with the target settings. |
| 256 TEST_F(P2PInvalidatorTest, SendNotificationData) { | 252 TEST_F(P2PInvalidatorTest, SendNotificationData) { |
| 257 const ModelTypeSet enabled_types(BOOKMARKS, PREFERENCES, THEMES); | 253 ObjectIdSet enabled_ids; |
| 258 const ModelTypeSet changed_types(THEMES, APPS); | 254 ObjectIdSet changed_ids; |
| 259 const ModelTypeSet expected_types(THEMES); | 255 ObjectIdSet expected_ids; |
| 256 |
| 257 enabled_ids.insert(invalidation::ObjectId(20, "A")); |
| 258 enabled_ids.insert(invalidation::ObjectId(20, "B")); |
| 259 enabled_ids.insert(invalidation::ObjectId(20, "C")); |
| 260 |
| 261 changed_ids.insert(invalidation::ObjectId(20, "A")); |
| 262 changed_ids.insert(invalidation::ObjectId(20, "Z")); |
| 263 |
| 264 expected_ids.insert(invalidation::ObjectId(20, "A")); |
| 260 | 265 |
| 261 const ObjectIdInvalidationMap& invalidation_map = | 266 const ObjectIdInvalidationMap& invalidation_map = |
| 262 MakeInvalidationMap(changed_types); | 267 MakeInvalidationMap(changed_ids); |
| 263 | 268 |
| 264 P2PInvalidator* const invalidator = delegate_.GetInvalidator(); | 269 P2PInvalidator* const invalidator = delegate_.GetInvalidator(); |
| 265 notifier::FakePushClient* const push_client = delegate_.GetPushClient(); | 270 notifier::FakePushClient* const push_client = delegate_.GetPushClient(); |
| 266 | 271 |
| 267 invalidator->UpdateRegisteredIds(&fake_handler_, | 272 invalidator->UpdateRegisteredIds(&fake_handler_, enabled_ids); |
| 268 ModelTypeSetToObjectIdSet(enabled_types)); | |
| 269 | 273 |
| 270 invalidator->UpdateCredentials("foo@bar.com", "fake_token"); | 274 invalidator->UpdateCredentials("foo@bar.com", "fake_token"); |
| 271 | 275 |
| 272 ReflectSentNotifications(); | 276 ReflectSentNotifications(); |
| 273 push_client->EnableNotifications(); | 277 push_client->EnableNotifications(); |
| 274 EXPECT_EQ(INVALIDATIONS_ENABLED, fake_handler_.GetInvalidatorState()); | 278 EXPECT_EQ(INVALIDATIONS_ENABLED, fake_handler_.GetInvalidatorState()); |
| 275 | 279 |
| 276 ReflectSentNotifications(); | 280 ReflectSentNotifications(); |
| 277 EXPECT_EQ(1, fake_handler_.GetInvalidationCount()); | 281 EXPECT_EQ(1, fake_handler_.GetInvalidationCount()); |
| 278 EXPECT_EQ(ModelTypeSetToObjectIdSet(enabled_types), | 282 EXPECT_EQ(enabled_ids, fake_handler_.GetLastInvalidationMap().GetObjectIds()); |
| 279 fake_handler_.GetLastInvalidationMap().GetObjectIds()); | |
| 280 | 283 |
| 281 // Should be dropped. | 284 // Should be dropped. |
| 282 invalidator->SendNotificationDataForTest(P2PNotificationData()); | 285 invalidator->SendNotificationDataForTest(P2PNotificationData()); |
| 283 ReflectSentNotifications(); | 286 ReflectSentNotifications(); |
| 284 EXPECT_EQ(1, fake_handler_.GetInvalidationCount()); | 287 EXPECT_EQ(1, fake_handler_.GetInvalidationCount()); |
| 285 | 288 |
| 286 const ObjectIdSet& expected_ids = ModelTypeSetToObjectIdSet(expected_types); | |
| 287 | |
| 288 // Should be propagated. | 289 // Should be propagated. |
| 289 invalidator->SendNotificationDataForTest( | 290 invalidator->SendNotificationDataForTest( |
| 290 P2PNotificationData("sender", NOTIFY_SELF, invalidation_map)); | 291 P2PNotificationData("sender", NOTIFY_SELF, invalidation_map)); |
| 291 ReflectSentNotifications(); | 292 ReflectSentNotifications(); |
| 292 EXPECT_EQ(2, fake_handler_.GetInvalidationCount()); | 293 EXPECT_EQ(2, fake_handler_.GetInvalidationCount()); |
| 293 EXPECT_EQ(expected_ids, | 294 EXPECT_EQ(expected_ids, |
| 294 fake_handler_.GetLastInvalidationMap().GetObjectIds()); | 295 fake_handler_.GetLastInvalidationMap().GetObjectIds()); |
| 295 | 296 |
| 296 // Should be dropped. | 297 // Should be dropped. |
| 297 invalidator->SendNotificationDataForTest( | 298 invalidator->SendNotificationDataForTest( |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 EXPECT_EQ(5, fake_handler_.GetInvalidationCount()); | 349 EXPECT_EQ(5, fake_handler_.GetInvalidationCount()); |
| 349 } | 350 } |
| 350 | 351 |
| 351 INSTANTIATE_TYPED_TEST_CASE_P( | 352 INSTANTIATE_TYPED_TEST_CASE_P( |
| 352 P2PInvalidatorTest, InvalidatorTest, | 353 P2PInvalidatorTest, InvalidatorTest, |
| 353 P2PInvalidatorTestDelegate); | 354 P2PInvalidatorTestDelegate); |
| 354 | 355 |
| 355 } // namespace | 356 } // namespace |
| 356 | 357 |
| 357 } // namespace syncer | 358 } // namespace syncer |
| OLD | NEW |