~chris.gagnon/+junk/qtpim-coverage

« back to all changes in this revision

Viewing changes to tests/auto/contacts/qcontactmanagerdetails/tst_qcontactmanagerdetails.cpp

  • Committer: chris.gagnon
  • Date: 2013-12-10 23:09:37 UTC
  • Revision ID: chris.gagnon@canonical.com-20131210230937-2akf1ft1edcttk87
first post

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/legal
 
5
**
 
6
** This file is part of the test suite of the Qt Toolkit.
 
7
**
 
8
** $QT_BEGIN_LICENSE:LGPL$
 
9
** Commercial License Usage
 
10
** Licensees holding valid commercial Qt licenses may use this file in
 
11
** accordance with the commercial license agreement provided with the
 
12
** Software or, alternatively, in accordance with the terms contained in
 
13
** a written agreement between you and Digia.  For licensing terms and
 
14
** conditions see http://qt.digia.com/licensing.  For further information
 
15
** use the contact form at http://qt.digia.com/contact-us.
 
16
**
 
17
** GNU Lesser General Public License Usage
 
18
** Alternatively, this file may be used under the terms of the GNU Lesser
 
19
** General Public License version 2.1 as published by the Free Software
 
20
** Foundation and appearing in the file LICENSE.LGPL included in the
 
21
** packaging of this file.  Please review the following information to
 
22
** ensure the GNU Lesser General Public License version 2.1 requirements
 
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 
24
**
 
25
** In addition, as a special exception, Digia gives you certain additional
 
26
** rights.  These rights are described in the Digia Qt LGPL Exception
 
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 
28
**
 
29
** GNU General Public License Usage
 
30
** Alternatively, this file may be used under the terms of the GNU
 
31
** General Public License version 3.0 as published by the Free Software
 
32
** Foundation and appearing in the file LICENSE.GPL included in the
 
33
** packaging of this file.  Please review the following information to
 
34
** ensure the GNU General Public License version 3.0 requirements will be
 
35
** met: http://www.gnu.org/copyleft/gpl.html.
 
36
**
 
37
**
 
38
** $QT_END_LICENSE$
 
39
**
 
40
****************************************************************************/
 
41
 
 
42
#include <QtTest/QtTest>
 
43
#include <QDateTime>
 
44
#include <QtContacts>
 
45
 
 
46
#include "qcontactmanagerdataholder.h"
 
47
#include "jsondbprocess.h"
 
48
 
 
49
QTCONTACTS_USE_NAMESPACE
 
50
 
 
51
// to get QFETCH to work with the template expression...
 
52
typedef QMap<QString,QString> tst_QContactManagerDetails_QStringMap;
 
53
Q_DECLARE_METATYPE(tst_QContactManagerDetails_QStringMap)
 
54
 
 
55
 
 
56
class tst_QContactManagerDetails : public QObject
 
