File: dbus.cpp

package info (click to toggle)
lomiri 0.5.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,088 kB
  • sloc: cpp: 39,498; python: 2,559; javascript: 1,426; ansic: 1,012; sh: 289; xml: 252; makefile: 69
file content (223 lines) | stat: -rw-r--r-- 7,581 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
 * Copyright (C) 2013 Canonical Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "Greeter.h"
#include "PromptsModel.h"

#include <QCoreApplication>
#include <QDBusInterface>
#include <QDBusReply>
#include <QSignalSpy>
#include <QQuickItem>
#include <QQuickView>
#include <QtTestGui>
#include <paths.h>

class GreeterDBusTest : public QObject
{
    Q_OBJECT

Q_SIGNALS:
    void PropertiesChangedRelay(const QString &interface, const QVariantMap &changed, const QStringList &invalidated);

private Q_SLOTS:

    void initTestCase()
    {
        // Qt doesn't like us connecting to PropertiesChanged using normal
        // SIGNAL method, because QtDBus doesn't know about PropertiesChanged.
        // So we connect the hard way for the benefit of any tests that want
        // to watch.
        QDBusConnection::sessionBus().connect(
            "com.lomiri.LomiriGreeter",
            "/com/lomiri/LomiriGreeter",
            "org.freedesktop.DBus.Properties",
            "PropertiesChanged",
            this,
            SIGNAL(PropertiesChangedRelay(const QString&, const QVariantMap&, const QStringList&)));
        QDBusConnection::sessionBus().connect(
            "com.lomiri.LomiriGreeter",
            "/com/lomiri/LomiriGreeter/list",
            "org.freedesktop.DBus.Properties",
            "PropertiesChanged",
            this,
            SIGNAL(PropertiesChangedRelay(const QString&, const QVariantMap&, const QStringList&)));
    }

    void init()
    {
        view = new QQuickView();
        view->setSource(QUrl::fromLocalFile(testDataDir() + "/plugins/LightDM/IntegratedLightDM/greeter.qml"));
        greeter = dynamic_cast<Greeter*>(view->rootObject()->property("greeter").value<QObject*>());
        QVERIFY(greeter);
        QVERIFY(greeter->authenticationUser() == "");
        view->show();
        QVERIFY(QTest::qWaitForWindowExposed(view));

        dbusMain = new QDBusInterface("com.lomiri.LomiriGreeter",
                                      "/com/lomiri/LomiriGreeter",
                                      "com.lomiri.LomiriGreeter",
                                      QDBusConnection::sessionBus(), view);
        QVERIFY(dbusMain->isValid());

        dbusList = new QDBusInterface("com.lomiri.LomiriGreeter",
                                      "/com/lomiri/LomiriGreeter/list",
                                      "com.lomiri.LomiriGreeter.List",
                                      QDBusConnection::sessionBus(), view);
        QVERIFY(dbusList->isValid());
    }

    void cleanup()
    {
        delete view;
        delete greeter; // reset singleton
    }

    void testGetActiveEntry()
    {
        greeter->authenticate("has-password");

        QDBusReply<QString> reply = dbusList->call("GetActiveEntry");
        QVERIFY(reply.isValid());
        QVERIFY(reply.value() == "has-password");
    }

    void testSetActiveEntry()
    {
        QSignalSpy spy(greeter, &Greeter::requestAuthenticationUser);
        QDBusReply<void> reply = dbusList->call("SetActiveEntry", "has-password");
        QVERIFY(reply.isValid());
        spy.wait();

        QCOMPARE(spy.count(), 1);
        QList<QVariant> arguments = spy.takeFirst();
        QVERIFY(arguments.at(0).toString() == "has-password");
    }

    void testEntrySelectedSignal()
    {
        QSignalSpy spy(dbusList, SIGNAL(EntrySelected(QString)));
        greeter->authenticate("has-password");
        spy.wait();

        QCOMPARE(spy.count(), 1);
        QList<QVariant> arguments = spy.takeFirst();
        QVERIFY(arguments.at(0).toString() == "has-password");
    }

