| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | 
| 4 | ** Contact: https://www.qt.io/licensing/ | 
| 5 | ** | 
| 6 | ** This file is part of the test suite of the Qt Toolkit. | 
| 7 | ** | 
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ | 
| 9 | ** Commercial License Usage | 
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | 
| 11 | ** accordance with the commercial license agreement provided with the | 
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | 
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | 
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | 
| 16 | ** | 
| 17 | ** GNU General Public License Usage | 
| 18 | ** Alternatively, this file may be used under the terms of the GNU | 
| 19 | ** General Public License version 3 as published by the Free Software | 
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT | 
| 21 | ** included in the packaging of this file. Please review the following | 
| 22 | ** information to ensure the GNU General Public License requirements will | 
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. | 
| 24 | ** | 
| 25 | ** $QT_END_LICENSE$ | 
| 26 | ** | 
| 27 | ****************************************************************************/ | 
| 28 |  | 
| 29 |  | 
| 30 | #include <QtTest/QtTest> | 
| 31 |  | 
| 32 | #include <QtCore/QTextCodec> | 
| 33 | #include <QtXmlPatterns/QXmlSerializer> | 
| 34 | #include <QtXmlPatterns/QXmlQuery> | 
| 35 |  | 
| 36 | #include "../qxmlquery/MessageSilencer.h" | 
| 37 |  | 
| 38 | /*! | 
| 39 |  \class tst_QXmlSerializer | 
| 40 |  \internal | 
| 41 |  \since 4.4 | 
| 42 |  \brief Tests QSourceLocation | 
| 43 |  | 
| 44 |  */ | 
| 45 | class tst_QXmlSerializer : public QObject | 
| 46 | { | 
| 47 |     Q_OBJECT | 
| 48 |  | 
| 49 | private Q_SLOTS: | 
| 50 |     void constructorTriggerWarnings() const; | 
| 51 |     void objectSize() const; | 
| 52 |     void constCorrectness() const; | 
| 53 |     void setCodec() const; | 
| 54 |     void codec() const; | 
| 55 |     void outputDevice() const; | 
| 56 |     void serializationError() const; | 
| 57 |     void serializationError_data() const; | 
| 58 |     void cleanUpTestCase() const; | 
| 59 | }; | 
| 60 |  | 
| 61 | void tst_QXmlSerializer::constructorTriggerWarnings() const | 
| 62 | { | 
| 63 |     QXmlQuery query; | 
| 64 |  | 
| 65 |     QTest::ignoreMessage(type: QtWarningMsg, message: "outputDevice cannot be null." ); | 
| 66 |     QXmlSerializer(query, 0); | 
| 67 |  | 
| 68 |     QTest::ignoreMessage(type: QtWarningMsg, message: "outputDevice must be opened in write mode." ); | 
| 69 |     QBuffer buffer; | 
| 70 |     QXmlSerializer(query, &buffer); | 
| 71 | } | 
| 72 |  | 
| 73 | void tst_QXmlSerializer::constCorrectness() const | 
| 74 | { | 
| 75 |     QXmlQuery query; | 
| 76 |     QFile file(QLatin1String("dummy.xml" )); | 
| 77 |     file.open(flags: QIODevice::WriteOnly); | 
| 78 |     const QXmlSerializer serializer(query, &file); | 
| 79 |     /* These functions must be const. */ | 
| 80 |  | 
| 81 |     serializer.outputDevice(); | 
| 82 |     serializer.codec(); | 
| 83 | } | 
| 84 |  | 
| 85 | void tst_QXmlSerializer::objectSize() const | 
| 86 | { | 
| 87 |     QCOMPARE(sizeof(QXmlSerializer), sizeof(QAbstractXmlReceiver)); | 
| 88 | } | 
| 89 |  | 
| 90 | void tst_QXmlSerializer::setCodec() const | 
| 91 | { | 
| 92 |     QFile file(QLatin1String("dummy.xml" )); | 
| 93 |     file.open(flags: QIODevice::WriteOnly); | 
| 94 |  | 
| 95 |     /* Ensure we can take a const pointer. */ | 
| 96 |     { | 
| 97 |         QXmlQuery query; | 
| 98 |         QXmlSerializer serializer(query, &file); | 
| 99 |         serializer.setCodec(const_cast<const QTextCodec *>(QTextCodec::codecForName(name: "UTF-8" ))); | 
| 100 |     } | 
| 101 |  | 
| 102 |     /* Check that setting the codec has effect. */ | 
| 103 |     { | 
| 104 |         QXmlQuery query; | 
| 105 |         QXmlSerializer serializer(query, &file); | 
| 106 |         serializer.setCodec(const_cast<const QTextCodec *>(QTextCodec::codecForName(name: "UTF-16" ))); | 
| 107 |         QCOMPARE(serializer.codec()->name(), QTextCodec::codecForName("UTF-16" )->name()); | 
| 108 |  | 
| 109 |         /* Set it back. */ | 
| 110 |         serializer.setCodec(const_cast<const QTextCodec *>(QTextCodec::codecForName(name: "UTF-8" ))); | 
| 111 |         QCOMPARE(serializer.codec()->name(), QTextCodec::codecForName("UTF-8" )->name()); | 
| 112 |     } | 
| 113 | } | 
| 114 |  | 
| 115 | void tst_QXmlSerializer::codec() const | 
| 116 | { | 
| 117 |     QFile file(QLatin1String("dummy.xml" )); | 
| 118 |     file.open(flags: QIODevice::WriteOnly); | 
| 119 |  | 
| 120 |     /* Check default value. */ | 
| 121 |     { | 
| 122 |         const QXmlQuery query; | 
| 123 |         const QXmlSerializer serializer(query, &file); | 
| 124 |         QCOMPARE(serializer.codec()->name(), QTextCodec::codecForName("UTF-8" )->name()); | 
| 125 |     } | 
| 126 | } | 
| 127 |  | 
| 128 | void tst_QXmlSerializer::outputDevice() const | 
| 129 | { | 
| 130 |     QFile file(QLatin1String("dummy.xml" )); | 
| 131 |     file.open(flags: QIODevice::WriteOnly); | 
| 132 |  | 
| 133 |     /* Check default value. */ | 
| 134 |     { | 
| 135 |         const QXmlQuery query; | 
| 136 |         const QXmlSerializer serializer(query, &file); | 
| 137 |         QCOMPARE(serializer.outputDevice(), static_cast< QIODevice *>(&file)); | 
| 138 |     } | 
| 139 | } | 
| 140 |  | 
| 141 | void tst_QXmlSerializer::serializationError() const | 
| 142 | { | 
| 143 |     QFETCH(QString, queryString); | 
| 144 |     QXmlQuery query; | 
| 145 |     MessageSilencer silencer; | 
| 146 |     query.setMessageHandler(&silencer); | 
| 147 |  | 
| 148 |     query.setQuery(sourceCode: queryString); | 
| 149 |  | 
| 150 |     QByteArray output; | 
| 151 |     QBuffer buffer(&output); | 
| 152 |     QVERIFY(buffer.open(QIODevice::WriteOnly)); | 
| 153 |     QVERIFY(query.isValid()); | 
| 154 |  | 
| 155 |     QXmlSerializer serializer(query, &buffer); | 
| 156 |  | 
| 157 |     QEXPECT_FAIL("Two top elements" , "Bug, this is not checked for" , Continue); | 
| 158 |     QVERIFY(!query.evaluateTo(&serializer)); | 
| 159 | } | 
| 160 |  | 
| 161 | void tst_QXmlSerializer::serializationError_data() const | 
| 162 | { | 
| 163 |     QTest::addColumn<QString>(name: "queryString" ); | 
| 164 |  | 
| 165 |     QTest::newRow(dataTag: "Two top elements" ) | 
| 166 |         << QString::fromLatin1(str: "<e/>, <e/>" ); | 
| 167 |  | 
| 168 |     QTest::newRow(dataTag: "An attribute" ) | 
| 169 |         << QString::fromLatin1(str: "attribute name {'foo'}" ); | 
| 170 | } | 
| 171 |  | 
| 172 | void tst_QXmlSerializer::cleanUpTestCase() const | 
| 173 | { | 
| 174 |     QVERIFY(QFile::remove(QLatin1String("dummy.xml" ))); | 
| 175 | } | 
| 176 |  | 
| 177 | QTEST_MAIN(tst_QXmlSerializer) | 
| 178 |  | 
| 179 | #include "tst_qxmlserializer.moc" | 
| 180 |  | 
| 181 | // vim: et:ts=4:sw=4:sts=4 | 
| 182 |  |