~chris.gagnon/+junk/qtpim-coverage

« back to all changes in this revision

Viewing changes to tests/auto/contacts/qcontact/tst_qcontact.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 <qcontacts.h>
 
44
#include <QSet>
 
45
#include "../qcontactidmock.h"
 
46
 
 
47
//TESTED_COMPONENT=src/contacts
 
48
 
 
49
QTCONTACTS_USE_NAMESPACE
 
50
 
 
51
class tst_QContact: public QObject
 
52
{
 
53
    Q_OBJECT
 
54
 
 
55
public:
 
56
    tst_QContact();
 
57
    virtual ~tst_QContact();
 
58
 
 
59
private slots:
 
60
    void details();
 
61
    void preferences();
 
62
    void relationships();
 
63
    void type();
 
64
    void tags();
 
65
    void emptiness();
 
66
    void idLessThan();
 
67
    void idHash();
 
68
    void hash();
 
69
    void datastream();
 
70
    void traits();
 
71
    void idTraits();
 
72
    void equality();
 
73
    void inequality();
 
74
    void preferredDetails();
 
75
};
 
76
 
 
77
tst_QContact::tst_QContact()
 
78
{
 
79
}
 
80
 
 
81
tst_QContact::~tst_QContact()
 
82
{
 
83
}
 
84
 
 
85
void tst_QContact::details()
 
