File: test-sim-plugin.cpp

package info (click to toggle)
contactsd 1.4.15-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,392 kB
  • sloc: cpp: 8,862; ansic: 3,793; sh: 361; xml: 75; python: 22; makefile: 13
file content (617 lines) | stat: -rw-r--r-- 23,522 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/** This file is part of Contacts daemon
 **
 ** Copyright (c) 2013 Jolla Ltd.
 **
 ** Contact: Matt Vogt <matthew.vogt@jollamobile.com>
 **
 ** GNU Lesser General Public License Usage
 ** This file may be used under the terms of the GNU Lesser General Public License
 ** version 2.1 as published by the Free Software Foundation and appearing in the
 ** file LICENSE.LGPL included in the packaging of this file.  Please review the
 ** following information to ensure the GNU Lesser General Public License version
 ** 2.1 requirements will be met:
 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 **/

#include "test-sim-plugin.h"

#include <test-common.h>

#include <QContactCollectionFilter>
#include <QContactNickname>
#include <QContactPhoneNumber>

QTCONTACTS_USE_NAMESPACE

namespace {

const QString DummyModemPath = QStringLiteral("dummy-cardId");

}

// In this test, we bypass the contacts daemon entirely, and test the controller
// class which contains all the logic, without involving the real SIM at all.

TestSimPlugin::TestSimPlugin(QObject *parent) :
    QObject(parent)
{
}

QList<QContact> TestSimPlugin::getAllSimContacts(const QContactManager &m)
{
    QContactCollectionFilter filter;
    filter.setCollectionId(m_collection.id());

    return m.contacts(filter);
}

QContact TestSimPlugin::createTestContact()
{
    QContact rv;
    rv.setCollectionId(m_collection.id());

    return rv;
}

void TestSimPlugin::init()
{
}

void TestSimPlugin::initTestCase()
{
    m_controller = new CDSimController(this, false);
    m_controller->setModemPaths(QStringList() << DummyModemPath);

    m_controller->m_modems.first()->setReady(true);
    m_collection = m_controller->contactCollection(DummyModemPath);
}

void TestSimPlugin::testAdd()
{
    QContactManager &m(m_controller->contactManager());

    QCOMPARE(getAllSimContacts(m).count(), 0);
    QCOMPARE(m_controller->busy(), false);

    // We don't have a modem path configured, so the controller can't query the SIM
    m_controller->m_modems.first()->setReady(true);
    QCOMPARE(m_controller->busy(), false);

    // Normally, a sim-present notification would cause the controller to fetch
    // VCard data from the SIM. Instead, we bypass the first part here, and supply
    // the VCard data directly
    m_controller->m_modems.first()->vcardDataAvailable(QStringLiteral(
"BEGIN:VCARD\n"
"VERSION:3.0\n"
"FN:Forrest Gump\n"
"TEL;TYPE=HOME,VOICE:(404) 555-1212\n"
"END:VCARD\n"));
    QCOMPARE(m_controller->busy(), true);

    QTRY_VERIFY(m_controller->busy() == false);

    QList<QContact> simContacts(getAllSimContacts(m));
    QCOMPARE(simContacts.count(), 1);
    QCOMPARE(simContacts.at(0).detail<QContactNickname>().nickname(), QStringLiteral("Forrest Gump"));
    QCOMPARE(simContacts.at(0).detail<QContactPhoneNumber>().number(), QStringLiteral("(404) 555-1212"));
    QVERIFY(simContacts.at(0).detail<QContactPhoneNumber>().contexts().contains(QContactDetail::ContextHome));
    QVERIFY(simContacts.at(0).detail<QContactPhoneNumber>().subTypes().contains(QContactPhoneNumber::SubTypeVoice));
}

