on bionic help sign in
383 lines 16 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 "types.h"
23#include <QSignalSpy>
24#include <QTest>
25#include <QDebug>
26#include <QDBusReply>
27#include <QDBusMetaType>
29template <class T>
30QVariant dbusVariant(const T& value) { return QVariant::fromValue(QDBusVariant(value)); }
32#define ASSERT_DBUS_CALL(call) \
33 { \
34 QDBusReply<void> reply = call; \
35 if (!reply.isValid()) QFAIL(reply.error().message().toLatin1()); \
36 }
38class AccountsServiceTest : public QObject
40 Q_OBJECT
42public:
43 AccountsServiceTest(QObject* parent = 0)
44 : QObject(parent)
45 , m_userInterface(nullptr)
46 , m_spy(this, &AccountsServiceTest::propertiesChanged)
47 , m_mousePrimaryButtonSpy(this, &AccountsServiceTest::setMousePrimaryButtonCalled)
48 {
49 m_uscInputInterface = new QDBusInterface("com.canonical.Unity.Input",
50 "/com/canonical/Unity/Input",
51 "com.canonical.Unity.Input",
52 QDBusConnection::sessionBus(),
53 this);
55 QObject::connect(m_uscInputInterface, SIGNAL(setMousePrimaryButtonCalled(int)),
56 this, SIGNAL(setMousePrimaryButtonCalled(int)));
58 qDBusRegisterMetaType<StringMap>();
59 qDBusRegisterMetaType<StringMapList>();
60 }
62private Q_SLOTS:
64 void init() {
65 QDBusReply<bool> addReply = QDBusInterface("org.freedesktop.Accounts",
66 "/org/freedesktop/Accounts",
67 "org.freedesktop.Accounts").call("AddUser", QTest::currentTestFunction());
68 QVERIFY(addReply.isValid());
69 QCOMPARE(addReply.value(), true);
71 m_userInterface = new QDBusInterface("org.freedesktop.Accounts",
72 QString("/%1").arg(QTest::currentTestFunction()),
73 "org.freedesktop.DBus.Properties", QDBusConnection::sessionBus(), this);
75 QVERIFY(QObject::connect(m_userInterface, SIGNAL(PropertiesChanged(QString, QVariantMap, QStringList)),
76 this, SIGNAL(propertiesChanged(QString, QVariantMap, QStringList))));
77 }
79 void cleanup() {
80 QDBusReply<bool> reply = QDBusInterface("org.freedesktop.Accounts",
81 "/org/freedesktop/Accounts",
82 "org.freedesktop.Accounts").call("RemoveUser", QTest::currentTestFunction());
83 QVERIFY(reply.isValid());
84 QCOMPARE(reply.value(), true);
86 delete m_userInterface;
87 m_spy.clear();
88 m_mousePrimaryButtonSpy.clear();
89 }
91 void testInvalids()
92 {
93 // Test various invalid calls
94 AccountsServiceDBusAdaptor session;
95 QCOMPARE(session.getUserPropertyAsync("NOPE", "com.canonical.unity.AccountsService", "demo-edges").value(), QVariant());
96 QCOMPARE(session.getUserPropertyAsync(QTest::currentTestFunction(), "com.canonical.unity.AccountsService", "NOPE").value(), QVariant());
97 }
99 void testGetSetServiceDBusAdaptor()
100 {
101 AccountsServiceDBusAdaptor session;
102 session.setUserPropertyAsync(QTest::currentTestFunction(), "com.canonical.unity.AccountsService", "demo-edges", QVariant(true)).waitForFinished();
103 QCOMPARE(session.getUserPropertyAsync(QTest::currentTestFunction(), "com.canonical.unity.AccountsService", "demo-edges").value(), QVariant(true));
104 session.setUserPropertyAsync(QTest::currentTestFunction(), "com.canonical.unity.AccountsService", "demo-edges", QVariant(false)).waitForFinished();
105 QCOMPARE(session.getUserPropertyAsync(QTest::currentTestFunction(), "com.canonical.unity.AccountsService", "demo-edges").value(), QVariant(false));
106 }
108 void testGetSetService()
109 {
110 AccountsService session(this, QTest::currentTestFunction());
112 QCOMPARE(session.demoEdges(), false);
113 session.setDemoEdges(true);
114 QCOMPARE(session.demoEdges(), true);
116 QCOMPARE(session.failedLogins(), (uint)0);
117 session.setFailedLogins(5);
118 QCOMPARE(session.failedLogins(), (uint)5);
120 QCOMPARE(session.hereEnabled(), false);
121 session.setHereEnabled(true);
122 QCOMPARE(session.hereEnabled(), true);
123 }
125 void testMarkDemoEdgeCompleted()
126 {
127 AccountsService session(this, QTest::currentTestFunction());
128 QSignalSpy changedSpy(&session, &AccountsService::demoEdgesCompletedChanged);
130 QCOMPARE(changedSpy.count(), 0);
131 QCOMPARE(session.demoEdgesCompleted(), QStringList());
133 session.markDemoEdgeCompleted("testedge");
134 QCOMPARE(changedSpy.count(), 1);
135 QCOMPARE(session.demoEdgesCompleted(), QStringList() << "testedge");
137 session.markDemoEdgeCompleted("testedge");
138 QCOMPARE(changedSpy.count(), 1);
139 QCOMPARE(session.demoEdgesCompleted(), QStringList() << "testedge");
141 session.markDemoEdgeCompleted("testedge2");
142 QCOMPARE(changedSpy.count(), 2);
143 QCOMPARE(session.demoEdgesCompleted(), QStringList() << "testedge" << "testedge2");
144 }
146 void testAsynchronousChangeForDemoEdgesCompleted()
147 {
148 AccountsService session(this, QTest::currentTestFunction());
149 QSignalSpy changedSpy(&session, &AccountsService::demoEdgesCompletedChanged);
151 QCOMPARE(changedSpy.count(), 0);
152 QCOMPARE(session.demoEdgesCompleted(), QStringList());
154 ASSERT_DBUS_CALL(m_userInterface->call("Set",
155 "com.canonical.unity.AccountsService",
156 "DemoEdgesCompleted",
157 dbusVariant(QStringList() << "testedge")));
158 QTRY_COMPARE(changedSpy.count(), 1);
159 QCOMPARE(session.demoEdgesCompleted(), QStringList() << "testedge");
160 }
162 void testAsynchronousChangeForDemoEdges()
163 {
164 AccountsService session(this, QTest::currentTestFunction());
166 QCOMPARE(session.demoEdges(), false);
167 ASSERT_DBUS_CALL(m_userInterface->call("Set",
168 "com.canonical.unity.AccountsService",
169 "demo-edges",
170 dbusVariant(true)));
171 QTRY_COMPARE(session.demoEdges(), true);
172 }
174 void testAsynchronousChangeForFailedLogins()
175 {
176 AccountsService session(this, QTest::currentTestFunction());
178 QCOMPARE(session.failedLogins(), (uint)0);
179 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
180 "com.canonical.unity.AccountsService.Private",
181 "FailedLogins",
182 dbusVariant(5)));
183 QTRY_COMPARE(session.failedLogins(), (uint)5);
184 }
186 void testAsynchronousChangeForStatsWelcomeScreen()
187 {
188 AccountsService session(this, QTest::currentTestFunction());
190 QCOMPARE(session.statsWelcomeScreen(), true);
191 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
192 "com.ubuntu.touch.AccountsService.SecurityPrivacy",
193 "StatsWelcomeScreen",
194 dbusVariant(false)));
195 QTRY_COMPARE(session.statsWelcomeScreen(), false);
196 }
198 void testAsynchronousChangeForEnableLauncherWhileLocked()
199 {
200 AccountsService session(this, QTest::currentTestFunction());
202 QCOMPARE(session.enableLauncherWhileLocked(), true);
203 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
204 "com.ubuntu.AccountsService.SecurityPrivacy",
205 "EnableLauncherWhileLocked",
206 dbusVariant(false)));
207 QTRY_COMPARE(session.enableLauncherWhileLocked(), false);
208 }
210 void testAsynchronousChangeForEnableIndicatorsWhileLocked()
211 {
212 AccountsService session(this, QTest::currentTestFunction());
214 QCOMPARE(session.enableIndicatorsWhileLocked(), true);
215 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
216 "com.ubuntu.AccountsService.SecurityPrivacy",
217 "EnableIndicatorsWhileLocked",
218 dbusVariant(false)));
219 QTRY_COMPARE(session.enableIndicatorsWhileLocked(), false);
220 }
222 void testAsynchronousChangeForPasswordDisplayHint()
223 {
224 AccountsService session(this, QTest::currentTestFunction());
226 QCOMPARE(session.passwordDisplayHint(), AccountsService::Keyboard);
227 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
228 "com.ubuntu.AccountsService.SecurityPrivacy",
229 "PasswordDisplayHint",
230 dbusVariant(AccountsService::Numeric)));
231 QTRY_COMPARE(session.passwordDisplayHint(), AccountsService::Numeric);
232 }
234 void testAsynchronousChangeForLicenseAccepted()
235 {
236 AccountsService session(this, QTest::currentTestFunction());
238 QCOMPARE(session.hereEnabled(), false);
239 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
240 "com.ubuntu.location.providers.here.AccountsService",
241 "LicenseAccepted",
242 dbusVariant(true)));
243 QTRY_COMPARE(session.hereEnabled(), true);
244 }
246 void testAsynchronousChangeForLicenseBasePath()
247 {
248 AccountsService session(this, QTest::currentTestFunction());
250 QCOMPARE(session.hereLicensePath(), QString());
251 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
252 "com.ubuntu.location.providers.here.AccountsService",
253 "LicenseBasePath",
254 dbusVariant("/")));
255 QTRY_COMPARE(session.hereLicensePath(), QString("/"));
256 }
258 void testAsynchronousChangeForBackgroundFile()
259 {
260 AccountsService session(this, QTest::currentTestFunction());
262 QCOMPARE(session.backgroundFile(), QString());
264 QDBusInterface accountsIface(m_userInterface->service(),
265 m_userInterface->path(),
266 "org.freedesktop.Accounts.User");
267 ASSERT_DBUS_CALL(accountsIface.asyncCall("SetBackgroundFile",
268 "/test/BackgroundFile"));
270 QTRY_COMPARE(session.backgroundFile(), QString("/test/BackgroundFile"));
271 }
273 void testProxyOnStartup()
274 {
275 AccountsService session(this, QTest::currentTestFunction());
277 QTRY_COMPARE(m_mousePrimaryButtonSpy.count(), 1);
278 QList<QVariant> arguments = m_mousePrimaryButtonSpy.takeFirst();
279 QCOMPARE(arguments.at(0).toInt(), 1);
280 }
282 void testProxyOnChange()
283 {
284 AccountsService session(this, QTest::currentTestFunction());
285 QTRY_COMPARE(m_mousePrimaryButtonSpy.count(), 1);
286 m_mousePrimaryButtonSpy.clear();
288 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
289 "com.ubuntu.AccountsService.Input",
290 "MousePrimaryButton",
291 dbusVariant("left")));
293 QTRY_COMPARE(m_mousePrimaryButtonSpy.count(), 1);
294 QList<QVariant> arguments = m_mousePrimaryButtonSpy.takeFirst();
295 QCOMPARE(arguments.at(0).toInt(), 0);
296 }
298 void testInvalidPrimaryButton()
299 {
300 AccountsService session(this, QTest::currentTestFunction());
301 QTRY_COMPARE(m_mousePrimaryButtonSpy.count(), 1);
302 m_mousePrimaryButtonSpy.clear();
304 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
305 "com.ubuntu.AccountsService.Input",
306 "MousePrimaryButton",
307 dbusVariant("NOPE")));
309 QTRY_COMPARE(m_mousePrimaryButtonSpy.count(), 1);
310 QList<QVariant> arguments = m_mousePrimaryButtonSpy.takeFirst();
311 QCOMPARE(arguments.at(0).toInt(), 0);
312 }
314 void testSetRealName()
315 {
316 AccountsService session(this, QTest::currentTestFunction());
318 // Just confirm that we can't set normal way
319 auto message = m_userInterface->call("Set",
320 "org.freedesktop.Accounts.User",
321 "RealName",
322 dbusVariant("Stallman"));
323 QCOMPARE(message.type(), QDBusMessage::ErrorMessage);
324 QCOMPARE(message.errorName(), QStringLiteral("org.freedesktop.DBus.Error.InvalidArgs"));
326 message = m_userInterface->call("Get",
327 "org.freedesktop.Accounts.User",
328 "RealName");
329 QCOMPARE(message.type(), QDBusMessage::ReplyMessage);
330 auto response = message.arguments().first().value<QDBusVariant>().variant().toString();
331 QCOMPARE(response, QStringLiteral(""));
333 // Now try it via our AS wrapper and confirm it did get through, unlike above
334 session.setRealName("Stallman");
336 QCOMPARE(session.realName(), QStringLiteral("Stallman"));
338 message = m_userInterface->call("Get",
339 "org.freedesktop.Accounts.User",
340 "RealName");
341 QCOMPARE(message.type(), QDBusMessage::ReplyMessage);
342 response = message.arguments().first().value<QDBusVariant>().variant().toString();
343 QCOMPARE(response, QStringLiteral("Stallman"));
345 }
347 void testAsynchronousChangeForKeymaps()
348 {
349 AccountsService session(this, QTest::currentTestFunction());
351 QCOMPARE(session.keymaps(), {"us"});
353 StringMapList inputSources;
354 StringMap map1;
355 map1.insert("xkb", "cz+qwerty");
356 inputSources.append(map1);
357 StringMap map2;
358 map2.insert("xkb", "fr");
359 inputSources.append(map2);
361 QDBusInterface accountsIface(m_userInterface->service(),
362 m_userInterface->path(),
363 "org.freedesktop.Accounts.User");
364 ASSERT_DBUS_CALL(accountsIface.asyncCall("SetInputSources",
365 QVariant::fromValue(inputSources)));
366 QStringList result = {"cz+qwerty", "fr"};
367 QTRY_COMPARE(session.keymaps(), result);
368 }
370Q_SIGNALS:
371 void propertiesChanged(const QString &interface, const QVariantMap &changed, const QStringList &invalid);
372 void setMousePrimaryButtonCalled(int button);
374private:
375 QDBusInterface* m_uscInputInterface;
376 QDBusInterface* m_userInterface;
377 QSignalSpy m_spy;
378 QSignalSpy m_mousePrimaryButtonSpy;
379};
381QTEST_MAIN(AccountsServiceTest)
383#include "client.moc"