File indexing completed on 2024-04-28 05:27:45
0001 /* 0002 SPDX-FileCopyrightText: 2020 David Edmundson <davidedmundson@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 0007 #include <QTest> 0008 #include <QSignalSpy> 0009 0010 #include "../src/daemon.h" 0011 0012 #include <systemstats/SensorContainer.h> 0013 #include <systemstats/SensorObject.h> 0014 #include <systemstats/SensorPlugin.h> 0015 #include <systemstats/DBusInterface.h> 0016 0017 #include <QDBusConnection> 0018 #include <QDBusMetaType> 0019 0020 class TestPlugin : public KSysGuard::SensorPlugin 0021 { 0022 public: 0023 TestPlugin(QObject *parent) 0024 : SensorPlugin(parent, {}) 0025 { 0026 m_testContainer = new KSysGuard::SensorContainer("testContainer", "Test Container", this); 0027 m_testObject = new KSysGuard::SensorObject("testObject", "Test Object", m_testContainer); 0028 m_property1 = new KSysGuard::SensorProperty("property1", m_testObject); 0029 m_property1->setMin(0); 0030 m_property1->setMax(100); 0031 m_property1->setShortName("Some Sensor 1"); 0032 m_property1->setName("Some Sensor Name 1"); 0033 0034 m_property2 = new KSysGuard::SensorProperty("property2", m_testObject); 0035 } 0036 QString providerName() const override 0037 { 0038 return "testPlugin"; 0039 } 0040 void update() override 0041 { 0042 m_updateCount++; 0043 } 0044 KSysGuard::SensorContainer *m_testContainer; 0045 KSysGuard::SensorObject *m_testObject; 0046 KSysGuard::SensorProperty *m_property1; 0047 KSysGuard::SensorProperty *m_property2; 0048 int m_updateCount = 0; 0049 }; 0050 0051 class KStatsTest : public Daemon 0052 { 0053 Q_OBJECT 0054 public: 0055 KStatsTest(); 0056 0057 protected: 0058 void loadProviders() override; 0059 private Q_SLOTS: 0060 void initTestCase(); 0061 void findById(); 0062 void update(); 0063 void subscription(); 0064 void changes(); 0065 void dbusApi(); 0066 0067 private: 0068 TestPlugin *m_testPlugin = nullptr; 0069 }; 0070 0071 KStatsTest::KStatsTest() 0072 { 0073 qDBusRegisterMetaType<KSysGuard::SensorData>(); 0074 qDBusRegisterMetaType<KSysGuard::SensorInfo>(); 0075 qDBusRegisterMetaType<KSysGuard::SensorDataList>(); 0076 qDBusRegisterMetaType<QHash<QString, KSysGuard::SensorInfo>>(); 0077 qDBusRegisterMetaType<QStringList>(); 0078 } 0079 0080 void KStatsTest::loadProviders() 0081 { 0082 m_testPlugin = new TestPlugin(this); 0083 registerProvider(m_testPlugin); 0084 0085 // Sensor registration in containers uses queued connections, trigger those 0086 QTest::qWait(0); 0087 } 0088 0089 void KStatsTest::initTestCase() 0090 { 0091 QDBusConnection::sessionBus().registerObject(KSysGuard::SystemStats::ObjectPath, this, QDBusConnection::ExportAdaptors); 0092 loadProviders(); 0093 } 0094 0095 void KStatsTest::findById() 0096 { 0097 QVERIFY(findSensor("testContainer/testObject/property1")); 0098 QVERIFY(findSensor("testContainer/testObject/property2")); 0099 QVERIFY(!findSensor("testContainer/asdfasdfasfs/property1")); 0100 } 0101 0102 void KStatsTest::update() 0103 { 0104 QCOMPARE(m_testPlugin->m_updateCount, 0); 0105 sendFrame(); 0106 QCOMPARE(m_testPlugin->m_updateCount, 1); 0107 } 0108 0109 void KStatsTest::subscription() 0110 { 0111 QSignalSpy property1Subscribed(m_testPlugin->m_property1, &KSysGuard::SensorProperty::subscribedChanged); 0112 QSignalSpy property2Subscribed(m_testPlugin->m_property2, &KSysGuard::SensorProperty::subscribedChanged); 0113 QSignalSpy objectSubscribed(m_testPlugin->m_testObject, &KSysGuard::SensorObject::subscribedChanged); 0114 0115 m_testPlugin->m_property1->subscribe(); 0116 QCOMPARE(property1Subscribed.count(), 1); 0117 QCOMPARE(objectSubscribed.count(), 1); 0118 0119 m_testPlugin->m_property1->subscribe(); 0120 QCOMPARE(property1Subscribed.count(), 1); 0121 QCOMPARE(objectSubscribed.count(), 1); 0122 0123 m_testPlugin->m_property2->subscribe(); 0124 QCOMPARE(objectSubscribed.count(), 1); 0125 0126 m_testPlugin->m_property1->unsubscribe(); 0127 QCOMPARE(property1Subscribed.count(), 1); 0128 m_testPlugin->m_property1->unsubscribe(); 0129 QCOMPARE(property1Subscribed.count(), 2); 0130 } 0131 0132 void KStatsTest::changes() 0133 { 0134 QSignalSpy property1Changed(m_testPlugin->m_property1, &KSysGuard::SensorProperty::valueChanged); 0135 m_testPlugin->m_property1->setValue(14); 0136 QCOMPARE(property1Changed.count(), 1); 0137 QCOMPARE(m_testPlugin->m_property1->value(), QVariant(14)); 0138 } 0139 0140 void KStatsTest::dbusApi() 0141 { 0142 KSysGuard::SystemStats::DBusInterface iface(QDBusConnection::sessionBus().baseService(), 0143 KSysGuard::SystemStats::ObjectPath, 0144 QDBusConnection::sessionBus(), 0145 this); 0146 // list all objects 0147 auto pendingSensors = iface.allSensors(); 0148 pendingSensors.waitForFinished(); 0149 auto sensors = pendingSensors.value(); 0150 QVERIFY(sensors.count() == 4); 0151 0152 // test metadata 0153 QCOMPARE(sensors["testContainer/testObject/property1"].name, "Some Sensor Name 1"); 0154 0155 // query value 0156 m_testPlugin->m_property1->setValue(100); 0157 0158 auto pendingValues = iface.sensorData({ "testContainer/testObject/property1" }); 0159 pendingValues.waitForFinished(); 0160 QCOMPARE(pendingValues.value().first().sensorProperty, "testContainer/testObject/property1"); 0161 QCOMPARE(pendingValues.value().first().payload.toInt(), 100); 0162 0163 // change updates 0164 QSignalSpy changesSpy(&iface, &KSysGuard::SystemStats::DBusInterface::newSensorData); 0165 0166 iface.subscribe({ "testContainer/testObject/property1" }); 0167 0168 sendFrame(); 0169 // a frame with no changes, does nothing 0170 QVERIFY(!changesSpy.wait(20)); 0171 0172 m_testPlugin->m_property1->setValue(101); 0173 // an update does nothing till it gets a frame, in order to batch 0174 QVERIFY(!changesSpy.wait(20)); 0175 sendFrame(); 0176 0177 QVERIFY(changesSpy.wait(20)); 0178 QCOMPARE(changesSpy.first().first().value<KSysGuard::SensorDataList>().first().sensorProperty, "testContainer/testObject/property1"); 0179 QCOMPARE(changesSpy.first().first().value<KSysGuard::SensorDataList>().first().payload, QVariant(101)); 0180 0181 // we're not subscribed to property 2 so if that updates we should not get anything 0182 m_testPlugin->m_property2->setValue(102); 0183 sendFrame(); 0184 QVERIFY(!changesSpy.wait(20)); 0185 } 0186 0187 QTEST_GUILESS_MAIN(KStatsTest) 0188 0189 #include "main.moc"