diff options
Diffstat (limited to 'Swiften/MUC/UnitTest')
-rw-r--r-- | Swiften/MUC/UnitTest/MUCTest.cpp | 76 | ||||
-rw-r--r-- | Swiften/MUC/UnitTest/MockMUC.cpp | 51 | ||||
-rw-r--r-- | Swiften/MUC/UnitTest/MockMUC.h | 96 |
3 files changed, 220 insertions, 3 deletions
diff --git a/Swiften/MUC/UnitTest/MUCTest.cpp b/Swiften/MUC/UnitTest/MUCTest.cpp index 427e938..773edb6 100644 --- a/Swiften/MUC/UnitTest/MUCTest.cpp +++ b/Swiften/MUC/UnitTest/MUCTest.cpp @@ -1,4 +1,4 @@ /* - * Copyright (c) 2010 Remko Tronçon + * Copyright (c) 2010-2014 Remko Tronçon * Licensed under the GNU General Public License v3. * See Documentation/Licenses/GPLv3.txt for more information. @@ -11,5 +11,5 @@ #include <boost/bind.hpp> -#include <Swiften/MUC/MUC.h> +#include <Swiften/MUC/MUCImpl.h> #include <Swiften/Client/DummyStanzaChannel.h> #include <Swiften/Presence/StanzaChannelPresenceSender.h> @@ -31,4 +31,5 @@ class MUCTest : public CppUnit::TestFixture { CPPUNIT_TEST(testCreateInstant); CPPUNIT_TEST(testReplicateBug); + CPPUNIT_TEST(testNicknameChange); /*CPPUNIT_TEST(testJoin_Success); CPPUNIT_TEST(testJoin_Fail);*/ @@ -42,4 +43,5 @@ class MUCTest : public CppUnit::TestFixture { stanzaChannelPresenceSender = new StanzaChannelPresenceSender(channel); presenceSender = new DirectedPresenceSender(stanzaChannelPresenceSender); + nickChanges = 0; } @@ -142,4 +144,65 @@ class MUCTest : public CppUnit::TestFixture { } + void testNicknameChange() { + MUC::ref testling = createMUC(JID("foo@bar.com")); + // Join as Rabbit + testling->joinAs("Rabbit"); + + // Rabbit joins + Presence::ref rabbitJoins = boost::make_shared<Presence>(); + rabbitJoins->setTo("test@swift.im/6913d576d55f0b67"); + rabbitJoins->setFrom(testling->getJID().toString() + "/Rabbit"); + channel->onPresenceReceived(rabbitJoins); + CPPUNIT_ASSERT_EQUAL(true, testling->hasOccupant("Rabbit")); + + // Alice joins + Presence::ref aliceJoins = boost::make_shared<Presence>(); + aliceJoins->setTo("test@swift.im/6913d576d55f0b67"); + aliceJoins->setFrom(testling->getJID().toString() + "/Alice"); + channel->onPresenceReceived(aliceJoins); + CPPUNIT_ASSERT_EQUAL(true, testling->hasOccupant("Alice")); + + // Change nick to Dodo + testling->changeNickname("Dodo"); + Presence::ref stanza = channel->getStanzaAtIndex<Presence>(1); + CPPUNIT_ASSERT(stanza); + CPPUNIT_ASSERT_EQUAL(std::string("Dodo"), stanza->getTo().getResource()); + + // Alice changes nick to Alice2 + stanza = boost::make_shared<Presence>(); + stanza->setFrom(JID("foo@bar.com/Alice")); + stanza->setTo(JID(router->getJID())); + stanza->setType(Presence::Unavailable); + MUCUserPayload::ref mucPayload(new MUCUserPayload()); + MUCItem myItem; + myItem.affiliation = MUCOccupant::Member; + myItem.nick = "Alice2"; + myItem.role = MUCOccupant::Participant; + mucPayload->addItem(myItem); + mucPayload->addStatusCode(303); + stanza->addPayload(mucPayload); + channel->onPresenceReceived(stanza); + CPPUNIT_ASSERT_EQUAL(1, nickChanges); + CPPUNIT_ASSERT_EQUAL(false, testling->hasOccupant("Alice")); + CPPUNIT_ASSERT_EQUAL(true, testling->hasOccupant("Alice2")); + + // We (Rabbit) change nick to Robot + stanza = boost::make_shared<Presence>(); + stanza->setFrom(JID("foo@bar.com/Rabbit")); + stanza->setTo(JID(router->getJID())); + stanza->setType(Presence::Unavailable); + mucPayload = MUCUserPayload::ref(new MUCUserPayload()); + myItem.affiliation = MUCOccupant::Member; + myItem.nick = "Robot"; + myItem.role = MUCOccupant::Participant; + mucPayload->addItem(myItem); + mucPayload->addStatusCode(303); + stanza->addPayload(mucPayload); + channel->onPresenceReceived(stanza); + CPPUNIT_ASSERT_EQUAL(2, nickChanges); + CPPUNIT_ASSERT_EQUAL(false, testling->hasOccupant("Rabbit")); + CPPUNIT_ASSERT_EQUAL(true, testling->hasOccupant("Robot")); + } + /*void testJoin_Success() { MUC::ref testling = createMUC(JID("foo@bar.com")); @@ -159,5 +222,7 @@ class MUCTest : public CppUnit::TestFixture { private: MUC::ref createMUC(const JID& jid) { - return boost::make_shared<MUC>(channel, router, presenceSender, jid, mucRegistry); + MUC::ref muc = boost::make_shared<MUCImpl>(channel, router, presenceSender, jid, mucRegistry); + muc->onOccupantNicknameChanged.connect(boost::bind(&MUCTest::handleOccupantNicknameChanged, this, _1, _2)); + return muc; } @@ -178,4 +243,8 @@ class MUCTest : public CppUnit::TestFixture { } + void handleOccupantNicknameChanged(const std::string&, const std::string&) { + nickChanges++; + } + private: DummyStanzaChannel* channel; @@ -189,4 +258,5 @@ class MUCTest : public CppUnit::TestFixture { }; std::vector<JoinResult> joinResults; + int nickChanges; }; diff --git a/Swiften/MUC/UnitTest/MockMUC.cpp b/Swiften/MUC/UnitTest/MockMUC.cpp new file mode 100644 index 0000000..9ca35ec --- /dev/null +++ b/Swiften/MUC/UnitTest/MockMUC.cpp @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2013 Kevin Smith and Remko Tronçon + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#include <Swiften/MUC/UnitTest/MockMUC.h> + +namespace Swift { + +MockMUC::MockMUC(const JID &muc) +: ownMUCJID(muc) +{ +} + +MockMUC::~MockMUC() { +} + +void MockMUC::insertOccupant(const MUCOccupant& occupant) +{ + occupants_.insert(std::make_pair(occupant.getNick(), occupant)); + onOccupantJoined(occupant); +} + +const MUCOccupant& MockMUC::getOccupant(const std::string& nick) { + return occupants_.find(nick)->second; +} + +bool MockMUC::hasOccupant(const std::string& nick) { + return occupants_.find(nick) != occupants_.end(); +} + +void MockMUC::changeAffiliation(const JID &jid, MUCOccupant::Affiliation newAffilation) { + std::map<std::string, MUCOccupant>::iterator i = occupants_.find(jid.getResource()); + if (i != occupants_.end()) { + const MUCOccupant old = i->second; + i->second = MUCOccupant(old.getNick(), old.getRole(), newAffilation); + onOccupantAffiliationChanged(i->first, newAffilation, old.getAffiliation()); + } +} + +void MockMUC::changeOccupantRole(const JID &jid, MUCOccupant::Role newRole) { + std::map<std::string, MUCOccupant>::iterator i = occupants_.find(jid.getResource()); + if (i != occupants_.end()) { + const MUCOccupant old = i->second; + i->second = MUCOccupant(old.getNick(), newRole, old.getAffiliation()); + onOccupantRoleChanged(i->first, i->second, old.getRole()); + } +} + +} diff --git a/Swiften/MUC/UnitTest/MockMUC.h b/Swiften/MUC/UnitTest/MockMUC.h new file mode 100644 index 0000000..8673a90 --- /dev/null +++ b/Swiften/MUC/UnitTest/MockMUC.h @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2013 Kevin Smith and Remko Tronçon + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#pragma once + +#include <Swiften/MUC/MUC.h> +#include <Swiften/MUC/MUCRegistry.h> +#include <Swiften/JID/JID.h> +#include <Swiften/Elements/Message.h> +#include <Swiften/Elements/Presence.h> +#include <Swiften/Elements/MUCOccupant.h> +#include <Swiften/Elements/MUCOwnerPayload.h> +#include <Swiften/Elements/MUCAdminPayload.h> +#include <Swiften/Elements/Form.h> +#include <Swiften/Base/API.h> +#include <Swiften/Base/boost_bsignals.h> +#include <boost/signals/connection.hpp> +#include <boost/shared_ptr.hpp> +#include <string> +#include <map> + +namespace Swift { + class StanzaChannel; + class IQRouter; + class DirectedPresenceSender; + + class SWIFTEN_API MockMUC : public MUC{ + public: + typedef boost::shared_ptr<MockMUC> ref; + + public: + MockMUC(const JID &muc); + virtual ~MockMUC(); + + /** + * Cause a user to appear to have entered the room. For testing only. + */ + void insertOccupant(const MUCOccupant& occupant); + + /** + * Returns the (bare) JID of the MUC. + */ + virtual JID getJID() const { + return ownMUCJID.toBare(); + } + /** + * Returns if the room is unlocked and other people can join the room. + * @return True if joinable by others; false otherwise. + */ + virtual bool isUnlocked() const { return true; } + + virtual void joinAs(const std::string&) {} + virtual void joinWithContextSince(const std::string&, const boost::posix_time::ptime&) {} + /*virtual void queryRoomInfo(); */ + /*virtual void queryRoomItems(); */ + /*virtual std::string getCurrentNick() = 0; */ + virtual std::map<std::string, MUCOccupant> getOccupants() const { return occupants_; } + virtual void changeNickname(const std::string&) { } + virtual void part() {} + /*virtual void handleIncomingMessage(Message::ref message) = 0; */ + /** Expose public so it can be called when e.g. user goes offline */ + virtual void handleUserLeft(LeavingType) {} + /** Get occupant information*/ + virtual const MUCOccupant& getOccupant(const std::string&); + virtual bool hasOccupant(const std::string&); + virtual void kickOccupant(const JID&) {} + virtual void changeOccupantRole(const JID&, MUCOccupant::Role); + virtual void requestAffiliationList(MUCOccupant::Affiliation) {} + virtual void changeAffiliation(const JID&, MUCOccupant::Affiliation); + virtual void changeSubject(const std::string&) {} + virtual void requestConfigurationForm() {} + virtual void configureRoom(Form::ref) {} + virtual void cancelConfigureRoom() {} + virtual void destroyRoom() {} + /** Send an invite for the person to join the MUC */ + virtual void invitePerson(const JID&, const std::string&, bool, bool) {} + virtual void setCreateAsReservedIfNew() {} + virtual void setPassword(const boost::optional<std::string>&) {} + + protected: + virtual bool isFromMUC(const JID& j) const { + return ownMUCJID.equals(j, JID::WithoutResource); + } + + virtual const std::string& getOwnNick() const { + return ownMUCJID.getResource(); + } + + private: + JID ownMUCJID; + std::map<std::string, MUCOccupant> occupants_; + }; +} |