on xenial help sign in
Add tests for the DBusGnomeSessionWrapper.
520e4ceChris Townsend
plugins/Unity/Session/dbusunitysessionservice.h+6−3
@@ -21,6 +21,9 @@
21
21
22#include "unitydbusobject.h"
22#include "unitydbusobject.h"
23
23
24+typedef QList<QDBusObjectPath> QDbusList;
25+Q_DECLARE_METATYPE(QList<QDBusObjectPath>)
26+
24/**
27/**
25 * DBusUnitySessionService provides com.canonical.Unity.Session dbus
28 * DBusUnitySessionService provides com.canonical.Unity.Session dbus
26 * interface.
29 * interface.
@@ -51,7 +54,7 @@ Q_SIGNALS:
51 * @param have_inhibitors if there are any special running applications
54 * @param have_inhibitors if there are any special running applications
52 * which inhibit the logout.
55 * which inhibit the logout.
53 */
56 */
54− void logoutRequested(bool have_inhibitors);
57+ Q_SCRIPTABLE void logoutRequested(bool have_inhibitors);
55
58
56 /**
59 /**
57 * rebootRequested signal
60 * rebootRequested signal
@@ -61,7 +64,7 @@ Q_SIGNALS:
61 * @param have_inhibitors if there are any special running applications
64 * @param have_inhibitors if there are any special running applications
62 * which inhibit the reboot.
65 * which inhibit the reboot.
63 */
66 */
64− void rebootRequested(bool have_inhibitors);
67+ Q_SCRIPTABLE void rebootRequested(bool have_inhibitors);
65
68
66 /**
69 /**
67 * shutdownRequested signal
70 * shutdownRequested signal
@@ -71,7 +74,7 @@ Q_SIGNALS:
71 * @param have_inhibitors if there are any special running applications
74 * @param have_inhibitors if there are any special running applications
72 * which inhibit the shutdown.
75 * which inhibit the shutdown.
73 */
76 */
74− void shutdownRequested(bool have_inhibitors);
77+ Q_SCRIPTABLE void shutdownRequested(bool have_inhibitors);
75
78
76
79
77 /**
80 /**
tests/plugins/Unity/Session/sessionbackendtest.cpp+82−10
@@ -26,6 +26,14 @@
26
26
27#include "dbusunitysessionservice.h"
27#include "dbusunitysessionservice.h"
28
28
29+enum class Action : unsigned
30+{
31+ LOGOUT = 0,
32+ SHUTDOWN,
33+ REBOOT,
34+ NONE
35+};
36+
29class SessionBackendTest : public QObject
37class SessionBackendTest : public QObject
30{
38{
31 Q_OBJECT
39 Q_OBJECT
@@ -33,32 +41,96 @@ class SessionBackendTest : public QObject
33private Q_SLOTS:
41private Q_SLOTS:
34
42
35 void initTestCase() {
43 void initTestCase() {
44+ dbusUnitySession = new QDBusInterface ("com.canonical.Unity",
45+ "/com/canonical/Unity/Session",
46+ "com.canonical.Unity.Session",
47+ QDBusConnection::sessionBus());
36 }
48 }
37
49
38− void testDbusIfaceMethods_data() {
50+ void testUnitySessionLogoutRequested_data() {
39 QTest::addColumn<QString>("method");
51 QTest::addColumn<QString>("method");
40
52
41 QTest::newRow("Logout") << "RequestLogout";
53 QTest::newRow("Logout") << "RequestLogout";
42 }
54 }
43
55
44− void testDbusIfaceMethods() {
56+ void testUnitySessionLogoutRequested() {
45 QFETCH(QString, method);
57 QFETCH(QString, method);
46
58
47 DBusUnitySessionService dbusUnitySessionService;
59 DBusUnitySessionService dbusUnitySessionService;
48 QCoreApplication::processEvents(); // to let the service register on DBus
60 QCoreApplication::processEvents(); // to let the service register on DBus
49
61
50− QDBusConnection con = QDBusConnection::sessionBus();
62+ QSignalSpy spy(&dbusUnitySessionService, SIGNAL(logoutRequested(bool)));
63+
64+ QDBusReply<void> reply = dbusUnitySession->call(method);
65+ QCOMPARE(reply.isValid(), true);
66+
67+ QCOMPARE(spy.count(), 1);
68+ }
69+
70+ void testGnomeSessionWrapperLogoutRequested() {
71+ DBusUnitySessionService dbusUnitySessionService;
72+ QCoreApplication::processEvents(); // to let the service register on DBus
73+
74+ // Spy on the logoutRequested signal on the /com/canonical/Unity/Session object
75+ // as proof we are actually calling the actual method.
76+ QSignalSpy spy(&dbusUnitySessionService, SIGNAL(logoutRequested(bool)));
77+
78+ DBusGnomeSessionManagerWrapper dbusGnomeSessionManagerWrapper;
79+ QCoreApplication::processEvents(); // to let the service register on DBus
80+
51− QDBusInterface interface ("com.canonical.Unity",
81+ QDBusInterface dbusGnomeSessionWrapper ("com.canonical.Unity",
52− "/com/canonical/Unity/Session",
82+ "/org/gnome/SessionManager/EndSessionDialog",
53− "com.canonical.Unity.Session",
83+ "org.gnome.SessionManager.EndSessionDialog",
54− con);
84+ QDBusConnection::sessionBus());
55− QDBusReply<void> reply;
85+
86+ QCOMPARE(dbusGnomeSessionWrapper.isValid(), true);
87+
88+ // Set the QVariant as a QList<QDBusObjectPath> type
89+ QDbusList var;
90+ QVariant inhibitors;
91+ inhibitors.setValue(var);
92+
93+ QDBusReply<void> reply = dbusGnomeSessionWrapper.call("Open", (unsigned)Action::LOGOUT, (unsigned)0, (unsigned)0, inhibitors);
94+ QCOMPARE(reply.isValid(), true);
95+
96+ // Make sure we see the signal being emitted.
97+ QCOMPARE(spy.count(), 1);
98+ }
99+
100+ void testGnomeSessionWrapperShutdownRequested() {
101+ DBusUnitySessionService dbusUnitySessionService;
102+ QCoreApplication::processEvents(); // to let the service register on DBus
103+
104+ // Spy on the shutdownRequested signal on the /com/canonical/Unity/Session object
105+ // as proof we are actually calling the actual method.
106+ QSignalSpy spy(&dbusUnitySessionService, SIGNAL(shutdownRequested(bool)));
107+
108+ DBusGnomeSessionManagerWrapper dbusGnomeSessionManagerWrapper;
109+ QCoreApplication::processEvents(); // to let the service register on DBus
110+
111+ QDBusInterface dbusGnomeSessionWrapper ("com.canonical.Unity",
112+ "/org/gnome/SessionManager/EndSessionDialog",
113+ "org.gnome.SessionManager.EndSessionDialog",
114+ QDBusConnection::sessionBus());
56
115
57− QCOMPARE(interface.isValid(), true);
116+ QCOMPARE(dbusGnomeSessionWrapper.isValid(), true);
58− reply = interface.call(method);
117+
118+ // Set the QVariant as a QList<QDBusObjectPath> type
119+ QDbusList var;
120+ QVariant inhibitors;
121+ inhibitors.setValue(var);
122+
123+ // * Reboot action translates to the shutdown signal due to some weird idiosyncracy
124+ // in the indicator-session/Unity interaction. *
125+ QDBusReply<void> reply = dbusGnomeSessionWrapper.call("Open", (unsigned)Action::REBOOT, (unsigned)0, (unsigned)0, inhibitors);
59 QCOMPARE(reply.isValid(), true);
126 QCOMPARE(reply.isValid(), true);
60
127
128+ // Make sure we see the signal being emitted.
129+ QCOMPARE(spy.count(), 1);
61 }
130 }
131+
132+private:
133+ QDBusInterface *dbusUnitySession;
62};
134};
63
135
64QTEST_GUILESS_MAIN(SessionBackendTest)
136QTEST_GUILESS_MAIN(SessionBackendTest)