OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/sync/notifier/p2p_notifier.h" |
| 6 |
| 7 #include <cstddef> |
| 8 |
| 9 #include "base/compiler_specific.h" |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/message_loop.h" |
| 12 #include "chrome/browser/sync/notifier/mock_sync_notifier_observer.h" |
| 13 #include "chrome/browser/sync/syncable/model_type.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 namespace sync_notifier { |
| 17 |
| 18 namespace { |
| 19 |
| 20 using ::testing::_; |
| 21 using ::testing::Mock; |
| 22 using ::testing::StrictMock; |
| 23 |
| 24 class FakeTalkMediator : public notifier::TalkMediator { |
| 25 public: |
| 26 FakeTalkMediator() : delegate_(NULL) {} |
| 27 virtual ~FakeTalkMediator() {} |
| 28 |
| 29 // notifier::TalkMediator implementation. |
| 30 virtual void SetDelegate(Delegate* delegate) OVERRIDE { |
| 31 delegate_ = delegate; |
| 32 } |
| 33 virtual void SetAuthToken(const std::string& email, |
| 34 const std::string& token, |
| 35 const std::string& token_service) OVERRIDE {} |
| 36 virtual bool Login() OVERRIDE { |
| 37 if (delegate_) { |
| 38 delegate_->OnNotificationStateChange(true /* notifications_enabled */); |
| 39 } |
| 40 return true; |
| 41 } |
| 42 virtual bool Logout() OVERRIDE { |
| 43 if (delegate_) { |
| 44 delegate_->OnNotificationStateChange(false /* notifiations_enabled */); |
| 45 } |
| 46 return true; |
| 47 } |
| 48 virtual void SendNotification(const notifier::Notification& data) OVERRIDE { |
| 49 if (delegate_) { |
| 50 delegate_->OnOutgoingNotification(); |
| 51 delegate_->OnIncomingNotification(data); |
| 52 } |
| 53 } |
| 54 virtual void AddSubscription( |
| 55 const notifier::Subscription& subscription) OVERRIDE {} |
| 56 |
| 57 private: |
| 58 Delegate* delegate_; |
| 59 }; |
| 60 |
| 61 class P2PNotifierTest : public testing::Test { |
| 62 protected: |
| 63 P2PNotifierTest() : talk_mediator_(NULL) {} |
| 64 |
| 65 virtual void SetUp() { |
| 66 talk_mediator_ = new FakeTalkMediator(); |
| 67 p2p_notifier_.reset(new P2PNotifier(talk_mediator_)); |
| 68 p2p_notifier_->AddObserver(&mock_observer_); |
| 69 } |
| 70 |
| 71 virtual void TearDown() { |
| 72 p2p_notifier_->RemoveObserver(&mock_observer_); |
| 73 p2p_notifier_.reset(); |
| 74 talk_mediator_ = NULL; |
| 75 } |
| 76 |
| 77 syncable::ModelTypePayloadMap MakePayloadMap( |
| 78 const syncable::ModelTypeSet& types) { |
| 79 return syncable::ModelTypePayloadMapFromBitSet( |
| 80 syncable::ModelTypeBitSetFromSet(types), ""); |
| 81 } |
| 82 |
| 83 MessageLoop message_loop_; |
| 84 // Owned by |p2p_notifier_|. |
| 85 notifier::TalkMediator* talk_mediator_; |
| 86 scoped_ptr<P2PNotifier> p2p_notifier_; |
| 87 StrictMock<MockSyncNotifierObserver> mock_observer_; |
| 88 }; |
| 89 |
| 90 TEST_F(P2PNotifierTest, P2PNotificationTarget) { |
| 91 for (int i = FIRST_NOTIFICATION_TARGET; |
| 92 i <= LAST_NOTIFICATION_TARGET; ++i) { |
| 93 P2PNotificationTarget target = static_cast<P2PNotificationTarget>(i); |
| 94 const std::string& target_str = P2PNotificationTargetToString(target); |
| 95 EXPECT_FALSE(target_str.empty()); |
| 96 EXPECT_EQ(target, P2PNotificationTargetFromString(target_str)); |
| 97 } |
| 98 EXPECT_EQ(NOTIFY_SELF, P2PNotificationTargetFromString("unknown")); |
| 99 } |
| 100 |
| 101 TEST_F(P2PNotifierTest, P2PNotificationDataIsTargeted) { |
| 102 { |
| 103 const P2PNotificationData notification_data( |
| 104 "sender", NOTIFY_SELF, syncable::ModelTypeSet()); |
| 105 EXPECT_TRUE(notification_data.IsTargeted("sender")); |
| 106 EXPECT_FALSE(notification_data.IsTargeted("other1")); |
| 107 EXPECT_FALSE(notification_data.IsTargeted("other2")); |
| 108 } |
| 109 { |
| 110 const P2PNotificationData notification_data( |
| 111 "sender", NOTIFY_OTHERS, syncable::ModelTypeSet()); |
| 112 EXPECT_FALSE(notification_data.IsTargeted("sender")); |
| 113 EXPECT_TRUE(notification_data.IsTargeted("other1")); |
| 114 EXPECT_TRUE(notification_data.IsTargeted("other2")); |
| 115 } |
| 116 { |
| 117 const P2PNotificationData notification_data( |
| 118 "sender", NOTIFY_ALL, syncable::ModelTypeSet()); |
| 119 EXPECT_TRUE(notification_data.IsTargeted("sender")); |
| 120 EXPECT_TRUE(notification_data.IsTargeted("other1")); |
| 121 EXPECT_TRUE(notification_data.IsTargeted("other2")); |
| 122 } |
| 123 } |
| 124 |
| 125 TEST_F(P2PNotifierTest, P2PNotificationDataDefault) { |
| 126 const P2PNotificationData notification_data; |
| 127 EXPECT_TRUE(notification_data.IsTargeted("")); |
| 128 EXPECT_FALSE(notification_data.IsTargeted("other1")); |
| 129 EXPECT_FALSE(notification_data.IsTargeted("other2")); |
| 130 EXPECT_TRUE(notification_data.GetChangedTypes().empty()); |
| 131 const std::string& notification_data_str = notification_data.ToString(); |
| 132 EXPECT_EQ( |
| 133 "{\"changedTypes\":[],\"notificationType\":\"notifySelf\"," |
| 134 "\"senderId\":\"\"}", notification_data_str); |
| 135 |
| 136 P2PNotificationData notification_data_parsed; |
| 137 EXPECT_TRUE(notification_data_parsed.ResetFromString(notification_data_str)); |
| 138 EXPECT_TRUE(notification_data.Equals(notification_data_parsed)); |
| 139 } |
| 140 |
| 141 TEST_F(P2PNotifierTest, P2PNotificationDataNonDefault) { |
| 142 syncable::ModelTypeSet changed_types; |
| 143 changed_types.insert(syncable::BOOKMARKS); |
| 144 changed_types.insert(syncable::THEMES); |
| 145 const P2PNotificationData notification_data( |
| 146 "sender", NOTIFY_ALL, changed_types); |
| 147 EXPECT_TRUE(notification_data.IsTargeted("sender")); |
| 148 EXPECT_TRUE(notification_data.IsTargeted("other1")); |
| 149 EXPECT_TRUE(notification_data.IsTargeted("other2")); |
| 150 EXPECT_EQ(changed_types, notification_data.GetChangedTypes()); |
| 151 const std::string& notification_data_str = notification_data.ToString(); |
| 152 EXPECT_EQ( |
| 153 "{\"changedTypes\":[\"Bookmarks\",\"Themes\"]," |
| 154 "\"notificationType\":\"notifyAll\"," |
| 155 "\"senderId\":\"sender\"}", notification_data_str); |
| 156 |
| 157 P2PNotificationData notification_data_parsed; |
| 158 EXPECT_TRUE(notification_data_parsed.ResetFromString(notification_data_str)); |
| 159 EXPECT_TRUE(notification_data.Equals(notification_data_parsed)); |
| 160 } |
| 161 |
| 162 TEST_F(P2PNotifierTest, NotificationsBasic) { |
| 163 syncable::ModelTypeSet enabled_types; |
| 164 enabled_types.insert(syncable::BOOKMARKS); |
| 165 enabled_types.insert(syncable::PREFERENCES); |
| 166 |
| 167 EXPECT_CALL(mock_observer_, OnNotificationStateChange(true)); |
| 168 EXPECT_CALL(mock_observer_, |
| 169 OnIncomingNotification(MakePayloadMap(enabled_types))); |
| 170 |
| 171 p2p_notifier_->SetUniqueId("sender"); |
| 172 p2p_notifier_->UpdateCredentials("foo@bar.com", "fake_token"); |
| 173 p2p_notifier_->UpdateEnabledTypes(enabled_types); |
| 174 // Sent with target NOTIFY_OTHERS so should not be propagated to |
| 175 // |mock_observer_|. |
| 176 { |
| 177 syncable::ModelTypeSet changed_types; |
| 178 enabled_types.insert(syncable::THEMES); |
| 179 enabled_types.insert(syncable::APPS); |
| 180 p2p_notifier_->SendNotification(changed_types); |
| 181 } |
| 182 } |
| 183 |
| 184 TEST_F(P2PNotifierTest, SendNotificationData) { |
| 185 syncable::ModelTypeSet enabled_types; |
| 186 enabled_types.insert(syncable::BOOKMARKS); |
| 187 enabled_types.insert(syncable::PREFERENCES); |
| 188 |
| 189 syncable::ModelTypeSet changed_types; |
| 190 changed_types.insert(syncable::THEMES); |
| 191 changed_types.insert(syncable::APPS); |
| 192 |
| 193 const syncable::ModelTypePayloadMap& changed_payload_map = |
| 194 MakePayloadMap(changed_types); |
| 195 |
| 196 EXPECT_CALL(mock_observer_, OnNotificationStateChange(true)); |
| 197 EXPECT_CALL(mock_observer_, |
| 198 OnIncomingNotification(MakePayloadMap(enabled_types))); |
| 199 |
| 200 p2p_notifier_->SetUniqueId("sender"); |
| 201 p2p_notifier_->UpdateCredentials("foo@bar.com", "fake_token"); |
| 202 p2p_notifier_->UpdateEnabledTypes(enabled_types); |
| 203 |
| 204 // Should be dropped. |
| 205 Mock::VerifyAndClearExpectations(&mock_observer_); |
| 206 p2p_notifier_->SendNotificationDataForTest(P2PNotificationData()); |
| 207 |
| 208 // Should be propagated. |
| 209 Mock::VerifyAndClearExpectations(&mock_observer_); |
| 210 EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map)); |
| 211 p2p_notifier_->SendNotificationDataForTest( |
| 212 P2PNotificationData("sender", NOTIFY_SELF, changed_types)); |
| 213 |
| 214 // Should be dropped. |
| 215 Mock::VerifyAndClearExpectations(&mock_observer_); |
| 216 p2p_notifier_->SendNotificationDataForTest( |
| 217 P2PNotificationData("sender2", NOTIFY_SELF, changed_types)); |
| 218 |
| 219 // Should be dropped. |
| 220 Mock::VerifyAndClearExpectations(&mock_observer_); |
| 221 p2p_notifier_->SendNotificationDataForTest( |
| 222 P2PNotificationData("sender", NOTIFY_SELF, syncable::ModelTypeSet())); |
| 223 |
| 224 // Should be dropped. |
| 225 p2p_notifier_->SendNotificationDataForTest( |
| 226 P2PNotificationData("sender", NOTIFY_OTHERS, changed_types)); |
| 227 |
| 228 // Should be propagated. |
| 229 Mock::VerifyAndClearExpectations(&mock_observer_); |
| 230 EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map)); |
| 231 p2p_notifier_->SendNotificationDataForTest( |
| 232 P2PNotificationData("sender2", NOTIFY_OTHERS, changed_types)); |
| 233 |
| 234 // Should be dropped. |
| 235 Mock::VerifyAndClearExpectations(&mock_observer_); |
| 236 p2p_notifier_->SendNotificationDataForTest( |
| 237 P2PNotificationData("sender2", NOTIFY_OTHERS, syncable::ModelTypeSet())); |
| 238 |
| 239 // Should be propagated. |
| 240 Mock::VerifyAndClearExpectations(&mock_observer_); |
| 241 EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map)); |
| 242 p2p_notifier_->SendNotificationDataForTest( |
| 243 P2PNotificationData("sender", NOTIFY_ALL, changed_types)); |
| 244 |
| 245 // Should be propagated. |
| 246 Mock::VerifyAndClearExpectations(&mock_observer_); |
| 247 EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map)); |
| 248 p2p_notifier_->SendNotificationDataForTest( |
| 249 P2PNotificationData("sender2", NOTIFY_ALL, changed_types)); |
| 250 |
| 251 // Should be dropped. |
| 252 Mock::VerifyAndClearExpectations(&mock_observer_); |
| 253 p2p_notifier_->SendNotificationDataForTest( |
| 254 P2PNotificationData("sender2", NOTIFY_ALL, syncable::ModelTypeSet())); |
| 255 } |
| 256 |
| 257 } // namespace |
| 258 |
| 259 } // namespace sync_notifier |
OLD | NEW |