qtcontactsmobility/tests/auto/qversitreader/ut_qversitreader.cpp
changeset 25 76a2435edfd4
parent 24 0ba2181d7c28
equal deleted inserted replaced
24:0ba2181d7c28 25:76a2435edfd4
    40 ****************************************************************************/
    40 ****************************************************************************/
    41 
    41 
    42 #include "ut_qversitreader.h"
    42 #include "ut_qversitreader.h"
    43 #include "qversitreader.h"
    43 #include "qversitreader.h"
    44 #include "qversitreader_p.h"
    44 #include "qversitreader_p.h"
       
    45 #include "versitutils_p.h"
    45 #include <QtTest/QtTest>
    46 #include <QtTest/QtTest>
       
    47 #include <QSignalSpy>
       
    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 // This says "NOKIA" in Katakana encoded with UTF-8
       
    67 const QByteArray KATAKANA_NOKIA("\xe3\x83\x8e\xe3\x82\xad\xe3\x82\xa2");
    46 
    68 
    47 QTM_USE_NAMESPACE
    69 QTM_USE_NAMESPACE
    48 
    70 
    49 void UT_QVersitReader::init()
    71 void UT_QVersitReader::init()
    50 {
    72 {
    51     mReadingDoneCalled = false;
       
    52     mExpectedDocumentCount = 0;
       
    53     mInputDevice = new QBuffer;
    73     mInputDevice = new QBuffer;
    54     mInputDevice->open(QBuffer::ReadWrite);
    74     mInputDevice->open(QBuffer::ReadWrite);
    55     mReader = new QVersitReader;
    75     mReader = new QVersitReader;
    56     connect(mReader,SIGNAL(readingDone()),this,SLOT(readingDone()),Qt::DirectConnection);
       
    57     mReaderPrivate = new QVersitReaderPrivate;
    76     mReaderPrivate = new QVersitReaderPrivate;
       
    77     mSignalCatcher = new SignalCatcher;
       
    78     qRegisterMetaType<QVersitReader::State>("QVersitReader::State");
       
    79     connect(mReader, SIGNAL(stateChanged(QVersitReader::State)),
       
    80             mSignalCatcher, SLOT(stateChanged(QVersitReader::State)));
       
    81     connect(mReader, SIGNAL(resultsAvailable()),
       
    82             mSignalCatcher, SLOT(resultsAvailable()));
       
    83     mAsciiCodec = QTextCodec::codecForName("ISO 8859-1");
    58 }
    84 }
    59 
    85 
    60 void UT_QVersitReader::cleanup()
    86 void UT_QVersitReader::cleanup()
    61 {   
    87 {   
    62     delete mReaderPrivate;
    88     delete mReaderPrivate;
    63     delete mReader;
    89     delete mReader;
    64     delete mInputDevice;
    90     delete mInputDevice;
    65 }
    91     delete mSignalCatcher;
    66 
       
    67 void UT_QVersitReader::readingDone()
       
    68 {
       
    69     QCOMPARE(mReader->result().count(),mExpectedDocumentCount);
       
    70     mReadingDoneCalled = true;
       
    71 }
    92 }
    72 
    93 
    73 void UT_QVersitReader::testDevice()
    94 void UT_QVersitReader::testDevice()
    74 {
    95 {
    75     // No device
    96     // No device
    76     QVERIFY(mReader->device() == NULL);    
    97     QVERIFY(mReader->device() == NULL);    
    77     
    98     
    78     // Device has been set
    99     // Device has been set
    79     mReader->setDevice(mInputDevice);
   100     mReader->setDevice(mInputDevice);
    80     QVERIFY(mReader->device() == mInputDevice);
   101     QVERIFY(mReader->device() == mInputDevice);
       
   102 
       
   103     delete mInputDevice;
       
   104     QVERIFY(mReader->device() == NULL);
       
   105 
       
   106     mInputDevice = new QBuffer;
       
   107     mInputDevice->open(QBuffer::ReadWrite);
       
   108 
       
   109     QVERIFY(mReader->device() == NULL);
       
   110     mReader->setDevice(mInputDevice);
       
   111     QVERIFY(mReader->device() == mInputDevice);
       
   112 }
       
   113 
       
   114 void UT_QVersitReader::testDefaultCodec()
       
   115 {
       
   116     QVERIFY(mReader->defaultCodec() == QTextCodec::codecForName("UTF-8"));
       
   117     mReader->setDefaultCodec(QTextCodec::codecForName("UTF-16BE"));
       
   118     QVERIFY(mReader->defaultCodec() == QTextCodec::codecForName("UTF-16BE"));
    81 }
   119 }
    82 
   120 
    83 void UT_QVersitReader::testReading()
   121 void UT_QVersitReader::testReading()
    84 {
   122 {
    85     // No I/O device set
   123     // No I/O device set
    86     QVERIFY(!mReader->readAll());
   124     QVERIFY(!mReader->startReading());
       
   125     QCOMPARE(mReader->error(), QVersitReader::IOError);
    87     
   126     
    88     // Device set, no data
   127     // Device set, no data
    89     mReader->setDevice(mInputDevice);
   128     mReader->setDevice(mInputDevice);
    90     mInputDevice->open(QBuffer::ReadWrite);
   129     mInputDevice->open(QBuffer::ReadOnly);
    91     QVERIFY(!mReader->readAll());
   130     QVERIFY(mReader->startReading());
    92     QCOMPARE(mReader->result().count(),0);
   131     QVERIFY(mReader->waitForFinished());
       
   132     QList<QVersitDocument> results(mReader->results());
       
   133     QCOMPARE(mReader->state(), QVersitReader::FinishedState);
       
   134     QCOMPARE(mReader->error(), QVersitReader::NoError);
       
   135     QCOMPARE(results.count(),0);
    93 
   136 
    94     // Device set, one document
   137     // Device set, one document
    95     const QByteArray& oneDocument = 
   138     const QByteArray& oneDocument = 
    96         "BEGIN:VCARD\r\nVERSION:2.1\r\nFN:John\r\nEND:VCARD\r\n";
   139         "BEGIN:VCARD\r\nVERSION:2.1\r\nFN:John\r\nEND:VCARD\r\n";
    97     mInputDevice->write(oneDocument);
   140     mInputDevice->close();
       
   141     mInputDevice->setData(oneDocument);
       
   142     mInputDevice->open(QBuffer::ReadOnly);
    98     mInputDevice->seek(0);
   143     mInputDevice->seek(0);
    99     QVERIFY(mReader->readAll());
   144     QVERIFY(mReader->startReading());
   100     QCOMPARE(mReader->result().count(),1);
   145     QVERIFY(mReader->waitForFinished());
       
   146     results = mReader->results();
       
   147     QCOMPARE(mReader->state(), QVersitReader::FinishedState);
       
   148     QCOMPARE(mReader->error(), QVersitReader::NoError);
       
   149     QCOMPARE(results.count(),1);
       
   150 
       
   151     // Wide charset with no byte-order mark
       
   152     QTextCodec* codec = QTextCodec::codecForName("UTF-16BE");
       
   153     const QByteArray& wideDocument =
       
   154             VersitUtils::encode("BEGIN:VCARD\r\nVERSION:2.1\r\nFN:John\r\nEND:VCARD\r\n", codec);
       
   155     mInputDevice->close();
       
   156     mInputDevice->setData(wideDocument);
       
   157     mInputDevice->open(QBuffer::ReadOnly);
       
   158     mInputDevice->seek(0);
       
   159     mReader->setDefaultCodec(codec);
       
   160     QVERIFY(mReader->startReading());
       
   161     QVERIFY(mReader->waitForFinished());
       
   162     results = mReader->results();
       
   163     QCOMPARE(mReader->state(), QVersitReader::FinishedState);
       
   164     QCOMPARE(mReader->error(), QVersitReader::NoError);
       
   165     QCOMPARE(mReader->results().count(),1);
       
   166     mReader->setDefaultCodec(NULL);
   101 
   167 
   102     // Two documents
   168     // Two documents
   103     const QByteArray& twoDocuments =
   169     const QByteArray& twoDocuments =
   104         "BEGIN:VCARD\r\nFN:Jenny\r\nEND:VCARD\r\nBEGIN:VCARD\r\nFN:Jake\r\nEND:VCARD\r\n";
   170         " \r\n BEGIN:VCARD\r\nFN:Jenny\r\nEND:VCARD\r\nBEGIN:VCARD\r\nFN:Jake\r\nEND:VCARD\r\n";
   105     delete mInputDevice;
   171     mInputDevice->close();
   106     mInputDevice = 0;
   172     mInputDevice->setData(twoDocuments);
   107     mInputDevice = new QBuffer;
   173     mInputDevice->open(QBuffer::ReadOnly);
   108     mInputDevice->open(QBuffer::ReadWrite);
       
   109     mInputDevice->write(twoDocuments);
       
   110     mInputDevice->seek(0);
   174     mInputDevice->seek(0);
   111     QVERIFY(mReader->readAll());
   175     QVERIFY(mReader->startReading());
   112     QCOMPARE(mReader->result().count(),2);
   176     QVERIFY(mReader->waitForFinished());
       
   177     results = mReader->results();
       
   178     QCOMPARE(mReader->state(), QVersitReader::FinishedState);
       
   179     QCOMPARE(mReader->error(), QVersitReader::NoError);
       
   180     QCOMPARE(results.count(),2);
       
   181 
       
   182     // Erroneous document (missing property name)
       
   183     mInputDevice->close();
       
   184     mInputDevice->setData(QByteArray(
       
   185             "BEGIN:VCARD\r\nFN:Jenny\r\n;Jenny;;;\r\nEND:VCARD\r\n"
       
   186             "BEGIN:VCARD\r\nFN:Jake\r\nEND:VCARD\r\n"));
       
   187     mInputDevice->open(QBuffer::ReadOnly);
       
   188     mInputDevice->seek(0);
       
   189     QVERIFY(mReader->startReading());
       
   190     QVERIFY(mReader->waitForFinished());
       
   191     results = mReader->results();
       
   192     QCOMPARE(mReader->state(), QVersitReader::FinishedState);
       
   193     QCOMPARE(mReader->error(), QVersitReader::ParseError);
       
   194     QCOMPARE(results.count(), 1);
   113 
   195 
   114     // Valid documents and a grouped document between them
   196     // Valid documents and a grouped document between them
   115     const QByteArray& validDocumentsAndGroupedDocument =
   197     const QByteArray& validDocumentsAndGroupedDocument =
   116 "BEGIN:VCARD\r\nFN:Jenny\r\nEND:VCARD\r\n\
   198 "BEGIN:VCARD\r\nFN:Jenny\r\nEND:VCARD\r\n\
   117 BEGIN:VCARD\r\n\
   199 BEGIN:VCARD\r\n\
   120 BEGIN:VCARD\r\nFN:Jeffery\r\nEND:VCARD\r\n\
   202 BEGIN:VCARD\r\nFN:Jeffery\r\nEND:VCARD\r\n\
   121 END:VCARD\r\n\
   203 END:VCARD\r\n\
   122 BEGIN:VCARD\r\nFN:Jake\r\nEND:VCARD\r\n\
   204 BEGIN:VCARD\r\nFN:Jake\r\nEND:VCARD\r\n\
   123 BEGIN:VCARD\r\nFN:James\r\nEND:VCARD\r\n\
   205 BEGIN:VCARD\r\nFN:James\r\nEND:VCARD\r\n\
   124 BEGIN:VCARD\r\nFN:Jane\r\nEND:VCARD\r\n";
   206 BEGIN:VCARD\r\nFN:Jane\r\nEND:VCARD\r\n";
   125     delete mInputDevice;
   207     mInputDevice->close();
   126     mInputDevice = 0;
   208     mInputDevice->setData(validDocumentsAndGroupedDocument);
   127     mInputDevice = new QBuffer;
       
   128     mInputDevice->open(QBuffer::ReadWrite);
   209     mInputDevice->open(QBuffer::ReadWrite);
   129     mInputDevice->write(validDocumentsAndGroupedDocument);
       
   130     mInputDevice->seek(0);
   210     mInputDevice->seek(0);
   131     QVERIFY(mReader->readAll());
   211     QVERIFY(mReader->startReading());
   132     QCOMPARE(mReader->result().count(),4);
   212     QVERIFY(mReader->waitForFinished());
       
   213     results = mReader->results();
       
   214     QCOMPARE(mReader->state(), QVersitReader::FinishedState);
       
   215     // An error is logged because one failed, but the rest are readable.
       
   216     QCOMPARE(mReader->error(), QVersitReader::ParseError);
       
   217     QCOMPARE(results.count(),4);
       
   218 
       
   219     // Valid documents and a grouped document between them
       
   220     const QByteArray& validDocumentsAndGroupedDocument2 =
       
   221 "BEGIN:VCARD\r\nFN:Jenny\r\nEND:VCARD\r\n\
       
   222 BEGIN:VCARD\r\n\
       
   223 X-GROUPING:pub gang\r\n\
       
   224 BEGIN:VCARD\r\nFN:Jeremy\r\nEND:VCARD\r\n\
       
   225 BEGIN:VCARD\r\nFN:Jeffery\r\nEND:VCARD\r\n\
       
   226 END:VCARD\r\n\
       
   227 BEGIN:VCARD\r\nFN:Jake\r\nEND:VCARD\r\n\
       
   228 BEGIN:VCARD\r\nFN:James\r\nEND:VCARD\r\n\
       
   229 BEGIN:VCARD\r\nFN:Jane\r\nEND:VCARD";
       
   230     mInputDevice->close();
       
   231     mInputDevice->setData(validDocumentsAndGroupedDocument2);
       
   232     mInputDevice->open(QBuffer::ReadWrite);
       
   233     mInputDevice->seek(0);
       
   234     QVERIFY(mReader->startReading());
       
   235     QVERIFY(mReader->waitForFinished());
       
   236     results = mReader->results();
       
   237     QCOMPARE(mReader->state(), QVersitReader::FinishedState);
       
   238     // An error is logged because one failed, but the rest are readable.
       
   239     QCOMPARE(mReader->error(), QVersitReader::ParseError);
       
   240     QCOMPARE(mReader->results().count(),4);
   133 
   241 
   134     // Asynchronous reading
   242     // Asynchronous reading
   135     QVERIFY(!mReadingDoneCalled);
   243     mInputDevice->close();
   136     delete mInputDevice;
   244     mInputDevice->setData(twoDocuments);
   137     mInputDevice = 0;
       
   138     mInputDevice = new QBuffer;
       
   139     mInputDevice->open(QBuffer::ReadWrite);
   245     mInputDevice->open(QBuffer::ReadWrite);
   140     mInputDevice->write(oneDocument);
       
   141     mInputDevice->seek(0);
   246     mInputDevice->seek(0);
   142     mExpectedDocumentCount = 1;
   247     mSignalCatcher->mStateChanges.clear();
       
   248     mSignalCatcher->mResultsCount = 0;
   143     QVERIFY(mReader->startReading());
   249     QVERIFY(mReader->startReading());
   144     delete mReader; // waits for the thread to finish
   250     QTRY_VERIFY(mSignalCatcher->mStateChanges.count() >= 2);
   145     mReader = 0;
   251     QCOMPARE(mSignalCatcher->mStateChanges.at(0), QVersitReader::ActiveState);
   146     QVERIFY(mReadingDoneCalled);
   252     QCOMPARE(mSignalCatcher->mStateChanges.at(1), QVersitReader::FinishedState);
       
   253     QVERIFY(mSignalCatcher->mResultsCount >= 2);
       
   254     QCOMPARE(mReader->results().size(), 2);
       
   255     QCOMPARE(mReader->error(), QVersitReader::NoError);
       
   256 
       
   257     // Cancelling
       
   258     mInputDevice->close();
       
   259     mInputDevice->setData(twoDocuments);
       
   260     mInputDevice->open(QBuffer::ReadOnly);
       
   261     mInputDevice->seek(0);
       
   262     mSignalCatcher->mStateChanges.clear();
       
   263     mSignalCatcher->mResultsCount = 0;
       
   264     QVERIFY(mReader->startReading());
       
   265     mReader->cancel();
       
   266     mReader->waitForFinished();
       
   267     QTRY_VERIFY(mSignalCatcher->mStateChanges.count() >= 2);
       
   268     QCOMPARE(mSignalCatcher->mStateChanges.at(0), QVersitReader::ActiveState);
       
   269     QVersitReader::State state(mSignalCatcher->mStateChanges.at(1));
       
   270     // It's possible that it finishes before it cancels.
       
   271     QVERIFY(state == QVersitReader::CanceledState
       
   272             || state == QVersitReader::FinishedState);
   147 }
   273 }
   148 
   274 
   149 void UT_QVersitReader::testResult()
   275 void UT_QVersitReader::testResult()
   150 {
   276 {
   151     QCOMPARE(mReader->result().count(),0);
   277     QCOMPARE(mReader->results().count(),0);
   152 }
   278 }
   153 
   279 
   154 void UT_QVersitReader::testSetVersionFromProperty()
   280 void UT_QVersitReader::testSetVersionFromProperty()
   155 {
   281 {
   156     QVersitDocument document;
   282     QVersitDocument document;
   160     property.setName(QString::fromAscii("N"));
   286     property.setName(QString::fromAscii("N"));
   161     QVERIFY(mReaderPrivate->setVersionFromProperty(document,property));
   287     QVERIFY(mReaderPrivate->setVersionFromProperty(document,property));
   162     
   288     
   163     // VERSION property with 2.1
   289     // VERSION property with 2.1
   164     property.setName(QString::fromAscii("VERSION"));
   290     property.setName(QString::fromAscii("VERSION"));
   165     property.setValue(QByteArray("2.1"));
   291     property.setValue(QString::fromAscii("2.1"));
   166     QVERIFY(mReaderPrivate->setVersionFromProperty(document,property));
   292     QVERIFY(mReaderPrivate->setVersionFromProperty(document,property));
   167     QVERIFY(document.versitType() == QVersitDocument::VCard21);
   293     QVERIFY(document.type() == QVersitDocument::VCard21Type);
   168     
   294     
   169     // VERSION property with 3.0
   295     // VERSION property with 3.0
   170     property.setValue(QByteArray("3.0"));
   296     property.setValue(QString::fromAscii("3.0"));
   171     QVERIFY(mReaderPrivate->setVersionFromProperty(document,property));
   297     QVERIFY(mReaderPrivate->setVersionFromProperty(document,property));
   172     QVERIFY(document.versitType() == QVersitDocument::VCard30);
   298     QVERIFY(document.type() == QVersitDocument::VCard30Type);
   173 
   299 
   174     // VERSION property with a not supported value
   300     // VERSION property with a not supported value
   175     property.setValue(QByteArray("4.0"));
   301     property.setValue(QString::fromAscii("4.0"));
   176     QVERIFY(!mReaderPrivate->setVersionFromProperty(document,property));
   302     QVERIFY(!mReaderPrivate->setVersionFromProperty(document,property));
   177     
   303     
   178     // VERSION property with BASE64 encoded supported value
   304     // VERSION property with BASE64 encoded supported value
   179     property.setValue(QByteArray("2.1").toBase64());
   305     property.setValue(QString::fromAscii(QByteArray("2.1").toBase64()));
   180     property.addParameter(QString::fromAscii("ENCODING"),QString::fromAscii("BASE64"));
   306     property.insertParameter(QString::fromAscii("ENCODING"),QString::fromAscii("BASE64"));
   181     QVERIFY(mReaderPrivate->setVersionFromProperty(document,property));
   307     QVERIFY(mReaderPrivate->setVersionFromProperty(document,property));
   182     QVERIFY(document.versitType() == QVersitDocument::VCard21);
   308     QVERIFY(document.type() == QVersitDocument::VCard21Type);
   183     
   309     
   184     // VERSION property with BASE64 encoded not supported value
   310     // VERSION property with BASE64 encoded not supported value
   185     property.setValue(QByteArray("4.0").toBase64());
   311     property.setValue(QString::fromAscii(QByteArray("4.0").toBase64()));
   186     QVERIFY(!mReaderPrivate->setVersionFromProperty(document,property));
   312     QVERIFY(!mReaderPrivate->setVersionFromProperty(document,property));
   187 }
   313 }
   188 
   314 
   189 void UT_QVersitReader::testParseNextVersitPropertyVCard21()
   315 void UT_QVersitReader::testParseNextVersitPropertyVCard21()
   190 {
   316 {
   191     QVersitDocument::VersitType type = QVersitDocument::VCard21;
   317     QVersitDocument::VersitType type = QVersitDocument::VCard21Type;
   192 
   318 
   193     // Test a valid vCard 2.1 with properties having separate handling:
   319     // Test a valid vCard 2.1 with properties having separate handling:
   194     // AGENT property, some property with parameter ENCODING=QUOTED-PRINTABLE 
   320     // AGENT property, ENCODING parameters (BASE64 and QUOTED-PRINTABLE) and CHARSET parameter
   195     // and some other property without this parameter
   321     QTextCodec* codec = QTextCodec::codecForName("UTF-16BE");
   196     QByteArray vCard("Begin:vcard\r\n");
   322     QByteArray vCard("Begin:vcard\r\n");
   197     vCard.append("VERSION:2.1\r\n");
   323     vCard.append("VERSION:2.1\r\n");
   198     vCard.append("FN:John\r\n");
   324     vCard.append("FN:John\r\n");
       
   325     vCard.append("ORG;CHARSET=UTF-8:");
       
   326     vCard.append(KATAKANA_NOKIA);
       
   327     vCard.append("\r\n");
       
   328     // "NOKIA" in Katakana, UTF-8 encoded, then base-64 encoded:
       
   329     vCard.append("NOTE;ENCODING=BASE64;CHARSET=UTF-8:");
       
   330     vCard.append(KATAKANA_NOKIA.toBase64());
       
   331     vCard.append("\r\n");
       
   332     // The value here is "UXQgaXMgZ3JlYXQh", which is the base64 encoding of "Qt is great!".
   199     vCard.append("PHOTO;ENCODING=BASE64: U\t XQgaX MgZ\t3Jl YXQh\r\n\r\n");
   333     vCard.append("PHOTO;ENCODING=BASE64: U\t XQgaX MgZ\t3Jl YXQh\r\n\r\n");
       
   334     // Again, but without the explicity "ENCODING" parameter
       
   335     vCard.append("PHOTO;BASE64: U\t XQgaX MgZ\t3Jl YXQh\r\n\r\n");
   200     vCard.append("HOME.Springfield.EMAIL;Encoding=Quoted-Printable:john.citizen=40exam=\r\nple.com\r\n");
   336     vCard.append("HOME.Springfield.EMAIL;Encoding=Quoted-Printable:john.citizen=40exam=\r\nple.com\r\n");
       
   337     vCard.append("EMAIL;ENCODING=QUOTED-PRINTABLE;CHARSET=UTF-16BE:");
       
   338     vCard.append(codec->fromUnicode(QLatin1String("john.citizen=40exam=\r\nple.com")));
       
   339     vCard.append("\r\n");
   201     vCard.append("AGENT:\r\nBEGIN:VCARD\r\nFN:Jenny\r\nEND:VCARD\r\n\r\n");
   340     vCard.append("AGENT:\r\nBEGIN:VCARD\r\nFN:Jenny\r\nEND:VCARD\r\n\r\n");
   202     vCard.append("End:VCARD\r\n");
   341     vCard.append("End:VCARD\r\n");
   203 
   342     QBuffer buffer(&vCard);
   204     QVersitProperty property = mReaderPrivate->parseNextVersitProperty(type,vCard);
   343     buffer.open(QIODevice::ReadOnly);
       
   344     LineReader lineReader(&buffer, mAsciiCodec);
       
   345 
       
   346     QVersitProperty property = mReaderPrivate->parseNextVersitProperty(type, lineReader);
   205     QCOMPARE(property.name(),QString::fromAscii("BEGIN"));
   347     QCOMPARE(property.name(),QString::fromAscii("BEGIN"));
   206     QCOMPARE(property.value(),QByteArray("vcard"));
   348     QCOMPARE(property.value(),QString::fromAscii("vcard"));
   207     
   349     
   208     property = mReaderPrivate->parseNextVersitProperty(type,vCard);
   350     property = mReaderPrivate->parseNextVersitProperty(type, lineReader);
   209     QCOMPARE(property.name(),QString::fromAscii("VERSION"));
   351     QCOMPARE(property.name(),QString::fromAscii("VERSION"));
   210     QCOMPARE(property.value(),QByteArray("2.1"));
   352     QCOMPARE(property.value(),QString::fromAscii("2.1"));
   211     
   353 
   212     property = mReaderPrivate->parseNextVersitProperty(type,vCard);
   354     property = mReaderPrivate->parseNextVersitProperty(type, lineReader);
   213     QCOMPARE(property.name(),QString::fromAscii("FN"));
   355     QCOMPARE(property.name(),QString::fromAscii("FN"));
   214     QCOMPARE(property.value(),QByteArray("John"));
   356     QCOMPARE(property.value(),QString::fromAscii("John"));
   215     
   357 
   216     property = mReaderPrivate->parseNextVersitProperty(type,vCard);
   358     property = mReaderPrivate->parseNextVersitProperty(type, lineReader);
       
   359     QCOMPARE(property.name(),QString::fromAscii("ORG"));
       
   360     QCOMPARE(property.value(),QString::fromUtf8(KATAKANA_NOKIA));
       
   361 
       
   362     property = mReaderPrivate->parseNextVersitProperty(type, lineReader);
       
   363     QCOMPARE(property.name(),QString::fromAscii("NOTE"));
       
   364     QCOMPARE(property.value(),QString::fromUtf8(KATAKANA_NOKIA));
       
   365     
       
   366     property = mReaderPrivate->parseNextVersitProperty(type, lineReader);
   217     QCOMPARE(property.name(),QString::fromAscii("PHOTO"));
   367     QCOMPARE(property.name(),QString::fromAscii("PHOTO"));
   218     QCOMPARE(1,property.parameters().count());
   368     // Linear whitespaces (SPACEs and TABs) removed from the value and base64 decoded:
   219     // Linear whitespaces (SPACEs and TABs) removed from the value:
   369     QCOMPARE(property.variantValue().type(), QVariant::ByteArray);
   220     QCOMPARE(property.value(),QByteArray("UXQgaXMgZ3JlYXQh"));
   370     QCOMPARE(property.value<QByteArray>(), QByteArray("Qt is great!"));
   221 
   371     // Ensure that base-64 encoded strings can be retrieved as strings.
   222     property = mReaderPrivate->parseNextVersitProperty(type,vCard);
   372     QCOMPARE(property.value(), QLatin1String("Qt is great!"));
       
   373 
       
   374     property = mReaderPrivate->parseNextVersitProperty(type, lineReader);
       
   375     QCOMPARE(property.name(),QString::fromAscii("PHOTO"));
       
   376     QCOMPARE(property.variantValue().type(), QVariant::ByteArray);
       
   377     QCOMPARE(property.value<QByteArray>(), QByteArray("Qt is great!"));
       
   378     QCOMPARE(property.value(), QLatin1String("Qt is great!"));
       
   379 
       
   380     property = mReaderPrivate->parseNextVersitProperty(type, lineReader);
   223     QStringList propertyGroup(QString::fromAscii("HOME"));
   381     QStringList propertyGroup(QString::fromAscii("HOME"));
   224     propertyGroup.append(QString::fromAscii("Springfield"));
   382     propertyGroup.append(QString::fromAscii("Springfield"));
   225     QCOMPARE(property.groups(),propertyGroup);
   383     QCOMPARE(property.groups(),propertyGroup);
   226     QCOMPARE(property.name(),QString::fromAscii("EMAIL"));
   384     QCOMPARE(property.name(),QString::fromAscii("EMAIL"));
   227     QCOMPARE(0,property.parameters().count());
   385     QCOMPARE(0,property.parameters().count());
   228     QCOMPARE(property.value(),QByteArray("john.citizen@example.com"));
   386     QCOMPARE(property.value(),QString::fromAscii("john.citizen@example.com"));
   229     
   387 
   230     property = mReaderPrivate->parseNextVersitProperty(type,vCard);
   388     property = mReaderPrivate->parseNextVersitProperty(type, lineReader);
       
   389     QCOMPARE(property.name(),QString::fromAscii("EMAIL"));
       
   390     // The base64 parameter should be stripped by the reader.
       
   391     QCOMPARE(property.parameters().count(), 0);
       
   392     QCOMPARE(property.value(),QString::fromAscii("john.citizen@example.com"));
       
   393 
       
   394     property = mReaderPrivate->parseNextVersitProperty(type, lineReader);
   231     QCOMPARE(property.name(),QString::fromAscii("AGENT"));
   395     QCOMPARE(property.name(),QString::fromAscii("AGENT"));
   232     QCOMPARE(property.value(),QByteArray());
   396     QCOMPARE(property.value(),QString());
   233     QCOMPARE(property.embeddedDocument().properties().count(),1);
   397     QVERIFY(property.variantValue().userType() == qMetaTypeId<QVersitDocument>());
   234     
   398     QCOMPARE(property.value<QVersitDocument>().properties().count(), 1);
   235     property = mReaderPrivate->parseNextVersitProperty(type,vCard);
   399     
       
   400     property = mReaderPrivate->parseNextVersitProperty(type, lineReader);
   236     QCOMPARE(property.name(),QString::fromAscii("END"));
   401     QCOMPARE(property.name(),QString::fromAscii("END"));
   237     QCOMPARE(property.value(),QByteArray("VCARD"));
   402     QCOMPARE(property.value(),QString::fromAscii("VCARD"));
   238     
   403     
   239     property = mReaderPrivate->parseNextVersitProperty(type,vCard);
   404     property = mReaderPrivate->parseNextVersitProperty(type, lineReader);
   240     QCOMPARE(property.name(),QString());
   405     QCOMPARE(property.name(),QString());
   241     QCOMPARE(property.value(),QByteArray());
   406     QCOMPARE(property.value(),QString());
   242     
   407     
   243     // Simulate a situation where the document nesting level is exceeded
   408     // Simulate a situation where the document nesting level is exceeded
   244     // In practice this would mean a big number of nested AGENT properties
   409     // In practice this would mean a big number of nested AGENT properties
   245     mReaderPrivate->mDocumentNestingLevel = 20;
   410     mReaderPrivate->mDocumentNestingLevel = 20;
   246     QByteArray agentProperty("AGENT:BEGIN:VCARD\r\nN:Jenny\r\nEND:VCARD\r\n\r\n");
   411     QByteArray agentProperty("AGENT:BEGIN:VCARD\r\nN:Jenny\r\nEND:VCARD\r\n\r\n");
   247     property = mReaderPrivate->parseNextVersitProperty(type,agentProperty);
   412     buffer.close();
   248     QCOMPARE(property.name(),QString());
   413     buffer.setData(agentProperty);
   249     QCOMPARE(property.embeddedDocument().properties().count(),0);
   414     buffer.open(QIODevice::ReadOnly);
   250     QCOMPARE(property.value(),QByteArray());
   415     LineReader agentLineReader(&buffer, mAsciiCodec);
       
   416 
       
   417     property = mReaderPrivate->parseNextVersitProperty(type, agentLineReader);
       
   418     QVERIFY(property.isEmpty());
   251 }
   419 }
   252 
   420 
   253 void UT_QVersitReader::testParseNextVersitPropertyVCard30()
   421 void UT_QVersitReader::testParseNextVersitPropertyVCard30()
   254 {
   422 {
   255     QVersitDocument::VersitType type = QVersitDocument::VCard30;
   423     QVersitDocument::VersitType type = QVersitDocument::VCard30Type;
   256 
   424 
   257     // Test a valid vCard 3.0 with properties having separate handling:
   425     // Test a valid vCard 3.0 with properties having separate handling:
   258     // AGENT property and some other property
   426     // AGENT property and some other property
   259     QByteArray vCard("Begin:vcard\r\n");
   427     QByteArray vCard("Begin:vcard\r\n");
   260     vCard.append("VERSION:3.0\r\n");
   428     vCard.append("VERSION:3.0\r\n");
   261     vCard.append("FN:John\r\n");
   429     vCard.append("FN:John\r\n");
       
   430     vCard.append("ORG;CHARSET=UTF-8:");
       
   431     vCard.append(KATAKANA_NOKIA);
       
   432     vCard.append("\r\n");
       
   433     // "NOKIA" in Katakana, UTF-8 encoded, then base-64 encoded:
       
   434     vCard.append("NOTE;ENCODING=B;CHARSET=UTF-8:");
       
   435     vCard.append(KATAKANA_NOKIA.toBase64());
       
   436     vCard.append("\r\n");
   262     vCard.append("TEL;TYPE=PREF;HOME:123\r\n");
   437     vCard.append("TEL;TYPE=PREF;HOME:123\r\n");
       
   438     // The value here is "UXQgaXMgZ3JlYXQh", which is the base64 encoding of "Qt is great!".
   263     vCard.append("PHOTO;ENCODING=B:UXQgaXMgZ3JlYXQh\r\n");
   439     vCard.append("PHOTO;ENCODING=B:UXQgaXMgZ3JlYXQh\r\n");
       
   440     // Again, but without the explicity "ENCODING" parameter
       
   441     vCard.append("PHOTO;B:UXQgaXMgZ3JlYXQh\r\n");
   264     vCard.append("EMAIL:john.citizen@example.com\r\n");
   442     vCard.append("EMAIL:john.citizen@example.com\r\n");
   265     vCard.append("AGENT:BEGIN:VCARD\\nFN:Jenny\\nEND:VCARD\\n\r\n");
   443     vCard.append("AGENT:BEGIN:VCARD\\nFN:Jenny\\nEND:VCARD\\n\r\n");
   266     vCard.append("End:VCARD\r\n");
   444     vCard.append("End:VCARD\r\n");
   267 
   445     QBuffer buffer(&vCard);
   268     QVersitProperty property = mReaderPrivate->parseNextVersitProperty(type,vCard);
   446     buffer.open(QIODevice::ReadOnly);
       
   447     LineReader lineReader(&buffer, mAsciiCodec);
       
   448 
       
   449     QVersitProperty property = mReaderPrivate->parseNextVersitProperty(type, lineReader);
   269     QCOMPARE(property.name(),QString::fromAscii("BEGIN"));
   450     QCOMPARE(property.name(),QString::fromAscii("BEGIN"));
   270     QCOMPARE(property.value(),QByteArray("vcard"));
   451     QCOMPARE(property.value(),QString::fromAscii("vcard"));
   271 
   452 
   272     property = mReaderPrivate->parseNextVersitProperty(type,vCard);
   453     property = mReaderPrivate->parseNextVersitProperty(type, lineReader);
   273     QCOMPARE(property.name(),QString::fromAscii("VERSION"));
   454     QCOMPARE(property.name(),QString::fromAscii("VERSION"));
   274     QCOMPARE(property.value(),QByteArray("3.0"));
   455     QCOMPARE(property.value(),QString::fromAscii("3.0"));
   275 
   456 
   276     property = mReaderPrivate->parseNextVersitProperty(type,vCard);
   457     property = mReaderPrivate->parseNextVersitProperty(type, lineReader);
   277     QCOMPARE(property.name(),QString::fromAscii("FN"));
   458     QCOMPARE(property.name(),QString::fromAscii("FN"));
   278     QCOMPARE(property.value(),QByteArray("John"));
   459     QCOMPARE(property.value(),QString::fromAscii("John"));
   279 
   460 
   280     property = mReaderPrivate->parseNextVersitProperty(type,vCard);
   461     property = mReaderPrivate->parseNextVersitProperty(type, lineReader);
       
   462     QCOMPARE(property.name(),QString::fromAscii("ORG"));
       
   463     QCOMPARE(property.value(),QString::fromUtf8(KATAKANA_NOKIA));
       
   464 
       
   465     property = mReaderPrivate->parseNextVersitProperty(type, lineReader);
       
   466     QCOMPARE(property.name(),QString::fromAscii("NOTE"));
       
   467     QCOMPARE(property.value(),QString::fromUtf8(KATAKANA_NOKIA));
       
   468 
       
   469     property = mReaderPrivate->parseNextVersitProperty(type, lineReader);
   281     QCOMPARE(property.name(),QString::fromAscii("TEL"));
   470     QCOMPARE(property.name(),QString::fromAscii("TEL"));
   282     QCOMPARE(property.value(),QByteArray("123"));
   471     QCOMPARE(property.value(),QString::fromAscii("123"));
   283     QCOMPARE(property.parameters().count(), 2);
   472     QCOMPARE(property.parameters().count(), 2);
   284 
   473 
   285     property = mReaderPrivate->parseNextVersitProperty(type,vCard);
   474     property = mReaderPrivate->parseNextVersitProperty(type, lineReader);
   286     QCOMPARE(property.name(),QString::fromAscii("PHOTO"));
   475     QCOMPARE(property.name(),QString::fromAscii("PHOTO"));
   287     QCOMPARE(1,property.parameters().count());
   476     QCOMPARE(property.variantValue().type(), QVariant::ByteArray);
   288     QCOMPARE(property.value(),QByteArray("UXQgaXMgZ3JlYXQh"));
   477     QCOMPARE(property.value<QByteArray>(), QByteArray("Qt is great!"));
   289 
   478     // Ensure that base-64 encoded strings can be retrieved as strings.
   290     property = mReaderPrivate->parseNextVersitProperty(type,vCard);
   479     QCOMPARE(property.value(), QLatin1String("Qt is great!"));
       
   480 
       
   481     property = mReaderPrivate->parseNextVersitProperty(type, lineReader);
       
   482     QCOMPARE(property.name(),QString::fromAscii("PHOTO"));
       
   483     QCOMPARE(property.variantValue().type(), QVariant::ByteArray);
       
   484     QCOMPARE(property.value<QByteArray>(), QByteArray("Qt is great!"));
       
   485     QCOMPARE(property.value(), QLatin1String("Qt is great!"));
       
   486 
       
   487     property = mReaderPrivate->parseNextVersitProperty(type, lineReader);
   291     QCOMPARE(property.name(),QString::fromAscii("EMAIL"));
   488     QCOMPARE(property.name(),QString::fromAscii("EMAIL"));
   292     QCOMPARE(0,property.parameters().count());
   489     QCOMPARE(0,property.parameters().count());
   293     QCOMPARE(property.value(),QByteArray("john.citizen@example.com"));
   490     QCOMPARE(property.value(),QString::fromAscii("john.citizen@example.com"));
   294 
   491 
   295     property = mReaderPrivate->parseNextVersitProperty(type,vCard);
   492     property = mReaderPrivate->parseNextVersitProperty(type, lineReader);
   296     QCOMPARE(property.name(),QString::fromAscii("AGENT"));
   493     QCOMPARE(property.name(),QString::fromAscii("AGENT"));
   297     QCOMPARE(property.value(),QByteArray());
   494     QVERIFY(property.variantValue().userType() == qMetaTypeId<QVersitDocument>());
   298     QCOMPARE(property.embeddedDocument().properties().count(),1);
   495     QCOMPARE(property.value<QVersitDocument>().properties().count(), 1);
   299 
   496 
   300     property = mReaderPrivate->parseNextVersitProperty(type,vCard);
   497     property = mReaderPrivate->parseNextVersitProperty(type, lineReader);
   301     QCOMPARE(property.name(),QString::fromAscii("END"));
   498     QCOMPARE(property.name(),QString::fromAscii("END"));
   302     QCOMPARE(property.value(),QByteArray("VCARD"));
   499     QCOMPARE(property.value(),QString::fromAscii("VCARD"));
   303 
   500 
   304     property = mReaderPrivate->parseNextVersitProperty(type,vCard);
   501     property = mReaderPrivate->parseNextVersitProperty(type, lineReader);
   305     QCOMPARE(property.name(),QString());
   502     QCOMPARE(property.name(),QString());
   306     QCOMPARE(property.value(),QByteArray());
   503     QCOMPARE(property.value(),QString());
   307 
   504 
   308     // Simulate a situation where the document nesting level is exceeded
   505     // Simulate a situation where the document nesting level is exceeded
   309     // In practice this would mean a big number of nested AGENT properties
   506     // In practice this would mean a big number of nested AGENT properties
   310     mReaderPrivate->mDocumentNestingLevel = 20;
   507     mReaderPrivate->mDocumentNestingLevel = 20;
   311     QByteArray agentProperty("AGENT:BEGIN\\:VCARD\\nFN\\:Jenny\\nEND\\:VCARD\\n\r\n");
   508     QByteArray agentProperty("AGENT:BEGIN\\:VCARD\\nFN\\:Jenny\\nEND\\:VCARD\\n\r\n");
   312     property = mReaderPrivate->parseNextVersitProperty(type,agentProperty);
   509     buffer.close();
   313     QCOMPARE(property.name(),QString());
   510     buffer.setData(agentProperty);
   314     QCOMPARE(property.embeddedDocument().properties().count(),0);
   511     buffer.open(QIODevice::ReadOnly);
   315     QCOMPARE(property.value(),QByteArray());
   512     LineReader agentLineReader(&buffer, mAsciiCodec);
       
   513 
       
   514     property = mReaderPrivate->parseNextVersitProperty(type, agentLineReader);
       
   515     QVERIFY(property.isEmpty());
   316 }
   516 }
   317 
   517 
   318 void UT_QVersitReader::testParseVersitDocument()
   518 void UT_QVersitReader::testParseVersitDocument()
   319 {
   519 {
   320     // Valid vCard 2.1
   520     QFETCH(QByteArray, vCard);
   321     const char validCard21[] =
   521     QFETCH(bool, expectedSuccess);
   322 "BEGIN:VCARD\r\n\
   522     QFETCH(int, expectedProperties);
   323 VERSION:2.1\r\n\
   523 
   324 FN:John\r\n\
   524     QBuffer buffer(&vCard);
   325 AGENT:BEGIN:VCARD\r\nN:Jenny\r\nEND:VCARD\r\n\r\n\
   525     buffer.open(QIODevice::ReadOnly);
   326 EMAIL;ENCODING=QUOTED-PRINTABLE:john.citizen=40exam=\r\nple.com\r\n\
   526     LineReader lineReader(&buffer, QTextCodec::codecForName("UTF-8"));
   327 END:VCARD\r\n";
   527 
   328     QByteArray vCard(validCard21);
       
   329     QVersitDocument document;
   528     QVersitDocument document;
   330     QVERIFY(mReaderPrivate->parseVersitDocument(vCard,document));
   529     QCOMPARE(mReaderPrivate->parseVersitDocument(lineReader, document), expectedSuccess);
   331     QCOMPARE(document.properties().count(),3);
   530     QCOMPARE(document.properties().count(), expectedProperties);
   332     QCOMPARE(mReaderPrivate->mDocumentNestingLevel,0);
       
   333 
       
   334     // Valid vCard 3.0
       
   335     const char validCard30[] =
       
   336 "BEGIN:VCARD\r\n\
       
   337 VERSION:3.0\r\n\
       
   338 FN:John\r\n\
       
   339 AGENT:BEGIN\\:VCARD\\nN\\:Jenny\\nEND\\:VCARD\\n\r\n\
       
   340 EMAIL:john.citizen@example.com\r\n\
       
   341 END:VCARD\r\n";
       
   342     vCard = validCard30;
       
   343     document = QVersitDocument();
       
   344     QVERIFY(mReaderPrivate->parseVersitDocument(vCard,document));
       
   345     QCOMPARE(document.properties().count(),3);
       
   346     QCOMPARE(mReaderPrivate->mDocumentNestingLevel,0);
       
   347     
       
   348     // No BEGIN found
       
   349     const char beginMissing[] = 
       
   350 "VCARD\r\n\
       
   351 VERSION:2.1\r\n\
       
   352 FN:Nobody\r\n\
       
   353 END:VCARD\r\n";
       
   354     vCard = beginMissing;
       
   355     document = QVersitDocument();
       
   356     QVERIFY(mReaderPrivate->parseVersitDocument(vCard,document));
       
   357     QCOMPARE(document.properties().count(),0);
       
   358     QCOMPARE(mReaderPrivate->mDocumentNestingLevel,0);
       
   359     
       
   360     // Wrong card type
       
   361     const char wrongType[] = 
       
   362 "BEGIN:VCAL\r\n\
       
   363 END:VCAL\r\n";
       
   364     vCard = wrongType;
       
   365     document = QVersitDocument();
       
   366     QVERIFY(mReaderPrivate->parseVersitDocument(vCard,document));
       
   367     QCOMPARE(document.properties().count(),0);
       
   368     QCOMPARE(mReaderPrivate->mDocumentNestingLevel,0);
       
   369     
       
   370     // Wrong version
       
   371     const char wrongVersion[] = 
       
   372 "BEGIN:VCARD\r\n\
       
   373 VERSION:4.0\r\n\
       
   374 FN:Nobody\r\n\
       
   375 END:VCARD\r\n";
       
   376     vCard = wrongVersion;
       
   377     document = QVersitDocument();
       
   378     QVERIFY(!mReaderPrivate->parseVersitDocument(vCard,document));
       
   379     QCOMPARE(document.properties().count(),0);
       
   380     QCOMPARE(mReaderPrivate->mDocumentNestingLevel,0);
       
   381 
       
   382     // Grouped vCards are not supported. The whole vCard will be discarded.
       
   383     const char groupedCard[] =
       
   384 "BEGIN:VCARD\r\n\
       
   385 VERSION:2.1\r\n\
       
   386 X-EXAMPLES:Family vCard\r\n\
       
   387 BEGIN:VCARD\r\n\
       
   388 VERSION:2.1\r\n\
       
   389 N:Citizen;John\r\n\
       
   390 TEL;CELL:1111\r\n\
       
   391 EMAIL;ENCODING=QUOTED-PRINTABLE:john.citizen=40example.com\r\n\
       
   392 END:VCARD\r\n\
       
   393 BEGIN:VCARD\r\n\
       
   394 VERSION:2.1\r\n\
       
   395 N:Citizen;Jenny\r\n\
       
   396 TEL;CELL:7777\r\n\
       
   397 END:VCARD\r\n\
       
   398 END:VCARD";
       
   399     vCard = groupedCard;
       
   400     document = QVersitDocument();
       
   401     QVERIFY(!mReaderPrivate->parseVersitDocument(vCard,document));
       
   402     QCOMPARE(mReaderPrivate->mDocumentNestingLevel, 0);
   531     QCOMPARE(mReaderPrivate->mDocumentNestingLevel, 0);
   403     QCOMPARE(mReaderPrivate->mVersitDocuments.count(), 0);
   532 }
       
   533 
       
   534 void UT_QVersitReader::testParseVersitDocument_data()
       
   535 {
       
   536     QTest::addColumn<QByteArray>("vCard");
       
   537     QTest::addColumn<bool>("expectedSuccess");
       
   538     QTest::addColumn<int>("expectedProperties");
       
   539 
       
   540     QTest::newRow("Basic vCard 2.1")
       
   541             << QByteArray(
       
   542                     "BEGIN:VCARD\r\n"
       
   543                     "VERSION:2.1\r\n"
       
   544                     "FN:John\r\n"
       
   545                     "END:VCARD\r\n")
       
   546             << true
       
   547             << 1;
       
   548 
       
   549     QTest::newRow("vCard 2.1 with Agent")
       
   550             << QByteArray(
       
   551                     "BEGIN:VCARD\r\n"
       
   552                     "VERSION:2.1\r\n"
       
   553                     "FN:John\r\n"
       
   554                     "AGENT:BEGIN:VCARD\r\nN:Jenny\r\nEND:VCARD\r\n\r\n"
       
   555                     "EMAIL;ENCODING=QUOTED-PRINTABLE:john.citizen=40exam=\r\nple.com\r\n"
       
   556                     "END:VCARD\r\n")
       
   557             << true
       
   558             << 3;
       
   559 
       
   560     QTest::newRow("vCard 3.0 with Agent")
       
   561             << QByteArray(
       
   562                     "BEGIN:VCARD\r\n"
       
   563                     "VERSION:3.0\r\n"
       
   564                     "FN:John\r\n"
       
   565                     "AGENT:BEGIN\\:VCARD\\nN\\:Jenny\\nEND\\:VCARD\\n\r\n"
       
   566                     "EMAIL:john.citizen@example.com\r\n"
       
   567                     "END:VCARD\r\n")
       
   568             << true
       
   569             << 3;
       
   570     
       
   571     QTest::newRow("No BEGIN found")
       
   572             << QByteArray(
       
   573                     "VCARD\r\n"
       
   574                     "VERSION:2.1\r\n"
       
   575                     "FN:Nobody\r\n"
       
   576                     "END:VCARD\r\n")
       
   577             << false
       
   578             << 0;
       
   579     
       
   580     QTest::newRow("Wrong card type")
       
   581             << QByteArray(
       
   582                     "BEGIN:VCAL\r\n"
       
   583                     "END:VCAL\r\n")
       
   584             << false
       
   585             << 0;
       
   586     
       
   587     QTest::newRow("Wrong version")
       
   588             << QByteArray(
       
   589                     "BEGIN:VCARD\r\n"
       
   590                     "VERSION:4.0\r\n"
       
   591                     "FN:Nobody\r\n"
       
   592                     "END:VCARD\r\n")
       
   593             << false
       
   594             << 0;
       
   595 
       
   596     QTest::newRow("No trailing crlf")
       
   597             << QByteArray(
       
   598                     "BEGIN:VCARD\r\n"
       
   599                     "VERSION:2.1\r\n"
       
   600                     "FN:Nobody\r\n"
       
   601                     "END:VCARD")
       
   602             << true
       
   603             << 1;
       
   604 
       
   605     QTest::newRow("No end")
       
   606             << QByteArray(
       
   607                     "BEGIN:VCARD\r\n"
       
   608                     "VERSION:2.1\r\n"
       
   609                     "FN:Nobody\r\n")
       
   610             << false
       
   611             << 0;
       
   612 
       
   613     QTest::newRow("Grouped vCards are not supported. The whole vCard will be discarded.")
       
   614             << QByteArray(
       
   615                     "BEGIN:VCARD\r\n"
       
   616                     "VERSION:2.1\r\n"
       
   617                     "X-EXAMPLES:Family vCard\r\n"
       
   618                     "BEGIN:VCARD\r\n"
       
   619                     "VERSION:2.1\r\n"
       
   620                     "N:Citizen;John\r\n"
       
   621                     "TEL;CELL:1111\r\n"
       
   622                     "EMAIL;ENCODING=QUOTED-PRINTABLE:john.citizen=40example.com\r\n"
       
   623                     "END:VCARD\r\n"
       
   624                     "BEGIN:VCARD\r\n"
       
   625                     "VERSION:2.1\r\n"
       
   626                     "N:Citizen;Jenny\r\n"
       
   627                     "TEL;CELL:7777\r\n"
       
   628                     "END:VCARD\r\n"
       
   629                     "END:VCARD")
       
   630             << false
       
   631             << 0;
       
   632 }
       
   633 
       
   634 void UT_QVersitReader::testDecodeQuotedPrintable()
       
   635 {
       
   636     // Soft line breaks
       
   637     QString encoded(QLatin1String("This=\r\n is =\r\none line."));
       
   638     QString decoded(QLatin1String("This is one line."));
       
   639     mReaderPrivate->decodeQuotedPrintable(encoded);
       
   640     QCOMPARE(encoded, decoded);
       
   641 
       
   642     // Characters recommended to be encoded according to RFC 1521:
       
   643     encoded = QLatin1String("To be decoded: =0A=0D=21=22=23=24=3D=40=5B=5C=5D=5E=60=7B=7C=7D=7E");
       
   644     decoded = QLatin1String("To be decoded: \n\r!\"#$=@[\\]^`{|}~");
       
   645     mReaderPrivate->decodeQuotedPrintable(encoded);
       
   646     QCOMPARE(encoded, decoded);
       
   647 
       
   648     // Other random characters encoded.
       
   649     // Some implementation may encode these too, as it is allowed.
       
   650     encoded = QLatin1String("=45=6E=63=6F=64=65=64 =64=61=74=61");
       
   651     decoded = QLatin1String("Encoded data");
       
   652     mReaderPrivate->decodeQuotedPrintable(encoded);
       
   653     QCOMPARE(encoded, decoded);
       
   654 }
       
   655 void UT_QVersitReader::testParamName()
       
   656 {
       
   657     // Empty value
       
   658     QByteArray param;
       
   659     QCOMPARE(mReaderPrivate->paramName(param, mAsciiCodec),QString());
       
   660 
       
   661     // Only value present
       
   662     param = "WORK";
       
   663     QCOMPARE(mReaderPrivate->paramName(param, mAsciiCodec),
       
   664              QString::fromAscii("TYPE"));
       
   665 
       
   666     // The below tests intentionally use the misspelling TIPE to avoid the default behaviour of
       
   667     // returning TYPE when the name can't be parsed.
       
   668     // Both name and value, spaces after the name
       
   669     param = "TIPE \t =WORK";
       
   670     QCOMPARE(mReaderPrivate->paramName(param, mAsciiCodec),
       
   671              QString::fromAscii("TIPE"));
       
   672 
       
   673     // Both name and value, no spaces after the name
       
   674     param = "TIPE=WORK";
       
   675     QCOMPARE(mReaderPrivate->paramName(param, mAsciiCodec),
       
   676              QString::fromAscii("TIPE"));
       
   677 
       
   678     // Test wide character support.
       
   679     QTextCodec* codec = QTextCodec::codecForName("UTF-16BE");
       
   680     param = codec->fromUnicode(QString::fromAscii("TIPE=WORK"));
       
   681     QCOMPARE(mReaderPrivate->paramName(param, codec),
       
   682              QString::fromAscii("TIPE"));
       
   683 
       
   684 }
       
   685 
       
   686 void UT_QVersitReader::testParamValue()
       
   687 {
       
   688     // Empty value
       
   689     QByteArray param;
       
   690     QCOMPARE(mReaderPrivate->paramValue(param, mAsciiCodec),QString());
       
   691 
       
   692     // Only value present
       
   693     param = "WORK";
       
   694     QCOMPARE(mReaderPrivate->paramValue(param, mAsciiCodec),
       
   695              QString::fromAscii("WORK"));
       
   696 
       
   697     // Name and equals sign, but no value
       
   698     param = "TYPE=";
       
   699     QCOMPARE(mReaderPrivate->paramValue(param, mAsciiCodec),QString());
       
   700 
       
   701     // Name and equals sign, but value has only spaces
       
   702     param = "TYPE=  \t ";
       
   703     QCOMPARE(mReaderPrivate->paramValue(param, mAsciiCodec),QString());
       
   704 
       
   705     // Both name and value, spaces before the value
       
   706     param = "TYPE= \t  WORK";
       
   707     QCOMPARE(mReaderPrivate->paramValue(param, mAsciiCodec),
       
   708              QString::fromAscii("WORK"));
       
   709 
       
   710     // Both name and value, no spaces before the value
       
   711     param = "ENCODING=QUOTED-PRINTABLE";
       
   712     QCOMPARE(mReaderPrivate->paramValue(param, mAsciiCodec),
       
   713              QString::fromAscii("QUOTED-PRINTABLE"));
       
   714 
       
   715     // Test wide character support.
       
   716     QTextCodec* codec = QTextCodec::codecForName("UTF-16BE");
       
   717     param = codec->fromUnicode(QString::fromAscii("TYPE=WORK"));
       
   718     QCOMPARE(mReaderPrivate->paramValue(param, codec),
       
   719              QString::fromAscii("WORK"));
       
   720 }
       
   721 
       
   722 void UT_QVersitReader::testExtractPart()
       
   723 {
       
   724     QByteArray originalStr;
       
   725 
       
   726     // Negative starting position
       
   727     QCOMPARE(mReaderPrivate->extractPart(originalStr,-1,1), QByteArray());
       
   728 
       
   729     // Empty original string
       
   730     QCOMPARE(mReaderPrivate->extractPart(originalStr,0,1), QByteArray());
       
   731 
       
   732     // Trimmed substring empty
       
   733     originalStr = " \t \t";
       
   734     QCOMPARE(mReaderPrivate->extractPart(originalStr,0,4), QByteArray());
       
   735 
       
   736     // The given substring length is greater than the original string length
       
   737     originalStr = "ENCODING=7BIT";
       
   738     QCOMPARE(mReaderPrivate->extractPart(originalStr,0,100), originalStr);
       
   739 
       
   740     // Non-empty substring, from the beginning
       
   741     originalStr = " TYPE=WORK ; X-PARAM=X-VALUE; ENCODING=8BIT";
       
   742     QCOMPARE(mReaderPrivate->extractPart(originalStr,0,11),
       
   743              QByteArray("TYPE=WORK"));
       
   744 
       
   745     // Non-empty substring, from the middle
       
   746     QCOMPARE(mReaderPrivate->extractPart(originalStr,12,16),
       
   747              QByteArray("X-PARAM=X-VALUE"));
       
   748 
       
   749     // Non-empty substring, from the middle to the end
       
   750     QCOMPARE(mReaderPrivate->extractPart(originalStr,29),
       
   751              QByteArray("ENCODING=8BIT"));
       
   752 }
       
   753 
       
   754 void UT_QVersitReader::testExtractParts()
       
   755 {
       
   756     QList<QByteArray> parts;
       
   757 
       
   758     // Empty value
       
   759     QByteArray text;
       
   760     parts = mReaderPrivate->extractParts(text,";", mAsciiCodec);
       
   761     QVERIFY(parts.isEmpty());
       
   762 
       
   763     // Only separator
       
   764     text = ";";
       
   765     parts = mReaderPrivate->extractParts(text,";", mAsciiCodec);
       
   766     QVERIFY(parts.isEmpty());
       
   767 
       
   768     // One part
       
   769     text = "part";
       
   770     parts = mReaderPrivate->extractParts(text,";", mAsciiCodec);
       
   771     QCOMPARE(parts.count(),1);
       
   772     QCOMPARE(QString::fromAscii(parts[0]),QString::fromAscii("part"));
       
   773 
       
   774     // Separator in the beginning, one part
       
   775     text = ";part";
       
   776     parts = mReaderPrivate->extractParts(text,";", mAsciiCodec);
       
   777     QCOMPARE(parts.count(),1);
       
   778     QCOMPARE(QString::fromAscii(parts[0]),QString::fromAscii("part"));
       
   779 
       
   780     // Separator in the end, one part
       
   781     text = "part;";
       
   782     parts = mReaderPrivate->extractParts(text,";", mAsciiCodec);
       
   783     QCOMPARE(parts.count(),1);
       
   784     QCOMPARE(QString::fromAscii(parts[0]),QString::fromAscii("part"));
       
   785 
       
   786     // One part that contains escaped separator
       
   787     text = "part\\;";
       
   788     parts = mReaderPrivate->extractParts(text,";", mAsciiCodec);
       
   789     QCOMPARE(parts.count(),1);
       
   790     QCOMPARE(QString::fromAscii(parts[0]),QString::fromAscii("part\\;"));
       
   791 
       
   792     // Two parts
       
   793     text = "part1;part2";
       
   794     parts = mReaderPrivate->extractParts(text,";", mAsciiCodec);
       
   795     QCOMPARE(parts.count(),2);
       
   796     QCOMPARE(QString::fromAscii(parts[0]),QString::fromAscii("part1"));
       
   797     QCOMPARE(QString::fromAscii(parts[1]),QString::fromAscii("part2"));
       
   798 
       
   799     // Two parts that contain escaped separators
       
   800     text = "pa\\;rt1;par\\;t2";
       
   801     parts = mReaderPrivate->extractParts(text,";", mAsciiCodec);
       
   802     QCOMPARE(parts.count(),2);
       
   803     QCOMPARE(QString::fromAscii(parts[0]),QString::fromAscii("pa\\;rt1"));
       
   804     QCOMPARE(QString::fromAscii(parts[1]),QString::fromAscii("par\\;t2"));
       
   805 
       
   806     // Test wide character support
       
   807     QTextCodec* codec = QTextCodec::codecForName("UTF-16BE");
       
   808     text = codec->fromUnicode(QString::fromAscii("part1;part2"));
       
   809     parts = mReaderPrivate->extractParts(text,";", codec);
       
   810     QCOMPARE(parts.count(),2);
       
   811     QCOMPARE(codec->toUnicode(parts[0]),QString::fromAscii("part1"));
       
   812     QCOMPARE(codec->toUnicode(parts[1]),QString::fromAscii("part2"));
       
   813 }
       
   814 
       
   815 void UT_QVersitReader::testExtractPropertyGroupsAndName()
       
   816 {
       
   817     QPair<QStringList,QString> groupsAndName;
       
   818 
       
   819     // Empty string
       
   820     VersitCursor cursor(QByteArray(" "));
       
   821     groupsAndName = mReaderPrivate->extractPropertyGroupsAndName(cursor, mAsciiCodec);
       
   822     QCOMPARE(groupsAndName.first.count(),0);
       
   823     QCOMPARE(groupsAndName.second,QString());
       
   824 
       
   825     // No value -> returns empty string and no groups
       
   826     QByteArray property("TEL");
       
   827     cursor.setData(property);
       
   828     cursor.selection = property.size();
       
   829     groupsAndName = mReaderPrivate->extractPropertyGroupsAndName(cursor, mAsciiCodec);
       
   830     QCOMPARE(groupsAndName.first.count(),0);
       
   831     QCOMPARE(groupsAndName.second,QString());
       
   832 
       
   833     // Simple name and value
       
   834     property = "TEL:123";
       
   835     cursor.setData(property);
       
   836     cursor.selection = property.size();
       
   837     groupsAndName = mReaderPrivate->extractPropertyGroupsAndName(cursor, mAsciiCodec);
       
   838     QCOMPARE(groupsAndName.first.count(),0);
       
   839     QCOMPARE(groupsAndName.second,QString::fromAscii("TEL"));
       
   840 
       
   841     // One whitespace before colon
       
   842     property = "TEL :123";
       
   843     cursor.setData(property);
       
   844     cursor.selection = property.size();
       
   845     groupsAndName = mReaderPrivate->extractPropertyGroupsAndName(cursor, mAsciiCodec);
       
   846     QCOMPARE(groupsAndName.first.count(),0);
       
   847     QCOMPARE(groupsAndName.second,QString::fromAscii("TEL"));
       
   848 
       
   849     // Several whitespaces before colon
       
   850     property = "TEL \t  :123";
       
   851     cursor.setData(property);
       
   852     cursor.selection = property.size();
       
   853     groupsAndName = mReaderPrivate->extractPropertyGroupsAndName(cursor, mAsciiCodec);
       
   854     QCOMPARE(groupsAndName.first.count(),0);
       
   855     QCOMPARE(groupsAndName.second,QString::fromAscii("TEL"));
       
   856 
       
   857     // Name contains a group
       
   858     property = "group1.TEL:1234";
       
   859     cursor.setData(property);
       
   860     cursor.selection = property.size();
       
   861     groupsAndName = mReaderPrivate->extractPropertyGroupsAndName(cursor, mAsciiCodec);
       
   862     QCOMPARE(groupsAndName.first.count(),1);
       
   863     QCOMPARE(groupsAndName.first.takeFirst(),QString::fromAscii("group1"));
       
   864     QCOMPARE(groupsAndName.second,QString::fromAscii("TEL"));
       
   865 
       
   866     // Name contains more than one group
       
   867     property = "group1.group2.TEL:12345";
       
   868     cursor.setData(property);
       
   869     cursor.selection = property.size();
       
   870     groupsAndName = mReaderPrivate->extractPropertyGroupsAndName(cursor, mAsciiCodec);
       
   871     QCOMPARE(groupsAndName.first.count(),2);
       
   872     QCOMPARE(groupsAndName.first.takeFirst(),QString::fromAscii("group1"));
       
   873     QCOMPARE(groupsAndName.first.takeFirst(),QString::fromAscii("group2"));
       
   874     QCOMPARE(groupsAndName.second,QString::fromAscii("TEL"));
       
   875     QCOMPARE(cursor.position, 17);
       
   876 
       
   877     // Property contains one parameter
       
   878     property = "TEL;WORK:123";
       
   879     cursor.setData(property);
       
   880     cursor.selection = property.size();
       
   881     groupsAndName = mReaderPrivate->extractPropertyGroupsAndName(cursor, mAsciiCodec);
       
   882     QCOMPARE(groupsAndName.first.count(),0);
       
   883     QCOMPARE(groupsAndName.second,QString::fromAscii("TEL"));
       
   884 
       
   885     // Property contains several parameters
       
   886     property = "EMAIL;INTERNET;ENCODING=QUOTED-PRINTABLE:user=40ovi.com";
       
   887     cursor.setData(property);
       
   888     cursor.selection = property.size();
       
   889     groupsAndName = mReaderPrivate->extractPropertyGroupsAndName(cursor, mAsciiCodec);
       
   890     QCOMPARE(groupsAndName.first.count(),0);
       
   891     QCOMPARE(groupsAndName.second,QString::fromAscii("EMAIL"));
       
   892 
       
   893     // Name contains an escaped semicolon
       
   894     property = "X-proper\\;ty:value";
       
   895     cursor.setData(property);
       
   896     cursor.selection = property.size();
       
   897     groupsAndName = mReaderPrivate->extractPropertyGroupsAndName(cursor, mAsciiCodec);
       
   898     QCOMPARE(groupsAndName.first.count(),0);
       
   899     QCOMPARE(groupsAndName.second,QString::fromAscii("X-proper\\;ty"));
       
   900 
       
   901     // Test wide character support
       
   902     QTextCodec* codec = QTextCodec::codecForName("UTF-16BE");
       
   903     property = codec->fromUnicode(QString::fromAscii("group1.group2.TEL;WORK:123"));
       
   904     cursor.setData(property);
       
   905     cursor.selection = property.size();
       
   906     groupsAndName = mReaderPrivate->extractPropertyGroupsAndName(cursor, codec);
       
   907     QCOMPARE(groupsAndName.first.count(),2);
       
   908     QCOMPARE(groupsAndName.first.takeFirst(),QString::fromAscii("group1"));
       
   909     QCOMPARE(groupsAndName.first.takeFirst(),QString::fromAscii("group2"));
       
   910     QCOMPARE(groupsAndName.second,QString::fromAscii("TEL"));
       
   911     QCOMPARE(cursor.position, 36); // 2 bytes * 17 characters + 2 byte BOM.
       
   912 
       
   913 }
       
   914 
       
   915 void UT_QVersitReader::testExtractVCard21PropertyParams()
       
   916 {
       
   917     // No parameters
       
   918     VersitCursor cursor(QByteArray(":123"));
       
   919     cursor.setSelection(cursor.data.size());
       
   920     QCOMPARE(mReaderPrivate->extractVCard21PropertyParams(cursor, mAsciiCodec).count(), 0);
       
   921 
       
   922     // "Empty" parameter
       
   923     cursor.setData(QByteArray(";:123"));
       
   924     cursor.setSelection(cursor.data.size());
       
   925     QCOMPARE(mReaderPrivate->extractVCard21PropertyParams(cursor, mAsciiCodec).count(), 0);
       
   926 
       
   927     // Semicolon found, but no value for the property
       
   928     cursor.setData(QByteArray(";TYPE=X-TYPE"));
       
   929     cursor.setSelection(cursor.data.size());
       
   930     QCOMPARE(mReaderPrivate->extractVCard21PropertyParams(cursor, mAsciiCodec).count(), 0);
       
   931 
       
   932     // The property name contains an escaped semicolon, no parameters
       
   933     cursor.setData(QByteArray(":value"));
       
   934     cursor.setSelection(cursor.data.size());
       
   935     QCOMPARE(mReaderPrivate->extractVCard21PropertyParams(cursor, mAsciiCodec).count(), 0);
       
   936 
       
   937     // The property value contains a semicolon, no parameters
       
   938     cursor.setData(QByteArray(":va;lue"));
       
   939     cursor.setSelection(cursor.data.size());
       
   940     QCOMPARE(mReaderPrivate->extractVCard21PropertyParams(cursor, mAsciiCodec).count(), 0);
       
   941 
       
   942     // One parameter
       
   943     cursor.setData(QByteArray(";HOME:123"));
       
   944     cursor.setSelection(cursor.data.size());
       
   945     QMultiHash<QString,QString> params = mReaderPrivate->extractVCard21PropertyParams(cursor,
       
   946                                                                                    mAsciiCodec);
       
   947     QCOMPARE(1, params.count());
       
   948     QCOMPARE(1, params.values(QString::fromAscii("TYPE")).count());
       
   949     QCOMPARE(params.values(QString::fromAscii("TYPE"))[0],QString::fromAscii("HOME"));
       
   950 
       
   951     // Two parameters of the same type
       
   952     cursor.setData(QByteArray(";HOME;VOICE:123"));
       
   953     cursor.setSelection(cursor.data.size());
       
   954     params = mReaderPrivate->extractVCard21PropertyParams(cursor, mAsciiCodec);
       
   955     QCOMPARE(2, params.count());
       
   956     QCOMPARE(2, params.values(QString::fromAscii("TYPE")).count());
       
   957     QCOMPARE(params.values(QString::fromAscii("TYPE"))[0],QString::fromAscii("HOME"));
       
   958     QCOMPARE(params.values(QString::fromAscii("TYPE"))[1],QString::fromAscii("VOICE"));
       
   959 
       
   960     // Two parameters, several empty parameters (extra semicolons)
       
   961     cursor.setData(QByteArray(";;;;HOME;;;;;VOICE;;;:123"));
       
   962     cursor.setSelection(cursor.data.size());
       
   963     params = mReaderPrivate->extractVCard21PropertyParams(cursor, mAsciiCodec);
       
   964     QCOMPARE(2, params.count());
       
   965     QCOMPARE(2, params.values(QString::fromAscii("TYPE")).count());
       
   966     QCOMPARE(params.values(QString::fromAscii("TYPE"))[0],QString::fromAscii("HOME"));
       
   967     QCOMPARE(params.values(QString::fromAscii("TYPE"))[1],QString::fromAscii("VOICE"));
       
   968 
       
   969     // Two parameters with different types
       
   970     cursor.setData(QByteArray(";INTERNET;ENCODING=QUOTED-PRINTABLE:user=40ovi.com"));
       
   971     cursor.setSelection(cursor.data.size());
       
   972     params.clear();
       
   973     params = mReaderPrivate->extractVCard21PropertyParams(cursor, mAsciiCodec);
       
   974     QCOMPARE(2, params.count());
       
   975     QList<QString> typeParams = params.values(QString::fromAscii("TYPE"));
       
   976     QCOMPARE(1, typeParams.count());
       
   977     QCOMPARE(typeParams[0],QString::fromAscii("INTERNET"));
       
   978     QList<QString> encodingParams = params.values(QString::fromAscii("ENCODING"));
       
   979     QCOMPARE(1, encodingParams.count());
       
   980     QCOMPARE(encodingParams[0],QString::fromAscii("QUOTED-PRINTABLE"));
       
   981 
       
   982     // Test wide character support.
       
   983     QTextCodec* codec = QTextCodec::codecForName("UTF-16BE");
       
   984     QByteArray data = VersitUtils::encode(";HOME;CHARSET=UTF-16:123", codec);
       
   985     cursor.setData(data);
       
   986     cursor.setSelection(cursor.data.size());
       
   987     params = mReaderPrivate->extractVCard21PropertyParams(cursor, codec);
       
   988     QCOMPARE(2, params.count());
       
   989     typeParams = params.values(QString::fromAscii("TYPE"));
       
   990     QCOMPARE(1, typeParams.count());
       
   991     QCOMPARE(typeParams[0],QString::fromAscii("HOME"));
       
   992     encodingParams = params.values(QString::fromAscii("CHARSET"));
       
   993     QCOMPARE(1, encodingParams.count());
       
   994     QCOMPARE(encodingParams[0],QString::fromAscii("UTF-16"));
       
   995 }
       
   996 
       
   997 void UT_QVersitReader::testExtractVCard30PropertyParams()
       
   998 {
       
   999     // No parameters
       
  1000     VersitCursor cursor(QByteArray(":123"));
       
  1001     cursor.setSelection(cursor.data.size());
       
  1002     QCOMPARE(mReaderPrivate->extractVCard30PropertyParams(cursor, mAsciiCodec).count(), 0);
       
  1003 
       
  1004     // One parameter
       
  1005     cursor.setData(QByteArray(";TYPE=HOME:123"));
       
  1006     cursor.setSelection(cursor.data.size());
       
  1007     QMultiHash<QString,QString> params = mReaderPrivate->extractVCard30PropertyParams(cursor,
       
  1008                                                                                    mAsciiCodec);
       
  1009     QCOMPARE(params.count(), 1);
       
  1010     QCOMPARE(params.values(QString::fromAscii("TYPE")).count(), 1);
       
  1011     QCOMPARE(params.values(QString::fromAscii("TYPE"))[0], QString::fromAscii("HOME"));
       
  1012 
       
  1013     // One parameter with an escaped semicolon
       
  1014     cursor.setData(QByteArray(";para\\;meter:value"));
       
  1015     cursor.setSelection(cursor.data.size());
       
  1016     params = mReaderPrivate->extractVCard30PropertyParams(cursor, mAsciiCodec);
       
  1017     QCOMPARE(params.count(), 1);
       
  1018     QCOMPARE(params.values(QString::fromAscii("TYPE")).count(), 1);
       
  1019     QCOMPARE(params.values(QString::fromAscii("TYPE"))[0], QString::fromAscii("para;meter"));
       
  1020 
       
  1021     // One parameter with and escaped comma in the name and the value
       
  1022     cursor.setData(QByteArray(";X-PA\\,RAM=VAL\\,UE:123"));
       
  1023     cursor.setSelection(cursor.data.size());
       
  1024     params = mReaderPrivate->extractVCard30PropertyParams(cursor, mAsciiCodec);
       
  1025     QCOMPARE(params.count(), 1);
       
  1026     QCOMPARE(params.values(QString::fromAscii("X-PA,RAM")).count(), 1);
       
  1027     QCOMPARE(params.values(QString::fromAscii("X-PA,RAM"))[0], QString::fromAscii("VAL,UE"));
       
  1028 
       
  1029     // Two parameters of the same type
       
  1030     cursor.setData(QByteArray(";TYPE=HOME,VOICE:123"));
       
  1031     cursor.setSelection(cursor.data.size());
       
  1032     params = mReaderPrivate->extractVCard30PropertyParams(cursor, mAsciiCodec);
       
  1033     QCOMPARE(params.count(), 2);
       
  1034     QCOMPARE(params.values(QString::fromAscii("TYPE")).count(), 2);
       
  1035     QVERIFY(params.values(QString::fromAscii("TYPE")).contains(QString::fromAscii("HOME")));
       
  1036     QVERIFY(params.values(QString::fromAscii("TYPE")).contains(QString::fromAscii("VOICE")));
       
  1037 
       
  1038     // Two parameters of the same type in separate name-values
       
  1039     cursor.setData(QByteArray(";TYPE=HOME;TYPE=VOICE:123"));
       
  1040     cursor.setSelection(cursor.data.size());
       
  1041     params = mReaderPrivate->extractVCard30PropertyParams(cursor, mAsciiCodec);
       
  1042     QCOMPARE(params.count(), 2);
       
  1043     QCOMPARE(params.values(QString::fromAscii("TYPE")).count(), 2);
       
  1044     QVERIFY(params.values(QString::fromAscii("TYPE")).contains(QString::fromAscii("HOME")));
       
  1045     QVERIFY(params.values(QString::fromAscii("TYPE")).contains(QString::fromAscii("VOICE")));
       
  1046 
       
  1047     // Three parameters of the same type
       
  1048     cursor.setData(QByteArray(";TYPE=PREF,HOME,VOICE:123"));
       
  1049     cursor.setSelection(cursor.data.size());
       
  1050     params = mReaderPrivate->extractVCard30PropertyParams(cursor, mAsciiCodec);
       
  1051     QCOMPARE(params.count(), 3);
       
  1052     QCOMPARE(params.values(QString::fromAscii("TYPE")).count(), 3);
       
  1053     QVERIFY(params.values(QString::fromAscii("TYPE")).contains(QString::fromAscii("PREF")));
       
  1054     QVERIFY(params.values(QString::fromAscii("TYPE")).contains(QString::fromAscii("HOME")));
       
  1055     QVERIFY(params.values(QString::fromAscii("TYPE")).contains(QString::fromAscii("VOICE")));
       
  1056 
       
  1057     // Two parameters with different types
       
  1058     cursor.setData(QByteArray(";TYPE=HOME;X-PARAM=X-VALUE:Home Street 1"));
       
  1059     cursor.setSelection(cursor.data.size());
       
  1060     params.clear();
       
  1061     params = mReaderPrivate->extractVCard30PropertyParams(cursor, mAsciiCodec);
       
  1062     QCOMPARE(params.count(), 2);
       
  1063     QList<QString> typeParams = params.values(QString::fromAscii("TYPE"));
       
  1064     QCOMPARE(typeParams.count(), 1);
       
  1065     QCOMPARE(typeParams[0],QString::fromAscii("HOME"));
       
  1066     QList<QString> encodingParams = params.values(QString::fromAscii("X-PARAM"));
       
  1067     QCOMPARE(encodingParams.count(), 1);
       
  1068     QCOMPARE(encodingParams[0],QString::fromAscii("X-VALUE"));
       
  1069 
       
  1070     // Test wide character support.
       
  1071     QTextCodec* codec = QTextCodec::codecForName("UTF-16BE");
       
  1072     QByteArray data = VersitUtils::encode(";TIPE=HOME,VOICE;CHARSET=UTF-16:123", codec);
       
  1073     cursor.setData(data);
       
  1074     cursor.setSelection(cursor.data.size());
       
  1075     params = mReaderPrivate->extractVCard30PropertyParams(cursor, codec);
       
  1076     QCOMPARE(params.count(), 3);
       
  1077     typeParams = params.values(QString::fromAscii("TIPE"));
       
  1078     QCOMPARE(params.values(QString::fromAscii("TIPE")).count(), 2);
       
  1079     QVERIFY(params.values(QString::fromAscii("TIPE")).contains(QString::fromAscii("HOME")));
       
  1080     QVERIFY(params.values(QString::fromAscii("TIPE")).contains(QString::fromAscii("VOICE")));
       
  1081     encodingParams = params.values(QString::fromAscii("CHARSET"));
       
  1082     QCOMPARE(1, encodingParams.count());
       
  1083     QCOMPARE(encodingParams[0],QString::fromAscii("UTF-16"));
       
  1084 }
       
  1085 
       
  1086 void UT_QVersitReader::testExtractParams()
       
  1087 {
       
  1088     VersitCursor cursor;
       
  1089     QByteArray data = ":123";
       
  1090     cursor.setData(data);
       
  1091     cursor.setPosition(0);
       
  1092     cursor.setSelection(cursor.data.size());
       
  1093     QList<QByteArray> params = mReaderPrivate->extractParams(cursor, mAsciiCodec);
       
  1094     QCOMPARE(params.size(), 0);
       
  1095     QCOMPARE(cursor.position, 1);
       
  1096 
       
  1097     data = "a;b:123";
       
  1098     cursor.setData(data);
       
  1099     cursor.setPosition(0);
       
  1100     cursor.setSelection(cursor.data.size());
       
  1101     params = mReaderPrivate->extractParams(cursor, mAsciiCodec);
       
  1102     QCOMPARE(params.size(), 2);
       
  1103     QCOMPARE(cursor.position, 4);
       
  1104     QCOMPARE(params.at(0), QByteArray("a"));
       
  1105     QCOMPARE(params.at(1), QByteArray("b"));
       
  1106 
       
  1107     QTextCodec* codec = QTextCodec::codecForName("UTF-16BE");
       
  1108     data = VersitUtils::encode(":123", codec);
       
  1109     cursor.setData(data);
       
  1110     cursor.setPosition(0);
       
  1111     cursor.setSelection(cursor.data.size());
       
  1112     params = mReaderPrivate->extractParams(cursor, codec);
       
  1113     QCOMPARE(params.size(), 0);
       
  1114     QCOMPARE(cursor.position, 2);
       
  1115 
       
  1116     data = VersitUtils::encode("a;b:123", codec);
       
  1117     cursor.setData(data);
       
  1118     cursor.setPosition(0);
       
  1119     cursor.setSelection(cursor.data.size());
       
  1120     params = mReaderPrivate->extractParams(cursor, codec);
       
  1121     QCOMPARE(params.size(), 2);
       
  1122     QCOMPARE(cursor.position, 8);
       
  1123 
       
  1124 }
       
  1125 
       
  1126 Q_DECLARE_METATYPE(QList<QString>)
       
  1127 
       
  1128 void UT_QVersitReader::testReadLine()
       
  1129 {
       
  1130     QFETCH(QByteArray, codecName);
       
  1131     QFETCH(QString, data);
       
  1132     QFETCH(QList<QString>, expectedLines);
       
  1133 
       
  1134     QTextCodec* codec = QTextCodec::codecForName(codecName);
       
  1135     QTextEncoder* encoder = codec->makeEncoder();
       
  1136     encoder->fromUnicode(QString());
       
  1137 
       
  1138     QByteArray bytes(encoder->fromUnicode(data));
       
  1139 
       
  1140     mInputDevice->close();
       
  1141     mInputDevice->setData(bytes);
       
  1142     mInputDevice->open(QIODevice::ReadWrite);
       
  1143 
       
  1144     LineReader lineReader(mInputDevice, codec, 10);
       
  1145 
       
  1146     // Check that all expected lines are read.
       
  1147     foreach (QString expectedLine, expectedLines) {
       
  1148         QByteArray expectedBytes(encoder->fromUnicode(expectedLine));
       
  1149         QVERIFY(!lineReader.atEnd());
       
  1150         VersitCursor line = lineReader.readLine();
       
  1151         QVERIFY(line.data.indexOf(expectedBytes) == line.position);
       
  1152         QCOMPARE(line.selection - line.position, expectedBytes.length());
       
  1153     }
       
  1154     // And that there are no more lines
       
  1155     VersitCursor line = lineReader.readLine();
       
  1156     QCOMPARE(line.selection, line.position);
       
  1157     QVERIFY(lineReader.atEnd());
       
  1158 
       
  1159     delete encoder;
       
  1160 }
       
  1161 
       
  1162 void UT_QVersitReader::testReadLine_data()
       
  1163 {
       
  1164     // Note: for this test, we set mLineReader to read 10 bytes at a time.  Lines of multiples of
       
  1165     // 10 bytes are hence border cases.
       
  1166     QTest::addColumn<QByteArray>("codecName");
       
  1167     QTest::addColumn<QString>("data");
       
  1168     QTest::addColumn<QList<QString> >("expectedLines");
       
  1169 
       
  1170     QList<QByteArray> codecNames;
       
  1171     codecNames << "UTF-8" << "UTF-16";
       
  1172 
       
  1173     foreach (QByteArray codecName, codecNames) {
       
  1174         QTest::newRow("empty " + codecName)
       
  1175                 << codecName
       
  1176                 << ""
       
  1177                 << QList<QString>();
       
  1178 
       
  1179         QTest::newRow("one line " + codecName)
       
  1180                 << codecName
       
  1181                 << "line"
       
  1182                 << (QList<QString>() << QLatin1String("line"));
       
  1183 
       
  1184         QTest::newRow("one ten-byte line " + codecName)
       
  1185                 << codecName
       
  1186                 << "tenletters"
       
  1187                 << (QList<QString>() << QLatin1String("tenletters"));
       
  1188 
       
  1189         QTest::newRow("one long line " + codecName)
       
  1190                 << codecName
       
  1191                 << "one line longer than ten characters"
       
  1192                 << (QList<QString>() << QLatin1String("one line longer than ten characters"));
       
  1193 
       
  1194         QTest::newRow("one terminated line " + codecName)
       
  1195                 << codecName
       
  1196                 << "one line longer than ten characters\r\n"
       
  1197                 << (QList<QString>() << QLatin1String("one line longer than ten characters"));
       
  1198 
       
  1199         QTest::newRow("two lines " + codecName)
       
  1200                 << codecName
       
  1201                 << "two\r\nlines"
       
  1202                 << (QList<QString>() << QLatin1String("two") << QLatin1String("lines"));
       
  1203 
       
  1204         QTest::newRow("two terminated lines " + codecName)
       
  1205                 << codecName
       
  1206                 << "two\r\nlines\r\n"
       
  1207                 << (QList<QString>() << QLatin1String("two") << QLatin1String("lines"));
       
  1208 
       
  1209         QTest::newRow("two long lines " + codecName)
       
  1210                 << codecName
       
  1211                 << "one line longer than ten characters\r\nanother line\r\n"
       
  1212                 << (QList<QString>() << QLatin1String("one line longer than ten characters") << QLatin1String("another line"));
       
  1213 
       
  1214         QTest::newRow("two full lines " + codecName)
       
  1215                 << codecName
       
  1216                 << "tenletters\r\n8letters\r\n"
       
  1217                 << (QList<QString>() << QLatin1String("tenletters") << QLatin1String("8letters"));
       
  1218 
       
  1219         QTest::newRow("a nine-byte line " + codecName)
       
  1220                 << codecName
       
  1221                 << "9 letters\r\nanother line\r\n"
       
  1222                 << (QList<QString>() << QLatin1String("9 letters") << QLatin1String("another line"));
       
  1223 
       
  1224         QTest::newRow("a blank line " + codecName)
       
  1225                 << codecName
       
  1226                 << "one\r\n\r\ntwo\r\n"
       
  1227                 << (QList<QString>() << QLatin1String("one") << QLatin1String("two"));
       
  1228 
       
  1229         QTest::newRow("folded lines " + codecName)
       
  1230                 << codecName
       
  1231                 << "folded\r\n  line\r\nsecond line\r\n"
       
  1232                 << (QList<QString>() << QLatin1String("folded line") << QLatin1String("second line"));
       
  1233 
       
  1234         QTest::newRow("multiply folded lines " + codecName)
       
  1235                 << codecName
       
  1236                 << "fo\r\n lded\r\n  line\r\nseco\r\n\tnd l\r\n ine\r\n"
       
  1237                 << (QList<QString>() << QLatin1String("folded line") << QLatin1String("second line"));
       
  1238 
       
  1239         QTest::newRow("fold hidden after a chunk " + codecName)
       
  1240                 << codecName
       
  1241                 << "8letters\r\n  on one line\r\n"
       
  1242                 << (QList<QString>() << QLatin1String("8letters on one line"));
       
  1243 
       
  1244         QTest::newRow("three mac lines " + codecName)
       
  1245                 << codecName
       
  1246                 << "one\rtwo\rthree\r"
       
  1247                 << (QList<QString>() << QLatin1String("one") << QLatin1String("two") << QLatin1String("three"));
       
  1248     }
   404 }
  1249 }
   405 
  1250 
   406 QTEST_MAIN(UT_QVersitReader)
  1251 QTEST_MAIN(UT_QVersitReader)
   407 
  1252