~chris.gagnon/+junk/qtpim-coverage

« back to all changes in this revision

Viewing changes to tests/auto/versit/qversitcontactimporter/tst_qversitcontactimporter.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
//TESTED_COMPONENT=src/versit
 
43
 
 
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>
 
51
#include <QDir>
 
52
 
 
53
QTVERSIT_USE_NAMESPACE
 
54
class MyQVersitContactImporterPropertyHandler : public QVersitContactImporterPropertyHandler
 
55
{
 
56
public:
 
57
    MyQVersitContactImporterPropertyHandler()
 
58
        : mPreProcess(false)
 
59
    {
 
60
    }
 
61
 
 
62
    bool preProcessProperty(const QVersitDocument& document,
 
63
                            const QVersitProperty& property,
 
64
                            int contactIndex,
 
65
                            QContact* contact)
 
66
    {
 
67
        Q_UNUSED(document)
 
68
        Q_UNUSED(contact)
 
69
        Q_UNUSED(contactIndex);
 
70
        mPreProcessedProperties.append(property);
 
71
        return mPreProcess;
 
72
    }
 
73
 
 
74
    bool postProcessProperty(const QVersitDocument& document,
 
75
                             const QVersitProperty& property,
 
76
                             bool alreadyProcessed,
 
77
                             int contactIndex,
 
78
                             QContact* contact)
 
79
    {
 
80
        Q_UNUSED(document)
 
81
        Q_UNUSED(contact)
 
82
        Q_UNUSED(contactIndex)
 
83
        if (!alreadyProcessed)
 
84
            mUnknownProperties.append(property);
 
85
        else
 
86
            mPostProcessedProperties.append(property);
 
87
        return false;
 
88
    }
 
89
 
 
90
    void clear()
 
91
    {
 
92
        mPreProcess = false;
 
93
        mPropertyNamesToProcess.clear();
 
94
        mUnknownProperties.clear();
 
95
        mPreProcessedProperties.clear();
 
96
        mPostProcessedProperties.clear();
 
97
    }
 
98
 
 
99
    // a hook to control what preProcess returns:
 
100
    bool mPreProcess;
 
101
    QStringList mPropertyNamesToProcess;
 
102
    QList<QVersitProperty> mUnknownProperties;
 
103
    QList<QVersitProperty> mPreProcessedProperties;
 
104
    QList<QVersitProperty> mPostProcessedProperties;
 
105
};
 
106
 
 
107
class MyQVersitResourceHandler : public QVersitResourceHandler
 
108
{
 
109
public:
 
110
    MyQVersitResourceHandler() : mIndex(0)
 
111
    {
 
112
    }
 
113
 
 
114
    bool saveResource(const QByteArray& contents, const QVersitProperty& property,
 
115
                      QString* location)
 
116
    {
 
117
        Q_UNUSED(property);
 
118
        *location = QString::number(mIndex++);
 
119
        mObjects.insert(*location, contents);
 
120
        return true;
 
121
    }
 
122
 
 
123
    bool loadResource(const QString &location, QByteArray *contents, QString *mimeType)
 
124
    {
 
125
        Q_UNUSED(location)
 
126
        Q_UNUSED(contents)
 
127
        Q_UNUSED(mimeType)
 
128
        return false;
 
129
    }
 
130
 
 
131
    void clear()
 
132
    {
 
133
        mIndex = 0;
 
134
        mObjects.clear();
 
135
    }
 
136
 
 
137
    int mIndex;
 
138
    QMap<QUrl, QByteArray> mObjects;
 
139
};
 
140
 
 
141
const static QByteArray SAMPLE_GIF(QByteArray::fromBase64(
 
142
        "R0lGODlhEgASAIAAAAAAAP///yH5BAEAAAEALAAAAAASABIAAAIdjI+py+0G"
 
143
        "wEtxUmlPzRDnzYGfN3KBaKGT6rDmGxQAOw=="));
 
144
 
 
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="));
 
152
 
 
153
void tst_QVersitContactImporter::init()
 
