tests/auto/qmessage/tst_qmessage.cpp
changeset 0 876b1a06bc25
equal deleted inserted replaced
-1:000000000000 0:876b1a06bc25
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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 #include <QObject>
       
    42 #include <QTest>
       
    43 #include <QDebug>
       
    44 
       
    45 #include "qmessage.h"
       
    46 #include "../support/support.h"
       
    47 
       
    48 #if (defined(Q_OS_SYMBIAN) || defined(Q_OS_WIN) && defined(_WIN32_WCE))
       
    49 # if defined(TESTDATA_DIR)
       
    50 #  undef TESTDATA_DIR
       
    51 # endif
       
    52 # define TESTDATA_DIR "."
       
    53 #endif
       
    54 
       
    55 //TESTED_CLASS=
       
    56 //TESTED_FILES=
       
    57 
       
    58 QTM_USE_NAMESPACE
       
    59 #ifdef Q_OS_WIN
       
    60 const QByteArray defaultCharset("UTF-16");
       
    61 #else
       
    62 const QByteArray defaultCharset("UTF-8");
       
    63 #endif
       
    64 
       
    65 template<typename T1, typename T2>
       
    66 void approximateCompare(const T1 &actual, const T2 &expected, const T2 &variance, int line)
       
    67 {
       
    68     if (!((expected - variance) < actual) ||
       
    69         !(actual < (expected + variance))) {
       
    70         qWarning() << "at line:" << line;
       
    71         qWarning() << "\tactual:" << actual;
       
    72         qWarning() << "\texpected:" << expected;
       
    73         qWarning() << "\tvariance:" << variance;
       
    74     }
       
    75     QVERIFY((expected - variance) < actual);
       
    76     QVERIFY(actual < (expected + variance));
       
    77 }
       
    78 #define QAPPROXIMATECOMPARE(a,e,v) approximateCompare(a,e,v,(__LINE__))
       
    79 
       
    80 /*
       
    81     Unit test for QMessage class.
       
    82 */
       
    83 class tst_QMessage : public QObject
       
    84 {
       
    85     Q_OBJECT
       
    86 
       
    87 public:
       
    88     tst_QMessage();
       
    89     virtual ~tst_QMessage();
       
    90 
       
    91 private slots:
       
    92     void initTestCase();
       
    93     void cleanup();
       
    94     void cleanupTestCase();
       
    95 
       
    96     void testType();
       
    97     void testParentAccountId();
       
    98     void testFrom();
       
    99     void testSubject();
       
   100     void testDate();
       
   101     void testReceivedDate();
       
   102     void testTo();
       
   103     void testCc();
       
   104     void testBcc();
       
   105     void testStatus();
       
   106     void testPriority();
       
   107     void testPreferredCharsets_data();
       
   108     void testPreferredCharsets();
       
   109     void testMessageAddress_data();
       
   110     void testMessageAddress();
       
   111     void testHeaderFields();
       
   112     void testStandardFolder();
       
   113 
       
   114 private:
       
   115     QMessageAccountId testAccountId;
       
   116     QMessageFolderId testFolderId;
       
   117 };
       
   118 
       
   119 QTEST_MAIN(tst_QMessage)
       
   120 
       
   121 #include "tst_qmessage.moc"
       
   122 
       
   123 tst_QMessage::tst_QMessage()
       
   124 {
       
   125 }
       
   126 
       
   127 tst_QMessage::~tst_QMessage()
       
   128 {
       
   129 }
       
   130 
       
   131 void tst_QMessage::initTestCase()
       
   132 {
       
   133 #if defined(Q_OS_WIN) && !defined(Q_OS_WINCE)
       
   134     if (!Support::mapiAvailable())
       
   135         QSKIP("Skipping tests because a MAPI subsystem does not appear to be available", SkipAll);
       
   136 #endif
       
   137 
       
   138     Support::clearMessageStore();
       
   139 
       
   140     {
       
   141         Support::Parameters p;
       
   142         p.insert("name", "testAccount");
       
   143 
       
   144         testAccountId = Support::addAccount(p);
       
   145         QVERIFY(testAccountId.isValid());
       
   146     }
       
   147 
       
   148     {
       
   149         Support::Parameters p;
       
   150         p.insert("path", "/root");
       
   151         p.insert("name", "Root");
       
   152         p.insert("parentAccountName", "testAccount");
       
   153 
       
   154         testFolderId = Support::addFolder(p);
       
   155         QVERIFY(testFolderId.isValid());
       
   156     }
       
   157 }
       
   158 
       
   159 void tst_QMessage::cleanup()
       
   160 {
       
   161 }
       
   162 
       
   163 void tst_QMessage::cleanupTestCase()
       
   164 {
       
   165 }
       
   166 
       
   167 void tst_QMessage::testType()
       
   168 {
       
   169     QMessage msg;
       
   170     QCOMPARE(msg.type(), QMessage::NoType);
       
   171     QCOMPARE(msg.isModified(), false);
       
   172 
       
   173     msg.setType(QMessage::Email);
       
   174     QCOMPARE(msg.type(), QMessage::Email);
       
   175     QCOMPARE(msg.isModified(), true);
       
   176 
       
   177     msg.setType(QMessage::Mms);
       
   178     QCOMPARE(msg.type(), QMessage::Mms);
       
   179 }
       
   180 
       
   181 void tst_QMessage::testParentAccountId()
       
   182 {
       
   183     QMessage msg;
       
   184     QCOMPARE(msg.parentAccountId(), QMessageAccountId());
       
   185     QCOMPARE(msg.isModified(), false);
       
   186 
       
   187     msg.setParentAccountId(testAccountId);
       
   188     QCOMPARE(msg.parentAccountId(), testAccountId);
       
   189     QCOMPARE(msg.isModified(), true);
       
   190 }
       
   191 
       
   192 void tst_QMessage::testFrom()
       
   193 {
       
   194     QMessage msg;
       
   195     QCOMPARE(msg.from(), QMessageAddress());
       
   196     QCOMPARE(msg.isModified(), false);
       
   197 
       
   198     QMessageAddress addr(QMessageAddress::Email, "alice@example.org");
       
   199     msg.setFrom(addr);
       
   200     QCOMPARE(msg.from(), addr);
       
   201     QCOMPARE(msg.from() != QMessageAddress(), true);
       
   202     QCOMPARE(msg.isModified(), true);
       
   203 
       
   204     addr = QMessageAddress(QMessageAddress::InstantMessage, "bob@example.org");
       
   205     msg.setFrom(addr);
       
   206     QCOMPARE(msg.from(), addr);
       
   207     QCOMPARE(msg.from() != QMessageAddress(), true);
       
   208 }
       
   209 
       
   210 void tst_QMessage::testSubject()
       
   211 {
       
   212     QMessage msg;
       
   213     QCOMPARE(msg.subject(), QString());
       
   214     QCOMPARE(msg.isModified(), false);
       
   215 
       
   216     QString subject("Short message");
       
   217     msg.setSubject(subject);
       
   218     QCOMPARE(msg.subject(), subject);
       
   219     QCOMPARE(msg.isModified(), true);
       
   220 
       
   221     subject = QString("A slightly longer message than the predecessor message");
       
   222     msg.setSubject(subject);
       
   223     QCOMPARE(msg.subject(), subject);
       
   224 }
       
   225 
       
   226 void tst_QMessage::testDate()
       
   227 {
       
   228     QMessage msg;
       
   229     QCOMPARE(msg.date(), QDateTime());
       
   230     QCOMPARE(msg.isModified(), false);
       
   231 
       
   232     QDateTime now(QDateTime::fromString(QDateTime::currentDateTime().toString(Qt::ISODate), Qt::ISODate));
       
   233     msg.setDate(now);
       
   234     QCOMPARE(msg.date(), now);
       
   235     QCOMPARE(msg.isModified(), true);
       
   236 
       
   237     now = QDateTime::fromString("2000-01-01T00:00:01Z", Qt::ISODate);
       
   238     msg.setDate(now);
       
   239     QCOMPARE(msg.date(), now);
       
   240 }
       
   241 
       
   242 void tst_QMessage::testReceivedDate()
       
   243 {
       
   244     QMessage msg;
       
   245     QCOMPARE(msg.receivedDate(), QDateTime());
       
   246     QCOMPARE(msg.isModified(), false);
       
   247 
       
   248     QDateTime now(QDateTime::fromString(QDateTime::currentDateTime().toString(Qt::ISODate), Qt::ISODate));
       
   249     msg.setReceivedDate(now);
       
   250     QCOMPARE(msg.receivedDate(), now);
       
   251     QCOMPARE(msg.isModified(), true);
       
   252 
       
   253     now = QDateTime::fromString("2000-01-01T00:00:01Z", Qt::ISODate);
       
   254     msg.setReceivedDate(now);
       
   255     QCOMPARE(msg.receivedDate(), now);
       
   256 }
       
   257 
       
   258 void tst_QMessage::testTo()
       
   259 {
       
   260     QMessage msg;
       
   261     QCOMPARE(msg.to(), QMessageAddressList());
       
   262     QCOMPARE(msg.isModified(), false);
       
   263 
       
   264     QMessageAddressList addresses;
       
   265     addresses.append(QMessageAddress(QMessageAddress::Email, "alice@example.org"));
       
   266     addresses.append(QMessageAddress(QMessageAddress::Email, "bob@example.org"));
       
   267 
       
   268     msg.setTo(addresses);
       
   269     QCOMPARE(msg.to(), addresses);
       
   270     QCOMPARE(msg.isModified(), true);
       
   271 
       
   272     addresses = QMessageAddressList();
       
   273     addresses.append(QMessageAddress(QMessageAddress::System, "charlie@example.org"));
       
   274     msg.setTo(addresses);
       
   275     QCOMPARE(msg.to(), addresses);
       
   276 }
       
   277 
       
   278 void tst_QMessage::testCc()
       
   279 {
       
   280     QMessage msg;
       
   281     QCOMPARE(msg.isModified(), false);
       
   282     QCOMPARE(msg.cc(), QMessageAddressList());
       
   283     QCOMPARE(msg.isModified(), false);
       
   284 
       
   285     QMessageAddressList addresses;
       
   286     addresses.append(QMessageAddress(QMessageAddress::Email, "alice@example.org"));
       
   287     addresses.append(QMessageAddress(QMessageAddress::Email, "bob@example.org"));
       
   288 
       
   289     msg.setCc(addresses);
       
   290     QCOMPARE(msg.cc(), addresses);
       
   291     QCOMPARE(msg.isModified(), true);
       
   292 
       
   293     addresses = QMessageAddressList();
       
   294     addresses.append(QMessageAddress(QMessageAddress::Phone, "charlie@example.org"));
       
   295     msg.setCc(addresses);
       
   296     QCOMPARE(msg.cc(), addresses);
       
   297 }
       
   298 
       
   299 void tst_QMessage::testBcc()
       
   300 {
       
   301     QMessage msg;
       
   302     QCOMPARE(msg.bcc(), QMessageAddressList());
       
   303     QCOMPARE(msg.isModified(), false);
       
   304 
       
   305     QMessageAddressList addresses;
       
   306     addresses.append(QMessageAddress(QMessageAddress::Email, "alice@example.org"));
       
   307     addresses.append(QMessageAddress(QMessageAddress::Email, "bob@example.org"));
       
   308 
       
   309     msg.setBcc(addresses);
       
   310     QCOMPARE(msg.bcc(), addresses);
       
   311     QCOMPARE(msg.isModified(), true);
       
   312 
       
   313     addresses = QMessageAddressList();
       
   314     addresses.append(QMessageAddress(QMessageAddress::InstantMessage, "charlie@example.org"));
       
   315     msg.setBcc(addresses);
       
   316     QCOMPARE(msg.bcc(), addresses);
       
   317 }
       
   318 
       
   319 void tst_QMessage::testStatus()
       
   320 {
       
   321     QMessage msg;
       
   322     QCOMPARE(msg.status(), static_cast<QMessage::StatusFlags>(0));
       
   323     QCOMPARE(msg.isModified(), false);
       
   324 
       
   325     msg.setStatus(QMessage::Removed);
       
   326     QCOMPARE(msg.status(), QMessage::Removed);
       
   327     QCOMPARE(msg.isModified(), true);
       
   328 
       
   329     msg.setStatus(QMessage::Read | QMessage::HasAttachments);
       
   330     QCOMPARE(msg.status(), QMessage::Read | QMessage::HasAttachments);
       
   331 }
       
   332 
       
   333 void tst_QMessage::testPriority()
       
   334 {
       
   335     QMessage msg;
       
   336     QCOMPARE(msg.priority(), QMessage::NormalPriority);
       
   337     QCOMPARE(msg.isModified(), false);
       
   338 
       
   339     msg.setPriority(QMessage::HighPriority);
       
   340     QCOMPARE(msg.priority(), QMessage::HighPriority);
       
   341     QCOMPARE(msg.isModified(), true);
       
   342 
       
   343     msg.setPriority(QMessage::LowPriority);
       
   344     QCOMPARE(msg.priority(), QMessage::LowPriority);
       
   345 }
       
   346 
       
   347 void tst_QMessage::testPreferredCharsets_data()
       
   348 {
       
   349     QTest::addColumn<QString>("text");
       
   350     QTest::addColumn<QByteArray>("preferred");
       
   351     QTest::addColumn<QByteArray>("encoded");
       
   352 
       
   353     const QString asciiText("ASCII text");
       
   354 
       
   355     QTest::newRow("ascii")
       
   356         << asciiText
       
   357         << QByteArray("ISO-8859-1")
       
   358         << QByteArray("ISO-8859-1");
       
   359 
       
   360     const QChar koreanChars[] = { 0xd55c };
       
   361     const QString koreanText(koreanChars, 1);
       
   362 
       
   363 #ifndef _WIN32_WCE
       
   364     // Qt for Win CE does not appear to include EUC-KR support...
       
   365     QTest::newRow("euc-kr")
       
   366         << koreanText
       
   367         << QByteArray("EUC-KR")
       
   368         << QByteArray("EUC-KR");
       
   369 #endif
       
   370 
       
   371     const QChar arabicChars[] = { 0x0636 };
       
   372     const QString mixedText(koreanText + QString(arabicChars, 1));
       
   373 
       
   374     QTest::newRow("mixed")
       
   375         << mixedText
       
   376         << QByteArray("")
       
   377         << defaultCharset;
       
   378 }
       
   379 
       
   380 void tst_QMessage::testPreferredCharsets()
       
   381 {
       
   382     QFETCH(QString, text);
       
   383     QFETCH(QByteArray, preferred);
       
   384     QFETCH(QByteArray, encoded);
       
   385    
       
   386     QCOMPARE(QMessage::preferredCharsets(), QList<QByteArray>());
       
   387 
       
   388     QList<QByteArray> preferredCharsets;
       
   389     preferredCharsets << "ISO-8859-1" << "EUC-KR";
       
   390 
       
   391     QMessage::setPreferredCharsets(preferredCharsets);
       
   392     QCOMPARE(QMessage::preferredCharsets(), preferredCharsets);
       
   393 
       
   394     QCOMPARE(QMessage::preferredCharsetFor(text).toLower(), preferred.toLower());
       
   395 
       
   396     QMessage msg;
       
   397     msg.setBody(text, "text/plain");
       
   398 
       
   399     QMessageContentContainer body(msg.find(msg.bodyId()));
       
   400     QCOMPARE(body.contentCharset().toLower(), encoded.toLower());
       
   401 
       
   402     QMessage::setPreferredCharsets(QList<QByteArray>());
       
   403 }
       
   404 
       
   405 void tst_QMessage::testMessageAddress_data()
       
   406 {
       
   407     QTest::addColumn<QString>("from");
       
   408     QTest::addColumn<QString>("targetName");
       
   409     QTest::addColumn<QString>("targetAddress");
       
   410 
       
   411     QTest::newRow("no angle brackets") 
       
   412         << "wizard@oz.test" 
       
   413         << "wizard@oz.test" 
       
   414         << "wizard@oz.test";
       
   415 
       
   416     QTest::newRow("name and address") 
       
   417         << "Wizard of Oz <wizard@oz.test>" 
       
   418         << "Wizard of Oz" 
       
   419         << "wizard@oz.test";
       
   420 
       
   421     QTest::newRow("quoted name and address") 
       
   422         << "\"First Last\" <first.last@example.com>"
       
   423         << "\"First Last\""
       
   424         << "first.last@example.com";
       
   425 
       
   426     QTest::newRow("quoted name with bracket and address") 
       
   427         << "\"First <Middle> Last\" <first.last@example.com>"
       
   428         << "\"First <Middle> Last\""
       
   429         << "first.last@example.com";
       
   430 }
       
   431 
       
   432 void tst_QMessage::testMessageAddress()
       
   433 {
       
   434     QFETCH(QString, from); 
       
   435     QFETCH(QString, targetName); 
       
   436     QFETCH(QString, targetAddress); 
       
   437 
       
   438     QString name;
       
   439     QString address;
       
   440     QMessageAddress::parseEmailAddress(from, &name, &address);
       
   441     QCOMPARE(targetName, name);
       
   442     QCOMPARE(targetAddress, address);
       
   443 }
       
   444 
       
   445 void tst_QMessage::testHeaderFields()
       
   446 {
       
   447     QMessage msg;
       
   448     
       
   449     QCOMPARE(msg.headerFields().isEmpty(), true);
       
   450     QCOMPARE(msg.headerFieldValues("Subject").isEmpty(), true);
       
   451     QCOMPARE(msg.headerFieldValues("From").isEmpty(), true);
       
   452     QCOMPARE(msg.headerFieldValues("To").isEmpty(), true);
       
   453     QCOMPARE(msg.headerFieldValues("X-None").isEmpty(), true);
       
   454     QCOMPARE(msg.headerFieldValue("Subject").isEmpty(), true);
       
   455     QCOMPARE(msg.headerFieldValue("From").isEmpty(), true);
       
   456     QCOMPARE(msg.headerFieldValue("To").isEmpty(), true);
       
   457     QCOMPARE(msg.headerFieldValue("X-None").isEmpty(), true);
       
   458 }
       
   459 
       
   460 void tst_QMessage::testStandardFolder()
       
   461 {
       
   462     QMessage msg;
       
   463     QCOMPARE(msg.standardFolder(), QMessage::DraftsFolder);
       
   464 }