diff options
| author | Linus Jahn <lnj@kaidan.im> | 2022-08-16 21:00:15 +0200 |
|---|---|---|
| committer | Linus Jahn <lnj@kaidan.im> | 2023-01-03 22:05:54 +0100 |
| commit | b17284ee7d674416e0d11f1699f73fcc606262d4 (patch) | |
| tree | 86597f2bc2a1ed2d257e0cbf8e7de1ca54080c08 /tests | |
| parent | 3271c6642439d4d3c0d8c634e2b3f4cf17b908a0 (diff) | |
| download | qxmpp-b17284ee7d674416e0d11f1699f73fcc606262d4.tar.gz | |
Introduce QXmppTask & QXmppPromise
Closes #502.
Co-authored-by: Jonah BrĂ¼chert <jbb@kaidan.im>
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/qxmppclient/tst_qxmppclient.cpp | 20 | ||||
| -rw-r--r-- | tests/qxmppdiscoverymanager/tst_qxmppdiscoverymanager.cpp | 4 | ||||
| -rw-r--r-- | tests/qxmpphttpuploadmanager/tst_qxmpphttpuploadmanager.cpp | 8 | ||||
| -rw-r--r-- | tests/qxmppomemomanager/tst_qxmppomemomanager.cpp | 26 | ||||
| -rw-r--r-- | tests/qxmpppubsubmanager/tst_qxmpppubsubmanager.cpp | 95 | ||||
| -rw-r--r-- | tests/qxmppuserlocationmanager/tst_qxmppuserlocationmanager.cpp | 1 | ||||
| -rw-r--r-- | tests/util.h | 13 |
7 files changed, 94 insertions, 73 deletions
diff --git a/tests/qxmppclient/tst_qxmppclient.cpp b/tests/qxmppclient/tst_qxmppclient.cpp index d25fdda4..8005a580 100644 --- a/tests/qxmppclient/tst_qxmppclient.cpp +++ b/tests/qxmppclient/tst_qxmppclient.cpp @@ -96,23 +96,25 @@ public: bool messageCalled = false; bool iqCalled = false; - QFuture<MessageEncryptResult> encryptMessage(QXmppMessage &&, const std::optional<QXmppSendStanzaParams> &) override + QXmppTask<MessageEncryptResult> encryptMessage(QXmppMessage &&, const std::optional<QXmppSendStanzaParams> &) override { messageCalled = true; - return makeReadyFuture<MessageEncryptResult>(QXmppError { "it's only a test", QXmpp::SendError::EncryptionError }); + return makeReadyTask<MessageEncryptResult>(QXmppError { "it's only a test", QXmpp::SendError::EncryptionError }); + } + QXmppTask<MessageDecryptResult> decryptMessage(QXmppMessage &&) override + { + return makeReadyTask<MessageDecryptResult>(QXmppError { "it's only a test", QXmpp::SendError::EncryptionError }); } - QFuture<MessageDecryptResult> decryptMessage(QXmppMessage &&) override { return {}; }; - - QFuture<IqEncryptResult> encryptIq(QXmppIq &&, const std::optional<QXmppSendStanzaParams> &) override + QXmppTask<IqEncryptResult> encryptIq(QXmppIq &&, const std::optional<QXmppSendStanzaParams> &) override { iqCalled = true; - return makeReadyFuture<IqEncryptResult>(QXmppError { "it's only a test", QXmpp::SendError::EncryptionError }); + return makeReadyTask<IqEncryptResult>(QXmppError { "it's only a test", QXmpp::SendError::EncryptionError }); } - QFuture<IqDecryptResult> decryptIq(const QDomElement &) override + QXmppTask<IqDecryptResult> decryptIq(const QDomElement &) override { - return makeReadyFuture<IqDecryptResult>(QXmppError { "it's only a test", QXmpp::SendError::EncryptionError }); + return makeReadyTask<IqDecryptResult>(QXmppError { "it's only a test", QXmpp::SendError::EncryptionError }); } bool isEncrypted(const QDomElement &) override { return false; }; @@ -129,7 +131,7 @@ void tst_QXmppClient::testE2eeExtension() QVERIFY(encrypter.messageCalled); QVERIFY(!encrypter.iqCalled); QCoreApplication::processEvents(); - expectFutureVariant<QXmppError>(result); + expectFutureVariant<QXmppError>(result.toFuture(this)); encrypter.messageCalled = false; result = client.send(QXmppPresence(QXmppPresence::Available)); diff --git a/tests/qxmppdiscoverymanager/tst_qxmppdiscoverymanager.cpp b/tests/qxmppdiscoverymanager/tst_qxmppdiscoverymanager.cpp index 055236ed..7c26c6f6 100644 --- a/tests/qxmppdiscoverymanager/tst_qxmppdiscoverymanager.cpp +++ b/tests/qxmppdiscoverymanager/tst_qxmppdiscoverymanager.cpp @@ -30,7 +30,7 @@ void tst_QXmppDiscoveryManager::testInfo() </query> </iq>)"); - const auto info = expectFutureVariant<QXmppDiscoveryIq>(future); + const auto info = expectFutureVariant<QXmppDiscoveryIq>(future.toFuture(this)); const QStringList expFeatures = { "http://jabber.org/protocol/pubsub", "urn:xmpp:mix:core:1" }; QCOMPARE(info.features(), expFeatures); @@ -57,7 +57,7 @@ void tst_QXmppDiscoveryManager::testItems() </query> </iq>)"); - const auto items = expectFutureVariant<QList<QXmppDiscoveryIq::Item>>(future); + const auto items = expectFutureVariant<QList<QXmppDiscoveryIq::Item>>(future.toFuture(this)); const QStringList expFeatures = { "http://jabber.org/protocol/pubsub", "urn:xmpp:mix:core:1" }; QCOMPARE(items.size(), 4); diff --git a/tests/qxmpphttpuploadmanager/tst_qxmpphttpuploadmanager.cpp b/tests/qxmpphttpuploadmanager/tst_qxmpphttpuploadmanager.cpp index 86e2983c..f5cade30 100644 --- a/tests/qxmpphttpuploadmanager/tst_qxmpphttpuploadmanager.cpp +++ b/tests/qxmpphttpuploadmanager/tst_qxmpphttpuploadmanager.cpp @@ -375,15 +375,15 @@ void tst_QXmppHttpUploadManager::testUpload() QVERIFY(test.isConnected()); // get server items - auto items = expectVariant<QList<DiscoItem>>(wait(disco->requestDiscoItems(test.configuration().domain()))); + auto items = expectVariant<QList<DiscoItem>>(wait(disco->requestDiscoItems(test.configuration().domain()).toFuture(this))); // request disco info for each item - std::vector<QFuture<DiscoInfoResult>> infoFutures; + std::vector<QXmppTask<DiscoInfoResult>> infoFutures; std::transform(items.cbegin(), items.cend(), std::back_inserter(infoFutures), [disco](const auto &item) { return disco->requestDiscoInfo(item.jid(), item.node()); }); auto uploadServiceJid = [&]() { - for (const auto &future : std::as_const(infoFutures)) { - auto result = expectVariant<QXmppDiscoveryIq>(wait(future)); + for (auto &future : infoFutures) { + auto result = expectVariant<QXmppDiscoveryIq>(wait(future.toFuture(this))); for (const auto &identity : result.identities()) { if (identity.category() == "store" && identity.type() == "file" && diff --git a/tests/qxmppomemomanager/tst_qxmppomemomanager.cpp b/tests/qxmppomemomanager/tst_qxmppomemomanager.cpp index 0f3e636f..85a4792b 100644 --- a/tests/qxmppomemomanager/tst_qxmppomemomanager.cpp +++ b/tests/qxmppomemomanager/tst_qxmppomemomanager.cpp @@ -180,7 +180,7 @@ void tst_QXmppOmemoManager::testSetUp() connect(&m_alice1.client, &QXmppClient::connected, &context, [=, &isManagerSetUp]() { auto future = m_alice1.manager->setUp(); - await(future, this, [=, &isManagerSetUp](bool isSetUp) { + future.then(this, [=, &isManagerSetUp](bool isSetUp) { if (isSetUp) { isManagerSetUp = true; } @@ -266,14 +266,14 @@ void tst_QXmppOmemoManager::testSendMessage() connect(&m_alice1.client, &QXmppClient::connected, &context, [=]() { auto future = m_alice1.manager->setUp(); - await(future, this, [=](bool isSetUp) { + future.then(this, [=](bool isSetUp) { if (isSetUp) { m_alice1.carbonManager->setCarbonsEnabled(true); auto future = m_alice1.manager->setSecurityPolicy(Toakafa); - await(future, this, [=]() { + future.then(this, [=]() { auto future = m_alice2.manager->setSecurityPolicy(Toakafa); - await(future, this, [=]() { + future.then(this, [=]() { m_alice2.client.connectToServer(config2); }); }); @@ -283,7 +283,7 @@ void tst_QXmppOmemoManager::testSendMessage() connect(&m_alice2.client, &QXmppClient::connected, &context, [=]() { auto future = m_alice2.manager->setUp(); - await(future, this, [=](bool isSetUp) { + future.then(this, [=](bool isSetUp) { if (isSetUp) { m_alice2.carbonManager->setCarbonsEnabled(true); } @@ -329,7 +329,7 @@ void tst_QXmppOmemoManager::testSendMessage() isEmptyOmemoMessageReceivedByAlice1 = true; auto future = m_alice1.client.send(std::move(message2), QXmppSendStanzaParams()); - await(future, this, [=, &isSecondMessageSentByAlice1](QXmpp::SendResult result) { + future.then(this, [=, &isSecondMessageSentByAlice1](QXmpp::SendResult result) { if (std::get_if<QXmpp::SendSuccess>(&result)) { isSecondMessageSentByAlice1 = true; } @@ -345,7 +345,7 @@ void tst_QXmppOmemoManager::testSendMessage() if (jid == m_alice2.client.configuration().jidBare()) { if (!isFirstMessageSentByAlice1) { auto future = m_alice1.client.send(std::move(message1), QXmppSendStanzaParams()); - await(future, this, [=, &isFirstMessageSentByAlice1](QXmpp::SendResult result) { + future.then(this, [=, &isFirstMessageSentByAlice1](QXmpp::SendResult result) { if (std::get_if<QXmpp::SendSuccess>(&result)) { isFirstMessageSentByAlice1 = true; } @@ -402,12 +402,12 @@ void tst_QXmppOmemoManager::testSendIq() connect(&m_alice1.client, &QXmppClient::connected, &context, [=]() { auto future = m_alice1.manager->setUp(); - await(future, this, [=](bool isSetUp) { + future.then(this, [=](bool isSetUp) { if (isSetUp) { auto future = m_alice1.manager->setSecurityPolicy(Toakafa); - await(future, this, [=]() { + future.then(this, [=]() { auto future = m_alice2.manager->setSecurityPolicy(Toakafa); - await(future, this, [=]() { + future.then(this, [=]() { m_alice2.client.connectToServer(config2); }); }); @@ -443,7 +443,7 @@ void tst_QXmppOmemoManager::testSendIq() if (!isFirstRequestSent && !isSecondRequestSent) { auto requestIqCopy = requestIq; auto future = m_alice1.client.sendSensitiveIq(std::move(requestIqCopy)); - await(future, this, [=, &isFirstRequestSent, &isErrorResponseReceived, &isSecondRequestSent, &isResultResponseReceived, &iqHandler](QXmppClient::IqResult result) { + future.then(this, [=, &isFirstRequestSent, &isErrorResponseReceived, &isSecondRequestSent, &isResultResponseReceived, &iqHandler](QXmppClient::IqResult result) { if (const auto response = std::get_if<QDomElement>(&result)) { isFirstRequestSent = true; @@ -460,7 +460,7 @@ void tst_QXmppOmemoManager::testSendIq() auto requestIqCopy = requestIq; auto future = m_alice1.client.sendSensitiveIq(std::move(requestIqCopy)); - await(future, this, [=, &isSecondRequestSent, &isResultResponseReceived](QXmppClient::IqResult result) { + future.then(this, [=, &isSecondRequestSent, &isResultResponseReceived](QXmppClient::IqResult result) { if (const auto response = std::get_if<QDomElement>(&result)) { isSecondRequestSent = true; @@ -496,7 +496,7 @@ void tst_QXmppOmemoManager::finish(OmemoUser &omemoUser) bool isManagerReset = false; auto future = omemoUser.manager->resetAll(); - await(future, this, [=, &isManagerReset, &omemoUser](bool isReset) { + future.then(this, [=, &isManagerReset, &omemoUser](bool isReset) { if (isReset) { isManagerReset = true; } diff --git a/tests/qxmpppubsubmanager/tst_qxmpppubsubmanager.cpp b/tests/qxmpppubsubmanager/tst_qxmpppubsubmanager.cpp index f6322779..251d57b5 100644 --- a/tests/qxmpppubsubmanager/tst_qxmpppubsubmanager.cpp +++ b/tests/qxmpppubsubmanager/tst_qxmpppubsubmanager.cpp @@ -299,13 +299,14 @@ void tst_QXmppPubSubManager::testCreateNodes() auto [test, psManager] = Client(); - QFuture<PSManager::Result> future; - if (isPep) { - test.configuration().setJid(jid); - future = psManager->createOwnPepNode(node); - } else { - future = psManager->createNode(jid, node); - } + QXmppTask<PSManager::Result> future = [=, &t = test, psM = psManager]() { + if (isPep) { + t.configuration().setJid(jid); + return psM->createOwnPepNode(node); + } else { + return psM->createNode(jid, node); + } + }(); test.expect(QStringLiteral("<iq id='qxmpp1' to='%1' type='set'><pubsub xmlns='http://jabber.org/protocol/pubsub'><create node='%2'/></pubsub></iq>").arg(jid, node)); test.inject(QStringLiteral("<iq id='qxmpp1' from='%1' type='result'/>").arg(jid)); @@ -400,12 +401,13 @@ void tst_QXmppPubSubManager::testDeleteNodes() test.configuration().setJid(jid); } - QFuture<PSManager::Result> future; - if (isPep) { - future = psManager->deleteOwnPepNode(node); - } else { - future = psManager->deleteNode(jid, node); - } + QXmppTask<PSManager::Result> future = [=, psM = psManager]() { + if (isPep) { + return psM->deleteOwnPepNode(node); + } else { + return psM->deleteNode(jid, node); + } + }(); // FIXME: pubsub#owner here, but not for <create/>? test.expect(QStringLiteral("<iq id='qxmpp1' to='%1' type='set'><pubsub xmlns='http://jabber.org/protocol/pubsub#owner'><delete node='%2'/></pubsub></iq>").arg(jid, node)); @@ -516,20 +518,21 @@ void tst_QXmppPubSubManager::testPublishItems() }; if (items.size() == 1) { - QFuture<PSManager::PublishItemResult> future; - if (isPep) { - if (publishOptions) { - future = psManager->publishOwnPepItem(node, items.constFirst(), *publishOptions); + QXmppTask<PSManager::PublishItemResult> future = [=, psM = psManager]() { + if (isPep) { + if (publishOptions) { + return psManager->publishOwnPepItem(node, items.constFirst(), *publishOptions); + } else { + return psManager->publishOwnPepItem(node, items.constFirst()); + } } else { - future = psManager->publishOwnPepItem(node, items.constFirst()); + if (publishOptions) { + return psManager->publishItem(jid, node, items.constFirst(), *publishOptions); + } else { + return psManager->publishItem(jid, node, items.constFirst()); + } } - } else { - if (publishOptions) { - future = psManager->publishItem(jid, node, items.constFirst(), *publishOptions); - } else { - future = psManager->publishItem(jid, node, items.constFirst()); - } - } + }(); injectXml(); const auto id = expectFutureVariant<QString>(future); @@ -539,20 +542,21 @@ void tst_QXmppPubSubManager::testPublishItems() QVERIFY(id.isNull()); } } else { - QFuture<PSManager::PublishItemsResult> future; - if (isPep) { - if (publishOptions) { - future = psManager->publishOwnPepItems(node, items, *publishOptions); + QXmppTask<PSManager::PublishItemsResult> future = [=, psM = psManager]() { + if (isPep) { + if (publishOptions) { + return psManager->publishOwnPepItems(node, items, *publishOptions); + } else { + return psManager->publishOwnPepItems(node, items); + } } else { - future = psManager->publishOwnPepItems(node, items); + if (publishOptions) { + return psManager->publishItems(jid, node, items, *publishOptions); + } else { + return psManager->publishItems(jid, node, items); + } } - } else { - if (publishOptions) { - future = psManager->publishItems(jid, node, items, *publishOptions); - } else { - future = psManager->publishItems(jid, node, items); - } - } + }(); injectXml(); const auto ids = expectFutureVariant<QVector<QString>>(future); @@ -614,13 +618,14 @@ void tst_QXmppPubSubManager::testRetractItem() auto [test, psManager] = Client(); - QFuture<PSManager::Result> future; - if (isPep) { - test.configuration().setJid(jid); - future = psManager->retractOwnPepItem(node, itemId); - } else { - future = psManager->retractItem(jid, node, itemId); - } + QXmppTask<PSManager::Result> future = [=, psM = psManager, &t = test]() { + if (isPep) { + t.configuration().setJid(jid); + return psM->retractOwnPepItem(node, itemId); + } else { + return psM->retractItem(jid, node, itemId); + } + }(); test.expect(QStringLiteral("<iq id='qxmpp1' to='%1' type='set'><pubsub xmlns='http://jabber.org/protocol/pubsub'><retract node='%2'><item id='%3'/></retract></pubsub></iq>") .arg(jid, node, itemId)); @@ -1024,7 +1029,7 @@ void tst_QXmppPubSubManager::testRequestOptions() TestClient test; auto *psManager = test.addNewExtension<PSManager>(); - auto testOpts = [&](QFuture<PSManager::OptionsResult> &&future) { + auto testOpts = [&](QXmppTask<PSManager::OptionsResult> &&future) { test.expect("<iq id='qxmpp1' to='pubsub.shakespeare.lit' type='get'><pubsub xmlns='http://jabber.org/protocol/pubsub'>" "<options jid='me@qxmpp.org' node='node1'/>" "</pubsub></iq>"); diff --git a/tests/qxmppuserlocationmanager/tst_qxmppuserlocationmanager.cpp b/tests/qxmppuserlocationmanager/tst_qxmppuserlocationmanager.cpp index 3ff30a28..485520f3 100644 --- a/tests/qxmppuserlocationmanager/tst_qxmppuserlocationmanager.cpp +++ b/tests/qxmppuserlocationmanager/tst_qxmppuserlocationmanager.cpp @@ -53,6 +53,7 @@ void tst_QXmppUserLocationManager::testRequest() "</pubsub></iq>"); QCoreApplication::processEvents(); + auto item = expectFutureVariant<QXmppGeolocItem>(future); QCOMPARE(item.id(), QString("abc3")); COMPARE_OPT(item.accuracy(), 20.0); diff --git a/tests/util.h b/tests/util.h index bb2628f7..e0d30aa5 100644 --- a/tests/util.h +++ b/tests/util.h @@ -8,6 +8,7 @@ #define TESTS_UTIL_H #include "QXmppPasswordChecker.h" +#include "QXmppTask.h" #include <memory> #include <variant> @@ -96,6 +97,18 @@ T expectFutureVariant(const QFuture<Input> &future) return expectVariant<T>(future.result()); } +template<typename T, typename Input> +T expectFutureVariant(QXmppTask<Input> &future) +{ +#define return \ + return \ + { \ + } + QVERIFY(future.isFinished()); +#undef return + return expectVariant<T>(future.result()); +} + template<typename T> T wait(const QFuture<T> &future) { |
