diff options
Diffstat (limited to 'Swiften/Presence/UnitTest')
-rw-r--r-- | Swiften/Presence/UnitTest/PresenceOracleTest.cpp | 143 |
1 files changed, 65 insertions, 78 deletions
diff --git a/Swiften/Presence/UnitTest/PresenceOracleTest.cpp b/Swiften/Presence/UnitTest/PresenceOracleTest.cpp index 691beb7..e96d8a4 100644 --- a/Swiften/Presence/UnitTest/PresenceOracleTest.cpp +++ b/Swiften/Presence/UnitTest/PresenceOracleTest.cpp @@ -8,58 +8,29 @@ #include <cppunit/extensions/TestFactoryRegistry.h> #include <boost/bind.hpp> #include <boost/shared_ptr.hpp> -#include <boost/optional.hpp> #include "Swiften/Presence/PresenceOracle.h" #include "Swiften/Client/DummyStanzaChannel.h" using namespace Swift; -class PresencePointerPair { - public: - boost::shared_ptr<Presence> one; - boost::shared_ptr<Presence> two; -}; - -class SubscriptionRequestInfo { - public: - boost::optional<JID> jid; - boost::optional<String> reason; -}; - -class PresenceOracleTest : public CppUnit::TestFixture -{ +class PresenceOracleTest : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(PresenceOracleTest); - CPPUNIT_TEST(testFirstPresence); - CPPUNIT_TEST(testSecondPresence); + CPPUNIT_TEST(testReceivePresence); + CPPUNIT_TEST(testReceivePresenceFromDifferentResources); CPPUNIT_TEST(testSubscriptionRequest); + CPPUNIT_TEST(testReconnectResetsPresences); CPPUNIT_TEST_SUITE_END(); - private: - PresenceOracle* oracle_; - DummyStanzaChannel* stanzaChannel_; - public: - - void handlePresenceChange(boost::shared_ptr<Presence> newPresence, boost::shared_ptr<Presence> lastPresence, PresencePointerPair* out) { - CPPUNIT_ASSERT(out->one.get() == NULL); - CPPUNIT_ASSERT(out->two.get() == NULL); - out->one = newPresence; - out->two = lastPresence; - CPPUNIT_ASSERT(newPresence.get()); - CPPUNIT_ASSERT_EQUAL(newPresence, out->one); - } - - void handlePresenceSubscriptionRequest(const JID& jid, const String& reason, SubscriptionRequestInfo* info) { - CPPUNIT_ASSERT(!info->jid); - CPPUNIT_ASSERT(!info->reason); - info->jid = jid; - info->reason = reason; - } - void setUp() { stanzaChannel_ = new DummyStanzaChannel(); oracle_ = new PresenceOracle(stanzaChannel_); + oracle_->onPresenceChange.connect(boost::bind(&PresenceOracleTest::handlePresenceChange, this, _1)); + oracle_->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() { @@ -67,49 +38,27 @@ class PresenceOracleTest : public CppUnit::TestFixture delete stanzaChannel_; } - void testFirstPresence() { - PresencePointerPair out; - oracle_->onPresenceChange.connect(boost::bind(&PresenceOracleTest::handlePresenceChange, this, _1, _2, &out)); - - SubscriptionRequestInfo info; - oracle_->onPresenceSubscriptionRequest.connect(boost::bind(&PresenceOracleTest::handlePresenceSubscriptionRequest, this, _1, _2, &info)); - - boost::shared_ptr<Presence> sentPresence(new Presence("blarb")); + void testReceivePresence() { + boost::shared_ptr<Presence> sentPresence(createPresence(user1)); stanzaChannel_->onPresenceReceived(sentPresence); - CPPUNIT_ASSERT(!info.jid); - CPPUNIT_ASSERT(!info.reason); - CPPUNIT_ASSERT(out.two.get() == NULL); - CPPUNIT_ASSERT_EQUAL(sentPresence, out.one); + 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 testSecondPresence() { - PresencePointerPair out; - oracle_->onPresenceChange.connect(boost::bind(&PresenceOracleTest::handlePresenceChange, this, _1, _2, &out)); - boost::shared_ptr<Presence> sentPresence1(new Presence("blarb")); + void testReceivePresenceFromDifferentResources() { + boost::shared_ptr<Presence> sentPresence1(createPresence(user1)); + boost::shared_ptr<Presence> sentPresence2(createPresence(user1alt)); stanzaChannel_->onPresenceReceived(sentPresence1); - CPPUNIT_ASSERT_EQUAL(sentPresence1, out.one); - out.one = boost::shared_ptr<Presence>(); - - SubscriptionRequestInfo info; - oracle_->onPresenceSubscriptionRequest.connect(boost::bind(&PresenceOracleTest::handlePresenceSubscriptionRequest, this, _1, _2, &info)); - - boost::shared_ptr<Presence> sentPresence2(new Presence("test2")); stanzaChannel_->onPresenceReceived(sentPresence2); - CPPUNIT_ASSERT(!info.jid); - CPPUNIT_ASSERT(!info.reason); - CPPUNIT_ASSERT_EQUAL(sentPresence1, out.two); - CPPUNIT_ASSERT_EQUAL(sentPresence2, out.one); + CPPUNIT_ASSERT_EQUAL(sentPresence1, oracle_->getLastPresence(user1)); + CPPUNIT_ASSERT_EQUAL(sentPresence2, oracle_->getLastPresence(user1alt)); } - + void testSubscriptionRequest() { - PresencePointerPair out; - oracle_->onPresenceChange.connect(boost::bind(&PresenceOracleTest::handlePresenceChange, this, _1, _2, &out)); - SubscriptionRequestInfo info; - oracle_->onPresenceSubscriptionRequest.connect(boost::bind(&PresenceOracleTest::handlePresenceSubscriptionRequest, this, _1, _2, &info)); - String reasonText = "Because I want to"; JID sentJID = JID("me@example.com"); @@ -119,14 +68,52 @@ class PresenceOracleTest : public CppUnit::TestFixture sentPresence->setStatus(reasonText); stanzaChannel_->onPresenceReceived(sentPresence); - CPPUNIT_ASSERT(info.jid); - CPPUNIT_ASSERT(info.reason); - CPPUNIT_ASSERT_EQUAL(sentJID, info.jid.get()); - CPPUNIT_ASSERT_EQUAL(reasonText, info.reason.get()); + 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(!out.two); - CPPUNIT_ASSERT(!out.one); + CPPUNIT_ASSERT(!oracle_->getLastPresence(user1)); } + + private: + void handlePresenceChange(boost::shared_ptr<Presence> newPresence) { + changes.push_back(newPresence); + } + + void handlePresenceSubscriptionRequest(const JID& jid, const 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; + String reason; + }; + PresenceOracle* oracle_; + DummyStanzaChannel* stanzaChannel_; + std::vector<Presence::ref> changes; + std::vector<SubscriptionRequestInfo> subscriptionRequests; + JID user1; + JID user1alt; + JID user2; }; + CPPUNIT_TEST_SUITE_REGISTRATION(PresenceOracleTest); |