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>
30QVariant dbusVariant(const T& value) { return QVariant::fromValue(QDBusVariant(value)); }
32#define ASSERT_DBUS_CALL(call) \
34 QDBusReply<void> reply = call; \
35 if (!reply.isValid()) QFAIL(reply.error().message().toLatin1()); \
38class AccountsServiceTest : public QObject
43 AccountsServiceTest(QObject* parent = 0)
45 , m_userInterface(nullptr)
46 , m_spy(this, &AccountsServiceTest::propertiesChanged)
47 , m_mousePrimaryButtonSpy(this, &AccountsServiceTest::setMousePrimaryButtonCalled)
49 m_uscInputInterface = new QDBusInterface("com.canonical.Unity.Input",
50 "/com/canonical/Unity/Input",
51 "com.canonical.Unity.Input",
52 QDBusConnection::sessionBus(),
55 QObject::connect(m_uscInputInterface, SIGNAL(setMousePrimaryButtonCalled(int)),
56 this, SIGNAL(setMousePrimaryButtonCalled(int)));
58 qDBusRegisterMetaType<StringMap>();
59 qDBusRegisterMetaType<StringMapList>();
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))));
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;
88 m_mousePrimaryButtonSpy.clear();
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());
99 void testGetSetServiceDBusAdaptor()
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));
108 void testGetSetService()
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);
125 void testMarkDemoEdgeCompleted()
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");
146 void testAsynchronousChangeForDemoEdgesCompleted()
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");
162 void testAsynchronousChangeForDemoEdges()
164 AccountsService session(this, QTest::currentTestFunction());
166 QCOMPARE(session.demoEdges(), false);
167 ASSERT_DBUS_CALL(m_userInterface->call("Set",
168 "com.canonical.unity.AccountsService",
171 QTRY_COMPARE(session.demoEdges(), true);
174 void testAsynchronousChangeForFailedLogins()
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",
183 QTRY_COMPARE(session.failedLogins(), (uint)5);
186 void testAsynchronousChangeForStatsWelcomeScreen()
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);
198 void testAsynchronousChangeForEnableLauncherWhileLocked()
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);
210 void testAsynchronousChangeForEnableIndicatorsWhileLocked()
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);
222 void testAsynchronousChangeForPasswordDisplayHint()
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);
234 void testAsynchronousChangeForLicenseAccepted()
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",
243 QTRY_COMPARE(session.hereEnabled(), true);
246 void testAsynchronousChangeForLicenseBasePath()
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",
255 QTRY_COMPARE(session.hereLicensePath(), QString("/"));
258 void testAsynchronousChangeForBackgroundFile()
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"));
273 void testProxyOnStartup()
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);
282 void testProxyOnChange()
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);
298 void testInvalidPrimaryButton()
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);
314 void testSetRealName()
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",
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",
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",
341 QCOMPARE(message.type(), QDBusMessage::ReplyMessage);
342 response = message.arguments().first().value<QDBusVariant>().variant().toString();
343 QCOMPARE(response, QStringLiteral("Stallman"));
347 void testAsynchronousChangeForKeymaps()
349 AccountsService session(this, QTest::currentTestFunction());
351 QCOMPARE(session.keymaps(), {"us"});
353 StringMapList inputSources;
355 map1.insert("xkb", "cz+qwerty");
356 inputSources.append(map1);
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);
371 void propertiesChanged(const QString &interface, const QVariantMap &changed, const QStringList &invalid);
372 void setMousePrimaryButtonCalled(int button);
375 QDBusInterface* m_uscInputInterface;
376 QDBusInterface* m_userInterface;
378 QSignalSpy m_mousePrimaryButtonSpy;
381QTEST_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