void TestSimPlugin::testAppend()
{
    QContactManager &m(m_controller->contactManager());

    // Add the Forrest Gump contact manually
    QContact forrest(createTestContact());

    QContactNickname n;
    n.setNickname("Forrest Gump");
    forrest.saveDetail(&n);

    QVERIFY(m.saveContact(&forrest));

    QCOMPARE(getAllSimContacts(m).count(), 1);
    QCOMPARE(m_controller->busy(), false);

    // Process a VCard set containing Forrest Gump and another contact
    m_controller->m_modems.first()->setReady(true);
    m_controller->m_modems.first()->vcardDataAvailable(QStringLiteral(
"BEGIN:VCARD\n"
"VERSION:3.0\n"
"FN:Forrest Gump\n"
"TEL;TYPE=HOME,VOICE:(404) 555-1212\n"
"END:VCARD\n"
"BEGIN:VCARD\n"
"VERSION:3.0\n"
"FN:Forrest Whittaker\n"
"TEL;TYPE=HOME,VOICE:(404) 555-1234\n"
"END:VCARD\n"));
    QTRY_VERIFY(m_controller->busy() == false);

    // Forrest Whittaker should be added
    QList<QContact> simContacts(getAllSimContacts(m));
    QCOMPARE(simContacts.count(), 2);
    QCOMPARE(simContacts.at(0).detail<QContactNickname>().nickname(), QStringLiteral("Forrest Gump"));
    QCOMPARE(simContacts.at(1).detail<QContactNickname>().nickname(), QStringLiteral("Forrest Whittaker"));
}

void TestSimPlugin::testRemove()
{
    QContactManager &m(m_controller->contactManager());

    // Add two contacts manually
    QContact gump(createTestContact());

    QContactNickname n;
    n.setNickname("Forrest Gump");
    gump.saveDetail(&n);

    QContact whittaker(createTestContact());

    n.setNickname("Forrest Whittaker");
    whittaker.saveDetail(&n);

    QVERIFY(m.saveContact(&gump));
    QVERIFY(m.saveContact(&whittaker));

    QCOMPARE(getAllSimContacts(m).count(), 2);
    QCOMPARE(m_controller->busy(), false);

    // Process a VCard set containing only Forrest Whittaker
    m_controller->m_modems.first()->setReady(true);
    m_controller->m_modems.first()->vcardDataAvailable(QStringLiteral(
"BEGIN:VCARD\n"
"VERSION:3.0\n"
"FN:Forrest Whittaker\n"
"TEL;TYPE=HOME,VOICE:(404) 555-1234\n"
"END:VCARD\n"));
    QTRY_VERIFY(m_controller->busy() == false);

    // Forrest Gump should be removed
    QList<QContact> simContacts(getAllSimContacts(m));
    QCOMPARE(simContacts.count(), 1);
    QCOMPARE(simContacts.at(0).detail<QContactNickname>().nickname(), QStringLiteral("Forrest Whittaker"));
}

void TestSimPlugin::testAddAndRemove()
{
    QContactManager &m(m_controller->contactManager());

    // Add the Forrest Gump contact manually
    QContact forrest(createTestContact());

    QContactNickname n;
    n.setNickname("Forrest Gump");
    forrest.saveDetail(&n);

    QVERIFY(m.saveContact(&forrest));

    QCOMPARE(getAllSimContacts(m).count(), 1);
    QCOMPARE(m_controller->busy(), false);

    // Process a VCard set not containing Forrest Gump but another contact
    m_controller->m_modems.first()->setReady(true);
    m_controller->m_modems.first()->vcardDataAvailable(QStringLiteral(
"BEGIN:VCARD\n"
"VERSION:3.0\n"
"FN:Forrest Whittaker\n"
"TEL;TYPE=HOME,VOICE:(404) 555-1234\n"
"END:VCARD\n"));
    QTRY_VERIFY(m_controller->busy() == false);

    // Forrest Gump should be removed, Forrest Whittaker added
    QList<QContact> simContacts(getAllSimContacts(m));
    QCOMPARE(simContacts.count(), 1);
    QCOMPARE(simContacts.at(0).detail<QContactNickname>().nickname(), QStringLiteral("Forrest Whittaker"));
}

