Commit 875357e1 authored by Àlex Fiestas's avatar Àlex Fiestas

Test Inhibition->stop and ~Inhibition

On both cases we want to properly stop the inhibition which in the case
of systemd it means closing the file descriptor we got via DBus.
parent 45f5d268
......@@ -23,9 +23,11 @@
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <QtCore/qtemporaryfile.h>
#include <QDebug>
#include <qtimer.h>
#include <QTimer>
#include <QTemporaryFile>
FakeLogind::FakeLogind(QObject* parent) : QObject(parent)
{
......@@ -36,24 +38,33 @@ QDBusUnixFileDescriptor FakeLogind::Inhibit(const QString& what, const QString&
{
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);
/*
foo.setFileDescriptor(-1);
QTemporaryFile file;
if (!file.open()) {
qDebug() << "Could not open a temporary file";
return foo;
}
m_fd = file.handle();
foo.giveFileDescriptor(m_fd);
//We could use epoll for this, but it will make the code harder to read for a test.
auto t = new QTimer();
t->setInterval(100);
connect(t, SIGNAL(timeout()), SLOT(checkFd()));
t->start();*/
t->start();
return foo;
}
void FakeLogind::checkFd()
{
int e = fcntl(m_fd, F_GETFD) != -1 || errno != EBADF;
if (e == 0) {
Q_EMIT inhibitionRemoved();
delete sender();
}
}
\ No newline at end of file
......@@ -39,7 +39,7 @@ public Q_SLOTS:
void checkFd();
Q_SIGNALS:
void fdClosed();
void inhibitionRemoved();
void newInhibition(const QString &what, const QString &who, const QString &why, const QString &mode);
private:
......
......@@ -99,9 +99,25 @@ void solidFreedesktopTest::testAddInhibition()
job->setInhibitions(Power::Hibernation | Power::Sleep);
job->exec();
spy.wait(100);
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()
......
......@@ -32,6 +32,7 @@ using namespace Solid;
InhibitionJobPrivate::InhibitionJobPrivate()
: inhibitions(Power::None)
, inhibition(Q_NULLPTR)
, backendJob(Q_NULLPTR)
{
......@@ -44,11 +45,12 @@ InhibitionJob::InhibitionJob(QObject* parent) : Job(*new InhibitionJobPrivate(),
Inhibition* InhibitionJob::inhibition() const
{
Q_ASSERT_X(d_func()->backendJob, "addInhibitionJob", "::inhibition() called before result() has been emitted");
Q_ASSERT_X(d_func()->inhibition, "addInhibitionJob", "::inhibition() called before result() has been emitted");
if(d_func()->backendJob) {
return d_func()->backendJob->inhibition();
if(d_func()->inhibition) {
return d_func()->inhibition;
}
qWarning() << "result() has not been emitted yet, job not finished";
return Q_NULLPTR;
}
......@@ -69,7 +71,8 @@ void InhibitionJob::doStart()
}
d->backendJob = PowerBackendLoader::addInhibitionJob(d->inhibitions, d->description);
connect(d->backendJob, &AbstractInhibitionJob::result, [this]() {
connect(d->backendJob, &AbstractInhibitionJob::result, [this, d]() {
d_func()->inhibition = d->backendJob->inhibition();
emitResult();
});
......
......@@ -33,9 +33,9 @@ class InhibitionJobPrivate : public JobPrivate
public:
InhibitionJobPrivate();
QString description;
Power::States inhibitions;
Inhibition *inhibition;
AbstractInhibitionJob *backendJob;
Q_DECLARE_PUBLIC(InhibitionJob)
};
......
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