Index: sync/notifier/p2p_notifier_unittest.cc |
diff --git a/sync/notifier/p2p_notifier_unittest.cc b/sync/notifier/p2p_notifier_unittest.cc |
index 61831218e4062db6d8b122933ffc2b41c9edac0a..fe1a62340c1d418a45e7e290b6821b125ddbeb0a 100644 |
--- a/sync/notifier/p2p_notifier_unittest.cc |
+++ b/sync/notifier/p2p_notifier_unittest.cc |
@@ -6,14 +6,7 @@ |
#include <cstddef> |
-#include "base/compiler_specific.h" |
-#include "base/memory/scoped_ptr.h" |
-#include "base/message_loop.h" |
-#include "jingle/notifier/base/fake_base_task.h" |
-#include "jingle/notifier/base/notifier_options.h" |
-#include "jingle/notifier/listener/push_client.h" |
-#include "net/base/mock_host_resolver.h" |
-#include "net/url_request/url_request_test_util.h" |
+#include "jingle/notifier/listener/fake_push_client.h" |
#include "sync/notifier/mock_sync_notifier_observer.h" |
#include "sync/syncable/model_type.h" |
#include "testing/gtest/include/gtest/gtest.h" |
@@ -26,36 +19,19 @@ using ::testing::_; |
using ::testing::Mock; |
using ::testing::StrictMock; |
-class MyTestURLRequestContext : public TestURLRequestContext { |
- public: |
- MyTestURLRequestContext() : TestURLRequestContext(true) { |
- context_storage_.set_host_resolver(new net::HangingHostResolver()); |
- Init(); |
- } |
- virtual ~MyTestURLRequestContext() {} |
-}; |
- |
class P2PNotifierTest : public testing::Test { |
protected: |
- P2PNotifierTest() { |
- notifier_options_.request_context_getter = |
- new TestURLRequestContextGetter( |
- message_loop_.message_loop_proxy(), |
- scoped_ptr<TestURLRequestContext>(new MyTestURLRequestContext())); |
+ P2PNotifierTest() |
+ : fake_push_client_(new notifier::FakePushClient()), |
+ p2p_notifier_( |
+ scoped_ptr<notifier::PushClient>(fake_push_client_), |
+ NOTIFY_OTHERS), |
+ next_sent_notification_to_reflect_(0) { |
+ p2p_notifier_.AddObserver(&mock_observer_); |
} |
- virtual ~P2PNotifierTest() {} |
- |
- virtual void SetUp() OVERRIDE { |
- p2p_notifier_.reset(new P2PNotifier(notifier_options_, NOTIFY_OTHERS)); |
- p2p_notifier_->AddObserver(&mock_observer_); |
- } |
- |
- virtual void TearDown() OVERRIDE { |
- message_loop_.RunAllPending(); |
- p2p_notifier_->RemoveObserver(&mock_observer_); |
- p2p_notifier_.reset(); |
- message_loop_.RunAllPending(); |
+ virtual ~P2PNotifierTest() { |
+ p2p_notifier_.RemoveObserver(&mock_observer_); |
} |
syncable::ModelTypePayloadMap MakePayloadMap( |
@@ -63,14 +39,28 @@ class P2PNotifierTest : public testing::Test { |
return syncable::ModelTypePayloadMapFromEnumSet(types, ""); |
} |
- // The sockets created by the XMPP code expect an IO loop. |
- MessageLoopForIO message_loop_; |
- notifier::NotifierOptions notifier_options_; |
- scoped_ptr<P2PNotifier> p2p_notifier_; |
+ // Simulate receiving all the notifications we sent out since last |
+ // time this was called. |
+ void ReflectSentNotifications() { |
+ const std::vector<notifier::Notification>& sent_notifications = |
+ fake_push_client_->sent_notifications(); |
+ for(size_t i = next_sent_notification_to_reflect_; |
+ i < sent_notifications.size(); ++i) { |
+ p2p_notifier_.OnIncomingNotification(sent_notifications[i]); |
+ } |
+ next_sent_notification_to_reflect_ = sent_notifications.size(); |
+ } |
+ |
+ // Owned by |p2p_notifier_|. |
+ notifier::FakePushClient* fake_push_client_; |
+ P2PNotifier p2p_notifier_; |
StrictMock<MockSyncNotifierObserver> mock_observer_; |
- notifier::FakeBaseTask fake_base_task_; |
+ |
+ private: |
+ size_t next_sent_notification_to_reflect_; |
}; |
+// Make sure the P2PNotificationTarget <-> string conversions work. |
TEST_F(P2PNotifierTest, P2PNotificationTarget) { |
for (int i = FIRST_NOTIFICATION_TARGET; |
i <= LAST_NOTIFICATION_TARGET; ++i) { |
@@ -82,6 +72,7 @@ TEST_F(P2PNotifierTest, P2PNotificationTarget) { |
EXPECT_EQ(NOTIFY_SELF, P2PNotificationTargetFromString("unknown")); |
} |
+// Make sure notification targeting works correctly. |
TEST_F(P2PNotifierTest, P2PNotificationDataIsTargeted) { |
{ |
const P2PNotificationData notification_data( |
@@ -106,6 +97,8 @@ TEST_F(P2PNotifierTest, P2PNotificationDataIsTargeted) { |
} |
} |
+// Make sure the P2PNotificationData <-> string conversions work for a |
+// default-constructed P2PNotificationData. |
TEST_F(P2PNotifierTest, P2PNotificationDataDefault) { |
const P2PNotificationData notification_data; |
EXPECT_TRUE(notification_data.IsTargeted("")); |
@@ -122,6 +115,8 @@ TEST_F(P2PNotifierTest, P2PNotificationDataDefault) { |
EXPECT_TRUE(notification_data.Equals(notification_data_parsed)); |
} |
+// Make sure the P2PNotificationData <-> string conversions work for a |
+// non-default-constructed P2PNotificationData. |
TEST_F(P2PNotifierTest, P2PNotificationDataNonDefault) { |
const syncable::ModelTypeSet changed_types( |
syncable::BOOKMARKS, syncable::THEMES); |
@@ -142,6 +137,10 @@ TEST_F(P2PNotifierTest, P2PNotificationDataNonDefault) { |
EXPECT_TRUE(notification_data.Equals(notification_data_parsed)); |
} |
+// Set up the P2PNotifier, simulate a successful connection, and send |
+// a notification with the default target (NOTIFY_OTHERS). The |
+// observer should receive only a notification from the call to |
+// UpdateEnabledTypes(). |
TEST_F(P2PNotifierTest, NotificationsBasic) { |
syncable::ModelTypeSet enabled_types( |
syncable::BOOKMARKS, syncable::PREFERENCES); |
@@ -151,23 +150,41 @@ TEST_F(P2PNotifierTest, NotificationsBasic) { |
OnIncomingNotification(MakePayloadMap(enabled_types), |
REMOTE_NOTIFICATION)); |
- p2p_notifier_->ReflectSentNotificationsForTest(); |
+ p2p_notifier_.SetUniqueId("sender"); |
- p2p_notifier_->SetUniqueId("sender"); |
- p2p_notifier_->UpdateCredentials("foo@bar.com", "fake_token"); |
- p2p_notifier_->UpdateEnabledTypes(enabled_types); |
+ const char kEmail[] = "foo@bar.com"; |
+ const char kToken[] = "token"; |
+ p2p_notifier_.UpdateCredentials(kEmail, kToken); |
+ { |
+ notifier::Subscription expected_subscription; |
+ expected_subscription.channel = kSyncP2PNotificationChannel; |
+ expected_subscription.from = kEmail; |
+ EXPECT_TRUE(notifier::SubscriptionListsEqual( |
+ fake_push_client_->subscriptions(), |
+ notifier::SubscriptionList(1, expected_subscription))); |
+ } |
+ EXPECT_EQ(kEmail, fake_push_client_->email()); |
+ EXPECT_EQ(kToken, fake_push_client_->token()); |
- p2p_notifier_->SimulateConnectForTest(fake_base_task_.AsWeakPtr()); |
+ p2p_notifier_.UpdateEnabledTypes(enabled_types); |
+ |
+ ReflectSentNotifications(); |
+ fake_push_client_->SimulateNotificationStateChange(true); |
// Sent with target NOTIFY_OTHERS so should not be propagated to |
// |mock_observer_|. |
{ |
syncable::ModelTypeSet changed_types( |
syncable::THEMES, syncable::APPS); |
- p2p_notifier_->SendNotification(changed_types); |
+ p2p_notifier_.SendNotification(changed_types); |
} |
+ |
+ ReflectSentNotifications(); |
} |
+// Set up the P2PNotifier and send out notifications with various |
+// target settings. The notifications received by the observer should |
+// be consistent with the target settings. |
TEST_F(P2PNotifierTest, SendNotificationData) { |
syncable::ModelTypeSet enabled_types( |
syncable::BOOKMARKS, syncable::PREFERENCES); |
@@ -183,91 +200,90 @@ TEST_F(P2PNotifierTest, SendNotificationData) { |
OnIncomingNotification(MakePayloadMap(enabled_types), |
REMOTE_NOTIFICATION)); |
- p2p_notifier_->ReflectSentNotificationsForTest(); |
- |
- p2p_notifier_->SetUniqueId("sender"); |
- p2p_notifier_->UpdateCredentials("foo@bar.com", "fake_token"); |
- p2p_notifier_->UpdateEnabledTypes(enabled_types); |
+ p2p_notifier_.SetUniqueId("sender"); |
+ p2p_notifier_.UpdateCredentials("foo@bar.com", "fake_token"); |
+ p2p_notifier_.UpdateEnabledTypes(enabled_types); |
- p2p_notifier_->SimulateConnectForTest(fake_base_task_.AsWeakPtr()); |
+ ReflectSentNotifications(); |
+ fake_push_client_->SimulateNotificationStateChange(true); |
- message_loop_.RunAllPending(); |
+ ReflectSentNotifications(); |
// Should be dropped. |
Mock::VerifyAndClearExpectations(&mock_observer_); |
- p2p_notifier_->SendNotificationDataForTest(P2PNotificationData()); |
+ p2p_notifier_.SendNotificationDataForTest(P2PNotificationData()); |
- message_loop_.RunAllPending(); |
+ ReflectSentNotifications(); |
// Should be propagated. |
Mock::VerifyAndClearExpectations(&mock_observer_); |
EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map, |
REMOTE_NOTIFICATION)); |
- p2p_notifier_->SendNotificationDataForTest( |
+ p2p_notifier_.SendNotificationDataForTest( |
P2PNotificationData("sender", NOTIFY_SELF, changed_types)); |
- message_loop_.RunAllPending(); |
+ ReflectSentNotifications(); |
// Should be dropped. |
Mock::VerifyAndClearExpectations(&mock_observer_); |
- p2p_notifier_->SendNotificationDataForTest( |
+ p2p_notifier_.SendNotificationDataForTest( |
P2PNotificationData("sender2", NOTIFY_SELF, changed_types)); |
- message_loop_.RunAllPending(); |
+ ReflectSentNotifications(); |
// Should be dropped. |
Mock::VerifyAndClearExpectations(&mock_observer_); |
- p2p_notifier_->SendNotificationDataForTest( |
+ p2p_notifier_.SendNotificationDataForTest( |
P2PNotificationData("sender", NOTIFY_SELF, syncable::ModelTypeSet())); |
- message_loop_.RunAllPending(); |
+ ReflectSentNotifications(); |
// Should be dropped. |
- p2p_notifier_->SendNotificationDataForTest( |
+ p2p_notifier_.SendNotificationDataForTest( |
P2PNotificationData("sender", NOTIFY_OTHERS, changed_types)); |
- message_loop_.RunAllPending(); |
+ ReflectSentNotifications(); |
// Should be propagated. |
Mock::VerifyAndClearExpectations(&mock_observer_); |
EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map, |
REMOTE_NOTIFICATION)); |
- p2p_notifier_->SendNotificationDataForTest( |
+ p2p_notifier_.SendNotificationDataForTest( |
P2PNotificationData("sender2", NOTIFY_OTHERS, changed_types)); |
- message_loop_.RunAllPending(); |
+ ReflectSentNotifications(); |
// Should be dropped. |
Mock::VerifyAndClearExpectations(&mock_observer_); |
- p2p_notifier_->SendNotificationDataForTest( |
+ p2p_notifier_.SendNotificationDataForTest( |
P2PNotificationData("sender2", NOTIFY_OTHERS, syncable::ModelTypeSet())); |
- message_loop_.RunAllPending(); |
+ ReflectSentNotifications(); |
// Should be propagated. |
Mock::VerifyAndClearExpectations(&mock_observer_); |
EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map, |
REMOTE_NOTIFICATION)); |
- p2p_notifier_->SendNotificationDataForTest( |
+ p2p_notifier_.SendNotificationDataForTest( |
P2PNotificationData("sender", NOTIFY_ALL, changed_types)); |
- message_loop_.RunAllPending(); |
+ ReflectSentNotifications(); |
// Should be propagated. |
Mock::VerifyAndClearExpectations(&mock_observer_); |
EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map, |
REMOTE_NOTIFICATION)); |
- p2p_notifier_->SendNotificationDataForTest( |
+ p2p_notifier_.SendNotificationDataForTest( |
P2PNotificationData("sender2", NOTIFY_ALL, changed_types)); |
- message_loop_.RunAllPending(); |
+ ReflectSentNotifications(); |
// Should be dropped. |
Mock::VerifyAndClearExpectations(&mock_observer_); |
- p2p_notifier_->SendNotificationDataForTest( |
+ p2p_notifier_.SendNotificationDataForTest( |
P2PNotificationData("sender2", NOTIFY_ALL, syncable::ModelTypeSet())); |
- message_loop_.RunAllPending(); |
+ ReflectSentNotifications(); |
} |
} // namespace |