From c198064db0a6444a24220eee65cbf747eb7fbfb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Remko=20Tron=C3=A7on?= Date: Wed, 25 Nov 2009 18:39:10 +0100 Subject: Added PresenceSenderTest. diff --git a/Swiften/Presence/UnitTest/PresenceSenderTest.cpp b/Swiften/Presence/UnitTest/PresenceSenderTest.cpp new file mode 100644 index 0000000..1df0269 --- /dev/null +++ b/Swiften/Presence/UnitTest/PresenceSenderTest.cpp @@ -0,0 +1,120 @@ +#include +#include + +#include "Swiften/Client/DummyStanzaChannel.h" +#include "Swiften/Presence/PresenceSender.h" + +using namespace Swift; + +class PresenceSenderTest : public CppUnit::TestFixture { + CPPUNIT_TEST_SUITE(PresenceSenderTest); + CPPUNIT_TEST(testSendPresence); + CPPUNIT_TEST(testSendPresence_UndirectedPresenceWithDirectedPresenceReceivers); + CPPUNIT_TEST(testSendPresence_DirectedPresenceWithDirectedPresenceReceivers); + CPPUNIT_TEST(testAddDirectedPresenceReceiver); + CPPUNIT_TEST(testAddDirectedPresenceReceiver_AfterSendingDirectedPresence); + CPPUNIT_TEST(testRemoveDirectedPresenceReceiver); + CPPUNIT_TEST_SUITE_END(); + + public: + void setUp() { + channel = new DummyStanzaChannel(); + testPresence = boost::shared_ptr(new Presence()); + testPresence->setStatus("Foo"); + secondTestPresence = boost::shared_ptr(new Presence()); + secondTestPresence->setStatus("Bar"); + } + + void tearDown() { + delete channel; + } + + void testSendPresence() { + std::auto_ptr testling(createPresenceSender()); + testling->sendPresence(testPresence); + + CPPUNIT_ASSERT_EQUAL(1, static_cast(channel->sentStanzas.size())); + boost::shared_ptr presence = boost::dynamic_pointer_cast(channel->sentStanzas[0]); + CPPUNIT_ASSERT(testPresence == presence); + } + + void testSendPresence_UndirectedPresenceWithDirectedPresenceReceivers() { + std::auto_ptr testling(createPresenceSender()); + testling->addDirectedPresenceReceiver(JID("alice@wonderland.lit/teaparty")); + + testling->sendPresence(testPresence); + + CPPUNIT_ASSERT_EQUAL(2, static_cast(channel->sentStanzas.size())); + boost::shared_ptr presence = boost::dynamic_pointer_cast(channel->sentStanzas[0]); + CPPUNIT_ASSERT(testPresence == presence); + presence = boost::dynamic_pointer_cast(channel->sentStanzas[1]); + CPPUNIT_ASSERT_EQUAL(testPresence->getStatus(), presence->getStatus()); + CPPUNIT_ASSERT_EQUAL(JID("alice@wonderland.lit/teaparty"), presence->getTo()); + } + + void testSendPresence_DirectedPresenceWithDirectedPresenceReceivers() { + std::auto_ptr testling(createPresenceSender()); + testling->addDirectedPresenceReceiver(JID("alice@wonderland.lit/teaparty")); + channel->sentStanzas.clear(); + + testPresence->setTo(JID("foo@bar.com")); + testling->sendPresence(testPresence); + + CPPUNIT_ASSERT_EQUAL(1, static_cast(channel->sentStanzas.size())); + boost::shared_ptr presence = boost::dynamic_pointer_cast(channel->sentStanzas[0]); + CPPUNIT_ASSERT(testPresence == presence); + } + + void testAddDirectedPresenceReceiver() { + std::auto_ptr testling(createPresenceSender()); + testling->sendPresence(testPresence); + channel->sentStanzas.clear(); + + testling->addDirectedPresenceReceiver(JID("alice@wonderland.lit/teaparty")); + + CPPUNIT_ASSERT_EQUAL(1, static_cast(channel->sentStanzas.size())); + boost::shared_ptr presence = boost::dynamic_pointer_cast(channel->sentStanzas[0]); + CPPUNIT_ASSERT_EQUAL(testPresence->getStatus(), presence->getStatus()); + CPPUNIT_ASSERT_EQUAL(JID("alice@wonderland.lit/teaparty"), presence->getTo()); + } + + void testAddDirectedPresenceReceiver_AfterSendingDirectedPresence() { + std::auto_ptr testling(createPresenceSender()); + testling->sendPresence(testPresence); + secondTestPresence->setTo(JID("foo@bar.com")); + testling->sendPresence(secondTestPresence); + channel->sentStanzas.clear(); + + testling->addDirectedPresenceReceiver(JID("alice@wonderland.lit/teaparty")); + + CPPUNIT_ASSERT_EQUAL(1, static_cast(channel->sentStanzas.size())); + boost::shared_ptr presence = boost::dynamic_pointer_cast(channel->sentStanzas[0]); + CPPUNIT_ASSERT_EQUAL(testPresence->getStatus(), presence->getStatus()); + CPPUNIT_ASSERT_EQUAL(JID("alice@wonderland.lit/teaparty"), presence->getTo()); + } + + void testRemoveDirectedPresenceReceiver() { + std::auto_ptr testling(createPresenceSender()); + testling->addDirectedPresenceReceiver(JID("alice@wonderland.lit/teaparty")); + channel->sentStanzas.clear(); + + testling->removeDirectedPresenceReceiver(JID("alice@wonderland.lit/teaparty")); + testling->sendPresence(testPresence); + + CPPUNIT_ASSERT_EQUAL(2, static_cast(channel->sentStanzas.size())); + CPPUNIT_ASSERT_EQUAL(boost::dynamic_pointer_cast(channel->sentStanzas[0])->getType(), Presence::Unavailable); + CPPUNIT_ASSERT(channel->sentStanzas[1] == testPresence); + } + + private: + PresenceSender* createPresenceSender() { + return new PresenceSender(channel); + } + + private: + DummyStanzaChannel* channel; + boost::shared_ptr testPresence; + boost::shared_ptr secondTestPresence; +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(PresenceSenderTest); -- cgit v0.10.2-6-g49f6