on f621663 help sign in
206 lines 6.9 kB history raw
1/*
2 * Copyright (C) 2013 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU 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 General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
17#include "Greeter.h"
19#include <QDBusInterface>
20#include <QDBusReply>
21#include <QSignalSpy>
22#include <QQuickItem>
23#include <QQuickView>
24#include <QtTestGui>
26class GreeterDBusTest : public QObject
28 Q_OBJECT
30Q_SIGNALS:
31 void PropertiesChangedRelay(const QString &interface, const QVariantMap &changed, const QStringList &invalidated);
33private Q_SLOTS:
35 void initTestCase()
36 {
37 // Qt doesn't like us connecting to PropertiesChanged using normal
38 // SIGNAL method, because QtDBus doesn't know about PropertiesChanged.
39 // So we connect the hard way for the benefit of any tests that want
40 // to watch.
41 QDBusConnection::sessionBus().connect(
42 "com.canonical.UnityGreeter",
43 "/",
44 "org.freedesktop.DBus.Properties",
45 "PropertiesChanged",
46 this,
47 SIGNAL(PropertiesChangedRelay(const QString&, const QVariantMap&, const QStringList&)));
48 QDBusConnection::sessionBus().connect(
49 "com.canonical.UnityGreeter",
50 "/list",
51 "org.freedesktop.DBus.Properties",
52 "PropertiesChanged",
53 this,
54 SIGNAL(PropertiesChangedRelay(const QString&, const QVariantMap&, const QStringList&)));
55 }
57 void init()
58 {
59 view = new QQuickView();
60 view->setSource(QUrl::fromLocalFile(CURRENT_SOURCE_DIR "/greeter.qml"));
61 greeter = dynamic_cast<Greeter*>(view->rootObject()->property("greeter").value<QObject*>());
62 QVERIFY(greeter);
63 QVERIFY(greeter->authenticationUser() == "");
64 view->show();
65 QTest::qWaitForWindowExposed(view);
67 dbusMain = new QDBusInterface("com.canonical.UnityGreeter",
68 "/",
69 "com.canonical.UnityGreeter",
70 QDBusConnection::sessionBus(), view);
71 QVERIFY(dbusMain->isValid());
73 dbusList = new QDBusInterface("com.canonical.UnityGreeter",
74 "/list",
75 "com.canonical.UnityGreeter.List",
76 QDBusConnection::sessionBus(), view);
77 QVERIFY(dbusList->isValid());
78 }
80 void cleanup()
81 {
82 delete view;
83 }
85 void testGetActiveEntry()
86 {
87 greeter->authenticate("has-password");
89 QDBusReply<QString> reply = dbusList->call("GetActiveEntry");
90 QVERIFY(reply.isValid());
91 QVERIFY(reply.value() == "has-password");
92 }
94 void testSetActiveEntry()
95 {
96 QSignalSpy spy(greeter, SIGNAL(requestAuthenticationUser(QString)));
97 QDBusReply<void> reply = dbusList->call("SetActiveEntry", "has-password");
98 QVERIFY(reply.isValid());
99 spy.wait();
101 QCOMPARE(spy.count(), 1);
102 QList<QVariant> arguments = spy.takeFirst();
103 QVERIFY(arguments.at(0).toString() == "has-password");
104 }
106 void testEntrySelectedSignal()
107 {
108 QSignalSpy spy(dbusList, SIGNAL(EntrySelected(QString)));
109 greeter->authenticate("has-password");
110 spy.wait();
112 QCOMPARE(spy.count(), 1);
113 QList<QVariant> arguments = spy.takeFirst();
114 QVERIFY(arguments.at(0).toString() == "has-password");
115 }
117 void testActiveEntryGet()
118 {
119 greeter->authenticate("has-password");
120 QVERIFY(dbusList->property("ActiveEntry").toString() == "has-password");
121 }
123 void testActiveEntrySet()
124 {
125 QSignalSpy spy(greeter, SIGNAL(requestAuthenticationUser(QString)));
126 QVERIFY(dbusList->setProperty("ActiveEntry", "has-password"));
127 spy.wait();
129 QCOMPARE(spy.count(), 1);
130 QList<QVariant> arguments = spy.takeFirst();
131 QVERIFY(arguments.at(0).toString() == "has-password");
132 }
134 void testActiveEntryChanged()
135 {
136 QSignalSpy spy(this, SIGNAL(PropertiesChangedRelay(QString, QVariantMap, QStringList)));
137 greeter->authenticate("has-password");
138 spy.wait();
140 QCOMPARE(spy.count(), 1);
141 QList<QVariant> arguments = spy.takeFirst();
142 QVERIFY(arguments.at(0).toString() == "com.canonical.UnityGreeter.List");
143 QVERIFY(arguments.at(1).toMap().contains("ActiveEntry"));
144 QVERIFY(arguments.at(1).toMap()["ActiveEntry"] == "has-password");
145 }
147 void testEntryIsLockedGet()
148 {
149 QVERIFY(dbusList->property("EntryIsLocked").toBool());
151 greeter->authenticate("no-password");
152 QVERIFY(!dbusList->property("EntryIsLocked").toBool());
154 greeter->authenticate("has-password");
155 QVERIFY(dbusList->property("EntryIsLocked").toBool());
156 }
158 void testEntryIsLockedChanged()
159 {
160 QSignalSpy spy(this, SIGNAL(PropertiesChangedRelay(QString, QVariantMap, QStringList)));
161 greeter->authenticate("no-password");
162 spy.wait();
164 QCOMPARE(spy.count(), 2); // once for locked, once for user; first will be locked mode
165 QList<QVariant> arguments = spy.takeFirst();
166 QVERIFY(arguments.at(0).toString() == "com.canonical.UnityGreeter.List");
167 QVERIFY(arguments.at(1).toMap().contains("EntryIsLocked"));
168 QVERIFY(arguments.at(1).toMap()["EntryIsLocked"] == false);
169 }
171 void testIsActive()
172 {
173 QVERIFY(!greeter->isActive());
174 QVERIFY(!dbusMain->property("IsActive").toBool());
176 QSignalSpy spy(this, SIGNAL(PropertiesChangedRelay(QString, QVariantMap, QStringList)));
177 greeter->setIsActive(true);
178 spy.wait();
180 QVERIFY(greeter->isActive());
181 QVERIFY(dbusMain->property("IsActive").toBool());
183 QCOMPARE(spy.count(), 1);
184 QList<QVariant> arguments = spy.takeFirst();
185 QCOMPARE(arguments.at(0).toString(), QString("com.canonical.UnityGreeter"));
186 QVERIFY(arguments.at(1).toMap().contains("IsActive"));
187 QVERIFY(arguments.at(1).toMap()["IsActive"].toBool());
188 }
190 void testShowGreeter()
191 {
192 // Just confirm the call exists and doesn't fail
193 QDBusReply<void> reply = dbusMain->call("ShowGreeter");
194 QVERIFY(reply.isValid());
195 }
197private:
198 QQuickView *view;
199 Greeter *greeter;
200 QDBusInterface *dbusMain;
201 QDBusInterface *dbusList;
202};
204QTEST_MAIN(GreeterDBusTest)
206#include "dbus.moc"