qtmobility/tests/auto/qversitcontactimporter/ut_qversitcontactimporter.cpp
changeset 4 90517678cc4f
parent 1 2b40d63a9c3d
child 5 453da2cfceef
equal deleted inserted replaced
1:2b40d63a9c3d 4:90517678cc4f
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt Mobility Components.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "qversitdefs_p.h"
       
    43 #include "ut_qversitcontactimporter.h"
       
    44 #include "qversitcontactimporter.h"
       
    45 #include "qversitcontactimporter_p.h"
       
    46 #include <qversitproperty.h>
       
    47 #include <qversitdocument.h>
       
    48 #include <QtTest/QtTest>
       
    49 #include <qcontact.h>
       
    50 #include <qcontactdetail.h>
       
    51 #include <qcontactname.h>
       
    52 #include <qcontactaddress.h>
       
    53 #include <qcontactphonenumber.h>
       
    54 #include <qcontactemailaddress.h>
       
    55 #include <qcontacturl.h>
       
    56 #include <qcontactguid.h>
       
    57 #include <qcontactorganization.h>
       
    58 #include <qcontacttimestamp.h>
       
    59 #include <qcontactanniversary.h>
       
    60 #include <qcontactbirthday.h>
       
    61 #include <qcontactgender.h>
       
    62 #include <qcontactnickname.h>
       
    63 #include <qcontactavatar.h>
       
    64 #include <qcontactgeolocation.h>
       
    65 #include <qcontactnote.h>
       
    66 #include <qcontactonlineaccount.h>
       
    67 #include <qcontactfamily.h>
       
    68 #include <QDir>
       
    69 
       
    70 QTM_BEGIN_NAMESPACE
       
    71 class MyQVersitContactImporterPropertyHandler : public QVersitContactImporterPropertyHandler
       
    72 {
       
    73 public:
       
    74     MyQVersitContactImporterPropertyHandler()
       
    75         : mPreProcess(false)
       
    76     {
       
    77     }
       
    78 
       
    79     bool preProcessProperty(const QVersitDocument& document,
       
    80                             const QVersitProperty& property,
       
    81                             int contactIndex,
       
    82                             QContact* contact)
       
    83     {
       
    84         Q_UNUSED(document)
       
    85         Q_UNUSED(contact)
       
    86         Q_UNUSED(contactIndex);
       
    87         mPreProcessedProperties.append(property);
       
    88         return mPreProcess;
       
    89     }
       
    90 
       
    91     bool postProcessProperty(const QVersitDocument& document,
       
    92                              const QVersitProperty& property,
       
    93                              bool alreadyProcessed,
       
    94                              int contactIndex,
       
    95                              QContact* contact)
       
    96     {
       
    97         Q_UNUSED(document)
       
    98         Q_UNUSED(contact)
       
    99         Q_UNUSED(contactIndex)
       
   100         if (!alreadyProcessed)
       
   101             mUnknownProperties.append(property);
       
   102         else
       
   103             mPostProcessedProperties.append(property);
       
   104         return false;
       
   105     }
       
   106 
       
   107     void clear()
       
   108     {
       
   109         mPreProcess = false;
       
   110         mPropertyNamesToProcess.clear();
       
   111         mUnknownProperties.clear();
       
   112         mPreProcessedProperties.clear();
       
   113         mPostProcessedProperties.clear();
       
   114     }
       
   115 
       
   116     // a hook to control what preProcess returns:
       
   117     bool mPreProcess;
       
   118     QStringList mPropertyNamesToProcess;
       
   119     QList<QVersitProperty> mUnknownProperties;
       
   120     QList<QVersitProperty> mPreProcessedProperties;
       
   121     QList<QVersitProperty> mPostProcessedProperties;
       
   122 };
       
   123 
       
   124 class MyQVersitResourceHandler : public QVersitResourceHandler
       
   125 {
       
   126 public:
       
   127     MyQVersitResourceHandler() : mIndex(0)
       
   128     {
       
   129     }
       
   130 
       
   131     bool saveResource(const QByteArray& contents, const QVersitProperty& property,
       
   132                       QString* location)
       
   133     {
       
   134         Q_UNUSED(property);
       
   135         *location = QString::number(mIndex++);
       
   136         mObjects.insert(*location, contents);
       
   137         return true;
       
   138     }
       
   139 
       
   140     bool loadResource(const QString &location, QByteArray *contents, QString *mimeType)
       
   141     {
       
   142         Q_UNUSED(location)
       
   143         Q_UNUSED(contents)
       
   144         Q_UNUSED(mimeType)
       
   145         return false;
       
   146     }
       
   147 
       
   148     void clear()
       
   149     {
       
   150         mIndex = 0;
       
   151         mObjects.clear();
       
   152     }
       
   153 
       
   154     int mIndex;
       
   155     QMap<QString, QByteArray> mObjects;
       
   156 };
       
   157 
       
   158 const static QByteArray SAMPLE_GIF(QByteArray::fromBase64(
       
   159         "R0lGODlhEgASAIAAAAAAAP///yH5BAEAAAEALAAAAAASABIAAAIdjI+py+0G"
       
   160         "wEtxUmlPzRDnzYGfN3KBaKGT6rDmGxQAOw=="));
       
   161 
       
   162 QTM_END_NAMESPACE
       
   163 
       
   164 QTM_USE_NAMESPACE
       
   165 
       
   166 void UT_QVersitContactImporter::init()
       
   167 {
       
   168     mImporter = new QVersitContactImporter();
       
   169     mResourceHandler = new MyQVersitResourceHandler();
       
   170     mImporter->setResourceHandler(mResourceHandler);
       
   171     mPropertyHandler = new MyQVersitContactImporterPropertyHandler();
       
   172     mImporter->setPropertyHandler(mPropertyHandler);
       
   173 }
       
   174 
       
   175 void UT_QVersitContactImporter::cleanup()
       
   176 {
       
   177     QVERIFY(mImporter->propertyHandler() == mPropertyHandler);
       
   178     mImporter->setPropertyHandler(0);
       
   179     delete mPropertyHandler;
       
   180     QVERIFY(mImporter->resourceHandler() == mResourceHandler);
       
   181     mImporter->setResourceHandler(0);
       
   182     delete mResourceHandler;
       
   183     delete mImporter;
       
   184 }
       
   185 
       
   186 void UT_QVersitContactImporter::testName()
       
   187 {
       
   188     QVersitDocument document;
       
   189     QVersitProperty nameProperty;
       
   190     QStringList value;
       
   191     value.append(QString::fromAscii("John"));//FirstName
       
   192     value.append(QString::fromAscii("Citizen"));//LastName
       
   193     value.append(QString::fromAscii("Anonymous"));//GivenName
       
   194     value.append(QString::fromAscii("Dr"));//PreFix
       
   195     value.append(QString::fromAscii("MSc"));//Suffix
       
   196     nameProperty.setName(QString::fromAscii("N"));
       
   197     nameProperty.setValue(value.join(QString::fromAscii(";")));
       
   198     document.addProperty(nameProperty);
       
   199     QList<QVersitDocument> documentList;
       
   200     documentList.append(document);
       
   201     QContact contact = mImporter->importContacts(documentList).first();
       
   202     QContactName name = (QContactName)contact.detail(QContactName::DefinitionName);
       
   203     QCOMPARE(name.lastName(),value[0]);
       
   204     QCOMPARE(name.firstName(),value[1]);
       
   205     QCOMPARE(name.middleName(),value[2]);
       
   206     QCOMPARE(name.prefix(),value[3]);
       
   207     QCOMPARE(name.suffix(),value[4]);
       
   208 
       
   209     // Multiple names, first one will be picked and rest will be discarded
       
   210     nameProperty = QVersitProperty();
       
   211     QStringList anotherValue;
       
   212     anotherValue.append(QString::fromAscii("FakeJohn"));//FirstName
       
   213     anotherValue.append(QString::fromAscii("FakeCitizen"));//LastName
       
   214     anotherValue.append(QString::fromAscii("FakeAnonymous"));//GivenName
       
   215     anotherValue.append(QString::fromAscii("FakeDr"));//PreFix
       
   216     anotherValue.append(QString::fromAscii("FakeMSc"));//Suffix
       
   217     nameProperty.setName(QString::fromAscii("N"));
       
   218     nameProperty.setValue(anotherValue.join(QString::fromAscii(";")));
       
   219     document.addProperty(nameProperty);
       
   220     documentList.clear();
       
   221     documentList.append(document);
       
   222     contact = mImporter->importContacts(documentList).first();
       
   223     QList<QContactDetail> names = contact.details(QContactName::DefinitionName);
       
   224     QCOMPARE(names.count(),1);
       
   225     // anotherValue should be discarded, so check for value
       
   226     name = (QContactName)names[0];
       
   227     QCOMPARE(name.lastName(),value[0]);
       
   228     QCOMPARE(name.firstName(),value[1]);
       
   229     QCOMPARE(name.middleName(),value[2]);
       
   230     QCOMPARE(name.prefix(),value[3]);
       
   231     QCOMPARE(name.suffix(),value[4]);
       
   232 }
       
   233 
       
   234 // check that it doesn't crash if the FN property comes before the N property.
       
   235 void UT_QVersitContactImporter::testNameWithFormatted()
       
   236 {
       
   237     QVersitDocument document;
       
   238     QVersitProperty fnProperty;
       
   239     fnProperty.setName(QString::fromAscii("FN"));
       
   240     fnProperty.setValue(QString::fromAscii("First Last"));
       
   241     document.addProperty(fnProperty);
       
   242     QVersitProperty nProperty;
       
   243     nProperty.setName(QString::fromAscii("N"));
       
   244     nProperty.setValue(QString::fromAscii("Last;First;Middle;Prefix;Suffix"));
       
   245     document.addProperty(nProperty);
       
   246     QList<QVersitDocument> documentList;
       
   247     documentList.append(document);
       
   248     QContact contact = mImporter->importContacts(documentList).first();
       
   249     QContactName name = contact.detail<QContactName>();
       
   250     QCOMPARE(name.firstName(), QString::fromAscii("First"));
       
   251     QCOMPARE(name.lastName(), QString::fromAscii("Last"));
       
   252     QCOMPARE(name.middleName(), QString::fromAscii("Middle"));
       
   253     QCOMPARE(name.prefix(), QString::fromAscii("Prefix"));
       
   254     QCOMPARE(name.suffix(), QString::fromAscii("Suffix"));
       
   255     QCOMPARE(name.customLabel(), QString::fromAscii("First Last"));
       
   256 }
       
   257 
       
   258 void UT_QVersitContactImporter::testAddress()
       
   259 {
       
   260     QVersitDocument document;
       
   261     QVersitProperty property;
       
   262     property.setName(QString::fromAscii("ADR"));
       
   263 
       
   264     // Empty value for the address
       
   265     document = createDocumentWithProperty(property);
       
   266     QList<QVersitDocument> documentList;
       
   267     documentList.append(document);
       
   268     QContact contact = mImporter->importContacts(documentList).first();
       
   269     QContactAddress address = contact.detail<QContactAddress>();
       
   270     QCOMPARE(address.postOfficeBox(),QString());
       
   271     QCOMPARE(address.street(),QString());
       
   272     QCOMPARE(address.locality(),QString());
       
   273     QCOMPARE(address.region(),QString());
       
   274     QCOMPARE(address.postcode(),QString());
       
   275     QCOMPARE(address.country(),QString());
       
   276 
       
   277     // Address with just seprators
       
   278     property.setValue(QString::fromAscii(";;;;;;"));
       
   279     document = createDocumentWithProperty(property);
       
   280     documentList.clear();
       
   281     documentList.append(document);
       
   282     contact = mImporter->importContacts(documentList).first();
       
   283     address = contact.detail<QContactAddress>();
       
   284     QCOMPARE(address.postOfficeBox(),QString());
       
   285     QCOMPARE(address.street(),QString());
       
   286     QCOMPARE(address.locality(),QString());
       
   287     QCOMPARE(address.region(),QString());
       
   288     QCOMPARE(address.postcode(),QString());
       
   289     QCOMPARE(address.country(),QString());
       
   290 
       
   291     // Address with some fields missing
       
   292     property.setValue(QString::fromAscii(";;My Street;My Town;;12345;"));
       
   293     document = createDocumentWithProperty(property);
       
   294     documentList.clear();
       
   295     documentList.append(document);
       
   296     contact = mImporter->importContacts(documentList).first();
       
   297     address = contact.detail<QContactAddress>();
       
   298     QCOMPARE(address.postOfficeBox(),QString());
       
   299     QCOMPARE(address.street(),QString::fromAscii("My Street"));
       
   300     QCOMPARE(address.locality(),QString::fromAscii("My Town"));
       
   301     QCOMPARE(address.region(),QString());
       
   302     QCOMPARE(address.postcode(),QString::fromAscii("12345"));
       
   303     QCOMPARE(address.country(),QString());
       
   304 
       
   305     // Address with all the fields filled
       
   306     property.setValue(QString::fromAscii("PO Box;E;My Street;My Town;My State;12345;My Country"));
       
   307     document = createDocumentWithProperty(property);
       
   308     documentList.clear();
       
   309     documentList.append(document);
       
   310     contact = mImporter->importContacts(documentList).first();
       
   311     address = contact.detail<QContactAddress>();
       
   312     QCOMPARE(address.postOfficeBox(),QString::fromAscii("PO Box"));
       
   313     QCOMPARE(address.street(),QString::fromAscii("My Street"));
       
   314     QCOMPARE(address.locality(),QString::fromAscii("My Town"));
       
   315     QCOMPARE(address.region(),QString::fromAscii("My State"));
       
   316     QCOMPARE(address.postcode(),QString::fromAscii("12345"));
       
   317     QCOMPARE(address.country(),QString::fromAscii("My Country"));
       
   318 
       
   319     // Address with TYPE parameters converted to contexts and subtypes
       
   320     property.insertParameter(QString::fromAscii("TYPE"),QString::fromAscii("HOME"));
       
   321     property.insertParameter(QString::fromAscii("TYPE"),QString::fromAscii("WORK"));
       
   322     property.insertParameter(QString::fromAscii("TYPE"),QString::fromAscii("DOM"));
       
   323     property.insertParameter(QString::fromAscii("TYPE"),QString::fromAscii("INTL"));
       
   324     property.insertParameter(QString::fromAscii("TYPE"),QString::fromAscii("POSTAL"));
       
   325     property.insertParameter(QString::fromAscii("TYPE"),QString::fromAscii("PARCEL"));
       
   326     property.insertParameter(QString::fromAscii("TYPE"),QString::fromAscii("X-EXTENSION"));
       
   327     document = createDocumentWithProperty(property);
       
   328     documentList.clear();
       
   329     documentList.append(document);
       
   330     contact = mImporter->importContacts(documentList).first();
       
   331     address = contact.detail<QContactAddress>();
       
   332     QStringList contexts = address.contexts();
       
   333     QVERIFY(contexts.contains(QContactDetail::ContextHome));
       
   334     QVERIFY(contexts.contains(QContactDetail::ContextWork));
       
   335     QStringList subTypes = address.subTypes();
       
   336     QVERIFY(subTypes.contains(QContactAddress::SubTypeDomestic));
       
   337     QVERIFY(subTypes.contains(QContactAddress::SubTypeInternational));
       
   338     QVERIFY(subTypes.contains(QContactAddress::SubTypePostal));
       
   339     QVERIFY(subTypes.contains(QContactAddress::SubTypeParcel));
       
   340 }
       
   341 
       
   342 void UT_QVersitContactImporter::testOrganizationName()
       
   343 {
       
   344     QVersitDocument document;
       
   345     QVersitProperty property;
       
   346 
       
   347     // Empty value for the organization
       
   348     property.setName(QString::fromAscii("ORG"));
       
   349     document = createDocumentWithProperty(property);
       
   350     QList<QVersitDocument> documentList;
       
   351     documentList.append(document);
       
   352     QContact contact = mImporter->importContacts(documentList).first();
       
   353     QContactOrganization organization = contact.detail<QContactOrganization>();
       
   354     QCOMPARE(organization.name(),QString());
       
   355     QCOMPARE(organization.department().count(),0);
       
   356 
       
   357     // Organization without separators
       
   358     property.setValue(QString::fromAscii("Nokia"));
       
   359     document = createDocumentWithProperty(property);
       
   360     documentList.clear();
       
   361     documentList.append(document);
       
   362     contact = mImporter->importContacts(documentList).first();
       
   363     organization = contact.detail<QContactOrganization>();
       
   364     QCOMPARE(organization.name(),QString::fromAscii("Nokia"));
       
   365     QCOMPARE(organization.department().count(),0);
       
   366 
       
   367     // Organization with one separator
       
   368     property.setValue(QString::fromAscii(";"));
       
   369     document = createDocumentWithProperty(property);
       
   370     documentList.clear();
       
   371     documentList.append(document);
       
   372     contact = mImporter->importContacts(documentList).first();
       
   373     organization = contact.detail<QContactOrganization>();
       
   374     QCOMPARE(organization.name(),QString::fromAscii(""));
       
   375     QCOMPARE(organization.department().count(),0);
       
   376 
       
   377     // Organization with just separators
       
   378     property.setValue(QString::fromAscii(";;;"));
       
   379     document = createDocumentWithProperty(property);
       
   380     documentList.clear();
       
   381     documentList.append(document);
       
   382     contact = mImporter->importContacts(documentList).first();
       
   383     organization = contact.detail<QContactOrganization>();
       
   384     QCOMPARE(organization.name(),QString::fromAscii(""));
       
   385     QCOMPARE(organization.department().count(),0);
       
   386 
       
   387     // Organization with one Organizational Unit
       
   388     property.setValue(QString::fromAscii("Nokia;R&D"));
       
   389     document = createDocumentWithProperty(property);
       
   390     documentList.clear();
       
   391     documentList.append(document);
       
   392     contact = mImporter->importContacts(documentList).first();
       
   393     organization = contact.detail<QContactOrganization>();
       
   394     QCOMPARE(organization.name(),QString::fromAscii("Nokia"));
       
   395     QCOMPARE(organization.department().count(),1);
       
   396     QCOMPARE(organization.department().at(0),QString::fromAscii("R&D"));
       
   397 
       
   398     // Organization with organization name and semicolon
       
   399     property.setValue(QString::fromAscii("Nokia;"));
       
   400     document = createDocumentWithProperty(property);
       
   401     documentList.clear();
       
   402     documentList.append(document);
       
   403     contact = mImporter->importContacts(documentList).first();
       
   404     organization = contact.detail<QContactOrganization>();
       
   405     QCOMPARE(organization.name(),QString::fromAscii("Nokia"));
       
   406     QCOMPARE(organization.department().count(),0);
       
   407 
       
   408     // Organization with semicolon and department
       
   409     property.setValue(QString::fromAscii(";R&D"));
       
   410     document = createDocumentWithProperty(property);
       
   411     documentList.clear();
       
   412     documentList.append(document);
       
   413     contact = mImporter->importContacts(documentList).first();
       
   414     organization = contact.detail<QContactOrganization>();
       
   415     QCOMPARE(organization.name(),QString());
       
   416     QCOMPARE(organization.department().count(),1);
       
   417     QCOMPARE(organization.department().at(0),QString::fromAscii("R&D"));
       
   418 
       
   419     // Organization with more Organizational Units
       
   420     property.setValue(QString::fromAscii("Nokia;R&D;Devices;Qt"));
       
   421     document = createDocumentWithProperty(property);
       
   422     documentList.clear();
       
   423     documentList.append(document);
       
   424     contact = mImporter->importContacts(documentList).first();
       
   425     organization = contact.detail<QContactOrganization>();
       
   426     QCOMPARE(organization.name(),QString::fromAscii("Nokia"));
       
   427     QCOMPARE(organization.department().count(),3);
       
   428     QCOMPARE(organization.department().at(0),QString::fromAscii("R&D"));
       
   429     QCOMPARE(organization.department().at(1),QString::fromAscii("Devices"));
       
   430     QCOMPARE(organization.department().at(2),QString::fromAscii("Qt"));
       
   431 }
       
   432 
       
   433 void UT_QVersitContactImporter::testOrganizationTitle()
       
   434 {
       
   435     QVersitDocument document;
       
   436     QVersitProperty property;
       
   437 
       
   438     // One title
       
   439     property.setName(QString::fromAscii("TITLE"));
       
   440     QString titleValue(QString::fromAscii("Developer"));
       
   441     property.setValue(titleValue);
       
   442     document = createDocumentWithProperty(property);
       
   443     QList<QVersitDocument> documentList;
       
   444     documentList.append(document);
       
   445     QContact contact = mImporter->importContacts(documentList).first();
       
   446     QList<QContactDetail> organizationDetails =
       
   447         contact.details(QContactOrganization::DefinitionName);
       
   448     QCOMPARE(organizationDetails.count(), 1);
       
   449     QContactOrganization organization = static_cast<QContactOrganization>(organizationDetails[0]);
       
   450     QCOMPARE(organization.title(),titleValue);
       
   451 
       
   452     // Two titles -> two QContactOrganizations created
       
   453     property.setName(QString::fromAscii("TITLE"));
       
   454     QString secondTitleValue(QString::fromAscii("Hacker"));
       
   455     property.setValue(secondTitleValue);
       
   456     document.addProperty(property);
       
   457     documentList.clear();
       
   458     documentList.append(document);
       
   459     contact = mImporter->importContacts(documentList).first();
       
   460     organizationDetails = contact.details(QContactOrganization::DefinitionName);
       
   461     QCOMPARE(organizationDetails.count(), 2);
       
   462     QContactOrganization firstOrganization =
       
   463         static_cast<QContactOrganization>(organizationDetails[0]);
       
   464     QCOMPARE(firstOrganization.title(),titleValue);
       
   465     QContactOrganization secondOrganization =
       
   466         static_cast<QContactOrganization>(organizationDetails[1]);
       
   467     QCOMPARE(secondOrganization.title(),secondTitleValue);
       
   468 
       
   469     // Two titles and one organization name -> two QContactOrganizations created
       
   470     property.setName(QString::fromAscii("ORG"));
       
   471     QString organizationName(QString::fromAscii("Nokia"));
       
   472     property.setValue(organizationName);
       
   473     document.addProperty(property);
       
   474     documentList.clear();
       
   475     documentList.append(document);
       
   476     contact = mImporter->importContacts(documentList).first();
       
   477     organizationDetails = contact.details(QContactOrganization::DefinitionName);
       
   478     QCOMPARE(organizationDetails.count(), 2);
       
   479     firstOrganization = static_cast<QContactOrganization>(organizationDetails[0]);
       
   480     QCOMPARE(firstOrganization.title(),titleValue);
       
   481     QCOMPARE(firstOrganization.name(),organizationName);
       
   482     secondOrganization = static_cast<QContactOrganization>(organizationDetails[1]);
       
   483     QCOMPARE(secondOrganization.title(),secondTitleValue);
       
   484     QCOMPARE(secondOrganization.name(),QString());
       
   485 }
       
   486 
       
   487 void UT_QVersitContactImporter::testOrganizationAssistant()
       
   488 {
       
   489     QContact contact;
       
   490     QVersitDocument document;
       
   491     QVersitProperty property;
       
   492     property.setName(QString::fromAscii("X-ASSISTANT"));
       
   493     QString assistantValue(QString::fromAscii("Jenny"));
       
   494     property.setValue(assistantValue);
       
   495     document = createDocumentWithProperty(property);
       
   496     QList<QVersitDocument> documentList;
       
   497     documentList.append(document);
       
   498     contact = mImporter->importContacts(documentList).first();
       
   499     QContactOrganization organization = contact.detail<QContactOrganization>();
       
   500     QCOMPARE(organization.assistantName(), assistantValue);
       
   501 }
       
   502 
       
   503 void UT_QVersitContactImporter::testOrganizationLogo()
       
   504 {
       
   505     QContact contact;
       
   506     QVersitDocument document;
       
   507     QVersitProperty property;
       
   508     QList<QVersitDocument> documentList;
       
   509 
       
   510     // Embedded LOGO
       
   511     property.setName(QString::fromAscii("LOGO"));
       
   512     QByteArray logo(QByteArray::fromBase64(
       
   513             "R0lGODlhEgASAIAAAAAAAP///yH5BAEAAAEALAAAAAASABIAAAIdjI+py+0G"));
       
   514     property.setValue(logo);
       
   515     property.insertParameter(QString::fromAscii("TYPE"),
       
   516                           QString::fromAscii("GIF"));
       
   517     document = createDocumentWithProperty(property);
       
   518     documentList.clear();
       
   519     documentList.append(document);
       
   520     contact = mImporter->importContacts(documentList).first();
       
   521     QContactOrganization organization = contact.detail<QContactOrganization>();
       
   522     QByteArray content = mResourceHandler->mObjects.value(organization.logo());
       
   523     QCOMPARE(content, logo);
       
   524 
       
   525     // LOGO as a URL
       
   526     property.setName(QString::fromAscii("LOGO"));
       
   527     QString logoUrl(QString::fromAscii("http://www.organization.org/logo.gif"));
       
   528     property.setValue(logoUrl);
       
   529     property.insertParameter(QString::fromAscii("VALUE"),QString::fromAscii("URL"));
       
   530     document = createDocumentWithProperty(property);
       
   531     documentList.clear();
       
   532     documentList.append(document);
       
   533     contact = mImporter->importContacts(documentList).first();
       
   534     organization = contact.detail<QContactOrganization>();
       
   535     QCOMPARE(organization.logo(),logoUrl);
       
   536 }
       
   537 
       
   538 void UT_QVersitContactImporter::testOrganizationRole()
       
   539 {
       
   540     QContact contact;
       
   541     QVersitDocument document;
       
   542     QVersitProperty property;
       
   543 
       
   544     // Setting the role is not yet supported by QContactOrganization
       
   545     property.setName(QString::fromAscii("ROLE"));
       
   546     QString roleValue(QString::fromAscii("Very important manager and proud of it"));
       
   547     property.setValue(roleValue);
       
   548     document = createDocumentWithProperty(property);
       
   549     QList<QVersitDocument> documentList;
       
   550     documentList.append(document);
       
   551     contact = mImporter->importContacts(documentList).first();
       
   552     QContactOrganization organization = contact.detail<QContactOrganization>();
       
   553     QCOMPARE(organization.role(), roleValue);
       
   554 }
       
   555 
       
   556 void UT_QVersitContactImporter::testTel()
       
   557 {
       
   558     QVersitDocument document;
       
   559     QVersitProperty property;
       
   560     property.setName(QString::fromAscii("TEL"));
       
   561     QString value(QString::fromAscii("+35850987654321"));
       
   562     property.setValue(value);
       
   563 
       
   564     property.insertParameter(QString::fromAscii("TYPE"),QString::fromAscii("VOICE"));
       
   565     property.insertParameter(QString::fromAscii("TYPE"),QString::fromAscii("CELL"));
       
   566     property.insertParameter(QString::fromAscii("TYPE"),QString::fromAscii("MODEM"));
       
   567     property.insertParameter(QString::fromAscii("TYPE"),QString::fromAscii("CAR"));
       
   568     property.insertParameter(QString::fromAscii("TYPE"),QString::fromAscii("VIDEO"));
       
   569     property.insertParameter(QString::fromAscii("TYPE"),QString::fromAscii("FAX"));
       
   570     property.insertParameter(QString::fromAscii("TYPE"),QString::fromAscii("BBS"));
       
   571     property.insertParameter(QString::fromAscii("TYPE"),QString::fromAscii("PAGER"));
       
   572     property.insertParameter(QString::fromAscii("TYPE"),QString::fromAscii("HOME"));
       
   573     property.insertParameter(QString::fromAscii("TYPE"),QString::fromAscii("WORK"));
       
   574 
       
   575     document.addProperty(property);
       
   576     QList<QVersitDocument> documentList;
       
   577     documentList.append(document);
       
   578     QContact contact = mImporter->importContacts(documentList).first();
       
   579     const QContactPhoneNumber& phone = contact.detail<QContactPhoneNumber>();
       
   580     QCOMPARE(phone.number(),QString(value));
       
   581 
       
   582     const QStringList subTypes = phone.subTypes();
       
   583     QCOMPARE(subTypes.count(),8);
       
   584     QVERIFY(subTypes.contains(QContactPhoneNumber::SubTypeVoice));
       
   585     QVERIFY(subTypes.contains(QContactPhoneNumber::SubTypeMobile));
       
   586     QVERIFY(subTypes.contains(QContactPhoneNumber::SubTypeModem));
       
   587     QVERIFY(subTypes.contains(QContactPhoneNumber::SubTypeCar));
       
   588     QVERIFY(subTypes.contains(QContactPhoneNumber::SubTypeVideo));
       
   589     QVERIFY(subTypes.contains(QContactPhoneNumber::SubTypeFacsimile));
       
   590     QVERIFY(subTypes.contains(QContactPhoneNumber::SubTypeBulletinBoardSystem));
       
   591     QVERIFY(subTypes.contains(QContactPhoneNumber::SubTypePager));
       
   592 
       
   593     const QStringList contexts = phone.contexts();
       
   594     QCOMPARE(contexts.count(),2);
       
   595     QVERIFY(contexts.contains(QContactDetail::ContextWork));
       
   596     QVERIFY(contexts.contains(QContactDetail::ContextHome));
       
   597 }
       
   598 
       
   599 void UT_QVersitContactImporter::testEmail()
       
   600 {
       
   601     QVersitProperty property;
       
   602     property.setName(QString::fromAscii("EMAIL"));
       
   603     QString value(QString::fromAscii("john.citizen@example.com"));
       
   604     property.setValue(value);
       
   605     property.insertParameter(QString::fromAscii("TYPE"),QString::fromAscii("WORK"));
       
   606     QVersitDocument document = createDocumentWithProperty(property);
       
   607     QList<QVersitDocument> documentList;
       
   608     documentList.append(document);
       
   609     QContact contact = mImporter->importContacts(documentList).first();
       
   610     QContactEmailAddress email = contact.detail<QContactEmailAddress>();
       
   611     QCOMPARE(email.emailAddress(),value);
       
   612     const QStringList contexts = email.contexts();
       
   613     QCOMPARE(contexts.count(),1);
       
   614     QVERIFY(contexts.contains(QContactDetail::ContextWork));
       
   615 
       
   616     QCOMPARE(mPropertyHandler->mUnknownProperties.size(), 0);
       
   617 }
       
   618 
       
   619 void UT_QVersitContactImporter::testUrl()
       
   620 {
       
   621     QVersitProperty property;
       
   622     property.setName(QString::fromAscii("URL"));
       
   623     QString value(QString::fromAscii("http://example.com"));
       
   624     property.setValue(value);
       
   625     property.insertParameter(QString::fromAscii("TYPE"),QString::fromAscii("WORK"));
       
   626     QVersitDocument document = createDocumentWithProperty(property);
       
   627     QList<QVersitDocument> documentList;
       
   628     documentList.append(document);
       
   629     QContact contact = mImporter->importContacts(documentList).first();
       
   630     QContactUrl url = contact.detail<QContactUrl>();
       
   631     QCOMPARE(url.url(),value);
       
   632     const QStringList contexts = url.contexts();
       
   633     QCOMPARE(contexts.count(),1);
       
   634     QVERIFY(contexts.contains(QContactDetail::ContextWork));
       
   635 }
       
   636 
       
   637 void UT_QVersitContactImporter::testUid()
       
   638 {
       
   639     QVersitProperty property;
       
   640     property.setName(QString::fromAscii("UID"));
       
   641     QString value(QString::fromAscii("unique identifier"));
       
   642     property.setValue(value);
       
   643     QVersitDocument document = createDocumentWithProperty(property);
       
   644     QList<QVersitDocument> documentList;
       
   645     documentList.append(document);
       
   646     QContact contact = mImporter->importContacts(documentList).first();
       
   647     QContactGuid uid = contact.detail<QContactGuid>();
       
   648     QCOMPARE(uid.guid(),value);
       
   649 }
       
   650 
       
   651 void UT_QVersitContactImporter::testTimeStamp()
       
   652 {
       
   653     // Simple date : ISO 8601 extended format
       
   654     QVersitProperty property;
       
   655     property.setName(QString::fromAscii("REV"));
       
   656     QString dateValue(QString::fromAscii("1981-05-20"));
       
   657     property.setValue(dateValue);
       
   658     QVersitDocument document = createDocumentWithProperty(property);
       
   659     QList<QVersitDocument> documentList;
       
   660     documentList.append(document);
       
   661     QContact contact = mImporter->importContacts(documentList).first();
       
   662     QContactTimestamp timeStamp = contact.detail<QContactTimestamp>();
       
   663     QCOMPARE(timeStamp.lastModified().date().toString(Qt::ISODate),dateValue);
       
   664 
       
   665     // Date and Time : ISO 8601 extended format without utc offset
       
   666     QString dateAndTimeValue(QString::fromAscii("1981-05-20T23:55:55"));
       
   667     property.setValue(dateAndTimeValue);
       
   668     document = createDocumentWithProperty(property);
       
   669     documentList.clear();
       
   670     documentList.append(document);
       
   671     contact = mImporter->importContacts(documentList).first();
       
   672     timeStamp = contact.detail<QContactTimestamp>();
       
   673     QCOMPARE(timeStamp.lastModified().toString(Qt::ISODate),dateAndTimeValue);
       
   674 
       
   675     // Date and Time : ISO 8601 extented format with utc offset
       
   676     QString utcOffset(QString::fromAscii("Z"));
       
   677     QString dateAndTimeWithUtcValue = dateAndTimeValue+utcOffset;
       
   678     property.setValue(dateAndTimeWithUtcValue);
       
   679     document = createDocumentWithProperty(property);
       
   680     documentList.clear();
       
   681     documentList.append(document);
       
   682     contact = mImporter->importContacts(documentList).first();
       
   683     timeStamp = contact.detail<QContactTimestamp>();
       
   684     QCOMPARE(timeStamp.lastModified().toString(Qt::ISODate),dateAndTimeValue);
       
   685     QCOMPARE(timeStamp.lastModified().timeSpec(),Qt::UTC);
       
   686 
       
   687     // Date and Time : ISO 8601 in basic format without utc offset
       
   688     dateAndTimeValue = QString::fromAscii("19810520T235555");
       
   689     property.setValue(dateAndTimeValue);
       
   690     document = createDocumentWithProperty(property);
       
   691     documentList.clear();
       
   692     documentList.append(document);
       
   693     contact = mImporter->importContacts(documentList).first();
       
   694     timeStamp = contact.detail<QContactTimestamp>();
       
   695 
       
   696     QCOMPARE(timeStamp.lastModified().toString(QString::fromAscii("yyyyMMddThhmmss")),
       
   697              dateAndTimeValue);
       
   698 
       
   699     // Date and Time : ISO 8601 in basic format with utc offset
       
   700     dateAndTimeValue = QString::fromAscii("19810520T235555");
       
   701     dateAndTimeWithUtcValue = dateAndTimeValue+utcOffset;
       
   702     property.setValue(dateAndTimeWithUtcValue);
       
   703     document = createDocumentWithProperty(property);
       
   704     documentList.clear();
       
   705     documentList.append(document);
       
   706     contact = mImporter->importContacts(documentList).first();
       
   707     timeStamp = contact.detail<QContactTimestamp>();
       
   708     QCOMPARE(timeStamp.lastModified().toString(QString::fromAscii("yyyyMMddThhmmss")),
       
   709              dateAndTimeValue);
       
   710     QCOMPARE(timeStamp.lastModified().timeSpec(),Qt::UTC);
       
   711 }
       
   712 
       
   713 void UT_QVersitContactImporter::testAnniversary()
       
   714 {
       
   715     // Date : ISO 8601 extended format
       
   716     QVersitProperty property;
       
   717     property.setName(QString::fromAscii("X-ANNIVERSARY"));
       
   718     QString dateValue(QString::fromAscii("1981-05-20"));
       
   719     property.setValue(dateValue);
       
   720     QVersitDocument document = createDocumentWithProperty(property);
       
   721     QList<QVersitDocument> documentList;
       
   722     documentList.append(document);
       
   723     QContact contact = mImporter->importContacts(documentList).first();
       
   724     QContactAnniversary anniversary = contact.detail<QContactAnniversary>();
       
   725     QCOMPARE(anniversary.originalDate().toString(Qt::ISODate),dateValue);
       
   726 
       
   727     // Date : ISO 8601 in basic format
       
   728     dateValue = QString::fromAscii("19810520");
       
   729     property.setValue(dateValue);
       
   730     document = createDocumentWithProperty(property);
       
   731     documentList.clear();
       
   732     documentList.append(document);
       
   733     contact = mImporter->importContacts(documentList).first();
       
   734     anniversary = contact.detail<QContactAnniversary>();
       
   735     QCOMPARE(anniversary.originalDate().toString(QString::fromAscii("yyyyMMdd")),
       
   736              dateValue);
       
   737 
       
   738 }
       
   739 
       
   740 void UT_QVersitContactImporter::testBirthday()
       
   741 {
       
   742     // Date : ISO 8601 extended format
       
   743     QVersitProperty property;
       
   744     property.setName(QString::fromAscii("BDAY"));
       
   745     QString dateValue(QString::fromAscii("1981-05-20"));
       
   746     property.setValue(dateValue);
       
   747     QVersitDocument document = createDocumentWithProperty(property);
       
   748     QList<QVersitDocument> documentList;
       
   749     documentList.append(document);
       
   750     QContact contact = mImporter->importContacts(documentList).first();
       
   751     QContactBirthday bday = contact.detail<QContactBirthday>();
       
   752     QCOMPARE(bday.date().toString(Qt::ISODate),
       
   753              dateValue);
       
   754 
       
   755     // Date : ISO 8601 in basic format
       
   756     dateValue = QString::fromAscii("19810520");
       
   757     property.setValue(dateValue);
       
   758     document = createDocumentWithProperty(property);
       
   759     documentList.clear();
       
   760     documentList.append(document);
       
   761     contact = mImporter->importContacts(documentList).first();
       
   762     bday = contact.detail<QContactBirthday>();
       
   763     QCOMPARE(bday.date().toString(QString::fromAscii("yyyyMMdd")),
       
   764              dateValue);
       
   765 
       
   766 }
       
   767 
       
   768 void UT_QVersitContactImporter::testGender()
       
   769 {
       
   770     // Date : ISO 8601 extended format
       
   771     QVersitProperty property;
       
   772     property.setName(QString::fromAscii("X-GENDER"));
       
   773     QString val(QString::fromAscii("Male"));
       
   774     property.setValue(val);
       
   775     QVersitDocument document = createDocumentWithProperty(property);
       
   776     QList<QVersitDocument> documentList;
       
   777     documentList.append(document);
       
   778     QContact contact = mImporter->importContacts(documentList).first();
       
   779     QContactGender  gender = contact.detail<QContactGender >();
       
   780     QCOMPARE(gender.gender(),val);
       
   781 }
       
   782 
       
   783 void UT_QVersitContactImporter::testNickname()
       
   784 {
       
   785     // one value
       
   786     QVersitDocument document;
       
   787     QVersitProperty nameProperty;
       
   788     QString singleVal(QString::fromAscii("Homie"));
       
   789     nameProperty.setName(QString::fromAscii("NICKNAME"));
       
   790     nameProperty.setValue(singleVal);
       
   791     document.addProperty(nameProperty);
       
   792     QList<QVersitDocument> documentList;
       
   793     documentList.append(document);
       
   794     QContact contact = mImporter->importContacts(documentList).first();
       
   795     QContactNickname nickName = (QContactNickname)contact.detail(QContactNickname::DefinitionName);
       
   796     QCOMPARE(nickName.nickname(),singleVal);
       
   797 
       
   798     // comma separated values should generate multiple nickname fields
       
   799     contact.clearDetails();
       
   800     document = QVersitDocument();
       
   801     QStringList multiVal;
       
   802     multiVal.append(QString::fromAscii("Homie"));
       
   803     multiVal.append(QString::fromAscii("SuperHero"));
       
   804     multiVal.append(QString::fromAscii("NukeSpecialist"));
       
   805     nameProperty.setName(QString::fromAscii("NICKNAME"));
       
   806     nameProperty.setValue(multiVal.join(QString::fromAscii(",")));
       
   807     document.addProperty(nameProperty);
       
   808     documentList.clear();
       
   809     documentList.append(document);
       
   810     contact = mImporter->importContacts(documentList).first();
       
   811     QList<QContactDetail> nickNames = contact.details(QContactNickname::DefinitionName);
       
   812     QCOMPARE(nickNames.count(),3);
       
   813     nickName = static_cast<QContactNickname>(nickNames[0]);
       
   814     QCOMPARE(nickName.nickname(),QString::fromAscii("Homie"));
       
   815     nickName = static_cast<QContactNickname>(nickNames[1]);
       
   816     QCOMPARE(nickName.nickname(),QString::fromAscii("SuperHero"));
       
   817     nickName = static_cast<QContactNickname>(nickNames[2]);
       
   818     QCOMPARE(nickName.nickname(),QString::fromAscii("NukeSpecialist"));
       
   819 
       
   820     // X-NICKNAME
       
   821     document = QVersitDocument();
       
   822     nameProperty = QVersitProperty();
       
   823     nameProperty.setName(QString::fromAscii("X-NICKNAME"));
       
   824     nameProperty.setValue(singleVal);
       
   825     document.addProperty(nameProperty);
       
   826     documentList.clear();
       
   827     documentList.append(document);
       
   828     contact = mImporter->importContacts(documentList).first();
       
   829     nickName = contact.detail<QContactNickname>();
       
   830     QCOMPARE(nickName.nickname(),singleVal);
       
   831 }
       
   832 
       
   833 void UT_QVersitContactImporter::testAvatarStored()
       
   834 {
       
   835     QByteArray gif(SAMPLE_GIF);
       
   836     QStringList nameValues(QString::fromAscii("John")); // First name
       
   837     nameValues.append(QString::fromAscii("Citizen")); // Last name
       
   838     QString name = nameValues.join(QString::fromAscii(";"));
       
   839     QVersitDocument document = createDocumentWithNameAndPhoto(name, gif, QLatin1String("GIF"));
       
   840     QList<QVersitDocument> documentList;
       
   841     documentList.append(document);
       
   842     QContact contact = mImporter->importContacts(documentList).first();
       
   843     QContactAvatar avatar = contact.detail<QContactAvatar>();
       
   844     QVERIFY(avatar.subType() == QContactAvatar::SubTypeImage);
       
   845     QByteArray content = mResourceHandler->mObjects.value(avatar.avatar());
       
   846     QCOMPARE(content, gif);
       
   847     QPixmap pixmap(avatar.pixmap());
       
   848     QPixmap expectedPixmap;
       
   849     expectedPixmap.loadFromData(gif);
       
   850     QEXPECT_FAIL("", "Pixmap creation disabled.  Will switch to QImage later.", Abort);
       
   851     QCOMPARE(pixmap, expectedPixmap);
       
   852 
       
   853     // Without the resource handler, the pixmap should still be set.
       
   854     mImporter->setResourceHandler(0);
       
   855     contact = mImporter->importContacts(documentList).first();
       
   856     avatar = contact.detail<QContactAvatar>();
       
   857     QVERIFY(avatar.subType() == QContactAvatar::SubTypeImage);
       
   858     QVERIFY(avatar.avatar().isEmpty());
       
   859     pixmap = avatar.pixmap();
       
   860     QCOMPARE(pixmap, expectedPixmap);
       
   861 
       
   862     // Empty photo.  The avatar should not be added to the QContact.
       
   863     QVersitProperty property;
       
   864     property.setName(QLatin1String("PHOTO"));
       
   865     property.setValue(QByteArray());
       
   866     document.clear();
       
   867     document.addProperty(property);
       
   868     documentList.clear();
       
   869     documentList.append(document);
       
   870     contact = mImporter->importContacts(documentList).first();
       
   871     QCOMPARE(contact.details(QContactAvatar::DefinitionName).size(), 0);
       
   872 
       
   873     mImporter->setResourceHandler(mResourceHandler);
       
   874 }
       
   875 
       
   876 void UT_QVersitContactImporter::testAvatarUrl()
       
   877 {
       
   878     QVersitProperty property;
       
   879     property.setName(QLatin1String("PHOTO"));
       
   880     QString value(QLatin1String("http://example.com/example.jpg"));
       
   881     property.setValue(value);
       
   882     property.insertParameter(QLatin1String("VALUE"), QLatin1String("URL"));
       
   883 
       
   884     QVersitDocument document;
       
   885     document.addProperty(property);
       
   886     QList<QVersitDocument> documentList;
       
   887     documentList.append(document);
       
   888 
       
   889     QContact contact = mImporter->importContacts(documentList).first();
       
   890     QContactAvatar avatar = contact.detail<QContactAvatar>();
       
   891     QCOMPARE(avatar.avatar(), QLatin1String("http://example.com/example.jpg"));
       
   892     QVERIFY(avatar.subType() == QContactAvatar::SubTypeImage);
       
   893 
       
   894 
       
   895     // A URL disguised inside a QByteArray.
       
   896     document.clear();
       
   897     property.clear();
       
   898     property.setName(QLatin1String("PHOTO"));
       
   899     property.setValue(QByteArray("http://example.com/example.jpg"));
       
   900     property.insertParameter(QLatin1String("VALUE"), QLatin1String("URL"));
       
   901     property.insertParameter(QLatin1String("CHARSET"), QLatin1String("UTF-8"));
       
   902     document.addProperty(property);
       
   903     documentList.clear();
       
   904     documentList.append(document);
       
   905     contact = mImporter->importContacts(documentList).first();
       
   906     avatar = contact.detail<QContactAvatar>();
       
   907     QCOMPARE(avatar.avatar(), QLatin1String("http://example.com/example.jpg"));
       
   908     QVERIFY(avatar.subType() == QContactAvatar::SubTypeImage);
       
   909 }
       
   910 
       
   911 void UT_QVersitContactImporter::testAvatarInvalid()
       
   912 {
       
   913     // An avatar that's a QVersitDocument?  It shouldn't work.
       
   914     QVersitDocument document;
       
   915     QVersitProperty property;
       
   916     property.setName(QLatin1String("PHOTO"));
       
   917     QVersitDocument nestedDocument;
       
   918     property.setValue(QVariant::fromValue(nestedDocument));
       
   919     property.insertParameter(QLatin1String("VALUE"), QLatin1String("URL"));
       
   920     document.addProperty(property);
       
   921     QList<QVersitDocument> list;
       
   922     list.append(document);
       
   923     QContact contact = mImporter->importContacts(list).first();
       
   924     QCOMPARE(contact.details(QContactAvatar::DefinitionName).size(), 0);
       
   925 
       
   926     document.clear();
       
   927     property.clear();
       
   928     list.clear();
       
   929     property.setName(QLatin1String("PHOTO"));
       
   930     property.setValue(QVariant::fromValue(nestedDocument));
       
   931     document.addProperty(property);
       
   932     list.append(document);
       
   933     contact = mImporter->importContacts(list).first();
       
   934     QCOMPARE(contact.details(QContactAvatar::DefinitionName).size(), 0);
       
   935 }
       
   936 
       
   937 void UT_QVersitContactImporter::testGeo()
       
   938 {
       
   939     // some positive values
       
   940     QVersitDocument document;
       
   941     QVersitProperty nameProperty;
       
   942     QStringList val;
       
   943     val.append(QString::fromAscii("18.53"));// Longtitude
       
   944     val.append(QString::fromAscii("45.32")); // Latitude
       
   945     nameProperty.setName(QString::fromAscii("GEO"));
       
   946     nameProperty.setValue(val.join(QString::fromAscii(",")));
       
   947     document.addProperty(nameProperty);
       
   948     QList<QVersitDocument> documentList;
       
   949     documentList.append(document);
       
   950     QContact contact = mImporter->importContacts(documentList).first();
       
   951     QContactGeoLocation geo = (QContactGeoLocation)contact.detail(QContactGeoLocation::DefinitionName);
       
   952     QString str;
       
   953     str.setNum(geo.longitude(),'.',2);
       
   954     QCOMPARE(str,val[0]);
       
   955     str.setNum(geo.latitude(),'.',2);
       
   956     QCOMPARE(str,val[1]);
       
   957 
       
   958     // some negative values
       
   959     document = QVersitDocument();
       
   960     nameProperty = QVersitProperty();
       
   961     val.append(QString::fromAscii("18.53"));// Longtitude
       
   962     val.append(QString::fromAscii("-45.32")); // Latitude
       
   963     nameProperty.setName(QString::fromAscii("GEO"));
       
   964     nameProperty.setValue(val.join(QString::fromAscii(",")));
       
   965     document.addProperty(nameProperty);
       
   966     documentList.clear();
       
   967     documentList.append(document);
       
   968     contact = mImporter->importContacts(documentList).first();
       
   969     geo = (QContactGeoLocation)contact.detail(QContactGeoLocation::DefinitionName);
       
   970     str.setNum(geo.longitude(),'.',2);
       
   971     QCOMPARE(str,val[0]);
       
   972     str.setNum(geo.latitude(),'.',2);
       
   973     QCOMPARE(str,val[1]);
       
   974 }
       
   975 
       
   976 void UT_QVersitContactImporter::testNote()
       
   977 {
       
   978     // single line value
       
   979     QVersitDocument document;
       
   980     QVersitProperty nameProperty;
       
   981     QString val(QString::fromAscii("I will not sleep at my work -John"));
       
   982     nameProperty.setName(QString::fromAscii("NOTE"));
       
   983     nameProperty.setValue(val);
       
   984     document.addProperty(nameProperty);
       
   985     QList<QVersitDocument> documentList;
       
   986     documentList.append(document);
       
   987     QContact contact = mImporter->importContacts(documentList).first();
       
   988     QContactNote note = (QContactNote)contact.detail(QContactNote::DefinitionName);
       
   989     QCOMPARE(note.note(),val);
       
   990 
       
   991     // Multiline value and quoted printable encoding
       
   992     document = QVersitDocument();
       
   993     nameProperty = QVersitProperty();
       
   994     val = QString::fromAscii("My Dad acts like he belongs,=0D=0AHe belongs in the zoo.=0D=0A");
       
   995     nameProperty.setName(QString::fromAscii("NOTE"));
       
   996     nameProperty.setValue(val);
       
   997     QMultiHash<QString,QString> params;
       
   998     params.insert(QString::fromAscii("QUOTED-PRINTABLE"),val);
       
   999     nameProperty.setParameters(params);
       
  1000     document.addProperty(nameProperty);
       
  1001     documentList.clear();
       
  1002     documentList.append(document);
       
  1003     contact = mImporter->importContacts(documentList).first();
       
  1004     note = (QContactNote)contact.detail(QContactNote::DefinitionName);
       
  1005     QCOMPARE(note.note(),val);
       
  1006 }
       
  1007 
       
  1008 void UT_QVersitContactImporter::testLabel()
       
  1009 {
       
  1010     QVersitDocument document;
       
  1011     QVersitProperty nameProperty;
       
  1012     QString val(QString::fromAscii("John Citizen"));
       
  1013     nameProperty.setName(QString::fromAscii("FN"));
       
  1014     nameProperty.setValue(val);
       
  1015     document.addProperty(nameProperty);
       
  1016     QList<QVersitDocument> documentList;
       
  1017     documentList.append(document);
       
  1018     QContact contact = mImporter->importContacts(documentList).first();
       
  1019     QContactName name =
       
  1020             (QContactName)contact.detail(QContactName::DefinitionName);
       
  1021     QCOMPARE(name.customLabel(),val);
       
  1022 }
       
  1023 
       
  1024 void UT_QVersitContactImporter::testOnlineAccount()
       
  1025 {
       
  1026     QString accountUri(QString::fromAscii("sip:john.citizen@example.com"));
       
  1027 
       
  1028     // Plain X-SIP, no TYPE ->
       
  1029     QVersitDocument document;
       
  1030     QVersitProperty property;
       
  1031     property.setName(QString::fromAscii("X-SIP"));
       
  1032     property.setValue(accountUri);
       
  1033     document.addProperty(property);
       
  1034     QList<QVersitDocument> documentList;
       
  1035     documentList.append(document);
       
  1036     QContact contact = mImporter->importContacts(documentList).first();
       
  1037     QContactOnlineAccount onlineAccount = contact.detail<QContactOnlineAccount>();
       
  1038     QCOMPARE(onlineAccount.accountUri(),accountUri);
       
  1039     QStringList subTypes = onlineAccount.subTypes();
       
  1040     QCOMPARE(subTypes.count(),1);
       
  1041     QVERIFY(subTypes.first() == QContactOnlineAccount::SubTypeSip);
       
  1042 
       
  1043     // X-SIP;SWIS
       
  1044     document = QVersitDocument();
       
  1045     property = QVersitProperty();
       
  1046     property.setName(QString::fromAscii("X-SIP"));
       
  1047     property.setValue(accountUri);
       
  1048     QMultiHash<QString,QString> params;
       
  1049     params.insert(QString::fromAscii("TYPE"),QString::fromAscii("SWIS"));
       
  1050     property.setParameters(params);
       
  1051     document.addProperty(property);
       
  1052     documentList.clear();
       
  1053     documentList.append(document);
       
  1054     contact = mImporter->importContacts(documentList).first();
       
  1055     onlineAccount =  contact.detail<QContactOnlineAccount>();
       
  1056     QCOMPARE(onlineAccount.accountUri(),accountUri);
       
  1057     subTypes = onlineAccount.subTypes();
       
  1058     QCOMPARE(subTypes.count(),1);
       
  1059     QVERIFY(subTypes.first() == QContactOnlineAccount::SubTypeVideoShare);
       
  1060 
       
  1061     // X-SIP;VOIP
       
  1062     document = QVersitDocument();
       
  1063     property = QVersitProperty();
       
  1064     property.setName(QString::fromAscii("X-SIP"));
       
  1065     property.setValue(accountUri);
       
  1066     params.clear();
       
  1067     params.insert(QString::fromAscii("TYPE"),QString::fromAscii("VOIP"));
       
  1068     property.setParameters(params);
       
  1069     document.addProperty(property);
       
  1070     documentList.clear();
       
  1071     documentList.append(document);
       
  1072     contact = mImporter->importContacts(documentList).first();
       
  1073     onlineAccount =  contact.detail<QContactOnlineAccount>();
       
  1074     QCOMPARE(onlineAccount.accountUri(),accountUri);
       
  1075     subTypes = onlineAccount.subTypes();
       
  1076     QCOMPARE(subTypes.count(),1);
       
  1077     QVERIFY(subTypes.first() == QContactOnlineAccount::SubTypeSipVoip);
       
  1078 
       
  1079     // X-IMPP
       
  1080     document = QVersitDocument();
       
  1081     property = QVersitProperty();
       
  1082     property.setName(QString::fromAscii("X-IMPP"));
       
  1083     property.setValue(accountUri);
       
  1084     document.addProperty(property);
       
  1085     documentList.clear();
       
  1086     documentList.append(document);
       
  1087     contact = mImporter->importContacts(documentList).first();
       
  1088     onlineAccount =  contact.detail<QContactOnlineAccount>();
       
  1089     QCOMPARE(onlineAccount.accountUri(),accountUri);
       
  1090     subTypes = onlineAccount.subTypes();
       
  1091     QCOMPARE(subTypes.count(),1);
       
  1092     QVERIFY(subTypes.first() == QContactOnlineAccount::SubTypeImpp);
       
  1093 
       
  1094     // IMPP
       
  1095     document = QVersitDocument();
       
  1096     property = QVersitProperty();
       
  1097     property.setName(QString::fromAscii("IMPP"));
       
  1098     property.setValue(accountUri);
       
  1099     document.addProperty(property);
       
  1100     documentList.clear();
       
  1101     documentList.append(document);
       
  1102     contact = mImporter->importContacts(documentList).first();
       
  1103     onlineAccount =  contact.detail<QContactOnlineAccount>();
       
  1104     QCOMPARE(onlineAccount.accountUri(),accountUri);
       
  1105     subTypes = onlineAccount.subTypes();
       
  1106     QCOMPARE(subTypes.count(),1);
       
  1107     QVERIFY(subTypes.first() == QContactOnlineAccount::SubTypeImpp);
       
  1108 
       
  1109     // X-JABBER
       
  1110     document = QVersitDocument();
       
  1111     property = QVersitProperty();
       
  1112     property.setName(QString::fromAscii("X-JABBER"));
       
  1113     property.setValue(accountUri);
       
  1114     document.addProperty(property);
       
  1115     documentList.clear();
       
  1116     documentList.append(document);
       
  1117     contact = mImporter->importContacts(documentList).first();
       
  1118     onlineAccount =  contact.detail<QContactOnlineAccount>();
       
  1119     QCOMPARE(onlineAccount.accountUri(),accountUri);
       
  1120     subTypes = onlineAccount.subTypes();
       
  1121     QCOMPARE(subTypes.count(),1);
       
  1122     QVERIFY(subTypes.first() == QContactOnlineAccount::SubTypeImpp);
       
  1123 }
       
  1124 
       
  1125 void UT_QVersitContactImporter::testFamily()
       
  1126 {
       
  1127     // Interesting : kid but no wife :)
       
  1128     QVersitDocument document;
       
  1129     QVersitProperty nameProperty;
       
  1130     QString val(QString::fromAscii("Jane")); // one is enough
       
  1131     nameProperty.setName(QString::fromAscii("X-CHILDREN"));
       
  1132     nameProperty.setValue(val);
       
  1133     document.addProperty(nameProperty);
       
  1134     QList<QVersitDocument> documentList;
       
  1135     documentList.append(document);
       
  1136     QContact contact = mImporter->importContacts(documentList).first();
       
  1137     QContactFamily family = (QContactFamily)contact.detail(QContactFamily::DefinitionName);
       
  1138     QStringList children = family.children();
       
  1139     QCOMPARE(children.count(),1); // ensure no other kids in list
       
  1140     QCOMPARE(family.spouse(),QString()); // make sure no wife
       
  1141     QCOMPARE(children[0],val); // ensure it is your kid
       
  1142 
       
  1143     // Critical : wife but no kids , happy hours
       
  1144     document = QVersitDocument();
       
  1145     nameProperty = QVersitProperty();
       
  1146     nameProperty.setName(QString::fromAscii("X-SPOUSE"));
       
  1147     val = QString::fromAscii("Jenny");
       
  1148     nameProperty.setValue(val);
       
  1149     document.addProperty(nameProperty);
       
  1150     documentList.clear();
       
  1151     documentList.append(document);
       
  1152     contact = mImporter->importContacts(documentList).first();
       
  1153     family = (QContactFamily)contact.detail(QContactFamily::DefinitionName);
       
  1154     children = family.children();
       
  1155     QCOMPARE(children.count(),0); // list should be empty as you know
       
  1156     QCOMPARE(family.spouse(),val); // make sure thats your wife:(
       
  1157 
       
  1158     // Hopeless : couple of kids and wife
       
  1159     document = QVersitDocument();
       
  1160     // Add kids first
       
  1161     nameProperty = QVersitProperty();
       
  1162     nameProperty.setName(QString::fromAscii("X-CHILDREN"));
       
  1163     QStringList kidsVal;
       
  1164     kidsVal.append(QString::fromAscii("James"));
       
  1165     kidsVal.append(QString::fromAscii("Jake"));
       
  1166     kidsVal.append(QString::fromAscii("Jane"));
       
  1167     nameProperty.setValue(kidsVal.join(QString::fromAscii(",")));
       
  1168     document.addProperty(nameProperty);
       
  1169     // Add wife next
       
  1170     val = QString::fromAscii("Jenny");
       
  1171     nameProperty = QVersitProperty();
       
  1172     nameProperty.setName(QString::fromAscii("X-SPOUSE"));
       
  1173     nameProperty.setValue(val);
       
  1174     document.addProperty(nameProperty);
       
  1175     documentList.clear();
       
  1176     documentList.append(document);
       
  1177     contact = mImporter->importContacts(documentList).first();
       
  1178     family = (QContactFamily)contact.detail(QContactFamily::DefinitionName);
       
  1179     children = family.children();
       
  1180     QCOMPARE(children.count(),3); // too late , count them now.
       
  1181     // painfull but ensure they are your kids
       
  1182     QCOMPARE(children.join(QString::fromAscii(",")),kidsVal.join(QString::fromAscii(",")));
       
  1183     QCOMPARE(family.spouse(),val); // make sure thats your wife:(
       
  1184 }
       
  1185 
       
  1186 void UT_QVersitContactImporter::testSound()
       
  1187 {
       
  1188     QVersitDocument document;
       
  1189     QVersitProperty nameProperty;
       
  1190     nameProperty.setName(QString::fromAscii("N"));
       
  1191     nameProperty.setValue(QString::fromAscii("Citizen;John;;;"));
       
  1192     document.addProperty(nameProperty);
       
  1193     nameProperty = QVersitProperty();
       
  1194     QVersitProperty soundProperty;
       
  1195     QMultiHash<QString,QString> param;
       
  1196     param.insert(QString::fromAscii("TYPE"),QString::fromAscii("WAVE"));
       
  1197     soundProperty.setName(QString::fromAscii("SOUND"));
       
  1198     QByteArray val("111110000011111");
       
  1199     soundProperty.setValue(val);
       
  1200     soundProperty.setParameters(param);
       
  1201     document.addProperty(soundProperty);
       
  1202     QList<QVersitDocument> documents;
       
  1203     documents.append(document);
       
  1204     QContact contact = mImporter->importContacts(documents).first();
       
  1205     QContactAvatar avatar = (QContactAvatar)contact.detail(QContactAvatar::DefinitionName);
       
  1206     QCOMPARE(avatar.value(QContactAvatar::FieldSubType),QContactAvatar::SubTypeAudioRingtone.operator QString());
       
  1207     QVERIFY(!avatar.hasValue(QContactAvatar::FieldAvatarPixmap));
       
  1208     QByteArray content = mResourceHandler->mObjects.value(avatar.avatar());
       
  1209     QCOMPARE(content, val);
       
  1210 }
       
  1211 
       
  1212 void UT_QVersitContactImporter::testPref()
       
  1213 {
       
  1214     QVersitDocument document;
       
  1215     QVersitProperty property1;
       
  1216     property1.setName(QLatin1String("TEL"));
       
  1217     property1.setValue(QLatin1String("1"));
       
  1218     document.addProperty(property1);
       
  1219     QVersitProperty property2;
       
  1220     property2.setName(QLatin1String("TEL"));
       
  1221     property2.setValue(QLatin1String("2"));
       
  1222     property2.insertParameter(QLatin1String("TYPE"), QLatin1String("PREF"));
       
  1223     document.addProperty(property2);
       
  1224     QVersitProperty property3;
       
  1225     property3.setName(QLatin1String("TEL"));
       
  1226     property3.setValue(QLatin1String("3"));
       
  1227     property3.insertParameter(QLatin1String("TYPE"), QLatin1String("PREF"));
       
  1228     document.addProperty(property3);
       
  1229     QVersitProperty property4;
       
  1230     property4.setName(QLatin1String("TEL"));
       
  1231     property4.setValue(QLatin1String("4"));
       
  1232     document.addProperty(property4);
       
  1233 
       
  1234     // Test that pref details comes first.
       
  1235     QList<QVersitDocument> documents;
       
  1236     documents.append(document);
       
  1237     QContact contact = mImporter->importContacts(documents).first();
       
  1238     QContactPhoneNumber firstNumber = contact.detail<QContactPhoneNumber>();
       
  1239     QCOMPARE(firstNumber.number(), QLatin1String("2"));
       
  1240     QList<QContactPhoneNumber> numbers = contact.details<QContactPhoneNumber>();
       
  1241     QCOMPARE(numbers.at(0).number(), QLatin1String("2"));
       
  1242     QCOMPARE(numbers.at(1).number(), QLatin1String("3"));
       
  1243     QCOMPARE(numbers.at(2).number(), QLatin1String("1"));
       
  1244     QCOMPARE(numbers.at(3).number(), QLatin1String("4"));
       
  1245 }
       
  1246 
       
  1247 void UT_QVersitContactImporter::testPropertyHandler()
       
  1248 {
       
  1249     QVersitDocument document;
       
  1250     QVersitProperty property;
       
  1251 
       
  1252     // No unconverted properties, no converted properties either
       
  1253     QList<QVersitDocument> documents;
       
  1254     documents.append(document);
       
  1255     mImporter->importContacts(documents);
       
  1256     QCOMPARE(mPropertyHandler->mUnknownProperties.size(), 0);
       
  1257     QCOMPARE(mPropertyHandler->mPreProcessedProperties.size(), 0);
       
  1258     QCOMPARE(mPropertyHandler->mPostProcessedProperties.size(), 0);
       
  1259 
       
  1260     // No unconverted properties, one converted property
       
  1261     mPropertyHandler->clear();
       
  1262     property.setName(QString::fromAscii("N"));
       
  1263     property.setValue(QString::fromAscii("Citizen;John;Q;;"));
       
  1264     document.addProperty(property);
       
  1265     documents.clear();
       
  1266     documents.append(document);
       
  1267     QContact contact = mImporter->importContacts(documents).first();
       
  1268     QCOMPARE(mPropertyHandler->mUnknownProperties.size(), 0);
       
  1269     QCOMPARE(mPropertyHandler->mPreProcessedProperties.size(), 1);
       
  1270     QCOMPARE(mPropertyHandler->mPostProcessedProperties.size(), 1);
       
  1271 
       
  1272     // Set the handler to override handling of the property
       
  1273     mPropertyHandler->clear();
       
  1274     mPropertyHandler->mPreProcess = true;
       
  1275     document = QVersitDocument();
       
  1276     property.setName(QString::fromAscii("N"));
       
  1277     property.setValue(QString::fromAscii("Citizen;John;Q;;"));
       
  1278     document.addProperty(property);
       
  1279     documents.clear();
       
  1280     documents.append(document);
       
  1281     contact = mImporter->importContacts(documents).first();
       
  1282     QCOMPARE(mPropertyHandler->mUnknownProperties.size(), 0);
       
  1283     QCOMPARE(mPropertyHandler->mPreProcessedProperties.size(), 1);
       
  1284     QCOMPARE(mPropertyHandler->mPostProcessedProperties.size(), 0);
       
  1285     QContactDetail nameDetail = contact.detail(QContactName::DefinitionName);
       
  1286     QVERIFY(nameDetail.isEmpty());
       
  1287 
       
  1288     // One unknown property
       
  1289     mPropertyHandler->clear();
       
  1290     property.setName(QString::fromAscii("X-EXTENSION-1"));
       
  1291     property.setValue(QString::fromAscii("extension value 1"));
       
  1292     document.addProperty(property);
       
  1293     documents.clear();
       
  1294     documents.append(document);
       
  1295     mImporter->importContacts(documents);
       
  1296     QList<QVersitProperty> unknownProperties = mPropertyHandler->mUnknownProperties;
       
  1297     QCOMPARE(unknownProperties.count(), 1);
       
  1298     QCOMPARE(unknownProperties[0].name(), QString::fromAscii("X-EXTENSION-1"));
       
  1299     QCOMPARE(unknownProperties[0].value(), QString::fromAscii("extension value 1"));
       
  1300 
       
  1301     // Two unknown properties
       
  1302     mPropertyHandler->clear();
       
  1303     property.setName(QString::fromAscii("X-EXTENSION-2"));
       
  1304     property.setValue(QString::fromAscii("extension value 2"));
       
  1305     document.addProperty(property);
       
  1306     documents.clear();
       
  1307     documents.append(document);
       
  1308     mImporter->importContacts(documents);
       
  1309     unknownProperties = mPropertyHandler->mUnknownProperties;
       
  1310     QCOMPARE(unknownProperties.count(), 2);
       
  1311     QCOMPARE(unknownProperties[0].name(), QString::fromAscii("X-EXTENSION-1"));
       
  1312     QCOMPARE(unknownProperties[0].value(), QString::fromAscii("extension value 1"));
       
  1313     QCOMPARE(unknownProperties[1].name(), QString::fromAscii("X-EXTENSION-2"));
       
  1314     QCOMPARE(unknownProperties[1].value(), QString::fromAscii("extension value 2"));
       
  1315 }
       
  1316 
       
  1317 QVersitDocument UT_QVersitContactImporter::createDocumentWithProperty(
       
  1318     const QVersitProperty& property)
       
  1319 {
       
  1320     QVersitDocument document;
       
  1321     document.addProperty(property);
       
  1322     return document;
       
  1323 }
       
  1324 
       
  1325 QVersitDocument UT_QVersitContactImporter::createDocumentWithNameAndPhoto(
       
  1326     const QString& name,
       
  1327     QByteArray image,
       
  1328     const QString& imageType)
       
  1329 {
       
  1330     QVersitDocument document;
       
  1331 
       
  1332     QVersitProperty nameProperty;
       
  1333     nameProperty.setName(QString::fromAscii("N"));
       
  1334     nameProperty.setValue(name);
       
  1335     document.addProperty(nameProperty);
       
  1336 
       
  1337     QVersitProperty property;
       
  1338     property.setName(QString::fromAscii("PHOTO"));
       
  1339     property.setValue(image);
       
  1340     if (imageType != QString()) {
       
  1341         property.insertParameter(QString::fromAscii("TYPE"), imageType);
       
  1342     }
       
  1343     document.addProperty(property);
       
  1344 
       
  1345     return document;
       
  1346 }
       
  1347 
       
  1348 QTEST_MAIN(UT_QVersitContactImporter)
       
  1349