qtmobility/tests/auto/qvcard30writer/tst_qvcard30writer.cpp
changeset 4 90517678cc4f
child 8 71781823f776
equal deleted inserted replaced
1:2b40d63a9c3d 4:90517678cc4f
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt Mobility Components.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "tst_qvcard30writer.h"
       
    43 #include "qvcard30writer_p.h"
       
    44 #include "qversitdocument.h"
       
    45 #include "qversitproperty.h"
       
    46 #include <QtTest/QtTest>
       
    47 #include <QByteArray>
       
    48 #include <QVariant>
       
    49 
       
    50 // This says "NOKIA" in Katakana encoded with UTF-8
       
    51 const QString KATAKANA_NOKIA(QString::fromUtf8("\xe3\x83\x8e\xe3\x82\xad\xe3\x82\xa2"));
       
    52 
       
    53 QTM_USE_NAMESPACE
       
    54 
       
    55 Q_DECLARE_METATYPE(QVersitProperty)
       
    56 
       
    57 void tst_QVCard30Writer::init()
       
    58 {
       
    59     mWriter = new QVCard30Writer;
       
    60     mWriter->setCodec(QTextCodec::codecForName("UTF-8"));
       
    61 }
       
    62 
       
    63 void tst_QVCard30Writer::cleanup()
       
    64 {
       
    65     delete mWriter;
       
    66 }
       
    67 
       
    68 void tst_QVCard30Writer::testEncodeVersitProperty()
       
    69 {
       
    70     QFETCH(QVersitProperty, property);
       
    71     QFETCH(QByteArray, expectedResult);
       
    72     QByteArray encodedProperty;
       
    73     QBuffer buffer(&encodedProperty);
       
    74     mWriter->setDevice(&buffer);
       
    75     buffer.open(QIODevice::WriteOnly);
       
    76     mWriter->encodeVersitProperty(property);
       
    77     QCOMPARE(encodedProperty, expectedResult);
       
    78 }
       
    79 
       
    80 
       
    81 void tst_QVCard30Writer::testEncodeVersitProperty_data()
       
    82 {
       
    83     QTest::addColumn<QVersitProperty>("property");
       
    84     QTest::addColumn<QByteArray>("expectedResult");
       
    85 
       
    86     QVersitProperty property;
       
    87     QByteArray expectedResult;
       
    88 
       
    89     // No parameters
       
    90     expectedResult = "FN:John Citizen\r\n";
       
    91     property.setName(QString::fromAscii("FN"));
       
    92     property.setValue(QString::fromAscii("John Citizen"));
       
    93     QTest::newRow("No parameters") << property << expectedResult;
       
    94 
       
    95     // With parameter(s)
       
    96     expectedResult = "TEL;TYPE=HOME:123\r\n";
       
    97     property.setName(QString::fromAscii("TEL"));
       
    98     property.setValue(QString::fromAscii("123"));
       
    99     property.insertParameter(QString::fromAscii("TYPE"),QString::fromAscii("HOME"));
       
   100     QTest::newRow("With parameters, plain value") << property << expectedResult;
       
   101 
       
   102     // normal FN property is backslash escaped
       
   103     property.clear();
       
   104     property.setName(QLatin1String("FN"));
       
   105     property.setValue(QLatin1String(";,:\\"));
       
   106     // semicolons, commas and backslashes are escaped (not colons, as per RFC2426)
       
   107     expectedResult = "FN:\\;\\,:\\\\\r\n";
       
   108     QTest::newRow("FN property") << property << expectedResult;
       
   109 
       
   110     // Structured N
       
   111     property.setName(QLatin1String("N"));
       
   112     property.setValue(QStringList()
       
   113                       << QLatin1String("La;st")    // needs to be backslash escaped
       
   114                       << QLatin1String("Fi,rst")
       
   115                       << QLatin1String("Mi:ddle")
       
   116                       << QLatin1String("Pr\\efix") // needs to be QP encoded
       
   117                       << QLatin1String("Suffix"));
       
   118     property.setValueType(QVersitProperty::CompoundType);
       
   119     expectedResult = "N:La\\;st;Fi\\,rst;Mi:ddle;Pr\\\\efix;Suffix\r\n";
       
   120     QTest::newRow("N property") << property << expectedResult;
       
   121 
       
   122     // Structured CATEGORIES
       
   123     property.setName(QLatin1String("CATEGORIES"));
       
   124     property.setValue(QStringList()
       
   125                       << QLatin1String("re;d")
       
   126                       << QLatin1String("gr,een")
       
   127                       << QLatin1String("bl:ue")
       
   128                       << QLatin1String("ye\\llow"));
       
   129     property.setValueType(QVersitProperty::ListType);
       
   130     expectedResult = "CATEGORIES:re\\;d,gr\\,een,bl:ue,ye\\\\llow\r\n";
       
   131     QTest::newRow("CATEGORIES property") << property << expectedResult;
       
   132 
       
   133     // Convert X-NICKNAME to NICKNAME
       
   134     expectedResult = "NICKNAME:Jack\r\n";
       
   135     property.setParameters(QMultiHash<QString,QString>());
       
   136     property.setName(QString::fromAscii("X-NICKNAME"));
       
   137     property.setValue(QString::fromAscii("Jack"));
       
   138     QTest::newRow("NICKNAME property") << property << expectedResult;
       
   139 
       
   140     // Convert X-IMPP to IMPP;
       
   141     expectedResult = "IMPP:msn:msn-address\r\n";
       
   142     property.setParameters(QMultiHash<QString,QString>());
       
   143     property.setName(QString::fromAscii("X-IMPP"));
       
   144     property.setValue(QString::fromAscii("msn:msn-address"));
       
   145     QTest::newRow("IMPP property") << property << expectedResult;
       
   146 
       
   147     // AGENT property
       
   148     expectedResult = "AGENT:BEGIN:VCARD\\nVERSION:3.0\\nFN:Secret Agent\\nEND:VCARD\\n\r\n";
       
   149     property.setName(QString::fromAscii("AGENT"));
       
   150     property.setValue(QString());
       
   151     QVersitDocument document;
       
   152     QVersitProperty embeddedProperty;
       
   153     embeddedProperty.setName(QString(QString::fromAscii("FN")));
       
   154     embeddedProperty.setValue(QString::fromAscii("Secret Agent"));
       
   155     document.addProperty(embeddedProperty);
       
   156     property.setValue(QVariant::fromValue(document));
       
   157     QTest::newRow("AGENT property") << property << expectedResult;
       
   158 
       
   159     // Value is base64 encoded.
       
   160     QByteArray value("value");
       
   161     expectedResult = "Springfield.HOUSE.PHOTO;ENCODING=B:" + value.toBase64() + "\r\n";
       
   162     QStringList groups(QString::fromAscii("Springfield"));
       
   163     groups.append(QString::fromAscii("HOUSE"));
       
   164     property.setGroups(groups);
       
   165     property.setParameters(QMultiHash<QString,QString>());
       
   166     property.setName(QString::fromAscii("PHOTO"));
       
   167     property.setValue(value);
       
   168     QTest::newRow("base64 encoded") << property << expectedResult;
       
   169 
       
   170     // Characters other than ASCII:
       
   171     expectedResult = "ORG:" + KATAKANA_NOKIA.toUtf8() + "\r\n";
       
   172     property = QVersitProperty();
       
   173     property.setName(QLatin1String("ORG"));
       
   174     property.setValue(KATAKANA_NOKIA);
       
   175     QTest::newRow("non-ASCII") << property << expectedResult;
       
   176 
       
   177     // No CHARSET and QUOTED-PRINTABLE parameters
       
   178     expectedResult = "EMAIL:john@" + KATAKANA_NOKIA.toUtf8() + ".com\r\n";
       
   179     property = QVersitProperty();
       
   180     property.setName(QLatin1String("EMAIL"));
       
   181     property.setValue(QString::fromAscii("john@%1.com").arg(KATAKANA_NOKIA));
       
   182     QTest::newRow("special chars") << property << expectedResult;
       
   183 }
       
   184 
       
   185 void tst_QVCard30Writer::testEncodeParameters()
       
   186 {
       
   187     QByteArray encodedParameters;
       
   188     QBuffer buffer(&encodedParameters);
       
   189     mWriter->setDevice(&buffer);
       
   190     buffer.open(QIODevice::WriteOnly);
       
   191 
       
   192     QString typeParameterName(QString::fromAscii("TYPE"));
       
   193     QString encodingParameterName(QString::fromAscii("ENCODING"));
       
   194 
       
   195     // No parameters
       
   196     QMultiHash<QString,QString> parameters;
       
   197     mWriter->encodeParameters(parameters);
       
   198     QCOMPARE(encodedParameters, QByteArray(""));
       
   199 
       
   200     // One TYPE parameter
       
   201     parameters.insert(typeParameterName,QString::fromAscii("HOME"));
       
   202     mWriter->writeCrlf(); // so it doesn't start folding
       
   203     buffer.close();
       
   204     encodedParameters.clear();
       
   205     buffer.open(QIODevice::WriteOnly);
       
   206     mWriter->encodeParameters(parameters);
       
   207     QCOMPARE(encodedParameters, QByteArray(";TYPE=HOME"));
       
   208 
       
   209     // Two TYPE parameters
       
   210     parameters.insert(typeParameterName,QString::fromAscii("VOICE"));
       
   211     mWriter->writeCrlf(); // so it doesn't start folding
       
   212     buffer.close();
       
   213     encodedParameters.clear();
       
   214     buffer.open(QIODevice::WriteOnly);
       
   215     mWriter->encodeParameters(parameters);
       
   216     QCOMPARE(encodedParameters, QByteArray(";TYPE=VOICE,HOME"));
       
   217 
       
   218     // One ENCODING parameter
       
   219     parameters.clear();
       
   220     parameters.insert(encodingParameterName,QString::fromAscii("8BIT"));
       
   221     mWriter->writeCrlf(); // so it doesn't start folding
       
   222     buffer.close();
       
   223     encodedParameters.clear();
       
   224     buffer.open(QIODevice::WriteOnly);
       
   225     mWriter->encodeParameters(parameters);
       
   226     QCOMPARE(encodedParameters, QByteArray(";ENCODING=8BIT"));
       
   227 
       
   228     // Two parameters
       
   229     parameters.insert(QString::fromAscii("X-PARAM"),QString::fromAscii("VALUE"));
       
   230     mWriter->writeCrlf(); // so it doesn't start folding
       
   231     buffer.close();
       
   232     encodedParameters.clear();
       
   233     buffer.open(QIODevice::WriteOnly);
       
   234     mWriter->encodeParameters(parameters);
       
   235     QCOMPARE(encodedParameters, QByteArray(";X-PARAM=VALUE;ENCODING=8BIT"));
       
   236 
       
   237     // Parameter with characters that require backslash escaping
       
   238     parameters.clear();
       
   239     parameters.insert(QString::fromAscii("X-P;ARAM"),QString::fromAscii("VA,LUE"));
       
   240     mWriter->writeCrlf(); // so it doesn't start folding
       
   241     buffer.close();
       
   242     encodedParameters.clear();
       
   243     buffer.open(QIODevice::WriteOnly);
       
   244     mWriter->encodeParameters(parameters);
       
   245     QCOMPARE(encodedParameters, QByteArray(";X-P\\;ARAM=VA\\,LUE"));
       
   246 }
       
   247 
       
   248 void tst_QVCard30Writer::testBackSlashEscape()
       
   249 {
       
   250     // Empty string
       
   251     QString input;
       
   252     QVCard30Writer::backSlashEscape(input);
       
   253     QCOMPARE(input,QString());
       
   254 
       
   255     // Nothing to escape in the string
       
   256     input = QString::fromAscii("Nothing to escape");
       
   257     QVCard30Writer::backSlashEscape(input);
       
   258     QCOMPARE(input,QString::fromAscii("Nothing to escape"));
       
   259 
       
   260     // Line break in the beginning
       
   261     input = QString::fromAscii("\r\n input");
       
   262     QVCard30Writer::backSlashEscape(input);
       
   263     QCOMPARE(input,QString::fromAscii("\\n input"));
       
   264 
       
   265     // Line break in the end
       
   266     input = QString::fromAscii("input\r\n");
       
   267     QVCard30Writer::backSlashEscape(input);
       
   268     QCOMPARE(input,QString::fromAscii("input\\n"));
       
   269 
       
   270     // Semicolon in the beginning
       
   271     input = QString::fromAscii(";input");
       
   272     QVCard30Writer::backSlashEscape(input);
       
   273     QCOMPARE(input,QString::fromAscii("\\;input"));
       
   274 
       
   275     // Semicolon in the end
       
   276     input = QString::fromAscii("input;");
       
   277     QVCard30Writer::backSlashEscape(input);
       
   278     QCOMPARE(input,QString::fromAscii("input\\;"));
       
   279 
       
   280     // Comma in the beginning
       
   281     input = QString::fromAscii(",input");
       
   282     QVCard30Writer::backSlashEscape(input);
       
   283     QCOMPARE(input,QString::fromAscii("\\,input"));
       
   284 
       
   285     // Comma in the end
       
   286     input = QString::fromAscii("input,");
       
   287     QVCard30Writer::backSlashEscape(input);
       
   288     QCOMPARE(input,QString::fromAscii("input\\,"));
       
   289 
       
   290     // Backslash in the beginning
       
   291     input = QString::fromAscii("\\input");
       
   292     QVCard30Writer::backSlashEscape(input);
       
   293     QCOMPARE(input,QString::fromAscii("\\\\input"));
       
   294 
       
   295     // Backslash in the end
       
   296     input = QString::fromAscii("input\\");
       
   297     QVCard30Writer::backSlashEscape(input);
       
   298     QCOMPARE(input,QString::fromAscii("input\\\\"));
       
   299 
       
   300     // Line break, semicolon, backslash and comma in the middle of the string
       
   301     input = QString::fromAscii("Escape these \r\n ; , \\ ");
       
   302     QVCard30Writer::backSlashEscape(input);
       
   303     QCOMPARE(input, QString::fromAscii("Escape these \\n \\; \\, \\\\ "));
       
   304 }
       
   305 
       
   306 QTEST_MAIN(tst_QVCard30Writer)
       
   307