summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPeter Burgess <pete.burgess@isode.com>2018-04-20 15:18:20 (GMT)
committerPeter Burgess <pete.burgess@isode.com>2018-07-24 15:05:11 (GMT)
commitd3bbd300be4480c0a3b7285c91b3b27e82ca9c2f (patch)
treea86f47b56647363141523a7182cd84cd2551ef62 /Swift/Controllers
parent5b4a0ba19ae6994db6a193a231fd2486bff6f0ec (diff)
downloadswift-d3bbd300be4480c0a3b7285c91b3b27e82ca9c2f.zip
swift-d3bbd300be4480c0a3b7285c91b3b27e82ca9c2f.tar.bz2
Add a XEP-346 FDP Form Submission Dialog
A new form submission dialog that will discover nodes containing fdp form templates in a given pubsub domain, and will present the form to the user ready to be filled out and submitted. Test-Infomation: Personally tested the request for all node items, loading the latest form from one of the discovered nodes, submitting a form and checking it turns up on my mlink server using mlc, checked that the submitted infomation is correct, what happens when an incorrect domain is queried, what happens when a valid domain is queried but it has no fdp nodes, and what happens when a form is requested from an invalid node. Unit tests written to test success and failure of the three controller functions requestPubSubNodeData(), requestTemplateForm() and testSubmitForm(). Change-Id: If8c57bb4e3120dd44d52f7332069eb18a14cb385
Diffstat (limited to 'Swift/Controllers')
-rw-r--r--Swift/Controllers/AccountController.cpp5
-rw-r--r--Swift/Controllers/AccountController.h2
-rw-r--r--Swift/Controllers/FdpFormSubmitController.cpp145
-rw-r--r--Swift/Controllers/FdpFormSubmitController.h47
-rw-r--r--Swift/Controllers/SConscript3
-rw-r--r--Swift/Controllers/UIEvents/FdpFormSubmitWindowOpenUIEvent.h17
-rw-r--r--Swift/Controllers/UIInterfaces/FdpFormSubmitWindow.cpp32
-rw-r--r--Swift/Controllers/UIInterfaces/FdpFormSubmitWindow.h54
-rw-r--r--Swift/Controllers/UIInterfaces/FdpFormSubmitWindowFactory.h19
-rw-r--r--Swift/Controllers/UIInterfaces/MainWindow.h4
-rw-r--r--Swift/Controllers/UIInterfaces/UIFactory.h6
-rw-r--r--Swift/Controllers/UnitTest/FdpFormSubmitControllerTest.cpp269
-rw-r--r--Swift/Controllers/UnitTest/MockFdpFormSubmitWindow.h37
-rw-r--r--Swift/Controllers/UnitTest/MockFdpFormSubmitWindowFactory.h31
-rw-r--r--Swift/Controllers/UnitTest/MockMainWindow.h3
15 files changed, 668 insertions, 6 deletions
diff --git a/Swift/Controllers/AccountController.cpp b/Swift/Controllers/AccountController.cpp
index 048d140..fe7c200 100644
--- a/Swift/Controllers/AccountController.cpp
+++ b/Swift/Controllers/AccountController.cpp
@@ -56,6 +56,7 @@
#include <Swift/Controllers/ContactsFromXMPPRoster.h>
#include <Swift/Controllers/EventNotifier.h>
#include <Swift/Controllers/EventWindowController.h>
+#include <Swift/Controllers/FdpFormSubmitController.h>
#include <Swift/Controllers/FileTransfer/FileTransferOverview.h>
#include <Swift/Controllers/FileTransferListController.h>
#include <Swift/Controllers/Highlighting/HighlightEditorController.h>
@@ -125,7 +126,8 @@ AccountController::AccountController(
loginWindow_(nullptr) ,
useDelayForLatency_(useDelayForLatency),
ftOverview_(nullptr),
- emoticons_(emoticons) {
+ emoticons_(emoticons),
+ fdpFormSubmitController_(nullptr) {
storages_ = nullptr;
certificateStorage_ = nullptr;
certificateTrustChecker_ = nullptr;
@@ -580,6 +582,7 @@ void AccountController::performLoginFromCachedCredentials() {
presence->setShow(static_cast<StatusShow::Type>(profileSettings_->getIntSetting("lastShow", StatusShow::Online)));
presence->setStatus(profileSettings_->getStringSetting("lastStatus"));
statusTracker_->setRequestedPresence(presence);
+ fdpFormSubmitController_ = std::make_unique<FdpFormSubmitController>(jid_, client_->getIQRouter(), uiEventStream_, uiFactory_);
} else {
/* In case we're in the middle of another login, make sure they don't overlap */
client_->disconnect();
diff --git a/Swift/Controllers/AccountController.h b/Swift/Controllers/AccountController.h
index 30ae265..774aa8b 100644
--- a/Swift/Controllers/AccountController.h
+++ b/Swift/Controllers/AccountController.h
@@ -81,6 +81,7 @@ namespace Swift {
class BlockListController;
class ContactSuggester;
class ContactsFromXMPPRoster;
+ class FdpFormSubmitController;
class AccountController {
public:
@@ -197,5 +198,6 @@ namespace Swift {
HighlightEditorController* highlightEditorController_;
std::map<std::string, std::string> emoticons_;
boost::signals2::connection enableCarbonsRequestHandlerConnection_;
+ std::unique_ptr<FdpFormSubmitController> fdpFormSubmitController_;
};
}
diff --git a/Swift/Controllers/FdpFormSubmitController.cpp b/Swift/Controllers/FdpFormSubmitController.cpp
new file mode 100644
index 0000000..639b4e9
--- /dev/null
+++ b/Swift/Controllers/FdpFormSubmitController.cpp
@@ -0,0 +1,145 @@
+/*
+ * Copyright (c) 2018 Isode Limited.
+ * All rights reserved.
+ * See the COPYING file for more information.
+ */
+
+#include <Swift/Controllers/FdpFormSubmitController.h>
+
+#include <Swiften/Disco/GetDiscoItemsRequest.h>
+#include <Swiften/Elements/DiscoItems.h>
+#include <Swiften/Queries/IQRouter.h>
+#include <Swiften/Queries/PubSubRequest.h>
+
+#include <Swift/Controllers/UIEvents/FdpFormSubmitWindowOpenUIEvent.h>
+#include <Swift/Controllers/UIEvents/UIEventStream.h>
+#include <Swift/Controllers/UIInterfaces/FdpFormSubmitWindow.h>
+#include <Swift/Controllers/UIInterfaces/FdpFormSubmitWindowFactory.h>
+
+namespace Swift {
+
+FdpFormSubmitController::FdpFormSubmitController(const JID& self, IQRouter* iqRouter, UIEventStream* uiEventStream, FdpFormSubmitWindowFactory* factory) : selfJID_(self), iqRouter_(iqRouter), uiEventStream_(uiEventStream), factory_(factory), formSubmitWindow_(nullptr) {
+ fdpFormSubmitWindowOpenUIEventConnection_= uiEventStream_->onUIEvent.connect( [this](const std::shared_ptr<UIEvent>& uiEvent){ handleUIEvent(uiEvent); });
+}
+
+FdpFormSubmitController::~FdpFormSubmitController() {
+}
+
+void FdpFormSubmitController::handleUIEvent(const std::shared_ptr<UIEvent>& uiEvent) {
+ if (auto openEvent = std::dynamic_pointer_cast<FdpFormSubmitWindowOpenUIEvent>(uiEvent)) {
+ if (formSubmitWindow_) {
+ formSubmitWindow_->raise();
+ }
+ else {
+ createFormSubmitWindow();
+ }
+ }
+}
+
+void FdpFormSubmitController::createFormSubmitWindow() {
+ formSubmitWindow_ = factory_->createFdpFormSubmitWindow();
+ formSubmitWindow_->onCloseEvent.connect([this](){ closeFormSubmitWindow(); });
+ formSubmitWindow_->onRequestPubSubNodeData.connect([this](const std::string& string){ requestPubSubNodeData(string); });
+ formSubmitWindow_->onRequestTemplateForm.connect([this](const std::string& fdpTemplateNodeName){ requestTemplateForm(fdpTemplateNodeName); });
+ formSubmitWindow_->onSubmitForm.connect([this](const std::shared_ptr<Form>& form){ submitForm(form); });
+ formSubmitWindow_->show();
+}
+
+void FdpFormSubmitController::closeFormSubmitWindow() {
+ formSubmitWindow_.reset();
+}
+
+void FdpFormSubmitController::requestPubSubNodeData(const std::string& domainName) {
+ JID domainJID(domainName);
+ auto discoItemsRequest = GetDiscoItemsRequest::create(domainJID, iqRouter_);
+ discoItemsRequest->onResponse.connect(
+ [this, domainName](std::shared_ptr<DiscoItems> discoItems, ErrorPayload::ref errorPayloadRef) {
+ if (!discoItems || errorPayloadRef) {
+ formSubmitWindow_->showNodePlaceholder(FdpFormSubmitWindow::NodeError::DomainNotFound);
+ return;
+ }
+ currentDomain_ = domainName;
+
+ bool templateNodeAdded = false;
+ formSubmitWindow_->clearNodeData();
+ auto discoItemList = discoItems->getItems();
+ for (auto discoItem : discoItemList) {
+ auto node = discoItem.getNode();
+ if (node.substr(0, 13) != "fdp/template/") {
+ continue;
+ }
+
+ std::string nodeName = discoItem.getName().empty() ? node : discoItem.getName();
+
+ formSubmitWindow_->addNode(node, nodeName);
+ templateNodeAdded = true;
+ }
+ if (!templateNodeAdded) {
+ formSubmitWindow_->showNodePlaceholder(FdpFormSubmitWindow::NodeError::NoFdpNodesInDomain);
+ }
+ }
+ );
+ discoItemsRequest->send();
+}
+
+void FdpFormSubmitController::requestTemplateForm(const std::string& nodeName) {
+ auto pubSubItems = std::make_shared<PubSubItems>(nodeName);
+ pubSubItems->setMaximumItems(1);
+ auto formRequest = std::make_shared<PubSubRequest<PubSubItems>>(IQ::Type::Get, selfJID_, currentDomain_, pubSubItems, iqRouter_);
+
+ formRequest->onResponse.connect(
+ [this, nodeName](std::shared_ptr<PubSubItems> response, ErrorPayload::ref errorPayload) {
+ if (!response || errorPayload) {
+ formSubmitWindow_->showFormPlaceholder(FdpFormSubmitWindow::TemplateError::RequestFailed);
+ return;
+ }
+ auto pubSubItemList = response->getItems();
+ if (pubSubItemList.empty()) {
+ formSubmitWindow_->showFormPlaceholder(FdpFormSubmitWindow::TemplateError::CannotLocateForm);
+ return;
+ }
+ auto payloadList = pubSubItemList[0]->getData();
+ if (payloadList.empty()) {
+ formSubmitWindow_->showFormPlaceholder(FdpFormSubmitWindow::TemplateError::CannotLocateForm);
+ return;
+ }
+ if (auto form = std::dynamic_pointer_cast<Form>(payloadList[0])) {
+ currentTemplateNode_ = nodeName;
+ formSubmitWindow_->setFormData(form);
+ }
+ else {
+ formSubmitWindow_->showFormPlaceholder(FdpFormSubmitWindow::TemplateError::InvalidPayload);
+ return;
+ }
+ }
+ );
+
+ formRequest->send();
+}
+
+void FdpFormSubmitController::submitForm(const std::shared_ptr<Form>& form) {
+ std::string submittedNode = currentTemplateNode_;
+ submittedNode.replace(submittedNode.find("/template/", 0), 10, "/submitted/");
+ auto pubSubItem = std::make_shared<PubSubItem>();
+ auto pubSubItems = std::make_shared<PubSubItems>(submittedNode);
+ auto pubSubPublish = std::make_shared<PubSubPublish>();
+ pubSubPublish->setNode(submittedNode);
+ pubSubPublish->addItem(pubSubItem);
+ pubSubItem->addData(form);
+ pubSubItems->addItem(pubSubItem);
+ auto formRequest = std::make_shared<PubSubRequest<PubSubPublish>>(IQ::Type::Set, selfJID_, currentDomain_, pubSubPublish, iqRouter_);
+
+ formRequest->onResponse.connect(
+ [this](std::shared_ptr<PubSubPublish> response, ErrorPayload::ref errorPayload) {
+ if (!response || errorPayload) {
+ formSubmitWindow_->handleSubmitServerResponse(false);
+ return;
+ }
+ formSubmitWindow_->handleSubmitServerResponse(true);
+ }
+ );
+
+ formRequest->send();
+}
+
+}
diff --git a/Swift/Controllers/FdpFormSubmitController.h b/Swift/Controllers/FdpFormSubmitController.h
new file mode 100644
index 0000000..69db08c
--- /dev/null
+++ b/Swift/Controllers/FdpFormSubmitController.h
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2018 Isode Limited.
+ * All rights reserved.
+ * See the COPYING file for more information.
+ */
+
+#pragma once
+
+#include <memory>
+#include <string>
+
+#include <boost/signals2.hpp>
+
+#include <Swiften/JID/JID.h>
+
+namespace Swift {
+ class FdpFormSubmitWindow;
+ class FdpFormSubmitWindowFactory;
+ class Form;
+ class IQRouter;
+ class UIEvent;
+ class UIEventStream;
+
+ class FdpFormSubmitController {
+ public:
+ FdpFormSubmitController(const JID& self, IQRouter* iqRouter, UIEventStream* uiEventStream, FdpFormSubmitWindowFactory* factory);
+ ~FdpFormSubmitController();
+
+ private:
+ void handleUIEvent(const std::shared_ptr<UIEvent>& uiEvent);
+ void createFormSubmitWindow();
+ void closeFormSubmitWindow();
+ void requestPubSubNodeData(const std::string& domainName);
+ void requestTemplateForm(const std::string& nodeName);
+ void submitForm(const std::shared_ptr<Form>& form);
+
+ JID selfJID_;
+ IQRouter* iqRouter_;
+ UIEventStream* uiEventStream_;
+ FdpFormSubmitWindowFactory* factory_;
+ std::unique_ptr<FdpFormSubmitWindow> formSubmitWindow_;
+ std::string currentDomain_;
+ std::string currentTemplateNode_;
+
+ boost::signals2::scoped_connection fdpFormSubmitWindowOpenUIEventConnection_;
+ };
+}
diff --git a/Swift/Controllers/SConscript b/Swift/Controllers/SConscript
index cbd3bf3..25d326a 100644
--- a/Swift/Controllers/SConscript
+++ b/Swift/Controllers/SConscript
@@ -41,6 +41,7 @@ if env["SCONS_STAGE"] == "build" :
"ContactsFromXMPPRoster.cpp",
"EventNotifier.cpp",
"EventWindowController.cpp",
+ "FdpFormSubmitController.cpp",
"FileTransfer/FileTransferController.cpp",
"FileTransfer/FileTransferOverview.cpp",
"FileTransfer/FileTransferProgressInfo.cpp",
@@ -85,6 +86,7 @@ if env["SCONS_STAGE"] == "build" :
"Translator.cpp",
"UIEvents/UIEvent.cpp",
"UIInterfaces/ChatListWindow.cpp",
+ "UIInterfaces/FdpFormSubmitWindow.cpp",
"UIInterfaces/HighlightEditorWindow.cpp",
"UIInterfaces/XMLConsoleWidget.cpp",
"WhiteboardManager.cpp",
@@ -106,6 +108,7 @@ if env["SCONS_STAGE"] == "build" :
File("Settings/UnitTest/SettingsProviderHierachyTest.cpp"),
File("UnitTest/ChatMessageSummarizerTest.cpp"),
File("UnitTest/ContactSuggesterTest.cpp"),
+ File("UnitTest/FdpFormSubmitControllerTest.cpp"),
File("UnitTest/MockChatWindow.cpp"),
File("UnitTest/PresenceNotifierTest.cpp"),
File("UnitTest/PreviousStatusStoreTest.cpp"),
diff --git a/Swift/Controllers/UIEvents/FdpFormSubmitWindowOpenUIEvent.h b/Swift/Controllers/UIEvents/FdpFormSubmitWindowOpenUIEvent.h
new file mode 100644
index 0000000..d540cb2
--- /dev/null
+++ b/Swift/Controllers/UIEvents/FdpFormSubmitWindowOpenUIEvent.h
@@ -0,0 +1,17 @@
+/*
+ * Copyright (c) 2018 Isode Limited.
+ * All rights reserved.
+ * See the COPYING file for more information.
+ */
+
+#pragma once
+
+#include <Swift/Controllers/UIEvents/UIEvent.h>
+
+namespace Swift {
+ class FdpFormSubmitWindowOpenUIEvent : public UIEvent {
+ public:
+ FdpFormSubmitWindowOpenUIEvent() {
+ }
+ };
+}
diff --git a/Swift/Controllers/UIInterfaces/FdpFormSubmitWindow.cpp b/Swift/Controllers/UIInterfaces/FdpFormSubmitWindow.cpp
new file mode 100644
index 0000000..47ef9cd
--- /dev/null
+++ b/Swift/Controllers/UIInterfaces/FdpFormSubmitWindow.cpp
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2018 Isode Limited.
+ * All rights reserved.
+ * See the COPYING file for more information.
+ */
+
+#include <Swift/Controllers/UIInterfaces/FdpFormSubmitWindow.h>
+
+#include <Swift/Controllers/Intl.h>
+
+namespace Swift {
+
+std::string FdpFormSubmitWindow::getNodeErrorText(NodeError nodeError) const {
+ switch(nodeError) {
+ case NodeError::DomainNotFound: return QT_TRANSLATE_NOOP("", "Error: No pubsub domain found");
+ case NodeError::NoFdpNodesInDomain: return QT_TRANSLATE_NOOP("", "Error: Domain does not contain an FDP template node");
+ case NodeError::NoError: return "";
+ }
+ return "";
+}
+
+std::string FdpFormSubmitWindow::getTemplateErrorText(TemplateError templateError) const {
+ switch(templateError) {
+ case TemplateError::CannotLocateForm: return QT_TRANSLATE_NOOP("", "Error: Could not locate template form");
+ case TemplateError::InvalidPayload: return QT_TRANSLATE_NOOP("", "Error: Invalid payload returned from node");
+ case TemplateError::RequestFailed: return QT_TRANSLATE_NOOP("", "Error: Pubsub request failed");
+ case TemplateError::NoError: return "";
+ }
+ return "";
+}
+
+}
diff --git a/Swift/Controllers/UIInterfaces/FdpFormSubmitWindow.h b/Swift/Controllers/UIInterfaces/FdpFormSubmitWindow.h
new file mode 100644
index 0000000..572f910
--- /dev/null
+++ b/Swift/Controllers/UIInterfaces/FdpFormSubmitWindow.h
@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2018 Isode Limited.
+ * All rights reserved.
+ * See the COPYING file for more information.
+ */
+
+#pragma once
+
+#include <memory>
+#include <string>
+
+#include <boost/signals2.hpp>
+
+namespace Swift {
+
+ class Form;
+
+ class FdpFormSubmitWindow {
+
+ public:
+ enum class NodeError {
+ DomainNotFound,
+ NoFdpNodesInDomain,
+ NoError,
+ };
+ enum class TemplateError {
+ CannotLocateForm,
+ InvalidPayload,
+ RequestFailed,
+ NoError,
+ };
+
+ virtual ~FdpFormSubmitWindow() {}
+
+ virtual void show() = 0;
+ virtual void raise() = 0;
+ virtual void addNode(const std::string& node, const std::string& nodeName) = 0;
+ virtual void clearNodeData() = 0;
+ virtual void setFormData(const std::shared_ptr<Form>& form) = 0;
+ virtual void showNodePlaceholder(NodeError nodeError) = 0;
+ virtual void showFormPlaceholder(TemplateError templateError) = 0;
+ virtual void handleSubmitServerResponse(bool submissionSuccess) = 0;
+
+ boost::signals2::signal<void ()> onCloseEvent;
+ boost::signals2::signal<void (const std::string&)> onRequestPubSubNodeData;
+ boost::signals2::signal<void (const std::string&)> onRequestTemplateForm;
+ boost::signals2::signal<void (const std::shared_ptr<Form>&)> onSubmitForm;
+
+ protected:
+
+ std::string getNodeErrorText(NodeError nodeError) const;
+ std::string getTemplateErrorText(TemplateError templateError) const;
+ };
+}
diff --git a/Swift/Controllers/UIInterfaces/FdpFormSubmitWindowFactory.h b/Swift/Controllers/UIInterfaces/FdpFormSubmitWindowFactory.h
new file mode 100644
index 0000000..ef11eaf
--- /dev/null
+++ b/Swift/Controllers/UIInterfaces/FdpFormSubmitWindowFactory.h
@@ -0,0 +1,19 @@
+/*
+ * Copyright (c) 2018 Isode Limited.
+ * All rights reserved.
+ * See the COPYING file for more information.
+ */
+
+#pragma once
+
+#include <memory>
+
+namespace Swift {
+ class FdpFormSubmitWindow;
+
+ class FdpFormSubmitWindowFactory {
+ public:
+ virtual ~FdpFormSubmitWindowFactory() {}
+ virtual std::unique_ptr<FdpFormSubmitWindow> createFdpFormSubmitWindow() = 0;
+ };
+}
diff --git a/Swift/Controllers/UIInterfaces/MainWindow.h b/Swift/Controllers/UIInterfaces/MainWindow.h
index bfd8c67..e4b4657 100644
--- a/Swift/Controllers/UIInterfaces/MainWindow.h
+++ b/Swift/Controllers/UIInterfaces/MainWindow.h
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2010-2016 Isode Limited.
+ * Copyright (c) 2010-2018 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
@@ -13,12 +13,12 @@
#include <Swiften/Elements/DiscoItems.h>
#include <Swiften/Elements/StatusShow.h>
-#include <Swiften/JID/JID.h>
#include <Swiften/TLS/Certificate.h>
#include <Swift/Controllers/Roster/ContactRosterItem.h>
namespace Swift {
+ class JID;
class Roster;
class MainWindow {
diff --git a/Swift/Controllers/UIInterfaces/UIFactory.h b/Swift/Controllers/UIInterfaces/UIFactory.h
index a0976dc..c49364a 100644
--- a/Swift/Controllers/UIInterfaces/UIFactory.h
+++ b/Swift/Controllers/UIInterfaces/UIFactory.h
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2010-2016 Isode Limited.
+ * Copyright (c) 2010-2018 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
@@ -12,6 +12,7 @@
#include <Swift/Controllers/UIInterfaces/ChatWindowFactory.h>
#include <Swift/Controllers/UIInterfaces/ContactEditWindowFactory.h>
#include <Swift/Controllers/UIInterfaces/EventWindowFactory.h>
+#include <Swift/Controllers/UIInterfaces/FdpFormSubmitWindowFactory.h>
#include <Swift/Controllers/UIInterfaces/FileTransferListWidgetFactory.h>
#include <Swift/Controllers/UIInterfaces/HighlightEditorWindowFactory.h>
#include <Swift/Controllers/UIInterfaces/HistoryWindowFactory.h>
@@ -42,7 +43,8 @@ namespace Swift {
public FileTransferListWidgetFactory,
public WhiteboardWindowFactory,
public HighlightEditorWindowFactory,
- public BlockListEditorWidgetFactory {
+ public BlockListEditorWidgetFactory,
+ public FdpFormSubmitWindowFactory {
public:
virtual ~UIFactory() {}
};
diff --git a/Swift/Controllers/UnitTest/FdpFormSubmitControllerTest.cpp b/Swift/Controllers/UnitTest/FdpFormSubmitControllerTest.cpp
new file mode 100644
index 0000000..8dc3442
--- /dev/null
+++ b/Swift/Controllers/UnitTest/FdpFormSubmitControllerTest.cpp
@@ -0,0 +1,269 @@
+/*
+ * Copyright (c) 2018 Isode Limited.
+ * All rights reserved.
+ * See the COPYING file for more information.
+ */
+
+#include <memory>
+#include <string>
+
+#include <gtest/gtest.h>
+
+#include <Swiften/Client/DummyStanzaChannel.h>
+#include <Swiften/Elements/DiscoItems.h>
+#include <Swiften/Elements/Form.h>
+#include <Swiften/Elements/FormField.h>
+#include <Swiften/Elements/IQ.h>
+#include <Swiften/Elements/Payload.h>
+#include <Swiften/Elements/PubSub.h>
+#include <Swiften/Elements/PubSubItems.h>
+#include <Swiften/Elements/PubSubPublish.h>
+#include <Swiften/JID/JID.h>
+#include <Swiften/Queries/IQRouter.h>
+
+#include <Swift/Controllers/FdpFormSubmitController.h>
+#include <Swift/Controllers/UIEvents/FdpFormSubmitWindowOpenUIEvent.h>
+#include <Swift/Controllers/UIEvents/UIEventStream.h>
+#include <Swift/Controllers/UnitTest/MockFdpFormSubmitWindowFactory.h>
+#include <Swift/Controllers/UnitTest/MockFdpFormSubmitWindow.h>
+
+namespace Swift {
+
+class FdpFormSubmitControllerTest : public ::testing::Test {
+
+ protected:
+ void SetUp() {
+ clientJid_ = JID(clientJIDString_);
+ stanzaChannel_ = std::make_unique<DummyStanzaChannel>();
+ iqRouter_ = std::make_unique<IQRouter>(stanzaChannel_.get());
+ uiEventStream_ = std::make_unique<UIEventStream>();
+ fdpWindowFactory_ = std::make_unique<MockFdpFormSubmitWindowFactory>();
+ fdpController_ = std::make_unique<FdpFormSubmitController>(clientJid_, iqRouter_.get(), uiEventStream_.get(), fdpWindowFactory_.get());
+ auto fdpWindowOpenUIEvent = std::make_shared<FdpFormSubmitWindowOpenUIEvent>();
+ uiEventStream_->send(fdpWindowOpenUIEvent);
+ fdpWindow_ = fdpWindowFactory_->getMockFdpFormSubmitWindow();
+ }
+
+ void TearDown() {
+ }
+
+ std::shared_ptr<DiscoItems> createDiscoItemsResult();
+ std::shared_ptr<PubSub> createTemplatePubSubResult(const std::shared_ptr<Form>& form);
+ std::shared_ptr<PubSub> createSubmittedPubSubResult();
+
+ std::string clientJIDString_ = "testjid@test.im/swift";
+ JID clientJid_;
+ std::unique_ptr<DummyStanzaChannel> stanzaChannel_;
+ std::unique_ptr<IQRouter> iqRouter_;
+ std::unique_ptr<UIEventStream> uiEventStream_;
+ std::unique_ptr<MockFdpFormSubmitWindowFactory> fdpWindowFactory_;
+ std::unique_ptr<FdpFormSubmitController> fdpController_;
+ MockFdpFormSubmitWindow* fdpWindow_;
+};
+
+std::shared_ptr<DiscoItems> FdpFormSubmitControllerTest::createDiscoItemsResult() {
+ auto discoItems = std::make_shared<DiscoItems>();
+ discoItems->addItem(DiscoItems::Item("node0", JID(), "fdp/template/testNode0"));
+ discoItems->addItem(DiscoItems::Item("node1", JID(), "fdp/template/testNode1"));
+ discoItems->addItem(DiscoItems::Item("node2", JID(), "fdp/template/testNode2"));
+ return discoItems;
+}
+
+std::shared_ptr<PubSub> FdpFormSubmitControllerTest::createTemplatePubSubResult(const std::shared_ptr<Form>& form) {
+ auto pubSubItem = std::make_shared<PubSubItem>();
+ pubSubItem->addData(form);
+ auto pubSubItems = std::make_shared<PubSubItems>();
+ pubSubItems->addItem(pubSubItem);
+ auto pubSub = std::make_shared<PubSub>();
+ pubSub->setPayload(pubSubItems);
+ return pubSub;
+}
+
+std::shared_ptr<PubSub> FdpFormSubmitControllerTest::createSubmittedPubSubResult() {
+ auto pubSubItem = std::make_shared<PubSubItem>();
+ pubSubItem->setID("testID");
+ auto pubSubPublish = std::make_shared<PubSubPublish>();
+ pubSubPublish->addItem(pubSubItem);
+ pubSubPublish->setNode("fdp/submitted/test");
+ auto pubSub = std::make_shared<PubSub>();
+ pubSub->setPayload(pubSubPublish);
+ return pubSub;
+}
+
+TEST_F(FdpFormSubmitControllerTest, testRequestPubSubNodeData) {
+ std::string domainName = "fdp.example.test";
+ fdpWindow_->onRequestPubSubNodeData(domainName);
+ ASSERT_EQ(1, stanzaChannel_->sentStanzas.size());
+ auto requestIq = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[0]);
+ ASSERT_EQ(domainName, requestIq->getTo());
+ auto discoItemsPayloads = requestIq->getPayloads<DiscoItems>();
+ ASSERT_EQ(1, discoItemsPayloads.size());
+
+ auto discoItemsResult = createDiscoItemsResult();
+ auto resultIq = IQ::createResult(clientJid_, domainName, requestIq->getID(), discoItemsResult);
+ stanzaChannel_->onIQReceived(resultIq);
+
+ auto discoItemsList = discoItemsResult->getItems();
+ ASSERT_EQ(discoItemsList.size(), fdpWindow_->nodeData.size());
+ for (unsigned int i = 0; i < fdpWindow_->nodeData.size(); i++) {
+ auto nodeItem = fdpWindow_->nodeData[i];
+ auto discoItem = discoItemsList[i];
+ ASSERT_EQ(discoItem.getNode(), nodeItem.first);
+ ASSERT_EQ(discoItem.getName(), nodeItem.second);
+ }
+ ASSERT_EQ(FdpFormSubmitWindow::NodeError::NoError, fdpWindow_->nodeError_);
+}
+
+TEST_F(FdpFormSubmitControllerTest, testRequestPubSubNodeDataError) {
+ std::string domainName = "fdp.example.test";
+ fdpWindow_->onRequestPubSubNodeData(domainName);
+ ASSERT_EQ(1, stanzaChannel_->sentStanzas.size());
+ auto requestIq = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[0]);
+ ASSERT_EQ(domainName, requestIq->getTo());
+
+ auto resultIq = IQ::createError(clientJid_, domainName, requestIq->getID());
+ stanzaChannel_->onIQReceived(resultIq);
+
+ ASSERT_EQ(true, fdpWindow_->nodeData.empty());
+ ASSERT_EQ(FdpFormSubmitWindow::NodeError::DomainNotFound, fdpWindow_->nodeError_);
+}
+
+TEST_F(FdpFormSubmitControllerTest, testRequestTemplateForm) {
+ std::string domainName = "fdp.example.test";
+ fdpWindow_->onRequestPubSubNodeData(domainName);
+ auto nodeDataRequestIq = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[0]);
+ auto discoItemsResult = createDiscoItemsResult();
+ auto discoItemsResultIq = IQ::createResult(clientJid_, domainName, nodeDataRequestIq->getID(), discoItemsResult);
+ stanzaChannel_->onIQReceived(discoItemsResultIq);
+
+ std::string templateNodeName = "fdp/template/test";
+ fdpWindow_->onRequestTemplateForm(templateNodeName);
+ ASSERT_EQ(2, stanzaChannel_->sentStanzas.size());
+ auto requestIq = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[1]);
+ ASSERT_EQ(domainName, requestIq->getTo());
+ auto pubSubPayloads = requestIq->getPayloads<PubSub>();
+ ASSERT_EQ(1, pubSubPayloads.size());
+
+ std::string value0("value0");
+ std::string value1("value1@example.test");
+ auto field0 = std::make_shared<FormField>(FormField::TextSingleType, value0);
+ auto field1 = std::make_shared<FormField>(FormField::JIDSingleType, value1);
+ auto form = std::make_shared<Form>();
+ form->addField(field0);
+ form->addField(field1);
+ auto pubSubResult = createTemplatePubSubResult(form);
+ auto resultIq = IQ::createResult(clientJid_, domainName, requestIq->getID(), pubSubResult);
+ stanzaChannel_->onIQReceived(resultIq);
+
+ ASSERT_EQ(form, fdpWindow_->templateForm_);
+ auto fields = fdpWindow_->templateForm_->getFields();
+ ASSERT_EQ(2, fields.size());
+ ASSERT_EQ(field0, fields[0]);
+ ASSERT_EQ(field1, fields[1]);
+ ASSERT_EQ(value0, fields[0]->getTextSingleValue());
+ ASSERT_EQ(value1, fields[1]->getJIDSingleValue());
+ ASSERT_EQ(FdpFormSubmitWindow::TemplateError::NoError, fdpWindow_->templateError_);
+}
+
+TEST_F(FdpFormSubmitControllerTest, testRequestTemplateFormError) {
+ std::string domainName = "fdp.example.test";
+ fdpWindow_->onRequestPubSubNodeData(domainName);
+ auto nodeDataRequestIq = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[0]);
+ auto discoItemsResult = createDiscoItemsResult();
+ auto discoItemsResultIq = IQ::createResult(clientJid_, domainName, nodeDataRequestIq->getID(), discoItemsResult);
+ stanzaChannel_->onIQReceived(discoItemsResultIq);
+
+ std::string templateNodeName = "fdp/template/test";
+ fdpWindow_->onRequestTemplateForm(templateNodeName);
+ ASSERT_EQ(2, stanzaChannel_->sentStanzas.size());
+ auto requestIq = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[1]);
+ ASSERT_EQ(domainName, requestIq->getTo());
+
+ auto resultIq = IQ::createError(clientJid_, domainName, requestIq->getID());
+ stanzaChannel_->onIQReceived(resultIq);
+
+ ASSERT_EQ(nullptr, fdpWindow_->templateForm_);
+ ASSERT_EQ(FdpFormSubmitWindow::TemplateError::RequestFailed, fdpWindow_->templateError_);
+}
+
+TEST_F(FdpFormSubmitControllerTest, testSubmitForm) {
+ std::string domainName = "fdp.example.test";
+ std::string templateNodeName = "fdp/template/test";
+ fdpWindow_->onRequestPubSubNodeData(domainName);
+ auto nodeDataRequestIq = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[0]);
+ auto discoItemsResult = createDiscoItemsResult();
+ auto discoItemsResultIq = IQ::createResult(clientJid_, domainName, nodeDataRequestIq->getID(), discoItemsResult);
+ stanzaChannel_->onIQReceived(discoItemsResultIq);
+ fdpWindow_->onRequestTemplateForm(templateNodeName);
+ auto templateFormRequestIq = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[1]);
+ auto templateForm = std::make_shared<Form>();
+ auto templatePubSubResult = createTemplatePubSubResult(templateForm);
+ auto templatePubSubResultIq = IQ::createResult(clientJid_, domainName, templateFormRequestIq->getID(), templatePubSubResult);
+ stanzaChannel_->onIQReceived(templatePubSubResultIq);
+
+ std::string value0("value0");
+ std::string value1("value1@example.test");
+ auto field0 = std::make_shared<FormField>(FormField::TextSingleType, value0);
+ auto field1 = std::make_shared<FormField>(FormField::JIDSingleType, value1);
+ auto form = std::make_shared<Form>();
+ form->addField(field0);
+ form->addField(field1);
+ fdpWindow_->onSubmitForm(form);
+
+ ASSERT_EQ(3, stanzaChannel_->sentStanzas.size());
+ auto requestIq = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[2]);
+ ASSERT_EQ(domainName, requestIq->getTo());
+ auto pubSubPayloads = requestIq->getPayloads<PubSub>();
+ ASSERT_EQ(1, pubSubPayloads.size());
+ auto pubSubPayload = pubSubPayloads[0];
+ auto pubSubPublishPayload = std::dynamic_pointer_cast<PubSubPublish>(pubSubPayload->getPayload());
+ ASSERT_TRUE(pubSubPublishPayload);
+ auto pubSubItems = pubSubPublishPayload->getItems();
+ ASSERT_EQ(1, pubSubItems.size());
+ auto dataList = pubSubItems[0]->getData();
+ ASSERT_EQ(1, dataList.size());
+ auto submittedForm = std::dynamic_pointer_cast<Form>(dataList[0]);
+ ASSERT_TRUE(submittedForm);
+ ASSERT_EQ(form, submittedForm);
+ auto fields = submittedForm->getFields();
+ ASSERT_EQ(2, fields.size());
+ ASSERT_EQ(field0, fields[0]);
+ ASSERT_EQ(field1, fields[1]);
+ ASSERT_EQ(value0, fields[0]->getTextSingleValue());
+ ASSERT_EQ(value1, fields[1]->getJIDSingleValue());
+
+ auto pubSubResult = createSubmittedPubSubResult();
+ auto resultIq = IQ::createResult(clientJid_, domainName, requestIq->getID(), pubSubResult);
+ stanzaChannel_->onIQReceived(resultIq);
+
+ ASSERT_EQ(true, fdpWindow_->submissionSuccess_);
+}
+
+TEST_F(FdpFormSubmitControllerTest, testSubmitFormError) {
+ std::string domainName = "fdp.example.test";
+ std::string templateNodeName = "fdp/template/test";
+ fdpWindow_->onRequestPubSubNodeData(domainName);
+ auto nodeDataRequestIq = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[0]);
+ auto discoItemsResult = createDiscoItemsResult();
+ auto discoItemsResultIq = IQ::createResult(clientJid_, domainName, nodeDataRequestIq->getID(), discoItemsResult);
+ stanzaChannel_->onIQReceived(discoItemsResultIq);
+ fdpWindow_->onRequestTemplateForm(templateNodeName);
+ auto templateFormRequestIq = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[1]);
+ auto templateForm = std::make_shared<Form>();
+ auto templatePubSubResult = createTemplatePubSubResult(templateForm);
+ auto templatePubSubResultIq = IQ::createResult(clientJid_, domainName, templateFormRequestIq->getID(), templatePubSubResult);
+ stanzaChannel_->onIQReceived(templatePubSubResultIq);
+
+ auto form = std::make_shared<Form>();
+ fdpWindow_->onSubmitForm(form);
+ ASSERT_EQ(3, stanzaChannel_->sentStanzas.size());
+ auto requestIq = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[2]);
+ ASSERT_EQ(domainName, requestIq->getTo());
+
+ auto resultIq = IQ::createError(clientJid_, domainName, requestIq->getID());
+ stanzaChannel_->onIQReceived(resultIq);
+
+ ASSERT_EQ(false, fdpWindow_->submissionSuccess_);
+}
+
+}
diff --git a/Swift/Controllers/UnitTest/MockFdpFormSubmitWindow.h b/Swift/Controllers/UnitTest/MockFdpFormSubmitWindow.h
new file mode 100644
index 0000000..28ef35f
--- /dev/null
+++ b/Swift/Controllers/UnitTest/MockFdpFormSubmitWindow.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2018 Isode Limited.
+ * All rights reserved.
+ * See the COPYING file for more information.
+ */
+
+#pragma once
+
+#include <memory>
+#include <string>
+
+#include <Swift/Controllers/UIInterfaces/FdpFormSubmitWindow.h>
+
+namespace Swift {
+
+ class Form;
+
+ class MockFdpFormSubmitWindow : public FdpFormSubmitWindow {
+ public:
+ MockFdpFormSubmitWindow() : FdpFormSubmitWindow() {}
+ virtual void show() override {}
+ virtual void raise() override {}
+ virtual void addNode(const std::string& node, const std::string& nodeName) override { nodeData.push_back(std::pair<std::string, std::string>(node, nodeName)); }
+ virtual void clearNodeData() override { nodeData.clear(); }
+ virtual void setFormData(const std::shared_ptr<Form>& form) override { templateForm_ = form; }
+ virtual void showNodePlaceholder(NodeError nodeError) override { nodeError_ = nodeError; }
+ virtual void showFormPlaceholder(TemplateError templateError) override { templateError_ = templateError; }
+ virtual void handleSubmitServerResponse(bool submissionSuccess) override { submissionSuccess_ = submissionSuccess; }
+
+ std::vector<std::pair<std::string, std::string>> nodeData;
+ std::shared_ptr<Form> templateForm_ = nullptr;
+ NodeError nodeError_ = NodeError::NoError;
+ TemplateError templateError_ = TemplateError::NoError;
+ bool submissionSuccess_ = false;
+ };
+
+}
diff --git a/Swift/Controllers/UnitTest/MockFdpFormSubmitWindowFactory.h b/Swift/Controllers/UnitTest/MockFdpFormSubmitWindowFactory.h
new file mode 100644
index 0000000..8015419
--- /dev/null
+++ b/Swift/Controllers/UnitTest/MockFdpFormSubmitWindowFactory.h
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2018 Isode Limited.
+ * All rights reserved.
+ * See the COPYING file for more information.
+ */
+
+#pragma once
+
+#include <memory>
+
+#include <Swift/Controllers/UIInterfaces/FdpFormSubmitWindowFactory.h>
+#include <Swift/Controllers/UnitTest/MockFdpFormSubmitWindow.h>
+
+namespace Swift {
+
+ class MockFdpFormSubmitWindowFactory : public FdpFormSubmitWindowFactory {
+ public:
+ MockFdpFormSubmitWindowFactory() : FdpFormSubmitWindowFactory() {}
+
+ virtual std::unique_ptr<FdpFormSubmitWindow> createFdpFormSubmitWindow() override {
+ std::unique_ptr<FdpFormSubmitWindow> fdpFormSubmitWindow = std::make_unique<MockFdpFormSubmitWindow>();
+ mockFdpFormSubmitWindow_ = static_cast<MockFdpFormSubmitWindow*>(fdpFormSubmitWindow.get());
+ return fdpFormSubmitWindow;
+ }
+
+ MockFdpFormSubmitWindow* getMockFdpFormSubmitWindow() { return mockFdpFormSubmitWindow_; }
+
+ private:
+ MockFdpFormSubmitWindow* mockFdpFormSubmitWindow_;
+ };
+}
diff --git a/Swift/Controllers/UnitTest/MockMainWindow.h b/Swift/Controllers/UnitTest/MockMainWindow.h
index 6ae2aa7..9265310 100644
--- a/Swift/Controllers/UnitTest/MockMainWindow.h
+++ b/Swift/Controllers/UnitTest/MockMainWindow.h
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2010-2016 Isode Limited.
+ * Copyright (c) 2010-2018 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
@@ -26,6 +26,7 @@ namespace Swift {
virtual void setStreamEncryptionStatus(bool /*tlsInPlaceAndValid*/) {}
virtual void openCertificateDialog(const std::vector<Certificate::ref>& /*chain*/) {}
virtual void setBlockingCommandAvailable(bool /*isAvailable*/) {}
+ virtual void openFdpFormSubmitDialog(const JID& /*self*/, IQRouter* /*iqRouter*/) {}
Roster* roster;
};