| 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_notifier.h" | 5 #include "sync/notifier/p2p_notifier.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 16 matching lines...) Expand all Loading... |
| 27 scoped_ptr<notifier::PushClient>(fake_push_client_), | 27 scoped_ptr<notifier::PushClient>(fake_push_client_), |
| 28 NOTIFY_OTHERS), | 28 NOTIFY_OTHERS), |
| 29 next_sent_notification_to_reflect_(0) { | 29 next_sent_notification_to_reflect_(0) { |
| 30 p2p_notifier_.AddObserver(&mock_observer_); | 30 p2p_notifier_.AddObserver(&mock_observer_); |
| 31 } | 31 } |
| 32 | 32 |
| 33 virtual ~P2PNotifierTest() { | 33 virtual ~P2PNotifierTest() { |
| 34 p2p_notifier_.RemoveObserver(&mock_observer_); | 34 p2p_notifier_.RemoveObserver(&mock_observer_); |
| 35 } | 35 } |
| 36 | 36 |
| 37 syncer::ModelTypePayloadMap MakePayloadMap(syncer::ModelTypeSet types) { | 37 ModelTypePayloadMap MakePayloadMap(ModelTypeSet types) { |
| 38 return syncer::ModelTypePayloadMapFromEnumSet(types, ""); | 38 return ModelTypePayloadMapFromEnumSet(types, ""); |
| 39 } | 39 } |
| 40 | 40 |
| 41 // Simulate receiving all the notifications we sent out since last | 41 // Simulate receiving all the notifications we sent out since last |
| 42 // time this was called. | 42 // time this was called. |
| 43 void ReflectSentNotifications() { | 43 void ReflectSentNotifications() { |
| 44 const std::vector<notifier::Notification>& sent_notifications = | 44 const std::vector<notifier::Notification>& sent_notifications = |
| 45 fake_push_client_->sent_notifications(); | 45 fake_push_client_->sent_notifications(); |
| 46 for(size_t i = next_sent_notification_to_reflect_; | 46 for(size_t i = next_sent_notification_to_reflect_; |
| 47 i < sent_notifications.size(); ++i) { | 47 i < sent_notifications.size(); ++i) { |
| 48 p2p_notifier_.OnIncomingNotification(sent_notifications[i]); | 48 p2p_notifier_.OnIncomingNotification(sent_notifications[i]); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 68 EXPECT_FALSE(target_str.empty()); | 68 EXPECT_FALSE(target_str.empty()); |
| 69 EXPECT_EQ(target, P2PNotificationTargetFromString(target_str)); | 69 EXPECT_EQ(target, P2PNotificationTargetFromString(target_str)); |
| 70 } | 70 } |
| 71 EXPECT_EQ(NOTIFY_SELF, P2PNotificationTargetFromString("unknown")); | 71 EXPECT_EQ(NOTIFY_SELF, P2PNotificationTargetFromString("unknown")); |
| 72 } | 72 } |
| 73 | 73 |
| 74 // Make sure notification targeting works correctly. | 74 // Make sure notification targeting works correctly. |
| 75 TEST_F(P2PNotifierTest, P2PNotificationDataIsTargeted) { | 75 TEST_F(P2PNotifierTest, P2PNotificationDataIsTargeted) { |
| 76 { | 76 { |
| 77 const P2PNotificationData notification_data( | 77 const P2PNotificationData notification_data( |
| 78 "sender", NOTIFY_SELF, syncer::ModelTypeSet()); | 78 "sender", NOTIFY_SELF, ModelTypeSet()); |
| 79 EXPECT_TRUE(notification_data.IsTargeted("sender")); | 79 EXPECT_TRUE(notification_data.IsTargeted("sender")); |
| 80 EXPECT_FALSE(notification_data.IsTargeted("other1")); | 80 EXPECT_FALSE(notification_data.IsTargeted("other1")); |
| 81 EXPECT_FALSE(notification_data.IsTargeted("other2")); | 81 EXPECT_FALSE(notification_data.IsTargeted("other2")); |
| 82 } | 82 } |
| 83 { | 83 { |
| 84 const P2PNotificationData notification_data( | 84 const P2PNotificationData notification_data( |
| 85 "sender", NOTIFY_OTHERS, syncer::ModelTypeSet()); | 85 "sender", NOTIFY_OTHERS, ModelTypeSet()); |
| 86 EXPECT_FALSE(notification_data.IsTargeted("sender")); | 86 EXPECT_FALSE(notification_data.IsTargeted("sender")); |
| 87 EXPECT_TRUE(notification_data.IsTargeted("other1")); | 87 EXPECT_TRUE(notification_data.IsTargeted("other1")); |
| 88 EXPECT_TRUE(notification_data.IsTargeted("other2")); | 88 EXPECT_TRUE(notification_data.IsTargeted("other2")); |
| 89 } | 89 } |
| 90 { | 90 { |
| 91 const P2PNotificationData notification_data( | 91 const P2PNotificationData notification_data( |
| 92 "sender", NOTIFY_ALL, syncer::ModelTypeSet()); | 92 "sender", NOTIFY_ALL, ModelTypeSet()); |
| 93 EXPECT_TRUE(notification_data.IsTargeted("sender")); | 93 EXPECT_TRUE(notification_data.IsTargeted("sender")); |
| 94 EXPECT_TRUE(notification_data.IsTargeted("other1")); | 94 EXPECT_TRUE(notification_data.IsTargeted("other1")); |
| 95 EXPECT_TRUE(notification_data.IsTargeted("other2")); | 95 EXPECT_TRUE(notification_data.IsTargeted("other2")); |
| 96 } | 96 } |
| 97 } | 97 } |
| 98 | 98 |
| 99 // Make sure the P2PNotificationData <-> string conversions work for a | 99 // Make sure the P2PNotificationData <-> string conversions work for a |
| 100 // default-constructed P2PNotificationData. | 100 // default-constructed P2PNotificationData. |
| 101 TEST_F(P2PNotifierTest, P2PNotificationDataDefault) { | 101 TEST_F(P2PNotifierTest, P2PNotificationDataDefault) { |
| 102 const P2PNotificationData notification_data; | 102 const P2PNotificationData notification_data; |
| 103 EXPECT_TRUE(notification_data.IsTargeted("")); | 103 EXPECT_TRUE(notification_data.IsTargeted("")); |
| 104 EXPECT_FALSE(notification_data.IsTargeted("other1")); | 104 EXPECT_FALSE(notification_data.IsTargeted("other1")); |
| 105 EXPECT_FALSE(notification_data.IsTargeted("other2")); | 105 EXPECT_FALSE(notification_data.IsTargeted("other2")); |
| 106 EXPECT_TRUE(notification_data.GetChangedTypes().Empty()); | 106 EXPECT_TRUE(notification_data.GetChangedTypes().Empty()); |
| 107 const std::string& notification_data_str = notification_data.ToString(); | 107 const std::string& notification_data_str = notification_data.ToString(); |
| 108 EXPECT_EQ( | 108 EXPECT_EQ( |
| 109 "{\"changedTypes\":[],\"notificationType\":\"notifySelf\"," | 109 "{\"changedTypes\":[],\"notificationType\":\"notifySelf\"," |
| 110 "\"senderId\":\"\"}", notification_data_str); | 110 "\"senderId\":\"\"}", notification_data_str); |
| 111 | 111 |
| 112 P2PNotificationData notification_data_parsed; | 112 P2PNotificationData notification_data_parsed; |
| 113 EXPECT_TRUE(notification_data_parsed.ResetFromString(notification_data_str)); | 113 EXPECT_TRUE(notification_data_parsed.ResetFromString(notification_data_str)); |
| 114 EXPECT_TRUE(notification_data.Equals(notification_data_parsed)); | 114 EXPECT_TRUE(notification_data.Equals(notification_data_parsed)); |
| 115 } | 115 } |
| 116 | 116 |
| 117 // Make sure the P2PNotificationData <-> string conversions work for a | 117 // Make sure the P2PNotificationData <-> string conversions work for a |
| 118 // non-default-constructed P2PNotificationData. | 118 // non-default-constructed P2PNotificationData. |
| 119 TEST_F(P2PNotifierTest, P2PNotificationDataNonDefault) { | 119 TEST_F(P2PNotifierTest, P2PNotificationDataNonDefault) { |
| 120 const syncer::ModelTypeSet changed_types(syncer::BOOKMARKS, syncer::THEMES); | 120 const ModelTypeSet changed_types(BOOKMARKS, THEMES); |
| 121 const P2PNotificationData notification_data( | 121 const P2PNotificationData notification_data( |
| 122 "sender", NOTIFY_ALL, changed_types); | 122 "sender", NOTIFY_ALL, changed_types); |
| 123 EXPECT_TRUE(notification_data.IsTargeted("sender")); | 123 EXPECT_TRUE(notification_data.IsTargeted("sender")); |
| 124 EXPECT_TRUE(notification_data.IsTargeted("other1")); | 124 EXPECT_TRUE(notification_data.IsTargeted("other1")); |
| 125 EXPECT_TRUE(notification_data.IsTargeted("other2")); | 125 EXPECT_TRUE(notification_data.IsTargeted("other2")); |
| 126 EXPECT_TRUE(notification_data.GetChangedTypes().Equals(changed_types)); | 126 EXPECT_TRUE(notification_data.GetChangedTypes().Equals(changed_types)); |
| 127 const std::string& notification_data_str = notification_data.ToString(); | 127 const std::string& notification_data_str = notification_data.ToString(); |
| 128 EXPECT_EQ( | 128 EXPECT_EQ( |
| 129 "{\"changedTypes\":[\"Bookmarks\",\"Themes\"]," | 129 "{\"changedTypes\":[\"Bookmarks\",\"Themes\"]," |
| 130 "\"notificationType\":\"notifyAll\"," | 130 "\"notificationType\":\"notifyAll\"," |
| 131 "\"senderId\":\"sender\"}", notification_data_str); | 131 "\"senderId\":\"sender\"}", notification_data_str); |
| 132 | 132 |
| 133 P2PNotificationData notification_data_parsed; | 133 P2PNotificationData notification_data_parsed; |
| 134 EXPECT_TRUE(notification_data_parsed.ResetFromString(notification_data_str)); | 134 EXPECT_TRUE(notification_data_parsed.ResetFromString(notification_data_str)); |
| 135 EXPECT_TRUE(notification_data.Equals(notification_data_parsed)); | 135 EXPECT_TRUE(notification_data.Equals(notification_data_parsed)); |
| 136 } | 136 } |
| 137 | 137 |
| 138 // Set up the P2PNotifier, simulate a successful connection, and send | 138 // Set up the P2PNotifier, simulate a successful connection, and send |
| 139 // a notification with the default target (NOTIFY_OTHERS). The | 139 // a notification with the default target (NOTIFY_OTHERS). The |
| 140 // observer should receive only a notification from the call to | 140 // observer should receive only a notification from the call to |
| 141 // UpdateEnabledTypes(). | 141 // UpdateEnabledTypes(). |
| 142 TEST_F(P2PNotifierTest, NotificationsBasic) { | 142 TEST_F(P2PNotifierTest, NotificationsBasic) { |
| 143 syncer::ModelTypeSet enabled_types(syncer::BOOKMARKS, syncer::PREFERENCES); | 143 ModelTypeSet enabled_types(BOOKMARKS, PREFERENCES); |
| 144 | 144 |
| 145 EXPECT_CALL(mock_observer_, OnNotificationsEnabled()); | 145 EXPECT_CALL(mock_observer_, OnNotificationsEnabled()); |
| 146 EXPECT_CALL(mock_observer_, | 146 EXPECT_CALL(mock_observer_, |
| 147 OnIncomingNotification(MakePayloadMap(enabled_types), | 147 OnIncomingNotification(MakePayloadMap(enabled_types), |
| 148 REMOTE_NOTIFICATION)); | 148 REMOTE_NOTIFICATION)); |
| 149 | 149 |
| 150 p2p_notifier_.SetUniqueId("sender"); | 150 p2p_notifier_.SetUniqueId("sender"); |
| 151 | 151 |
| 152 const char kEmail[] = "foo@bar.com"; | 152 const char kEmail[] = "foo@bar.com"; |
| 153 const char kToken[] = "token"; | 153 const char kToken[] = "token"; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 164 EXPECT_EQ(kToken, fake_push_client_->token()); | 164 EXPECT_EQ(kToken, fake_push_client_->token()); |
| 165 | 165 |
| 166 p2p_notifier_.UpdateEnabledTypes(enabled_types); | 166 p2p_notifier_.UpdateEnabledTypes(enabled_types); |
| 167 | 167 |
| 168 ReflectSentNotifications(); | 168 ReflectSentNotifications(); |
| 169 fake_push_client_->EnableNotifications(); | 169 fake_push_client_->EnableNotifications(); |
| 170 | 170 |
| 171 // Sent with target NOTIFY_OTHERS so should not be propagated to | 171 // Sent with target NOTIFY_OTHERS so should not be propagated to |
| 172 // |mock_observer_|. | 172 // |mock_observer_|. |
| 173 { | 173 { |
| 174 syncer::ModelTypeSet changed_types(syncer::THEMES, syncer::APPS); | 174 ModelTypeSet changed_types(THEMES, APPS); |
| 175 p2p_notifier_.SendNotification(changed_types); | 175 p2p_notifier_.SendNotification(changed_types); |
| 176 } | 176 } |
| 177 | 177 |
| 178 ReflectSentNotifications(); | 178 ReflectSentNotifications(); |
| 179 } | 179 } |
| 180 | 180 |
| 181 // Set up the P2PNotifier and send out notifications with various | 181 // Set up the P2PNotifier and send out notifications with various |
| 182 // target settings. The notifications received by the observer should | 182 // target settings. The notifications received by the observer should |
| 183 // be consistent with the target settings. | 183 // be consistent with the target settings. |
| 184 TEST_F(P2PNotifierTest, SendNotificationData) { | 184 TEST_F(P2PNotifierTest, SendNotificationData) { |
| 185 syncer::ModelTypeSet enabled_types(syncer::BOOKMARKS, syncer::PREFERENCES); | 185 ModelTypeSet enabled_types(BOOKMARKS, PREFERENCES); |
| 186 | 186 |
| 187 syncer::ModelTypeSet changed_types(syncer::THEMES, syncer::APPS); | 187 ModelTypeSet changed_types(THEMES, APPS); |
| 188 | 188 |
| 189 const syncer::ModelTypePayloadMap& changed_payload_map = | 189 const ModelTypePayloadMap& changed_payload_map = |
| 190 MakePayloadMap(changed_types); | 190 MakePayloadMap(changed_types); |
| 191 | 191 |
| 192 EXPECT_CALL(mock_observer_, OnNotificationsEnabled()); | 192 EXPECT_CALL(mock_observer_, OnNotificationsEnabled()); |
| 193 EXPECT_CALL(mock_observer_, | 193 EXPECT_CALL(mock_observer_, |
| 194 OnIncomingNotification(MakePayloadMap(enabled_types), | 194 OnIncomingNotification(MakePayloadMap(enabled_types), |
| 195 REMOTE_NOTIFICATION)); | 195 REMOTE_NOTIFICATION)); |
| 196 | 196 |
| 197 p2p_notifier_.SetUniqueId("sender"); | 197 p2p_notifier_.SetUniqueId("sender"); |
| 198 p2p_notifier_.UpdateCredentials("foo@bar.com", "fake_token"); | 198 p2p_notifier_.UpdateCredentials("foo@bar.com", "fake_token"); |
| 199 p2p_notifier_.UpdateEnabledTypes(enabled_types); | 199 p2p_notifier_.UpdateEnabledTypes(enabled_types); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 221 // Should be dropped. | 221 // Should be dropped. |
| 222 Mock::VerifyAndClearExpectations(&mock_observer_); | 222 Mock::VerifyAndClearExpectations(&mock_observer_); |
| 223 p2p_notifier_.SendNotificationDataForTest( | 223 p2p_notifier_.SendNotificationDataForTest( |
| 224 P2PNotificationData("sender2", NOTIFY_SELF, changed_types)); | 224 P2PNotificationData("sender2", NOTIFY_SELF, changed_types)); |
| 225 | 225 |
| 226 ReflectSentNotifications(); | 226 ReflectSentNotifications(); |
| 227 | 227 |
| 228 // Should be dropped. | 228 // Should be dropped. |
| 229 Mock::VerifyAndClearExpectations(&mock_observer_); | 229 Mock::VerifyAndClearExpectations(&mock_observer_); |
| 230 p2p_notifier_.SendNotificationDataForTest( | 230 p2p_notifier_.SendNotificationDataForTest( |
| 231 P2PNotificationData("sender", NOTIFY_SELF, syncer::ModelTypeSet())); | 231 P2PNotificationData("sender", NOTIFY_SELF, ModelTypeSet())); |
| 232 | 232 |
| 233 ReflectSentNotifications(); | 233 ReflectSentNotifications(); |
| 234 | 234 |
| 235 // Should be dropped. | 235 // Should be dropped. |
| 236 p2p_notifier_.SendNotificationDataForTest( | 236 p2p_notifier_.SendNotificationDataForTest( |
| 237 P2PNotificationData("sender", NOTIFY_OTHERS, changed_types)); | 237 P2PNotificationData("sender", NOTIFY_OTHERS, changed_types)); |
| 238 | 238 |
| 239 ReflectSentNotifications(); | 239 ReflectSentNotifications(); |
| 240 | 240 |
| 241 // Should be propagated. | 241 // Should be propagated. |
| 242 Mock::VerifyAndClearExpectations(&mock_observer_); | 242 Mock::VerifyAndClearExpectations(&mock_observer_); |
| 243 EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map, | 243 EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map, |
| 244 REMOTE_NOTIFICATION)); | 244 REMOTE_NOTIFICATION)); |
| 245 p2p_notifier_.SendNotificationDataForTest( | 245 p2p_notifier_.SendNotificationDataForTest( |
| 246 P2PNotificationData("sender2", NOTIFY_OTHERS, changed_types)); | 246 P2PNotificationData("sender2", NOTIFY_OTHERS, changed_types)); |
| 247 | 247 |
| 248 ReflectSentNotifications(); | 248 ReflectSentNotifications(); |
| 249 | 249 |
| 250 // Should be dropped. | 250 // Should be dropped. |
| 251 Mock::VerifyAndClearExpectations(&mock_observer_); | 251 Mock::VerifyAndClearExpectations(&mock_observer_); |
| 252 p2p_notifier_.SendNotificationDataForTest( | 252 p2p_notifier_.SendNotificationDataForTest( |
| 253 P2PNotificationData("sender2", NOTIFY_OTHERS, syncer::ModelTypeSet())); | 253 P2PNotificationData("sender2", NOTIFY_OTHERS, ModelTypeSet())); |
| 254 | 254 |
| 255 ReflectSentNotifications(); | 255 ReflectSentNotifications(); |
| 256 | 256 |
| 257 // Should be propagated. | 257 // Should be propagated. |
| 258 Mock::VerifyAndClearExpectations(&mock_observer_); | 258 Mock::VerifyAndClearExpectations(&mock_observer_); |
| 259 EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map, | 259 EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map, |
| 260 REMOTE_NOTIFICATION)); | 260 REMOTE_NOTIFICATION)); |
| 261 p2p_notifier_.SendNotificationDataForTest( | 261 p2p_notifier_.SendNotificationDataForTest( |
| 262 P2PNotificationData("sender", NOTIFY_ALL, changed_types)); | 262 P2PNotificationData("sender", NOTIFY_ALL, changed_types)); |
| 263 | 263 |
| 264 ReflectSentNotifications(); | 264 ReflectSentNotifications(); |
| 265 | 265 |
| 266 // Should be propagated. | 266 // Should be propagated. |
| 267 Mock::VerifyAndClearExpectations(&mock_observer_); | 267 Mock::VerifyAndClearExpectations(&mock_observer_); |
| 268 EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map, | 268 EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map, |
| 269 REMOTE_NOTIFICATION)); | 269 REMOTE_NOTIFICATION)); |
| 270 p2p_notifier_.SendNotificationDataForTest( | 270 p2p_notifier_.SendNotificationDataForTest( |
| 271 P2PNotificationData("sender2", NOTIFY_ALL, changed_types)); | 271 P2PNotificationData("sender2", NOTIFY_ALL, changed_types)); |
| 272 | 272 |
| 273 ReflectSentNotifications(); | 273 ReflectSentNotifications(); |
| 274 | 274 |
| 275 // Should be dropped. | 275 // Should be dropped. |
| 276 Mock::VerifyAndClearExpectations(&mock_observer_); | 276 Mock::VerifyAndClearExpectations(&mock_observer_); |
| 277 p2p_notifier_.SendNotificationDataForTest( | 277 p2p_notifier_.SendNotificationDataForTest( |
| 278 P2PNotificationData("sender2", NOTIFY_ALL, syncer::ModelTypeSet())); | 278 P2PNotificationData("sender2", NOTIFY_ALL, ModelTypeSet())); |
| 279 | 279 |
| 280 ReflectSentNotifications(); | 280 ReflectSentNotifications(); |
| 281 } | 281 } |
| 282 | 282 |
| 283 } // namespace | 283 } // namespace |
| 284 | 284 |
| 285 } // namespace syncer | 285 } // namespace syncer |
| OLD | NEW |