void TestModbusConnection::connectionSuccesAfterFail()
{

    ModbusConnection * pConnection = new ModbusConnection(this);

    QSignalSpy spySuccess(pConnection, &ModbusConnection::connectionSuccess);
    QSignalSpy spyError(pConnection, &ModbusConnection::connectionError);

    pConnection->openConnection(_serverConnectionData.host(), _serverConnectionData.port(), 1000);

    QVERIFY(spyError.wait(1500));

    QCOMPARE(spySuccess.count(), 0);
    QCOMPARE(spyError.count(), 1);

    QCOMPARE(pConnection->connectionState(), QModbusDevice::UnconnectedState);

    // Start server
    QVERIFY(_pTestSlaveModbus->connect(_serverConnectionData, _slaveId));

    pConnection->openConnection(_serverConnectionData.host(), _serverConnectionData.port(), 1000);

    QVERIFY(spySuccess.wait(500));

    QCOMPARE(spySuccess.count(), 1);
    QCOMPARE(pConnection->connectionState(), QModbusDevice::ConnectedState);

    pConnection->closeConnection();
}
void WebEngineExportHtmlPageJobTest::shouldHaveDefaultValue()
{
    WebEngineViewer::WebEngineExportHtmlPageJob job;
    QSignalSpy spyFailed(&job, SIGNAL(failed()));
    QSignalSpy spySuccess(&job, SIGNAL(success(QString)));
    QVERIFY(!job.engineView());
    job.start();
    QCOMPARE(spyFailed.count(), 1);
    QCOMPARE(spySuccess.count(), 0);
}
void TestModbusConnection::readRequestSuccess()
{
    /* Start server */
    QVERIFY(_pTestSlaveModbus->connect(_serverConnectionData, _slaveId));

    _pTestSlaveData->setRegisterState(0, true);
    _pTestSlaveData->setRegisterState(1, true);

    _pTestSlaveData->setRegisterValue(0, 0);
    _pTestSlaveData->setRegisterValue(1, 1);

    /* Open connection */
    ModbusConnection * pConnection = new ModbusConnection(this);
    QSignalSpy spySuccess(pConnection, &ModbusConnection::connectionSuccess);
    pConnection->openConnection(_serverConnectionData.host(), _serverConnectionData.port(), 1000);

    pConnection->openConnection(_serverConnectionData.host(), _serverConnectionData.port(), 1000);

    QVERIFY(spySuccess.wait(100));

    QCOMPARE(pConnection->connectionState(), QModbusDevice::ConnectedState);

    QSignalSpy spyResultSuccess(pConnection, &ModbusConnection::readRequestSuccess);
    QSignalSpy spyResultProtocolError(pConnection, &ModbusConnection::readRequestProtocolError);
    QSignalSpy spyResultError(pConnection, &ModbusConnection::readRequestError);

    pConnection->sendReadRequest(40001, 2, _slaveId);

    QVERIFY(spyResultSuccess.wait(100));
    QCOMPARE(spyResultSuccess.count(), 1);
    QCOMPARE(spyResultProtocolError.count(), 0);
    QCOMPARE(spyResultError.count(), 0);

    QList<QVariant> arguments = spyResultSuccess.takeFirst(); // take the first signal
    QCOMPARE(arguments.count(), 2);


    /* Check start address */
    QCOMPARE(arguments.first().toInt(), 40001);

    /* Check result */
    QVERIFY((arguments[1].canConvert<QList<quint16> >()));
    QList<quint16> resultList = arguments[1].value<QList<quint16> >();
    QCOMPARE(resultList.count(), 2);
    QCOMPARE(resultList[0], static_cast<quint16>(0));
    QCOMPARE(resultList[1], static_cast<quint16>(1));

}
void TestModbusConnection::connectionFail()
{
    ModbusConnection * pConnection = new ModbusConnection(this);

    QSignalSpy spySuccess(pConnection, &ModbusConnection::connectionSuccess);
    QSignalSpy spyError(pConnection, &ModbusConnection::connectionError);

    pConnection->openConnection(_serverConnectionData.host(), _serverConnectionData.port(), 1000);

    QVERIFY(spyError.wait(1500));

    QCOMPARE(spySuccess.count(), 0);
    QCOMPARE(spyError.count(), 1);

    QCOMPARE(pConnection->connectionState(), QModbusDevice::UnconnectedState);
}
void TestModbusConnection::readRequestProtocolError()
{
    /* Start server */
    QVERIFY(_pTestSlaveModbus->connect(_serverConnectionData, _slaveId));

    _pTestSlaveData->setRegisterState(0, false);
    _pTestSlaveData->setRegisterState(1, true);

    _pTestSlaveData->setRegisterValue(0, 0);
    _pTestSlaveData->setRegisterValue(1, 1);

    /* Open connection */
    ModbusConnection * pConnection = new ModbusConnection(this);
    QSignalSpy spySuccess(pConnection, &ModbusConnection::connectionSuccess);
    pConnection->openConnection(_serverConnectionData.host(), _serverConnectionData.port(), 1000);

    pConnection->openConnection(_serverConnectionData.host(), _serverConnectionData.port(), 1000);

    QVERIFY(spySuccess.wait(100));

    QCOMPARE(pConnection->connectionState(), QModbusDevice::ConnectedState);

    QSignalSpy spyResultSuccess(pConnection, &ModbusConnection::readRequestSuccess);
    QSignalSpy spyResultProtocolError(pConnection, &ModbusConnection::readRequestProtocolError);
    QSignalSpy spyResultError(pConnection, &ModbusConnection::readRequestError);

    pConnection->sendReadRequest(40001, 2, _slaveId);

    QVERIFY(spyResultProtocolError.wait(100));
    QCOMPARE(spyResultSuccess.count(), 0);
    QCOMPARE(spyResultProtocolError.count(), 1);
    QCOMPARE(spyResultError.count(), 0);

    QList<QVariant> arguments = spyResultProtocolError.takeFirst(); // take the first signal
    QCOMPARE(arguments.count(), 1);

    /* Check modbus exception */
    QCOMPARE(static_cast<QModbusPdu::ExceptionCode>(arguments.first().toInt()), QModbusPdu::IllegalDataAddress);

}