@@ -21,39 +21,198 @@
2121#include "AccountsServiceDBusAdaptor.h"
2222#include <QSignalSpy>
2323#include <QTest>
24+#include <QDebug>
25+#include <QDBusReply>
26+
27+template <class T>
28+QVariant dbusVariant(const T& value) { return QVariant::fromValue(QDBusVariant(value)); }
29+
30+#define ASSERT_DBUS_CALL(call) \
31+ { \
32+ QDBusReply<void> reply = call; \
33+ if (!reply.isValid()) QFAIL(reply.error().message().toLatin1()); \
34+ }
2435
2536class AccountsServiceTest : public QObject
2637{
2738 Q_OBJECT
2839
40+public:
41+ AccountsServiceTest(QObject* parent = 0)
42+ : QObject(parent)
43+ , m_userInterface(nullptr)
44+ , m_spy(this, &AccountsServiceTest::propertiesChanged)
45+ {
46+ }
47+
2948private Q_SLOTS:
3049
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);
56+
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());
62+
63+ QVERIFY(QObject::connect(m_userInterface, SIGNAL(PropertiesChanged(QString, QVariantMap, QStringList)),
64+ this, SIGNAL(propertiesChanged(QString, QVariantMap, QStringList))));
65+ }
66+
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);
73+
74+ delete m_userInterface;
75+ m_spy.clear();
76+ }
77+
3178 void testInvalids()
3279 {
3380 // Test various invalid calls
3481 AccountsServiceDBusAdaptor session;
3582 QCOMPARE(session.getUserProperty("NOPE", "com.canonical.unity.AccountsService", "demo-edges"), QVariant());
36− QCOMPARE(session.getUserProperty("testuser", "com.canonical.unity.AccountsService", "NOPE"), QVariant());
83+ QCOMPARE(session.getUserProperty(QTest::currentTestFunction(), "com.canonical.unity.AccountsService", "NOPE"), QVariant());
3784 }
3885
3986 void testGetSetServiceDBusAdaptor()
4087 {
4188 AccountsServiceDBusAdaptor session;
42− session.setUserProperty("testuser", "com.canonical.unity.AccountsService", "demo-edges", QVariant(true));
43− QCOMPARE(session.getUserProperty("testuser", "com.canonical.unity.AccountsService", "demo-edges"), QVariant(true));
44− session.setUserProperty("testuser", "com.canonical.unity.AccountsService", "demo-edges", QVariant(false));
45− QCOMPARE(session.getUserProperty("testuser", "com.canonical.unity.AccountsService", "demo-edges"), QVariant(false));
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));
4693 }
4794
4895 void testGetSetService()
4996 {
5097 AccountsService session;
51− session.setUser("testuser");
98+ session.setUser(QTest::currentTestFunction());
99+
100+ QCOMPARE(session.demoEdges(), false);
52101 session.setDemoEdges(true);
53102 QCOMPARE(session.demoEdges(), true);
54− session.setDemoEdges(false);
103+
104+ QCOMPARE(session.failedLogins(), (uint)0);
105+ session.setFailedLogins(5);
106+ QCOMPARE(session.failedLogins(), (uint)5);
107+
108+ QCOMPARE(session.hereEnabled(), false);
109+ session.setHereEnabled(true);
110+ QCOMPARE(session.hereEnabled(), true);
111+ }
112+
113+ void testAsynchornousChangeForDemoEdges()
114+ {
115+ AccountsService session;
116+ session.setUser(QTest::currentTestFunction());
117+
55118 QCOMPARE(session.demoEdges(), false);
119+ ASSERT_DBUS_CALL(m_userInterface->call("Set", "com.canonical.unity.AccountsService", "demo-edges", dbusVariant(true)));
120+ QTRY_COMPARE(session.demoEdges(), true);
121+ }
122+
123+ void testAsynchornousChangeForFailedLogins()
124+ {
125+ AccountsService session;
126+ session.setUser(QTest::currentTestFunction());
127+
128+ QCOMPARE(session.failedLogins(), (uint)0);
129+ ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set", "com.canonical.unity.AccountsService.Private", "FailedLogins", dbusVariant(5)));
130+ QTRY_COMPARE(session.failedLogins(), (uint)5);
131+ }
132+
133+ void testAsynchornousChangeForStatsWelcomeScreen()
134+ {
135+ AccountsService session;
136+ session.setUser(QTest::currentTestFunction());
137+
138+ QCOMPARE(session.statsWelcomeScreen(), true);
139+ ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set", "com.ubuntu.touch.AccountsService.SecurityPrivacy", "StatsWelcomeScreen", dbusVariant(false)));
140+ QTRY_COMPARE(session.statsWelcomeScreen(), false);
56141 }
142+
143+ void testAsynchornousChangeForStatsEnableLauncherWhileLocked()
144+ {
145+ AccountsService session;
146+ session.setUser(QTest::currentTestFunction());
147+
148+ QCOMPARE(session.enableLauncherWhileLocked(), true);
149+ ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set", "com.ubuntu.AccountsService.SecurityPrivacy", "EnableLauncherWhileLocked", dbusVariant(false)));
150+ QTRY_COMPARE(session.enableLauncherWhileLocked(), false);
151+ }
152+
153+ void testAsynchornousChangeForStatsEnableIndicatorsWhileLocked()
154+ {
155+ AccountsService session;
156+ session.setUser(QTest::currentTestFunction());
157+
158+ QCOMPARE(session.enableIndicatorsWhileLocked(), true);
159+ ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set", "com.ubuntu.AccountsService.SecurityPrivacy", "EnableIndicatorsWhileLocked", dbusVariant(false)));
160+ QTRY_COMPARE(session.enableIndicatorsWhileLocked(), false);
161+ }
162+
163+ void testAsynchornousChangeForStatsPasswordDisplayHint()
164+ {
165+ AccountsService session;
166+ session.setUser(QTest::currentTestFunction());
167+
168+ QCOMPARE(session.passwordDisplayHint(), AccountsService::Keyboard);
169+ ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set", "com.ubuntu.AccountsService.SecurityPrivacy", "PasswordDisplayHint", dbusVariant(AccountsService::Numeric)));
170+ QTRY_COMPARE(session.passwordDisplayHint(), AccountsService::Numeric);
171+ }
172+
173+ void testAsynchornousChangeForStatsLicenseAccepted()
174+ {
175+ AccountsService session;
176+ session.setUser(QTest::currentTestFunction());
177+
178+ QCOMPARE(session.hereEnabled(), false);
179+ ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set", "com.ubuntu.location.providers.here.AccountsService", "LicenseAccepted", dbusVariant(true)));
180+ QTRY_COMPARE(session.hereEnabled(), true);
181+ }
182+
183+ void testAsynchornousChangeForLicenseBasePath()
184+ {
185+ AccountsService session;
186+ session.setUser(QTest::currentTestFunction());
187+
188+ QCOMPARE(session.hereLicensePath(), QString());
189+ ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set", "com.ubuntu.location.providers.here.AccountsService", "LicenseBasePath", dbusVariant("/")));
190+ QTRY_COMPARE(session.hereLicensePath(), QString("/"));
191+ }
192+
193+ void testAsynchornousChangeForStatsBackgroundFile()
194+ {
195+ AccountsService session;
196+ session.setUser(QTest::currentTestFunction());
197+
198+ QCOMPARE(session.backgroundFile(), QString());
199+ ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set", "org.freedesktop.Accounts.User", "BackgroundFile", dbusVariant("/test/BackgroundFile")));
200+ QTRY_COMPARE(session.backgroundFile(), QString("/test/BackgroundFile"));
201+ }
202+
203+Q_SIGNALS:
204+ void propertiesChanged(const QString &interface, const QVariantMap &changed, const QStringList &invalid);
205+
206+//public Q_SLOTS:
207+// void propertiesChangedSlot(const QString &interface, const QVariantMap &changed, const QStringList &invalid) {
208+// QDBusInterface* pInterface = qobject_cast<QDBusInterface*>(sender());
209+// qDebug() << "propertiesChangedSlot" << pInterface->path() << QTest::currentTestFunction() << changed;
210+// Q_EMIT propertiesChanged(interface, changed, invalid);
211+// }
212+
213+private:
214+ QDBusInterface* m_userInterface;
215+ QSignalSpy m_spy;
57216};
58217
59218QTEST_MAIN(AccountsServiceTest)