154
{
 
155
    mImporter = new QVersitContactImporter();
 
156
    mResourceHandler = new MyQVersitResourceHandler();
 
157
    mImporter->setResourceHandler(mResourceHandler);
 
158
    mPropertyHandler = new MyQVersitContactImporterPropertyHandler();
 
159
    mImporter->setPropertyHandler(mPropertyHandler);
 
160
}
 
161
 
 
162
void tst_QVersitContactImporter::cleanup()
 
163
{
 
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;
 
171
    delete mImporter;
 
172
}
 
173
 
 
174
void tst_QVersitContactImporter::testName()
 
175
{
 
176
    QVersitDocument document(QVersitDocument::VCard30Type);
 
177
    QVersitProperty nameProperty;
 
178
    QStringList value;
 
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]);
 
196
 
 
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]);
 
220
}
 
221
 
 
222
// check that it doesn't crash if the FN property comes before the N property.
 
223
void tst_QVersitContactImporter::testNameWithFormatted()
 
224
{
 
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"));
 
243
    }
 
244
 
 
245
void tst_QVersitContactImporter::testAddress()
 
246
{
 
247
    QVersitDocument document(QVersitDocument::VCard30Type);
 
248
    QVersitProperty property;
 
249
    property.setName(QStringLiteral("ADR"));
 
250
    property.setValue(QStringList(QString()));
 
251
    property.setValueType(QVersitProperty::CompoundType);
 
252
 
 
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());
 
264
 
 
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")
 
274
                      );
 
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"));
 
286
 
 
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));
 
309
}
 
310
 
 
311
void tst_QVersitContactImporter::testAddressWithoutSubTypes()
 
312
{
 
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")
 
324
                      );
 
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>();
 
330
 
 
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"));
 
338
 
 
339
    QCOMPARE(address, otherAddress);
 
340
}
 
341
 
 
342
void tst_QVersitContactImporter::testOrganizationName()
 
343
{
 
344
    QVersitDocument document(QVersitDocument::VCard30Type);
 
345
    QVersitProperty property;
 
346
 
 
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);
 
355
 
 
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);
 
365
 
 
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"));
 
375
 
 
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"));
 
392
}
 
393
 
 
394
void tst_QVersitContactImporter::testOrganizationTitle()
 
395
{
 
396
    QVersitDocument document(QVersitDocument::VCard30Type);
 
397
    QVersitProperty property;
 
398
 
 
399
    // One title
 
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);
 
411
 
 
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);
 
427
 
 
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());
 
443
}
 
444
 
 
445
void tst_QVersitContactImporter::testOrganizationAssistant()
 
446
{
 
447
    QContact contact;
 
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);
 
458
}
 
459
 
 
460
void tst_QVersitContactImporter::testOrganizationLogo()
 
461
{
 
462
    QContact contact;
 
463
    QVersitDocument document(QVersitDocument::VCard30Type);
 
464
    QVersitProperty property;
 
465
 
 
466
    // Embedded LOGO
 
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);
 
479
 
 
480
    // LOGO as a URL
 
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);
 
490
}
 
491
 
 
492
void tst_QVersitContactImporter::testOrganizationRole()
 
493
{
 
494
    QContact contact;
 
495
    QVersitDocument document(QVersitDocument::VCard30Type);
 
496
    QVersitProperty property;
 
497
 
 
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);
 
507
}
 
508
 
 
509
void tst_QVersitContactImporter::testTel()
 
510
{
 
511
    QVersitDocument document(QVersitDocument::VCard30Type);
 
512
    QVersitProperty property;
 
513
    property.setName(QStringLiteral("TEL"));
 
514
    QString value(QStringLiteral("+35850987654321"));
 
515
    property.setValue(value);
 
516
 
 
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"));
 
528
 
 
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));
 
534
 
 
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));
 
546
 
 
547
    const QList<int> contexts = phone.contexts();
 
548
    QCOMPARE(contexts.count(),2);
 
549
    QVERIFY(contexts.contains(QContactDetail::ContextWork));
 
