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", "DemoEdges2").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", "DemoEdges2", QVariant(true)).waitForFinished();
103 QCOMPARE(session.getUserPropertyAsync(QTest::currentTestFunction(), "com.canonical.unity.AccountsService", "DemoEdges2").value(), QVariant(true));
104 session.setUserPropertyAsync(QTest::currentTestFunction(), "com.canonical.unity.AccountsService", "DemoEdges2", QVariant(false)).waitForFinished();
105 QCOMPARE(session.getUserPropertyAsync(QTest::currentTestFunction(), "com.canonical.unity.AccountsService", "DemoEdges2").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);
121 void testMarkDemoEdgeCompleted()
123 AccountsService session(this, QTest::currentTestFunction());
124 QSignalSpy changedSpy(&session, &AccountsService::demoEdgesCompletedChanged);
126 QCOMPARE(changedSpy.count(), 0);
127 QCOMPARE(session.demoEdgesCompleted(), QStringList());
129 session.markDemoEdgeCompleted("testedge");
130 QCOMPARE(changedSpy.count(), 1);
131 QCOMPARE(session.demoEdgesCompleted(), QStringList() << "testedge");
133 session.markDemoEdgeCompleted("testedge");
134 QCOMPARE(changedSpy.count(), 1);
135 QCOMPARE(session.demoEdgesCompleted(), QStringList() << "testedge");
137 session.markDemoEdgeCompleted("testedge2");
138 QCOMPARE(changedSpy.count(), 2);
139 QCOMPARE(session.demoEdgesCompleted(), QStringList() << "testedge" << "testedge2");
142 void testAsynchronousChangeForDemoEdgesCompleted()
144 AccountsService session(this, QTest::currentTestFunction());
145 QSignalSpy changedSpy(&session, &AccountsService::demoEdgesCompletedChanged);
147 QCOMPARE(changedSpy.count(), 0);
148 QCOMPARE(session.demoEdgesCompleted(), QStringList());
150 ASSERT_DBUS_CALL(m_userInterface->call("Set",
151 "com.canonical.unity.AccountsService",
152 "DemoEdgesCompleted",
153 dbusVariant(QStringList() << "testedge")));
154 QTRY_COMPARE(changedSpy.count(), 1);
155 QCOMPARE(session.demoEdgesCompleted(), QStringList() << "testedge");
158 void testAsynchronousChangeForDemoEdges()
160 AccountsService session(this, QTest::currentTestFunction());
162 QCOMPARE(session.demoEdges(), false);
163 ASSERT_DBUS_CALL(m_userInterface->call("Set",
164 "com.canonical.unity.AccountsService",
167 QTRY_COMPARE(session.demoEdges(), true);
170 void testAsynchronousChangeForFailedLogins()
172 AccountsService session(this, QTest::currentTestFunction());
174 QCOMPARE(session.failedLogins(), (uint)0);
175 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
176 "com.canonical.unity.AccountsService.Private",
179 QTRY_COMPARE(session.failedLogins(), (uint)5);
182 void testAsynchronousChangeForStatsWelcomeScreen()
184 AccountsService session(this, QTest::currentTestFunction());
186 QCOMPARE(session.statsWelcomeScreen(), true);
187 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
188 "com.ubuntu.touch.AccountsService.SecurityPrivacy",
189 "StatsWelcomeScreen",
190 dbusVariant(false)));
191 QTRY_COMPARE(session.statsWelcomeScreen(), false);
194 void testAsynchronousChangeForEnableLauncherWhileLocked()
196 AccountsService session(this, QTest::currentTestFunction());
198 QCOMPARE(session.enableLauncherWhileLocked(), true);
199 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
200 "com.ubuntu.AccountsService.SecurityPrivacy",
201 "EnableLauncherWhileLocked",
202 dbusVariant(false)));
203 QTRY_COMPARE(session.enableLauncherWhileLocked(), false);
206 void testAsynchronousChangeForEnableIndicatorsWhileLocked()
208 AccountsService session(this, QTest::currentTestFunction());
210 QCOMPARE(session.enableIndicatorsWhileLocked(), true);
211 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
212 "com.ubuntu.AccountsService.SecurityPrivacy",
213 "EnableIndicatorsWhileLocked",
214 dbusVariant(false)));
215 QTRY_COMPARE(session.enableIndicatorsWhileLocked(), false);
218 void testAsynchronousChangeForPasswordDisplayHint()
220 AccountsService session(this, QTest::currentTestFunction());
222 QCOMPARE(session.passwordDisplayHint(), AccountsService::Keyboard);
223 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
224 "com.ubuntu.AccountsService.SecurityPrivacy",
225 "PasswordDisplayHint",
226 dbusVariant(AccountsService::Numeric)));
227 QTRY_COMPARE(session.passwordDisplayHint(), AccountsService::Numeric);
230 void testAsynchronousChangeForBackgroundFile()
232 AccountsService session(this, QTest::currentTestFunction());
234 QCOMPARE(session.backgroundFile(), QString());
236 QDBusInterface accountsIface(m_userInterface->service(),
237 m_userInterface->path(),
238 "org.freedesktop.Accounts.User");
239 ASSERT_DBUS_CALL(accountsIface.asyncCall("SetBackgroundFile",
240 "/test/BackgroundFile"));
242 QTRY_COMPARE(session.backgroundFile(), QString("/test/BackgroundFile"));
245 void testProxyOnStartup()
247 AccountsService session(this, QTest::currentTestFunction());
249 QTRY_COMPARE(m_mousePrimaryButtonSpy.count(), 1);
250 QList<QVariant> arguments = m_mousePrimaryButtonSpy.takeFirst();
251 QCOMPARE(arguments.at(0).toInt(), 1);
254 void testProxyOnChange()
256 AccountsService session(this, QTest::currentTestFunction());
257 QTRY_COMPARE(m_mousePrimaryButtonSpy.count(), 1);
258 m_mousePrimaryButtonSpy.clear();
260 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
261 "com.ubuntu.AccountsService.Input",
262 "MousePrimaryButton",
263 dbusVariant("left")));
265 QTRY_COMPARE(m_mousePrimaryButtonSpy.count(), 1);
266 QList<QVariant> arguments = m_mousePrimaryButtonSpy.takeFirst();
267 QCOMPARE(arguments.at(0).toInt(), 0);
270 void testInvalidPrimaryButton()
272 AccountsService session(this, QTest::currentTestFunction());
273 QTRY_COMPARE(m_mousePrimaryButtonSpy.count(), 1);
274 m_mousePrimaryButtonSpy.clear();
276 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
277 "com.ubuntu.AccountsService.Input",
278 "MousePrimaryButton",
279 dbusVariant("NOPE")));
281 QTRY_COMPARE(m_mousePrimaryButtonSpy.count(), 1);
282 QList<QVariant> arguments = m_mousePrimaryButtonSpy.takeFirst();
283 QCOMPARE(arguments.at(0).toInt(), 0);
286 void testSetRealName()
288 AccountsService session(this, QTest::currentTestFunction());
290 // Just confirm that we can't set normal way
291 auto message = m_userInterface->call("Set",
292 "org.freedesktop.Accounts.User",
294 dbusVariant("Stallman"));
295 QCOMPARE(message.type(), QDBusMessage::ErrorMessage);
296 QCOMPARE(message.errorName(), QStringLiteral("org.freedesktop.DBus.Error.InvalidArgs"));
298 message = m_userInterface->call("Get",
299 "org.freedesktop.Accounts.User",
301 QCOMPARE(message.type(), QDBusMessage::ReplyMessage);
302 auto response = message.arguments().first().value<QDBusVariant>().variant().toString();
303 QCOMPARE(response, QStringLiteral(""));
305 // Now try it via our AS wrapper and confirm it did get through, unlike above
306 session.setRealName("Stallman");
308 QCOMPARE(session.realName(), QStringLiteral("Stallman"));
310 message = m_userInterface->call("Get",
311 "org.freedesktop.Accounts.User",
313 QCOMPARE(message.type(), QDBusMessage::ReplyMessage);
314 response = message.arguments().first().value<QDBusVariant>().variant().toString();
315 QCOMPARE(response, QStringLiteral("Stallman"));
319 void testAsynchronousChangeForKeymaps()
321 AccountsService session(this, QTest::currentTestFunction());
323 QCOMPARE(session.keymaps(), {"us"});
325 StringMapList inputSources;
327 map1.insert("xkb", "cz+qwerty");
328 inputSources.append(map1);
330 map2.insert("xkb", "fr");
331 inputSources.append(map2);
333 QDBusInterface accountsIface(m_userInterface->service(),
334 m_userInterface->path(),
335 "org.freedesktop.Accounts.User");
336 ASSERT_DBUS_CALL(accountsIface.asyncCall("SetInputSources",
337 QVariant::fromValue(inputSources)));
338 QStringList result = {"cz+qwerty", "fr"};
339 QTRY_COMPARE(session.keymaps(), result);
343 void propertiesChanged(const QString &interface, const QVariantMap &changed, const QStringList &invalid);
344 void setMousePrimaryButtonCalled(int button);
347 QDBusInterface* m_uscInputInterface;
348 QDBusInterface* m_userInterface;
350 QSignalSpy m_mousePrimaryButtonSpy;
353QTEST_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