on d355152 help sign in
unity8/tests/plugins/Unity/Session/sessionbackendtest.cpp
197 lines 7.4 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 init() {
57 qputenv("TEST_USER", "testuser");
58 qputenv("TEST_NOPASSWD_USERS", "");
59 }
61 void testUnitySessionLogoutRequested_data() {
62 QTest::addColumn<QString>("method");
63 QTest::addColumn<QString>("signal");
65 QTest::newRow("Logout") << "RequestLogout" << "LogoutRequested(bool)";
66 QTest::newRow("Reboot") << "RequestReboot" << "RebootRequested(bool)";
67 QTest::newRow("Shutdown") << "RequestShutdown" << "ShutdownRequested(bool)";
68 QTest::newRow("PromptLock") << "PromptLock" << "LockRequested()";
69 QTest::newRow("Lock") << "Lock" << "LockRequested()";
70 QTest::newRow("LockLightDM") << "Lock" << "Locked()"; // happens when we lock LightDM, only for Lock()
71 }
73 void testUnitySessionLogoutRequested() {
74 QFETCH(QString, method);
75 QFETCH(QString, signal);
77 DBusUnitySessionService dbusUnitySessionService;
78 QCoreApplication::processEvents(); // to let the service register on DBus
80 // .. because QSignalSpy checks the signal signature like this: "if (((aSignal[0] - '0') & 0x03) != QSIGNAL_CODE)"
81 QSignalSpy spy(&dbusUnitySessionService, qPrintable(signal.prepend(QSIGNAL_CODE)));
83 QDBusReply<void> reply = dbusUnitySession->call(method);
84 QCOMPARE(reply.isValid(), true);
86 QTRY_COMPARE(spy.count(), 1);
87 }
89 void testGnomeSessionWrapper_data() {
90 QTest::addColumn<uint>("method");
91 QTest::addColumn<QString>("signal");
93 QTest::newRow("Logout") << (uint)Action::LOGOUT << "LogoutRequested(bool)";
94 QTest::newRow("Shutdown") << (uint)Action::SHUTDOWN << "ShutdownRequested(bool)";
95 QTest::newRow("Reboot") << (uint)Action::REBOOT << "RebootRequested(bool)";
96 }
98 void testGnomeSessionWrapper() {
99 QFETCH(uint, method);
100 QFETCH(QString, signal);
102 DBusUnitySessionService dbusUnitySessionService;
103 QCoreApplication::processEvents(); // to let the service register on DBus
105 // Spy on the given signal on the /com/canonical/Unity/Session object
106 // as proof we are actually calling the actual method.
107 // .. because QSignalSpy checks the signal signature like this: "if (((aSignal[0] - '0') & 0x03) != QSIGNAL_CODE)"
108 QSignalSpy spy(&dbusUnitySessionService, qPrintable(signal.prepend(QSIGNAL_CODE)));
110 DBusGnomeSessionManagerWrapper dbusGnomeSessionManagerWrapper;
111 QCoreApplication::processEvents(); // to let the service register on DBus
113 QDBusInterface dbusGnomeSessionWrapper("com.canonical.Unity",
114 "/org/gnome/SessionManager/EndSessionDialog",
115 "org.gnome.SessionManager.EndSessionDialog",
116 QDBusConnection::sessionBus());
118 QCOMPARE(dbusGnomeSessionWrapper.isValid(), true);
120 // Set the QVariant as a QList<QDBusObjectPath> type
121 QDbusList var;
122 QVariant inhibitors;
123 inhibitors.setValue(var);
125 QDBusReply<void> reply = dbusGnomeSessionWrapper.call("Open", method, (unsigned)0, (unsigned)0, inhibitors);
126 QCOMPARE(reply.isValid(), true);
128 // Make sure we see the signal being emitted.
129 QCOMPARE(spy.count(), 1);
130 }
132 void testUnlockFromLogind() {
133 DBusUnitySessionService dbusUnitySessionService;
134 QCoreApplication::processEvents(); // to let the service register on DBus
136 QDBusInterface iface("org.freedesktop.login1", "/logindsession", "org.freedesktop.login1.Session");
137 QVERIFY(iface.isValid());
139 QSignalSpy spy(&dbusUnitySessionService, SIGNAL(Unlocked()));
140 QCOMPARE(iface.call("MockEmitUnlock").errorMessage(), QString());
141 QTRY_COMPARE(spy.count(), 1);
142 }
144 void testUserName() {
145 DBusUnitySessionService dbusUnitySessionService;
146 QCOMPARE(dbusUnitySessionService.UserName(), QString("testuser"));
147 }
149 void testRealName() {
150 DBusUnitySessionService dbusUnitySessionService;
151 QCoreApplication::processEvents(); // to let the service register on DBus
153 QDBusInterface accIface("org.freedesktop.Accounts", "/org/freedesktop/Accounts", "org.freedesktop.Accounts", QDBusConnection::systemBus());
154 if (accIface.isValid()) {
155 QDBusReply<QDBusObjectPath> userPath = accIface.asyncCall("FindUserById", static_cast<qint64>(geteuid()));
156 if (userPath.isValid()) {
157 QDBusInterface userAccIface("org.freedesktop.Accounts", userPath.value().path(), "org.freedesktop.Accounts.User", QDBusConnection::systemBus());
158 QCOMPARE(dbusUnitySessionService.RealName(), userAccIface.property("RealName").toString());
159 }
160 }
161 }
163 void testCanLock() {
164 DBusUnitySessionService dbusUnitySessionService;
166 qputenv("TEST_USER", "testuser");
167 QVERIFY(dbusUnitySessionService.CanLock());
169 qputenv("TEST_USER", "guest-abcdef"); // guest-* can't lock
170 QVERIFY(!dbusUnitySessionService.CanLock());
172 qputenv("TEST_USER", "testuser");
173 qputenv("TEST_NOPASSWD_USERS", "testuser"); // nopasswdlogin can't lock
174 QVERIFY(!dbusUnitySessionService.CanLock());
175 }
177 void testPromptLockRespectsCanLock() {
178 DBusUnitySessionService dbusUnitySessionService;
180 QSignalSpy spy(&dbusUnitySessionService, SIGNAL(lockRequested()));
181 QVERIFY(dbusUnitySessionService.CanLock());
182 dbusUnitySessionService.PromptLock();
183 QCOMPARE(spy.count(), 1);
185 spy.clear();
186 qputenv("TEST_USER", "guest-abcdef");
187 QVERIFY(!dbusUnitySessionService.CanLock());
188 dbusUnitySessionService.PromptLock();
189 QCOMPARE(spy.count(), 0);
190 }
192private:
193 QDBusInterface *dbusUnitySession;
194};
196QTEST_GUILESS_MAIN(SessionBackendTest)
197#include "sessionbackendtest.moc"