void TestSimPlugin::testChangedNumber()
{
    QContactManager &m(m_controller->contactManager());

    QCOMPARE(getAllSimContacts(m).count(), 0);
    QCOMPARE(m_controller->busy(), false);

    m_controller->m_modems.first()->setReady(true);
    QCOMPARE(m_controller->busy(), false);

    m_controller->m_modems.first()->vcardDataAvailable(QStringLiteral(
"BEGIN:VCARD\n"
"VERSION:3.0\n"
"FN:Forrest Gump\n"
"TEL;TYPE=HOME,VOICE:(404) 555-1212\n"
"END:VCARD\n"));
    QCOMPARE(m_controller->busy(), true);
    QTRY_VERIFY(m_controller->busy() == false);

    QList<QContact> simContacts(getAllSimContacts(m));
    QCOMPARE(simContacts.count(), 1);
    QCOMPARE(simContacts.at(0).detail<QContactNickname>().nickname(), QStringLiteral("Forrest Gump"));
    QCOMPARE(simContacts.at(0).detail<QContactPhoneNumber>().number(), QStringLiteral("(404) 555-1212"));
    QVERIFY(simContacts.at(0).detail<QContactPhoneNumber>().contexts().contains(QContactDetail::ContextHome));
    QVERIFY(simContacts.at(0).detail<QContactPhoneNumber>().subTypes().contains(QContactPhoneNumber::SubTypeVoice));

    QContactId existingId = simContacts.at(0).id();

    // Change the number and verify that it is updated in the database, but the contact was not recreated
    m_controller->m_modems.first()->vcardDataAvailable(QStringLiteral(
"BEGIN:VCARD\n"
"VERSION:3.0\n"
"FN:Forrest Gump\n"
"TEL;TYPE=WORK,VIDEO:(404) 555-6789\n"
"END:VCARD\n"));
    QCOMPARE(m_controller->busy(), true);
    QTRY_VERIFY(m_controller->busy() == false);

    simContacts = getAllSimContacts(m);
    QCOMPARE(simContacts.count(), 1);
    QCOMPARE(simContacts.at(0).id(), existingId);
    QCOMPARE(simContacts.at(0).detail<QContactNickname>().nickname(), QStringLiteral("Forrest Gump"));
    QCOMPARE(simContacts.at(0).detail<QContactPhoneNumber>().number(), QStringLiteral("(404) 555-6789"));
    QVERIFY(simContacts.at(0).detail<QContactPhoneNumber>().contexts().contains(QContactDetail::ContextWork));
    QVERIFY(simContacts.at(0).detail<QContactPhoneNumber>().subTypes().contains(QContactPhoneNumber::SubTypeVideo));

    // Change the context and verify that it is updated in the database
    m_controller->m_modems.first()->vcardDataAvailable(QStringLiteral(
"BEGIN:VCARD\n"
"VERSION:3.0\n"
"FN:Forrest Gump\n"
"TEL;TYPE=HOME,VIDEO:(404) 555-6789\n"
"END:VCARD\n"));
    QCOMPARE(m_controller->busy(), true);
    QTRY_VERIFY(m_controller->busy() == false);

    simContacts = getAllSimContacts(m);
    QCOMPARE(simContacts.count(), 1);
    QCOMPARE(simContacts.at(0).id(), existingId);
    QCOMPARE(simContacts.at(0).detail<QContactNickname>().nickname(), QStringLiteral("Forrest Gump"));
    QCOMPARE(simContacts.at(0).detail<QContactPhoneNumber>().number(), QStringLiteral("(404) 555-6789"));
    QVERIFY(simContacts.at(0).detail<QContactPhoneNumber>().contexts().contains(QContactDetail::ContextHome));
    QVERIFY(simContacts.at(0).detail<QContactPhoneNumber>().subTypes().contains(QContactPhoneNumber::SubTypeVideo));

    // Change the subtype and verify that it is updated in the database
    m_controller->m_modems.first()->vcardDataAvailable(QStringLiteral(
"BEGIN:VCARD\n"
"VERSION:3.0\n"
"FN:Forrest Gump\n"
"TEL;TYPE=HOME,VOICE,CELL:(404) 555-6789\n"
"END:VCARD\n"));
    QCOMPARE(m_controller->busy(), true);
    QTRY_VERIFY(m_controller->busy() == false);

    simContacts = getAllSimContacts(m);
    QCOMPARE(simContacts.count(), 1);
    QCOMPARE(simContacts.at(0).id(), existingId);
    QCOMPARE(simContacts.at(0).detail<QContactNickname>().nickname(), QStringLiteral("Forrest Gump"));
    QCOMPARE(simContacts.at(0).detail<QContactPhoneNumber>().number(), QStringLiteral("(404) 555-6789"));
    QVERIFY(simContacts.at(0).detail<QContactPhoneNumber>().contexts().contains(QContactDetail::ContextHome));
    QVERIFY(simContacts.at(0).detail<QContactPhoneNumber>().subTypes().contains(QContactPhoneNumber::SubTypeVoice));
    QVERIFY(simContacts.at(0).detail<QContactPhoneNumber>().subTypes().contains(QContactPhoneNumber::SubTypeMobile));
}

