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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
/*
* 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 <boost/shared_ptr.hpp>
#include "Swiften/Presence/PresenceOracle.h"
#include "Swiften/Client/DummyStanzaChannel.h"
#include "Swiften/Presence/SubscriptionManager.h"
using namespace Swift;
class PresenceOracleTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(PresenceOracleTest);
CPPUNIT_TEST(testReceivePresence);
CPPUNIT_TEST(testReceivePresenceFromDifferentResources);
CPPUNIT_TEST(testSubscriptionRequest);
CPPUNIT_TEST(testReconnectResetsPresences);
CPPUNIT_TEST(testHighestPresenceSingle);
CPPUNIT_TEST(testHighestPresenceMultiple);
CPPUNIT_TEST(testHighestPresenceGlobal);
CPPUNIT_TEST(testHighestPresenceChangePriority);
CPPUNIT_TEST_SUITE_END();
public:
void setUp() {
stanzaChannel_ = new DummyStanzaChannel();
oracle_ = new PresenceOracle(stanzaChannel_);
oracle_->onPresenceChange.connect(boost::bind(&PresenceOracleTest::handlePresenceChange, this, _1));
subscriptionManager_ = new SubscriptionManager(stanzaChannel_);
subscriptionManager_->onPresenceSubscriptionRequest.connect(boost::bind(&PresenceOracleTest::handlePresenceSubscriptionRequest, this, _1, _2));
user1 = JID("user1@foo.com/Foo");
user1alt = JID("user1@foo.com/Bar");
user2 = JID("user2@bar.com/Bar");
}
void tearDown() {
delete subscriptionManager_;
delete oracle_;
delete stanzaChannel_;
}
void testHighestPresenceSingle() {
JID bareJID("alice@wonderland.lit");
Presence::ref fiveOn = makeOnline("blah", 5);
Presence::ref fiveOff = makeOffline("/blah");
CPPUNIT_ASSERT_EQUAL(Presence::ref(), oracle_->getHighestPriorityPresence(bareJID));
stanzaChannel_->onPresenceReceived(fiveOn);
CPPUNIT_ASSERT_EQUAL(fiveOn, oracle_->getHighestPriorityPresence(bareJID));
stanzaChannel_->onPresenceReceived(fiveOff);
CPPUNIT_ASSERT_EQUAL(fiveOff, oracle_->getHighestPriorityPresence(bareJID));
}
void testHighestPresenceMultiple() {
JID bareJID("alice@wonderland.lit");
Presence::ref fiveOn = makeOnline("blah", 5);
Presence::ref fiveOff = makeOffline("/blah");
Presence::ref tenOn = makeOnline("bert", 10);
Presence::ref tenOff = makeOffline("/bert");
stanzaChannel_->onPresenceReceived(fiveOn);
stanzaChannel_->onPresenceReceived(tenOn);
CPPUNIT_ASSERT_EQUAL(tenOn, oracle_->getHighestPriorityPresence(bareJID));
stanzaChannel_->onPresenceReceived(fiveOff);
CPPUNIT_ASSERT_EQUAL(tenOn, oracle_->getHighestPriorityPresence(bareJID));
stanzaChannel_->onPresenceReceived(fiveOn);
stanzaChannel_->onPresenceReceived(tenOff);
CPPUNIT_ASSERT_EQUAL(fiveOn, oracle_->getHighestPriorityPresence(bareJID));
}
void testHighestPresenceGlobal() {
JID bareJID("alice@wonderland.lit");
Presence::ref fiveOn = makeOnline("blah", 5);
Presence::ref fiveOff = makeOffline("/blah");
Presence::ref tenOn = makeOnline("bert", 10);
Presence::ref allOff = makeOffline("");
stanzaChannel_->onPresenceReceived(fiveOn);
stanzaChannel_->onPresenceReceived(tenOn);
stanzaChannel_->onPresenceReceived(allOff);
CPPUNIT_ASSERT_EQUAL(allOff, oracle_->getHighestPriorityPresence(bareJID));
}
void testHighestPresenceChangePriority() {
JID bareJID("alice@wonderland.lit");
Presence::ref fiveOn = makeOnline("blah", 5);
Presence::ref fiveOff = makeOffline("/blah");
Presence::ref tenOn = makeOnline("bert", 10);
Presence::ref tenOnThree = makeOnline("bert", 3);
Presence::ref tenOff = makeOffline("/bert");
stanzaChannel_->onPresenceReceived(fiveOn);
stanzaChannel_->onPresenceReceived(tenOn);
stanzaChannel_->onPresenceReceived(tenOnThree);
CPPUNIT_ASSERT_EQUAL(fiveOn, oracle_->getHighestPriorityPresence(bareJID));
stanzaChannel_->onPresenceReceived(fiveOff);
CPPUNIT_ASSERT_EQUAL(tenOnThree, oracle_->getHighestPriorityPresence(bareJID));
stanzaChannel_->onPresenceReceived(fiveOn);
CPPUNIT_ASSERT_EQUAL(fiveOn, oracle_->getHighestPriorityPresence(bareJID));
}
void testReceivePresence() {
boost::shared_ptr<Presence> sentPresence(createPresence(user1));
stanzaChannel_->onPresenceReceived(sentPresence);
CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(changes.size()));
CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(subscriptionRequests.size()));
CPPUNIT_ASSERT_EQUAL(sentPresence, changes[0]);
CPPUNIT_ASSERT_EQUAL(sentPresence, oracle_->getLastPresence(user1));
}
void testReceivePresenceFromDifferentResources() {
boost::shared_ptr<Presence> sentPresence1(createPresence(user1));
boost::shared_ptr<Presence> sentPresence2(createPresence(user1alt));
stanzaChannel_->onPresenceReceived(sentPresence1);
stanzaChannel_->onPresenceReceived(sentPresence2);
CPPUNIT_ASSERT_EQUAL(sentPresence1, oracle_->getLastPresence(user1));
CPPUNIT_ASSERT_EQUAL(sentPresence2, oracle_->getLastPresence(user1alt));
}
void testSubscriptionRequest() {
String reasonText = "Because I want to";
JID sentJID = JID("me@example.com");
boost::shared_ptr<Presence> sentPresence(new Presence());
sentPresence->setType(Presence::Subscribe);
sentPresence->setFrom(sentJID);
sentPresence->setStatus(reasonText);
stanzaChannel_->onPresenceReceived(sentPresence);
CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(changes.size()));
CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(subscriptionRequests.size()));
CPPUNIT_ASSERT_EQUAL(sentJID, subscriptionRequests[0].jid);
CPPUNIT_ASSERT_EQUAL(reasonText, subscriptionRequests[0].reason);
}
void testReconnectResetsPresences() {
boost::shared_ptr<Presence> sentPresence(createPresence(user1));
stanzaChannel_->onPresenceReceived(sentPresence);
stanzaChannel_->setAvailable(false);
stanzaChannel_->setAvailable(true);
CPPUNIT_ASSERT(!oracle_->getLastPresence(user1));
}
private:
Presence::ref makeOnline(const String& resource, int priority) {
Presence::ref presence(new Presence());
presence->setPriority(priority);
presence->setFrom(JID("alice@wonderland.lit/" + resource));
return presence;
}
Presence::ref makeOffline(const String& resource) {
Presence::ref presence(new Presence());
presence->setFrom(JID("alice@wonderland.lit" + resource));
presence->setType(Presence::Unavailable);
return presence;
}
void handlePresenceChange(boost::shared_ptr<Presence> newPresence) {
changes.push_back(newPresence);
}
void handlePresenceSubscriptionRequest(const JID& jid, const String& reason) {
SubscriptionRequestInfo subscriptionRequest;
subscriptionRequest.jid = jid;
subscriptionRequest.reason = reason;
subscriptionRequests.push_back(subscriptionRequest);
}
boost::shared_ptr<Presence> createPresence(const JID& jid) {
boost::shared_ptr<Presence> sentPresence(new Presence("blarb"));
sentPresence->setFrom(jid);
return sentPresence;
}
private:
struct SubscriptionRequestInfo {
JID jid;
String reason;
};
PresenceOracle* oracle_;
SubscriptionManager* subscriptionManager_;
DummyStanzaChannel* stanzaChannel_;
std::vector<Presence::ref> changes;
std::vector<SubscriptionRequestInfo> subscriptionRequests;
JID user1;
JID user1alt;
JID user2;
};
CPPUNIT_TEST_SUITE_REGISTRATION(PresenceOracleTest);
|