summaryrefslogtreecommitdiffstats
blob: 913bdcc870f358a55b9b5c9d88466c156e35abbc (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
/*
 * 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"

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_SUITE_END();

		std::vector<String> groups_;

	public:
		NickResolverTest() {}

		void testMUCNick() {
			boost::shared_ptr<XMPPRoster> xmppRoster(new XMPPRoster());
			NickResolver resolver(xmppRoster);
			MockMUCRegistry registry;
			resolver.setMUCRegistry(&registry);
			registry.setNext(true);
			JID testJID("foo@bar/baz");

			CPPUNIT_ASSERT_EQUAL(String("baz"), resolver.jidToNick(testJID));
		}

		void testMUCNoNick() {
			boost::shared_ptr<XMPPRoster> xmppRoster(new XMPPRoster());
			NickResolver resolver(xmppRoster);
			MockMUCRegistry registry;
			resolver.setMUCRegistry(&registry);
			registry.setNext(true);
			JID testJID("foo@bar");

			CPPUNIT_ASSERT_EQUAL(String("foo@bar"), resolver.jidToNick(testJID));
		}


		void testNoMatch() {
			boost::shared_ptr<XMPPRoster> xmppRoster(new XMPPRoster());
			NickResolver resolver(xmppRoster);
			JID testJID("foo@bar/baz");

			CPPUNIT_ASSERT_EQUAL(String("foo@bar"), resolver.jidToNick(testJID));
		}
		
		void testZeroLengthMatch() {
			boost::shared_ptr<XMPPRoster> xmppRoster(new XMPPRoster());
			NickResolver resolver(xmppRoster);
			JID testJID("foo@bar/baz");
			xmppRoster->addContact(testJID, "", groups_, RosterItemPayload::Both);
			CPPUNIT_ASSERT_EQUAL(String("foo@bar"), resolver.jidToNick(testJID));
		}

		void testMatch() {
			boost::shared_ptr<XMPPRoster> xmppRoster(new XMPPRoster());
			NickResolver resolver(xmppRoster);
			JID testJID("foo@bar/baz");
			xmppRoster->addContact(testJID, "Test", groups_, RosterItemPayload::Both);

			CPPUNIT_ASSERT_EQUAL(String("Test"), resolver.jidToNick(testJID));
		}

		void testOverwrittenMatch() {
			boost::shared_ptr<XMPPRoster> xmppRoster(new XMPPRoster());
			NickResolver resolver(xmppRoster);
			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() {
			boost::shared_ptr<XMPPRoster> xmppRoster(new XMPPRoster());
			NickResolver resolver(xmppRoster);
			JID testJID("foo@bar/baz");
			xmppRoster->addContact(testJID, "FailTest", groups_, RosterItemPayload::Both);
			xmppRoster->removeContact(testJID);
			CPPUNIT_ASSERT_EQUAL(String("foo@bar"), resolver.jidToNick(testJID));
		}

};

CPPUNIT_TEST_SUITE_REGISTRATION(NickResolverTest);