86
{
 
87
    // Check that detail keys are unique, regardless of order of initialisation
 
88
    // First, construct the detail first, then the contact
 
89
    QContactOrganization org;
 
90
    org.setTitle("Example Title");
 
91
    QContact keyTest;
 
92
    QVERIFY(keyTest.saveDetail(&org));
 
93
    QList<QContactDetail> allDetails = keyTest.details();
 
94
    QList<int> detailKeys;
 
95
    foreach (const QContactDetail& det, allDetails) {
 
96
        int currKey = det.key();
 
97
        QVERIFY(!detailKeys.contains(currKey));
 
98
        detailKeys.append(currKey);
 
99
    }
 
100
    // Second, construct the detail after the contact has been constructed
 
101
    QContactPhoneNumber num;
 
102
    num.setNumber("123456");
 
103
    QVERIFY(keyTest.saveDetail(&num));
 
104
    allDetails = keyTest.details();
 
105
    detailKeys.clear();
 
106
    foreach (const QContactDetail& det, allDetails) {
 
107
        int currKey = det.key();
 
108
        QVERIFY(!detailKeys.contains(currKey));
 
109
        detailKeys.append(currKey);
 
110
    }
 
111
 
 
112
    // now test for default construction sanity
 
113
    QContact c;
 
114
 
 
115
    // Test there are no details (apart from type) by default
 
116
    QVERIFY(c.isEmpty() == true);
 
117
    QVERIFY(c.details().count() == 1);
 
118
    QVERIFY(c.details(QContactPhoneNumber::Type).count() == 0);
 
119
    QVERIFY(c.details<QContactPhoneNumber>().count() == 0);
 
120
    QVERIFY(c.detail(QContactPhoneNumber::Type).isEmpty());
 
121
    QVERIFY(c.detail<QContactPhoneNumber>().isEmpty());
 
122
    QVERIFY(c.details(QContactPhoneNumber::Type).count() == 0);
 
123
    QVERIFY(c.detail(QContactPhoneNumber::Type).isEmpty());
 
124
    QVERIFY(c.details(QContactPhoneNumber::Type).count() == 0);
 
125
    QVERIFY(c.detail(QContactPhoneNumber::Type).isEmpty());
 
126
 
 
127
    // Test retrieving the first detail (the contact type)
 
128
    QList<QContactDetail> details = c.details();
 
129
    QVERIFY(details.at(0).type() == QContactType::Type);
 
130
 
 
131
    // Fetch non existent detail
 
132
    QContactDetail detail = c.detail(QContactDetail::TypeAddress);
 
133
 
 
134
    QVERIFY(detail.isEmpty());
 
135
    QVERIFY(detail.type() == QContactDetail::TypeUndefined);
 
136
 
 
137
    // retrieve the first detail using the undefined type accessor.
 
138
    detail = c.detail(QContactDetail::TypeUndefined);
 
139
    QVERIFY(detail == details.at(0));
 
140
 
 
141
    QVERIFY(c.details(QContactDetail::TypeAddress).count() == 0);
 
142
 
 
143
    // Add a detail
 
144
    QContactPhoneNumber p;
 
145
    p.setNumber("12345678");
 
146
    QVERIFY(c.saveDetail(&p));
 
147
    QVERIFY(c.isEmpty() == false);
 
148
 
 
149
    QVERIFY(c.details().count() == 2);
 
150
 
 
151
    QVERIFY(c.details(QContactPhoneNumber::Type).count() == 1);
 
152
        QVERIFY(c.details<QContactPhoneNumber>().count() == 1);
 
153
    QVERIFY(!c.detail(QContactPhoneNumber::Type).isEmpty());
 
154
    QVERIFY(!c.detail<QContactPhoneNumber>().isEmpty());
 
155
    QCOMPARE(c.detail<QContactPhoneNumber>(), p);
 
156
 
 
157
    // Remove detail
 
158
    QVERIFY(c.removeDetail(&p));
 
159
    QVERIFY(c.details().count() == 1);
 
160
    QVERIFY(c.isEmpty() == true);
 
161
    QVERIFY(c.details(QContactPhoneNumber::Type).count() == 0);
 
162
    QVERIFY(c.details<QContactPhoneNumber>().count() == 0);
 
163
    QVERIFY(c.detail(QContactPhoneNumber::Type).isEmpty());
 
164
    QVERIFY(c.detail<QContactPhoneNumber>().isEmpty());
 
165
 
 
166
    // Try removing it again
 
167
    QVERIFY(!c.removeDetail(&p));
 
168
 
 
169
    // Add again, and remove a different way (retrieved copy)
 
170
    QVERIFY(c.saveDetail(&p));
 
171
    QVERIFY(c.isEmpty() == false);
 
172
    QVERIFY(c.details().count() == 2);
 
173
    QContactPhoneNumber p2 = c.detail(QContactPhoneNumber::Type);
 
174
    QCOMPARE(p, p2);
 
175
 
 
176
    QVERIFY(c.removeDetail(&p2));
 
177
    QVERIFY(c.details().count() == 1);
 
178
    QVERIFY(c.isEmpty() == true);
 
179
    QVERIFY(c.details(QContactPhoneNumber::Type).count() == 0);
 
180
    QVERIFY(c.details<QContactPhoneNumber>().count() == 0);
 
181
    QVERIFY(c.detail(QContactPhoneNumber::Type).isEmpty());
 
182
    QVERIFY(c.detail<QContactPhoneNumber>().isEmpty());
 
183
 
 
184
    QCOMPARE(p, p2);
 
185
 
 
186
    // Add again again, and remove a different way (base class)
 
187
    QVERIFY(c.saveDetail(&p));
 
188
    QVERIFY(c.details().count() == 2);
 
189
    QContactDetail p3 = c.detail(QContactPhoneNumber::Type);
 
190
    QVERIFY(p == p3);
 
191
 
 
192
    QVERIFY(c.removeDetail(&p3));
 
193
    QVERIFY(c.details().count() == 1);
 
194
    QVERIFY(c.details(QContactPhoneNumber::Type).count() == 0);
 
195
    QVERIFY(c.details<QContactPhoneNumber>().count() == 0);
 
196
    QVERIFY(c.detail(QContactPhoneNumber::Type).isEmpty());
 
197
    QVERIFY(c.detail<QContactPhoneNumber>().isEmpty());
 
198
 
 
199
    QVERIFY(p == p3);
 
200
 
 
201
    // now we want to add multiple details of the same type, and test that retrieval works correctly.
 
202
    p2 = QContactPhoneNumber();
 
203
    p2.setNumber("22222");
 
204
    c.saveDetail(&p);
 
205
    c.saveDetail(&p2);
 
206
    QVERIFY(c.details().count() == 3);
 
207
    QVERIFY(c.details(QContactPhoneNumber::Type).count() == 2);
 
208
    QVERIFY(c.details<QContactPhoneNumber>().count() == 2);
 
209
    QVERIFY(!c.detail(QContactPhoneNumber::Type).isEmpty());
 
210
    QVERIFY(!c.detail<QContactPhoneNumber>().isEmpty());
 
211
    QCOMPARE(c.detail<QContactPhoneNumber>(), p);
 
212
    QVERIFY(c.removeDetail(&p2));
 
213
 
 
214
    // now try removing a detail for which we've set a preference
 
215
    QContactEmailAddress pref;
 
216
    pref.setEmailAddress("test@test");
 
217
    c.saveDetail(&pref);
 
218
    c.setPreferredDetail("SendEmail", pref);
 
219
    QVERIFY(c.isPreferredDetail(QString(), pref));
 
220
    QVERIFY(c.removeDetail(&pref));
 
221
    QVERIFY(!c.isPreferredDetail(QString(), pref));
 
222
 
 
223
    // Now try adding a detail to multiple contacts
 
224
 
 
225
    QContact c2;
 
226
    QVERIFY(c2.isEmpty() == true);
 
227
    QVERIFY(c.saveDetail(&p));
 
228
    QVERIFY(c2.saveDetail(&p));
 
229
    QVERIFY(c2.isEmpty() == false);
 
230
 
 
231
    QVERIFY(c.details().count() == 2);
 
232
    QVERIFY(c.details(QContactPhoneNumber::Type).count() == 1);
 
233
    QVERIFY(c.details<QContactPhoneNumber>().count() == 1);
 
234
    QVERIFY(!c.detail(QContactPhoneNumber::Type).isEmpty());
 
235
    QVERIFY(!c.detail<QContactPhoneNumber>().isEmpty());
 
236
    QCOMPARE(c.detail<QContactPhoneNumber>(), p);
 
237
 
 
238
    QVERIFY(c2.details().count() == 2);
 
239
    QVERIFY(c2.details(QContactPhoneNumber::Type).count() == 1);
 
240
    QVERIFY(c2.details<QContactPhoneNumber>().count() == 1);
 
241
    QVERIFY(!c2.detail(QContactPhoneNumber::Type).isEmpty());
 
242
    QVERIFY(!c2.detail<QContactPhoneNumber>().isEmpty());
 
243
    QCOMPARE(c2.detail<QContactPhoneNumber>(), p);
 
244
 
 
245
    // Now try removing it from one
 
246
    QVERIFY(c.removeDetail(&p));
 
247
 
 
248
    // Make sure it's gone from the first contact
 
249
    QVERIFY(c.isEmpty() == true);
 
250
    QVERIFY(c.details().count() == 1);
 
251
    QVERIFY(c.details(QContactPhoneNumber::Type).count() == 0);
 
252
    QVERIFY(c.details<QContactPhoneNumber>().count() == 0);
 
253
    QVERIFY(c.detail(QContactPhoneNumber::Type).isEmpty());
 
254
    QVERIFY(c.detail<QContactPhoneNumber>().isEmpty());
 
255
 
 
256
    // but not the second
 
257
    QVERIFY(c2.isEmpty() == false);
 
258
    QVERIFY(c2.details().count() == 2);
 
259
    QVERIFY(c2.details(QContactPhoneNumber::Type).count() == 1);
 
260
    QVERIFY(c2.details<QContactPhoneNumber>().count() == 1);
 
261
    QVERIFY(!c2.detail(QContactPhoneNumber::Type).isEmpty());
 
262
    QVERIFY(!c2.detail<QContactPhoneNumber>().isEmpty());
 
263
    QCOMPARE(c2.detail<QContactPhoneNumber>(), p);
 
264
 
 
265
    // Now remove it from the second as well
 
266
    QVERIFY(c2.removeDetail(&p));
 
267
 
 
268
    // Make sure it's gone from both
 
269
    QVERIFY(c.details().count() == 1);
 
270
    QVERIFY(c.details(QContactPhoneNumber::Type).count() == 0);
 
271
    QVERIFY(c.details<QContactPhoneNumber>().count() == 0);
 
272
    QVERIFY(c.detail(QContactPhoneNumber::Type).isEmpty());
 
273
    QVERIFY(c.detail<QContactPhoneNumber>().isEmpty());
 
274
 
 
275
    QVERIFY(c2.details().count() == 1);
 
276
    QVERIFY(c2.details(QContactPhoneNumber::Type).count() == 0);
 
277
    QVERIFY(c2.details<QContactPhoneNumber>().count() == 0);
 
278
    QVERIFY(c2.detail(QContactPhoneNumber::Type).isEmpty());
 
279
    QVERIFY(c2.detail<QContactPhoneNumber>().isEmpty());
 
280
 
 
281
    // add a, add b, remove a, add a, remove b, remove a
 
282
    QVERIFY(c.saveDetail(&p));
 
283
    QVERIFY(c2.saveDetail(&p));
 
284
    QVERIFY(c.removeDetail(&p));
 
285
    QVERIFY(c.saveDetail(&p));
 
286
    QVERIFY(c2.removeDetail(&p));
 
287
    QVERIFY(c.removeDetail(&p));
 
288
 
 
289
    // Now add a detail with the same values twice
 
290
    QContactPhoneNumber one;
 
291
    QContactPhoneNumber two;
 
292
 
 
293
    one.setNumber("12345");
 
294
    two.setNumber("12345");
 
295
 
 
296
    // add it once
 
297
    QVERIFY(c.saveDetail(&one));
 
298
    QVERIFY(c.details().count() == 2);
 
299
    QVERIFY(c.details(QContactPhoneNumber::Type).count() == 1);
 
300
    QVERIFY(c.details<QContactPhoneNumber>().count() == 1);
 
301
    QVERIFY(!c.detail(QContactPhoneNumber::Type).isEmpty());
 
302
    QVERIFY(!c.detail<QContactPhoneNumber>().isEmpty());
 
303
    QCOMPARE(c.detail<QContactPhoneNumber>(), one);
 
304
 
 
305
    // add it twice
 
306
    QVERIFY(c.saveDetail(&two));
 
307
    QVERIFY(c.details().count() == 3);
 
308
    QVERIFY(c.details(QContactPhoneNumber::Type).count() == 2);
 
309
    QVERIFY(c.details<QContactPhoneNumber>().count() == 2);
 
310
    QVERIFY(!c.detail(QContactPhoneNumber::Type).isEmpty());
 
311
    QVERIFY(!c.detail<QContactPhoneNumber>().isEmpty());
 
312
    QCOMPARE(c.detail<QContactPhoneNumber>(), one);
 
313
    QCOMPARE(c.details<QContactPhoneNumber>()[0], one);
 
314
    QCOMPARE(c.details<QContactPhoneNumber>()[1], two);
 
315
 
 
316
    // Remove it once
 
317
    QVERIFY(c.removeDetail(&one));
 
318
    QVERIFY(c.details().count() == 2);
 
319
    QVERIFY(c.details(QContactPhoneNumber::Type).count() == 1);
 
320
    QVERIFY(c.details<QContactPhoneNumber>().count() == 1);
 
321
    QVERIFY(!c.detail(QContactPhoneNumber::Type).isEmpty());
 
322
    QVERIFY(!c.detail<QContactPhoneNumber>().isEmpty());
 
323
    QCOMPARE(c.detail<QContactPhoneNumber>(), two);
 
324
 
 
325
    // Remove it twice
 
326
    QVERIFY(c.removeDetail(&two));
 
327
    QVERIFY(c.details().count() == 1);
 
328
    QVERIFY(c.details(QContactPhoneNumber::Type).count() == 0);
 
329
    QVERIFY(c.details<QContactPhoneNumber>().count() == 0);
 
330
    QVERIFY(c.detail(QContactPhoneNumber::Type).isEmpty());
 
331
    QVERIFY(c.detail<QContactPhoneNumber>().isEmpty());
 
332
 
 
333
    // Null pointer tests
 
334
    QVERIFY(c.saveDetail(0) == false);
 
335
    QVERIFY(c.removeDetail(0) == false);
 
336
 
 
337
    // Reference tests...
 
338
    QContactDetail& ref = one;
 
339
    QVERIFY(c.saveDetail(&one));
 
340
    QVERIFY(ref == one);
 
341
    one.setNumber("56678");
 
342
    QVERIFY(c.saveDetail(&one));
 
343
    QVERIFY(ref == one);
 
344
 
 
345
    // Retrieve the detail again and modify it
 
346
    QContactPhoneNumber three = c.detail<QContactPhoneNumber>();
 
347
    QVERIFY(ref == three);
 
348
    QVERIFY(one == three);
 
349
    three.setNumber("542343");
 
350
    QVERIFY(c.saveDetail(&three));
 
351
 
 
352
    // Now see if we got any updates to ref/one
 
353
    QVERIFY(ref == one);
 
354
    QVERIFY(ref != three);
 
355
 
 
356
    // test saving of a detail with an empty field.
 
357
    QContactPhoneNumber four;
 
358
    four.setNumber("");
 
359
    c.saveDetail(&four);
 
360
    QVERIFY(c.details(QContactPhoneNumber::Type).count() == 2);
 
361
    QVERIFY(!four.values().isEmpty()); // an empty qstring is not invalid; make sure it exists in the detail.
 
362
 
 
363
    // ensure that clearing a contact's details works correctly
 
364
    QContactName nameDetail;
 
365
    nameDetail.setFirstName("test");
 
366
    c.saveDetail(&nameDetail);
 
367
    QCOMPARE(c.detail(QContactName::Type).value(QContactName::FieldFirstName).toString(), QString("test"));
 
368
    QVERIFY(c.details().size() > 0);
 
369
    QVERIFY(!c.isEmpty());
 
370
    QContactId oldId = c.id();
 
371
    c.clearDetails();
 
372
    QVERIFY(c.details().size() == 1); // contact type.
 
373
    QCOMPARE(c.detail(QContactName::Type).value(QContactName::FieldFirstName).toString(), QString());
 
374
    QVERIFY(c.isEmpty());
 
375
    QCOMPARE(c.id(), oldId); // id shouldn't change.
 
376
}
 
