Change copyrights from Nokia to Digia
[profile/ivi/qtxmlpatterns.git] / tests / auto / qxmlschema / tst_qxmlschema.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the test suite module 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 Digia.  For licensing terms and
14 ** conditions see http://qt.digia.com/licensing.  For further information
15 ** use the contact form at http://qt.digia.com/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 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights.  These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file.  Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <QtTest/QtTest>
43
44 #include <QAbstractMessageHandler>
45 #include <QAbstractUriResolver>
46 #include <QtNetwork/QNetworkAccessManager>
47 #include <QXmlName>
48 #include <QXmlSchema>
49
50 #include "../qabstracturiresolver/TestURIResolver.h"
51 #include "../qxmlquery/MessageSilencer.h"
52
53 /*!
54  \class tst_QXmlSchema
55  \internal
56  \brief Tests class QXmlSchema.
57
58  This test is not intended for testing the engine, but the functionality specific
59  to the QXmlSchema class.
60  */
61 class tst_QXmlSchema : public QObject
62 {
63     Q_OBJECT
64
65 private Q_SLOTS:
66     void defaultConstructor() const;
67     void copyConstructor() const;
68     void constructorQXmlNamePool() const;
69     void copyMutationTest() const;
70
71     void isValid() const;
72     void documentUri() const;
73
74     void loadSchemaUrlSuccess() const;
75     void loadSchemaUrlFail() const;
76     void loadSchemaDeviceSuccess() const;
77     void loadSchemaDeviceFail() const;
78     void loadSchemaDataSuccess() const;
79     void loadSchemaDataFail() const;
80
81     void networkAccessManagerSignature() const;
82     void networkAccessManagerDefaultValue() const;
83     void networkAccessManager() const;
84
85     void messageHandlerSignature() const;
86     void messageHandlerDefaultValue() const;
87     void messageHandler() const;
88
89     void uriResolverSignature() const;
90     void uriResolverDefaultValue() const;
91     void uriResolver() const;
92 };
93
94 void tst_QXmlSchema::defaultConstructor() const
95 {
96     /* Allocate instance in different orders. */
97     {
98         QXmlSchema schema;
99     }
100
101     {
102         QXmlSchema schema1;
103         QXmlSchema schema2;
104     }
105
106     {
107         QXmlSchema schema1;
108         QXmlSchema schema2;
109         QXmlSchema schema3;
110     }
111 }
112
113 void tst_QXmlSchema::copyConstructor() const
114 {
115     /* Verify that we can take a const reference, and simply do a copy of a default constructed object. */
116     {
117         const QXmlSchema schema1;
118         QXmlSchema schema2(schema1);
119     }
120
121     /* Copy twice. */
122     {
123         const QXmlSchema schema1;
124         QXmlSchema schema2(schema1);
125         QXmlSchema schema3(schema2);
126     }
127
128     /* Verify that copying default values works. */
129     {
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());
136     }
137 }
138
139 void tst_QXmlSchema::constructorQXmlNamePool() const
140 {
141     QXmlSchema schema;
142
143     QXmlNamePool np = schema.namePool();
144
145     const QXmlName name(np, QLatin1String("localName"),
146                             QLatin1String("http://example.com/"),
147                             QLatin1String("prefix"));
148
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"));
153
154     // make sure namePool() is const
155     const QXmlSchema constSchema;
156     np = constSchema.namePool();
157 }
158
159 void tst_QXmlSchema::copyMutationTest() const
160 {
161     QXmlSchema schema1;
162     QXmlSchema schema2(schema1);
163
164     // check that everything is equal
165     QVERIFY(schema2.messageHandler() == schema1.messageHandler());
166     QVERIFY(schema2.uriResolver() == schema1.uriResolver());
167     QVERIFY(schema2.networkAccessManager() == schema1.networkAccessManager());
168
169     MessageSilencer handler;
170     const TestURIResolver resolver;
171     QNetworkAccessManager manager;
172
173     // modify schema1
174     schema1.setMessageHandler(&handler);
175     schema1.setUriResolver(&resolver);
176     schema1.setNetworkAccessManager(&manager);
177
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());
182
183     // modify schema1 further
184     const QByteArray data( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
185                            "<xsd:schema"
186                            "        xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\""
187                            "        xmlns=\"http://qt.nokia.com/xmlschematest\""
188                            "        targetNamespace=\"http://qt.nokia.com/xmlschematest\""
189                            "        version=\"1.0\""
190                            "        elementFormDefault=\"qualified\">"
191                            "</xsd:schema>" );
192
193     const QUrl documentUri("http://qt.nokia.com/xmlschematest");
194     schema1.load(data, documentUri);
195
196     QVERIFY(schema2.isValid() != schema1.isValid());
197 }
198
199 void tst_QXmlSchema::isValid() const
200 {
201     /* Check default value. */
202     QXmlSchema schema;
203     QVERIFY(!schema.isValid());
204 }
205
206 void tst_QXmlSchema::documentUri() const
207 {
208     const QByteArray data( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
209                            "<xsd:schema"
210                            "        xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\""
211                            "        xmlns=\"http://qt.nokia.com/xmlschematest\""
212                            "        targetNamespace=\"http://qt.nokia.com/xmlschematest\""
213                            "        version=\"1.0\""
214                            "        elementFormDefault=\"qualified\">"
215                            "</xsd:schema>" );
216
217     const QUrl documentUri("http://qt.nokia.com/xmlschematest");
218     QXmlSchema schema;
219     schema.load(data, documentUri);
220
221     QCOMPARE(documentUri, schema.documentUri());
222 }
223
224 void tst_QXmlSchema::loadSchemaUrlSuccess() const
225 {
226 /**
227     TODO: put valid schema file on given url and enable test
228     const QUrl url("http://notavailable/");
229
230     QXmlSchema schema;
231     QVERIFY(!schema.load(url));
232 */
233 }
234
235 void tst_QXmlSchema::loadSchemaUrlFail() const
236 {
237     const QUrl url("http://notavailable/");
238
239     QXmlSchema schema;
240     QVERIFY(!schema.load(url));
241 }
242
243 void tst_QXmlSchema::loadSchemaDeviceSuccess() const
244 {
245     QByteArray data( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
246                      "<xsd:schema"
247                      "        xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\""
248                      "        xmlns=\"http://qt.nokia.com/xmlschematest\""
249                      "        targetNamespace=\"http://qt.nokia.com/xmlschematest\""
250                      "        version=\"1.0\""
251                      "        elementFormDefault=\"qualified\">"
252                      "</xsd:schema>" );
253
254     QBuffer buffer(&data);
255     buffer.open(QIODevice::ReadOnly);
256
257     QXmlSchema schema;
258     QVERIFY(schema.load(&buffer));
259 }
260
261 void tst_QXmlSchema::loadSchemaDeviceFail() const
262 {
263     QByteArray data( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
264                      "<xsd:schema"
265                      "        xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\""
266                      "        xmlns=\"http://qt.nokia.com/xmlschematest\""
267                      "        targetNamespace=\"http://qt.nokia.com/xmlschematest\""
268                      "        version=\"1.0\""
269                      "        elementFormDefault=\"qualified\">"
270                      "</xsd:schema>" );
271
272     QBuffer buffer(&data);
273     // a closed device can not be loaded
274
275     QXmlSchema schema;
276     QVERIFY(!schema.load(&buffer));
277 }
278
279 void tst_QXmlSchema::loadSchemaDataSuccess() const
280 {
281     const QByteArray data( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
282                            "<xsd:schema"
283                            "        xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\""
284                            "        xmlns=\"http://qt.nokia.com/xmlschematest\""
285                            "        targetNamespace=\"http://qt.nokia.com/xmlschematest\""
286                            "        version=\"1.0\""
287                            "        elementFormDefault=\"qualified\">"
288                            "</xsd:schema>" );
289     QXmlSchema schema;
290     QVERIFY(schema.load(data));
291 }
292
293 void tst_QXmlSchema::loadSchemaDataFail() const
294 {
295     // empty schema can not be loaded
296     const QByteArray data;
297
298     QXmlSchema schema;
299     QVERIFY(!schema.load(data));
300 }
301
302
303 void tst_QXmlSchema::networkAccessManagerSignature() const
304 {
305     /* Const object. */
306     const QXmlSchema schema;
307
308     /* The function should be const. */
309     schema.networkAccessManager();
310 }
311
312 void tst_QXmlSchema::networkAccessManagerDefaultValue() const
313 {
314     /* Test that the default value of network access manager is not empty. */
315     {
316         QXmlSchema schema;
317         QVERIFY(schema.networkAccessManager() != static_cast<QNetworkAccessManager*>(0));
318     }
319 }
320
321 void tst_QXmlSchema::networkAccessManager() const
322 {
323     /* Test that we return the network manager that was set. */
324     {
325         QNetworkAccessManager manager;
326         QXmlSchema schema;
327         schema.setNetworkAccessManager(&manager);
328         QCOMPARE(schema.networkAccessManager(), &manager);
329     }
330 }
331
332 void tst_QXmlSchema::messageHandlerSignature() const
333 {
334     /* Const object. */
335     const QXmlSchema schema;
336
337     /* The function should be const. */
338     schema.messageHandler();
339 }
340
341 void tst_QXmlSchema::messageHandlerDefaultValue() const
342 {
343     /* Test that the default value of message handler is not empty. */
344     {
345         QXmlSchema schema;
346         QVERIFY(schema.messageHandler() != static_cast<QAbstractMessageHandler*>(0));
347     }
348 }
349
350 void tst_QXmlSchema::messageHandler() const
351 {
352     /* Test that we return the message handler that was set. */
353     {
354         MessageSilencer handler;
355
356         QXmlSchema schema;
357         schema.setMessageHandler(&handler);
358         QCOMPARE(schema.messageHandler(), static_cast<QAbstractMessageHandler *>(&handler));
359     }
360 }
361
362 void tst_QXmlSchema::uriResolverSignature() const
363 {
364     /* Const object. */
365     const QXmlSchema schema;
366
367     /* The function should be const. */
368     schema.uriResolver();
369
370     /* Const object. */
371     const TestURIResolver resolver;
372
373     /* This should compile */
374     QXmlSchema schema2;
375     schema2.setUriResolver(&resolver);
376 }
377
378 void tst_QXmlSchema::uriResolverDefaultValue() const
379 {
380     /* Test that the default value of uri resolver is empty. */
381     {
382         QXmlSchema schema;
383         QVERIFY(schema.uriResolver() == static_cast<QAbstractUriResolver*>(0));
384     }
385 }
386
387 void tst_QXmlSchema::uriResolver() const
388 {
389     /* Test that we return the uri resolver that was set. */
390     {
391         TestURIResolver resolver;
392
393         QXmlSchema schema;
394         schema.setUriResolver(&resolver);
395         QCOMPARE(schema.uriResolver(), static_cast<const QAbstractUriResolver *>(&resolver));
396     }
397 }
398
399 QTEST_MAIN(tst_QXmlSchema)
400
401 #include "tst_qxmlschema.moc"