File: solidfreedesktoptest.cpp

package info (click to toggle)
solid 5.78.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 5,100 kB
  • sloc: cpp: 21,549; xml: 464; lex: 111; yacc: 83; sh: 14; makefile: 5
file content (120 lines) | stat: -rw-r--r-- 3,229 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
    SPDX-FileCopyrightText: 2014 Alejandro Fiestas Olivares <afiestas@kde.org>

    SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/

#include "qtest_dbus.h"
#include "fakeUpower.h"
#include "fakelogind.h"

#include <QTest>
#include <QSignalSpy>
#include <QDBusConnection>
#include <Solid/Power>
#include <Solid/AcPluggedJob>
#include <Solid/Inhibition>
#include <Solid/InhibitionJob>
#include <Solid/StatesJob>
#include <Solid/RequestStateJob>

using namespace Solid;
class solidFreedesktopTest : public QObject
{
    Q_OBJECT
private Q_SLOTS:
    void initTestCase();
    void testAcPluggedJob();
    void testAcPluggedChanged();
    void testAddInhibition();
    void testSupportedStates();
    void testRequestState();

private:
    FakeUpower *m_fakeUPower;
    FakeLogind *m_fakeLogind;
};

void solidFreedesktopTest::initTestCase()
{
    qputenv("SOLID_POWER_BACKEND", "FREE_DESKTOP");

    m_fakeUPower = new FakeUpower(this);
    QDBusConnection::systemBus().registerService(QStringLiteral("org.freedesktop.UPower"));
    QDBusConnection::systemBus().registerObject(QStringLiteral("/org/freedesktop/UPower"), m_fakeUPower, QDBusConnection::ExportAllContents);

    m_fakeLogind = new FakeLogind(this);
    QDBusConnection::systemBus().registerService(QStringLiteral("org.freedesktop.login1"));
    QDBusConnection::systemBus().registerObject(QStringLiteral("/org/freedesktop/login1"), m_fakeLogind, QDBusConnection::ExportAllContents);
}

void solidFreedesktopTest::testAcPluggedJob()
{
    m_fakeUPower->m_onBattery = true;
    auto job = new AcPluggedJob(this);
    QVERIFY(job->exec());
    QCOMPARE(job->isPlugged(), false);

    m_fakeUPower->m_onBattery = false;
    job = Solid::Power::isAcPlugged();
    QVERIFY(job->exec());
    QCOMPARE(job->isPlugged(), true);
}

void solidFreedesktopTest::testAcPluggedChanged()
{
    auto power = Solid::Power::self();
    QSignalSpy spy(power, SIGNAL(acPluggedChanged(bool)));

    m_fakeUPower->setOnBattery(true);
    m_fakeUPower->setOnBattery(false);
    spy.wait(10000);

    QCOMPARE(spy.count(), 2);

    QCOMPARE(spy.first().first().toBool(), false);
    QCOMPARE(spy.at(1).first().toBool(), true);
}

void solidFreedesktopTest::testAddInhibition()
{
    QSignalSpy spy(m_fakeLogind, SIGNAL(newInhibition(QString,QString,QString,QString)));
    auto job = new InhibitionJob(this);
    job->setDescription(QStringLiteral("Foo! I am inhibing!"));
    job->setInhibitions(Power::Shutdown | Power::Sleep);

    job->exec();

    QCOMPARE(spy.count(), 1);

    QSignalSpy spyRemoved(m_fakeLogind, SIGNAL(inhibitionRemoved()));
    auto inhibition = job->inhibition();
    inhibition->stop();

    spyRemoved.wait(100);
    QCOMPARE(spy.count(), 1);

    QSignalSpy spyStatusChanged(inhibition, SIGNAL(stateChanged(Inhibition::State)));
    inhibition->start();
    spyStatusChanged.wait(100);

    QCOMPARE(spyStatusChanged.count(), 1);

    delete inhibition;
    spyRemoved.wait(100);
    QCOMPARE(spyRemoved.count(), 2);
}

void solidFreedesktopTest::testSupportedStates()
{

}

void solidFreedesktopTest::testRequestState()
{

}

QTEST_GUILESS_MAIN_SYSTEM_DBUS(solidFreedesktopTest)

#include "solidfreedesktoptest.moc"