void TestSimPlugin::testMultipleNumbers()
{
    QContactManager &m(m_controller->contactManager());

    QCOMPARE(getAllSimContacts(m).count(), 0);
    QCOMPARE(m_controller->busy(), false);

    m_controller->m_modems.first()->setReady(true);
    QCOMPARE(m_controller->busy(), false);

    // Add a contact with two numbers
    m_controller->m_modems.first()->vcardDataAvailable(QStringLiteral(
"BEGIN:VCARD\n"
"VERSION:3.0\n"
"FN:Forrest Gump\n"
"TEL;TYPE=HOME,VOICE:(404) 555-1212\n"
"TEL;TYPE=WORK,VIDEO:(404) 555-6789\n"
"END:VCARD\n"));
    QCOMPARE(m_controller->busy(), true);
    QTRY_VERIFY(m_controller->busy() == false);

    QList<QContact> simContacts(getAllSimContacts(m));
    QCOMPARE(simContacts.count(), 1);
    QCOMPARE(simContacts.at(0).detail<QContactNickname>().nickname(), QStringLiteral("Forrest Gump"));
    QCOMPARE(simContacts.at(0).details<QContactPhoneNumber>().count(), 2);
    foreach (const QContactPhoneNumber &number, simContacts.at(0).details<QContactPhoneNumber>()) {
        if (number.number() == QStringLiteral("(404) 555-1212")) {
            QVERIFY(number.contexts().contains(QContactDetail::ContextHome));
            QVERIFY(number.subTypes().contains(QContactPhoneNumber::SubTypeVoice));
        } else {
            QCOMPARE(number.number(), QStringLiteral("(404) 555-6789"));
            QVERIFY(number.contexts().contains(QContactDetail::ContextWork));
            QVERIFY(number.subTypes().contains(QContactPhoneNumber::SubTypeVideo));
        }
    }

    QContactId existingId = simContacts.at(0).id();

    // Change the set of numbers
    m_controller->m_modems.first()->vcardDataAvailable(QStringLiteral(
"BEGIN:VCARD\n"
"VERSION:3.0\n"
"FN:Forrest Gump\n"
"TEL;TYPE=WORK,VIDEO:(404) 555-6789\n"
"TEL;TYPE=HOME,CELL:(404) 555-5555\n"
"TEL;TYPE=WORK,VOICE:(404) 555-4321\n"
"END:VCARD\n"));
    QCOMPARE(m_controller->busy(), true);
    QTRY_VERIFY(m_controller->busy() == false);

    // Verify that numbers were added and removed, but the contact was not recreated
    simContacts = getAllSimContacts(m);
    QCOMPARE(simContacts.count(), 1);
    QCOMPARE(simContacts.at(0).id(), existingId);
    QCOMPARE(simContacts.at(0).detail<QContactNickname>().nickname(), QStringLiteral("Forrest Gump"));
    QCOMPARE(simContacts.at(0).details<QContactPhoneNumber>().count(), 3);
    foreach (const QContactPhoneNumber &number, simContacts.at(0).details<QContactPhoneNumber>()) {
        if (number.number() == QStringLiteral("(404) 555-6789")) {
            QVERIFY(number.contexts().contains(QContactDetail::ContextWork));
            QVERIFY(number.subTypes().contains(QContactPhoneNumber::SubTypeVideo));
        } else if (number.number() == QStringLiteral("(404) 555-5555")) {
            QVERIFY(number.contexts().contains(QContactDetail::ContextHome));
            QVERIFY(number.subTypes().contains(QContactPhoneNumber::SubTypeMobile));
        } else {
            QCOMPARE(number.number(), QStringLiteral("(404) 555-4321"));
            QVERIFY(number.contexts().contains(QContactDetail::ContextWork));
            QVERIFY(number.subTypes().contains(QContactPhoneNumber::SubTypeVoice));
        }
    }
}