57
{
 
58
Q_OBJECT
 
59
 
 
60
public:
 
61
    tst_QContactManagerDetails();
 
62
    virtual ~tst_QContactManagerDetails();
 
63
 
 
64
private:
 
65
    void addManagers();
 
66
 
 
67
    template <typename T>
 
68
    void removeDetail( QContact& contact )
 
69
    {
 
70
        T detail = contact.detail<T>();
 
71
        contact.removeDetail( &detail );
 
72
    }
 
73
 
 
74
    bool saveAndLoadContact( QContactManager *cm, QContact &original, QContact &loaded );
 
75
    void saveAndVerifyContact( QContactManager *cm, QContact &original );
 
76
 
 
77
    QScopedPointer<QContactManagerDataHolder> managerDataHolder;
 
78
 
 
79
    JsonDbProcess m_jsondbProcess;
 
80
 
 
81
private slots:
 
82
    void initTestCase();
 
83
    void cleanupTestCase();
 
84
 
 
85
    void testAddress();
 
86
    void testAddress_data() {addManagers();}
 
87
    void testAnniversary();
 
88
    void testAnniversary_data() {addManagers();}
 
89
    void testAvatar();
 
90
    void testAvatar_data() {addManagers();}
 
91
    void testBirthday();
 
92
    void testBirthday_data() {addManagers();}
 
93
    void testDisplayLabel();
 
94
    void testDisplayLabel_data() {addManagers();}
 
95
    void testEmailAddress();
 
96
    void testEmailAddress_data() {addManagers();}
 
97
    void testEmptyExtendedDetail();
 
98
    void testEmptyExtendedDetail_data() {addManagers();}
 
99
    void testExtendedDetail();
 
100
    void testExtendedDetail_data() {addManagers();}
 
101
    void testGuid();
 
102
    void testGuid_data() {addManagers();}
 
103
    void testName();
 
104
    void testName_data() {addManagers();}
 
105
    void testNickName();
 
106
    void testNickName_data() {addManagers();}
 
107
    void testOrganisation();
 
108
    void testOrganisation_data() {addManagers();}
 
109
    void testOnlineAccount();
 
110
    void testOnlineAccount_data() {addManagers();}
 
111
    void testPhoneNumber();
 
112
    void testPhoneNumber_data() {addManagers();}
 
113
    void testSyncTarget();
 
114
    void testSyncTarget_data() {addManagers();}
 
115
    void testUrl();
 
116
    void testUrl_data() {addManagers();}
 
117
    void testRingtone();
 
118
    void testRingtone_data() {addManagers();}
 
119
 
 
120
private slots:
 
121
 
 
122
};
 
123
 
 
124
tst_QContactManagerDetails::tst_QContactManagerDetails()
 
125
{
 
126
}
 
127
 
 
128
tst_QContactManagerDetails::~tst_QContactManagerDetails()
 
129
{
 
130
}
 
131
 
 
132
void tst_QContactManagerDetails::addManagers()
 
133
{
 
134
    QTest::addColumn<QString>("uri");
 
135
 
 
136
    QStringList managers = QContactManager::availableManagers();
 
137
 
 
138
    /* Known one that will not pass */
 
139
    managers.removeAll("invalid");
 
140
    managers.removeAll("testdummy");
 
141
    managers.removeAll("teststaticdummy");
 
142
    managers.removeAll("maliciousplugin");
 
143
 
 
144
    foreach(QString mgr, managers) {
 
145
        QMap<QString, QString> params;
 
146
        QTest::newRow(QString("mgr='%1'").arg(mgr).toLatin1().constData()) << QContactManager::buildUri(mgr, params);
 
147
    }
 
148
}
 
149
 
 
150
void tst_QContactManagerDetails::initTestCase()
 
151
{
 
152
    // Start JsonDb daemon if needed
 
153
    if (QContactManager::availableManagers().contains("jsondb")) {
 
154
        QString partitions_json = QFINDTESTDATA("partitions.json");
 
155
        QVERIFY2(!partitions_json.isEmpty(), "partitions.json file is missing");
 
156
        QVERIFY2(m_jsondbProcess.start(partitions_json), "Failed to start JsonDb process");
 
157
    }
 
158
 
 
159
    managerDataHolder.reset(new QContactManagerDataHolder());
 
160
}
 
161
 
 
162
void tst_QContactManagerDetails::cleanupTestCase()
 
163
{
 
164
    managerDataHolder.reset(0);
 
165
 
 
166
    if (QContactManager::availableManagers().contains("jsondb"))
 
167
        m_jsondbProcess.terminate();
 
168
}
 
169
 
 
170
bool tst_QContactManagerDetails::saveAndLoadContact( QContactManager *cm, QContact &original, QContact &loaded )
 
171
{
 
172
    // Save contact
 
173
    if( cm->saveContact(&original) == false )
 
174
        return false;
 
175
 
 
176
    // Check the id
 
177
    if (original.id().isNull())
 
178
        return false;
 
179
 
 
180
    // Load same contact from database
 
181
    loaded = cm->contact(original.id());
 
182
    if( cm->error() )
 
183
        return false;
 
184
 
 
185
    // Ignore some details which are not relevant and will mess
 
186
    // up direct comparison between two contacts.
 
187
    removeDetail<QContactGuid>(original);
 
188
    removeDetail<QContactGuid>(loaded);
 
189
    removeDetail<QContactTimestamp>(original);
 
190
    removeDetail<QContactTimestamp>(loaded);
 
191
    removeDetail<QContactVersion>(loaded);
 
192
 
 
193
    return true;
 
194
}
 
