Change copyrights from Nokia to Digia
[profile/ivi/qtxmlpatterns.git] / tests / auto / qsimplexmlnodemodel / tst_qsimplexmlnodemodel.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 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
43 #include <QtTest/QtTest>
44
45 #include <QSimpleXmlNodeModel>
46 #include <QXmlNamePool>
47 #include <QXmlQuery>
48 #include <QXmlSerializer>
49 #include <QXmlStreamReader>
50
51 #include "TestSimpleNodeModel.h"
52
53 /*!
54  \class tst_QSimpleXmlNodeModel
55  \internal
56  \since 4.4
57  \brief Tests class QSimpleXmlNodeModel.
58  */
59 class tst_QSimpleXmlNodeModel : public QObject
60 {
61     Q_OBJECT
62
63 private Q_SLOTS:
64     void namePool() const;
65     void namePoolIsReference() const;
66     void defaultConstructor() const;
67     void objectSize() const;
68     void constCorrectness() const;
69     void stringValue() const;
70 };
71
72 void tst_QSimpleXmlNodeModel::namePool() const
73 {
74     /* Check that the name pool we pass in, is what actually is returned. */
75     QXmlNamePool np;
76     const QXmlName name(np, QLatin1String("localName"),
77                             QLatin1String("http://example.com/XYZ"),
78                             QLatin1String("prefix432"));
79     TestSimpleNodeModel model(np);
80     const QXmlNamePool np2(model.namePool());
81
82     /* If it's a bug, this will more or less crash. */
83     QCOMPARE(name.namespaceUri(np2), QString::fromLatin1("http://example.com/XYZ"));
84     QCOMPARE(name.localName(np2), QString::fromLatin1("localName"));
85     QCOMPARE(name.prefix(np2), QString::fromLatin1("prefix432"));
86 }
87
88 void tst_QSimpleXmlNodeModel::namePoolIsReference() const
89 {
90     /* Test is placed in TestSimpleNodeModel.h:~50. */
91 }
92
93 void tst_QSimpleXmlNodeModel::defaultConstructor() const
94 {
95     QXmlNamePool np;
96 }
97
98 void tst_QSimpleXmlNodeModel::objectSize() const
99 {
100     /* We shouldn't have added any members. */
101     QCOMPARE(sizeof(QSimpleXmlNodeModel), sizeof(QAbstractXmlNodeModel));
102 }
103
104 void tst_QSimpleXmlNodeModel::constCorrectness() const
105 {
106     QXmlNamePool np;
107     const TestSimpleNodeModel instance(np);
108
109     instance.nextFromSimpleAxis(QSimpleXmlNodeModel::Parent, QXmlNodeModelIndex());
110     instance.attributes(QXmlNodeModelIndex());
111
112     instance.namePool();
113 }
114
115 /*!
116  Verify that if QAbstractXmlNodeModel::typedValue() return a null
117  QVariant, it is treated as that the node has no typed value.
118
119  This verifies the default implementation of QSimpleXmlNodeModel::stringValue().
120  */
121 void tst_QSimpleXmlNodeModel::stringValue() const
122 {
123     class TypedModel : public TestSimpleNodeModel
124     {
125     public:
126         TypedModel(const QXmlNamePool &np) : TestSimpleNodeModel(np)
127         {
128         }
129
130         virtual QXmlNodeModelIndex::NodeKind kind(const QXmlNodeModelIndex &) const
131         {
132             return QXmlNodeModelIndex::Element;
133         }
134
135         virtual QVariant typedValue(const QXmlNodeModelIndex &) const
136         {
137             return QVariant();
138         }
139
140         QXmlNodeModelIndex root() const
141         {
142             return createIndex(qint64(1));
143         }
144     };
145
146     QXmlNamePool np;
147     TypedModel model(np);
148
149     QXmlQuery query(np);
150     query.bindVariable(QLatin1String("node"), model.root());
151     query.setQuery(QLatin1String("declare variable $node external;"
152                                  "string($node), data($node)"));
153
154     QByteArray output;
155     QBuffer buffer(&output);
156     QVERIFY(buffer.open(QIODevice::WriteOnly));
157     QVERIFY(query.isValid());
158
159     QXmlSerializer serializer(query, &buffer);
160     QVERIFY(query.evaluateTo(&serializer));
161
162     QVERIFY(output.isEmpty());
163 }
164
165 QTEST_MAIN(tst_QSimpleXmlNodeModel)
166
167 #include "tst_qsimplexmlnodemodel.moc"