Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 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. | |
| 4 | |
| 5 #include "base/bind.h" | |
| 6 #include "base/memory/ref_counted.h" | |
| 7 #include "base/stringprintf.h" | |
| 8 #include "remoting/jingle_glue/iq_sender.h" | |
| 9 #include "remoting/jingle_glue/mock_objects.h" | |
| 10 #include "testing/gmock/include/gmock/gmock.h" | |
| 11 #include "testing/gtest/include/gtest/gtest.h" | |
| 12 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" | |
| 13 #include "third_party/libjingle/source/talk/xmpp/constants.h" | |
| 14 | |
| 15 using ::testing::_; | |
| 16 using ::testing::DeleteArg; | |
| 17 using ::testing::NotNull; | |
| 18 using ::testing::Return; | |
| 19 using ::testing::SaveArg; | |
| 20 | |
| 21 using ::buzz::QName; | |
| 22 using ::buzz::XmlElement; | |
| 23 | |
| 24 namespace remoting { | |
| 25 | |
| 26 namespace { | |
| 27 | |
| 28 const char kMessageId[] = "0"; | |
| 29 const char kNamespace[] = "chromium:testns"; | |
| 30 const char kNamespacePrefix[] = "tes"; | |
| 31 const char kBodyTag[] = "test"; | |
| 32 const char kType[] = "get"; | |
| 33 const char kTo[] = "user@domain.com"; | |
| 34 const char kStanzaId[] = "123"; | |
| 35 | |
| 36 class MockCallback { | |
| 37 public: | |
| 38 MOCK_METHOD1(OnReply, void(const XmlElement* reply)); | |
| 39 }; | |
| 40 | |
| 41 } // namespace | |
| 42 | |
| 43 class IqSenderTest : public testing::Test { | |
| 44 public: | |
| 45 IqSenderTest() { | |
| 46 EXPECT_CALL(signal_strategy_, AddListener(NotNull())); | |
| 47 sender_.reset(new IqSender(&signal_strategy_)); | |
| 48 EXPECT_CALL(signal_strategy_, RemoveListener( | |
| 49 static_cast<SignalStrategy::Listener*>(sender_.get()))); | |
| 50 } | |
| 51 | |
| 52 protected: | |
| 53 MockSignalStrategy signal_strategy_; | |
| 54 scoped_ptr<IqSender> sender_; | |
| 55 MockCallback callback_; | |
| 56 }; | |
| 57 | |
| 58 TEST_F(IqSenderTest, MakeIqStanza) { | |
| 59 std::string expected_xml_string = | |
| 60 base::StringPrintf( | |
| 61 "<cli:iq type=\"%s\" to=\"%s\" id=\"%s\" " | |
| 62 "xmlns:cli=\"jabber:client\">" | |
| 63 "<%s:%s xmlns:%s=\"%s\"/>" | |
| 64 "</cli:iq>", | |
| 65 kType, kTo, kMessageId, kNamespacePrefix, kBodyTag, | |
| 66 kNamespacePrefix, kNamespace); | |
|
Wez
2011/11/03 02:09:30
nit: This entire stanza could be a constant, could
Sergey Ulanov
2011/11/03 02:41:31
Yes, but it means we would duplicate some data. Do
| |
| 67 | |
| 68 XmlElement* iq_body = | |
| 69 new XmlElement(QName(kNamespace, kBodyTag)); | |
| 70 scoped_ptr<XmlElement> stanza( | |
| 71 IqSender::MakeIqStanza(kType, kTo, iq_body)); | |
| 72 stanza->AddAttr(QName("", "id"), kMessageId); | |
| 73 | |
| 74 EXPECT_EQ(expected_xml_string, stanza->Str()); | |
| 75 } | |
| 76 | |
| 77 TEST_F(IqSenderTest, SendIq) { | |
| 78 XmlElement* iq_body = | |
| 79 new XmlElement(QName(kNamespace, kBodyTag)); | |
| 80 XmlElement* stanza = | |
| 81 IqSender::MakeIqStanza(kType, kTo, iq_body); | |
| 82 EXPECT_CALL(signal_strategy_, GetNextId()) | |
| 83 .WillOnce(Return(kStanzaId)); | |
| 84 EXPECT_CALL(signal_strategy_, SendStanza(_)) | |
| 85 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); | |
| 86 sender_->SendIq(stanza, base::Bind(&MockCallback::OnReply, | |
| 87 base::Unretained(&callback_))); | |
| 88 | |
| 89 scoped_ptr<XmlElement> response(new XmlElement(buzz::QN_IQ)); | |
| 90 response->AddAttr(QName("", "type"), "result"); | |
| 91 response->AddAttr(QName("", "id"), kStanzaId); | |
| 92 | |
| 93 XmlElement* result = new XmlElement( | |
| 94 QName("test:namespace", "response-body")); | |
| 95 response->AddElement(result); | |
| 96 | |
| 97 EXPECT_CALL(callback_, OnReply(response.get())); | |
| 98 EXPECT_TRUE(sender_->OnIncomingStanza(response.get())); | |
| 99 } | |
| 100 | |
| 101 } // namespace remoting | |
| OLD | NEW |