on d8fd129 help sign in
unity8/tests/plugins/Unity/Session/sessionbackendtest.cpp
172 lines 7.1 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"
34#include "../../AccountsService/AccountsServiceDBusAdaptor.h"
36enum class Action : unsigned
38 LOGOUT = 0,
39 SHUTDOWN,
40 REBOOT,
41 NONE
42};
44class SessionBackendTest : public QObject
46 Q_OBJECT
48private Q_SLOTS:
50 void initTestCase() {
51 dbusUnitySession = new QDBusInterface ("com.canonical.Unity",
52 "/com/canonical/Unity/Session",
53 "com.canonical.Unity.Session",
54 QDBusConnection::sessionBus());
55 }
57 void testUnitySessionLogoutRequested_data() {
58 QTest::addColumn<QString>("method");
59 QTest::addColumn<QString>("signal");
61 QTest::newRow("Logout") << "RequestLogout" << "LogoutRequested(bool)";
62 QTest::newRow("Reboot") << "RequestReboot" << "RebootRequested(bool)";
63 QTest::newRow("Shutdown") << "RequestShutdown" << "ShutdownRequested(bool)";
64 QTest::newRow("PromptLock") << "PromptLock" << "LockRequested()";
65 }
67 void testUnitySessionLogoutRequested() {
68 QFETCH(QString, method);
69 QFETCH(QString, signal);
71 DBusUnitySessionService dbusUnitySessionService;
72 QCoreApplication::processEvents(); // to let the service register on DBus
74 // .. because QSignalSpy checks the signal signature like this: "if (((aSignal[0] - '0') & 0x03) != QSIGNAL_CODE)"
75 QSignalSpy spy(&dbusUnitySessionService, qPrintable(signal.prepend(QSIGNAL_CODE)));
77 QDBusReply<void> reply = dbusUnitySession->call(method);
78 QCOMPARE(reply.isValid(), true);
80 QCOMPARE(spy.count(), 1);
81 }
83 void testGnomeSessionWrapper_data() {
84 QTest::addColumn<uint>("method");
85 QTest::addColumn<QString>("signal");
87 QTest::newRow("Logout") << (uint)Action::LOGOUT << "LogoutRequested(bool)";
88 QTest::newRow("Shutdown") << (unsigned)Action::SHUTDOWN << "ShutdownRequested(bool)";
89 QTest::newRow("Reboot") << (unsigned)Action::REBOOT << "RebootRequested(bool)";
90 }
92 void testGnomeSessionWrapper() {
93 QFETCH(uint, method);
94 QFETCH(QString, signal);
96 DBusUnitySessionService dbusUnitySessionService;
97 QCoreApplication::processEvents(); // to let the service register on DBus
99 // Spy on the given signal on the /com/canonical/Unity/Session object
100 // as proof we are actually calling the actual method.
101 // .. because QSignalSpy checks the signal signature like this: "if (((aSignal[0] - '0') & 0x03) != QSIGNAL_CODE)"
102 QSignalSpy spy(&dbusUnitySessionService, qPrintable(signal.prepend(QSIGNAL_CODE)));
104 DBusGnomeSessionManagerWrapper dbusGnomeSessionManagerWrapper;
105 QCoreApplication::processEvents(); // to let the service register on DBus
107 QDBusInterface dbusGnomeSessionWrapper("com.canonical.Unity",
108 "/org/gnome/SessionManager/EndSessionDialog",
109 "org.gnome.SessionManager.EndSessionDialog",
110 QDBusConnection::sessionBus());
112 QCOMPARE(dbusGnomeSessionWrapper.isValid(), true);
114 // Set the QVariant as a QList<QDBusObjectPath> type
115 QDbusList var;
116 QVariant inhibitors;
117 inhibitors.setValue(var);
119 QDBusReply<void> reply = dbusGnomeSessionWrapper.call("Open", method, (unsigned)0, (unsigned)0, inhibitors);
120 QCOMPARE(reply.isValid(), true);
122 // Make sure we see the signal being emitted.
123 QCOMPARE(spy.count(), 1);
124 }
126 void testUserName() {
127 DBusUnitySessionService dbusUnitySessionService;
128 QCoreApplication::processEvents(); // to let the service register on DBus
130 QCOMPARE(dbusUnitySessionService.UserName(), QString::fromUtf8(qgetenv("USER")));
131 }
133 void testRealName() {
134 DBusUnitySessionService dbusUnitySessionService;
135 QCoreApplication::processEvents(); // to let the service register on DBus
137 QDBusInterface accIface("org.freedesktop.Accounts", "/org/freedesktop/Accounts", "org.freedesktop.Accounts", QDBusConnection::systemBus());
138 QVERIFY(accIface.isValid());
139 QDBusReply<QDBusObjectPath> userPath = accIface.asyncCall("FindUserById", static_cast<qint64>(geteuid()));
140 if (!userPath.isValid()) {
141 QFAIL("Could not get user path in Account Service");
142 }
144 QDBusInterface userAccIface("org.freedesktop.Accounts", userPath.value().path(), "org.freedesktop.Accounts.User", QDBusConnection::systemBus());
145 QCOMPARE(dbusUnitySessionService.RealName(), userAccIface.property("RealName").toString());
146 }
148 void testLogin1Capabilities() {
149 DBusUnitySessionService dbusUnitySessionService;
150 QDBusInterface login1face("org.freedesktop.login1", "/org/freedesktop/login1", "org.freedesktop.login1.Manager", QDBusConnection::systemBus());
151 QCoreApplication::processEvents(); // to let the services register on DBus
153 QCOMPARE(dbusUnitySessionService.CanHibernate(), (login1face.call("CanHibernate").arguments().first().toString() != "no"));
154 QCOMPARE(dbusUnitySessionService.CanSuspend(), (login1face.call("CanSuspend").arguments().first().toString() != "no"));
155 QCOMPARE(dbusUnitySessionService.CanReboot(), (login1face.call("CanReboot").arguments().first().toString() != "no"));
156 QCOMPARE(dbusUnitySessionService.CanShutdown(), (login1face.call("CanPowerOff").arguments().first().toString() != "no"));
157 QCOMPARE(dbusUnitySessionService.CanHybridSleep(), (login1face.call("CanHybridSleep").arguments().first().toString() != "no"));
158 }
160// void testLock() {
161// DBusUnitySessionService dbusUnitySessionService;
162// QCoreApplication::processEvents(); // to let the service register on DBus
163// qDebug() << "Locking up...";
164// dbusUnitySessionService.Lock();
165// }
167private:
168 QDBusInterface *dbusUnitySession;
169};
171QTEST_GUILESS_MAIN(SessionBackendTest)
172#include "sessionbackendtest.moc"