| 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" |
| 11 #include "sync/internal_api/public/base/model_type_payload_map.h" | 11 #include "sync/internal_api/public/base/model_type_state_map.h" |
| 12 #include "sync/notifier/mock_sync_notifier_observer.h" | 12 #include "sync/notifier/mock_sync_notifier_observer.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 14 |
| 15 namespace syncer { | 15 namespace syncer { |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 using ::testing::_; | 19 using ::testing::_; |
| 20 using ::testing::Mock; | 20 using ::testing::Mock; |
| 21 using ::testing::StrictMock; | 21 using ::testing::StrictMock; |
| 22 | 22 |
| 23 class P2PNotifierTest : public testing::Test { | 23 class P2PNotifierTest : public testing::Test { |
| 24 protected: | 24 protected: |
| 25 P2PNotifierTest() | 25 P2PNotifierTest() |
| 26 : fake_push_client_(new notifier::FakePushClient()), | 26 : fake_push_client_(new notifier::FakePushClient()), |
| 27 p2p_notifier_( | 27 p2p_notifier_( |
| 28 scoped_ptr<notifier::PushClient>(fake_push_client_), | 28 scoped_ptr<notifier::PushClient>(fake_push_client_), |
| 29 NOTIFY_OTHERS), | 29 NOTIFY_OTHERS), |
| 30 next_sent_notification_to_reflect_(0) { | 30 next_sent_notification_to_reflect_(0) { |
| 31 p2p_notifier_.RegisterHandler(&mock_observer_); | 31 p2p_notifier_.RegisterHandler(&mock_observer_); |
| 32 } | 32 } |
| 33 | 33 |
| 34 virtual ~P2PNotifierTest() { | 34 virtual ~P2PNotifierTest() { |
| 35 p2p_notifier_.UnregisterHandler(&mock_observer_); | 35 p2p_notifier_.UnregisterHandler(&mock_observer_); |
| 36 } | 36 } |
| 37 | 37 |
| 38 ModelTypePayloadMap MakePayloadMap(ModelTypeSet types) { | 38 ModelTypeStateMap MakeStateMap(ModelTypeSet types) { |
| 39 return ModelTypePayloadMapFromEnumSet(types, std::string()); | 39 return ModelTypeStateMapFromEnumSet(types, std::string()); |
| 40 } | 40 } |
| 41 | 41 |
| 42 // Simulate receiving all the notifications we sent out since last | 42 // Simulate receiving all the notifications we sent out since last |
| 43 // time this was called. | 43 // time this was called. |
| 44 void ReflectSentNotifications() { | 44 void ReflectSentNotifications() { |
| 45 const std::vector<notifier::Notification>& sent_notifications = | 45 const std::vector<notifier::Notification>& sent_notifications = |
| 46 fake_push_client_->sent_notifications(); | 46 fake_push_client_->sent_notifications(); |
| 47 for(size_t i = next_sent_notification_to_reflect_; | 47 for(size_t i = next_sent_notification_to_reflect_; |
| 48 i < sent_notifications.size(); ++i) { | 48 i < sent_notifications.size(); ++i) { |
| 49 p2p_notifier_.OnIncomingNotification(sent_notifications[i]); | 49 p2p_notifier_.OnIncomingNotification(sent_notifications[i]); |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 | 138 |
| 139 // Set up the P2PNotifier, simulate a successful connection, and send | 139 // Set up the P2PNotifier, simulate a successful connection, and send |
| 140 // a notification with the default target (NOTIFY_OTHERS). The | 140 // a notification with the default target (NOTIFY_OTHERS). The |
| 141 // observer should receive only a notification from the call to | 141 // observer should receive only a notification from the call to |
| 142 // UpdateEnabledTypes(). | 142 // UpdateEnabledTypes(). |
| 143 TEST_F(P2PNotifierTest, NotificationsBasic) { | 143 TEST_F(P2PNotifierTest, NotificationsBasic) { |
| 144 const ModelTypeSet enabled_types(BOOKMARKS, PREFERENCES); | 144 const ModelTypeSet enabled_types(BOOKMARKS, PREFERENCES); |
| 145 | 145 |
| 146 EXPECT_CALL(mock_observer_, OnNotificationsEnabled()); | 146 EXPECT_CALL(mock_observer_, OnNotificationsEnabled()); |
| 147 EXPECT_CALL(mock_observer_, OnIncomingNotification( | 147 EXPECT_CALL(mock_observer_, OnIncomingNotification( |
| 148 ModelTypePayloadMapToObjectIdPayloadMap(MakePayloadMap(enabled_types)), | 148 ModelTypeStateMapToObjectIdStateMap(MakeStateMap(enabled_types)), |
| 149 REMOTE_NOTIFICATION)); | 149 REMOTE_NOTIFICATION)); |
| 150 | 150 |
| 151 p2p_notifier_.UpdateRegisteredIds(&mock_observer_, | 151 p2p_notifier_.UpdateRegisteredIds(&mock_observer_, |
| 152 ModelTypeSetToObjectIdSet(enabled_types)); | 152 ModelTypeSetToObjectIdSet(enabled_types)); |
| 153 | 153 |
| 154 p2p_notifier_.SetUniqueId("sender"); | 154 p2p_notifier_.SetUniqueId("sender"); |
| 155 | 155 |
| 156 const char kEmail[] = "foo@bar.com"; | 156 const char kEmail[] = "foo@bar.com"; |
| 157 const char kToken[] = "token"; | 157 const char kToken[] = "token"; |
| 158 p2p_notifier_.UpdateCredentials(kEmail, kToken); | 158 p2p_notifier_.UpdateCredentials(kEmail, kToken); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 184 // target settings. The notifications received by the observer should | 184 // target settings. The notifications received by the observer should |
| 185 // be consistent with the target settings. | 185 // be consistent with the target settings. |
| 186 TEST_F(P2PNotifierTest, SendNotificationData) { | 186 TEST_F(P2PNotifierTest, SendNotificationData) { |
| 187 const ModelTypeSet enabled_types(BOOKMARKS, PREFERENCES, THEMES); | 187 const ModelTypeSet enabled_types(BOOKMARKS, PREFERENCES, THEMES); |
| 188 const ModelTypeSet changed_types(THEMES, APPS); | 188 const ModelTypeSet changed_types(THEMES, APPS); |
| 189 const ModelTypeSet expected_types(THEMES); | 189 const ModelTypeSet expected_types(THEMES); |
| 190 | 190 |
| 191 EXPECT_CALL(mock_observer_, OnNotificationsEnabled()); | 191 EXPECT_CALL(mock_observer_, OnNotificationsEnabled()); |
| 192 EXPECT_CALL(mock_observer_, | 192 EXPECT_CALL(mock_observer_, |
| 193 OnIncomingNotification( | 193 OnIncomingNotification( |
| 194 ModelTypePayloadMapToObjectIdPayloadMap( | 194 ModelTypeStateMapToObjectIdStateMap( |
| 195 MakePayloadMap(enabled_types)), | 195 MakeStateMap(enabled_types)), |
| 196 REMOTE_NOTIFICATION)); | 196 REMOTE_NOTIFICATION)); |
| 197 | 197 |
| 198 p2p_notifier_.UpdateRegisteredIds(&mock_observer_, | 198 p2p_notifier_.UpdateRegisteredIds(&mock_observer_, |
| 199 ModelTypeSetToObjectIdSet(enabled_types)); | 199 ModelTypeSetToObjectIdSet(enabled_types)); |
| 200 | 200 |
| 201 const ModelTypePayloadMap& expected_payload_map = | 201 const ModelTypeStateMap& expected_state_map = |
| 202 MakePayloadMap(expected_types); | 202 MakeStateMap(expected_types); |
| 203 | 203 |
| 204 p2p_notifier_.SetUniqueId("sender"); | 204 p2p_notifier_.SetUniqueId("sender"); |
| 205 p2p_notifier_.UpdateCredentials("foo@bar.com", "fake_token"); | 205 p2p_notifier_.UpdateCredentials("foo@bar.com", "fake_token"); |
| 206 | 206 |
| 207 ReflectSentNotifications(); | 207 ReflectSentNotifications(); |
| 208 fake_push_client_->EnableNotifications(); | 208 fake_push_client_->EnableNotifications(); |
| 209 | 209 |
| 210 ReflectSentNotifications(); | 210 ReflectSentNotifications(); |
| 211 | 211 |
| 212 // Should be dropped. | 212 // Should be dropped. |
| 213 Mock::VerifyAndClearExpectations(&mock_observer_); | 213 Mock::VerifyAndClearExpectations(&mock_observer_); |
| 214 p2p_notifier_.SendNotificationDataForTest(P2PNotificationData()); | 214 p2p_notifier_.SendNotificationDataForTest(P2PNotificationData()); |
| 215 | 215 |
| 216 ReflectSentNotifications(); | 216 ReflectSentNotifications(); |
| 217 | 217 |
| 218 // Should be propagated. | 218 // Should be propagated. |
| 219 Mock::VerifyAndClearExpectations(&mock_observer_); | 219 Mock::VerifyAndClearExpectations(&mock_observer_); |
| 220 EXPECT_CALL(mock_observer_, OnIncomingNotification( | 220 EXPECT_CALL(mock_observer_, OnIncomingNotification( |
| 221 ModelTypePayloadMapToObjectIdPayloadMap(expected_payload_map), | 221 ModelTypeStateMapToObjectIdStateMap(expected_state_map), |
| 222 REMOTE_NOTIFICATION)); | 222 REMOTE_NOTIFICATION)); |
| 223 p2p_notifier_.SendNotificationDataForTest( | 223 p2p_notifier_.SendNotificationDataForTest( |
| 224 P2PNotificationData("sender", NOTIFY_SELF, changed_types)); | 224 P2PNotificationData("sender", 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("sender2", NOTIFY_SELF, changed_types)); | 231 P2PNotificationData("sender2", NOTIFY_SELF, changed_types)); |
| 232 | 232 |
| 233 ReflectSentNotifications(); | 233 ReflectSentNotifications(); |
| 234 | 234 |
| 235 // Should be dropped. | 235 // Should be dropped. |
| 236 Mock::VerifyAndClearExpectations(&mock_observer_); | 236 Mock::VerifyAndClearExpectations(&mock_observer_); |
| 237 p2p_notifier_.SendNotificationDataForTest( | 237 p2p_notifier_.SendNotificationDataForTest( |
| 238 P2PNotificationData("sender", NOTIFY_SELF, ModelTypeSet())); | 238 P2PNotificationData("sender", NOTIFY_SELF, ModelTypeSet())); |
| 239 | 239 |
| 240 ReflectSentNotifications(); | 240 ReflectSentNotifications(); |
| 241 | 241 |
| 242 // Should be dropped. | 242 // Should be dropped. |
| 243 p2p_notifier_.SendNotificationDataForTest( | 243 p2p_notifier_.SendNotificationDataForTest( |
| 244 P2PNotificationData("sender", NOTIFY_OTHERS, changed_types)); | 244 P2PNotificationData("sender", NOTIFY_OTHERS, changed_types)); |
| 245 | 245 |
| 246 ReflectSentNotifications(); | 246 ReflectSentNotifications(); |
| 247 | 247 |
| 248 // Should be propagated. | 248 // Should be propagated. |
| 249 Mock::VerifyAndClearExpectations(&mock_observer_); | 249 Mock::VerifyAndClearExpectations(&mock_observer_); |
| 250 EXPECT_CALL(mock_observer_, OnIncomingNotification( | 250 EXPECT_CALL(mock_observer_, OnIncomingNotification( |
| 251 ModelTypePayloadMapToObjectIdPayloadMap(expected_payload_map), | 251 ModelTypeStateMapToObjectIdStateMap(expected_state_map), |
| 252 REMOTE_NOTIFICATION)); | 252 REMOTE_NOTIFICATION)); |
| 253 p2p_notifier_.SendNotificationDataForTest( | 253 p2p_notifier_.SendNotificationDataForTest( |
| 254 P2PNotificationData("sender2", NOTIFY_OTHERS, changed_types)); | 254 P2PNotificationData("sender2", NOTIFY_OTHERS, changed_types)); |
| 255 | 255 |
| 256 ReflectSentNotifications(); | 256 ReflectSentNotifications(); |
| 257 | 257 |
| 258 // Should be dropped. | 258 // Should be dropped. |
| 259 Mock::VerifyAndClearExpectations(&mock_observer_); | 259 Mock::VerifyAndClearExpectations(&mock_observer_); |
| 260 p2p_notifier_.SendNotificationDataForTest( | 260 p2p_notifier_.SendNotificationDataForTest( |
| 261 P2PNotificationData("sender2", NOTIFY_OTHERS, ModelTypeSet())); | 261 P2PNotificationData("sender2", NOTIFY_OTHERS, ModelTypeSet())); |
| 262 | 262 |
| 263 ReflectSentNotifications(); | 263 ReflectSentNotifications(); |
| 264 | 264 |
| 265 // Should be propagated. | 265 // Should be propagated. |
| 266 Mock::VerifyAndClearExpectations(&mock_observer_); | 266 Mock::VerifyAndClearExpectations(&mock_observer_); |
| 267 EXPECT_CALL(mock_observer_, OnIncomingNotification( | 267 EXPECT_CALL(mock_observer_, OnIncomingNotification( |
| 268 ModelTypePayloadMapToObjectIdPayloadMap(expected_payload_map), | 268 ModelTypeStateMapToObjectIdStateMap(expected_state_map), |
| 269 REMOTE_NOTIFICATION)); | 269 REMOTE_NOTIFICATION)); |
| 270 p2p_notifier_.SendNotificationDataForTest( | 270 p2p_notifier_.SendNotificationDataForTest( |
| 271 P2PNotificationData("sender", NOTIFY_ALL, changed_types)); | 271 P2PNotificationData("sender", NOTIFY_ALL, changed_types)); |
| 272 | 272 |
| 273 ReflectSentNotifications(); | 273 ReflectSentNotifications(); |
| 274 | 274 |
| 275 // Should be propagated. | 275 // Should be propagated. |
| 276 Mock::VerifyAndClearExpectations(&mock_observer_); | 276 Mock::VerifyAndClearExpectations(&mock_observer_); |
| 277 EXPECT_CALL(mock_observer_, OnIncomingNotification( | 277 EXPECT_CALL(mock_observer_, OnIncomingNotification( |
| 278 ModelTypePayloadMapToObjectIdPayloadMap(expected_payload_map), | 278 ModelTypeStateMapToObjectIdStateMap(expected_state_map), |
| 279 REMOTE_NOTIFICATION)); | 279 REMOTE_NOTIFICATION)); |
| 280 p2p_notifier_.SendNotificationDataForTest( | 280 p2p_notifier_.SendNotificationDataForTest( |
| 281 P2PNotificationData("sender2", NOTIFY_ALL, changed_types)); | 281 P2PNotificationData("sender2", NOTIFY_ALL, changed_types)); |
| 282 | 282 |
| 283 ReflectSentNotifications(); | 283 ReflectSentNotifications(); |
| 284 | 284 |
| 285 // Should be dropped. | 285 // Should be dropped. |
| 286 Mock::VerifyAndClearExpectations(&mock_observer_); | 286 Mock::VerifyAndClearExpectations(&mock_observer_); |
| 287 p2p_notifier_.SendNotificationDataForTest( | 287 p2p_notifier_.SendNotificationDataForTest( |
| 288 P2PNotificationData("sender2", NOTIFY_ALL, ModelTypeSet())); | 288 P2PNotificationData("sender2", NOTIFY_ALL, ModelTypeSet())); |
| 289 | 289 |
| 290 ReflectSentNotifications(); | 290 ReflectSentNotifications(); |
| 291 } | 291 } |
| 292 | 292 |
| 293 } // namespace | 293 } // namespace |
| 294 | 294 |
| 295 } // namespace syncer | 295 } // namespace syncer |
| OLD | NEW |