qtmobility/tests/auto/qversitcontactexporter/tst_qversitcontactexporter.cpp
changeset 4 90517678cc4f
child 8 71781823f776
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 "tst_qversitcontactexporter.h"
       
    43 #include "qversitcontactexporter.h"
       
    44 #include "qversitcontactexporter_p.h"
       
    45 #include "qversitdocument.h"
       
    46 #include "qversitproperty.h"
       
    47 #include "qversitdefs_p.h"
       
    48 #include <QString>
       
    49 #include <QStringList>
       
    50 #include <QList>
       
    51 #include <QPixmap>
       
    52 #include <QImageWriter>
       
    53 #include <QtTest/QtTest>
       
    54 #include <qcontact.h>
       
    55 #include <qcontactaddress.h>
       
    56 #include <qcontactemailaddress.h>
       
    57 #include <qcontactname.h>
       
    58 #include <qcontactphonenumber.h>
       
    59 #include <qcontacturl.h>
       
    60 #include <qcontactguid.h>
       
    61 #include <qcontacttimestamp.h>
       
    62 #include <qcontactbirthday.h>
       
    63 #include <qcontactnote.h>
       
    64 #include <qcontactgeolocation.h>
       
    65 #include <qcontactorganization.h>
       
    66 #include <qcontactavatar.h>
       
    67 #include <qcontactgender.h>
       
    68 #include <qcontactnickname.h>
       
    69 #include <qcontactanniversary.h>
       
    70 #include <qcontactonlineaccount.h>
       
    71 #include <qcontactfamily.h>
       
    72 #include <qcontactdisplaylabel.h>
       
    73 
       
    74 QTM_BEGIN_NAMESPACE
       
    75 
       
    76 class MyQVersitContactExporterDetailHandler : public QVersitContactExporterDetailHandler
       
    77 {
       
    78 public:
       
    79     MyQVersitContactExporterDetailHandler() : mPreProcess(false)
       
    80     {
       
    81     }
       
    82 
       
    83     bool preProcessDetail(const QContact& contact,
       
    84                           const QContactDetail& detail,
       
    85                           QVersitDocument* document)
       
    86     {
       
    87         Q_UNUSED(contact)
       
    88         Q_UNUSED(document)
       
    89         mPreProcessedDetails.append(detail);
       
    90         return mPreProcess;
       
    91     }
       
    92 
       
    93     bool postProcessDetail(const QContact& contact,
       
    94                            const QContactDetail& detail,
       
    95                            bool alreadyProcessed,
       
    96                            QVersitDocument* document)
       
    97     {
       
    98         Q_UNUSED(contact)
       
    99         Q_UNUSED(document)
       
   100         if (!alreadyProcessed)
       
   101             mUnknownDetails.append(detail);
       
   102         else
       
   103             mPostProcessedDetails.append(detail);
       
   104         return false;
       
   105     }
       
   106 
       
   107     void clear()
       
   108     {
       
   109         mPreProcess = false;
       
   110         mDefinitionNamesToProcess.clear();
       
   111         mUnknownDetails.clear();
       
   112         mPreProcessedDetails.clear();
       
   113         mPostProcessedDetails.clear();
       
   114     }
       
   115 
       
   116     // a hook to control what preProcess returns:
       
   117     bool mPreProcess;
       
   118     QStringList mDefinitionNamesToProcess;
       
   119     QList<QContactDetail> mUnknownDetails;
       
   120     QList<QContactDetail> mPreProcessedDetails;
       
   121     QList<QContactDetail> mPostProcessedDetails;
       
   122 };
       
   123 
       
   124 class MyQVersitResourceHandler : public QVersitResourceHandler
       
   125 {
       
   126 public:
       
   127     MyQVersitResourceHandler()
       
   128         : mLoadResourceCalled(false),
       
   129         mLoadSuccess(true)
       
   130     {
       
   131     }
       
   132 
       
   133     bool loadResource(const QString& location, QByteArray* contents, QString* mimeType)
       
   134     {
       
   135         mLocation = location;
       
   136         *contents = mSimulatedData;
       
   137         *mimeType = mSimulatedMimeType;
       
   138         mLoadResourceCalled = true;
       
   139         return mLoadSuccess;
       
   140     }
       
   141 
       
   142     bool saveResource(const QByteArray &contents, const QVersitProperty &property, QString *location)
       
   143     {
       
   144         Q_UNUSED(contents)
       
   145         Q_UNUSED(property)
       
   146         Q_UNUSED(location)
       
   147         return false;
       
   148     }
       
   149 
       
   150     void clear()
       
   151     {
       
   152         mSimulatedData.clear();
       
   153         mSimulatedMimeType.clear();
       
   154         mLocation.clear();
       
   155         mLoadResourceCalled = false;
       
   156         mLoadSuccess = true;
       
   157     }
       
   158 
       
   159     QByteArray mSimulatedData;
       
   160     QString mSimulatedMimeType;
       
   161     QString mLocation;
       
   162     bool mLoadResourceCalled;
       
   163     bool mLoadSuccess; // A hook to control what loadResource returns.
       
   164 };
       
   165 
       
   166 const static QByteArray SAMPLE_GIF(QByteArray::fromBase64(
       
   167         "R0lGODlhEgASAIAAAAAAAP///yH5BAEAAAEALAAAAAASABIAAAIdjI+py+0G"
       
   168         "wEtxUmlPzRDnzYGfN3KBaKGT6rDmGxQAOw=="));
       
   169 
       
   170 // The number of vCard properties generated by a contact made with createContactWithName()
       
   171 const static int BASE_PROPERTY_COUNT = 2;
       
   172 
       
   173 QTM_END_NAMESPACE
       
   174 
       
   175 QTM_USE_NAMESPACE
       
   176 
       
   177 const QString TEST_PHOTO_FILE(QLatin1String("versitTest001.jpg"));
       
   178 const QString TEST_AUDIO_FILE(QLatin1String("versitTest001.wav"));
       
   179 
       
   180 // Checks that the property has a value of the given expectedType and the given expectedValue.
       
   181 #define CHECK_VALUE(property,expectedValueType,expectedValue) {\
       
   182     QCOMPARE(property.valueType(), expectedValueType); \
       
   183     QVariant value = property.variantValue(); \
       
   184     QCOMPARE(value.type(), QVariant::StringList); \
       
   185     QCOMPARE(value.toStringList(), expectedValue); \
       
   186 }
       
   187 
       
   188 void tst_QVersitContactExporter::init()
       
   189 {
       
   190     mExporter = new QVersitContactExporter();
       
   191     mDetailHandler = new MyQVersitContactExporterDetailHandler;
       
   192     mExporter->setDetailHandler(mDetailHandler);
       
   193     mResourceHandler = new MyQVersitResourceHandler;
       
   194     mExporter->setResourceHandler(mResourceHandler);
       
   195 }
       
   196 
       
   197 void tst_QVersitContactExporter::cleanup()
       
   198 {
       
   199     QVERIFY(mExporter->detailHandler() == mDetailHandler);
       
   200     mExporter->setDetailHandler(0);
       
   201     delete mDetailHandler;
       
   202     QVERIFY(mExporter->resourceHandler() == mResourceHandler);
       
   203     mExporter->setResourceHandler(0);
       
   204     delete mResourceHandler;
       
   205     delete mExporter;
       
   206 }
       
   207 
       
   208 void tst_QVersitContactExporter::testConvertContact()
       
   209 {
       
   210     QContact contact;
       
   211 
       
   212     // Adding name to the contact
       
   213     QContactName name;
       
   214     name.setFirstName(QString::fromAscii("Moido"));
       
   215     contact.saveDetail(&name);
       
   216 
       
   217     // Adding phone number to the Contact.
       
   218     QContactPhoneNumber phoneNumber;
       
   219     phoneNumber.setNumber(QString::fromAscii("12345678"));
       
   220     contact.saveDetail(&phoneNumber);
       
   221 
       
   222     // Convert contact into versit properties
       
   223     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard30Type));
       
   224     QList<QVersitDocument> documents = mExporter->documents();
       
   225 
       
   226     // Each Contact has display label detail by default. Display label is enocded
       
   227     // if some value exist for the Label or if value for Name exist.
       
   228     QCOMPARE(documents.size(), 1);
       
   229     QCOMPARE(documents.first().properties().count(), 3);
       
   230 }
       
   231 
       
   232 void tst_QVersitContactExporter::testContactDetailHandler()
       
   233 {
       
   234     // Test1: Un-supported Avatar Test
       
   235     QContact contact(createContactWithName(QLatin1String("asdf")));
       
   236     QVersitDocument document;
       
   237     QContactDetail unknownDetail;
       
   238     unknownDetail.setValue(QLatin1String("Unknown"), QLatin1String("Detail"));
       
   239     contact.saveDetail(&unknownDetail);
       
   240     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard30Type));
       
   241     document = mExporter->documents().first();
       
   242     QCOMPARE(document.properties().count(), BASE_PROPERTY_COUNT);
       
   243     QList<QContactDetail> unknownDetails = mDetailHandler->mUnknownDetails;
       
   244     QVERIFY(unknownDetails.size() > 0);
       
   245     QString definitionName = unknownDetail.definitionName();
       
   246     QContactDetail detail = searchDetail(unknownDetails,definitionName);
       
   247     QCOMPARE(definitionName, detail.definitionName());
       
   248 
       
   249     // Test2: Un-supported Online Account
       
   250     QContactOnlineAccount onlineAccount;
       
   251     QString testUri = QString::fromAscii("sip:abc@temp.com");
       
   252     onlineAccount.setAccountUri(testUri);
       
   253     onlineAccount.setSubTypes(QString::fromAscii("unsupported"));
       
   254     contact.saveDetail(&onlineAccount);
       
   255     mDetailHandler->clear();
       
   256     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard30Type));
       
   257     document = mExporter->documents().first();
       
   258     QCOMPARE(document.properties().count(), BASE_PROPERTY_COUNT);
       
   259     unknownDetails = mDetailHandler->mUnknownDetails;
       
   260     QVERIFY(unknownDetails.size() > 0);
       
   261     definitionName = onlineAccount.definitionName();
       
   262     detail = searchDetail(unknownDetails, definitionName);
       
   263     QCOMPARE(definitionName, detail.definitionName());
       
   264 
       
   265     // Test that preProcessDetail returns true stops the exporter from doing anything.
       
   266     contact.clearDetails();
       
   267     QContactName contactName;
       
   268     contactName.setFirstName(QLatin1String("John"));
       
   269     contact.saveDetail(&contactName);
       
   270     mDetailHandler->clear();
       
   271     mDetailHandler->mPreProcess = true;
       
   272     // Fails, with NoNameError
       
   273     QVERIFY(!mExporter->exportContacts(QList<QContact>() << contact,
       
   274             QVersitDocument::VCard30Type));
       
   275     QList<QVersitDocument> documents = mExporter->documents();
       
   276     QCOMPARE(documents.size(), 0);
       
   277     QVERIFY(mDetailHandler->mPreProcessedDetails.count() > BASE_PROPERTY_COUNT);
       
   278     QCOMPARE(mDetailHandler->mPostProcessedDetails.count(), 0);
       
   279     QCOMPARE(mDetailHandler->mUnknownDetails.count(), 0);
       
   280 
       
   281     QVERIFY(mExporter->detailHandler() == mDetailHandler);
       
   282 }
       
   283 
       
   284 void tst_QVersitContactExporter::testEncodeName()
       
   285 {
       
   286     QContact contact;
       
   287     QContactName name;
       
   288 
       
   289     // Special characters are NOT backslash escaped by the exporter, only by the writer.
       
   290     name.setFirstName(QString::fromAscii("He;ido"));
       
   291     name.setLastName(QString::fromAscii("HH"));
       
   292     name.setMiddleName(QString::fromAscii("A"));
       
   293     name.setPrefix(QString::fromAscii("Mr."));
       
   294     name.setContexts(QContactDetail::ContextHome);
       
   295     contact.saveDetail(&name);
       
   296     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard21Type));
       
   297     QVersitDocument document = mExporter->documents().first();
       
   298 
       
   299     // Each Contact has display label detail by default. Display label is enocded
       
   300     // if some value exists for the Label or if value for Name exists.
       
   301     QCOMPARE(document.properties().count(), 2);
       
   302 
       
   303     QVersitProperty displayProperty = document.properties().at(0);
       
   304     // Check name
       
   305     QCOMPARE(displayProperty.name(), QString::fromAscii("FN"));
       
   306     // Check value
       
   307     QCOMPARE(displayProperty.value(), QString::fromAscii("He;ido HH"));
       
   308 
       
   309     QVersitProperty nameProperty = document.properties().at(1);
       
   310     // Check parameters, contexts not allowed for N property
       
   311     QCOMPARE(nameProperty.parameters().count(), 0);
       
   312     // Check name
       
   313     QCOMPARE(nameProperty.name(), QString::fromAscii("N"));
       
   314     CHECK_VALUE(nameProperty, QVersitProperty::CompoundType,
       
   315                 QStringList() << QLatin1String("HH") << QLatin1String("He;ido")
       
   316                 << QLatin1String("A") << QLatin1String("Mr.") << QString());
       
   317 }
       
   318 
       
   319 void tst_QVersitContactExporter::testEncodePhoneNumber()
       
   320 {
       
   321     QContact contact(createContactWithName(QLatin1String("asdf")));
       
   322     QContactPhoneNumber phoneNumber;
       
   323     phoneNumber.setNumber(QString::fromAscii("12345678"));
       
   324     phoneNumber.setContexts(QContactDetail::ContextHome);
       
   325     phoneNumber.setSubTypes(QContactPhoneNumber::SubTypeMobile);
       
   326     contact.saveDetail(&phoneNumber);
       
   327     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard30Type));
       
   328     QVersitDocument document = mExporter->documents().first();
       
   329     QCOMPARE(document.properties().count(), BASE_PROPERTY_COUNT+1);
       
   330     QVersitProperty property = findPropertyByName(document, QLatin1String("TEL"));
       
   331     QVERIFY(!property.isEmpty());
       
   332     // Check parameters
       
   333     QCOMPARE(property.parameters().count(), 2);
       
   334     QVERIFY(property.parameters().contains(
       
   335         QString::fromAscii("TYPE"),QString::fromAscii("HOME")));
       
   336     QVERIFY(property.parameters().contains(
       
   337         QString::fromAscii("TYPE"),QString::fromAscii("CELL")));
       
   338     // Check value
       
   339     QCOMPARE(property.value(), phoneNumber.number());
       
   340 }
       
   341 
       
   342 void tst_QVersitContactExporter::testEncodeEmailAddress()
       
   343 {
       
   344     QContact contact(createContactWithName(QLatin1String("asdf")));
       
   345     QContactEmailAddress email;
       
   346     email.setEmailAddress(QString::fromAscii("test@test"));
       
   347     email.setContexts(QContactDetail::ContextHome);
       
   348     contact.saveDetail(&email);
       
   349     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard30Type));
       
   350     QVersitDocument document = mExporter->documents().first();
       
   351     QCOMPARE(document.properties().count(), BASE_PROPERTY_COUNT+1);
       
   352     QVersitProperty property = findPropertyByName(document, QLatin1String("EMAIL"));
       
   353     QVERIFY(!property.isEmpty());
       
   354     // Check parameters
       
   355     QCOMPARE(property.parameters().count(), 1);
       
   356     QVERIFY(property.parameters().contains(
       
   357         QString::fromAscii("TYPE"),QString::fromAscii("HOME")));
       
   358     // Check value
       
   359     QCOMPARE(property.value(), email.emailAddress());
       
   360 }
       
   361 
       
   362 void tst_QVersitContactExporter::testEncodeStreetAddress()
       
   363 {
       
   364     QContact contact(createContactWithName(QLatin1String("asdf")));
       
   365     QContactAddress address;
       
   366 
       
   367     address.setPostOfficeBox(QLatin1String("1234"));
       
   368     address.setCountry(QLatin1String("Finland"));
       
   369     address.setPostcode(QLatin1String("00440"));
       
   370     // Special characters are not escaped by the exporter, but by the writer
       
   371     address.setStreet(QLatin1String("HKKI; 1X 90"));
       
   372     address.setLocality(QLatin1String("Helsinki"));
       
   373     address.setContexts(QContactDetail::ContextHome);
       
   374     address.setSubTypes(QContactAddress::SubTypePostal);
       
   375     contact.saveDetail(&address);
       
   376     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard21Type));
       
   377     QVersitDocument document = mExporter->documents().first();
       
   378     QCOMPARE(document.properties().count(), BASE_PROPERTY_COUNT+1);
       
   379     QVersitProperty property = findPropertyByName(document, QLatin1String("ADR"));
       
   380     QVERIFY(!property.isEmpty());
       
   381     // Check parameters
       
   382     QCOMPARE(property.parameters().count(), 2);
       
   383     QVERIFY(property.parameters().contains(
       
   384         QString::fromAscii("TYPE"),QString::fromAscii("HOME")));
       
   385     QVERIFY(property.parameters().contains(
       
   386         QString::fromAscii("TYPE"),QString::fromAscii("POSTAL")));
       
   387     // Check name
       
   388     QCOMPARE(property.name(), QString::fromAscii("ADR"));
       
   389     CHECK_VALUE(property, QVersitProperty::CompoundType,
       
   390                 QStringList() << QLatin1String("1234") << QString() << QLatin1String("HKKI; 1X 90")
       
   391                 << QLatin1String("Helsinki") << QString() << QLatin1String("00440")
       
   392                 << QLatin1String("Finland"));
       
   393 }
       
   394 
       
   395 void tst_QVersitContactExporter::testEncodeUrl()
       
   396 {
       
   397     QContact contact(createContactWithName(QLatin1String("asdf")));
       
   398     QContactUrl url;
       
   399     url.setUrl(QString::fromAscii("http://www.myhome.com"));
       
   400     url.setContexts(QContactDetail::ContextHome);
       
   401     url.setSubType(QContactUrl::SubTypeHomePage);
       
   402     contact.saveDetail(&url);
       
   403     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard30Type));
       
   404     QVersitDocument document = mExporter->documents().first();
       
   405     QCOMPARE(document.properties().count(), BASE_PROPERTY_COUNT+1);
       
   406     QVersitProperty property = findPropertyByName(document, QLatin1String("URL"));
       
   407     QVERIFY(!property.isEmpty());
       
   408     // Check parameters
       
   409     QCOMPARE(property.parameters().count(), 1);
       
   410     QVERIFY(property.parameters().contains(
       
   411         QString::fromAscii("TYPE"),QString::fromAscii("HOME")));
       
   412     // Check value
       
   413     QCOMPARE(property.value(), url.url());
       
   414 }
       
   415 
       
   416 void tst_QVersitContactExporter::testEncodeUid()
       
   417 {
       
   418     QContact contact(createContactWithName(QLatin1String("asdf")));
       
   419     QContactGuid guid;
       
   420 
       
   421     guid.setContexts(QContactDetail::ContextHome);
       
   422     guid.setGuid(QString::fromAscii("0101222"));
       
   423     contact.saveDetail(&guid);
       
   424     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard21Type));
       
   425     QVersitDocument document = mExporter->documents().first();
       
   426     QCOMPARE(document.properties().count(), BASE_PROPERTY_COUNT+1);
       
   427     QVersitProperty property = findPropertyByName(document, QLatin1String("UID"));
       
   428     QVERIFY(!property.isEmpty());
       
   429     // Check parameters
       
   430     // Contexts are not allowed for UID
       
   431     QCOMPARE(property.parameters().count(), 0);
       
   432     // Check value
       
   433     QCOMPARE(property.value(), guid.guid());
       
   434 }
       
   435 
       
   436 void tst_QVersitContactExporter::testEncodeRev()
       
   437 {
       
   438     QContact contact(createContactWithName(QLatin1String("asdf")));
       
   439     QContactTimestamp timeStamp;
       
   440 
       
   441     // Last modified time found
       
   442     QDateTime revisionTime =
       
   443         QDateTime::fromString(
       
   444             QString::fromAscii("M1d1y200906:01:02"),
       
   445             QString::fromAscii("'M'M'd'd'y'yyyyhh:mm:ss"));
       
   446     revisionTime.setTimeSpec(Qt::UTC);
       
   447     timeStamp.setLastModified(revisionTime);
       
   448     // Contexts not allowed in REV property, check that they are not added
       
   449     timeStamp.setContexts(QContactDetail::ContextHome);
       
   450     contact.saveDetail(&timeStamp);
       
   451     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard30Type));
       
   452     QVersitDocument document = mExporter->documents().first();
       
   453     QCOMPARE(document.properties().count(), BASE_PROPERTY_COUNT+1);
       
   454     QVersitProperty property = findPropertyByName(document, QLatin1String("REV"));
       
   455     QVERIFY(!property.isEmpty());
       
   456     QCOMPARE(property.parameters().count(), 0);
       
   457     QString expectedValueUTCEncoded = QString::fromAscii("2009-01-01T06:01:02Z");
       
   458     QCOMPARE(property.value(), expectedValueUTCEncoded);
       
   459 
       
   460     // Last modified time not found, use the creation time
       
   461     QDateTime emptyTime;
       
   462     timeStamp.setLastModified(emptyTime);
       
   463     timeStamp.setCreated(revisionTime);
       
   464     contact.saveDetail(&timeStamp);
       
   465     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard30Type));
       
   466     document = mExporter->documents().first();
       
   467     QCOMPARE(document.properties().count(), BASE_PROPERTY_COUNT+1);
       
   468     property = findPropertyByName(document, QLatin1String("REV"));
       
   469     QVERIFY(!property.isEmpty());
       
   470     QCOMPARE(property.value(), expectedValueUTCEncoded);
       
   471 
       
   472     // Last modified time found, Local Time spec not UTC
       
   473     QDateTime localTime;
       
   474     revisionTime.setTimeSpec(Qt::LocalTime);
       
   475     timeStamp.setLastModified(revisionTime);
       
   476     localTime.setTimeSpec(Qt::LocalTime);
       
   477     timeStamp.setCreated(localTime);
       
   478     contact.saveDetail(&timeStamp);
       
   479     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard30Type));
       
   480     document = mExporter->documents().first();
       
   481     QCOMPARE(document.properties().count(), BASE_PROPERTY_COUNT+1);
       
   482     property = findPropertyByName(document, QLatin1String("REV"));
       
   483     QVERIFY(!property.isEmpty());
       
   484     QString expectedValueEncoded = QString::fromAscii("2009-01-01T06:01:02");
       
   485     QCOMPARE(property.value(), expectedValueEncoded);
       
   486 
       
   487     // Last modified time not found, creation time not found
       
   488     timeStamp.setLastModified(emptyTime);
       
   489     timeStamp.setCreated(emptyTime);
       
   490     contact.saveDetail(&timeStamp);
       
   491     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard30Type));
       
   492     document = mExporter->documents().first();
       
   493     QCOMPARE(document.properties().count(), BASE_PROPERTY_COUNT);
       
   494 }
       
   495 
       
   496 void tst_QVersitContactExporter::testEncodeBirthDay()
       
   497 {
       
   498     QContact contact(createContactWithName(QLatin1String("asdf")));
       
   499     QDate date(2009,1,1);
       
   500     QContactBirthday birthDay;
       
   501     birthDay.setDate(date);
       
   502     // Contexts not allowed in BDAY property, check that they are not added
       
   503     birthDay.setContexts(QContactDetail::ContextHome);
       
   504     contact.saveDetail(&birthDay);
       
   505     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard30Type));
       
   506     QVersitDocument document = mExporter->documents().first();
       
   507     QCOMPARE(document.properties().count(), BASE_PROPERTY_COUNT+1);
       
   508     QVersitProperty property = findPropertyByName(document, QLatin1String("BDAY"));
       
   509     QVERIFY(!property.isEmpty());
       
   510     QCOMPARE(property.parameters().count(), 0);
       
   511     QCOMPARE(property.value(), QString::fromAscii("2009-01-01"));
       
   512 }
       
   513 
       
   514 void tst_QVersitContactExporter::testEncodeNote()
       
   515 {
       
   516     QContact contact(createContactWithName(QLatin1String("asdf")));
       
   517     QContactNote note;
       
   518     note.setNote(QString::fromAscii("My Note"));
       
   519     // Contexts not allowed in NOTE property, check that they are not added
       
   520     note.setContexts(QContactDetail::ContextHome);
       
   521     contact.saveDetail(&note);
       
   522     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard30Type));
       
   523     QVersitDocument document = mExporter->documents().first();
       
   524     QCOMPARE(document.properties().count(), BASE_PROPERTY_COUNT+1);
       
   525     QVersitProperty property = findPropertyByName(document, QLatin1String("NOTE"));
       
   526     QVERIFY(!property.isEmpty());
       
   527     QCOMPARE(property.parameters().count(), 0);
       
   528     QCOMPARE(property.value(), note.note());
       
   529 }
       
   530 
       
   531 void tst_QVersitContactExporter::testEncodeGeoLocation()
       
   532 {
       
   533     QContact contact(createContactWithName(QLatin1String("asdf")));
       
   534     QContactGeoLocation geoLocation;
       
   535     QString longitude = QString::fromAscii("99.9");
       
   536     geoLocation.setLongitude(longitude.toDouble());
       
   537     QString latitude = QString::fromAscii("98.9");
       
   538     geoLocation.setLatitude(latitude.toDouble());
       
   539     // Contexts not allowed in GEO property, check that they are not added
       
   540     geoLocation.setContexts(QContactDetail::ContextHome);
       
   541     contact.saveDetail(&geoLocation);
       
   542     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard30Type));
       
   543     QVersitDocument document = mExporter->documents().first();
       
   544     QCOMPARE(document.properties().count(), BASE_PROPERTY_COUNT+1);
       
   545     QVersitProperty property = findPropertyByName(document, QLatin1String("GEO"));
       
   546     QVERIFY(!property.isEmpty());
       
   547     QCOMPARE(property.parameters().count(), 0);
       
   548     QCOMPARE(property.name(), QString::fromAscii("GEO"));
       
   549     CHECK_VALUE(property, QVersitProperty::CompoundType,
       
   550                 QStringList() << QLatin1String("99.9") << QLatin1String("98.9"));
       
   551 }
       
   552 
       
   553 void tst_QVersitContactExporter::testEncodeOrganization()
       
   554 {
       
   555     QContact contact(createContactWithName(QLatin1String("asdf")));
       
   556     QContactOrganization organization;
       
   557     QVersitDocument document;
       
   558     QVersitProperty property;
       
   559     QString title(QString::fromAscii("Developer"));
       
   560 
       
   561     // TITLE
       
   562     organization.setTitle(title);
       
   563     contact.saveDetail(&organization);
       
   564     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard30Type));
       
   565     document = mExporter->documents().first();
       
   566     QCOMPARE(document.properties().count(), BASE_PROPERTY_COUNT+1);
       
   567     property = findPropertyByName(document, QLatin1String("TITLE"));
       
   568     QVERIFY(!property.isEmpty());
       
   569     QCOMPARE(property.value(), title);
       
   570 
       
   571     // ORG with name
       
   572     organization.setTitle(QString());
       
   573     organization.setName(QString::fromAscii("Nokia"));
       
   574     contact.saveDetail(&organization);
       
   575     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard30Type));
       
   576     document = mExporter->documents().first();
       
   577     QCOMPARE(document.properties().count(), BASE_PROPERTY_COUNT+1);
       
   578     property = document.properties().at(BASE_PROPERTY_COUNT);
       
   579     QCOMPARE(property.name(), QString::fromAscii("ORG"));
       
   580     CHECK_VALUE(property, QVersitProperty::CompoundType, QStringList(QLatin1String("Nokia")));
       
   581 
       
   582     // ORG with department/unit
       
   583     organization.setName(QString());
       
   584     QStringList departments(QString::fromAscii("R&D"));
       
   585     departments.append(QString::fromAscii("Qt"));
       
   586     organization.setDepartment(departments);
       
   587     contact.saveDetail(&organization);
       
   588     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard30Type));
       
   589     document = mExporter->documents().first();
       
   590     QCOMPARE(document.properties().count(), BASE_PROPERTY_COUNT+1);
       
   591     property = document.properties().at(BASE_PROPERTY_COUNT);
       
   592     QCOMPARE(property.name(), QString::fromAscii("ORG"));
       
   593     CHECK_VALUE(property, QVersitProperty::CompoundType, QStringList()
       
   594                 << QString() << QLatin1String("R&D") << QLatin1String("Qt"));
       
   595 
       
   596     // ORG with name and department/unit
       
   597     organization.setName(QString::fromAscii("Nokia"));
       
   598     contact.saveDetail(&organization);
       
   599     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard30Type));
       
   600     document = mExporter->documents().first();
       
   601     QCOMPARE(document.properties().count(), BASE_PROPERTY_COUNT+1);
       
   602     property = document.properties().at(BASE_PROPERTY_COUNT);
       
   603     QCOMPARE(property.name(), QString::fromAscii("ORG"));
       
   604     CHECK_VALUE(property, QVersitProperty::CompoundType, QStringList()
       
   605                 << QLatin1String("Nokia") << QLatin1String("R&D") << QLatin1String("Qt"));
       
   606 
       
   607     // TITLE and ORG
       
   608     organization.setTitle(QString::fromAscii("Developer"));
       
   609     contact.saveDetail(&organization);
       
   610     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard30Type));
       
   611     document = mExporter->documents().first();
       
   612     QCOMPARE(document.properties().count(), BASE_PROPERTY_COUNT+2);
       
   613     property = findPropertyByName(document, QLatin1String("TITLE"));
       
   614     QVERIFY(!property.isEmpty());
       
   615     QCOMPARE(property.value(), title);
       
   616     property = findPropertyByName(document, QLatin1String("ORG"));
       
   617     QVERIFY(!property.isEmpty());
       
   618     QCOMPARE(property.name(), QString::fromAscii("ORG"));
       
   619     CHECK_VALUE(property, QVersitProperty::CompoundType, QStringList()
       
   620                 << QLatin1String("Nokia") << QLatin1String("R&D") << QLatin1String("Qt"));
       
   621 
       
   622     // ORG LOGO Test1: LOGO as remote Resouce
       
   623     const QString url = QString::fromAscii("http://myhome.com/test.jpg");
       
   624     contact = createContactWithName(QLatin1String("asdf"));
       
   625     organization = QContactOrganization();
       
   626     organization.setLogoUrl(url);
       
   627     contact.saveDetail(&organization);
       
   628     mResourceHandler->mSimulatedMimeType = QLatin1String("image/jpeg");
       
   629     mExporter->setResourceHandler(mResourceHandler);
       
   630     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard30Type));
       
   631     document = mExporter->documents().first();
       
   632     QVERIFY(!mResourceHandler->mLoadResourceCalled);
       
   633 
       
   634     // Source type is encoded, but media type is not for a URL.
       
   635     property = findPropertyByName(document, QLatin1String("LOGO"));
       
   636     QVERIFY(!property.isEmpty());
       
   637     QCOMPARE(property.parameters().count(), 1);
       
   638 
       
   639     QVERIFY(property.parameters().contains(
       
   640             QString::fromAscii("VALUE"), QString::fromAscii("URL")));
       
   641 
       
   642     //Check property value
       
   643     QCOMPARE(property.value(), url);
       
   644 
       
   645     // ORG LOGO Test2: LOGO File.
       
   646     mResourceHandler->mSimulatedData = "simulated data";
       
   647     contact = createContactWithName(QLatin1String("asdf"));
       
   648     organization = QContactOrganization();
       
   649     organization.setLogoUrl(TEST_PHOTO_FILE);
       
   650     contact.saveDetail(&organization);
       
   651     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard30Type));
       
   652     document = mExporter->documents().first();
       
   653     QVERIFY(mResourceHandler->mLoadResourceCalled);
       
   654     QCOMPARE(mResourceHandler->mLocation, TEST_PHOTO_FILE);
       
   655 
       
   656     // It should be stored in the property as a QVariant of QByteArray
       
   657     property = findPropertyByName(document, QLatin1String("LOGO"));
       
   658     QVERIFY(!property.isEmpty());
       
   659     QMultiHash<QString,QString> parameters = property.parameters();
       
   660     // Media type is encoded
       
   661     QCOMPARE(parameters.count(), 1);
       
   662     QVERIFY(parameters.contains(
       
   663             QString::fromAscii("TYPE"), QString::fromAscii("JPEG")));
       
   664     // Verify value.
       
   665     QVariant variantValue = property.variantValue();
       
   666     QVERIFY(variantValue.type() == QVariant::ByteArray);
       
   667     QCOMPARE(variantValue.value<QByteArray>(), mResourceHandler->mSimulatedData);
       
   668 
       
   669     // Assistant Name Test.
       
   670     contact = createContactWithName(QLatin1String("asdf"));
       
   671     organization = QContactOrganization();
       
   672     organization.setAssistantName(QString::fromAscii("myAssistant"));
       
   673     contact.saveDetail(&organization);
       
   674     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard30Type));
       
   675     document = mExporter->documents().first();
       
   676     QCOMPARE(document.properties().count(), BASE_PROPERTY_COUNT+1);
       
   677     property = findPropertyByName(document, QLatin1String("X-ASSISTANT"));
       
   678     QVERIFY(!property.isEmpty());
       
   679     QCOMPARE(property.value(), QString::fromAscii("myAssistant"));
       
   680 
       
   681     // Test: Role
       
   682     contact = createContactWithName(QLatin1String("asdf"));
       
   683     organization = QContactOrganization();
       
   684     organization.setRole(QString::fromAscii("Executive"));
       
   685     contact.saveDetail(&organization);
       
   686     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard30Type));
       
   687     document = mExporter->documents().first();
       
   688     QCOMPARE(document.properties().count(), BASE_PROPERTY_COUNT+1);
       
   689     property = findPropertyByName(document, QLatin1String("ROLE"));
       
   690     QVERIFY(!property.isEmpty());
       
   691     QCOMPARE(property.value(), QString::fromAscii("Executive"));
       
   692 
       
   693 }
       
   694 
       
   695 void tst_QVersitContactExporter::testEncodeAvatar()
       
   696 {
       
   697     QContact contact = createContactWithName(QLatin1String("asdf"));
       
   698     QContactAvatar contactAvatar;
       
   699     mResourceHandler->mSimulatedData = "simulated data";
       
   700     mResourceHandler->mSimulatedMimeType = QLatin1String("image/jpeg");
       
   701 
       
   702     // Test1: Web URL
       
   703     const QString url = QString::fromAscii("http://www.myhome.com/test.jpg");
       
   704     contactAvatar.setImageUrl(url);
       
   705     contact.saveDetail(&contactAvatar);
       
   706     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard30Type));
       
   707     QVersitDocument document = mExporter->documents().first();
       
   708     QVERIFY(document.properties().length() > BASE_PROPERTY_COUNT);
       
   709     QVersitProperty property = findPropertyByName(document, QLatin1String("PHOTO"));
       
   710     QVERIFY(!property.isEmpty());
       
   711     QCOMPARE(property.parameters().count(), 1);
       
   712     QCOMPARE(property.value(), url);
       
   713     QVERIFY(!mResourceHandler->mLoadResourceCalled);
       
   714 
       
   715     // Test 2: Local Media PHOTO
       
   716     contactAvatar.setImageUrl(TEST_PHOTO_FILE);
       
   717     contact.saveDetail(&contactAvatar);
       
   718     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard30Type));
       
   719     document = mExporter->documents().first();
       
   720     QVERIFY(mResourceHandler->mLoadResourceCalled);
       
   721     QCOMPARE(mResourceHandler->mLocation, TEST_PHOTO_FILE);
       
   722     // verify the value
       
   723     QVERIFY(document.properties().length() > BASE_PROPERTY_COUNT);
       
   724     property = findPropertyByName(document, QLatin1String("PHOTO"));
       
   725     QVERIFY(!property.isEmpty());
       
   726     QVariant variantValue = property.variantValue();
       
   727     QVERIFY(variantValue.type() == QVariant::ByteArray);
       
   728     QCOMPARE(variantValue.value<QByteArray>(), mResourceHandler->mSimulatedData);
       
   729     QVERIFY(property.parameters().contains(QString::fromAscii("TYPE"),
       
   730                                            QString::fromAscii("JPEG")));
       
   731 }
       
   732 
       
   733 void tst_QVersitContactExporter::testEncodeThumbnail() {
       
   734     QImage image;
       
   735     image.loadFromData(SAMPLE_GIF);
       
   736     if (QImageWriter::supportedImageFormats().contains("png")) {
       
   737         QContactThumbnail thumbnail;
       
   738         thumbnail.setThumbnail(image);
       
   739         QContact contact(createContactWithName(QLatin1String("asdf")));
       
   740         contact.saveDetail(&thumbnail);
       
   741         QVERIFY(mExporter->exportContacts(QList<QContact>() << contact,
       
   742                                           QVersitDocument::VCard30Type));
       
   743         QVersitDocument document = mExporter->documents().first();
       
   744         // verify the value
       
   745         QVERIFY(document.properties().length() > BASE_PROPERTY_COUNT);
       
   746         QVersitProperty property = findPropertyByName(document, QLatin1String("PHOTO"));
       
   747         QVERIFY(!property.isEmpty());
       
   748         QVariant variantValue = property.variantValue();
       
   749         QVERIFY(variantValue.type() == QVariant::ByteArray);
       
   750         QByteArray retrievedData = variantValue.value<QByteArray>();
       
   751         QImage retrievedImage;
       
   752         retrievedImage.loadFromData(retrievedData);
       
   753         QCOMPARE(retrievedImage, image);
       
   754     }
       
   755 }
       
   756 
       
   757 
       
   758 void tst_QVersitContactExporter::testEncodeEmbeddedContent()
       
   759 {
       
   760     QContact contact = createContactWithName(QLatin1String("asdf"));
       
   761     QContactAvatar contactAvatar;
       
   762     QVariant variantValue;
       
   763 
       
   764     // Test 1: URL
       
   765     const QString url = QString::fromAscii("http://www.myhome.com/test.jpg");
       
   766     contactAvatar.setImageUrl(url);
       
   767     contact.saveDetail(&contactAvatar);
       
   768     mResourceHandler->mSimulatedMimeType = QLatin1String("image/jpeg");
       
   769     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard30Type));
       
   770     QVersitDocument document = mExporter->documents().first();
       
   771     QVERIFY(!mResourceHandler->mLoadResourceCalled);
       
   772     QVERIFY(document.properties().size() > BASE_PROPERTY_COUNT);
       
   773     QVersitProperty photoProperty = findPropertyByName(document, QLatin1String("PHOTO"));
       
   774     QVERIFY(!photoProperty.isEmpty());
       
   775     QCOMPARE(photoProperty.parameters().count(), 1);
       
   776     QVERIFY(photoProperty.parameters().contains(
       
   777             QString::fromAscii("VALUE"),QString::fromAscii("URL")));
       
   778     QCOMPARE(photoProperty.value(), url);
       
   779 
       
   780     // Test 2: Local PHOTO, image loaded by the loader
       
   781     contactAvatar.setImageUrl(TEST_PHOTO_FILE);
       
   782     contact.saveDetail(&contactAvatar);
       
   783     mResourceHandler->clear();
       
   784     mResourceHandler->mSimulatedMimeType = QLatin1String("image/jpeg");
       
   785     mResourceHandler->mSimulatedData = "simulated image data";
       
   786     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard30Type));
       
   787     document = mExporter->documents().first();
       
   788     QVERIFY(mResourceHandler->mLoadResourceCalled);
       
   789     QVERIFY(document.properties().size() > BASE_PROPERTY_COUNT);
       
   790     photoProperty = findPropertyByName(document, QLatin1String("PHOTO"));
       
   791     QVERIFY(!photoProperty.isEmpty());
       
   792     QCOMPARE(photoProperty.parameters().count(), 1);
       
   793     QVERIFY(photoProperty.parameters().contains(QString::fromAscii("TYPE"),
       
   794                                                 QString::fromAscii("JPEG")));
       
   795     variantValue = photoProperty.variantValue();
       
   796     QVERIFY(variantValue.type() == QVariant::ByteArray);
       
   797     QCOMPARE(variantValue.value<QByteArray>(), mResourceHandler->mSimulatedData);
       
   798 
       
   799     // Without a resource handler
       
   800     mExporter->setResourceHandler(0);
       
   801     contactAvatar.setImageUrl(TEST_PHOTO_FILE);
       
   802     contact.saveDetail(&contactAvatar);
       
   803     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard30Type));
       
   804     document = mExporter->documents().first();
       
   805     QCOMPARE(document.properties().count(), BASE_PROPERTY_COUNT);
       
   806 
       
   807     mExporter->setResourceHandler(mResourceHandler);
       
   808 }
       
   809 
       
   810 void tst_QVersitContactExporter::testEncodeRingtone()
       
   811 {
       
   812     QContactRingtone ringtone;
       
   813     mResourceHandler->clear();
       
   814     mResourceHandler->mSimulatedMimeType = QLatin1String("audio/wav");
       
   815     mResourceHandler->mSimulatedData = "simulated audio data";
       
   816     ringtone.setAudioRingtoneUrl(TEST_AUDIO_FILE);
       
   817     QContact contact(createContactWithName(QLatin1String("asdf")));
       
   818     contact.saveDetail(&ringtone);
       
   819     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard30Type));
       
   820     QVersitDocument document = mExporter->documents().first();
       
   821     QVERIFY(mResourceHandler->mLoadResourceCalled);
       
   822     QVersitProperty soundProperty = findPropertyByName(document, QLatin1String("SOUND"));
       
   823     QVERIFY(!soundProperty.isEmpty());
       
   824     QCOMPARE(soundProperty.parameters().count(), 1);
       
   825     QVERIFY(soundProperty.parameters().contains(
       
   826         QString::fromAscii("TYPE"),
       
   827         QString::fromAscii("WAV")));
       
   828     QVariant variantValue = soundProperty.variantValue();
       
   829     QVERIFY(variantValue.type() == QVariant::ByteArray);
       
   830     QCOMPARE(variantValue.value<QByteArray>(), mResourceHandler->mSimulatedData);
       
   831 }
       
   832 
       
   833 void tst_QVersitContactExporter::testEncodeParameters()
       
   834 {
       
   835     QContact contact(createContactWithName(QLatin1String("asdf")));
       
   836     QContactPhoneNumber phoneNumber;
       
   837     phoneNumber.setNumber(QString::fromAscii("12345678"));
       
   838     QStringList subtypes;
       
   839     subtypes.append(QContactPhoneNumber::SubTypeMobile);
       
   840     subtypes.append(QContactPhoneNumber::SubTypeVideo);
       
   841     // Add a not supported subtype in vCard, to make sure its not encoded.
       
   842     subtypes.append(QContactPhoneNumber::SubTypeDtmfMenu);
       
   843     phoneNumber.setSubTypes(subtypes);
       
   844     contact.saveDetail(&phoneNumber);
       
   845     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard30Type));
       
   846     QVersitDocument document = mExporter->documents().first();
       
   847     QCOMPARE(document.properties().count(), BASE_PROPERTY_COUNT+1);
       
   848     QVersitProperty property = findPropertyByName(document, QLatin1String("TEL"));
       
   849     QVERIFY(!property.isEmpty());
       
   850     QCOMPARE(property.parameters().count(), 2);
       
   851     QVERIFY(property.parameters().contains(
       
   852         QString::fromAscii("TYPE"), QString::fromAscii("CELL")));
       
   853     QVERIFY(property.parameters().contains(
       
   854         QString::fromAscii("TYPE"),QString::fromAscii("VIDEO")));
       
   855 }
       
   856 
       
   857 void tst_QVersitContactExporter::testEncodeGender()
       
   858 {
       
   859     QContact contact(createContactWithName(QLatin1String("asdf")));
       
   860     QContactGender gender;
       
   861     gender.setGender(QContactGender::GenderMale);
       
   862     gender.setContexts(QContactGender::ContextHome); // Should not be encoded
       
   863     contact.saveDetail(&gender);
       
   864     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard30Type));
       
   865     QVersitDocument document = mExporter->documents().first();
       
   866     QCOMPARE(document.properties().count(), BASE_PROPERTY_COUNT+1);
       
   867     QVersitProperty property = findPropertyByName(document, QLatin1String("X-GENDER"));
       
   868     QVERIFY(!property.isEmpty());
       
   869     QCOMPARE(property.parameters().count(), 0);
       
   870     QCOMPARE(property.value(), gender.gender());
       
   871 }
       
   872 
       
   873 void tst_QVersitContactExporter::testEncodeNickName()
       
   874 {
       
   875     QContact contact(createContactWithName(QLatin1String("asdf")));
       
   876 
       
   877     // Add an extra detail
       
   878     QContactGender gender;
       
   879     gender.setGender(QContactGender::GenderMale);
       
   880     contact.saveDetail(&gender);
       
   881 
       
   882     // One nickname given
       
   883     QContactNickname firstNickname;
       
   884     firstNickname.setNickname(QLatin1String("Homie"));
       
   885     contact.saveDetail(&firstNickname);
       
   886     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard30Type));
       
   887     QVersitDocument document = mExporter->documents().first();
       
   888     QCOMPARE(document.properties().count(), BASE_PROPERTY_COUNT+2);
       
   889     QVersitProperty property = document.properties().at(BASE_PROPERTY_COUNT+1);
       
   890     QCOMPARE(property.name(), QLatin1String("X-NICKNAME"));
       
   891     CHECK_VALUE(property, QVersitProperty::ListType, QStringList(QLatin1String("Homie")));
       
   892 
       
   893     // Two nicknames given, should be collated into a single property
       
   894     contact = createContactWithName(QLatin1String("asdf"));
       
   895     contact.saveDetail(&gender);
       
   896     contact.saveDetail(&firstNickname);
       
   897     QContactNickname secondNickname;
       
   898     secondNickname.setNickname(QLatin1String("Jay"));
       
   899     contact.saveDetail(&secondNickname);
       
   900     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard30Type));
       
   901     document = mExporter->documents().first();
       
   902     QCOMPARE(document.properties().count(), BASE_PROPERTY_COUNT+2);
       
   903     property = findPropertyByName(document, QLatin1String("X-NICKNAME"));
       
   904     QVERIFY(!property.isEmpty());
       
   905     QCOMPARE(property.name(), QString::fromAscii("X-NICKNAME"));
       
   906     CHECK_VALUE(property, QVersitProperty::ListType,
       
   907                 QStringList() << QLatin1String("Homie") << QLatin1String("Jay"));
       
   908 }
       
   909 
       
   910 void tst_QVersitContactExporter::testEncodeTag()
       
   911 {
       
   912     QContact contact(createContactWithName(QLatin1String("asdf")));
       
   913 
       
   914     // Add an extra detail
       
   915     QContactGender gender;
       
   916     gender.setGender(QContactGender::GenderMale);
       
   917     contact.saveDetail(&gender);
       
   918 
       
   919     // One tag given
       
   920     QContactTag firstTag;
       
   921     firstTag.setTag(QLatin1String("red"));
       
   922     contact.saveDetail(&firstTag);
       
   923     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard30Type));
       
   924     QVersitDocument document = mExporter->documents().first();
       
   925     QCOMPARE(document.properties().count(), BASE_PROPERTY_COUNT+2);
       
   926     QVersitProperty property = document.properties().at(BASE_PROPERTY_COUNT+1);
       
   927     QCOMPARE(property.name(), QLatin1String("CATEGORIES"));
       
   928     CHECK_VALUE(property, QVersitProperty::ListType, QStringList(QLatin1String("red")));
       
   929 
       
   930     // Two tags given, should be collated into a single property
       
   931     contact = createContactWithName(QLatin1String("asdf"));
       
   932     contact.saveDetail(&firstTag);
       
   933     contact.saveDetail(&gender);
       
   934     QContactTag secondTag;
       
   935     secondTag.setTag(QLatin1String("green"));
       
   936     contact.saveDetail(&secondTag);
       
   937     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard30Type));
       
   938     document = mExporter->documents().first();
       
   939     QCOMPARE(document.properties().count(), BASE_PROPERTY_COUNT+2);
       
   940     property = document.properties().at(BASE_PROPERTY_COUNT+1);
       
   941     QCOMPARE(property.name(), QString::fromAscii("CATEGORIES"));
       
   942     CHECK_VALUE(property, QVersitProperty::ListType,
       
   943                 QStringList() << QLatin1String("red") << QLatin1String("green"));
       
   944 }
       
   945 
       
   946 void tst_QVersitContactExporter::testEncodeAnniversary()
       
   947 {
       
   948     QContact contact(createContactWithName(QLatin1String("asdf")));
       
   949     QContactAnniversary anniversary;
       
   950     QDate date(2009,1,1);
       
   951     anniversary.setOriginalDate(date);
       
   952     anniversary.setContexts(QContactDetail::ContextHome);
       
   953     anniversary.setSubType(QContactAnniversary::SubTypeWedding);
       
   954     contact.saveDetail(&anniversary);
       
   955     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard30Type));
       
   956     QVersitDocument document = mExporter->documents().first();
       
   957     QCOMPARE(document.properties().count(), BASE_PROPERTY_COUNT+1);
       
   958     QVersitProperty property = findPropertyByName(document, QLatin1String("X-ANNIVERSARY"));
       
   959     QVERIFY(!property.isEmpty());
       
   960     // The contexts and subtypes are not defined for X-ANNIVERSARY property
       
   961     QCOMPARE(property.parameters().count(), 0);
       
   962     // Check value
       
   963     QCOMPARE(property.value(), date.toString(Qt::ISODate));
       
   964 }
       
   965 
       
   966 
       
   967 void tst_QVersitContactExporter::testEncodeOnlineAccount()
       
   968 {
       
   969     QContact contact(createContactWithName(QLatin1String("asdf")));
       
   970     QContactOnlineAccount onlineAccount;
       
   971     QString accountUri(QString::fromAscii("sip:abc@temp.com"));
       
   972     onlineAccount.setAccountUri(accountUri);
       
   973 
       
   974     // Video sharing
       
   975     onlineAccount.setSubTypes(QContactOnlineAccount::SubTypeVideoShare);
       
   976     onlineAccount.setContexts(QContactDetail::ContextHome);
       
   977     contact.saveDetail(&onlineAccount);
       
   978     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard30Type));
       
   979     QVersitDocument document = mExporter->documents().first();
       
   980     QCOMPARE(document.properties().count(), BASE_PROPERTY_COUNT+1);
       
   981     QVersitProperty property = findPropertyByName(document, QLatin1String("X-SIP"));
       
   982     QVERIFY(!property.isEmpty());
       
   983     // Check parameters
       
   984     QCOMPARE(property.parameters().count(), 2);
       
   985     QVERIFY(property.parameters().contains(
       
   986         QString::fromAscii("TYPE"),QString::fromAscii("HOME")));
       
   987     QVERIFY(property.parameters().contains(
       
   988         QString::fromAscii("TYPE"),QString::fromAscii("SWIS")));
       
   989     // Check value
       
   990     QCOMPARE(property.value(), accountUri);
       
   991 
       
   992     // VoIP
       
   993     onlineAccount.setSubTypes(QContactOnlineAccount::SubTypeSipVoip);
       
   994     onlineAccount.setContexts(QContactDetail::ContextWork);
       
   995     contact.saveDetail(&onlineAccount);
       
   996     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard30Type));
       
   997     document = mExporter->documents().first();
       
   998     QCOMPARE(document.properties().count(), BASE_PROPERTY_COUNT+1);
       
   999     property = findPropertyByName(document, QLatin1String("X-SIP"));
       
  1000     QVERIFY(!property.isEmpty());
       
  1001     // Check parameters
       
  1002     QCOMPARE(property.parameters().count(), 2);
       
  1003     QVERIFY(property.parameters().contains(
       
  1004         QString::fromAscii("TYPE"),QString::fromAscii("WORK")));
       
  1005     QVERIFY(property.parameters().contains(
       
  1006         QString::fromAscii("TYPE"),QString::fromAscii("VOIP")));
       
  1007     // Check value
       
  1008     QCOMPARE(property.value(), accountUri);
       
  1009 
       
  1010     // Plain SIP
       
  1011     onlineAccount.setSubTypes(QContactOnlineAccount::SubTypeSip);
       
  1012     onlineAccount.setContexts(QContactDetail::ContextWork);
       
  1013     contact.saveDetail(&onlineAccount);
       
  1014     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard30Type));
       
  1015     document = mExporter->documents().first();
       
  1016     QCOMPARE(document.properties().count(), BASE_PROPERTY_COUNT+1);
       
  1017     property = findPropertyByName(document, QLatin1String("X-SIP"));
       
  1018     QVERIFY(!property.isEmpty());
       
  1019     // Check parameters, SIP not added as a TYPE parameter
       
  1020     QCOMPARE(property.parameters().count(), 1);
       
  1021     QVERIFY(property.parameters().contains(
       
  1022         QString::fromAscii("TYPE"),QString::fromAscii("WORK")));
       
  1023     // Check value
       
  1024     QCOMPARE(property.value(), accountUri);
       
  1025 
       
  1026     // IMPP / X-IMPP
       
  1027     onlineAccount.setSubTypes(QContactOnlineAccount::SubTypeImpp);
       
  1028     onlineAccount.setContexts(QContactDetail::ContextHome);
       
  1029     contact.saveDetail(&onlineAccount);
       
  1030     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard30Type));
       
  1031     document = mExporter->documents().first();
       
  1032     QCOMPARE(document.properties().count(), BASE_PROPERTY_COUNT+1);
       
  1033     property = findPropertyByName(document, QLatin1String("X-IMPP"));
       
  1034     QVERIFY(!property.isEmpty());
       
  1035     // Check parameters, SIP not added as a TYPE parameter
       
  1036     QCOMPARE(property.parameters().count(), 1);
       
  1037     QVERIFY(property.parameters().contains(
       
  1038         QString::fromAscii("TYPE"),QString::fromAscii("HOME")));
       
  1039     // Check value
       
  1040     QCOMPARE(property.value(), accountUri);
       
  1041 
       
  1042     // Other subtypes not converted
       
  1043     onlineAccount.setSubTypes(QString::fromAscii("INVALIDSUBTYPE"));
       
  1044     contact.saveDetail(&onlineAccount);
       
  1045     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard30Type));
       
  1046     document = mExporter->documents().first();
       
  1047     QCOMPARE(document.properties().count(), BASE_PROPERTY_COUNT);
       
  1048 }
       
  1049 
       
  1050 void tst_QVersitContactExporter::testEncodeFamily()
       
  1051 {
       
  1052     QContact contact(createContactWithName(QLatin1String("asdf")));
       
  1053     QContactFamily family;
       
  1054 
       
  1055     // No spouse, no family
       
  1056     family.setContexts(QContactDetail::ContextHome);
       
  1057     contact.saveDetail(&family);
       
  1058     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard30Type));
       
  1059     QVersitDocument document = mExporter->documents().first();
       
  1060     QCOMPARE(document.properties().count(), BASE_PROPERTY_COUNT);
       
  1061 
       
  1062     // Only spouse present
       
  1063     QString spouce = QString::fromAscii("ABC");
       
  1064     family.setSpouse(spouce);
       
  1065     contact.saveDetail(&family);
       
  1066     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard30Type));
       
  1067     document = mExporter->documents().first();
       
  1068     QCOMPARE(document.properties().count(), BASE_PROPERTY_COUNT+1);
       
  1069     QVersitProperty spouseProperty = findPropertyByName(document, QLatin1String("X-SPOUSE"));
       
  1070     QVERIFY(!spouseProperty.isEmpty());
       
  1071     QCOMPARE(spouseProperty.parameters().count(), 0);
       
  1072     QCOMPARE(spouseProperty.value(), spouce);
       
  1073 
       
  1074     // Spouse and children
       
  1075     QStringList children;
       
  1076     children << QString::fromAscii("A") << QString::fromAscii("B") ;
       
  1077     family.setChildren(children);
       
  1078     family.setSpouse(spouce);
       
  1079     contact.saveDetail(&family);
       
  1080     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard30Type));
       
  1081     document = mExporter->documents().first();
       
  1082     QCOMPARE(document.properties().count(), BASE_PROPERTY_COUNT+2);
       
  1083     spouseProperty = findPropertyByName(document, QLatin1String("X-SPOUSE"));
       
  1084     QVERIFY(!spouseProperty.isEmpty());
       
  1085     QCOMPARE(spouseProperty.parameters().count(), 0);
       
  1086     QCOMPARE(spouseProperty.value(), spouce);
       
  1087     QVersitProperty childrenProperty = findPropertyByName(document, QLatin1String("X-CHILDREN"));
       
  1088     QVERIFY(!spouseProperty.isEmpty());
       
  1089     QCOMPARE(childrenProperty.parameters().count(), 0);
       
  1090     QCOMPARE(childrenProperty.name(), QString::fromAscii("X-CHILDREN"));
       
  1091     CHECK_VALUE(childrenProperty, QVersitProperty::ListType, children);
       
  1092 }
       
  1093 
       
  1094 
       
  1095 void tst_QVersitContactExporter::testEncodeDisplayLabel()
       
  1096 {
       
  1097     QContact contact;
       
  1098     QContactName contactName;
       
  1099 
       
  1100     // No display label, but QContactName found
       
  1101     contactName.setFirstName(QString::fromAscii("First"));
       
  1102     contactName.setLastName(QString::fromAscii("Last"));
       
  1103     contactName.setMiddleName(QString::fromAscii("Middle"));
       
  1104     contact.saveDetail(&contactName);
       
  1105     QVERIFY(mExporter->exportContacts(QList<QContact>() << contact, QVersitDocument::VCard21Type));
       
  1106     QVersitDocument document = mExporter->documents().first();
       
  1107     QCOMPARE(document.properties().count(), 2);
       
  1108     QVersitProperty displayProperty = document.properties().at(0);
       
  1109     QCOMPARE(displayProperty.name(), QString::fromAscii("FN"));
       
  1110     QCOMPARE(displayProperty.value(), QString::fromAscii("First Last"));
       
  1111     QVersitProperty nameProperty = document.properties().at(1);
       
  1112     QCOMPARE(nameProperty.name(), QString::fromAscii("N"));
       
  1113     CHECK_VALUE(nameProperty, QVersitProperty::CompoundType, QStringList()
       
  1114                 << QLatin1String("Last") << QLatin1String("First") << QLatin1String("Middle")
       
  1115                 << QString() << QString());
       
  1116 }
       
  1117 
       
  1118 void tst_QVersitContactExporter::testDefaultResourceHandler()
       
  1119 {
       
  1120     QVersitDefaultResourceHandler handler;
       
  1121     QByteArray contents;
       
  1122     QString mimeType;
       
  1123     handler.loadResource(QLatin1String("test.jpg"), &contents, &mimeType);
       
  1124     QCOMPARE(mimeType, QLatin1String("image/jpeg"));
       
  1125 
       
  1126     QVersitProperty property;
       
  1127     QString location;
       
  1128     QVERIFY(!handler.saveResource("test contents", property, &location));
       
  1129 }
       
  1130 
       
  1131 // Test utility functions
       
  1132 QContact tst_QVersitContactExporter::createContactWithName(QString name)
       
  1133 {
       
  1134     QContact contact;
       
  1135     QContactName nameDetail;
       
  1136     nameDetail.setFirstName(name);
       
  1137     contact.saveDetail(&nameDetail);
       
  1138     return contact;
       
  1139 }
       
  1140 
       
  1141 QContactDetail tst_QVersitContactExporter::searchDetail(
       
  1142     QList<QContactDetail> details,
       
  1143     QString search)
       
  1144 {
       
  1145     QContactDetail detail;
       
  1146     for (int i= 0; i < details.count(); i++) {
       
  1147         if ( details.at(i).definitionName() == search )
       
  1148             detail = details.at(i);
       
  1149     }
       
  1150     return detail;
       
  1151 }
       
  1152 
       
  1153 QVersitProperty tst_QVersitContactExporter::findPropertyByName(
       
  1154         const QVersitDocument &document, const QString &propertyName)
       
  1155 {
       
  1156     foreach (const QVersitProperty& property, document.properties()) {
       
  1157         if (property.name() == propertyName)
       
  1158             return property;
       
  1159     }
       
  1160     return QVersitProperty();
       
  1161 }
       
  1162 
       
  1163 QTEST_MAIN(tst_QVersitContactExporter)
       
  1164