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

#include <boost/shared_ptr.hpp>

#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>

#include <Swift/Controllers/Roster/GroupRosterItem.h>
#include <Swift/Controllers/Roster/ItemOperations/SetPresence.h>
#include <Swift/Controllers/Roster/Roster.h>

using namespace Swift;

class RosterTest : public CppUnit::TestFixture {
        CPPUNIT_TEST_SUITE(RosterTest);
        CPPUNIT_TEST(testGetGroup);
        CPPUNIT_TEST(testRemoveContact);
        CPPUNIT_TEST(testRemoveSecondContact);
        CPPUNIT_TEST(testRemoveSecondContactSameBare);
        CPPUNIT_TEST(testApplyPresenceLikeMUC);
        CPPUNIT_TEST(testReSortLikeMUC);
        CPPUNIT_TEST_SUITE_END();

    public:
        void setUp() {
            jid1_ = JID("a@b.c");
            jid2_ = JID("b@c.d");
            jid3_ = JID("c@d.e");
            roster_ = new Roster();
        }

        void tearDown() {
            delete roster_;
        }

        void testGetGroup() {
            roster_->addContact(jid1_, JID(), "Bert", "group1", "");
            roster_->addContact(jid2_, JID(), "Ernie", "group2", "");
            roster_->addContact(jid3_, JID(), "Cookie", "group1", "");

            CPPUNIT_ASSERT_EQUAL(2, static_cast<int>(roster_->getRoot()->getChildren().size()));
            CPPUNIT_ASSERT_EQUAL(std::string("group1"), roster_->getRoot()->getChildren()[0]->getDisplayName());
            CPPUNIT_ASSERT_EQUAL(std::string("group2"), roster_->getRoot()->getChildren()[1]->getDisplayName());
            CPPUNIT_ASSERT_EQUAL(std::string("Bert"), static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[0])->getChildren()[0]->getDisplayName());
            CPPUNIT_ASSERT_EQUAL(std::string("Cookie"), static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[0])->getChildren()[1]->getDisplayName());
            CPPUNIT_ASSERT_EQUAL(std::string("Ernie"), static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[1])->getChildren()[0]->getDisplayName());

        }

        void testRemoveContact() {
            roster_->addContact(jid1_, jid1_, "Bert", "group1", "");
            CPPUNIT_ASSERT_EQUAL(std::string("Bert"), static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[0])->getChildren()[0]->getDisplayName());

            roster_->removeContact(jid1_);
            CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[0])->getChildren().size()));
        }

        void testRemoveSecondContact() {
            roster_->addContact(jid1_, jid1_, "Bert", "group1", "");
            roster_->addContact(jid2_, jid2_, "Cookie", "group1", "");
            CPPUNIT_ASSERT_EQUAL(std::string("Cookie"), static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[0])->getChildren()[1]->getDisplayName());

            roster_->removeContact(jid2_);
            CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[0])->getChildren().size()));
            CPPUNIT_ASSERT_EQUAL(std::string("Bert"), static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[0])->getChildren()[0]->getDisplayName());
        }

        void testRemoveSecondContactSameBare() {
            JID jid4a("a@b/c");
            JID jid4b("a@b/d");
            roster_->addContact(jid4a, JID(), "Bert", "group1", "");
            roster_->addContact(jid4b, JID(), "Cookie", "group1", "");
            CPPUNIT_ASSERT_EQUAL(std::string("Cookie"), static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[0])->getChildren()[1]->getDisplayName());

            roster_->removeContact(jid4b);
            CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[0])->getChildren().size()));
            CPPUNIT_ASSERT_EQUAL(std::string("Bert"), static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[0])->getChildren()[0]->getDisplayName());
        }

        void testApplyPresenceLikeMUC() {
            JID jid4a("a@b/c");
            JID jid4b("a@b/d");
            JID jid4c("a@b/e");
            roster_->addContact(jid4a, JID(), "Bird", "group1", "");
            roster_->addContact(jid4b, JID(), "Cookie", "group1", "");
            roster_->removeContact(jid4b);
            roster_->addContact(jid4c, JID(), "Bert", "group1", "");
            roster_->addContact(jid4b, JID(), "Ernie", "group1", "");
            boost::shared_ptr<Presence> presence(new Presence());
            presence->setShow(StatusShow::DND);
            presence->setFrom(jid4a);
            roster_->applyOnItems(SetPresence(presence, JID::WithResource));
            presence->setFrom(jid4b);
            roster_->applyOnItems(SetPresence(presence, JID::WithResource));
            presence->setFrom(jid4c);
            roster_->applyOnItems(SetPresence(presence, JID::WithResource));

            presence = boost::make_shared<Presence>();
            presence->setFrom(jid4b);
            presence->setShow(StatusShow::Online);
            roster_->applyOnItems(SetPresence(presence, JID::WithResource));
            std::vector<RosterItem*> children = static_cast<GroupRosterItem*>(roster_->getRoot()->getDisplayedChildren()[0])->getDisplayedChildren();
            CPPUNIT_ASSERT_EQUAL(3, static_cast<int>(children.size()));

            /* Check order */
            CPPUNIT_ASSERT_EQUAL(std::string("Ernie"), children[0]->getDisplayName());
            CPPUNIT_ASSERT_EQUAL(std::string("Bert"), children[1]->getDisplayName());
            CPPUNIT_ASSERT_EQUAL(std::string("Bird"), children[2]->getDisplayName());

            presence = boost::make_shared<Presence>();
            presence->setFrom(jid4c);
            presence->setType(Presence::Unavailable);
            roster_->removeContact(jid4c);
            roster_->applyOnItems(SetPresence(presence, JID::WithResource));

        }

        void testReSortLikeMUC() {
            JID jid4a("a@b/c");
            JID jid4b("a@b/d");
            JID jid4c("a@b/e");
            roster_->addContact(jid4a, JID(), "Bird", "group1", "");
            roster_->addContact(jid4b, JID(), "Cookie", "group2", "");
            roster_->addContact(jid4b, JID(), "Ernie", "group1", "");
            roster_->getGroup("group1")->setManualSort("2");
            roster_->getGroup("group2")->setManualSort("1");
            GroupRosterItem* root = roster_->getRoot();
            const std::vector<RosterItem*> kids = root->getDisplayedChildren();
            CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), kids.size());
            CPPUNIT_ASSERT_EQUAL(std::string("group2"), kids[0]->getDisplayName());
            CPPUNIT_ASSERT_EQUAL(std::string("group1"), kids[1]->getDisplayName());
        }

    private:
        Roster *roster_;
        JID jid1_;
        JID jid2_;
        JID jid3_;
};

CPPUNIT_TEST_SUITE_REGISTRATION(RosterTest);