~phablet-team/telephony-service/trunk

« back to all changes in this revision

Viewing changes to approver/tests/GreeterContactsTest.cpp

  • Committer: CI bot
  • Author(s): Michael Terry
  • Date: 2014-02-06 14:19:18 UTC
  • mfrom: (762.2.2 greeter)
  • Revision ID: ps-jenkins@lists.canonical.com-20140206141918-68sl1hbl9wcomb3s
Share current call contact info with the greeter. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2013 Canonical, Ltd.
 
3
 *
 
4
 * This file is part of telephony-service.
 
5
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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/>.
 
17
 */
 
18
 
 
19
#include "greetercontacts.h"
 
20
 
 
21
#include <QContact>
 
22
#include <QContactAvatar>
 
23
#include <QContactName>
 
24
#include <QContactPhoneNumber>
 
25
#include <QDBusInterface>
 
26
#include <QDBusPendingCallWatcher>
 
27
#include <QDBusReply>
 
28
#include <QObject>
 
29
#include <QtTest>
 
30
#include <QUrl>
 
31
 
 
32
Q_DECLARE_METATYPE(QtContacts::QContact) // for QVariant's benefit
 
33
 
 
34
QTCONTACTS_USE_NAMESPACE
 
35
 
 
36
class GreeterContactsTest : public QObject
 
37
{
 
38
    Q_OBJECT
 
39
 
 
40
public:
 
41
    GreeterContactsTest();
 
42
 
 
43
public Q_SLOTS:
 
44
    void setFilter();
 
45
 
 
46
private Q_SLOTS:
 
47
    void initTestCase();
 
48
    void cleanup();
 
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();
 
58
 
 
59
private:
 
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);
 
68
 
 
69
    GreeterContacts *mGreeterContacts;
 
70
    QSignalSpy *mSpy;
 
71
};
 
72
 
 
73
 
 
74
 
 
75
GreeterContactsTest::GreeterContactsTest()
 
76
: QObject()
 
77
{
 
78
}
 
79
 
 
80
void GreeterContactsTest::initTestCase()
 
81
{
 
82
    mGreeterContacts = NULL;
 
83
    mSpy = NULL;
 
84
    qRegisterMetaType<QContact>();
 
85
}
 
86
 
 
87
void GreeterContactsTest::cleanup()
 
88
{
 
89
    if (mSpy) {
 
90
        delete mSpy;
 
91
        mSpy = NULL;
 
92
    }
 
93
    if (mGreeterContacts) {
 
94
        delete mGreeterContacts;
 
95
        mGreeterContacts = NULL;
 
96
    }
 
97
 
 
98
    setActiveEntry("");
 
99
    setCurrentContact(QVariantMap());
 
100
    setUseInvalidated("/list", "com.canonical.UnityGreeter.List", false);
 
101
    setUseInvalidated("/org/freedesktop/Accounts/User12345", "com.canonical.TelephonyServiceApprover", false);
 
102
}
 
103
 
 
104
void GreeterContactsTest::testContactToMap()
 
105
{
 
106
    QVariantMap map = GreeterContacts::contactToMap(makeTestContact());
 
107
    QVariantMap expectedMap = makeTestMap();
 
108
    QCOMPARE(map, expectedMap);
 
109
}
 
110
 
 
111
void GreeterContactsTest::testMapToContact()
 
112
{
 
113
    QContact contact = GreeterContacts::mapToContact(makeTestMap());
 
114
    QContact expectedContact = makeTestContact();
 
115
    QCOMPARE(contact, expectedContact);
 
116
}
 
117
 
 
118
void GreeterContactsTest::testInitialValues()
 
119
{
 
120
    setActiveEntry("testuser");
 
121
    setCurrentContact(makeTestMap());
 
122
    makeGreeterContacts();
 
123
    setFilter();
 
124
    waitForUpdatedSignal();
 
125
}
 
126
 
 
127
void GreeterContactsTest::testSignalOnFilter()
 
128
{
 
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();
 
136
}
 
137
 
 
138
void GreeterContactsTest::testSignalOnEntry()
 
139
{
 
140
    setCurrentContact(makeTestMap());
 
141
    makeGreeterContacts();
 
142
    setFilter();
 
143
    waitForInitialQuery();
 
144
    setActiveEntry("testuser");
 
145
    waitForUpdatedSignal();
 
146
}
 
147
 
 
148
void GreeterContactsTest::testSignalOnEntryInvalidated()
 
149
{
 
150
    setCurrentContact(makeTestMap());
 
151
    makeGreeterContacts();
 
152
    setFilter();
 
153
    waitForInitialQuery();
 
154
    setUseInvalidated("/list", "com.canonical.UnityGreeter.List", true);
 
155
    setActiveEntry("testuser");
 
156
    waitForUpdatedSignal();
 
157
}
 
158
 
 
159
void GreeterContactsTest::testSignalOnContacts()
 
160
{
 
161
    setActiveEntry("testuser");
 
162
    makeGreeterContacts();
 
163
    setFilter();
 
164
    waitForInitialQuery();
 
165
    setCurrentContact(makeTestMap());
 
166
    waitForUpdatedSignal();
 
167
}
 
