/*! * * Copyright (c) 2019-2019 Diality Inc. - All Rights Reserved. * copyright * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, * IN PART OR IN WHOLE, * WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. * * file unittests.cpp * date 11/21/2019 * author Behrouz NematiPour * */ #include "unittests.h" //using namespace Can; // Project #include "guiglobals.h" #include "caninterface.h" #include "messagehandler.h" #include "applicationcontroller.h" #include "guicontroller.h" #include "maintimer.h" unittests::unittests(QObject *parent) : QObject(parent) { } void unittests::initTestCase() { } void unittests::initTestCase_data() { } void unittests::init() { _emited = false; } void unittests::tst_CanInterface_Connect_Error_Interface() { QString mTr = tr("Error: Connection"); Can::_CanInterface->_canInterface = "can1"; Can::_CanInterface ->init(); QCOMPARE(Can::_CanInterface->status().left(mTr.length()), mTr); } void unittests::tst_CanInterface_Connect_NoError() { Can::_CanInterface->_canInterface = "can0"; Can::_CanInterface ->init(); QString mTr = tr("Connected"); QCOMPARE(Can::_CanInterface->status().left(mTr.length()), mTr); Can::_CanInterface->onFrameReceive (); } void unittests::tst_MessageHandler_Init() { Can::_MessageHandler->init(); connect(Can::_MessageHandler, &Can::MessageHandler::didActionPerform, [=](const QCanBusFrame &vFrame) { _emited = true; QString packet = vFrame.toString(); QCOMPARE(packet, _expected); }); connect(Can::_MessageHandler, &Can::MessageHandler::didActionRequest, [=](const QCanBusFrame &vFrame) { _emited = true; QString packet = vFrame.toString(); QCOMPARE(packet, _expected); }); connect(Can::_MessageHandler, &Can::MessageHandler::didActionCommand, [=](Gui::GuiActionType vAction) { _emited = true; QCOMPARE(vAction, _action); }); } void unittests::tst_MessageHandler_ActionPerform_PowerOff_Accepted() { _expected = " 100 [8] A5 01 00 01 01 00 00 00"; Can::_MessageHandler->onActionPerform(Gui::GuiActionType::PowerOff,Gui::GuiActionData::Accepted); QVERIFY(_emited); } void unittests::tst_MessageHandler_ActionPerform_PowerOff_Rejected() { _expected = " 100 [8] A5 01 00 01 00 00 00 00"; Can::_MessageHandler->onActionPerform(Gui::GuiActionType::PowerOff,Gui::GuiActionData::Rejected); QVERIFY(_emited); } void unittests::tst_MessageHandler_ActionPerform_Unknown() { _expected = " 000 [0]"; Can::_MessageHandler->onActionPerform(Gui::GuiActionType::Unknown,Gui::GuiActionData::NoData); QVERIFY(_emited); } void unittests::tst_MessageHandler_ActionRequest_PowerOff() { _expected = " 100 [8] A5 01 00 00 00 00 00 00"; Can::_MessageHandler->onActionRequest(Gui::GuiActionType::PowerOff); QVERIFY(_emited); } void unittests::tst_MessageHandler_ActionRequest_Unknown() { _expected = " 000 [0]"; Can::_MessageHandler->onActionRequest(Gui::GuiActionType::Unknown); QVERIFY(_emited); } void unittests::tst_MessageHandler_ActionCommand_PowerOff() { _action = Gui::GuiActionType::PowerOff; QCanBusFrame mFrame; QString mPayload; mFrame.setFrameId(Can::MessageHandler::eChlid_HD); mPayload = "A5 01 00 00 00 00 00 00"; mFrame.setPayload(QByteArray::fromHex(mPayload.remove(QLatin1Char(' ')).toLatin1())); emit Can::_CanInterface->didRead(mFrame); QVERIFY(_emited); } void unittests::cleanup() { disconnect(Can::_MessageHandler); Can::_CanInterface ->initConnections(); Can::_MessageHandler->initConnections(); } void unittests::cleanupTestCase() { Can::_CanInterface->quit(); QString mTr = tr("Disconnected"); QCOMPARE(Can::_CanInterface->status().left(mTr.length()), mTr); }