qtmobility/tests/auto/qversitwriter/tst_qversitwriter.cpp
changeset 4 90517678cc4f
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 "tst_qversitwriter.h"
       
    43 #include "qversitwriter.h"
       
    44 #include "qversitdocument.h"
       
    45 #include "qversitproperty.h"
       
    46 #include <QtTest/QtTest>
       
    47 #include <QByteArray>
       
    48 
       
    49 // Copied from tst_qcontactmanager.cpp
       
    50 // Waits until __expr is true and fails if it doesn't happen within 5s.
       
    51 #ifndef QTRY_VERIFY
       
    52 #define QTRY_VERIFY(__expr) \
       
    53         do { \
       
    54         const int __step = 50; \
       
    55         const int __timeout = 5000; \
       
    56         if (!(__expr)) { \
       
    57             QTest::qWait(0); \
       
    58         } \
       
    59         for (int __i = 0; __i < __timeout && !(__expr); __i+=__step) { \
       
    60             QTest::qWait(__step); \
       
    61         } \
       
    62         QVERIFY(__expr); \
       
    63     } while(0)
       
    64 #endif
       
    65 
       
    66 QTM_USE_NAMESPACE
       
    67 
       
    68 void tst_QVersitWriter::init()
       
    69 {
       
    70     mOutputDevice = new QBuffer;
       
    71     mWriter = new QVersitWriter;
       
    72     mSignalCatcher = new SignalCatcher;
       
    73     connect(mWriter, SIGNAL(stateChanged(QVersitWriter::State)),
       
    74             mSignalCatcher, SLOT(stateChanged(QVersitWriter::State)));
       
    75 }
       
    76 
       
    77 void tst_QVersitWriter::cleanup()
       
    78 {
       
    79     delete mWriter;
       
    80     delete mOutputDevice;
       
    81     delete mSignalCatcher;
       
    82 }
       
    83 
       
    84 void tst_QVersitWriter::testDevice()
       
    85 {
       
    86     // No device
       
    87     QVERIFY(mWriter->device() == NULL);
       
    88 
       
    89     // Device has been set
       
    90     mWriter->setDevice(mOutputDevice);
       
    91     QVERIFY(mWriter->device() == mOutputDevice);
       
    92 }
       
    93 
       
    94 void tst_QVersitWriter::testDefaultCodec()
       
    95 {
       
    96     QVERIFY(mWriter->defaultCodec() == 0);
       
    97     mWriter->setDefaultCodec(QTextCodec::codecForName("UTF-16BE"));
       
    98     QVERIFY(mWriter->defaultCodec() == QTextCodec::codecForName("UTF-16BE"));
       
    99 }
       
   100 
       
   101 void tst_QVersitWriter::testFold()
       
   102 {
       
   103     // 87 characters long
       
   104     QString longString(QLatin1String(
       
   105         "4567890123456789012345678901234567890123456789012345678901234567890123456"
       
   106         "234567890123456789012345678901234567890123456789012345678901234567890123456"
       
   107         "234567890123456789012"));
       
   108     QByteArray expected(
       
   109             "BEGIN:VCARD\r\n"
       
   110             "VERSION:2.1\r\n"
       
   111             "FN:4567890123456789012345678901234567890123456789012345678901234567890123456\r\n"
       
   112             " 234567890123456789012345678901234567890123456789012345678901234567890123456\r\n"
       
   113             " 234567890123456789012\r\n"
       
   114             "END:VCARD\r\n");
       
   115     QVersitDocument document;
       
   116     QVersitProperty property;
       
   117     property.setName(QLatin1String("FN"));
       
   118     property.setValue(longString);
       
   119     document.addProperty(property);
       
   120     document.setType(QVersitDocument::VCard21Type);
       
   121     QList<QVersitDocument> list;
       
   122     list.append(document);
       
   123     mWriter->setDevice(mOutputDevice);
       
   124     mOutputDevice->open(QBuffer::ReadWrite);
       
   125     QVERIFY(mWriter->startWriting(list));
       
   126     QVERIFY(mWriter->waitForFinished());
       
   127     QCOMPARE(mWriter->state(), QVersitWriter::FinishedState);
       
   128     QCOMPARE(mWriter->error(), QVersitWriter::NoError);
       
   129     mOutputDevice->seek(0);
       
   130     QByteArray result(mOutputDevice->readAll());
       
   131     QCOMPARE(result, expected);
       
   132 }
       
   133 
       
   134 void tst_QVersitWriter::testWriting21()
       
   135 {
       
   136     // vCard 2.1
       
   137     QByteArray vCard21(
       
   138 "BEGIN:VCARD\r\n\
       
   139 VERSION:2.1\r\n\
       
   140 FN:John\r\n\
       
   141 END:VCARD\r\n");
       
   142     QVersitDocument document;
       
   143     QVersitProperty property;
       
   144     property.setName(QString(QString::fromAscii("FN")));
       
   145     property.setValue(QString::fromAscii("John"));
       
   146     document.addProperty(property);
       
   147     document.setType(QVersitDocument::VCard21Type);
       
   148     QList<QVersitDocument> list;
       
   149     list.append(document);
       
   150 
       
   151     // Device not set
       
   152     QCOMPARE(mWriter->state(), QVersitWriter::InactiveState);
       
   153     QCOMPARE(mWriter->error(), QVersitWriter::NoError);
       
   154     QVERIFY(!mWriter->startWriting(list));
       
   155     QCOMPARE(mWriter->state(), QVersitWriter::InactiveState);
       
   156     QCOMPARE(mWriter->error(), QVersitWriter::IOError);
       
   157     QVERIFY(!mWriter->waitForFinished());
       
   158 
       
   159     // Device not opened
       
   160     mWriter->setDevice(mOutputDevice);
       
   161     QVERIFY(!mWriter->startWriting(list));
       
   162     QCOMPARE(mWriter->state(), QVersitWriter::InactiveState);
       
   163     QCOMPARE(mWriter->error(), QVersitWriter::IOError);
       
   164 
       
   165     // Now open the device and it should work.
       
   166     mOutputDevice->open(QBuffer::ReadWrite);
       
   167     QVERIFY(mWriter->startWriting(list));
       
   168     QVERIFY(mWriter->waitForFinished());
       
   169     QCOMPARE(mWriter->state(), QVersitWriter::FinishedState);
       
   170     QCOMPARE(mWriter->error(), QVersitWriter::NoError);
       
   171     mOutputDevice->seek(0);
       
   172     QByteArray result(mOutputDevice->readAll());
       
   173     QCOMPARE(result, vCard21);
       
   174 
       
   175     // Try some other codec
       
   176     delete mOutputDevice;
       
   177     mOutputDevice = new QBuffer;
       
   178     mOutputDevice->open(QBuffer::ReadWrite);
       
   179     mWriter->setDevice(mOutputDevice);
       
   180     QTextCodec* utf16(QTextCodec::codecForName("UTF-16"));
       
   181     mWriter->setDefaultCodec(utf16);
       
   182     QVERIFY(mWriter->startWriting(list));
       
   183     QVERIFY(mWriter->waitForFinished());
       
   184     QCOMPARE(mWriter->state(), QVersitWriter::FinishedState);
       
   185     QCOMPARE(mWriter->error(), QVersitWriter::NoError);
       
   186     mOutputDevice->seek(0);
       
   187     result = mOutputDevice->readAll();
       
   188     QByteArray expected(utf16->fromUnicode(QLatin1String(vCard21.data())));
       
   189     QString out;
       
   190     for (int i = 0; i < result.length(); i++) {
       
   191         QString t;
       
   192         out += t.sprintf("%02X ", (unsigned char)result.at(i));
       
   193     }
       
   194     QCOMPARE(result, expected);
       
   195 }
       
   196 
       
   197 void tst_QVersitWriter::testWriting30()
       
   198 {
       
   199     // vCard 3.0
       
   200     QByteArray vCard30(
       
   201 "BEGIN:VCARD\r\n\
       
   202 VERSION:3.0\r\n\
       
   203 FN:John\r\n\
       
   204 END:VCARD\r\n");
       
   205 
       
   206     QVersitDocument document;
       
   207     QVersitProperty property;
       
   208     property.setName(QString(QString::fromAscii("FN")));
       
   209     property.setValue(QString::fromAscii("John"));
       
   210     document.addProperty(property);
       
   211     document.setType(QVersitDocument::VCard30Type);
       
   212     QList<QVersitDocument> list;
       
   213     list.append(document);
       
   214 
       
   215     // Basic 3.0 test
       
   216     mOutputDevice->open(QBuffer::ReadWrite);
       
   217     mWriter->setDevice(mOutputDevice);
       
   218     QVERIFY(mWriter->startWriting(list));
       
   219     QVERIFY(mWriter->waitForFinished());
       
   220     QCOMPARE(mWriter->state(), QVersitWriter::FinishedState);
       
   221     QCOMPARE(mWriter->error(), QVersitWriter::NoError);
       
   222     mOutputDevice->seek(0);
       
   223     QByteArray result(mOutputDevice->readAll());
       
   224     QCOMPARE(result, vCard30);
       
   225 
       
   226     // Asynchronous writing
       
   227     mOutputDevice->reset();
       
   228     mSignalCatcher->mReceived.clear();
       
   229     QVERIFY(mWriter->startWriting(list));
       
   230     QTRY_VERIFY(mSignalCatcher->mReceived.count() >= 2);
       
   231     QCOMPARE(mSignalCatcher->mReceived.at(0), QVersitWriter::ActiveState);
       
   232     QCOMPARE(mSignalCatcher->mReceived.at(1), QVersitWriter::FinishedState);
       
   233 
       
   234     // Cancelling
       
   235     delete mOutputDevice;
       
   236     mOutputDevice = new QBuffer;
       
   237     mOutputDevice->open(QBuffer::ReadWrite);
       
   238     mSignalCatcher->mReceived.clear();
       
   239     mWriter->setDevice(mOutputDevice);
       
   240     mWriter->startWriting(list);
       
   241     mWriter->cancel();
       
   242     mWriter->waitForFinished();
       
   243     QTRY_VERIFY(mSignalCatcher->mReceived.count() >= 2);
       
   244     QCOMPARE(mSignalCatcher->mReceived.at(0), QVersitWriter::ActiveState);
       
   245     QVersitWriter::State state(mSignalCatcher->mReceived.at(1));
       
   246     // It's possible that it finishes before it cancels.
       
   247     QVERIFY(state == QVersitWriter::CanceledState
       
   248             || state == QVersitWriter::FinishedState);
       
   249 }
       
   250 
       
   251 void tst_QVersitWriter::testByteArrayOutput()
       
   252 {
       
   253     const QByteArray vCard30(
       
   254         "BEGIN:VCARD\r\n"
       
   255         "VERSION:3.0\r\n"
       
   256         "FN:John\r\n"
       
   257         "END:VCARD\r\n");
       
   258 
       
   259     delete mWriter; // we don't want the init()ed writer.
       
   260 
       
   261     QByteArray output;
       
   262     mWriter = new QVersitWriter(&output);
       
   263 
       
   264     QVERIFY(mWriter->device() == 0);
       
   265 
       
   266     QVersitDocument document(QVersitDocument::VCard30Type);
       
   267     QVersitProperty property;
       
   268     property.setName(QString(QString::fromAscii("FN")));
       
   269     property.setValue(QString::fromAscii("John"));
       
   270     document.addProperty(property);
       
   271     QVERIFY(mWriter->startWriting(QList<QVersitDocument>() << document));
       
   272     QVERIFY(mWriter->waitForFinished());
       
   273     QCOMPARE(output, vCard30);
       
   274 
       
   275 }
       
   276 
       
   277 QTEST_MAIN(tst_QVersitWriter)