    void testActiveEntryGet()
    {
        greeter->authenticate("has-password");
        QVERIFY(dbusList->property("ActiveEntry").toString() == "has-password");
    }

    void testActiveEntrySet()
    {
        QSignalSpy spy(greeter, &Greeter::requestAuthenticationUser);
        QVERIFY(dbusList->setProperty("ActiveEntry", "has-password"));
        spy.wait();

        QCOMPARE(spy.count(), 1);
        QList<QVariant> arguments = spy.takeFirst();
        QVERIFY(arguments.at(0).toString() == "has-password");
    }

    void testActiveEntryChanged()
    {
        QSignalSpy spy(this, &GreeterDBusTest::PropertiesChangedRelay);

        greeter->authenticate("has-password");

        // EntryIsLocked's default is true, thus only single property change.
        QTRY_COMPARE(spy.count(), 1);

        QList<QVariant> arguments = spy[0];
        QVERIFY(arguments.at(0).toString() == "com.lomiri.LomiriGreeter.List");
        QVERIFY(arguments.at(1).toMap().contains("ActiveEntry"));
        QVERIFY(arguments.at(1).toMap()["ActiveEntry"] == "has-password");
    }

    void testEntryIsLockedGet()
    {
        QVERIFY(dbusList->property("EntryIsLocked").toBool());

        greeter->authenticate("no-password");
        QTRY_VERIFY(!dbusList->property("EntryIsLocked").toBool());

        QSignalSpy promptSpy(greeter->promptsModel(), &PromptsModel::countChanged);
        QSignalSpy successSpy(greeter, &Greeter::loginSuccess);

        greeter->authenticate("has-password");
        QTRY_VERIFY(dbusList->property("EntryIsLocked").toBool());
        // Make sure that EntryIsLocked stays `true` after authenticated.
        // Regression test for https://github.com/ubports/lomiri-touch/issues/1406
        promptSpy.wait();
        greeter->respond(QStringLiteral("password"));
        successSpy.wait();
        QVERIFY(dbusList->property("EntryIsLocked").toBool());
    }

    void testEntryIsLockedChanged()
    {
        QSignalSpy spy(this, &GreeterDBusTest::PropertiesChangedRelay);

        greeter->authenticate("no-password");

        // ActiveEntry, then EntryIsLocked=false
        QTRY_COMPARE(spy.count(), 2);

        QList<QVariant> arguments = spy[1];
        QVERIFY(arguments.at(0).toString() == "com.lomiri.LomiriGreeter.List");
        QVERIFY(arguments.at(1).toMap().contains("EntryIsLocked"));
        QVERIFY(arguments.at(1).toMap()["EntryIsLocked"] == false);
    }

    void testIsActive()
    {
        QVERIFY(!greeter->isActive());
        QVERIFY(!dbusMain->property("IsActive").toBool());

        QSignalSpy spy(this, &GreeterDBusTest::PropertiesChangedRelay);
        greeter->setIsActive(true);
        spy.wait();

        QVERIFY(greeter->isActive());
        QVERIFY(dbusMain->property("IsActive").toBool());

        QCOMPARE(spy.count(), 1);
        QList<QVariant> arguments = spy.takeFirst();
        QCOMPARE(arguments.at(0).toString(), QString("com.lomiri.LomiriGreeter"));
        QVERIFY(arguments.at(1).toMap().contains("IsActive"));
        QVERIFY(arguments.at(1).toMap()["IsActive"].toBool());
    }

    void testShowGreeter()
    {
        // Just confirm the call exists and doesn't fail
        QDBusReply<void> reply = dbusMain->call("ShowGreeter");
        QVERIFY(reply.isValid());
    }

private:
    QQuickView *view;
    Greeter *greeter;
    QDBusInterface *dbusMain;
    QDBusInterface *dbusList;
};

QTEST_MAIN(GreeterDBusTest)

#include "dbus.moc"