File indexing completed on 2024-10-13 03:42:25

0001 /*
0002     SPDX-FileCopyrightText: 2014 Alejandro Fiestas Olivares <afiestas@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include "fakeUpower.h"
0008 #include "fakelogind.h"
0009 #include "qtest_dbus.h"
0010 
0011 #include <QDBusConnection>
0012 #include <QSignalSpy>
0013 #include <QTest>
0014 #include <Solid/AcPluggedJob>
0015 #include <Solid/Inhibition>
0016 #include <Solid/InhibitionJob>
0017 #include <Solid/Power>
0018 #include <Solid/RequestStateJob>
0019 #include <Solid/StatesJob>
0020 
0021 using namespace Solid;
0022 class solidFreedesktopTest : public QObject
0023 {
0024     Q_OBJECT
0025 private Q_SLOTS:
0026     void initTestCase();
0027     void testAcPluggedJob();
0028     void testAcPluggedChanged();
0029     void testAddInhibition();
0030     void testSupportedStates();
0031     void testRequestState();
0032 
0033 private:
0034     FakeUpower *m_fakeUPower;
0035     FakeLogind *m_fakeLogind;
0036 };
0037 
0038 void solidFreedesktopTest::initTestCase()
0039 {
0040     qputenv("SOLID_POWER_BACKEND", "FREE_DESKTOP");
0041 
0042     m_fakeUPower = new FakeUpower(this);
0043     QDBusConnection::systemBus().registerService(QStringLiteral("org.freedesktop.UPower"));
0044     QDBusConnection::systemBus().registerObject(QStringLiteral("/org/freedesktop/UPower"), m_fakeUPower, QDBusConnection::ExportAllContents);
0045 
0046     m_fakeLogind = new FakeLogind(this);
0047     QDBusConnection::systemBus().registerService(QStringLiteral("org.freedesktop.login1"));
0048     QDBusConnection::systemBus().registerObject(QStringLiteral("/org/freedesktop/login1"), m_fakeLogind, QDBusConnection::ExportAllContents);
0049 }
0050 
0051 void solidFreedesktopTest::testAcPluggedJob()
0052 {
0053     m_fakeUPower->m_onBattery = true;
0054     auto job = new AcPluggedJob(this);
0055     QVERIFY(job->exec());
0056     QCOMPARE(job->isPlugged(), false);
0057 
0058     m_fakeUPower->m_onBattery = false;
0059     job = Solid::Power::isAcPlugged();
0060     QVERIFY(job->exec());
0061     QCOMPARE(job->isPlugged(), true);
0062 }
0063 
0064 void solidFreedesktopTest::testAcPluggedChanged()
0065 {
0066     auto power = Solid::Power::self();
0067     QSignalSpy spy(power, SIGNAL(acPluggedChanged(bool)));
0068 
0069     m_fakeUPower->setOnBattery(true);
0070     m_fakeUPower->setOnBattery(false);
0071     spy.wait(10000);
0072 
0073     QCOMPARE(spy.count(), 2);
0074 
0075     QCOMPARE(spy.first().first().toBool(), false);
0076     QCOMPARE(spy.at(1).first().toBool(), true);
0077 }
0078 
0079 void solidFreedesktopTest::testAddInhibition()
0080 {
0081     QSignalSpy spy(m_fakeLogind, SIGNAL(newInhibition(QString, QString, QString, QString)));
0082     auto job = new InhibitionJob(this);
0083     job->setDescription(QStringLiteral("Foo! I am inhibing!"));
0084     job->setInhibitions(Power::Shutdown | Power::Sleep);
0085 
0086     job->exec();
0087 
0088     QCOMPARE(spy.count(), 1);
0089 
0090     QSignalSpy spyRemoved(m_fakeLogind, SIGNAL(inhibitionRemoved()));
0091     auto inhibition = job->inhibition();
0092     inhibition->stop();
0093 
0094     spyRemoved.wait(100);
0095     QCOMPARE(spy.count(), 1);
0096 
0097     QSignalSpy spyStatusChanged(inhibition, SIGNAL(stateChanged(Inhibition::State)));
0098     inhibition->start();
0099     spyStatusChanged.wait(100);
0100 
0101     QCOMPARE(spyStatusChanged.count(), 1);
0102 
0103     delete inhibition;
0104     spyRemoved.wait(100);
0105     QCOMPARE(spyRemoved.count(), 2);
0106 }
0107 
0108 void solidFreedesktopTest::testSupportedStates()
0109 {
0110 }
0111 
0112 void solidFreedesktopTest::testRequestState()
0113 {
0114 }
0115 
0116 QTEST_GUILESS_MAIN_SYSTEM_DBUS(solidFreedesktopTest)
0117 
0118 #include "solidfreedesktoptest.moc"