on 5259b85 help sign in
unity8/tests/plugins/Unity/Session/sessionbackendtest.cpp
170 lines 7.0 kB history raw
1/*
2 * Copyright 2013-2015 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Authors:
17 * Ying-Chun Liu (PaulLiu) <paul.liu@canonical.com>
18 * Lukáš Tinkl <lukas.tinkl@canonical.com>
19 */
21#include <QtTest>
22#include <QDBusConnection>
23#include <QDBusMessage>
24#include <QDBusInterface>
25#include <QDBusReply>
26#include <QDBusVariant>
27#include <QDebug>
28#include <QDBusObjectPath>
30#include <unistd.h>
31#include <sys/types.h>
33#include "dbusunitysessionservice.h"
35enum class Action : unsigned
37 LOGOUT = 0,
38 SHUTDOWN,
39 REBOOT,
40 NONE
41};
43class SessionBackendTest : public QObject
45 Q_OBJECT
47private Q_SLOTS:
49 void initTestCase() {
50 dbusUnitySession = new QDBusInterface ("com.canonical.Unity",
51 "/com/canonical/Unity/Session",
52 "com.canonical.Unity.Session",
53 QDBusConnection::sessionBus());
54 }
56 void testUnitySessionLogoutRequested_data() {
57 QTest::addColumn<QString>("method");
58 QTest::addColumn<QString>("signal");
60 QTest::newRow("Logout") << "RequestLogout" << "LogoutRequested(bool)";
61 QTest::newRow("Reboot") << "RequestReboot" << "RebootRequested(bool)";
62 QTest::newRow("Shutdown") << "RequestShutdown" << "ShutdownRequested(bool)";
63 QTest::newRow("PromptLock") << "PromptLock" << "LockRequested()";
64 }
66 void testUnitySessionLogoutRequested() {
67 QFETCH(QString, method);
68 QFETCH(QString, signal);
70 DBusUnitySessionService dbusUnitySessionService;
71 QCoreApplication::processEvents(); // to let the service register on DBus
73 // .. because QSignalSpy checks the signal signature like this: "if (((aSignal[0] - '0') & 0x03) != QSIGNAL_CODE)"
74 QSignalSpy spy(&dbusUnitySessionService, qPrintable(signal.prepend(QSIGNAL_CODE)));
76 QDBusReply<void> reply = dbusUnitySession->call(method);
77 QCOMPARE(reply.isValid(), true);
79 QCOMPARE(spy.count(), 1);
80 }
82 void testGnomeSessionWrapper_data() {
83 QTest::addColumn<uint>("method");
84 QTest::addColumn<QString>("signal");
86 QTest::newRow("Logout") << (uint)Action::LOGOUT << "LogoutRequested(bool)";
87 QTest::newRow("Shutdown") << (uint)Action::SHUTDOWN << "ShutdownRequested(bool)";
88 QTest::newRow("Reboot") << (uint)Action::REBOOT << "RebootRequested(bool)";
89 }
91 void testGnomeSessionWrapper() {
92 QFETCH(uint, method);
93 QFETCH(QString, signal);
95 DBusUnitySessionService dbusUnitySessionService;
96 QCoreApplication::processEvents(); // to let the service register on DBus
98 // Spy on the given signal on the /com/canonical/Unity/Session object
99 // as proof we are actually calling the actual method.
100 // .. because QSignalSpy checks the signal signature like this: "if (((aSignal[0] - '0') & 0x03) != QSIGNAL_CODE)"
101 QSignalSpy spy(&dbusUnitySessionService, qPrintable(signal.prepend(QSIGNAL_CODE)));
103 DBusGnomeSessionManagerWrapper dbusGnomeSessionManagerWrapper;
104 QCoreApplication::processEvents(); // to let the service register on DBus
106 QDBusInterface dbusGnomeSessionWrapper("com.canonical.Unity",
107 "/org/gnome/SessionManager/EndSessionDialog",
108 "org.gnome.SessionManager.EndSessionDialog",
109 QDBusConnection::sessionBus());
111 QCOMPARE(dbusGnomeSessionWrapper.isValid(), true);
113 // Set the QVariant as a QList<QDBusObjectPath> type
114 QDbusList var;
115 QVariant inhibitors;
116 inhibitors.setValue(var);
118 QDBusReply<void> reply = dbusGnomeSessionWrapper.call("Open", method, (unsigned)0, (unsigned)0, inhibitors);
119 QCOMPARE(reply.isValid(), true);
121 // Make sure we see the signal being emitted.
122 QCOMPARE(spy.count(), 1);
123 }
125 void testUserName() {
126 DBusUnitySessionService dbusUnitySessionService;
127 QCoreApplication::processEvents(); // to let the service register on DBus
129 QProcess * proc = new QProcess(this);
130 proc->start("id -un", QProcess::ReadOnly);
131 proc->waitForFinished();
132 const QByteArray out = proc->readAll().trimmed();
134 QCOMPARE(dbusUnitySessionService.UserName(), QString::fromUtf8(out));
135 }
137 void testRealName() {
138 DBusUnitySessionService dbusUnitySessionService;
139 QCoreApplication::processEvents(); // to let the service register on DBus
141 QDBusInterface accIface("org.freedesktop.Accounts", "/org/freedesktop/Accounts", "org.freedesktop.Accounts", QDBusConnection::systemBus());
142 if (accIface.isValid()) {
143 QDBusReply<QDBusObjectPath> userPath = accIface.asyncCall("FindUserById", static_cast<qint64>(geteuid()));
144 if (userPath.isValid()) {
145 QDBusInterface userAccIface("org.freedesktop.Accounts", userPath.value().path(), "org.freedesktop.Accounts.User", QDBusConnection::systemBus());
146 QCOMPARE(dbusUnitySessionService.RealName(), userAccIface.property("RealName").toString());
147 }
148 }
149 }
151 void testLogin1Capabilities() {
152 DBusUnitySessionService dbusUnitySessionService;
153 QDBusInterface login1face("org.freedesktop.login1", "/org/freedesktop/login1", "org.freedesktop.login1.Manager", QDBusConnection::systemBus());
154 QCoreApplication::processEvents(); // to let the services register on DBus
156 if (login1face.isValid()) {
157 QCOMPARE(dbusUnitySessionService.CanHibernate(), (login1face.call("CanHibernate").arguments().first().toString() != "no"));
158 QCOMPARE(dbusUnitySessionService.CanSuspend(), (login1face.call("CanSuspend").arguments().first().toString() != "no"));
159 QCOMPARE(dbusUnitySessionService.CanReboot(), (login1face.call("CanReboot").arguments().first().toString() != "no"));
160 QCOMPARE(dbusUnitySessionService.CanShutdown(), (login1face.call("CanPowerOff").arguments().first().toString() != "no"));
161 QCOMPARE(dbusUnitySessionService.CanHybridSleep(), (login1face.call("CanHybridSleep").arguments().first().toString() != "no"));
162 }
163 }
165private:
166 QDBusInterface *dbusUnitySession;
167};
169QTEST_GUILESS_MAIN(SessionBackendTest)
170#include "sessionbackendtest.moc"