~ubuntu-branches/ubuntu/saucy/kopete/saucy-proposed

« back to all changes in this revision

Viewing changes to protocols/jabber/googletalk/libjingle/talk/xmpp/mucroomlookuptask_unittest.cc

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-06-21 02:22:39 UTC
  • Revision ID: package-import@ubuntu.com-20130621022239-63l3zc8p0nf26pt6
Tags: upstream-4.10.80
ImportĀ upstreamĀ versionĀ 4.10.80

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 "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"
 
38
 
 
39
class MucRoomLookupListener : public sigslot::has_slots<> {
 
40
 public:
 
41
  MucRoomLookupListener() : error_count(0) {}
 
42
 
 
43
  void OnResult(buzz::MucRoomLookupTask* task,
 
44
                const buzz::MucRoomInfo& room) {
 
45
    last_room = room;
 
46
  }
 
47
 
 
48
  void OnError(buzz::IqTask* task,
 
49
               const buzz::XmlElement* error) {
 
50
    ++error_count;
 
51
  }
 
52
 
 
53
  buzz::MucRoomInfo last_room;
 
54
  int error_count;
 
55
};
 
56
 
 
57
class MucRoomLookupTaskTest : public testing::Test {
 
58
 public:
 
59
  MucRoomLookupTaskTest() :
 
60
      lookup_server_jid("lookup@domain.com"),
 
61
      room_jid("muc-jid-ponies@domain.com"),
 
62
      room_name("ponies"),
 
63
      room_domain("domain.com"),
 
64
      room_full_name("ponies@domain.com") {
 
65
  }
 
66
 
 
67
  virtual void SetUp() {
 
68
    runner = new talk_base::FakeTaskRunner();
 
69
    xmpp_client = new buzz::FakeXmppClient(runner);
 
70
    listener = new MucRoomLookupListener();
 
71
  }
 
72
 
 
73
  virtual void TearDown() {
 
74
    delete listener;
 
75
    // delete xmpp_client;  Deleted by deleting runner.
 
76
    delete runner;
 
77
  }
 
78
 
 
79
  talk_base::FakeTaskRunner* runner;
 
80
  buzz::FakeXmppClient* xmpp_client;
 
81
  MucRoomLookupListener* listener;
 
82
  buzz::Jid lookup_server_jid;
 
83
  buzz::Jid room_jid;
 
84
  std::string room_name;
 
85
  std::string room_domain;
 
86
  std::string room_full_name;
 
87
};
 
88
 
 
89
TEST_F(MucRoomLookupTaskTest, TestLookupName) {
 
90
  ASSERT_EQ(0U, xmpp_client->sent_stanzas().size());
 
91
 
 
92
  buzz::MucRoomLookupTask* task = new buzz::MucRoomLookupTask(
 
93
      xmpp_client, lookup_server_jid, room_name, room_domain);
 
94
  task->SignalResult.connect(listener, &MucRoomLookupListener::OnResult);
 
95
  task->Start();
 
96
 
 
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>"
 
103
        "</query>"
 
104
      "</cli:iq>";
 
105
 
 
106
  ASSERT_EQ(1U, xmpp_client->sent_stanzas().size());
 
107
  EXPECT_EQ(expected_iq, xmpp_client->sent_stanzas()[0]->Str());
 
108
 
 
109
  EXPECT_EQ("", listener->last_room.name);
 
110
 
 
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>"
 
117
      "    </item>"
 
118
      "  </query>"
 
119
      "</iq>";
 
120
 
 
121
  xmpp_client->HandleStanza(buzz::XmlElement::ForStr(response_iq));
 
122
 
 
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);
 
128
}
 
129
 
 
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);
 
134
  task->Start();
 
135
 
 
136
  std::string error_iq =
 
137
      "<iq xmlns='jabber:client' id='0' type='error'"
 
138
      "  from='lookup@domain.com'>"
 
139
      "</iq>";
 
140
 
 
141
  EXPECT_EQ(0, listener->error_count);
 
142
  xmpp_client->HandleStanza(buzz::XmlElement::ForStr(error_iq));
 
143
  EXPECT_EQ(1, listener->error_count);
 
144
}
 
145
 
 
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);
 
150
  task->Start();
 
151
 
 
152
  std::string response_iq =
 
153
      "<iq xmlns='jabber:client' from='lookup@domain.com' id='0' type='result'>"
 
154
      "  <query xmlns='jabber:iq:search'>"
 
155
      "    <item/>"
 
156
      "  </query>"
 
157
      "</iq>";
 
158
 
 
159
  EXPECT_EQ(0, listener->error_count);
 
160
  xmpp_client->HandleStanza(buzz::XmlElement::ForStr(response_iq));
 
161
  EXPECT_EQ(1, listener->error_count);
 
162
}