550
    QVERIFY(contexts.contains(QContactDetail::ContextHome));
 
551
}
 
552
 
 
553
void tst_QVersitContactImporter::testTelWithoutSubTypes()
 
554
{
 
555
    QVersitDocument document(QVersitDocument::VCard30Type);
 
556
    QVersitProperty property;
 
557
    property.setName(QStringLiteral("TEL"));
 
558
    QString value(QStringLiteral("+35850987654321"));
 
559
    property.setValue(value);
 
560
 
 
561
    document.addProperty(property);
 
562
    QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
 
563
    QContact contact = mImporter->contacts().first();
 
564
    const QContactPhoneNumber& phone = contact.detail<QContactPhoneNumber>();
 
565
 
 
566
    QContactPhoneNumber otherPhone;
 
567
    otherPhone.setNumber(QStringLiteral("+35850987654321"));
 
568
 
 
569
    QCOMPARE(otherPhone, phone);
 
570
}
 
571
 
 
572
void tst_QVersitContactImporter::testEmail()
 
573
{
 
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));
 
587
 
 
588
    QCOMPARE(mPropertyHandler->mUnknownProperties.size(), 0);
 
589
}
 
590
 
 
591
void tst_QVersitContactImporter::testUrl()
 
592
{
 
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));
 
606
}
 
607
 
 
608
void tst_QVersitContactImporter::testUid()
 
609
{
 
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);
 
619
}
 
620
 
 
621
void tst_QVersitContactImporter::testTimeStamp()
 
622
{
 
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);
 
633
 
 
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);
 
642
 
 
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);
 
653
 
 
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>();
 
661
 
 
662
    QCOMPARE(timeStamp.lastModified().toString(QStringLiteral("yyyyMMddThhmmss")),
 
663
             dateAndTimeValue);
 
664
 
 
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")),
 
674
             dateAndTimeValue);
 
675
    QCOMPARE(timeStamp.lastModified().timeSpec(),Qt::UTC);
 
676
}
 
677
 
 
678
void tst_QVersitContactImporter::testVersion()
 
679
{
 
680
    QFETCH(QString, sequenceNumber);
 
681
    QFETCH(QString, extendedVersion);
 
682
    QFETCH(bool, versionCreated);
 
683
 
 
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);
 
690
        return;
 
691
    }
 
692
    QContactVersion version = (QContactVersion)contact.detail(QContactVersion::Type);
 
693
    QCOMPARE(version.sequenceNumber(), sequenceNumber.toInt());
 
694
    QCOMPARE(QString(version.extendedVersion()), extendedVersion);
 
695
}
 
696
 
 
697
void tst_QVersitContactImporter::testVersion_data()
 
698
{
 
699
    QTest::addColumn<QString>("sequenceNumber");
 
700
    QTest::addColumn<QString>("extendedVersion");
 
701
    QTest::addColumn<bool>("versionCreated");
 
702
 
 
703
    {
 
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;
 
711
    }
 
712
}
 
713
 
 
714
void tst_QVersitContactImporter::testMultipleVersions()
 
715
{
 
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"));
 
725
}
 
726
 
 
727
void tst_QVersitContactImporter::addVersionPropertyToDocument(QString sequenceNumber, QString extendedVersion, QVersitDocument &document)
 
728
{
 
729
    QVersitProperty property;
 
730
    property.setName(QStringLiteral("X-QTPROJECT-VERSION"));
 
731
    QStringList value;
 
732
    value.append(sequenceNumber);
 
733
    value.append(extendedVersion);
 
734
    property.setValue(value);
 
735
    property.setValueType(QVersitProperty::CompoundType);
 
736
    document.addProperty(property);
 
737
}
 
738
 
 
739
void tst_QVersitContactImporter::testAnniversary()
 
740
{
 
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);
 
751
 
 
752
    // Evolution format
 
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);
 
761
 
 
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);
 
771
 
 
772
    // Date time tests
 
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);
 
782
 
 
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);
 
791
}
 
792
 
 
793
void tst_QVersitContactImporter::testBirthday()
 
