File indexing completed on 2024-05-12 09:36:20
0001 /* 0002 SPDX-FileCopyrightText: 2020 Arjen Hiemstra <ahiemstra@heimr.nl> 0003 0004 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0005 */ 0006 0007 #include <QAbstractItemModelTester> 0008 #include <QTest> 0009 #include <QTransposeProxyModel> 0010 0011 #include <QDBusInterface> 0012 0013 #include "SensorDataModel.h" 0014 #include "Unit.h" 0015 0016 #define qs QStringLiteral 0017 0018 class SensorDataModelTest : public QObject 0019 { 0020 Q_OBJECT 0021 private Q_SLOTS: 0022 void initTestCase() 0023 { 0024 QDBusInterface interface{QStringLiteral("org.kde.ksystemstats"), QStringLiteral("/")}; 0025 if (!interface.isValid()) { 0026 QSKIP("KSystemStats Daemon is not running"); 0027 } 0028 } 0029 0030 void testModel() 0031 { 0032 KSysGuard::SensorDataModel model; 0033 QAbstractItemModelTester tester{&model}; 0034 Q_UNUSED(tester) 0035 0036 QVERIFY(model.columnCount() == 0); 0037 0038 model.setSensors({qs("cpu/all/usage"), qs("memory/physical/used"), qs("network/all/download"), qs("disk/all/used")}); 0039 0040 QTRY_VERIFY(model.isReady()); 0041 0042 QCOMPARE(model.columnCount(), 4); 0043 0044 auto id = KSysGuard::SensorDataModel::SensorId; 0045 auto unit = KSysGuard::SensorDataModel::Unit; 0046 0047 QCOMPARE(model.headerData(0, Qt::Horizontal, id).toString(), qs("cpu/all/usage")); 0048 QCOMPARE(model.headerData(1, Qt::Horizontal, id).toString(), qs("memory/physical/used")); 0049 QCOMPARE(model.headerData(2, Qt::Horizontal, id).toString(), qs("network/all/download")); 0050 QCOMPARE(model.headerData(3, Qt::Horizontal, id).toString(), qs("disk/all/used")); 0051 0052 // Verify that metadata is also loaded correctly. Not using names to sidestep translation issues. 0053 QCOMPARE(model.headerData(0, Qt::Horizontal, unit).value<KSysGuard::Unit>(), KSysGuard::UnitPercent); 0054 QCOMPARE(model.headerData(1, Qt::Horizontal, unit).value<KSysGuard::Unit>(), KSysGuard::UnitByte); 0055 QCOMPARE(model.headerData(2, Qt::Horizontal, unit).value<KSysGuard::Unit>(), KSysGuard::UnitByteRate); 0056 QCOMPARE(model.headerData(3, Qt::Horizontal, unit).value<KSysGuard::Unit>(), KSysGuard::UnitByte); 0057 0058 model.setSensors({qs("disk/all/used"), qs("network/all/download"), qs("cpu/all/usage")}); 0059 0060 QVERIFY(!model.isReady()); 0061 0062 QTRY_VERIFY(model.isReady()); 0063 0064 QCOMPARE(model.columnCount(), 3); 0065 0066 QCOMPARE(model.headerData(0, Qt::Horizontal, id).toString(), qs("disk/all/used")); 0067 QCOMPARE(model.headerData(1, Qt::Horizontal, id).toString(), qs("network/all/download")); 0068 QCOMPARE(model.headerData(2, Qt::Horizontal, id).toString(), qs("cpu/all/usage")); 0069 0070 // Verify that metadata is also loaded correctly. Not using names to sidestep translation issues. 0071 QCOMPARE(model.headerData(0, Qt::Horizontal, unit).value<KSysGuard::Unit>(), KSysGuard::UnitByte); 0072 QCOMPARE(model.headerData(1, Qt::Horizontal, unit).value<KSysGuard::Unit>(), KSysGuard::UnitByteRate); 0073 QCOMPARE(model.headerData(2, Qt::Horizontal, unit).value<KSysGuard::Unit>(), KSysGuard::UnitPercent); 0074 } 0075 0076 void testTransposeProxy() 0077 { 0078 KSysGuard::SensorDataModel model; 0079 QAbstractItemModelTester tester{&model}; 0080 Q_UNUSED(tester) 0081 0082 QTransposeProxyModel transpose; 0083 transpose.setSourceModel(&model); 0084 0085 model.setSensors({qs("cpu/all/usage"), qs("memory/physical/used"), qs("network/all/download"), qs("disk/all/used")}); 0086 0087 QTRY_VERIFY(model.isReady()); 0088 0089 QCOMPARE(model.columnCount(), transpose.rowCount()); 0090 QCOMPARE(model.rowCount(), transpose.columnCount()); 0091 } 0092 }; 0093 0094 QTEST_MAIN(SensorDataModelTest); 0095 0096 #include "sensordatamodeltest.moc"