377
 
 
378
void tst_QContact::preferences()
 
379
{
 
380
    QContact c;
 
381
 
 
382
    // test first set
 
383
    QContactDetail det(QContactDetail::TypeExtendedDetail);
 
384
    c.saveDetail(&det);
 
385
    QCOMPARE(c.isPreferredDetail("testAction", det), false);
 
386
 
 
387
    QCOMPARE(c.setPreferredDetail("testAction", det), true);
 
388
 
 
389
    QCOMPARE(c.isPreferredDetail("testAction", det), true);
 
390
 
 
391
    QCOMPARE(c.isPreferredDetail(QString(), det), true);
 
392
 
 
393
    QCOMPARE(c.preferredDetail("testAction"), det);
 
394
 
 
395
    // test replacement
 
396
    QContactDetail det2(QContactDetail::TypeExtendedDetail);
 
397
    c.saveDetail(&det2);
 
398
    QCOMPARE(c.isPreferredDetail("testAction", det2), false);
 
399
 
 
400
    QCOMPARE(c.setPreferredDetail("testAction", det2), true);
 
401
 
 
402
    QCOMPARE(c.isPreferredDetail("testAction", det2), true);
 
403
 
 
404
    QCOMPARE(c.isPreferredDetail("testAction", det), false);
 
405
 
 
406
    QCOMPARE(c.preferredDetail("testAction"), det2);
 
407
 
 
408
    // test for detail that is not part of the contact
 
409
    QContactDetail det3(QContactDetail::TypeEmailAddress);
 
410
    QCOMPARE(c.setPreferredDetail("testAction", det3), false);
 
411
 
 
412
    QCOMPARE(c.preferredDetail("testAction"), det2); // shouldn't have changed.
 
413
 
 
414
    // test invalid set
 
415
    QCOMPARE(c.setPreferredDetail(QString(), det3), false);
 
416
 
 
417
    QCOMPARE(c.setPreferredDetail(QString(), QContactDetail()), false);
 
418
 
 
419
    QCOMPARE(c.setPreferredDetail("testAction", QContactDetail()), false);
 
420
 
 
421
    QCOMPARE(c.preferredDetail("testAction"), det2); // shouldn't have changed.
 
422
 
 
423
    // test invalid query
 
424
    QContactDetail det4;
 
425
    c.saveDetail(&det4);
 
426
    QCOMPARE(c.isPreferredDetail(QString(), QContactDetail()), false);
 
427
 
 
428
    QCOMPARE(c.isPreferredDetail(QString(), det4), false); // valid detail, but no pref set.
 
429
 
 
430
    QCOMPARE(c.isPreferredDetail("testAction", QContactDetail()), false);
 
431
 
 
432
    // test retrieving preferred details
 
433
    QContactDetail pd = c.preferredDetail(QString());
 
434
    QVERIFY(pd.isEmpty());
 
435
    pd = c.preferredDetail("testAction");
 
436
    QVERIFY(pd == det2); // shouldn't have changed.
 
437
 
 
438
    // test for preference for action that hasn't been added
 
439
    QVERIFY(c.preferredDetail("NonexistentAction").isEmpty());
 
440
 
 
441
    // Remove a non preferred detail
 
442
    QContactDetail det2copy(QContactDetail::TypeExtendedDetail);
 
443
    QVERIFY(c.saveDetail(&det2copy));
 
444
 
 
445
    QVERIFY(c.isPreferredDetail("testAction", det2) == true);
 
446
    QVERIFY(c.isPreferredDetail("testAction", det2copy) == false);
 
447
    QVERIFY(c.removeDetail(&det2copy));
 
448
    QVERIFY(c.isPreferredDetail("testAction", det2) == true);
 
449
    QVERIFY(c.isPreferredDetail("testAction", det2copy) == false);
 
450
 
 
451
    // Add it again
 
452
    QVERIFY(c.saveDetail(&det2copy));
 
453
    QVERIFY(c.isPreferredDetail("testAction", det2) == true);
 
454
    QVERIFY(c.isPreferredDetail("testAction", det2copy) == false);
 
455
 
 
456
    // Remove the preferred detail (the copy should not become preferred)
 
457
    QVERIFY(c.removeDetail(&det2));
 
458
    QVERIFY(c.isPreferredDetail("testAction", det2) == false);
 
459
    QVERIFY(c.isPreferredDetail("testAction", det2copy) == false);
 
460
}
 
