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
|
/*
* Copyright (c) 2010 Kevin Smith
* 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 "Swift/Controllers/NickResolver.h"
#include "Swift/Controllers/UnitTest/MockMUCRegistry.h"
#include "Swiften/Roster/XMPPRoster.h"
#include "Swiften/VCards/VCardManager.h"
#include "Swiften/VCards/VCardMemoryStorage.h"
#include "Swiften/Queries/IQRouter.h"
#include "Swiften/Client/DummyStanzaChannel.h"
using namespace Swift;
class NickResolverTest : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE(NickResolverTest);
CPPUNIT_TEST(testNoMatch);
CPPUNIT_TEST(testZeroLengthMatch);
CPPUNIT_TEST(testMatch);
CPPUNIT_TEST(testOverwrittenMatch);
CPPUNIT_TEST(testRemovedMatch);
CPPUNIT_TEST(testMUCNick);
CPPUNIT_TEST(testMUCNoNick);
CPPUNIT_TEST(testRemovedMatch);
CPPUNIT_TEST(testOwnNickFullOnly);
CPPUNIT_TEST(testOwnNickGivenAndFull);
CPPUNIT_TEST(testOwnNickNickEtAl);
CPPUNIT_TEST_SUITE_END();
std::vector<String> groups_;
boost::shared_ptr<XMPPRoster> xmppRoster_;
VCardStorage* vCardStorage_;
IQRouter* iqRouter_;
DummyStanzaChannel* stanzaChannel_;
VCardManager* vCardManager_;
NickResolver* resolver_;
JID ownJID_;
public:
NickResolverTest() {}
void setUp() {
ownJID_ = JID("kev@wonderland.lit");
xmppRoster_ = boost::shared_ptr<XMPPRoster>(new XMPPRoster());
stanzaChannel_ = new DummyStanzaChannel();
iqRouter_ = new IQRouter(stanzaChannel_);
vCardStorage_ = new VCardMemoryStorage();
vCardManager_ = new VCardManager(ownJID_, iqRouter_, vCardStorage_);
resolver_ = new NickResolver(ownJID_, xmppRoster_, vCardManager_);
}
void tearDown() {
delete vCardManager_;
delete stanzaChannel_;
delete iqRouter_;
delete vCardStorage_;
delete resolver_;
}
void testMUCNick() {
MockMUCRegistry registry;
resolver_->setMUCRegistry(®istry);
registry.setNext(true);
JID testJID("foo@bar/baz");
CPPUNIT_ASSERT_EQUAL(String("baz"), resolver_->jidToNick(testJID));
}
void testMUCNoNick() {
MockMUCRegistry registry;
resolver_->setMUCRegistry(®istry);
registry.setNext(true);
JID testJID("foo@bar");
CPPUNIT_ASSERT_EQUAL(String("foo@bar"), resolver_->jidToNick(testJID));
}
void testNoMatch() {
JID testJID("foo@bar/baz");
CPPUNIT_ASSERT_EQUAL(String("foo@bar"), resolver_->jidToNick(testJID));
}
void testZeroLengthMatch() {
JID testJID("foo@bar/baz");
xmppRoster_->addContact(testJID, "", groups_, RosterItemPayload::Both);
CPPUNIT_ASSERT_EQUAL(String("foo@bar"), resolver_->jidToNick(testJID));
}
void testMatch() {
JID testJID("foo@bar/baz");
xmppRoster_->addContact(testJID, "Test", groups_, RosterItemPayload::Both);
CPPUNIT_ASSERT_EQUAL(String("Test"), resolver_->jidToNick(testJID));
}
void testOverwrittenMatch() {
JID testJID("foo@bar/baz");
xmppRoster_->addContact(testJID, "FailTest", groups_, RosterItemPayload::Both);
xmppRoster_->addContact(testJID, "Test", groups_, RosterItemPayload::Both);
CPPUNIT_ASSERT_EQUAL(String("Test"), resolver_->jidToNick(testJID));
}
void testRemovedMatch() {
JID testJID("foo@bar/baz");
xmppRoster_->addContact(testJID, "FailTest", groups_, RosterItemPayload::Both);
xmppRoster_->removeContact(testJID);
CPPUNIT_ASSERT_EQUAL(String("foo@bar"), resolver_->jidToNick(testJID));
}
void testOwnNickFullOnly() {
populateOwnVCard("", "", "Kevin Smith");
CPPUNIT_ASSERT_EQUAL(String("Kevin Smith"), resolver_->jidToNick(ownJID_));
}
void testOwnNickGivenAndFull() {
populateOwnVCard("", "Kevin", "Kevin Smith");
CPPUNIT_ASSERT_EQUAL(String("Kevin"), resolver_->jidToNick(ownJID_));
}
void testOwnNickNickEtAl() {
populateOwnVCard("Kev", "Kevin", "Kevin Smith");
CPPUNIT_ASSERT_EQUAL(String("Kev"), resolver_->jidToNick(ownJID_));
}
void populateOwnVCard(const String& nick, const String& given, const String& full) {
VCard::ref vcard(new VCard());
if (!nick.isEmpty()) {
vcard->setNickname(nick);
}
if (!given.isEmpty()) {
vcard->setGivenName(given);
}
if (!full.isEmpty()) {
vcard->setFullName(full);
}
vCardManager_->requestVCard(ownJID_);
IQ::ref result(IQ::createResult(JID(), stanzaChannel_->sentStanzas[0]->getID(), vcard));
stanzaChannel_->onIQReceived(result);
}
};
CPPUNIT_TEST_SUITE_REGISTRATION(NickResolverTest);
|