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/mucroomuniquehangoutidtask.h"
35#include "webrtc/base/faketaskrunner.h"
36#include "webrtc/base/gunit.h"
37#include "webrtc/base/sigslot.h"
38
39class MucRoomUniqueHangoutIdListener : public sigslot::has_slots<> {
40 public:
41  MucRoomUniqueHangoutIdListener() : error_count(0) {}
42
43  void OnResult(buzz::MucRoomUniqueHangoutIdTask* task,
44                const std::string& hangout_id) {
45    last_hangout_id = hangout_id;
46  }
47
48  void OnError(buzz::IqTask* task,
49               const buzz::XmlElement* error) {
50    ++error_count;
51  }
52
53  std::string last_hangout_id;
54  int error_count;
55};
56
57class MucRoomUniqueHangoutIdTaskTest : public testing::Test {
58 public:
59  MucRoomUniqueHangoutIdTaskTest() :
60      lookup_server_jid("lookup@domain.com"),
61      hangout_id("some_hangout_id") {
62  }
63
64  virtual void SetUp() {
65    runner = new rtc::FakeTaskRunner();
66    xmpp_client = new buzz::FakeXmppClient(runner);
67    listener = new MucRoomUniqueHangoutIdListener();
68  }
69
70  virtual void TearDown() {
71    delete listener;
72    // delete xmpp_client;  Deleted by deleting runner.
73    delete runner;
74  }
75
76  rtc::FakeTaskRunner* runner;
77  buzz::FakeXmppClient* xmpp_client;
78  MucRoomUniqueHangoutIdListener* listener;
79  buzz::Jid lookup_server_jid;
80  std::string hangout_id;
81};
82
83TEST_F(MucRoomUniqueHangoutIdTaskTest, Test) {
84  ASSERT_EQ(0U, xmpp_client->sent_stanzas().size());
85
86  buzz::MucRoomUniqueHangoutIdTask* task = new buzz::MucRoomUniqueHangoutIdTask(
87      xmpp_client, lookup_server_jid);
88  task->SignalResult.connect(listener, &MucRoomUniqueHangoutIdListener::OnResult);
89  task->Start();
90
91  std::string expected_iq =
92      "<cli:iq type=\"get\" to=\"lookup@domain.com\" id=\"0\" "
93          "xmlns:cli=\"jabber:client\">"
94        "<uni:unique hangout-id=\"true\" "
95          "xmlns:uni=\"http://jabber.org/protocol/muc#unique\"/>"
96      "</cli:iq>";
97
98  ASSERT_EQ(1U, xmpp_client->sent_stanzas().size());
99  EXPECT_EQ(expected_iq, xmpp_client->sent_stanzas()[0]->Str());
100
101  EXPECT_EQ("", listener->last_hangout_id);
102
103  std::string response_iq =
104      "<iq xmlns='jabber:client' from='lookup@domain.com' id='0' type='result'>"
105        "<unique hangout-id=\"some_hangout_id\" "
106            "xmlns=\"http://jabber.org/protocol/muc#unique\">"
107          "muvc-private-chat-00001234-5678-9abc-def0-123456789abc"
108        "</unique>"
109      "</iq>";
110
111  xmpp_client->HandleStanza(buzz::XmlElement::ForStr(response_iq));
112
113  EXPECT_EQ(hangout_id, listener->last_hangout_id);
114  EXPECT_EQ(0, listener->error_count);
115}
116
117