2
* Copyright (C) 2013 Canonical, Ltd.
4
* This file is part of telephony-service.
6
* telephony-service is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 3.
10
* telephony-service is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19
#include "greetercontacts.h"
22
#include <QContactAvatar>
23
#include <QContactName>
24
#include <QContactPhoneNumber>
25
#include <QDBusInterface>
26
#include <QDBusPendingCallWatcher>
32
Q_DECLARE_METATYPE(QtContacts::QContact) // for QVariant's benefit
34
QTCONTACTS_USE_NAMESPACE
36
class GreeterContactsTest : public QObject
41
GreeterContactsTest();
49
void testContactToMap();
50
void testMapToContact();
51
void testInitialValues();
52
void testSignalOnFilter();
53
void testSignalOnEntry();
54
void testSignalOnEntryInvalidated();
55
void testSignalOnContacts();
56
void testSignalOnContactsInvalidated();
57
void testEmitContact();
60
void waitForUpdatedSignal(bool convertedPath = false);
61
void makeGreeterContacts();
62
void waitForInitialQuery();
63
QContact makeTestContact(bool convertedPath = false);
64
QVariantMap makeTestMap();
65
void setActiveEntry(const QString &entry);
66
void setCurrentContact(const QVariantMap &map);
67
void setUseInvalidated(const QString &path, const QString &interface, bool useInvalidated);
69
GreeterContacts *mGreeterContacts;
75
GreeterContactsTest::GreeterContactsTest()
80
void GreeterContactsTest::initTestCase()
82
mGreeterContacts = NULL;
84
qRegisterMetaType<QContact>();
87
void GreeterContactsTest::cleanup()
93
if (mGreeterContacts) {
94
delete mGreeterContacts;
95
mGreeterContacts = NULL;
99
setCurrentContact(QVariantMap());
100
setUseInvalidated("/list", "com.canonical.UnityGreeter.List", false);
101
setUseInvalidated("/org/freedesktop/Accounts/User12345", "com.canonical.TelephonyServiceApprover", false);
104
void GreeterContactsTest::testContactToMap()
106
QVariantMap map = GreeterContacts::contactToMap(makeTestContact());
107
QVariantMap expectedMap = makeTestMap();
108
QCOMPARE(map, expectedMap);
111
void GreeterContactsTest::testMapToContact()
113
QContact contact = GreeterContacts::mapToContact(makeTestMap());
114
QContact expectedContact = makeTestContact();
115
QCOMPARE(contact, expectedContact);
118
void GreeterContactsTest::testInitialValues()
120
setActiveEntry("testuser");
121
setCurrentContact(makeTestMap());
122
makeGreeterContacts();
124
waitForUpdatedSignal();
127
void GreeterContactsTest::testSignalOnFilter()
129
setActiveEntry("testuser");
130
setCurrentContact(makeTestMap());
131
makeGreeterContacts();
132
waitForInitialQuery();
133
// setFilter might immediately send the signal, so wait until we can start the spy
134
QTimer::singleShot(0, this, SLOT(setFilter()));
135
waitForUpdatedSignal();
138
void GreeterContactsTest::testSignalOnEntry()
140
setCurrentContact(makeTestMap());
141
makeGreeterContacts();
143
waitForInitialQuery();
144
setActiveEntry("testuser");
145
waitForUpdatedSignal();
148
void GreeterContactsTest::testSignalOnEntryInvalidated()
150
setCurrentContact(makeTestMap());
151
makeGreeterContacts();
153
waitForInitialQuery();
154
setUseInvalidated("/list", "com.canonical.UnityGreeter.List", true);
155
setActiveEntry("testuser");
156
waitForUpdatedSignal();
159
void GreeterContactsTest::testSignalOnContacts()
161
setActiveEntry("testuser");
162
makeGreeterContacts();
164
waitForInitialQuery();
165
setCurrentContact(makeTestMap());
166
waitForUpdatedSignal();
169
void GreeterContactsTest::testSignalOnContactsInvalidated()
171
setActiveEntry("testuser");
172
makeGreeterContacts();
174
waitForInitialQuery();
175
setUseInvalidated("/org/freedesktop/Accounts/User12345", "com.canonical.TelephonyServiceApprover", true);
176
setCurrentContact(makeTestMap());
177
waitForUpdatedSignal();
180
void GreeterContactsTest::testEmitContact()
182
setActiveEntry("testuser");
183
makeGreeterContacts();
185
waitForInitialQuery();
186
// this next line acts like setCurrentContact() because uid is set to 12345
187
GreeterContacts::emitContact(makeTestContact());
188
waitForUpdatedSignal(true);
191
QVariantMap GreeterContactsTest::makeTestMap()
194
map.insert("FirstName", QVariant("First"));
195
map.insert("Image", QVariant(CMAKE_SOURCE_DIR "/icons/hicolor/48x48/apps/telephony-service-call.png"));
196
map.insert("LastName", QVariant("Last"));
197
map.insert("PhoneNumber", QVariant("555"));
201
QContact GreeterContactsTest::makeTestContact(bool convertedPath)
203
// This is the same contact that the test server will give out.
206
QContactAvatar avatarDetail;
209
imagePath = QDir::home().filePath(".telephony-service-contact-image");
211
imagePath = CMAKE_SOURCE_DIR "/icons/hicolor/48x48/apps/telephony-service-call.png";
213
avatarDetail.setValue(QContactAvatar::FieldImageUrl, QUrl::fromLocalFile(imagePath));
214
contact.saveDetail(&avatarDetail);
216
QContactName nameDetail;
217
nameDetail.setValue(QContactName::FieldFirstName, "First");
218
nameDetail.setValue(QContactName::FieldLastName, "Last");
219
contact.saveDetail(&nameDetail);
221
QContactPhoneNumber numberDetail;
222
numberDetail.setValue(QContactPhoneNumber::FieldNumber, "555");
223
contact.saveDetail(&numberDetail);
228
void GreeterContactsTest::setFilter()
230
mGreeterContacts->setFilter(QContactPhoneNumber::match("555"));
233
void GreeterContactsTest::setActiveEntry(const QString &entry)
235
QDBusInterface iface("com.canonical.UnityGreeter",
237
"org.freedesktop.DBus.Properties",
238
QDBusConnection::sessionBus());
239
QDBusReply<void> reply = iface.call("Set", "com.canonical.UnityGreeter.List", "ActiveEntry", QVariant::fromValue(QDBusVariant(QVariant(entry))));
240
QVERIFY(reply.isValid());
243
void GreeterContactsTest::setCurrentContact(const QVariantMap &map)
245
QDBusInterface iface("org.freedesktop.Accounts",
246
"/org/freedesktop/Accounts/User12345",
247
"org.freedesktop.DBus.Properties",
248
QDBusConnection::sessionBus());
249
QDBusReply<void> reply = iface.call("Set", "com.canonical.TelephonyServiceApprover", "CurrentContact", QVariant::fromValue(QDBusVariant(QVariant(map))));
250
QVERIFY(reply.isValid());
253
void GreeterContactsTest::waitForInitialQuery()
255
QDBusInterface iface("org.freedesktop.Accounts",
256
"/org/freedesktop/Accounts/User12345",
257
"com.canonical.TelephonyServiceApprover",
258
QDBusConnection::sessionBus());
259
QSignalSpy spy(&iface, SIGNAL(InitialQueriesDone()));
261
QCOMPARE(spy.count(), 1);
264
void GreeterContactsTest::makeGreeterContacts()
266
mGreeterContacts = new GreeterContacts();
267
mSpy = new QSignalSpy(mGreeterContacts, SIGNAL(contactUpdated(QtContacts::QContact)));
270
void GreeterContactsTest::waitForUpdatedSignal(bool convertedPath)
272
QVERIFY(mSpy->wait());
273
QCOMPARE(mSpy->count(), 1);
275
QList<QVariant> arguments = mSpy->takeFirst();
276
QContact expectedContact = makeTestContact(convertedPath);
277
QCOMPARE(arguments.at(0).value<QContact>(), expectedContact);
280
void GreeterContactsTest::setUseInvalidated(const QString &path, const QString &interface, bool useInvalidated)
282
QDBusInterface iface("org.freedesktop.Accounts",
285
QDBusConnection::sessionBus());
286
QDBusReply<void> reply = iface.call("SetUseInvalidated", useInvalidated);
287
QVERIFY(reply.isValid());
290
QTEST_MAIN(GreeterContactsTest)
291
#include "GreeterContactsTest.moc"