File indexing completed on 2024-04-14 03:58:06

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 <QSignalSpy>
0008 #include <QTest>
0009 #include <Solid/AcPluggedJob>
0010 #include <Solid/Inhibition>
0011 #include <Solid/InhibitionJob>
0012 #include <Solid/Power>
0013 #include <Solid/RequestStateJob>
0014 #include <Solid/StatesJob>
0015 
0016 using namespace Solid;
0017 class solidPowerTest : public QObject
0018 {
0019     Q_OBJECT
0020 private Q_SLOTS:
0021     void initTestCase();
0022     void testAcPluggedJob();
0023     void testAcPluggedChanged();
0024     void testAddInhibition();
0025     void testSupportedStates();
0026     void testRequestState();
0027 };
0028 
0029 void solidPowerTest::initTestCase()
0030 {
0031     qputenv("SOLID_POWER_BACKEND", "DUMMY");
0032 }
0033 
0034 void solidPowerTest::testAcPluggedJob()
0035 {
0036     AcPluggedJob *job = new AcPluggedJob();
0037     QVERIFY(job->exec());
0038     QVERIFY(job->isPlugged());
0039 
0040     job = Power::isAcPlugged();
0041     QVERIFY(job->exec());
0042     QVERIFY(job->isPlugged());
0043 }
0044 
0045 void solidPowerTest::testAcPluggedChanged()
0046 {
0047     Power *power = Power::self();
0048     QSignalSpy spy(power, SIGNAL(acPluggedChanged(bool)));
0049 
0050     QVERIFY(spy.wait());
0051     QVERIFY(spy.takeFirst().first().toBool());
0052 }
0053 
0054 void solidPowerTest::testAddInhibition()
0055 {
0056     InhibitionJob *job = new InhibitionJob();
0057     QVERIFY(!job->exec());
0058 
0059     QCOMPARE(job->error(), (int)InhibitionJob::InvalidInhibitions);
0060     delete job;
0061 
0062     job = new InhibitionJob();
0063     job->setInhibitions(Power::Sleep);
0064     QVERIFY(!job->exec());
0065     QCOMPARE(job->error(), (int)InhibitionJob::EmptyDescription);
0066     delete job;
0067 
0068     job = Power::inhibit(Power::Sleep, QLatin1String("Running a test, we don't want to suspend now"));
0069     QVERIFY(job->exec());
0070 
0071     auto inhibition = job->inhibition();
0072     QSignalSpy spy(inhibition, SIGNAL(stateChanged(Inhibition::State)));
0073     inhibition->deleteLater();
0074 
0075     QVERIFY(spy.wait());
0076     QCOMPARE(spy.takeFirst().first().toInt(), (int)Inhibition::Stopped);
0077 }
0078 
0079 void solidPowerTest::testSupportedStates()
0080 {
0081     auto job = new StatesJob();
0082     QVERIFY(job->exec());
0083 
0084     QCOMPARE(job->states(), Power::Shutdown | Power::Sleep);
0085 
0086     job = Power::supportedStates();
0087     QVERIFY(job->exec());
0088 
0089     QCOMPARE(job->states(), Power::Shutdown | Power::Sleep);
0090 }
0091 
0092 void solidPowerTest::testRequestState()
0093 {
0094     auto job = new RequestStateJob();
0095     job->setState(Power::Sleep);
0096     QVERIFY(job->exec());
0097 
0098     job = Power::requestState(Power::Sleep);
0099     QVERIFY(job->exec());
0100 
0101     job = Power::requestState(Power::Shutdown);
0102     QVERIFY(!job->exec());
0103 
0104     QCOMPARE(job->error(), (int)RequestStateJob::Unsupported);
0105     QCOMPARE(job->errorText(), QLatin1String(QLatin1String("State Brightness is unsupported")));
0106 }
0107 
0108 QTEST_MAIN(solidPowerTest)
0109 
0110 #include "solidpowertest.moc"