on xenial help sign in
Add tests for the DBusGnomeSessionWrapper.

Re-work the original DBus session test.

commit520e4ce51b56fca66f59143795c4aa2b4e14eb49 authorChris Townsend <christopher.townsend@canonical.com> dateTue 3 Feb 2015, 05:52 (12 years ago) parenta938179
2 files changed+88−13
plugins/Unity/Session/dbusunitysessionservice.h+6−3 tests/plugins/Unity/Session/sessionbackendtest.cpp+82−10
plugins/Unity/Session/dbusunitysessionservice.h+6−3
@@ -21,6 +21,9 @@
2121
2222#include "unitydbusobject.h"
2323
24+typedef QList<QDBusObjectPath> QDbusList;
25+Q_DECLARE_METATYPE(QList<QDBusObjectPath>)
26+
2427/**
2528 * DBusUnitySessionService provides com.canonical.Unity.Session dbus
2629 * interface.
@@ -51,7 +54,7 @@ Q_SIGNALS:
5154 * @param have_inhibitors if there are any special running applications
5255 * which inhibit the logout.
5356 */
54− void logoutRequested(bool have_inhibitors);
57+ Q_SCRIPTABLE void logoutRequested(bool have_inhibitors);
5558
5659 /**
5760 * rebootRequested signal
@@ -61,7 +64,7 @@ Q_SIGNALS:
6164 * @param have_inhibitors if there are any special running applications
6265 * which inhibit the reboot.
6366 */
64− void rebootRequested(bool have_inhibitors);
67+ Q_SCRIPTABLE void rebootRequested(bool have_inhibitors);
6568
6669 /**
6770 * shutdownRequested signal
@@ -71,7 +74,7 @@ Q_SIGNALS:
7174 * @param have_inhibitors if there are any special running applications
7275 * which inhibit the shutdown.
7376 */
74− void shutdownRequested(bool have_inhibitors);
77+ Q_SCRIPTABLE void shutdownRequested(bool have_inhibitors);
7578
7679
7780 /**
tests/plugins/Unity/Session/sessionbackendtest.cpp+82−10
@@ -26,6 +26,14 @@
2626
2727#include "dbusunitysessionservice.h"
2828
29+enum class Action : unsigned
30+{
31+ LOGOUT = 0,
32+ SHUTDOWN,
33+ REBOOT,
34+ NONE
35+};
36+
2937class SessionBackendTest : public QObject
3038{
3139 Q_OBJECT
@@ -33,32 +41,96 @@ class SessionBackendTest : public QObject
3341private Q_SLOTS:
3442
3543 void initTestCase() {
44+ dbusUnitySession = new QDBusInterface ("com.canonical.Unity",
45+ "/com/canonical/Unity/Session",
46+ "com.canonical.Unity.Session",
47+ QDBusConnection::sessionBus());
3648 }
3749
38− void testDbusIfaceMethods_data() {
50+ void testUnitySessionLogoutRequested_data() {
3951 QTest::addColumn<QString>("method");
4052
4153 QTest::newRow("Logout") << "RequestLogout";
4254 }
4355
44− void testDbusIfaceMethods() {
56+ void testUnitySessionLogoutRequested() {
4557 QFETCH(QString, method);
4658
4759 DBusUnitySessionService dbusUnitySessionService;
4860 QCoreApplication::processEvents(); // to let the service register on DBus
4961
50− QDBusConnection con = QDBusConnection::sessionBus();
51− QDBusInterface interface ("com.canonical.Unity",
52− "/com/canonical/Unity/Session",
53− "com.canonical.Unity.Session",
54− con);
55− QDBusReply<void> reply;
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+
81+ QDBusInterface dbusGnomeSessionWrapper ("com.canonical.Unity",
82+ "/org/gnome/SessionManager/EndSessionDialog",
83+ "org.gnome.SessionManager.EndSessionDialog",
84+ QDBusConnection::sessionBus());
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());
56115
57− QCOMPARE(interface.isValid(), true);
58− reply = interface.call(method);
116+ QCOMPARE(dbusGnomeSessionWrapper.isValid(), true);
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);
59126 QCOMPARE(reply.isValid(), true);
60127
128+ // Make sure we see the signal being emitted.
129+ QCOMPARE(spy.count(), 1);
61130 }
131+
132+private:
133+ QDBusInterface *dbusUnitySession;
62134};
63135
64136QTEST_GUILESS_MAIN(SessionBackendTest)