461
 
 
462
void tst_QContact::relationships()
 
463
{
 
464
    QContact c;
 
465
 
 
466
    // boring test, because the default contact has no relationships
 
467
    // we test this more convincingly in the QContactManager tests.
 
468
    QList<QContact> related = c.relatedContacts();
 
469
    QVERIFY(related.isEmpty());
 
470
 
 
471
    related = c.relatedContacts(QContactRelationship::HasMember());
 
472
    QVERIFY(related.isEmpty());
 
473
 
 
474
    related = c.relatedContacts(QContactRelationship::HasMember(), QContactRelationship::First);
 
475
    QVERIFY(related.isEmpty());
 
476
 
 
477
    QList<QContactRelationship> relationshipList = c.relationships();
 
478
    QVERIFY(relationshipList.isEmpty());
 
479
 
 
480
    relationshipList = c.relationships(QContactRelationship::HasMember());
 
481
    QVERIFY(relationshipList.isEmpty());
 
482
}
 
483
 
 
484
void tst_QContact::type()
 
485
{
 
486
    QContact c;
 
487
    QVERIFY(c.isEmpty() == true);
 
488
 
 
489
    // ensure that the default type is the QContactType::TypeContact type
 
490
    QVERIFY(c.type() == QContactType::TypeContact);
 
491
 
 
492
    // now set it to be a group via the type mutator, and test that it works
 
493
    c.setType(QContactType::TypeGroup);
 
494
    QCOMPARE(c.type(), QContactType::TypeGroup);
 
495
 
 
496
    // set it back to a contact, via the string mutator
 
497
    c.setType(QContactType::TypeContact);
 
498
    QCOMPARE(c.type(), QContactType::TypeContact);
 
499
    QVERIFY(c.isEmpty() == true); // type doesn't affect emptiness
 
500
}
 
