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
3 // found in the LICENSE file.
5 #include "jingle/notifier/listener/xmpp_push_client.h"
7 #include "base/compiler_specific.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "jingle/notifier/base/fake_base_task.h"
11 #include "jingle/notifier/base/notifier_options.h"
12 #include "jingle/notifier/listener/push_client_observer.h"
13 #include "net/url_request/url_request_test_util.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
22 using ::testing::Mock;
23 using ::testing::StrictMock;
25 class MockObserver : public PushClientObserver {
27 MOCK_METHOD0(OnNotificationsEnabled, void());
28 MOCK_METHOD1(OnNotificationsDisabled, void(NotificationsDisabledReason));
29 MOCK_METHOD1(OnIncomingNotification, void(const Notification&));
30 MOCK_METHOD0(OnPingResponse, void());
33 class XmppPushClientTest : public testing::Test {
35 XmppPushClientTest() {
36 notifier_options_.request_context_getter =
37 new net::TestURLRequestContextGetter(
38 message_loop_.message_loop_proxy());
41 virtual ~XmppPushClientTest() {}
43 virtual void SetUp() OVERRIDE {
44 xmpp_push_client_.reset(new XmppPushClient(notifier_options_));
45 xmpp_push_client_->AddObserver(&mock_observer_);
48 virtual void TearDown() OVERRIDE {
49 // Clear out any messages posted by XmppPushClient.
50 message_loop_.RunUntilIdle();
51 xmpp_push_client_->RemoveObserver(&mock_observer_);
52 xmpp_push_client_.reset();
55 // The sockets created by the XMPP code expect an IO loop.
56 base::MessageLoopForIO message_loop_;
57 NotifierOptions notifier_options_;
58 StrictMock<MockObserver> mock_observer_;
59 scoped_ptr<XmppPushClient> xmpp_push_client_;
60 FakeBaseTask fake_base_task_;
63 // Make sure the XMPP push client notifies its observers of incoming
64 // notifications properly.
65 TEST_F(XmppPushClientTest, OnIncomingNotification) {
66 EXPECT_CALL(mock_observer_, OnIncomingNotification(_));
67 xmpp_push_client_->OnNotificationReceived(Notification());
70 // Make sure the XMPP push client notifies its observers of a
71 // successful connection properly.
72 TEST_F(XmppPushClientTest, ConnectAndSubscribe) {
73 EXPECT_CALL(mock_observer_, OnNotificationsEnabled());
74 xmpp_push_client_->OnConnect(fake_base_task_.AsWeakPtr());
75 xmpp_push_client_->OnSubscribed();
78 // Make sure the XMPP push client notifies its observers of a
79 // terminated connection properly.
80 TEST_F(XmppPushClientTest, Disconnect) {
81 EXPECT_CALL(mock_observer_,
82 OnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR));
83 xmpp_push_client_->OnTransientDisconnection();
86 // Make sure the XMPP push client notifies its observers of
87 // rejected credentials properly.
88 TEST_F(XmppPushClientTest, RejectCredentials) {
89 EXPECT_CALL(mock_observer_,
90 OnNotificationsDisabled(NOTIFICATION_CREDENTIALS_REJECTED));
91 xmpp_push_client_->OnCredentialsRejected();
94 // Make sure the XMPP push client notifies its observers of a
95 // subscription error properly.
96 TEST_F(XmppPushClientTest, SubscriptionError) {
97 EXPECT_CALL(mock_observer_,
98 OnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR));
99 xmpp_push_client_->OnSubscriptionError();
102 // Make sure nothing blows up when the XMPP push client sends a
105 // TODO(akalin): Figure out how to test that the notification was
107 TEST_F(XmppPushClientTest, SendNotification) {
108 EXPECT_CALL(mock_observer_, OnNotificationsEnabled());
110 xmpp_push_client_->OnConnect(fake_base_task_.AsWeakPtr());
111 xmpp_push_client_->OnSubscribed();
112 xmpp_push_client_->SendNotification(Notification());
115 // Make sure nothing blows up when the XMPP push client sends a ping.
117 // TODO(akalin): Figure out how to test that the ping was actually sent.
118 TEST_F(XmppPushClientTest, SendPing) {
119 EXPECT_CALL(mock_observer_, OnNotificationsEnabled());
121 xmpp_push_client_->OnConnect(fake_base_task_.AsWeakPtr());
122 xmpp_push_client_->OnSubscribed();
123 xmpp_push_client_->SendPing();
126 // Make sure nothing blows up when the XMPP push client sends a
127 // notification when disconnected, and the client connects.
129 // TODO(akalin): Figure out how to test that the notification was
131 TEST_F(XmppPushClientTest, SendNotificationPending) {
132 xmpp_push_client_->SendNotification(Notification());
134 Mock::VerifyAndClearExpectations(&mock_observer_);
136 EXPECT_CALL(mock_observer_, OnNotificationsEnabled());
138 xmpp_push_client_->OnConnect(fake_base_task_.AsWeakPtr());
139 xmpp_push_client_->OnSubscribed();
144 } // namespace notifier