794
{
 
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);
 
806
 
 
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);
 
816
 
 
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);
 
825
 
 
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);
 
834
 
 
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);
 
843
 
 
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);
 
852
 
 
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);
 
861
}
 
862
 
 
863
void tst_QVersitContactImporter::testGender()
 
864
{
 
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());
 
874
 
 
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);
 
885
 
 
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);
 
895
 
 
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);
 
905
 
 
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());
 
915
}
 
916
 
 
917
void tst_QVersitContactImporter::testNickname()
 
918
{
 
919
    // one value
 
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"));
 
930
 
 
931
    // comma separated values should generate multiple nickname fields
 
932
    contact.clearDetails();
 
933
    document.clear();
 
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"));
 
953
 
 
954
    // X-NICKNAME
 
955
    document.clear();
 
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"));
 
966
 
 
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"));
 
975
}
 
976
 
 
977
void tst_QVersitContactImporter::testAvatarUrl()
 
978
{
 
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"));
 
984
 
 
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")));
 
991
 
 
992
 
 
993
    // A URL disguised inside a QByteArray.
 
994
    document.clear();
 
995
    document.setType(QVersitDocument::VCard30Type);
 
996
    property.clear();
 
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")));
 
1006
}
 
1007
 
 
1008
void tst_QVersitContactImporter::testAvatarInvalid()
 
1009
{
 
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);
 
1021
 
 
1022
    document.clear();
 
1023
    document.setType(QVersitDocument::VCard30Type);
 
1024
    property.clear();
 
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);
 
1031
}
 
1032
 
 
1033
void tst_QVersitContactImporter::testGeo()
 
1034
{
 
1035
    // some positive values
 
1036
    QVersitDocument document(QVersitDocument::VCard30Type);
 
1037
    QVersitProperty nameProperty;
 
1038
    QStringList val;
 
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);
 
1048
    QString str;
 
1049
    str.setNum(geo.latitude(),'.',2);
 
1050
    QCOMPARE(str,val[0]);
 
1051
    str.setNum(geo.longitude(),'.',2);
 
1052
    QCOMPARE(str,val[1]);
 
1053
 
 
1054
    // some negative values
 
1055
    document.clear();
 
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]);
 
1071
}
 
1072
 
 
1073
void tst_QVersitContactImporter::testNote()
 
1074
{
 
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);
 
1086
 
 
1087
    // Multiline value and quoted printable encoding
 
1088
    document.clear();
 
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);
 
1102
}
 
1103
 
 
1104
void tst_QVersitContactImporter::testDisplayLabel()
 
1105
{
 
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"));
 
1116
}
 
1117
 
 
1118
void tst_QVersitContactImporter::testOnlineAccount()
 
1119
{
 
1120
    QString accountUri(QStringLiteral("sip:john.citizen@example.com"));
 
1121
    QVersitDocument document;
 
1122
 
 
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);
 
1135
 
 
1136
    // X-SIP;SWIS
 
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);
 
1151
 
 
1152
    // X-SIP;VOIP
 
1153
    property = QVersitProperty();
 
1154
    property.setName(QStringLiteral("X-SIP"));
 
1155
    property.setValue(accountUri);
 
1156
    params.clear();
 
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);
 
1167
 
 
1168
    // X-IMPP
 
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);
 
1180
 
 
1181
    // IMPP
 
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);
 
1193
 
 
1194
    // X-JABBER
 
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);
 
1207
 
 
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");
 
1258
}
 
1259
 
 
1260
void tst_QVersitContactImporter::testFamily()
 