195
 
 
196
void tst_QContactManagerDetails::saveAndVerifyContact( QContactManager *cm, QContact &original )
 
197
{
 
198
    QContact loaded;
 
199
    QVERIFY( saveAndLoadContact( cm, original, loaded ) );
 
200
    if (original != loaded) {
 
201
        qDebug() << "expected: " << original;
 
202
        qDebug() << "loaded: " << loaded;
 
203
        QCOMPARE( loaded.details().count(), original.details().count() );
 
204
        QCOMPARE( loaded, original );
 
205
    }
 
206
}
 
207
 
 
208
void tst_QContactManagerDetails::testAddress()
 
209
{
 
210
    QFETCH(QString, uri);
 
211
    QScopedPointer<QContactManager> cm(QContactManager::fromUri(uri));
 
212
 
 
213
    QContact c;
 
214
 
 
215
    QContactAddress a;
 
216
    a.setStreet("street");
 
217
    a.setLocality("locality");
 
218
    a.setRegion("region");
 
219
    a.setPostcode("postcode");
 
220
    a.setCountry("country");
 
221
    a.setPostOfficeBox("POBox");
 
222
    c.saveDetail( &a );
 
223
 
 
224
    saveAndVerifyContact( cm.data(), c );
 
225
    QContact c1;
 
226
 
 
227
    QContactAddress a1;
 
228
    a1.setStreet("street1");
 
229
    a1.setLocality("locality1");
 
230
    a1.setRegion("region1");
 
231
    a1.setPostcode("postcode1");
 
232
    a1.setCountry("country1");
 
233
    a1.setPostOfficeBox("POBox1");
 
234
    c1.saveDetail( &a1 );
 
235
 
 
236
    QContactAddress a2;
 
237
    a2.setStreet("street2");
 
238
    a2.setLocality("locality2");
 
239
    a2.setRegion("region2");
 
240
    a2.setPostcode("postcode2");
 
241
    a2.setCountry("country2");
 
242
    a2.setPostOfficeBox("POBox2");
 
243
    c1.saveDetail( &a2 );
 
244
 
 
245
    QContact c2;
 
246
    saveAndLoadContact( cm.data(), c1, c2 );
 
247
 
 
248
    QList<QContactAddress> addresses = c2.details<QContactAddress>();
 
249
    QCOMPARE(addresses.count(), 2);
 
250
    QCOMPARE(addresses[0], a1);
 
251
    QCOMPARE(addresses[1], a2);
 
252
 
 
253
    QContact c3;
 
254
 
 
255
    // General address
 
256
    QContactAddress a3;
 
257
    a3.setStreet("street1");
 
258
    c3.saveDetail( &a3 );
 
259
 
 
260
    // home address
 
261
    QContactAddress a4;
 
262
    a4.setStreet("street2");
 
263
    a4.setContexts( QContactDetail::ContextHome );
 
264
    c3.saveDetail( &a4 );
 
265
 
 
266
    // work address
 
267
    QContactAddress a5;
 
268
    a5.setStreet("street3");
 
269
    a5.setContexts( QContactDetail::ContextWork );
 
270
    c3.saveDetail( &a5 );
 
271
 
 
272
    saveAndVerifyContact( cm.data(), c3 );
 
273
}
 
274
 
 
275
void tst_QContactManagerDetails::testAnniversary()
 
276
{
 
277
    QFETCH(QString, uri);
 
278
    if (uri == "qtcontacts:jsondb:") {
 
279
        QSKIP("JsonDb backend does not support QContactAnniversary detail!");
 
280
    } else {
 
281
        QScopedPointer<QContactManager> cm(QContactManager::fromUri(uri));
 
282
        QContact c;
 
283
        QContactAnniversary a;
 
284
 
 
285
        a.setOriginalDate( QDate(2009,9,9) );
 
286
        c.saveDetail( &a );
 
287
        saveAndVerifyContact( cm.data(), c );
 
288
    }
 
289
}
 