501
 
 
502
void tst_QContact::tags()
 
503
{
 
504
    QContact c;
 
505
    QVERIFY(c.tags().isEmpty());
 
506
 
 
507
    c.addTag("tag 1");
 
508
    QStringList tags;
 
509
    tags.append("tag 1");
 
510
    QCOMPARE(c.tags(), tags);
 
511
    QList<QContactTag> tagDetails = c.details<QContactTag>();
 
512
    QCOMPARE(tagDetails.size(), 1);
 
513
    QCOMPARE(tagDetails.first().tag(), QStringLiteral("tag 1"));
 
514
 
 
515
    c.clearTags();
 
516
    QVERIFY(c.tags().isEmpty());
 
517
    QVERIFY(c.details<QContactTag>().isEmpty());
 
518
 
 
519
    tags.append("tag 2"); // tags is now "tag 1", "tag 2"
 
520
    c.setTags(tags);
 
521
    QCOMPARE(c.tags(), tags);
 
522
    tagDetails = c.details<QContactTag>();
 
523
    QCOMPARE(tagDetails.size(), 2);
 
524
    QCOMPARE(tagDetails.at(0).tag(), QStringLiteral("tag 1"));
 
525
    QCOMPARE(tagDetails.at(1).tag(), QStringLiteral("tag 2"));
 
526
}
 
