on da0fcd1 help sign in
unity8/tests/plugins/Unity/Session/sessionbackendtest.cpp
124 lines 4.6 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>
28#include "dbusunitysessionservice.h"
30enum class Action : unsigned
32 LOGOUT = 0,
33 SHUTDOWN,
34 REBOOT,
35 NONE
36};
38class SessionBackendTest : public QObject
40 Q_OBJECT
42private Q_SLOTS:
44 void initTestCase() {
45 dbusUnitySession = new QDBusInterface ("com.canonical.Unity",
46 "/com/canonical/Unity/Session",
47 "com.canonical.Unity.Session",
48 QDBusConnection::sessionBus());
49 }
51 void testUnitySessionLogoutRequested_data() {
52 QTest::addColumn<QString>("method");
53 QTest::addColumn<QString>("signal");
55 QTest::newRow("Logout") << "RequestLogout" << "logoutRequested(bool)";
56 QTest::newRow("Reboot") << "RequestReboot" << "rebootRequested(bool)";
57 QTest::newRow("Shutdown") << "RequestShutdown" << "shutdownRequested(bool)";
58 }
60 void testUnitySessionLogoutRequested() {
61 QFETCH(QString, method);
62 QFETCH(QString, signal);
64 DBusUnitySessionService dbusUnitySessionService;
65 QCoreApplication::processEvents(); // to let the service register on DBus
67 // .. because QSignalSpy checks the signal signature like this: "if (((aSignal[0] - '0') & 0x03) != QSIGNAL_CODE)"
68 QSignalSpy spy(&dbusUnitySessionService, signal.prepend(QSIGNAL_CODE).toLocal8Bit().constData());
70 QDBusReply<void> reply = dbusUnitySession->call(method);
71 QCOMPARE(reply.isValid(), true);
73 QCOMPARE(spy.count(), 1);
74 }
76 void testGnomeSessionWrapper_data() {
77 QTest::addColumn<uint>("method");
78 QTest::addColumn<QString>("signal");
80 QTest::newRow("Logout") << (uint)Action::LOGOUT << "logoutRequested(bool)";
81 QTest::newRow("Shutdown") << (unsigned)Action::SHUTDOWN << "shutdownRequested(bool)";
82 QTest::newRow("Reboot") << (unsigned)Action::REBOOT << "rebootRequested(bool)";
83 }
85 void testGnomeSessionWrapper() {
86 QFETCH(uint, method);
87 QFETCH(QString, signal);
89 DBusUnitySessionService dbusUnitySessionService;
90 QCoreApplication::processEvents(); // to let the service register on DBus
92 // Spy on the given signal on the /com/canonical/Unity/Session object
93 // as proof we are actually calling the actual method.
94 // .. because QSignalSpy checks the signal signature like this: "if (((aSignal[0] - '0') & 0x03) != QSIGNAL_CODE)"
95 QSignalSpy spy(&dbusUnitySessionService, signal.prepend(QSIGNAL_CODE).toLocal8Bit().constData());
97 DBusGnomeSessionManagerWrapper dbusGnomeSessionManagerWrapper;
98 QCoreApplication::processEvents(); // to let the service register on DBus
100 QDBusInterface dbusGnomeSessionWrapper("com.canonical.Unity",
101 "/org/gnome/SessionManager/EndSessionDialog",
102 "org.gnome.SessionManager.EndSessionDialog",
103 QDBusConnection::sessionBus());
105 QCOMPARE(dbusGnomeSessionWrapper.isValid(), true);
107 // Set the QVariant as a QList<QDBusObjectPath> type
108 QDbusList var;
109 QVariant inhibitors;
110 inhibitors.setValue(var);
112 QDBusReply<void> reply = dbusGnomeSessionWrapper.call("Open", method, (unsigned)0, (unsigned)0, inhibitors);
113 QCOMPARE(reply.isValid(), true);
115 // Make sure we see the signal being emitted.
116 QCOMPARE(spy.count(), 1);
117 }
119private:
120 QDBusInterface *dbusUnitySession;
121};
123QTEST_GUILESS_MAIN(SessionBackendTest)
124#include "sessionbackendtest.moc"