1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the test suite of the Qt Toolkit.
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.
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.
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.
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.
40
****************************************************************************/
42
//TESTED_COMPONENT=src/versit
44
#include "tst_qversitcontactimporter.h"
45
#include <qversitcontactimporter.h>
46
#include <private/qversitcontactimporter_p.h>
47
#include <qversitproperty.h>
48
#include <qversitdocument.h>
49
#include <qcontacts.h>
50
#include <QtTest/QtTest>
53
QTVERSIT_USE_NAMESPACE
54
class MyQVersitContactImporterPropertyHandler : public QVersitContactImporterPropertyHandler
57
MyQVersitContactImporterPropertyHandler()
62
bool preProcessProperty(const QVersitDocument& document,
63
const QVersitProperty& property,
69
Q_UNUSED(contactIndex);
70
mPreProcessedProperties.append(property);
74
bool postProcessProperty(const QVersitDocument& document,
75
const QVersitProperty& property,
76
bool alreadyProcessed,
82
Q_UNUSED(contactIndex)
83
if (!alreadyProcessed)
84
mUnknownProperties.append(property);
86
mPostProcessedProperties.append(property);
93
mPropertyNamesToProcess.clear();
94
mUnknownProperties.clear();
95
mPreProcessedProperties.clear();
96
mPostProcessedProperties.clear();
99
// a hook to control what preProcess returns:
101
QStringList mPropertyNamesToProcess;
102
QList<QVersitProperty> mUnknownProperties;
103
QList<QVersitProperty> mPreProcessedProperties;
104
QList<QVersitProperty> mPostProcessedProperties;
107
class MyQVersitResourceHandler : public QVersitResourceHandler
110
MyQVersitResourceHandler() : mIndex(0)
114
bool saveResource(const QByteArray& contents, const QVersitProperty& property,
118
*location = QString::number(mIndex++);
119
mObjects.insert(*location, contents);
123
bool loadResource(const QString &location, QByteArray *contents, QString *mimeType)
138
QMap<QUrl, QByteArray> mObjects;
141
const static QByteArray SAMPLE_GIF(QByteArray::fromBase64(
142
"R0lGODlhEgASAIAAAAAAAP///yH5BAEAAAEALAAAAAASABIAAAIdjI+py+0G"
143
"wEtxUmlPzRDnzYGfN3KBaKGT6rDmGxQAOw=="));
145
const static QByteArray NOKIA_GIF(QByteArray::fromBase64(
146
"R0lGODdhOAAKAIQRAAAvwQAwwwAwxAAxxwAyygAzywAzzBBHwC9nz0+A0HCf35+/4LDQ78/f79/o"
147
"8O/v8PD3/////////////////////////////////////////////////////////////ywAAAAA"
148
"OAAKAAAFsCAiik9kRqPJHIfhGixjisuJpqk9Inb0vjaBC0UwFH+uhM+gNBUCw6Wh92vYDAXkCZhF"
149
"apMmA3Qajppav6tr8TqUp0DqEIwtqsmRR/Kl2A4RfFKCcnBMbYR+Uw5xg2lAjIlLCS88dyYNLn1S"
150
"TYwvk3NmkXSQLgVvXmQuBCcQXlI7Io9MpyWCbKgublgCNgxfP0eOs6dvUgsPyMgvEAUAeCafUWhe"
151
"bpI2LQMFenuhZy8hADs="));
153
void tst_QVersitContactImporter::init()
155
mImporter = new QVersitContactImporter();
156
mResourceHandler = new MyQVersitResourceHandler();
157
mImporter->setResourceHandler(mResourceHandler);
158
mPropertyHandler = new MyQVersitContactImporterPropertyHandler();
159
mImporter->setPropertyHandler(mPropertyHandler);
162
void tst_QVersitContactImporter::cleanup()
164
QVERIFY(mImporter->propertyHandler() == mPropertyHandler);
165
MyQVersitContactImporterPropertyHandler* nullPtr = NULL;
166
mImporter->setPropertyHandler(nullPtr);
167
delete mPropertyHandler;
168
QVERIFY(mImporter->resourceHandler() == mResourceHandler);
169
mImporter->setResourceHandler(0);
170
delete mResourceHandler;
174
void tst_QVersitContactImporter::testName()
176
QVersitDocument document(QVersitDocument::VCard30Type);
177
QVersitProperty nameProperty;
179
value.append(QStringLiteral("John"));//FirstName
180
value.append(QStringLiteral("Citizen"));//LastName
181
value.append(QStringLiteral("Anonymous"));//GivenName
182
value.append(QStringLiteral("Dr"));//PreFix
183
value.append(QStringLiteral("MSc"));//Suffix
184
nameProperty.setName(QStringLiteral("N"));
185
nameProperty.setValue(value);
186
nameProperty.setValueType(QVersitProperty::CompoundType);
187
document.addProperty(nameProperty);
188
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
189
QContact contact = mImporter->contacts().first();
190
QContactName name = (QContactName)contact.detail(QContactName::Type);
191
QCOMPARE(name.lastName(),value[0]);
192
QCOMPARE(name.firstName(),value[1]);
193
QCOMPARE(name.middleName(),value[2]);
194
QCOMPARE(name.prefix(),value[3]);
195
QCOMPARE(name.suffix(),value[4]);
197
// Multiple names, first one will be picked and rest will be discarded
198
nameProperty = QVersitProperty();
199
QStringList anotherValue;
200
anotherValue.append(QStringLiteral("FakeJohn"));//FirstName
201
anotherValue.append(QStringLiteral("FakeCitizen"));//LastName
202
anotherValue.append(QStringLiteral("FakeAnonymous"));//GivenName
203
anotherValue.append(QStringLiteral("FakeDr"));//PreFix
204
anotherValue.append(QStringLiteral("FakeMSc"));//Suffix
205
nameProperty.setName(QStringLiteral("N"));
206
nameProperty.setValue(anotherValue);
207
nameProperty.setValueType(QVersitProperty::CompoundType);
208
document.addProperty(nameProperty);
209
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
210
contact = mImporter->contacts().first();
211
QList<QContactDetail> names = contact.details(QContactName::Type);
212
QCOMPARE(names.count(),1);
213
// anotherValue should be discarded, so check for value
214
name = (QContactName)names[0];
215
QCOMPARE(name.lastName(),value[0]);
216
QCOMPARE(name.firstName(),value[1]);
217
QCOMPARE(name.middleName(),value[2]);
218
QCOMPARE(name.prefix(),value[3]);
219
QCOMPARE(name.suffix(),value[4]);
222
// check that it doesn't crash if the FN property comes before the N property.
223
void tst_QVersitContactImporter::testNameWithFormatted()
225
QVersitDocument document(QVersitDocument::VCard30Type);
226
QVersitProperty fnProperty;
227
fnProperty.setName(QStringLiteral("FN"));
228
fnProperty.setValue(QStringLiteral("First Last"));
229
document.addProperty(fnProperty);
230
QVersitProperty nProperty;
231
nProperty.setName(QStringLiteral("N"));
232
nProperty.setValue(QStringList() << QStringLiteral("Last") << QStringLiteral("First") << QStringLiteral("Middle") << QStringLiteral("Prefix") << QStringLiteral("Suffix"));
233
nProperty.setValueType(QVersitProperty::CompoundType);
234
document.addProperty(nProperty);
235
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
236
QContact contact = mImporter->contacts().first();
237
QContactName name = contact.detail<QContactName>();
238
QCOMPARE(name.firstName(), QStringLiteral("First"));
239
QCOMPARE(name.lastName(), QStringLiteral("Last"));
240
QCOMPARE(name.middleName(), QStringLiteral("Middle"));
241
QCOMPARE(name.prefix(), QStringLiteral("Prefix"));
242
QCOMPARE(name.suffix(), QStringLiteral("Suffix"));
245
void tst_QVersitContactImporter::testAddress()
247
QVersitDocument document(QVersitDocument::VCard30Type);
248
QVersitProperty property;
249
property.setName(QStringLiteral("ADR"));
250
property.setValue(QStringList(QString()));
251
property.setValueType(QVersitProperty::CompoundType);
253
// Empty value for the address
254
document = createDocumentWithProperty(property);
255
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
256
QContact contact = mImporter->contacts().first();
257
QContactAddress address = contact.detail<QContactAddress>();
258
QCOMPARE(address.postOfficeBox(),QString());
259
QCOMPARE(address.street(),QString());
260
QCOMPARE(address.locality(),QString());
261
QCOMPARE(address.region(),QString());
262
QCOMPARE(address.postcode(),QString());
263
QCOMPARE(address.country(),QString());
265
// Address with all the fields filled
266
property.setValue(QStringList()
267
<< QStringLiteral("PO Box")
268
<< QStringLiteral("E")
269
<< QStringLiteral("My Street")
270
<< QStringLiteral("My Town")
271
<< QStringLiteral("My State")
272
<< QStringLiteral("12345")
273
<< QStringLiteral("My Country")
275
property.setValueType(QVersitProperty::CompoundType);
276
document = createDocumentWithProperty(property);
277
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
278
contact = mImporter->contacts().first();
279
address = contact.detail<QContactAddress>();
280
QCOMPARE(address.postOfficeBox(),QStringLiteral("PO Box"));
281
QCOMPARE(address.street(),QStringLiteral("My Street"));
282
QCOMPARE(address.locality(),QStringLiteral("My Town"));
283
QCOMPARE(address.region(),QStringLiteral("My State"));
284
QCOMPARE(address.postcode(),QStringLiteral("12345"));
285
QCOMPARE(address.country(),QStringLiteral("My Country"));
287
// Address with TYPE parameters converted to contexts and subtypes
288
property.insertParameter(QStringLiteral("TYPE"),QStringLiteral("HOME"));
289
property.insertParameter(QStringLiteral("TYPE"),QStringLiteral("WORK"));
290
property.insertParameter(QStringLiteral("TYPE"),QStringLiteral("DOM"));
291
property.insertParameter(QStringLiteral("TYPE"),QStringLiteral("INTL"));
292
property.insertParameter(QStringLiteral("TYPE"),QStringLiteral("POSTAL"));
293
property.insertParameter(QStringLiteral("TYPE"),QStringLiteral("PARCEL"));
294
property.insertParameter(QStringLiteral("TYPE"),QStringLiteral("X-EXTENSION"));
295
document = createDocumentWithProperty(property);
296
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
297
contact = mImporter->contacts().first();
298
address = contact.detail<QContactAddress>();
299
QList<int> contexts = address.contexts();
300
QCOMPARE(contexts.size(), 2);
301
QVERIFY(contexts.contains(QContactDetail::ContextHome));
302
QVERIFY(contexts.contains(QContactDetail::ContextWork));
303
QList<int> subTypes = address.subTypes();
304
QCOMPARE(subTypes.size(), 4);
305
QVERIFY(subTypes.contains(QContactAddress::SubTypeDomestic));
306
QVERIFY(subTypes.contains(QContactAddress::SubTypeInternational));
307
QVERIFY(subTypes.contains(QContactAddress::SubTypePostal));
308
QVERIFY(subTypes.contains(QContactAddress::SubTypeParcel));
311
void tst_QVersitContactImporter::testAddressWithoutSubTypes()
313
QVersitDocument document(QVersitDocument::VCard30Type);
314
QVersitProperty property;
315
property.setName(QStringLiteral("ADR"));
316
property.setValue(QStringList()
317
<< QStringLiteral("PO Box")
318
<< QStringLiteral("E")
319
<< QStringLiteral("My Street")
320
<< QStringLiteral("My Town")
321
<< QStringLiteral("My State")
322
<< QStringLiteral("12345")
323
<< QStringLiteral("My Country")
325
property.setValueType(QVersitProperty::CompoundType);
326
document = createDocumentWithProperty(property);
327
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
328
QContact contact = mImporter->contacts().first();
329
QContactAddress address = contact.detail<QContactAddress>();
331
QContactAddress otherAddress;
332
otherAddress.setPostOfficeBox(QStringLiteral("PO Box"));
333
otherAddress.setStreet(QStringLiteral("My Street"));
334
otherAddress.setLocality(QStringLiteral("My Town"));
335
otherAddress.setRegion(QStringLiteral("My State"));
336
otherAddress.setPostcode(QStringLiteral("12345"));
337
otherAddress.setCountry(QStringLiteral("My Country"));
339
QCOMPARE(address, otherAddress);
342
void tst_QVersitContactImporter::testOrganizationName()
344
QVersitDocument document(QVersitDocument::VCard30Type);
345
QVersitProperty property;
347
// Empty value for the organization
348
property.setName(QStringLiteral("ORG"));
349
document = createDocumentWithProperty(property);
350
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
351
QContact contact = mImporter->contacts().first();
352
QContactOrganization organization = contact.detail<QContactOrganization>();
353
QCOMPARE(organization.name(),QString());
354
QCOMPARE(organization.department().count(),0);
356
// Organization with single value
357
property.setValueType(QVersitProperty::CompoundType);
358
property.setValue(QStringList(QStringLiteral("Nokia")));
359
document = createDocumentWithProperty(property);
360
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
361
contact = mImporter->contacts().first();
362
organization = contact.detail<QContactOrganization>();
363
QCOMPARE(organization.name(),QStringLiteral("Nokia"));
364
QCOMPARE(organization.department().count(),0);
366
// Organization with one Organizational Unit
367
property.setValue(QStringList() << QStringLiteral("Nokia") << QStringLiteral("R&D"));
368
document = createDocumentWithProperty(property);
369
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
370
contact = mImporter->contacts().first();
371
organization = contact.detail<QContactOrganization>();
372
QCOMPARE(organization.name(),QStringLiteral("Nokia"));
373
QCOMPARE(organization.department().count(),1);
374
QCOMPARE(organization.department().at(0),QStringLiteral("R&D"));
376
// Organization with more Organizational Units
377
property.setValue(QStringList()
378
<< QStringLiteral("Nokia")
379
<< QStringLiteral("R&D")
380
<< QStringLiteral("Devices")
381
<< QStringLiteral("Qt"));
382
property.setValueType(QVersitProperty::CompoundType);
383
document = createDocumentWithProperty(property);
384
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
385
contact = mImporter->contacts().first();
386
organization = contact.detail<QContactOrganization>();
387
QCOMPARE(organization.name(),QStringLiteral("Nokia"));
388
QCOMPARE(organization.department().count(),3);
389
QCOMPARE(organization.department().at(0),QStringLiteral("R&D"));
390
QCOMPARE(organization.department().at(1),QStringLiteral("Devices"));
391
QCOMPARE(organization.department().at(2),QStringLiteral("Qt"));
394
void tst_QVersitContactImporter::testOrganizationTitle()
396
QVersitDocument document(QVersitDocument::VCard30Type);
397
QVersitProperty property;
400
property.setName(QStringLiteral("TITLE"));
401
QString titleValue(QStringLiteral("Developer"));
402
property.setValue(titleValue);
403
document = createDocumentWithProperty(property);
404
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
405
QContact contact = mImporter->contacts().first();
406
QList<QContactDetail> organizationDetails =
407
contact.details(QContactOrganization::Type);
408
QCOMPARE(organizationDetails.count(), 1);
409
QContactOrganization organization = static_cast<QContactOrganization>(organizationDetails[0]);
410
QCOMPARE(organization.title(),titleValue);
412
// Two titles -> two QContactOrganizations created
413
property.setName(QStringLiteral("TITLE"));
414
QString secondTitleValue(QStringLiteral("Hacker"));
415
property.setValue(secondTitleValue);
416
document.addProperty(property);
417
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
418
contact = mImporter->contacts().first();
419
organizationDetails = contact.details(QContactOrganization::Type);
420
QCOMPARE(organizationDetails.count(), 2);
421
QContactOrganization firstOrganization =
422
static_cast<QContactOrganization>(organizationDetails[0]);
423
QCOMPARE(firstOrganization.title(),titleValue);
424
QContactOrganization secondOrganization =
425
static_cast<QContactOrganization>(organizationDetails[1]);
426
QCOMPARE(secondOrganization.title(),secondTitleValue);
428
// Two titles and one organization name -> two QContactOrganizations created
429
property.setName(QStringLiteral("ORG"));
430
property.setValueType(QVersitProperty::CompoundType);
431
property.setValue(QStringList(QStringLiteral("Nokia")));
432
document.addProperty(property);
433
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
434
contact = mImporter->contacts().first();
435
organizationDetails = contact.details(QContactOrganization::Type);
436
QCOMPARE(organizationDetails.count(), 2);
437
firstOrganization = static_cast<QContactOrganization>(organizationDetails[0]);
438
QCOMPARE(firstOrganization.title(),titleValue);
439
QCOMPARE(firstOrganization.name(),QStringLiteral("Nokia"));
440
secondOrganization = static_cast<QContactOrganization>(organizationDetails[1]);
441
QCOMPARE(secondOrganization.title(),secondTitleValue);
442
QCOMPARE(secondOrganization.name(),QString());
445
void tst_QVersitContactImporter::testOrganizationAssistant()
448
QVersitDocument document(QVersitDocument::VCard30Type);
449
QVersitProperty property;
450
property.setName(QStringLiteral("X-ASSISTANT"));
451
QString assistantValue(QStringLiteral("Jenny"));
452
property.setValue(assistantValue);
453
document = createDocumentWithProperty(property);
454
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
455
contact = mImporter->contacts().first();
456
QContactOrganization organization = contact.detail<QContactOrganization>();
457
QCOMPARE(organization.assistantName(), assistantValue);
460
void tst_QVersitContactImporter::testOrganizationLogo()
463
QVersitDocument document(QVersitDocument::VCard30Type);
464
QVersitProperty property;
467
property.setName(QStringLiteral("LOGO"));
468
QByteArray logo(QByteArray::fromBase64(
469
"R0lGODlhEgASAIAAAAAAAP///yH5BAEAAAEALAAAAAASABIAAAIdjI+py+0G"));
470
property.setValue(logo);
471
property.insertParameter(QStringLiteral("TYPE"),
472
QStringLiteral("GIF"));
473
document = createDocumentWithProperty(property);
474
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
475
contact = mImporter->contacts().first();
476
QContactOrganization organization = contact.detail<QContactOrganization>();
477
QByteArray content = mResourceHandler->mObjects.value(organization.logoUrl().toString());
478
QCOMPARE(content, logo);
481
property.setName(QStringLiteral("LOGO"));
482
QString logoUrl(QStringLiteral("http://www.organization.org/logo.gif"));
483
property.setValue(logoUrl);
484
property.insertParameter(QStringLiteral("VALUE"),QStringLiteral("URL"));
485
document = createDocumentWithProperty(property);
486
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
487
contact = mImporter->contacts().first();
488
organization = contact.detail<QContactOrganization>();
489
QCOMPARE(organization.logoUrl().toString(),logoUrl);
492
void tst_QVersitContactImporter::testOrganizationRole()
495
QVersitDocument document(QVersitDocument::VCard30Type);
496
QVersitProperty property;
498
// Setting the role is not yet supported by QContactOrganization
499
property.setName(QStringLiteral("ROLE"));
500
QString roleValue(QStringLiteral("Very important manager and proud of it"));
501
property.setValue(roleValue);
502
document = createDocumentWithProperty(property);
503
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
504
contact = mImporter->contacts().first();
505
QContactOrganization organization = contact.detail<QContactOrganization>();
506
QCOMPARE(organization.role(), roleValue);
509
void tst_QVersitContactImporter::testTel()
511
QVersitDocument document(QVersitDocument::VCard30Type);
512
QVersitProperty property;
513
property.setName(QStringLiteral("TEL"));
514
QString value(QStringLiteral("+35850987654321"));
515
property.setValue(value);
517
property.insertParameter(QStringLiteral("TYPE"),QStringLiteral("VOICE"));
518
property.insertParameter(QStringLiteral("TYPE"),QStringLiteral("CELL"));
519
property.insertParameter(QStringLiteral("TYPE"),QStringLiteral("MODEM"));
520
property.insertParameter(QStringLiteral("TYPE"),QStringLiteral("CAR"));
521
property.insertParameter(QStringLiteral("TYPE"),QStringLiteral("VIDEO"));
522
property.insertParameter(QStringLiteral("TYPE"),QStringLiteral("FAX"));
523
property.insertParameter(QStringLiteral("TYPE"),QStringLiteral("BBS"));
524
property.insertParameter(QStringLiteral("TYPE"),QStringLiteral("PAGER"));
525
property.insertParameter(QStringLiteral("TYPE"),QStringLiteral("HOME"));
526
property.insertParameter(QStringLiteral("TYPE"),QStringLiteral("WORK"));
527
property.insertParameter(QStringLiteral("TYPE"),QStringLiteral("ISDN"));
529
document.addProperty(property);
530
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
531
QContact contact = mImporter->contacts().first();
532
const QContactPhoneNumber& phone = contact.detail<QContactPhoneNumber>();
533
QCOMPARE(phone.number(),QString(value));
535
const QList<int> subTypes = phone.subTypes();
536
QCOMPARE(subTypes.count(), 9);
537
QVERIFY(subTypes.contains(QContactPhoneNumber::SubTypeVoice));
538
QVERIFY(subTypes.contains(QContactPhoneNumber::SubTypeMobile));
539
QVERIFY(subTypes.contains(QContactPhoneNumber::SubTypeModem));
540
QVERIFY(subTypes.contains(QContactPhoneNumber::SubTypeCar));
541
QVERIFY(subTypes.contains(QContactPhoneNumber::SubTypeVideo));
542
QVERIFY(subTypes.contains(QContactPhoneNumber::SubTypeFax));
543
QVERIFY(subTypes.contains(QContactPhoneNumber::SubTypeBulletinBoardSystem));
544
QVERIFY(subTypes.contains(QContactPhoneNumber::SubTypePager));
545
QVERIFY(subTypes.contains(QContactPhoneNumber::SubTypeLandline));
547
const QList<int> contexts = phone.contexts();
548
QCOMPARE(contexts.count(),2);
549
QVERIFY(contexts.contains(QContactDetail::ContextWork));
550
QVERIFY(contexts.contains(QContactDetail::ContextHome));
553
void tst_QVersitContactImporter::testTelWithoutSubTypes()
555
QVersitDocument document(QVersitDocument::VCard30Type);
556
QVersitProperty property;
557
property.setName(QStringLiteral("TEL"));
558
QString value(QStringLiteral("+35850987654321"));
559
property.setValue(value);
561
document.addProperty(property);
562
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
563
QContact contact = mImporter->contacts().first();
564
const QContactPhoneNumber& phone = contact.detail<QContactPhoneNumber>();
566
QContactPhoneNumber otherPhone;
567
otherPhone.setNumber(QStringLiteral("+35850987654321"));
569
QCOMPARE(otherPhone, phone);
572
void tst_QVersitContactImporter::testEmail()
574
QVersitProperty property;
575
property.setName(QStringLiteral("EMAIL"));
576
QString value(QStringLiteral("john.citizen@example.com"));
577
property.setValue(value);
578
property.insertParameter(QStringLiteral("TYPE"),QStringLiteral("WORK"));
579
QVersitDocument document = createDocumentWithProperty(property);
580
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
581
QContact contact = mImporter->contacts().first();
582
QContactEmailAddress email = contact.detail<QContactEmailAddress>();
583
QCOMPARE(email.emailAddress(),value);
584
const QList<int> contexts = email.contexts();
585
QCOMPARE(contexts.count(),1);
586
QVERIFY(contexts.contains(QContactDetail::ContextWork));
588
QCOMPARE(mPropertyHandler->mUnknownProperties.size(), 0);
591
void tst_QVersitContactImporter::testUrl()
593
QVersitProperty property;
594
property.setName(QStringLiteral("URL"));
595
QString value(QStringLiteral("http://example.com"));
596
property.setValue(value);
597
property.insertParameter(QStringLiteral("TYPE"),QStringLiteral("WORK"));
598
QVersitDocument document = createDocumentWithProperty(property);
599
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
600
QContact contact = mImporter->contacts().first();
601
QContactUrl url = contact.detail<QContactUrl>();
602
QCOMPARE(url.url(),value);
603
const QList<int> contexts = url.contexts();
604
QCOMPARE(contexts.count(),1);
605
QVERIFY(contexts.contains(QContactDetail::ContextWork));
608
void tst_QVersitContactImporter::testUid()
610
QVersitProperty property;
611
property.setName(QStringLiteral("UID"));
612
QString value(QStringLiteral("unique identifier"));
613
property.setValue(value);
614
QVersitDocument document = createDocumentWithProperty(property);
615
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
616
QContact contact = mImporter->contacts().first();
617
QContactGuid uid = contact.detail<QContactGuid>();
618
QCOMPARE(uid.guid(),value);
621
void tst_QVersitContactImporter::testTimeStamp()
623
// Simple date : ISO 8601 extended format
624
QVersitProperty property;
625
property.setName(QStringLiteral("REV"));
626
QString dateValue(QStringLiteral("1981-05-20"));
627
property.setValue(dateValue);
628
QVersitDocument document = createDocumentWithProperty(property);
629
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
630
QContact contact = mImporter->contacts().first();
631
QContactTimestamp timeStamp = contact.detail<QContactTimestamp>();
632
QCOMPARE(timeStamp.lastModified().date().toString(Qt::ISODate),dateValue);
634
// Date and Time : ISO 8601 extended format without utc offset
635
QString dateAndTimeValue(QStringLiteral("1981-05-20T23:55:55"));
636
property.setValue(dateAndTimeValue);
637
document = createDocumentWithProperty(property);
638
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
639
contact = mImporter->contacts().first();
640
timeStamp = contact.detail<QContactTimestamp>();
641
QCOMPARE(timeStamp.lastModified().toString(Qt::ISODate),dateAndTimeValue);
643
// Date and Time : ISO 8601 extented format with utc offset
644
QString utcOffset(QStringLiteral("Z"));
645
QString dateAndTimeWithUtcValue = dateAndTimeValue+utcOffset;
646
property.setValue(dateAndTimeWithUtcValue);
647
document = createDocumentWithProperty(property);
648
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
649
contact = mImporter->contacts().first();
650
timeStamp = contact.detail<QContactTimestamp>();
651
QCOMPARE(timeStamp.lastModified().toString(Qt::ISODate), dateAndTimeWithUtcValue);
652
QCOMPARE(timeStamp.lastModified().timeSpec(), Qt::UTC);
654
// Date and Time : ISO 8601 in basic format without utc offset
655
dateAndTimeValue = QStringLiteral("19810520T235555");
656
property.setValue(dateAndTimeValue);
657
document = createDocumentWithProperty(property);
658
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
659
contact = mImporter->contacts().first();
660
timeStamp = contact.detail<QContactTimestamp>();
662
QCOMPARE(timeStamp.lastModified().toString(QStringLiteral("yyyyMMddThhmmss")),
665
// Date and Time : ISO 8601 in basic format with utc offset
666
dateAndTimeValue = QStringLiteral("19810520T235555");
667
dateAndTimeWithUtcValue = dateAndTimeValue+utcOffset;
668
property.setValue(dateAndTimeWithUtcValue);
669
document = createDocumentWithProperty(property);
670
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
671
contact = mImporter->contacts().first();
672
timeStamp = contact.detail<QContactTimestamp>();
673
QCOMPARE(timeStamp.lastModified().toString(QStringLiteral("yyyyMMddThhmmss")),
675
QCOMPARE(timeStamp.lastModified().timeSpec(),Qt::UTC);
678
void tst_QVersitContactImporter::testVersion()
680
QFETCH(QString, sequenceNumber);
681
QFETCH(QString, extendedVersion);
682
QFETCH(bool, versionCreated);
684
QVersitDocument document(QVersitDocument::VCard30Type);
685
addVersionPropertyToDocument(sequenceNumber,extendedVersion,document);
686
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
687
QContact contact = mImporter->contacts().first();
688
if (!versionCreated) {
689
QCOMPARE(contact.details(QContactVersion::Type).size(), 0);
692
QContactVersion version = (QContactVersion)contact.detail(QContactVersion::Type);
693
QCOMPARE(version.sequenceNumber(), sequenceNumber.toInt());
694
QCOMPARE(QString(version.extendedVersion()), extendedVersion);
697
void tst_QVersitContactImporter::testVersion_data()
699
QTest::addColumn<QString>("sequenceNumber");
700
QTest::addColumn<QString>("extendedVersion");
701
QTest::addColumn<bool>("versionCreated");
704
QTest::newRow("proper version") << QString("4711") << QString("134f23dbb2") << true;
705
QTest::newRow("all empty") << QString("") << QString("") << false;
706
QTest::newRow("sequenceNumber empty") << QString("") << QString("134f23dbb3") << false;
707
QTest::newRow("sequenceNumber negative") << QString("-2") << QString("134f23dbb4") << true;
708
QTest::newRow("sequenceNumber invalid mix") << QString("2withletters") << QString("134f23dbb5") << false;
709
QTest::newRow("extendedVersion empty") << QString("4712") << QString("") << true;
710
QTest::newRow("extendedVersion negative") << QString("4713") << QString("-1234567") << true;
714
void tst_QVersitContactImporter::testMultipleVersions()
716
QVersitDocument document(QVersitDocument::VCard30Type);
717
addVersionPropertyToDocument(QStringLiteral("1"), QStringLiteral("134f23dbb2"), document);
718
addVersionPropertyToDocument(QStringLiteral("2"), QStringLiteral("134f23dbb3"), document);
719
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
720
QContact contact = mImporter->contacts().first();
721
QCOMPARE(contact.details(QContactVersion::Type).size(), 1);
722
QContactVersion version = (QContactVersion)contact.detail(QContactVersion::Type);
723
QCOMPARE(version.sequenceNumber(), 1);
724
QCOMPARE(version.extendedVersion(), QByteArray("134f23dbb2"));
727
void tst_QVersitContactImporter::addVersionPropertyToDocument(QString sequenceNumber, QString extendedVersion, QVersitDocument &document)
729
QVersitProperty property;
730
property.setName(QStringLiteral("X-QTPROJECT-VERSION"));
732
value.append(sequenceNumber);
733
value.append(extendedVersion);
734
property.setValue(value);
735
property.setValueType(QVersitProperty::CompoundType);
736
document.addProperty(property);
739
void tst_QVersitContactImporter::testAnniversary()
741
// Date : ISO 8601 extended format
742
QVersitProperty property;
743
property.setName(QStringLiteral("X-ANNIVERSARY"));
744
property.setValue(QStringLiteral("1981-05-20"));
745
QVersitDocument document = createDocumentWithProperty(property);
746
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
747
QContact contact = mImporter->contacts().first();
748
QContactAnniversary anniversary = contact.detail<QContactAnniversary>();
749
QCOMPARE(anniversary.originalDate(), QDate(1981, 5, 20));
750
QCOMPARE(anniversary.value(QContactAnniversary::FieldOriginalDate).type(), QVariant::Date);
753
property.setName(QStringLiteral("X-EVOLUTION-ANNIVERSARY"));
754
property.setValue(QStringLiteral("1981-05-20"));
755
document = createDocumentWithProperty(property);
756
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
757
contact = mImporter->contacts().first();
758
anniversary = contact.detail<QContactAnniversary>();
759
QCOMPARE(anniversary.originalDate(), QDate(1981, 5, 20));
760
QCOMPARE(anniversary.value(QContactAnniversary::FieldOriginalDate).type(), QVariant::Date);
762
// Date : ISO 8601 in basic format
763
property.setName(QStringLiteral("X-ANNIVERSARY"));
764
property.setValue(QStringLiteral("19810520"));
765
document = createDocumentWithProperty(property);
766
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
767
contact = mImporter->contacts().first();
768
anniversary = contact.detail<QContactAnniversary>();
769
QCOMPARE(anniversary.originalDate(), QDate(1981, 5, 20));
770
QCOMPARE(anniversary.value(QContactAnniversary::FieldOriginalDate).type(), QVariant::Date);
773
property.setName(QStringLiteral("X-ANNIVERSARY"));
774
property.setValue(QStringLiteral("19810520T000102")); // localtime
775
document = createDocumentWithProperty(property);
776
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
777
contact = mImporter->contacts().first();
778
anniversary = contact.detail<QContactAnniversary>();
779
QCOMPARE(anniversary.originalDate(), QDate(1981, 5, 20));
780
QCOMPARE(anniversary.originalDateTime(), QDateTime(QDate(1981, 5, 20), QTime(0,1,2), Qt::LocalTime));
781
QCOMPARE(anniversary.value(QContactAnniversary::FieldOriginalDate).type(), QVariant::DateTime);
783
property.setValue(QStringLiteral("19810520T010203Z")); // utc
784
document = createDocumentWithProperty(property);
785
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
786
contact = mImporter->contacts().first();
787
anniversary = contact.detail<QContactAnniversary>();
788
QCOMPARE(anniversary.originalDate(), QDate(1981, 5, 20));
789
QCOMPARE(anniversary.originalDateTime(), QDateTime(QDate(1981, 5, 20), QTime(1,2,3), Qt::UTC));
790
QCOMPARE(anniversary.value(QContactAnniversary::FieldOriginalDate).type(), QVariant::DateTime);
793
void tst_QVersitContactImporter::testBirthday()
795
// Date : ISO 8601 extended format
796
QVersitProperty property;
797
property.setName(QStringLiteral("BDAY"));
798
QString dateValue(QStringLiteral("1981-05-20"));
799
property.setValue(dateValue);
800
QVersitDocument document = createDocumentWithProperty(property);
801
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
802
QContact contact = mImporter->contacts().first();
803
QContactBirthday bday = contact.detail<QContactBirthday>();
804
QCOMPARE(bday.date().toString(Qt::ISODate), dateValue);
805
QCOMPARE(bday.value(QContactBirthday::FieldBirthday).type(), QVariant::Date);
807
// Date : ISO 8601 in basic format
808
dateValue = QStringLiteral("19810520");
809
property.setValue(dateValue);
810
document = createDocumentWithProperty(property);
811
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
812
contact = mImporter->contacts().first();
813
bday = contact.detail<QContactBirthday>();
814
QCOMPARE(bday.date(), QDate(1981, 5, 20));
815
QCOMPARE(bday.value(QContactBirthday::FieldBirthday).type(), QVariant::Date);
817
dateValue = QStringLiteral("1981-05-20");
818
property.setValue(dateValue);
819
document = createDocumentWithProperty(property);
820
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
821
contact = mImporter->contacts().first();
822
bday = contact.detail<QContactBirthday>();
823
QCOMPARE(bday.date(), QDate(1981, 5, 20));
824
QCOMPARE(bday.value(QContactBirthday::FieldBirthday).type(), QVariant::Date);
826
dateValue = QStringLiteral("19810520T100000");
827
property.setValue(dateValue);
828
document = createDocumentWithProperty(property);
829
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
830
contact = mImporter->contacts().first();
831
bday = contact.detail<QContactBirthday>();
832
QCOMPARE(bday.dateTime(), QDateTime(QDate(1981, 5, 20), QTime(10, 0, 0), Qt::LocalTime));
833
QCOMPARE(bday.value(QContactBirthday::FieldBirthday).type(), QVariant::DateTime);
835
dateValue = QStringLiteral("1981-05-20T10:00:00");
836
property.setValue(dateValue);
837
document = createDocumentWithProperty(property);
838
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
839
contact = mImporter->contacts().first();
840
bday = contact.detail<QContactBirthday>();
841
QCOMPARE(bday.dateTime(), QDateTime(QDate(1981, 5, 20), QTime(10, 0, 0), Qt::LocalTime));
842
QCOMPARE(bday.value(QContactBirthday::FieldBirthday).type(), QVariant::DateTime);
844
dateValue = QStringLiteral("19810520T100000Z");
845
property.setValue(dateValue);
846
document = createDocumentWithProperty(property);
847
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
848
contact = mImporter->contacts().first();
849
bday = contact.detail<QContactBirthday>();
850
QCOMPARE(bday.dateTime(), QDateTime(QDate(1981, 5, 20), QTime(10, 0, 0), Qt::UTC));
851
QCOMPARE(bday.value(QContactBirthday::FieldBirthday).type(), QVariant::DateTime);
853
dateValue = QStringLiteral("1981-05-20T10:00:00Z");
854
property.setValue(dateValue);
855
document = createDocumentWithProperty(property);
856
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
857
contact = mImporter->contacts().first();
858
bday = contact.detail<QContactBirthday>();
859
QCOMPARE(bday.dateTime(), QDateTime(QDate(1981, 5, 20), QTime(10, 0, 0), Qt::UTC));
860
QCOMPARE(bday.value(QContactBirthday::FieldBirthday).type(), QVariant::DateTime);
863
void tst_QVersitContactImporter::testGender()
865
// Check empty property.
866
QVersitProperty property1;
867
property1.setName(QStringLiteral("X-GENDER"));
868
QString val1(QStringLiteral(""));
869
property1.setValue(val1);
870
QVersitDocument document1 = createDocumentWithProperty(property1);
871
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document1));
872
QContact contact1 = mImporter->contacts().first();
873
QCOMPARE (contact1, QContact());
875
// Check valid values.
876
QVersitProperty property2;
877
property2.setName(QStringLiteral("X-GENDER"));
878
QString val2(QStringLiteral("male"));
879
property2.setValue(val2);
880
QVersitDocument document2 = createDocumentWithProperty(property2);
881
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document2));
882
QContact contact2 = mImporter->contacts().first();
883
QContactGender gender2 = contact2.detail<QContactGender >();
884
QCOMPARE(gender2.gender(),QContactGender::GenderMale);
886
QVersitProperty property3;
887
property3.setName(QStringLiteral("x-gender"));
888
QString val3(QStringLiteral("FEMALE"));
889
property3.setValue(val3);
890
QVersitDocument document3 = createDocumentWithProperty(property3);
891
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document3));
892
QContact contact3 = mImporter->contacts().first();
893
QContactGender gender3 = contact3.detail<QContactGender >();
894
QCOMPARE(gender3.gender(),QContactGender::GenderFemale);
896
QVersitProperty property4;
897
property4.setName(QStringLiteral("x-Gender"));
898
QString val4(QStringLiteral("Unspecified"));
899
property4.setValue(val4);
900
QVersitDocument document4 = createDocumentWithProperty(property4);
901
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document4));
902
QContact contact4 = mImporter->contacts().first();
903
QContactGender gender4 = contact4.detail<QContactGender >();
904
QCOMPARE(gender4.gender(),QContactGender::GenderUnspecified);
906
// Check property having an invalid value.
907
QVersitProperty property5;
908
property5.setName(QStringLiteral("X-GENDER"));
909
QString val5(QStringLiteral("Garbage"));
910
property5.setValue(val5);
911
QVersitDocument document5 = createDocumentWithProperty(property5);
912
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document5));
913
QContact contact5 = mImporter->contacts().first();
914
QCOMPARE(contact5, QContact());
917
void tst_QVersitContactImporter::testNickname()
920
QVersitDocument document(QVersitDocument::VCard30Type);
921
QVersitProperty nameProperty;
922
nameProperty.setName(QStringLiteral("NICKNAME"));
923
nameProperty.setValue(QStringList(QStringLiteral("Homie")));
924
nameProperty.setValueType(QVersitProperty::ListType);
925
document.addProperty(nameProperty);
926
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
927
QContact contact = mImporter->contacts().first();
928
QContactNickname nickName = (QContactNickname)contact.detail(QContactNickname::Type);
929
QCOMPARE(nickName.nickname(), QStringLiteral("Homie"));
931
// comma separated values should generate multiple nickname fields
932
contact.clearDetails();
934
document.setType(QVersitDocument::VCard30Type);
935
QStringList multiVal;
936
multiVal.append(QStringLiteral("Homie"));
937
multiVal.append(QStringLiteral("SuperHero"));
938
multiVal.append(QStringLiteral("NukeSpecialist"));
939
nameProperty.setName(QStringLiteral("NICKNAME"));
940
nameProperty.setValue(multiVal);
941
nameProperty.setValueType(QVersitProperty::ListType);
942
document.addProperty(nameProperty);
943
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
944
contact = mImporter->contacts().first();
945
QList<QContactNickname> nickNames = contact.details<QContactNickname>();
946
QCOMPARE(nickNames.count(),3);
947
nickName = nickNames[0];
948
QCOMPARE(nickName.nickname(),QStringLiteral("Homie"));
949
nickName = nickNames[1];
950
QCOMPARE(nickName.nickname(),QStringLiteral("SuperHero"));
951
nickName = nickNames[2];
952
QCOMPARE(nickName.nickname(),QStringLiteral("NukeSpecialist"));
956
document.setType(QVersitDocument::VCard30Type);
957
nameProperty = QVersitProperty();
958
nameProperty.setName(QStringLiteral("X-NICKNAME"));
959
nameProperty.setValue(QStringList(QStringLiteral("Homie")));
960
nameProperty.setValueType(QVersitProperty::ListType);
961
document.addProperty(nameProperty);
962
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
963
contact = mImporter->contacts().first();
964
nickName = contact.detail<QContactNickname>();
965
QCOMPARE(nickName.nickname(),QStringLiteral("Homie"));
967
// both X-NICKNAME and NICKNAME
968
nameProperty.setName(QStringLiteral("NICKNAME"));
969
document.addProperty(nameProperty);
970
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
971
contact = mImporter->contacts().first();
972
nickNames = contact.details<QContactNickname>();
973
QCOMPARE(nickNames.size(), 1);
974
QCOMPARE(nickNames.first().nickname(), QStringLiteral("Homie"));
977
void tst_QVersitContactImporter::testAvatarUrl()
979
QVersitProperty property;
980
property.setName(QStringLiteral("PHOTO"));
981
QString value(QStringLiteral("http://example.com/example.jpg"));
982
property.setValue(value);
983
property.insertParameter(QStringLiteral("VALUE"), QStringLiteral("URL"));
985
QVersitDocument document(QVersitDocument::VCard30Type);
986
document.addProperty(property);
987
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
988
QContact contact = mImporter->contacts().first();
989
QContactAvatar avatar = contact.detail<QContactAvatar>();
990
QCOMPARE(avatar.imageUrl(), QUrl(QStringLiteral("http://example.com/example.jpg")));
993
// A URL disguised inside a QByteArray.
995
document.setType(QVersitDocument::VCard30Type);
997
property.setName(QStringLiteral("PHOTO"));
998
property.setValue(QByteArray("http://example.com/example.jpg"));
999
property.insertParameter(QStringLiteral("VALUE"), QStringLiteral("URL"));
1000
property.insertParameter(QStringLiteral("CHARSET"), QStringLiteral("UTF-8"));
1001
document.addProperty(property);
1002
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
1003
contact = mImporter->contacts().first();
1004
avatar = contact.detail<QContactAvatar>();
1005
QCOMPARE(avatar.imageUrl(), QUrl(QStringLiteral("http://example.com/example.jpg")));
1008
void tst_QVersitContactImporter::testAvatarInvalid()
1010
// An avatar that's a QVersitDocument? It shouldn't work.
1011
QVersitDocument document(QVersitDocument::VCard30Type);
1012
QVersitProperty property;
1013
property.setName(QStringLiteral("PHOTO"));
1014
QVersitDocument nestedDocument;
1015
property.setValue(QVariant::fromValue(nestedDocument));
1016
property.insertParameter(QStringLiteral("VALUE"), QStringLiteral("URL"));
1017
document.addProperty(property);
1018
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
1019
QContact contact = mImporter->contacts().first();
1020
QCOMPARE(contact.details(QContactAvatar::Type).size(), 0);
1023
document.setType(QVersitDocument::VCard30Type);
1025
property.setName(QStringLiteral("PHOTO"));
1026
property.setValue(QVariant::fromValue(nestedDocument));
1027
document.addProperty(property);
1028
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
1029
contact = mImporter->contacts().first();
1030
QCOMPARE(contact.details(QContactAvatar::Type).size(), 0);
1033
void tst_QVersitContactImporter::testGeo()
1035
// some positive values
1036
QVersitDocument document(QVersitDocument::VCard30Type);
1037
QVersitProperty nameProperty;
1039
val.append(QStringLiteral("45.32")); // Latitude
1040
val.append(QStringLiteral("18.53"));// Longtitude
1041
nameProperty.setName(QStringLiteral("GEO"));
1042
nameProperty.setValue(val);
1043
nameProperty.setValueType(QVersitProperty::CompoundType);
1044
document.addProperty(nameProperty);
1045
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
1046
QContact contact = mImporter->contacts().first();
1047
QContactGeoLocation geo = (QContactGeoLocation)contact.detail(QContactGeoLocation::Type);
1049
str.setNum(geo.latitude(),'.',2);
1050
QCOMPARE(str,val[0]);
1051
str.setNum(geo.longitude(),'.',2);
1052
QCOMPARE(str,val[1]);
1054
// some negative values
1056
document.setType(QVersitDocument::VCard30Type);
1057
nameProperty = QVersitProperty();
1058
val.append(QStringLiteral("-45.32")); // Latitude
1059
val.append(QStringLiteral("18.53"));// Longtitude
1060
nameProperty.setName(QStringLiteral("GEO"));
1061
nameProperty.setValue(val);
1062
nameProperty.setValueType(QVersitProperty::CompoundType);
1063
document.addProperty(nameProperty);
1064
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
1065
contact = mImporter->contacts().first();
1066
geo = (QContactGeoLocation)contact.detail(QContactGeoLocation::Type);
1067
str.setNum(geo.latitude(),'.',2);
1068
QCOMPARE(str,val[0]);
1069
str.setNum(geo.longitude(),'.',2);
1070
QCOMPARE(str,val[1]);
1073
void tst_QVersitContactImporter::testNote()
1075
// single line value
1076
QVersitDocument document(QVersitDocument::VCard30Type);
1077
QVersitProperty nameProperty;
1078
QString val(QStringLiteral("I will not sleep at my work -John"));
1079
nameProperty.setName(QStringLiteral("NOTE"));
1080
nameProperty.setValue(val);
1081
document.addProperty(nameProperty);
1082
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
1083
QContact contact = mImporter->contacts().first();
1084
QContactNote note = (QContactNote)contact.detail(QContactNote::Type);
1085
QCOMPARE(note.note(),val);
1087
// Multiline value and quoted printable encoding
1089
document.setType(QVersitDocument::VCard30Type);
1090
nameProperty = QVersitProperty();
1091
val = QStringLiteral("My Dad acts like he belongs,=0D=0AHe belongs in the zoo.=0D=0A");
1092
nameProperty.setName(QStringLiteral("NOTE"));
1093
nameProperty.setValue(val);
1094
QMultiHash<QString,QString> params;
1095
params.insert(QStringLiteral("QUOTED-PRINTABLE"),val);
1096
nameProperty.setParameters(params);
1097
document.addProperty(nameProperty);
1098
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
1099
contact = mImporter->contacts().first();
1100
note = (QContactNote)contact.detail(QContactNote::Type);
1101
QCOMPARE(note.note(),val);
1104
void tst_QVersitContactImporter::testDisplayLabel()
1106
QVersitDocument document(QVersitDocument::VCard30Type);
1107
QVersitProperty fnProperty;
1108
fnProperty.setName(QStringLiteral("FN"));
1109
fnProperty.setValue(QStringLiteral("fn"));
1110
document.addProperty(fnProperty);
1111
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
1112
QContact contact = mImporter->contacts().first();
1113
contact = mImporter->contacts().first();
1114
QContactDisplayLabel displayLabel = contact.detail<QContactDisplayLabel>();
1115
QCOMPARE(displayLabel.label(), QStringLiteral("fn"));
1118
void tst_QVersitContactImporter::testOnlineAccount()
1120
QString accountUri(QStringLiteral("sip:john.citizen@example.com"));
1121
QVersitDocument document;
1123
// Plain X-SIP, no TYPE ->
1124
QVersitProperty property;
1125
property.setName(QStringLiteral("X-SIP"));
1126
property.setValue(accountUri);
1127
document = createDocumentWithProperty(property);
1128
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
1129
QContact contact = mImporter->contacts().first();
1130
QContactOnlineAccount onlineAccount = contact.detail<QContactOnlineAccount>();
1131
QCOMPARE(onlineAccount.accountUri(),accountUri);
1132
QList<int> subTypes = onlineAccount.subTypes();
1133
QCOMPARE(subTypes.count(),1);
1134
QVERIFY(subTypes.first() == QContactOnlineAccount::SubTypeSip);
1137
property = QVersitProperty();
1138
property.setName(QStringLiteral("X-SIP"));
1139
property.setValue(accountUri);
1140
QMultiHash<QString,QString> params;
1141
params.insert(QStringLiteral("TYPE"),QStringLiteral("SWIS"));
1142
property.setParameters(params);
1143
document = createDocumentWithProperty(property);
1144
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
1145
contact = mImporter->contacts().first();
1146
onlineAccount = contact.detail<QContactOnlineAccount>();
1147
QCOMPARE(onlineAccount.accountUri(),accountUri);
1148
subTypes = onlineAccount.subTypes();
1149
QCOMPARE(subTypes.count(),1);
1150
QVERIFY(subTypes.first() == QContactOnlineAccount::SubTypeVideoShare);
1153
property = QVersitProperty();
1154
property.setName(QStringLiteral("X-SIP"));
1155
property.setValue(accountUri);
1157
params.insert(QStringLiteral("TYPE"),QStringLiteral("VOIP"));
1158
property.setParameters(params);
1159
document = createDocumentWithProperty(property);
1160
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
1161
contact = mImporter->contacts().first();
1162
onlineAccount = contact.detail<QContactOnlineAccount>();
1163
QCOMPARE(onlineAccount.accountUri(),accountUri);
1164
subTypes = onlineAccount.subTypes();
1165
QCOMPARE(subTypes.count(),1);
1166
QVERIFY(subTypes.first() == QContactOnlineAccount::SubTypeSipVoip);
1169
property = QVersitProperty();
1170
property.setName(QStringLiteral("X-IMPP"));
1171
property.setValue(accountUri);
1172
document = createDocumentWithProperty(property);
1173
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
1174
contact = mImporter->contacts().first();
1175
onlineAccount = contact.detail<QContactOnlineAccount>();
1176
QCOMPARE(onlineAccount.accountUri(),accountUri);
1177
subTypes = onlineAccount.subTypes();
1178
QCOMPARE(subTypes.count(),1);
1179
QVERIFY(subTypes.first() == QContactOnlineAccount::SubTypeImpp);
1182
property = QVersitProperty();
1183
property.setName(QStringLiteral("IMPP"));
1184
property.setValue(accountUri);
1185
document = createDocumentWithProperty(property);
1186
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
1187
contact = mImporter->contacts().first();
1188
onlineAccount = contact.detail<QContactOnlineAccount>();
1189
QCOMPARE(onlineAccount.accountUri(),accountUri);
1190
subTypes = onlineAccount.subTypes();
1191
QCOMPARE(subTypes.count(),1);
1192
QVERIFY(subTypes.first() == QContactOnlineAccount::SubTypeImpp);
1195
property = QVersitProperty();
1196
property.setName(QStringLiteral("X-JABBER"));
1197
property.setValue(accountUri);
1198
document = createDocumentWithProperty(property);
1199
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
1200
contact = mImporter->contacts().first();
1201
onlineAccount = contact.detail<QContactOnlineAccount>();
1202
QCOMPARE(onlineAccount.accountUri(),accountUri);
1203
subTypes = onlineAccount.subTypes();
1204
QCOMPARE(subTypes.count(),1);
1205
QVERIFY(subTypes.first() == QContactOnlineAccount::SubTypeImpp);
1206
QVERIFY(onlineAccount.protocol() == QContactOnlineAccount::ProtocolJabber);
1208
document = QVersitDocument(QVersitDocument::VCard30Type);
1209
property.setName("X-AIM");
1210
property.setValue("a");
1211
document.addProperty(property);
1212
property.setName("X-ICQ");
1213
property.setValue("b");
1214
document.addProperty(property);
1215
property.setName("X-MSN");
1216
property.setValue("c");
1217
document.addProperty(property);
1218
property.setName("X-QQ");
1219
property.setValue("d");
1220
document.addProperty(property);
1221
property.setName("X-YAHOO");
1222
property.setValue("e");
1223
document.addProperty(property);
1224
property.setName("X-SKYPE");
1225
property.setValue("f");
1226
document.addProperty(property);
1227
property.setName("X-SKYPE-USERNAME");
1228
property.setValue("g");
1229
document.addProperty(property);
1230
property.setName("X-MS-IMADDRESS");
1231
property.setValue("h");
1232
document.addProperty(property);
1233
property.setName("X-KADDRESSBOOK-X-IMADDRESS");
1234
property.setValue("i");
1235
document.addProperty(property);
1236
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
1237
contact = mImporter->contacts().first();
1238
QList<QContactOnlineAccount> onlineAccounts = contact.details<QContactOnlineAccount>();
1239
QCOMPARE(onlineAccounts.size(), 9);
1240
QVERIFY(onlineAccounts[0].protocol() == QContactOnlineAccount::ProtocolAim);
1241
QVERIFY(onlineAccounts[0].accountUri() == "a");
1242
QVERIFY(onlineAccounts[1].protocol() == QContactOnlineAccount::ProtocolIcq);
1243
QVERIFY(onlineAccounts[1].accountUri() == "b");
1244
QVERIFY(onlineAccounts[2].protocol() == QContactOnlineAccount::ProtocolMsn);
1245
QVERIFY(onlineAccounts[2].accountUri() == "c");
1246
QVERIFY(onlineAccounts[3].protocol() == QContactOnlineAccount::ProtocolQq);
1247
QVERIFY(onlineAccounts[3].accountUri() == "d");
1248
QVERIFY(onlineAccounts[4].protocol() == QContactOnlineAccount::ProtocolYahoo);
1249
QVERIFY(onlineAccounts[4].accountUri() == "e");
1250
QVERIFY(onlineAccounts[5].protocol() == QContactOnlineAccount::ProtocolSkype);
1251
QVERIFY(onlineAccounts[5].accountUri() == "f");
1252
QVERIFY(onlineAccounts[6].protocol() == QContactOnlineAccount::ProtocolSkype);
1253
QVERIFY(onlineAccounts[6].accountUri() == "g");
1254
QVERIFY(onlineAccounts[7].protocol() == QContactOnlineAccount::ProtocolUnknown);
1255
QVERIFY(onlineAccounts[7].accountUri() == "h");
1256
QVERIFY(onlineAccounts[8].protocol() == QContactOnlineAccount::ProtocolUnknown);
1257
QVERIFY(onlineAccounts[8].accountUri() == "i");
1260
void tst_QVersitContactImporter::testFamily()
1262
// Interesting : kid but no wife :)
1263
QVersitDocument document(QVersitDocument::VCard30Type);
1264
QVersitProperty nameProperty;
1265
QString val(QStringLiteral("Jane")); // one is enough
1266
nameProperty.setName(QStringLiteral("X-CHILDREN"));
1267
nameProperty.setValue(QStringList(val));
1268
nameProperty.setValueType(QVersitProperty::ListType);
1269
document.addProperty(nameProperty);
1270
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
1271
QContact contact = mImporter->contacts().first();
1272
QContactFamily family = (QContactFamily)contact.detail(QContactFamily::Type);
1273
QStringList children = family.children();
1274
QCOMPARE(children.count(),1); // ensure no other kids in list
1275
QCOMPARE(family.spouse(),QString()); // make sure no wife
1276
QCOMPARE(children[0],val); // ensure it is your kid
1278
// Critical : wife but no kids , happy hours
1280
document.setType(QVersitDocument::VCard30Type);
1281
nameProperty = QVersitProperty();
1282
nameProperty.setName(QStringLiteral("X-SPOUSE"));
1283
val = QStringLiteral("Jenny");
1284
nameProperty.setValue(val);
1285
document.addProperty(nameProperty);
1286
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
1287
contact = mImporter->contacts().first();
1288
family = (QContactFamily)contact.detail(QContactFamily::Type);
1289
children = family.children();
1290
QCOMPARE(children.count(),0); // list should be empty as you know
1291
QCOMPARE(family.spouse(),val); // make sure thats your wife:(
1293
// Hopeless : couple of kids and wife
1295
document.setType(QVersitDocument::VCard30Type);
1297
nameProperty = QVersitProperty();
1298
nameProperty.setName(QStringLiteral("X-CHILDREN"));
1299
QStringList kidsVal;
1300
kidsVal.append(QStringLiteral("James"));
1301
kidsVal.append(QStringLiteral("Jake"));
1302
kidsVal.append(QStringLiteral("Jane"));
1303
nameProperty.setValue(kidsVal);
1304
nameProperty.setValueType(QVersitProperty::ListType);
1305
document.addProperty(nameProperty);
1307
val = QStringLiteral("Jenny");
1308
nameProperty = QVersitProperty();
1309
nameProperty.setName(QStringLiteral("X-SPOUSE"));
1310
nameProperty.setValue(val);
1311
document.addProperty(nameProperty);
1312
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
1313
contact = mImporter->contacts().first();
1314
family = (QContactFamily)contact.detail(QContactFamily::Type);
1315
children = family.children();
1316
QCOMPARE(children.count(),3); // too late , count them now.
1317
// painfull but ensure they are your kids
1318
QCOMPARE(children, kidsVal);
1319
QCOMPARE(family.spouse(),val); // make sure thats your wife:(
1322
void tst_QVersitContactImporter::testFavorite()
1324
QFETCH(QString, favoriteValue);
1325
QFETCH(QString, indexValue);
1326
QFETCH(bool, favoriteCreated);
1327
QVersitDocument document(QVersitDocument::VCard30Type);
1328
addFavoritePropertyToDocument(favoriteValue, indexValue, document);
1330
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
1332
QContact contact = mImporter->contacts().first();
1333
if (!favoriteCreated) {
1334
QCOMPARE(contact.details(QContactFavorite::Type).size(), 0);
1337
QContactFavorite favorite = (QContactFavorite)contact.detail(QContactFavorite::Type);
1338
QString actualFavoriteValue = favorite.isFavorite() ? QStringLiteral("true") : QStringLiteral("false");
1339
QCOMPARE(actualFavoriteValue, favoriteValue);
1340
QCOMPARE(QString::number(favorite.index()), indexValue);
1343
void tst_QVersitContactImporter::testFavorite_data()
1345
QTest::addColumn<QString>("favoriteValue");
1346
QTest::addColumn<QString>("indexValue");
1347
QTest::addColumn<bool>("favoriteCreated");
1350
QTest::newRow("favorite true") << QString("true") << QString("1") << true;
1351
QTest::newRow("favorite false") << QString("false") << QString("1") << true;
1352
QTest::newRow("favorite invalid") << QString("invalid") << QString("1") << false;
1353
QTest::newRow("favorite empty") << QString("") << QString("1") << false;
1354
QTest::newRow("index negative") << QString("true") << QString("-1") << true;
1355
QTest::newRow("index multiple digits") << QString("true") << QString("10") << true;
1356
QTest::newRow("index invalid") << QString("true") << QString("invalid") << false;
1357
QTest::newRow("index invalid mix") << QString("true") << QString("2letters") << false;
1358
QTest::newRow("index empty") << QString("true") << QString("") << false;
1362
void tst_QVersitContactImporter::testMultipleFavorites()
1364
QVersitDocument document(QVersitDocument::VCard30Type);
1365
addFavoritePropertyToDocument(QStringLiteral("true"), QStringLiteral("1"), document);
1366
addFavoritePropertyToDocument(QStringLiteral("false"), QStringLiteral("2"), document);
1368
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
1370
QContact contact = mImporter->contacts().first();
1371
QCOMPARE(contact.details(QContactFavorite::Type).size(), 1);
1372
QContactFavorite favorite = (QContactFavorite)contact.detail(QContactFavorite::Type);
1373
QCOMPARE(favorite.isFavorite(), true);
1374
QCOMPARE(favorite.index(), 1);
1377
void tst_QVersitContactImporter::addFavoritePropertyToDocument(QString favorite, QString index, QVersitDocument &document)
1379
QVersitProperty property;
1380
property.setName(QStringLiteral("X-QTPROJECT-FAVORITE"));
1382
value.append(favorite);
1383
value.append(index);
1384
property.setValue(value);
1385
property.setValueType(QVersitProperty::CompoundType);
1386
document.addProperty(property);
1389
void tst_QVersitContactImporter::testSound()
1391
// Test embedded sound file
1392
QVersitDocument document(QVersitDocument::VCard30Type);
1393
QVersitProperty soundProperty;
1394
QMultiHash<QString,QString> param;
1395
param.insert(QStringLiteral("TYPE"),QStringLiteral("WAVE"));
1396
soundProperty.setName(QStringLiteral("SOUND"));
1397
QByteArray val("111110000011111");
1398
soundProperty.setValue(val);
1399
soundProperty.setParameters(param);
1400
document.addProperty(soundProperty);
1401
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
1402
QContact contact = mImporter->contacts().first();
1403
QContactRingtone ringtone = contact.detail<QContactRingtone>();
1404
QByteArray content = mResourceHandler->mObjects.value(ringtone.audioRingtoneUrl());
1405
QCOMPARE(content, val);
1407
// Test sound file as URL
1409
soundProperty.clear();
1410
soundProperty.setName(QStringLiteral("SOUND"));
1411
QString soundUrl(QStringLiteral("http://qt.nokia.com/audioringtoneurl"));
1412
soundProperty.setValue(soundUrl);
1413
soundProperty.insertParameter(QStringLiteral("VALUE"),QStringLiteral("URL"));
1414
document = createDocumentWithProperty(soundProperty);
1415
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
1416
contact = mImporter->contacts().first();
1417
ringtone = contact.detail<QContactRingtone>();
1418
QCOMPARE(ringtone.audioRingtoneUrl().toString(), soundUrl);
1421
void tst_QVersitContactImporter::testTag()
1424
QVersitDocument document(QVersitDocument::VCard30Type);
1425
QVersitProperty tagProperty;
1426
tagProperty.setName(QStringLiteral("CATEGORIES"));
1427
tagProperty.setValue(QStringList(QStringLiteral("red")));
1428
tagProperty.setValueType(QVersitProperty::ListType);
1429
document.addProperty(tagProperty);
1430
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
1431
QContact contact = mImporter->contacts().first();
1432
QContactTag tagDetail = contact.detail<QContactTag>();
1433
QCOMPARE(tagDetail.tag(), QStringLiteral("red"));
1436
tagProperty.setName(QStringLiteral("CATEGORIES"));
1437
tagProperty.setValue(QStringList()
1438
<< QStringLiteral("red") // duplicate from previous property should be pruned
1439
<< QStringLiteral("green")
1440
<< QStringLiteral("blue")
1441
<< QStringLiteral("blue")); // duplicates should be pruned
1442
tagProperty.setValueType(QVersitProperty::ListType);
1443
document.addProperty(tagProperty);
1444
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
1445
contact = mImporter->contacts().first();
1446
QList<QContactTag> tagDetails = contact.details<QContactTag>();
1447
QCOMPARE(tagDetails.count(), 3);
1448
QCOMPARE(tagDetails.at(0).tag(), QStringLiteral("red"));
1449
QCOMPARE(tagDetails.at(1).tag(), QStringLiteral("green"));
1450
QCOMPARE(tagDetails.at(2).tag(), QStringLiteral("blue"));
1453
void tst_QVersitContactImporter::testExtendedDetail()
1455
QFETCH(QString, extendedDetailName);
1456
QFETCH(QVariant, extendedDetailData);
1457
QFETCH(QString, extendedDetailDataInProperty);
1458
QFETCH(bool, extendedDetailCreated);
1459
QVersitDocument document(QVersitDocument::VCard30Type);
1460
addExtendedDetailPropertyToDocument(extendedDetailName, extendedDetailDataInProperty, document);
1462
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
1464
QContact contact = mImporter->contacts().first();
1465
if (!extendedDetailCreated) {
1466
QCOMPARE(contact.details(QContactExtendedDetail::Type).size(), 0);
1469
QContactExtendedDetail extendedDetail = (QContactExtendedDetail)contact.detail(QContactExtendedDetail::Type);
1470
QCOMPARE(extendedDetail.name(), extendedDetailName);
1471
QCOMPARE(extendedDetail.data(), extendedDetailData);
1472
QCOMPARE(extendedDetail.data().type(), extendedDetailData.type());
1475
void tst_QVersitContactImporter::testExtendedDetail_data()
1477
QTest::addColumn<QString>("extendedDetailName");
1478
QTest::addColumn<QVariant>("extendedDetailData");
1479
QTest::addColumn<QString>("extendedDetailDataInProperty");
1480
QTest::addColumn<bool>("extendedDetailCreated");
1482
QString jsonArrayWith("[\n %1\n]\n");
1483
QString jsonArrayWithString = jsonArrayWith.arg("\"%1\"");
1485
QTest::newRow("string data")
1487
<< QVariant(QString("data"))
1488
<< jsonArrayWithString.arg("data")
1490
QTest::newRow("string data, empty")
1492
<< QVariant(QString(""))
1493
<< jsonArrayWithString.arg("")
1495
QTest::newRow("string data, containing reserved characters")
1497
<< QVariant(QString(",;:\\"))
1498
<< jsonArrayWithString.arg(",;:\\\\")
1503
QTest::newRow("double data")
1505
<< QVariant((double)2.0)
1506
<< jsonArrayWith.arg("2")
1508
QTest::newRow("double data, negative")
1510
<< QVariant((double)-1.0)
1511
<< jsonArrayWith.arg("-1")
1513
QTest::newRow("double data, multiple digits")
1515
<< QVariant((double)10.2)
1516
<< jsonArrayWith.arg("10.2")
1521
QTest::newRow("boolean data")
1524
<< jsonArrayWith.arg("true")
1529
QTest::newRow("datetime data, imported as a string")
1531
<< QVariant(QString("1997-07-16T19:20:30+01:00"))
1532
<< jsonArrayWithString.arg("1997-07-16T19:20:30+01:00")
1537
QTest::newRow("list data")
1539
<< QVariant(QVariantList() << QString("string 1") << QString("string 2"))
1540
<< QString("[\n [\n \"string 1\",\n \"string 2\"\n ]\n]\n")
1546
map["key 1"] = QString("string 1");
1547
map["key 2"] = QString("string 2");
1548
QTest::newRow("map data")
1551
<< QString("[\n {\n \"key 1\": \"string 1\",\n \"key 2\": \"string 2\"\n }\n]\n")
1557
map["key"] = QVariantList() << (double)1 << (double)2;
1558
QTest::newRow("map data, containing a nested list")
1561
<< QString("[\n {\n \"key\": [\n 1,\n 2\n ]\n }\n]\n")
1566
QTest::newRow("empty string as name")
1568
<< QVariant(QString("data"))
1569
<< jsonArrayWithString.arg("data")
1571
QTest::newRow("name containing reserved characters")
1573
<< QVariant(QString("data"))
1574
<< jsonArrayWithString.arg("data")
1579
QTest::newRow("null denoting an empty variant")
1582
<< jsonArrayWith.arg("null")
1584
QTest::newRow("compact json with extra whitespace removed")
1586
<< QVariant(QString("data"))
1587
<< QString("[\"data\"]")
1592
QTest::newRow("invalid property value: empty json array as property value")
1595
<< jsonArrayWith.arg("")
1597
QTest::newRow("invalid property value: invalid json value")
1600
<< jsonArrayWith.arg("invalid")
1605
void tst_QVersitContactImporter::addExtendedDetailPropertyToDocument(QString name, QString data, QVersitDocument &document)
1607
QVersitProperty property;
1608
property.setName(QStringLiteral("X-QTPROJECT-EXTENDED-DETAIL"));
1609
property.setValue(QStringList()
1612
property.setValueType(QVersitProperty::CompoundType);
1613
document.addProperty(property);
1616
void tst_QVersitContactImporter::testMultipleExtendedDetails()
1618
QVersitDocument document(QVersitDocument::VCard30Type);
1619
QString jsonArrayWith("[\n %1\n]\n");
1620
QString jsonArrayWithString = jsonArrayWith.arg("\"%1\"");
1621
addExtendedDetailPropertyToDocument("detailName1", jsonArrayWithString.arg("detailData1"), document);
1622
addExtendedDetailPropertyToDocument("detailName2", jsonArrayWithString.arg("detailData2"), document);
1624
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
1626
QContact contact = mImporter->contacts().first();
1627
QCOMPARE(contact.details(QContactDetail::TypeExtendedDetail).size(), 2);
1628
QList<QContactDetail> actualDetails = contact.details(QContactDetail::TypeExtendedDetail);
1629
QContactExtendedDetail extendedDetail1 = (QContactExtendedDetail)actualDetails.at(0);
1630
QContactExtendedDetail extendedDetail2 = (QContactExtendedDetail)actualDetails.at(1);
1631
QCOMPARE(extendedDetail1.name(), QString("detailName1"));
1632
QCOMPARE(extendedDetail1.data().toString(), QString("detailData1"));
1633
QCOMPARE(extendedDetail2.name(), QString("detailName2"));
1634
QCOMPARE(extendedDetail2.data().toString(), QString("detailData2"));
1637
void tst_QVersitContactImporter::testPref()
1639
QVersitDocument document(QVersitDocument::VCard30Type);
1640
QVersitProperty property1;
1641
property1.setName(QStringLiteral("TEL"));
1642
property1.setValue(QStringLiteral("1"));
1643
document.addProperty(property1);
1644
QVersitProperty property2;
1645
property2.setName(QStringLiteral("TEL"));
1646
property2.setValue(QStringLiteral("2"));
1647
property2.insertParameter(QStringLiteral("TYPE"), QStringLiteral("PREF"));
1648
document.addProperty(property2);
1649
QVersitProperty property3;
1650
property3.setName(QStringLiteral("TEL"));
1651
property3.setValue(QStringLiteral("3"));
1652
property3.insertParameter(QStringLiteral("TYPE"), QStringLiteral("PREF"));
1653
document.addProperty(property3);
1654
QVersitProperty property4;
1655
property4.setName(QStringLiteral("TEL"));
1656
property4.setValue(QStringLiteral("4"));
1657
document.addProperty(property4);
1659
// Test that pref details comes first.
1660
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
1661
QContact contact = mImporter->contacts().first();
1662
QContactPhoneNumber firstNumber = contact.detail<QContactPhoneNumber>();
1663
QCOMPARE(firstNumber.number(), QStringLiteral("2"));
1664
QList<QContactPhoneNumber> numbers = contact.details<QContactPhoneNumber>();
1665
QCOMPARE(numbers.at(0).number(), QStringLiteral("2"));
1666
QCOMPARE(numbers.at(1).number(), QStringLiteral("3"));
1667
QCOMPARE(numbers.at(2).number(), QStringLiteral("1"));
1668
QCOMPARE(numbers.at(3).number(), QStringLiteral("4"));
1671
void tst_QVersitContactImporter::testPropertyHandler()
1673
QVersitDocument document(QVersitDocument::VCard30Type);
1674
QVersitProperty property;
1676
// No unconverted properties, no converted properties either. Fails with EmptyDocumentError
1677
QVERIFY(!mImporter->importDocuments(QList<QVersitDocument>() << document));
1678
QCOMPARE(mPropertyHandler->mUnknownProperties.size(), 0);
1679
QCOMPARE(mPropertyHandler->mPreProcessedProperties.size(), 0);
1680
QCOMPARE(mPropertyHandler->mPostProcessedProperties.size(), 0);
1682
// No unconverted properties, one converted property
1683
mPropertyHandler->clear();
1684
property.setName(QStringLiteral("FN"));
1685
property.setValue(QStringLiteral("John Citizen"));
1686
document.addProperty(property);
1687
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
1688
QContact contact = mImporter->contacts().first();
1689
QCOMPARE(mPropertyHandler->mUnknownProperties.size(), 0);
1690
QCOMPARE(mPropertyHandler->mPreProcessedProperties.size(), 1);
1691
QCOMPARE(mPropertyHandler->mPostProcessedProperties.size(), 1);
1693
// Set the handler to override handling of the property
1694
mPropertyHandler->clear();
1695
mPropertyHandler->mPreProcess = true;
1697
document.setType(QVersitDocument::VCard30Type);
1698
property.setName(QStringLiteral("FN"));
1699
property.setValue(QStringLiteral("John Citizen"));
1700
document.addProperty(property);
1701
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
1702
contact = mImporter->contacts().first();
1703
QCOMPARE(mPropertyHandler->mUnknownProperties.size(), 0);
1704
QCOMPARE(mPropertyHandler->mPreProcessedProperties.size(), 1);
1705
QCOMPARE(mPropertyHandler->mPostProcessedProperties.size(), 0);
1706
QContactDetail nameDetail = contact.detail(QContactName::Type);
1707
QVERIFY(nameDetail.isEmpty());
1709
// One unknown property
1710
mPropertyHandler->clear();
1711
property.setName(QStringLiteral("X-EXTENSION-1"));
1712
property.setValue(QStringLiteral("extension value 1"));
1713
document.addProperty(property);
1714
mImporter->importDocuments(QList<QVersitDocument>() << document);
1715
QList<QVersitProperty> unknownProperties = mPropertyHandler->mUnknownProperties;
1716
QCOMPARE(unknownProperties.count(), 1);
1717
QCOMPARE(unknownProperties[0].name(), QStringLiteral("X-EXTENSION-1"));
1718
QCOMPARE(unknownProperties[0].value(), QStringLiteral("extension value 1"));
1720
// Two unknown properties
1721
mPropertyHandler->clear();
1722
property.setName(QStringLiteral("X-EXTENSION-2"));
1723
property.setValue(QStringLiteral("extension value 2"));
1724
document.addProperty(property);
1725
mImporter->importDocuments(QList<QVersitDocument>() << document);
1726
unknownProperties = mPropertyHandler->mUnknownProperties;
1727
QCOMPARE(unknownProperties.count(), 2);
1728
QCOMPARE(unknownProperties[0].name(), QStringLiteral("X-EXTENSION-1"));
1729
QCOMPARE(unknownProperties[0].value(), QStringLiteral("extension value 1"));
1730
QCOMPARE(unknownProperties[1].name(), QStringLiteral("X-EXTENSION-2"));
1731
QCOMPARE(unknownProperties[1].value(), QStringLiteral("extension value 2"));
1734
void tst_QVersitContactImporter::testInvalidDocument()
1736
// invalid document (invalid type)
1737
QList<QVersitDocument> documents;
1738
QVersitDocument document(QVersitDocument::InvalidType);
1739
QVersitProperty nameProperty;
1740
nameProperty.setName(QStringLiteral("FN"));
1741
nameProperty.setValue(QStringLiteral("John Citizen"));
1742
document.addProperty(nameProperty);
1743
documents.append(document);
1745
// valid document in the same list
1746
QVersitProperty telProperty;
1747
telProperty.setName(QStringLiteral("TEL"));
1748
telProperty.setValue(QStringLiteral("1234"));
1749
document.addProperty(telProperty);
1750
document.setType(QVersitDocument::VCard21Type);
1751
documents.append(document);
1752
QVERIFY(!mImporter->importDocuments(documents));
1753
QMap<int, QVersitContactImporter::Error> errorMap = mImporter->errorMap();
1754
QCOMPARE(errorMap.size(), 1);
1755
QVERIFY(errorMap.contains(0));
1756
QVERIFY(errorMap.value(0) == QVersitContactImporter::InvalidDocumentError);
1757
QList<QContact> contacts = mImporter->contacts();
1758
QCOMPARE(contacts.size(), 1);
1759
QContactPhoneNumber phoneDetail = contacts.first().detail<QContactPhoneNumber>();
1760
QCOMPARE(phoneDetail.number(), QStringLiteral("1234"));
1764
document.setType(QVersitDocument::VCard21Type);
1765
QVERIFY(!mImporter->importDocuments(QList<QVersitDocument>() << document));
1766
errorMap = mImporter->errorMap();
1767
QCOMPARE(errorMap.size(), 1);
1768
QCOMPARE(errorMap.value(0), QVersitContactImporter::EmptyDocumentError);
1769
QCOMPARE(mImporter->errors(), errorMap);
1773
void tst_QVersitContactImporter::testEmailWithContextOther()
1775
QVersitProperty property;
1776
property.setName(QStringLiteral("EMAIL"));
1777
QString value(QStringLiteral("john.citizen@example.com"));
1778
property.setValue(value);
1779
property.insertParameter(QStringLiteral("TYPE"),QStringLiteral("OTHER"));
1780
QVersitDocument document = createDocumentWithProperty(property);
1781
QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
1782
QContact contact = mImporter->contacts().first();
1783
QContactEmailAddress email = contact.detail<QContactEmailAddress>();
1784
QCOMPARE(email.emailAddress(),value);
1785
const QList<int> contexts = email.contexts();
1786
QCOMPARE(contexts.count(),1);
1787
QVERIFY(contexts.contains(QContactDetail::ContextOther));
1790
QVersitDocument tst_QVersitContactImporter::createDocumentWithProperty(
1791
const QVersitProperty& property)
1793
QVersitDocument document(QVersitDocument::VCard30Type);
1794
document.addProperty(property);
1798
QVersitDocument tst_QVersitContactImporter::createDocumentWithNameAndPhoto(
1799
const QString& name,
1801
const QString& imageType)
1803
QVersitDocument document(QVersitDocument::VCard30Type);
1805
QVersitProperty nameProperty;
1806
nameProperty.setName(QStringLiteral("FN"));
1807
nameProperty.setValue(name);
1808
document.addProperty(nameProperty);
1810
QVersitProperty property;
1811
property.setName(QStringLiteral("PHOTO"));
1812
property.setValue(image);
1813
if (imageType != QString()) {
1814
property.insertParameter(QStringLiteral("TYPE"), imageType);
1816
document.addProperty(property);
1821
QTEST_MAIN(tst_QVersitContactImporter)