527
 
 
528
void tst_QContact::emptiness()
 
529
{
 
530
    QContact c;
 
531
    QVERIFY(c.isEmpty() == true);
 
532
    c.setType(QContactType::TypeContact);
 
533
    QVERIFY(c.type() == QContactType::TypeContact);
 
534
    QVERIFY(c.isEmpty() == true); // type doesn't affect emptiness
 
535
}
 
536
 
 
537
void tst_QContact::idLessThan()
 
538
{
 
539
    QContactId id1 = QContactIdMock::createId("a", 1);
 
540
    QContactId id2 = QContactIdMock::createId("a", 1);
 
541
    QVERIFY(!(id1 < id2));
 
542
    QVERIFY(!(id2 < id1));
 
543
    QContactId id3 = QContactIdMock::createId("a", 2);
 
544
    QContactId id4 = QContactIdMock::createId("b", 1);
 
545
    QContactId id5 = QContactIdMock::createId("", 2);
 
546
    QVERIFY(id1 < id3);
 
547
    QVERIFY(!(id3 < id1));
 
548
    QVERIFY(id1 < id4);
 
549
    QVERIFY(!(id4 < id1));
 
550
    QVERIFY(id3 < id4);
 
551
    QVERIFY(!(id4 < id3));
 
552
    QVERIFY(id5 < id1);
 
553
    QVERIFY(!(id1 < id5));
 
554
}
 