1261
{
 
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
 
1277
 
 
1278
    // Critical : wife but no kids , happy hours
 
1279
    document.clear();
 
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:(
 
1292
 
 
1293
    // Hopeless : couple of kids and wife
 
1294
    document.clear();
 
1295
    document.setType(QVersitDocument::VCard30Type);
 
1296
    // Add kids first
 
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);
 
1306
    // Add wife next
 
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:(
 
1320
}
 
1321
 
 
1322
void tst_QVersitContactImporter::testFavorite()
 
1323
{
 
1324
    QFETCH(QString, favoriteValue);
 
1325
    QFETCH(QString, indexValue);
 
1326
    QFETCH(bool, favoriteCreated);
 
1327
    QVersitDocument document(QVersitDocument::VCard30Type);
 
1328
    addFavoritePropertyToDocument(favoriteValue, indexValue, document);
 
1329
 
 
1330
    QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
 
1331
 
 
1332
    QContact contact = mImporter->contacts().first();
 
1333
    if (!favoriteCreated) {
 
1334
        QCOMPARE(contact.details(QContactFavorite::Type).size(), 0);
 
1335
        return;
 
1336
    }
 
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);
 
1341
}
 
1342
 
 
1343
void tst_QVersitContactImporter::testFavorite_data()
 
1344
{
 
1345
    QTest::addColumn<QString>("favoriteValue");
 
1346
    QTest::addColumn<QString>("indexValue");
 
1347
    QTest::addColumn<bool>("favoriteCreated");
 
1348
 
 
1349
    {
 
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;
 
1359
    }
 
1360
}
 
1361
 
 
1362
void tst_QVersitContactImporter::testMultipleFavorites()
 
1363
{
 
1364
    QVersitDocument document(QVersitDocument::VCard30Type);
 
1365
    addFavoritePropertyToDocument(QStringLiteral("true"), QStringLiteral("1"), document);
 
1366
    addFavoritePropertyToDocument(QStringLiteral("false"), QStringLiteral("2"), document);
 
1367
 
 
1368
    QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
 
1369
 
 
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);
 
1375
}
 
1376
 
 
1377
void tst_QVersitContactImporter::addFavoritePropertyToDocument(QString favorite, QString index, QVersitDocument &document)
 
1378
{
 
1379
    QVersitProperty property;
 
1380
    property.setName(QStringLiteral("X-QTPROJECT-FAVORITE"));
 
1381
    QStringList value;
 
1382
    value.append(favorite);
 
1383
    value.append(index);
 
1384
    property.setValue(value);
 
1385
    property.setValueType(QVersitProperty::CompoundType);
 
1386
    document.addProperty(property);
 
1387
}
 
1388
 
 
1389
void tst_QVersitContactImporter::testSound()
 
1390
{
 
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);
 
1406
 
 
1407
    // Test sound file as URL
 
1408
    document.clear();
 
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);
 
1419
}
 
1420
 
 
1421
void tst_QVersitContactImporter::testTag()
 
1422
{
 
1423
    // one value
 
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"));
 
1434
 
 
1435
    // multiple values
 
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"));
 
1451
}
 
1452
 
 
1453
void tst_QVersitContactImporter::testExtendedDetail()
 
1454
{
 
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);
 
1461
 
 
1462
    QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
 
1463
 
 
1464
    QContact contact = mImporter->contacts().first();
 
1465
    if (!extendedDetailCreated) {
 
1466
        QCOMPARE(contact.details(QContactExtendedDetail::Type).size(), 0);
 
1467
        return;
 
1468
    }
 
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());
 
1473
}
 
1474
 
 
1475
void tst_QVersitContactImporter::testExtendedDetail_data()
 
