on 657fdca help sign in
166 lines 5.4 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 "/list",
44 "org.freedesktop.DBus.Properties",
45 "PropertiesChanged",
46 this,
47 SIGNAL(PropertiesChangedRelay(const QString&, const QVariantMap&, const QStringList&)));
48 }
50 void init()
51 {
52 view = new QQuickView();
53 view->setSource(QUrl::fromLocalFile(CURRENT_SOURCE_DIR "/greeter.qml"));
54 greeter = dynamic_cast<Greeter*>(view->rootObject()->property("greeter").value<QObject*>());
55 QVERIFY(greeter);
56 QVERIFY(greeter->authenticationUser() == "");
57 view->show();
58 QTest::qWaitForWindowExposed(view);
60 dbusList = new QDBusInterface("com.canonical.UnityGreeter",
61 "/list",
62 "com.canonical.UnityGreeter.List",
63 QDBusConnection::sessionBus(), view);
64 QVERIFY(dbusList->isValid());
65 }
67 void cleanup()
68 {
69 delete view;
70 }
72 void testGetActiveEntry()
73 {
74 greeter->authenticate("has-password");
76 QDBusReply<QString> reply = dbusList->call("GetActiveEntry");
77 QVERIFY(reply.isValid());
78 QVERIFY(reply.value() == "has-password");
79 }
81 void testSetActiveEntry()
82 {
83 QSignalSpy spy(greeter, SIGNAL(requestAuthenticationUser(QString)));
84 QDBusReply<void> reply = dbusList->call("SetActiveEntry", "has-password");
85 QVERIFY(reply.isValid());
86 spy.wait();
88 QCOMPARE(spy.count(), 1);
89 QList<QVariant> arguments = spy.takeFirst();
90 QVERIFY(arguments.at(0).toString() == "has-password");
91 }
93 void testEntrySelectedSignal()
94 {
95 QSignalSpy spy(dbusList, SIGNAL(EntrySelected(QString)));
96 greeter->authenticate("has-password");
97 spy.wait();
99 QCOMPARE(spy.count(), 1);
100 QList<QVariant> arguments = spy.takeFirst();
101 QVERIFY(arguments.at(0).toString() == "has-password");
102 }
104 void testActiveEntryGet()
105 {
106 greeter->authenticate("has-password");
107 QVERIFY(dbusList->property("ActiveEntry").toString() == "has-password");
108 }
110 void testActiveEntrySet()
111 {
112 QSignalSpy spy(greeter, SIGNAL(requestAuthenticationUser(QString)));
113 QVERIFY(dbusList->setProperty("ActiveEntry", "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 testActiveEntryChanged()
122 {
123 QSignalSpy spy(this, SIGNAL(PropertiesChangedRelay(QString, QVariantMap, QStringList)));
124 greeter->authenticate("has-password");
125 spy.wait();
127 QCOMPARE(spy.count(), 1);
128 QList<QVariant> arguments = spy.takeFirst();
129 QVERIFY(arguments.at(0).toString() == "com.canonical.UnityGreeter.List");
130 QVERIFY(arguments.at(1).toMap().contains("ActiveEntry"));
131 QVERIFY(arguments.at(1).toMap()["ActiveEntry"] == "has-password");
132 }
134 void testEntryIsLockedGet()
135 {
136 QVERIFY(dbusList->property("EntryIsLocked").toBool());
138 greeter->authenticate("no-password");
139 QVERIFY(!dbusList->property("EntryIsLocked").toBool());
141 greeter->authenticate("has-password");
142 QVERIFY(dbusList->property("EntryIsLocked").toBool());
143 }
145 void testEntryIsLockedChanged()
146 {
147 QSignalSpy spy(this, SIGNAL(PropertiesChangedRelay(QString, QVariantMap, QStringList)));
148 greeter->authenticate("no-password");
149 spy.wait();
151 QCOMPARE(spy.count(), 2); // once for locked, once for user; first will be locked mode
152 QList<QVariant> arguments = spy.takeFirst();
153 QVERIFY(arguments.at(0).toString() == "com.canonical.UnityGreeter.List");
154 QVERIFY(arguments.at(1).toMap().contains("EntryIsLocked"));
155 QVERIFY(arguments.at(1).toMap()["EntryIsLocked"] == false);
156 }
158private:
159 QQuickView *view;
160 Greeter *greeter;
161 QDBusInterface *dbusList;
162};
164QTEST_MAIN(GreeterDBusTest)
166#include "dbus.moc"