diff options
Diffstat (limited to 'Swiften/Presence/UnitTest')
3 files changed, 449 insertions, 449 deletions
diff --git a/Swiften/Presence/UnitTest/DirectedPresenceSenderTest.cpp b/Swiften/Presence/UnitTest/DirectedPresenceSenderTest.cpp index e3885ad..37679a9 100644 --- a/Swiften/Presence/UnitTest/DirectedPresenceSenderTest.cpp +++ b/Swiften/Presence/UnitTest/DirectedPresenceSenderTest.cpp @@ -14,140 +14,140 @@ using namespace Swift; class DirectedPresenceSenderTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(DirectedPresenceSenderTest); - CPPUNIT_TEST(testSendPresence); - CPPUNIT_TEST(testSendPresence_UndirectedPresenceWithDirectedPresenceReceivers); - CPPUNIT_TEST(testSendPresence_DirectedPresenceWithDirectedPresenceReceivers); - CPPUNIT_TEST(testAddDirectedPresenceReceiver); - CPPUNIT_TEST(testAddDirectedPresenceReceiver_WithoutSendingPresence); - CPPUNIT_TEST(testAddDirectedPresenceReceiver_AfterSendingDirectedPresence); - CPPUNIT_TEST(testRemoveDirectedPresenceReceiver); - CPPUNIT_TEST(testRemoveDirectedPresenceReceiver_WithoutSendingPresence); - CPPUNIT_TEST_SUITE_END(); - - public: - void setUp() { - channel = new DummyStanzaChannel(); - testPresence = boost::make_shared<Presence>(); - testPresence->setStatus("Foo"); - secondTestPresence = boost::make_shared<Presence>(); - secondTestPresence->setStatus("Bar"); - stanzaChannelPresenceSender = new StanzaChannelPresenceSender(channel); - } - - void tearDown() { - delete stanzaChannelPresenceSender; - delete channel; - } - - void testSendPresence() { - boost::shared_ptr<DirectedPresenceSender> testling(createPresenceSender()); - testling->sendPresence(testPresence); - - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(channel->sentStanzas.size())); - boost::shared_ptr<Presence> presence = boost::dynamic_pointer_cast<Presence>(channel->sentStanzas[0]); - CPPUNIT_ASSERT(testPresence == presence); - } - - void testSendPresence_UndirectedPresenceWithDirectedPresenceReceivers() { - boost::shared_ptr<DirectedPresenceSender> testling(createPresenceSender()); - testling->addDirectedPresenceReceiver(JID("alice@wonderland.lit/teaparty"), DirectedPresenceSender::AndSendPresence); - - testling->sendPresence(testPresence); - - CPPUNIT_ASSERT_EQUAL(2, static_cast<int>(channel->sentStanzas.size())); - boost::shared_ptr<Presence> presence = boost::dynamic_pointer_cast<Presence>(channel->sentStanzas[0]); - CPPUNIT_ASSERT(testPresence == presence); - presence = boost::dynamic_pointer_cast<Presence>(channel->sentStanzas[1]); - CPPUNIT_ASSERT_EQUAL(testPresence->getStatus(), presence->getStatus()); - CPPUNIT_ASSERT_EQUAL(JID("alice@wonderland.lit/teaparty"), presence->getTo()); - } - - void testSendPresence_DirectedPresenceWithDirectedPresenceReceivers() { - boost::shared_ptr<DirectedPresenceSender> testling(createPresenceSender()); - testling->addDirectedPresenceReceiver(JID("alice@wonderland.lit/teaparty"), DirectedPresenceSender::AndSendPresence); - channel->sentStanzas.clear(); - - testPresence->setTo(JID("foo@bar.com")); - testling->sendPresence(testPresence); - - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(channel->sentStanzas.size())); - boost::shared_ptr<Presence> presence = boost::dynamic_pointer_cast<Presence>(channel->sentStanzas[0]); - CPPUNIT_ASSERT(testPresence == presence); - } - - void testAddDirectedPresenceReceiver() { - boost::shared_ptr<DirectedPresenceSender> testling(createPresenceSender()); - testling->sendPresence(testPresence); - channel->sentStanzas.clear(); - - testling->addDirectedPresenceReceiver(JID("alice@wonderland.lit/teaparty"), DirectedPresenceSender::AndSendPresence); - - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(channel->sentStanzas.size())); - boost::shared_ptr<Presence> presence = boost::dynamic_pointer_cast<Presence>(channel->sentStanzas[0]); - CPPUNIT_ASSERT_EQUAL(testPresence->getStatus(), presence->getStatus()); - CPPUNIT_ASSERT_EQUAL(JID("alice@wonderland.lit/teaparty"), presence->getTo()); - } - - void testAddDirectedPresenceReceiver_WithoutSendingPresence() { - boost::shared_ptr<DirectedPresenceSender> testling(createPresenceSender()); - testling->sendPresence(testPresence); - channel->sentStanzas.clear(); - - testling->addDirectedPresenceReceiver(JID("alice@wonderland.lit/teaparty"), DirectedPresenceSender::DontSendPresence); - - CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(channel->sentStanzas.size())); - } - - void testAddDirectedPresenceReceiver_AfterSendingDirectedPresence() { - boost::shared_ptr<DirectedPresenceSender> testling(createPresenceSender()); - testling->sendPresence(testPresence); - secondTestPresence->setTo(JID("foo@bar.com")); - testling->sendPresence(secondTestPresence); - channel->sentStanzas.clear(); - - testling->addDirectedPresenceReceiver(JID("alice@wonderland.lit/teaparty"), DirectedPresenceSender::AndSendPresence); - - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(channel->sentStanzas.size())); - boost::shared_ptr<Presence> presence = boost::dynamic_pointer_cast<Presence>(channel->sentStanzas[0]); - CPPUNIT_ASSERT_EQUAL(testPresence->getStatus(), presence->getStatus()); - CPPUNIT_ASSERT_EQUAL(JID("alice@wonderland.lit/teaparty"), presence->getTo()); - } - - void testRemoveDirectedPresenceReceiver() { - boost::shared_ptr<DirectedPresenceSender> testling(createPresenceSender()); - testling->addDirectedPresenceReceiver(JID("alice@wonderland.lit/teaparty"), DirectedPresenceSender::DontSendPresence); - - testling->removeDirectedPresenceReceiver(JID("alice@wonderland.lit/teaparty"), DirectedPresenceSender::AndSendPresence); - testling->sendPresence(testPresence); - - CPPUNIT_ASSERT_EQUAL(2, static_cast<int>(channel->sentStanzas.size())); - CPPUNIT_ASSERT_EQUAL(boost::dynamic_pointer_cast<Presence>(channel->sentStanzas[0])->getType(), Presence::Unavailable); - CPPUNIT_ASSERT(channel->sentStanzas[1] == testPresence); - } - - void testRemoveDirectedPresenceReceiver_WithoutSendingPresence() { - boost::shared_ptr<DirectedPresenceSender> testling(createPresenceSender()); - testling->addDirectedPresenceReceiver(JID("alice@wonderland.lit/teaparty"), DirectedPresenceSender::AndSendPresence); - channel->sentStanzas.clear(); - - testling->removeDirectedPresenceReceiver(JID("alice@wonderland.lit/teaparty"), DirectedPresenceSender::DontSendPresence); - testling->sendPresence(testPresence); - - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(channel->sentStanzas.size())); - CPPUNIT_ASSERT(channel->sentStanzas[0] == testPresence); - } - - private: - DirectedPresenceSender* createPresenceSender() { - return new DirectedPresenceSender(stanzaChannelPresenceSender); - } - - private: - DummyStanzaChannel* channel; - StanzaChannelPresenceSender* stanzaChannelPresenceSender; - boost::shared_ptr<Presence> testPresence; - boost::shared_ptr<Presence> secondTestPresence; + CPPUNIT_TEST_SUITE(DirectedPresenceSenderTest); + CPPUNIT_TEST(testSendPresence); + CPPUNIT_TEST(testSendPresence_UndirectedPresenceWithDirectedPresenceReceivers); + CPPUNIT_TEST(testSendPresence_DirectedPresenceWithDirectedPresenceReceivers); + CPPUNIT_TEST(testAddDirectedPresenceReceiver); + CPPUNIT_TEST(testAddDirectedPresenceReceiver_WithoutSendingPresence); + CPPUNIT_TEST(testAddDirectedPresenceReceiver_AfterSendingDirectedPresence); + CPPUNIT_TEST(testRemoveDirectedPresenceReceiver); + CPPUNIT_TEST(testRemoveDirectedPresenceReceiver_WithoutSendingPresence); + CPPUNIT_TEST_SUITE_END(); + + public: + void setUp() { + channel = new DummyStanzaChannel(); + testPresence = boost::make_shared<Presence>(); + testPresence->setStatus("Foo"); + secondTestPresence = boost::make_shared<Presence>(); + secondTestPresence->setStatus("Bar"); + stanzaChannelPresenceSender = new StanzaChannelPresenceSender(channel); + } + + void tearDown() { + delete stanzaChannelPresenceSender; + delete channel; + } + + void testSendPresence() { + boost::shared_ptr<DirectedPresenceSender> testling(createPresenceSender()); + testling->sendPresence(testPresence); + + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(channel->sentStanzas.size())); + boost::shared_ptr<Presence> presence = boost::dynamic_pointer_cast<Presence>(channel->sentStanzas[0]); + CPPUNIT_ASSERT(testPresence == presence); + } + + void testSendPresence_UndirectedPresenceWithDirectedPresenceReceivers() { + boost::shared_ptr<DirectedPresenceSender> testling(createPresenceSender()); + testling->addDirectedPresenceReceiver(JID("alice@wonderland.lit/teaparty"), DirectedPresenceSender::AndSendPresence); + + testling->sendPresence(testPresence); + + CPPUNIT_ASSERT_EQUAL(2, static_cast<int>(channel->sentStanzas.size())); + boost::shared_ptr<Presence> presence = boost::dynamic_pointer_cast<Presence>(channel->sentStanzas[0]); + CPPUNIT_ASSERT(testPresence == presence); + presence = boost::dynamic_pointer_cast<Presence>(channel->sentStanzas[1]); + CPPUNIT_ASSERT_EQUAL(testPresence->getStatus(), presence->getStatus()); + CPPUNIT_ASSERT_EQUAL(JID("alice@wonderland.lit/teaparty"), presence->getTo()); + } + + void testSendPresence_DirectedPresenceWithDirectedPresenceReceivers() { + boost::shared_ptr<DirectedPresenceSender> testling(createPresenceSender()); + testling->addDirectedPresenceReceiver(JID("alice@wonderland.lit/teaparty"), DirectedPresenceSender::AndSendPresence); + channel->sentStanzas.clear(); + + testPresence->setTo(JID("foo@bar.com")); + testling->sendPresence(testPresence); + + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(channel->sentStanzas.size())); + boost::shared_ptr<Presence> presence = boost::dynamic_pointer_cast<Presence>(channel->sentStanzas[0]); + CPPUNIT_ASSERT(testPresence == presence); + } + + void testAddDirectedPresenceReceiver() { + boost::shared_ptr<DirectedPresenceSender> testling(createPresenceSender()); + testling->sendPresence(testPresence); + channel->sentStanzas.clear(); + + testling->addDirectedPresenceReceiver(JID("alice@wonderland.lit/teaparty"), DirectedPresenceSender::AndSendPresence); + + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(channel->sentStanzas.size())); + boost::shared_ptr<Presence> presence = boost::dynamic_pointer_cast<Presence>(channel->sentStanzas[0]); + CPPUNIT_ASSERT_EQUAL(testPresence->getStatus(), presence->getStatus()); + CPPUNIT_ASSERT_EQUAL(JID("alice@wonderland.lit/teaparty"), presence->getTo()); + } + + void testAddDirectedPresenceReceiver_WithoutSendingPresence() { + boost::shared_ptr<DirectedPresenceSender> testling(createPresenceSender()); + testling->sendPresence(testPresence); + channel->sentStanzas.clear(); + + testling->addDirectedPresenceReceiver(JID("alice@wonderland.lit/teaparty"), DirectedPresenceSender::DontSendPresence); + + CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(channel->sentStanzas.size())); + } + + void testAddDirectedPresenceReceiver_AfterSendingDirectedPresence() { + boost::shared_ptr<DirectedPresenceSender> testling(createPresenceSender()); + testling->sendPresence(testPresence); + secondTestPresence->setTo(JID("foo@bar.com")); + testling->sendPresence(secondTestPresence); + channel->sentStanzas.clear(); + + testling->addDirectedPresenceReceiver(JID("alice@wonderland.lit/teaparty"), DirectedPresenceSender::AndSendPresence); + + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(channel->sentStanzas.size())); + boost::shared_ptr<Presence> presence = boost::dynamic_pointer_cast<Presence>(channel->sentStanzas[0]); + CPPUNIT_ASSERT_EQUAL(testPresence->getStatus(), presence->getStatus()); + CPPUNIT_ASSERT_EQUAL(JID("alice@wonderland.lit/teaparty"), presence->getTo()); + } + + void testRemoveDirectedPresenceReceiver() { + boost::shared_ptr<DirectedPresenceSender> testling(createPresenceSender()); + testling->addDirectedPresenceReceiver(JID("alice@wonderland.lit/teaparty"), DirectedPresenceSender::DontSendPresence); + + testling->removeDirectedPresenceReceiver(JID("alice@wonderland.lit/teaparty"), DirectedPresenceSender::AndSendPresence); + testling->sendPresence(testPresence); + + CPPUNIT_ASSERT_EQUAL(2, static_cast<int>(channel->sentStanzas.size())); + CPPUNIT_ASSERT_EQUAL(boost::dynamic_pointer_cast<Presence>(channel->sentStanzas[0])->getType(), Presence::Unavailable); + CPPUNIT_ASSERT(channel->sentStanzas[1] == testPresence); + } + + void testRemoveDirectedPresenceReceiver_WithoutSendingPresence() { + boost::shared_ptr<DirectedPresenceSender> testling(createPresenceSender()); + testling->addDirectedPresenceReceiver(JID("alice@wonderland.lit/teaparty"), DirectedPresenceSender::AndSendPresence); + channel->sentStanzas.clear(); + + testling->removeDirectedPresenceReceiver(JID("alice@wonderland.lit/teaparty"), DirectedPresenceSender::DontSendPresence); + testling->sendPresence(testPresence); + + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(channel->sentStanzas.size())); + CPPUNIT_ASSERT(channel->sentStanzas[0] == testPresence); + } + + private: + DirectedPresenceSender* createPresenceSender() { + return new DirectedPresenceSender(stanzaChannelPresenceSender); + } + + private: + DummyStanzaChannel* channel; + StanzaChannelPresenceSender* stanzaChannelPresenceSender; + boost::shared_ptr<Presence> testPresence; + boost::shared_ptr<Presence> secondTestPresence; }; CPPUNIT_TEST_SUITE_REGISTRATION(DirectedPresenceSenderTest); diff --git a/Swiften/Presence/UnitTest/PayloadAddingPresenceSenderTest.cpp b/Swiften/Presence/UnitTest/PayloadAddingPresenceSenderTest.cpp index 32f671e..84c0838 100644 --- a/Swiften/Presence/UnitTest/PayloadAddingPresenceSenderTest.cpp +++ b/Swiften/Presence/UnitTest/PayloadAddingPresenceSenderTest.cpp @@ -19,128 +19,128 @@ using namespace Swift; class PayloadAddingPresenceSenderTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(PayloadAddingPresenceSenderTest); - CPPUNIT_TEST(testSetPayloadAddsPayloadOnPresenceSend); - CPPUNIT_TEST(testSetNullPayloadDoesNotAddPayloadOnPresenceSend); - CPPUNIT_TEST(testSendPresenceDoesNotAlterOriginalPayload); - CPPUNIT_TEST(testSetPayloadAfterInitialPresenceResendsPresence); - CPPUNIT_TEST(testSetPayloadAfterUnavailablePresenceDoesNotResendPresence); - CPPUNIT_TEST(testSetPayloadAfterResetDoesNotResendPresence); - CPPUNIT_TEST(testSendDirectedPresenceIsNotResent); - CPPUNIT_TEST_SUITE_END(); + CPPUNIT_TEST_SUITE(PayloadAddingPresenceSenderTest); + CPPUNIT_TEST(testSetPayloadAddsPayloadOnPresenceSend); + CPPUNIT_TEST(testSetNullPayloadDoesNotAddPayloadOnPresenceSend); + CPPUNIT_TEST(testSendPresenceDoesNotAlterOriginalPayload); + CPPUNIT_TEST(testSetPayloadAfterInitialPresenceResendsPresence); + CPPUNIT_TEST(testSetPayloadAfterUnavailablePresenceDoesNotResendPresence); + CPPUNIT_TEST(testSetPayloadAfterResetDoesNotResendPresence); + CPPUNIT_TEST(testSendDirectedPresenceIsNotResent); + CPPUNIT_TEST_SUITE_END(); - public: - void setUp() { - stanzaChannel = new DummyStanzaChannel(); - presenceSender = new StanzaChannelPresenceSender(stanzaChannel); - } + public: + void setUp() { + stanzaChannel = new DummyStanzaChannel(); + presenceSender = new StanzaChannelPresenceSender(stanzaChannel); + } - void tearDown() { - delete presenceSender; - delete stanzaChannel; - } + void tearDown() { + delete presenceSender; + delete stanzaChannel; + } - void testSetPayloadAddsPayloadOnPresenceSend() { - boost::shared_ptr<PayloadAddingPresenceSender> testling(createSender()); + void testSetPayloadAddsPayloadOnPresenceSend() { + boost::shared_ptr<PayloadAddingPresenceSender> testling(createSender()); - testling->setPayload(MyPayload::create("foo")); - testling->sendPresence(Presence::create("bar")); + testling->setPayload(MyPayload::create("foo")); + testling->sendPresence(Presence::create("bar")); - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(stanzaChannel->sentStanzas.size())); - CPPUNIT_ASSERT_EQUAL(std::string("bar"), stanzaChannel->getStanzaAtIndex<Presence>(0)->getStatus()); - CPPUNIT_ASSERT(stanzaChannel->getStanzaAtIndex<Presence>(0)->getPayload<MyPayload>()); - } + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(stanzaChannel->sentStanzas.size())); + CPPUNIT_ASSERT_EQUAL(std::string("bar"), stanzaChannel->getStanzaAtIndex<Presence>(0)->getStatus()); + CPPUNIT_ASSERT(stanzaChannel->getStanzaAtIndex<Presence>(0)->getPayload<MyPayload>()); + } - void testSetNullPayloadDoesNotAddPayloadOnPresenceSend() { - boost::shared_ptr<PayloadAddingPresenceSender> testling(createSender()); + void testSetNullPayloadDoesNotAddPayloadOnPresenceSend() { + boost::shared_ptr<PayloadAddingPresenceSender> testling(createSender()); - testling->setPayload(MyPayload::ref()); - testling->sendPresence(Presence::create("bar")); + testling->setPayload(MyPayload::ref()); + testling->sendPresence(Presence::create("bar")); - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(stanzaChannel->sentStanzas.size())); - CPPUNIT_ASSERT_EQUAL(std::string("bar"), stanzaChannel->getStanzaAtIndex<Presence>(0)->getStatus()); - CPPUNIT_ASSERT(!stanzaChannel->getStanzaAtIndex<Presence>(0)->getPayload<MyPayload>()); - } + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(stanzaChannel->sentStanzas.size())); + CPPUNIT_ASSERT_EQUAL(std::string("bar"), stanzaChannel->getStanzaAtIndex<Presence>(0)->getStatus()); + CPPUNIT_ASSERT(!stanzaChannel->getStanzaAtIndex<Presence>(0)->getPayload<MyPayload>()); + } - void testSendPresenceDoesNotAlterOriginalPayload() { - boost::shared_ptr<PayloadAddingPresenceSender> testling(createSender()); + void testSendPresenceDoesNotAlterOriginalPayload() { + boost::shared_ptr<PayloadAddingPresenceSender> testling(createSender()); - testling->setPayload(MyPayload::create("foo")); - Presence::ref presence(Presence::create("bar")); - testling->sendPresence(presence); + testling->setPayload(MyPayload::create("foo")); + Presence::ref presence(Presence::create("bar")); + testling->sendPresence(presence); - CPPUNIT_ASSERT(!presence->getPayload<MyPayload>()); - } + CPPUNIT_ASSERT(!presence->getPayload<MyPayload>()); + } - void testSetPayloadAfterInitialPresenceResendsPresence() { - boost::shared_ptr<PayloadAddingPresenceSender> testling(createSender()); + void testSetPayloadAfterInitialPresenceResendsPresence() { + boost::shared_ptr<PayloadAddingPresenceSender> testling(createSender()); - testling->sendPresence(Presence::create("bar")); - testling->setPayload(MyPayload::create("foo")); + testling->sendPresence(Presence::create("bar")); + testling->setPayload(MyPayload::create("foo")); - CPPUNIT_ASSERT_EQUAL(2, static_cast<int>(stanzaChannel->sentStanzas.size())); - CPPUNIT_ASSERT_EQUAL(std::string("bar"), stanzaChannel->getStanzaAtIndex<Presence>(1)->getStatus()); - CPPUNIT_ASSERT(stanzaChannel->getStanzaAtIndex<Presence>(1)->getPayload<MyPayload>()); - } + CPPUNIT_ASSERT_EQUAL(2, static_cast<int>(stanzaChannel->sentStanzas.size())); + CPPUNIT_ASSERT_EQUAL(std::string("bar"), stanzaChannel->getStanzaAtIndex<Presence>(1)->getStatus()); + CPPUNIT_ASSERT(stanzaChannel->getStanzaAtIndex<Presence>(1)->getPayload<MyPayload>()); + } - void testSetPayloadAfterUnavailablePresenceDoesNotResendPresence() { - boost::shared_ptr<PayloadAddingPresenceSender> testling(createSender()); + void testSetPayloadAfterUnavailablePresenceDoesNotResendPresence() { + boost::shared_ptr<PayloadAddingPresenceSender> testling(createSender()); - testling->sendPresence(Presence::create("bar")); + testling->sendPresence(Presence::create("bar")); - Presence::ref presence = Presence::create("bar"); - presence->setType(Presence::Unavailable); - testling->sendPresence(presence); + Presence::ref presence = Presence::create("bar"); + presence->setType(Presence::Unavailable); + testling->sendPresence(presence); - testling->setPayload(MyPayload::create("foo")); + testling->setPayload(MyPayload::create("foo")); - CPPUNIT_ASSERT_EQUAL(2, static_cast<int>(stanzaChannel->sentStanzas.size())); - } + CPPUNIT_ASSERT_EQUAL(2, static_cast<int>(stanzaChannel->sentStanzas.size())); + } - void testSetPayloadAfterResetDoesNotResendPresence() { - boost::shared_ptr<PayloadAddingPresenceSender> testling(createSender()); - testling->sendPresence(Presence::create("bar")); + void testSetPayloadAfterResetDoesNotResendPresence() { + boost::shared_ptr<PayloadAddingPresenceSender> testling(createSender()); + testling->sendPresence(Presence::create("bar")); - testling->reset(); - testling->setPayload(MyPayload::create("foo")); + testling->reset(); + testling->setPayload(MyPayload::create("foo")); - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(stanzaChannel->sentStanzas.size())); - } + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(stanzaChannel->sentStanzas.size())); + } - void testSendDirectedPresenceIsNotResent() { - boost::shared_ptr<PayloadAddingPresenceSender> testling(createSender()); + void testSendDirectedPresenceIsNotResent() { + boost::shared_ptr<PayloadAddingPresenceSender> testling(createSender()); - testling->sendPresence(Presence::create("bar")); - Presence::ref directedPresence = Presence::create("baz"); - directedPresence->setTo(JID("foo@bar.com")); - testling->sendPresence(directedPresence); - testling->setPayload(MyPayload::create("foo")); + testling->sendPresence(Presence::create("bar")); + Presence::ref directedPresence = Presence::create("baz"); + directedPresence->setTo(JID("foo@bar.com")); + testling->sendPresence(directedPresence); + testling->setPayload(MyPayload::create("foo")); - CPPUNIT_ASSERT_EQUAL(3, static_cast<int>(stanzaChannel->sentStanzas.size())); - CPPUNIT_ASSERT_EQUAL(std::string("bar"), stanzaChannel->getStanzaAtIndex<Presence>(2)->getStatus()); - } + CPPUNIT_ASSERT_EQUAL(3, static_cast<int>(stanzaChannel->sentStanzas.size())); + CPPUNIT_ASSERT_EQUAL(std::string("bar"), stanzaChannel->getStanzaAtIndex<Presence>(2)->getStatus()); + } - private: - boost::shared_ptr<PayloadAddingPresenceSender> createSender() { - boost::shared_ptr<PayloadAddingPresenceSender> sender(new PayloadAddingPresenceSender(presenceSender)); - return sender; - } + private: + boost::shared_ptr<PayloadAddingPresenceSender> createSender() { + boost::shared_ptr<PayloadAddingPresenceSender> sender(new PayloadAddingPresenceSender(presenceSender)); + return sender; + } - struct MyPayload : public Payload { - typedef boost::shared_ptr<MyPayload> ref; + struct MyPayload : public Payload { + typedef boost::shared_ptr<MyPayload> ref; - MyPayload(const std::string& body) : body(body) {} + MyPayload(const std::string& body) : body(body) {} - static ref create(const std::string& body) { - return ref(new MyPayload(body)); - } + static ref create(const std::string& body) { + return ref(new MyPayload(body)); + } - std::string body; - }; + std::string body; + }; - private: - DummyStanzaChannel* stanzaChannel; - StanzaChannelPresenceSender* presenceSender; + private: + DummyStanzaChannel* stanzaChannel; + StanzaChannelPresenceSender* presenceSender; }; CPPUNIT_TEST_SUITE_REGISTRATION(PayloadAddingPresenceSenderTest); diff --git a/Swiften/Presence/UnitTest/PresenceOracleTest.cpp b/Swiften/Presence/UnitTest/PresenceOracleTest.cpp index 6d16d80..4f066c7 100644 --- a/Swiften/Presence/UnitTest/PresenceOracleTest.cpp +++ b/Swiften/Presence/UnitTest/PresenceOracleTest.cpp @@ -19,230 +19,230 @@ using namespace Swift; class PresenceOracleTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(PresenceOracleTest); - CPPUNIT_TEST(testReceivePresence); - CPPUNIT_TEST(testReceivePresenceFromDifferentResources); - CPPUNIT_TEST(testSubscriptionRequest); - CPPUNIT_TEST(testReconnectResetsPresences); - CPPUNIT_TEST(testHighestPresenceSingle); - CPPUNIT_TEST(testHighestPresenceMultiple); - CPPUNIT_TEST(testHighestPresenceGlobal); - CPPUNIT_TEST(testHighestPresenceChangePriority); - CPPUNIT_TEST(testGetActivePresence); - CPPUNIT_TEST_SUITE_END(); - - public: - void setUp() { - stanzaChannel_ = new DummyStanzaChannel(); - xmppRoster_ = new XMPPRosterImpl(); - - oracle_ = new PresenceOracle(stanzaChannel_, xmppRoster_); - oracle_->onPresenceChange.connect(boost::bind(&PresenceOracleTest::handlePresenceChange, this, _1)); - subscriptionManager_ = new SubscriptionManager(stanzaChannel_); - subscriptionManager_->onPresenceSubscriptionRequest.connect(boost::bind(&PresenceOracleTest::handlePresenceSubscriptionRequest, this, _1, _2)); - user1 = JID("user1@foo.com/Foo"); - user1alt = JID("user1@foo.com/Bar"); - user2 = JID("user2@bar.com/Bar"); - } - - void tearDown() { - delete subscriptionManager_; - delete oracle_; - delete xmppRoster_; - delete stanzaChannel_; - } - - void testHighestPresenceSingle() { - JID bareJID("alice@wonderland.lit"); - Presence::ref fiveOn = makeOnline("blah", 5); - Presence::ref fiveOff = makeOffline("/blah"); - CPPUNIT_ASSERT_EQUAL(Presence::ref(), oracle_->getHighestPriorityPresence(bareJID)); - stanzaChannel_->onPresenceReceived(fiveOn); - CPPUNIT_ASSERT_EQUAL(fiveOn, oracle_->getHighestPriorityPresence(bareJID)); - stanzaChannel_->onPresenceReceived(fiveOff); - CPPUNIT_ASSERT_EQUAL(fiveOff, oracle_->getHighestPriorityPresence(bareJID)); - } - - void testHighestPresenceMultiple() { - JID bareJID("alice@wonderland.lit"); - Presence::ref fiveOn = makeOnline("blah", 5); - Presence::ref fiveOff = makeOffline("/blah"); - Presence::ref tenOn = makeOnline("bert", 10); - Presence::ref tenOff = makeOffline("/bert"); - stanzaChannel_->onPresenceReceived(fiveOn); - stanzaChannel_->onPresenceReceived(tenOn); - CPPUNIT_ASSERT_EQUAL(tenOn, oracle_->getHighestPriorityPresence(bareJID)); - stanzaChannel_->onPresenceReceived(fiveOff); - CPPUNIT_ASSERT_EQUAL(tenOn, oracle_->getHighestPriorityPresence(bareJID)); - stanzaChannel_->onPresenceReceived(fiveOn); - stanzaChannel_->onPresenceReceived(tenOff); - CPPUNIT_ASSERT_EQUAL(fiveOn, oracle_->getHighestPriorityPresence(bareJID)); - } - - void testHighestPresenceGlobal() { - JID bareJID("alice@wonderland.lit"); - Presence::ref fiveOn = makeOnline("blah", 5); - Presence::ref fiveOff = makeOffline("/blah"); - Presence::ref tenOn = makeOnline("bert", 10); - Presence::ref allOff = makeOffline(""); - stanzaChannel_->onPresenceReceived(fiveOn); - stanzaChannel_->onPresenceReceived(tenOn); - stanzaChannel_->onPresenceReceived(allOff); - CPPUNIT_ASSERT_EQUAL(allOff, oracle_->getHighestPriorityPresence(bareJID)); - } - - void testHighestPresenceChangePriority() { - JID bareJID("alice@wonderland.lit"); - Presence::ref fiveOn = makeOnline("blah", 5); - Presence::ref fiveOff = makeOffline("/blah"); - Presence::ref tenOn = makeOnline("bert", 10); - Presence::ref tenOnThree = makeOnline("bert", 3); - Presence::ref tenOff = makeOffline("/bert"); - stanzaChannel_->onPresenceReceived(fiveOn); - stanzaChannel_->onPresenceReceived(tenOn); - stanzaChannel_->onPresenceReceived(tenOnThree); - CPPUNIT_ASSERT_EQUAL(fiveOn, oracle_->getHighestPriorityPresence(bareJID)); - stanzaChannel_->onPresenceReceived(fiveOff); - CPPUNIT_ASSERT_EQUAL(tenOnThree, oracle_->getHighestPriorityPresence(bareJID)); - stanzaChannel_->onPresenceReceived(fiveOn); - CPPUNIT_ASSERT_EQUAL(fiveOn, oracle_->getHighestPriorityPresence(bareJID)); - } - - void testReceivePresence() { - boost::shared_ptr<Presence> sentPresence(createPresence(user1)); - stanzaChannel_->onPresenceReceived(sentPresence); - - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(changes.size())); - CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(subscriptionRequests.size())); - CPPUNIT_ASSERT_EQUAL(sentPresence, changes[0]); - CPPUNIT_ASSERT_EQUAL(sentPresence, oracle_->getLastPresence(user1)); - } - - void testReceivePresenceFromDifferentResources() { - boost::shared_ptr<Presence> sentPresence1(createPresence(user1)); - boost::shared_ptr<Presence> sentPresence2(createPresence(user1alt)); - stanzaChannel_->onPresenceReceived(sentPresence1); - stanzaChannel_->onPresenceReceived(sentPresence2); - - CPPUNIT_ASSERT_EQUAL(sentPresence1, oracle_->getLastPresence(user1)); - CPPUNIT_ASSERT_EQUAL(sentPresence2, oracle_->getLastPresence(user1alt)); - } - - void testSubscriptionRequest() { - std::string reasonText = "Because I want to"; - JID sentJID = JID("me@example.com"); - - boost::shared_ptr<Presence> sentPresence(new Presence()); - sentPresence->setType(Presence::Subscribe); - sentPresence->setFrom(sentJID); - sentPresence->setStatus(reasonText); - stanzaChannel_->onPresenceReceived(sentPresence); - - CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(changes.size())); - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(subscriptionRequests.size())); - CPPUNIT_ASSERT_EQUAL(sentJID, subscriptionRequests[0].jid); - CPPUNIT_ASSERT_EQUAL(reasonText, subscriptionRequests[0].reason); - } - - void testReconnectResetsPresences() { - boost::shared_ptr<Presence> sentPresence(createPresence(user1)); - stanzaChannel_->onPresenceReceived(sentPresence); - stanzaChannel_->setAvailable(false); - stanzaChannel_->setAvailable(true); - - CPPUNIT_ASSERT(!oracle_->getLastPresence(user1)); - } - - void testGetActivePresence() { - { - std::vector<Presence::ref> presenceList; - presenceList.push_back(createPresence("alice@wonderland.lit/resourceA", 10, Presence::Available, StatusShow::Away)); - presenceList.push_back(createPresence("alice@wonderland.lit/resourceB", 5, Presence::Available, StatusShow::Online)); - - CPPUNIT_ASSERT_EQUAL(StatusShow::Online, PresenceOracle::getActivePresence(presenceList)->getShow()); - } - - { - std::vector<Presence::ref> presenceList; - presenceList.push_back(createPresence("alice@wonderland.lit/resourceA", 10, Presence::Available, StatusShow::Away)); - presenceList.push_back(createPresence("alice@wonderland.lit/resourceB", 5, Presence::Available, StatusShow::DND)); - - CPPUNIT_ASSERT_EQUAL(StatusShow::DND, PresenceOracle::getActivePresence(presenceList)->getShow()); - } - - { - std::vector<Presence::ref> presenceList; - presenceList.push_back(createPresence("alice@wonderland.lit/resourceA", 0, Presence::Available, StatusShow::Online)); - presenceList.push_back(createPresence("alice@wonderland.lit/resourceB", 0, Presence::Available, StatusShow::DND)); - - CPPUNIT_ASSERT_EQUAL(StatusShow::Online, PresenceOracle::getActivePresence(presenceList)->getShow()); - } - - { - std::vector<Presence::ref> presenceList; - presenceList.push_back(createPresence("alice@wonderland.lit/resourceA", 1, Presence::Available, StatusShow::Online)); - presenceList.push_back(createPresence("alice@wonderland.lit/resourceB", 0, Presence::Available, StatusShow::Online)); - - CPPUNIT_ASSERT_EQUAL(JID("alice@wonderland.lit/resourceA"), PresenceOracle::getActivePresence(presenceList)->getFrom()); - } - } - - private: - Presence::ref createPresence(const JID &jid, int priority, Presence::Type type, const StatusShow::Type& statusShow) { - Presence::ref presence = boost::make_shared<Presence>(); - presence->setFrom(jid); - presence->setPriority(priority); - presence->setType(type); - presence->setShow(statusShow); - return presence; - } - - - Presence::ref makeOnline(const std::string& resource, int priority) { - Presence::ref presence(new Presence()); - presence->setPriority(priority); - presence->setFrom(JID("alice@wonderland.lit/" + resource)); - return presence; - } - - Presence::ref makeOffline(const std::string& resource) { - Presence::ref presence(new Presence()); - presence->setFrom(JID("alice@wonderland.lit" + resource)); - presence->setType(Presence::Unavailable); - return presence; - } - - void handlePresenceChange(boost::shared_ptr<Presence> newPresence) { - changes.push_back(newPresence); - } - - void handlePresenceSubscriptionRequest(const JID& jid, const std::string& reason) { - SubscriptionRequestInfo subscriptionRequest; - subscriptionRequest.jid = jid; - subscriptionRequest.reason = reason; - subscriptionRequests.push_back(subscriptionRequest); - } - - boost::shared_ptr<Presence> createPresence(const JID& jid) { - boost::shared_ptr<Presence> sentPresence(new Presence("blarb")); - sentPresence->setFrom(jid); - return sentPresence; - } - - private: - struct SubscriptionRequestInfo { - JID jid; - std::string reason; - }; - PresenceOracle* oracle_; - SubscriptionManager* subscriptionManager_; - DummyStanzaChannel* stanzaChannel_; - XMPPRoster* xmppRoster_; - std::vector<Presence::ref> changes; - std::vector<SubscriptionRequestInfo> subscriptionRequests; - JID user1; - JID user1alt; - JID user2; + CPPUNIT_TEST_SUITE(PresenceOracleTest); + CPPUNIT_TEST(testReceivePresence); + CPPUNIT_TEST(testReceivePresenceFromDifferentResources); + CPPUNIT_TEST(testSubscriptionRequest); + CPPUNIT_TEST(testReconnectResetsPresences); + CPPUNIT_TEST(testHighestPresenceSingle); + CPPUNIT_TEST(testHighestPresenceMultiple); + CPPUNIT_TEST(testHighestPresenceGlobal); + CPPUNIT_TEST(testHighestPresenceChangePriority); + CPPUNIT_TEST(testGetActivePresence); + CPPUNIT_TEST_SUITE_END(); + + public: + void setUp() { + stanzaChannel_ = new DummyStanzaChannel(); + xmppRoster_ = new XMPPRosterImpl(); + + oracle_ = new PresenceOracle(stanzaChannel_, xmppRoster_); + oracle_->onPresenceChange.connect(boost::bind(&PresenceOracleTest::handlePresenceChange, this, _1)); + subscriptionManager_ = new SubscriptionManager(stanzaChannel_); + subscriptionManager_->onPresenceSubscriptionRequest.connect(boost::bind(&PresenceOracleTest::handlePresenceSubscriptionRequest, this, _1, _2)); + user1 = JID("user1@foo.com/Foo"); + user1alt = JID("user1@foo.com/Bar"); + user2 = JID("user2@bar.com/Bar"); + } + + void tearDown() { + delete subscriptionManager_; + delete oracle_; + delete xmppRoster_; + delete stanzaChannel_; + } + + void testHighestPresenceSingle() { + JID bareJID("alice@wonderland.lit"); + Presence::ref fiveOn = makeOnline("blah", 5); + Presence::ref fiveOff = makeOffline("/blah"); + CPPUNIT_ASSERT_EQUAL(Presence::ref(), oracle_->getHighestPriorityPresence(bareJID)); + stanzaChannel_->onPresenceReceived(fiveOn); + CPPUNIT_ASSERT_EQUAL(fiveOn, oracle_->getHighestPriorityPresence(bareJID)); + stanzaChannel_->onPresenceReceived(fiveOff); + CPPUNIT_ASSERT_EQUAL(fiveOff, oracle_->getHighestPriorityPresence(bareJID)); + } + + void testHighestPresenceMultiple() { + JID bareJID("alice@wonderland.lit"); + Presence::ref fiveOn = makeOnline("blah", 5); + Presence::ref fiveOff = makeOffline("/blah"); + Presence::ref tenOn = makeOnline("bert", 10); + Presence::ref tenOff = makeOffline("/bert"); + stanzaChannel_->onPresenceReceived(fiveOn); + stanzaChannel_->onPresenceReceived(tenOn); + CPPUNIT_ASSERT_EQUAL(tenOn, oracle_->getHighestPriorityPresence(bareJID)); + stanzaChannel_->onPresenceReceived(fiveOff); + CPPUNIT_ASSERT_EQUAL(tenOn, oracle_->getHighestPriorityPresence(bareJID)); + stanzaChannel_->onPresenceReceived(fiveOn); + stanzaChannel_->onPresenceReceived(tenOff); + CPPUNIT_ASSERT_EQUAL(fiveOn, oracle_->getHighestPriorityPresence(bareJID)); + } + + void testHighestPresenceGlobal() { + JID bareJID("alice@wonderland.lit"); + Presence::ref fiveOn = makeOnline("blah", 5); + Presence::ref fiveOff = makeOffline("/blah"); + Presence::ref tenOn = makeOnline("bert", 10); + Presence::ref allOff = makeOffline(""); + stanzaChannel_->onPresenceReceived(fiveOn); + stanzaChannel_->onPresenceReceived(tenOn); + stanzaChannel_->onPresenceReceived(allOff); + CPPUNIT_ASSERT_EQUAL(allOff, oracle_->getHighestPriorityPresence(bareJID)); + } + + void testHighestPresenceChangePriority() { + JID bareJID("alice@wonderland.lit"); + Presence::ref fiveOn = makeOnline("blah", 5); + Presence::ref fiveOff = makeOffline("/blah"); + Presence::ref tenOn = makeOnline("bert", 10); + Presence::ref tenOnThree = makeOnline("bert", 3); + Presence::ref tenOff = makeOffline("/bert"); + stanzaChannel_->onPresenceReceived(fiveOn); + stanzaChannel_->onPresenceReceived(tenOn); + stanzaChannel_->onPresenceReceived(tenOnThree); + CPPUNIT_ASSERT_EQUAL(fiveOn, oracle_->getHighestPriorityPresence(bareJID)); + stanzaChannel_->onPresenceReceived(fiveOff); + CPPUNIT_ASSERT_EQUAL(tenOnThree, oracle_->getHighestPriorityPresence(bareJID)); + stanzaChannel_->onPresenceReceived(fiveOn); + CPPUNIT_ASSERT_EQUAL(fiveOn, oracle_->getHighestPriorityPresence(bareJID)); + } + + void testReceivePresence() { + boost::shared_ptr<Presence> sentPresence(createPresence(user1)); + stanzaChannel_->onPresenceReceived(sentPresence); + + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(changes.size())); + CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(subscriptionRequests.size())); + CPPUNIT_ASSERT_EQUAL(sentPresence, changes[0]); + CPPUNIT_ASSERT_EQUAL(sentPresence, oracle_->getLastPresence(user1)); + } + + void testReceivePresenceFromDifferentResources() { + boost::shared_ptr<Presence> sentPresence1(createPresence(user1)); + boost::shared_ptr<Presence> sentPresence2(createPresence(user1alt)); + stanzaChannel_->onPresenceReceived(sentPresence1); + stanzaChannel_->onPresenceReceived(sentPresence2); + + CPPUNIT_ASSERT_EQUAL(sentPresence1, oracle_->getLastPresence(user1)); + CPPUNIT_ASSERT_EQUAL(sentPresence2, oracle_->getLastPresence(user1alt)); + } + + void testSubscriptionRequest() { + std::string reasonText = "Because I want to"; + JID sentJID = JID("me@example.com"); + + boost::shared_ptr<Presence> sentPresence(new Presence()); + sentPresence->setType(Presence::Subscribe); + sentPresence->setFrom(sentJID); + sentPresence->setStatus(reasonText); + stanzaChannel_->onPresenceReceived(sentPresence); + + CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(changes.size())); + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(subscriptionRequests.size())); + CPPUNIT_ASSERT_EQUAL(sentJID, subscriptionRequests[0].jid); + CPPUNIT_ASSERT_EQUAL(reasonText, subscriptionRequests[0].reason); + } + + void testReconnectResetsPresences() { + boost::shared_ptr<Presence> sentPresence(createPresence(user1)); + stanzaChannel_->onPresenceReceived(sentPresence); + stanzaChannel_->setAvailable(false); + stanzaChannel_->setAvailable(true); + + CPPUNIT_ASSERT(!oracle_->getLastPresence(user1)); + } + + void testGetActivePresence() { + { + std::vector<Presence::ref> presenceList; + presenceList.push_back(createPresence("alice@wonderland.lit/resourceA", 10, Presence::Available, StatusShow::Away)); + presenceList.push_back(createPresence("alice@wonderland.lit/resourceB", 5, Presence::Available, StatusShow::Online)); + + CPPUNIT_ASSERT_EQUAL(StatusShow::Online, PresenceOracle::getActivePresence(presenceList)->getShow()); + } + + { + std::vector<Presence::ref> presenceList; + presenceList.push_back(createPresence("alice@wonderland.lit/resourceA", 10, Presence::Available, StatusShow::Away)); + presenceList.push_back(createPresence("alice@wonderland.lit/resourceB", 5, Presence::Available, StatusShow::DND)); + + CPPUNIT_ASSERT_EQUAL(StatusShow::DND, PresenceOracle::getActivePresence(presenceList)->getShow()); + } + + { + std::vector<Presence::ref> presenceList; + presenceList.push_back(createPresence("alice@wonderland.lit/resourceA", 0, Presence::Available, StatusShow::Online)); + presenceList.push_back(createPresence("alice@wonderland.lit/resourceB", 0, Presence::Available, StatusShow::DND)); + + CPPUNIT_ASSERT_EQUAL(StatusShow::Online, PresenceOracle::getActivePresence(presenceList)->getShow()); + } + + { + std::vector<Presence::ref> presenceList; + presenceList.push_back(createPresence("alice@wonderland.lit/resourceA", 1, Presence::Available, StatusShow::Online)); + presenceList.push_back(createPresence("alice@wonderland.lit/resourceB", 0, Presence::Available, StatusShow::Online)); + + CPPUNIT_ASSERT_EQUAL(JID("alice@wonderland.lit/resourceA"), PresenceOracle::getActivePresence(presenceList)->getFrom()); + } + } + + private: + Presence::ref createPresence(const JID &jid, int priority, Presence::Type type, const StatusShow::Type& statusShow) { + Presence::ref presence = boost::make_shared<Presence>(); + presence->setFrom(jid); + presence->setPriority(priority); + presence->setType(type); + presence->setShow(statusShow); + return presence; + } + + + Presence::ref makeOnline(const std::string& resource, int priority) { + Presence::ref presence(new Presence()); + presence->setPriority(priority); + presence->setFrom(JID("alice@wonderland.lit/" + resource)); + return presence; + } + + Presence::ref makeOffline(const std::string& resource) { + Presence::ref presence(new Presence()); + presence->setFrom(JID("alice@wonderland.lit" + resource)); + presence->setType(Presence::Unavailable); + return presence; + } + + void handlePresenceChange(boost::shared_ptr<Presence> newPresence) { + changes.push_back(newPresence); + } + + void handlePresenceSubscriptionRequest(const JID& jid, const std::string& reason) { + SubscriptionRequestInfo subscriptionRequest; + subscriptionRequest.jid = jid; + subscriptionRequest.reason = reason; + subscriptionRequests.push_back(subscriptionRequest); + } + + boost::shared_ptr<Presence> createPresence(const JID& jid) { + boost::shared_ptr<Presence> sentPresence(new Presence("blarb")); + sentPresence->setFrom(jid); + return sentPresence; + } + + private: + struct SubscriptionRequestInfo { + JID jid; + std::string reason; + }; + PresenceOracle* oracle_; + SubscriptionManager* subscriptionManager_; + DummyStanzaChannel* stanzaChannel_; + XMPPRoster* xmppRoster_; + std::vector<Presence::ref> changes; + std::vector<SubscriptionRequestInfo> subscriptionRequests; + JID user1; + JID user1alt; + JID user2; }; CPPUNIT_TEST_SUITE_REGISTRATION(PresenceOracleTest); |