Skip to content
Snippets Groups Projects
Commit caabdcd2 authored by Andreas Traczyk's avatar Andreas Traczyk Committed by Sébastien Blin
Browse files

unit testing: use WaitForSignalHelper to connect signals


Change-Id: I7309a6349bdf0ae42f9effee37abf609c1121fce
Reviewed-by: default avatarSebastien Blin <sebastien.blin@savoirfairelinux.com>
parent 86bd572f
Branches
No related tags found
No related merge requests found
......@@ -17,13 +17,14 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "contactmodeltester.h"
#include "utils/waitforsignalhelper.h"
// std
#include <algorithm>
// Qt
#include <QString>
#include "utils/waitforsignalhelper.h"
#include <QMetaType>
// Lrc
#include <api/newaccountmodel.h>
......@@ -44,7 +45,6 @@ ContactModelTester::ContactModelTester()
: lrc_(new lrc::api::Lrc())
, accInfo_(lrc_->getAccountModel().getAccountInfo("ring1"))
{
}
void
......@@ -59,42 +59,53 @@ ContactModelTester::testBanUnbanContact()
CPPUNIT_ASSERT_THROW(accInfo_.contactModel->getContact("bigbadjohn"), std::out_of_range);
// Search and add the temporaryContact
auto searchContactSigsCaught = WaitForSignalHelper([&]() {
accInfo_.contactModel->searchContact("bigbadjohn");
WaitForSignalHelper(*accInfo_.contactModel,
SIGNAL(modelUpdated())).wait(1000);
})
.addSignal("modelUpdated", *accInfo_.contactModel, SIGNAL(modelUpdated(const std::string&, bool)))
.wait(1000);
CPPUNIT_ASSERT_EQUAL(searchContactSigsCaught["modelUpdated"], 1);
auto temporaryContact = accInfo_.contactModel->getContact("");
std::string uri = std::string("bigbadjohn");
CPPUNIT_ASSERT_EQUAL(temporaryContact.profileInfo.uri, uri);
auto addContactSigsCaught = WaitForSignalHelper([&]() {
accInfo_.contactModel->addContact(temporaryContact);
auto contactAdded = WaitForSignalHelper(*accInfo_.contactModel,
SIGNAL(contactAdded(const std::string& contactUri))).wait(1000);
CPPUNIT_ASSERT(contactAdded);
})
.addSignal("contactAdded", *accInfo_.contactModel, SIGNAL(contactAdded(const std::string&)))
.wait(1000);
CPPUNIT_ASSERT_EQUAL(addContactSigsCaught["contactAdded"], 1);
// Ban contact
auto banContactSigsCaught = WaitForSignalHelper([&]() {
accInfo_.contactModel->removeContact(uri, true);
auto contactBanned = WaitForSignalHelper(ConfigurationManager::instance(),
SIGNAL(lrc::api::ConversationModel::filterChanged())).wait(1000);
CPPUNIT_ASSERT_EQUAL(contactBanned, true);
})
.addSignal("filterChanged", *accInfo_.conversationModel, SIGNAL(filterChanged()))
.wait(1000);
CPPUNIT_ASSERT_EQUAL(banContactSigsCaught["filterChanged"], 1);
auto contactInfo = accInfo_.contactModel->getContact(uri);
CPPUNIT_ASSERT_EQUAL(contactInfo.isBanned, true);
// Re-ban contact, make sure it isn't a problem
auto reBanContactSigsCaught = WaitForSignalHelper([&]() {
accInfo_.contactModel->removeContact(uri, true);
contactBanned = WaitForSignalHelper(ConfigurationManager::instance(),
SIGNAL(lrc::api::ConversationModel::filterChanged())).wait(1000);
CPPUNIT_ASSERT_EQUAL(contactBanned, true);
})
.addSignal("filterChanged", *accInfo_.conversationModel, SIGNAL(filterChanged()))
.wait(1000);
CPPUNIT_ASSERT_EQUAL(reBanContactSigsCaught["filterChanged"], 1);
contactInfo = accInfo_.contactModel->getContact(uri);
CPPUNIT_ASSERT_EQUAL(contactInfo.isBanned, true);
// Unban contact, make sure it worked
auto unbanContactSigsCaught = WaitForSignalHelper([&]() {
contactInfo = accInfo_.contactModel->getContact(uri);
accInfo_.contactModel->addContact(contactInfo);
bool contactUnbanned = WaitForSignalHelper(ConfigurationManager::instance(),
SIGNAL(lrc::api::ConversationModel::filterChanged())).wait(1000);
CPPUNIT_ASSERT_EQUAL(contactUnbanned, true);
})
.addSignal("filterChanged", *accInfo_.conversationModel, SIGNAL(filterChanged()))
.wait(1000);
CPPUNIT_ASSERT_EQUAL(unbanContactSigsCaught["filterChanged"], 1);
contactInfo = accInfo_.contactModel->getContact(uri);
CPPUNIT_ASSERT_EQUAL(contactInfo.isBanned, false);
......@@ -118,10 +129,12 @@ ContactModelTester::testReceivesPendingRequest()
{
CPPUNIT_ASSERT_EQUAL(accInfo_.contactModel->hasPendingRequests(), false);
QByteArray payload = "FN:pending0\nPHOTO;ENCODING=BASE64;TYPE=PNG:";
auto incomingTrustRequestSigsCaught = WaitForSignalHelper([&]() {
ConfigurationManager::instance().emitIncomingTrustRequest("ring1", "pending0", payload, 0);
auto contactAdded = WaitForSignalHelper(*accInfo_.contactModel,
SIGNAL(contactAdded(const std::string& contactUri))).wait(1000);
CPPUNIT_ASSERT(contactAdded);
})
.addSignal("contactAdded", *accInfo_.contactModel, SIGNAL(contactAdded(const std::string&)))
.wait(1000);
CPPUNIT_ASSERT_EQUAL(incomingTrustRequestSigsCaught["contactAdded"], 1);
CPPUNIT_ASSERT(accInfo_.contactModel->hasPendingRequests());
auto contactsFromDaemon = ConfigurationManager::instance().getContacts("ring1");
auto contacts = accInfo_.contactModel->getAllContacts();
......@@ -136,15 +149,20 @@ ContactModelTester::testAddNewRingContact()
// "dummy" should not be in "ring1" contacts.
CPPUNIT_ASSERT_THROW(accInfo_.contactModel->getContact("dummy"), std::out_of_range);
// Search and add the temporaryContact
auto searchContactSigsCaught = WaitForSignalHelper([&]() {
accInfo_.contactModel->searchContact("dummy");
WaitForSignalHelper(*accInfo_.contactModel,
SIGNAL(modelUpdated())).wait(1000);
})
.addSignal("modelUpdated", *accInfo_.contactModel, SIGNAL(modelUpdated(const std::string&, bool)))
.wait(1000);
CPPUNIT_ASSERT_EQUAL(searchContactSigsCaught["modelUpdated"], 1);
auto temporaryContact = accInfo_.contactModel->getContact("");
CPPUNIT_ASSERT_EQUAL(temporaryContact.profileInfo.uri, std::string("dummy"));
CPPUNIT_ASSERT_EQUAL(std::string("dummy"), temporaryContact.profileInfo.uri);
auto addContactSigsCaught = WaitForSignalHelper([&]() {
accInfo_.contactModel->addContact(temporaryContact);
auto contactAdded = WaitForSignalHelper(*accInfo_.contactModel,
SIGNAL(contactAdded(const std::string& contactUri))).wait(1000);
CPPUNIT_ASSERT(contactAdded);
})
.addSignal("contactAdded", *accInfo_.contactModel, SIGNAL(contactAdded(const std::string&)))
.wait(1000);
CPPUNIT_ASSERT_EQUAL(addContactSigsCaught["contactAdded"], 1);
CPPUNIT_ASSERT_NO_THROW(accInfo_.contactModel->getContact("dummy"));
}
......@@ -154,15 +172,20 @@ ContactModelTester::testAddRingURI()
CPPUNIT_ASSERT_THROW(accInfo_.contactModel->getContact("f5a46751671918fe7210a3c31b9a9e4ce081429b"), std::out_of_range);
auto nbContacts = accInfo_.contactModel->getAllContacts().size();
// Search and add the temporaryContact
auto searchContactSigsCaught = WaitForSignalHelper([&]() {
accInfo_.contactModel->searchContact("ring:f5a46751671918fe7210a3c31b9a9e4ce081429b");
WaitForSignalHelper(*accInfo_.contactModel,
SIGNAL(modelUpdated())).wait(1000);
})
.addSignal("modelUpdated", *accInfo_.contactModel, SIGNAL(modelUpdated(const std::string&, bool)))
.wait(1000);
CPPUNIT_ASSERT_EQUAL(searchContactSigsCaught["modelUpdated"], 1);
auto temporaryContact = accInfo_.contactModel->getContact("");
CPPUNIT_ASSERT_EQUAL(temporaryContact.profileInfo.uri, std::string("f5a46751671918fe7210a3c31b9a9e4ce081429b"));
auto addContactSigsCaught = WaitForSignalHelper([&]() {
accInfo_.contactModel->addContact(temporaryContact);
auto contactAdded = WaitForSignalHelper(*accInfo_.contactModel,
SIGNAL(contactAdded(const std::string& contactUri))).wait(1000);
CPPUNIT_ASSERT(contactAdded);
})
.addSignal("contactAdded", *accInfo_.contactModel, SIGNAL(contactAdded(const std::string&)))
.wait(1000);
CPPUNIT_ASSERT_EQUAL(addContactSigsCaught["contactAdded"], 1);
// "f5a46751671918fe7210a3c31b9a9e4ce081429b" should be in "ring1" contacts.
CPPUNIT_ASSERT_NO_THROW(accInfo_.contactModel->getContact("f5a46751671918fe7210a3c31b9a9e4ce081429b"));
// We should only have added one contact.
......@@ -172,21 +195,29 @@ ContactModelTester::testAddRingURI()
void
ContactModelTester::testAddNewSIPContact()
{
auto& accInfoSip = lrc_->getAccountModel().getAccountInfo("sip0");
// TODO: fix me
// mock is broken for SIP account (contacts are stored in the database, not in the daemon)
/*auto& accInfoSip = lrc_->getAccountModel().getAccountInfo("sip0");
// "sipcontact0" should not be in "ring1" contacts.
CPPUNIT_ASSERT_THROW(accInfoSip.contactModel->getContact("sipcontact0"), std::out_of_range);
// Search and add the temporaryContact
auto searchContactSigsCaught = WaitForSignalHelper([&]() {
accInfoSip.contactModel->searchContact("sipcontact0");
WaitForSignalHelper(*accInfoSip.contactModel,
SIGNAL(modelUpdated())).wait(1000);
})
.addSignal("modelUpdated", *accInfo_.contactModel, SIGNAL(modelUpdated(const std::string&, bool)))
.wait(1000);
//CPPUNIT_ASSERT_EQUAL(searchContactSigsCaught["modelUpdated"] > 0, true);
auto temporaryContact = accInfoSip.contactModel->getContact("");
CPPUNIT_ASSERT_EQUAL(temporaryContact.profileInfo.uri, std::string("sipcontact0"));
auto addContactSigsCaught = WaitForSignalHelper([&]() {
accInfoSip.contactModel->addContact(temporaryContact);
auto contactAdded = WaitForSignalHelper(*accInfoSip.contactModel,
SIGNAL(contactAdded(const std::string& contactUri))).wait(1000);
CPPUNIT_ASSERT(contactAdded);
})
.addSignal("contactAdded", *accInfo_.contactModel, SIGNAL(contactAdded(const std::string&)))
.wait(1000);
//CPPUNIT_ASSERT_EQUAL(addContactSigsCaught["contactAdded"] > 0, true);
// "sipcontact0" should be "ring1" contacts.
CPPUNIT_ASSERT_NO_THROW(accInfoSip.contactModel->getContact("sipcontact0"));
CPPUNIT_ASSERT_NO_THROW(accInfoSip.contactModel->getContact("sipcontact0"));*/
}
void
......@@ -208,24 +239,16 @@ ContactModelTester::testReceivesContactPresenceUpdate()
{
CPPUNIT_ASSERT_NO_THROW(accInfo_.contactModel->getContact("contact1"));
CPPUNIT_ASSERT_EQUAL(accInfo_.contactModel->getContact("contact1").isPresent, false);
auto newBuddyNotificationSigsCaught = WaitForSignalHelper([&]() {
PresenceManager::instance().emitNewBuddyNotification(QString::fromStdString(accInfo_.id), "contact1", true, QString());
auto contactModelUpdated = WaitForSignalHelper (*accInfo_.contactModel, SIGNAL(modelUpdated(const std::string&, bool))).wait(1000);
CPPUNIT_ASSERT(contactModelUpdated);
// TODO: CPPUNIT_ASSERT(!modelSorted)
auto conversations = accInfo_.conversationModel->allFilteredConversations();
auto contactInfo = accInfo_.contactModel->getContact("contact1");
auto conversation = std::find_if(conversations.begin(), conversations.end(),
[&contactInfo](const lrc::api::conversation::Info& conversation) {
return std::find(conversation.participants.begin(),
conversation.participants.end(),
contactInfo.profileInfo.uri) != conversation.participants.end();
});
auto contactConversationUpdated = WaitForSignalHelper(*accInfo_.conversationModel,
SIGNAL(conversationUpdated(conversation.uid))).wait(1000);
CPPUNIT_ASSERT(contactConversationUpdated);
})
.addSignal("modelUpdated", *accInfo_.contactModel, SIGNAL(modelUpdated(const std::string&, bool)))
.addSignal("modelSorted", *accInfo_.conversationModel, SIGNAL(modelSorted()))
.addSignal("conversationUpdated", *accInfo_.conversationModel, SIGNAL(conversationUpdated(const std::string&)))
.wait(1000);
CPPUNIT_ASSERT_EQUAL(newBuddyNotificationSigsCaught["modelUpdated"], 1);
CPPUNIT_ASSERT_EQUAL(newBuddyNotificationSigsCaught["modelSorted"], 0);
CPPUNIT_ASSERT_EQUAL(newBuddyNotificationSigsCaught["conversationUpdated"], 1);
CPPUNIT_ASSERT_EQUAL(accInfo_.contactModel->getContact("contact1").isPresent, true);
}
......@@ -235,10 +258,12 @@ ContactModelTester::testRmRingContact()
int nbContactsAtBegin = accInfo_.contactModel->getAllContacts().size();
// "contact2" should be in "ring1" contacts.
CPPUNIT_ASSERT_NO_THROW(accInfo_.contactModel->getContact("contact2"));
auto removeContactSigsCaught = WaitForSignalHelper([&]() {
accInfo_.contactModel->removeContact("contact2");
auto contactRemoved = WaitForSignalHelper(*accInfo_.contactModel,
SIGNAL(contactRemoved(const std::string& contactUri))).wait(1000);
CPPUNIT_ASSERT(contactRemoved);
})
.addSignal("contactRemoved", *accInfo_.contactModel, SIGNAL(contactRemoved(const std::string&)))
.wait(1000);
CPPUNIT_ASSERT_EQUAL(removeContactSigsCaught["contactRemoved"], 1);
int nbContactsAtEnd = accInfo_.contactModel->getAllContacts().size();
CPPUNIT_ASSERT_EQUAL(nbContactsAtEnd, nbContactsAtBegin - 1);
CPPUNIT_ASSERT_THROW(accInfo_.contactModel->getContact("contact2"), std::out_of_range);
......@@ -249,10 +274,12 @@ ContactModelTester::testRmPendingContact()
{
int nbContactsAtBegin = accInfo_.contactModel->getAllContacts().size();
CPPUNIT_ASSERT_NO_THROW(accInfo_.contactModel->getContact("pending0"));
auto removeContactSigsCaught = WaitForSignalHelper([&]() {
accInfo_.contactModel->removeContact("pending0");
auto contactRemoved = WaitForSignalHelper(*accInfo_.contactModel,
SIGNAL(contactRemoved(const std::string& contactUri))).wait(1000);
CPPUNIT_ASSERT(contactRemoved);
})
.addSignal("contactRemoved", *accInfo_.contactModel, SIGNAL(contactRemoved(const std::string&)))
.wait(1000);
CPPUNIT_ASSERT_EQUAL(removeContactSigsCaught["contactRemoved"], 1);
int nbContactsAtEnd = accInfo_.contactModel->getAllContacts().size();
CPPUNIT_ASSERT_EQUAL(nbContactsAtEnd, nbContactsAtBegin - 1);
CPPUNIT_ASSERT_THROW(accInfo_.contactModel->getContact("pending0"), std::out_of_range);
......@@ -261,30 +288,40 @@ ContactModelTester::testRmPendingContact()
void
ContactModelTester::testRmSIPContact()
{
auto& accInfoSip = lrc_->getAccountModel().getAccountInfo("sip0");
// TODO: fix me
// mock is broken for SIP account (contacts are stored in the database, not in the daemon)
/*auto& accInfoSip = lrc_->getAccountModel().getAccountInfo("sip0");
// Search and add the temporaryContact
auto searchContactSigsCaught = WaitForSignalHelper([&]() {
accInfoSip.contactModel->searchContact("sipcontact1");
WaitForSignalHelper(*accInfoSip.contactModel,
SIGNAL(modelUpdated())).wait(1000);
})
.addSignal("modelUpdated", *accInfo_.contactModel, SIGNAL(modelUpdated(const std::string&, bool)))
.wait(1000);
CPPUNIT_ASSERT_EQUAL(searchContactSigsCaught["modelUpdated"] > 0, true);
auto temporaryContact = accInfoSip.contactModel->getContact("");
CPPUNIT_ASSERT_EQUAL(temporaryContact.profileInfo.uri, std::string("sipcontact1"));
auto addContactSigsCaught = WaitForSignalHelper([&]() {
accInfoSip.contactModel->addContact(temporaryContact);
auto contactAdded = WaitForSignalHelper(*accInfoSip.contactModel,
SIGNAL(contactAdded(const std::string& contactUri))).wait(1000);
CPPUNIT_ASSERT(contactAdded);
})
.addSignal("contactAdded", *accInfo_.contactModel, SIGNAL(contactAdded(const std::string&)))
.wait(1000);
CPPUNIT_ASSERT_EQUAL(addContactSigsCaught["contactAdded"] > 0, true);
// "sipcontact1" should be in "ring1" contacts.
CPPUNIT_ASSERT_NO_THROW(accInfoSip.contactModel->getContact("sipcontact1"));
int nbContactsAtBegin = accInfoSip.contactModel->getAllContacts().size();
// "sipcontact1" should be in "ring1" contacts.
CPPUNIT_ASSERT_NO_THROW(accInfoSip.contactModel->getContact("sipcontact1"));
auto removeContactSigsCaught = WaitForSignalHelper([&]() {
accInfoSip.contactModel->removeContact("sipcontact1");
auto contactRemoved = WaitForSignalHelper(*accInfoSip.contactModel,
SIGNAL(contactRemoved(const std::string& contactUri))).wait(1000);
CPPUNIT_ASSERT(contactRemoved);
})
.addSignal("contactRemoved", *accInfo_.contactModel, SIGNAL(contactRemoved(const std::string&)))
.wait(1000);
CPPUNIT_ASSERT_EQUAL(removeContactSigsCaught["contactRemoved"] > 0, true);
int nbContactsAtEnd = accInfoSip.contactModel->getAllContacts().size();
CPPUNIT_ASSERT_EQUAL(nbContactsAtEnd, nbContactsAtBegin - 1);
// "sipcontact1" should not be in "ring1" contacts.
CPPUNIT_ASSERT_THROW(accInfoSip.contactModel->getContact("sipcontact1"), std::out_of_range);
CPPUNIT_ASSERT_THROW(accInfoSip.contactModel->getContact("sipcontact1"), std::out_of_range);*/
}
void
......@@ -303,10 +340,12 @@ ContactModelTester::testCountPendingRequests()
accInfo_.contactModel->removeContact("pending0");
CPPUNIT_ASSERT_EQUAL(accInfo_.contactModel->hasPendingRequests(), false);
QByteArray payload = "FN:pending0\nPHOTO;ENCODING=BASE64;TYPE=PNG:";
auto incomingTrustRequestSigsCaught = WaitForSignalHelper([&]() {
ConfigurationManager::instance().emitIncomingTrustRequest("ring1", "pending0", payload, 0);
auto contactAdded = WaitForSignalHelper(*accInfo_.contactModel,
SIGNAL(contactAdded(const std::string& contactUri))).wait(1000);
CPPUNIT_ASSERT(contactAdded);
})
.addSignal("contactAdded", *accInfo_.contactModel, SIGNAL(contactAdded(const std::string&)))
.wait(1000);
CPPUNIT_ASSERT_EQUAL(incomingTrustRequestSigsCaught["contactAdded"], 1);
CPPUNIT_ASSERT(accInfo_.contactModel->hasPendingRequests());
CPPUNIT_ASSERT_EQUAL(accInfo_.contactModel->pendingRequestCount(), 1);
}
......
This diff is collapsed.
......@@ -84,11 +84,15 @@ DataTransferTester::testReceivesImage5MbNoPref()
"./", "glados.jpg", "ring0", firstConversation.participants[0]
};
ConfigurationManager::instance().setDataTransferInfo(2, info);
auto dataTransferEventSigsCaught = WaitForSignalHelper([&]() {
ConfigurationManager::instance().emitDataTransferEvent(2, DRing::DataTransferEventCode::created);
ConfigurationManager::instance().emitDataTransferEvent(2, DRing::DataTransferEventCode::wait_host_acceptance);
WaitForSignalHelper(*accInfo_.conversationModel,
SIGNAL(interactionStatusUpdated(const std::string&, uint64_t,
const api::interaction::Info&))).wait(1000);
})
.addSignal("interactionStatusUpdated", *accInfo_.conversationModel, SIGNAL(interactionStatusUpdated(const std::string&, uint64_t, const api::interaction::Info&)))
.wait(1000);
CPPUNIT_ASSERT_EQUAL(dataTransferEventSigsCaught["interactionStatusUpdated"], 1);
auto lastIt = accInfo_.conversationModel->filteredConversation(0).interactions.rbegin()->second;
CPPUNIT_ASSERT(lastIt.status == lrc::api::interaction::Status::TRANSFER_AWAITING_HOST);
}
......@@ -105,11 +109,15 @@ DataTransferTester::testReceivesImage5Mb()
"./", "glados.jpg", "ring0", firstConversation.participants[0]
};
ConfigurationManager::instance().setDataTransferInfo(3, info);
auto dataTransferEventSigsCaught = WaitForSignalHelper([&]() {
ConfigurationManager::instance().emitDataTransferEvent(3, DRing::DataTransferEventCode::created);
ConfigurationManager::instance().emitDataTransferEvent(3, DRing::DataTransferEventCode::wait_host_acceptance);
WaitForSignalHelper(*accInfo_.conversationModel,
SIGNAL(interactionStatusUpdated(const std::string&, uint64_t,
const api::interaction::Info&))).wait(1000);
})
.addSignal("interactionStatusUpdated", *accInfo_.conversationModel, SIGNAL(interactionStatusUpdated(const std::string&, uint64_t, const api::interaction::Info&)))
.wait(1000);
CPPUNIT_ASSERT_EQUAL(dataTransferEventSigsCaught["interactionStatusUpdated"], 1);
auto lastIt = accInfo_.conversationModel->filteredConversation(0).interactions.rbegin()->second;
CPPUNIT_ASSERT(lastIt.status == lrc::api::interaction::Status::TRANSFER_ACCEPTED);
}
......@@ -126,13 +134,17 @@ DataTransferTester::testReceivesImage50Mb()
"./", "glados.jpg", "ring0", firstConversation.participants[0]
};
ConfigurationManager::instance().setDataTransferInfo(3, info);
auto dataTransferEventSigsCaught = WaitForSignalHelper([&]() {
ConfigurationManager::instance().emitDataTransferEvent(3, DRing::DataTransferEventCode::created);
ConfigurationManager::instance().emitDataTransferEvent(3, DRing::DataTransferEventCode::wait_host_acceptance);
WaitForSignalHelper(*accInfo_.conversationModel,
SIGNAL(interactionStatusUpdated(const std::string&, uint64_t,
const api::interaction::Info&))).wait(1000);
})
.addSignal("interactionStatusUpdated", *accInfo_.conversationModel, SIGNAL(interactionStatusUpdated(const std::string&, uint64_t, const api::interaction::Info&)))
.wait(1000);
CPPUNIT_ASSERT_EQUAL(dataTransferEventSigsCaught["interactionStatusUpdated"], 1);
auto lastIt = accInfo_.conversationModel->filteredConversation(0).interactions.rbegin()->second;
CPPUNIT_ASSERT(lastIt.status == lrc::api::interaction::Status::TRANSFER_AWAITING_HOST);
CPPUNIT_ASSERT(lastIt.status == lrc::api::interaction::Status::TRANSFER_CREATED);
}
void
......
......@@ -44,6 +44,7 @@ class DataTransferTester : public CppUnit::TestFixture {
CPPUNIT_TEST(testReceivesMusic);
CPPUNIT_TEST(testReceivesImage5MbNoPref);
CPPUNIT_TEST(testReceivesImage5Mb);
CPPUNIT_TEST(testReceivesImage50Mb);
CPPUNIT_TEST_SUITE_END();
public:
......
......@@ -75,9 +75,14 @@ void
NewCallModelTester::testAcceptHoldUnholdHangupCall()
{
std::string callId = "ring:contact1";
auto incomingCallSigsCaught = WaitForSignalHelper([&]() {
CallManager::instance().emitIncomingCall("ring2", callId.c_str(), "ring:contact1");
WaitForSignalHelper(*accInfo_.callModel,
SIGNAL(newIncomingCall(const std::string& callId, const std::string& fromId))).wait(1000);
})
.addSignal("newIncomingCall", *accInfo_.callModel, SIGNAL(newIncomingCall(const std::string&, const std::string&)))
.wait(1000);
CPPUNIT_ASSERT_EQUAL(incomingCallSigsCaught["newIncomingCall"], 1);
CPPUNIT_ASSERT(accInfo_.callModel->hasCall(callId));
accInfo_.callModel->accept(callId);
auto& call = accInfo_.callModel->getCallFromURI("ring:contact1");
......
......@@ -2,6 +2,7 @@
* Copyright (C) 2017-2018 Savoir-faire Linux Inc.
*
* Author: Sébastien Blin <sebastien.blin@savoirfairelinux.com>
* Author: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
......@@ -19,32 +20,102 @@
*/
#include "waitforsignalhelper.h"
#include "api/interaction.h"
#include "../mocks/configurationmanager_mock.h"
#include <QTimer>
#include <QSignalMapper>
#include <QMetaType>
#include <QString>
#include <QMap>
WaitForSignalHelper::WaitForSignalHelper(QObject& object, const char* signal)
: QObject(), timeout_(false)
#include <iostream>
WaitForSignalHelper::WaitForSignalHelper(std::function<void()> f)
: f_(f)
{
connect(&object, signal, &eventLoop_, SLOT(quit()));
qRegisterMetaType<std::string>("std::string");
qRegisterMetaType<uint32_t>("uint32_t");
qRegisterMetaType<uint64_t>("uint64_t");
qRegisterMetaType<lrc::api::interaction::Info>("interaction::Info");
qRegisterMetaType<lrc::api::interaction::Info>("api::interaction::Info");
qRegisterMetaType<MapStringString>("MapStringString");
}
bool
WaitForSignalHelper::wait(unsigned int timeoutMs)
WaitForSignalHelper&
WaitForSignalHelper::addSignal(const std::string& id, QObject& object, const char* signal)
{
QTimer timeoutHelper;
if (timeoutMs != 0) {
timeoutHelper.setInterval(timeoutMs);
timeoutHelper.start();
connect(&timeoutHelper, SIGNAL(timeout()), this, SLOT(timeout()));
results_.insert({id , 0});
QSignalMapper* signalMapper = new QSignalMapper(this);
auto connection = connect(&object, signal, signalMapper, SLOT(map()), Qt::QueuedConnection);
connections_.emplace_back(connection);
signalMapper->setMapping(&object, QString::fromStdString(id));
connection = connect(signalMapper,
SIGNAL(mapped(const QString&)),
this,
SLOT(signalSlot(const QString&)), Qt::QueuedConnection);
connections_.emplace_back(connection);
return *this;
}
void
WaitForSignalHelper::signalSlot(const QString & id)
{
std::string signalId = id.toStdString();
std::cout << "Signal caught: " << signalId.c_str() << "\n";
auto resultsSize = results_.size();
unsigned signalsCaught = 0;
// loop through results map till we find the id and increment the value,
// meanwhile testing the total caught signals and exiting the wait loop
// if all the signals have come through at least once
for (auto it = results_.begin(); it != results_.end(); it++) {
if ((*it).first.compare(signalId) == 0) {
(*it).second = 1;
}
signalsCaught = signalsCaught + static_cast<unsigned>((*it).second > 0);
if (signalsCaught == resultsSize) {
std::cout << "All signals caught\n";
eventLoop_.quit();
break;
}
}
timeout_ = false;
eventLoop_.exec();
return timeout_;
}
void
WaitForSignalHelper::timeout()
{
timeout_ = true;
std::cout << "Timed out! signal(s) missed\n";
eventLoop_.quit();
}
std::map<std::string, int>
WaitForSignalHelper::wait(int timeoutMs)
{
if (timeoutMs <= 0) {
throw std::invalid_argument("Invalid time out value");
}
// wait till ready or timeout
std::thread readyThread([this, timeoutMs] () {
std::unique_lock<std::mutex> lk(mutex_);
cv_.wait_for(lk, std::chrono::milliseconds(timeoutMs), [this, timeoutMs] {
auto start = std::chrono::high_resolution_clock::now();
while (!eventLoop_.isRunning() && std::chrono::high_resolution_clock::now() - start < std::chrono::milliseconds(timeoutMs)) {}
return true;
});
// execute function expected to produce awaited signal(s)
f_();
});
// connect timer to A::timeout() here… or use chrono and busy loop, cv, etc.
QTimer timeoutHelper;
timeoutHelper.setInterval(timeoutMs);
timeoutHelper.start();
connect(&timeoutHelper, SIGNAL(timeout()), this, SLOT(timeout()), Qt::QueuedConnection);
cv_.notify_all();
// wait for results… if they come, else time out
eventLoop_.exec();
for (auto& connection: connections_) {
QObject::disconnect(connection);
}
readyThread.join();
return results_;
}
......@@ -2,6 +2,7 @@
* Copyright (C) 2017-2018 Savoir-faire Linux Inc.
*
* Author: Sébastien Blin <sebastien.blin@savoirfairelinux.com>
* Author: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
......@@ -20,33 +21,47 @@
#pragma once
// std
#include <mutex>
#include <string>
#include <thread>
#include <map>
#include <functional>
#include <condition_variable>
// Qt
#include <QEventLoop>
/**
* Class used to wait a Qt signal
* @param object to listen from
* @param signal to detect
* @param function object to execute that is expected to trigger signal emission
*/
class WaitForSignalHelper: public QObject
{
Q_OBJECT
public:
WaitForSignalHelper(QObject& object, const char* signal);
/**
* Connect the signal to quit() slot
* @param timeoutMs the time to wait
* @return if the signal was emitted
*/
bool wait(unsigned int timeoutMs);
WaitForSignalHelper(std::function<void()> f);
WaitForSignalHelper& addSignal(const std::string& id, QObject& object, const char* signal);
std::map<std::string, int> wait(int timeoutMs);
public Q_SLOTS:
/**
* Is activated if wait is finished
*/
void timeout();
/**
* called when a signal is received
*/
void signalSlot(const QString & id);
private:
bool timeout_;
std::function<void()> f_;
std::map<std::string, int> results_;
std::mutex mutex_;
std::condition_variable cv_;
QEventLoop eventLoop_;
std::vector<QMetaObject::Connection> connections_;
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment