on d87fbf7 help sign in
355 lines 15 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", "DemoEdges2").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", "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));
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);
119 }
121 void testMarkDemoEdgeCompleted()
122 {
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");
140 }
142 void testAsynchronousChangeForDemoEdgesCompleted()
143 {
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");
156 }
158 void testAsynchronousChangeForDemoEdges()
159 {
160 AccountsService session(this, QTest::currentTestFunction());
162 QCOMPARE(session.demoEdges(), false);
163 ASSERT_DBUS_CALL(m_userInterface->call("Set",
164 "com.canonical.unity.AccountsService",
165 "DemoEdges2",
166 dbusVariant(true)));
167 QTRY_COMPARE(session.demoEdges(), true);
168 }
170 void testAsynchronousChangeForFailedLogins()
171 {
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",
177 "FailedLogins",
178 dbusVariant(5)));
179 QTRY_COMPARE(session.failedLogins(), (uint)5);
180 }
182 void testAsynchronousChangeForStatsWelcomeScreen()
183 {
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);
192 }
194 void testAsynchronousChangeForEnableLauncherWhileLocked()
195 {
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);
204 }
206 void testAsynchronousChangeForEnableIndicatorsWhileLocked()
207 {
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);
216 }
218 void testAsynchronousChangeForPasswordDisplayHint()
219 {
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);
228 }
230 void testAsynchronousChangeForBackgroundFile()
231 {
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"));
243 }
245 void testProxyOnStartup()
246 {
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);
252 }
254 void testProxyOnChange()
255 {
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);
268 }
270 void testInvalidPrimaryButton()
271 {
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);
284 }
286 void testSetRealName()
287 {
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",
293 "RealName",
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",
300 "RealName");
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",
312 "RealName");
313 QCOMPARE(message.type(), QDBusMessage::ReplyMessage);
314 response = message.arguments().first().value<QDBusVariant>().variant().toString();
315 QCOMPARE(response, QStringLiteral("Stallman"));
317 }
319 void testAsynchronousChangeForKeymaps()
320 {
321 AccountsService session(this, QTest::currentTestFunction());
323 QCOMPARE(session.keymaps(), {"us"});
325 StringMapList inputSources;
326 StringMap map1;
327 map1.insert("xkb", "cz+qwerty");
328 inputSources.append(map1);
329 StringMap map2;
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);
340 }
342Q_SIGNALS:
343 void propertiesChanged(const QString &interface, const QVariantMap &changed, const QStringList &invalid);
344 void setMousePrimaryButtonCalled(int button);
346private:
347 QDBusInterface* m_uscInputInterface;
348 QDBusInterface* m_userInterface;
349 QSignalSpy m_spy;
350 QSignalSpy m_mousePrimaryButtonSpy;
351};
353QTEST_MAIN(AccountsServiceTest)
355#include "client.moc"