1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
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 http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file. Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
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
constructorTriggerWarnings() const75 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
constCorrectness() const87 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
objectSize() const99 void tst_QXmlSerializer::objectSize() const
100 {
101 QCOMPARE(sizeof(QXmlSerializer), sizeof(QAbstractXmlReceiver));
102 }
103
setCodec() const104 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
codec() const129 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
outputDevice() const142 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(), static_cast< QIODevice *>(&file));
152 }
153 }
154
serializationError() const155 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
serializationError_data() const175 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
cleanUpTestCase() const186 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
199