void TestSimPlugin::testMultipleIdenticalNumbers()
{
    QContactManager &m(m_controller->contactManager());

    QCOMPARE(getAllSimContacts(m).count(), 0);
    QCOMPARE(m_controller->busy(), false);

    m_controller->m_modems.first()->setReady(true);
    QCOMPARE(m_controller->busy(), false);

    // Add a contact with two very similar numbers
    m_controller->m_modems.first()->vcardDataAvailable(QStringLiteral(
"BEGIN:VCARD\n"
"VERSION:3.0\n"
"FN:Forrest Gump\n"
"TEL;TYPE=HOME,VOICE:(404) 555-1212\n"
"TEL;TYPE=WORK,VIDEO:(404) 555-1212\n"
"END:VCARD\n"));
    QCOMPARE(m_controller->busy(), true);
    QTRY_VERIFY(m_controller->busy() == false);

    QList<QContact> simContacts(getAllSimContacts(m));
    QCOMPARE(simContacts.count(), 1);
    QCOMPARE(simContacts.at(0).detail<QContactNickname>().nickname(), QStringLiteral("Forrest Gump"));
    QCOMPARE(simContacts.at(0).details<QContactPhoneNumber>().count(), 2);
    foreach (const QContactPhoneNumber &number, simContacts.at(0).details<QContactPhoneNumber>()) {
        if (number.contexts().contains(QContactDetail::ContextHome)) {
            QVERIFY(number.number() == QStringLiteral("(404) 555-1212"));
            QVERIFY(number.subTypes().contains(QContactPhoneNumber::SubTypeVoice));
        } else {
            QCOMPARE(number.number(), QStringLiteral("(404) 555-1212"));
            QVERIFY(number.contexts().contains(QContactDetail::ContextWork));
            QVERIFY(number.subTypes().contains(QContactPhoneNumber::SubTypeVideo));
        }
    }

    QContactId existingId = simContacts.at(0).id();

    // Modify contact so that the number is now identical
    m_controller->m_modems.first()->vcardDataAvailable(QStringLiteral(
"BEGIN:VCARD\n"
"VERSION:3.0\n"
"FN:Forrest Gump\n"
"TEL;TYPE=HOME,VOICE:(404) 555-1212\n"
"TEL;TYPE=HOME,VOICE:(404) 555-1212\n"
"END:VCARD\n"));
    QCOMPARE(m_controller->busy(), true);
    QTRY_VERIFY(m_controller->busy() == false);

    // Verify that the contact was not recreated, but instead updated, and that the
    // duplicate phone number detail has not been duplicated to the stored contact.
    simContacts = getAllSimContacts(m);
    QCOMPARE(simContacts.count(), 1);
    QCOMPARE(simContacts.at(0).id(), existingId);
    QCOMPARE(simContacts.at(0).detail<QContactNickname>().nickname(), QStringLiteral("Forrest Gump"));
    QCOMPARE(simContacts.at(0).details<QContactPhoneNumber>().count(), 1);
    QVERIFY(simContacts.at(0).detail<QContactPhoneNumber>().number() == QStringLiteral("(404) 555-1212"));
    QVERIFY(simContacts.at(0).detail<QContactPhoneNumber>().contexts().contains(QContactDetail::ContextHome));
    QVERIFY(simContacts.at(0).detail<QContactPhoneNumber>().subTypes().contains(QContactPhoneNumber::SubTypeVoice));
}