555
 
 
556
void tst_QContact::idHash()
 
557
{
 
558
    QContactId id1 = QContactIdMock::createId("a", 1);
 
559
    QContactId id2 = QContactIdMock::createId("a", 1);
 
560
    QContactId id3 = QContactIdMock::createId("b", 1);
 
561
    QVERIFY(qHash(id1) == qHash(id2));
 
562
    QVERIFY(qHash(id1) != qHash(id3));
 
563
    QSet<QContactId> set;
 
564
    set.insert(id1);
 
565
    set.insert(id2);
 
566
    set.insert(id3);
 
567
    QCOMPARE(set.size(), 2);
 
568
}
 
569
 
 
570
void tst_QContact::hash()
 
571
{
 
572
    QContactId id = QContactIdMock::createId("a", 1);
 
573
    QContact contact1;
 
574
    contact1.setId(id);
 
575
    QContactDetail detail1(QContactDetail::TypeExtendedDetail);
 
576
    detail1.setValue(QContactDetail::FieldContext, "value");
 
577
    contact1.saveDetail(&detail1);
 
578
    QContact contact2;
 
579
    contact2.setId(id);
 
580
    contact2.saveDetail(&detail1);
 
581
    QContact contact3;
 
582
    contact3.setId(id);
 
583
    QContactDetail detail3(QContactDetail::TypeExtendedDetail);
 
584
    detail3.setValue(QContactDetail::FieldContext, "another value");
 
585
    contact3.saveDetail(&detail3);
 
586
    QContact contact4; // no details
 
587
    contact4.setId(id);
 
588
    QContact contact5; // preferred details and relationships shouldn't affect the hash
 
589
    contact5.setId(id);
 
590
    contact5.saveDetail(&detail1);
 
591
    contact5.setPreferredDetail("action", detail1);
 
592
    QContactRelationship rel;
 
593
    QContactManagerEngine::setContactRelationships(&contact5, QList<QContactRelationship>() << rel);
 
594
    QVERIFY(qHash(contact1) == qHash(contact2));
 
595
    QVERIFY(qHash(contact1) != qHash(contact3));
 
596
    QVERIFY(qHash(contact1) != qHash(contact4));
 
597
    QVERIFY(qHash(contact1) == qHash(contact5));
 
598
}
 
599
 
 
600
void tst_QContact::datastream()
 
601
{
 
602
    QByteArray buffer;
 
603
    QDataStream stream1(&buffer, QIODevice::WriteOnly);
 
604
    QContact contactIn;
 
605
    QContactId id = QContactIdMock::createId("manager", 1234);
 
606
    contactIn.setId(id);
 
607
    QContactPhoneNumber phone;
 
608
    phone.setNumber("5678");
 
609
    contactIn.saveDetail(&phone);
 
610
    stream1 << contactIn;
 
611
 
 
612
    QVERIFY(buffer.size() > 0);
 
613
 
 
614
    /* TODO: fix me?
 
615
    QDataStream stream2(buffer);
 
616
    QContact contactOut;
 
617
    stream2 >> contactOut;
 
618
    QCOMPARE(contactOut, contactIn);*/
 
619
}
 
620
 
 
621
void tst_QContact::traits()
 
622
{
 
623
    QVERIFY(sizeof(QContact) == sizeof(void *));
 
624
    QVERIFY(QTypeInfo<QContact>::isComplex);
 
625
    QVERIFY(!QTypeInfo<QContact>::isStatic);
 
626
    QVERIFY(!QTypeInfo<QContact>::isLarge);
 
627
    QVERIFY(!QTypeInfo<QContact>::isPointer);
 
628
    QVERIFY(!QTypeInfo<QContact>::isDummy);
 
629
}
 
630
 
 
631
void tst_QContact::idTraits()
 
