From 3acf1a8f4cc5a221566de1d3a037f1b19953665b Mon Sep 17 00:00:00 2001 From: Kevin Smith Date: Sun, 25 Oct 2009 22:56:43 +0000 Subject: Starting to unit test PresenceOracle. diff --git a/Swiften/Client/DummyStanzaChannel.h b/Swiften/Client/DummyStanzaChannel.h index 211a4ab..052c470 100644 --- a/Swiften/Client/DummyStanzaChannel.h +++ b/Swiften/Client/DummyStanzaChannel.h @@ -28,6 +28,10 @@ namespace Swift { virtual String getNewIQID() { return "test-id"; } + + virtual bool isAvailable() { + return true; + } std::vector > sentStanzas_; }; diff --git a/Swiften/Presence/UnitTest/PresenceOracleTest.cpp b/Swiften/Presence/UnitTest/PresenceOracleTest.cpp new file mode 100644 index 0000000..e6f56b2 --- /dev/null +++ b/Swiften/Presence/UnitTest/PresenceOracleTest.cpp @@ -0,0 +1,98 @@ +#include +#include +#include +#include +#include + +#include "Swiften/Presence/PresenceOracle.h" +#include "Swiften/Client/DummyStanzaChannel.h" + +using namespace Swift; + +class PresencePointerPair { + public: + boost::shared_ptr one; + boost::shared_ptr two; +}; + +class PresenceOracleTest : public CppUnit::TestFixture +{ + CPPUNIT_TEST_SUITE(PresenceOracleTest); + CPPUNIT_TEST(testFirstPresence); + CPPUNIT_TEST(testSecondPresence); + CPPUNIT_TEST_SUITE_END(); + + private: + PresenceOracle* oracle_; + DummyStanzaChannel* stanzaChannel_; + + public: + + void handlePresenceChange(boost::shared_ptr newPresence, boost::shared_ptr 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, boost::optional& outJID, boost::optional& outReason) { + CPPUNIT_ASSERT(!outJID); + CPPUNIT_ASSERT(!outReason); + outJID = jid; + outReason = reason; + } + + void setUp() { + stanzaChannel_ = new DummyStanzaChannel(); + oracle_ = new PresenceOracle(stanzaChannel_); + } + + void tearDown() { + delete oracle_; + delete stanzaChannel_; + } + + void testFirstPresence() { + PresencePointerPair out; + oracle_->onPresenceChange.connect(boost::bind(&PresenceOracleTest::handlePresenceChange, this, _1, _2, &out)); + + boost::optional jid; + boost::optional reason; + oracle_->onPresenceSubscriptionRequest.connect(boost::bind(&PresenceOracleTest::handlePresenceSubscriptionRequest, this, _1, _2, jid, reason)); + + boost::shared_ptr sentPresence(new Presence("blarb")); + stanzaChannel_->onPresenceReceived(sentPresence); + + CPPUNIT_ASSERT(!jid); + CPPUNIT_ASSERT(!reason); + CPPUNIT_ASSERT(out.two.get() == NULL); + CPPUNIT_ASSERT_EQUAL(sentPresence, out.one); + } + + void testSecondPresence() { + PresencePointerPair out; + oracle_->onPresenceChange.connect(boost::bind(&PresenceOracleTest::handlePresenceChange, this, _1, _2, &out)); + + boost::shared_ptr sentPresence1(new Presence("blarb")); + stanzaChannel_->onPresenceReceived(sentPresence1); + CPPUNIT_ASSERT_EQUAL(sentPresence1, out.one); + out.one = boost::shared_ptr(); + + boost::optional jid; + boost::optional reason; + oracle_->onPresenceSubscriptionRequest.connect(boost::bind(&PresenceOracleTest::handlePresenceSubscriptionRequest, this, _1, _2, jid, reason)); + + boost::shared_ptr sentPresence2(new Presence("test2")); + stanzaChannel_->onPresenceReceived(sentPresence2); + + CPPUNIT_ASSERT(!jid); + CPPUNIT_ASSERT(!reason); + CPPUNIT_ASSERT_EQUAL(sentPresence1, out.two); + CPPUNIT_ASSERT_EQUAL(sentPresence2, out.one); + } + +}; +CPPUNIT_TEST_SUITE_REGISTRATION(PresenceOracleTest); + diff --git a/Swiften/SConscript b/Swiften/SConscript index 28af98d..86c2a81 100644 --- a/Swiften/SConscript +++ b/Swiften/SConscript @@ -144,6 +144,7 @@ env.Append(UNITTEST_SOURCES = [ File("Parser/UnitTest/StreamFeaturesParserTest.cpp"), File("Parser/UnitTest/XMLParserTest.cpp"), File("Parser/UnitTest/XMPPParserTest.cpp"), + File("Presence/UnitTest/PresenceOracleTest.cpp"), File("Queries/Requests/UnitTest/GetPrivateStorageRequestTest.cpp"), File("Queries/Responders/UnitTest/DiscoInfoResponderTest.cpp"), File("Queries/UnitTest/IQRouterTest.cpp"), -- cgit v0.10.2-6-g49f6