on ded455e help sign in
215 lines 7.2 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>
27class GreeterDBusTest : public QObject
29 Q_OBJECT
31Q_SIGNALS:
32 void PropertiesChangedRelay(const QString &interface, const QVariantMap &changed, const QStringList &invalidated);
34private Q_SLOTS:
36 void initTestCase()
37 {
38 // Qt doesn't like us connecting to PropertiesChanged using normal
39 // SIGNAL method, because QtDBus doesn't know about PropertiesChanged.
40 // So we connect the hard way for the benefit of any tests that want
41 // to watch.
42 QDBusConnection::sessionBus().connect(
43 "com.canonical.UnityGreeter",
44 "/",
45 "org.freedesktop.DBus.Properties",
46 "PropertiesChanged",
47 this,
48 SIGNAL(PropertiesChangedRelay(const QString&, const QVariantMap&, const QStringList&)));
49 QDBusConnection::sessionBus().connect(
50 "com.canonical.UnityGreeter",
51 "/list",
52 "org.freedesktop.DBus.Properties",
53 "PropertiesChanged",
54 this,
55 SIGNAL(PropertiesChangedRelay(const QString&, const QVariantMap&, const QStringList&)));
56 }
58 void init()
59 {
60 view = new QQuickView();
61 view->setSource(QUrl::fromLocalFile(CURRENT_SOURCE_DIR "/greeter.qml"));
62 greeter = dynamic_cast<Greeter*>(view->rootObject()->property("greeter").value<QObject*>());
63 QVERIFY(greeter);
64 QVERIFY(greeter->authenticationUser() == "");
65 view->show();
66 QTest::qWaitForWindowExposed(view);
68 dbusMain = new QDBusInterface("com.canonical.UnityGreeter",
69 "/",
70 "com.canonical.UnityGreeter",
71 QDBusConnection::sessionBus(), view);
72 QVERIFY(dbusMain->isValid());
74 dbusList = new QDBusInterface("com.canonical.UnityGreeter",
75 "/list",
76 "com.canonical.UnityGreeter.List",
77 QDBusConnection::sessionBus(), view);
78 QVERIFY(dbusList->isValid());
79 }
81 void cleanup()
82 {
83 delete view;
84 }
86 void testGetActiveEntry()
87 {
88 greeter->authenticate("has-password");
90 QDBusReply<QString> reply = dbusList->call("GetActiveEntry");
91 QVERIFY(reply.isValid());
92 QVERIFY(reply.value() == "has-password");
93 }
95 void testSetActiveEntry()
96 {
97 QSignalSpy spy(greeter, &Greeter::requestAuthenticationUser);
98 QDBusReply<void> reply = dbusList->call("SetActiveEntry", "has-password");
99 QVERIFY(reply.isValid());
100 spy.wait();
102 QCOMPARE(spy.count(), 1);
103 QList<QVariant> arguments = spy.takeFirst();
104 QVERIFY(arguments.at(0).toString() == "has-password");
105 }
107 void testEntrySelectedSignal()
108 {
109 QSignalSpy spy(dbusList, SIGNAL(EntrySelected(QString)));
110 greeter->authenticate("has-password");
111 spy.wait();
113 QCOMPARE(spy.count(), 1);
114 QList<QVariant> arguments = spy.takeFirst();
115 QVERIFY(arguments.at(0).toString() == "has-password");
116 }
118 void testActiveEntryGet()
119 {
120 greeter->authenticate("has-password");
121 QVERIFY(dbusList->property("ActiveEntry").toString() == "has-password");
122 }
124 void testActiveEntrySet()
125 {
126 QSignalSpy spy(greeter, &Greeter::requestAuthenticationUser);
127 QVERIFY(dbusList->setProperty("ActiveEntry", "has-password"));
128 spy.wait();
130 QCOMPARE(spy.count(), 1);
131 QList<QVariant> arguments = spy.takeFirst();
132 QVERIFY(arguments.at(0).toString() == "has-password");
133 }
135 void testActiveEntryChanged()
136 {
137 QSignalSpy spy(this, &GreeterDBusTest::PropertiesChangedRelay);
138 greeter->authenticate("has-password");
139 spy.wait();
141 QCOMPARE(spy.count(), 1);
142 QList<QVariant> arguments = spy.takeFirst();
143 QVERIFY(arguments.at(0).toString() == "com.canonical.UnityGreeter.List");
144 QVERIFY(arguments.at(1).toMap().contains("ActiveEntry"));
145 QVERIFY(arguments.at(1).toMap()["ActiveEntry"] == "has-password");
146 }
148 void testEntryIsLockedGet()
149 {
150 QVERIFY(dbusList->property("EntryIsLocked").toBool());
152 greeter->authenticate("no-password");
153 QCoreApplication::processEvents(); // wait for auth to finish
154 QVERIFY(!dbusList->property("EntryIsLocked").toBool());
156 greeter->authenticate("has-password");
157 QCoreApplication::processEvents(); // wait for auth to finish
158 QVERIFY(dbusList->property("EntryIsLocked").toBool());
159 }
161 void testEntryIsLockedChanged()
162 {
163 QSignalSpy spy(this, &GreeterDBusTest::PropertiesChangedRelay);
164 greeter->authenticate("no-password");
166 // Two property changed signals will be emitted, one for the IsLocked
167 // property, one for the ActiveEntry; the first will be IsLocked.
168 spy.wait();
169 if (spy.count() < 2) {
170 spy.wait();
171 }
172 QCOMPARE(spy.count(), 2);
174 QList<QVariant> arguments = spy.takeLast();
175 QVERIFY(arguments.at(0).toString() == "com.canonical.UnityGreeter.List");
176 QVERIFY(arguments.at(1).toMap().contains("EntryIsLocked"));
177 QVERIFY(arguments.at(1).toMap()["EntryIsLocked"] == false);
178 }
180 void testIsActive()
181 {
182 QVERIFY(!greeter->isActive());
183 QVERIFY(!dbusMain->property("IsActive").toBool());
185 QSignalSpy spy(this, &GreeterDBusTest::PropertiesChangedRelay);
186 greeter->setIsActive(true);
187 spy.wait();
189 QVERIFY(greeter->isActive());
190 QVERIFY(dbusMain->property("IsActive").toBool());
192 QCOMPARE(spy.count(), 1);
193 QList<QVariant> arguments = spy.takeFirst();
194 QCOMPARE(arguments.at(0).toString(), QString("com.canonical.UnityGreeter"));
195 QVERIFY(arguments.at(1).toMap().contains("IsActive"));
196 QVERIFY(arguments.at(1).toMap()["IsActive"].toBool());
197 }
199 void testShowGreeter()
200 {
201 // Just confirm the call exists and doesn't fail
202 QDBusReply<void> reply = dbusMain->call("ShowGreeter");
203 QVERIFY(reply.isValid());
204 }
206private:
207 QQuickView *view;
208 Greeter *greeter;
209 QDBusInterface *dbusMain;
210 QDBusInterface *dbusList;
211};
213QTEST_MAIN(GreeterDBusTest)
215#include "dbus.moc"