2 * Copyright 2011 The WebRTC Project Authors. All rights reserved.
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.
13 #include "webrtc/libjingle/xmllite/qname.h"
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/iqtask.h"
18 #include "webrtc/libjingle/xmpp/jid.h"
19 #include "webrtc/libjingle/xmpp/pubsubtasks.h"
20 #include "webrtc/base/faketaskrunner.h"
21 #include "webrtc/base/gunit.h"
22 #include "webrtc/base/sigslot.h"
24 struct HandledPubSubItem {
29 class TestPubSubTasksListener : public sigslot::has_slots<> {
31 TestPubSubTasksListener() : result_count(0), error_count(0) {}
33 void OnReceiveUpdate(buzz::PubSubReceiveTask* task,
34 const std::vector<buzz::PubSubItem>& items) {
38 void OnRequestResult(buzz::PubSubRequestTask* task,
39 const std::vector<buzz::PubSubItem>& items) {
43 void OnItems(const std::vector<buzz::PubSubItem>& items) {
44 for (std::vector<buzz::PubSubItem>::const_iterator item = items.begin();
45 item != items.end(); ++item) {
46 HandledPubSubItem handled_item;
47 handled_item.itemid = item->itemid;
48 if (item->elem->FirstElement() != NULL) {
49 handled_item.payload = item->elem->FirstElement()->Str();
51 this->items.push_back(handled_item);
55 void OnPublishResult(buzz::PubSubPublishTask* task) {
59 void OnRetractResult(buzz::PubSubRetractTask* task) {
63 void OnError(buzz::IqTask* task, const buzz::XmlElement* stanza) {
67 std::vector<HandledPubSubItem> items;
72 class PubSubTasksTest : public testing::Test {
75 pubsubjid("room@domain.com"),
78 runner.reset(new rtc::FakeTaskRunner());
79 client = new buzz::FakeXmppClient(runner.get());
80 listener.reset(new TestPubSubTasksListener());
83 rtc::scoped_ptr<rtc::FakeTaskRunner> runner;
84 // Client deleted by deleting runner.
85 buzz::FakeXmppClient* client;
86 rtc::scoped_ptr<TestPubSubTasksListener> listener;
92 TEST_F(PubSubTasksTest, TestRequest) {
93 buzz::PubSubRequestTask* task =
94 new buzz::PubSubRequestTask(client, pubsubjid, node);
95 task->SignalResult.connect(
96 listener.get(), &TestPubSubTasksListener::OnRequestResult);
99 std::string expected_iq =
100 "<cli:iq type=\"get\" to=\"room@domain.com\" id=\"0\" "
101 "xmlns:cli=\"jabber:client\">"
102 "<pub:pubsub xmlns:pub=\"http://jabber.org/protocol/pubsub\">"
103 "<pub:items node=\"topic\"/>"
107 ASSERT_EQ(1U, client->sent_stanzas().size());
108 EXPECT_EQ(expected_iq, client->sent_stanzas()[0]->Str());
110 std::string result_iq =
111 "<iq xmlns='jabber:client' id='0' type='result' from='room@domain.com'>"
112 " <pubsub xmlns='http://jabber.org/protocol/pubsub'>"
113 " <items node='topic'>"
124 client->HandleStanza(buzz::XmlElement::ForStr(result_iq));
126 ASSERT_EQ(2U, listener->items.size());
127 EXPECT_EQ("key0", listener->items[0].itemid);
128 EXPECT_EQ("<pub:value0 xmlns:pub=\"http://jabber.org/protocol/pubsub\"/>",
129 listener->items[0].payload);
130 EXPECT_EQ("key1", listener->items[1].itemid);
131 EXPECT_EQ("<pub:value1 xmlns:pub=\"http://jabber.org/protocol/pubsub\"/>",
132 listener->items[1].payload);
135 TEST_F(PubSubTasksTest, TestRequestError) {
136 std::string result_iq =
137 "<iq xmlns='jabber:client' id='0' type='error' from='room@domain.com'>"
138 " <error type='auth'>"
139 " <forbidden xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>"
143 buzz::PubSubRequestTask* task =
144 new buzz::PubSubRequestTask(client, pubsubjid, node);
145 task->SignalResult.connect(
146 listener.get(), &TestPubSubTasksListener::OnRequestResult);
147 task->SignalError.connect(
148 listener.get(), &TestPubSubTasksListener::OnError);
150 client->HandleStanza(buzz::XmlElement::ForStr(result_iq));
152 EXPECT_EQ(0, listener->result_count);
153 EXPECT_EQ(1, listener->error_count);
156 TEST_F(PubSubTasksTest, TestReceive) {
157 std::string items_message =
158 "<message xmlns='jabber:client' from='room@domain.com'>"
159 " <event xmlns='http://jabber.org/protocol/pubsub#event'>"
160 " <items node='topic'>"
171 buzz::PubSubReceiveTask* task =
172 new buzz::PubSubReceiveTask(client, pubsubjid, node);
173 task->SignalUpdate.connect(
174 listener.get(), &TestPubSubTasksListener::OnReceiveUpdate);
176 client->HandleStanza(buzz::XmlElement::ForStr(items_message));
178 ASSERT_EQ(2U, listener->items.size());
179 EXPECT_EQ("key0", listener->items[0].itemid);
181 "<eve:value0 xmlns:eve=\"http://jabber.org/protocol/pubsub#event\"/>",
182 listener->items[0].payload);
183 EXPECT_EQ("key1", listener->items[1].itemid);
185 "<eve:value1 xmlns:eve=\"http://jabber.org/protocol/pubsub#event\"/>",
186 listener->items[1].payload);
189 TEST_F(PubSubTasksTest, TestPublish) {
190 buzz::XmlElement* payload =
191 new buzz::XmlElement(buzz::QName(buzz::NS_PUBSUB, "value"));
192 std::string expected_iq =
193 "<cli:iq type=\"set\" to=\"room@domain.com\" id=\"0\" "
194 "xmlns:cli=\"jabber:client\">"
195 "<pubsub xmlns=\"http://jabber.org/protocol/pubsub\">"
196 "<publish node=\"topic\">"
204 std::vector<buzz::XmlElement*> children;
205 children.push_back(payload);
206 buzz::PubSubPublishTask* task =
207 new buzz::PubSubPublishTask(client, pubsubjid, node, itemid, children);
208 task->SignalResult.connect(
209 listener.get(), &TestPubSubTasksListener::OnPublishResult);
212 ASSERT_EQ(1U, client->sent_stanzas().size());
213 EXPECT_EQ(expected_iq, client->sent_stanzas()[0]->Str());
215 std::string result_iq =
216 "<iq xmlns='jabber:client' id='0' type='result' from='room@domain.com'/>";
218 client->HandleStanza(buzz::XmlElement::ForStr(result_iq));
220 EXPECT_EQ(1, listener->result_count);
221 EXPECT_EQ(0, listener->error_count);
224 TEST_F(PubSubTasksTest, TestPublishError) {
225 buzz::XmlElement* payload =
226 new buzz::XmlElement(buzz::QName(buzz::NS_PUBSUB, "value"));
228 std::vector<buzz::XmlElement*> children;
229 children.push_back(payload);
230 buzz::PubSubPublishTask* task =
231 new buzz::PubSubPublishTask(client, pubsubjid, node, itemid, children);
232 task->SignalResult.connect(
233 listener.get(), &TestPubSubTasksListener::OnPublishResult);
234 task->SignalError.connect(
235 listener.get(), &TestPubSubTasksListener::OnError);
238 std::string result_iq =
239 "<iq xmlns='jabber:client' id='0' type='error' from='room@domain.com'>"
240 " <error type='auth'>"
241 " <forbidden xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>"
245 client->HandleStanza(buzz::XmlElement::ForStr(result_iq));
247 EXPECT_EQ(0, listener->result_count);
248 EXPECT_EQ(1, listener->error_count);
251 TEST_F(PubSubTasksTest, TestRetract) {
252 buzz::PubSubRetractTask* task =
253 new buzz::PubSubRetractTask(client, pubsubjid, node, itemid);
254 task->SignalResult.connect(
255 listener.get(), &TestPubSubTasksListener::OnRetractResult);
256 task->SignalError.connect(
257 listener.get(), &TestPubSubTasksListener::OnError);
260 std::string expected_iq =
261 "<cli:iq type=\"set\" to=\"room@domain.com\" id=\"0\" "
262 "xmlns:cli=\"jabber:client\">"
263 "<pubsub xmlns=\"http://jabber.org/protocol/pubsub\">"
264 "<retract node=\"topic\" notify=\"true\">"
270 ASSERT_EQ(1U, client->sent_stanzas().size());
271 EXPECT_EQ(expected_iq, client->sent_stanzas()[0]->Str());
273 std::string result_iq =
274 "<iq xmlns='jabber:client' id='0' type='result' from='room@domain.com'/>";
276 client->HandleStanza(buzz::XmlElement::ForStr(result_iq));
278 EXPECT_EQ(1, listener->result_count);
279 EXPECT_EQ(0, listener->error_count);