/*! * \brief Unit tests for \ref NotificationParser * * \copyright Copyright (c) 2017 Governikus GmbH & Co. KG */ #include "NotificationHandler.h" #include "MockDataChannel.h" #include "RemoteCardNotificationChecker.h" #include #include using namespace governikus; class NotificationHandlerSpy : public QObject { Q_OBJECT private: const QSharedPointer mNotificationHandler; bool mClosed; GlobalStatus::Code mCloseCode; QVector > mReceivedNotifications; public: NotificationHandlerSpy(const QSharedPointer pNotificationHandler); virtual ~NotificationHandlerSpy(); bool isClosed() const; GlobalStatus::Code getCloseCode() const; const QVector >& getReceivedNotifications() const; private Q_SLOTS: void onClosed(GlobalStatus::Code pCloseCode); void onReceived(const QSharedPointer& pNotification); }; NotificationHandlerSpy::NotificationHandlerSpy(const QSharedPointer pNotificationHandler) : mNotificationHandler(pNotificationHandler) , mClosed(false) , mCloseCode(GlobalStatus::Code::RemoteReader_CloseCode_Undefined) { connect(mNotificationHandler.data(), &NotificationHandler::fireClosed, this, &NotificationHandlerSpy::onClosed); connect(mNotificationHandler.data(), &NotificationHandler::fireReceived, this, &NotificationHandlerSpy::onReceived); } NotificationHandlerSpy::~NotificationHandlerSpy() { disconnect(mNotificationHandler.data(), &NotificationHandler::fireClosed, this, &NotificationHandlerSpy::onClosed); disconnect(mNotificationHandler.data(), &NotificationHandler::fireReceived, this, &NotificationHandlerSpy::onReceived); } bool NotificationHandlerSpy::isClosed() const { return mClosed; } GlobalStatus::Code NotificationHandlerSpy::getCloseCode() const { return mCloseCode; } const QVector >& NotificationHandlerSpy::getReceivedNotifications() const { return mReceivedNotifications; } void NotificationHandlerSpy::onClosed(GlobalStatus::Code pCloseCode) { mClosed = true; mCloseCode = pCloseCode; } void NotificationHandlerSpy::onReceived(const QSharedPointer& pNotification) { mReceivedNotifications += pNotification; } class test_notificationHandler : public QObject { Q_OBJECT private: const QSharedPointer mChecker; private Q_SLOTS: void channelClosedNormally() { const QSharedPointer channel(new MockDataChannel()); const QSharedPointer handler(new NotificationHandler(channel)); NotificationHandlerSpy spy(handler); channel->close(); QVERIFY(spy.isClosed()); QCOMPARE(spy.getCloseCode(), GlobalStatus::Code::RemoteReader_CloseCode_NormalClose); } void channelClosedAbnormally() { const QSharedPointer channel(new MockDataChannel()); const QSharedPointer handler(new NotificationHandler(channel)); NotificationHandlerSpy spy(handler); channel->closeAbnormal(); QVERIFY(spy.isClosed()); QCOMPARE(spy.getCloseCode(), GlobalStatus::Code::RemoteReader_CloseCode_AbnormalClose); } void notificationsAreDelivered() { const QSharedPointer clientChannel(new MockDataChannel()); const QSharedPointer clientHandler(new NotificationHandler(clientChannel)); const QSharedPointer serverChannel(new MockDataChannel()); const QSharedPointer serverHandler(new NotificationHandler(serverChannel)); connect(clientChannel.data(), &MockDataChannel::fireSend, serverChannel.data(), &MockDataChannel::onReceived, Qt::DirectConnection); NotificationHandlerSpy spy(serverHandler); clientHandler->send(QSharedPointer(new ConnectCmd(QStringLiteral("NFC Reader")))); clientHandler->send(QSharedPointer(new TransmitCmd(QStringLiteral("NFC Reader"), QByteArray::fromHex("00A402022F00")))); clientHandler->send(QSharedPointer(new DisconnectCmd(QStringLiteral("NFC Reader")))); const QVector > receivedNotifications = spy.getReceivedNotifications(); QCOMPARE(receivedNotifications.size(), 3); QCOMPARE(receivedNotifications.at(0)->getType(), CardNotificationType::CONNECT); receivedNotifications.at(0)->accept(mChecker); QCOMPARE(receivedNotifications.at(1)->getType(), CardNotificationType::TRANSMIT); receivedNotifications.at(1)->accept(mChecker); QCOMPARE(receivedNotifications.at(2)->getType(), CardNotificationType::DISCONNECT); receivedNotifications.at(2)->accept(mChecker); } void channelIsClosedWhenNotificationHandlerIsDestroyed() { const QSharedPointer clientChannel(new MockDataChannel()); QSharedPointer clientHandler(new NotificationHandler(clientChannel)); const QSharedPointer serverChannel(new MockDataChannel()); const QSharedPointer serverHandler(new NotificationHandler(serverChannel)); connect(clientChannel.data(), &MockDataChannel::fireSend, serverChannel.data(), &MockDataChannel::onReceived, Qt::DirectConnection); connect(clientChannel.data(), &DataChannel::fireClosed, serverChannel.data(), &DataChannel::close, Qt::DirectConnection); NotificationHandlerSpy spy(serverHandler); QVERIFY(!spy.isClosed()); // Destroying a notification handler should close the underlying channel. clientHandler.reset(); QVERIFY(spy.isClosed()); QCOMPARE(spy.getCloseCode(), GlobalStatus::Code::RemoteReader_CloseCode_NormalClose); } public: test_notificationHandler() : mChecker(new RemoteCardNotificationChecker()) { } }; QTEST_GUILESS_MAIN(test_notificationHandler) #include "test_notificationHandler.moc"