1476
{
 
1477
    QTest::addColumn<QString>("extendedDetailName");
 
1478
    QTest::addColumn<QVariant>("extendedDetailData");
 
1479
    QTest::addColumn<QString>("extendedDetailDataInProperty");
 
1480
    QTest::addColumn<bool>("extendedDetailCreated");
 
1481
 
 
1482
    QString jsonArrayWith("[\n    %1\n]\n");
 
1483
    QString jsonArrayWithString = jsonArrayWith.arg("\"%1\"");
 
1484
    {
 
1485
        QTest::newRow("string data")
 
1486
                << QString("name")
 
1487
                << QVariant(QString("data"))
 
1488
                << jsonArrayWithString.arg("data")
 
1489
                << true;
 
1490
        QTest::newRow("string data, empty")
 
1491
                << QString("name")
 
1492
                << QVariant(QString(""))
 
1493
                << jsonArrayWithString.arg("")
 
1494
                << true;
 
1495
        QTest::newRow("string data, containing reserved characters")
 
1496
                << QString("name")
 
1497
                << QVariant(QString(",;:\\"))
 
1498
                << jsonArrayWithString.arg(",;:\\\\")
 
1499
                << true;
 
1500
    }
 
1501
 
 
1502
    {
 
1503
        QTest::newRow("double data")
 
1504
                << QString("name")
 
1505
                << QVariant((double)2.0)
 
1506
                << jsonArrayWith.arg("2")
 
1507
                << true;
 
1508
        QTest::newRow("double data, negative")
 
1509
                << QString("name")
 
1510
                << QVariant((double)-1.0)
 
1511
                << jsonArrayWith.arg("-1")
 
1512
                << true;
 
1513
        QTest::newRow("double data, multiple digits")
 
1514
                << QString("name")
 
1515
                << QVariant((double)10.2)
 
1516
                << jsonArrayWith.arg("10.2")
 
1517
                << true;
 
1518
    }
 
1519
 
 
1520
    {
 
1521
        QTest::newRow("boolean data")
 
1522
                << QString("name")
 
1523
                << QVariant(true)
 
1524
                << jsonArrayWith.arg("true")
 
1525
                << true;
 
1526
    }
 
1527
 
 
1528
    {
 
1529
        QTest::newRow("datetime data, imported as a string")
 
1530
                << QString("name")
 
1531
                << QVariant(QString("1997-07-16T19:20:30+01:00"))
 
1532
                << jsonArrayWithString.arg("1997-07-16T19:20:30+01:00")
 
1533
                << true;
 
1534
    }
 
1535
 
 
1536
    {
 
1537
        QTest::newRow("list data")
 
1538
                << QString("name")
 
1539
                << QVariant(QVariantList() << QString("string 1") << QString("string 2"))
 
1540
                << QString("[\n    [\n        \"string 1\",\n        \"string 2\"\n    ]\n]\n")
 
1541
                << true;
 
1542
    }
 
1543
 
 
1544
    {
 
1545
        QVariantMap map;
 
1546
        map["key 1"] = QString("string 1");
 
1547
        map["key 2"] = QString("string 2");
 
1548
        QTest::newRow("map data")
 
1549
                << QString("name")
 
1550
                << QVariant(map)
 
1551
                << QString("[\n    {\n        \"key 1\": \"string 1\",\n        \"key 2\": \"string 2\"\n    }\n]\n")
 
1552
                << true;
 
1553
    }
 
1554
 
 
1555
    {
 
1556
        QVariantMap map;
 
1557
        map["key"] = QVariantList() << (double)1 << (double)2;
 
1558
        QTest::newRow("map data, containing a nested list")
 
1559
                << QString("name")
 
1560
                << QVariant(map)
 
1561
                << QString("[\n    {\n        \"key\": [\n            1,\n            2\n        ]\n    }\n]\n")
 
1562
                << true;
 
1563
    }
 
1564
 
 
1565
    {
 
1566
        QTest::newRow("empty string as name")
 
1567
                << QString("")
 
1568
                << QVariant(QString("data"))
 
1569
                << jsonArrayWithString.arg("data")
 
1570
                << true;
 
1571
        QTest::newRow("name containing reserved characters")
 
1572
                << QString(",;:\\")
 
1573
                << QVariant(QString("data"))
 
1574
                << jsonArrayWithString.arg("data")
 
1575
                << true;
 
1576
    }
 
1577
 
 
1578
    {
 
1579
        QTest::newRow("null denoting an empty variant")
 
1580
                << QString("name")
 
1581
                << QVariant()
 
1582
                << jsonArrayWith.arg("null")
 
1583
                << true;
 
1584
        QTest::newRow("compact json with extra whitespace removed")
 
1585
                << QString("name")
 
1586
                << QVariant(QString("data"))
 
1587
                << QString("[\"data\"]")
 
1588
                << true;
 
1589
    }
 
1590
 
 
1591
    {
 
1592
        QTest::newRow("invalid property value: empty json array as property value")
 
1593
                << QString("name")
 
1594
                << QVariant()
 
1595
                << jsonArrayWith.arg("")
 
1596
                << false;
 
1597
        QTest::newRow("invalid property value: invalid json value")
 
1598
                << QString("name")
 
1599
                << QVariant()
 
1600
                << jsonArrayWith.arg("invalid")
 
1601
                << false;
 
1602
    }
 
1603
}
 
