Home | History | Annotate | Download | only in listener
      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.
      4 
      5 #include "jingle/notifier/listener/xmpp_push_client.h"
      6 
      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"
     16 
     17 namespace notifier {
     18 
     19 namespace {
     20 
     21 using ::testing::_;
     22 using ::testing::Mock;
     23 using ::testing::StrictMock;
     24 
     25 class MockObserver : public PushClientObserver {
     26  public:
     27   MOCK_METHOD0(OnNotificationsEnabled, void());
     28   MOCK_METHOD1(OnNotificationsDisabled, void(NotificationsDisabledReason));
     29   MOCK_METHOD1(OnIncomingNotification, void(const Notification&));
     30   MOCK_METHOD0(OnPingResponse, void());
     31 };
     32 
     33 class XmppPushClientTest : public testing::Test {
     34  protected:
     35   XmppPushClientTest() {
     36     notifier_options_.request_context_getter =
     37         new net::TestURLRequestContextGetter(
     38             message_loop_.message_loop_proxy());
     39   }
     40 
     41   virtual ~XmppPushClientTest() {}
     42 
     43   virtual void SetUp() OVERRIDE {
     44     xmpp_push_client_.reset(new XmppPushClient(notifier_options_));
     45     xmpp_push_client_->AddObserver(&mock_observer_);
     46   }
     47 
     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();
     53   }
     54 
     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_;
     61 };
     62 
     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());
     68 }
     69 
     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();
     76 }
     77 
     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();
     84 }
     85 
     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();
     92 }
     93 
     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();
    100 }
    101 
    102 // Make sure nothing blows up when the XMPP push client sends a
    103 // notification.
    104 //
    105 // TODO(akalin): Figure out how to test that the notification was
    106 // actually sent.
    107 TEST_F(XmppPushClientTest, SendNotification) {
    108   EXPECT_CALL(mock_observer_, OnNotificationsEnabled());
    109 
    110   xmpp_push_client_->OnConnect(fake_base_task_.AsWeakPtr());
    111   xmpp_push_client_->OnSubscribed();
    112   xmpp_push_client_->SendNotification(Notification());
    113 }
    114 
    115 // Make sure nothing blows up when the XMPP push client sends a ping.
    116 //
    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());
    120 
    121   xmpp_push_client_->OnConnect(fake_base_task_.AsWeakPtr());
    122   xmpp_push_client_->OnSubscribed();
    123   xmpp_push_client_->SendPing();
    124 }
    125 
    126 // Make sure nothing blows up when the XMPP push client sends a
    127 // notification when disconnected, and the client connects.
    128 //
    129 // TODO(akalin): Figure out how to test that the notification was
    130 // actually sent.
    131 TEST_F(XmppPushClientTest, SendNotificationPending) {
    132   xmpp_push_client_->SendNotification(Notification());
    133 
    134   Mock::VerifyAndClearExpectations(&mock_observer_);
    135 
    136   EXPECT_CALL(mock_observer_, OnNotificationsEnabled());
    137 
    138   xmpp_push_client_->OnConnect(fake_base_task_.AsWeakPtr());
    139   xmpp_push_client_->OnSubscribed();
    140 }
    141 
    142 }  // namespace
    143 
    144 }  // namespace notifier
    145