632
{
 
633
    QVERIFY(sizeof(QContactId) == sizeof(void *));
 
634
    QVERIFY(QTypeInfo<QContactId>::isComplex);
 
635
    QVERIFY(!QTypeInfo<QContactId>::isStatic);
 
636
    QVERIFY(!QTypeInfo<QContactId>::isLarge);
 
637
    QVERIFY(!QTypeInfo<QContactId>::isPointer);
 
638
    QVERIFY(!QTypeInfo<QContactId>::isDummy);
 
639
}
 
640
 
 
641
void tst_QContact::equality()
 
642
{
 
643
    QContactName name;
 
644
    name.setFirstName("John");
 
645
    name.setLastName("Doe");
 
646
    QContactPhoneNumber number;
 
647
    number.setNumber("7654321");
 
648
    QContactEmailAddress email;
 
649
    email.setEmailAddress("john.doe@nokia.com");
 
650
    QContactExtendedDetail xdetail;
 
651
    xdetail.setName("shoesize");
 
652
    xdetail.setData("45");
 
653
    // Setup two identical contacts
 
654
    QContact one, two;
 
655
    one.saveDetail(&name);
 
656
    one.saveDetail(&number);
 
657
    one.saveDetail(&email);
 
658
    one.saveDetail(&xdetail);
 
659
    two.saveDetail(&xdetail);
 
660
    two.saveDetail(&email);
 
661
    two.saveDetail(&number);
 
662
    two.saveDetail(&name);
 
663
 
 
664
    QVERIFY(one == two);
 
665
}
 
666
 
 
667
void tst_QContact::inequality()
 
668
{
 
669
    QContactId id = QContactIdMock::createId("a", 123);
 
670
    QContactName name;
 
671
    name.setFirstName("John");
 
672
    name.setLastName("Doe");
 
673
    QContactPhoneNumber number;
 
674
    number.setNumber("7654321");
 
675
    QContactEmailAddress email;
 
676
    email.setEmailAddress("john.doe@nokia.com");
 
677
    QContactExtendedDetail xdetail;
 
678
    xdetail.setName("shoesize");
 
679
    xdetail.setData("45");
 
680
    // Setup two contacts
 
681
    QContact one, two;
 
682
    one.setId(id);
 
683
    QVERIFY(one != two);
 
684
    two.setId(id);
 
685
    QVERIFY(one == two);
 
686
    // insert different amount of details
 
687
    one.saveDetail(&name);
 
688
    one.saveDetail(&number);
 
689
    two.saveDetail(&number);
 
690
    QVERIFY(one != two);
 
691
    two.clearDetails();
 
692
    // same amount of details with different types
 
693
    two.saveDetail(&number);
 
694
    two.saveDetail(&email);
 
695
    QVERIFY(one != two);
 
696
    two.clearDetails();
 
697
    // same amount of details with different value
 
698
    name.setFirstName("Jim");
 
699
    two.saveDetail(&name);
 
700
    two.saveDetail(&number);
 
701
    QVERIFY(one != two);
 
702
    two.clearDetails();
 
703
    name.setFirstName("John");
 
704
    // different types of details with same value
 
705
    email.setEmailAddress("7654321");
 
706
    two.saveDetail(&name);
 
707
    two.saveDetail(&email);
 
708
    QVERIFY(one != two);
 
709
}
 
710
 
 
711
void tst_QContact::preferredDetails()
 
712
{
 
713
    QContactPhoneNumber number;
 
714
    number.setNumber("7654321");
 
715
    QContactEmailAddress email;
 
716
    email.setEmailAddress("john.doe@nokia.com");
 
717
    QContactPhoneNumber number2;
 
718
    number2.setNumber("1234567");
 
719
 
 
720
    QContact one;
 
721
    one.saveDetail(&email);
 
722
    one.saveDetail(&number);
 
723
    one.saveDetail(&number2);
 
724
 
 
725
    one.setPreferredDetail("EMAIL", email);
 
726
    one.setPreferredDetail("PHONE", number);
 
727
 
 
728
    QVERIFY(one.preferredDetail("EMAIL") == email);
 
729
    QVERIFY(one.preferredDetail("PHONE") == number);
 
730
 
 
731
    QMap<QString, QContactDetail> prefDetails = one.preferredDetails();
 
732
    QCOMPARE(prefDetails.count(), 2);
 
733
    QVERIFY(prefDetails["EMAIL"] == email);
 
734
    QVERIFY(prefDetails["PHONE"] == number);
 
735
}
 
736
 
 
737
QTEST_MAIN(tst_QContact)
 
738
#include "tst_qcontact.moc"