168
 
 
169
void GreeterContactsTest::testSignalOnContactsInvalidated()
 
170
{
 
171
    setActiveEntry("testuser");
 
172
    makeGreeterContacts();
 
173
    setFilter();
 
174
    waitForInitialQuery();
 
175
    setUseInvalidated("/org/freedesktop/Accounts/User12345", "com.canonical.TelephonyServiceApprover", true);
 
176
    setCurrentContact(makeTestMap());
 
177
    waitForUpdatedSignal();
 
178
}
 
179
 
 
180
void GreeterContactsTest::testEmitContact()
 
181
{
 
182
    setActiveEntry("testuser");
 
183
    makeGreeterContacts();
 
184
    setFilter();
 
185
    waitForInitialQuery();
 
186
    // this next line acts like setCurrentContact() because uid is set to 12345
 
187
    GreeterContacts::emitContact(makeTestContact());
 
188
    waitForUpdatedSignal(true);
 
189
}
 
190
 
 
191
QVariantMap GreeterContactsTest::makeTestMap()
 
192
{
 
193
    QVariantMap map;
 
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"));
 
198
    return map;
 
199
}
 
200
 
 
201
QContact GreeterContactsTest::makeTestContact(bool convertedPath)
 
202
{
 
203
    // This is the same contact that the test server will give out.
 
204
    QContact contact;
 
205
 
 
206
    QContactAvatar avatarDetail;
 
207
    QString imagePath;
 
208
    if (convertedPath) {
 
209
        imagePath = QDir::home().filePath(".telephony-service-contact-image");
 
210
    } else {
 
211
        imagePath = CMAKE_SOURCE_DIR "/icons/hicolor/48x48/apps/telephony-service-call.png";
 
212
    }
 
213
    avatarDetail.setValue(QContactAvatar::FieldImageUrl, QUrl::fromLocalFile(imagePath));
 
214
    contact.saveDetail(&avatarDetail);
 
215
 
 
216
    QContactName nameDetail;
 
217
    nameDetail.setValue(QContactName::FieldFirstName, "First");
 
218
    nameDetail.setValue(QContactName::FieldLastName, "Last");
 
219
    contact.saveDetail(&nameDetail);
 
220
 
 
221
    QContactPhoneNumber numberDetail;
 
222
    numberDetail.setValue(QContactPhoneNumber::FieldNumber, "555");
 
223
    contact.saveDetail(&numberDetail);
 
224
 
 
225
    return contact;
 
226
}
 
227
 
 
228
void GreeterContactsTest::setFilter()
 
229
{
 
230
    mGreeterContacts->setFilter(QContactPhoneNumber::match("555"));
 
231
}
 
232
 
 
233
void GreeterContactsTest::setActiveEntry(const QString &entry)
 
234
{
 
235
    QDBusInterface iface("com.canonical.UnityGreeter",
 
236
                         "/list",
 
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());
 
241
}
 
242
 
 
243
void GreeterContactsTest::setCurrentContact(const QVariantMap &map)
 
244
{
 
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());
 
251
}
 
252
 
 
253
void GreeterContactsTest::waitForInitialQuery()
 
254
{
 
255
    QDBusInterface iface("org.freedesktop.Accounts",
 
256
                         "/org/freedesktop/Accounts/User12345",
 
257
                         "com.canonical.TelephonyServiceApprover",
 
258
                         QDBusConnection::sessionBus());
 
259
    QSignalSpy spy(&iface, SIGNAL(InitialQueriesDone()));
 
260
    QVERIFY(spy.wait());
 
261
    QCOMPARE(spy.count(), 1);
 
262
}
 
263
 
 
264
void GreeterContactsTest::makeGreeterContacts()
 
265
{
 
266
    mGreeterContacts = new GreeterContacts();
 
267
    mSpy = new QSignalSpy(mGreeterContacts, SIGNAL(contactUpdated(QtContacts::QContact)));
 
268
}
 
269
 
 
270
void GreeterContactsTest::waitForUpdatedSignal(bool convertedPath)
 
271
{
 
272
    QVERIFY(mSpy->wait());
 
273
    QCOMPARE(mSpy->count(), 1);
 
274
 
 
275
    QList<QVariant> arguments = mSpy->takeFirst();
 
276
    QContact expectedContact = makeTestContact(convertedPath);
 
277
    QCOMPARE(arguments.at(0).value<QContact>(), expectedContact);
 
278
}
 
279
 
 
280
void GreeterContactsTest::setUseInvalidated(const QString &path, const QString &interface, bool useInvalidated)
 
281
{
 
282
    QDBusInterface iface("org.freedesktop.Accounts",
 
283
                         path,
 
284
                         interface,
 
285
                         QDBusConnection::sessionBus());
 
286
    QDBusReply<void> reply = iface.call("SetUseInvalidated", useInvalidated);
 
287
    QVERIFY(reply.isValid());
 
288
}
 
289
 
 
290
QTEST_MAIN(GreeterContactsTest)
 
291
#include "GreeterContactsTest.moc"