AusweisApp2/test/qt/card/test_ReaderManager.cpp

159 lines
3.9 KiB
C++
Raw Normal View History

2017-07-03 09:30:10 +02:00
/*!
* \copyright Copyright (c) 2014 Governikus GmbH & Co. KG
*/
#include "MockReaderManagerPlugIn.h"
#include "ReaderManager.h"
2017-07-03 09:33:28 +02:00
#include "ReaderManagerWorker.h"
2017-07-03 09:30:10 +02:00
2017-07-03 09:33:28 +02:00
#include <QCoreApplication>
2017-07-03 09:30:10 +02:00
#include <QSharedPointer>
#include <QSignalSpy>
2017-07-03 09:33:28 +02:00
#include <QtTest>
2017-07-03 09:30:10 +02:00
Q_IMPORT_PLUGIN(MockReaderManagerPlugIn)
using namespace governikus;
class CreateCardConnectionCommandSlot
: public QObject
{
Q_OBJECT
public:
QSharedPointer<CardConnection> mCardConnection;
bool mSlotCalled = false;
QTime mDieTime;
void wait(int pMillis = 1000)
{
mDieTime = QTime::currentTime().addMSecs(pMillis);
while (QTime::currentTime() < mDieTime)
{
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}
}
public Q_SLOTS:
void onCardCommandDone(QSharedPointer<CreateCardConnectionCommand> pCommand)
{
mDieTime = QTime::currentTime();
mSlotCalled = true;
mCardConnection = pCommand->getCardConnection();
}
};
class test_ReaderManager
: public QObject
{
Q_OBJECT
private Q_SLOTS:
void initTestCase()
{
2017-07-03 09:33:28 +02:00
ReaderManagerWorker::registerMetaTypes();
2017-07-03 09:30:10 +02:00
ReaderManager::getInstance().init();
ReaderManager::getInstance().getPlugInInfos(); // just to wait until initialization finished
}
void cleanupTestCase()
{
ReaderManager::getInstance().shutdown();
}
void fireReaderAdded()
{
QSignalSpy spy(&ReaderManager::getInstance(), &ReaderManager::fireReaderAdded);
MockReaderManagerPlugIn::getInstance().addReader("MockReader 4711");
spy.wait();
QCOMPARE(spy.count(), 1);
QCOMPARE(spy.takeFirst().at(0).toString(), QString("MockReader 4711"));
}
void fireReaderRemoved()
{
QSignalSpy spy(&ReaderManager::getInstance(), &ReaderManager::fireReaderRemoved);
MockReaderManagerPlugIn::getInstance().addReader("MockReader 4711");
MockReaderManagerPlugIn::getInstance().removeReader("MockReader 4711");
spy.wait();
QCOMPARE(spy.count(), 1);
QCOMPARE(spy.takeFirst().at(0).toString(), QString("MockReader 4711"));
}
void fireCreateCardConnection_forUnknownReader()
{
CreateCardConnectionCommandSlot commandSlot;
ReaderManager::getInstance().callCreateCardConnectionCommand("UnknownReader", &commandSlot, &CreateCardConnectionCommandSlot::onCardCommandDone);
commandSlot.wait();
QVERIFY(commandSlot.mSlotCalled);
QVERIFY(commandSlot.mCardConnection.isNull());
}
void fireCreateCardConnection_noCard()
{
CreateCardConnectionCommandSlot commandSlot;
MockReaderManagerPlugIn::getInstance().addReader("MockReader 4711");
ReaderManager::getInstance().callCreateCardConnectionCommand("UnknownReader", &commandSlot, &CreateCardConnectionCommandSlot::onCardCommandDone);
commandSlot.wait();
QVERIFY(commandSlot.mSlotCalled);
QVERIFY(commandSlot.mCardConnection.isNull());
}
void fireCreateCardConnection_cardConnectFail()
{
CreateCardConnectionCommandSlot commandSlot;
MockReader* reader = MockReaderManagerPlugIn::getInstance().addReader("MockReader 4711");
MockCardConfig cardConfig;
2017-07-03 09:33:28 +02:00
cardConfig.mConnect = CardReturnCode::COMMAND_FAILED;
2017-07-03 09:30:10 +02:00
reader->setCard(cardConfig);
ReaderManager::getInstance().callCreateCardConnectionCommand("MockReader 4711", &commandSlot, &CreateCardConnectionCommandSlot::onCardCommandDone);
commandSlot.wait();
QVERIFY(commandSlot.mSlotCalled);
QVERIFY(commandSlot.mCardConnection.isNull());
}
void fireCreateCardConnection()
{
CreateCardConnectionCommandSlot commandSlot;
MockReader* reader = MockReaderManagerPlugIn::getInstance().addReader("MockReader 4711");
MockCardConfig cardConfig;
2017-07-03 09:33:28 +02:00
cardConfig.mConnect = CardReturnCode::OK;
2017-07-03 09:30:10 +02:00
reader->setCard(cardConfig);
ReaderManager::getInstance().callCreateCardConnectionCommand("MockReader 4711", &commandSlot, &CreateCardConnectionCommandSlot::onCardCommandDone);
commandSlot.wait();
QVERIFY(commandSlot.mSlotCalled);
QVERIFY(!commandSlot.mCardConnection.isNull());
}
};
QTEST_GUILESS_MAIN(test_ReaderManager)
#include "test_ReaderManager.moc"