diff options
author | Remko Tronçon <git@el-tramo.be> | 2011-02-07 18:18:59 (GMT) |
---|---|---|
committer | Remko Tronçon <git@el-tramo.be> | 2011-02-07 18:19:27 (GMT) |
commit | d1837d06c5d655de759bbfcc66711a71c92296ad (patch) | |
tree | 31f93f55949efdc6047d707d5a32f14a392043e8 /Swift/Controllers/Roster/UnitTest | |
parent | 3db6fa2e985a286c3b53dc621ba164023272cb60 (diff) | |
download | swift-contrib-d1837d06c5d655de759bbfcc66711a71c92296ad.zip swift-contrib-d1837d06c5d655de759bbfcc66711a71c92296ad.tar.bz2 |
Moved Swift-specific roster code out of Swiften.
Diffstat (limited to 'Swift/Controllers/Roster/UnitTest')
-rw-r--r-- | Swift/Controllers/Roster/UnitTest/RosterControllerTest.cpp | 290 | ||||
-rw-r--r-- | Swift/Controllers/Roster/UnitTest/RosterTest.cpp | 128 |
2 files changed, 418 insertions, 0 deletions
diff --git a/Swift/Controllers/Roster/UnitTest/RosterControllerTest.cpp b/Swift/Controllers/Roster/UnitTest/RosterControllerTest.cpp new file mode 100644 index 0000000..604cda6 --- /dev/null +++ b/Swift/Controllers/Roster/UnitTest/RosterControllerTest.cpp @@ -0,0 +1,290 @@ + +/* + * 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/Roster/RosterController.h" +#include "Swift/Controllers/UnitTest/MockMainWindowFactory.h" +// #include "Swiften/Elements/Payload.h" +// #include "Swiften/Elements/RosterItemPayload.h" +// #include "Swiften/Elements/RosterPayload.h" +#include "Swiften/Queries/DummyIQChannel.h" +#include "Swiften/Client/DummyStanzaChannel.h" +#include "Swiften/Queries/IQRouter.h" +#include "Swiften/Roster/XMPPRosterImpl.h" +#include "Swift/Controllers/Roster/Roster.h" +#include "Swift/Controllers/Roster/GroupRosterItem.h" +#include "Swift/Controllers/Roster/ContactRosterItem.h" +#include "Swift/Controllers/Settings/DummySettingsProvider.h" +#include "Swiften/Avatars/NullAvatarManager.h" +#include "Swift/Controllers/XMPPEvents/EventController.h" +#include "Swiften/Presence/PresenceOracle.h" +#include "Swiften/Presence/SubscriptionManager.h" +#include "Swiften/Client/NickResolver.h" +#include "Swift/Controllers/UIEvents/UIEventStream.h" +#include "Swift/Controllers/UIEvents/RenameRosterItemUIEvent.h" +#include "Swiften/MUC/MUCRegistry.h" +#include <Swiften/Client/DummyNickManager.h> + +using namespace Swift; + +#define CHILDREN mainWindow_->roster->getRoot()->getChildren() + +class RosterControllerTest : public CppUnit::TestFixture { + CPPUNIT_TEST_SUITE(RosterControllerTest); + CPPUNIT_TEST(testAdd); + CPPUNIT_TEST(testAddSubscription); + CPPUNIT_TEST(testReceiveRename); + CPPUNIT_TEST(testSendRename); + CPPUNIT_TEST(testPresence); + CPPUNIT_TEST(testHighestPresence); + CPPUNIT_TEST(testNotHighestPresence); + CPPUNIT_TEST(testUnavailablePresence); + CPPUNIT_TEST_SUITE_END(); + + public: + void setUp() { + jid_ = JID("testjid@swift.im/swift"); + xmppRoster_ = new XMPPRosterImpl(); + avatarManager_ = new NullAvatarManager(); + mainWindowFactory_ = new MockMainWindowFactory(); + mucRegistry_ = new MUCRegistry(); + nickResolver_ = new NickResolver(jid_.toBare(), xmppRoster_, NULL, mucRegistry_); + channel_ = new DummyIQChannel(); + router_ = new IQRouter(channel_); + stanzaChannel_ = new DummyStanzaChannel(); + presenceOracle_ = new PresenceOracle(stanzaChannel_); + subscriptionManager_ = new SubscriptionManager(stanzaChannel_); + eventController_ = new EventController(); + uiEventStream_ = new UIEventStream(); + settings_ = new DummySettingsProvider(); + nickManager_ = new DummyNickManager(); + rosterController_ = new RosterController(jid_, xmppRoster_, avatarManager_, mainWindowFactory_, nickManager_, nickResolver_, presenceOracle_, subscriptionManager_, eventController_, uiEventStream_, router_, settings_); + mainWindow_ = mainWindowFactory_->last; + }; + + void tearDown() { + delete rosterController_; + delete nickManager_; + delete nickResolver_; + delete mucRegistry_; + delete mainWindowFactory_; + delete avatarManager_; + delete router_; + delete channel_; + delete eventController_; + delete subscriptionManager_; + delete presenceOracle_; + delete stanzaChannel_; + delete uiEventStream_; + delete settings_; + delete xmppRoster_; + }; + + GroupRosterItem* groupChild(size_t i) { + return dynamic_cast<GroupRosterItem*>(CHILDREN[i]); + } + + JID withResource(const JID& jid, const String& resource) { + return JID(jid.toBare().toString() + "/" + resource); + } + + void testPresence() { + std::vector<String> groups; + groups.push_back("testGroup1"); + groups.push_back("testGroup2"); + JID from("test@testdomain.com"); + xmppRoster_->addContact(from, "name", groups, RosterItemPayload::Both); + Presence::ref presence(new Presence()); + presence->setFrom(withResource(from, "bob")); + presence->setPriority(2); + presence->setStatus("So totally here"); + stanzaChannel_->onPresenceReceived(presence); + ContactRosterItem* item = dynamic_cast<ContactRosterItem*>(dynamic_cast<GroupRosterItem*>(CHILDREN[0])->getChildren()[0]); + CPPUNIT_ASSERT(item); + CPPUNIT_ASSERT_EQUAL(presence->getStatus(), item->getStatusText()); + ContactRosterItem* item2 = dynamic_cast<ContactRosterItem*>(dynamic_cast<GroupRosterItem*>(CHILDREN[1])->getChildren()[0]); + CPPUNIT_ASSERT(item2); + CPPUNIT_ASSERT_EQUAL(presence->getStatus(), item2->getStatusText()); + + }; + + void testHighestPresence() { + std::vector<String> groups; + groups.push_back("testGroup1"); + JID from("test@testdomain.com"); + xmppRoster_->addContact(from, "name", groups, RosterItemPayload::Both); + Presence::ref lowPresence(new Presence()); + lowPresence->setFrom(withResource(from, "bob")); + lowPresence->setPriority(2); + lowPresence->setStatus("Not here"); + Presence::ref highPresence(new Presence()); + highPresence->setFrom(withResource(from, "bert")); + highPresence->setPriority(10); + highPresence->setStatus("So totally here"); + stanzaChannel_->onPresenceReceived(lowPresence); + stanzaChannel_->onPresenceReceived(highPresence); + ContactRosterItem* item = dynamic_cast<ContactRosterItem*>(dynamic_cast<GroupRosterItem*>(CHILDREN[0])->getChildren()[0]); + CPPUNIT_ASSERT(item); + CPPUNIT_ASSERT_EQUAL(highPresence->getStatus(), item->getStatusText()); + }; + + void testNotHighestPresence() { + std::vector<String> groups; + groups.push_back("testGroup1"); + JID from("test@testdomain.com"); + xmppRoster_->addContact(from, "name", groups, RosterItemPayload::Both); + Presence::ref lowPresence(new Presence()); + lowPresence->setFrom(withResource(from, "bob")); + lowPresence->setPriority(2); + lowPresence->setStatus("Not here"); + Presence::ref highPresence(new Presence()); + highPresence->setFrom(withResource(from, "bert")); + highPresence->setPriority(10); + highPresence->setStatus("So totally here"); + stanzaChannel_->onPresenceReceived(highPresence); + stanzaChannel_->onPresenceReceived(lowPresence); + ContactRosterItem* item = dynamic_cast<ContactRosterItem*>(dynamic_cast<GroupRosterItem*>(CHILDREN[0])->getChildren()[0]); + CPPUNIT_ASSERT(item); + CPPUNIT_ASSERT_EQUAL(highPresence->getStatus(), item->getStatusText()); + }; + + void testUnavailablePresence() { + std::vector<String> groups; + groups.push_back("testGroup1"); + JID from("test@testdomain.com"); + xmppRoster_->addContact(from, "name", groups, RosterItemPayload::Both); + Presence::ref lowPresence(new Presence()); + lowPresence->setFrom(withResource(from, "bob")); + lowPresence->setPriority(2); + lowPresence->setStatus("Not here"); + Presence::ref highPresence(new Presence()); + highPresence->setFrom(withResource(from, "bert")); + highPresence->setPriority(10); + highPresence->setStatus("So totally here"); + Presence::ref highPresenceOffline(new Presence()); + highPresenceOffline->setFrom(withResource(from, "bert")); + highPresenceOffline->setType(Presence::Unavailable); + Presence::ref lowPresenceOffline(new Presence()); + lowPresenceOffline->setFrom(withResource(from, "bob")); + lowPresenceOffline->setStatus("Signing out"); + lowPresenceOffline->setType(Presence::Unavailable); + stanzaChannel_->onPresenceReceived(lowPresence); + stanzaChannel_->onPresenceReceived(highPresence); + stanzaChannel_->onPresenceReceived(highPresenceOffline); + ContactRosterItem* item = dynamic_cast<ContactRosterItem*>(dynamic_cast<GroupRosterItem*>(CHILDREN[0])->getChildren()[0]); + CPPUNIT_ASSERT(item); + /* A verification that if the test fails, it's the RosterController, not the PresenceOracle. */ + Presence::ref high = presenceOracle_->getHighestPriorityPresence(from); + CPPUNIT_ASSERT_EQUAL(Presence::Available, high->getType()); + CPPUNIT_ASSERT_EQUAL(lowPresence->getStatus(), high->getStatus()); + CPPUNIT_ASSERT_EQUAL(StatusShow::Online, item->getStatusShow()); + CPPUNIT_ASSERT_EQUAL(lowPresence->getStatus(), item->getStatusText()); + stanzaChannel_->onPresenceReceived(lowPresenceOffline); + item = dynamic_cast<ContactRosterItem*>(dynamic_cast<GroupRosterItem*>(CHILDREN[0])->getChildren()[0]); + CPPUNIT_ASSERT(item); + /* A verification that if the test fails, it's the RosterController, not the PresenceOracle. */ + high = presenceOracle_->getHighestPriorityPresence(from); + CPPUNIT_ASSERT_EQUAL(Presence::Unavailable, high->getType()); + CPPUNIT_ASSERT_EQUAL(lowPresenceOffline->getStatus(), high->getStatus()); + CPPUNIT_ASSERT_EQUAL(StatusShow::None, item->getStatusShow()); + CPPUNIT_ASSERT_EQUAL(lowPresenceOffline->getStatus(), item->getStatusText()); + }; + + void testAdd() { + std::vector<String> groups; + groups.push_back("testGroup1"); + groups.push_back("testGroup2"); + xmppRoster_->addContact(JID("test@testdomain.com/bob"), "name", groups, RosterItemPayload::Both); + + CPPUNIT_ASSERT_EQUAL(2, static_cast<int>(CHILDREN.size())); + //CPPUNIT_ASSERT_EQUAL(String("Bob"), xmppRoster_->getNameForJID(JID("foo@bar.com"))); + }; + + void testAddSubscription() { + std::vector<String> groups; + JID jid("test@testdomain.com"); + xmppRoster_->addContact(jid, "name", groups, RosterItemPayload::None); + + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(CHILDREN.size())); + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(groupChild(0)->getChildren().size())); + xmppRoster_->addContact(jid, "name", groups, RosterItemPayload::To); + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(CHILDREN.size())); + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(groupChild(0)->getChildren().size())); + + xmppRoster_->addContact(jid, "name", groups, RosterItemPayload::Both); + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(CHILDREN.size())); + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(groupChild(0)->getChildren().size())); + + }; + + void testReceiveRename() { + std::vector<String> groups; + JID jid("test@testdomain.com"); + xmppRoster_->addContact(jid, "name", groups, RosterItemPayload::Both); + + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(CHILDREN.size())); + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(groupChild(0)->getChildren().size())); + CPPUNIT_ASSERT_EQUAL(String("name"), groupChild(0)->getChildren()[0]->getDisplayName()); + xmppRoster_->addContact(jid, "NewName", groups, RosterItemPayload::Both); + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(CHILDREN.size())); + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(groupChild(0)->getChildren().size())); + CPPUNIT_ASSERT_EQUAL(String("NewName"), groupChild(0)->getChildren()[0]->getDisplayName()); + }; + + void testSendRename() { + JID jid("testling@wonderland.lit"); + std::vector<String> groups; + groups.push_back("Friends"); + groups.push_back("Enemies"); + xmppRoster_->addContact(jid, "Bob", groups, RosterItemPayload::From); + CPPUNIT_ASSERT_EQUAL(groups.size(), xmppRoster_->getGroupsForJID(jid).size()); + uiEventStream_->send(boost::shared_ptr<UIEvent>(new RenameRosterItemUIEvent(jid, "Robert"))); + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), channel_->iqs_.size()); + CPPUNIT_ASSERT_EQUAL(IQ::Set, channel_->iqs_[0]->getType()); + boost::shared_ptr<RosterPayload> payload = channel_->iqs_[0]->getPayload<RosterPayload>(); + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), payload->getItems().size()); + RosterItemPayload item = payload->getItems()[0]; + CPPUNIT_ASSERT_EQUAL(jid, item.getJID()); + CPPUNIT_ASSERT_EQUAL(String("Robert"), item.getName()); + + CPPUNIT_ASSERT_EQUAL(groups.size(), item.getGroups().size()); + assertVectorsEqual(groups, item.getGroups(), __LINE__); + } + + void assertVectorsEqual(const std::vector<String>& v1, const std::vector<String>& v2, int line) { + foreach (const String& entry, v1) { + if (std::find(v2.begin(), v2.end(), entry) == v2.end()) { + std::stringstream stream; + stream << "Couldn't find " << entry.getUTF8String() << " in v2 (line " << line << ")"; + CPPUNIT_FAIL(stream.str()); + } + } + } + + private: + JID jid_; + XMPPRosterImpl* xmppRoster_; + MUCRegistry* mucRegistry_; + AvatarManager* avatarManager_; + MockMainWindowFactory* mainWindowFactory_; + NickManager* nickManager_; + NickResolver* nickResolver_; + RosterController* rosterController_; + DummyIQChannel* channel_; + DummyStanzaChannel* stanzaChannel_; + IQRouter* router_; + PresenceOracle* presenceOracle_; + SubscriptionManager* subscriptionManager_; + EventController* eventController_; + UIEventStream* uiEventStream_; + MockMainWindow* mainWindow_; + DummySettingsProvider* settings_; +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(RosterControllerTest); diff --git a/Swift/Controllers/Roster/UnitTest/RosterTest.cpp b/Swift/Controllers/Roster/UnitTest/RosterTest.cpp new file mode 100644 index 0000000..754f3e1 --- /dev/null +++ b/Swift/Controllers/Roster/UnitTest/RosterTest.cpp @@ -0,0 +1,128 @@ +/* + * 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/shared_ptr.hpp> + +#include "Swift/Controllers/Roster/Roster.h" +#include "Swift/Controllers/Roster/GroupRosterItem.h" +#include "Swift/Controllers/Roster/SetPresence.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_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(String("group1"), roster_->getRoot()->getChildren()[0]->getDisplayName()); + CPPUNIT_ASSERT_EQUAL(String("group2"), roster_->getRoot()->getChildren()[1]->getDisplayName()); + CPPUNIT_ASSERT_EQUAL(String("Bert"), static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[0])->getChildren()[0]->getDisplayName()); + CPPUNIT_ASSERT_EQUAL(String("Cookie"), static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[0])->getChildren()[1]->getDisplayName()); + CPPUNIT_ASSERT_EQUAL(String("Ernie"), static_cast<GroupRosterItem*>(roster_->getRoot()->getChildren()[1])->getChildren()[0]->getDisplayName()); + + } + + void testRemoveContact() { + roster_->addContact(jid1_, jid1_, "Bert", "group1", ""); + CPPUNIT_ASSERT_EQUAL(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(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(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(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(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::shared_ptr<Presence>(new 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(String("Ernie"), children[0]->getDisplayName()); + CPPUNIT_ASSERT_EQUAL(String("Bert"), children[1]->getDisplayName()); + CPPUNIT_ASSERT_EQUAL(String("Bird"), children[2]->getDisplayName()); + + presence = boost::shared_ptr<Presence>(new Presence()); + presence->setFrom(jid4c); + presence->setType(Presence::Unavailable); + roster_->removeContact(jid4c); + roster_->applyOnItems(SetPresence(presence, JID::WithResource)); + + } + + private: + Roster *roster_; + JID jid1_; + JID jid2_; + JID jid3_; +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(RosterTest); + |