| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/sync/notifier/p2p_notifier.h" | 5 #include "chrome/browser/sync/notifier/p2p_notifier.h" |
| 6 | 6 |
| 7 #include <cstddef> | 7 #include <cstddef> |
| 8 | 8 |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 p2p_notifier_->AddObserver(&mock_observer_); | 68 p2p_notifier_->AddObserver(&mock_observer_); |
| 69 } | 69 } |
| 70 | 70 |
| 71 virtual void TearDown() { | 71 virtual void TearDown() { |
| 72 p2p_notifier_->RemoveObserver(&mock_observer_); | 72 p2p_notifier_->RemoveObserver(&mock_observer_); |
| 73 p2p_notifier_.reset(); | 73 p2p_notifier_.reset(); |
| 74 talk_mediator_ = NULL; | 74 talk_mediator_ = NULL; |
| 75 } | 75 } |
| 76 | 76 |
| 77 syncable::ModelTypePayloadMap MakePayloadMap( | 77 syncable::ModelTypePayloadMap MakePayloadMap( |
| 78 syncable::ModelEnumSet types) { | 78 syncable::ModelTypeSet types) { |
| 79 return syncable::ModelTypePayloadMapFromEnumSet(types, ""); | 79 return syncable::ModelTypePayloadMapFromEnumSet(types, ""); |
| 80 } | 80 } |
| 81 | 81 |
| 82 MessageLoop message_loop_; | 82 MessageLoop message_loop_; |
| 83 // Owned by |p2p_notifier_|. | 83 // Owned by |p2p_notifier_|. |
| 84 notifier::TalkMediator* talk_mediator_; | 84 notifier::TalkMediator* talk_mediator_; |
| 85 scoped_ptr<P2PNotifier> p2p_notifier_; | 85 scoped_ptr<P2PNotifier> p2p_notifier_; |
| 86 StrictMock<MockSyncNotifierObserver> mock_observer_; | 86 StrictMock<MockSyncNotifierObserver> mock_observer_; |
| 87 }; | 87 }; |
| 88 | 88 |
| 89 TEST_F(P2PNotifierTest, P2PNotificationTarget) { | 89 TEST_F(P2PNotifierTest, P2PNotificationTarget) { |
| 90 for (int i = FIRST_NOTIFICATION_TARGET; | 90 for (int i = FIRST_NOTIFICATION_TARGET; |
| 91 i <= LAST_NOTIFICATION_TARGET; ++i) { | 91 i <= LAST_NOTIFICATION_TARGET; ++i) { |
| 92 P2PNotificationTarget target = static_cast<P2PNotificationTarget>(i); | 92 P2PNotificationTarget target = static_cast<P2PNotificationTarget>(i); |
| 93 const std::string& target_str = P2PNotificationTargetToString(target); | 93 const std::string& target_str = P2PNotificationTargetToString(target); |
| 94 EXPECT_FALSE(target_str.empty()); | 94 EXPECT_FALSE(target_str.empty()); |
| 95 EXPECT_EQ(target, P2PNotificationTargetFromString(target_str)); | 95 EXPECT_EQ(target, P2PNotificationTargetFromString(target_str)); |
| 96 } | 96 } |
| 97 EXPECT_EQ(NOTIFY_SELF, P2PNotificationTargetFromString("unknown")); | 97 EXPECT_EQ(NOTIFY_SELF, P2PNotificationTargetFromString("unknown")); |
| 98 } | 98 } |
| 99 | 99 |
| 100 TEST_F(P2PNotifierTest, P2PNotificationDataIsTargeted) { | 100 TEST_F(P2PNotifierTest, P2PNotificationDataIsTargeted) { |
| 101 { | 101 { |
| 102 const P2PNotificationData notification_data( | 102 const P2PNotificationData notification_data( |
| 103 "sender", NOTIFY_SELF, syncable::ModelEnumSet()); | 103 "sender", NOTIFY_SELF, syncable::ModelTypeSet()); |
| 104 EXPECT_TRUE(notification_data.IsTargeted("sender")); | 104 EXPECT_TRUE(notification_data.IsTargeted("sender")); |
| 105 EXPECT_FALSE(notification_data.IsTargeted("other1")); | 105 EXPECT_FALSE(notification_data.IsTargeted("other1")); |
| 106 EXPECT_FALSE(notification_data.IsTargeted("other2")); | 106 EXPECT_FALSE(notification_data.IsTargeted("other2")); |
| 107 } | 107 } |
| 108 { | 108 { |
| 109 const P2PNotificationData notification_data( | 109 const P2PNotificationData notification_data( |
| 110 "sender", NOTIFY_OTHERS, syncable::ModelEnumSet()); | 110 "sender", NOTIFY_OTHERS, syncable::ModelTypeSet()); |
| 111 EXPECT_FALSE(notification_data.IsTargeted("sender")); | 111 EXPECT_FALSE(notification_data.IsTargeted("sender")); |
| 112 EXPECT_TRUE(notification_data.IsTargeted("other1")); | 112 EXPECT_TRUE(notification_data.IsTargeted("other1")); |
| 113 EXPECT_TRUE(notification_data.IsTargeted("other2")); | 113 EXPECT_TRUE(notification_data.IsTargeted("other2")); |
| 114 } | 114 } |
| 115 { | 115 { |
| 116 const P2PNotificationData notification_data( | 116 const P2PNotificationData notification_data( |
| 117 "sender", NOTIFY_ALL, syncable::ModelEnumSet()); | 117 "sender", NOTIFY_ALL, syncable::ModelTypeSet()); |
| 118 EXPECT_TRUE(notification_data.IsTargeted("sender")); | 118 EXPECT_TRUE(notification_data.IsTargeted("sender")); |
| 119 EXPECT_TRUE(notification_data.IsTargeted("other1")); | 119 EXPECT_TRUE(notification_data.IsTargeted("other1")); |
| 120 EXPECT_TRUE(notification_data.IsTargeted("other2")); | 120 EXPECT_TRUE(notification_data.IsTargeted("other2")); |
| 121 } | 121 } |
| 122 } | 122 } |
| 123 | 123 |
| 124 TEST_F(P2PNotifierTest, P2PNotificationDataDefault) { | 124 TEST_F(P2PNotifierTest, P2PNotificationDataDefault) { |
| 125 const P2PNotificationData notification_data; | 125 const P2PNotificationData notification_data; |
| 126 EXPECT_TRUE(notification_data.IsTargeted("")); | 126 EXPECT_TRUE(notification_data.IsTargeted("")); |
| 127 EXPECT_FALSE(notification_data.IsTargeted("other1")); | 127 EXPECT_FALSE(notification_data.IsTargeted("other1")); |
| 128 EXPECT_FALSE(notification_data.IsTargeted("other2")); | 128 EXPECT_FALSE(notification_data.IsTargeted("other2")); |
| 129 EXPECT_TRUE(notification_data.GetChangedTypes().Empty()); | 129 EXPECT_TRUE(notification_data.GetChangedTypes().Empty()); |
| 130 const std::string& notification_data_str = notification_data.ToString(); | 130 const std::string& notification_data_str = notification_data.ToString(); |
| 131 EXPECT_EQ( | 131 EXPECT_EQ( |
| 132 "{\"changedTypes\":[],\"notificationType\":\"notifySelf\"," | 132 "{\"changedTypes\":[],\"notificationType\":\"notifySelf\"," |
| 133 "\"senderId\":\"\"}", notification_data_str); | 133 "\"senderId\":\"\"}", notification_data_str); |
| 134 | 134 |
| 135 P2PNotificationData notification_data_parsed; | 135 P2PNotificationData notification_data_parsed; |
| 136 EXPECT_TRUE(notification_data_parsed.ResetFromString(notification_data_str)); | 136 EXPECT_TRUE(notification_data_parsed.ResetFromString(notification_data_str)); |
| 137 EXPECT_TRUE(notification_data.Equals(notification_data_parsed)); | 137 EXPECT_TRUE(notification_data.Equals(notification_data_parsed)); |
| 138 } | 138 } |
| 139 | 139 |
| 140 TEST_F(P2PNotifierTest, P2PNotificationDataNonDefault) { | 140 TEST_F(P2PNotifierTest, P2PNotificationDataNonDefault) { |
| 141 const syncable::ModelEnumSet changed_types( | 141 const syncable::ModelTypeSet changed_types( |
| 142 syncable::BOOKMARKS, syncable::THEMES); | 142 syncable::BOOKMARKS, syncable::THEMES); |
| 143 const P2PNotificationData notification_data( | 143 const P2PNotificationData notification_data( |
| 144 "sender", NOTIFY_ALL, changed_types); | 144 "sender", NOTIFY_ALL, changed_types); |
| 145 EXPECT_TRUE(notification_data.IsTargeted("sender")); | 145 EXPECT_TRUE(notification_data.IsTargeted("sender")); |
| 146 EXPECT_TRUE(notification_data.IsTargeted("other1")); | 146 EXPECT_TRUE(notification_data.IsTargeted("other1")); |
| 147 EXPECT_TRUE(notification_data.IsTargeted("other2")); | 147 EXPECT_TRUE(notification_data.IsTargeted("other2")); |
| 148 EXPECT_TRUE(notification_data.GetChangedTypes().Equals(changed_types)); | 148 EXPECT_TRUE(notification_data.GetChangedTypes().Equals(changed_types)); |
| 149 const std::string& notification_data_str = notification_data.ToString(); | 149 const std::string& notification_data_str = notification_data.ToString(); |
| 150 EXPECT_EQ( | 150 EXPECT_EQ( |
| 151 "{\"changedTypes\":[\"Bookmarks\",\"Themes\"]," | 151 "{\"changedTypes\":[\"Bookmarks\",\"Themes\"]," |
| 152 "\"notificationType\":\"notifyAll\"," | 152 "\"notificationType\":\"notifyAll\"," |
| 153 "\"senderId\":\"sender\"}", notification_data_str); | 153 "\"senderId\":\"sender\"}", notification_data_str); |
| 154 | 154 |
| 155 P2PNotificationData notification_data_parsed; | 155 P2PNotificationData notification_data_parsed; |
| 156 EXPECT_TRUE(notification_data_parsed.ResetFromString(notification_data_str)); | 156 EXPECT_TRUE(notification_data_parsed.ResetFromString(notification_data_str)); |
| 157 EXPECT_TRUE(notification_data.Equals(notification_data_parsed)); | 157 EXPECT_TRUE(notification_data.Equals(notification_data_parsed)); |
| 158 } | 158 } |
| 159 | 159 |
| 160 TEST_F(P2PNotifierTest, NotificationsBasic) { | 160 TEST_F(P2PNotifierTest, NotificationsBasic) { |
| 161 syncable::ModelEnumSet enabled_types( | 161 syncable::ModelTypeSet enabled_types( |
| 162 syncable::BOOKMARKS, syncable::PREFERENCES); | 162 syncable::BOOKMARKS, syncable::PREFERENCES); |
| 163 | 163 |
| 164 EXPECT_CALL(mock_observer_, OnNotificationStateChange(true)); | 164 EXPECT_CALL(mock_observer_, OnNotificationStateChange(true)); |
| 165 EXPECT_CALL(mock_observer_, | 165 EXPECT_CALL(mock_observer_, |
| 166 OnIncomingNotification(MakePayloadMap(enabled_types))); | 166 OnIncomingNotification(MakePayloadMap(enabled_types))); |
| 167 | 167 |
| 168 p2p_notifier_->SetUniqueId("sender"); | 168 p2p_notifier_->SetUniqueId("sender"); |
| 169 p2p_notifier_->UpdateCredentials("foo@bar.com", "fake_token"); | 169 p2p_notifier_->UpdateCredentials("foo@bar.com", "fake_token"); |
| 170 p2p_notifier_->UpdateEnabledTypes(enabled_types); | 170 p2p_notifier_->UpdateEnabledTypes(enabled_types); |
| 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 syncable::ModelEnumSet changed_types( | 174 syncable::ModelTypeSet changed_types( |
| 175 syncable::THEMES, syncable::APPS); | 175 syncable::THEMES, syncable::APPS); |
| 176 p2p_notifier_->SendNotification(changed_types); | 176 p2p_notifier_->SendNotification(changed_types); |
| 177 } | 177 } |
| 178 } | 178 } |
| 179 | 179 |
| 180 TEST_F(P2PNotifierTest, SendNotificationData) { | 180 TEST_F(P2PNotifierTest, SendNotificationData) { |
| 181 syncable::ModelEnumSet enabled_types( | 181 syncable::ModelTypeSet enabled_types( |
| 182 syncable::BOOKMARKS, syncable::PREFERENCES); | 182 syncable::BOOKMARKS, syncable::PREFERENCES); |
| 183 | 183 |
| 184 syncable::ModelEnumSet changed_types( | 184 syncable::ModelTypeSet changed_types( |
| 185 syncable::THEMES, syncable::APPS); | 185 syncable::THEMES, syncable::APPS); |
| 186 | 186 |
| 187 const syncable::ModelTypePayloadMap& changed_payload_map = | 187 const syncable::ModelTypePayloadMap& changed_payload_map = |
| 188 MakePayloadMap(changed_types); | 188 MakePayloadMap(changed_types); |
| 189 | 189 |
| 190 EXPECT_CALL(mock_observer_, OnNotificationStateChange(true)); | 190 EXPECT_CALL(mock_observer_, OnNotificationStateChange(true)); |
| 191 EXPECT_CALL(mock_observer_, | 191 EXPECT_CALL(mock_observer_, |
| 192 OnIncomingNotification(MakePayloadMap(enabled_types))); | 192 OnIncomingNotification(MakePayloadMap(enabled_types))); |
| 193 | 193 |
| 194 p2p_notifier_->SetUniqueId("sender"); | 194 p2p_notifier_->SetUniqueId("sender"); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 206 P2PNotificationData("sender", NOTIFY_SELF, changed_types)); | 206 P2PNotificationData("sender", NOTIFY_SELF, changed_types)); |
| 207 | 207 |
| 208 // Should be dropped. | 208 // Should be dropped. |
| 209 Mock::VerifyAndClearExpectations(&mock_observer_); | 209 Mock::VerifyAndClearExpectations(&mock_observer_); |
| 210 p2p_notifier_->SendNotificationDataForTest( | 210 p2p_notifier_->SendNotificationDataForTest( |
| 211 P2PNotificationData("sender2", NOTIFY_SELF, changed_types)); | 211 P2PNotificationData("sender2", NOTIFY_SELF, changed_types)); |
| 212 | 212 |
| 213 // Should be dropped. | 213 // Should be dropped. |
| 214 Mock::VerifyAndClearExpectations(&mock_observer_); | 214 Mock::VerifyAndClearExpectations(&mock_observer_); |
| 215 p2p_notifier_->SendNotificationDataForTest( | 215 p2p_notifier_->SendNotificationDataForTest( |
| 216 P2PNotificationData("sender", NOTIFY_SELF, syncable::ModelEnumSet())); | 216 P2PNotificationData("sender", NOTIFY_SELF, syncable::ModelTypeSet())); |
| 217 | 217 |
| 218 // Should be dropped. | 218 // Should be dropped. |
| 219 p2p_notifier_->SendNotificationDataForTest( | 219 p2p_notifier_->SendNotificationDataForTest( |
| 220 P2PNotificationData("sender", NOTIFY_OTHERS, changed_types)); | 220 P2PNotificationData("sender", NOTIFY_OTHERS, changed_types)); |
| 221 | 221 |
| 222 // Should be propagated. | 222 // Should be propagated. |
| 223 Mock::VerifyAndClearExpectations(&mock_observer_); | 223 Mock::VerifyAndClearExpectations(&mock_observer_); |
| 224 EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map)); | 224 EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map)); |
| 225 p2p_notifier_->SendNotificationDataForTest( | 225 p2p_notifier_->SendNotificationDataForTest( |
| 226 P2PNotificationData("sender2", NOTIFY_OTHERS, changed_types)); | 226 P2PNotificationData("sender2", NOTIFY_OTHERS, changed_types)); |
| 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_OTHERS, syncable::ModelEnumSet())); | 231 P2PNotificationData("sender2", NOTIFY_OTHERS, syncable::ModelTypeSet())); |
| 232 | 232 |
| 233 // Should be propagated. | 233 // Should be propagated. |
| 234 Mock::VerifyAndClearExpectations(&mock_observer_); | 234 Mock::VerifyAndClearExpectations(&mock_observer_); |
| 235 EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map)); | 235 EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map)); |
| 236 p2p_notifier_->SendNotificationDataForTest( | 236 p2p_notifier_->SendNotificationDataForTest( |
| 237 P2PNotificationData("sender", NOTIFY_ALL, changed_types)); | 237 P2PNotificationData("sender", NOTIFY_ALL, changed_types)); |
| 238 | 238 |
| 239 // Should be propagated. | 239 // Should be propagated. |
| 240 Mock::VerifyAndClearExpectations(&mock_observer_); | 240 Mock::VerifyAndClearExpectations(&mock_observer_); |
| 241 EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map)); | 241 EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map)); |
| 242 p2p_notifier_->SendNotificationDataForTest( | 242 p2p_notifier_->SendNotificationDataForTest( |
| 243 P2PNotificationData("sender2", NOTIFY_ALL, changed_types)); | 243 P2PNotificationData("sender2", NOTIFY_ALL, changed_types)); |
| 244 | 244 |
| 245 // Should be dropped. | 245 // Should be dropped. |
| 246 Mock::VerifyAndClearExpectations(&mock_observer_); | 246 Mock::VerifyAndClearExpectations(&mock_observer_); |
| 247 p2p_notifier_->SendNotificationDataForTest( | 247 p2p_notifier_->SendNotificationDataForTest( |
| 248 P2PNotificationData("sender2", NOTIFY_ALL, syncable::ModelEnumSet())); | 248 P2PNotificationData("sender2", NOTIFY_ALL, syncable::ModelTypeSet())); |
| 249 } | 249 } |
| 250 | 250 |
| 251 } // namespace | 251 } // namespace |
| 252 | 252 |
| 253 } // namespace sync_notifier | 253 } // namespace sync_notifier |
| OLD | NEW |