on f9099ff help sign in
282 lines 12 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>
27template <class T>
28QVariant dbusVariant(const T& value) { return QVariant::fromValue(QDBusVariant(value)); }
30#define ASSERT_DBUS_CALL(call) \
31 { \
32 QDBusReply<void> reply = call; \
33 if (!reply.isValid()) QFAIL(reply.error().message().toLatin1()); \
34 }
36class AccountsServiceTest : public QObject
38 Q_OBJECT
40public:
41 AccountsServiceTest(QObject* parent = 0)
42 : QObject(parent)
43 , m_userInterface(nullptr)
44 , m_spy(this, &AccountsServiceTest::propertiesChanged)
45 , m_mousePrimaryButtonSpy(this, &AccountsServiceTest::setMousePrimaryButtonCalled)
46 {
47 m_uscInputInterface = new QDBusInterface("com.canonical.Unity.Input",
48 "/com/canonical/Unity/Input",
49 "com.canonical.Unity.Input",
50 QDBusConnection::sessionBus(),
51 this);
53 QObject::connect(m_uscInputInterface, SIGNAL(setMousePrimaryButtonCalled(int)),
54 this, SIGNAL(setMousePrimaryButtonCalled(int)));
55 }
57private Q_SLOTS:
59 void init() {
60 QDBusReply<bool> addReply = QDBusInterface("org.freedesktop.Accounts",
61 "/org/freedesktop/Accounts",
62 "org.freedesktop.Accounts").call("AddUser", QTest::currentTestFunction());
63 QVERIFY(addReply.isValid());
64 QCOMPARE(addReply.value(), true);
66 m_userInterface = new QDBusInterface("org.freedesktop.Accounts",
67 QString("/%1").arg(QTest::currentTestFunction()),
68 "org.freedesktop.DBus.Properties", QDBusConnection::sessionBus(), this);
70 QVERIFY(QObject::connect(m_userInterface, SIGNAL(PropertiesChanged(QString, QVariantMap, QStringList)),
71 this, SIGNAL(propertiesChanged(QString, QVariantMap, QStringList))));
72 }
74 void cleanup() {
75 QDBusReply<bool> reply = QDBusInterface("org.freedesktop.Accounts",
76 "/org/freedesktop/Accounts",
77 "org.freedesktop.Accounts").call("RemoveUser", QTest::currentTestFunction());
78 QVERIFY(reply.isValid());
79 QCOMPARE(reply.value(), true);
81 delete m_userInterface;
82 m_spy.clear();
83 m_mousePrimaryButtonSpy.clear();
84 }
86 void testInvalids()
87 {
88 // Test various invalid calls
89 AccountsServiceDBusAdaptor session;
90 QCOMPARE(session.getUserPropertyAsync("NOPE", "com.canonical.unity.AccountsService", "demo-edges").value(), QVariant());
91 QCOMPARE(session.getUserPropertyAsync(QTest::currentTestFunction(), "com.canonical.unity.AccountsService", "NOPE").value(), QVariant());
92 }
94 void testGetSetServiceDBusAdaptor()
95 {
96 AccountsServiceDBusAdaptor session;
97 session.setUserPropertyAsync(QTest::currentTestFunction(), "com.canonical.unity.AccountsService", "demo-edges", QVariant(true)).waitForFinished();
98 QCOMPARE(session.getUserPropertyAsync(QTest::currentTestFunction(), "com.canonical.unity.AccountsService", "demo-edges").value(), QVariant(true));
99 session.setUserPropertyAsync(QTest::currentTestFunction(), "com.canonical.unity.AccountsService", "demo-edges", QVariant(false)).waitForFinished();
100 QCOMPARE(session.getUserPropertyAsync(QTest::currentTestFunction(), "com.canonical.unity.AccountsService", "demo-edges").value(), QVariant(false));
101 }
103 void testGetSetService()
104 {
105 AccountsService session(this, QTest::currentTestFunction());
107 QCOMPARE(session.demoEdges(), false);
108 session.setDemoEdges(true);
109 QCOMPARE(session.demoEdges(), true);
111 QCOMPARE(session.failedLogins(), (uint)0);
112 session.setFailedLogins(5);
113 QCOMPARE(session.failedLogins(), (uint)5);
115 QCOMPARE(session.hereEnabled(), false);
116 session.setHereEnabled(true);
117 QCOMPARE(session.hereEnabled(), true);
118 }
120 void testAsynchronousChangeForDemoEdges()
121 {
122 AccountsService session(this, QTest::currentTestFunction());
124 QCOMPARE(session.demoEdges(), false);
125 ASSERT_DBUS_CALL(m_userInterface->call("Set",
126 "com.canonical.unity.AccountsService",
127 "demo-edges",
128 dbusVariant(true)));
129 QTRY_COMPARE(session.demoEdges(), true);
130 }
132 void testAsynchronousChangeForFailedLogins()
133 {
134 AccountsService session(this, QTest::currentTestFunction());
136 QCOMPARE(session.failedLogins(), (uint)0);
137 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
138 "com.canonical.unity.AccountsService.Private",
139 "FailedLogins",
140 dbusVariant(5)));
141 QTRY_COMPARE(session.failedLogins(), (uint)5);
142 }
144 void testAsynchronousChangeForStatsWelcomeScreen()
145 {
146 AccountsService session(this, QTest::currentTestFunction());
148 QCOMPARE(session.statsWelcomeScreen(), true);
149 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
150 "com.ubuntu.touch.AccountsService.SecurityPrivacy",
151 "StatsWelcomeScreen",
152 dbusVariant(false)));
153 QTRY_COMPARE(session.statsWelcomeScreen(), false);
154 }
156 void testAsynchronousChangeForEnableLauncherWhileLocked()
157 {
158 AccountsService session(this, QTest::currentTestFunction());
160 QCOMPARE(session.enableLauncherWhileLocked(), true);
161 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
162 "com.ubuntu.AccountsService.SecurityPrivacy",
163 "EnableLauncherWhileLocked",
164 dbusVariant(false)));
165 QTRY_COMPARE(session.enableLauncherWhileLocked(), false);
166 }
168 void testAsynchronousChangeForEnableIndicatorsWhileLocked()
169 {
170 AccountsService session(this, QTest::currentTestFunction());
172 QCOMPARE(session.enableIndicatorsWhileLocked(), true);
173 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
174 "com.ubuntu.AccountsService.SecurityPrivacy",
175 "EnableIndicatorsWhileLocked",
176 dbusVariant(false)));
177 QTRY_COMPARE(session.enableIndicatorsWhileLocked(), false);
178 }
180 void testAsynchronousChangeForPasswordDisplayHint()
181 {
182 AccountsService session(this, QTest::currentTestFunction());
184 QCOMPARE(session.passwordDisplayHint(), AccountsService::Keyboard);
185 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
186 "com.ubuntu.AccountsService.SecurityPrivacy",
187 "PasswordDisplayHint",
188 dbusVariant(AccountsService::Numeric)));
189 QTRY_COMPARE(session.passwordDisplayHint(), AccountsService::Numeric);
190 }
192 void testAsynchronousChangeForLicenseAccepted()
193 {
194 AccountsService session(this, QTest::currentTestFunction());
196 QCOMPARE(session.hereEnabled(), false);
197 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
198 "com.ubuntu.location.providers.here.AccountsService",
199 "LicenseAccepted",
200 dbusVariant(true)));
201 QTRY_COMPARE(session.hereEnabled(), true);
202 }
204 void testAsynchronousChangeForLicenseBasePath()
205 {
206 AccountsService session(this, QTest::currentTestFunction());
208 QCOMPARE(session.hereLicensePath(), QString());
209 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
210 "com.ubuntu.location.providers.here.AccountsService",
211 "LicenseBasePath",
212 dbusVariant("/")));
213 QTRY_COMPARE(session.hereLicensePath(), QString("/"));
214 }
216 void testAsynchronousChangeForBackgroundFile()
217 {
218 AccountsService session(this, QTest::currentTestFunction());
220 QCOMPARE(session.backgroundFile(), QString());
221 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
222 "org.freedesktop.Accounts.User",
223 "BackgroundFile",
224 dbusVariant("/test/BackgroundFile")));
225 QTRY_COMPARE(session.backgroundFile(), QString("/test/BackgroundFile"));
226 }
228 void testProxyOnStartup()
229 {
230 AccountsService session(this, QTest::currentTestFunction());
232 QTRY_COMPARE(m_mousePrimaryButtonSpy.count(), 1);
233 QList<QVariant> arguments = m_mousePrimaryButtonSpy.takeFirst();
234 QCOMPARE(arguments.at(0).toInt(), 1);
235 }
237 void testProxyOnChange()
238 {
239 AccountsService session(this, QTest::currentTestFunction());
240 QTRY_COMPARE(m_mousePrimaryButtonSpy.count(), 1);
241 m_mousePrimaryButtonSpy.clear();
243 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
244 "com.ubuntu.AccountsService.Input",
245 "MousePrimaryButton",
246 dbusVariant("left")));
248 QTRY_COMPARE(m_mousePrimaryButtonSpy.count(), 1);
249 QList<QVariant> arguments = m_mousePrimaryButtonSpy.takeFirst();
250 QCOMPARE(arguments.at(0).toInt(), 0);
251 }
253 void testInvalidPrimaryButton()
254 {
255 AccountsService session(this, QTest::currentTestFunction());
256 QTRY_COMPARE(m_mousePrimaryButtonSpy.count(), 1);
257 m_mousePrimaryButtonSpy.clear();
259 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
260 "com.ubuntu.AccountsService.Input",
261 "MousePrimaryButton",
262 dbusVariant("NOPE")));
264 QTRY_COMPARE(m_mousePrimaryButtonSpy.count(), 1);
265 QList<QVariant> arguments = m_mousePrimaryButtonSpy.takeFirst();
266 QCOMPARE(arguments.at(0).toInt(), 0);
267 }
269Q_SIGNALS:
270 void propertiesChanged(const QString &interface, const QVariantMap &changed, const QStringList &invalid);
271 void setMousePrimaryButtonCalled(int button);
273private:
274 QDBusInterface* m_uscInputInterface;
275 QDBusInterface* m_userInterface;
276 QSignalSpy m_spy;
277 QSignalSpy m_mousePrimaryButtonSpy;
278};
280QTEST_MAIN(AccountsServiceTest)
282#include "client.moc"