Implement self-contained gpu data collection on Android
[chromium-blink-merge.git] / remoting / signaling / iq_sender_unittest.cc
blobe49829165385f13fe1a09d81d9ababda42dec798
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/bind.h"
6 #include "base/memory/ref_counted.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/run_loop.h"
9 #include "base/strings/stringprintf.h"
10 #include "remoting/signaling/iq_sender.h"
11 #include "remoting/signaling/mock_signal_strategy.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h"
15 #include "third_party/webrtc/libjingle/xmpp/constants.h"
17 using ::testing::_;
18 using ::testing::DeleteArg;
19 using ::testing::InvokeWithoutArgs;
20 using ::testing::NotNull;
21 using ::testing::Return;
22 using ::testing::SaveArg;
24 using ::buzz::QName;
25 using ::buzz::XmlElement;
27 namespace remoting {
29 namespace {
31 const char kStanzaId[] = "123";
32 const char kNamespace[] = "chromium:testns";
33 const char kNamespacePrefix[] = "tes";
34 const char kBodyTag[] = "test";
35 const char kType[] = "get";
36 const char kTo[] = "user@domain.com";
38 class MockCallback {
39 public:
40 MOCK_METHOD2(OnReply, void(IqRequest* request, const XmlElement* reply));
43 MATCHER_P(XmlEq, expected, "") {
44 return arg->Str() == expected->Str();
47 } // namespace
49 class IqSenderTest : public testing::Test {
50 public:
51 IqSenderTest() {
52 EXPECT_CALL(signal_strategy_, AddListener(NotNull()));
53 sender_.reset(new IqSender(&signal_strategy_));
54 EXPECT_CALL(signal_strategy_, RemoveListener(
55 static_cast<SignalStrategy::Listener*>(sender_.get())));
58 protected:
59 void SendTestMessage() {
60 scoped_ptr<XmlElement> iq_body(
61 new XmlElement(QName(kNamespace, kBodyTag)));
62 XmlElement* sent_stanza;
63 EXPECT_CALL(signal_strategy_, GetNextId())
64 .WillOnce(Return(kStanzaId));
65 EXPECT_CALL(signal_strategy_, SendStanzaPtr(_))
66 .WillOnce(DoAll(SaveArg<0>(&sent_stanza), Return(true)));
67 request_ = sender_->SendIq(kType, kTo, iq_body.Pass(), base::Bind(
68 &MockCallback::OnReply, base::Unretained(&callback_)));
70 std::string expected_xml_string =
71 base::StringPrintf(
72 "<cli:iq type=\"%s\" to=\"%s\" id=\"%s\" "
73 "xmlns:cli=\"jabber:client\">"
74 "<%s:%s xmlns:%s=\"%s\"/>"
75 "</cli:iq>",
76 kType, kTo, kStanzaId, kNamespacePrefix, kBodyTag,
77 kNamespacePrefix, kNamespace);
78 EXPECT_EQ(expected_xml_string, sent_stanza->Str());
79 delete sent_stanza;
82 bool FormatAndDeliverResponse(const std::string& from,
83 scoped_ptr<XmlElement>* response_out) {
84 scoped_ptr<XmlElement> response(new XmlElement(buzz::QN_IQ));
85 response->AddAttr(QName(std::string(), "type"), "result");
86 response->AddAttr(QName(std::string(), "id"), kStanzaId);
87 response->AddAttr(QName(std::string(), "from"), from);
89 XmlElement* response_body = new XmlElement(
90 QName("test:namespace", "response-body"));
91 response->AddElement(response_body);
93 bool result = sender_->OnSignalStrategyIncomingStanza(response.get());
95 if (response_out)
96 *response_out = response.Pass();
98 return result;
101 base::MessageLoop message_loop_;
102 MockSignalStrategy signal_strategy_;
103 scoped_ptr<IqSender> sender_;
104 MockCallback callback_;
105 scoped_ptr<IqRequest> request_;
108 TEST_F(IqSenderTest, SendIq) {
109 ASSERT_NO_FATAL_FAILURE({
110 SendTestMessage();
113 scoped_ptr<XmlElement> response;
114 EXPECT_TRUE(FormatAndDeliverResponse(kTo, &response));
116 EXPECT_CALL(callback_, OnReply(request_.get(), XmlEq(response.get())));
117 base::RunLoop().RunUntilIdle();
120 TEST_F(IqSenderTest, Timeout) {
121 ASSERT_NO_FATAL_FAILURE({
122 SendTestMessage();
125 request_->SetTimeout(base::TimeDelta::FromMilliseconds(2));
127 EXPECT_CALL(callback_, OnReply(request_.get(), nullptr))
128 .WillOnce(InvokeWithoutArgs(&message_loop_, &base::MessageLoop::Quit));
129 message_loop_.Run();
132 TEST_F(IqSenderTest, NotNormalizedJid) {
133 ASSERT_NO_FATAL_FAILURE({
134 SendTestMessage();
137 // Set upper-case from value, which is equivalent to kTo in the original
138 // message.
139 scoped_ptr<XmlElement> response;
140 EXPECT_TRUE(FormatAndDeliverResponse("USER@domain.com", &response));
142 EXPECT_CALL(callback_, OnReply(request_.get(), XmlEq(response.get())));
143 base::RunLoop().RunUntilIdle();
146 TEST_F(IqSenderTest, InvalidFrom) {
147 ASSERT_NO_FATAL_FAILURE({
148 SendTestMessage();
151 EXPECT_FALSE(FormatAndDeliverResponse("different_user@domain.com", nullptr));
153 EXPECT_CALL(callback_, OnReply(_, _)).Times(0);
154 base::RunLoop().RunUntilIdle();
157 } // namespace remoting