| 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 "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 12 #include "jingle/notifier/base/fake_base_task.h" |
| 13 #include "jingle/notifier/base/notifier_options.h" |
| 14 #include "jingle/notifier/listener/push_client.h" |
| 15 #include "net/url_request/url_request_test_util.h" |
| 12 #include "sync/notifier/mock_sync_notifier_observer.h" | 16 #include "sync/notifier/mock_sync_notifier_observer.h" |
| 13 #include "sync/syncable/model_type.h" | 17 #include "sync/syncable/model_type.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 19 |
| 16 namespace sync_notifier { | 20 namespace sync_notifier { |
| 17 | 21 |
| 18 namespace { | 22 namespace { |
| 19 | 23 |
| 20 using ::testing::_; | 24 using ::testing::_; |
| 21 using ::testing::Mock; | 25 using ::testing::Mock; |
| 22 using ::testing::StrictMock; | 26 using ::testing::StrictMock; |
| 23 | 27 |
| 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 { | 28 class P2PNotifierTest : public testing::Test { |
| 62 protected: | 29 protected: |
| 63 P2PNotifierTest() : talk_mediator_(NULL) {} | 30 P2PNotifierTest() { |
| 31 notifier_options_.request_context_getter = |
| 32 new TestURLRequestContextGetter(message_loop_.message_loop_proxy()); |
| 33 } |
| 64 | 34 |
| 65 virtual void SetUp() { | 35 virtual ~P2PNotifierTest() {} |
| 66 talk_mediator_ = new FakeTalkMediator(); | 36 |
| 67 p2p_notifier_.reset(new P2PNotifier(talk_mediator_, NOTIFY_OTHERS)); | 37 virtual void SetUp() OVERRIDE { |
| 38 p2p_notifier_.reset(new P2PNotifier(notifier_options_, NOTIFY_OTHERS)); |
| 68 p2p_notifier_->AddObserver(&mock_observer_); | 39 p2p_notifier_->AddObserver(&mock_observer_); |
| 69 } | 40 } |
| 70 | 41 |
| 71 virtual void TearDown() { | 42 virtual void TearDown() OVERRIDE { |
| 43 message_loop_.RunAllPending(); |
| 72 p2p_notifier_->RemoveObserver(&mock_observer_); | 44 p2p_notifier_->RemoveObserver(&mock_observer_); |
| 73 p2p_notifier_.reset(); | 45 p2p_notifier_.reset(); |
| 74 talk_mediator_ = NULL; | 46 message_loop_.RunAllPending(); |
| 75 } | 47 } |
| 76 | 48 |
| 77 syncable::ModelTypePayloadMap MakePayloadMap( | 49 syncable::ModelTypePayloadMap MakePayloadMap( |
| 78 syncable::ModelTypeSet types) { | 50 syncable::ModelTypeSet types) { |
| 79 return syncable::ModelTypePayloadMapFromEnumSet(types, ""); | 51 return syncable::ModelTypePayloadMapFromEnumSet(types, ""); |
| 80 } | 52 } |
| 81 | 53 |
| 82 MessageLoop message_loop_; | 54 // The sockets created by the XMPP code expect an IO loop. |
| 83 // Owned by |p2p_notifier_|. | 55 MessageLoopForIO message_loop_; |
| 84 notifier::TalkMediator* talk_mediator_; | 56 notifier::NotifierOptions notifier_options_; |
| 85 scoped_ptr<P2PNotifier> p2p_notifier_; | 57 scoped_ptr<P2PNotifier> p2p_notifier_; |
| 86 StrictMock<MockSyncNotifierObserver> mock_observer_; | 58 StrictMock<MockSyncNotifierObserver> mock_observer_; |
| 59 notifier::FakeBaseTask fake_base_task_; |
| 87 }; | 60 }; |
| 88 | 61 |
| 89 TEST_F(P2PNotifierTest, P2PNotificationTarget) { | 62 TEST_F(P2PNotifierTest, P2PNotificationTarget) { |
| 90 for (int i = FIRST_NOTIFICATION_TARGET; | 63 for (int i = FIRST_NOTIFICATION_TARGET; |
| 91 i <= LAST_NOTIFICATION_TARGET; ++i) { | 64 i <= LAST_NOTIFICATION_TARGET; ++i) { |
| 92 P2PNotificationTarget target = static_cast<P2PNotificationTarget>(i); | 65 P2PNotificationTarget target = static_cast<P2PNotificationTarget>(i); |
| 93 const std::string& target_str = P2PNotificationTargetToString(target); | 66 const std::string& target_str = P2PNotificationTargetToString(target); |
| 94 EXPECT_FALSE(target_str.empty()); | 67 EXPECT_FALSE(target_str.empty()); |
| 95 EXPECT_EQ(target, P2PNotificationTargetFromString(target_str)); | 68 EXPECT_EQ(target, P2PNotificationTargetFromString(target_str)); |
| 96 } | 69 } |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 | 132 |
| 160 TEST_F(P2PNotifierTest, NotificationsBasic) { | 133 TEST_F(P2PNotifierTest, NotificationsBasic) { |
| 161 syncable::ModelTypeSet enabled_types( | 134 syncable::ModelTypeSet enabled_types( |
| 162 syncable::BOOKMARKS, syncable::PREFERENCES); | 135 syncable::BOOKMARKS, syncable::PREFERENCES); |
| 163 | 136 |
| 164 EXPECT_CALL(mock_observer_, OnNotificationStateChange(true)); | 137 EXPECT_CALL(mock_observer_, OnNotificationStateChange(true)); |
| 165 EXPECT_CALL(mock_observer_, | 138 EXPECT_CALL(mock_observer_, |
| 166 OnIncomingNotification(MakePayloadMap(enabled_types), | 139 OnIncomingNotification(MakePayloadMap(enabled_types), |
| 167 REMOTE_NOTIFICATION)); | 140 REMOTE_NOTIFICATION)); |
| 168 | 141 |
| 142 p2p_notifier_->ReflectSentNotificationsForTest(); |
| 143 |
| 169 p2p_notifier_->SetUniqueId("sender"); | 144 p2p_notifier_->SetUniqueId("sender"); |
| 170 p2p_notifier_->UpdateCredentials("foo@bar.com", "fake_token"); | 145 p2p_notifier_->UpdateCredentials("foo@bar.com", "fake_token"); |
| 171 p2p_notifier_->UpdateEnabledTypes(enabled_types); | 146 p2p_notifier_->UpdateEnabledTypes(enabled_types); |
| 147 |
| 148 p2p_notifier_->SimulateConnectForTest(fake_base_task_.AsWeakPtr()); |
| 149 |
| 172 // Sent with target NOTIFY_OTHERS so should not be propagated to | 150 // Sent with target NOTIFY_OTHERS so should not be propagated to |
| 173 // |mock_observer_|. | 151 // |mock_observer_|. |
| 174 { | 152 { |
| 175 syncable::ModelTypeSet changed_types( | 153 syncable::ModelTypeSet changed_types( |
| 176 syncable::THEMES, syncable::APPS); | 154 syncable::THEMES, syncable::APPS); |
| 177 p2p_notifier_->SendNotification(changed_types); | 155 p2p_notifier_->SendNotification(changed_types); |
| 178 } | 156 } |
| 179 } | 157 } |
| 180 | 158 |
| 181 TEST_F(P2PNotifierTest, SendNotificationData) { | 159 TEST_F(P2PNotifierTest, SendNotificationData) { |
| 182 syncable::ModelTypeSet enabled_types( | 160 syncable::ModelTypeSet enabled_types( |
| 183 syncable::BOOKMARKS, syncable::PREFERENCES); | 161 syncable::BOOKMARKS, syncable::PREFERENCES); |
| 184 | 162 |
| 185 syncable::ModelTypeSet changed_types( | 163 syncable::ModelTypeSet changed_types( |
| 186 syncable::THEMES, syncable::APPS); | 164 syncable::THEMES, syncable::APPS); |
| 187 | 165 |
| 188 const syncable::ModelTypePayloadMap& changed_payload_map = | 166 const syncable::ModelTypePayloadMap& changed_payload_map = |
| 189 MakePayloadMap(changed_types); | 167 MakePayloadMap(changed_types); |
| 190 | 168 |
| 191 EXPECT_CALL(mock_observer_, OnNotificationStateChange(true)); | 169 EXPECT_CALL(mock_observer_, OnNotificationStateChange(true)); |
| 192 EXPECT_CALL(mock_observer_, | 170 EXPECT_CALL(mock_observer_, |
| 193 OnIncomingNotification(MakePayloadMap(enabled_types), | 171 OnIncomingNotification(MakePayloadMap(enabled_types), |
| 194 REMOTE_NOTIFICATION)); | 172 REMOTE_NOTIFICATION)); |
| 195 | 173 |
| 174 p2p_notifier_->ReflectSentNotificationsForTest(); |
| 175 |
| 196 p2p_notifier_->SetUniqueId("sender"); | 176 p2p_notifier_->SetUniqueId("sender"); |
| 197 p2p_notifier_->UpdateCredentials("foo@bar.com", "fake_token"); | 177 p2p_notifier_->UpdateCredentials("foo@bar.com", "fake_token"); |
| 198 p2p_notifier_->UpdateEnabledTypes(enabled_types); | 178 p2p_notifier_->UpdateEnabledTypes(enabled_types); |
| 199 | 179 |
| 180 p2p_notifier_->SimulateConnectForTest(fake_base_task_.AsWeakPtr()); |
| 181 |
| 182 message_loop_.RunAllPending(); |
| 183 |
| 200 // Should be dropped. | 184 // Should be dropped. |
| 201 Mock::VerifyAndClearExpectations(&mock_observer_); | 185 Mock::VerifyAndClearExpectations(&mock_observer_); |
| 202 p2p_notifier_->SendNotificationDataForTest(P2PNotificationData()); | 186 p2p_notifier_->SendNotificationDataForTest(P2PNotificationData()); |
| 203 | 187 |
| 188 message_loop_.RunAllPending(); |
| 189 |
| 204 // Should be propagated. | 190 // Should be propagated. |
| 205 Mock::VerifyAndClearExpectations(&mock_observer_); | 191 Mock::VerifyAndClearExpectations(&mock_observer_); |
| 206 EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map, | 192 EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map, |
| 207 REMOTE_NOTIFICATION)); | 193 REMOTE_NOTIFICATION)); |
| 208 p2p_notifier_->SendNotificationDataForTest( | 194 p2p_notifier_->SendNotificationDataForTest( |
| 209 P2PNotificationData("sender", NOTIFY_SELF, changed_types)); | 195 P2PNotificationData("sender", NOTIFY_SELF, changed_types)); |
| 210 | 196 |
| 197 message_loop_.RunAllPending(); |
| 198 |
| 211 // Should be dropped. | 199 // Should be dropped. |
| 212 Mock::VerifyAndClearExpectations(&mock_observer_); | 200 Mock::VerifyAndClearExpectations(&mock_observer_); |
| 213 p2p_notifier_->SendNotificationDataForTest( | 201 p2p_notifier_->SendNotificationDataForTest( |
| 214 P2PNotificationData("sender2", NOTIFY_SELF, changed_types)); | 202 P2PNotificationData("sender2", NOTIFY_SELF, changed_types)); |
| 215 | 203 |
| 204 message_loop_.RunAllPending(); |
| 205 |
| 216 // Should be dropped. | 206 // Should be dropped. |
| 217 Mock::VerifyAndClearExpectations(&mock_observer_); | 207 Mock::VerifyAndClearExpectations(&mock_observer_); |
| 218 p2p_notifier_->SendNotificationDataForTest( | 208 p2p_notifier_->SendNotificationDataForTest( |
| 219 P2PNotificationData("sender", NOTIFY_SELF, syncable::ModelTypeSet())); | 209 P2PNotificationData("sender", NOTIFY_SELF, syncable::ModelTypeSet())); |
| 220 | 210 |
| 211 message_loop_.RunAllPending(); |
| 212 |
| 221 // Should be dropped. | 213 // Should be dropped. |
| 222 p2p_notifier_->SendNotificationDataForTest( | 214 p2p_notifier_->SendNotificationDataForTest( |
| 223 P2PNotificationData("sender", NOTIFY_OTHERS, changed_types)); | 215 P2PNotificationData("sender", NOTIFY_OTHERS, changed_types)); |
| 224 | 216 |
| 217 message_loop_.RunAllPending(); |
| 218 |
| 225 // Should be propagated. | 219 // Should be propagated. |
| 226 Mock::VerifyAndClearExpectations(&mock_observer_); | 220 Mock::VerifyAndClearExpectations(&mock_observer_); |
| 227 EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map, | 221 EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map, |
| 228 REMOTE_NOTIFICATION)); | 222 REMOTE_NOTIFICATION)); |
| 229 p2p_notifier_->SendNotificationDataForTest( | 223 p2p_notifier_->SendNotificationDataForTest( |
| 230 P2PNotificationData("sender2", NOTIFY_OTHERS, changed_types)); | 224 P2PNotificationData("sender2", NOTIFY_OTHERS, changed_types)); |
| 231 | 225 |
| 226 message_loop_.RunAllPending(); |
| 227 |
| 232 // Should be dropped. | 228 // Should be dropped. |
| 233 Mock::VerifyAndClearExpectations(&mock_observer_); | 229 Mock::VerifyAndClearExpectations(&mock_observer_); |
| 234 p2p_notifier_->SendNotificationDataForTest( | 230 p2p_notifier_->SendNotificationDataForTest( |
| 235 P2PNotificationData("sender2", NOTIFY_OTHERS, syncable::ModelTypeSet())); | 231 P2PNotificationData("sender2", NOTIFY_OTHERS, syncable::ModelTypeSet())); |
| 236 | 232 |
| 233 message_loop_.RunAllPending(); |
| 234 |
| 237 // Should be propagated. | 235 // Should be propagated. |
| 238 Mock::VerifyAndClearExpectations(&mock_observer_); | 236 Mock::VerifyAndClearExpectations(&mock_observer_); |
| 239 EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map, | 237 EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map, |
| 240 REMOTE_NOTIFICATION)); | 238 REMOTE_NOTIFICATION)); |
| 241 p2p_notifier_->SendNotificationDataForTest( | 239 p2p_notifier_->SendNotificationDataForTest( |
| 242 P2PNotificationData("sender", NOTIFY_ALL, changed_types)); | 240 P2PNotificationData("sender", NOTIFY_ALL, changed_types)); |
| 243 | 241 |
| 242 message_loop_.RunAllPending(); |
| 243 |
| 244 // Should be propagated. | 244 // Should be propagated. |
| 245 Mock::VerifyAndClearExpectations(&mock_observer_); | 245 Mock::VerifyAndClearExpectations(&mock_observer_); |
| 246 EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map, | 246 EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map, |
| 247 REMOTE_NOTIFICATION)); | 247 REMOTE_NOTIFICATION)); |
| 248 p2p_notifier_->SendNotificationDataForTest( | 248 p2p_notifier_->SendNotificationDataForTest( |
| 249 P2PNotificationData("sender2", NOTIFY_ALL, changed_types)); | 249 P2PNotificationData("sender2", NOTIFY_ALL, changed_types)); |
| 250 | 250 |
| 251 message_loop_.RunAllPending(); |
| 252 |
| 251 // Should be dropped. | 253 // Should be dropped. |
| 252 Mock::VerifyAndClearExpectations(&mock_observer_); | 254 Mock::VerifyAndClearExpectations(&mock_observer_); |
| 253 p2p_notifier_->SendNotificationDataForTest( | 255 p2p_notifier_->SendNotificationDataForTest( |
| 254 P2PNotificationData("sender2", NOTIFY_ALL, syncable::ModelTypeSet())); | 256 P2PNotificationData("sender2", NOTIFY_ALL, syncable::ModelTypeSet())); |
| 257 |
| 258 message_loop_.RunAllPending(); |
| 255 } | 259 } |
| 256 | 260 |
| 257 } // namespace | 261 } // namespace |
| 258 | 262 |
| 259 } // namespace sync_notifier | 263 } // namespace sync_notifier |
| OLD | NEW |