diff --git a/autotests/CMakeLists.txt b/autotests/CMakeLists.txt index 8911ead..edb9f13 100644 --- a/autotests/CMakeLists.txt +++ b/autotests/CMakeLists.txt @@ -1,50 +1,51 @@ find_package(Qt5 REQUIRED CONFIG COMPONENTS Test) include_directories(${libksysguard_SOURCE_DIR}) if(Qt5WebEngineWidgets_FOUND) # Process unit test ecm_qt_declare_logging_category(processtest_debug_SRCS HEADER processcore_debug.h IDENTIFIER LIBKSYSGUARD_PROCESSCORE CATEGORY_NAME org.kde.libksysguard.processcore) ecm_add_test(processtest.cpp ${processtest_debug_SRCS} TEST_NAME processtest LINK_LIBRARIES KSysGuard::ProcessUi Qt5::Test) endif() if (KF5Plasma_FOUND) set(SIGNALPLOTTER_DEBUG_SRCS) ecm_qt_declare_logging_category(SIGNALPLOTTER_DEBUG_SRCS HEADER ksignalplotter_debug.h IDENTIFIER LIBKSYSGUARD_KSIGNALPLOTTER CATEGORY_NAME org.kde.libksysguard.ksignalplotter) ecm_add_test(signalplotterbenchmark.cpp ../signalplotter/ksignalplotter.cpp ${SIGNALPLOTTER_DEBUG_SRCS} TEST_NAME signalplotterbenchmark LINK_LIBRARIES KSysGuard::SignalPlotter Qt5::Test Qt5::Widgets KF5::IconThemes ) ecm_add_test(graphicssignalplotterbenchmark.cpp ../signalplotter/kgraphicssignalplotter.cpp ${SIGNALPLOTTER_DEBUG_SRCS} TEST_NAME graphicssignalplotterbenchmark LINK_LIBRARIES KSysGuard::SignalPlotter Qt5::Test Qt5::Widgets KF5::IconThemes KF5::Plasma ) ecm_add_test(signalplottertest.cpp ../signalplotter/ksignalplotter.cpp ${SIGNALPLOTTER_DEBUG_SRCS} TEST_NAME signalplottertest LINK_LIBRARIES KSysGuard::SignalPlotter Qt5::Test Qt5::Widgets KF5::IconThemes ) endif() ecm_add_test(chronotest.cpp TEST_NAME chronotest LINK_LIBRARIES Qt5::Test KF5::I18n ) ecm_add_test(formattertest.cpp LINK_LIBRARIES Qt5::Test KSysGuard::Formatter) ecm_add_test(sensortreemodeltest.cpp LINK_LIBRARIES Qt5::Test Qt5::DBus KSysGuard::Sensors) +ecm_add_test(sensordatamodeltest.cpp LINK_LIBRARIES Qt5::Test Qt5::DBus KSysGuard::Sensors) ecm_add_test(processdatamodeltest.cpp LINK_LIBRARIES Qt5::Test KSysGuard::ProcessCore) # set( ksysguarddtest_SRCS ksysguarddtest.cpp ${libksysguard_SOURCE_DIR}/ksgrd/SensorAgent.cpp ${libksysguard_SOURCE_DIR}/ksgrd/SensorManager.cpp ${libksysguard_SOURCE_DIR}/ksgrd/SensorSocketAgent.cpp ${libksysguard_SOURCE_DIR}/ksgrd/SensorShellAgent.cpp) # # ecm_add_test(${ksysguarddtest_SRCS} # TEST_NAME "ksysguard-ksysguarddtest" # LINK_LIBRARIES # Qt5::Test # Qt5::Network # Qt5::Widgets # KF5::ConfigCore # KF5::CoreAddons # KF5::I18n # ) diff --git a/autotests/sensordatamodeltest.cpp b/autotests/sensordatamodeltest.cpp new file mode 100644 index 0000000..4042420 --- /dev/null +++ b/autotests/sensordatamodeltest.cpp @@ -0,0 +1,98 @@ +/* + * Copyright 2020 Arjen Hiemstra + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) version 3, or any + * later version accepted by the membership of KDE e.V. (or its + * successor approved by the membership of KDE e.V.), which shall + * act as a proxy defined in Section 6 of version 3 of the license. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . + */ + +#include +#include + +#include + +#include "SensorDataModel.h" +#include "Unit.h" + +#define qs QStringLiteral + +class SensorDataModelTest : public QObject +{ + Q_OBJECT +private Q_SLOTS: + void initTestCase() + { + QDBusInterface interface{QStringLiteral("org.kde.kstats"), QStringLiteral("/")}; + if (!interface.isValid()) { + QSKIP("KStats Deamon is not running"); + } + } + + void testModel() + { + auto model = new KSysGuard::SensorDataModel(); + auto tester = new QAbstractItemModelTester(model); + + QVERIFY(model->columnCount() == 0); + + model->setSensors({ + qs("cpu/system/TotalLoad"), + qs("mem/physical/allocated"), + qs("network/all/sentDataRate"), + qs("partitions/all/usedspace") + }); + + QTest::qWait(500); // Allow the model to communicate with the daemon. + + QCOMPARE(model->columnCount(), 4); + + auto id = KSysGuard::SensorDataModel::SensorId; + auto unit = KSysGuard::SensorDataModel::Unit; + + QCOMPARE(model->headerData(0, Qt::Horizontal, id).toString(), qs("cpu/system/TotalLoad")); + QCOMPARE(model->headerData(1, Qt::Horizontal, id).toString(), qs("mem/physical/allocated")); + QCOMPARE(model->headerData(2, Qt::Horizontal, id).toString(), qs("network/all/sentDataRate")); + QCOMPARE(model->headerData(3, Qt::Horizontal, id).toString(), qs("partitions/all/usedspace")); + + // Verify that metadata is also loaded correctly. Not using names to sidestep translation issues. + QCOMPARE(model->headerData(0, Qt::Horizontal, unit).toInt(), KSysGuard::UnitPercent); + QCOMPARE(model->headerData(1, Qt::Horizontal, unit).toInt(), KSysGuard::UnitKiloByte); + QCOMPARE(model->headerData(2, Qt::Horizontal, unit).toInt(), KSysGuard::UnitKiloByteRate); + QCOMPARE(model->headerData(3, Qt::Horizontal, unit).toInt(), KSysGuard::UnitKiloByte); + + model->setSensors({ + qs("partitions/all/usedspace"), + qs("network/all/sentDataRate"), + qs("cpu/system/TotalLoad") + }); + + QTest::qWait(500); // As above, give some time for communication. + + QCOMPARE(model->columnCount(), 3); + + QCOMPARE(model->headerData(0, Qt::Horizontal, id).toString(), qs("partitions/all/usedspace")); + QCOMPARE(model->headerData(1, Qt::Horizontal, id).toString(), qs("network/all/sentDataRate")); + QCOMPARE(model->headerData(2, Qt::Horizontal, id).toString(), qs("cpu/system/TotalLoad")); + + // Verify that metadata is also loaded correctly. Not using names to sidestep translation issues. + QCOMPARE(model->headerData(0, Qt::Horizontal, unit).toInt(), KSysGuard::UnitKiloByte); + QCOMPARE(model->headerData(1, Qt::Horizontal, unit).toInt(), KSysGuard::UnitKiloByteRate); + QCOMPARE(model->headerData(2, Qt::Horizontal, unit).toInt(), KSysGuard::UnitPercent); + } +}; + +QTEST_MAIN(SensorDataModelTest); + +#include "sensordatamodeltest.moc"