on 16ab292 help sign in
223 lines 7.5 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"
18#include "PromptsModel.h"
20#include <QCoreApplication>
21#include <QDBusInterface>
22#include <QDBusReply>
23#include <QSignalSpy>
24#include <QQuickItem>
25#include <QQuickView>
26#include <QtTestGui>
27#include <paths.h>
29class GreeterDBusTest : public QObject
31 Q_OBJECT
33Q_SIGNALS:
34 void PropertiesChangedRelay(const QString &interface, const QVariantMap &changed, const QStringList &invalidated);
36private Q_SLOTS:
38 void initTestCase()
39 {
40 // Qt doesn't like us connecting to PropertiesChanged using normal
41 // SIGNAL method, because QtDBus doesn't know about PropertiesChanged.
42 // So we connect the hard way for the benefit of any tests that want
43 // to watch.
44 QDBusConnection::sessionBus().connect(
45 "com.canonical.UnityGreeter",
46 "/",
47 "org.freedesktop.DBus.Properties",
48 "PropertiesChanged",
49 this,
50 SIGNAL(PropertiesChangedRelay(const QString&, const QVariantMap&, const QStringList&)));
51 QDBusConnection::sessionBus().connect(
52 "com.canonical.UnityGreeter",
53 "/list",
54 "org.freedesktop.DBus.Properties",
55 "PropertiesChanged",
56 this,
57 SIGNAL(PropertiesChangedRelay(const QString&, const QVariantMap&, const QStringList&)));
58 }
60 void init()
61 {
62 view = new QQuickView();
63 view->setSource(QUrl::fromLocalFile(testDataDir() + "/plugins/LightDM/IntegratedLightDM/greeter.qml"));
64 greeter = dynamic_cast<Greeter*>(view->rootObject()->property("greeter").value<QObject*>());
65 QVERIFY(greeter);
66 QVERIFY(greeter->authenticationUser() == "");
67 view->show();
68 QTest::qWaitForWindowExposed(view);
70 dbusMain = new QDBusInterface("com.canonical.UnityGreeter",
71 "/",
72 "com.canonical.UnityGreeter",
73 QDBusConnection::sessionBus(), view);
74 QVERIFY(dbusMain->isValid());
76 dbusList = new QDBusInterface("com.canonical.UnityGreeter",
77 "/list",
78 "com.canonical.UnityGreeter.List",
79 QDBusConnection::sessionBus(), view);
80 QVERIFY(dbusList->isValid());
81 }
83 void cleanup()
84 {
85 delete view;
86 delete greeter; // reset singleton
87 }
89 void testGetActiveEntry()
90 {
91 greeter->authenticate("has-password");
93 QDBusReply<QString> reply = dbusList->call("GetActiveEntry");
94 QVERIFY(reply.isValid());
95 QVERIFY(reply.value() == "has-password");
96 }
98 void testSetActiveEntry()
99 {
100 QSignalSpy spy(greeter, &Greeter::requestAuthenticationUser);
101 QDBusReply<void> reply = dbusList->call("SetActiveEntry", "has-password");
102 QVERIFY(reply.isValid());
103 spy.wait();
105 QCOMPARE(spy.count(), 1);
106 QList<QVariant> arguments = spy.takeFirst();
107 QVERIFY(arguments.at(0).toString() == "has-password");
108 }
110 void testEntrySelectedSignal()
111 {
112 QSignalSpy spy(dbusList, SIGNAL(EntrySelected(QString)));
113 greeter->authenticate("has-password");
114 spy.wait();
116 QCOMPARE(spy.count(), 1);
117 QList<QVariant> arguments = spy.takeFirst();
118 QVERIFY(arguments.at(0).toString() == "has-password");
119 }
121 void testActiveEntryGet()
122 {
123 greeter->authenticate("has-password");
124 QVERIFY(dbusList->property("ActiveEntry").toString() == "has-password");
125 }
127 void testActiveEntrySet()
128 {
129 QSignalSpy spy(greeter, &Greeter::requestAuthenticationUser);
130 QVERIFY(dbusList->setProperty("ActiveEntry", "has-password"));
131 spy.wait();
133 QCOMPARE(spy.count(), 1);
134 QList<QVariant> arguments = spy.takeFirst();
135 QVERIFY(arguments.at(0).toString() == "has-password");
136 }
138 void testActiveEntryChanged()
139 {
140 QSignalSpy spy(this, &GreeterDBusTest::PropertiesChangedRelay);
142 greeter->authenticate("has-password");
144 // EntryIsLocked's default is true, thus only single property change.
145 QTRY_COMPARE(spy.count(), 1);
147 QList<QVariant> arguments = spy[0];
148 QVERIFY(arguments.at(0).toString() == "com.canonical.UnityGreeter.List");
149 QVERIFY(arguments.at(1).toMap().contains("ActiveEntry"));
150 QVERIFY(arguments.at(1).toMap()["ActiveEntry"] == "has-password");
151 }
153 void testEntryIsLockedGet()
154 {
155 QVERIFY(dbusList->property("EntryIsLocked").toBool());
157 greeter->authenticate("no-password");
158 QTRY_VERIFY(!dbusList->property("EntryIsLocked").toBool());
160 QSignalSpy promptSpy(greeter->promptsModel(), &PromptsModel::countChanged);
161 QSignalSpy successSpy(greeter, &Greeter::loginSuccess);
163 greeter->authenticate("has-password");
164 QTRY_VERIFY(dbusList->property("EntryIsLocked").toBool());
165 // Make sure that EntryIsLocked stays `true` after authenticated.
166 // Regression test for https://github.com/ubports/ubuntu-touch/issues/1406
167 promptSpy.wait();
168 greeter->respond(QStringLiteral("password"));
169 successSpy.wait();
170 QVERIFY(dbusList->property("EntryIsLocked").toBool());
171 }
173 void testEntryIsLockedChanged()
174 {
175 QSignalSpy spy(this, &GreeterDBusTest::PropertiesChangedRelay);
177 greeter->authenticate("no-password");
179 // ActiveEntry, then EntryIsLocked=false
180 QTRY_COMPARE(spy.count(), 2);
182 QList<QVariant> arguments = spy[1];
183 QVERIFY(arguments.at(0).toString() == "com.canonical.UnityGreeter.List");
184 QVERIFY(arguments.at(1).toMap().contains("EntryIsLocked"));
185 QVERIFY(arguments.at(1).toMap()["EntryIsLocked"] == false);
186 }
188 void testIsActive()
189 {
190 QVERIFY(!greeter->isActive());
191 QVERIFY(!dbusMain->property("IsActive").toBool());
193 QSignalSpy spy(this, &GreeterDBusTest::PropertiesChangedRelay);
194 greeter->setIsActive(true);
195 spy.wait();
197 QVERIFY(greeter->isActive());
198 QVERIFY(dbusMain->property("IsActive").toBool());
200 QCOMPARE(spy.count(), 1);
201 QList<QVariant> arguments = spy.takeFirst();
202 QCOMPARE(arguments.at(0).toString(), QString("com.canonical.UnityGreeter"));
203 QVERIFY(arguments.at(1).toMap().contains("IsActive"));
204 QVERIFY(arguments.at(1).toMap()["IsActive"].toBool());
205 }
207 void testShowGreeter()
208 {
209 // Just confirm the call exists and doesn't fail
210 QDBusReply<void> reply = dbusMain->call("ShowGreeter");
211 QVERIFY(reply.isValid());
212 }
214private:
215 QQuickView *view;
216 Greeter *greeter;
217 QDBusInterface *dbusMain;
218 QDBusInterface *dbusList;
219};
221QTEST_MAIN(GreeterDBusTest)
223#include "dbus.moc"