summaryrefslogtreecommitdiffstats
blob: f954aa17f5696cb1cbd890c026aba7b4b056c1f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 * 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 "Swiften/Elements/VCardUpdate.h"
#include "Swiften/Avatars/AvatarManager.h"
#include "Swiften/Avatars/AvatarStorage.h"
#include "Swiften/MUC/MUCRegistry.h"
#include "Swiften/Queries/IQRouter.h"
#include "Swiften/Client/DummyStanzaChannel.h"

using namespace Swift;

class AvatarManagerTest : public CppUnit::TestFixture {
		CPPUNIT_TEST_SUITE(AvatarManagerTest);
		CPPUNIT_TEST(testUpdate_UpdateNewHash);
		/*&
		CPPUNIT_TEST(testUpdate_UpdateNewHashAlreadyHaveAvatar);
		CPPUNIT_TEST(testUpdate_UpdateNewHashFromMUC);
		CPPUNIT_TEST(testUpdate_UpdateSameHash);*/
		CPPUNIT_TEST(testUpdate_UpdateWithError);
		/*
		CPPUNIT_TEST(testUpdate_UpdateNewHashSameThanOtherUser);
		CPPUNIT_TEST(testReceiveVCard);
		CPPUNIT_TEST(testGetAvatarPath);
		CPPUNIT_TEST(testGetAvatarPathFromMUC);*/
		CPPUNIT_TEST_SUITE_END();

	public:
		void setUp() {
			stanzaChannel_ = new DummyStanzaChannel();
			iqRouter_ = new IQRouter(stanzaChannel_);
			mucRegistry_ = new DummyMUCRegistry();
			avatarStorage_ = new DummyAvatarStorage();
		}

		void tearDown() {
			delete avatarStorage_;
			delete mucRegistry_;
			delete iqRouter_;
			delete stanzaChannel_;
		}

		void testUpdate_UpdateNewHash() {
			std::auto_ptr<AvatarManager> testling = createManager();
			stanzaChannel_->onPresenceReceived(createPresenceWithPhotoHash());

			CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(stanzaChannel_->sentStanzas.size()));
			CPPUNIT_ASSERT(stanzaChannel_->isRequestAtIndex<VCard>(0, JID("foo@bar.com"), IQ::Get));
		}

		void testUpdate_UpdateNewHashAlreadyHaveAvatar() {
			avatarStorage_->addAvatar("aef56135bcce35eb24a43fcd684005b4ca286497", ByteArray("ghij"));
			std::auto_ptr<AvatarManager> testling = createManager();
			stanzaChannel_->onPresenceReceived(createPresenceWithPhotoHash());

			CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(stanzaChannel_->sentStanzas.size()));
		}
/*
		void testUpdate_UpdateNewHashFromMUC() {
			std::auto_ptr<AvatarManager> testling = createManager();
		}

		void testUpdate_UpdateSameHash() {
			std::auto_ptr<AvatarManager> testling = createManager();
		}

		void testUpdate_UpdateNewHashSameThanOtherUser() {
			std::auto_ptr<AvatarManager> testling = createManager();
		}

		void testReceiveVCard() {
			std::auto_ptr<AvatarManager> testling = createManager();
		}

		void testGetAvatarPath() {
			std::auto_ptr<AvatarManager> testling = createManager();
		}

		void testGetAvatarPathFromMUC() {
			std::auto_ptr<AvatarManager> testling = createManager();
		}
		*/

		void testUpdate_UpdateWithError() {
			std::auto_ptr<AvatarManager> testling = createManager();
			boost::shared_ptr<Presence> update = createPresenceWithPhotoHash();
			update->addPayload(boost::shared_ptr<ErrorPayload>(new ErrorPayload()));
			stanzaChannel_->onPresenceReceived(update);

			CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(stanzaChannel_->sentStanzas.size()));
		}


	private:
		std::auto_ptr<AvatarManager> createManager() {
			return std::auto_ptr<AvatarManager>(new AvatarManager(stanzaChannel_, iqRouter_, avatarStorage_, mucRegistry_));
		}

		boost::shared_ptr<Presence> createPresenceWithPhotoHash() {
			boost::shared_ptr<Presence> presence(new Presence());
			presence->setFrom(JID("foo@bar.com/baz"));
			presence->addPayload(boost::shared_ptr<VCardUpdate>(new VCardUpdate("aef56135bcce35eb24a43fcd684005b4ca286497")));
			return presence;
		}

	private:
		struct DummyMUCRegistry : public MUCRegistry {
			bool isMUC(const JID& jid) const { return std::find(mucs_.begin(), mucs_.end(), jid) != mucs_.end(); }
			std::vector<JID> mucs_;
		};
		struct DummyAvatarStorage : public AvatarStorage {
			virtual bool hasAvatar(const String& hash) const { return avatars.find(hash) != avatars.end(); }
			virtual void addAvatar(const String& hash, const ByteArray& avatar) { avatars[hash] = avatar; }
			virtual boost::filesystem::path getAvatarPath(const String& hash) const {
				return boost::filesystem::path("/avatars") / hash.getUTF8String();
			}
			std::map<String, ByteArray> avatars;
		};
		DummyStanzaChannel* stanzaChannel_;
		IQRouter* iqRouter_;
		DummyMUCRegistry* mucRegistry_;
		DummyAvatarStorage* avatarStorage_;
};

CPPUNIT_TEST_SUITE_REGISTRATION(AvatarManagerTest);