290
 
 
291
void tst_QContactManagerDetails::testAvatar()
 
292
{
 
293
    QFETCH(QString, uri);
 
294
    QScopedPointer<QContactManager> cm(QContactManager::fromUri(uri));
 
295
 
 
296
    QContact c;
 
297
 
 
298
    QContactAvatar a;
 
299
    a.setImageUrl( QUrl::fromLocalFile("c:\\foo\\bar.jpeg") );
 
300
    c.saveDetail( &a );
 
301
 
 
302
    saveAndVerifyContact( cm.data(), c );
 
303
}
 
304
 
 
305
void tst_QContactManagerDetails::testBirthday()
 
306
{
 
307
    QFETCH(QString, uri);
 
308
    QScopedPointer<QContactManager> cm(QContactManager::fromUri(uri));
 
309
 
 
310
    QContact c;
 
311
 
 
312
    QContactBirthday b;
 
313
    b.setDateTime( QDateTime(QDate(2001,1,1)) );
 
314
    c.saveDetail( &b );
 
315
 
 
316
    saveAndVerifyContact( cm.data(), c );
 
317
}
 
318
 
 
319
void tst_QContactManagerDetails::testEmailAddress()
 
320
{
 
321
    QFETCH(QString, uri);
 
322
    QScopedPointer<QContactManager> cm(QContactManager::fromUri(uri));
 
323
 
 
324
    QContact c;
 
325
 
 
326
    QContactEmailAddress e;
 
327
    e.setEmailAddress( "foo@bar.com" );
 
328
    c.saveDetail( &e );
 
329
 
 
330
    saveAndVerifyContact( cm.data(), c );
 
331
}
 
332
 
 
333
void tst_QContactManagerDetails::testName()
 
334
{
 
335
    QFETCH(QString, uri);
 
336
    QScopedPointer<QContactManager> cm(QContactManager::fromUri(uri));
 
337
    QContact c;
 
338
 
 
339
    QContactName n;
 
340
    n.setPrefix( "prefix" );
 
341
    n.setFirstName( "first" );
 
342
    n.setMiddleName( "middle" );
 
343
    n.setLastName( "last" );
 
344
    n.setSuffix( "suffix" );
 
345
    c.saveDetail( &n );
 
346
    saveAndVerifyContact( cm.data(), c );
 
347
}
 
348
 
 
349
void tst_QContactManagerDetails::testDisplayLabel()
 
350
{
 
351
    QFETCH(QString, uri);
 
352
    QScopedPointer<QContactManager> cm(QContactManager::fromUri(uri));
 
353
 
 
354
    QContact originalContact;
 
355
 
 
356
    QContactDisplayLabel dl1;
 
357
    dl1.setLabel("displayLabel1");
 
358
    originalContact.saveDetail(&dl1);
 
359
 
 
360
    saveAndVerifyContact(cm.data(), originalContact);
 
361
 
 
362
    QContact loadedContact;
 
363
    QVERIFY(saveAndLoadContact(cm.data(), originalContact, loadedContact));
 
364
    QCOMPARE(loadedContact.details().count(), originalContact.details().count());
 
365
    QCOMPARE(loadedContact, originalContact);
 
366
}
 
367
 
 
368
void tst_QContactManagerDetails::testNickName()
 
369
{
 
370
    QFETCH(QString, uri);
 
371
    QScopedPointer<QContactManager> cm(QContactManager::fromUri(uri));
 
372
 
 
373
    QContact originalContact;
 
374
 
 
375
    QContactNickname n1;
 
376
    n1.setNickname("nickname1");
 
377
    originalContact.saveDetail(&n1);
 
378
 
 
379
    saveAndVerifyContact(cm.data(), originalContact);
 
380
 
 
381
    QContactNickname n2;
 
382
    n2.setNickname("nickname2");
 
383
    originalContact.saveDetail(&n2);
 
384
 
 
385
    QContact loadedContact;
 
386
    QVERIFY(saveAndLoadContact(cm.data(), originalContact, loadedContact));
 
387
    if (uri == "qtcontacts:jsondb:")
 
388
        QEXPECT_FAIL("", "JsonDb backend does not support QContactNickname properly!", Abort);
 
389
    QCOMPARE(loadedContact.details().count(), originalContact.details().count());
 
390
    QCOMPARE(loadedContact, originalContact);
 
391
}
 