void TestSimPlugin::testTrimWhitespace()
{
    QContactManager &m(m_controller->contactManager());

    QCOMPARE(getAllSimContacts(m).count(), 0);
    QCOMPARE(m_controller->busy(), false);

    m_controller->m_modems.first()->setReady(true);
    QCOMPARE(m_controller->busy(), false);

    // Add a contact with two numbers
    m_controller->m_modems.first()->vcardDataAvailable(QStringLiteral(
"BEGIN:VCARD\n"
"VERSION:3.0\n"
"FN:Forrest Gump\n"
"TEL;TYPE=HOME,VOICE:(404) 555-1212\n"
"TEL;TYPE=WORK,VIDEO:(404) 555-6789\n"
"END:VCARD\n"));
    QCOMPARE(m_controller->busy(), true);
    QTRY_VERIFY(m_controller->busy() == false);

    QList<QContact> simContacts(getAllSimContacts(m));
    QCOMPARE(simContacts.count(), 1);
    QCOMPARE(simContacts.at(0).detail<QContactNickname>().nickname(), QStringLiteral("Forrest Gump"));
    QCOMPARE(simContacts.at(0).details<QContactPhoneNumber>().count(), 2);
    foreach (const QContactPhoneNumber &number, simContacts.at(0).details<QContactPhoneNumber>()) {
        if (number.number() == QStringLiteral("(404) 555-1212")) {
            QVERIFY(number.contexts().contains(QContactDetail::ContextHome));
            QVERIFY(number.subTypes().contains(QContactPhoneNumber::SubTypeVoice));
        } else {
            QCOMPARE(number.number(), QStringLiteral("(404) 555-6789"));
            QVERIFY(number.contexts().contains(QContactDetail::ContextWork));
            QVERIFY(number.subTypes().contains(QContactPhoneNumber::SubTypeVideo));
        }
    }

    QContactId existingId = simContacts.at(0).id();

    // Add another contact with identical + whitespace name, changed number
    m_controller->m_modems.first()->vcardDataAvailable(QStringLiteral(
"BEGIN:VCARD\n"
"VERSION:3.0\n"
"FN:Forrest Gump \n"
"TEL;TYPE=HOME,VOICE:(404) 555-1212\n"
"TEL;TYPE=WORK,VIDEO:(404) 555-6888\n"
"END:VCARD\n"));
    QCOMPARE(m_controller->busy(), true);
    QTRY_VERIFY(m_controller->busy() == false);

    // Verify that the contact was not recreated, but instead updated
    simContacts = getAllSimContacts(m);
    QCOMPARE(simContacts.count(), 1);
    QCOMPARE(simContacts.at(0).id(), existingId);
    QCOMPARE(simContacts.at(0).detail<QContactNickname>().nickname(), QStringLiteral("Forrest Gump"));
    QCOMPARE(simContacts.at(0).details<QContactPhoneNumber>().count(), 2);
    foreach (const QContactPhoneNumber &number, simContacts.at(0).details<QContactPhoneNumber>()) {
        if (number.number() == QStringLiteral("(404) 555-1212")) {
            QVERIFY(number.contexts().contains(QContactDetail::ContextHome));
            QVERIFY(number.subTypes().contains(QContactPhoneNumber::SubTypeVoice));
        } else {
            QCOMPARE(number.number(), QStringLiteral("(404) 555-6888"));
            QVERIFY(number.contexts().contains(QContactDetail::ContextWork));
            QVERIFY(number.subTypes().contains(QContactPhoneNumber::SubTypeVideo));
        }
    }
}

