on 75b8c19 help sign in
265 lines 11 kB history raw
1/*
2 * Copyright 2013 Canonical Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 3, as published
6 * by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranties of
10 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
11 * PURPOSE. See the GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * version 3 along with this program. If not, see
15 * <http://www.gnu.org/licenses/>
16 *
17 * Authored by: Michael Terry <michael.terry@canonical.com>
18 */
20#include "AccountsService.h"
21#include "AccountsServiceDBusAdaptor.h"
22#include <QSignalSpy>
23#include <QTest>
24#include <QDebug>
25#include <QDBusReply>
27template <class T>
28QVariant dbusVariant(const T& value) { return QVariant::fromValue(QDBusVariant(value)); }
30#define ASSERT_DBUS_CALL(call) \
31 { \
32 QDBusReply<void> reply = call; \
33 if (!reply.isValid()) QFAIL(reply.error().message().toLatin1()); \
34 }
36class AccountsServiceTest : public QObject
38 Q_OBJECT
40public:
41 AccountsServiceTest(QObject* parent = 0)
42 : QObject(parent)
43 , m_userInterface(nullptr)
44 , m_spy(this, &AccountsServiceTest::propertiesChanged)
45 {
46 }
48private Q_SLOTS:
50 void init() {
51 QDBusReply<bool> addReply = QDBusInterface("org.freedesktop.Accounts",
52 "/org/freedesktop/Accounts",
53 "org.freedesktop.Accounts").call("AddUser", QTest::currentTestFunction());
54 QVERIFY(addReply.isValid());
55 QCOMPARE(addReply.value(), true);
57 m_userInterface = new QDBusInterface("org.freedesktop.Accounts",
58 QString("/%1").arg(QTest::currentTestFunction()),
59 "org.freedesktop.DBus.Properties", QDBusConnection::sessionBus(), this);
61 QVERIFY(QObject::connect(m_userInterface, SIGNAL(PropertiesChanged(QString, QVariantMap, QStringList)),
62 this, SIGNAL(propertiesChanged(QString, QVariantMap, QStringList))));
63 }
65 void cleanup() {
66 QDBusReply<bool> reply = QDBusInterface("org.freedesktop.Accounts",
67 "/org/freedesktop/Accounts",
68 "org.freedesktop.Accounts").call("RemoveUser", QTest::currentTestFunction());
69 QVERIFY(reply.isValid());
70 QCOMPARE(reply.value(), true);
72 delete m_userInterface;
73 m_spy.clear();
74 }
76 void testInvalids()
77 {
78 // Test various invalid calls
79 AccountsServiceDBusAdaptor session;
80 QCOMPARE(session.getUserPropertyAsync("NOPE", "com.canonical.unity.AccountsService", "demo-edges").value(), QVariant());
81 QCOMPARE(session.getUserPropertyAsync(QTest::currentTestFunction(), "com.canonical.unity.AccountsService", "NOPE").value(), QVariant());
82 }
84 void testGetSetServiceDBusAdaptor()
85 {
86 AccountsServiceDBusAdaptor session;
87 session.setUserPropertyAsync(QTest::currentTestFunction(), "com.canonical.unity.AccountsService", "demo-edges", QVariant(true)).waitForFinished();
88 QCOMPARE(session.getUserPropertyAsync(QTest::currentTestFunction(), "com.canonical.unity.AccountsService", "demo-edges").value(), QVariant(true));
89 session.setUserPropertyAsync(QTest::currentTestFunction(), "com.canonical.unity.AccountsService", "demo-edges", QVariant(false)).waitForFinished();
90 QCOMPARE(session.getUserPropertyAsync(QTest::currentTestFunction(), "com.canonical.unity.AccountsService", "demo-edges").value(), QVariant(false));
91 }
93 void testGetSetService()
94 {
95 AccountsService session(this, QTest::currentTestFunction());
97 QCOMPARE(session.demoEdges(), false);
98 session.setDemoEdges(true);
99 QCOMPARE(session.demoEdges(), true);
101 QCOMPARE(session.failedLogins(), (uint)0);
102 session.setFailedLogins(5);
103 QCOMPARE(session.failedLogins(), (uint)5);
105 QCOMPARE(session.hereEnabled(), false);
106 session.setHereEnabled(true);
107 QCOMPARE(session.hereEnabled(), true);
108 }
110 void testMarkDemoEdgeCompleted()
111 {
112 AccountsService session(this, QTest::currentTestFunction());
113 QSignalSpy changedSpy(&session, &AccountsService::demoEdgesCompletedChanged);
115 QCOMPARE(changedSpy.count(), 0);
116 QCOMPARE(session.demoEdgesCompleted(), QStringList());
118 session.markDemoEdgeCompleted("testedge");
119 QCOMPARE(changedSpy.count(), 1);
120 QCOMPARE(session.demoEdgesCompleted(), QStringList() << "testedge");
122 session.markDemoEdgeCompleted("testedge");
123 QCOMPARE(changedSpy.count(), 1);
124 QCOMPARE(session.demoEdgesCompleted(), QStringList() << "testedge");
126 session.markDemoEdgeCompleted("testedge2");
127 QCOMPARE(changedSpy.count(), 2);
128 QCOMPARE(session.demoEdgesCompleted(), QStringList() << "testedge" << "testedge2");
129 }
131 void testAsynchronousChangeForDemoEdgesCompleted()
132 {
133 AccountsService session(this, QTest::currentTestFunction());
134 QSignalSpy changedSpy(&session, &AccountsService::demoEdgesCompletedChanged);
136 QCOMPARE(changedSpy.count(), 0);
137 QCOMPARE(session.demoEdgesCompleted(), QStringList());
139 ASSERT_DBUS_CALL(m_userInterface->call("Set",
140 "com.canonical.unity.AccountsService",
141 "DemoEdgesCompleted",
142 dbusVariant(QStringList() << "testedge")));
143 QTRY_COMPARE(changedSpy.count(), 1);
144 QCOMPARE(session.demoEdgesCompleted(), QStringList() << "testedge");
145 }
147 void testAsynchornousChangeForDemoEdges()
148 {
149 AccountsService session(this, QTest::currentTestFunction());
151 QCOMPARE(session.demoEdges(), false);
152 ASSERT_DBUS_CALL(m_userInterface->call("Set",
153 "com.canonical.unity.AccountsService",
154 "demo-edges",
155 dbusVariant(true)));
156 QTRY_COMPARE(session.demoEdges(), true);
157 }
159 void testAsynchornousChangeForFailedLogins()
160 {
161 AccountsService session(this, QTest::currentTestFunction());
163 QCOMPARE(session.failedLogins(), (uint)0);
164 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
165 "com.canonical.unity.AccountsService.Private",
166 "FailedLogins",
167 dbusVariant(5)));
168 QTRY_COMPARE(session.failedLogins(), (uint)5);
169 }
171 void testAsynchornousChangeForStatsWelcomeScreen()
172 {
173 AccountsService session(this, QTest::currentTestFunction());
175 QCOMPARE(session.statsWelcomeScreen(), true);
176 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
177 "com.ubuntu.touch.AccountsService.SecurityPrivacy",
178 "StatsWelcomeScreen",
179 dbusVariant(false)));
180 QTRY_COMPARE(session.statsWelcomeScreen(), false);
181 }
183 void testAsynchornousChangeForStatsEnableLauncherWhileLocked()
184 {
185 AccountsService session(this, QTest::currentTestFunction());
187 QCOMPARE(session.enableLauncherWhileLocked(), true);
188 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
189 "com.ubuntu.AccountsService.SecurityPrivacy",
190 "EnableLauncherWhileLocked",
191 dbusVariant(false)));
192 QTRY_COMPARE(session.enableLauncherWhileLocked(), false);
193 }
195 void testAsynchornousChangeForStatsEnableIndicatorsWhileLocked()
196 {
197 AccountsService session(this, QTest::currentTestFunction());
199 QCOMPARE(session.enableIndicatorsWhileLocked(), true);
200 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
201 "com.ubuntu.AccountsService.SecurityPrivacy",
202 "EnableIndicatorsWhileLocked",
203 dbusVariant(false)));
204 QTRY_COMPARE(session.enableIndicatorsWhileLocked(), false);
205 }
207 void testAsynchornousChangeForStatsPasswordDisplayHint()
208 {
209 AccountsService session(this, QTest::currentTestFunction());
211 QCOMPARE(session.passwordDisplayHint(), AccountsService::Keyboard);
212 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
213 "com.ubuntu.AccountsService.SecurityPrivacy",
214 "PasswordDisplayHint",
215 dbusVariant(AccountsService::Numeric)));
216 QTRY_COMPARE(session.passwordDisplayHint(), AccountsService::Numeric);
217 }
219 void testAsynchornousChangeForStatsLicenseAccepted()
220 {
221 AccountsService session(this, QTest::currentTestFunction());
223 QCOMPARE(session.hereEnabled(), false);
224 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
225 "com.ubuntu.location.providers.here.AccountsService",
226 "LicenseAccepted",
227 dbusVariant(true)));
228 QTRY_COMPARE(session.hereEnabled(), true);
229 }
231 void testAsynchornousChangeForLicenseBasePath()
232 {
233 AccountsService session(this, QTest::currentTestFunction());
235 QCOMPARE(session.hereLicensePath(), QString());
236 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
237 "com.ubuntu.location.providers.here.AccountsService",
238 "LicenseBasePath",
239 dbusVariant("/")));
240 QTRY_COMPARE(session.hereLicensePath(), QString("/"));
241 }
243 void testAsynchornousChangeForStatsBackgroundFile()
244 {
245 AccountsService session(this, QTest::currentTestFunction());
247 QCOMPARE(session.backgroundFile(), QString());
248 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
249 "org.freedesktop.Accounts.User",
250 "BackgroundFile",
251 dbusVariant("/test/BackgroundFile")));
252 QTRY_COMPARE(session.backgroundFile(), QString("/test/BackgroundFile"));
253 }
255Q_SIGNALS:
256 void propertiesChanged(const QString &interface, const QVariantMap &changed, const QStringList &invalid);
258private:
259 QDBusInterface* m_userInterface;
260 QSignalSpy m_spy;
261};
263QTEST_MAIN(AccountsServiceTest)
265#include "client.moc"