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 "talk/base/faketaskrunner.h"
32
#include "talk/base/gunit.h"
33
#include "talk/base/sigslot.h"
34
#include "talk/xmllite/xmlelement.h"
35
#include "talk/xmpp/constants.h"
36
#include "talk/xmpp/fakexmppclient.h"
37
#include "talk/xmpp/mucroomlookuptask.h"
39
class MucRoomLookupListener : public sigslot::has_slots<> {
41
MucRoomLookupListener() : error_count(0) {}
43
void OnResult(buzz::MucRoomLookupTask* task,
44
const buzz::MucRoomInfo& room) {
48
void OnError(buzz::IqTask* task,
49
const buzz::XmlElement* error) {
53
buzz::MucRoomInfo last_room;
57
class MucRoomLookupTaskTest : public testing::Test {
59
MucRoomLookupTaskTest() :
60
lookup_server_jid("lookup@domain.com"),
61
room_jid("muc-jid-ponies@domain.com"),
63
room_domain("domain.com"),
64
room_full_name("ponies@domain.com") {
67
virtual void SetUp() {
68
runner = new talk_base::FakeTaskRunner();
69
xmpp_client = new buzz::FakeXmppClient(runner);
70
listener = new MucRoomLookupListener();
73
virtual void TearDown() {
75
// delete xmpp_client; Deleted by deleting runner.
79
talk_base::FakeTaskRunner* runner;
80
buzz::FakeXmppClient* xmpp_client;
81
MucRoomLookupListener* listener;
82
buzz::Jid lookup_server_jid;
84
std::string room_name;
85
std::string room_domain;
86
std::string room_full_name;
89
TEST_F(MucRoomLookupTaskTest, TestLookupName) {
90
ASSERT_EQ(0U, xmpp_client->sent_stanzas().size());
92
buzz::MucRoomLookupTask* task = new buzz::MucRoomLookupTask(
93
xmpp_client, lookup_server_jid, room_name, room_domain);
94
task->SignalResult.connect(listener, &MucRoomLookupListener::OnResult);
97
std::string expected_iq =
98
"<cli:iq type=\"set\" to=\"lookup@domain.com\" id=\"0\" "
99
"xmlns:cli=\"jabber:client\">"
100
"<query xmlns=\"jabber:iq:search\">"
101
"<room-name>ponies</room-name>"
102
"<room-domain>domain.com</room-domain>"
106
ASSERT_EQ(1U, xmpp_client->sent_stanzas().size());
107
EXPECT_EQ(expected_iq, xmpp_client->sent_stanzas()[0]->Str());
109
EXPECT_EQ("", listener->last_room.name);
111
std::string response_iq =
112
"<iq xmlns='jabber:client' from='lookup@domain.com' id='0' type='result'>"
113
" <query xmlns='jabber:iq:search'>"
114
" <item jid='muc-jid-ponies@domain.com'>"
115
" <room-name>ponies</room-name>"
116
" <room-domain>domain.com</room-domain>"
121
xmpp_client->HandleStanza(buzz::XmlElement::ForStr(response_iq));
123
EXPECT_EQ(room_name, listener->last_room.name);
124
EXPECT_EQ(room_domain, listener->last_room.domain);
125
EXPECT_EQ(room_jid, listener->last_room.jid);
126
EXPECT_EQ(room_full_name, listener->last_room.full_name());
127
EXPECT_EQ(0, listener->error_count);
130
TEST_F(MucRoomLookupTaskTest, TestError) {
131
buzz::MucRoomLookupTask* task = new buzz::MucRoomLookupTask(
132
xmpp_client, lookup_server_jid, room_name, room_domain);
133
task->SignalError.connect(listener, &MucRoomLookupListener::OnError);
136
std::string error_iq =
137
"<iq xmlns='jabber:client' id='0' type='error'"
138
" from='lookup@domain.com'>"
141
EXPECT_EQ(0, listener->error_count);
142
xmpp_client->HandleStanza(buzz::XmlElement::ForStr(error_iq));
143
EXPECT_EQ(1, listener->error_count);
146
TEST_F(MucRoomLookupTaskTest, TestBadJid) {
147
buzz::MucRoomLookupTask* task = new buzz::MucRoomLookupTask(
148
xmpp_client, lookup_server_jid, room_name, room_domain);
149
task->SignalError.connect(listener, &MucRoomLookupListener::OnError);
152
std::string response_iq =
153
"<iq xmlns='jabber:client' from='lookup@domain.com' id='0' type='result'>"
154
" <query xmlns='jabber:iq:search'>"
159
EXPECT_EQ(0, listener->error_count);
160
xmpp_client->HandleStanza(buzz::XmlElement::ForStr(response_iq));
161
EXPECT_EQ(1, listener->error_count);