|
| 1 | +#include "tst_modbuspoll.h" |
| 2 | + |
| 3 | +#include "communication/modbuspoll.h" |
| 4 | +#include "models/settingsmodel.h" |
| 5 | + |
| 6 | +#include <QLoggingCategory> |
| 7 | +#include <QTest> |
| 8 | + |
| 9 | +namespace |
| 10 | +{ |
| 11 | + QtMsgType g_capturedType{}; |
| 12 | + QString g_capturedMessage{}; |
| 13 | + |
| 14 | + void captureHandler(QtMsgType type, const QMessageLogContext&, const QString& msg) |
| 15 | + { |
| 16 | + g_capturedType = type; |
| 17 | + g_capturedMessage = msg; |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +void TestModbusPoll::init() |
| 22 | +{ |
| 23 | + _pSettingsModel = new SettingsModel; |
| 24 | + _pModbusPoll = new ModbusPoll(_pSettingsModel); |
| 25 | + /* Enable debug output for scope.comm so qCDebug calls reach the handler */ |
| 26 | + QLoggingCategory::setFilterRules(QStringLiteral("scope.comm.debug=true")); |
| 27 | +} |
| 28 | + |
| 29 | +void TestModbusPoll::cleanup() |
| 30 | +{ |
| 31 | + QLoggingCategory::setFilterRules(QString()); |
| 32 | + delete _pModbusPoll; |
| 33 | + delete _pSettingsModel; |
| 34 | +} |
| 35 | + |
| 36 | +void TestModbusPoll::diagnosticDebugLevel() |
| 37 | +{ |
| 38 | + QtMessageHandler previous = qInstallMessageHandler(captureHandler); |
| 39 | + QMetaObject::invokeMethod(_pModbusPoll, "onAdapterDiagnostic", |
| 40 | + Q_ARG(QString, QStringLiteral("debug")), |
| 41 | + Q_ARG(QString, QStringLiteral("polling started"))); |
| 42 | + qInstallMessageHandler(previous); |
| 43 | + |
| 44 | + QCOMPARE(g_capturedType, QtDebugMsg); |
| 45 | + QVERIFY(g_capturedMessage.contains(QStringLiteral("polling started"))); |
| 46 | +} |
| 47 | + |
| 48 | +void TestModbusPoll::diagnosticInfoLevel() |
| 49 | +{ |
| 50 | + QtMessageHandler previous = qInstallMessageHandler(captureHandler); |
| 51 | + QMetaObject::invokeMethod(_pModbusPoll, "onAdapterDiagnostic", |
| 52 | + Q_ARG(QString, QStringLiteral("info")), |
| 53 | + Q_ARG(QString, QStringLiteral("session active"))); |
| 54 | + qInstallMessageHandler(previous); |
| 55 | + |
| 56 | + QCOMPARE(g_capturedType, QtInfoMsg); |
| 57 | + QVERIFY(g_capturedMessage.contains(QStringLiteral("session active"))); |
| 58 | +} |
| 59 | + |
| 60 | +void TestModbusPoll::diagnosticWarningLevel() |
| 61 | +{ |
| 62 | + QtMessageHandler previous = qInstallMessageHandler(captureHandler); |
| 63 | + QMetaObject::invokeMethod(_pModbusPoll, "onAdapterDiagnostic", |
| 64 | + Q_ARG(QString, QStringLiteral("warning")), |
| 65 | + Q_ARG(QString, QStringLiteral("register read failed"))); |
| 66 | + qInstallMessageHandler(previous); |
| 67 | + |
| 68 | + QCOMPARE(g_capturedType, QtWarningMsg); |
| 69 | + QVERIFY(g_capturedMessage.contains(QStringLiteral("register read failed"))); |
| 70 | +} |
| 71 | + |
| 72 | +void TestModbusPoll::diagnosticUnknownLevel() |
| 73 | +{ |
| 74 | + QtMessageHandler previous = qInstallMessageHandler(captureHandler); |
| 75 | + QMetaObject::invokeMethod(_pModbusPoll, "onAdapterDiagnostic", |
| 76 | + Q_ARG(QString, QStringLiteral("critical")), |
| 77 | + Q_ARG(QString, QStringLiteral("unexpected error"))); |
| 78 | + qInstallMessageHandler(previous); |
| 79 | + |
| 80 | + QCOMPARE(g_capturedType, QtWarningMsg); |
| 81 | + QVERIFY(g_capturedMessage.contains(QStringLiteral("unknown diagnostic level"))); |
| 82 | +} |
| 83 | + |
| 84 | +QTEST_GUILESS_MAIN(TestModbusPoll) |
0 commit comments