summaryrefslogtreecommitdiffstats
blob: 5b385c26433182453caf4239b7530cd07961902a (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
 * 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/Client/DummyStanzaChannel.h>
#include <Swiften/Presence/DirectedPresenceSender.h>
#include <Swiften/Presence/StanzaChannelPresenceSender.h>

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::shared_ptr<Presence>(new Presence());
			testPresence->setStatus("Foo");
			secondTestPresence = boost::shared_ptr<Presence>(new 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);