Commit 25657e31 authored by Àlex Fiestas's avatar Àlex Fiestas

Implement testAddInhibitionJob and freedesktop code

We have created a fakeLogind dbus daemon to do a component test of
InhibitionJob which emits a "newInhibition" signal when the dbus method
gets called.

Also, there is code prepared to return proper file descriptors and to
test the Inhibition object further, but that part is still not done.
parent eff05d49
......@@ -38,6 +38,6 @@ endif()
########### solidpowertest ###############
if(WITH_NEW_POWER_ASYNC_API AND WITH_NEW_POWER_ASYNC_FREEDESKTOP)
set(solidFreedesktopTest_SRCS solidfreedesktoptest.cpp fakeUpower.cpp)
set(solidFreedesktopTest_SRCS solidfreedesktoptest.cpp fakeUpower.cpp fakelogind.cpp)
ecm_add_test(${solidFreedesktopTest_SRCS} TEST_NAME "solidfreedesktopbackend" LINK_LIBRARIES Qt5::Test Qt5::DBus ${LIBS} KF5Solid_static)
endif()
\ No newline at end of file
/*
Copyright 2014 Alejandro Fiestas Olivares <afiestas@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) version 3, or any
later version accepted by the membership of KDE e.V. (or its
successor approved by the membership of KDE e.V.), which shall
act as a proxy defined in Section 6 of version 3 of the license.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "fakelogind.h"
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <QDebug>
#include <qtimer.h>
FakeLogind::FakeLogind(QObject* parent) : QObject(parent)
{
}
QDBusUnixFileDescriptor FakeLogind::Inhibit(const QString& what, const QString& who, const QString& why, const QString& mode)
{
Q_EMIT newInhibition(what, who, why, mode);
// m_fd = open("/tmp/rolf", O_WRONLY|O_CLOEXEC|O_NDELAY);
// qDebug() << "Sending" << m_fd;
QDBusUnixFileDescriptor foo;
foo.giveFileDescriptor(33);
/*
auto t = new QTimer();
t->setInterval(100);
connect(t, SIGNAL(timeout()), SLOT(checkFd()));
t->start();*/
return foo;
}
void FakeLogind::checkFd()
{
int e = fcntl(m_fd, F_GETFD) != -1 || errno != EBADF;
if (e == 0) {
delete sender();
}
}
\ No newline at end of file
/*
Copyright 2014 Alejandro Fiestas Olivares <afiestas@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) version 3, or any
later version accepted by the membership of KDE e.V. (or its
successor approved by the membership of KDE e.V.), which shall
act as a proxy defined in Section 6 of version 3 of the license.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SOLID_FAKE_LOGIND_H
#define SOLID_FAKE_LOGIND_H
#include <QList>
#include <QString>
#include <QDBusUnixFileDescriptor>
#include <QDBusObjectPath>
class FakeLogind : public QObject
{
Q_OBJECT
Q_CLASSINFO("D-Bus Interface", "org.freedesktop.login1.Manager")
public:
explicit FakeLogind(QObject* parent);
public Q_SLOTS:
QDBusUnixFileDescriptor Inhibit(const QString &what, const QString &who, const QString &why, const QString &mode);
void checkFd();
Q_SIGNALS:
void fdClosed();
void newInhibition(const QString &what, const QString &who, const QString &why, const QString &mode);
private:
int m_fd;
};
#endif //SOLID_FAKE_LOGIND_H
\ No newline at end of file
......@@ -20,6 +20,7 @@
#include "qtest_dbus.h"
#include "fakeUpower.h"
#include "fakelogind.h"
#include <QTest>
#include <QDebug>
......@@ -46,6 +47,7 @@ private Q_SLOTS:
private:
FakeUpower *m_fakeUPower;
FakeLogind *m_fakeLogind;
};
void solidFreedesktopTest::initTestCase()
......@@ -55,6 +57,10 @@ void solidFreedesktopTest::initTestCase()
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()
......@@ -87,7 +93,15 @@ void solidFreedesktopTest::testAcPluggedChanged()
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::Hibernation | Power::Sleep);
job->exec();
spy.wait(100);
QCOMPARE(spy.count(), 1);
}
void solidFreedesktopTest::testSupportedStates()
......
......@@ -4,4 +4,6 @@ set(solid_LIB_SRCS
${solid_LIB_SRCS}
power/backends/freedesktop/fdacpluggedjob.cpp
power/backends/freedesktop/fdpowernotifier.cpp
power/backends/freedesktop/fdinhibitionjob.cpp
power/backends/freedesktop/fdinhibition.cpp
)
/*
Copyright 2014 Alejandro Fiestas Olivares <afiestas@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) version 3, or any
later version accepted by the membership of KDE e.V. (or its
successor approved by the membership of KDE e.V.), which shall
act as a proxy defined in Section 6 of version 3 of the license.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "fdinhibition.h"
#include <unistd.h>
#include <QVariant>
#include <QDebug>
#include <QApplication>
#include <QDBusConnection>
#include <QDBusUnixFileDescriptor>
using namespace Solid;
FdInhibition::FdInhibition(Power::States inhibitions, const QString &description, QObject* parent)
: AbstractInhibition(parent)
, m_state(Inhibition::Stopped)
, m_description(description)
, m_inhibitions(inhibitions)
{
}
FdInhibition::~FdInhibition()
{
close(m_fd);
}
void FdInhibition::start()
{
QDBusMessage msg = QDBusMessage::createMethodCall(
QStringLiteral("org.freedesktop.login1"),
QStringLiteral("/org/freedesktop/login1"),
QStringLiteral("org.freedesktop.login1.Manager"),
QStringLiteral("Inhibit"));
QList<QVariant> args;
args << QStringLiteral("shutdown")
<< qApp->applicationName()
<< m_description
<< QStringLiteral("block");
msg.setArguments(args);
QDBusConnection::systemBus().callWithCallback(msg, this, SLOT(slotDBusReply(QDBusMessage)), SLOT(slotDBusError(QDBusError)));
}
void FdInhibition::stop()
{
close(m_fd);
setState(Inhibition::Stopped);
}
void FdInhibition::setDescription(const QString& description)
{
m_description = description;
}
void FdInhibition::slotDBusReply(const QDBusMessage& msg)
{
m_fd = msg.arguments().first().value<QDBusUnixFileDescriptor>().fileDescriptor();
setState(Inhibition::Started);
}
void FdInhibition::slotDBusError(const QDBusError& error)
{
qDebug() << error.message();
}
void FdInhibition::setState(const Inhibition::State& state)
{
if (m_state == state) {
return;
}
m_state = state;
Q_EMIT stateChanged(m_state);
}
Inhibition::State FdInhibition::state() const
{
return m_state;
}
/*
Copyright 2014 Alejandro Fiestas Olivares <afiestas@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) version 3, or any
later version accepted by the membership of KDE e.V. (or its
successor approved by the membership of KDE e.V.), which shall
act as a proxy defined in Section 6 of version 3 of the license.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SOLID_FD_INHIBITION_H
#define SOLID_FD_INHIBITION_H
#include "backends/abstractinhibition.h"
#include "inhibition.h"
#include "solid/power.h"
#include <qglobal.h>
#include <QDBusMessage>
namespace Solid
{
class FdInhibition : public AbstractInhibition
{
Q_OBJECT
public:
explicit FdInhibition(Power::States inhibitions, const QString &description, QObject* parent = 0);
virtual ~FdInhibition();
virtual void start() Q_DECL_OVERRIDE;
virtual void stop() Q_DECL_OVERRIDE;
virtual Inhibition::State state() const Q_DECL_OVERRIDE;
void setDescription(const QString &description);
private Q_SLOTS:
void slotDBusReply(const QDBusMessage &msg);
void slotDBusError(const QDBusError &error);
private:
void setState(const Inhibition::State &state);
int m_fd;
Inhibition::State m_state;
QString m_description;
Power::States m_inhibitions;
};
}
#endif //SOLID_FD_INHIBITION_H
\ No newline at end of file
/*
Copyright 2014 Alejandro Fiestas Olivares <afiestas@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) version 3, or any
later version accepted by the membership of KDE e.V. (or its
successor approved by the membership of KDE e.V.), which shall
act as a proxy defined in Section 6 of version 3 of the license.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "fdinhibitionjob.h"
#include "fdinhibition.h"
#include <QCoreApplication>
#include <QVariant>
#include <QDebug>
#include <unistd.h>
using namespace Solid;
FdInhibitionJob::FdInhibitionJob(Power::States inhibitions, const QString &description, QObject* parent)
: AbstractInhibitionJob(parent)
, m_inhibition(new FdInhibition(inhibitions, description))
{
}
FdInhibitionJob::~FdInhibitionJob()
{
}
void FdInhibitionJob::doStart()
{
connect(m_inhibition, &FdInhibition::stateChanged, this, &FdInhibitionJob::stateChanged);
m_inhibition->start();
}
void FdInhibitionJob::stateChanged(Inhibition::State state)
{
Q_UNUSED(state);
disconnect(this, SLOT(stateChanged(Inhibition::State)));
emitResult();
}
Inhibition* FdInhibitionJob::inhibition()
{
return new Inhibition(m_inhibition);
}
\ No newline at end of file
/*
Copyright 2014 Alejandro Fiestas Olivares <afiestas@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) version 3, or any
later version accepted by the membership of KDE e.V. (or its
successor approved by the membership of KDE e.V.), which shall
act as a proxy defined in Section 6 of version 3 of the license.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SOLID_FD_ADD_INHIBITION_JOB_H
#define SOLID_FD_ADD_INHIBITION_JOB_H
#include "backends/abstractinhibitionjob.h"
#include "solid/power.h"
#include "solid/inhibition.h"
#include <QDBusMessage>
#include <QDBusError>
namespace Solid
{
class Inhibition;
class FdInhibition;
class FdInhibitionJob : public AbstractInhibitionJob
{
Q_OBJECT
public:
explicit FdInhibitionJob(Power::States inhibitions, const QString &description, QObject* parent = 0);
virtual ~FdInhibitionJob();
virtual Inhibition* inhibition() Q_DECL_OVERRIDE;
private Q_SLOTS:
virtual void doStart() Q_DECL_OVERRIDE;
void stateChanged(Inhibition::State state);
private:
FdInhibition *m_inhibition;
};
}
#endif //SOLID_FD_ADD_INHIBITION_JOB_H
\ No newline at end of file
......@@ -27,6 +27,7 @@
#include "backends/dummy/dummyrequeststatejob.h"
#include "backends/freedesktop/fdacpluggedjob.h"
#include "backends/freedesktop/fdpowernotifier.h"
#include "backends/freedesktop/fdinhibitionjob.h"
using namespace Solid;
......@@ -40,7 +41,10 @@ AbstractAcPluggedJob* PowerBackendLoader::AcPluggedJob()
AbstractInhibitionJob* PowerBackendLoader::addInhibitionJob(Power::States inhibitions, const QString &description)
{
return new DummyInhibitionJob(inhibitions, description);
if (qgetenv("SOLID_POWER_BACKEND") == "DUMMY") {
return new DummyInhibitionJob(inhibitions, description);
}
return new FdInhibitionJob(inhibitions, description);
}
AbstractStatesJob* PowerBackendLoader::statesJob()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment