diff options
author | Kevin Smith <git@kismith.co.uk> | 2009-11-28 21:44:26 (GMT) |
---|---|---|
committer | Kevin Smith <git@kismith.co.uk> | 2009-12-24 13:43:44 (GMT) |
commit | f08d7dc30a1242ea5a148377c5593188a8fb1317 (patch) | |
tree | f55fd5175d21c804528c5f531c08880621274e71 /Swift | |
parent | 39209e6b419af417d3db011d7fd29433e88c8b3a (diff) | |
download | swift-contrib-f08d7dc30a1242ea5a148377c5593188a8fb1317.zip swift-contrib-f08d7dc30a1242ea5a148377c5593188a8fb1317.tar.bz2 |
Extract the Chat dialog management from MainController into ChatsManager.
There is a single unit test so far - but that's more than was there before, so I'm pushing. Expect more unit tests shortly.
Resolves: #139
Diffstat (limited to 'Swift')
-rw-r--r-- | Swift/Controllers/ChatsManager.cpp | 139 | ||||
-rw-r--r-- | Swift/Controllers/ChatsManager.h | 54 | ||||
-rw-r--r-- | Swift/Controllers/MainController.cpp | 104 | ||||
-rw-r--r-- | Swift/Controllers/MainController.h | 16 | ||||
-rw-r--r-- | Swift/Controllers/RosterController.cpp | 5 | ||||
-rw-r--r-- | Swift/Controllers/SConscript | 3 | ||||
-rw-r--r-- | Swift/Controllers/UIEvents/JoinMUCUIEvent.h | 19 | ||||
-rw-r--r-- | Swift/Controllers/UIEvents/RequestChatUIEvent.h | 17 | ||||
-rw-r--r-- | Swift/Controllers/UnitTest/ChatsManagerTest.cpp | 99 | ||||
-rw-r--r-- | Swift/Controllers/UnitTest/MockChatWindow.cpp | 7 | ||||
-rw-r--r-- | Swift/Controllers/UnitTest/MockChatWindow.h | 36 |
11 files changed, 391 insertions, 108 deletions
diff --git a/Swift/Controllers/ChatsManager.cpp b/Swift/Controllers/ChatsManager.cpp new file mode 100644 index 0000000..ef187f4 --- /dev/null +++ b/Swift/Controllers/ChatsManager.cpp @@ -0,0 +1,139 @@ +#include "Swift/Controllers/ChatsManager.h" + +#include "Swiften/Client/Client.h" + +#include "Swift/Controllers/ChatController.h" +#include "Swift/Controllers/EventController.h" +#include "Swift/Controllers/MUCController.h" +#include "Swiften/Presence/PresenceSender.h" + +namespace Swift { + +typedef std::pair<JID, ChatController*> JIDChatControllerPair; +typedef std::pair<JID, MUCController*> JIDMUCControllerPair; + +ChatsManager::ChatsManager(JID jid, StanzaChannel* stanzaChannel, IQRouter* iqRouter, EventController* eventController, ChatWindowFactory* chatWindowFactory, TreeWidgetFactory* treeWidgetFactory, NickResolver* nickResolver, PresenceOracle* presenceOracle, boost::shared_ptr<DiscoInfo> serverDiscoInfo, PresenceSender* presenceSender) : jid_(jid) { + eventController_ = eventController; + stanzaChannel_ = stanzaChannel; + iqRouter_ = iqRouter; + chatWindowFactory_ = chatWindowFactory; + treeWidgetFactory_ = treeWidgetFactory; + nickResolver_ = nickResolver; + presenceOracle_ = presenceOracle; + avatarManager_ = NULL; + serverDiscoInfo_ = serverDiscoInfo; + presenceSender_ = presenceSender; +} + +ChatsManager::~ChatsManager() { + foreach (JIDChatControllerPair controllerPair, chatControllers_) { + delete controllerPair.second; + } + foreach (JIDMUCControllerPair controllerPair, mucControllers_) { + delete controllerPair.second; + } + +} + +void ChatsManager::setAvatarManager(AvatarManager* avatarManager) { + avatarManager_ = avatarManager; +} + +// void ChatsManager::handleUIEvent(boost::shared_ptr<UIEvent> rawEvent) { +// { +// boost::shared_ptr<RequestChatUIEvent> event = boost::dynamic_pointer_cast<RequestChatUIEvent>(rawEvent); +// if (event != NULL) { +// handleChatRequest(event->getContact()); +// return; +// } +// } +// { +// boost::shared_ptr<JoinMUCUIEvent> event = boost::dynamic_pointer_cast<JoinMUCUIEvent>(rawEvent); +// if (event != NULL) { +// handleJoinMUCRequest(event->getRoom(), event->getNick()); +// } +// } +// } + + +void ChatsManager::setServerDiscoInfo(boost::shared_ptr<DiscoInfo> info) { + foreach (JIDChatControllerPair pair, chatControllers_) { + pair.second->setAvailableServerFeatures(info); + } + foreach (JIDMUCControllerPair pair, mucControllers_) { + pair.second->setAvailableServerFeatures(info); + } +} + +void ChatsManager::setEnabled(bool enabled) { + foreach (JIDChatControllerPair controllerPair, chatControllers_) { + //printf("Setting enabled on %d to %d\n", controllerPair.second, enabled); + controllerPair.second->setEnabled(enabled); + } + foreach (JIDMUCControllerPair controllerPair, mucControllers_) { + controllerPair.second->setEnabled(enabled); + } + +} + +void ChatsManager::handleChatRequest(const String &contact) { + ChatController* controller = getChatController(JID(contact)); + controller->showChatWindow(); + controller->activateChatWindow(); +} + +ChatController* ChatsManager::getChatController(const JID &contact) { + JID lookupContact(contact); + if (chatControllers_.find(lookupContact) == chatControllers_.end()) { + lookupContact = JID(contact.toBare()); + } + if (chatControllers_.find(lookupContact) == chatControllers_.end()) { + chatControllers_[contact] = new ChatController(jid_, stanzaChannel_, iqRouter_, chatWindowFactory_, contact, nickResolver_, presenceOracle_, avatarManager_); + chatControllers_[contact]->setAvailableServerFeatures(serverDiscoInfo_); + lookupContact = contact; + } + return chatControllers_[lookupContact]; +} + +void ChatsManager::handleChatControllerJIDChanged(const JID& from, const JID& to) { + chatControllers_[to] = chatControllers_[from]; + chatControllers_.erase(from); +} + +void ChatsManager::handleJoinMUCRequest(const JID &muc, const String &nick) { + mucControllers_[muc] = new MUCController(jid_, muc, nick, stanzaChannel_, presenceSender_, iqRouter_, chatWindowFactory_, treeWidgetFactory_, presenceOracle_, avatarManager_); + mucControllers_[muc]->setAvailableServerFeatures(serverDiscoInfo_); +} + +void ChatsManager::handleIncomingMessage(boost::shared_ptr<Message> message) { + JID jid = message->getFrom(); + boost::shared_ptr<MessageEvent> event(new MessageEvent(message)); + if (!event->isReadable()) { + return; + } + + // Try to deliver it to a MUC + if (message->getType() == Message::Groupchat || message->getType() == Message::Error) { + std::map<JID, MUCController*>::iterator i = mucControllers_.find(jid.toBare()); + if (i != mucControllers_.end()) { + i->second->handleIncomingMessage(event); + return; + } + else if (message->getType() == Message::Groupchat) { + //FIXME: Error handling - groupchat messages from an unknown muc. + return; + } + } + + //if not a mucroom + eventController_->handleIncomingEvent(event); + getChatController(jid)->handleIncomingMessage(event); +} + +bool ChatsManager::isMUC(const JID& jid) const { + return mucControllers_.find(jid.toBare()) != mucControllers_.end(); +} + + + +} diff --git a/Swift/Controllers/ChatsManager.h b/Swift/Controllers/ChatsManager.h new file mode 100644 index 0000000..e897e59 --- /dev/null +++ b/Swift/Controllers/ChatsManager.h @@ -0,0 +1,54 @@ +#pragma once + +#include <map> + +#include <boost/shared_ptr.hpp> + +#include "Swiften/Base/String.h" +#include "Swiften/Elements/DiscoInfo.h" +#include "Swiften/Elements/Message.h" +#include "Swiften/JID/JID.h" +#include "Swiften/MUC/MUCRegistry.h" + +namespace Swift { + class EventController; + class ChatController; + class MUCController; + class ChatWindowFactory; + class TreeWidgetFactory; + class NickResolver; + class PresenceOracle; + class AvatarManager; + class StanzaChannel; + class IQRouter; + class PresenceSender; + class ChatsManager : public MUCRegistry { + public: + ChatsManager(JID jid, StanzaChannel* stanzaChannel, IQRouter* iqRouter, EventController* eventController, ChatWindowFactory* chatWindowFactory, TreeWidgetFactory* treeWidgetFactory, NickResolver* nickResolver, PresenceOracle* presenceOracle, boost::shared_ptr<DiscoInfo> serverDiscoInfo, PresenceSender* presenceSender); + ~ChatsManager(); + void setAvatarManager(AvatarManager* avatarManager); + void setEnabled(bool enabled); + void setServerDiscoInfo(boost::shared_ptr<DiscoInfo> info); + void handleIncomingMessage(boost::shared_ptr<Message> message); + void handleChatRequest(const String& contact); + void handleJoinMUCRequest(const JID& muc, const String& nick); + private: + void handleChatControllerJIDChanged(const JID& from, const JID& to); + ChatController* getChatController(const JID &contact); + virtual bool isMUC(const JID& muc) const; + + std::map<JID, MUCController*> mucControllers_; + std::map<JID, ChatController*> chatControllers_; + EventController* eventController_; + JID jid_; + StanzaChannel* stanzaChannel_; + IQRouter* iqRouter_;; + ChatWindowFactory* chatWindowFactory_; + TreeWidgetFactory* treeWidgetFactory_; + NickResolver* nickResolver_; + PresenceOracle* presenceOracle_; + AvatarManager* avatarManager_; + PresenceSender* presenceSender_; + boost::shared_ptr<DiscoInfo> serverDiscoInfo_; + }; +} diff --git a/Swift/Controllers/MainController.cpp b/Swift/Controllers/MainController.cpp index 8800da0..29d67b5 100644 --- a/Swift/Controllers/MainController.cpp +++ b/Swift/Controllers/MainController.cpp @@ -9,6 +9,7 @@ #include "Swiften/Application/ApplicationMessageDisplay.h" #include "Swift/Controllers/ChatController.h" #include "Swift/Controllers/ChatWindowFactory.h" +#include "Swift/Controllers/ChatsManager.h" #include "Swift/Controllers/EventController.h" #include "Swift/Controllers/UIInterfaces/LoginWindow.h" #include "Swift/Controllers/UIInterfaces/LoginWindowFactory.h" @@ -51,14 +52,13 @@ static const String CLIENT_NAME = "Swift"; static const String CLIENT_VERSION = "0.3"; static const String CLIENT_NODE = "http://swift.im"; -typedef std::pair<JID, ChatController*> JIDChatControllerPair; -typedef std::pair<JID, MUCController*> JIDMUCControllerPair; MainController::MainController(ChatWindowFactory* chatWindowFactory, MainWindowFactory *mainWindowFactory, LoginWindowFactory *loginWindowFactory, TreeWidgetFactory *treeWidgetFactory, SettingsProvider *settings, Application* application, SystemTray* systemTray, SoundPlayer* soundPlayer, XMLConsoleWidgetFactory* xmlConsoleWidgetFactory) : timerFactory_(&boostIOServiceThread_.getIOService()), idleDetector_(&idleQuerier_, &timerFactory_, 100), client_(NULL), presenceSender_(NULL), chatWindowFactory_(chatWindowFactory), mainWindowFactory_(mainWindowFactory), loginWindowFactory_(loginWindowFactory), treeWidgetFactory_(treeWidgetFactory), settings_(settings), xmppRosterController_(NULL), rosterController_(NULL), loginWindow_(NULL), clientVersionResponder_(NULL), nickResolver_(NULL), discoResponder_(NULL) { application_ = application; presenceOracle_ = NULL; avatarManager_ = NULL; + chatsManager_ = NULL; uiEventStream_ = new UIEventStream(); avatarStorage_ = new AvatarFileStorage(application_->getAvatarDir()); @@ -84,12 +84,6 @@ MainController::MainController(ChatWindowFactory* chatWindowFactory, MainWindowF } MainController::~MainController() { - foreach (JIDChatControllerPair controllerPair, chatControllers_) { - delete controllerPair.second; - } - foreach (JIDMUCControllerPair controllerPair, mucControllers_) { - delete controllerPair.second; - } delete systemTrayController_; delete soundEventController_; delete avatarStorage_; @@ -101,6 +95,8 @@ MainController::~MainController() { void MainController::resetClient() { serverDiscoInfo_ = boost::shared_ptr<DiscoInfo>(); xmppRoster_ = boost::shared_ptr<XMPPRoster>(); + delete chatsManager_; + chatsManager_ = NULL; delete presenceOracle_; presenceOracle_ = NULL; delete nickResolver_; @@ -129,17 +125,21 @@ void MainController::handleConnected() { serverDiscoInfo_ = boost::shared_ptr<DiscoInfo>(new DiscoInfo()); xmppRoster_ = boost::shared_ptr<XMPPRoster>(new XMPPRoster()); presenceOracle_ = new PresenceOracle(client_); + nickResolver_ = new NickResolver(xmppRoster_); + chatsManager_ = new ChatsManager(jid_, client_, client_, eventController_, chatWindowFactory_, treeWidgetFactory_, nickResolver_, presenceOracle_, serverDiscoInfo_, presenceSender_); lastSentPresence_ = boost::shared_ptr<Presence>(); client_->onPresenceReceived.connect(boost::bind(&MainController::handleIncomingPresence, this, _1)); - nickResolver_ = new NickResolver(xmppRoster_); + avatarManager_ = new AvatarManager(client_, client_, avatarStorage_, chatsManager_); + chatsManager_->setAvatarManager(avatarManager_); + + client_->onMessageReceived.connect(boost::bind(&ChatsManager::handleIncomingMessage, chatsManager_, _1)); - avatarManager_ = new AvatarManager(client_, client_, avatarStorage_, this); rosterController_ = new RosterController(jid_, xmppRoster_, avatarManager_, mainWindowFactory_, treeWidgetFactory_, nickResolver_); - rosterController_->onStartChatRequest.connect(boost::bind(&MainController::handleChatRequest, this, _1)); - rosterController_->onJoinMUCRequest.connect(boost::bind(&MainController::handleJoinMUCRequest, this, _1, _2)); + rosterController_->onStartChatRequest.connect(boost::bind(&ChatsManager::handleChatRequest, chatsManager_, _1)); + rosterController_->onJoinMUCRequest.connect(boost::bind(&ChatsManager::handleJoinMUCRequest, chatsManager_, _1, _2)); rosterController_->onChangeStatusRequest.connect(boost::bind(&MainController::handleChangeStatusRequest, this, _1, _2)); rosterController_->onSignOutRequest.connect(boost::bind(&MainController::signOut, this)); @@ -272,7 +272,6 @@ void MainController::performLoginFromCachedCredentials() { } client_->onError.connect(boost::bind(&MainController::handleError, this, _1)); client_->onConnected.connect(boost::bind(&MainController::handleConnected, this)); - client_->onMessageReceived.connect(boost::bind(&MainController::handleIncomingMessage, this, _1)); } client_->connect(); } @@ -314,14 +313,6 @@ void MainController::handleCancelLoginRequest() { void MainController::signOut() { logout(); loginWindow_->loggedOut(); - foreach (JIDChatControllerPair controllerPair, chatControllers_) { - delete controllerPair.second; - } - chatControllers_.clear(); - foreach (JIDMUCControllerPair controllerPair, mucControllers_) { - delete controllerPair.second; - } - mucControllers_.clear(); delete rosterController_; rosterController_ = NULL; resetClient(); @@ -339,88 +330,21 @@ void MainController::logout() { } void MainController::setManagersEnabled(bool enabled) { - foreach (JIDChatControllerPair controllerPair, chatControllers_) { - //printf("Setting enabled on %d to %d\n", controllerPair.second, enabled); - controllerPair.second->setEnabled(enabled); - } - foreach (JIDMUCControllerPair controllerPair, mucControllers_) { - controllerPair.second->setEnabled(enabled); - } + chatsManager_->setEnabled(enabled); if (rosterController_) { rosterController_->setEnabled(enabled); } } -void MainController::handleChatRequest(const String &contact) { - ChatController* controller = getChatController(JID(contact)); - controller->showChatWindow(); - controller->activateChatWindow(); -} - -ChatController* MainController::getChatController(const JID &contact) { - JID lookupContact(contact); - if (chatControllers_.find(lookupContact) == chatControllers_.end()) { - lookupContact = JID(contact.toBare()); - } - if (chatControllers_.find(lookupContact) == chatControllers_.end()) { - chatControllers_[contact] = new ChatController(jid_, client_, client_, chatWindowFactory_, contact, nickResolver_, presenceOracle_, avatarManager_); - chatControllers_[contact]->setAvailableServerFeatures(serverDiscoInfo_); - lookupContact = contact; - } - return chatControllers_[lookupContact]; -} - -void MainController::handleChatControllerJIDChanged(const JID& from, const JID& to) { - chatControllers_[to] = chatControllers_[from]; - chatControllers_.erase(from); -} - -void MainController::handleJoinMUCRequest(const JID &muc, const String &nick) { - mucControllers_[muc] = new MUCController(jid_, muc, nick, client_, presenceSender_, client_, chatWindowFactory_, treeWidgetFactory_, presenceOracle_, avatarManager_); - mucControllers_[muc]->setAvailableServerFeatures(serverDiscoInfo_); -} -void MainController::handleIncomingMessage(boost::shared_ptr<Message> message) { - JID jid = message->getFrom(); - boost::shared_ptr<MessageEvent> event(new MessageEvent(message)); - if (!event->isReadable()) { - return; - } - - // Try to deliver it to a MUC - if (message->getType() == Message::Groupchat || message->getType() == Message::Error) { - std::map<JID, MUCController*>::iterator i = mucControllers_.find(jid.toBare()); - if (i != mucControllers_.end()) { - i->second->handleIncomingMessage(event); - return; - } - else if (message->getType() == Message::Groupchat) { - //FIXME: Error handling - groupchat messages from an unknown muc. - return; - } - } - - //if not a mucroom - eventController_->handleIncomingEvent(event); - getChatController(jid)->handleIncomingMessage(event); -} void MainController::handleServerDiscoInfoResponse(boost::shared_ptr<DiscoInfo> info, const boost::optional<ErrorPayload>& error) { if (!error) { serverDiscoInfo_ = info; - foreach (JIDChatControllerPair pair, chatControllers_) { - pair.second->setAvailableServerFeatures(info); - } - foreach (JIDMUCControllerPair pair, mucControllers_) { - pair.second->setAvailableServerFeatures(info); - } + chatsManager_->setServerDiscoInfo(info); } } -bool MainController::isMUC(const JID& jid) const { - return mucControllers_.find(jid.toBare()) != mucControllers_.end(); -} - void MainController::handleOwnVCardReceived(boost::shared_ptr<VCard> vCard, const boost::optional<ErrorPayload>& error) { if (!error && !vCard->getPhoto().isEmpty()) { vCardPhotoHash_ = Hexify::hexify(SHA1::getHash(vCard->getPhoto())); diff --git a/Swift/Controllers/MainController.h b/Swift/Controllers/MainController.h index c260f33..b6a1254 100644 --- a/Swift/Controllers/MainController.h +++ b/Swift/Controllers/MainController.h @@ -15,10 +15,8 @@ #include "Swiften/Elements/DiscoInfo.h" #include "Swiften/Elements/ErrorPayload.h" #include "Swiften/Elements/Presence.h" -#include "Swiften/Elements/Message.h" #include "Swiften/Settings/SettingsProvider.h" #include "Swiften/Elements/CapsInfo.h" -#include "Swiften/MUC/MUCRegistry.h" #include "Swiften/Roster/XMPPRoster.h" namespace Swift { @@ -27,6 +25,7 @@ namespace Swift { class Client; class ChatWindowFactory; class ChatController; + class ChatsManager; class EventController; class MainWindowFactory; class MainWindow; @@ -51,7 +50,7 @@ namespace Swift { class UIEventStream; class XMLConsoleWidgetFactory; - class MainController : public MUCRegistry { + class MainController { public: MainController(ChatWindowFactory* chatWindowFactory, MainWindowFactory *mainWindowFactory, LoginWindowFactory *loginWindowFactory, TreeWidgetFactory* treeWidgetFactory, SettingsProvider *settings, Application* application, SystemTray* systemTray, SoundPlayer* soundPlayer, XMLConsoleWidgetFactory* xmlConsoleWidgetFactory); ~MainController(); @@ -59,27 +58,19 @@ namespace Swift { private: void resetClient(); - void handleConnected(); void handleLoginRequest(const String& username, const String& password, const String& certificateFile, bool remember); void handleCancelLoginRequest(); - void handleChatRequest(const String& contact); - void handleJoinMUCRequest(const JID& muc, const String& nick); void handleIncomingPresence(boost::shared_ptr<Presence> presence); - void handleChatControllerJIDChanged(const JID& from, const JID& to); - void handleIncomingMessage(boost::shared_ptr<Message> message); void handleChangeStatusRequest(StatusShow::Type show, const String &statusText); void handleError(const ClientError& error); void handleServerDiscoInfoResponse(boost::shared_ptr<DiscoInfo>, const boost::optional<ErrorPayload>&); void handleEventQueueLengthChange(int count); void handleOwnVCardReceived(boost::shared_ptr<VCard> vCard, const boost::optional<ErrorPayload>& error); - ChatController* getChatController(const JID &contact); void sendPresence(boost::shared_ptr<Presence> presence); void handleInputIdleChanged(bool); void logout(); void signOut(); - - virtual bool isMUC(const JID& muc) const; void performLoginFromCachedCredentials(); void reconnectAfterError(); @@ -108,9 +99,8 @@ namespace Swift { DiscoInfoResponder* discoResponder_; UIEventStream* uiEventStream_; XMLConsoleController* xmlConsoleController_; + ChatsManager* chatsManager_; boost::shared_ptr<CapsInfo> capsInfo_; - std::map<JID, MUCController*> mucControllers_; - std::map<JID, ChatController*> chatControllers_; boost::shared_ptr<DiscoInfo> serverDiscoInfo_; boost::shared_ptr<XMPPRoster> xmppRoster_;; JID jid_; diff --git a/Swift/Controllers/RosterController.cpp b/Swift/Controllers/RosterController.cpp index 807fc19..df3453d 100644 --- a/Swift/Controllers/RosterController.cpp +++ b/Swift/Controllers/RosterController.cpp @@ -28,7 +28,6 @@ RosterController::RosterController(const JID& jid, boost::shared_ptr<XMPPRoster> roster_->addFilter(offlineFilter_); - mainWindow_->onStartChatRequest.connect(boost::bind(&RosterController::handleStartChatRequest, this, _1)); mainWindow_->onJoinMUCRequest.connect(boost::bind(&RosterController::handleJoinMUCRequest, this, _1, _2)); mainWindow_->onChangeStatusRequest.connect(boost::bind(&RosterController::handleChangeStatusRequest, this, _1, _2)); mainWindow_->onShowOfflineToggled.connect(boost::bind(&RosterController::handleShowOfflineToggled, this, _1)); @@ -145,10 +144,6 @@ void RosterController::handleAvatarChanged(const JID& jid, const String&) { } } -void RosterController::handleStartChatRequest(const JID& contact) { - onStartChatRequest(contact); -} - void RosterController::handleJoinMUCRequest(const JID &muc, const String &nick) { onJoinMUCRequest(JID(muc), nick); } diff --git a/Swift/Controllers/SConscript b/Swift/Controllers/SConscript index 02d0b52..82bacaa 100644 --- a/Swift/Controllers/SConscript +++ b/Swift/Controllers/SConscript @@ -10,6 +10,7 @@ myenv.MergeFlags(env["BOOST_FLAGS"]) myenv.StaticLibrary("SwiftControllers", [ "ChatController.cpp", "ChatControllerBase.cpp", + "ChatsManager.cpp", "MainController.cpp", "NickResolver.cpp", "RosterController.cpp", @@ -29,4 +30,6 @@ env.Append(UNITTEST_SOURCES = [ File("UnitTest/RosterControllerTest.cpp"), File("UnitTest/XMPPRosterControllerTest.cpp"), File("UnitTest/PreviousStatusStoreTest.cpp"), + File("UnitTest/ChatsManagerTest.cpp"), + File("UnitTest/MockChatWindow.cpp"), ]) diff --git a/Swift/Controllers/UIEvents/JoinMUCUIEvent.h b/Swift/Controllers/UIEvents/JoinMUCUIEvent.h new file mode 100644 index 0000000..3f31abb --- /dev/null +++ b/Swift/Controllers/UIEvents/JoinMUCUIEvent.h @@ -0,0 +1,19 @@ +//Not used yet. + +#pragma once + +#include "Swiften/Base/String.h" + +#include "Swift/Controllers/UIEvents/UIEvent.h" + +namespace Swift { + class JoinMUCUIEvent : public UIEvent { + public: + JoinMUCUIEvent(const JID& jid, const String& contact) : jid_(jid), contact_(contact) {}; + String getContact() {return contact_;}; + JID getJID() {return jid_;}; + private: + String contact_; + JID jid_; + }; +} diff --git a/Swift/Controllers/UIEvents/RequestChatUIEvent.h b/Swift/Controllers/UIEvents/RequestChatUIEvent.h new file mode 100644 index 0000000..529f498 --- /dev/null +++ b/Swift/Controllers/UIEvents/RequestChatUIEvent.h @@ -0,0 +1,17 @@ +//Not used yet. + +#pragma once + +#include "Swiften/Base/String.h" + +#include "Swift/Controllers/UIEvents/UIEvent.h" + +namespace Swift { + class RequestChatUIEvent : public UIEvent { + public: + RequestChatUIEvent(const String& contact) : contact_(contact) {}; + String getContact() {return contact_;} + private: + String contact_; + }; +} diff --git a/Swift/Controllers/UnitTest/ChatsManagerTest.cpp b/Swift/Controllers/UnitTest/ChatsManagerTest.cpp new file mode 100644 index 0000000..6f6958e --- /dev/null +++ b/Swift/Controllers/UnitTest/ChatsManagerTest.cpp @@ -0,0 +1,99 @@ +#include <cppunit/extensions/HelperMacros.h> +#include <cppunit/extensions/TestFactoryRegistry.h> +#include "3rdParty/hippomocks.h" + +#include "Swift/Controllers/ChatsManager.h" + +#include "Swift/Controllers/ChatWindow.h" +#include "Swift/Controllers/ChatWindowFactory.h" +#include "Swiften/Roster/TreeWidgetFactory.h" +#include "Swiften/Client/Client.h" +#include "Swift/Controllers/ChatController.h" +#include "Swift/Controllers/EventController.h" +#include "Swift/Controllers/MUCController.h" +#include "Swiften/Presence/PresenceSender.h" +#include "Swiften/Avatars/UnitTest/MockAvatarManager.h" +#include "Swift/Controllers/NickResolver.h" +#include "Swiften/Roster/XMPPRoster.h" +#include "Swift/Controllers/UnitTest/MockChatWindow.h" +#include "Swiften/Client/DummyStanzaChannel.h" +#include "Swiften/Queries/DummyIQChannel.h" +#include "Swiften/Presence/PresenceOracle.h" + + +using namespace Swift; + +class ChatsManagerTest : public CppUnit::TestFixture +{ + CPPUNIT_TEST_SUITE(ChatsManagerTest); + CPPUNIT_TEST(testFirstOpenWindow); + CPPUNIT_TEST_SUITE_END(); + +public: + ChatsManagerTest() {}; + + void setUp() { + mocks_ = new MockRepository(); + jid_ = JID("test@test.com/resource"); + stanzaChannel_ = new DummyStanzaChannel(); + iqChannel_ = new DummyIQChannel(); + iqRouter_ = new IQRouter(iqChannel_); + eventController_ = new EventController(); + chatWindowFactory_ = mocks_->InterfaceMock<ChatWindowFactory>(); + treeWidgetFactory_ = NULL; + xmppRoster_ = boost::shared_ptr<XMPPRoster>(new XMPPRoster()); + nickResolver_ = new NickResolver(xmppRoster_); + presenceOracle_ = new PresenceOracle(stanzaChannel_); + serverDiscoInfo_ = boost::shared_ptr<DiscoInfo>(new DiscoInfo()); + presenceSender_ = NULL; + manager_ = new ChatsManager(jid_, stanzaChannel_, iqRouter_, eventController_, chatWindowFactory_, treeWidgetFactory_, nickResolver_, presenceOracle_, serverDiscoInfo_, presenceSender_); + avatarManager_ = new MockAvatarManager(); + manager_->setAvatarManager(avatarManager_); + }; + + void tearDown() { + delete manager_; + delete presenceSender_; + delete avatarManager_; + delete presenceOracle_; + delete nickResolver_; + delete treeWidgetFactory_; + //delete chatWindowFactory_; + delete stanzaChannel_; + delete iqChannel_; + delete iqRouter_; + delete mocks_; + } + + void testFirstOpenWindow() { + JID messageJID("testling@test.com/resource1"); + + ChatWindow* window = new MockChatWindow();//mocks_->InterfaceMock<ChatWindow>(); + mocks_->ExpectCall(chatWindowFactory_, ChatWindowFactory::createChatWindow).With(messageJID).Return(window); + + boost::shared_ptr<Message> message(new Message()); + message->setFrom(messageJID); + message->setBody("This is a legible message."); + manager_->handleIncomingMessage(message); + } + +private: + JID jid_; + ChatsManager* manager_; + StanzaChannel* stanzaChannel_; + IQChannel* iqChannel_; + IQRouter* iqRouter_; + EventController* eventController_; + ChatWindowFactory* chatWindowFactory_; + TreeWidgetFactory* treeWidgetFactory_; + NickResolver* nickResolver_; + PresenceOracle* presenceOracle_; + AvatarManager* avatarManager_; + boost::shared_ptr<DiscoInfo> serverDiscoInfo_; + boost::shared_ptr<XMPPRoster> xmppRoster_; + PresenceSender* presenceSender_; + MockRepository* mocks_; +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(ChatsManagerTest); + diff --git a/Swift/Controllers/UnitTest/MockChatWindow.cpp b/Swift/Controllers/UnitTest/MockChatWindow.cpp new file mode 100644 index 0000000..406bb55 --- /dev/null +++ b/Swift/Controllers/UnitTest/MockChatWindow.cpp @@ -0,0 +1,7 @@ +#include "Swift/Controllers/UnitTest/MockChatWindow.h" + +namespace Swift { +MockChatWindow::~MockChatWindow() { + +} +} diff --git a/Swift/Controllers/UnitTest/MockChatWindow.h b/Swift/Controllers/UnitTest/MockChatWindow.h new file mode 100644 index 0000000..015bb9b --- /dev/null +++ b/Swift/Controllers/UnitTest/MockChatWindow.h @@ -0,0 +1,36 @@ +#pragma once + +#include "Swift/Controllers/ChatWindow.h" + +namespace Swift { + class MockChatWindow : public ChatWindow { + public: + MockChatWindow() {}; + virtual ~MockChatWindow(); + + virtual void addMessage(const String& /*message*/, const String& /*senderName*/, bool /*senderIsSelf*/, const boost::optional<SecurityLabel>& /*label*/, const String& /*avatarPath*/) {}; + virtual void addSystemMessage(const String& /*message*/) {}; + virtual void addErrorMessage(const String& /*message*/) {}; + + virtual void setName(const String& name) {name_ = name;}; + virtual void show() {}; + virtual void activate() {}; + virtual void setAvailableSecurityLabels(const std::vector<SecurityLabel>& labels) {labels_ = labels;}; + virtual void setSecurityLabelsEnabled(bool enabled) {labelsEnabled_ = enabled;}; + virtual void setUnreadMessageCount(int /*count*/) {}; + virtual void convertToMUC() {}; + virtual TreeWidget *getTreeWidget() {return NULL;}; + virtual void setSecurityLabelsError() {}; + virtual SecurityLabel getSelectedSecurityLabel() {return SecurityLabel();}; + virtual void setInputEnabled(bool /*enabled*/) {}; + + boost::signal<void ()> onClosed; + boost::signal<void ()> onAllMessagesRead; + boost::signal<void (const String&)> onSendMessageRequest; + + String name_; + std::vector<SecurityLabel> labels_; + bool labelsEnabled_; + }; +} + |