2 * Copyright 2013 Canonical Ltd.
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.
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.
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/>
17 * Authored by: Michael Terry <michael.terry@canonical.com>
20#include "AccountsService.h"
21#include "AccountsServiceDBusAdaptor.h"
27#include <QDBusMetaType>
32QVariant dbusVariant(const T& value) { return QVariant::fromValue(QDBusVariant(value)); }
34#define ASSERT_DBUS_CALL(call) \
36 QDBusReply<void> reply = call; \
37 if (!reply.isValid()) QFAIL(reply.error().message().toLatin1()); \
40class AccountsServiceTest : public QObject
45 AccountsServiceTest(QObject* parent = 0)
47 , m_userInterface(nullptr)
48 , m_spy(this, &AccountsServiceTest::propertiesChanged)
49 , m_mousePrimaryButtonSpy(this, &AccountsServiceTest::setMousePrimaryButtonCalled)
51 m_uscInputInterface = new QDBusInterface("com.canonical.Unity.Input",
52 "/com/canonical/Unity/Input",
53 "com.canonical.Unity.Input",
54 QDBusConnection::sessionBus(),
57 QObject::connect(m_uscInputInterface, SIGNAL(setMousePrimaryButtonCalled(int)),
58 this, SIGNAL(setMousePrimaryButtonCalled(int)));
60 qDBusRegisterMetaType<StringMap>();
61 qDBusRegisterMetaType<StringMapList>();
67 addUser(QTest::currentTestFunction());
69 m_userInterface = new QDBusInterface("org.freedesktop.Accounts",
70 QString("/%1").arg(QTest::currentTestFunction()),
71 "org.freedesktop.DBus.Properties", QDBusConnection::sessionBus(), this);
73 QVERIFY(QObject::connect(m_userInterface, SIGNAL(PropertiesChanged(QString, QVariantMap, QStringList)),
74 this, SIGNAL(propertiesChanged(QString, QVariantMap, QStringList))));
78 QDBusReply<void> reply = QDBusInterface("org.freedesktop.Accounts",
79 "/org/freedesktop/Accounts",
80 "org.freedesktop.Accounts").call("RemoveAllUsers");
81 QVERIFY(reply.isValid());
83 delete m_userInterface;
85 m_mousePrimaryButtonSpy.clear();
88 void addUser(const QString &user)
90 QDBusReply<bool> addReply = QDBusInterface("org.freedesktop.Accounts",
91 "/org/freedesktop/Accounts",
92 "org.freedesktop.Accounts").call("AddUser", user);
93 QVERIFY(addReply.isValid());
94 QCOMPARE(addReply.value(), true);
97 QVariant getASProperty(const QString &interface, const QString &key, const QString &user = QString())
99 QString ifaceUser = user;
100 if (ifaceUser.isEmpty())
101 ifaceUser = QTest::currentTestFunction();
103 QDBusInterface iface("org.freedesktop.Accounts",
104 QString("/%1").arg(ifaceUser),
105 "org.freedesktop.DBus.Properties",
106 QDBusConnection::sessionBus());
108 auto message = iface.call("Get", interface, key);
109 if (message.type() == QDBusMessage::ReplyMessage) {
110 return message.arguments().first().value<QDBusVariant>().variant();
113 qWarning() << "Could not get property" << key << ":" << message.errorMessage();
120 // Test various invalid calls
121 AccountsServiceDBusAdaptor session;
122 QCOMPARE(session.getUserPropertyAsync("NOPE", "com.canonical.unity.AccountsService", "demo-edges").value(), QVariant());
123 QCOMPARE(session.getUserPropertyAsync(QTest::currentTestFunction(), "com.canonical.unity.AccountsService", "NOPE").value(), QVariant());
126 void testGetSetServiceDBusAdaptor()
128 AccountsServiceDBusAdaptor session;
129 session.setUserPropertyAsync(QTest::currentTestFunction(), "com.canonical.unity.AccountsService", "demo-edges", QVariant(true)).waitForFinished();
130 QCOMPARE(session.getUserPropertyAsync(QTest::currentTestFunction(), "com.canonical.unity.AccountsService", "demo-edges").value(), QVariant(true));
131 session.setUserPropertyAsync(QTest::currentTestFunction(), "com.canonical.unity.AccountsService", "demo-edges", QVariant(false)).waitForFinished();
132 QCOMPARE(session.getUserPropertyAsync(QTest::currentTestFunction(), "com.canonical.unity.AccountsService", "demo-edges").value(), QVariant(false));
135 void testGetSetService()
137 AccountsService session(this, QTest::currentTestFunction());
139 QCOMPARE(session.demoEdges(), false);
140 session.setDemoEdges(true);
141 QCOMPARE(session.demoEdges(), true);
143 QCOMPARE(session.failedLogins(), (uint)0);
144 session.setFailedLogins(5);
145 QCOMPARE(session.failedLogins(), (uint)5);
147 QCOMPARE(session.hereEnabled(), false);
148 session.setHereEnabled(true);
149 QCOMPARE(session.hereEnabled(), true);
152 void testMarkDemoEdgeCompleted()
154 AccountsService session(this, QTest::currentTestFunction());
155 QSignalSpy changedSpy(&session, &AccountsService::demoEdgesCompletedChanged);
157 QCOMPARE(changedSpy.count(), 0);
158 QCOMPARE(session.demoEdgesCompleted(), QStringList());
160 session.markDemoEdgeCompleted("testedge");
161 QCOMPARE(changedSpy.count(), 1);
162 QCOMPARE(session.demoEdgesCompleted(), QStringList() << "testedge");
164 session.markDemoEdgeCompleted("testedge");
165 QCOMPARE(changedSpy.count(), 1);
166 QCOMPARE(session.demoEdgesCompleted(), QStringList() << "testedge");
168 session.markDemoEdgeCompleted("testedge2");
169 QCOMPARE(changedSpy.count(), 2);
170 QCOMPARE(session.demoEdgesCompleted(), QStringList() << "testedge" << "testedge2");
173 void testAsynchronousChangeForDemoEdgesCompleted()
175 AccountsService session(this, QTest::currentTestFunction());
176 QSignalSpy changedSpy(&session, &AccountsService::demoEdgesCompletedChanged);
178 QCOMPARE(changedSpy.count(), 0);
179 QCOMPARE(session.demoEdgesCompleted(), QStringList());
181 ASSERT_DBUS_CALL(m_userInterface->call("Set",
182 "com.canonical.unity.AccountsService",
183 "DemoEdgesCompleted",
184 dbusVariant(QStringList() << "testedge")));
185 QTRY_COMPARE(changedSpy.count(), 1);
186 QCOMPARE(session.demoEdgesCompleted(), QStringList() << "testedge");
189 void testAsynchronousChangeForDemoEdges()
191 AccountsService session(this, QTest::currentTestFunction());
193 QCOMPARE(session.demoEdges(), false);
194 ASSERT_DBUS_CALL(m_userInterface->call("Set",
195 "com.canonical.unity.AccountsService",
198 QTRY_COMPARE(session.demoEdges(), true);
201 void testAsynchronousChangeForFailedLogins()
203 AccountsService session(this, QTest::currentTestFunction());
205 QCOMPARE(session.failedLogins(), (uint)0);
206 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
207 "com.canonical.unity.AccountsService.Private",
210 QTRY_COMPARE(session.failedLogins(), (uint)5);
213 void testAsynchronousChangeForStatsWelcomeScreen()
215 AccountsService session(this, QTest::currentTestFunction());
217 QCOMPARE(session.statsWelcomeScreen(), true);
218 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
219 "com.ubuntu.touch.AccountsService.SecurityPrivacy",
220 "StatsWelcomeScreen",
221 dbusVariant(false)));
222 QTRY_COMPARE(session.statsWelcomeScreen(), false);
225 void testAsynchronousChangeForEnableLauncherWhileLocked()
227 AccountsService session(this, QTest::currentTestFunction());
229 QCOMPARE(session.enableLauncherWhileLocked(), true);
230 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
231 "com.ubuntu.AccountsService.SecurityPrivacy",
232 "EnableLauncherWhileLocked",
233 dbusVariant(false)));
234 QTRY_COMPARE(session.enableLauncherWhileLocked(), false);
237 void testAsynchronousChangeForEnableIndicatorsWhileLocked()
239 AccountsService session(this, QTest::currentTestFunction());
241 QCOMPARE(session.enableIndicatorsWhileLocked(), true);
242 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
243 "com.ubuntu.AccountsService.SecurityPrivacy",
244 "EnableIndicatorsWhileLocked",
245 dbusVariant(false)));
246 QTRY_COMPARE(session.enableIndicatorsWhileLocked(), false);
249 void testAsynchronousChangeForPasswordDisplayHint()
251 AccountsService session(this, QTest::currentTestFunction());
253 QCOMPARE(session.passwordDisplayHint(), AccountsService::Keyboard);
254 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
255 "com.ubuntu.AccountsService.SecurityPrivacy",
256 "PasswordDisplayHint",
257 dbusVariant(AccountsService::Numeric)));
258 QTRY_COMPARE(session.passwordDisplayHint(), AccountsService::Numeric);
261 void testAsynchronousChangeForLicenseAccepted()
263 AccountsService session(this, QTest::currentTestFunction());
265 QCOMPARE(session.hereEnabled(), false);
266 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
267 "com.ubuntu.location.providers.here.AccountsService",
270 QTRY_COMPARE(session.hereEnabled(), true);
273 void testAsynchronousChangeForLicenseBasePath()
275 AccountsService session(this, QTest::currentTestFunction());
277 QCOMPARE(session.hereLicensePath(), QString());
278 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
279 "com.ubuntu.location.providers.here.AccountsService",
282 QTRY_COMPARE(session.hereLicensePath(), QString("/"));
285 void testAsynchronousChangeForBackgroundFile()
287 AccountsService session(this, QTest::currentTestFunction());
289 QCOMPARE(session.backgroundFile(), QString());
291 QDBusInterface accountsIface(m_userInterface->service(),
292 m_userInterface->path(),
293 "org.freedesktop.Accounts.User");
294 ASSERT_DBUS_CALL(accountsIface.asyncCall("SetBackgroundFile",
295 "/test/BackgroundFile"));
297 QTRY_COMPARE(session.backgroundFile(), QString("/test/BackgroundFile"));
300 void testProxyOnStartup()
302 AccountsService session(this, QTest::currentTestFunction());
304 QTRY_COMPARE(m_mousePrimaryButtonSpy.count(), 1);
305 QList<QVariant> arguments = m_mousePrimaryButtonSpy.takeFirst();
306 QCOMPARE(arguments.at(0).toInt(), 1);
309 void testProxyOnChange()
311 AccountsService session(this, QTest::currentTestFunction());
312 QTRY_COMPARE(m_mousePrimaryButtonSpy.count(), 1);
313 m_mousePrimaryButtonSpy.clear();
315 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
316 "com.ubuntu.AccountsService.Input",
317 "MousePrimaryButton",
318 dbusVariant("left")));
320 QTRY_COMPARE(m_mousePrimaryButtonSpy.count(), 1);
321 QList<QVariant> arguments = m_mousePrimaryButtonSpy.takeFirst();
322 QCOMPARE(arguments.at(0).toInt(), 0);
325 void testInvalidPrimaryButton()
327 AccountsService session(this, QTest::currentTestFunction());
328 QTRY_COMPARE(m_mousePrimaryButtonSpy.count(), 1);
329 m_mousePrimaryButtonSpy.clear();
331 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
332 "com.ubuntu.AccountsService.Input",
333 "MousePrimaryButton",
334 dbusVariant("NOPE")));
336 QTRY_COMPARE(m_mousePrimaryButtonSpy.count(), 1);
337 QList<QVariant> arguments = m_mousePrimaryButtonSpy.takeFirst();
338 QCOMPARE(arguments.at(0).toInt(), 0);
341 void testSetRealName()
343 AccountsService session(this, QTest::currentTestFunction());
345 // Just confirm that we can't set normal way
346 auto message = m_userInterface->call("Set",
347 "org.freedesktop.Accounts.User",
349 dbusVariant("Stallman"));
350 QCOMPARE(message.type(), QDBusMessage::ErrorMessage);
351 QCOMPARE(message.errorName(), QStringLiteral("org.freedesktop.DBus.Error.InvalidArgs"));
353 auto response = getASProperty("org.freedesktop.Accounts.User", "RealName");
354 QCOMPARE(response.toString(), QStringLiteral(""));
356 // Now try it via our AS wrapper and confirm it did get through, unlike above
357 session.setRealName("Stallman");
359 QCOMPARE(session.realName(), QStringLiteral("Stallman"));
361 response = getASProperty("org.freedesktop.Accounts.User", "RealName");
362 QCOMPARE(response.toString(), QStringLiteral("Stallman"));
365 void testAsynchronousChangeForKeymaps()
367 AccountsService session(this, QTest::currentTestFunction());
369 QCOMPARE(session.keymaps(), {"us"});
371 StringMapList inputSources;
373 map1.insert("xkb", "cz+qwerty");
374 inputSources.append(map1);
376 map2.insert("xkb", "fr");
377 inputSources.append(map2);
379 QDBusInterface accountsIface(m_userInterface->service(),
380 m_userInterface->path(),
381 "org.freedesktop.Accounts.User");
382 ASSERT_DBUS_CALL(accountsIface.asyncCall("SetInputSources",
383 QVariant::fromValue(inputSources)));
384 QStringList result = {"cz+qwerty", "fr"};
385 QTRY_COMPARE(session.keymaps(), result);
388 void testGreeterMode()
390 addUser(g_get_user_name());
391 addUser("OtherUser"); // presumably will never conflict with running user
392 AccountsService session(this);
393 QVERIFY(session.greeterMode());
395 session.setGreeterMode(false);
397 // NOTE: we change real user data here, but it's a harmless change
398 session.setFailedLogins(1);
399 QCOMPARE(session.failedLogins(), 1u);
400 QCOMPARE(getASProperty("com.canonical.unity.AccountsService.Private",
401 "FailedLogins", session.user()), QVariant(1));
403 // This will fail because we aren't the active user
404 session.setUser("OtherUser");
405 session.setFailedLogins(2);
406 QCOMPARE(session.failedLogins(), 2u);
407 QCOMPARE(getASProperty("com.canonical.unity.AccountsService.Private",
408 "FailedLogins", "OtherUser"), QVariant(0));
410 session.setGreeterMode(true);
412 // But now a similar call will succeed because we're in greeter mode
413 session.setFailedLogins(3);
414 QCOMPARE(session.failedLogins(), 3u);
415 QCOMPARE(getASProperty("com.canonical.unity.AccountsService.Private",
416 "FailedLogins", "OtherUser"), QVariant(3));
421 void propertiesChanged(const QString &interface, const QVariantMap &changed, const QStringList &invalid);
422 void setMousePrimaryButtonCalled(int button);
425 QDBusInterface* m_uscInputInterface;
426 QDBusInterface* m_userInterface;
428 QSignalSpy m_mousePrimaryButtonSpy;
431QTEST_MAIN(AccountsServiceTest)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list