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