1604
 
 
1605
void tst_QVersitContactImporter::addExtendedDetailPropertyToDocument(QString name, QString data, QVersitDocument &document)
 
1606
{
 
1607
    QVersitProperty property;
 
1608
    property.setName(QStringLiteral("X-QTPROJECT-EXTENDED-DETAIL"));
 
1609
    property.setValue(QStringList()
 
1610
                      << name
 
1611
                      << data);
 
1612
    property.setValueType(QVersitProperty::CompoundType);
 
1613
    document.addProperty(property);
 
1614
}
 
1615
 
 
1616
void tst_QVersitContactImporter::testMultipleExtendedDetails()
 
1617
{
 
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);
 
1623
 
 
1624
    QVERIFY(mImporter->importDocuments(QList<QVersitDocument>() << document));
 
1625
 
 
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"));
 
1635
}
 
1636
 
 
1637
void tst_QVersitContactImporter::testPref()
 
1638
{
 
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);
 
1658
 
 
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"));
 
1669
}
 
1670
 
 
1671
void tst_QVersitContactImporter::testPropertyHandler()
 
1672
{
 
1673
    QVersitDocument document(QVersitDocument::VCard30Type);
 
1674
    QVersitProperty property;
 
1675
 
 
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);
 
1681
 
 
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);
 
1692
 
 
1693
    // Set the handler to override handling of the property
 
1694
    mPropertyHandler->clear();
 
1695
    mPropertyHandler->mPreProcess = true;
 
1696
    document.clear();
 
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());
 
1708
 
 
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"));
 
1719
 
 
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"));
 
1732
}
 
1733
 
 
1734
void tst_QVersitContactImporter::testInvalidDocument()
 
1735
{
 
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);
 
1744
 
 
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"));
 
1761
 
 
1762
    // empty document
 
1763
    document.clear();
 
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);
 
1770
 
 
1771
}
 
1772
 
 
1773
void tst_QVersitContactImporter::testEmailWithContextOther()
 
1774
{
 
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));
 
1788
}
 
1789
 
 
1790
QVersitDocument tst_QVersitContactImporter::createDocumentWithProperty(
 
1791
    const QVersitProperty& property)
 
1792
{
 
1793
    QVersitDocument document(QVersitDocument::VCard30Type);
 
1794
    document.addProperty(property);
 
1795
    return document;
 
1796
}
 
1797
 
 
1798
QVersitDocument tst_QVersitContactImporter::createDocumentWithNameAndPhoto(
 
1799
    const QString& name,
 
1800
    QByteArray image,
 
1801
    const QString& imageType)
 
1802
{
 
1803
    QVersitDocument document(QVersitDocument::VCard30Type);
 
1804
 
 
1805
    QVersitProperty nameProperty;
 
1806
    nameProperty.setName(QStringLiteral("FN"));
 
1807
    nameProperty.setValue(name);
 
1808
    document.addProperty(nameProperty);
 
1809
 
 
1810
    QVersitProperty property;
 
1811
    property.setName(QStringLiteral("PHOTO"));
 
1812
    property.setValue(image);
 
1813
    if (imageType != QString()) {
 
1814
        property.insertParameter(QStringLiteral("TYPE"), imageType);
 
1815
    }
 
1816
    document.addProperty(property);
 
1817
 
 
1818
    return document;
 
1819
}
 
1820
 
 
1821
QTEST_MAIN(tst_QVersitContactImporter)
 
1822