392
 
 
393
void tst_QContactManagerDetails::testOrganisation()
 
394
{
 
395
    QFETCH(QString, uri);
 
396
    QScopedPointer<QContactManager> cm(QContactManager::fromUri(uri));
 
397
 
 
398
    QContact c;
 
399
 
 
400
    QContactOrganization o;
 
401
    o.setName( "Foreign legion" );
 
402
    o.setTitle( "Bicycle mechanic" );
 
403
    c.saveDetail( &o );
 
404
 
 
405
    saveAndVerifyContact( cm.data(), c );
 
406
}
 
407
 
 
408
void tst_QContactManagerDetails::testOnlineAccount()
 
409
{
 
410
    QFETCH(QString, uri);
 
411
    if (uri == "qtcontacts:jsondb:") {
 
412
        QSKIP("JsonDb backend does not support QContactOnlineAccount detail!");
 
413
    } else {
 
414
        QScopedPointer<QContactManager> cm(QContactManager::fromUri(uri));
 
415
        QContact c;
 
416
 
 
417
        QContactOnlineAccount o;
 
418
        o.setAccountUri( "john@example.com" );
 
419
        o.setProtocol(QContactOnlineAccount::ProtocolJabber);
 
420
        c.saveDetail( &o );
 
421
        saveAndVerifyContact( cm.data(), c );
 
422
    }
 
423
}
 
424
 
 
425
void tst_QContactManagerDetails::testPhoneNumber()
 
426
{
 
427
    QFETCH(QString, uri);
 
428
    QScopedPointer<QContactManager> cm(QContactManager::fromUri(uri));
 
429
    QContact c;
 
430
 
 
431
    // General landline number
 
432
    QContactPhoneNumber n2;
 
433
    n2.setNumber( "2" );
 
434
    n2.setSubTypes( QList<int>() << QContactPhoneNumber::SubTypeLandline );
 
435
    c.saveDetail( &n2 );
 
436
 
 
437
    saveAndVerifyContact( cm.data(), c );
 
438
 
 
439
    QContact c2;
 
440
 
 
441
    // home mobile number
 
442
    QContactPhoneNumber n1;
 
443
    n1.setNumber( "1" );
 
444
    n1.setSubTypes( QList<int>() << QContactPhoneNumber::SubTypeMobile );
 
445
    n1.setContexts( QContactDetail::ContextHome );
 
446
    c2.saveDetail( &n1 );
 
447
 
 
448
    // work landline number
 
449
    QContactPhoneNumber n3;
 
450
    n3.setNumber( "3" );
 
451
    n3.setSubTypes( QList<int>() << QContactPhoneNumber::SubTypeLandline );
 
452
    n3.setContexts( QContactDetail::ContextWork );
 
453
    c2.saveDetail( &n3 );
 
454
 
 
455
    saveAndVerifyContact( cm.data(), c2 );
 
456
}
 
457
 
 
458
void tst_QContactManagerDetails::testSyncTarget()
 
459
{
 
460
    QFETCH(QString, uri);
 
461
    QScopedPointer<QContactManager> cm(QContactManager::fromUri(uri));
 
462
    QContact c;
 
463
 
 
464
    QContactSyncTarget syncTarget;
 
465
    syncTarget.setSyncTarget("TestSyncTarget" );
 
466
    c.saveDetail( &syncTarget );
 
467
    saveAndVerifyContact( cm.data(), c );
 
468
}
 
469
 
 
470
void tst_QContactManagerDetails::testUrl()
 
