/* * Copyright (c) 2010 Remko Tronçon * Licensed under the GNU General Public License v3. * See Documentation/Licenses/GPLv3.txt for more information. */ #include #include #include #include #include "Swiften/Elements/ProtocolHeader.h" #include "Swiften/Elements/Presence.h" #include "Swiften/Base/ByteArray.h" #include "Swiften/StreamStack/XMPPLayer.h" #include "Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.h" #include "Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.h" using namespace Swift; class XMPPLayerTest : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(XMPPLayerTest); CPPUNIT_TEST(testParseData_Error); CPPUNIT_TEST(testResetParser); CPPUNIT_TEST(testResetParser_FromSlot); CPPUNIT_TEST(testWriteHeader); CPPUNIT_TEST(testWriteElement); CPPUNIT_TEST(testWriteFooter); CPPUNIT_TEST_SUITE_END(); public: XMPPLayerTest() {} void setUp() { testling_ = new XMPPLayer(&parserFactories_, &serializers_); elementsReceived_ = 0; dataReceived_ = ""; errorReceived_ = 0; } void tearDown() { delete testling_; } void testParseData_Error() { testling_->onError.connect(boost::bind(&XMPPLayerTest::handleError, this)); testling_->parseData(""); CPPUNIT_ASSERT_EQUAL(1, errorReceived_); } void testResetParser() { testling_->onElement.connect(boost::bind(&XMPPLayerTest::handleElement, this, _1)); testling_->onError.connect(boost::bind(&XMPPLayerTest::handleError, this)); testling_->parseData(""); testling_->resetParser(); testling_->parseData(""); testling_->parseData(""); CPPUNIT_ASSERT_EQUAL(1, elementsReceived_); CPPUNIT_ASSERT_EQUAL(0, errorReceived_); } void testResetParser_FromSlot() { testling_->onElement.connect(boost::bind(&XMPPLayerTest::handleElementAndReset, this, _1)); testling_->parseData(""); testling_->parseData(""); CPPUNIT_ASSERT_EQUAL(2, elementsReceived_); CPPUNIT_ASSERT_EQUAL(0, errorReceived_); } void testWriteHeader() { testling_->onWriteData.connect(boost::bind(&XMPPLayerTest::handleWriteData, this, _1)); ProtocolHeader header; header.setTo("example.com"); testling_->writeHeader(header); CPPUNIT_ASSERT_EQUAL(String(""), dataReceived_); } void testWriteElement() { testling_->onWriteData.connect(boost::bind(&XMPPLayerTest::handleWriteData, this, _1)); testling_->writeElement(boost::shared_ptr(new Presence())); CPPUNIT_ASSERT_EQUAL(String(""), dataReceived_); } void testWriteFooter() { testling_->onWriteData.connect(boost::bind(&XMPPLayerTest::handleWriteData, this, _1)); testling_->writeFooter(); CPPUNIT_ASSERT_EQUAL(String(""), dataReceived_); } void handleElement(boost::shared_ptr) { ++elementsReceived_; } void handleElementAndReset(boost::shared_ptr) { ++elementsReceived_; testling_->resetParser(); } void handleWriteData(ByteArray ba) { dataReceived_ += std::string(ba.getData(), ba.getSize()); } void handleError() { ++errorReceived_; } private: FullPayloadParserFactoryCollection parserFactories_; FullPayloadSerializerCollection serializers_; XMPPLayer* testling_; int elementsReceived_; String dataReceived_; int errorReceived_; }; CPPUNIT_TEST_SUITE_REGISTRATION(XMPPLayerTest);