summaryrefslogtreecommitdiffstats
path: root/jingle/notifier/listener/xmpp_push_client_unittest.cc
blob: f282ca80b01dcf5cb856f7aea88db1f9499170dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "jingle/notifier/listener/xmpp_push_client.h"

#include "base/compiler_specific.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "jingle/notifier/base/fake_base_task.h"
#include "jingle/notifier/base/notifier_options.h"
#include "jingle/notifier/listener/push_client_observer.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace notifier {

namespace {

using ::testing::_;
using ::testing::Mock;
using ::testing::StrictMock;

class MockObserver : public PushClientObserver {
 public:
  MOCK_METHOD0(OnNotificationsEnabled, void());
  MOCK_METHOD1(OnNotificationsDisabled, void(NotificationsDisabledReason));
  MOCK_METHOD1(OnIncomingNotification, void(const Notification&));
  MOCK_METHOD0(OnPingResponse, void());
};

class XmppPushClientTest : public testing::Test {
 protected:
  XmppPushClientTest() {
    notifier_options_.request_context_getter =
        new net::TestURLRequestContextGetter(
            message_loop_.task_runner());
  }

  ~XmppPushClientTest() override {}

  void SetUp() override {
    xmpp_push_client_.reset(new XmppPushClient(notifier_options_));
    xmpp_push_client_->AddObserver(&mock_observer_);
  }

  void TearDown() override {
    // Clear out any messages posted by XmppPushClient.
    message_loop_.RunUntilIdle();
    xmpp_push_client_->RemoveObserver(&mock_observer_);
    xmpp_push_client_.reset();
  }

  // The sockets created by the XMPP code expect an IO loop.
  base::MessageLoopForIO message_loop_;
  NotifierOptions notifier_options_;
  StrictMock<MockObserver> mock_observer_;
  scoped_ptr<XmppPushClient> xmpp_push_client_;
  FakeBaseTask fake_base_task_;
};

// Make sure the XMPP push client notifies its observers of incoming
// notifications properly.
TEST_F(XmppPushClientTest, OnIncomingNotification) {
  EXPECT_CALL(mock_observer_, OnIncomingNotification(_));
  xmpp_push_client_->OnNotificationReceived(Notification());
}

// Make sure the XMPP push client notifies its observers of a
// successful connection properly.
TEST_F(XmppPushClientTest, ConnectAndSubscribe) {
  EXPECT_CALL(mock_observer_, OnNotificationsEnabled());
  xmpp_push_client_->OnConnect(fake_base_task_.AsWeakPtr());
  xmpp_push_client_->OnSubscribed();
}

// Make sure the XMPP push client notifies its observers of a
// terminated connection properly.
TEST_F(XmppPushClientTest, Disconnect) {
  EXPECT_CALL(mock_observer_,
              OnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR));
  xmpp_push_client_->OnTransientDisconnection();
}

// Make sure the XMPP push client notifies its observers of
// rejected credentials properly.
TEST_F(XmppPushClientTest, RejectCredentials) {
  EXPECT_CALL(mock_observer_,
              OnNotificationsDisabled(NOTIFICATION_CREDENTIALS_REJECTED));
  xmpp_push_client_->OnCredentialsRejected();
}

// Make sure the XMPP push client notifies its observers of a
// subscription error properly.
TEST_F(XmppPushClientTest, SubscriptionError) {
  EXPECT_CALL(mock_observer_,
              OnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR));
  xmpp_push_client_->OnSubscriptionError();
}

// Make sure nothing blows up when the XMPP push client sends a
// notification.
//
// TODO(akalin): Figure out how to test that the notification was
// actually sent.
TEST_F(XmppPushClientTest, SendNotification) {
  EXPECT_CALL(mock_observer_, OnNotificationsEnabled());

  xmpp_push_client_->OnConnect(fake_base_task_.AsWeakPtr());
  xmpp_push_client_->OnSubscribed();
  xmpp_push_client_->SendNotification(Notification());
}

// Make sure nothing blows up when the XMPP push client sends a ping.
//
// TODO(akalin): Figure out how to test that the ping was actually sent.
TEST_F(XmppPushClientTest, SendPing) {
  EXPECT_CALL(mock_observer_, OnNotificationsEnabled());

  xmpp_push_client_->OnConnect(fake_base_task_.AsWeakPtr());
  xmpp_push_client_->OnSubscribed();
  xmpp_push_client_->SendPing();
}

// Make sure nothing blows up when the XMPP push client sends a
// notification when disconnected, and the client connects.
//
// TODO(akalin): Figure out how to test that the notification was
// actually sent.
TEST_F(XmppPushClientTest, SendNotificationPending) {
  xmpp_push_client_->SendNotification(Notification());

  Mock::VerifyAndClearExpectations(&mock_observer_);

  EXPECT_CALL(mock_observer_, OnNotificationsEnabled());

  xmpp_push_client_->OnConnect(fake_base_task_.AsWeakPtr());
  xmpp_push_client_->OnSubscribed();
}

}  // namespace

}  // namespace notifier