on 1b4804e help sign in
unity8/tests/plugins/Unity/Session/sessionbackendtest.cpp
188 lines 7.9 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 QCOMPARE(dbusUnitySessionService.UserName(), QString::fromUtf8(qgetenv("USER")));
130 }
132 void testRealName() {
133 DBusUnitySessionService dbusUnitySessionService;
134 QCoreApplication::processEvents(); // to let the service register on DBus
136 QDBusInterface accIface("org.freedesktop.Accounts", "/org/freedesktop/Accounts", "org.freedesktop.Accounts", QDBusConnection::systemBus());
137 QVERIFY(accIface.isValid());
138 QDBusReply<QDBusObjectPath> userPath = accIface.asyncCall("FindUserById", static_cast<qint64>(geteuid()));
139 if (!userPath.isValid()) {
140 QFAIL("Could not get user path in Account Service");
141 }
143 QDBusInterface userAccIface("org.freedesktop.Accounts", userPath.value().path(), "org.freedesktop.Accounts.User", QDBusConnection::systemBus());
144 QCOMPARE(dbusUnitySessionService.RealName(), userAccIface.property("RealName").toString());
145 }
147 void testLogin1Capabilities() {
148 DBusUnitySessionService dbusUnitySessionService;
149 QDBusInterface login1face("org.freedesktop.login1", "/org/freedesktop/login1", "org.freedesktop.login1.Manager", QDBusConnection::systemBus());
150 QCoreApplication::processEvents(); // to let the services register on DBus
152 QCOMPARE(dbusUnitySessionService.CanHibernate(), (login1face.call("CanHibernate").arguments().first().toString() != "no"));
153 QCOMPARE(dbusUnitySessionService.CanSuspend(), (login1face.call("CanSuspend").arguments().first().toString() != "no"));
154 QCOMPARE(dbusUnitySessionService.CanReboot(), (login1face.call("CanReboot").arguments().first().toString() != "no"));
155 QCOMPARE(dbusUnitySessionService.CanShutdown(), (login1face.call("CanPowerOff").arguments().first().toString() != "no"));
156 QCOMPARE(dbusUnitySessionService.CanHybridSleep(), (login1face.call("CanHybridSleep").arguments().first().toString() != "no"));
157 }
159// void testLock() {
160// DBusUnitySessionService dbusUnitySessionService;
161// DBusScreensaverWrapper saverIface;
162// QDBusInterface saverCompatIface("org.freedesktop.ScreenSaver", "/ScreenSaver", "org.freedesktop.ScreenSaver", QDBusConnection::sessionBus());
163// QSignalSpy spy(&saverIface, &DBusScreensaverWrapper::ActiveChanged);
164// QCoreApplication::processEvents(); // to let the service register on DBus
166// QTRY_COMPARE(saverCompatIface.isValid(), true);
168// qDebug() << "Locking up...";
169// dbusUnitySessionService.Lock();
171// QTRY_COMPARE(spy.count(), 1);
172// QList<QVariant> arguments = spy.takeFirst(); // take the first signal
173// QVERIFY(arguments.at(0).toBool() == true); // verify the first argument
175// // verify the session is locked
176// QCOMPARE(saverIface.GetActive(), true);
178// sleep(5);
179// qDebug() << "Screensaver has been active for" << saverIface.GetActiveTime() << "seconds";
180// qDebug() << "Session has been idle for" << saverIface.GetSessionIdleTime() << "seconds";
181 }
183private:
184 QDBusInterface *dbusUnitySession;
185};
187QTEST_GUILESS_MAIN(SessionBackendTest)
188#include "sessionbackendtest.moc"