Warning, file /plasma/ksystemstats/autotests/main.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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