/
tst_seasideperson.cpp
1175 lines (947 loc) · 45.2 KB
/
tst_seasideperson.cpp
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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2012 - 2020 Jolla Ltd.
*
* You may use this file under the terms of the BSD license as follows:
*
* "Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Nemo Mobile nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
*/
#include <QObject>
#include <QtTest>
#include <QContactAvatar>
#include <QContactBirthday>
#include <QContactAnniversary>
#include <QContactName>
#include <QContactNickname>
#include <QContactFavorite>
#include <QContactPhoneNumber>
#include <QContactEmailAddress>
#include <QContactAddress>
#include <QContactOnlineAccount>
#include <QContactOrganization>
#include <QContactUrl>
#include <QContactManager>
#include <cacheconfiguration.h>
#include <qtcontacts-extensions_impl.h>
#include "seasideperson.h"
#include <algorithm>
QTCONTACTS_USE_NAMESPACE
class tst_SeasidePerson : public QObject
{
Q_OBJECT
public:
// Ensure that the cache has been initialized
tst_SeasidePerson() : m_displayLabelGroups(SeasideCache::allDisplayLabelGroups()) {}
private slots:
void firstName();
void lastName();
void middleName();
void displayLabel();
void sectionBucket();
void companyName();
void title();
void favorite();
void avatarPath();
void nicknameDetails();
void phoneDetails();
void emailDetails();
void websiteDetails();
void accountDetails();
void birthday();
void anniversaryDetails();
void addressDetails();
void globalPresenceState();
void complete();
void marshalling();
void setContact();
void vcard();
void syncTarget();
void constituents();
void removeDuplicatePhoneNumbers();
void removeDuplicateOnlineAccounts();
void removeDuplicateEmailAddresses();
private:
QStringList m_displayLabelGroups;
};
void tst_SeasidePerson::firstName()
{
QScopedPointer<SeasidePerson> person(new SeasidePerson);
QCOMPARE(person->firstName(), QString());
QSignalSpy spy(person.data(), SIGNAL(firstNameChanged()));
person->setFirstName("Test");
QCOMPARE(spy.count(), 1);
QCOMPARE(person->firstName(), QString::fromLatin1("Test"));
QCOMPARE(person->property("firstName").toString(), person->firstName());
}
void tst_SeasidePerson::lastName()
{
QScopedPointer<SeasidePerson> person(new SeasidePerson);
QCOMPARE(person->lastName(), QString());
QSignalSpy spy(person.data(), SIGNAL(lastNameChanged()));
person->setLastName("Test");
QCOMPARE(spy.count(), 1);
QCOMPARE(person->lastName(), QString::fromLatin1("Test"));
QCOMPARE(person->property("lastName").toString(), person->lastName());
}
void tst_SeasidePerson::middleName()
{
QScopedPointer<SeasidePerson> person(new SeasidePerson);
QCOMPARE(person->middleName(), QString());
QSignalSpy spy(person.data(), SIGNAL(middleNameChanged()));
person->setMiddleName("Test");
QCOMPARE(spy.count(), 1);
QCOMPARE(person->middleName(), QString::fromLatin1("Test"));
QCOMPARE(person->property("middleName").toString(), person->middleName());
}
void tst_SeasidePerson::displayLabel()
{
CacheConfiguration::DisplayLabelOrder displayLabelOrder = CacheConfiguration().displayLabelOrder();
QScopedPointer<SeasidePerson> person(new SeasidePerson);
QSignalSpy spy(person.data(), SIGNAL(displayLabelChanged()));
person->setFirstName("Test");
person->setLastName("Last");
QCOMPARE(spy.count(), 2); // TODO: it would be nicer if this would only emit once per event loop
QCOMPARE(person->firstName(), QString::fromLatin1("Test"));
QCOMPARE(person->lastName(), QString::fromLatin1("Last"));
if (displayLabelOrder == CacheConfiguration::FirstNameFirst) {
QCOMPARE(person->displayLabel(), QString::fromLatin1("Test Last"));
} else {
QCOMPARE(person->displayLabel(), QString::fromLatin1("Last Test"));
}
// TODO: test additional cases for displayLabel:
// - email/IM id
// - company
// - other fallbacks?
// - "(unnamed)"
}
void tst_SeasidePerson::sectionBucket()
{
QScopedPointer<SeasidePerson> person(new SeasidePerson);
QSignalSpy spy(person.data(), SIGNAL(displayLabelChanged()));
QCOMPARE(person->displayLabel(), SeasidePerson::placeholderDisplayLabel());
QCOMPARE(spy.count(), 0);
// ignore the placeholder id if necessary. unit tests don't load translation catalogues.
if (person->displayLabel() != QStringLiteral("nemo_contacts-la-placeholder_display_label")) {
QCOMPARE(person->sectionBucket(), QString());
}
// set first
person->setLastName("Test");
QCOMPARE(person->displayLabel(), QString::fromLatin1("Test"));
QCOMPARE(person->sectionBucket(), QString::fromLatin1("T"));
QCOMPARE(spy.count(), 1);
// change first
person->setFirstName("Another");
const bool firstNameFirst(SeasideCache::displayLabelOrder() == SeasideCache::FirstNameFirst);
if (firstNameFirst) {
QCOMPARE(person->displayLabel(), QString::fromLatin1("Another Test"));
QCOMPARE(person->sectionBucket(), QString::fromLatin1("A"));
} else {
QCOMPARE(person->displayLabel(), QString::fromLatin1("Test Another"));
QCOMPARE(person->sectionBucket(), QString::fromLatin1("T"));
}
QCOMPARE(spy.count(), 2);
}
void tst_SeasidePerson::companyName()
{
QScopedPointer<SeasidePerson> person(new SeasidePerson);
QCOMPARE(person->companyName(), QString());
QSignalSpy spy(person.data(), SIGNAL(companyNameChanged()));
person->setCompanyName("Test");
QCOMPARE(spy.count(), 1);
QCOMPARE(person->companyName(), QString::fromLatin1("Test"));
QCOMPARE(person->property("companyName").toString(), person->companyName());
}
void tst_SeasidePerson::title()
{
QScopedPointer<SeasidePerson> person(new SeasidePerson);
QCOMPARE(person->title(), QString());
QSignalSpy spy(person.data(), SIGNAL(titleChanged()));
person->setTitle("Test");
QCOMPARE(spy.count(), 1);
QCOMPARE(person->title(), QString::fromLatin1("Test"));
QCOMPARE(person->property("title").toString(), person->title());
}
void tst_SeasidePerson::favorite()
{
QScopedPointer<SeasidePerson> person(new SeasidePerson);
QVERIFY(!person->favorite());
QSignalSpy spy(person.data(), SIGNAL(favoriteChanged()));
person->setFavorite(true);
QCOMPARE(spy.count(), 1);
QCOMPARE(person->property("favorite").toBool(), person->favorite());
QVERIFY(person->favorite());
}
void tst_SeasidePerson::avatarPath()
{
QScopedPointer<SeasidePerson> person(new SeasidePerson);
QCOMPARE(person->avatarPath(), QUrl("image://theme/icon-m-telephony-contact-avatar")); // TODO: should this not really belong in QML level instead of API?
QSignalSpy spy(person.data(), SIGNAL(avatarPathChanged()));
person->setAvatarPath(QUrl("http://test.com"));
QCOMPARE(spy.count(), 1);
QCOMPARE(person->avatarPath(), QUrl("http://test.com"));
QCOMPARE(person->property("avatarPath").toUrl(), person->avatarPath());
}
QVariantMap makeNickname(const QString &nick, int label = SeasidePerson::NoLabel)
{
QVariantMap rv;
rv.insert(QString::fromLatin1("nickname"), nick);
rv.insert(QString::fromLatin1("type"), static_cast<int>(SeasidePerson::NicknameType));
rv.insert(QString::fromLatin1("label"), label);
rv.insert(QString::fromLatin1("index"), -1);
return rv;
}
void tst_SeasidePerson::nicknameDetails()
{
QScopedPointer<SeasidePerson> person(new SeasidePerson);
QCOMPARE(person->nicknameDetails(), QVariantList());
QStringList nicks(QStringList() << "Ace" << "Blocker" << "Chopper");
QSignalSpy spy(person.data(), SIGNAL(nicknameDetailsChanged()));
QVariantList nicknameDetails;
for (int i = 0; i < 2; ++i) {
nicknameDetails.append(makeNickname(nicks.at(i)));
}
person->setNicknameDetails(nicknameDetails);
QCOMPARE(spy.count(), 1);
QList<SeasidePerson::DetailLabel> nicknameLabels;
nicknameLabels.append(SeasidePerson::HomeLabel);
nicknameLabels.append(SeasidePerson::WorkLabel);
nicknameLabels.append(SeasidePerson::OtherLabel);
QVariantList nicknames = person->nicknameDetails();
QCOMPARE(nicknames.count(), 2);
for (int i=0; i<nicknames.count(); i++) {
QVariant &var(nicknames[i]);
QVariantMap nickname(var.value<QVariantMap>());
QCOMPARE(nickname.value(QString::fromLatin1("nickname")).toString(), nicks.at(i));
QCOMPARE(nickname.value(QString::fromLatin1("type")).toInt(), static_cast<int>(SeasidePerson::NicknameType));
QCOMPARE(nickname.value(QString::fromLatin1("label")), QVariant());
// Modify the label of this detail
nickname.insert(QString::fromLatin1("label"), static_cast<int>(nicknameLabels.at(i)));
var = nickname;
}
// Add another to the list
nicknames.append(makeNickname(nicks.at(2), nicknameLabels.at(2)));
person->setNicknameDetails(nicknames);
QCOMPARE(spy.count(), 2);
nicknames = person->nicknameDetails();
QCOMPARE(nicknames.count(), 3);
for (int i=0; i<nicknames.count(); i++) {
QVariant &var(nicknames[i]);
QVariantMap nickname(var.value<QVariantMap>());
QCOMPARE(nickname.value(QString::fromLatin1("nickname")).toString(), nicks.at(i));
QCOMPARE(nickname.value(QString::fromLatin1("type")).toInt(), static_cast<int>(SeasidePerson::NicknameType));
QCOMPARE(nickname.value(QString::fromLatin1("label")).toInt(), static_cast<int>(nicknameLabels.at(i)));
}
// Remove all but the middle
person->setNicknameDetails(nicknames.mid(1, 1));
QCOMPARE(spy.count(), 3);
nicknames = person->nicknameDetails();
QCOMPARE(nicknames.count(), 1);
{
QVariant &var(nicknames[0]);
QVariantMap nickname(var.value<QVariantMap>());
QCOMPARE(nickname.value(QString::fromLatin1("nickname")).toString(), nicks.at(1));
QCOMPARE(nickname.value(QString::fromLatin1("type")).toInt(), static_cast<int>(SeasidePerson::NicknameType));
QCOMPARE(nickname.value(QString::fromLatin1("label")).toInt(), static_cast<int>(nicknameLabels.at(1)));
}
}
QVariantMap makePhoneNumber(const QString &number, int label = SeasidePerson::NoLabel, int subType = SeasidePerson::NoSubType)
{
QVariantMap rv;
rv.insert(QString::fromLatin1("number"), number);
rv.insert(QString::fromLatin1("type"), static_cast<int>(SeasidePerson::PhoneNumberType));
rv.insert(QString::fromLatin1("subTypes"), QVariantList() << subType);
rv.insert(QString::fromLatin1("label"), label);
rv.insert(QString::fromLatin1("index"), -1);
return rv;
}
void tst_SeasidePerson::phoneDetails()
{
QScopedPointer<SeasidePerson> person(new SeasidePerson);
QCOMPARE(person->phoneDetails(), QVariantList());
QStringList phoneNumbers(QStringList() << "111" << "222"<< "333"<< "444"<< "555");
QSignalSpy spy(person.data(), SIGNAL(phoneDetailsChanged()));
QVariantList phoneDetails;
for (int i = 0; i < 4; ++i) {
phoneDetails.append(makePhoneNumber(phoneNumbers.at(i)));
}
person->setPhoneDetails(phoneDetails);
QCOMPARE(spy.count(), 1);
QList<SeasidePerson::DetailSubType> phoneSubTypes;
phoneSubTypes.append(SeasidePerson::PhoneSubTypeLandline);
phoneSubTypes.append(SeasidePerson::PhoneSubTypeMobile);
phoneSubTypes.append(SeasidePerson::PhoneSubTypeFax);
phoneSubTypes.append(SeasidePerson::PhoneSubTypePager);
phoneSubTypes.append(SeasidePerson::PhoneSubTypeVoice);
QVariantList numbers = person->phoneDetails();
QCOMPARE(numbers.count(), 4);
for (int i=0; i<numbers.count(); i++) {
QVariant &var(numbers[i]);
QVariantMap number(var.value<QVariantMap>());
QCOMPARE(number.value(QString::fromLatin1("number")).toString(), phoneNumbers.at(i));
QCOMPARE(number.value(QString::fromLatin1("type")).toInt(), static_cast<int>(SeasidePerson::PhoneNumberType));
QCOMPARE(number.value(QString::fromLatin1("subTypes")).value<QVariantList>(), QVariantList());
QCOMPARE(number.value(QString::fromLatin1("label")), QVariant());
// Modify the subType of this detail
number.insert(QString::fromLatin1("subTypes"), QVariantList() << static_cast<int>(phoneSubTypes.at(i)));
// Modify the label also
number.insert(QString::fromLatin1("label"), static_cast<int>(SeasidePerson::OtherLabel));
var = number;
}
// Add another to the list
numbers.append(makePhoneNumber(phoneNumbers.at(4), SeasidePerson::OtherLabel, phoneSubTypes.at(4)));
person->setPhoneDetails(numbers);
QCOMPARE(spy.count(), 2);
numbers = person->phoneDetails();
QCOMPARE(numbers.count(), 5);
for (int i=0; i<numbers.count(); i++) {
QVariant &var(numbers[i]);
QVariantMap number(var.value<QVariantMap>());
QCOMPARE(number.value(QString::fromLatin1("number")).toString(), phoneNumbers.at(i));
QCOMPARE(number.value(QString::fromLatin1("type")).toInt(), static_cast<int>(SeasidePerson::PhoneNumberType));
QCOMPARE(number.value(QString::fromLatin1("subTypes")).value<QVariantList>(), QVariantList() << static_cast<int>(phoneSubTypes.at(i)));
QCOMPARE(number.value(QString::fromLatin1("label")).toInt(), static_cast<int>(SeasidePerson::OtherLabel));
// Now remove the label
number.remove(QString::fromLatin1("label"));
var = number;
}
// Remove all but the middle
person->setPhoneDetails(numbers.mid(2, 1));
QCOMPARE(spy.count(), 3);
numbers = person->phoneDetails();
QCOMPARE(numbers.count(), 1);
{
QVariant &var(numbers[0]);
QVariantMap number(var.value<QVariantMap>());
QCOMPARE(number.value(QString::fromLatin1("number")).toString(), phoneNumbers.at(2));
QCOMPARE(number.value(QString::fromLatin1("type")).toInt(), static_cast<int>(SeasidePerson::PhoneNumberType));
QCOMPARE(number.value(QString::fromLatin1("subTypes")).value<QVariantList>(), QVariantList() << static_cast<int>(phoneSubTypes.at(2)));
QCOMPARE(number.value(QString::fromLatin1("label")), QVariant());
}
}
QVariantMap makeEmailAddress(const QString &address, int label = SeasidePerson::NoLabel)
{
QVariantMap rv;
rv.insert(QString::fromLatin1("address"), address);
rv.insert(QString::fromLatin1("type"), static_cast<int>(SeasidePerson::EmailAddressType));
rv.insert(QString::fromLatin1("label"), label);
rv.insert(QString::fromLatin1("index"), -1);
return rv;
}
void tst_SeasidePerson::emailDetails()
{
QScopedPointer<SeasidePerson> person(new SeasidePerson);
QCOMPARE(person->emailDetails(), QVariantList());
QStringList emailAddresses(QStringList() << "foo@bar" << "bar@foo"<< "foo@baz");
QSignalSpy spy(person.data(), SIGNAL(emailDetailsChanged()));
QVariantList emailDetails;
for (int i = 0; i < 2; ++i) {
emailDetails.append(makeEmailAddress(emailAddresses.at(i)));
}
person->setEmailDetails(emailDetails);
QCOMPARE(spy.count(), 1);
QList<SeasidePerson::DetailLabel> emailLabels;
emailLabels.append(SeasidePerson::HomeLabel);
emailLabels.append(SeasidePerson::WorkLabel);
emailLabels.append(SeasidePerson::OtherLabel);
QVariantList addresses = person->emailDetails();
QCOMPARE(addresses.count(), 2);
for (int i=0; i<addresses.count(); i++) {
QVariant &var(addresses[i]);
QVariantMap address(var.value<QVariantMap>());
QCOMPARE(address.value(QString::fromLatin1("address")).toString(), emailAddresses.at(i));
QCOMPARE(address.value(QString::fromLatin1("type")).toInt(), static_cast<int>(SeasidePerson::EmailAddressType));
QCOMPARE(address.value(QString::fromLatin1("label")), QVariant());
// Modify the label of this detail
address.insert(QString::fromLatin1("label"), static_cast<int>(emailLabels.at(i)));
var = address;
}
// Add another to the list
addresses.append(makeEmailAddress(emailAddresses.at(2), emailLabels.at(2)));
person->setEmailDetails(addresses);
QCOMPARE(spy.count(), 2);
addresses = person->emailDetails();
QCOMPARE(addresses.count(), 3);
for (int i=0; i<addresses.count(); i++) {
QVariant &var(addresses[i]);
QVariantMap address(var.value<QVariantMap>());
QCOMPARE(address.value(QString::fromLatin1("address")).toString(), emailAddresses.at(i));
QCOMPARE(address.value(QString::fromLatin1("type")).toInt(), static_cast<int>(SeasidePerson::EmailAddressType));
QCOMPARE(address.value(QString::fromLatin1("label")).toInt(), static_cast<int>(emailLabels.at(i)));
}
// Remove all but the middle
person->setEmailDetails(addresses.mid(1, 1));
QCOMPARE(spy.count(), 3);
addresses = person->emailDetails();
QCOMPARE(addresses.count(), 1);
{
QVariant &var(addresses[0]);
QVariantMap address(var.value<QVariantMap>());
QCOMPARE(address.value(QString::fromLatin1("address")).toString(), emailAddresses.at(1));
QCOMPARE(address.value(QString::fromLatin1("type")).toInt(), static_cast<int>(SeasidePerson::EmailAddressType));
QCOMPARE(address.value(QString::fromLatin1("label")).toInt(), static_cast<int>(emailLabels.at(1)));
}
}
QVariantMap makeWebsite(const QString &url, int label = SeasidePerson::NoLabel, int subType = SeasidePerson::NoSubType)
{
QVariantMap rv;
rv.insert(QString::fromLatin1("url"), url);
rv.insert(QString::fromLatin1("type"), static_cast<int>(SeasidePerson::WebsiteType));
rv.insert(QString::fromLatin1("subType"), subType);
rv.insert(QString::fromLatin1("label"), label);
rv.insert(QString::fromLatin1("index"), -1);
return rv;
}
void tst_SeasidePerson::websiteDetails()
{
QScopedPointer<SeasidePerson> person(new SeasidePerson);
QCOMPARE(person->websiteDetails(), QVariantList());
QStringList urls(QStringList() << "www.example.com" << "www.test.com" << "www.foobar.com");
QSignalSpy spy(person.data(), SIGNAL(websiteDetailsChanged()));
QVariantList websiteDetails;
for (int i = 0; i < 2; ++i) {
websiteDetails.append(makeWebsite(urls.at(i)));
}
person->setWebsiteDetails(websiteDetails);
QCOMPARE(spy.count(), 1);
QList<SeasidePerson::DetailLabel> websiteLabels;
websiteLabels.append(SeasidePerson::HomeLabel);
websiteLabels.append(SeasidePerson::WorkLabel);
websiteLabels.append(SeasidePerson::OtherLabel);
QList<SeasidePerson::DetailSubType> websiteSubTypes;
websiteSubTypes.append(SeasidePerson::WebsiteSubTypeHomePage);
websiteSubTypes.append(SeasidePerson::WebsiteSubTypeBlog);
websiteSubTypes.append(SeasidePerson::WebsiteSubTypeFavorite);
QVariantList websites = person->websiteDetails();
QCOMPARE(websites.count(), 2);
for (int i=0; i<websites.count(); i++) {
QVariant &var(websites[i]);
QVariantMap website(var.value<QVariantMap>());
QCOMPARE(website.value(QString::fromLatin1("url")).toString(), urls.at(i));
QCOMPARE(website.value(QString::fromLatin1("type")).toInt(), static_cast<int>(SeasidePerson::WebsiteType));
QCOMPARE(website.value(QString::fromLatin1("subType")).toInt(), static_cast<int>(SeasidePerson::NoSubType));
QCOMPARE(website.value(QString::fromLatin1("label")), QVariant());
// Modify the subType of this detail
website.insert(QString::fromLatin1("subType"), static_cast<int>(websiteSubTypes.at(i)));
// Modify the label of this detail
website.insert(QString::fromLatin1("label"), static_cast<int>(websiteLabels.at(i)));
var = website;
}
// Add another to the list
websites.append(makeWebsite(urls.at(2), websiteLabels.at(2), websiteSubTypes.at(2)));
person->setWebsiteDetails(websites);
QCOMPARE(spy.count(), 2);
websites = person->websiteDetails();
QCOMPARE(websites.count(), 3);
for (int i=0; i<websites.count(); i++) {
QVariant &var(websites[i]);
QVariantMap website(var.value<QVariantMap>());
QCOMPARE(website.value(QString::fromLatin1("url")).toString(), urls.at(i));
QCOMPARE(website.value(QString::fromLatin1("type")).toInt(), static_cast<int>(SeasidePerson::WebsiteType));
QCOMPARE(website.value(QString::fromLatin1("subType")).toInt(), static_cast<int>(websiteSubTypes.at(i)));
QCOMPARE(website.value(QString::fromLatin1("label")).toInt(), static_cast<int>(websiteLabels.at(i)));
}
// Remove all but the middle
person->setWebsiteDetails(websites.mid(1, 1));
QCOMPARE(spy.count(), 3);
websites = person->websiteDetails();
QCOMPARE(websites.count(), 1);
{
QVariant &var(websites[0]);
QVariantMap website(var.value<QVariantMap>());
QCOMPARE(website.value(QString::fromLatin1("url")).toString(), urls.at(1));
QCOMPARE(website.value(QString::fromLatin1("type")).toInt(), static_cast<int>(SeasidePerson::WebsiteType));
QCOMPARE(website.value(QString::fromLatin1("subType")).toInt(), static_cast<int>(websiteSubTypes.at(1)));
QCOMPARE(website.value(QString::fromLatin1("label")).toInt(), static_cast<int>(websiteLabels.at(1)));
}
}
QVariantMap makeAccount(const QString &uri, int label = SeasidePerson::NoLabel)
{
QVariantMap rv;
rv.insert(QString::fromLatin1("accountUri"), uri);
rv.insert(QString::fromLatin1("type"), static_cast<int>(SeasidePerson::OnlineAccountType));
rv.insert(QString::fromLatin1("label"), label);
rv.insert(QString::fromLatin1("index"), -1);
return rv;
}
void tst_SeasidePerson::accountDetails()
{
QScopedPointer<SeasidePerson> person(new SeasidePerson);
QCOMPARE(person->accountDetails(), QVariantList());
QStringList uris(QStringList() << "fred@foo.org" << "fred@bar.com" << "123456789");
QSignalSpy spy(person.data(), SIGNAL(accountDetailsChanged()));
QVariantList accountDetails;
for (int i = 0; i < 2; ++i) {
accountDetails.append(makeAccount(uris.at(i)));
}
person->setAccountDetails(accountDetails);
QCOMPARE(spy.count(), 1);
QList<SeasidePerson::DetailLabel> accountLabels;
accountLabels.append(SeasidePerson::HomeLabel);
accountLabels.append(SeasidePerson::WorkLabel);
accountLabels.append(SeasidePerson::OtherLabel);
QVariantList accounts = person->accountDetails();
QCOMPARE(accounts.count(), 2);
for (int i=0; i<accounts.count(); i++) {
QVariant &var(accounts[i]);
QVariantMap account(var.value<QVariantMap>());
QCOMPARE(account.value(QString::fromLatin1("accountUri")).toString(), uris.at(i));
QCOMPARE(account.value(QString::fromLatin1("type")).toInt(), static_cast<int>(SeasidePerson::OnlineAccountType));
QCOMPARE(account.value(QString::fromLatin1("label")), QVariant());
// Modify the label of this detail
account.insert(QString::fromLatin1("label"), static_cast<int>(accountLabels.at(i)));
var = account;
}
// Add another to the list
accounts.append(makeAccount(uris.at(2), accountLabels.at(2)));
person->setAccountDetails(accounts);
QCOMPARE(spy.count(), 2);
accounts = person->accountDetails();
QCOMPARE(accounts.count(), 3);
for (int i=0; i<accounts.count(); i++) {
QVariant &var(accounts[i]);
QVariantMap account(var.value<QVariantMap>());
QCOMPARE(account.value(QString::fromLatin1("accountUri")).toString(), uris.at(i));
QCOMPARE(account.value(QString::fromLatin1("type")).toInt(), static_cast<int>(SeasidePerson::OnlineAccountType));
QCOMPARE(account.value(QString::fromLatin1("label")).toInt(), static_cast<int>(accountLabels.at(i)));
}
// Remove all but the middle
person->setAccountDetails(accounts.mid(1, 1));
QCOMPARE(spy.count(), 3);
accounts = person->accountDetails();
QCOMPARE(accounts.count(), 1);
{
QVariant &var(accounts[0]);
QVariantMap account(var.value<QVariantMap>());
QCOMPARE(account.value(QString::fromLatin1("accountUri")).toString(), uris.at(1));
QCOMPARE(account.value(QString::fromLatin1("type")).toInt(), static_cast<int>(SeasidePerson::OnlineAccountType));
QCOMPARE(account.value(QString::fromLatin1("label")).toInt(), static_cast<int>(accountLabels.at(1)));
}
}
void tst_SeasidePerson::birthday()
{
QScopedPointer<SeasidePerson> person(new SeasidePerson);
QCOMPARE(person->birthday(), QDateTime());
QSignalSpy spy(person.data(), SIGNAL(birthdayChanged()));
person->setBirthday(QDateTime::fromString("05/01/1980 15:00:00.000", "dd/MM/yyyy hh:mm:ss.zzz"));
QCOMPARE(spy.count(), 1);
QCOMPARE(person->birthday(), QDateTime::fromString("05/01/1980 15:00:00.000", "dd/MM/yyyy hh:mm:ss.zzz"));
QCOMPARE(person->property("birthday").toDateTime(), person->birthday());
person->resetBirthday();
QCOMPARE(spy.count(), 2);
QCOMPARE(person->birthday(), QDateTime());
QCOMPARE(person->property("birthday").toDateTime(), person->birthday());
person->setBirthday(QDateTime(QDate::fromString("05/01/1980", "dd/MM/yyyy")));
QCOMPARE(spy.count(), 3);
QCOMPARE(person->birthday(), QDateTime::fromString("05/01/1980 12:00:00.000", "dd/MM/yyyy hh:mm:ss.zzz"));
QCOMPARE(person->property("birthday").toDateTime(), person->birthday());
}
QVariantMap makeAnniversary(const QDateTime ×tamp, int label = SeasidePerson::NoLabel, int subType = SeasidePerson::NoSubType)
{
QVariantMap rv;
rv.insert(QString::fromLatin1("originalDate"), timestamp);
rv.insert(QString::fromLatin1("type"), static_cast<int>(SeasidePerson::AnniversaryType));
rv.insert(QString::fromLatin1("subType"), subType);
rv.insert(QString::fromLatin1("label"), label);
rv.insert(QString::fromLatin1("index"), -1);
return rv;
}
void tst_SeasidePerson::anniversaryDetails()
{
QScopedPointer<SeasidePerson> person(new SeasidePerson);
QCOMPARE(person->anniversaryDetails(), QVariantList());
QList<QDateTime> dates(QList<QDateTime>() << QDateTime::fromString("05/01/1980 15:00:00.000", "dd/MM/yyyy hh:mm:ss.zzz")
<< QDateTime::fromString("05/01/1980 23:59:59.999", "dd/MM/yyyy hh:mm:ss.zzz")
<< QDateTime::fromString("06/01/1980 00:00:00.000", "dd/MM/yyyy hh:mm:ss.zzz"));
QSignalSpy spy(person.data(), SIGNAL(anniversaryDetailsChanged()));
QVariantList anniversaryDetails;
for (int i = 0; i < 2; ++i) {
anniversaryDetails.append(makeAnniversary(dates.at(i)));
}
person->setAnniversaryDetails(anniversaryDetails);
QCOMPARE(spy.count(), 1);
QList<SeasidePerson::DetailLabel> anniversaryLabels;
anniversaryLabels.append(SeasidePerson::HomeLabel);
anniversaryLabels.append(SeasidePerson::WorkLabel);
anniversaryLabels.append(SeasidePerson::OtherLabel);
QList<SeasidePerson::DetailSubType> anniversarySubTypes;
anniversarySubTypes.append(SeasidePerson::AnniversarySubTypeEngagement);
anniversarySubTypes.append(SeasidePerson::AnniversarySubTypeHouse);
anniversarySubTypes.append(SeasidePerson::AnniversarySubTypeEmployment);
QVariantList anniversaries = person->anniversaryDetails();
QCOMPARE(anniversaries.count(), 2);
for (int i=0; i<anniversaries.count(); i++) {
QVariant &var(anniversaries[i]);
QVariantMap anniversary(var.value<QVariantMap>());
QCOMPARE(anniversary.value(QString::fromLatin1("originalDate")).value<QDateTime>(), dates.at(i));
QCOMPARE(anniversary.value(QString::fromLatin1("type")).toInt(), static_cast<int>(SeasidePerson::AnniversaryType));
QCOMPARE(anniversary.value(QString::fromLatin1("subType")).toInt(), static_cast<int>(SeasidePerson::NoSubType));
QCOMPARE(anniversary.value(QString::fromLatin1("label")), QVariant());
// Modify the subType of this detail
anniversary.insert(QString::fromLatin1("subType"), static_cast<int>(anniversarySubTypes.at(i)));
// Modify the label of this detail
anniversary.insert(QString::fromLatin1("label"), static_cast<int>(anniversaryLabels.at(i)));
var = anniversary;
}
// Add another to the list
anniversaries.append(makeAnniversary(dates.at(2), anniversaryLabels.at(2), anniversarySubTypes.at(2)));
person->setAnniversaryDetails(anniversaries);
QCOMPARE(spy.count(), 2);
anniversaries = person->anniversaryDetails();
QCOMPARE(anniversaries.count(), 3);
for (int i=0; i<anniversaries.count(); i++) {
QVariant &var(anniversaries[i]);
QVariantMap anniversary(var.value<QVariantMap>());
QDateTime comparisonDate(dates.at(i));
if (i == 2)
comparisonDate.setTime(QTime(12, 0));
QCOMPARE(anniversary.value(QString::fromLatin1("originalDate")).value<QDateTime>(), comparisonDate);
QCOMPARE(anniversary.value(QString::fromLatin1("type")).toInt(), static_cast<int>(SeasidePerson::AnniversaryType));
QCOMPARE(anniversary.value(QString::fromLatin1("subType")).toInt(), static_cast<int>(anniversarySubTypes.at(i)));
QCOMPARE(anniversary.value(QString::fromLatin1("label")).toInt(), static_cast<int>(anniversaryLabels.at(i)));
}
// Remove all but the middle
person->setAnniversaryDetails(anniversaries.mid(1, 1));
QCOMPARE(spy.count(), 3);
anniversaries = person->anniversaryDetails();
QCOMPARE(anniversaries.count(), 1);
{
QVariant &var(anniversaries[0]);
QVariantMap anniversary(var.value<QVariantMap>());
QCOMPARE(anniversary.value(QString::fromLatin1("originalDate")).value<QDateTime>(), dates.at(1));
QCOMPARE(anniversary.value(QString::fromLatin1("type")).toInt(), static_cast<int>(SeasidePerson::AnniversaryType));
QCOMPARE(anniversary.value(QString::fromLatin1("subType")).toInt(), static_cast<int>(anniversarySubTypes.at(1)));
QCOMPARE(anniversary.value(QString::fromLatin1("label")).toInt(), static_cast<int>(anniversaryLabels.at(1)));
}
}
QVariantMap makeAddress(const QString &address, int label = SeasidePerson::NoLabel)
{
QVariantMap rv;
rv.insert(QString::fromLatin1("address"), address);
rv.insert(QString::fromLatin1("type"), static_cast<int>(SeasidePerson::AddressType));
rv.insert(QString::fromLatin1("label"), label);
rv.insert(QString::fromLatin1("index"), -1);
return rv;
}
void tst_SeasidePerson::addressDetails()
{
QScopedPointer<SeasidePerson> person(new SeasidePerson);
QCOMPARE(person->addressDetails(), QVariantList());
QStringList addressStrings(QStringList() << "Street 1\nLocality 1\nRegion 1\nPostcode 1\nCountry 1\nPoBox 1"
<< "Street 2\nLocality 2\nRegion 2\nPostcode 2\nCountry 2\nPoBox 2"
<< "Street 3\nLocality 3\nRegion 3\nPostcode 3\nCountry 3\nPoBox 3");
QSignalSpy spy(person.data(), SIGNAL(addressDetailsChanged()));
QVariantList addressDetails;
for (int i = 0; i < 2; ++i) {
addressDetails.append(makeAddress(addressStrings.at(i)));
}
person->setAddressDetails(addressDetails);
QCOMPARE(spy.count(), 1);
QList<SeasidePerson::DetailLabel> addressLabels;
addressLabels.append(SeasidePerson::HomeLabel);
addressLabels.append(SeasidePerson::WorkLabel);
addressLabels.append(SeasidePerson::OtherLabel);
QVariantList addresses = person->addressDetails();
QCOMPARE(addresses.count(), 2);
for (int i=0; i<addresses.count(); i++) {
QVariant &var(addresses[i]);
QVariantMap address(var.value<QVariantMap>());
QCOMPARE(address.value(QString::fromLatin1("address")).toString(), addressStrings.at(i));
QCOMPARE(address.value(QString::fromLatin1("type")).toInt(), static_cast<int>(SeasidePerson::AddressType));
QCOMPARE(address.value(QString::fromLatin1("label")), QVariant());
// Modify the label of this detail
address.insert(QString::fromLatin1("label"), static_cast<int>(addressLabels.at(i)));
var = address;
}
// Add another to the list
addresses.append(makeAddress(addressStrings.at(2), addressLabels.at(2)));
person->setAddressDetails(addresses);
QCOMPARE(spy.count(), 2);
addresses = person->addressDetails();
QCOMPARE(addresses.count(), 3);
for (int i=0; i<addresses.count(); i++) {
QVariant &var(addresses[i]);
QVariantMap address(var.value<QVariantMap>());
QCOMPARE(address.value(QString::fromLatin1("address")).toString(), addressStrings.at(i));
QCOMPARE(address.value(QString::fromLatin1("type")).toInt(), static_cast<int>(SeasidePerson::AddressType));
QCOMPARE(address.value(QString::fromLatin1("label")).toInt(), static_cast<int>(addressLabels.at(i)));
}
// Remove all but the middle
person->setAddressDetails(addresses.mid(1, 1));
QCOMPARE(spy.count(), 3);
addresses = person->addressDetails();
QCOMPARE(addresses.count(), 1);
{
QVariant &var(addresses[0]);
QVariantMap address(var.value<QVariantMap>());
QCOMPARE(address.value(QString::fromLatin1("address")).toString(), addressStrings.at(1));
QCOMPARE(address.value(QString::fromLatin1("type")).toInt(), static_cast<int>(SeasidePerson::AddressType));
QCOMPARE(address.value(QString::fromLatin1("label")).toInt(), static_cast<int>(addressLabels.at(1)));
}
}
void tst_SeasidePerson::globalPresenceState()
{
QScopedPointer<SeasidePerson> person(new SeasidePerson);
QCOMPARE(person->globalPresenceState(), SeasidePerson::PresenceUnknown);
// We can't test any changes, as the change is effected by a DBUS request...
}
void tst_SeasidePerson::complete()
{
QScopedPointer<SeasidePerson> person(new SeasidePerson);
// An empty person is always complete
QCOMPARE(person->isComplete(), true);
QSignalSpy spy(person.data(), SIGNAL(completeChanged()));
person->setComplete(false);
person->setComplete(true);
QCOMPARE(spy.count(), 2);
}
void tst_SeasidePerson::marshalling()
{
QContact contact;
{
QContactName nameDetail;
nameDetail.setFirstName("Hello");
nameDetail.setLastName("World");
contact.saveDetail(&nameDetail);
}
{
QContactFavorite favorite;
favorite.setFavorite(true);
contact.saveDetail(&favorite);
}
QScopedPointer<SeasidePerson> person(new SeasidePerson(contact));
QCOMPARE(person->firstName(), QString::fromLatin1("Hello"));
QCOMPARE(person->lastName(), QString::fromLatin1("World"));
QCOMPARE(person->displayLabel(), QString::fromLatin1("Hello World"));
QVERIFY(person->favorite());
QCOMPARE(person->contact(), contact);
}
void tst_SeasidePerson::setContact()
{
// The contact manager must have previously been loaded for this test to succeed
QContactManager cm("org.nemomobile.contacts.sqlite");
QScopedPointer<SeasidePerson> person(new SeasidePerson);
QContact contact;
{ // ### contactChanged is only emitted if the id differs, not any of the members.
contact.setId(QtContactsSqliteExtensions::apiContactId(5, cm.managerUri()));
}
{
QContactName nameDetail;
nameDetail.setFirstName("Hello");
nameDetail.setLastName("World");
contact.saveDetail(&nameDetail);
}
{
QSignalSpy spy(person.data(), SIGNAL(firstNameChanged()));
QSignalSpy spyTwo(person.data(), SIGNAL(lastNameChanged()));
QSignalSpy spyThree(person.data(), SIGNAL(displayLabelChanged()));
QSignalSpy spyFour(person.data(), SIGNAL(contactChanged()));
person->setContact(contact);
QCOMPARE(spy.count(), 1);
QCOMPARE(spyTwo.count(), 1);
QCOMPARE(spyThree.count(), 1);
QCOMPARE(spyFour.count(), 1);
// change them again, nothing should emit
person->setContact(contact);
QCOMPARE(spy.count(), 1);
QCOMPARE(spyTwo.count(), 1);
QCOMPARE(spyThree.count(), 1);
QCOMPARE(spyFour.count(), 1);
}
}
void tst_SeasidePerson::vcard()
{
QScopedPointer<SeasidePerson> person(new SeasidePerson);
person->setFirstName("Star");
person->setLastName("Fish");
person->setPhoneDetails(QVariantList() << makePhoneNumber("12345678"));
QString vcard = person->vCard();
QVERIFY(vcard.indexOf("N:Fish;Star;;;") > 0);
QVERIFY(vcard.indexOf("TEL:12345678") > 0);
// If we were exporting version 3.0, FN would also be expected
//QVERIFY(vcard.indexOf("FN:Star Fish") > 0);
}
void tst_SeasidePerson::syncTarget()
{
QScopedPointer<SeasidePerson> person(new SeasidePerson);
// Until stored to the contacts manager, a person has no syncTarget
QCOMPARE(person->syncTarget(), QString());
}
void tst_SeasidePerson::constituents()
{
QScopedPointer<SeasidePerson> person(new SeasidePerson);
// No constituents in this data
QCOMPARE(person->constituents(), QList<int>());
}
template<int N>
void prepare(int (&indices)[N]) {
for (int i = 0; i < N; ++i)
indices[i] = i;
}
template<int N>
bool permute(int (&indices)[N]) {
return std::next_permutation(indices, indices + N);
}