void TestSimPlugin::testCoalescing()
{
    QContactManager &m(m_controller->contactManager());

    QCOMPARE(getAllSimContacts(m).count(), 0);
    QCOMPARE(m_controller->busy(), false);

    m_controller->m_modems.first()->setReady(true);
    QCOMPARE(m_controller->busy(), false);

    // Add the same contact a bunch of times
    m_controller->m_modems.first()->vcardDataAvailable(QStringLiteral(
"BEGIN:VCARD\n"
"VERSION:3.0\n"
"FN:Forrest Gump\n"
"TEL;TYPE=HOME,VOICE:(404) 555-1212\n"
"TEL;TYPE=WORK,VIDEO:(404) 555-6789\n"
"END:VCARD\n"
"BEGIN:VCARD\n"
"VERSION:3.0\n"
"FN:Forrest Gump \n" /* whitespace shouldn't affect coalescing */
"TEL;TYPE=HOME,VOICE:(404) 555-1212\n"
"TEL;TYPE=WORK,VIDEO:(404) 555-6789\n"
"TEL;TYPE=WORK,VOICE:(404) 555-6888\n"
"END:VCARD\n"
"FN:Forrest Gump\n"
"TEL;TYPE=HOME,VOICE:(404) 555-1212\n"
"END:VCARD\n"));
    QCOMPARE(m_controller->busy(), true);
    QTRY_VERIFY(m_controller->busy() == false);

    QList<QContact> simContacts(getAllSimContacts(m));
    QCOMPARE(simContacts.count(), 1); // should all be coalesced into one.
    QCOMPARE(simContacts.at(0).detail<QContactNickname>().nickname(), QStringLiteral("Forrest Gump"));
    QCOMPARE(simContacts.at(0).details<QContactPhoneNumber>().count(), 3);
    foreach (const QContactPhoneNumber &number, simContacts.at(0).details<QContactPhoneNumber>()) {
        if (number.number() == QStringLiteral("(404) 555-1212")) {
            QVERIFY(number.contexts().contains(QContactDetail::ContextHome));
            QVERIFY(number.subTypes().contains(QContactPhoneNumber::SubTypeVoice));
        } else if (number.number() == QStringLiteral("(404) 555-6888")) {
            QVERIFY(number.contexts().contains(QContactDetail::ContextWork));
            QVERIFY(number.subTypes().contains(QContactPhoneNumber::SubTypeVoice));
        } else {
            QCOMPARE(number.number(), QStringLiteral("(404) 555-6789"));
            QVERIFY(number.contexts().contains(QContactDetail::ContextWork));
            QVERIFY(number.subTypes().contains(QContactPhoneNumber::SubTypeVideo));
        }
    }
}

void TestSimPlugin::testEmpty()
{
    QContactManager &m(m_controller->contactManager());

    // Add the Forrest Gump contact manually
    QContact forrest(createTestContact());

    QContactNickname n;
    n.setNickname("Forrest Gump");
    forrest.saveDetail(&n);

    QVERIFY(m.saveContact(&forrest));

    QCOMPARE(getAllSimContacts(m).count(), 1);
    QCOMPARE(m_controller->busy(), false);

    // Process an empty VCard
    m_controller->m_modems.first()->setReady(true);
    m_controller->m_modems.first()->vcardDataAvailable(QString());
    QCOMPARE(m_controller->busy(), true);

    QTRY_VERIFY(m_controller->busy() == false);

    // No-longer-present contact should be removed
    QList<QContact> simContacts(getAllSimContacts(m));
    QCOMPARE(simContacts.count(), 0);
}

void TestSimPlugin::testClear()
{
    QContactManager &m(m_controller->contactManager());

    // Add two contacts manually
    QContact gump(createTestContact());

    QContactNickname n;
    n.setNickname("Forrest Gump");
    gump.saveDetail(&n);

    QContact whittaker(createTestContact());
    n.setNickname("Forrest Whittaker");
    whittaker.saveDetail(&n);

    QVERIFY(m.saveContact(&gump));
    QVERIFY(m.saveContact(&whittaker));

    QCOMPARE(getAllSimContacts(m).count(), 2);
    QCOMPARE(m_controller->busy(), false);

    // Report the SIM card removed
    m_controller->m_modems.first()->phonebookValidChanged(false);
    QTRY_VERIFY(m_controller->busy() == false);

    // All sim contacts should be removed
    QList<QContact> simContacts(getAllSimContacts(m));
    QCOMPARE(simContacts.count(), 0);
}

void TestSimPlugin::cleanupTestCase()
{
    if (CDSimModemData::removeCollections(&m_controller->contactManager(),
                                          QList<QContactCollectionId>() << m_collection.id())) {
        qDebug() << "Remove test collection";
    }
}

void TestSimPlugin::cleanup()
{
    QContactManager &m(m_controller->contactManager());

    foreach (const QContact &contact, getAllSimContacts(m)) {
        QVERIFY(m.removeContact(contact.id()));
    }
}

CONTACTSD_TEST_MAIN(TestSimPlugin)