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 <qvariant.h>
33
34#include <qkeysequence.h>
35#include <qbitmap.h>
36#include <qcursor.h>
37#include <qimage.h>
38#include <qicon.h>
39#include <qmatrix.h>
40#include <qmatrix4x4.h>
41#include <qpen.h>
42#include <qpolygon.h>
43#include <qpalette.h>
44#include <qtransform.h>
45#include <qvector2d.h>
46#include <qvector3d.h>
47#include <qvector4d.h>
48#include <qquaternion.h>
49#include <qtextdocument.h>
50#include <qtextformat.h>
51#include <qfont.h>
52
53#include "tst_qvariant_common.h"
54
55class tst_QGuiVariant : public QObject
56{
57 Q_OBJECT
58
59private slots:
60 void constructor_invalid_data();
61 void constructor_invalid();
62
63 void canConvert_data();
64 void canConvert();
65
66 void toInt_data();
67 void toInt();
68
69 void toFont_data();
70 void toFont();
71
72 void toKeySequence_data();
73 void toKeySequence();
74
75 void toString_data();
76 void toString();
77
78 void toColor_data();
79 void toColor();
80
81 void toPixmap_data();
82 void toPixmap();
83
84 void toImage_data();
85 void toImage();
86
87 void toBrush_data();
88 void toBrush();
89
90 void matrix();
91
92 void transform();
93
94 void matrix4x4();
95 void vector2D();
96 void vector3D();
97 void vector4D();
98 void quaternion();
99
100 void writeToReadFromDataStream_data();
101 void writeToReadFromDataStream();
102 void writeToReadFromOldDataStream();
103
104 void colorInteger();
105 void invalidQColor();
106 void validQColor();
107
108 void debugStream_data();
109 void debugStream();
110
111 void implicitConstruction();
112
113 void guiVariantAtExit();
114
115 void iconEquality();
116 void qt4QPolygonFDataStream();
117};
118
119void tst_QGuiVariant::constructor_invalid_data()
120{
121 QTest::addColumn<uint>(name: "typeId");
122
123 QTest::newRow(dataTag: "LastGuiType + 1") << uint(QMetaType::LastGuiType + 1);
124 QVERIFY(!QMetaType::isRegistered(QMetaType::LastGuiType + 1));
125 QTest::newRow(dataTag: "LastWidgetsType + 1") << uint(QMetaType::LastWidgetsType + 1);
126 QVERIFY(!QMetaType::isRegistered(QMetaType::LastWidgetsType + 1));
127}
128
129void tst_QGuiVariant::constructor_invalid()
130{
131
132 QFETCH(uint, typeId);
133 {
134 QTest::ignoreMessage(type: QtWarningMsg, messagePattern: QRegularExpression("^Trying to construct an instance of an invalid type, type id:"));
135 QVariant variant(static_cast<QVariant::Type>(typeId));
136 QVERIFY(!variant.isValid());
137 QCOMPARE(variant.userType(), int(QMetaType::UnknownType));
138 }
139 {
140 QTest::ignoreMessage(type: QtWarningMsg, messagePattern: QRegularExpression("^Trying to construct an instance of an invalid type, type id:"));
141 QVariant variant(typeId, /* copy */ 0);
142 QVERIFY(!variant.isValid());
143 QCOMPARE(variant.userType(), int(QMetaType::UnknownType));
144 }
145}
146
147void tst_QGuiVariant::canConvert_data()
148{
149 TST_QVARIANT_CANCONVERT_DATATABLE_HEADERS
150
151#ifdef Y
152#undef Y
153#endif
154#ifdef N
155#undef N
156#endif
157#define Y true
158#define N false
159
160 QVariant var;
161
162 // bita bitm bool brsh byta col curs date dt dbl font img int inv kseq list ll map pal pen pix pnt rect reg size sp str strl time uint ull
163
164
165 var = QVariant::fromValue(value: QBitmap());
166 QTest::newRow(dataTag: "Bitmap")
167 << var << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N << N << N;
168 var = QVariant::fromValue(value: QBrush());
169 QTest::newRow(dataTag: "Brush")
170 << var << N << N << N << Y << N << Y << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N << N << N;
171 var = QVariant::fromValue(value: QColor());
172 QTest::newRow(dataTag: "Color")
173 << var << N << N << N << Y << Y << Y << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N;
174#ifndef QT_NO_CURSOR
175 var = QVariant::fromValue(value: QCursor());
176 QTest::newRow(dataTag: "Cursor")
177 << var << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N;
178#endif
179 var = QVariant::fromValue(value: QFont());
180 QTest::newRow(dataTag: "Font")
181 << var << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N;
182 var = QVariant::fromValue(value: QIcon());
183 QTest::newRow(dataTag: "Icon")
184 << var << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N;
185 var = QVariant::fromValue(value: QImage());
186 QTest::newRow(dataTag: "Image")
187 << var << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N << N << N;
188 var = QVariant::fromValue(value: QKeySequence());
189 QTest::newRow(dataTag: "KeySequence")
190 << var << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << Y << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N;
191 var = QVariant::fromValue(value: QPalette());
192 QTest::newRow(dataTag: "Palette")
193 << var << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N << N << N << N << N;
194 var = QVariant::fromValue(value: QPen());
195 QTest::newRow(dataTag: "Pen")
196 << var << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N << N << N << N;
197 var = QVariant::fromValue(value: QPixmap());
198 QTest::newRow(dataTag: "Pixmap")
199 << var << N << Y << N << Y << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N << N << N;
200 var = QVariant::fromValue(value: QPolygon());
201 QTest::newRow(dataTag: "PointArray")
202 << var << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N;
203 var = QVariant::fromValue(value: QRegion());
204 QTest::newRow(dataTag: "Region")
205 << var << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N;
206
207#undef N
208#undef Y
209}
210
211void tst_QGuiVariant::canConvert()
212{
213 TST_QVARIANT_CANCONVERT_FETCH_DATA
214
215 TST_QVARIANT_CANCONVERT_COMPARE_DATA
216}
217
218void tst_QGuiVariant::toInt_data()
219{
220 QTest::addColumn<QVariant>(name: "value");
221 QTest::addColumn<int>(name: "result");
222 QTest::addColumn<bool>(name: "valueOK");
223
224 QTest::newRow( dataTag: "keysequence" ) << QVariant::fromValue( value: QKeySequence( Qt::Key_A ) ) << 65 << true;
225}
226
227void tst_QGuiVariant::toInt()
228{
229 QFETCH( QVariant, value );
230 QFETCH( int, result );
231 QFETCH( bool, valueOK );
232 QVERIFY( value.isValid() == value.canConvert( QVariant::Int ) );
233 bool ok;
234 int i = value.toInt( ok: &ok );
235 QCOMPARE( i, result );
236 QVERIFY( ok == valueOK );
237}
238
239void tst_QGuiVariant::toColor_data()
240{
241 QTest::addColumn<QVariant>(name: "value");
242 QTest::addColumn<QColor>(name: "result");
243
244 QColor c("red");
245 QTest::newRow( dataTag: "string" ) << QVariant( QString( "red" ) ) << c;
246 QTest::newRow( dataTag: "solid brush" ) << QVariant( QBrush(c) ) << c;
247 QTest::newRow(dataTag: "qbytearray") << QVariant(QByteArray("red")) << c;
248 QTest::newRow(dataTag: "same color") << QVariant(c) << c;
249 QTest::newRow(dataTag: "qstring(#ff0000)") << QVariant(QString::fromUtf8(str: "#ff0000")) << c;
250 QTest::newRow(dataTag: "qbytearray(#ff0000)") << QVariant(QByteArray("#ff0000")) << c;
251
252 c.setNamedColor("#88112233");
253 QTest::newRow(dataTag: "qstring(#88112233)") << QVariant(QString::fromUtf8(str: "#88112233")) << c;
254 QTest::newRow(dataTag: "qbytearray(#88112233)") << QVariant(QByteArray("#88112233")) << c;
255}
256
257void tst_QGuiVariant::toColor()
258{
259 QFETCH( QVariant, value );
260 QFETCH( QColor, result );
261 QVERIFY( value.isValid() );
262 QVERIFY( value.canConvert( QVariant::Color ) );
263 QColor d = qvariant_cast<QColor>(v: value);
264 QCOMPARE( d, result );
265 QVERIFY(value.convert(QMetaType::QColor));
266 QCOMPARE(d, QColor(value.toString()));
267}
268
269void tst_QGuiVariant::toPixmap_data()
270{
271 QTest::addColumn<QVariant>(name: "value");
272 QTest::addColumn<QPixmap>(name: "result");
273
274 QPixmap pm(30, 30);
275 pm.fill(fillColor: Qt::red);
276 QTest::newRow( dataTag: "image" ) << QVariant( pm ) << pm;
277
278 QBitmap bm(30, 30);
279 bm.fill(fillColor: Qt::color1);
280 QTest::newRow( dataTag: "bitmap" ) << QVariant( bm ) << QPixmap(bm);
281}
282
283void tst_QGuiVariant::toPixmap()
284{
285 QFETCH( QVariant, value );
286 QFETCH( QPixmap, result );
287 QVERIFY( value.isValid() );
288 QVERIFY( value.canConvert( QVariant::Pixmap ) );
289 QPixmap d = qvariant_cast<QPixmap>(v: value);
290 QCOMPARE( d, result );
291}
292
293void tst_QGuiVariant::toImage_data()
294{
295 QTest::addColumn<QVariant>(name: "value");
296 QTest::addColumn<QImage>(name: "result");
297
298 QImage im(30, 30, QImage::Format_ARGB32);
299 im.fill(pixel: 0x7fff0000);
300 QTest::newRow( dataTag: "image" ) << QVariant( im ) << im;
301}
302
303void tst_QGuiVariant::toImage()
304{
305 QFETCH( QVariant, value );
306 QFETCH( QImage, result );
307 QVERIFY( value.isValid() );
308 QVERIFY( value.canConvert( QVariant::Image ) );
309 QImage d = qvariant_cast<QImage>(v: value);
310 QCOMPARE( d, result );
311}
312
313void tst_QGuiVariant::toBrush_data()
314{
315 QTest::addColumn<QVariant>(name: "value");
316 QTest::addColumn<QBrush>(name: "result");
317
318 QColor c(Qt::red);
319 QTest::newRow( dataTag: "color" ) << QVariant( c ) << QBrush(c);
320 QPixmap pm(30, 30);
321 pm.fill(fillColor: c);
322 QTest::newRow( dataTag: "pixmap" ) << QVariant( pm ) << QBrush(pm);
323}
324
325void tst_QGuiVariant::toBrush()
326{
327 QFETCH( QVariant, value );
328 QFETCH( QBrush, result );
329 QVERIFY( value.isValid() );
330 QVERIFY( value.canConvert( QVariant::Brush ) );
331 QBrush d = qvariant_cast<QBrush>(v: value);
332 QCOMPARE( d, result );
333}
334
335void tst_QGuiVariant::toFont_data()
336{
337 QTest::addColumn<QVariant>(name: "value");
338 QTest::addColumn<QFont>(name: "result");
339
340 QFont f("times",12,-1,false);
341 QTest::newRow( dataTag: "string" ) << QVariant( QString( "times,12,-1,5,50,0,0,0,0,0" ) ) << f;
342}
343
344void tst_QGuiVariant::toFont()
345{
346 QFETCH( QVariant, value );
347 QFETCH( QFont, result );
348 QVERIFY( value.isValid() );
349 QVERIFY( value.canConvert( QVariant::Font ) );
350 QFont d = qvariant_cast<QFont>(v: value);
351 QCOMPARE( d, result );
352}
353
354void tst_QGuiVariant::toKeySequence_data()
355{
356 QTest::addColumn<QVariant>(name: "value");
357 QTest::addColumn<QKeySequence>(name: "result");
358
359
360 QTest::newRow( dataTag: "int" ) << QVariant( 67108929 ) << QKeySequence( Qt::CTRL + Qt::Key_A );
361
362
363 QTest::newRow( dataTag: "qstring" )
364 << QVariant( QString( "Ctrl+A" ) )
365 << QKeySequence( Qt::CTRL + Qt::Key_A );
366}
367
368void tst_QGuiVariant::toKeySequence()
369{
370 QFETCH( QVariant, value );
371 QFETCH( QKeySequence, result );
372 QVERIFY( value.isValid() );
373 QVERIFY( value.canConvert( QVariant::KeySequence ) );
374 QKeySequence d = qvariant_cast<QKeySequence>(v: value);
375 QCOMPARE( d, result );
376}
377
378void tst_QGuiVariant::toString_data()
379{
380 QTest::addColumn<QVariant>(name: "value");
381 QTest::addColumn<QString>(name: "result");
382
383 QTest::newRow( dataTag: "qkeysequence" ) << QVariant::fromValue( value: QKeySequence( Qt::CTRL + Qt::Key_A ) )
384#ifndef Q_OS_MAC
385 << QString( "Ctrl+A" );
386#else
387 << QString(QChar(0x2318)) + QLatin1Char('A');
388#endif
389
390 QFont font( "times", 12 );
391 QTest::newRow( dataTag: "qfont" ) << QVariant::fromValue( value: font ) << QString("times,12,-1,5,50,0,0,0,0,0");
392 QTest::newRow( dataTag: "qcolor" ) << QVariant::fromValue( value: QColor( 10, 10, 10 ) ) << QString( "#0a0a0a" );
393}
394
395void tst_QGuiVariant::toString()
396{
397 QFETCH( QVariant, value );
398 QFETCH( QString, result );
399 QVERIFY( value.isValid() );
400 QVERIFY( value.canConvert( QVariant::String ) );
401 QString str = value.toString();
402 QCOMPARE( str, result );
403}
404
405#if QT_DEPRECATED_SINCE(5, 15)
406QT_WARNING_PUSH
407QT_WARNING_DISABLE_DEPRECATED
408void tst_QGuiVariant::matrix()
409{
410 QVariant variant;
411 QMatrix matrix = qvariant_cast<QMatrix>(v: variant);
412 QVERIFY(matrix.isIdentity());
413 variant.setValue(QMatrix().rotate(a: 90));
414 QCOMPARE(QMatrix().rotate(90), qvariant_cast<QMatrix>(variant));
415
416 void *mmatrix = QMetaType::create(type: QVariant::Matrix, copy: 0);
417 QVERIFY(mmatrix);
418 QMetaType::destroy(type: QVariant::Matrix, data: mmatrix);
419}
420QT_WARNING_POP
421#endif
422
423void tst_QGuiVariant::matrix4x4()
424{
425 QVariant variant;
426 QMatrix4x4 matrix = qvariant_cast<QMatrix4x4>(v: variant);
427 QVERIFY(matrix.isIdentity());
428 QMatrix4x4 m;
429 m.scale(factor: 2.0f);
430 variant.setValue(m);
431 QCOMPARE(m, qvariant_cast<QMatrix4x4>(variant));
432
433 void *mmatrix = QMetaType::create(type: QVariant::Matrix4x4, copy: 0);
434 QVERIFY(mmatrix);
435 QMetaType::destroy(type: QVariant::Matrix4x4, data: mmatrix);
436}
437
438void tst_QGuiVariant::transform()
439{
440 QVariant variant;
441 QTransform matrix = qvariant_cast<QTransform>(v: variant);
442 QVERIFY(matrix.isIdentity());
443 variant.setValue(QTransform().rotate(a: 90));
444 QCOMPARE(QTransform().rotate(90), qvariant_cast<QTransform>(variant));
445
446 void *mmatrix = QMetaType::create(type: QVariant::Transform, copy: 0);
447 QVERIFY(mmatrix);
448 QMetaType::destroy(type: QVariant::Transform, data: mmatrix);
449}
450
451
452void tst_QGuiVariant::vector2D()
453{
454 QVariant variant;
455 QVector2D vector = qvariant_cast<QVector2D>(v: variant);
456 QVERIFY(vector.isNull());
457 variant.setValue(QVector2D(0.1f, 0.2f));
458 QCOMPARE(QVector2D(0.1f, 0.2f), qvariant_cast<QVector2D>(variant));
459
460 void *pvector = QMetaType::create(type: QVariant::Vector2D, copy: 0);
461 QVERIFY(pvector);
462 QMetaType::destroy(type: QVariant::Vector2D, data: pvector);
463}
464
465void tst_QGuiVariant::vector3D()
466{
467 QVariant variant;
468 QVector3D vector = qvariant_cast<QVector3D>(v: variant);
469 QVERIFY(vector.isNull());
470 variant.setValue(QVector3D(0.1f, 0.2f, 0.3f));
471 QCOMPARE(QVector3D(0.1f, 0.2f, 0.3f), qvariant_cast<QVector3D>(variant));
472
473 void *pvector = QMetaType::create(type: QVariant::Vector3D, copy: 0);
474 QVERIFY(pvector);
475 QMetaType::destroy(type: QVariant::Vector3D, data: pvector);
476}
477
478void tst_QGuiVariant::vector4D()
479{
480 QVariant variant;
481 QVector4D vector = qvariant_cast<QVector4D>(v: variant);
482 QVERIFY(vector.isNull());
483 variant.setValue(QVector4D(0.1f, 0.2f, 0.3f, 0.4f));
484 QCOMPARE(QVector4D(0.1f, 0.2f, 0.3f, 0.4f), qvariant_cast<QVector4D>(variant));
485
486 void *pvector = QMetaType::create(type: QVariant::Vector4D, copy: 0);
487 QVERIFY(pvector);
488 QMetaType::destroy(type: QVariant::Vector4D, data: pvector);
489}
490
491void tst_QGuiVariant::quaternion()
492{
493 QVariant variant;
494 QQuaternion quaternion = qvariant_cast<QQuaternion>(v: variant);
495 QVERIFY(quaternion.isIdentity());
496 variant.setValue(QQuaternion(0.1f, 0.2f, 0.3f, 0.4f));
497 QCOMPARE(QQuaternion(0.1f, 0.2f, 0.3f, 0.4f), qvariant_cast<QQuaternion>(variant));
498
499 void *pquaternion = QMetaType::create(type: QVariant::Quaternion, copy: 0);
500 QVERIFY(pquaternion);
501 QMetaType::destroy(type: QVariant::Quaternion, data: pquaternion);
502}
503
504void tst_QGuiVariant::writeToReadFromDataStream_data()
505{
506 QTest::addColumn<QVariant>(name: "writeVariant");
507 QTest::addColumn<bool>(name: "isNull");
508
509 QTest::newRow( dataTag: "bitmap_invalid" ) << QVariant::fromValue( value: QBitmap() ) << true;
510 QBitmap bitmap( 10, 10 );
511 bitmap.fill( fillColor: Qt::red );
512 QTest::newRow( dataTag: "bitmap_valid" ) << QVariant::fromValue( value: bitmap ) << false;
513 QTest::newRow( dataTag: "brush_valid" ) << QVariant::fromValue( value: QBrush( Qt::red ) ) << false;
514 QTest::newRow( dataTag: "color_valid" ) << QVariant::fromValue( value: QColor( Qt::red ) ) << false;
515#ifndef QT_NO_CURSOR
516 QTest::newRow( dataTag: "cursor_valid" ) << QVariant::fromValue( value: QCursor( Qt::PointingHandCursor ) ) << false;
517#endif
518 QTest::newRow( dataTag: "font_valid" ) << QVariant::fromValue( value: QFont( "times", 12 ) ) << false;
519 QTest::newRow( dataTag: "pixmap_invalid" ) << QVariant::fromValue( value: QPixmap() ) << true;
520 QPixmap pixmap( 10, 10 );
521 pixmap.fill( fillColor: Qt::red );
522 QTest::newRow( dataTag: "pixmap_valid" ) << QVariant::fromValue( value: pixmap ) << false;
523 QTest::newRow( dataTag: "image_invalid" ) << QVariant::fromValue( value: QImage() ) << true;
524 QTest::newRow( dataTag: "keysequence_valid" ) << QVariant::fromValue( value: QKeySequence( Qt::CTRL + Qt::Key_A ) ) << false;
525 QTest::newRow( dataTag: "palette_valid" ) << QVariant::fromValue(value: QPalette(QColor("turquoise"))) << false;
526 QTest::newRow( dataTag: "pen_valid" ) << QVariant::fromValue( value: QPen( Qt::red ) ) << false;
527 QTest::newRow( dataTag: "pointarray_invalid" ) << QVariant::fromValue( value: QPolygon() ) << true;
528 QTest::newRow( dataTag: "pointarray_valid" ) << QVariant::fromValue( value: QPolygon( QRect( 10, 10, 20, 20 ) ) ) << false;
529 QTest::newRow( dataTag: "region_invalid" ) << QVariant::fromValue( value: QRegion() ) << true;
530 QTest::newRow( dataTag: "region_valid" ) << QVariant::fromValue( value: QRegion( 10, 10, 20, 20 ) ) << false;
531 QTest::newRow(dataTag: "polygonf_invalid") << QVariant::fromValue(value: QPolygonF()) << true;
532 QTest::newRow(dataTag: "polygonf_valid") << QVariant::fromValue(value: QPolygonF(QRectF(10, 10, 20, 20))) << false;
533}
534
535void tst_QGuiVariant::invalidQColor()
536{
537 QVariant va("An invalid QColor::name() value.");
538 QVERIFY(va.canConvert(QVariant::Color));
539
540 QVERIFY(!va.convert(QVariant::Color));
541
542 QVERIFY(!qvariant_cast<QColor>(va).isValid());
543}
544
545void tst_QGuiVariant::validQColor()
546{
547 QColor col(Qt::red);
548 QVariant va(col.name());
549 QVERIFY(va.canConvert(QVariant::Color));
550
551 QVERIFY(va.convert(QVariant::Color));
552
553 QVERIFY(col.isValid());
554
555 QVERIFY(va.convert(QVariant::String));
556
557 QCOMPARE(qvariant_cast<QString>(va), col.name());
558}
559
560void tst_QGuiVariant::colorInteger()
561{
562 QVariant v = QColor(Qt::red);
563 QCOMPARE(v.type(), QVariant::Color);
564 QCOMPARE(v.value<QColor>(), QColor(Qt::red));
565
566 v.setValue(1000);
567 QCOMPARE(v.type(), QVariant::Int);
568 QCOMPARE(v.toInt(), 1000);
569
570 v.setValue(QColor(Qt::yellow));
571 QCOMPARE(v.type(), QVariant::Color);
572 QCOMPARE(v.value<QColor>(), QColor(Qt::yellow));
573}
574
575void tst_QGuiVariant::writeToReadFromDataStream()
576{
577 QFETCH( QVariant, writeVariant );
578 QFETCH( bool, isNull );
579 QByteArray data;
580
581 QDataStream writeStream( &data, QIODevice::WriteOnly );
582 writeStream << writeVariant;
583
584 QVariant readVariant;
585 QDataStream readStream( &data, QIODevice::ReadOnly );
586 readStream >> readVariant;
587 QVERIFY( readVariant.isNull() == isNull );
588 // Best way to confirm the readVariant contains the same data?
589 // Since only a few won't match since the serial numbers are different
590 // I won't bother adding another bool in the data test.
591 const int writeType = writeVariant.userType();
592 if ( writeType != QVariant::Invalid && writeType != QVariant::Bitmap && writeType != QVariant::Pixmap
593 && writeType != QVariant::Image) {
594 switch (writeType) {
595 default:
596 QCOMPARE( readVariant, writeVariant );
597 break;
598
599 // compare types know by QMetaType but not QVariant (QVariant::operator==() knows nothing about them)
600 case QMetaType::Long:
601 QCOMPARE(qvariant_cast<long>(readVariant), qvariant_cast<long>(writeVariant));
602 break;
603 case QMetaType::ULong:
604 QCOMPARE(qvariant_cast<ulong>(readVariant), qvariant_cast<ulong>(writeVariant));
605 break;
606 case QMetaType::Short:
607 QCOMPARE(qvariant_cast<short>(readVariant), qvariant_cast<short>(writeVariant));
608 break;
609 case QMetaType::UShort:
610 QCOMPARE(qvariant_cast<ushort>(readVariant), qvariant_cast<ushort>(writeVariant));
611 break;
612 case QMetaType::Char:
613 QCOMPARE(qvariant_cast<char>(readVariant), qvariant_cast<char>(writeVariant));
614 break;
615 case QMetaType::UChar:
616 QCOMPARE(qvariant_cast<uchar>(readVariant), qvariant_cast<uchar>(writeVariant));
617 break;
618 case QMetaType::Float:
619 {
620 // the uninitialized float can be NaN (observed on Windows Mobile 5 ARMv4i)
621 float readFloat = qvariant_cast<float>(v: readVariant);
622 float writtenFloat = qvariant_cast<float>(v: writeVariant);
623 QCOMPARE(qIsNaN(readFloat), qIsNaN(writtenFloat));
624 if (!qIsNaN(f: readFloat))
625 QCOMPARE(readFloat, writtenFloat);
626 }
627 break;
628 }
629 }
630}
631
632void tst_QGuiVariant::writeToReadFromOldDataStream()
633{
634 QPolygonF polyF(QRectF(10, 10, 50, 50));
635 QVariant testVariant(polyF);
636 {
637 // Read into a variant and compare
638 QFile file(":/data/qpolygonf.bin");
639 QVERIFY(file.open(QIODevice::ReadOnly));
640 QDataStream dataFileStream(&file);
641 dataFileStream.setVersion(QDataStream::Qt_4_9);
642 QVariant readVariant;
643 dataFileStream >> readVariant;
644 QCOMPARE(readVariant.userType(), int(QMetaType::QPolygonF));
645 QCOMPARE(testVariant, readVariant);
646 file.close();
647 }
648 {
649 QByteArray variantData;
650 {
651 QDataStream varDataStream(&variantData, QIODevice::WriteOnly);
652 varDataStream << testVariant;
653 }
654 // Read into a bytearray and compare
655 QFile file(":/data/qpolygonf.bin");
656 QVERIFY(file.open(QIODevice::ReadOnly));
657 QDataStream dataFileStream(&file);
658 dataFileStream.setVersion(QDataStream::Qt_4_9);
659 int dummy;
660 dataFileStream >> dummy;
661 QByteArray polyData49;
662 dataFileStream >> polyData49;
663 file.close();
664 QByteArray polyData50;
665 QDataStream readVarData(variantData);
666 readVarData >> dummy;
667 readVarData >> polyData50;
668 QCOMPARE(polyData49, polyData50);
669 }
670}
671
672void tst_QGuiVariant::debugStream_data()
673{
674 QTest::addColumn<QVariant>(name: "variant");
675 QTest::addColumn<int>(name: "typeId");
676 for (int id = QMetaType::FirstGuiType; id <= QMetaType::LastGuiType; ++id) {
677 const char *tagName = QMetaType::typeName(type: id);
678 if (!tagName)
679 continue;
680 QTest::newRow(dataTag: tagName) << QVariant(static_cast<QVariant::Type>(id)) << id;
681 }
682}
683
684void tst_QGuiVariant::debugStream()
685{
686 QFETCH(QVariant, variant);
687 QFETCH(int, typeId);
688
689 MessageHandler msgHandler(typeId);
690 qDebug() << variant;
691 QVERIFY(msgHandler.testPassed());
692}
693
694void tst_QGuiVariant::implicitConstruction()
695{
696 // This is a compile-time test
697 QVariant v;
698
699#define FOR_EACH_GUI_CLASS_BASE(F) \
700 F(Font) \
701 F(Pixmap) \
702 F(Brush) \
703 F(Color) \
704 F(Palette) \
705 F(Icon) \
706 F(Image) \
707 F(Polygon) \
708 F(Region) \
709 F(Bitmap) \
710 F(KeySequence) \
711 F(Pen) \
712 F(TextLength) \
713 F(TextFormat) \
714 F(Matrix) \
715 F(Transform) \
716 F(Matrix4x4) \
717 F(Vector2D) \
718 F(Vector3D) \
719 F(Vector4D) \
720 F(Quaternion) \
721 F(PolygonF)
722
723#ifndef QT_NO_CURSOR
724# define FOR_EACH_GUI_CLASS(F) \
725 FOR_EACH_GUI_CLASS_BASE(F) \
726 F(Cursor)
727#else // !QT_NO_CURSOR
728# define FOR_EACH_GUI_CLASS(F) \
729 FOR_EACH_GUI_CLASS_BASE(F)
730#endif // QT_NO_CURSOR
731
732#define CONSTRUCT(TYPE) \
733 { \
734 Q##TYPE t; \
735 v = t; \
736 QVERIFY(true); \
737 }
738
739 FOR_EACH_GUI_CLASS(CONSTRUCT)
740
741#undef CONSTRUCT
742#undef FOR_EACH_GUI_CLASS
743}
744
745void tst_QGuiVariant::guiVariantAtExit()
746{
747 // crash test, it should not crash at QGuiApplication exit
748#ifndef QT_NO_CURSOR
749 static QVariant cursor = QCursor();
750#endif
751 static QVariant point = QPoint();
752 static QVariant icon = QIcon();
753 static QVariant image = QImage();
754 static QVariant palette = QPalette();
755#ifndef QT_NO_CURSOR
756 Q_UNUSED(cursor);
757#endif
758 Q_UNUSED(point);
759 Q_UNUSED(icon);
760 Q_UNUSED(image);
761 Q_UNUSED(palette);
762 QVERIFY(true);
763}
764
765void tst_QGuiVariant::iconEquality()
766{
767 QIcon i;
768 QVariant a = i;
769 QVariant b = i;
770 QCOMPARE(a, b);
771
772 i = QIcon(":/black.png");
773 a = i;
774 QVERIFY(a != b);
775
776 b = a;
777 QCOMPARE(a, b);
778
779 i = QIcon(":/black2.png");
780 a = i;
781 QVERIFY(a != b);
782
783 b = i;
784 QCOMPARE(a, b);
785
786 // This is a "different" QIcon
787 // even if the contents are the same
788 b = QIcon(":/black2.png");
789 QVERIFY(a != b);
790}
791
792void tst_QGuiVariant::qt4QPolygonFDataStream()
793{
794 qRegisterMetaTypeStreamOperators<QPolygonF>();
795
796 QByteArray data;
797 QDataStream stream(&data, QIODevice::WriteOnly);
798 stream.setVersion(QDataStream::Qt_4_8);
799 QPolygonF polygon;
800 polygon.append(t: QPointF(2, 3));
801 stream << QVariant::fromValue(value: polygon);
802 const QByteArray qt4Data = QByteArray::fromHex(hexEncoded: "0000007f000000000a51506f6c79676f6e46000000000140000000000000004008000000000000");
803 QCOMPARE(data, qt4Data);
804
805 QDataStream input(&data, QIODevice::ReadOnly);
806 input.setVersion(QDataStream::Qt_4_8);
807 QVariant result;
808 input >> result;
809 QCOMPARE(result.value<QPolygonF>(), polygon);
810}
811
812QTEST_MAIN(tst_QGuiVariant)
813#include "tst_qguivariant.moc"
814

source code of qtbase/tests/auto/gui/kernel/qguivariant/test/tst_qguivariant.cpp