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 module 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 ****************************************************************************/
42 #include <QtTest/QtTest>
44 #include <QAbstractMessageHandler>
45 #include <QAbstractUriResolver>
46 #include <QtNetwork/QNetworkAccessManager>
50 #include "../qabstracturiresolver/TestURIResolver.h"
51 #include "../qxmlquery/MessageSilencer.h"
56 \brief Tests class QXmlSchema.
58 This test is not intended for testing the engine, but the functionality specific
59 to the QXmlSchema class.
61 class tst_QXmlSchema : public QObject
66 void defaultConstructor() const;
67 void copyConstructor() const;
68 void constructorQXmlNamePool() const;
69 void copyMutationTest() const;
72 void documentUri() const;
74 void loadSchemaUrlSuccess() const;
75 void loadSchemaUrlFail() const;
76 void loadSchemaDeviceSuccess() const;
77 void loadSchemaDeviceFail() const;
78 void loadSchemaDataSuccess() const;
79 void loadSchemaDataFail() const;
81 void networkAccessManagerSignature() const;
82 void networkAccessManagerDefaultValue() const;
83 void networkAccessManager() const;
85 void messageHandlerSignature() const;
86 void messageHandlerDefaultValue() const;
87 void messageHandler() const;
89 void uriResolverSignature() const;
90 void uriResolverDefaultValue() const;
91 void uriResolver() const;
94 void tst_QXmlSchema::defaultConstructor() const
96 /* Allocate instance in different orders. */
113 void tst_QXmlSchema::copyConstructor() const
115 /* Verify that we can take a const reference, and simply do a copy of a default constructed object. */
117 const QXmlSchema schema1;
118 QXmlSchema schema2(schema1);
123 const QXmlSchema schema1;
124 QXmlSchema schema2(schema1);
125 QXmlSchema schema3(schema2);
128 /* Verify that copying default values works. */
130 const QXmlSchema schema1;
131 const QXmlSchema schema2(schema1);
132 QCOMPARE(schema2.messageHandler(), schema1.messageHandler());
133 QCOMPARE(schema2.uriResolver(), schema1.uriResolver());
134 QCOMPARE(schema2.networkAccessManager(), schema1.networkAccessManager());
135 QCOMPARE(schema2.isValid(), schema1.isValid());
139 void tst_QXmlSchema::constructorQXmlNamePool() const
143 QXmlNamePool np = schema.namePool();
145 const QXmlName name(np, QLatin1String("localName"),
146 QLatin1String("http://example.com/"),
147 QLatin1String("prefix"));
149 QXmlNamePool np2(schema.namePool());
150 QCOMPARE(name.namespaceUri(np2), QString::fromLatin1("http://example.com/"));
151 QCOMPARE(name.localName(np2), QString::fromLatin1("localName"));
152 QCOMPARE(name.prefix(np2), QString::fromLatin1("prefix"));
154 // make sure namePool() is const
155 const QXmlSchema constSchema;
156 np = constSchema.namePool();
159 void tst_QXmlSchema::copyMutationTest() const
162 QXmlSchema schema2(schema1);
164 // check that everything is equal
165 QVERIFY(schema2.messageHandler() == schema1.messageHandler());
166 QVERIFY(schema2.uriResolver() == schema1.uriResolver());
167 QVERIFY(schema2.networkAccessManager() == schema1.networkAccessManager());
169 MessageSilencer handler;
170 const TestURIResolver resolver;
171 QNetworkAccessManager manager;
174 schema1.setMessageHandler(&handler);
175 schema1.setUriResolver(&resolver);
176 schema1.setNetworkAccessManager(&manager);
178 // check that schema2 is not effected by the modifications of schema1
179 QVERIFY(schema2.messageHandler() != schema1.messageHandler());
180 QVERIFY(schema2.uriResolver() != schema1.uriResolver());
181 QVERIFY(schema2.networkAccessManager() != schema1.networkAccessManager());
183 // modify schema1 further
184 const QByteArray data( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
186 " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\""
187 " xmlns=\"http://qt.nokia.com/xmlschematest\""
188 " targetNamespace=\"http://qt.nokia.com/xmlschematest\""
190 " elementFormDefault=\"qualified\">"
193 const QUrl documentUri("http://qt.nokia.com/xmlschematest");
194 schema1.load(data, documentUri);
196 QVERIFY(schema2.isValid() != schema1.isValid());
199 void tst_QXmlSchema::isValid() const
201 /* Check default value. */
203 QVERIFY(!schema.isValid());
206 void tst_QXmlSchema::documentUri() const
208 const QByteArray data( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
210 " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\""
211 " xmlns=\"http://qt.nokia.com/xmlschematest\""
212 " targetNamespace=\"http://qt.nokia.com/xmlschematest\""
214 " elementFormDefault=\"qualified\">"
217 const QUrl documentUri("http://qt.nokia.com/xmlschematest");
219 schema.load(data, documentUri);
221 QCOMPARE(documentUri, schema.documentUri());
224 void tst_QXmlSchema::loadSchemaUrlSuccess() const
227 TODO: put valid schema file on given url and enable test
228 const QUrl url("http://notavailable/");
231 QVERIFY(!schema.load(url));
235 void tst_QXmlSchema::loadSchemaUrlFail() const
237 const QUrl url("http://notavailable/");
240 QVERIFY(!schema.load(url));
243 void tst_QXmlSchema::loadSchemaDeviceSuccess() const
245 QByteArray data( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
247 " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\""
248 " xmlns=\"http://qt.nokia.com/xmlschematest\""
249 " targetNamespace=\"http://qt.nokia.com/xmlschematest\""
251 " elementFormDefault=\"qualified\">"
254 QBuffer buffer(&data);
255 buffer.open(QIODevice::ReadOnly);
258 QVERIFY(schema.load(&buffer));
261 void tst_QXmlSchema::loadSchemaDeviceFail() const
263 QByteArray data( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
265 " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\""
266 " xmlns=\"http://qt.nokia.com/xmlschematest\""
267 " targetNamespace=\"http://qt.nokia.com/xmlschematest\""
269 " elementFormDefault=\"qualified\">"
272 QBuffer buffer(&data);
273 // a closed device can not be loaded
276 QVERIFY(!schema.load(&buffer));
279 void tst_QXmlSchema::loadSchemaDataSuccess() const
281 const QByteArray data( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
283 " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\""
284 " xmlns=\"http://qt.nokia.com/xmlschematest\""
285 " targetNamespace=\"http://qt.nokia.com/xmlschematest\""
287 " elementFormDefault=\"qualified\">"
290 QVERIFY(schema.load(data));
293 void tst_QXmlSchema::loadSchemaDataFail() const
295 // empty schema can not be loaded
296 const QByteArray data;
299 QVERIFY(!schema.load(data));
303 void tst_QXmlSchema::networkAccessManagerSignature() const
306 const QXmlSchema schema;
308 /* The function should be const. */
309 schema.networkAccessManager();
312 void tst_QXmlSchema::networkAccessManagerDefaultValue() const
314 /* Test that the default value of network access manager is not empty. */
317 QVERIFY(schema.networkAccessManager() != static_cast<QNetworkAccessManager*>(0));
321 void tst_QXmlSchema::networkAccessManager() const
323 /* Test that we return the network manager that was set. */
325 QNetworkAccessManager manager;
327 schema.setNetworkAccessManager(&manager);
328 QCOMPARE(schema.networkAccessManager(), &manager);
332 void tst_QXmlSchema::messageHandlerSignature() const
335 const QXmlSchema schema;
337 /* The function should be const. */
338 schema.messageHandler();
341 void tst_QXmlSchema::messageHandlerDefaultValue() const
343 /* Test that the default value of message handler is not empty. */
346 QVERIFY(schema.messageHandler() != static_cast<QAbstractMessageHandler*>(0));
350 void tst_QXmlSchema::messageHandler() const
352 /* Test that we return the message handler that was set. */
354 MessageSilencer handler;
357 schema.setMessageHandler(&handler);
358 QCOMPARE(schema.messageHandler(), static_cast<QAbstractMessageHandler *>(&handler));
362 void tst_QXmlSchema::uriResolverSignature() const
365 const QXmlSchema schema;
367 /* The function should be const. */
368 schema.uriResolver();
371 const TestURIResolver resolver;
373 /* This should compile */
375 schema2.setUriResolver(&resolver);
378 void tst_QXmlSchema::uriResolverDefaultValue() const
380 /* Test that the default value of uri resolver is empty. */
383 QVERIFY(schema.uriResolver() == static_cast<QAbstractUriResolver*>(0));
387 void tst_QXmlSchema::uriResolver() const
389 /* Test that we return the uri resolver that was set. */
391 TestURIResolver resolver;
394 schema.setUriResolver(&resolver);
395 QCOMPARE(schema.uriResolver(), static_cast<const QAbstractUriResolver *>(&resolver));
399 QTEST_MAIN(tst_QXmlSchema)
401 #include "tst_qxmlschema.moc"