qtmobility/tests/auto/qversit/tst_qversit.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 "qversitdefs_p.h"
       
    43 #include "tst_qversit.h"
       
    44 #include "qversitreader.h"
       
    45 #include "qversitreader_p.h"
       
    46 #include "qversitcontactimporter.h"
       
    47 #include "qcontact.h"
       
    48 #include "qcontactmanager.h"
       
    49 #include "qcontactmanagerengine.h"
       
    50 
       
    51 #include <QtTest/QtTest>
       
    52 #include <QDebug>
       
    53 #include <QDir>
       
    54 #include <QList>
       
    55 
       
    56 QTM_BEGIN_NAMESPACE
       
    57 class MyQVersitResourceHandler : public QVersitResourceHandler
       
    58 {
       
    59 public:
       
    60     MyQVersitResourceHandler() : mIndex(0)
       
    61     {
       
    62     }
       
    63 
       
    64     bool saveResource(const QByteArray& contents, const QVersitProperty& property,
       
    65                       QString* location)
       
    66     {
       
    67         Q_UNUSED(property)
       
    68         *location = QString::number(mIndex++);
       
    69         mObjects.insert(*location, contents);
       
    70         return true;
       
    71     }
       
    72 
       
    73     bool loadResource(const QString &location, QByteArray *contents, QString *mimeType)
       
    74     {
       
    75         Q_UNUSED(location)
       
    76         Q_UNUSED(contents)
       
    77         Q_UNUSED(mimeType)
       
    78         return false;
       
    79     }
       
    80 
       
    81     int mIndex;
       
    82     QMap<QString, QByteArray> mObjects;
       
    83 };
       
    84 QTM_END_NAMESPACE
       
    85 
       
    86 QTM_USE_NAMESPACE
       
    87 
       
    88 #ifndef TESTDATA_DIR
       
    89 #define TESTDATA_DIR "./"
       
    90 #endif
       
    91 
       
    92 Q_DECLARE_METATYPE(QList<QContact>)
       
    93 
       
    94 void tst_QVersit::testImportFiles()
       
    95 {
       
    96     QFETCH(QString, filename);
       
    97     QFETCH(QByteArray, charset);
       
    98     QFETCH(QList<QContact>, expectedContacts);
       
    99 
       
   100     QVersitReader reader;
       
   101     QFile file(filename);
       
   102     QVERIFY2(file.open(QIODevice::ReadOnly), filename.toAscii());
       
   103     reader.setDevice(&file);
       
   104     if (charset != "") {
       
   105         reader.setDefaultCodec(QTextCodec::codecForName(charset));
       
   106     }
       
   107     QVERIFY(reader.startReading());
       
   108     QVERIFY(reader.waitForFinished());
       
   109     QList<QVersitDocument> documents = reader.results();
       
   110     QCOMPARE(reader.error(), QVersitReader::NoError);
       
   111     QVersitContactImporter importer;
       
   112     MyQVersitResourceHandler resourceHandler;
       
   113     importer.setResourceHandler(&resourceHandler);
       
   114     QVERIFY(importer.importDocuments(documents));
       
   115     QList<QContact> contacts = importer.contacts();
       
   116 
       
   117     if (expectedContacts.size() > 0) {
       
   118         QCOMPARE(contacts.size(), expectedContacts.size());
       
   119         QListIterator<QContact> i(expectedContacts);
       
   120         foreach (QContact parsed, contacts) {
       
   121             QContact expected = i.next();
       
   122             QList<QContactDetail> expectedDetails = expected.details();
       
   123             foreach(QContactDetail expectedDetail, expectedDetails) {
       
   124                 QString name = expectedDetail.definitionName();
       
   125                 QContactDetail parsedDetail = parsed.detail(name);
       
   126                 if (parsedDetail != expectedDetail) {
       
   127                     qDebug() << "Detail: " << name.toAscii();
       
   128                     qDebug() << "Actual:" << parsedDetail.variantValues();
       
   129                     qDebug() << "Expected:" << expectedDetail.variantValues();
       
   130                     QCOMPARE(parsedDetail, expectedDetail);
       
   131                 }
       
   132             }
       
   133         }
       
   134     }
       
   135 }
       
   136 
       
   137 #define QTEST_NEW_ROW(filename,charset,contact) \
       
   138         QTest::newRow(filename) \
       
   139         << QString::fromAscii(TESTDATA_DIR "testdata/") + QString::fromAscii(filename) \
       
   140         << QByteArray(charset) \
       
   141         << (contact)
       
   142 
       
   143 void tst_QVersit::testImportFiles_data()
       
   144 {
       
   145     QTest::addColumn<QString>("filename");
       
   146     QTest::addColumn<QByteArray>("charset");
       
   147     QTest::addColumn<QList<QContact> >("expectedContacts");
       
   148 
       
   149     QTEST_NEW_ROW("AAB4-MultipleAll.vcf", "UTF-16BE", QList<QContact>());
       
   150     QTEST_NEW_ROW("AAB4-MultipleAscii.vcf", "", QList<QContact>());
       
   151     QTEST_NEW_ROW("AAB4-SingleCompany.vcf", "", QList<QContact>());
       
   152     QTEST_NEW_ROW("AAB4-SingleExtensive.vcf", "", QList<QContact>());
       
   153     QTEST_NEW_ROW("AAB4-SingleNonAscii.vcf", "UTF-16BE", QList<QContact>());
       
   154     QTEST_NEW_ROW("AAB4-SingleNonAsciiWithPhoto.vcf", "UTF-16BE", QList<QContact>());
       
   155     QTEST_NEW_ROW("AAB5-SingleNonAscii.vcf", "UTF-8", QList<QContact>());
       
   156 
       
   157     {
       
   158         QList<QContact> list;
       
   159         QContact contact;
       
   160         QContactName name;
       
   161         name.setCustomLabel(QLatin1String("Firstname Lastname"));
       
   162         name.setFirstName(QLatin1String("Firstname"));
       
   163         name.setLastName(QLatin1String("Lastname"));
       
   164         name.setMiddleName(QString());
       
   165         name.setPrefix(QLatin1String("Title"));
       
   166         name.setSuffix(QLatin1String("Suffix"));
       
   167         contact.saveDetail(&name);
       
   168         QContactOrganization org;
       
   169         org.setName(QLatin1String("Company Name"));
       
   170         org.setDepartment(QStringList(QLatin1String("Department Name")));
       
   171         org.setTitle(QLatin1String("Job title"));
       
   172         contact.saveDetail(&org);
       
   173         QContactNote note;
       
   174         note.setNote(QLatin1String("This is a note field.  Pretty boring."));
       
   175         contact.saveDetail(&note);
       
   176         QContactManagerEngine::setContactDisplayLabel(&contact, QLatin1String("Firstname Lastname"));
       
   177         list.append(contact);
       
   178         QContactUrl homeUrl;
       
   179         homeUrl.setUrl(QLatin1String("http://mywebpage.com"));
       
   180         homeUrl.setContexts(QContactDetail::ContextHome);
       
   181         contact.saveDetail(&homeUrl);
       
   182         QContactUrl workUrl;
       
   183         workUrl.setUrl(QLatin1String("http://workwebpage"));
       
   184         workUrl.setContexts(QContactDetail::ContextWork);
       
   185         contact.saveDetail(&workUrl);
       
   186         QTEST_NEW_ROW("Entourage11-basic.vcf", "UTF-16BE", list);
       
   187     }
       
   188 
       
   189     QTEST_NEW_ROW("Entourage11-image.vcf", "UTF-16BE", QList<QContact>());
       
   190 
       
   191     QTEST_NEW_ROW("Entourage11-nonascii.vcf", "UTF-16BE", QList<QContact>());
       
   192 
       
   193     {
       
   194         QList<QContact> list;
       
   195         QContact contact;
       
   196         QContactName name;
       
   197         name.setCustomLabel(QLatin1String("first last"));
       
   198         name.setFirstName(QLatin1String("first"));
       
   199         name.setLastName(QLatin1String("last"));
       
   200         name.setMiddleName(QString());
       
   201         name.setPrefix(QString());
       
   202         name.setSuffix(QString());
       
   203         contact.saveDetail(&name);
       
   204         QContactOrganization org;
       
   205         org.setName(QLatin1String("Nokia"));
       
   206         org.setDepartment(QStringList(QLatin1String("Qt DF")));
       
   207         contact.saveDetail(&org);
       
   208         QContactManagerEngine::setContactDisplayLabel(&contact, QLatin1String("first last"));
       
   209         list.append(contact);
       
   210         QTEST_NEW_ROW("Entourage12-basic.vcf", "", list);
       
   211     }
       
   212 
       
   213     QTEST_NEW_ROW("Entourage12-kevin.vcf", "UTF-8", QList<QContact>());
       
   214     QTEST_NEW_ROW("Entourage12-nonascii.vcf", "UTF-8", QList<QContact>());
       
   215     QTEST_NEW_ROW("gmail.vcf", "UTF-8", QList<QContact>());
       
   216 }
       
   217 
       
   218 QTEST_MAIN(tst_QVersit)