Commit 6761ed50 authored by Àlex Fiestas's avatar Àlex Fiestas

Add test for onAcPluggedChanged

We check that we are properly getting the
freedesktop.Properties.PropertiesChanged signal and that we are
extracting the value correclty from it.

We also emit it twice and make sure we get things right.
parent 694af205
......@@ -21,8 +21,11 @@
#include "fakeUpower.h"
#include <QDebug>
#include <QDBusPendingCall>
#include <QDBusConnection>
#include <qdbusmessage.h>
FakeUpower::FakeUpower(QObject* parent) : QDBusAbstractAdaptor(parent),
FakeUpower::FakeUpower(QObject* parent) : QObject(parent),
m_onBattery(false)
{
......@@ -63,6 +66,32 @@ bool FakeUpower::onBattery() const
return m_onBattery;
}
void FakeUpower::setOnBattery(bool onBattery)
{
m_onBattery = onBattery;
emitPropertiesChanged(QStringLiteral("onBattery"), m_onBattery);
}
void FakeUpower::emitPropertiesChanged(const QString& name, const QVariant& value)
{
auto msg = QDBusMessage::createSignal(
QStringLiteral("/org/freedesktop/UPower"),
QStringLiteral("org.freedesktop.DBus.Properties"),
QStringLiteral("PropertiesChanged"));
QVariantMap map;
map.insert(name, value);
QList<QVariant> args;
args << QString("org.freedesktop.UPower");
args << map;
args << QStringList();
msg.setArguments(args);
QDBusConnection::systemBus().asyncCall(msg);
}
QList< QDBusObjectPath > FakeUpower::EnumerateDevices()
{
QList<QDBusObjectPath> list;
......
......@@ -26,7 +26,7 @@
#include <QDBusAbstractAdaptor>
#include <QDBusObjectPath>
class FakeUpower : public QDBusAbstractAdaptor
class FakeUpower : public QObject
{
Q_OBJECT
Q_PROPERTY(bool IsDocked READ isDocked)
......@@ -40,14 +40,15 @@ public:
bool lidIsClosed() const;
bool lidIsPresent() const;
bool onBattery() const;
void setOnBattery(bool onBattery);
bool m_onBattery;
void emitPropertiesChanged(const QString &name, const QVariant &value);
public Q_SLOTS:
QList<QDBusObjectPath> EnumerateDevices();
QString GetCriticalAction();
QDBusObjectPath GetDisplayDevice();
};
#endif //SOLID_FAKE_UPOWER_H
\ No newline at end of file
......@@ -72,7 +72,17 @@ void solidFreedesktopTest::testAcPluggedJob()
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()
......
......@@ -3,4 +3,5 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}/power)
set(solid_LIB_SRCS
${solid_LIB_SRCS}
power/backends/freedesktop/fdacpluggedjob.cpp
power/backends/freedesktop/fdpowernotifier.cpp
)
\ 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 "fdpowernotifier.h"
#include <QDebug>
#include <QDBusConnection>
#include <QtDBus/QDBusConnection>
Solid::FDPowerNotifier::FDPowerNotifier(QObject* parent): PowerNotifier(parent)
{
auto conn = QDBusConnection::systemBus();
conn.connect(QStringLiteral("org.freedesktop.UPower"),
QStringLiteral("/org/freedesktop/UPower"),
QStringLiteral("org.freedesktop.DBus.Properties"),
QStringLiteral("PropertiesChanged"),
this,
SLOT(propertiesChanged(QString, QVariantMap, QStringList))
);
}
void Solid::FDPowerNotifier::propertiesChanged(const QString &interface, const QVariantMap &changedProperties, const QStringList &invalidated)
{
if (interface != QStringLiteral("org.freedesktop.UPower")) {
return;
}
if (changedProperties.contains(QStringLiteral("onBattery"))) {
Q_EMIT acPluggedChanged(!changedProperties.value(QStringLiteral("onBattery")).toBool());
}
//Just for debug purposes
if (!invalidated.isEmpty()) {
qDebug() << "Invalidated" << invalidated;
}
}
/*
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_POWER_NOTIFIER_H
#define SOLID_FD_POWER_NOTIFIER_H
#include "backends/powernotifier.h"
#include <QStringList>
namespace Solid
{
class FDPowerNotifier : public PowerNotifier
{
Q_OBJECT
public:
explicit FDPowerNotifier(QObject* parent = 0);
public Q_SLOTS:
void propertiesChanged(const QString& interface, const QVariantMap& changedProperties, const QStringList& invalidated);
};
}
#endif //SOLID_FD_POWER_NOTIFIER_H
\ No newline at end of file
......@@ -26,6 +26,7 @@
#include "backends/dummy/dummystatesjob.h"
#include "backends/dummy/dummyrequeststatejob.h"
#include "backends/freedesktop/fdacpluggedjob.h"
#include "backends/freedesktop/fdpowernotifier.h"
using namespace Solid;
......@@ -54,5 +55,8 @@ AbstractRequestStateJob* PowerBackendLoader::requestState()
PowerNotifier* PowerBackendLoader::notifier()
{
if (qgetenv("SOLID_POWER_BACKEND") == "FREE_DESKTOP") {
return new FDPowerNotifier();
}
return new DummyPowerNotifier();
}
\ No newline at end of file
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