471
{
 
472
    QFETCH(QString, uri);
 
473
    QScopedPointer<QContactManager> cm(QContactManager::fromUri(uri));
 
474
 
 
475
    QContact originalContact;
 
476
 
 
477
    QContactUrl u;
 
478
    u.setUrl("http://failblog.org");
 
479
    u.setSubType(QContactUrl::SubTypeHomePage);
 
480
    originalContact.saveDetail(&u);
 
481
 
 
482
    QContact loadedContact;
 
483
    QVERIFY(saveAndLoadContact(cm.data(), originalContact, loadedContact));
 
484
    QCOMPARE( loadedContact.details().count(), originalContact.details().count() );
 
485
    if (uri == "qtcontacts:jsondb:")
 
486
        QEXPECT_FAIL("", "JsonDb backend does not support QContactUrl detail properly!", Abort);
 
487
    QCOMPARE( loadedContact, originalContact );
 
488
}
 
489
 
 
490
void tst_QContactManagerDetails::testRingtone()
 
491
{
 
492
    QFETCH(QString, uri);
 
493
    QScopedPointer<QContactManager> cm(QContactManager::fromUri(uri));
 
494
 
 
495
    QUrl rtUrl(QUrl::fromLocalFile("/home/user/sample.wav"));
 
496
 
 
497
    QContact c;
 
498
    QContactRingtone rt;
 
499
    rt.setAudioRingtoneUrl(rtUrl);
 
500
    c.saveDetail(&rt);
 
501
 
 
502
    saveAndVerifyContact( cm.data(), c );
 
503
}
 
504
 
 
505
void tst_QContactManagerDetails::testEmptyExtendedDetail()
 
506
{
 
507
    QFETCH(QString, uri);
 
508
    QScopedPointer<QContactManager> cm(QContactManager::fromUri(uri));
 
509
 
 
510
    QContact c;
 
511
 
 
512
    // Adding an empty extended detail
 
513
    QContactExtendedDetail emptyDetail;
 
514
    QVERIFY(c.saveDetail(&emptyDetail));
 
515
    if (uri == "qtcontacts:jsondb:") {
 
516
        QSKIP("This manager does not store empty extended details, so skipping empty extended detail test!");
 
517
    } else {
 
518
        saveAndVerifyContact(cm.data(), c);
 
519
    }
 
520
}
 
521
 
 
522
void tst_QContactManagerDetails::testExtendedDetail()
 
523
{
 
524
    QFETCH(QString, uri);
 
525
    QScopedPointer<QContactManager> cm(QContactManager::fromUri(uri));
 
526
 
 
527
    QContact c;
 
528
 
 
529
    QContactExtendedDetail extendedDetail1, extendedDetail2;
 
530
    extendedDetail1.setName("testExtDetailVariantList");
 
531
    extendedDetail1.setData(QVariantList() << QString("QString in the QVariantlist detail.")
 
532
              << QVariant(1)
 
533
              << QString("Another QString in the QVariantlist detail.")
 
534
              << 2);
 
535
    QVERIFY(c.saveDetail(&extendedDetail1));
 
536
    saveAndVerifyContact(cm.data(), c);
 
537
 
 
538
    // Adding two extended details to same contact
 
539
    QContact c2;
 
540
    extendedDetail2.setName("testExtDetailInt");
 
541
    extendedDetail2.setData(2);
 
542
    QVERIFY (c2.saveDetail(&extendedDetail2));
 
543
    QVERIFY (c2.saveDetail(&extendedDetail1));
 
544
    saveAndVerifyContact(cm.data(), c2);
 
545
 
 
546
    // Adding same details but in different order
 
547
    QContact c3;
 
548
    QVERIFY (c3.saveDetail(&extendedDetail1));
 
549
    QVERIFY (c3.saveDetail(&extendedDetail2));
 
550
    saveAndVerifyContact(cm.data(), c3);
 
551
}
 
552
 
 
553
void tst_QContactManagerDetails::testGuid()
 
554
{
 
555
    QFETCH(QString, uri);
 
556
    QScopedPointer<QContactManager> cm(QContactManager::fromUri(uri));
 
557
    QContact c;
 
558
 
 
559
    QContactGuid guid;
 
560
    guid.setGuid("TestGUID" );
 
561
    c.saveDetail( &guid );
 
562
    saveAndVerifyContact( cm.data(), c );
 
563
}
 
564
 
 
565
QTEST_MAIN(tst_QContactManagerDetails)
 
566
#include "tst_qcontactmanagerdetails.moc"