1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #include <QtTest/QtTest>
45 #include <qbitarray.h>
46 #include <qhostaddress.h>
47 #include <qdatetime.h>
49 #include <qiodevice.h>
52 #include <qkeysequence.h>
58 #include <qmatrix4x4.h>
62 #include <qtransform.h>
63 #include <qvector2d.h>
64 #include <qvector3d.h>
65 #include <qvector4d.h>
66 #include <qquaternion.h>
71 Q_DECLARE_METATYPE(qlonglong)
72 Q_DECLARE_METATYPE(qulonglong)
73 Q_DECLARE_METATYPE(QPointF)
74 Q_DECLARE_METATYPE(QRectF)
75 Q_DECLARE_METATYPE(QSize)
76 Q_DECLARE_METATYPE(QSizeF)
77 Q_DECLARE_METATYPE(QLine)
78 Q_DECLARE_METATYPE(QLineF)
79 Q_DECLARE_METATYPE(QPoint)
80 Q_DECLARE_METATYPE(QRect)
81 Q_DECLARE_METATYPE(QPixmap)
82 Q_DECLARE_METATYPE(QBrush)
83 Q_DECLARE_METATYPE(QFont)
84 Q_DECLARE_METATYPE(QColor)
85 Q_DECLARE_METATYPE(QKeySequence)
87 class CustomNonQObject;
89 class tst_QVariant : public QObject
94 tst_QVariant(QObject *parent = 0)
95 : QObject(parent), customNonQObjectPointer(0)
101 void cleanupTestCase();
104 void copy_constructor();
105 void constructor_invalid_data();
106 void constructor_invalid();
110 void canConvert_data();
143 void toLongLong_data();
146 void toULongLong_data();
149 void toByteArray_data();
152 void toString_data();
161 void toDateTime_data();
164 void toDouble_data();
167 void toPointF_data();
173 void toKeySequence_data();
174 void toKeySequence();
182 void toPixmap_data();
191 void qvariant_cast_QObject_data();
192 void qvariant_cast_QObject();
193 void qvariant_cast_QObject_derived();
198 void toRegularExpression();
213 void basicUserType();
217 void writeToReadFromDataStream_data();
218 void writeToReadFromDataStream();
219 void writeToReadFromOldDataStream();
220 void checkDataStream();
222 void operator_eq_eq_data();
223 void operator_eq_eq();
225 void operator_eq_eq_rhs();
227 void typeName_data();
231 void streamInvalidVariant();
238 void saveLoadCustomTypes();
243 void convertToQUint8() const;
244 void invalidQColor() const;
245 void comparePointers() const;
246 void voidStar() const;
247 void dataStar() const;
248 void canConvertQStringList() const;
249 void canConvertQStringList_data() const;
250 void canConvertMetaTypeToInt() const;
251 void variantToDateTimeWithoutWarnings() const;
252 void invalidDateTime() const;
254 void loadUnknownUserType();
255 void loadBrokenUserType();
257 void invalidDate() const;
258 void compareCustomTypes() const;
259 void timeToDateTime() const;
260 void copyingUserTypes() const;
261 void convertBoolToByteArray() const;
262 void convertBoolToByteArray_data() const;
263 void convertByteArrayToBool() const;
264 void convertByteArrayToBool_data() const;
265 void toIntFromQString() const;
266 void toIntFromDouble() const;
269 void numericalConvert();
270 void moreCustomTypes();
271 void movabilityTest();
272 void variantInVariant();
276 void forwardDeclare();
277 void debugStream_data();
279 void debugStreamType_data();
280 void debugStreamType();
282 void loadQt4Stream_data();
283 void loadQt4Stream();
284 void saveQt4Stream_data();
285 void saveQt4Stream();
286 void loadQt5Stream_data();
287 void loadQt5Stream();
288 void saveQt5Stream_data();
289 void saveQt5Stream();
291 void guiVariantAtExit();
292 void widgetsVariantAtExit();
294 void dataStream_data(QDataStream::Version version);
295 void loadQVariantFromDataStream(QDataStream::Version version);
296 void saveQVariantFromDataStream(QDataStream::Version version);
298 CustomNonQObject *customNonQObjectPointer;
299 QVector<QObject*> objectPointerTestData;
302 Q_DECLARE_METATYPE(QDate)
303 Q_DECLARE_METATYPE(QTime)
304 Q_DECLARE_METATYPE(QDateTime)
305 Q_DECLARE_METATYPE(QVariant)
307 const qlonglong intMax1 = (qlonglong)INT_MAX + 1;
308 const qulonglong uintMax1 = (qulonglong)UINT_MAX + 1;
310 void tst_QVariant::constructor()
313 QVERIFY( !variant.isValid() );
314 QVERIFY( variant.isNull() );
316 QVariant var2(variant);
317 QVERIFY( !var2.isValid() );
318 QVERIFY( variant.isNull() );
320 QVariant varll(intMax1);
321 QVariant varll2(varll);
322 QCOMPARE(varll2, varll);
324 QVariant var3(QVariant::String);
325 QCOMPARE(var3.typeName(), "QString");
326 QVERIFY(var3.isNull());
327 QVERIFY(var3.isValid());
329 QVariant var4(QVariant::Invalid);
330 QCOMPARE(var4.type(), QVariant::Invalid);
331 QVERIFY(var4.isNull());
332 QVERIFY(!var4.isValid());
334 QVariant var5(QLatin1String("hallo"));
335 QCOMPARE(var5.type(), QVariant::String);
336 QCOMPARE(var5.typeName(), "QString");
338 QVariant var6(qlonglong(0));
339 QCOMPARE(var6.type(), QVariant::LongLong);
340 QCOMPARE(var6.typeName(), "qlonglong");
343 QVERIFY(var7.isValid());
344 QVERIFY(!var7.isNull());
346 var8.setValue<int>(5);
347 QVERIFY(var8.isValid());
348 QVERIFY(!var8.isNull());
351 void tst_QVariant::constructor_invalid_data()
353 QTest::addColumn<uint>("typeId");
355 QTest::newRow("-1") << uint(-1);
356 QTest::newRow("-122234567") << uint(-122234567);
357 QTest::newRow("0xfffffffff") << uint(0xfffffffff);
358 QTest::newRow("LastCoreType + 1") << uint(QMetaType::LastCoreType + 1);
359 QVERIFY(!QMetaType::isRegistered(QMetaType::LastCoreType + 1));
360 QTest::newRow("LastGuiType + 1") << uint(QMetaType::LastGuiType + 1);
361 QVERIFY(!QMetaType::isRegistered(QMetaType::LastGuiType + 1));
362 QTest::newRow("LastWidgetsType + 1") << uint(QMetaType::LastWidgetsType + 1);
363 QVERIFY(!QMetaType::isRegistered(QMetaType::LastWidgetsType + 1));
366 struct MessageHandlerInvalidType
368 MessageHandlerInvalidType()
369 : oldMsgHandler(qInstallMsgHandler(handler))
374 ~MessageHandlerInvalidType()
376 qInstallMsgHandler(oldMsgHandler);
379 QtMsgHandler oldMsgHandler;
381 static void handler(QtMsgType type, const char *txt)
384 QString msg = QString::fromLatin1(txt);
385 // uint(-1) can be platform dependent so we check only beginning of the message.
386 ok = msg.startsWith("Trying to construct an instance of an invalid type, type id:");
387 QVERIFY2(ok, (QString::fromLatin1("Message is not started correctly: '") + msg + '\'').toLatin1().constData());
391 bool MessageHandlerInvalidType::ok;
393 void tst_QVariant::constructor_invalid()
396 QFETCH(uint, typeId);
398 MessageHandlerInvalidType msg;
399 QVariant variant(static_cast<QVariant::Type>(typeId));
400 QVERIFY(!variant.isValid());
401 QVERIFY(variant.userType() == QMetaType::UnknownType);
405 MessageHandlerInvalidType msg;
406 QVariant variant(typeId, /* copy */ 0);
407 QVERIFY(!variant.isValid());
408 QVERIFY(variant.userType() == QMetaType::UnknownType);
413 void tst_QVariant::copy_constructor()
415 QVariant var7(QVariant::Int);
417 QCOMPARE(var8.type(), QVariant::Int);
418 QVERIFY(var8.isNull());
421 void tst_QVariant::isNull()
424 QVERIFY( var.isNull() );
427 QVariant var1( str1 );
428 QVERIFY( var1.isNull() );
430 QVariant var2( QString::null );
431 QVERIFY( var2.isNull() );
433 QVariant var3( QString( "blah" ) );
434 QVERIFY( !var3.isNull() );
437 QVERIFY( !var4.isNull() );
439 QVariant var5 = QString();
440 QVERIFY( var5.isNull() );
442 QVariant var6( QString( "blah" ) );
443 QVERIFY( !var6.isNull() );
445 QVERIFY( var6.isNull() );
446 var6.convert( QVariant::String );
447 QVERIFY( var6.isNull() );
448 QVariant varLL( (qlonglong)0 );
449 QVERIFY( !varLL.isNull() );
450 QVariant var7(QString::null);
451 QVERIFY(var7.isNull());
454 void tst_QVariant::swap()
456 QVariant v1 = 1, v2 = 2.0;
458 QCOMPARE(v1.type(),QVariant::Double);
459 QCOMPARE(v1.toDouble(),2.0);
460 QCOMPARE(v2.type(),QVariant::Int);
461 QCOMPARE(v2.toInt(),1);
464 void tst_QVariant::canConvert_data()
466 QTest::addColumn<QVariant>("val");
467 QTest::addColumn<bool>("BitArrayCast");
468 QTest::addColumn<bool>("BitmapCast");
469 QTest::addColumn<bool>("BoolCast");
470 QTest::addColumn<bool>("BrushCast");
471 QTest::addColumn<bool>("ByteArrayCast");
472 QTest::addColumn<bool>("ColorCast");
473 QTest::addColumn<bool>("CursorCast");
474 QTest::addColumn<bool>("DateCast");
475 QTest::addColumn<bool>("DateTimeCast");
476 QTest::addColumn<bool>("DoubleCast");
477 QTest::addColumn<bool>("FontCast");
478 QTest::addColumn<bool>("ImageCast");
479 QTest::addColumn<bool>("IntCast");
480 QTest::addColumn<bool>("InvalidCast");
481 QTest::addColumn<bool>("KeySequenceCast");
482 QTest::addColumn<bool>("ListCast");
483 QTest::addColumn<bool>("LongLongCast");
484 QTest::addColumn<bool>("MapCast");
485 QTest::addColumn<bool>("PaletteCast");
486 QTest::addColumn<bool>("PenCast");
487 QTest::addColumn<bool>("PixmapCast");
488 QTest::addColumn<bool>("PointCast");
489 QTest::addColumn<bool>("RectCast");
490 QTest::addColumn<bool>("RegionCast");
491 QTest::addColumn<bool>("SizeCast");
492 QTest::addColumn<bool>("SizePolicyCast");
493 QTest::addColumn<bool>("StringCast");
494 QTest::addColumn<bool>("StringListCast");
495 QTest::addColumn<bool>("TimeCast");
496 QTest::addColumn<bool>("UIntCast");
497 QTest::addColumn<bool>("ULongLongCast");
508 // 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
511 QVariant var(QBitArray(0));
512 QTest::newRow("BitArray")
513 << var << 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 << N << N << N << N << N << N;
514 var = QVariant::fromValue(QBitmap());
515 QTest::newRow("Bitmap")
516 << 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;
517 var = QVariant::fromValue(QBrush());
518 QTest::newRow("Brush")
519 << 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;
520 var = QVariant(QByteArray());
521 QTest::newRow("ByteArray")
522 << var << N << N << Y << N << Y << Y << N << N << N << Y << N << N << Y << N << N << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y;
523 var = QVariant::fromValue(QColor());
524 QTest::newRow("Color")
525 << 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;
527 var = QVariant::fromValue(QCursor());
528 QTest::newRow("Cursor")
529 << 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;
531 var = QVariant(QDate());
532 QTest::newRow("Date")
533 << var << N << N << N << N << N << N << N << Y << Y << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N;
534 var = QVariant(QDateTime());
535 QTest::newRow("DateTime")
536 << var << N << N << N << N << N << N << N << Y << Y << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << Y << N << N;
537 var = QVariant((double)0.1);
538 QTest::newRow("Double")
539 << var << N << N << Y << N << Y << N << N << N << N << Y << N << N << Y << N << N << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y;
540 var = QVariant(0.1f);
541 QTest::newRow("Float")
542 << var << N << N << Y << N << Y << N << N << N << N << Y << N << N << Y << N << N << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y;
543 var = QVariant::fromValue(QFont());
544 QTest::newRow("Font")
545 << 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;
546 var = QVariant::fromValue(QIcon());
547 QTest::newRow("Icon")
548 << 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;
549 var = QVariant::fromValue(QImage());
550 QTest::newRow("Image")
551 << 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;
552 var = QVariant((int)1);
554 << var << N << N << Y << N << Y << N << N << N << N << Y << N << N << Y << N << Y << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y;
556 QTest::newRow("Invalid")
557 << var << 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 << N << N << N << N << N;
558 var = QVariant::fromValue(QKeySequence());
559 QTest::newRow("KeySequence")
560 << 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;
561 var = QVariant(QList<QVariant>());
562 QTest::newRow("List")
563 << var << 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 << Y << N << N << N;
564 var = QVariant((qlonglong)1);
565 QTest::newRow("LongLong")
566 << var << N << N << Y << N << Y << N << N << N << N << Y << N << N << Y << N << N << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y;
567 var = QVariant(QMap<QString,QVariant>());
569 << var << 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 << N;
570 var = QVariant::fromValue(QPalette());
571 QTest::newRow("Palette")
572 << 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;
573 var = QVariant::fromValue(QPen());
575 << 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;
576 var = QVariant::fromValue(QPixmap());
577 QTest::newRow("Pixmap")
578 << 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;
579 var = QVariant::fromValue(QPolygon());
580 QTest::newRow("PointArray")
581 << 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;
582 var = QVariant(QPoint());
583 QTest::newRow("Point")
584 << var << 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 << N << N;
585 var = QVariant(QRect());
586 QTest::newRow("Rect")
587 << var << 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 << N;
588 var = QVariant::fromValue(QRegion());
589 QTest::newRow("Region")
590 << 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;
591 var = QVariant(QSize());
592 QTest::newRow("Size")
593 << 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 << Y << N << N << N << N << N << N;
594 var = QVariant::fromValue(QSizePolicy());
595 QTest::newRow("SizePolicy")
596 << 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 << Y << N << N << N << N << N;
597 var = QVariant(QString());
598 QTest::newRow("String")
599 << var << N << N << Y << N << Y << Y << N << Y << Y << Y << Y << N << Y << N << Y << N << Y << N << N << N << N << N << N << N << N << N << Y << Y << Y << Y << Y;
600 var = QVariant(QStringList("entry"));
601 QTest::newRow("StringList")
602 << var << 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 << Y << Y << N << N << N;
603 var = QVariant(QTime());
604 QTest::newRow("Time")
605 << 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 << Y << N << Y << N << N;
606 var = QVariant((uint)1);
607 QTest::newRow("UInt")
608 << var << N << N << Y << N << Y << N << N << N << N << Y << N << N << Y << N << N << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y;
609 var = QVariant((int)1);
611 << var << N << N << Y << N << Y << N << N << N << N << Y << N << N << Y << N << Y << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y;
612 var = QVariant((qulonglong)1);
613 QTest::newRow("ULongLong")
614 << var << N << N << Y << N << Y << N << N << N << N << Y << N << N << Y << N << N << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y;
615 var = QVariant::fromValue('a');
616 QTest::newRow("Char")
617 << var << N << N << Y << N << Y << N << N << N << N << Y << N << N << Y << N << N << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y;
618 var = QVariant::fromValue<signed char>(-1);
619 QTest::newRow("SChar")
620 << var << N << N << Y << N << Y << N << N << N << N << Y << N << N << Y << N << N << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y;
626 void tst_QVariant::canConvert()
628 QFETCH(QVariant, val);
629 QFETCH(bool, BitArrayCast);
630 QFETCH(bool, BitmapCast);
631 QFETCH(bool, BoolCast);
632 QFETCH(bool, BrushCast);
633 QFETCH(bool, ByteArrayCast);
634 QFETCH(bool, ColorCast);
635 QFETCH(bool, CursorCast);
636 QFETCH(bool, DateCast);
637 QFETCH(bool, DateTimeCast);
638 QFETCH(bool, DoubleCast);
639 QFETCH(bool, FontCast);
640 QFETCH(bool, ImageCast);
641 QFETCH(bool, IntCast);
642 QFETCH(bool, InvalidCast);
643 QFETCH(bool, KeySequenceCast);
644 QFETCH(bool, ListCast);
645 QFETCH(bool, LongLongCast);
646 QFETCH(bool, MapCast);
647 QFETCH(bool, PaletteCast);
648 QFETCH(bool, PenCast);
649 QFETCH(bool, PixmapCast);
650 QFETCH(bool, PointCast);
651 QFETCH(bool, RectCast);
652 QFETCH(bool, RegionCast);
653 QFETCH(bool, SizeCast);
654 QFETCH(bool, SizePolicyCast);
655 QFETCH(bool, StringCast);
656 QFETCH(bool, StringListCast);
657 QFETCH(bool, TimeCast);
658 QFETCH(bool, UIntCast);
659 QFETCH(bool, ULongLongCast);
661 QCOMPARE(val.canConvert(QVariant::BitArray), BitArrayCast);
662 QCOMPARE(val.canConvert(QVariant::Bitmap), BitmapCast);
663 QCOMPARE(val.canConvert(QVariant::Bool), BoolCast);
664 QCOMPARE(val.canConvert(QVariant::Brush), BrushCast);
665 QCOMPARE(val.canConvert(QVariant::ByteArray), ByteArrayCast);
666 QCOMPARE(val.canConvert(QVariant::Color), ColorCast);
667 QCOMPARE(val.canConvert(QVariant::Cursor), CursorCast);
668 QCOMPARE(val.canConvert(QVariant::Date), DateCast);
669 QCOMPARE(val.canConvert(QVariant::DateTime), DateTimeCast);
670 QCOMPARE(val.canConvert(QVariant::Double), DoubleCast);
671 QCOMPARE(val.canConvert(QVariant::Type(QMetaType::Float)), DoubleCast);
672 QCOMPARE(val.canConvert(QVariant::Font), FontCast);
673 QCOMPARE(val.canConvert(QVariant::Image), ImageCast);
674 QCOMPARE(val.canConvert(QVariant::Int), IntCast);
675 QCOMPARE(val.canConvert(QVariant::Invalid), InvalidCast);
676 QCOMPARE(val.canConvert(QVariant::KeySequence), KeySequenceCast);
677 QCOMPARE(val.canConvert(QVariant::List), ListCast);
678 QCOMPARE(val.canConvert(QVariant::LongLong), LongLongCast);
679 QCOMPARE(val.canConvert(QVariant::Map), MapCast);
680 QCOMPARE(val.canConvert(QVariant::Palette), PaletteCast);
681 QCOMPARE(val.canConvert(QVariant::Pen), PenCast);
682 QCOMPARE(val.canConvert(QVariant::Pixmap), PixmapCast);
683 QCOMPARE(val.canConvert(QVariant::Point), PointCast);
684 QCOMPARE(val.canConvert(QVariant::Rect), RectCast);
685 QCOMPARE(val.canConvert(QVariant::Region), RegionCast);
686 QCOMPARE(val.canConvert(QVariant::Size), SizeCast);
687 QCOMPARE(val.canConvert(QVariant::SizePolicy), SizePolicyCast);
688 QCOMPARE(val.canConvert(QVariant::String), StringCast);
689 QCOMPARE(val.canConvert(QVariant::StringList), StringListCast);
690 QCOMPARE(val.canConvert(QVariant::Time), TimeCast);
691 QCOMPARE(val.canConvert(QVariant::UInt), UIntCast);
692 QCOMPARE(val.canConvert(QVariant::ULongLong), ULongLongCast);
695 QCOMPARE(val.canConvert(-1), false);
696 QCOMPARE(val.canConvert(-23), false);
697 QCOMPARE(val.canConvert(-23876), false);
698 QCOMPARE(val.canConvert(23876), false);
701 void tst_QVariant::toInt_data()
703 QTest::addColumn<QVariant>("value");
704 QTest::addColumn<int>("result");
705 QTest::addColumn<bool>("valueOK");
707 QTest::newRow( "invalid" ) << QVariant() << 0 << false;
708 QTest::newRow( "int" ) << QVariant( 123 ) << 123 << true;
709 QTest::newRow( "char" ) << QVariant::fromValue('a') << int('a') << true;
710 signed char signedChar = -13;
711 QTest::newRow( "signed char" ) << QVariant::fromValue(signedChar) << -13 << true;
712 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << 3 << true;
713 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << 3 << true;
714 QTest::newRow( "uint" ) << QVariant( 123u ) << 123 << true;
715 QTest::newRow( "int-string" ) << QVariant( QString("123") ) << 123 << true;
716 QTest::newRow( "string" ) << QVariant( QString("Unicode String") ) << 0 << false;
717 QTest::newRow( "longlong0" ) << QVariant( (qlonglong)34 ) << 34 << true;
718 QTest::newRow( "longlong1" ) << QVariant( intMax1 ) << (int)INT_MIN << true;
719 QTest::newRow( "ulonglong0" ) << QVariant( (qulonglong)34 ) << 34 << true;
720 QTest::newRow( "ulonglong1" ) << QVariant( uintMax1 ) << 0 << true;
721 QTest::newRow( "signedint" ) << QVariant( -123 ) << -123 << true;
722 QTest::newRow( "signeddouble" ) << QVariant( -3.1415927 ) << -3 << true;
723 QTest::newRow( "signedfloat" ) << QVariant( -3.1415927f ) << -3 << true;
724 QTest::newRow( "signedint-string" ) << QVariant( QString("-123") ) << -123 << true;
725 QTest::newRow( "signedlonglong0" ) << QVariant( (qlonglong)-34 ) << -34 << true;
726 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << int('a') << true;
727 QTest::newRow( "keysequence" ) << QVariant::fromValue( QKeySequence( Qt::Key_A ) ) << 65 << true;
728 QByteArray bytearray(4, ' ');
733 QTest::newRow( "QByteArray1" ) << QVariant( bytearray ) << 0 << false;
738 QTest::newRow( "QByteArray2" ) << QVariant( bytearray ) << 4500 << true;
741 void tst_QVariant::toInt()
743 QFETCH( QVariant, value );
744 QFETCH( int, result );
745 QFETCH( bool, valueOK );
746 QVERIFY( value.isValid() == value.canConvert( QVariant::Int ) );
748 int i = value.toInt( &ok );
749 QCOMPARE( i, result );
750 QVERIFY( ok == valueOK );
753 void tst_QVariant::toUInt_data()
755 QTest::addColumn<QVariant>("value");
756 QTest::addColumn<uint>("result");
757 QTest::addColumn<bool>("valueOK");
759 QTest::newRow( "int" ) << QVariant( 123 ) << (uint)123 << true;
760 QTest::newRow( "char" ) << QVariant::fromValue('a') << uint('a') << true;
761 signed char signedChar = 12;
762 QTest::newRow( "signed char" ) << QVariant::fromValue(signedChar) << uint(12) << true;
763 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (uint)3 << true;
764 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (uint)3 << true;
765 QTest::newRow( "uint" ) << QVariant( 123u ) << (uint)123 << true;
766 QTest::newRow( "int-string" ) << QVariant( QString("123") ) << (uint)123 << true;
767 QTest::newRow( "string" ) << QVariant( QString("Unicode String") ) << (uint)0 << false;
768 QTest::newRow( "string2" ) << QVariant( QString("4") ) << (uint)4 << true;
769 QTest::newRow( "longlong0" ) << QVariant( (qlonglong)34 ) << (uint)34 << true;
770 QTest::newRow( "longlong1" ) << QVariant( intMax1 ) << (uint)INT_MIN << true;
771 QTest::newRow( "ulonglong0" ) << QVariant( (qulonglong)34 ) << (uint)34 << true;
772 QTest::newRow( "ulonglong1" ) << QVariant( uintMax1 ) << (uint)0 << true;
773 QTest::newRow( "negativeint" ) << QVariant( -123 ) << (uint)-123 << true;
774 QTest::newRow( "negativedouble" ) << QVariant( -3.1415927 ) << (uint)-3 << true;
775 QTest::newRow( "negativefloat" ) << QVariant( -3.1415927f ) << (uint)-3 << true;
776 QTest::newRow( "negativeint-string" ) << QVariant( QString("-123") ) << (uint)0 << false;
777 QTest::newRow( "negativelonglong0" ) << QVariant( (qlonglong)-34 ) << (uint)-34 << true;
778 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << uint('a') << true;
779 QByteArray bytearray(4, ' ');
784 QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (uint)4321 << true;
787 void tst_QVariant::toUInt()
789 QFETCH( QVariant, value );
790 QFETCH( uint, result );
791 QFETCH( bool, valueOK );
792 QVERIFY( value.isValid() );
793 QVERIFY( value.canConvert( QVariant::UInt ) );
796 uint i = value.toUInt( &ok );
797 QVERIFY( ok == valueOK );
798 QCOMPARE( i, result );
802 void tst_QVariant::toSize_data()
804 QTest::addColumn<QVariant>("value");
805 QTest::addColumn<QSize>("result");
806 QTest::newRow( "qsizef4" ) << QVariant( QSizeF(4, 2) ) << QSize(4, 2);
807 QTest::newRow( "qsizef1" ) << QVariant( QSizeF(0, 0) ) << QSize(0, 0);
808 QTest::newRow( "qsizef2" ) << QVariant( QSizeF(-5, -1) ) << QSize(-5, -1);
809 QTest::newRow( "qsizef3" ) << QVariant( QSizeF() ) << QSize();
812 void tst_QVariant::toSize()
814 QFETCH( QVariant, value );
815 QFETCH( QSize, result );
816 QVERIFY( value.isValid() );
817 QVERIFY( value.canConvert( QVariant::Size ) );
819 QSize i = value.toSize();
820 QCOMPARE( i, result );
823 void tst_QVariant::toSizeF_data()
825 QTest::addColumn<QVariant>("value");
826 QTest::addColumn<QSizeF>("result");
827 QTest::newRow( "qsize1" ) << QVariant( QSize(0, 0) ) << QSizeF(0, 0);
828 QTest::newRow( "qsize2" ) << QVariant( QSize(-5, -1) ) << QSizeF(-5, -1);
829 QTest::newRow( "qsize3" ) << QVariant( QSize() ) << QSizeF();
830 QTest::newRow( "qsize4" ) << QVariant(QSize(4,2)) << QSizeF(4,2);
833 void tst_QVariant::toSizeF()
835 QFETCH( QVariant, value );
836 QFETCH( QSizeF, result );
837 QVERIFY( value.isValid() );
838 QVERIFY( value.canConvert( QVariant::SizeF ) );
840 QSizeF i = value.toSizeF();
841 QCOMPARE( i, result );
844 void tst_QVariant::toLine_data()
846 QTest::addColumn<QVariant>("value");
847 QTest::addColumn<QLine>("result");
848 QTest::newRow( "linef1" ) << QVariant( QLineF(1, 2, 3, 4) ) << QLine(1, 2, 3, 4);
849 QTest::newRow( "linef2" ) << QVariant( QLineF(-1, -2, -3, -4) ) << QLine(-1, -2, -3, -4);
850 QTest::newRow( "linef3" ) << QVariant( QLineF(0, 0, 0, 0) ) << QLine(0, 0, 0, 0);
851 QTest::newRow( "linef4" ) << QVariant( QLineF() ) << QLine();
854 void tst_QVariant::toLine()
856 QFETCH( QVariant, value );
857 QFETCH( QLine, result );
858 QVERIFY( value.isValid() );
859 QVERIFY( value.canConvert( QVariant::Line ) );
861 QLine i = value.toLine();
862 QCOMPARE( i, result );
865 void tst_QVariant::toLineF_data()
867 QTest::addColumn<QVariant>("value");
868 QTest::addColumn<QLineF>("result");
869 QTest::newRow( "line1" ) << QVariant( QLine(-1, -2, -3, -4) ) << QLineF(-1, -2, -3, -4);
870 QTest::newRow( "line2" ) << QVariant( QLine(0, 0, 0, 0) ) << QLineF(0, 0, 0, 0);
871 QTest::newRow( "line3" ) << QVariant( QLine() ) << QLineF();
872 QTest::newRow( "line4" ) << QVariant( QLine(1, 2, 3, 4) ) << QLineF(1, 2, 3, 4);
875 void tst_QVariant::toLineF()
877 QFETCH( QVariant, value );
878 QFETCH( QLineF, result );
879 QVERIFY( value.isValid() );
880 QVERIFY( value.canConvert( QVariant::LineF ) );
882 QLineF i = value.toLineF();
883 QCOMPARE( i, result );
886 void tst_QVariant::toPoint_data()
888 QTest::addColumn<QVariant>("value");
889 QTest::addColumn<QPoint>("result");
890 QTest::newRow( "pointf1" ) << QVariant( QPointF(4, 2) ) << QPoint(4, 2);
891 QTest::newRow( "pointf2" ) << QVariant( QPointF(0, 0) ) << QPoint(0, 0);
892 QTest::newRow( "pointf3" ) << QVariant( QPointF(-4, -2) ) << QPoint(-4, -2);
893 QTest::newRow( "pointf4" ) << QVariant( QPointF() ) << QPoint();
894 QTest::newRow( "pointf5" ) << QVariant( QPointF(-4.2f, -2.3f) ) << QPoint(-4, -2);
897 void tst_QVariant::toPoint()
899 QFETCH( QVariant, value );
900 QFETCH( QPoint, result );
901 QVERIFY( value.isValid() );
902 QVERIFY( value.canConvert( QVariant::Point ) );
903 QPoint i = value.toPoint();
904 QCOMPARE( i, result );
907 void tst_QVariant::toRect_data()
909 QTest::addColumn<QVariant>("value");
910 QTest::addColumn<QRect>("result");
911 QTest::newRow( "rectf1" ) << QVariant(QRectF(1, 2, 3, 4)) << QRect(1, 2, 3, 4);
912 QTest::newRow( "rectf2" ) << QVariant(QRectF(0, 0, 0, 0)) << QRect(0, 0, 0, 0);
913 QTest::newRow( "rectf3" ) << QVariant(QRectF(-1, -2, -3, -4)) << QRect(-1, -2, -3, -4);
914 QTest::newRow( "rectf4" ) << QVariant(QRectF(-1.3f, 0, 3.9f, -4.0)) << QRect(-1, 0, 4, -4);
915 QTest::newRow( "rectf5" ) << QVariant(QRectF()) << QRect();
918 void tst_QVariant::toRect()
920 QFETCH( QVariant, value );
921 QFETCH( QRect, result );
922 QVERIFY( value.isValid() );
923 QVERIFY( value.canConvert( QVariant::Rect ) );
924 QRect i = value.toRect();
925 QCOMPARE( i, result );
928 void tst_QVariant::toChar_data()
930 QTest::addColumn<QVariant>("value");
931 QTest::addColumn<QChar>("result");
932 QTest::newRow( "longlong" ) << QVariant(qlonglong('6')) << QChar('6');
933 QTest::newRow( "ulonglong" ) << QVariant(qulonglong('7')) << QChar('7');
936 void tst_QVariant::toChar()
938 QFETCH( QVariant, value );
939 QFETCH( QChar, result );
940 QVERIFY( value.isValid() );
941 QVERIFY( value.canConvert( QVariant::Char ) );
943 QChar i = value.toChar();
944 QCOMPARE( i, result );
947 void tst_QVariant::toBool_data()
949 QTest::addColumn<QVariant>("value");
950 QTest::addColumn<bool>("result");
952 QTest::newRow( "int0" ) << QVariant( 0 ) << false;
953 QTest::newRow( "int1" ) << QVariant( 123 ) << true;
954 QTest::newRow( "uint0" ) << QVariant( 0u ) << false;
955 QTest::newRow( "uint1" ) << QVariant( 123u ) << true;
956 QTest::newRow( "double0" ) << QVariant( 0.0 ) << false;
957 QTest::newRow( "float0" ) << QVariant( 0.0f ) << false;
958 QTest::newRow( "double1" ) << QVariant( 3.1415927 ) << true;
959 QTest::newRow( "float1" ) << QVariant( 3.1415927f ) << true;
960 QTest::newRow( "string0" ) << QVariant( QString("3") ) << true;
961 QTest::newRow( "string1" ) << QVariant( QString("true") ) << true;
962 QTest::newRow( "string2" ) << QVariant( QString("0") ) << false;
963 QTest::newRow( "string3" ) << QVariant( QString("fAlSe") ) << false;
964 QTest::newRow( "longlong0" ) << QVariant( (qlonglong)0 ) << false;
965 QTest::newRow( "longlong1" ) << QVariant( (qlonglong)1 ) << true;
966 QTest::newRow( "ulonglong0" ) << QVariant( (qulonglong)0 ) << false;
967 QTest::newRow( "ulonglong1" ) << QVariant( (qulonglong)1 ) << true;
968 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << true;
969 QTest::newRow( "Null_QChar" ) << QVariant(QChar(0)) << false;
972 void tst_QVariant::toBool()
974 QFETCH( QVariant, value );
975 QFETCH( bool, result );
976 QVERIFY( value.isValid() );
977 QVERIFY( value.canConvert( QVariant::Bool ) );
979 bool i = value.toBool();
980 QCOMPARE( i, result );
983 void tst_QVariant::toPointF_data()
985 QTest::addColumn<QVariant>("value");
986 QTest::addColumn<QPointF>("result");
988 QTest::newRow( "QPoint" ) << QVariant( QPointF( 19, 84) ) << QPointF( 19, 84 );
991 void tst_QVariant::toPointF()
993 QFETCH( QVariant, value );
994 QFETCH( QPointF, result );
995 QVERIFY( value.isValid() );
996 QVERIFY( value.canConvert( QVariant::PointF ) );
997 QPointF d = value.toPointF();
998 QCOMPARE( d, result );
1001 void tst_QVariant::toRectF_data()
1003 QTest::addColumn<QVariant>("value");
1004 QTest::addColumn<QRectF>("result");
1006 QRect r( 1, 9, 8, 4 );
1007 QRectF rf( 1.0, 9.0, 8.0, 4.0 );
1008 QTest::newRow( "QRect" ) << QVariant( r ) << rf;
1011 void tst_QVariant::toRectF()
1013 QFETCH( QVariant, value );
1014 QFETCH( QRectF, result );
1015 QVERIFY( value.isValid() );
1016 QVERIFY( value.canConvert( QVariant::RectF ) );
1017 QRectF d = value.toRectF();
1018 QCOMPARE( d, result );
1021 void tst_QVariant::toColor_data()
1023 QTest::addColumn<QVariant>("value");
1024 QTest::addColumn<QColor>("result");
1027 QTest::newRow( "string" ) << QVariant( QString( "red" ) ) << c;
1028 QTest::newRow( "solid brush" ) << QVariant( QBrush(c) ) << c;
1031 void tst_QVariant::toColor()
1033 QFETCH( QVariant, value );
1034 QFETCH( QColor, result );
1035 QVERIFY( value.isValid() );
1036 QVERIFY( value.canConvert( QVariant::Color ) );
1037 QColor d = qvariant_cast<QColor>(value);
1038 QCOMPARE( d, result );
1041 void tst_QVariant::toPixmap_data()
1043 QTest::addColumn<QVariant>("value");
1044 QTest::addColumn<QPixmap>("result");
1048 QTest::newRow( "image" ) << QVariant( pm ) << pm;
1051 bm.fill(Qt::color1);
1052 QTest::newRow( "bitmap" ) << QVariant( bm ) << QPixmap(bm);
1055 void tst_QVariant::toPixmap()
1057 QFETCH( QVariant, value );
1058 QFETCH( QPixmap, result );
1059 QVERIFY( value.isValid() );
1060 QVERIFY( value.canConvert( QVariant::Pixmap ) );
1061 QPixmap d = qvariant_cast<QPixmap>(value);
1062 QCOMPARE( d, result );
1065 void tst_QVariant::toImage_data()
1067 QTest::addColumn<QVariant>("value");
1068 QTest::addColumn<QImage>("result");
1070 QImage im(30, 30, QImage::Format_ARGB32);
1071 im.fill(0x7fff0000);
1072 QTest::newRow( "image" ) << QVariant( im ) << im;
1075 void tst_QVariant::toImage()
1077 QFETCH( QVariant, value );
1078 QFETCH( QImage, result );
1079 QVERIFY( value.isValid() );
1080 QVERIFY( value.canConvert( QVariant::Image ) );
1081 QImage d = qvariant_cast<QImage>(value);
1082 QCOMPARE( d, result );
1085 void tst_QVariant::toBrush_data()
1087 QTest::addColumn<QVariant>("value");
1088 QTest::addColumn<QBrush>("result");
1091 QTest::newRow( "color" ) << QVariant( c ) << QBrush(c);
1094 QTest::newRow( "pixmap" ) << QVariant( pm ) << QBrush(pm);
1097 void tst_QVariant::toBrush()
1099 QFETCH( QVariant, value );
1100 QFETCH( QBrush, result );
1101 QVERIFY( value.isValid() );
1102 QVERIFY( value.canConvert( QVariant::Brush ) );
1103 QBrush d = qvariant_cast<QBrush>(value);
1104 QCOMPARE( d, result );
1107 void tst_QVariant::toFont_data()
1109 QTest::addColumn<QVariant>("value");
1110 QTest::addColumn<QFont>("result");
1112 QFont f("times",12,-1,false);
1113 QTest::newRow( "string" ) << QVariant( QString( "times,12,-1,5,50,0,0,0,0,0" ) ) << f;
1116 void tst_QVariant::toFont()
1118 QFETCH( QVariant, value );
1119 QFETCH( QFont, result );
1120 QVERIFY( value.isValid() );
1121 QVERIFY( value.canConvert( QVariant::Font ) );
1122 QFont d = qvariant_cast<QFont>(value);
1123 QCOMPARE( d, result );
1126 void tst_QVariant::toKeySequence_data()
1128 QTest::addColumn<QVariant>("value");
1129 QTest::addColumn<QKeySequence>("result");
1132 QTest::newRow( "int" ) << QVariant( 67108929 ) << QKeySequence( Qt::CTRL + Qt::Key_A );
1135 QTest::newRow( "qstring" )
1136 << QVariant( QString( "Ctrl+A" ) )
1137 << QKeySequence( Qt::CTRL + Qt::Key_A );
1140 void tst_QVariant::toKeySequence()
1142 QFETCH( QVariant, value );
1143 QFETCH( QKeySequence, result );
1144 QVERIFY( value.isValid() );
1145 QVERIFY( value.canConvert( QVariant::KeySequence ) );
1146 QKeySequence d = qvariant_cast<QKeySequence>(value);
1147 QCOMPARE( d, result );
1150 void tst_QVariant::toDouble_data()
1152 QTest::addColumn<QVariant>("value");
1153 QTest::addColumn<double>("result");
1154 QTest::addColumn<bool>("valueOK");
1156 QByteArray bytearray(4, ' ');
1161 QTest::newRow( "bytearray" ) << QVariant( bytearray ) << 32.1 << true;
1164 void tst_QVariant::toDouble()
1166 QFETCH( QVariant, value );
1167 QFETCH( double, result );
1168 QFETCH( bool, valueOK );
1169 QVERIFY( value.isValid() );
1170 QVERIFY( value.canConvert( QVariant::Double ) );
1172 double d = value.toDouble( &ok );
1173 QCOMPARE( d, result );
1174 QVERIFY( ok == valueOK );
1177 void tst_QVariant::toLongLong_data()
1179 QTest::addColumn<QVariant>("value");
1180 QTest::addColumn<qlonglong>("result");
1181 QTest::addColumn<bool>("valueOK");
1183 QTest::newRow( "int0" ) << QVariant( 123 ) << (qlonglong)123 << true;
1184 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (qlonglong)3 << true;
1185 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (qlonglong)3 << true;
1186 QTest::newRow( "uint" ) << QVariant( 123u ) << (qlonglong)123 << true;
1187 QTest::newRow( "int-string" ) << QVariant( QString("123") )
1188 << (qlonglong)123 << true;
1189 QTest::newRow( "string" ) << QVariant( QString("Unicode fun") ) << (qlonglong)0
1191 QTest::newRow( "longlong" ) << QVariant( intMax1 ) << intMax1 << true;
1192 QTest::newRow( "ulonglong" ) << QVariant( uintMax1 ) << (qlonglong)uintMax1 << true;
1193 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << qlonglong('a') << true;
1194 QByteArray bytearray(4, ' ');
1199 QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (qlonglong) 3200 << true;
1202 void tst_QVariant::toLongLong()
1204 QFETCH( QVariant, value );
1205 QFETCH( qlonglong, result );
1206 QFETCH( bool, valueOK );
1207 QVERIFY( value.isValid() );
1208 QVERIFY( value.canConvert( QVariant::LongLong ) );
1210 qlonglong ll = value.toLongLong( &ok );
1211 QCOMPARE( ll, result );
1212 QVERIFY( ok == valueOK );
1215 void tst_QVariant::toULongLong_data()
1217 QTest::addColumn<QVariant>("value");
1218 QTest::addColumn<qulonglong>("result");
1219 QTest::addColumn<bool>("valueOK");
1221 QTest::newRow( "int0" ) << QVariant( 123 ) << (qulonglong)123 << true;
1222 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (qulonglong)3 << true;
1223 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (qulonglong)3 << true;
1224 QTest::newRow( "uint" ) << QVariant( 123u ) << (qulonglong)123 << true;
1225 QTest::newRow( "int-string" ) << QVariant( QString("123") )
1226 << (qulonglong)123 << true;
1227 QTest::newRow( "string" ) << QVariant( QString("Unicode fun") ) << (qulonglong)0
1229 QTest::newRow( "ulonglong-string" ) << QVariant( QString("18446744073709551615") )
1230 << Q_UINT64_C(18446744073709551615)
1232 QTest::newRow( "bytaa-string" ) << QVariant( QString("18446744073709551615") )
1233 << Q_UINT64_C(18446744073709551615)
1235 QTest::newRow( "longlong" ) << QVariant( intMax1 ) << (qulonglong)intMax1 << true;
1236 QTest::newRow( "ulonglong" ) << QVariant( uintMax1 ) << uintMax1 << true;
1237 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << qulonglong('a') << true;
1238 QByteArray bytearray(4, ' ');
1243 QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (qulonglong) 3201 << true;
1246 void tst_QVariant::toULongLong()
1248 QFETCH( QVariant, value );
1249 QFETCH( qulonglong, result );
1250 QFETCH( bool, valueOK );
1251 QVERIFY( value.isValid() );
1252 QVERIFY( value.canConvert( QVariant::ULongLong ) );
1254 qulonglong ll = value.toULongLong( &ok );
1255 QCOMPARE( ll, result );
1256 QVERIFY( ok == valueOK );
1259 void tst_QVariant::toByteArray_data()
1261 QTest::addColumn<QVariant>("value");
1262 QTest::addColumn<QByteArray>("result");
1264 QByteArray ba(5, ' ');
1271 QByteArray variantBa = ba;
1273 QTest::newRow( "qbytearray" ) << QVariant( variantBa ) << ba;
1274 QTest::newRow( "int" ) << QVariant( -123 ) << QByteArray( "-123" );
1275 QTest::newRow( "uint" ) << QVariant( (uint)123 ) << QByteArray( "123" );
1276 QTest::newRow( "double" ) << QVariant( 123.456 ) << QByteArray( "123.456" );
1277 QTest::newRow( "float" ) << QVariant( 123.456f ) << QByteArray( "123.456" );
1278 QTest::newRow( "longlong" ) << QVariant( (qlonglong)34 ) << QByteArray( "34" );
1279 QTest::newRow( "ulonglong" ) << QVariant( (qulonglong)34 ) << QByteArray( "34" );
1282 void tst_QVariant::toByteArray()
1284 QFETCH( QVariant, value );
1285 QFETCH( QByteArray, result );
1286 QVERIFY( value.isValid() );
1287 QVERIFY( value.canConvert( QVariant::ByteArray ) );
1288 QByteArray ba = value.toByteArray();
1289 QCOMPARE( ba, result );
1292 void tst_QVariant::toString_data()
1294 QTest::addColumn<QVariant>("value");
1295 QTest::addColumn<QString>("result");
1297 QTest::newRow( "qstring" ) << QVariant( QString( "Test" ) ) << QString( "Test" );
1298 QTest::newRow( "charstar" ) << QVariant(QLatin1String("Test")) << QString("Test");
1299 QTest::newRow( "qbytearray") << QVariant( QByteArray( "Test\0" ) ) << QString( "Test" );
1300 QTest::newRow( "int" ) << QVariant( -123 ) << QString( "-123" );
1301 QTest::newRow( "uint" ) << QVariant( (uint)123 ) << QString( "123" );
1302 QTest::newRow( "double" ) << QVariant( 123.456 ) << QString( "123.456" );
1303 QTest::newRow( "float" ) << QVariant( 123.456f ) << QString( "123.456" );
1304 QTest::newRow( "bool" ) << QVariant( true ) << QString( "true" );
1305 QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 1, 1 ) ) << QString( "2002-01-01" );
1306 QTest::newRow( "qtime" ) << QVariant( QTime( 12, 34, 56 ) ) << QString( "12:34:56" );
1307 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 1, 1 ), QTime( 12, 34, 56 ) ) ) << QString( "2002-01-01T12:34:56" );
1308 QTest::newRow( "qkeysequence" ) << QVariant::fromValue( QKeySequence( Qt::CTRL + Qt::Key_A ) )
1310 << QString( "Ctrl+A" );
1312 << QString(QChar(0x2318)) + "A";
1315 QFont font( "times", 12 );
1316 QTest::newRow( "qfont" ) << QVariant::fromValue( font ) << QString("times,12,-1,5,50,0,0,0,0,0");
1317 QTest::newRow( "qcolor" ) << QVariant::fromValue( QColor( 10, 10, 10 ) ) << QString( "#0a0a0a" );
1318 QTest::newRow( "llong" ) << QVariant( (qlonglong)Q_INT64_C(123456789012) ) <<
1319 QString( "123456789012" );
1322 void tst_QVariant::toString()
1324 QFETCH( QVariant, value );
1325 QFETCH( QString, result );
1326 QVERIFY( value.isValid() );
1327 QVERIFY( value.canConvert( QVariant::String ) );
1328 QString str = value.toString();
1329 QCOMPARE( str, result );
1332 void tst_QVariant::toDate_data()
1334 QTest::addColumn<QVariant>("value");
1335 QTest::addColumn<QDate>("result");
1337 QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 10, 10 ) ) << QDate( 2002, 10, 10 );
1338 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) ) ) << QDate( 2002, 10, 10 );
1339 QTest::newRow( "qstring" ) << QVariant( QString( "2002-10-10" ) ) << QDate( 2002, 10, 10 );
1342 void tst_QVariant::toDate()
1344 QFETCH( QVariant, value );
1345 QFETCH( QDate, result );
1346 QVERIFY( value.isValid() );
1347 QVERIFY( value.canConvert( QVariant::Date ) );
1348 QCOMPARE( value.toDate(), result );
1351 void tst_QVariant::toTime_data()
1353 QTest::addColumn<QVariant>("value");
1354 QTest::addColumn<QTime>("result");
1356 QTest::newRow( "qtime" ) << QVariant( QTime( 12, 34, 56 ) ) << QTime( 12, 34, 56 );
1357 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) ) ) << QTime( 12, 34, 56 );
1358 QTest::newRow( "qstring" ) << QVariant( QString( "12:34:56" ) ) << QTime( 12, 34, 56 );
1361 void tst_QVariant::toTime()
1363 QFETCH( QVariant, value );
1364 QFETCH( QTime, result );
1365 QVERIFY( value.isValid() );
1366 QVERIFY( value.canConvert( QVariant::Time ) );
1367 QCOMPARE( value.toTime(), result );
1370 void tst_QVariant::toDateTime_data()
1372 QTest::addColumn<QVariant>("value");
1373 QTest::addColumn<QDateTime>("result");
1375 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) ) )
1376 << QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) );
1377 QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 10, 10 ) ) << QDateTime( QDate( 2002, 10, 10 ), QTime( 0, 0, 0 ) );
1378 QTest::newRow( "qstring" ) << QVariant( QString( "2002-10-10T12:34:56" ) ) << QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) );
1381 void tst_QVariant::toDateTime()
1383 QFETCH( QVariant, value );
1384 QFETCH( QDateTime, result );
1385 QVERIFY( value.isValid() );
1386 QVERIFY( value.canConvert( QVariant::DateTime ) );
1387 QCOMPARE( value.toDateTime(), result );
1390 void tst_QVariant::toLocale()
1393 QLocale loc = variant.toLocale();
1394 variant = QLocale::system();
1395 loc = variant.toLocale();
1398 void tst_QVariant::toRegExp()
1401 QRegExp rx = variant.toRegExp();
1402 variant = QRegExp("foo");
1403 rx = variant.toRegExp();
1406 void tst_QVariant::toRegularExpression()
1409 QRegularExpression re = variant.toRegularExpression();
1410 QCOMPARE(re, QRegularExpression());
1412 variant = QRegularExpression("abc.*def");
1413 re = variant.toRegularExpression();
1414 QCOMPARE(re, QRegularExpression("abc.*def"));
1416 variant = QVariant::fromValue(QRegularExpression("[ab]\\w+"));
1417 re = variant.value<QRegularExpression>();
1418 QCOMPARE(re, QRegularExpression("[ab]\\w+"));
1421 void tst_QVariant::matrix()
1424 QMatrix matrix = qvariant_cast<QMatrix>(variant);
1425 QVERIFY(matrix.isIdentity());
1426 variant.setValue(QMatrix().rotate(90));
1427 QCOMPARE(QMatrix().rotate(90), qvariant_cast<QMatrix>(variant));
1429 void *mmatrix = QMetaType::create(QVariant::Matrix, 0);
1431 QMetaType::destroy(QVariant::Matrix, mmatrix);
1434 void tst_QVariant::matrix4x4()
1437 QMatrix4x4 matrix = qvariant_cast<QMatrix4x4>(variant);
1438 QVERIFY(matrix.isIdentity());
1441 variant.setValue(m);
1442 QCOMPARE(m, qvariant_cast<QMatrix4x4>(variant));
1444 void *mmatrix = QMetaType::create(QVariant::Matrix4x4, 0);
1446 QMetaType::destroy(QVariant::Matrix4x4, mmatrix);
1449 void tst_QVariant::transform()
1452 QTransform matrix = qvariant_cast<QTransform>(variant);
1453 QVERIFY(matrix.isIdentity());
1454 variant.setValue(QTransform().rotate(90));
1455 QCOMPARE(QTransform().rotate(90), qvariant_cast<QTransform>(variant));
1457 void *mmatrix = QMetaType::create(QVariant::Transform, 0);
1459 QMetaType::destroy(QVariant::Transform, mmatrix);
1463 void tst_QVariant::vector2D()
1466 QVector2D vector = qvariant_cast<QVector2D>(variant);
1467 QVERIFY(vector.isNull());
1468 variant.setValue(QVector2D(0.1, 0.2));
1469 QCOMPARE(QVector2D(0.1, 0.2), qvariant_cast<QVector2D>(variant));
1471 void *pvector = QMetaType::create(QVariant::Vector2D, 0);
1473 QMetaType::destroy(QVariant::Vector2D, pvector);
1476 void tst_QVariant::vector3D()
1479 QVector3D vector = qvariant_cast<QVector3D>(variant);
1480 QVERIFY(vector.isNull());
1481 variant.setValue(QVector3D(0.1, 0.2, 0.3));
1482 QCOMPARE(QVector3D(0.1, 0.2, 0.3), qvariant_cast<QVector3D>(variant));
1484 void *pvector = QMetaType::create(QVariant::Vector3D, 0);
1486 QMetaType::destroy(QVariant::Vector3D, pvector);
1489 void tst_QVariant::vector4D()
1492 QVector4D vector = qvariant_cast<QVector4D>(variant);
1493 QVERIFY(vector.isNull());
1494 variant.setValue(QVector4D(0.1, 0.2, 0.3, 0.4));
1495 QCOMPARE(QVector4D(0.1, 0.2, 0.3, 0.4), qvariant_cast<QVector4D>(variant));
1497 void *pvector = QMetaType::create(QVariant::Vector4D, 0);
1499 QMetaType::destroy(QVariant::Vector4D, pvector);
1502 void tst_QVariant::quaternion()
1505 QQuaternion quaternion = qvariant_cast<QQuaternion>(variant);
1506 QVERIFY(quaternion.isIdentity());
1507 variant.setValue(QQuaternion(0.1, 0.2, 0.3, 0.4));
1508 QCOMPARE(QQuaternion(0.1, 0.2, 0.3, 0.4), qvariant_cast<QQuaternion>(variant));
1510 void *pquaternion = QMetaType::create(QVariant::Quaternion, 0);
1511 QVERIFY(pquaternion);
1512 QMetaType::destroy(QVariant::Quaternion, pquaternion);
1515 struct CustomStreamableClass
1518 bool operator==(const CustomStreamableClass& other) const
1520 return i == other.i;
1523 Q_DECLARE_METATYPE(CustomStreamableClass);
1525 QDataStream &operator<<(QDataStream &out, const CustomStreamableClass &myObj)
1527 return out << myObj.i;
1530 QDataStream &operator>>(QDataStream &in, CustomStreamableClass &myObj)
1532 return in >> myObj.i;
1535 void tst_QVariant::writeToReadFromDataStream_data()
1537 qRegisterMetaTypeStreamOperators<CustomStreamableClass>();
1539 QTest::addColumn<QVariant>("writeVariant");
1540 QTest::addColumn<bool>("isNull");
1542 typedef QList<QVariant> variantsList;
1543 variantsList valuelist;
1544 valuelist << QVariant( 1 ) << QVariant( QString("Two") ) << QVariant( 3.45 );
1545 QVariant var(valuelist);
1546 QTest::newRow( "list_valid" ) << var << false;
1549 QTest::newRow( "invalid" ) << QVariant() << true;
1550 QTest::newRow( "bitarray_invalid" ) << QVariant( QBitArray() ) << true;
1551 QBitArray bitarray( 3 );
1555 QTest::newRow( "bitarray_valid" ) << QVariant( bitarray ) << false;
1556 QTest::newRow( "bytearray_invalid" ) << QVariant( QByteArray() ) << true;
1557 QTest::newRow( "int_invalid") << QVariant(QVariant::Int) << true;
1558 QByteArray bytearray(5, ' ');
1563 bytearray[4] = '\0';
1564 QTest::newRow( "bytearray_valid" ) << QVariant( bytearray ) << false;
1565 QTest::newRow( "bitmap_invalid" ) << QVariant::fromValue( QBitmap() ) << true;
1566 QBitmap bitmap( 10, 10 );
1567 bitmap.fill( Qt::red );
1568 QTest::newRow( "bitmap_valid" ) << QVariant::fromValue( bitmap ) << false;
1569 QTest::newRow( "brush_valid" ) << QVariant::fromValue( QBrush( Qt::red ) ) << false;
1570 QTest::newRow( "color_valid" ) << QVariant::fromValue( QColor( Qt::red ) ) << false;
1571 #ifndef QT_NO_CURSOR
1572 QTest::newRow( "cursor_valid" ) << QVariant::fromValue( QCursor( Qt::PointingHandCursor ) ) << false;
1574 QTest::newRow( "date_invalid" ) << QVariant( QDate() ) << true;
1575 QTest::newRow( "date_valid" ) << QVariant( QDate( 2002, 07, 06 ) ) << false;
1576 QTest::newRow( "datetime_invalid" ) << QVariant( QDateTime() ) << true;
1577 QTest::newRow( "datetime_valid" ) << QVariant( QDateTime( QDate( 2002, 07, 06 ), QTime( 14, 0, 0 ) ) ) << false;
1578 QTest::newRow( "double_valid" ) << QVariant( 123.456 ) << false;
1579 QTest::newRow( "float_valid" ) << QVariant( 123.456f ) << false;
1580 QTest::newRow( "font_valid" ) << QVariant::fromValue( QFont( "times", 12 ) ) << false;
1581 QTest::newRow( "pixmap_invalid" ) << QVariant::fromValue( QPixmap() ) << true;
1582 QPixmap pixmap( 10, 10 );
1583 pixmap.fill( Qt::red );
1584 QTest::newRow( "pixmap_valid" ) << QVariant::fromValue( pixmap ) << false;
1585 // QTest::newRow( "iconset_valid" ) << QVariant( QIcon( pixmap ) ) << false;
1586 QTest::newRow( "image_invalid" ) << QVariant::fromValue( QImage() ) << true;
1587 QTest::newRow( "keysequence_valid" ) << QVariant::fromValue( QKeySequence( Qt::CTRL + Qt::Key_A ) ) << false;
1588 QTest::newRow( "int_valid" ) << QVariant( -123 ) << false;
1589 typedef QMap<QString, QVariant> variantsMap;
1591 vMap.insert( "int", QVariant( 1 ) );
1592 vMap.insert( "string", QVariant( QString("Two") ) );
1593 vMap.insert( "double", QVariant( 3.45 ) );
1594 vMap.insert( "float", QVariant( 3.45f ) );
1595 QTest::newRow( "map_valid" ) << QVariant( vMap ) << false;
1596 QTest::newRow( "palette_valid" ) << QVariant::fromValue(QPalette(QColor("turquoise"))) << false;
1597 QTest::newRow( "pen_valid" ) << QVariant::fromValue( QPen( Qt::red ) ) << false;
1598 QTest::newRow( "pointarray_invalid" ) << QVariant::fromValue( QPolygon() ) << true;
1599 QTest::newRow( "pointarray_valid" ) << QVariant::fromValue( QPolygon( QRect( 10, 10, 20, 20 ) ) ) << false;
1600 QTest::newRow( "region_invalid" ) << QVariant::fromValue( QRegion() ) << true;
1601 QTest::newRow( "region_valid" ) << QVariant::fromValue( QRegion( 10, 10, 20, 20 ) ) << false;
1602 QTest::newRow( "sizepolicy_valid" ) << QVariant::fromValue( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) ) << false;
1603 QTest::newRow( "point_invalid" ) << QVariant::fromValue( QPoint() ) << true;
1604 QTest::newRow( "point_valid" ) << QVariant::fromValue( QPoint( 10, 10 ) ) << false;
1605 QTest::newRow( "rect_invalid" ) << QVariant( QRect() ) << true;
1606 QTest::newRow( "rect_valid" ) << QVariant( QRect( 10, 10, 20, 20 ) ) << false;
1607 QTest::newRow( "size_invalid" ) << QVariant( QSize( 0, 0 ) ) << true;
1608 QTest::newRow( "size_valid" ) << QVariant( QSize( 10, 10 ) ) << false;
1609 QTest::newRow( "string_invalid" ) << QVariant( QString() ) << true;
1610 QTest::newRow( "string_valid" ) << QVariant( QString( "Test" ) ) << false;
1611 QStringList stringlist;
1612 stringlist << "One" << "Two" << "Three";
1613 QTest::newRow( "stringlist_valid" ) << QVariant( stringlist ) << false;
1614 QTest::newRow( "time_invalid" ) << QVariant( QTime() ) << true;
1615 QTest::newRow( "time_valid" ) << QVariant( QTime( 14, 0, 0 ) ) << false;
1616 QTest::newRow( "uint_valid" ) << QVariant( (uint)123 ) << false;
1617 QTest::newRow( "qchar" ) << QVariant(QChar('a')) << false;
1618 QTest::newRow( "qchar_null" ) << QVariant(QChar(0)) << true;
1619 QTest::newRow( "regexp" ) << QVariant(QRegExp("foo", Qt::CaseInsensitive)) << false;
1620 QTest::newRow( "regexp_empty" ) << QVariant(QRegExp()) << false;
1621 QTest::newRow( "regularexpression" ) << QVariant(QRegularExpression("abc.*def")) << false;
1622 QTest::newRow( "regularexpression_empty" ) << QVariant(QRegularExpression()) << false;
1624 // types known to QMetaType, but not part of QVariant::Type
1625 QTest::newRow("QMetaType::Long invalid") << QVariant(QMetaType::Long, (void *) 0) << false;
1627 QTest::newRow("QMetaType::Long") << QVariant(QMetaType::Long, &longInt) << false;
1628 QTest::newRow("QMetaType::Short invalid") << QVariant(QMetaType::Short, (void *) 0) << false;
1630 QTest::newRow("QMetaType::Short") << QVariant(QMetaType::Short, &shortInt) << false;
1631 QTest::newRow("QMetaType::Char invalid") << QVariant(QMetaType::Char, (void *) 0) << false;
1633 QTest::newRow("QMetaType::Char") << QVariant(QMetaType::Char, &ch) << false;
1634 QTest::newRow("QMetaType::ULong invalid") << QVariant(QMetaType::ULong, (void *) 0) << false;
1635 ulong ulongInt = 1ul;
1636 QTest::newRow("QMetaType::ULong") << QVariant(QMetaType::ULong, &ulongInt) << false;
1637 QTest::newRow("QMetaType::UShort invalid") << QVariant(QMetaType::UShort, (void *) 0) << false;
1638 ushort ushortInt = 1u;
1639 QTest::newRow("QMetaType::UShort") << QVariant(QMetaType::UShort, &ushortInt) << false;
1640 QTest::newRow("QMetaType::UChar invalid") << QVariant(QMetaType::UChar, (void *) 0) << false;
1642 QTest::newRow("QMetaType::UChar") << QVariant(QMetaType::UChar, &uch) << false;
1643 QTest::newRow("QMetaType::Float invalid") << QVariant(QMetaType::Float, (void *) 0) << false;
1645 QTest::newRow("QMetaType::Float") << QVariant(QMetaType::Float, &f) << false;
1646 CustomStreamableClass custom = {123};
1647 QTest::newRow("Custom type") << QVariant::fromValue(custom) << false;
1650 void tst_QVariant::writeToReadFromDataStream()
1652 QFETCH( QVariant, writeVariant );
1653 QFETCH( bool, isNull );
1656 QDataStream writeStream( &data, QIODevice::WriteOnly );
1657 writeStream << writeVariant;
1659 QVariant readVariant;
1660 QDataStream readStream( &data, QIODevice::ReadOnly );
1661 readStream >> readVariant;
1662 QVERIFY( readVariant.isNull() == isNull );
1663 // Best way to confirm the readVariant contains the same data?
1664 // Since only a few won't match since the serial numbers are different
1665 // I won't bother adding another bool in the data test.
1666 const int writeType = writeVariant.userType();
1667 if (writeType == qMetaTypeId<CustomStreamableClass>())
1668 QCOMPARE(qvariant_cast<CustomStreamableClass>(readVariant), qvariant_cast<CustomStreamableClass>(writeVariant));
1669 else if ( writeType != QVariant::Invalid && writeType != QVariant::Bitmap && writeType != QVariant::Pixmap
1670 && writeType != QVariant::Image) {
1671 switch (writeType) {
1673 QCOMPARE( readVariant, writeVariant );
1676 // compare types know by QMetaType but not QVariant (QVariant::operator==() knows nothing about them)
1677 case QMetaType::Long:
1678 QCOMPARE(qvariant_cast<long>(readVariant), qvariant_cast<long>(writeVariant));
1680 case QMetaType::ULong:
1681 QCOMPARE(qvariant_cast<ulong>(readVariant), qvariant_cast<ulong>(writeVariant));
1683 case QMetaType::Short:
1684 QCOMPARE(qvariant_cast<short>(readVariant), qvariant_cast<short>(writeVariant));
1686 case QMetaType::UShort:
1687 QCOMPARE(qvariant_cast<ushort>(readVariant), qvariant_cast<ushort>(writeVariant));
1689 case QMetaType::Char:
1690 QCOMPARE(qvariant_cast<char>(readVariant), qvariant_cast<char>(writeVariant));
1692 case QMetaType::UChar:
1693 QCOMPARE(qvariant_cast<uchar>(readVariant), qvariant_cast<uchar>(writeVariant));
1695 case QMetaType::Float:
1697 // the uninitialized float can be NaN (observed on Windows Mobile 5 ARMv4i)
1698 float readFloat = qvariant_cast<float>(readVariant);
1699 float writtenFloat = qvariant_cast<float>(writeVariant);
1700 QVERIFY(qIsNaN(readFloat) == qIsNaN(writtenFloat));
1701 if (!qIsNaN(readFloat))
1702 QVERIFY(readFloat == writtenFloat);
1709 void tst_QVariant::writeToReadFromOldDataStream()
1711 QVariant writeVariant = QString("hello");
1714 QDataStream writeStream(&data, QIODevice::WriteOnly);
1715 writeStream.setVersion(QDataStream::Qt_2_1);
1716 writeStream << writeVariant;
1718 QVariant readVariant;
1719 QDataStream readStream(&data, QIODevice::ReadOnly);
1720 readStream.setVersion(QDataStream::Qt_2_1);
1721 readStream >> readVariant;
1723 QCOMPARE(writeVariant.userType(), readVariant.userType());
1724 QCOMPARE(writeVariant, readVariant);
1727 void tst_QVariant::checkDataStream()
1729 const int typeId = QMetaType::LastCoreType + 1;
1730 QVERIFY(!QMetaType::isRegistered(typeId));
1732 QByteArray errorMessage("Trying to construct an instance of an invalid type, type id: ");
1733 errorMessage.append(QString::number(typeId, 10));
1735 QTest::ignoreMessage(QtWarningMsg, errorMessage.constData());
1736 QByteArray settingsHex("000000");
1737 settingsHex.append(QString::number(typeId, 16));
1738 settingsHex.append("ffffffffff");
1739 const QByteArray settings = QByteArray::fromHex(settingsHex);
1740 QDataStream in(settings);
1743 // the line below has been left out for now since the data stream
1744 // is not necessarily considered corrupt when an invalid QVariant is
1745 // constructed. However, it might be worth considering changing that behavior
1747 // QCOMPARE(in.status(), QDataStream::ReadCorruptData);
1748 QCOMPARE(v.type(), QVariant::Invalid);
1751 void tst_QVariant::operator_eq_eq_data()
1753 QTest::addColumn<QVariant>("left");
1754 QTest::addColumn<QVariant>("right");
1755 QTest::addColumn<bool>("equal"); // left == right ?
1758 QVariant i0( int(0) );
1759 QVariant i1( int(1) );
1761 QTest::newRow( "invinv" ) << inv << inv << true;
1763 QTest::newRow( "int1int1" ) << i1 << i1 << true;
1764 QTest::newRow( "int1int0" ) << i1 << i0 << false;
1765 QTest::newRow( "nullint" ) << i0 << QVariant(QVariant::Int) << false;
1767 // LongLong and ULongLong
1768 QVariant ll1( (qlonglong)1 );
1769 QVariant lln2( (qlonglong)-2 );
1770 QVariant ull1( (qulonglong)1 );
1771 QVariant ull3( (qulonglong)3 );
1772 QTest::newRow( "ll1ll1" ) << ll1 << ll1 << true;
1773 QTest::newRow( "ll1lln2" ) << ll1 << lln2 << false;
1774 QTest::newRow( "ll1ull1" ) << ull1 << ull1 << true;
1775 QTest::newRow( "ll1i1" ) << ull1 << i1 << true;
1776 QTest::newRow( "ull1ull1" ) << ull1 << ull1 << true;
1777 QTest::newRow( "ull1i1" ) << ull1 << ull1 << true;
1780 QVariant mIntString(QByteArray("-42"));
1781 QVariant mIntQString(QString("-42"));
1783 QVariant mUInt(42u);
1784 QVariant mUIntString(QByteArray("42"));
1785 QVariant mUIntQString(QString("42"));
1787 QVariant mDouble(42.11);
1788 QVariant mDoubleString(QByteArray("42.11"));
1789 QVariant mDoubleQString(QString("42.11"));
1791 QVariant mFloat(42.11f);
1792 QVariant mFloatString(QByteArray("42.11"));
1793 QVariant mFloatQString(QString("42.11"));
1795 QVariant mLongLong((qlonglong)-42);
1796 QVariant mLongLongString(QByteArray("-42"));
1797 QVariant mLongLongQString(QString("-42"));
1799 QVariant mULongLong((qulonglong)42);
1800 QVariant mULongLongString(QByteArray("42"));
1801 QVariant mULongLongQString(QString("42"));
1803 QVariant mBool(false);
1804 QVariant mBoolString(QByteArray("false"));
1805 QVariant mBoolQString(QString("false"));
1807 QTest::newRow( "double_int" ) << QVariant(42.0) << QVariant(42) << true;
1808 QTest::newRow( "float_int" ) << QVariant(42.f) << QVariant(42) << true;
1809 QTest::newRow( "mInt_mIntString" ) << mInt << mIntString << true;
1810 QTest::newRow( "mIntString_mInt" ) << mIntString << mInt << true;
1811 QTest::newRow( "mInt_mIntQString" ) << mInt << mIntQString << true;
1812 QTest::newRow( "mIntQString_mInt" ) << mIntQString << mInt << true;
1814 QTest::newRow( "mUInt_mUIntString" ) << mUInt << mUIntString << true;
1815 QTest::newRow( "mUIntString_mUInt" ) << mUIntString << mUInt << true;
1816 QTest::newRow( "mUInt_mUIntQString" ) << mUInt << mUIntQString << true;
1817 QTest::newRow( "mUIntQString_mUInt" ) << mUIntQString << mUInt << true;
1819 QTest::newRow( "mDouble_mDoubleString" ) << mDouble << mDoubleString << true;
1820 QTest::newRow( "mDoubleString_mDouble" ) << mDoubleString << mDouble << true;
1821 QTest::newRow( "mDouble_mDoubleQString" ) << mDouble << mDoubleQString << true;
1822 QTest::newRow( "mDoubleQString_mDouble" ) << mDoubleQString << mDouble << true;
1824 QTest::newRow( "mFloat_mFloatString" ) << mFloat << mFloatString << true;
1825 QTest::newRow( "mFloatString_mFloat" ) << mFloatString << mFloat << true;
1826 QTest::newRow( "mFloat_mFloatQString" ) << mFloat << mFloatQString << true;
1827 QTest::newRow( "mFloatQString_mFloat" ) << mFloatQString << mFloat << true;
1829 QTest::newRow( "mLongLong_mLongLongString" ) << mLongLong << mLongLongString << true;
1830 QTest::newRow( "mLongLongString_mLongLong" ) << mLongLongString << mLongLong << true;
1831 QTest::newRow( "mLongLong_mLongLongQString" ) << mLongLong << mLongLongQString << true;
1832 QTest::newRow( "mLongLongQString_mLongLong" ) << mLongLongQString << mLongLong << true;
1834 QTest::newRow( "mULongLong_mULongLongString" ) << mULongLong << mULongLongString << true;
1835 QTest::newRow( "mULongLongString_mULongLong" ) << mULongLongString << mULongLong << true;
1836 QTest::newRow( "mULongLong_mULongLongQString" ) << mULongLong << mULongLongQString << true;
1837 QTest::newRow( "mULongLongQString_mULongLong" ) << mULongLongQString << mULongLong << true;
1839 QTest::newRow( "mBool_mBoolString" ) << mBool << mBoolString << true;
1840 QTest::newRow( "mBoolString_mBool" ) << mBoolString << mBool << true;
1841 QTest::newRow( "mBool_mBoolQString" ) << mBool << mBoolQString << true;
1842 QTest::newRow( "mBoolQString_mBool" ) << mBoolQString << mBool << true;
1844 QTest::newRow("ba2qstring") << QVariant(QLatin1String("hallo")) << QVariant(QString("hallo")) << true;
1845 QTest::newRow("qstring2ba") << QVariant(QString("hallo")) << QVariant(QLatin1String("hallo")) << true;
1846 QTest::newRow("char_char") << QVariant(QChar('a')) << QVariant(QChar('a')) << true;
1847 QTest::newRow("char_char2") << QVariant(QChar('a')) << QVariant(QChar('b')) << false;
1849 QTest::newRow("invalidConversion") << QVariant(QString("bubu")) << QVariant(0) << false;
1850 QTest::newRow("invalidConversionR") << QVariant(0) << QVariant(QString("bubu")) << false;
1851 // ### many other combinations missing
1854 QMap<QString, QVariant> map1;
1855 map1.insert( "X", 1 );
1857 QMap<QString, QVariant> map2;
1858 map2.insert( "Y", 1 );
1860 QTest::newRow("TwoItemsInEqual") << QVariant(map1) << QVariant(map2) << false;
1865 QMap<QString, QVariant> map1;
1866 map1.insert( "X", 1 );
1868 QMap<QString, QVariant> map2;
1869 map2.insert( "X", 1 );
1871 QTest::newRow("TwoItemsEqual") << QVariant(map1) << QVariant(map2) << true;
1875 QMap<QString, QVariant> map1;
1876 map1.insert( "X", 1 );
1878 QMap<QString, QVariant> map2;
1880 QTest::newRow("PopulatedEmptyMap") << QVariant(map1) << QVariant(map2) << false;
1884 QMap<QString, QVariant> map1;
1886 QMap<QString, QVariant> map2;
1887 map2.insert( "X", 1 );
1889 QTest::newRow("EmptyPopulatedMap") << QVariant(map1) << QVariant(map2) << false;
1893 QMap<QString, QVariant> map1;
1894 map1.insert( "X", 1 );
1895 map1.insert( "Y", 1 );
1897 QMap<QString, QVariant> map2;
1898 map2.insert( "X", 1 );
1900 QTest::newRow("FirstLarger") << QVariant(map1) << QVariant(map2) << false;
1904 QMap<QString, QVariant> map1;
1905 map1.insert( "X", 1 );
1907 QMap<QString, QVariant> map2;
1908 map2.insert( "X", 1 );
1909 map2.insert( "Y", 1 );
1911 QTest::newRow("SecondLarger") << QVariant(map1) << QVariant(map2) << false;
1914 // same thing with hash
1916 QHash<QString, QVariant> hash1;
1917 hash1.insert( "X", 1 );
1919 QHash<QString, QVariant> hash2;
1920 hash2.insert( "Y", 1 );
1922 QTest::newRow("HashTwoItemsInEqual") << QVariant(hash1) << QVariant(hash2) << false;
1927 QHash<QString, QVariant> hash1;
1928 hash1.insert( "X", 1 );
1930 QHash<QString, QVariant> hash2;
1931 hash2.insert( "X", 1 );
1933 QTest::newRow("HashTwoItemsEqual") << QVariant(hash1) << QVariant(hash2) << true;
1937 QHash<QString, QVariant> hash1;
1938 hash1.insert( "X", 1 );
1940 QHash<QString, QVariant> hash2;
1942 QTest::newRow("HashPopulatedEmptyHash") << QVariant(hash1) << QVariant(hash2) << false;
1946 QHash<QString, QVariant> hash1;
1948 QHash<QString, QVariant> hash2;
1949 hash2.insert( "X", 1 );
1951 QTest::newRow("EmptyPopulatedHash") << QVariant(hash1) << QVariant(hash2) << false;
1955 QHash<QString, QVariant> hash1;
1956 hash1.insert( "X", 1 );
1957 hash1.insert( "Y", 1 );
1959 QHash<QString, QVariant> hash2;
1960 hash2.insert( "X", 1 );
1962 QTest::newRow("HashFirstLarger") << QVariant(hash1) << QVariant(hash2) << false;
1966 QHash<QString, QVariant> hash1;
1967 hash1.insert( "X", 1 );
1969 QHash<QString, QVariant> hash2;
1970 hash2.insert( "X", 1 );
1971 hash2.insert( "Y", 1 );
1973 QTest::newRow("HashSecondLarger") << QVariant(hash1) << QVariant(hash2) << false;
1977 void tst_QVariant::operator_eq_eq()
1979 QFETCH( QVariant, left );
1980 QFETCH( QVariant, right );
1981 QFETCH( bool, equal );
1982 QEXPECT_FAIL("nullint", "See QTBUG-22933", Continue);
1983 QCOMPARE( left == right, equal );
1986 void tst_QVariant::operator_eq_eq_rhs()
1994 /* This should _not_ compile */
1998 QVERIFY(dt == list);
2002 void tst_QVariant::typeName_data()
2004 QTest::addColumn<int>("type");
2005 QTest::addColumn<QByteArray>("res");
2006 QTest::newRow("0") << int(QVariant::Invalid) << QByteArray("");
2007 QTest::newRow("1") << int(QVariant::Map) << QByteArray("QVariantMap");
2008 QTest::newRow("2") << int(QVariant::List) << QByteArray("QVariantList");
2009 QTest::newRow("3") << int(QVariant::String) << QByteArray("QString");
2010 QTest::newRow("4") << int(QVariant::StringList) << QByteArray("QStringList");
2011 QTest::newRow("5") << int(QVariant::Font) << QByteArray("QFont");
2012 QTest::newRow("6") << int(QVariant::Pixmap) << QByteArray("QPixmap");
2013 QTest::newRow("7") << int(QVariant::Brush) << QByteArray("QBrush");
2014 QTest::newRow("8") << int(QVariant::Rect) << QByteArray("QRect");
2015 QTest::newRow("9") << int(QVariant::Size) << QByteArray("QSize");
2016 QTest::newRow("10") << int(QVariant::Color) << QByteArray("QColor");
2017 QTest::newRow("11") << int(QVariant::Palette) << QByteArray("QPalette");
2018 QTest::newRow("12") << int(QVariant::Point) << QByteArray("QPoint");
2019 QTest::newRow("13") << int(QVariant::Image) << QByteArray("QImage");
2020 QTest::newRow("14") << int(QVariant::Int) << QByteArray("int");
2021 QTest::newRow("15") << int(QVariant::UInt) << QByteArray("uint");
2022 QTest::newRow("16") << int(QVariant::Bool) << QByteArray("bool");
2023 QTest::newRow("17") << int(QVariant::Double) << QByteArray("double");
2024 QTest::newRow("18") << int(QMetaType::Float) << QByteArray("float");
2025 QTest::newRow("19") << int(QVariant::Polygon) << QByteArray("QPolygon");
2026 QTest::newRow("20") << int(QVariant::Region) << QByteArray("QRegion");
2027 QTest::newRow("21") << int(QVariant::Bitmap) << QByteArray("QBitmap");
2028 QTest::newRow("22") << int(QVariant::Cursor) << QByteArray("QCursor");
2029 QTest::newRow("23") << int(QVariant::SizePolicy) << QByteArray("QSizePolicy");
2030 QTest::newRow("24") << int(QVariant::Date) << QByteArray("QDate");
2031 QTest::newRow("25") << int(QVariant::Time) << QByteArray("QTime");
2032 QTest::newRow("26") << int(QVariant::DateTime) << QByteArray("QDateTime");
2033 QTest::newRow("27") << int(QVariant::ByteArray) << QByteArray("QByteArray");
2034 QTest::newRow("28") << int(QVariant::BitArray) << QByteArray("QBitArray");
2035 QTest::newRow("29") << int(QVariant::KeySequence) << QByteArray("QKeySequence");
2036 QTest::newRow("30") << int(QVariant::Pen) << QByteArray("QPen");
2037 QTest::newRow("31") << int(QVariant::LongLong) << QByteArray("qlonglong");
2038 QTest::newRow("32") << int(QVariant::ULongLong) << QByteArray("qulonglong");
2039 QTest::newRow("33") << int(QVariant::Char) << QByteArray("QChar");
2040 QTest::newRow("34") << int(QVariant::Url) << QByteArray("QUrl");
2041 QTest::newRow("35") << int(QVariant::TextLength) << QByteArray("QTextLength");
2042 QTest::newRow("36") << int(QVariant::TextFormat) << QByteArray("QTextFormat");
2043 QTest::newRow("37") << int(QVariant::Locale) << QByteArray("QLocale");
2044 QTest::newRow("38") << int(QVariant::LineF) << QByteArray("QLineF");
2045 QTest::newRow("39") << int(QVariant::RectF) << QByteArray("QRectF");
2046 QTest::newRow("40") << int(QVariant::PointF) << QByteArray("QPointF");
2047 QTest::newRow("41") << int(QVariant::RegExp) << QByteArray("QRegExp");
2048 QTest::newRow("43") << int(QVariant::Matrix) << QByteArray("QMatrix");
2049 QTest::newRow("44") << int(QVariant::Transform) << QByteArray("QTransform");
2050 QTest::newRow("45") << int(QVariant::Hash) << QByteArray("QVariantHash");
2051 QTest::newRow("46") << int(QVariant::Matrix4x4) << QByteArray("QMatrix4x4");
2052 QTest::newRow("47") << int(QVariant::Vector2D) << QByteArray("QVector2D");
2053 QTest::newRow("48") << int(QVariant::Vector3D) << QByteArray("QVector3D");
2054 QTest::newRow("49") << int(QVariant::Vector4D) << QByteArray("QVector4D");
2055 QTest::newRow("50") << int(QVariant::Quaternion) << QByteArray("QQuaternion");
2056 QTest::newRow("51") << int(QVariant::RegularExpression) << QByteArray("QRegularExpression");
2059 void tst_QVariant::typeName()
2061 QFETCH( int, type );
2062 QFETCH( QByteArray, res );
2063 QCOMPARE(QString::fromLatin1(QVariant::typeToName((QVariant::Type)type)),
2064 QString::fromLatin1(res.constData()));
2067 // test nameToType as well
2068 void tst_QVariant::typeToName()
2071 QCOMPARE( QVariant::typeToName( v.type() ), (const char*)0 ); // Invalid
2072 // assumes that QVariant::Type contains consecutive values
2074 int max = QVariant::LastGuiType;
2075 for ( int t = 1; t <= max; t++ ) {
2076 const char *n = QVariant::typeToName( (QVariant::Type)t );
2078 QCOMPARE( int(QVariant::nameToType( n )), t );
2081 QCOMPARE(QVariant::typeToName(QVariant::Int), "int");
2082 // not documented but we return 0 if the type is out of range
2083 // by testing this we catch cases where QVariant is extended
2084 // but type_map is not updated accordingly
2085 QCOMPARE( QVariant::typeToName( QVariant::Type(max+1) ), (char*)0 );
2086 // invalid type names
2087 QVERIFY( QVariant::nameToType( 0 ) == QVariant::Invalid );
2088 QVERIFY( QVariant::nameToType( "" ) == QVariant::Invalid );
2089 QVERIFY( QVariant::nameToType( "foo" ) == QVariant::Invalid );
2091 QCOMPARE(QVariant::nameToType("UserType"), QVariant::Invalid);
2093 // We don't support these old (Qt3) types anymore.
2094 QCOMPARE(QVariant::nameToType("QIconSet"), QVariant::Invalid);
2095 QCOMPARE(QVariant::nameToType("Q3CString"), QVariant::Invalid);
2096 QCOMPARE(QVariant::nameToType("Q_LLONG"), QVariant::Invalid);
2097 QCOMPARE(QVariant::nameToType("Q_ULLONG"), QVariant::Invalid);
2100 void tst_QVariant::streamInvalidVariant()
2106 QVariant writeVariant;
2107 QVariant readVariant;
2109 QVERIFY( writeVariant.type() == QVariant::Invalid );
2112 QDataStream writeStream( &data, QIODevice::WriteOnly );
2113 writeStream << writeX << writeVariant << writeY;
2115 QDataStream readStream( &data, QIODevice::ReadOnly );
2116 readStream >> readX >> readVariant >> readY;
2118 QVERIFY( readX == writeX );
2119 // Two invalid QVariant's aren't necessarily the same, so == will
2120 // return false if one is invalid, so check the type() instead
2121 QVERIFY( readVariant.type() == QVariant::Invalid );
2122 QVERIFY( readY == writeY );
2125 static int instanceCount = 0;
2129 MyType(int n = 0, const char *t=0): number(n), text(t)
2133 MyType(const MyType &other)
2134 : number(other.number), text(other.text)
2146 Q_DECLARE_METATYPE(MyType)
2147 Q_DECLARE_METATYPE(MyType*)
2149 void tst_QVariant::userType()
2152 MyType data(1, "eins");
2153 MyType data2(2, "zwei");
2157 userVar.setValue(data);
2159 QCOMPARE(userVar.type(), QVariant::UserType);
2160 QCOMPARE(userVar.userType(), qMetaTypeId<MyType>());
2161 QCOMPARE(userVar.typeName(), "MyType");
2162 QVERIFY(!userVar.isNull());
2163 QVERIFY(!userVar.canConvert(QVariant::String));
2165 QVariant userVar2(userVar);
2166 QVERIFY(userVar == userVar2);
2168 userVar2.setValue(data2);
2169 QVERIFY(userVar != userVar2);
2171 const MyType *varData = static_cast<const MyType *>(userVar.constData());
2173 QCOMPARE(varData->number, data.number);
2174 QCOMPARE(varData->text, data.text);
2177 userVar3.setValue(data2);
2179 userVar3 = userVar2;
2180 QVERIFY(userVar2 == userVar3);
2182 // At this point all QVariants got destroyed but we have 2 MyType instances.
2183 QCOMPARE(instanceCount, 2);
2186 userVar.setValue(&data);
2188 QCOMPARE(userVar.type(), QVariant::UserType);
2189 QCOMPARE(userVar.userType(), qMetaTypeId<MyType*>());
2190 QCOMPARE(userVar.typeName(), "MyType*");
2191 QVERIFY(!userVar.isNull());
2192 QVERIFY(!userVar.canConvert(QVariant::String));
2194 QVariant userVar2(userVar);
2195 QVERIFY(userVar == userVar2);
2197 userVar2.setValue(&data2);
2198 QVERIFY(userVar != userVar2);
2200 MyType * const*varData = reinterpret_cast<MyType *const *>(userVar.constData());
2202 QCOMPARE(*varData, &data);
2205 userVar3.setValue(&data2);
2207 /* This check is correct now. userVar2 contains a pointer to data2 and so
2209 QVERIFY(userVar2 == userVar3);
2211 userVar3 = userVar2;
2212 QVERIFY(userVar2 == userVar3);
2215 QCOMPARE(instanceCount, 2);
2217 myCarrier.setValue(data);
2218 QCOMPARE(instanceCount, 3);
2220 QVariant second = myCarrier;
2221 QCOMPARE(instanceCount, 3);
2223 QCOMPARE(instanceCount, 4);
2225 QCOMPARE(instanceCount, 3);
2227 MyType data3(0, "null");
2228 data3 = qvariant_cast<MyType>(myCarrier);
2229 QCOMPARE(data3.number, 1);
2230 QCOMPARE(data3.text, (const char *)"eins");
2232 QCOMPARE(instanceCount, 4);
2238 const MyType data(3, "drei");
2241 myCarrier.setValue(data);
2242 QCOMPARE(myCarrier.typeName(), "MyType");
2244 const MyType data2 = qvariant_cast<MyType>(myCarrier);
2245 QCOMPARE(data2.number, 3);
2246 QCOMPARE(data2.text, (const char *)"drei");
2253 myCarrier.setValue(s);
2254 QCOMPARE((int)qvariant_cast<short>(myCarrier), 42);
2258 qlonglong ll = Q_INT64_C(42);
2261 myCarrier.setValue(ll);
2262 QCOMPARE(qvariant_cast<int>(myCarrier), 42);
2265 // At this point all QVariants got destroyed and MyType objects too.
2266 QCOMPARE(instanceCount, 0);
2274 Q_DECLARE_METATYPE(MyTypePOD)
2276 void tst_QVariant::podUserType()
2282 QVariant pod_as_variant = QVariant::fromValue(pod);
2283 MyTypePOD pod2 = qvariant_cast<MyTypePOD>(pod_as_variant);
2285 QCOMPARE(pod.a, pod2.a);
2286 QCOMPARE(pod.b, pod2.b);
2288 pod_as_variant.setValue(pod);
2289 pod2 = qvariant_cast<MyTypePOD>(pod_as_variant);
2291 QCOMPARE(pod.a, pod2.a);
2292 QCOMPARE(pod.b, pod2.b);
2295 void tst_QVariant::basicUserType()
2300 v = QVariant(QMetaType::Int, &i);
2302 QCOMPARE(v.type(), QVariant::Int);
2303 QCOMPARE(v.toInt(), 7);
2307 v = QVariant(QMetaType::QString, &s);
2309 QCOMPARE(v.type(), QVariant::String);
2310 QCOMPARE(v.toString(), QString("foo"));
2314 v = QVariant(QMetaType::Double, &d);
2316 QCOMPARE(v.type(), QVariant::Double);
2317 QCOMPARE(v.toDouble(), 4.4);
2321 v = QVariant(QMetaType::Float, &f);
2323 QCOMPARE(v.userType(), int(QMetaType::Float));
2324 QCOMPARE(v.toDouble(), 4.5);
2327 QByteArray ba("bar");
2328 v = QVariant(QMetaType::QByteArray, &ba);
2330 QCOMPARE(v.type(), QVariant::ByteArray);
2331 QCOMPARE(v.toByteArray(), QByteArray("bar"));
2334 void tst_QVariant::data()
2341 QVariant ll = (qlonglong)2;
2342 QVariant ull = (qulonglong)3;
2343 QVariant s(QString("hallo"));
2344 QVariant r(QRect(1,2,3,4));
2348 QCOMPARE(*static_cast<int *>(v.data()), i.toInt());
2352 QCOMPARE(*static_cast<double *>(v.data()), d.toDouble());
2356 QCOMPARE(*static_cast<float *>(v.data()), qvariant_cast<float>(v));
2360 QCOMPARE(*static_cast<qlonglong *>(v.data()), ll.toLongLong());
2364 QCOMPARE(*static_cast<qulonglong *>(v.data()), ull.toULongLong());
2368 QCOMPARE(*static_cast<QString *>(v.data()), s.toString());
2372 QCOMPARE(*static_cast<QRect *>(v.data()), r.toRect());
2375 void tst_QVariant::constData()
2388 QVERIFY(v.constData());
2389 QCOMPARE(*static_cast<const int *>(v.constData()), i);
2392 QVERIFY(v.constData());
2393 QCOMPARE(*static_cast<const double *>(v.constData()), d);
2396 QVERIFY(v.constData());
2397 QCOMPARE(*static_cast<const float *>(v.constData()), f);
2400 QVERIFY(v.constData());
2401 QCOMPARE(*static_cast<const qlonglong *>(v.constData()), ll);
2404 QVERIFY(v.constData());
2405 QCOMPARE(*static_cast<const qulonglong *>(v.constData()), ull);
2408 QVERIFY(v.constData());
2409 QCOMPARE(*static_cast<const QString *>(v.constData()), s);
2412 QVERIFY(v.constData());
2413 QCOMPARE(*static_cast<const QRect *>(v.constData()), r);
2422 Q_DECLARE_METATYPE(Foo)
2424 void tst_QVariant::variant_to()
2433 sl << QLatin1String("blah");
2442 QCOMPARE(qvariant_cast<double>(v1), 4.2);
2443 QCOMPARE(qvariant_cast<float>(v1), 4.2f);
2444 QCOMPARE(qvariant_cast<int>(v2), 5);
2445 QCOMPARE(qvariant_cast<QStringList>(v3), sl);
2446 QCOMPARE(qvariant_cast<QString>(v3), QString::fromLatin1("blah"));
2448 QCOMPARE(qvariant_cast<Foo>(v4).i, 42);
2451 QCOMPARE(qvariant_cast<Foo>(v5).i, 0);
2453 QCOMPARE(qvariant_cast<int>(v1), 4);
2455 QVariant n = QVariant::fromValue<short>(42);
2456 QCOMPARE(qvariant_cast<int>(n), 42);
2457 QCOMPARE(qvariant_cast<uint>(n), 42u);
2458 QCOMPARE(qvariant_cast<double>(n), 42.0);
2459 QCOMPARE(qvariant_cast<float>(n), 42.f);
2460 QCOMPARE(qvariant_cast<short>(n), short(42));
2461 QCOMPARE(qvariant_cast<ushort>(n), ushort(42));
2463 n = QVariant::fromValue(43l);
2464 QCOMPARE(qvariant_cast<int>(n), 43);
2465 QCOMPARE(qvariant_cast<uint>(n), 43u);
2466 QCOMPARE(qvariant_cast<double>(n), 43.0);
2467 QCOMPARE(qvariant_cast<float>(n), 43.f);
2468 QCOMPARE(qvariant_cast<long>(n), 43l);
2470 n = QLatin1String("44");
2471 QCOMPARE(qvariant_cast<int>(n), 44);
2472 QCOMPARE(qvariant_cast<ulong>(n), 44ul);
2473 QCOMPARE(qvariant_cast<float>(n), 44.0f);
2475 QCOMPARE(QVariant::fromValue(0.25f).toDouble(), 0.25);
2478 struct Blah { int i; };
2480 QDataStream& operator>>(QDataStream& s, Blah& c)
2481 { return (s >> c.i); }
2483 QDataStream& operator<<(QDataStream& s, const Blah& c)
2484 { return (s << c.i); }
2486 void tst_QVariant::saveLoadCustomTypes()
2491 int tp = qRegisterMetaType<Blah>("Blah");
2492 QVariant v = QVariant(tp, &i);
2494 qRegisterMetaTypeStreamOperators<Blah>("Blah");
2496 QCOMPARE(v.userType(), tp);
2497 QVERIFY(v.type() == QVariant::UserType);
2499 QDataStream stream(&data, QIODevice::WriteOnly);
2506 QDataStream stream(data);
2510 QCOMPARE(int(v.userType()), QMetaType::type("Blah"));
2511 int value = *(int*)v.constData();
2512 QCOMPARE(value, 42);
2515 void tst_QVariant::url()
2517 QString str("http://qt.nokia.com");
2520 QVariant v(url); //built with a QUrl
2524 QVariant v3(str); //built with a QString
2526 QCOMPARE(v2.toUrl(), url);
2527 QVERIFY(v3.canConvert<QUrl>());
2528 QCOMPARE(v2.toUrl(), v3.toUrl());
2530 QVERIFY(v2.canConvert<QString>());
2531 QCOMPARE(v2.toString(), str);
2532 QCOMPARE(v3.toString(), str);
2535 void tst_QVariant::variantMap()
2537 QMap<QString, QVariant> map;
2541 QVariantMap map2 = qvariant_cast<QVariantMap>(v);
2543 QCOMPARE(map2.value("test").toInt(), 42);
2545 QVariant v2 = QVariant(QMetaType::type("QVariantMap"), &map);
2546 QCOMPARE(qvariant_cast<QVariantMap>(v2).value("test").toInt(), 42);
2548 QVariant v3 = QVariant(QMetaType::type("QMap<QString, QVariant>"), &map);
2549 QCOMPARE(qvariant_cast<QVariantMap>(v3).value("test").toInt(), 42);
2552 void tst_QVariant::variantHash()
2554 QHash<QString, QVariant> hash;
2558 QVariantHash hash2 = qvariant_cast<QVariantHash>(v);
2560 QCOMPARE(hash2.value("test").toInt(), 42);
2562 QVariant v2 = QVariant(QMetaType::type("QVariantHash"), &hash);
2563 QCOMPARE(qvariant_cast<QVariantHash>(v2).value("test").toInt(), 42);
2565 QVariant v3 = QVariant(QMetaType::type("QHash<QString, QVariant>"), &hash);
2566 QCOMPARE(qvariant_cast<QVariantHash>(v3).value("test").toInt(), 42);
2569 void tst_QVariant::invalidQColor() const
2571 QVariant va("An invalid QColor::name() value.");
2572 QVERIFY(va.canConvert(QVariant::Color));
2574 QVERIFY(!va.convert(QVariant::Color));
2576 QVERIFY(!qvariant_cast<QColor>(va).isValid());
2579 class CustomQObject : public QObject {
2582 CustomQObject(QObject *parent = 0) : QObject(parent) {}
2584 class CustomQWidget : public QWidget {
2587 CustomQWidget(QWidget *parent = 0) : QWidget(parent) {}
2589 Q_DECLARE_METATYPE(CustomQObject*)
2590 Q_DECLARE_METATYPE(CustomQWidget*)
2592 class CustomNonQObject { };
2593 Q_DECLARE_METATYPE(CustomNonQObject)
2594 Q_DECLARE_METATYPE(CustomNonQObject*)
2596 void tst_QVariant::cleanupTestCase()
2598 delete customNonQObjectPointer;
2599 qDeleteAll(objectPointerTestData);
2602 void tst_QVariant::qvariant_cast_QObject_data()
2604 QTest::addColumn<QVariant>("data");
2605 QTest::addColumn<bool>("success");
2606 QObject *obj = new QObject;
2607 obj->setObjectName(QString::fromLatin1("Hello"));
2608 QTest::newRow("from QObject") << QVariant(QMetaType::QObjectStar, &obj) << true;
2609 QTest::newRow("from QObject2") << QVariant::fromValue(obj) << true;
2610 QTest::newRow("from String") << QVariant(QLatin1String("1, 2, 3")) << false;
2611 QTest::newRow("from int") << QVariant((int) 123) << false;
2612 QWidget *widget = new QWidget;
2613 widget->setObjectName(QString::fromLatin1("Hello"));
2614 QTest::newRow("from QWidget") << QVariant::fromValue(widget) << true;
2615 CustomQObject *customObject = new CustomQObject(this);
2616 customObject->setObjectName(QString::fromLatin1("Hello"));
2617 QTest::newRow("from Derived QObject") << QVariant::fromValue(customObject) << true;
2618 CustomQWidget *customWidget = new CustomQWidget;
2619 customWidget->setObjectName(QString::fromLatin1("Hello"));
2620 QTest::newRow("from Derived QWidget") << QVariant::fromValue(customWidget) << true;
2621 QTest::newRow("from custom Object") << QVariant::fromValue(CustomNonQObject()) << false;
2623 // Deleted in cleanupTestCase.
2624 customNonQObjectPointer = new CustomNonQObject;
2625 QTest::newRow("from custom ObjectStar") << QVariant::fromValue(customNonQObjectPointer) << false;
2627 // Deleted in cleanupTestCase.
2628 objectPointerTestData.push_back(obj);
2629 objectPointerTestData.push_back(widget);
2630 objectPointerTestData.push_back(customObject);
2631 objectPointerTestData.push_back(customWidget);
2634 void tst_QVariant::qvariant_cast_QObject()
2636 QFETCH(QVariant, data);
2637 QFETCH(bool, success);
2639 QObject *o = qvariant_cast<QObject *>(data);
2640 QCOMPARE(o != 0, success);
2642 QCOMPARE(o->objectName(), QString::fromLatin1("Hello"));
2643 QVERIFY(data.canConvert<QObject*>());
2644 QVERIFY(data.canConvert(QMetaType::QObjectStar));
2645 QVERIFY(data.canConvert(::qMetaTypeId<QObject*>()));
2646 QVERIFY(data.value<QObject*>());
2647 QVERIFY(data.convert(QMetaType::QObjectStar));
2648 QCOMPARE(data.userType(), int(QMetaType::QObjectStar));
2650 QVERIFY(!data.canConvert<QObject*>());
2651 QVERIFY(!data.canConvert(QMetaType::QObjectStar));
2652 QVERIFY(!data.canConvert(::qMetaTypeId<QObject*>()));
2653 QVERIFY(!data.value<QObject*>());
2654 QVERIFY(!data.convert(QMetaType::QObjectStar));
2655 QVERIFY(data.userType() != QMetaType::QObjectStar);
2659 class CustomQObjectDerived : public CustomQObject {
2662 CustomQObjectDerived(QObject *parent = 0) : CustomQObject(parent) {}
2664 Q_DECLARE_METATYPE(CustomQObjectDerived*)
2666 class CustomQObjectDerivedNoMetaType : public CustomQObject {
2669 CustomQObjectDerivedNoMetaType(QObject *parent = 0) : CustomQObject(parent) {}
2672 void tst_QVariant::qvariant_cast_QObject_derived()
2675 CustomQObjectDerivedNoMetaType *object = new CustomQObjectDerivedNoMetaType(this);
2676 QVariant data = QVariant::fromValue(object);
2677 QVERIFY(data.userType() == qMetaTypeId<CustomQObjectDerivedNoMetaType*>());
2678 QCOMPARE(data.value<QObject *>(), object);
2679 QCOMPARE(data.value<CustomQObjectDerivedNoMetaType *>(), object);
2680 QCOMPARE(data.value<CustomQObject *>(), object);
2681 QVERIFY(data.value<CustomQWidget*>() == 0);
2684 CustomQObjectDerived *object = new CustomQObjectDerived(this);
2685 QVariant data = QVariant::fromValue(object);
2687 QVERIFY(data.userType() == qMetaTypeId<CustomQObjectDerived*>());
2689 QCOMPARE(data.value<QObject *>(), object);
2690 QCOMPARE(data.value<CustomQObjectDerived *>(), object);
2691 QCOMPARE(data.value<CustomQObject *>(), object);
2692 QVERIFY(data.value<CustomQWidget*>() == 0);
2695 CustomQWidget customWidget;
2696 QWidget *widget = &customWidget;
2697 QVariant data = QVariant::fromValue(widget);
2698 QVERIFY(data.userType() == QMetaType::QWidgetStar);
2700 QCOMPARE(data.value<QObject*>(), widget);
2701 QCOMPARE(data.value<QWidget*>(), widget);
2702 QCOMPARE(data.value<CustomQWidget*>(), widget);
2706 Q_DECLARE_METATYPE(qint8);
2708 void tst_QVariant::convertToQUint8() const
2712 const qint8 anInt = 32;
2714 /* QVariant(int) gets invoked here so the QVariant has nothing with qint8 to do.
2715 * It's of type QVariant::Int. */
2716 const QVariant v0 = anInt;
2718 QVERIFY(v0.canConvert<qint8>());
2719 QCOMPARE(int(qvariant_cast<qint8>(v0)), 32);
2720 QCOMPARE(int(v0.toInt()), 32);
2721 QCOMPARE(v0.toString(), QString("32"));
2723 QCOMPARE(int(qvariant_cast<qlonglong>(v0)), 32);
2724 QCOMPARE(int(qvariant_cast<char>(v0)), 32);
2725 QCOMPARE(int(qvariant_cast<short>(v0)), 32);
2726 QCOMPARE(int(qvariant_cast<long>(v0)), 32);
2727 QCOMPARE(int(qvariant_cast<float>(v0)), 32);
2728 QCOMPARE(int(qvariant_cast<double>(v0)), 32);
2733 const quint8 anInt = 32;
2734 const QVariant v0 = anInt;
2736 QVERIFY(v0.canConvert<quint8>());
2737 QCOMPARE(int(qvariant_cast<quint8>(v0)), 32);
2738 QCOMPARE(int(v0.toUInt()), 32);
2739 QCOMPARE(v0.toString(), QString("32"));
2744 const qint16 anInt = 32;
2745 const QVariant v0 = anInt;
2747 QVERIFY(v0.canConvert<qint16>());
2748 QCOMPARE(int(qvariant_cast<qint16>(v0)), 32);
2749 QCOMPARE(int(v0.toInt()), 32);
2750 QCOMPARE(v0.toString(), QString("32"));
2755 const quint16 anInt = 32;
2756 const QVariant v0 = anInt;
2758 QVERIFY(v0.canConvert<quint16>());
2759 QCOMPARE(int(qvariant_cast<quint16>(v0)), 32);
2760 QCOMPARE(int(v0.toUInt()), 32);
2761 QCOMPARE(v0.toString(), QString("32"));
2765 void tst_QVariant::comparePointers() const
2773 QVariant v = QVariant::fromValue<void *>(&myClass);
2774 QVariant v2 = QVariant::fromValue<void *>(&myClass);
2780 Q_DECLARE_METATYPE(Data*)
2782 void tst_QVariant::voidStar() const
2789 v1 = QVariant::fromValue(p1);
2793 v2 = QVariant::fromValue(p2);
2797 v2 = QVariant::fromValue(p2);
2801 void tst_QVariant::dataStar() const
2803 qRegisterMetaType<Data*>();
2804 Data *p1 = new Data;
2806 QVariant v1 = QVariant::fromValue(p1);
2807 QCOMPARE(v1.userType(), qMetaTypeId<Data*>());
2808 QCOMPARE(qvariant_cast<Data*>(v1), p1);
2813 v2 = QVariant::fromValue(p1);
2818 void tst_QVariant::canConvertQStringList() const
2820 QFETCH(bool, canConvert);
2821 QFETCH(QStringList, input);
2822 QFETCH(QString, result);
2826 QCOMPARE(v.canConvert(QVariant::String), canConvert);
2827 QCOMPARE(v.toString(), result);
2830 void tst_QVariant::canConvertQStringList_data() const
2832 QTest::addColumn<bool>("canConvert");
2833 QTest::addColumn<QStringList>("input");
2834 QTest::addColumn<QString>("result");
2836 QTest::newRow("An empty list") << false << QStringList() << QString();
2837 QTest::newRow("A single item") << true << QStringList(QLatin1String("foo")) << QString::fromLatin1("foo");
2838 QTest::newRow("A single, but empty item") << true << QStringList(QString()) << QString();
2843 QTest::newRow("Two items") << false << l << QString();
2846 QTest::newRow("Three items") << false << l << QString();
2849 template<typename T> void convertMetaType()
2851 QVERIFY(QVariant::fromValue<T>(10).isValid());
2852 QVERIFY(QVariant::fromValue<T>(10).canConvert(QVariant::Int));
2853 QCOMPARE(QVariant::fromValue<T>(10).toInt(), 10);
2854 QCOMPARE(QVariant::fromValue<T>(10), QVariant::fromValue<T>(10));
2857 #define CONVERT_META_TYPE(Type) \
2858 convertMetaType<Type>(); \
2859 if (QTest::currentTestFailed()) \
2860 QFAIL("convertMetaType<" #Type "> failed");
2862 void tst_QVariant::canConvertMetaTypeToInt() const
2864 CONVERT_META_TYPE(long);
2865 CONVERT_META_TYPE(short);
2866 CONVERT_META_TYPE(short);
2867 CONVERT_META_TYPE(unsigned short);
2868 CONVERT_META_TYPE(ushort);
2869 CONVERT_META_TYPE(ulong);
2870 CONVERT_META_TYPE(unsigned long);
2871 CONVERT_META_TYPE(uchar);
2872 CONVERT_META_TYPE(unsigned char);
2873 CONVERT_META_TYPE(char);
2874 CONVERT_META_TYPE(uint);
2875 CONVERT_META_TYPE(unsigned int);
2878 #undef CONVERT_META_TYPE
2881 These calls should not produce any warnings.
2883 void tst_QVariant::variantToDateTimeWithoutWarnings() const
2886 const QVariant variant(QLatin1String("An invalid QDateTime string"));
2887 const QDateTime dateTime(variant.toDateTime());
2888 QVERIFY(!dateTime.isValid());
2892 QVariant v1(QLatin1String("xyz"));
2893 v1.convert(QVariant::DateTime);
2895 QVariant v2(QLatin1String("xyz"));
2896 QDateTime dt1(v2.toDateTime());
2898 const QVariant v3(QLatin1String("xyz"));
2899 const QDateTime dt2(v3.toDateTime());
2903 void tst_QVariant::invalidDateTime() const
2905 QVariant variant(QString::fromLatin1("Invalid date time string"));
2906 QVERIFY(!variant.toDateTime().isValid());
2907 QVERIFY(!variant.convert(QVariant::DateTime));
2912 MyClass() : myValue(0) {}
2916 Q_DECLARE_METATYPE( MyClass )
2918 void tst_QVariant::loadUnknownUserType()
2920 qRegisterMetaType<MyClass>("MyClass");
2921 QTest::ignoreMessage(QtWarningMsg, "QVariant::load: unable to load type "
2922 + QByteArray::number(qMetaTypeId<MyClass>()) +".");
2923 char data[] = {0, 0, QMetaType::User >> 8 , char(QMetaType::User), 0, 0, 0, 0, 8, 'M', 'y', 'C', 'l', 'a', 's', 's', 0};
2925 QByteArray ba(data, sizeof(data));
2926 QDataStream ds(&ba, QIODevice::ReadOnly);
2929 QCOMPARE(ds.status(), QDataStream::ReadCorruptData);
2932 void tst_QVariant::loadBrokenUserType()
2934 QTest::ignoreMessage(QtWarningMsg, "Trying to construct an instance of an invalid type, type id: 127");
2935 char data[] = {0, 0, 0, 127, 0 };
2937 QByteArray ba(data, sizeof(data));
2938 QDataStream ds(&ba, QIODevice::ReadOnly);
2941 QCOMPARE(ds.status(), QDataStream::Ok);
2944 void tst_QVariant::invalidDate() const
2946 QString foo("Hello");
2947 QVariant variant(foo);
2948 QVERIFY(!variant.convert(QVariant::Date));
2951 QVERIFY(!variant.convert(QVariant::DateTime));
2954 QVERIFY(!variant.convert(QVariant::Time));
2957 QVERIFY(!variant.convert(QVariant::Int));
2960 QVERIFY(!variant.convert(QVariant::Double));
2963 QVERIFY(!variant.convert(QVariant::Type(QMetaType::Float)));
2968 int x,y,z,q,w,e,r,t;
2970 Q_DECLARE_METATYPE(WontCompare);
2972 void tst_QVariant::compareCustomTypes() const
2974 qRegisterMetaType<WontCompare>("WontCompare");
2978 const QVariant variant1(QVariant::fromValue(f1));
2982 const QVariant variant2(QVariant::fromValue(f2));
2984 /* We compare pointers. */
2985 QVERIFY(variant1 != variant2);
2986 QVERIFY(variant1 == variant1);
2987 QVERIFY(variant2 == variant2);
2990 void tst_QVariant::timeToDateTime() const
2992 const QVariant val(QTime::currentTime());
2993 QVERIFY(!val.canConvert(QVariant::DateTime));
2994 QVERIFY(!val.toDateTime().isValid());
2997 Q_DECLARE_METATYPE(QHostAddress)
2999 void tst_QVariant::copyingUserTypes() const
3003 const QHostAddress ha("127.0.0.1");
3007 QCOMPARE(qvariant_cast<QHostAddress>(var3), ha);
3010 void tst_QVariant::convertBoolToByteArray() const
3012 QFETCH(QByteArray, input);
3013 QFETCH(bool, canConvert);
3014 QFETCH(bool, value);
3016 const QVariant variant(input);
3018 QCOMPARE(variant.canConvert<bool>(), canConvert);
3021 /* Just call this function so we run the code path. */
3022 QCOMPARE(variant.toBool(), value);
3026 void tst_QVariant::convertBoolToByteArray_data() const
3028 QTest::addColumn<QByteArray>("input");
3029 QTest::addColumn<bool>("canConvert");
3030 QTest::addColumn<bool>("value");
3032 QTest::newRow("false")
3033 << QByteArray("false")
3037 QTest::newRow("FALSE")
3038 << QByteArray("FALSE")
3042 QTest::newRow("falSE")
3043 << QByteArray("FALSE")
3052 QTest::newRow("null QByteArray")
3057 QTest::newRow("any-content")
3058 << QByteArray("any-content")
3062 QTest::newRow("true")
3063 << QByteArray("true")
3067 QTest::newRow("TRUE")
3068 << QByteArray("TRUE")
3072 QTest::newRow("trUE")
3073 << QByteArray("trUE")
3078 void tst_QVariant::convertByteArrayToBool() const
3080 QFETCH(bool, input);
3081 QFETCH(QByteArray, output);
3083 const QVariant variant(input);
3084 QCOMPARE(variant.type(), QVariant::Bool);
3085 QCOMPARE(variant.toBool(), input);
3086 QVERIFY(variant.canConvert<bool>());
3088 QCOMPARE(variant.toByteArray(), output);
3091 void tst_QVariant::convertByteArrayToBool_data() const
3093 QTest::addColumn<bool>("input");
3094 QTest::addColumn<QByteArray>("output");
3096 QTest::newRow("false")
3098 << QByteArray("false");
3100 QTest::newRow("true")
3102 << QByteArray("true");
3107 1. Converting the string "9.9" to int fails. This is the behavior of
3108 toLongLong() and hence also QVariant, since it uses it.
3109 2. Converting the QVariant containing the double 9.9 to int works.
3111 Rationale: "9.9" is not a valid int. However, doubles are by definition not
3112 ints and therefore it makes more sense to perform conversion for those.
3114 void tst_QVariant::toIntFromQString() const
3116 QVariant first("9.9");
3118 QCOMPARE(first.toInt(&ok), 0);
3121 QCOMPARE(QString("9.9").toLongLong(&ok), qlonglong(0));
3125 QCOMPARE(v.toInt(&ok), 10);
3131 1. Conversion from (64 bit) double to int works (no overflow).
3132 2. Same conversion works for QVariant::convert.
3134 Rationale: if 2147483630 is set in float and then converted to int,
3135 there will be overflow and the result will be -2147483648.
3137 void tst_QVariant::toIntFromDouble() const
3139 double d = 2147483630; // max int 2147483647
3140 QVERIFY((int)d == 2147483630);
3143 QVERIFY( var.canConvert( QVariant::Int ) );
3146 int result = var.toInt(&ok);
3148 QVERIFY( ok == true );
3149 QCOMPARE(result, 2147483630);
3152 void tst_QVariant::setValue()
3154 QTransform t; //we just take a value so that we're sure that it will be shared
3156 QVERIFY( v1.isDetached() );
3158 QVERIFY( !v1.isDetached() );
3159 QVERIFY( !v2.isDetached() );
3161 v2.setValue(3); //set an integer value
3163 QVERIFY( v1.isDetached() );
3164 QVERIFY( v2.isDetached() );
3167 void tst_QVariant::numericalConvert()
3169 #if defined(Q_OS_LINUX) && defined(Q_CC_GNU) && !defined(__x86_64__)
3170 QSKIP("Known to fail due to a GCC bug on at least Ubuntu 10.04 32-bit - check QTBUG-8959");
3172 QVariant vfloat(float(5.3));
3173 QVariant vdouble(double(5.3));
3174 QVariant vreal(qreal(5.3));
3175 QVariant vint(int(5));
3176 QVariant vuint(uint(5));
3177 QVariant vshort(short(5));
3178 QVariant vlonglong(quint64(5));
3179 QVariant vstringint(QString::fromLatin1("5"));
3180 QVariant vstring(QString::fromLatin1("5.3"));
3182 QVector<QVariant *> vect;
3183 vect << &vfloat << &vdouble << &vreal << &vint << &vuint << &vshort<< &vlonglong << &vstringint << &vstring;
3185 for(int i = 0; i < vect.size(); i++) {
3187 if (i >= 3 && i <= 7)
3189 QVariant *v = vect.at(i);
3190 QCOMPARE(v->toFloat() , float(num));
3191 QCOMPARE(float(v->toReal()) , float(num));
3192 QCOMPARE(float(v->toDouble()) , float(num));
3194 QCOMPARE(v->toInt() , int(num));
3195 QCOMPARE(v->toUInt() , uint(num));
3196 QCOMPARE(v->toULongLong() , quint64(num));
3198 QCOMPARE(v->toString() , QString::number(num));
3203 template<class T> void playWithVariant(const T &orig, bool isNull, const QString &toString, double toDouble, bool toBool)
3205 QVariant v = QVariant::fromValue(orig);
3206 QVERIFY(v.isValid());
3207 QCOMPARE(v.isNull(), isNull);
3208 QCOMPARE(v.toString(), toString);
3209 QCOMPARE(v.toDouble(), toDouble);
3210 QCOMPARE(v.toBool(), toBool);
3211 QCOMPARE(qvariant_cast<T>(v), orig);
3215 if (!(QTypeInfo<T>::isStatic && QTypeInfo<T>::isComplex)) {
3216 // Type is movable so standard comparison algorithm in QVariant should work
3217 // In a custom type QVariant is not aware of ==operator so it won't be called,
3218 // which may cause problems especially visible when using a not-movable type
3221 QVERIFY(v2.isValid());
3222 QCOMPARE(v2.isNull(), isNull);
3223 QCOMPARE(v2.toString(), toString);
3224 QCOMPARE(v2.toDouble(), toDouble);
3225 QCOMPARE(v2.toBool(), toBool);
3226 QCOMPARE(qvariant_cast<T>(v2), orig);
3232 if (!(QTypeInfo<T>::isStatic && QTypeInfo<T>::isComplex)) {
3233 // Type is movable so standard comparison algorithm in QVariant should work
3234 // In a custom type QVariant is not aware of ==operator so it won't be called,
3235 // which may cause problems especially visible when using a not-movable type
3238 QCOMPARE(qvariant_cast<T>(v2), qvariant_cast<T>(v));
3239 QCOMPARE(v2.toString(), toString);
3240 v3 = QVariant::fromValue(orig);
3242 QVERIFY(v3.isValid());
3243 QCOMPARE(v3.isNull(), isNull);
3244 QCOMPARE(v3.toString(), toString);
3245 QCOMPARE(v3.toDouble(), toDouble);
3246 QCOMPARE(v3.toBool(), toBool);
3247 QCOMPARE(qvariant_cast<T>(v3), qvariant_cast<T>(v));
3250 QVERIFY(v.isValid());
3251 QCOMPARE(v.isNull(), isNull);
3252 QCOMPARE(v.toString(), toString);
3253 QCOMPARE(v.toDouble(), toDouble);
3254 QCOMPARE(v.toBool(), toBool);
3255 QCOMPARE(qvariant_cast<T>(v), orig);
3257 if (qMetaTypeId<T>() != qMetaTypeId<QVariant>()) {
3258 QCOMPARE(v.userType(), qMetaTypeId<T>());
3259 QCOMPARE(QVariant::typeToName(QVariant::Type(v.userType())), QMetaType::typeName(qMetaTypeId<T>()));
3263 #define PLAY_WITH_VARIANT(Orig, IsNull, ToString, ToDouble, ToBool) \
3264 playWithVariant(Orig, IsNull, ToString, ToDouble, ToBool);\
3265 if (QTest::currentTestFailed())\
3266 QFAIL("playWithVariant failed");
3271 bool operator==(const MyPrimitive &o) const
3273 return x == o.x && y == o.y;
3278 Q_DECLARE_TYPEINFO(MyPrimitive, Q_PRIMITIVE_TYPE);
3284 MyData() : ptr(this) {}
3287 if (ptr != this) qWarning("%s: object has moved", Q_FUNC_INFO);
3289 MyData(const MyData& o) : ptr(this)
3291 if (o.ptr != &o) qWarning("%s: other object has moved", Q_FUNC_INFO);
3293 MyData &operator=(const MyData &o)
3295 if (ptr != this) qWarning("%s: object has moved", Q_FUNC_INFO);
3296 if (o.ptr != &o) qWarning("%s: other object has moved", Q_FUNC_INFO);
3299 bool operator==(const MyData &o) const
3301 if (ptr != this) qWarning("%s: object has moved", Q_FUNC_INFO);
3302 if (o.ptr != &o) qWarning("%s: other object has moved", Q_FUNC_INFO);
3311 MyMovable() { v = count++; }
3312 ~MyMovable() { count--; }
3313 MyMovable(const MyMovable &o) : v(o.v) { count++; }
3315 bool operator==(const MyMovable &o) const
3321 int MyMovable::count = 0;
3327 MyNotMovable() : that(this) { count++; }
3328 ~MyNotMovable() { QCOMPARE(that, this); count--; }
3329 MyNotMovable(const MyNotMovable &o) : that(this) { QCOMPARE(o.that, &o); count++; }
3330 MyNotMovable &operator=(const MyNotMovable &o) {
3331 bool ok = that == this && o.that == &o;
3332 if (!ok) qFatal("MyNotMovable has been moved");
3336 //PLAY_WITH_VARIANT test that they are equal, but never that they are not equal
3337 // so it would be fine just to always return true
3338 bool operator==(const MyNotMovable &o) const
3340 bool ok = that == this && o.that == &o;
3341 if (!ok) qFatal("MyNotMovable has been moved");
3346 int MyNotMovable::count = 0;
3348 struct MyShared : QSharedData {
3353 Q_DECLARE_TYPEINFO(MyMovable, Q_MOVABLE_TYPE);
3356 Q_DECLARE_METATYPE(QList<QSize>)
3357 Q_DECLARE_METATYPE(MyPrimitive)
3358 Q_DECLARE_METATYPE(MyData)
3359 Q_DECLARE_METATYPE(MyMovable)
3360 Q_DECLARE_METATYPE(MyNotMovable)
3361 Q_DECLARE_METATYPE(QList<MyPrimitive>)
3362 Q_DECLARE_METATYPE(QList<MyData>)
3363 Q_DECLARE_METATYPE(QList<MyMovable>)
3364 Q_DECLARE_METATYPE(QList<MyNotMovable>)
3365 Q_DECLARE_METATYPE(MyPrimitive *)
3366 Q_DECLARE_METATYPE(MyData *)
3367 Q_DECLARE_METATYPE(MyMovable *)
3368 Q_DECLARE_METATYPE(MyNotMovable *)
3369 Q_DECLARE_METATYPE(QSharedDataPointer<MyShared>)
3372 void tst_QVariant::moreCustomTypes()
3375 QList<QSize> listSize;
3376 PLAY_WITH_VARIANT(listSize, false, QString(), 0, false);
3377 listSize << QSize(4,5) << QSize(89,23) << QSize(5,6);
3378 PLAY_WITH_VARIANT(listSize, false, QString(), 0, false);
3383 PLAY_WITH_VARIANT(str, true, QString(), 0, false);
3384 str = QString::fromLatin1("123456789.123");
3385 PLAY_WITH_VARIANT(str, false, str, 123456789.123, true);
3390 PLAY_WITH_VARIANT(size, false, QString(), 0, false);
3391 PLAY_WITH_VARIANT(QSize(45,78), false, QString(), 0, false);
3396 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3397 PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3399 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3400 l << MyData() << MyData();
3401 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3405 MyPrimitive d = { 4, 5 };
3406 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3407 PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3408 QList<MyPrimitive> l;
3409 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3411 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3416 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3417 PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3419 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3420 l << MyMovable() << d;
3421 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3423 QCOMPARE(MyMovable::count, 0);
3425 QCOMPARE(MyNotMovable::count, 0);
3428 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3429 PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3430 QList<MyNotMovable> l;
3431 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3432 l << MyNotMovable() << d;
3433 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3435 QCOMPARE(MyNotMovable::count, 0);
3438 PLAY_WITH_VARIANT(12.12, false, "12.12", 12.12, true);
3439 PLAY_WITH_VARIANT(12.12f, false, "12.12", 12.12f, true);
3440 PLAY_WITH_VARIANT('a', false, "a", 'a', true);
3441 PLAY_WITH_VARIANT((unsigned char)('a'), false, "a", 'a', true);
3442 PLAY_WITH_VARIANT( quint8(12), false, "\xc", 12, true);
3443 PLAY_WITH_VARIANT( qint8(13), false, "\xd", 13, true);
3444 PLAY_WITH_VARIANT(quint16(14), false, "14", 14, true);
3445 PLAY_WITH_VARIANT( qint16(15), false, "15", 15, true);
3446 PLAY_WITH_VARIANT(quint32(16), false, "16", 16, true);
3447 PLAY_WITH_VARIANT( qint32(17), false, "17", 17, true);
3448 PLAY_WITH_VARIANT(quint64(18), false, "18", 18, true);
3449 PLAY_WITH_VARIANT( qint64(19), false, "19", 19, true);
3450 PLAY_WITH_VARIANT( qint8(-12), false, QLatin1String("\xf4"), -12, true); // qint8 is char, so it gets converted via QChar
3451 PLAY_WITH_VARIANT( qint16(-13), false, "-13", -13, true);
3452 PLAY_WITH_VARIANT( qint32(-14), false, "-14", -14, true);
3453 PLAY_WITH_VARIANT( qint64(-15), false, "-15", -15, true);
3454 PLAY_WITH_VARIANT(quint64(0), false, "0", 0, false);
3455 PLAY_WITH_VARIANT( true, false, "true", 1, true);
3456 PLAY_WITH_VARIANT( false, false, "false", 0, false);
3458 PLAY_WITH_VARIANT(QString("hello\n"), false, "hello\n", 0, true);
3463 PLAY_WITH_VARIANT((void *)(&i), false, QString(), 0, false);
3464 PLAY_WITH_VARIANT((void *)(0), false, QString(), 0, false);
3468 QVariant v1 = QVariant::fromValue(5);
3469 QVariant v2 = QVariant::fromValue(5.0);
3470 QVariant v3 = QVariant::fromValue(quint16(5));
3472 QVariant v5 = QVariant::fromValue(MyPrimitive());
3473 QVariant v6 = QVariant::fromValue(MyMovable());
3474 QVariant v7 = QVariant::fromValue(MyData());
3475 PLAY_WITH_VARIANT(v1, false, "5", 5, true);
3476 PLAY_WITH_VARIANT(v2, false, "5", 5, true);
3477 PLAY_WITH_VARIANT(v3, false, "5", 5, true);
3478 PLAY_WITH_VARIANT(v4, false, "5", 5, true);
3480 PLAY_WITH_VARIANT(v5, false, QString(), 0, false);
3483 QCOMPARE(MyMovable::count, 0);
3485 QSharedDataPointer<MyShared> d(new MyShared);
3486 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3488 QCOMPARE(MyMovable::count, 0);
3491 QList<QList<int> > data;
3492 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3493 data << (QList<int>() << 42);
3494 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3498 QList<QVector<int> > data;
3499 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3500 data << (QVector<int>() << 42);
3501 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3505 QList<QSet<int> > data;
3506 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3507 data << (QSet<int>() << 42);
3508 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3512 QList<QLinkedList<int> > data;
3513 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3514 data << (QLinkedList<int>() << 42);
3515 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3519 void tst_QVariant::movabilityTest()
3521 // This test checks if QVariant is movable even if an internal data is not movable.
3522 QVERIFY(!MyNotMovable::count);
3524 QVariant variant = QVariant::fromValue(MyNotMovable());
3525 QVERIFY(MyNotMovable::count);
3527 // prepare destination memory space to which variant will be moved
3529 QCOMPARE(buffer[0].type(), QVariant::Invalid);
3530 buffer[0].~QVariant();
3532 memcpy(buffer, &variant, sizeof(QVariant));
3533 QCOMPARE(buffer[0].type(), QVariant::UserType);
3534 QCOMPARE(buffer[0].userType(), qMetaTypeId<MyNotMovable>());
3535 MyNotMovable tmp(buffer[0].value<MyNotMovable>());
3537 new (&variant) QVariant();
3539 QVERIFY(!MyNotMovable::count);
3542 void tst_QVariant::variantInVariant()
3545 QCOMPARE(var1.type(), QVariant::Int);
3546 QVariant var2 = var1;
3547 QCOMPARE(var2, var1);
3548 QCOMPARE(var2.type(), QVariant::Int);
3549 QVariant var3 = QVariant::fromValue(var1);
3550 QCOMPARE(var3, var1);
3551 QCOMPARE(var3.type(), QVariant::Int);
3552 QVariant var4 = qvariant_cast<QVariant>(var1);
3553 QCOMPARE(var4, var1);
3554 QCOMPARE(var4.type(), QVariant::Int);
3557 QCOMPARE(var5, var1);
3558 QCOMPARE(var5.type(), QVariant::Int);
3560 var6.setValue(var1);
3561 QCOMPARE(var6, var1);
3562 QCOMPARE(var6.type(), QVariant::Int);
3564 QCOMPARE(QVariant::fromValue(var1), QVariant::fromValue(var2));
3565 QCOMPARE(qvariant_cast<QVariant>(var3), QVariant::fromValue(var4));
3566 QCOMPARE(qvariant_cast<QVariant>(var5), qvariant_cast<QVariant>(var6));
3568 QString str("hello");
3569 QVariant var8 = qvariant_cast<QVariant>(QVariant::fromValue(QVariant::fromValue(str)));
3570 QCOMPARE((int)var8.type(), (int)QVariant::String);
3571 QCOMPARE(qvariant_cast<QString>(QVariant(qvariant_cast<QVariant>(var8))), str);
3573 QVariant var9(qMetaTypeId<QVariant>(), &var1);
3574 QCOMPARE(var9.userType(), qMetaTypeId<QVariant>());
3575 QCOMPARE(qvariant_cast<QVariant>(var9), var1);
3578 void tst_QVariant::colorInteger()
3580 QVariant v = QColor(Qt::red);
3581 QCOMPARE(v.type(), QVariant::Color);
3582 QCOMPARE(v.value<QColor>(), QColor(Qt::red));
3585 QCOMPARE(v.type(), QVariant::Int);
3586 QCOMPARE(v.toInt(), 1000);
3588 v.setValue(QColor(Qt::yellow));
3589 QCOMPARE(v.type(), QVariant::Color);
3590 QCOMPARE(v.value<QColor>(), QColor(Qt::yellow));
3594 Q_DECLARE_OPAQUE_POINTER(Forward*)
3595 Q_DECLARE_METATYPE(Forward*)
3597 void tst_QVariant::forwardDeclare()
3600 QVariant v = QVariant::fromValue(f);
3601 QCOMPARE(qvariant_cast<Forward*>(v), f);
3604 void tst_QVariant::loadQt5Stream_data()
3606 dataStream_data(QDataStream::Qt_5_0);
3609 void tst_QVariant::loadQt5Stream()
3611 loadQVariantFromDataStream(QDataStream::Qt_5_0);
3614 void tst_QVariant::saveQt5Stream_data()
3616 dataStream_data(QDataStream::Qt_5_0);
3619 void tst_QVariant::saveQt5Stream()
3621 saveQVariantFromDataStream(QDataStream::Qt_5_0);
3624 void tst_QVariant::loadQt4Stream_data()
3626 dataStream_data(QDataStream::Qt_4_9);
3629 void tst_QVariant::loadQt4Stream()
3631 loadQVariantFromDataStream(QDataStream::Qt_4_9);
3634 void tst_QVariant::saveQt4Stream_data()
3636 dataStream_data(QDataStream::Qt_4_9);
3639 void tst_QVariant::saveQt4Stream()
3641 saveQVariantFromDataStream(QDataStream::Qt_4_9);
3644 void tst_QVariant::dataStream_data(QDataStream::Version version)
3646 QTest::addColumn<QString>("fileName");
3650 case QDataStream::Qt_4_9:
3651 path = QString::fromLatin1("qt4.9");
3653 case QDataStream::Qt_5_0:
3654 path = QString::fromLatin1("qt5.0");
3660 path = path.prepend(":/stream/").append("/");
3663 foreach (const QFileInfo &fileInfo, dir.entryInfoList(QStringList() << "*.bin")) {
3664 QTest::newRow((path + fileInfo.fileName()).toLatin1()) << fileInfo.filePath();
3670 void tst_QVariant::loadQVariantFromDataStream(QDataStream::Version version)
3672 QFETCH(QString, fileName);
3674 QFile file(fileName);
3675 QVERIFY(file.open(QIODevice::ReadOnly));
3677 QDataStream stream(&file);
3678 stream.setVersion(version);
3681 QVariant loadedVariant;
3682 stream >> typeName >> loadedVariant;
3684 const int id = QMetaType::type(typeName.toLatin1());
3685 if (id == QMetaType::Void) {
3686 // Void type is not supported by QVariant
3690 QVariant constructedVariant(static_cast<QVariant::Type>(id));
3691 QCOMPARE(constructedVariant.userType(), id);
3692 QCOMPARE(QMetaType::typeName(loadedVariant.userType()), typeName.toLatin1().constData());
3693 QCOMPARE(loadedVariant.userType(), constructedVariant.userType());
3696 void tst_QVariant::saveQVariantFromDataStream(QDataStream::Version version)
3698 QFETCH(QString, fileName);
3700 QFile file(fileName);
3701 QVERIFY(file.open(QIODevice::ReadOnly));
3702 QDataStream dataFileStream(&file);
3705 dataFileStream >> typeName;
3706 QByteArray data = file.readAll();
3707 const int id = QMetaType::type(typeName.toLatin1());
3708 if (id == QMetaType::Void) {
3709 // Void type is not supported by QVariant
3714 buffer.open(QIODevice::ReadWrite);
3715 QDataStream stream(&buffer);
3716 stream.setVersion(version);
3718 QVariant constructedVariant(static_cast<QVariant::Type>(id));
3719 QCOMPARE(constructedVariant.userType(), id);
3720 stream << constructedVariant;
3722 // We are testing QVariant there is no point in testing full array.
3723 QCOMPARE(buffer.data().left(5), data.left(5));
3726 QVariant recunstructedVariant;
3727 stream >> recunstructedVariant;
3728 QCOMPARE(recunstructedVariant.userType(), constructedVariant.userType());
3731 class MessageHandler {
3733 MessageHandler(const int typeId, QtMsgHandler msgHandler = handler)
3734 : oldMsgHandler(qInstallMsgHandler(msgHandler))
3741 qInstallMsgHandler(oldMsgHandler);
3744 bool testPassed() const
3749 static void handler(QtMsgType, const char *txt)
3751 QString msg = QString::fromLatin1(txt);
3752 // Format itself is not important, but basic data as a type name should be included in the output
3753 ok = msg.startsWith("QVariant(");
3754 QVERIFY2(ok, (QString::fromLatin1("Message is not started correctly: '") + msg + '\'').toLatin1().constData());
3755 ok &= (currentId == QMetaType::UnknownType
3756 ? msg.contains("Invalid")
3757 : msg.contains(QMetaType::typeName(currentId)));
3758 QVERIFY2(ok, (QString::fromLatin1("Message doesn't contain type name: '") + msg + '\'').toLatin1().constData());
3759 if (currentId == QMetaType::Char || currentId == QMetaType::QChar) {
3760 // Chars insert '\0' into the qdebug stream, it is not possible to find a real string length
3763 if (QMetaType::typeFlags(currentId) & QMetaType::PointerToQObject) {
3764 QByteArray currentName = QMetaType::typeName(currentId);
3765 currentName.chop(1);
3766 ok &= (msg.contains(", " + currentName) || msg.contains(", 0x0"));
3768 ok &= msg.endsWith(") ");
3769 QVERIFY2(ok, (QString::fromLatin1("Message is not correctly finished: '") + msg + '\'').toLatin1().constData());
3773 QtMsgHandler oldMsgHandler;
3774 static int currentId;
3777 bool MessageHandler::ok;
3778 int MessageHandler::currentId;
3780 void tst_QVariant::debugStream_data()
3782 QTest::addColumn<QVariant>("variant");
3783 QTest::addColumn<int>("typeId");
3784 for (int id = 0; id < QMetaType::User; ++id) {
3785 const char *tagName = QMetaType::typeName(id);
3788 if (id != QMetaType::Void) {
3789 QTest::newRow(tagName) << QVariant(static_cast<QVariant::Type>(id)) << id;
3792 QTest::newRow("QBitArray(111)") << QVariant(QBitArray(3, true)) << qMetaTypeId<QBitArray>();
3793 QTest::newRow("CustomStreamableClass") << QVariant(qMetaTypeId<CustomStreamableClass>(), 0) << qMetaTypeId<CustomStreamableClass>();
3794 QTest::newRow("MyClass") << QVariant(qMetaTypeId<MyClass>(), 0) << qMetaTypeId<MyClass>();
3795 QTest::newRow("InvalidVariant") << QVariant() << int(QMetaType::UnknownType);
3796 QTest::newRow("CustomQObject") << QVariant::fromValue(this) << qMetaTypeId<tst_QVariant*>();
3799 void tst_QVariant::debugStream()
3801 QFETCH(QVariant, variant);
3802 QFETCH(int, typeId);
3804 MessageHandler msgHandler(typeId);
3805 qDebug() << variant;
3806 QVERIFY(msgHandler.testPassed());
3809 struct MessageHandlerType : public MessageHandler
3811 MessageHandlerType(const int typeId)
3812 : MessageHandler(typeId, handler)
3814 static void handler(QtMsgType, const char *txt)
3816 QString msg = QString::fromLatin1(txt);
3817 // Format itself is not important, but basic data as a type name should be included in the output
3818 ok = msg.startsWith("QVariant::");
3819 QVERIFY2(ok, (QString::fromLatin1("Message is not started correctly: '") + msg + '\'').toLatin1().constData());
3820 ok &= (currentId == QMetaType::UnknownType
3821 ? msg.contains("Invalid")
3822 : msg.contains(QMetaType::typeName(currentId)));
3823 QVERIFY2(ok, (QString::fromLatin1("Message doesn't contain type name: '") + msg + '\'').toLatin1().constData());
3827 void tst_QVariant::debugStreamType_data()
3832 void tst_QVariant::debugStreamType()
3834 QFETCH(QVariant, variant);
3835 QFETCH(int, typeId);
3837 MessageHandlerType msgHandler(typeId);
3838 qDebug() << QVariant::Type(typeId);
3839 QVERIFY(msgHandler.testPassed());
3842 void tst_QVariant::guiVariantAtExit()
3844 // crash test, it should not crash at QGuiApplication exit
3845 static QVariant cursor = QCursor();
3846 static QVariant point = QPoint();
3847 static QVariant image = QImage();
3848 static QVariant pallete = QPalette();
3856 void tst_QVariant::widgetsVariantAtExit()
3858 // crash test, it should not crash at QGuiApplication exit
3859 static QVariant icon= QIcon();
3860 static QVariant sizePolicy = QSizePolicy();
3862 Q_UNUSED(sizePolicy);
3866 QTEST_MAIN(tst_QVariant)
3867 #include "tst_qvariant.moc"