on e4400a2 help sign in
214 lines 7.1 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 <QCoreApplication>
20#include <QDBusInterface>
21#include <QDBusReply>
22#include <QSignalSpy>
23#include <QQuickItem>
24#include <QQuickView>
25#include <QtTestGui>
26#include <paths.h>
28class GreeterDBusTest : public QObject
30 Q_OBJECT
32Q_SIGNALS:
33 void PropertiesChangedRelay(const QString &interface, const QVariantMap &changed, const QStringList &invalidated);
35private Q_SLOTS:
37 void initTestCase()
38 {
39 // Qt doesn't like us connecting to PropertiesChanged using normal
40 // SIGNAL method, because QtDBus doesn't know about PropertiesChanged.
41 // So we connect the hard way for the benefit of any tests that want
42 // to watch.
43 QDBusConnection::sessionBus().connect(
44 "com.canonical.UnityGreeter",
45 "/",
46 "org.freedesktop.DBus.Properties",
47 "PropertiesChanged",
48 this,
49 SIGNAL(PropertiesChangedRelay(const QString&, const QVariantMap&, const QStringList&)));
50 QDBusConnection::sessionBus().connect(
51 "com.canonical.UnityGreeter",
52 "/list",
53 "org.freedesktop.DBus.Properties",
54 "PropertiesChanged",
55 this,
56 SIGNAL(PropertiesChangedRelay(const QString&, const QVariantMap&, const QStringList&)));
57 }
59 void init()
60 {
61 view = new QQuickView();
62 view->setSource(QUrl::fromLocalFile(testDataDir() + "/plugins/LightDM/IntegratedLightDM/greeter.qml"));
63 greeter = dynamic_cast<Greeter*>(view->rootObject()->property("greeter").value<QObject*>());
64 QVERIFY(greeter);
65 QVERIFY(greeter->authenticationUser() == "");
66 view->show();
67 QTest::qWaitForWindowExposed(view);
69 dbusMain = new QDBusInterface("com.canonical.UnityGreeter",
70 "/",
71 "com.canonical.UnityGreeter",
72 QDBusConnection::sessionBus(), view);
73 QVERIFY(dbusMain->isValid());
75 dbusList = new QDBusInterface("com.canonical.UnityGreeter",
76 "/list",
77 "com.canonical.UnityGreeter.List",
78 QDBusConnection::sessionBus(), view);
79 QVERIFY(dbusList->isValid());
80 }
82 void cleanup()
83 {
84 delete view;
85 }
87 void testGetActiveEntry()
88 {
89 greeter->authenticate("has-password");
91 QDBusReply<QString> reply = dbusList->call("GetActiveEntry");
92 QVERIFY(reply.isValid());
93 QVERIFY(reply.value() == "has-password");
94 }
96 void testSetActiveEntry()
97 {
98 QSignalSpy spy(greeter, &Greeter::requestAuthenticationUser);
99 QDBusReply<void> reply = dbusList->call("SetActiveEntry", "has-password");
100 QVERIFY(reply.isValid());
101 spy.wait();
103 QCOMPARE(spy.count(), 1);
104 QList<QVariant> arguments = spy.takeFirst();
105 QVERIFY(arguments.at(0).toString() == "has-password");
106 }
108 void testEntrySelectedSignal()
109 {
110 QSignalSpy spy(dbusList, SIGNAL(EntrySelected(QString)));
111 greeter->authenticate("has-password");
112 spy.wait();
114 QCOMPARE(spy.count(), 1);
115 QList<QVariant> arguments = spy.takeFirst();
116 QVERIFY(arguments.at(0).toString() == "has-password");
117 }
119 void testActiveEntryGet()
120 {
121 greeter->authenticate("has-password");
122 QVERIFY(dbusList->property("ActiveEntry").toString() == "has-password");
123 }
125 void testActiveEntrySet()
126 {
127 QSignalSpy spy(greeter, &Greeter::requestAuthenticationUser);
128 QVERIFY(dbusList->setProperty("ActiveEntry", "has-password"));
129 spy.wait();
131 QCOMPARE(spy.count(), 1);
132 QList<QVariant> arguments = spy.takeFirst();
133 QVERIFY(arguments.at(0).toString() == "has-password");
134 }
136 void testActiveEntryChanged()
137 {
138 QSignalSpy spy(this, &GreeterDBusTest::PropertiesChangedRelay);
139 greeter->authenticate("has-password");
140 spy.wait();
142 QVERIFY(spy.count() > 0);
143 QList<QVariant> arguments = spy.takeFirst();
144 QVERIFY(arguments.at(0).toString() == "com.canonical.UnityGreeter.List");
145 QVERIFY(arguments.at(1).toMap().contains("ActiveEntry"));
146 QVERIFY(arguments.at(1).toMap()["ActiveEntry"] == "has-password");
147 }
149 void testEntryIsLockedGet()
150 {
151 QVERIFY(dbusList->property("EntryIsLocked").toBool());
153 greeter->authenticate("no-password");
154 QTRY_VERIFY(!dbusList->property("EntryIsLocked").toBool());
156 greeter->authenticate("has-password");
157 QTRY_VERIFY(dbusList->property("EntryIsLocked").toBool());
158 }
160 void testEntryIsLockedChanged()
161 {
162 QSignalSpy spy(this, &GreeterDBusTest::PropertiesChangedRelay);
163 greeter->authenticate("no-password");
165 // Two property changed signals will be emitted, one for the IsLocked
166 // property, one for the ActiveEntry; the first will be IsLocked.
167 spy.wait();
168 if (spy.count() < 2) {
169 spy.wait();
170 }
171 QCOMPARE(spy.count(), 2);
173 QList<QVariant> arguments = spy.takeLast();
174 QVERIFY(arguments.at(0).toString() == "com.canonical.UnityGreeter.List");
175 QVERIFY(arguments.at(1).toMap().contains("EntryIsLocked"));
176 QVERIFY(arguments.at(1).toMap()["EntryIsLocked"] == false);
177 }
179 void testIsActive()
180 {
181 QVERIFY(!greeter->isActive());
182 QVERIFY(!dbusMain->property("IsActive").toBool());
184 QSignalSpy spy(this, &GreeterDBusTest::PropertiesChangedRelay);
185 greeter->setIsActive(true);
186 spy.wait();
188 QVERIFY(greeter->isActive());
189 QVERIFY(dbusMain->property("IsActive").toBool());
191 QCOMPARE(spy.count(), 1);
192 QList<QVariant> arguments = spy.takeFirst();
193 QCOMPARE(arguments.at(0).toString(), QString("com.canonical.UnityGreeter"));
194 QVERIFY(arguments.at(1).toMap().contains("IsActive"));
195 QVERIFY(arguments.at(1).toMap()["IsActive"].toBool());
196 }
198 void testShowGreeter()
199 {
200 // Just confirm the call exists and doesn't fail
201 QDBusReply<void> reply = dbusMain->call("ShowGreeter");
202 QVERIFY(reply.isValid());
203 }
205private:
206 QQuickView *view;
207 Greeter *greeter;
208 QDBusInterface *dbusMain;
209 QDBusInterface *dbusList;
210};
212QTEST_MAIN(GreeterDBusTest)
214#include "dbus.moc"