1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
43 #include <QtTest/QtTest>
45 #include <QtCore/QTextCodec>
46 #include <QtXmlPatterns/QXmlSerializer>
47 #include <QtXmlPatterns/QXmlQuery>
49 #include "../qxmlquery/MessageSilencer.h"
52 \class tst_QXmlSerializer
55 \brief Tests QSourceLocation
58 class tst_QXmlSerializer : public QObject
63 void constructorTriggerWarnings() const;
64 void objectSize() const;
65 void constCorrectness() const;
66 void setCodec() const;
68 void outputDevice() const;
69 void serializationError() const;
70 void serializationError_data() const;
71 void cleanUpTestCase() const;
74 void tst_QXmlSerializer::constructorTriggerWarnings() const
78 QTest::ignoreMessage(QtWarningMsg, "outputDevice cannot be null.");
79 QXmlSerializer(query, 0);
81 QTest::ignoreMessage(QtWarningMsg, "outputDevice must be opened in write mode.");
83 QXmlSerializer(query, &buffer);
86 void tst_QXmlSerializer::constCorrectness() const
89 QFile file(QLatin1String("dummy.xml"));
90 file.open(QIODevice::WriteOnly);
91 const QXmlSerializer serializer(query, &file);
92 /* These functions must be const. */
94 serializer.outputDevice();
98 void tst_QXmlSerializer::objectSize() const
100 QCOMPARE(sizeof(QXmlSerializer), sizeof(QAbstractXmlReceiver));
103 void tst_QXmlSerializer::setCodec() const
105 QFile file(QLatin1String("dummy.xml"));
106 file.open(QIODevice::WriteOnly);
108 /* Ensure we can take a const pointer. */
111 QXmlSerializer serializer(query, &file);
112 serializer.setCodec(const_cast<const QTextCodec *>(QTextCodec::codecForName("UTF-8")));
115 /* Check that setting the codec has effect. */
118 QXmlSerializer serializer(query, &file);
119 serializer.setCodec(const_cast<const QTextCodec *>(QTextCodec::codecForName("UTF-16")));
120 QCOMPARE(serializer.codec()->name(), QTextCodec::codecForName("UTF-16")->name());
123 serializer.setCodec(const_cast<const QTextCodec *>(QTextCodec::codecForName("UTF-8")));
124 QCOMPARE(serializer.codec()->name(), QTextCodec::codecForName("UTF-8")->name());
128 void tst_QXmlSerializer::codec() const
130 QFile file(QLatin1String("dummy.xml"));
131 file.open(QIODevice::WriteOnly);
133 /* Check default value. */
135 const QXmlQuery query;
136 const QXmlSerializer serializer(query, &file);
137 QCOMPARE(serializer.codec()->name(), QTextCodec::codecForName("UTF-8")->name());
141 void tst_QXmlSerializer::outputDevice() const
143 QFile file(QLatin1String("dummy.xml"));
144 file.open(QIODevice::WriteOnly);
146 /* Check default value. */
148 const QXmlQuery query;
149 const QXmlSerializer serializer(query, &file);
150 QCOMPARE(serializer.outputDevice(), static_cast< QIODevice *>(&file));
154 void tst_QXmlSerializer::serializationError() const
156 QFETCH(QString, queryString);
158 MessageSilencer silencer;
159 query.setMessageHandler(&silencer);
161 query.setQuery(queryString);
164 QBuffer buffer(&output);
165 QVERIFY(buffer.open(QIODevice::WriteOnly));
166 QVERIFY(query.isValid());
168 QXmlSerializer serializer(query, &buffer);
170 QEXPECT_FAIL("Two top elements", "Bug, this is not checked for", Continue);
171 QVERIFY(!query.evaluateTo(&serializer));
174 void tst_QXmlSerializer::serializationError_data() const
176 QTest::addColumn<QString>("queryString");
178 QTest::newRow("Two top elements")
179 << QString::fromLatin1("<e/>, <e/>");
181 QTest::newRow("An attribute")
182 << QString::fromLatin1("attribute name {'foo'}");
185 void tst_QXmlSerializer::cleanUpTestCase() const
187 QVERIFY(QFile::remove(QLatin1String("dummy.xml")));
190 QTEST_MAIN(tst_QXmlSerializer)
192 #include "tst_qxmlserializer.moc"
194 // vim: et:ts=4:sw=4:sts=4