summaryrefslogtreecommitdiffstats
blob: b14c6d083c1911d1539fd210cb6b9142853dbdc6 (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
/*
 * Copyright (c) 2010 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <vector>
#include <boost/bind.hpp>

#include <Swiften/Presence/PayloadAddingPresenceSender.h>
#include <Swiften/Presence/StanzaChannelPresenceSender.h>
#include <Swiften/Elements/Body.h>
#include <Swiften/Client/DummyStanzaChannel.h>

using namespace Swift;

class PayloadAddingPresenceSenderTest : public CppUnit::TestFixture {
		CPPUNIT_TEST_SUITE(PayloadAddingPresenceSenderTest);
		CPPUNIT_TEST(testSetPayloadAddsPayloadOnPresenceSend);
		CPPUNIT_TEST(testSetNullPayloadDoesNotAddPayloadOnPresenceSend);
		CPPUNIT_TEST(testSendPresenceDoesNotAlterOriginalPayload);
		CPPUNIT_TEST(testSetPayloadAfterInitialPresenceResendsPresence);
		CPPUNIT_TEST(testSetPayloadAfterUnavailablePresenceDoesNotResendPresence);
		CPPUNIT_TEST(testSetPayloadAfterResetDoesNotResendPresence);
		CPPUNIT_TEST(testSendDirectedPresenceIsNotResent);
		CPPUNIT_TEST_SUITE_END();

	public:
		void setUp() {
			stanzaChannel = new DummyStanzaChannel();
			presenceSender = new StanzaChannelPresenceSender(stanzaChannel);
		}

		void tearDown() {
			delete presenceSender;
			delete stanzaChannel;
		}

		void testSetPayloadAddsPayloadOnPresenceSend() {
			boost::shared_ptr<PayloadAddingPresenceSender> testling(createSender());

			testling->setPayload(MyPayload::create("foo"));
			testling->sendPresence(Presence::create("bar"));

			CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(stanzaChannel->sentStanzas.size()));
			CPPUNIT_ASSERT_EQUAL(std::string("bar"), stanzaChannel->getStanzaAtIndex<Presence>(0)->getStatus());
			CPPUNIT_ASSERT(stanzaChannel->getStanzaAtIndex<Presence>(0)->getPayload<MyPayload>());
		}

		void testSetNullPayloadDoesNotAddPayloadOnPresenceSend() {
			boost::shared_ptr<PayloadAddingPresenceSender> testling(createSender());

			testling->setPayload(MyPayload::ref());
			testling->sendPresence(Presence::create("bar"));

			CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(stanzaChannel->sentStanzas.size()));
			CPPUNIT_ASSERT_EQUAL(std::string("bar"), stanzaChannel->getStanzaAtIndex<Presence>(0)->getStatus());
			CPPUNIT_ASSERT(!stanzaChannel->getStanzaAtIndex<Presence>(0)->getPayload<MyPayload>());
		}

		void testSendPresenceDoesNotAlterOriginalPayload() {
			boost::shared_ptr<PayloadAddingPresenceSender> testling(createSender());

			testling->setPayload(MyPayload::create("foo"));
			Presence::ref presence(Presence::create("bar"));
			testling->sendPresence(presence);

			CPPUNIT_ASSERT(!presence->getPayload<MyPayload>());
		}

		void testSetPayloadAfterInitialPresenceResendsPresence() {
			boost::shared_ptr<PayloadAddingPresenceSender> testling(createSender());

			testling->sendPresence(Presence::create("bar"));
			testling->setPayload(MyPayload::create("foo"));

			CPPUNIT_ASSERT_EQUAL(2, static_cast<int>(stanzaChannel->sentStanzas.size()));
			CPPUNIT_ASSERT_EQUAL(std::string("bar"), stanzaChannel->getStanzaAtIndex<Presence>(1)->getStatus());
			CPPUNIT_ASSERT(stanzaChannel->getStanzaAtIndex<Presence>(1)->getPayload<MyPayload>());
		}

		void testSetPayloadAfterUnavailablePresenceDoesNotResendPresence() {
			boost::shared_ptr<PayloadAddingPresenceSender> testling(createSender());

			testling->sendPresence(Presence::create("bar"));

			Presence::ref presence = Presence::create("bar");
			presence->setType(Presence::Unavailable);
			testling->sendPresence(presence);

			testling->setPayload(MyPayload::create("foo"));

			CPPUNIT_ASSERT_EQUAL(2, static_cast<int>(stanzaChannel->sentStanzas.size()));
		}

		void testSetPayloadAfterResetDoesNotResendPresence() {
			boost::shared_ptr<PayloadAddingPresenceSender> testling(createSender());
			testling->sendPresence(Presence::create("bar"));

			testling->reset();
			testling->setPayload(MyPayload::create("foo"));

			CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(stanzaChannel->sentStanzas.size()));
		}

		void testSendDirectedPresenceIsNotResent() {
			boost::shared_ptr<PayloadAddingPresenceSender> testling(createSender());

			testling->sendPresence(Presence::create("bar"));
			Presence::ref directedPresence = Presence::create("baz");
			directedPresence->setTo(JID("foo@bar.com"));
			testling->sendPresence(directedPresence);
			testling->setPayload(MyPayload::create("foo"));

			CPPUNIT_ASSERT_EQUAL(3, static_cast<int>(stanzaChannel->sentStanzas.size()));
			CPPUNIT_ASSERT_EQUAL(std::string("bar"), stanzaChannel->getStanzaAtIndex<Presence>(2)->getStatus());
		}

	private:
		boost::shared_ptr<PayloadAddingPresenceSender> createSender() {
			boost::shared_ptr<PayloadAddingPresenceSender> sender(new PayloadAddingPresenceSender(presenceSender));
			return sender;
		}

		struct MyPayload : public Payload {
				typedef boost::shared_ptr<MyPayload> ref;

				MyPayload(const std::string& body) : body(body) {}

				static ref create(const std::string& body) {
					return ref(new MyPayload(body));
				}

				std::string body;
		};

	private:
		DummyStanzaChannel* stanzaChannel;
		StanzaChannelPresenceSender* presenceSender;
};

CPPUNIT_TEST_SUITE_REGISTRATION(PayloadAddingPresenceSenderTest);