1/*
2 * libjingle
3 * Copyright 2011, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 *  1. Redistributions of source code must retain the above copyright notice,
9 *     this list of conditions and the following disclaimer.
10 *  2. Redistributions in binary form must reproduce the above copyright notice,
11 *     this list of conditions and the following disclaimer in the documentation
12 *     and/or other materials provided with the distribution.
13 *  3. The name of the author may not be used to endorse or promote products
14 *     derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include <string>
29#include <vector>
30
31#include "webrtc/libjingle/xmllite/xmlelement.h"
32#include "talk/xmpp/constants.h"
33#include "talk/xmpp/fakexmppclient.h"
34#include "talk/xmpp/mucroomconfigtask.h"
35#include "webrtc/base/faketaskrunner.h"
36#include "webrtc/base/gunit.h"
37#include "webrtc/base/sigslot.h"
38
39class MucRoomConfigListener : public sigslot::has_slots<> {
40 public:
41  MucRoomConfigListener() : result_count(0), error_count(0) {}
42
43  void OnResult(buzz::MucRoomConfigTask*) {
44    ++result_count;
45  }
46
47  void OnError(buzz::IqTask* task,
48               const buzz::XmlElement* error) {
49    ++error_count;
50  }
51
52  int result_count;
53  int error_count;
54};
55
56class MucRoomConfigTaskTest : public testing::Test {
57 public:
58  MucRoomConfigTaskTest() :
59      room_jid("muc-jid-ponies@domain.com"),
60      room_name("ponies") {
61  }
62
63  virtual void SetUp() {
64    runner = new rtc::FakeTaskRunner();
65    xmpp_client = new buzz::FakeXmppClient(runner);
66    listener = new MucRoomConfigListener();
67  }
68
69  virtual void TearDown() {
70    delete listener;
71    // delete xmpp_client;  Deleted by deleting runner.
72    delete runner;
73  }
74
75  rtc::FakeTaskRunner* runner;
76  buzz::FakeXmppClient* xmpp_client;
77  MucRoomConfigListener* listener;
78  buzz::Jid room_jid;
79  std::string room_name;
80};
81
82TEST_F(MucRoomConfigTaskTest, TestConfigEnterprise) {
83  ASSERT_EQ(0U, xmpp_client->sent_stanzas().size());
84
85  std::vector<std::string> room_features;
86  room_features.push_back("feature1");
87  room_features.push_back("feature2");
88  buzz::MucRoomConfigTask* task = new buzz::MucRoomConfigTask(
89      xmpp_client, room_jid, "ponies", room_features);
90  EXPECT_EQ(room_jid, task->room_jid());
91
92  task->SignalResult.connect(listener, &MucRoomConfigListener::OnResult);
93  task->Start();
94
95  std::string expected_iq =
96      "<cli:iq type=\"set\" to=\"muc-jid-ponies@domain.com\" id=\"0\" "
97        "xmlns:cli=\"jabber:client\">"
98        "<query xmlns=\"http://jabber.org/protocol/muc#owner\">"
99          "<x xmlns=\"jabber:x:data\" type=\"form\">"
100            "<field var=\"muc#roomconfig_roomname\" type=\"text-single\">"
101              "<value>ponies</value>"
102            "</field>"
103            "<field var=\"muc#roomconfig_features\" type=\"list-multi\">"
104              "<value>feature1</value>"
105              "<value>feature2</value>"
106            "</field>"
107          "</x>"
108        "</query>"
109      "</cli:iq>";
110
111  ASSERT_EQ(1U, xmpp_client->sent_stanzas().size());
112  EXPECT_EQ(expected_iq, xmpp_client->sent_stanzas()[0]->Str());
113
114  EXPECT_EQ(0, listener->result_count);
115  EXPECT_EQ(0, listener->error_count);
116
117  std::string response_iq =
118      "<iq xmlns='jabber:client' id='0' type='result'"
119      "  from='muc-jid-ponies@domain.com'>"
120      "</iq>";
121
122  xmpp_client->HandleStanza(buzz::XmlElement::ForStr(response_iq));
123
124  EXPECT_EQ(1, listener->result_count);
125  EXPECT_EQ(0, listener->error_count);
126}
127
128TEST_F(MucRoomConfigTaskTest, TestError) {
129  std::vector<std::string> room_features;
130  buzz::MucRoomConfigTask* task = new buzz::MucRoomConfigTask(
131      xmpp_client, room_jid, "ponies", room_features);
132  task->SignalError.connect(listener, &MucRoomConfigListener::OnError);
133  task->Start();
134
135  std::string error_iq =
136      "<iq xmlns='jabber:client' id='0' type='error'"
137      " from='muc-jid-ponies@domain.com'>"
138      "</iq>";
139
140  xmpp_client->HandleStanza(buzz::XmlElement::ForStr(error_iq));
141
142  EXPECT_EQ(0, listener->result_count);
143  EXPECT_EQ(1, listener->error_count);
144}
145