diff options
author | Remko Tronçon <git@el-tramo.be> | 2010-09-09 13:20:18 (GMT) |
---|---|---|
committer | Remko Tronçon <git@el-tramo.be> | 2010-09-11 21:53:44 (GMT) |
commit | 904db4e398210192093b688ebf1ad66fb017b6d2 (patch) | |
tree | 083f01449f64dc27aff63e2be0aa00a73cbd511b /Swiften/Avatars/UnitTest | |
parent | 10d8ba50a9e09517cfe4a6d4c3d51a768e989125 (diff) | |
download | swift-contrib-904db4e398210192093b688ebf1ad66fb017b6d2.zip swift-contrib-904db4e398210192093b688ebf1ad66fb017b6d2.tar.bz2 |
Added CombinedAvatarProvider
Diffstat (limited to 'Swiften/Avatars/UnitTest')
-rw-r--r-- | Swiften/Avatars/UnitTest/CombinedAvatarProviderTest.cpp | 194 |
1 files changed, 194 insertions, 0 deletions
diff --git a/Swiften/Avatars/UnitTest/CombinedAvatarProviderTest.cpp b/Swiften/Avatars/UnitTest/CombinedAvatarProviderTest.cpp new file mode 100644 index 0000000..ad3a0f1 --- /dev/null +++ b/Swiften/Avatars/UnitTest/CombinedAvatarProviderTest.cpp @@ -0,0 +1,194 @@ +/* + * Copyright (c) 2010 Remko Tronçon + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#include <cppunit/extensions/HelperMacros.h> +#include <cppunit/extensions/TestFactoryRegistry.h> +#include <boost/bind.hpp> + +#include "Swiften/JID/JID.h" +#include "Swiften/Base/String.h" +#include "Swiften/Avatars/CombinedAvatarProvider.h" + +using namespace Swift; + +class CombinedAvatarProviderTest : public CppUnit::TestFixture { + CPPUNIT_TEST_SUITE(CombinedAvatarProviderTest); + CPPUNIT_TEST(testGetAvatarWithNoAvatarProviderReturnsEmpty); + CPPUNIT_TEST(testGetAvatarWithSingleAvatarProvider); + CPPUNIT_TEST(testGetAvatarWithMultipleAvatarProviderReturnsFirstAvatar); + CPPUNIT_TEST(testGetAvatarWithMultipleAvatarProviderAndFailingFirstProviderReturnsSecondAvatar); + CPPUNIT_TEST(testProviderUpdateTriggersChange); + CPPUNIT_TEST(testProviderUpdateWithoutChangeDoesNotTriggerChange); + CPPUNIT_TEST(testProviderSecondUpdateTriggersChange); + CPPUNIT_TEST(testProviderUpdateWithAvatarDisappearingTriggersChange); + CPPUNIT_TEST(testProviderUpdateAfterAvatarDisappearedTriggersChange); + CPPUNIT_TEST(testRemoveProviderDisconnectsUpdates); + CPPUNIT_TEST_SUITE_END(); + + public: + void setUp() { + avatarProvider1 = new DummyAvatarProvider(); + avatarProvider2 = new DummyAvatarProvider(); + user1 = JID("user1@bar.com/bla"); + user2 = JID("user2@foo.com/baz"); + avatarHash1 = "ABCDEFG"; + avatarHash2 = "XYZU"; + avatarHash3 = "IDGH"; + } + + void tearDown() { + delete avatarProvider1; + delete avatarProvider2; + } + + void testGetAvatarWithNoAvatarProviderReturnsEmpty() { + std::auto_ptr<CombinedAvatarProvider> testling(createProvider()); + + CPPUNIT_ASSERT(testling->getAvatarHash(user1).isEmpty()); + } + + void testGetAvatarWithSingleAvatarProvider() { + std::auto_ptr<CombinedAvatarProvider> testling(createProvider()); + avatarProvider1->avatars[user1] = avatarHash1; + testling->addProvider(avatarProvider1); + + CPPUNIT_ASSERT_EQUAL(avatarHash1, testling->getAvatarHash(user1)); + } + + void testGetAvatarWithMultipleAvatarProviderReturnsFirstAvatar() { + std::auto_ptr<CombinedAvatarProvider> testling(createProvider()); + avatarProvider1->avatars[user1] = avatarHash1; + avatarProvider2->avatars[user1] = avatarHash2; + testling->addProvider(avatarProvider1); + testling->addProvider(avatarProvider2); + + CPPUNIT_ASSERT_EQUAL(avatarHash1, testling->getAvatarHash(user1)); + } + + void testGetAvatarWithMultipleAvatarProviderAndFailingFirstProviderReturnsSecondAvatar() { + std::auto_ptr<CombinedAvatarProvider> testling(createProvider()); + avatarProvider2->avatars[user1] = avatarHash2; + testling->addProvider(avatarProvider1); + testling->addProvider(avatarProvider2); + + CPPUNIT_ASSERT_EQUAL(avatarHash2, testling->getAvatarHash(user1)); + } + + void testProviderUpdateTriggersChange() { + std::auto_ptr<CombinedAvatarProvider> testling(createProvider()); + testling->addProvider(avatarProvider1); + avatarProvider1->avatars[user1] = avatarHash1; + avatarProvider1->onAvatarChanged(user1); + + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(changes.size())); + CPPUNIT_ASSERT_EQUAL(user1, changes[0]); + } + + void testProviderUpdateWithoutChangeDoesNotTriggerChange() { + std::auto_ptr<CombinedAvatarProvider> testling(createProvider()); + testling->addProvider(avatarProvider1); + testling->addProvider(avatarProvider2); + avatarProvider1->avatars[user1] = avatarHash1; + avatarProvider1->onAvatarChanged(user1); + changes.clear(); + + avatarProvider2->avatars[user1] = avatarHash2; + avatarProvider2->onAvatarChanged(user1); + + CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(changes.size())); + } + + void testProviderSecondUpdateTriggersChange() { + std::auto_ptr<CombinedAvatarProvider> testling(createProvider()); + testling->addProvider(avatarProvider1); + avatarProvider1->avatars[user1] = avatarHash1; + avatarProvider1->onAvatarChanged(user1); + changes.clear(); + avatarProvider1->avatars[user1] = avatarHash2; + avatarProvider1->onAvatarChanged(user1); + + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(changes.size())); + CPPUNIT_ASSERT_EQUAL(user1, changes[0]); + } + + + void testProviderUpdateWithAvatarDisappearingTriggersChange() { + std::auto_ptr<CombinedAvatarProvider> testling(createProvider()); + testling->addProvider(avatarProvider1); + avatarProvider1->avatars[user1] = avatarHash1; + avatarProvider1->onAvatarChanged(user1); + changes.clear(); + avatarProvider1->avatars.clear(); + avatarProvider1->onAvatarChanged(user1); + + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(changes.size())); + CPPUNIT_ASSERT_EQUAL(user1, changes[0]); + } + + void testProviderUpdateAfterAvatarDisappearedTriggersChange() { + std::auto_ptr<CombinedAvatarProvider> testling(createProvider()); + testling->addProvider(avatarProvider1); + avatarProvider1->avatars[user1] = avatarHash1; + avatarProvider1->onAvatarChanged(user1); + avatarProvider1->avatars.clear(); + avatarProvider1->onAvatarChanged(user1); + changes.clear(); + avatarProvider1->avatars[user1] = avatarHash1; + avatarProvider1->onAvatarChanged(user1); + + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(changes.size())); + CPPUNIT_ASSERT_EQUAL(user1, changes[0]); + } + + void testRemoveProviderDisconnectsUpdates() { + std::auto_ptr<CombinedAvatarProvider> testling(createProvider()); + testling->addProvider(avatarProvider1); + testling->addProvider(avatarProvider2); + testling->removeProvider(avatarProvider1); + avatarProvider1->avatars[user1] = avatarHash1; + avatarProvider2->avatars[user1] = avatarHash2; + avatarProvider1->onAvatarChanged(user1); + + CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(changes.size())); + } + + private: + std::auto_ptr<CombinedAvatarProvider> createProvider() { + std::auto_ptr<CombinedAvatarProvider> result(new CombinedAvatarProvider()); + result->onAvatarChanged.connect(boost::bind(&CombinedAvatarProviderTest::handleAvatarChanged, this, _1)); + return result; + } + + void handleAvatarChanged(const JID& jid) { + changes.push_back(jid); + } + + private: + struct DummyAvatarProvider : public AvatarProvider { + String getAvatarHash(const JID& jid) const { + std::map<JID, String>::const_iterator i = avatars.find(jid); + if (i != avatars.end()) { + return i->second; + } + else { + return String(); + } + } + + std::map<JID, String> avatars; + }; + + DummyAvatarProvider* avatarProvider1; + DummyAvatarProvider* avatarProvider2; + JID user1; + JID user2; + String avatarHash1; + String avatarHash2; + String avatarHash3; + std::vector<JID> changes; +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(CombinedAvatarProviderTest); |