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