Home | History | Annotate | Download | only in xmpp
      1 /*
      2  *  Copyright 2011 The WebRTC Project Authors. All rights reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 #include <string>
     12 #include <vector>
     13 
     14 #include "webrtc/libjingle/xmllite/xmlelement.h"
     15 #include "webrtc/libjingle/xmpp/constants.h"
     16 #include "webrtc/libjingle/xmpp/fakexmppclient.h"
     17 #include "webrtc/libjingle/xmpp/mucroomconfigtask.h"
     18 #include "webrtc/base/faketaskrunner.h"
     19 #include "webrtc/base/gunit.h"
     20 #include "webrtc/base/sigslot.h"
     21 
     22 class MucRoomConfigListener : public sigslot::has_slots<> {
     23  public:
     24   MucRoomConfigListener() : result_count(0), error_count(0) {}
     25 
     26   void OnResult(buzz::MucRoomConfigTask*) {
     27     ++result_count;
     28   }
     29 
     30   void OnError(buzz::IqTask* task,
     31                const buzz::XmlElement* error) {
     32     ++error_count;
     33   }
     34 
     35   int result_count;
     36   int error_count;
     37 };
     38 
     39 class MucRoomConfigTaskTest : public testing::Test {
     40  public:
     41   MucRoomConfigTaskTest() :
     42       room_jid("muc-jid-ponies (at) domain.com"),
     43       room_name("ponies") {
     44   }
     45 
     46   virtual void SetUp() {
     47     runner = new rtc::FakeTaskRunner();
     48     xmpp_client = new buzz::FakeXmppClient(runner);
     49     listener = new MucRoomConfigListener();
     50   }
     51 
     52   virtual void TearDown() {
     53     delete listener;
     54     // delete xmpp_client;  Deleted by deleting runner.
     55     delete runner;
     56   }
     57 
     58   rtc::FakeTaskRunner* runner;
     59   buzz::FakeXmppClient* xmpp_client;
     60   MucRoomConfigListener* listener;
     61   buzz::Jid room_jid;
     62   std::string room_name;
     63 };
     64 
     65 TEST_F(MucRoomConfigTaskTest, TestConfigEnterprise) {
     66   ASSERT_EQ(0U, xmpp_client->sent_stanzas().size());
     67 
     68   std::vector<std::string> room_features;
     69   room_features.push_back("feature1");
     70   room_features.push_back("feature2");
     71   buzz::MucRoomConfigTask* task = new buzz::MucRoomConfigTask(
     72       xmpp_client, room_jid, "ponies", room_features);
     73   EXPECT_EQ(room_jid, task->room_jid());
     74 
     75   task->SignalResult.connect(listener, &MucRoomConfigListener::OnResult);
     76   task->Start();
     77 
     78   std::string expected_iq =
     79       "<cli:iq type=\"set\" to=\"muc-jid-ponies (at) domain.com\" id=\"0\" "
     80         "xmlns:cli=\"jabber:client\">"
     81         "<query xmlns=\"http://jabber.org/protocol/muc#owner\">"
     82           "<x xmlns=\"jabber:x:data\" type=\"form\">"
     83             "<field var=\"muc#roomconfig_roomname\" type=\"text-single\">"
     84               "<value>ponies</value>"
     85             "</field>"
     86             "<field var=\"muc#roomconfig_features\" type=\"list-multi\">"
     87               "<value>feature1</value>"
     88               "<value>feature2</value>"
     89             "</field>"
     90           "</x>"
     91         "</query>"
     92       "</cli:iq>";
     93 
     94   ASSERT_EQ(1U, xmpp_client->sent_stanzas().size());
     95   EXPECT_EQ(expected_iq, xmpp_client->sent_stanzas()[0]->Str());
     96 
     97   EXPECT_EQ(0, listener->result_count);
     98   EXPECT_EQ(0, listener->error_count);
     99 
    100   std::string response_iq =
    101       "<iq xmlns='jabber:client' id='0' type='result'"
    102       "  from='muc-jid-ponies (at) domain.com'>"
    103       "</iq>";
    104 
    105   xmpp_client->HandleStanza(buzz::XmlElement::ForStr(response_iq));
    106 
    107   EXPECT_EQ(1, listener->result_count);
    108   EXPECT_EQ(0, listener->error_count);
    109 }
    110 
    111 TEST_F(MucRoomConfigTaskTest, TestError) {
    112   std::vector<std::string> room_features;
    113   buzz::MucRoomConfigTask* task = new buzz::MucRoomConfigTask(
    114       xmpp_client, room_jid, "ponies", room_features);
    115   task->SignalError.connect(listener, &MucRoomConfigListener::OnError);
    116   task->Start();
    117 
    118   std::string error_iq =
    119       "<iq xmlns='jabber:client' id='0' type='error'"
    120       " from='muc-jid-ponies (at) domain.com'>"
    121       "</iq>";
    122 
    123   xmpp_client->HandleStanza(buzz::XmlElement::ForStr(error_iq));
    124 
    125   EXPECT_EQ(0, listener->result_count);
    126   EXPECT_EQ(1, listener->error_count);
    127 }
    128