1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the test suite of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
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 https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | |
29 | |
30 | #include <QtTest/QtTest> |
31 | |
32 | #include <QSimpleXmlNodeModel> |
33 | #include <QXmlNamePool> |
34 | #include <QXmlQuery> |
35 | #include <QXmlSerializer> |
36 | #include <QXmlStreamReader> |
37 | |
38 | #include "TestSimpleNodeModel.h" |
39 | |
40 | /*! |
41 | \class tst_QSimpleXmlNodeModel |
42 | \internal |
43 | \since 4.4 |
44 | \brief Tests class QSimpleXmlNodeModel. |
45 | */ |
46 | class tst_QSimpleXmlNodeModel : public QObject |
47 | { |
48 | Q_OBJECT |
49 | |
50 | private Q_SLOTS: |
51 | void namePool() const; |
52 | void namePoolIsReference() const; |
53 | void defaultConstructor() const; |
54 | void objectSize() const; |
55 | void constCorrectness() const; |
56 | void stringValue() const; |
57 | }; |
58 | |
59 | void tst_QSimpleXmlNodeModel::namePool() const |
60 | { |
61 | /* Check that the name pool we pass in, is what actually is returned. */ |
62 | QXmlNamePool np; |
63 | const QXmlName name(np, QLatin1String("localName" ), |
64 | QLatin1String("http://example.com/XYZ" ), |
65 | QLatin1String("prefix432" )); |
66 | TestSimpleNodeModel model(np); |
67 | const QXmlNamePool np2(model.namePool()); |
68 | |
69 | /* If it's a bug, this will more or less crash. */ |
70 | QCOMPARE(name.namespaceUri(np2), QString::fromLatin1("http://example.com/XYZ" )); |
71 | QCOMPARE(name.localName(np2), QString::fromLatin1("localName" )); |
72 | QCOMPARE(name.prefix(np2), QString::fromLatin1("prefix432" )); |
73 | } |
74 | |
75 | void tst_QSimpleXmlNodeModel::namePoolIsReference() const |
76 | { |
77 | /* Test is placed in TestSimpleNodeModel.h:~50. */ |
78 | } |
79 | |
80 | void tst_QSimpleXmlNodeModel::defaultConstructor() const |
81 | { |
82 | QXmlNamePool np; |
83 | } |
84 | |
85 | void tst_QSimpleXmlNodeModel::objectSize() const |
86 | { |
87 | /* We shouldn't have added any members. */ |
88 | QCOMPARE(sizeof(QSimpleXmlNodeModel), sizeof(QAbstractXmlNodeModel)); |
89 | } |
90 | |
91 | void tst_QSimpleXmlNodeModel::constCorrectness() const |
92 | { |
93 | QXmlNamePool np; |
94 | const TestSimpleNodeModel instance(np); |
95 | |
96 | instance.nextFromSimpleAxis(QSimpleXmlNodeModel::Parent, QXmlNodeModelIndex()); |
97 | instance.attributes(QXmlNodeModelIndex()); |
98 | |
99 | instance.namePool(); |
100 | } |
101 | |
102 | /*! |
103 | Verify that if QAbstractXmlNodeModel::typedValue() return a null |
104 | QVariant, it is treated as that the node has no typed value. |
105 | |
106 | This verifies the default implementation of QSimpleXmlNodeModel::stringValue(). |
107 | */ |
108 | void tst_QSimpleXmlNodeModel::stringValue() const |
109 | { |
110 | class TypedModel : public TestSimpleNodeModel |
111 | { |
112 | public: |
113 | TypedModel(const QXmlNamePool &np) : TestSimpleNodeModel(np) |
114 | { |
115 | } |
116 | |
117 | virtual QXmlNodeModelIndex::NodeKind kind(const QXmlNodeModelIndex &) const |
118 | { |
119 | return QXmlNodeModelIndex::Element; |
120 | } |
121 | |
122 | virtual QVariant typedValue(const QXmlNodeModelIndex &) const |
123 | { |
124 | return QVariant(); |
125 | } |
126 | |
127 | QXmlNodeModelIndex rootIndex() const |
128 | { |
129 | return createIndex(data: qint64(1)); |
130 | } |
131 | }; |
132 | |
133 | QXmlNamePool np; |
134 | TypedModel model(np); |
135 | |
136 | QXmlQuery query(np); |
137 | query.bindVariable(localName: QLatin1String("node" ), value: model.rootIndex()); |
138 | query.setQuery(sourceCode: QLatin1String("declare variable $node external;" |
139 | "string($node), data($node)" )); |
140 | |
141 | QByteArray output; |
142 | QBuffer buffer(&output); |
143 | QVERIFY(buffer.open(QIODevice::WriteOnly)); |
144 | QVERIFY(query.isValid()); |
145 | |
146 | QXmlSerializer serializer(query, &buffer); |
147 | QVERIFY(query.evaluateTo(&serializer)); |
148 | |
149 | QVERIFY(output.isEmpty()); |
150 | } |
151 | |
152 | QTEST_MAIN(tst_QSimpleXmlNodeModel) |
153 | |
154 | #include "tst_qsimplexmlnodemodel.moc" |
155 | |