3 * Copyright 2011, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
31 #include "webrtc/libjingle/xmllite/xmlelement.h"
32 #include "talk/xmpp/constants.h"
33 #include "talk/xmpp/fakexmppclient.h"
34 #include "talk/xmpp/mucroomdiscoverytask.h"
35 #include "webrtc/base/faketaskrunner.h"
36 #include "webrtc/base/gunit.h"
37 #include "webrtc/base/sigslot.h"
39 class MucRoomDiscoveryListener : public sigslot::has_slots<> {
41 MucRoomDiscoveryListener() : error_count(0) {}
43 void OnResult(buzz::MucRoomDiscoveryTask* task,
45 const std::string& name,
46 const std::string& conversation_id,
47 const std::set<std::string>& features,
48 const std::map<std::string, std::string>& extended_info) {
51 last_conversation_id = conversation_id;
52 last_features = features;
53 last_extended_info = extended_info;
56 void OnError(buzz::IqTask* task,
57 const buzz::XmlElement* error) {
62 std::string last_name;
63 std::string last_conversation_id;
64 std::set<std::string> last_features;
65 std::map<std::string, std::string> last_extended_info;
69 class MucRoomDiscoveryTaskTest : public testing::Test {
71 MucRoomDiscoveryTaskTest() :
72 room_jid("muc-jid-ponies@domain.com"),
74 conversation_id("test_conversation_id") {
77 virtual void SetUp() {
78 runner = new rtc::FakeTaskRunner();
79 xmpp_client = new buzz::FakeXmppClient(runner);
80 listener = new MucRoomDiscoveryListener();
83 virtual void TearDown() {
85 // delete xmpp_client; Deleted by deleting runner.
89 rtc::FakeTaskRunner* runner;
90 buzz::FakeXmppClient* xmpp_client;
91 MucRoomDiscoveryListener* listener;
93 std::string room_name;
94 std::string conversation_id;
97 TEST_F(MucRoomDiscoveryTaskTest, TestDiscovery) {
98 ASSERT_EQ(0U, xmpp_client->sent_stanzas().size());
100 buzz::MucRoomDiscoveryTask* task = new buzz::MucRoomDiscoveryTask(
101 xmpp_client, room_jid);
102 task->SignalResult.connect(listener, &MucRoomDiscoveryListener::OnResult);
105 std::string expected_iq =
106 "<cli:iq type=\"get\" to=\"muc-jid-ponies@domain.com\" id=\"0\" "
107 "xmlns:cli=\"jabber:client\">"
108 "<info:query xmlns:info=\"http://jabber.org/protocol/disco#info\"/>"
111 ASSERT_EQ(1U, xmpp_client->sent_stanzas().size());
112 EXPECT_EQ(expected_iq, xmpp_client->sent_stanzas()[0]->Str());
114 EXPECT_EQ("", listener->last_name);
115 EXPECT_EQ("", listener->last_conversation_id);
117 std::string response_iq =
118 "<iq xmlns='jabber:client'"
119 " from='muc-jid-ponies@domain.com' id='0' type='result'>"
120 " <info:query xmlns:info='http://jabber.org/protocol/disco#info'>"
121 " <info:identity name='ponies'>"
122 " <han:conversation-id xmlns:han='google:muc#hangout'>"
123 "test_conversation_id</han:conversation-id>"
125 " <info:feature var='feature1'/>"
126 " <info:feature var='feature2'/>"
127 " <data:x xmlns:data='jabber:x:data'>"
128 " <data:field var='var1' data:value='value1' />"
129 " <data:field var='var2' data:value='value2' />"
134 xmpp_client->HandleStanza(buzz::XmlElement::ForStr(response_iq));
136 EXPECT_EQ(true, listener->last_exists);
137 EXPECT_EQ(room_name, listener->last_name);
138 EXPECT_EQ(conversation_id, listener->last_conversation_id);
139 EXPECT_EQ(2U, listener->last_features.size());
140 EXPECT_EQ(1U, listener->last_features.count("feature1"));
141 EXPECT_EQ(2U, listener->last_extended_info.size());
142 EXPECT_EQ("value1", listener->last_extended_info["var1"]);
143 EXPECT_EQ(0, listener->error_count);
146 TEST_F(MucRoomDiscoveryTaskTest, TestMissingName) {
147 buzz::MucRoomDiscoveryTask* task = new buzz::MucRoomDiscoveryTask(
148 xmpp_client, room_jid);
149 task->SignalError.connect(listener, &MucRoomDiscoveryListener::OnError);
152 std::string error_iq =
153 "<iq xmlns='jabber:client'"
154 " from='muc-jid-ponies@domain.com' id='0' type='result'>"
155 " <info:query xmlns:info='http://jabber.org/protocol/disco#info'>"
159 EXPECT_EQ(0, listener->error_count);
160 xmpp_client->HandleStanza(buzz::XmlElement::ForStr(error_iq));
161 EXPECT_EQ(0, listener->error_count);