on 5578813 help sign in
267 lines 11 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 {
46 }
48private Q_SLOTS:
50 void init() {
51 QDBusReply<bool> addReply = QDBusInterface("org.freedesktop.Accounts",
52 "/org/freedesktop/Accounts",
53 "org.freedesktop.Accounts").call("AddUser", QTest::currentTestFunction());
54 QVERIFY(addReply.isValid());
55 QCOMPARE(addReply.value(), true);
57 m_userInterface = new QDBusInterface("org.freedesktop.Accounts",
58 QString("/%1").arg(QTest::currentTestFunction()),
59 "org.freedesktop.DBus.Properties", QDBusConnection::sessionBus(), this);
60 QDBusReply<void> resetReply = m_userInterface->call("Reset");
61 QVERIFY(resetReply.isValid());
63 QVERIFY(QObject::connect(m_userInterface, SIGNAL(PropertiesChanged(QString, QVariantMap, QStringList)),
64 this, SIGNAL(propertiesChanged(QString, QVariantMap, QStringList))));
65 }
67 void cleanup() {
68 QDBusReply<bool> reply = QDBusInterface("org.freedesktop.Accounts",
69 "/org/freedesktop/Accounts",
70 "org.freedesktop.Accounts").call("RemoveUser", QTest::currentTestFunction());
71 QVERIFY(reply.isValid());
72 QCOMPARE(reply.value(), true);
74 delete m_userInterface;
75 m_spy.clear();
76 }
78 void testInvalids()
79 {
80 // Test various invalid calls
81 AccountsServiceDBusAdaptor session;
82 QCOMPARE(session.getUserProperty("NOPE", "com.canonical.unity.AccountsService", "demo-edges"), QVariant());
83 QCOMPARE(session.getUserProperty(QTest::currentTestFunction(), "com.canonical.unity.AccountsService", "NOPE"), QVariant());
84 }
86 void testGetSetServiceDBusAdaptor()
87 {
88 AccountsServiceDBusAdaptor session;
89 session.setUserProperty(QTest::currentTestFunction(), "com.canonical.unity.AccountsService", "demo-edges", QVariant(true));
90 QCOMPARE(session.getUserProperty(QTest::currentTestFunction(), "com.canonical.unity.AccountsService", "demo-edges"), QVariant(true));
91 session.setUserProperty(QTest::currentTestFunction(), "com.canonical.unity.AccountsService", "demo-edges", QVariant(false));
92 QCOMPARE(session.getUserProperty(QTest::currentTestFunction(), "com.canonical.unity.AccountsService", "demo-edges"), QVariant(false));
93 }
95 void testGetSetService()
96 {
97 AccountsService session(this, QTest::currentTestFunction());
99 QCOMPARE(session.demoEdges(), false);
100 session.setDemoEdges(true);
101 QCOMPARE(session.demoEdges(), true);
103 QCOMPARE(session.failedLogins(), (uint)0);
104 session.setFailedLogins(5);
105 QCOMPARE(session.failedLogins(), (uint)5);
107 QCOMPARE(session.hereEnabled(), false);
108 session.setHereEnabled(true);
109 QCOMPARE(session.hereEnabled(), true);
110 }
112 void testMarkDemoEdgeCompleted()
113 {
114 AccountsService session(this, QTest::currentTestFunction());
115 QSignalSpy changedSpy(&session, &AccountsService::demoEdgesCompletedChanged);
117 QCOMPARE(changedSpy.count(), 0);
118 QCOMPARE(session.demoEdgesCompleted(), QStringList());
120 session.markDemoEdgeCompleted("testedge");
121 QCOMPARE(changedSpy.count(), 1);
122 QCOMPARE(session.demoEdgesCompleted(), QStringList() << "testedge");
124 session.markDemoEdgeCompleted("testedge");
125 QCOMPARE(changedSpy.count(), 1);
126 QCOMPARE(session.demoEdgesCompleted(), QStringList() << "testedge");
128 session.markDemoEdgeCompleted("testedge2");
129 QCOMPARE(changedSpy.count(), 2);
130 QCOMPARE(session.demoEdgesCompleted(), QStringList() << "testedge" << "testedge2");
131 }
133 void testAsynchronousChangeForDemoEdgesCompleted()
134 {
135 AccountsService session(this, QTest::currentTestFunction());
136 QSignalSpy changedSpy(&session, &AccountsService::demoEdgesCompletedChanged);
138 QCOMPARE(changedSpy.count(), 0);
139 QCOMPARE(session.demoEdgesCompleted(), QStringList());
141 ASSERT_DBUS_CALL(m_userInterface->call("Set",
142 "com.canonical.unity.AccountsService",
143 "DemoEdgesCompleted",
144 dbusVariant(QStringList() << "testedge")));
145 QTRY_COMPARE(changedSpy.count(), 1);
146 QCOMPARE(session.demoEdgesCompleted(), QStringList() << "testedge");
147 }
149 void testAsynchornousChangeForDemoEdges()
150 {
151 AccountsService session(this, QTest::currentTestFunction());
153 QCOMPARE(session.demoEdges(), false);
154 ASSERT_DBUS_CALL(m_userInterface->call("Set",
155 "com.canonical.unity.AccountsService",
156 "demo-edges",
157 dbusVariant(true)));
158 QTRY_COMPARE(session.demoEdges(), true);
159 }
161 void testAsynchornousChangeForFailedLogins()
162 {
163 AccountsService session(this, QTest::currentTestFunction());
165 QCOMPARE(session.failedLogins(), (uint)0);
166 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
167 "com.canonical.unity.AccountsService.Private",
168 "FailedLogins",
169 dbusVariant(5)));
170 QTRY_COMPARE(session.failedLogins(), (uint)5);
171 }
173 void testAsynchornousChangeForStatsWelcomeScreen()
174 {
175 AccountsService session(this, QTest::currentTestFunction());
177 QCOMPARE(session.statsWelcomeScreen(), true);
178 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
179 "com.ubuntu.touch.AccountsService.SecurityPrivacy",
180 "StatsWelcomeScreen",
181 dbusVariant(false)));
182 QTRY_COMPARE(session.statsWelcomeScreen(), false);
183 }
185 void testAsynchornousChangeForStatsEnableLauncherWhileLocked()
186 {
187 AccountsService session(this, QTest::currentTestFunction());
189 QCOMPARE(session.enableLauncherWhileLocked(), true);
190 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
191 "com.ubuntu.AccountsService.SecurityPrivacy",
192 "EnableLauncherWhileLocked",
193 dbusVariant(false)));
194 QTRY_COMPARE(session.enableLauncherWhileLocked(), false);
195 }
197 void testAsynchornousChangeForStatsEnableIndicatorsWhileLocked()
198 {
199 AccountsService session(this, QTest::currentTestFunction());
201 QCOMPARE(session.enableIndicatorsWhileLocked(), true);
202 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
203 "com.ubuntu.AccountsService.SecurityPrivacy",
204 "EnableIndicatorsWhileLocked",
205 dbusVariant(false)));
206 QTRY_COMPARE(session.enableIndicatorsWhileLocked(), false);
207 }
209 void testAsynchornousChangeForStatsPasswordDisplayHint()
210 {
211 AccountsService session(this, QTest::currentTestFunction());
213 QCOMPARE(session.passwordDisplayHint(), AccountsService::Keyboard);
214 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
215 "com.ubuntu.AccountsService.SecurityPrivacy",
216 "PasswordDisplayHint",
217 dbusVariant(AccountsService::Numeric)));
218 QTRY_COMPARE(session.passwordDisplayHint(), AccountsService::Numeric);
219 }
221 void testAsynchornousChangeForStatsLicenseAccepted()
222 {
223 AccountsService session(this, QTest::currentTestFunction());
225 QCOMPARE(session.hereEnabled(), false);
226 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
227 "com.ubuntu.location.providers.here.AccountsService",
228 "LicenseAccepted",
229 dbusVariant(true)));
230 QTRY_COMPARE(session.hereEnabled(), true);
231 }
233 void testAsynchornousChangeForLicenseBasePath()
234 {
235 AccountsService session(this, QTest::currentTestFunction());
237 QCOMPARE(session.hereLicensePath(), QString());
238 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
239 "com.ubuntu.location.providers.here.AccountsService",
240 "LicenseBasePath",
241 dbusVariant("/")));
242 QTRY_COMPARE(session.hereLicensePath(), QString("/"));
243 }
245 void testAsynchornousChangeForStatsBackgroundFile()
246 {
247 AccountsService session(this, QTest::currentTestFunction());
249 QCOMPARE(session.backgroundFile(), QString());
250 ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
251 "org.freedesktop.Accounts.User",
252 "BackgroundFile",
253 dbusVariant("/test/BackgroundFile")));
254 QTRY_COMPARE(session.backgroundFile(), QString("/test/BackgroundFile"));
255 }
257Q_SIGNALS:
258 void propertiesChanged(const QString &interface, const QVariantMap &changed, const QStringList &invalid);
260private:
261 QDBusInterface* m_userInterface;
262 QSignalSpy m_spy;
263};
265QTEST_MAIN(AccountsServiceTest)
267#include "client.moc"