on 8476a4e help sign in
349 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 <QSignalSpy>
23#include <QTest>
24#include <QDebug>
25#include <QDBusReply>
26#include <QDBusMetaType>
28using StringMap = QMap<QString,QString>;
29using StringMapList = QList<StringMap>;
30Q_DECLARE_METATYPE(StringMapList)
32template <class T>
33QVariant dbusVariant(const T& value) { return QVariant::fromValue(QDBusVariant(value)); }
35#define ASSERT_DBUS_CALL(call) \
36 { \
37 QDBusReply<void> reply = call; \
38 if (!reply.isValid()) QFAIL(reply.error().message().toLatin1()); \
39 }
41class AccountsServiceTest : public QObject
43 Q_OBJECT
45public:
46 AccountsServiceTest(QObject* parent = 0)
47 : QObject(parent)
48 , m_userInterface(nullptr)
49 , m_spy(this, &AccountsServiceTest::propertiesChanged)
50 , m_mousePrimaryButtonSpy(this, &AccountsServiceTest::setMousePrimaryButtonCalled)
51 {
52 m_uscInputInterface = new QDBusInterface("com.canonical.Unity.Input",
53 "/com/canonical/Unity/Input",
54 "com.canonical.Unity.Input",
55 QDBusConnection::sessionBus(),
56 this);
58 QObject::connect(m_uscInputInterface, SIGNAL(setMousePrimaryButtonCalled(int)),
59 this, SIGNAL(setMousePrimaryButtonCalled(int)));
61 qDBusRegisterMetaType<StringMap>();
62 qDBusRegisterMetaType<StringMapList>();
63 }
65private Q_SLOTS:
67 void init() {
68 QDBusReply<bool> addReply = QDBusInterface("org.freedesktop.Accounts",
69 "/org/freedesktop/Accounts",
70 "org.freedesktop.Accounts").call("AddUser", QTest::currentTestFunction());
71 QVERIFY(addReply.isValid());
72 QCOMPARE(addReply.value(), true);
74 m_userInterface = new QDBusInterface("org.freedesktop.Accounts",
75 QString("/%1").arg(QTest::currentTestFunction()),
76 "org.freedesktop.DBus.Properties", QDBusConnection::sessionBus(), this);
78 QVERIFY(QObject::connect(m_userInterface, SIGNAL(PropertiesChanged(QString, QVariantMap, QStringList)),
79 this, SIGNAL(propertiesChanged(QString, QVariantMap, QStringList))));
80 }
82 void cleanup() {
83 QDBusReply<bool> reply = QDBusInterface("org.freedesktop.Accounts",
84 "/org/freedesktop/Accounts",
85 "org.freedesktop.Accounts").call("RemoveUser", QTest::currentTestFunction());
86 QVERIFY(reply.isValid());
87 QCOMPARE(reply.value(), true);
89 delete m_userInterface;
90 m_spy.clear();
91 m_mousePrimaryButtonSpy.clear();
92 }
94 void testInvalids()
95 {
96 // Test various invalid calls
97 AccountsServiceDBusAdaptor session;
98 QCOMPARE(session.getUserPropertyAsync("NOPE", "com.canonical.unity.AccountsService", "demo-edges").value(), QVariant());
99 QCOMPARE(session.getUserPropertyAsync(QTest::currentTestFunction(), "com.canonical.unity.AccountsService", "NOPE").value(), QVariant());
100 }
102 void testGetSetServiceDBusAdaptor()
103 {
104 AccountsServiceDBusAdaptor session;
105 session.setUserPropertyAsync(QTest::currentTestFunction(), "com.canonical.unity.AccountsService", "demo-edges", QVariant(true)).waitForFinished();
106 QCOMPARE(session.getUserPropertyAsync(QTest::currentTestFunction(), "com.canonical.unity.AccountsService", "demo-edges").value(), QVariant(true));
107 session.setUserPropertyAsync(QTest::currentTestFunction(), "com.canonical.unity.AccountsService", "demo-edges", QVariant(false)).waitForFinished();
108 QCOMPARE(session.getUserPropertyAsync(QTest::currentTestFunction(), "com.canonical.unity.AccountsService", "demo-edges").value(), QVariant(false));
109 }
111 void testGetSetService()
112 {
113 AccountsService session(this, QTest::currentTestFunction());
115 QCOMPARE(session.demoEdges(), false);
116 session.setDemoEdges(true);
117 QCOMPARE(session.demoEdges(), true);
119 QCOMPARE(session.failedLogins(), (uint)0);
120 session.setFailedLogins(5);
121 QCOMPARE(session.failedLogins(), (uint)5);
123 QCOMPARE(session.hereEnabled(), false);
124 session.setHereEnabled(true);
125 QCOMPARE(session.hereEnabled(), true);
126 }
128 void testMarkDemoEdgeCompleted()
129 {
130 AccountsService session(this, QTest::currentTestFunction());
131 QSignalSpy changedSpy(&session, &AccountsService::demoEdgesCompletedChanged);
133 QCOMPARE(changedSpy.count(), 0);
134 QCOMPARE(session.demoEdgesCompleted(), QStringList());
136 session.markDemoEdgeCompleted("testedge");
137 QCOMPARE(changedSpy.count(), 1);
138 QCOMPARE(session.demoEdgesCompleted(), QStringList() << "testedge");
140 session.markDemoEdgeCompleted("testedge");
141 QCOMPARE(changedSpy.count(), 1);
142 QCOMPARE(session.demoEdgesCompleted(), QStringList() << "testedge");
144 session.markDemoEdgeCompleted("testedge2");
145 QCOMPARE(changedSpy.count(), 2);
146 QCOMPARE(session.demoEdgesCompleted(), QStringList() << "testedge" << "testedge2");
147 }
149 void testAsynchronousChangeForDemoEdgesCompleted()
150 {
151 AccountsService session(this, QTest::currentTestFunction());
152 QSignalSpy changedSpy(&session, &AccountsService::demoEdgesCompletedChanged);
154 QCOMPARE(changedSpy.count(), 0);
155 QCOMPARE(session.demoEdgesCompleted(), QStringList());
157 ASSERT_DBUS_CALL(m_userInterface->call("Set",
158 "com.canonical.unity.AccountsService",
159 "DemoEdgesCompleted",
160 dbusVariant(QStringList() << "testedge")));
161 QTRY_COMPARE(changedSpy.count(), 1);
162 QCOMPARE(session.demoEdgesCompleted(), QStringList() << "testedge");
163 }
165 void testAsynchronousChangeForDemoEdges()
166 {
167 AccountsService session(this, QTest::currentTestFunction());
169 QCOMPARE(session.demoEdges(), false);
170 ASSERT_DBUS_CALL(m_userInterface->call("Set",
171 "com.canonical.unity.AccountsService",
172 "demo-edges",
173 dbusVariant(true)));
174 QTRY_COMPARE(session.demoEdges(), true);
175 }
177 void testAsynchronousChangeForFailedLogins()
178 {
179 AccountsService session(this, QTest::currentTestFunction());
181 QCOMPARE(session.failedLogins(), (uint)0);
182 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
183 "com.canonical.unity.AccountsService.Private",
184 "FailedLogins",
185 dbusVariant(5)));
186 QTRY_COMPARE(session.failedLogins(), (uint)5);
187 }
189 void testAsynchronousChangeForStatsWelcomeScreen()
190 {
191 AccountsService session(this, QTest::currentTestFunction());
193 QCOMPARE(session.statsWelcomeScreen(), true);
194 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
195 "com.ubuntu.touch.AccountsService.SecurityPrivacy",
196 "StatsWelcomeScreen",
197 dbusVariant(false)));
198 QTRY_COMPARE(session.statsWelcomeScreen(), false);
199 }
201 void testAsynchronousChangeForEnableLauncherWhileLocked()
202 {
203 AccountsService session(this, QTest::currentTestFunction());
205 QCOMPARE(session.enableLauncherWhileLocked(), true);
206 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
207 "com.ubuntu.AccountsService.SecurityPrivacy",
208 "EnableLauncherWhileLocked",
209 dbusVariant(false)));
210 QTRY_COMPARE(session.enableLauncherWhileLocked(), false);
211 }
213 void testAsynchronousChangeForEnableIndicatorsWhileLocked()
214 {
215 AccountsService session(this, QTest::currentTestFunction());
217 QCOMPARE(session.enableIndicatorsWhileLocked(), true);
218 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
219 "com.ubuntu.AccountsService.SecurityPrivacy",
220 "EnableIndicatorsWhileLocked",
221 dbusVariant(false)));
222 QTRY_COMPARE(session.enableIndicatorsWhileLocked(), false);
223 }
225 void testAsynchronousChangeForPasswordDisplayHint()
226 {
227 AccountsService session(this, QTest::currentTestFunction());
229 QCOMPARE(session.passwordDisplayHint(), AccountsService::Keyboard);
230 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
231 "com.ubuntu.AccountsService.SecurityPrivacy",
232 "PasswordDisplayHint",
233 dbusVariant(AccountsService::Numeric)));
234 QTRY_COMPARE(session.passwordDisplayHint(), AccountsService::Numeric);
235 }
237 void testAsynchronousChangeForLicenseAccepted()
238 {
239 AccountsService session(this, QTest::currentTestFunction());
241 QCOMPARE(session.hereEnabled(), false);
242 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
243 "com.ubuntu.location.providers.here.AccountsService",
244 "LicenseAccepted",
245 dbusVariant(true)));
246 QTRY_COMPARE(session.hereEnabled(), true);
247 }
249 void testAsynchronousChangeForLicenseBasePath()
250 {
251 AccountsService session(this, QTest::currentTestFunction());
253 QCOMPARE(session.hereLicensePath(), QString());
254 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
255 "com.ubuntu.location.providers.here.AccountsService",
256 "LicenseBasePath",
257 dbusVariant("/")));
258 QTRY_COMPARE(session.hereLicensePath(), QString("/"));
259 }
261 void testAsynchronousChangeForBackgroundFile()
262 {
263 AccountsService session(this, QTest::currentTestFunction());
265 QCOMPARE(session.backgroundFile(), QString());
266 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
267 "org.freedesktop.Accounts.User",
268 "BackgroundFile",
269 dbusVariant("/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 testAsynchronousChangeForKeymaps()
315 {
316 AccountsService session(this, QTest::currentTestFunction());
318 QCOMPARE(session.keymaps(), {"us"});
320 StringMapList inputSources;
321 StringMap map1;
322 map1.insert("xkb", "cz+qwerty");
323 inputSources.append(map1);
324 StringMap map2;
325 map2.insert("xkb", "fr");
326 inputSources.append(map2);
328 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
329 "org.freedesktop.Accounts.User",
330 "InputSources",
331 QVariant::fromValue(QDBusVariant(QVariant::fromValue(inputSources)))));
332 QStringList result = {"cz+qwerty", "fr"};
333 QTRY_COMPARE(session.keymaps(), result);
334 }
336Q_SIGNALS:
337 void propertiesChanged(const QString &interface, const QVariantMap &changed, const QStringList &invalid);
338 void setMousePrimaryButtonCalled(int button);
340private:
341 QDBusInterface* m_uscInputInterface;
342 QDBusInterface* m_userInterface;
343 QSignalSpy m_spy;
344 QSignalSpy m_mousePrimaryButtonSpy;
345};
347QTEST_MAIN(AccountsServiceTest)
349#include "client.moc"