tests/auto/qxmlserializer/tst_qxmlserializer.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     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 test suite of the Qt Toolkit.
       
     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 
       
    43 #include <QtTest/QtTest>
       
    44 
       
    45 #ifdef QTEST_XMLPATTERNS
       
    46 #include <QtCore/QTextCodec>
       
    47 #include <QtXmlPatterns/QXmlSerializer>
       
    48 #include <QtXmlPatterns/QXmlQuery>
       
    49 
       
    50 #include "../qxmlquery/MessageSilencer.h"
       
    51 
       
    52 /*!
       
    53  \class tst_QXmlSerializer
       
    54  \internal
       
    55  \since 4.4
       
    56  \brief Tests QSourceLocation
       
    57 
       
    58  */
       
    59 class tst_QXmlSerializer : public QObject
       
    60 {
       
    61     Q_OBJECT
       
    62 
       
    63 private Q_SLOTS:
       
    64     void constructorTriggerWarnings() const;
       
    65     void objectSize() const;
       
    66     void constCorrectness() const;
       
    67     void setCodec() const;
       
    68     void codec() const;
       
    69     void outputDevice() const;
       
    70     void serializationError() const;
       
    71     void serializationError_data() const;
       
    72     void cleanUpTestCase() const;
       
    73 };
       
    74 
       
    75 void tst_QXmlSerializer::constructorTriggerWarnings() const
       
    76 {
       
    77     QXmlQuery query;
       
    78 
       
    79     QTest::ignoreMessage(QtWarningMsg, "outputDevice cannot be null.");
       
    80     QXmlSerializer(query, 0);
       
    81 
       
    82     QTest::ignoreMessage(QtWarningMsg, "outputDevice must be opened in write mode.");
       
    83     QBuffer buffer;
       
    84     QXmlSerializer(query, &buffer);
       
    85 }
       
    86 
       
    87 void tst_QXmlSerializer::constCorrectness() const
       
    88 {
       
    89     QXmlQuery query;
       
    90     QFile file(QLatin1String("dummy.xml"));
       
    91     file.open(QIODevice::WriteOnly);
       
    92     const QXmlSerializer serializer(query, &file);
       
    93     /* These functions must be const. */
       
    94 
       
    95     serializer.outputDevice();
       
    96     serializer.codec();
       
    97 }
       
    98 
       
    99 void tst_QXmlSerializer::objectSize() const
       
   100 {
       
   101     QCOMPARE(sizeof(QXmlSerializer), sizeof(QAbstractXmlReceiver));
       
   102 }
       
   103 
       
   104 void tst_QXmlSerializer::setCodec() const
       
   105 {
       
   106     QFile file(QLatin1String("dummy.xml"));
       
   107     file.open(QIODevice::WriteOnly);
       
   108 
       
   109     /* Ensure we can take a const pointer. */
       
   110     {
       
   111         QXmlQuery query;
       
   112         QXmlSerializer serializer(query, &file);
       
   113         serializer.setCodec(const_cast<const QTextCodec *>(QTextCodec::codecForName("UTF-8")));
       
   114     }
       
   115 
       
   116     /* Check that setting the codec has effect. */
       
   117     {
       
   118         QXmlQuery query;
       
   119         QXmlSerializer serializer(query, &file);
       
   120         serializer.setCodec(const_cast<const QTextCodec *>(QTextCodec::codecForName("UTF-16")));
       
   121         QCOMPARE(serializer.codec()->name(), QTextCodec::codecForName("UTF-16")->name());
       
   122 
       
   123         /* Set it back. */
       
   124         serializer.setCodec(const_cast<const QTextCodec *>(QTextCodec::codecForName("UTF-8")));
       
   125         QCOMPARE(serializer.codec()->name(), QTextCodec::codecForName("UTF-8")->name());
       
   126     }
       
   127 }
       
   128 
       
   129 void tst_QXmlSerializer::codec() const
       
   130 {
       
   131     QFile file(QLatin1String("dummy.xml"));
       
   132     file.open(QIODevice::WriteOnly);
       
   133 
       
   134     /* Check default value. */
       
   135     {
       
   136         const QXmlQuery query;
       
   137         const QXmlSerializer serializer(query, &file);
       
   138         QCOMPARE(serializer.codec()->name(), QTextCodec::codecForName("UTF-8")->name());
       
   139     }
       
   140 }
       
   141 
       
   142 void tst_QXmlSerializer::outputDevice() const
       
   143 {
       
   144     QFile file(QLatin1String("dummy.xml"));
       
   145     file.open(QIODevice::WriteOnly);
       
   146 
       
   147     /* Check default value. */
       
   148     {
       
   149         const QXmlQuery query;
       
   150         const QXmlSerializer serializer(query, &file);
       
   151         QCOMPARE(serializer.outputDevice(), &file);
       
   152     }
       
   153 }
       
   154 
       
   155 void tst_QXmlSerializer::serializationError() const
       
   156 {
       
   157     QFETCH(QString, queryString);
       
   158     QXmlQuery query;
       
   159     MessageSilencer silencer;
       
   160     query.setMessageHandler(&silencer);
       
   161     
       
   162     query.setQuery(queryString);
       
   163 
       
   164     QByteArray output;
       
   165     QBuffer buffer(&output);
       
   166     QVERIFY(buffer.open(QIODevice::WriteOnly));
       
   167     QVERIFY(query.isValid());
       
   168 
       
   169     QXmlSerializer serializer(query, &buffer);
       
   170 
       
   171     QEXPECT_FAIL("Two top elements", "Bug, this is not checked for", Continue);
       
   172     QVERIFY(!query.evaluateTo(&serializer));
       
   173 }
       
   174 
       
   175 void tst_QXmlSerializer::serializationError_data() const
       
   176 {
       
   177     QTest::addColumn<QString>("queryString");
       
   178 
       
   179     QTest::newRow("Two top elements")
       
   180         << QString::fromLatin1("<e/>, <e/>");
       
   181 
       
   182     QTest::newRow("An attribute")
       
   183         << QString::fromLatin1("attribute name {'foo'}");
       
   184 }
       
   185 
       
   186 void tst_QXmlSerializer::cleanUpTestCase() const
       
   187 {
       
   188     QVERIFY(QFile::remove(QLatin1String("dummy.xml")));
       
   189 }
       
   190 
       
   191 QTEST_MAIN(tst_QXmlSerializer)
       
   192 
       
   193 #include "tst_qxmlserializer.moc"
       
   194 #else
       
   195 QTEST_NOOP_MAIN
       
   196 #endif
       
   197 
       
   198 // vim: et:ts=4:sw=4:sts=4