1 /*
2  *  Copyright 2011 The WebRTC Project Authors. All rights reserved.
3  *
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.
9  */
10 
11 #include <string>
12 #include <vector>
13 
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/mucroomuniquehangoutidtask.h"
18 #include "webrtc/base/faketaskrunner.h"
19 #include "webrtc/base/gunit.h"
20 #include "webrtc/base/sigslot.h"
21 
22 class MucRoomUniqueHangoutIdListener : public sigslot::has_slots<> {
23  public:
MucRoomUniqueHangoutIdListener()24   MucRoomUniqueHangoutIdListener() : error_count(0) {}
25 
OnResult(buzz::MucRoomUniqueHangoutIdTask * task,const std::string & hangout_id)26   void OnResult(buzz::MucRoomUniqueHangoutIdTask* task,
27                 const std::string& hangout_id) {
28     last_hangout_id = hangout_id;
29   }
30 
OnError(buzz::IqTask * task,const buzz::XmlElement * error)31   void OnError(buzz::IqTask* task,
32                const buzz::XmlElement* error) {
33     ++error_count;
34   }
35 
36   std::string last_hangout_id;
37   int error_count;
38 };
39 
40 class MucRoomUniqueHangoutIdTaskTest : public testing::Test {
41  public:
MucRoomUniqueHangoutIdTaskTest()42   MucRoomUniqueHangoutIdTaskTest() :
43       lookup_server_jid("lookup@domain.com"),
44       hangout_id("some_hangout_id") {
45   }
46 
SetUp()47   virtual void SetUp() {
48     runner = new rtc::FakeTaskRunner();
49     xmpp_client = new buzz::FakeXmppClient(runner);
50     listener = new MucRoomUniqueHangoutIdListener();
51   }
52 
TearDown()53   virtual void TearDown() {
54     delete listener;
55     // delete xmpp_client;  Deleted by deleting runner.
56     delete runner;
57   }
58 
59   rtc::FakeTaskRunner* runner;
60   buzz::FakeXmppClient* xmpp_client;
61   MucRoomUniqueHangoutIdListener* listener;
62   buzz::Jid lookup_server_jid;
63   std::string hangout_id;
64 };
65 
TEST_F(MucRoomUniqueHangoutIdTaskTest,Test)66 TEST_F(MucRoomUniqueHangoutIdTaskTest, Test) {
67   ASSERT_EQ(0U, xmpp_client->sent_stanzas().size());
68 
69   buzz::MucRoomUniqueHangoutIdTask* task = new buzz::MucRoomUniqueHangoutIdTask(
70       xmpp_client, lookup_server_jid);
71   task->SignalResult.connect(listener, &MucRoomUniqueHangoutIdListener::OnResult);
72   task->Start();
73 
74   std::string expected_iq =
75       "<cli:iq type=\"get\" to=\"lookup@domain.com\" id=\"0\" "
76           "xmlns:cli=\"jabber:client\">"
77         "<uni:unique hangout-id=\"true\" "
78           "xmlns:uni=\"http://jabber.org/protocol/muc#unique\"/>"
79       "</cli:iq>";
80 
81   ASSERT_EQ(1U, xmpp_client->sent_stanzas().size());
82   EXPECT_EQ(expected_iq, xmpp_client->sent_stanzas()[0]->Str());
83 
84   EXPECT_EQ("", listener->last_hangout_id);
85 
86   std::string response_iq =
87       "<iq xmlns='jabber:client' from='lookup@domain.com' id='0' type='result'>"
88         "<unique hangout-id=\"some_hangout_id\" "
89             "xmlns=\"http://jabber.org/protocol/muc#unique\">"
90           "muvc-private-chat-00001234-5678-9abc-def0-123456789abc"
91         "</unique>"
92       "</iq>";
93 
94   xmpp_client->HandleStanza(buzz::XmlElement::ForStr(response_iq));
95 
96   EXPECT_EQ(hangout_id, listener->last_hangout_id);
97   EXPECT_EQ(0, listener->error_count);
98 }
99 
100