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