09d1923e41aeec85f2338b33ffa23a6a07626de7
[profile/ivi/qtxmlpatterns.git] / tests / auto / qxmlitem / tst_qxmlitem.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 <QXmlItem>
46
47 /*!
48  \class tst_QXmlItem
49  \internal
50  \since 4.4
51  \brief Tests class QXmlItem.
52  */
53 class tst_QXmlItem : public QObject
54 {
55     Q_OBJECT
56
57 private Q_SLOTS:
58     void defaultConstructor() const;
59     void copyConstructor() const;
60     void copyConstructorFromQVariant() const;
61     void copyConstructorFromQXmlNodeModelIndex() const;
62     void assignmentOperator() const;
63     void isNull() const;
64     void isNode() const;
65     void isAtomicValue() const;
66     void toAtomicValue() const;
67     void toNodeModelIndex() const;
68
69     void objectSize() const;
70     void constCorrectness() const;
71     void withinQVariant() const;
72 };
73
74 void tst_QXmlItem::defaultConstructor() const
75 {
76     {
77         QXmlItem();
78     }
79
80     {
81         QXmlItem();
82         QXmlItem();
83     }
84
85     {
86         QXmlItem();
87         QXmlItem();
88         QXmlItem();
89     }
90 }
91
92 void tst_QXmlItem::copyConstructor() const
93 {
94     /* Check that we can copy from a const reference. */
95     {
96         const QXmlItem item;
97         const QXmlItem copy(item);
98     }
99
100     /* On a QXmlItem constructed from a null QVariant. */
101     {
102         const QXmlItem item((QVariant()));
103         const QXmlItem copy(item);
104     }
105
106     /* On a QXmlItem constructed from a null QXmlNodeModelIndex. */
107     {
108         const QXmlItem item((QXmlNodeModelIndex()));
109         const QXmlItem copy(item);
110     }
111 }
112
113 void tst_QXmlItem::copyConstructorFromQVariant() const
114 {
115     /* Construct & destruct a single value. */
116     {
117         const QXmlItem item(QVariant(QString::fromLatin1("dummy")));
118     }
119
120     /* Copy a null QVariant. */
121     {
122         const QXmlItem item((QVariant()));
123         QVERIFY(item.isNull());
124     }
125
126 }
127
128 void tst_QXmlItem::copyConstructorFromQXmlNodeModelIndex() const
129 {
130     // TODO copy a valid model index.
131
132     /* Construct from a null QXmlNodeModelIndex. */
133     {
134         const QXmlItem item((QXmlNodeModelIndex()));
135         QVERIFY(item.isNull());
136     }
137 }
138
139 void tst_QXmlItem::assignmentOperator() const
140 {
141     /* Assign to self. */
142     {
143         /* With null value. */
144         {
145             QXmlItem item;
146             item = item;
147             item = item;
148             item = item;
149             item = item;
150             item = item;
151         }
152
153         /* With the same atomic value. */
154         {
155             QXmlItem item(QVariant(QString::fromLatin1("dummy")));
156             item = item;
157             item = item;
158             item = item;
159             item = item;
160             item = item;
161         }
162
163         /* With the same node. */
164         {
165             // TODO
166         }
167
168         /* With a QXmlItem constructed from a null QVariant. */
169         {
170             QXmlItem item((QVariant()));
171             item = item;
172             item = item;
173             item = item;
174             item = item;
175             item = item;
176         }
177
178         /* With a QXmlItem constructed from a null QXmlNodeModelIndex. */
179         {
180             QXmlItem item((QXmlNodeModelIndex()));
181             item = item;
182             item = item;
183             item = item;
184             item = item;
185             item = item;
186         }
187     }
188 }
189
190 void tst_QXmlItem::isNull() const
191 {
192     /* Check default value. */
193     {
194         const QXmlItem item;
195         QVERIFY(item.isNull());
196     }
197
198     /* On atomic value. */
199     {
200         const QXmlItem item(QVariant(3));
201         QVERIFY(!item.isNull());
202     }
203
204     /* On a QXmlItem constructed from a null QVariant. */
205     {
206         const QXmlItem item((QVariant()));
207         QVERIFY(item.isNull());
208     }
209
210     /* On a QXmlItem constructed from a null QXmlNodeModelIndex. */
211     {
212         const QXmlItem item((QXmlNodeModelIndex()));
213         QVERIFY(item.isNull());
214     }
215 }
216
217 void tst_QXmlItem::isNode() const
218 {
219     /* Check default value. */
220     {
221         const QXmlItem item;
222         QVERIFY(!item.isNode());
223     }
224
225     /* On atomic value. */
226     {
227         const QXmlItem item(QVariant(3));
228         QVERIFY(!item.isNode());
229     }
230     // TODO on valid node index
231
232     /* On a QXmlItem constructed from a null QVariant. */
233     {
234         const QXmlItem item((QVariant()));
235         QVERIFY(!item.isNode());
236     }
237
238     /* On a QXmlItem constructed from a null QXmlNodeModelIndex. */
239     {
240         const QXmlItem item((QXmlNodeModelIndex()));
241         QVERIFY(!item.isNode());
242     }
243 }
244
245 void tst_QXmlItem::isAtomicValue() const
246 {
247     /* Check default value. */
248     {
249         const QXmlItem item;
250         QVERIFY(!item.isAtomicValue());
251     }
252
253     /* On valid atomic value. */
254     {
255         const QXmlItem item(QVariant(3));
256         QVERIFY(item.isAtomicValue());
257     }
258
259     // TODO on valid node index
260
261     /* On a QXmlItem constructed from a null QVariant. */
262     {
263         const QXmlItem item((QVariant()));
264         QVERIFY(!item.isAtomicValue());
265     }
266
267     /* On a QXmlItem constructed from a null QXmlNodeModelIndex. */
268     {
269         const QXmlItem item((QXmlNodeModelIndex()));
270         QVERIFY(!item.isAtomicValue());
271     }
272 }
273
274 void tst_QXmlItem::toAtomicValue() const
275 {
276     /* Check default value. */
277     {
278         const QXmlItem item;
279         QVERIFY(item.toAtomicValue().isNull());
280     }
281
282     /* On atomic value. */
283     {
284         const QXmlItem item(QVariant(3));
285         QCOMPARE(item.toAtomicValue(), QVariant(3));
286     }
287
288     /* On a QXmlItem constructed from a null QVariant. */
289     {
290         const QXmlItem item((QVariant()));
291         QVERIFY(item.toAtomicValue().isNull());
292     }
293
294     /* On a QXmlItem constructed from a null QXmlNodeModelIndex. */
295     {
296         const QXmlItem item((QXmlNodeModelIndex()));
297         QVERIFY(item.toAtomicValue().isNull());
298     }
299 }
300
301 void tst_QXmlItem::toNodeModelIndex() const
302 {
303     /* Check default value. */
304     {
305         const QXmlItem item;
306         QVERIFY(item.toNodeModelIndex().isNull());
307     }
308
309     /* On valid atomic value. */
310     {
311         const QXmlItem item(QVariant(3));
312         QVERIFY(item.toNodeModelIndex().isNull());
313     }
314
315     /* On a QXmlItem constructed from a null QVariant. */
316     {
317         const QXmlItem item((QVariant()));
318         QVERIFY(item.isNull());
319     }
320
321     /* On a QXmlItem constructed from a null QXmlNodeModelIndex. */
322     {
323         const QXmlItem item((QXmlNodeModelIndex()));
324         QVERIFY(item.isNull());
325     }
326 }
327
328 void tst_QXmlItem::objectSize() const
329 {
330     /* We can't currently test this in portable way,
331      * so disable it. */
332     return;
333
334     QCOMPARE(sizeof(QPatternist::NodeIndexStorage), sizeof(QXmlItem));
335
336     /* Data, additional data, and pointer to model. We test for two, such that we
337      * account for the padding that MSVC do. */
338     QVERIFY(sizeof(QXmlItem) == sizeof(qint64) * 2 + sizeof(QAbstractXmlNodeModel *) * 2
339             || sizeof(QXmlItem) == sizeof(qint64) * 2 + sizeof(QAbstractXmlNodeModel *) * 2);
340 }
341
342 /*!
343   Check that the functions that should be const, are.
344  */
345 void tst_QXmlItem::constCorrectness() const
346 {
347     const QXmlItem item;
348     item.isNull();
349     item.isNode();
350     item.isAtomicValue();
351
352     item.toAtomicValue();
353     item.toNodeModelIndex();
354 }
355
356 /*!
357   Check that QXmlItem can be used inside QVariant.
358  */
359 void tst_QXmlItem::withinQVariant() const
360 {
361     QXmlItem val;
362     const QVariant variant(qVariantFromValue(val));
363     QXmlItem val2(qVariantValue<QXmlItem>(variant));
364 }
365
366 QTEST_MAIN(tst_QXmlItem)
367
368 #include "tst_qxmlitem.moc"