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();
108 void canConvert_data();
141 void toLongLong_data();
144 void toULongLong_data();
147 void toByteArray_data();
150 void toString_data();
159 void toDateTime_data();
162 void toDouble_data();
165 void toPointF_data();
171 void toKeySequence_data();
172 void toKeySequence();
180 void toPixmap_data();
189 void qvariant_cast_QObject_data();
190 void qvariant_cast_QObject();
191 void qvariant_cast_QObject_derived();
196 void toRegularExpression();
211 void basicUserType();
215 void writeToReadFromDataStream_data();
216 void writeToReadFromDataStream();
217 void writeToReadFromOldDataStream();
218 void checkDataStream();
220 void operator_eq_eq_data();
221 void operator_eq_eq();
223 void operator_eq_eq_rhs();
225 void typeName_data();
229 void streamInvalidVariant();
236 void saveLoadCustomTypes();
241 void convertToQUint8() const;
242 void invalidQColor() const;
243 void comparePointers() const;
244 void voidStar() const;
245 void dataStar() const;
246 void canConvertQStringList() const;
247 void canConvertQStringList_data() const;
248 void canConvertMetaTypeToInt() const;
249 void variantToDateTimeWithoutWarnings() const;
250 void invalidDateTime() const;
252 void loadUnknownUserType();
253 void loadBrokenUserType();
255 void invalidDate() const;
256 void compareCustomTypes() const;
257 void timeToDateTime() const;
258 void copyingUserTypes() const;
259 void convertBoolToByteArray() const;
260 void convertBoolToByteArray_data() const;
261 void convertByteArrayToBool() const;
262 void convertByteArrayToBool_data() const;
263 void toIntFromQString() const;
264 void toIntFromDouble() const;
267 void numericalConvert();
268 void moreCustomTypes();
269 void movabilityTest();
270 void variantInVariant();
274 void forwardDeclare();
275 void debugStream_data();
277 void debugStreamType_data();
278 void debugStreamType();
280 void loadQt4Stream_data();
281 void loadQt4Stream();
282 void saveQt4Stream_data();
283 void saveQt4Stream();
284 void loadQt5Stream_data();
285 void loadQt5Stream();
286 void saveQt5Stream_data();
287 void saveQt5Stream();
289 void guiVariantAtExit();
290 void widgetsVariantAtExit();
292 void dataStream_data(QDataStream::Version version);
293 void loadQVariantFromDataStream(QDataStream::Version version);
294 void saveQVariantFromDataStream(QDataStream::Version version);
296 CustomNonQObject *customNonQObjectPointer;
297 QVector<QObject*> objectPointerTestData;
300 Q_DECLARE_METATYPE(QDate)
301 Q_DECLARE_METATYPE(QTime)
302 Q_DECLARE_METATYPE(QDateTime)
303 Q_DECLARE_METATYPE(QVariant)
305 const qlonglong intMax1 = (qlonglong)INT_MAX + 1;
306 const qulonglong uintMax1 = (qulonglong)UINT_MAX + 1;
308 void tst_QVariant::constructor()
311 QVERIFY( !variant.isValid() );
312 QVERIFY( variant.isNull() );
314 QVariant var2(variant);
315 QVERIFY( !var2.isValid() );
316 QVERIFY( variant.isNull() );
318 QVariant varll(intMax1);
319 QVariant varll2(varll);
320 QCOMPARE(varll2, varll);
322 QVariant var3(QVariant::String);
323 QCOMPARE(var3.typeName(), "QString");
324 QVERIFY(var3.isNull());
325 QVERIFY(var3.isValid());
327 QVariant var4(QVariant::Invalid);
328 QCOMPARE(var4.type(), QVariant::Invalid);
329 QVERIFY(var4.isNull());
330 QVERIFY(!var4.isValid());
332 QVariant var5(QLatin1String("hallo"));
333 QCOMPARE(var5.type(), QVariant::String);
334 QCOMPARE(var5.typeName(), "QString");
336 QVariant var6(qlonglong(0));
337 QCOMPARE(var6.type(), QVariant::LongLong);
338 QCOMPARE(var6.typeName(), "qlonglong");
341 QVERIFY(var7.isValid());
342 QVERIFY(!var7.isNull());
344 var8.setValue<int>(5);
345 QVERIFY(var8.isValid());
346 QVERIFY(!var8.isNull());
349 void tst_QVariant::copy_constructor()
351 QVariant var7(QVariant::Int);
353 QCOMPARE(var8.type(), QVariant::Int);
354 QVERIFY(var8.isNull());
357 void tst_QVariant::isNull()
360 QVERIFY( var.isNull() );
363 QVariant var1( str1 );
364 QVERIFY( var1.isNull() );
366 QVariant var2( QString::null );
367 QVERIFY( var2.isNull() );
369 QVariant var3( QString( "blah" ) );
370 QVERIFY( !var3.isNull() );
373 QVERIFY( !var4.isNull() );
375 QVariant var5 = QString();
376 QVERIFY( var5.isNull() );
378 QVariant var6( QString( "blah" ) );
379 QVERIFY( !var6.isNull() );
381 QVERIFY( var6.isNull() );
382 var6.convert( QVariant::String );
383 QVERIFY( var6.isNull() );
384 QVariant varLL( (qlonglong)0 );
385 QVERIFY( !varLL.isNull() );
386 QVariant var7(QString::null);
387 QVERIFY(var7.isNull());
390 void tst_QVariant::swap()
392 QVariant v1 = 1, v2 = 2.0;
394 QCOMPARE(v1.type(),QVariant::Double);
395 QCOMPARE(v1.toDouble(),2.0);
396 QCOMPARE(v2.type(),QVariant::Int);
397 QCOMPARE(v2.toInt(),1);
400 void tst_QVariant::canConvert_data()
402 QTest::addColumn<QVariant>("val");
403 QTest::addColumn<bool>("BitArrayCast");
404 QTest::addColumn<bool>("BitmapCast");
405 QTest::addColumn<bool>("BoolCast");
406 QTest::addColumn<bool>("BrushCast");
407 QTest::addColumn<bool>("ByteArrayCast");
408 QTest::addColumn<bool>("ColorCast");
409 QTest::addColumn<bool>("CursorCast");
410 QTest::addColumn<bool>("DateCast");
411 QTest::addColumn<bool>("DateTimeCast");
412 QTest::addColumn<bool>("DoubleCast");
413 QTest::addColumn<bool>("FontCast");
414 QTest::addColumn<bool>("ImageCast");
415 QTest::addColumn<bool>("IntCast");
416 QTest::addColumn<bool>("InvalidCast");
417 QTest::addColumn<bool>("KeySequenceCast");
418 QTest::addColumn<bool>("ListCast");
419 QTest::addColumn<bool>("LongLongCast");
420 QTest::addColumn<bool>("MapCast");
421 QTest::addColumn<bool>("PaletteCast");
422 QTest::addColumn<bool>("PenCast");
423 QTest::addColumn<bool>("PixmapCast");
424 QTest::addColumn<bool>("PointCast");
425 QTest::addColumn<bool>("RectCast");
426 QTest::addColumn<bool>("RegionCast");
427 QTest::addColumn<bool>("SizeCast");
428 QTest::addColumn<bool>("SizePolicyCast");
429 QTest::addColumn<bool>("StringCast");
430 QTest::addColumn<bool>("StringListCast");
431 QTest::addColumn<bool>("TimeCast");
432 QTest::addColumn<bool>("UIntCast");
433 QTest::addColumn<bool>("ULongLongCast");
444 // 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
447 QVariant var(QBitArray(0));
448 QTest::newRow("BitArray")
449 << 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;
450 var = qVariantFromValue(QBitmap());
451 QTest::newRow("Bitmap")
452 << 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;
453 var = qVariantFromValue(QBrush());
454 QTest::newRow("Brush")
455 << 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;
456 var = QVariant(QByteArray());
457 QTest::newRow("ByteArray")
458 << 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;
459 var = qVariantFromValue(QColor());
460 QTest::newRow("Color")
461 << 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;
463 var = qVariantFromValue(QCursor());
464 QTest::newRow("Cursor")
465 << 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;
467 var = QVariant(QDate());
468 QTest::newRow("Date")
469 << 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;
470 var = QVariant(QDateTime());
471 QTest::newRow("DateTime")
472 << 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;
473 var = QVariant((double)0.1);
474 QTest::newRow("Double")
475 << 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;
476 var = QVariant(0.1f);
477 QTest::newRow("Float")
478 << 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;
479 var = qVariantFromValue(QFont());
480 QTest::newRow("Font")
481 << 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;
482 var = qVariantFromValue(QIcon());
483 QTest::newRow("Icon")
484 << 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;
485 var = qVariantFromValue(QImage());
486 QTest::newRow("Image")
487 << 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;
488 var = QVariant((int)1);
490 << 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;
492 QTest::newRow("Invalid")
493 << 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;
494 var = qVariantFromValue(QKeySequence());
495 QTest::newRow("KeySequence")
496 << 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;
497 var = QVariant(QList<QVariant>());
498 QTest::newRow("List")
499 << 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;
500 var = QVariant((qlonglong)1);
501 QTest::newRow("LongLong")
502 << 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;
503 var = QVariant(QMap<QString,QVariant>());
505 << 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;
506 var = qVariantFromValue(QPalette());
507 QTest::newRow("Palette")
508 << 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;
509 var = qVariantFromValue(QPen());
511 << 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;
512 var = qVariantFromValue(QPixmap());
513 QTest::newRow("Pixmap")
514 << 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;
515 var = qVariantFromValue(QPolygon());
516 QTest::newRow("PointArray")
517 << 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;
518 var = QVariant(QPoint());
519 QTest::newRow("Point")
520 << 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;
521 var = QVariant(QRect());
522 QTest::newRow("Rect")
523 << 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;
524 var = qVariantFromValue(QRegion());
525 QTest::newRow("Region")
526 << 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;
527 var = QVariant(QSize());
528 QTest::newRow("Size")
529 << 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;
530 var = qVariantFromValue(QSizePolicy());
531 QTest::newRow("SizePolicy")
532 << 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;
533 var = QVariant(QString());
534 QTest::newRow("String")
535 << 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;
536 var = QVariant(QStringList("entry"));
537 QTest::newRow("StringList")
538 << 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;
539 var = QVariant(QTime());
540 QTest::newRow("Time")
541 << 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;
542 var = QVariant((uint)1);
543 QTest::newRow("UInt")
544 << 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;
545 var = QVariant((qulonglong)1);
546 QTest::newRow("ULongLong")
547 << 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;
553 void tst_QVariant::canConvert()
555 QFETCH(QVariant, val);
556 QFETCH(bool, BitArrayCast);
557 QFETCH(bool, BitmapCast);
558 QFETCH(bool, BoolCast);
559 QFETCH(bool, BrushCast);
560 QFETCH(bool, ByteArrayCast);
561 QFETCH(bool, ColorCast);
562 QFETCH(bool, CursorCast);
563 QFETCH(bool, DateCast);
564 QFETCH(bool, DateTimeCast);
565 QFETCH(bool, DoubleCast);
566 QFETCH(bool, FontCast);
567 QFETCH(bool, ImageCast);
568 QFETCH(bool, IntCast);
569 QFETCH(bool, InvalidCast);
570 QFETCH(bool, KeySequenceCast);
571 QFETCH(bool, ListCast);
572 QFETCH(bool, LongLongCast);
573 QFETCH(bool, MapCast);
574 QFETCH(bool, PaletteCast);
575 QFETCH(bool, PenCast);
576 QFETCH(bool, PixmapCast);
577 QFETCH(bool, PointCast);
578 QFETCH(bool, RectCast);
579 QFETCH(bool, RegionCast);
580 QFETCH(bool, SizeCast);
581 QFETCH(bool, SizePolicyCast);
582 QFETCH(bool, StringCast);
583 QFETCH(bool, StringListCast);
584 QFETCH(bool, TimeCast);
585 QFETCH(bool, UIntCast);
586 QFETCH(bool, ULongLongCast);
588 QCOMPARE(val.canConvert(QVariant::BitArray), BitArrayCast);
589 QCOMPARE(val.canConvert(QVariant::Bitmap), BitmapCast);
590 QCOMPARE(val.canConvert(QVariant::Bool), BoolCast);
591 QCOMPARE(val.canConvert(QVariant::Brush), BrushCast);
592 QCOMPARE(val.canConvert(QVariant::ByteArray), ByteArrayCast);
593 QCOMPARE(val.canConvert(QVariant::Color), ColorCast);
594 QCOMPARE(val.canConvert(QVariant::Cursor), CursorCast);
595 QCOMPARE(val.canConvert(QVariant::Date), DateCast);
596 QCOMPARE(val.canConvert(QVariant::DateTime), DateTimeCast);
597 QCOMPARE(val.canConvert(QVariant::Double), DoubleCast);
598 QCOMPARE(val.canConvert(QVariant::Type(QMetaType::Float)), DoubleCast);
599 QCOMPARE(val.canConvert(QVariant::Font), FontCast);
600 QCOMPARE(val.canConvert(QVariant::Image), ImageCast);
601 QCOMPARE(val.canConvert(QVariant::Int), IntCast);
602 QCOMPARE(val.canConvert(QVariant::Invalid), InvalidCast);
603 QCOMPARE(val.canConvert(QVariant::KeySequence), KeySequenceCast);
604 QCOMPARE(val.canConvert(QVariant::List), ListCast);
605 QCOMPARE(val.canConvert(QVariant::LongLong), LongLongCast);
606 QCOMPARE(val.canConvert(QVariant::Map), MapCast);
607 QCOMPARE(val.canConvert(QVariant::Palette), PaletteCast);
608 QCOMPARE(val.canConvert(QVariant::Pen), PenCast);
609 QCOMPARE(val.canConvert(QVariant::Pixmap), PixmapCast);
610 QCOMPARE(val.canConvert(QVariant::Point), PointCast);
611 QCOMPARE(val.canConvert(QVariant::Rect), RectCast);
612 QCOMPARE(val.canConvert(QVariant::Region), RegionCast);
613 QCOMPARE(val.canConvert(QVariant::Size), SizeCast);
614 QCOMPARE(val.canConvert(QVariant::SizePolicy), SizePolicyCast);
615 QCOMPARE(val.canConvert(QVariant::String), StringCast);
616 QCOMPARE(val.canConvert(QVariant::StringList), StringListCast);
617 QCOMPARE(val.canConvert(QVariant::Time), TimeCast);
618 QCOMPARE(val.canConvert(QVariant::UInt), UIntCast);
619 QCOMPARE(val.canConvert(QVariant::ULongLong), ULongLongCast);
622 QCOMPARE(val.canConvert(-1), false);
623 QCOMPARE(val.canConvert(-23), false);
624 QCOMPARE(val.canConvert(-23876), false);
625 QCOMPARE(val.canConvert(23876), false);
628 void tst_QVariant::toInt_data()
630 QTest::addColumn<QVariant>("value");
631 QTest::addColumn<int>("result");
632 QTest::addColumn<bool>("valueOK");
634 QTest::newRow( "invalid" ) << QVariant() << 0 << false;
635 QTest::newRow( "int" ) << QVariant( 123 ) << 123 << true;
636 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << 3 << true;
637 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << 3 << true;
638 QTest::newRow( "uint" ) << QVariant( 123u ) << 123 << true;
639 QTest::newRow( "int-string" ) << QVariant( QString("123") ) << 123 << true;
640 QTest::newRow( "string" ) << QVariant( QString("Unicode String") ) << 0 << false;
641 QTest::newRow( "longlong0" ) << QVariant( (qlonglong)34 ) << 34 << true;
642 QTest::newRow( "longlong1" ) << QVariant( intMax1 ) << (int)INT_MIN << true;
643 QTest::newRow( "ulonglong0" ) << QVariant( (qulonglong)34 ) << 34 << true;
644 QTest::newRow( "ulonglong1" ) << QVariant( uintMax1 ) << 0 << true;
645 QTest::newRow( "signedint" ) << QVariant( -123 ) << -123 << true;
646 QTest::newRow( "signeddouble" ) << QVariant( -3.1415927 ) << -3 << true;
647 QTest::newRow( "signedfloat" ) << QVariant( -3.1415927f ) << -3 << true;
648 QTest::newRow( "signedint-string" ) << QVariant( QString("-123") ) << -123 << true;
649 QTest::newRow( "signedlonglong0" ) << QVariant( (qlonglong)-34 ) << -34 << true;
650 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << int('a') << true;
651 QTest::newRow( "keysequence" ) << qVariantFromValue( QKeySequence( Qt::Key_A ) ) << 65 << true;
652 QByteArray bytearray(4, ' ');
657 QTest::newRow( "QByteArray1" ) << QVariant( bytearray ) << 0 << false;
662 QTest::newRow( "QByteArray2" ) << QVariant( bytearray ) << 4500 << true;
665 void tst_QVariant::toInt()
667 QFETCH( QVariant, value );
668 QFETCH( int, result );
669 QFETCH( bool, valueOK );
670 QVERIFY( value.isValid() == value.canConvert( QVariant::Int ) );
672 int i = value.toInt( &ok );
673 QCOMPARE( i, result );
674 QVERIFY( ok == valueOK );
677 void tst_QVariant::toUInt_data()
679 QTest::addColumn<QVariant>("value");
680 QTest::addColumn<uint>("result");
681 QTest::addColumn<bool>("valueOK");
683 QTest::newRow( "int" ) << QVariant( 123 ) << (uint)123 << true;
684 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (uint)3 << true;
685 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (uint)3 << true;
686 QTest::newRow( "uint" ) << QVariant( 123u ) << (uint)123 << true;
687 QTest::newRow( "int-string" ) << QVariant( QString("123") ) << (uint)123 << true;
688 QTest::newRow( "string" ) << QVariant( QString("Unicode String") ) << (uint)0 << false;
689 QTest::newRow( "string2" ) << QVariant( QString("4") ) << (uint)4 << true;
690 QTest::newRow( "longlong0" ) << QVariant( (qlonglong)34 ) << (uint)34 << true;
691 QTest::newRow( "longlong1" ) << QVariant( intMax1 ) << (uint)INT_MIN << true;
692 QTest::newRow( "ulonglong0" ) << QVariant( (qulonglong)34 ) << (uint)34 << true;
693 QTest::newRow( "ulonglong1" ) << QVariant( uintMax1 ) << (uint)0 << true;
694 QTest::newRow( "negativeint" ) << QVariant( -123 ) << (uint)-123 << true;
695 QTest::newRow( "negativedouble" ) << QVariant( -3.1415927 ) << (uint)-3 << true;
696 QTest::newRow( "negativefloat" ) << QVariant( -3.1415927f ) << (uint)-3 << true;
697 QTest::newRow( "negativeint-string" ) << QVariant( QString("-123") ) << (uint)0 << false;
698 QTest::newRow( "negativelonglong0" ) << QVariant( (qlonglong)-34 ) << (uint)-34 << true;
699 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << uint('a') << true;
700 QByteArray bytearray(4, ' ');
705 QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (uint)4321 << true;
708 void tst_QVariant::toUInt()
710 QFETCH( QVariant, value );
711 QFETCH( uint, result );
712 QFETCH( bool, valueOK );
713 QVERIFY( value.isValid() );
714 QVERIFY( value.canConvert( QVariant::UInt ) );
717 uint i = value.toUInt( &ok );
718 QVERIFY( ok == valueOK );
719 QCOMPARE( i, result );
723 void tst_QVariant::toSize_data()
725 QTest::addColumn<QVariant>("value");
726 QTest::addColumn<QSize>("result");
727 QTest::newRow( "qsizef4" ) << QVariant( QSizeF(4, 2) ) << QSize(4, 2);
728 QTest::newRow( "qsizef1" ) << QVariant( QSizeF(0, 0) ) << QSize(0, 0);
729 QTest::newRow( "qsizef2" ) << QVariant( QSizeF(-5, -1) ) << QSize(-5, -1);
730 QTest::newRow( "qsizef3" ) << QVariant( QSizeF() ) << QSize();
733 void tst_QVariant::toSize()
735 QFETCH( QVariant, value );
736 QFETCH( QSize, result );
737 QVERIFY( value.isValid() );
738 QVERIFY( value.canConvert( QVariant::Size ) );
740 QSize i = value.toSize();
741 QCOMPARE( i, result );
744 void tst_QVariant::toSizeF_data()
746 QTest::addColumn<QVariant>("value");
747 QTest::addColumn<QSizeF>("result");
748 QTest::newRow( "qsize1" ) << QVariant( QSize(0, 0) ) << QSizeF(0, 0);
749 QTest::newRow( "qsize2" ) << QVariant( QSize(-5, -1) ) << QSizeF(-5, -1);
750 QTest::newRow( "qsize3" ) << QVariant( QSize() ) << QSizeF();
751 QTest::newRow( "qsize4" ) << QVariant(QSize(4,2)) << QSizeF(4,2);
754 void tst_QVariant::toSizeF()
756 QFETCH( QVariant, value );
757 QFETCH( QSizeF, result );
758 QVERIFY( value.isValid() );
759 QVERIFY( value.canConvert( QVariant::SizeF ) );
761 QSizeF i = value.toSizeF();
762 QCOMPARE( i, result );
765 void tst_QVariant::toLine_data()
767 QTest::addColumn<QVariant>("value");
768 QTest::addColumn<QLine>("result");
769 QTest::newRow( "linef1" ) << QVariant( QLineF(1, 2, 3, 4) ) << QLine(1, 2, 3, 4);
770 QTest::newRow( "linef2" ) << QVariant( QLineF(-1, -2, -3, -4) ) << QLine(-1, -2, -3, -4);
771 QTest::newRow( "linef3" ) << QVariant( QLineF(0, 0, 0, 0) ) << QLine(0, 0, 0, 0);
772 QTest::newRow( "linef4" ) << QVariant( QLineF() ) << QLine();
775 void tst_QVariant::toLine()
777 QFETCH( QVariant, value );
778 QFETCH( QLine, result );
779 QVERIFY( value.isValid() );
780 QVERIFY( value.canConvert( QVariant::Line ) );
782 QLine i = value.toLine();
783 QCOMPARE( i, result );
786 void tst_QVariant::toLineF_data()
788 QTest::addColumn<QVariant>("value");
789 QTest::addColumn<QLineF>("result");
790 QTest::newRow( "line1" ) << QVariant( QLine(-1, -2, -3, -4) ) << QLineF(-1, -2, -3, -4);
791 QTest::newRow( "line2" ) << QVariant( QLine(0, 0, 0, 0) ) << QLineF(0, 0, 0, 0);
792 QTest::newRow( "line3" ) << QVariant( QLine() ) << QLineF();
793 QTest::newRow( "line4" ) << QVariant( QLine(1, 2, 3, 4) ) << QLineF(1, 2, 3, 4);
796 void tst_QVariant::toLineF()
798 QFETCH( QVariant, value );
799 QFETCH( QLineF, result );
800 QVERIFY( value.isValid() );
801 QVERIFY( value.canConvert( QVariant::LineF ) );
803 QLineF i = value.toLineF();
804 QCOMPARE( i, result );
807 void tst_QVariant::toPoint_data()
809 QTest::addColumn<QVariant>("value");
810 QTest::addColumn<QPoint>("result");
811 QTest::newRow( "pointf1" ) << QVariant( QPointF(4, 2) ) << QPoint(4, 2);
812 QTest::newRow( "pointf2" ) << QVariant( QPointF(0, 0) ) << QPoint(0, 0);
813 QTest::newRow( "pointf3" ) << QVariant( QPointF(-4, -2) ) << QPoint(-4, -2);
814 QTest::newRow( "pointf4" ) << QVariant( QPointF() ) << QPoint();
815 QTest::newRow( "pointf5" ) << QVariant( QPointF(-4.2f, -2.3f) ) << QPoint(-4, -2);
818 void tst_QVariant::toPoint()
820 QFETCH( QVariant, value );
821 QFETCH( QPoint, result );
822 QVERIFY( value.isValid() );
823 QVERIFY( value.canConvert( QVariant::Point ) );
824 QPoint i = value.toPoint();
825 QCOMPARE( i, result );
828 void tst_QVariant::toRect_data()
830 QTest::addColumn<QVariant>("value");
831 QTest::addColumn<QRect>("result");
832 QTest::newRow( "rectf1" ) << QVariant(QRectF(1, 2, 3, 4)) << QRect(1, 2, 3, 4);
833 QTest::newRow( "rectf2" ) << QVariant(QRectF(0, 0, 0, 0)) << QRect(0, 0, 0, 0);
834 QTest::newRow( "rectf3" ) << QVariant(QRectF(-1, -2, -3, -4)) << QRect(-1, -2, -3, -4);
835 QTest::newRow( "rectf4" ) << QVariant(QRectF(-1.3f, 0, 3.9f, -4.0)) << QRect(-1, 0, 4, -4);
836 QTest::newRow( "rectf5" ) << QVariant(QRectF()) << QRect();
839 void tst_QVariant::toRect()
841 QFETCH( QVariant, value );
842 QFETCH( QRect, result );
843 QVERIFY( value.isValid() );
844 QVERIFY( value.canConvert( QVariant::Rect ) );
845 QRect i = value.toRect();
846 QCOMPARE( i, result );
849 void tst_QVariant::toChar_data()
851 QTest::addColumn<QVariant>("value");
852 QTest::addColumn<QChar>("result");
853 QTest::newRow( "longlong" ) << QVariant(qlonglong('6')) << QChar('6');
854 QTest::newRow( "ulonglong" ) << QVariant(qulonglong('7')) << QChar('7');
857 void tst_QVariant::toChar()
859 QFETCH( QVariant, value );
860 QFETCH( QChar, result );
861 QVERIFY( value.isValid() );
862 QVERIFY( value.canConvert( QVariant::Char ) );
864 QChar i = value.toChar();
865 QCOMPARE( i, result );
868 void tst_QVariant::toBool_data()
870 QTest::addColumn<QVariant>("value");
871 QTest::addColumn<bool>("result");
873 QTest::newRow( "int0" ) << QVariant( 0 ) << false;
874 QTest::newRow( "int1" ) << QVariant( 123 ) << true;
875 QTest::newRow( "uint0" ) << QVariant( 0u ) << false;
876 QTest::newRow( "uint1" ) << QVariant( 123u ) << true;
877 QTest::newRow( "double0" ) << QVariant( 0.0 ) << false;
878 QTest::newRow( "float0" ) << QVariant( 0.0f ) << false;
879 QTest::newRow( "double1" ) << QVariant( 3.1415927 ) << true;
880 QTest::newRow( "float1" ) << QVariant( 3.1415927f ) << true;
881 QTest::newRow( "string0" ) << QVariant( QString("3") ) << true;
882 QTest::newRow( "string1" ) << QVariant( QString("true") ) << true;
883 QTest::newRow( "string2" ) << QVariant( QString("0") ) << false;
884 QTest::newRow( "string3" ) << QVariant( QString("fAlSe") ) << false;
885 QTest::newRow( "longlong0" ) << QVariant( (qlonglong)0 ) << false;
886 QTest::newRow( "longlong1" ) << QVariant( (qlonglong)1 ) << true;
887 QTest::newRow( "ulonglong0" ) << QVariant( (qulonglong)0 ) << false;
888 QTest::newRow( "ulonglong1" ) << QVariant( (qulonglong)1 ) << true;
889 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << true;
890 QTest::newRow( "Null_QChar" ) << QVariant(QChar(0)) << false;
893 void tst_QVariant::toBool()
895 QFETCH( QVariant, value );
896 QFETCH( bool, result );
897 QVERIFY( value.isValid() );
898 QVERIFY( value.canConvert( QVariant::Bool ) );
900 bool i = value.toBool();
901 QCOMPARE( i, result );
904 void tst_QVariant::toPointF_data()
906 QTest::addColumn<QVariant>("value");
907 QTest::addColumn<QPointF>("result");
909 QTest::newRow( "QPoint" ) << QVariant( QPointF( 19, 84) ) << QPointF( 19, 84 );
912 void tst_QVariant::toPointF()
914 QFETCH( QVariant, value );
915 QFETCH( QPointF, result );
916 QVERIFY( value.isValid() );
917 QVERIFY( value.canConvert( QVariant::PointF ) );
918 QPointF d = value.toPointF();
919 QCOMPARE( d, result );
922 void tst_QVariant::toRectF_data()
924 QTest::addColumn<QVariant>("value");
925 QTest::addColumn<QRectF>("result");
927 QRect r( 1, 9, 8, 4 );
928 QRectF rf( 1.0, 9.0, 8.0, 4.0 );
929 QTest::newRow( "QRect" ) << QVariant( r ) << rf;
932 void tst_QVariant::toRectF()
934 QFETCH( QVariant, value );
935 QFETCH( QRectF, result );
936 QVERIFY( value.isValid() );
937 QVERIFY( value.canConvert( QVariant::RectF ) );
938 QRectF d = value.toRectF();
939 QCOMPARE( d, result );
942 void tst_QVariant::toColor_data()
944 QTest::addColumn<QVariant>("value");
945 QTest::addColumn<QColor>("result");
948 QTest::newRow( "string" ) << QVariant( QString( "red" ) ) << c;
949 QTest::newRow( "solid brush" ) << QVariant( QBrush(c) ) << c;
952 void tst_QVariant::toColor()
954 QFETCH( QVariant, value );
955 QFETCH( QColor, result );
956 QVERIFY( value.isValid() );
957 QVERIFY( value.canConvert( QVariant::Color ) );
958 QColor d = qVariantValue<QColor>(value);
959 QCOMPARE( d, result );
962 void tst_QVariant::toPixmap_data()
964 QTest::addColumn<QVariant>("value");
965 QTest::addColumn<QPixmap>("result");
969 QTest::newRow( "image" ) << QVariant( pm ) << pm;
973 QTest::newRow( "bitmap" ) << QVariant( bm ) << QPixmap(bm);
976 void tst_QVariant::toPixmap()
978 QFETCH( QVariant, value );
979 QFETCH( QPixmap, result );
980 QVERIFY( value.isValid() );
981 QVERIFY( value.canConvert( QVariant::Pixmap ) );
982 QPixmap d = qVariantValue<QPixmap>(value);
983 QCOMPARE( d, result );
986 void tst_QVariant::toImage_data()
988 QTest::addColumn<QVariant>("value");
989 QTest::addColumn<QImage>("result");
991 QImage im(30, 30, QImage::Format_ARGB32);
993 QTest::newRow( "image" ) << QVariant( im ) << im;
996 void tst_QVariant::toImage()
998 QFETCH( QVariant, value );
999 QFETCH( QImage, result );
1000 QVERIFY( value.isValid() );
1001 QVERIFY( value.canConvert( QVariant::Image ) );
1002 QImage d = qVariantValue<QImage>(value);
1003 QCOMPARE( d, result );
1006 void tst_QVariant::toBrush_data()
1008 QTest::addColumn<QVariant>("value");
1009 QTest::addColumn<QBrush>("result");
1012 QTest::newRow( "color" ) << QVariant( c ) << QBrush(c);
1015 QTest::newRow( "pixmap" ) << QVariant( pm ) << QBrush(pm);
1018 void tst_QVariant::toBrush()
1020 QFETCH( QVariant, value );
1021 QFETCH( QBrush, result );
1022 QVERIFY( value.isValid() );
1023 QVERIFY( value.canConvert( QVariant::Brush ) );
1024 QBrush d = qVariantValue<QBrush>(value);
1025 QCOMPARE( d, result );
1028 void tst_QVariant::toFont_data()
1030 QTest::addColumn<QVariant>("value");
1031 QTest::addColumn<QFont>("result");
1033 QFont f("times",12,-1,false);
1034 QTest::newRow( "string" ) << QVariant( QString( "times,12,-1,5,50,0,0,0,0,0" ) ) << f;
1037 void tst_QVariant::toFont()
1039 QFETCH( QVariant, value );
1040 QFETCH( QFont, result );
1041 QVERIFY( value.isValid() );
1042 QVERIFY( value.canConvert( QVariant::Font ) );
1043 QFont d = qVariantValue<QFont>(value);
1044 QCOMPARE( d, result );
1047 void tst_QVariant::toKeySequence_data()
1049 QTest::addColumn<QVariant>("value");
1050 QTest::addColumn<QKeySequence>("result");
1053 QTest::newRow( "int" ) << QVariant( 67108929 ) << QKeySequence( Qt::CTRL + Qt::Key_A );
1056 QTest::newRow( "qstring" )
1057 << QVariant( QString( "Ctrl+A" ) )
1058 << QKeySequence( Qt::CTRL + Qt::Key_A );
1061 void tst_QVariant::toKeySequence()
1063 QFETCH( QVariant, value );
1064 QFETCH( QKeySequence, result );
1065 QVERIFY( value.isValid() );
1066 QVERIFY( value.canConvert( QVariant::KeySequence ) );
1067 QKeySequence d = qVariantValue<QKeySequence>(value);
1068 QCOMPARE( d, result );
1071 void tst_QVariant::toDouble_data()
1073 QTest::addColumn<QVariant>("value");
1074 QTest::addColumn<double>("result");
1075 QTest::addColumn<bool>("valueOK");
1077 QByteArray bytearray(4, ' ');
1082 QTest::newRow( "bytearray" ) << QVariant( bytearray ) << 32.1 << true;
1085 void tst_QVariant::toDouble()
1087 QFETCH( QVariant, value );
1088 QFETCH( double, result );
1089 QFETCH( bool, valueOK );
1090 QVERIFY( value.isValid() );
1091 QVERIFY( value.canConvert( QVariant::Double ) );
1093 double d = value.toDouble( &ok );
1094 QCOMPARE( d, result );
1095 QVERIFY( ok == valueOK );
1098 void tst_QVariant::toLongLong_data()
1100 QTest::addColumn<QVariant>("value");
1101 QTest::addColumn<qlonglong>("result");
1102 QTest::addColumn<bool>("valueOK");
1104 QTest::newRow( "int0" ) << QVariant( 123 ) << (qlonglong)123 << true;
1105 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (qlonglong)3 << true;
1106 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (qlonglong)3 << true;
1107 QTest::newRow( "uint" ) << QVariant( 123u ) << (qlonglong)123 << true;
1108 QTest::newRow( "int-string" ) << QVariant( QString("123") )
1109 << (qlonglong)123 << true;
1110 QTest::newRow( "string" ) << QVariant( QString("Unicode fun") ) << (qlonglong)0
1112 QTest::newRow( "longlong" ) << QVariant( intMax1 ) << intMax1 << true;
1113 QTest::newRow( "ulonglong" ) << QVariant( uintMax1 ) << (qlonglong)uintMax1 << true;
1114 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << qlonglong('a') << true;
1115 QByteArray bytearray(4, ' ');
1120 QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (qlonglong) 3200 << true;
1123 void tst_QVariant::toLongLong()
1125 QFETCH( QVariant, value );
1126 QFETCH( qlonglong, result );
1127 QFETCH( bool, valueOK );
1128 QVERIFY( value.isValid() );
1129 QVERIFY( value.canConvert( QVariant::LongLong ) );
1131 qlonglong ll = value.toLongLong( &ok );
1132 QCOMPARE( ll, result );
1133 QVERIFY( ok == valueOK );
1136 void tst_QVariant::toULongLong_data()
1138 QTest::addColumn<QVariant>("value");
1139 QTest::addColumn<qulonglong>("result");
1140 QTest::addColumn<bool>("valueOK");
1142 QTest::newRow( "int0" ) << QVariant( 123 ) << (qulonglong)123 << true;
1143 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (qulonglong)3 << true;
1144 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (qulonglong)3 << true;
1145 QTest::newRow( "uint" ) << QVariant( 123u ) << (qulonglong)123 << true;
1146 QTest::newRow( "int-string" ) << QVariant( QString("123") )
1147 << (qulonglong)123 << true;
1148 QTest::newRow( "string" ) << QVariant( QString("Unicode fun") ) << (qulonglong)0
1150 QTest::newRow( "ulonglong-string" ) << QVariant( QString("18446744073709551615") )
1151 << Q_UINT64_C(18446744073709551615)
1153 QTest::newRow( "bytaa-string" ) << QVariant( QString("18446744073709551615") )
1154 << Q_UINT64_C(18446744073709551615)
1156 QTest::newRow( "longlong" ) << QVariant( intMax1 ) << (qulonglong)intMax1 << true;
1157 QTest::newRow( "ulonglong" ) << QVariant( uintMax1 ) << uintMax1 << true;
1158 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << qulonglong('a') << true;
1159 QByteArray bytearray(4, ' ');
1164 QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (qulonglong) 3201 << true;
1167 void tst_QVariant::toULongLong()
1169 QFETCH( QVariant, value );
1170 QFETCH( qulonglong, result );
1171 QFETCH( bool, valueOK );
1172 QVERIFY( value.isValid() );
1173 QVERIFY( value.canConvert( QVariant::ULongLong ) );
1175 qulonglong ll = value.toULongLong( &ok );
1176 QCOMPARE( ll, result );
1177 QVERIFY( ok == valueOK );
1180 void tst_QVariant::toByteArray_data()
1182 QTest::addColumn<QVariant>("value");
1183 QTest::addColumn<QByteArray>("result");
1185 QByteArray ba(5, ' ');
1192 QByteArray variantBa = ba;
1194 QTest::newRow( "qbytearray" ) << QVariant( variantBa ) << ba;
1195 QTest::newRow( "int" ) << QVariant( -123 ) << QByteArray( "-123" );
1196 QTest::newRow( "uint" ) << QVariant( (uint)123 ) << QByteArray( "123" );
1197 QTest::newRow( "double" ) << QVariant( 123.456 ) << QByteArray( "123.456" );
1198 QTest::newRow( "float" ) << QVariant( 123.456f ) << QByteArray( "123.456" );
1199 QTest::newRow( "longlong" ) << QVariant( (qlonglong)34 ) << QByteArray( "34" );
1200 QTest::newRow( "ulonglong" ) << QVariant( (qulonglong)34 ) << QByteArray( "34" );
1203 void tst_QVariant::toByteArray()
1205 QFETCH( QVariant, value );
1206 QFETCH( QByteArray, result );
1207 QVERIFY( value.isValid() );
1208 QVERIFY( value.canConvert( QVariant::ByteArray ) );
1209 QByteArray ba = value.toByteArray();
1210 QCOMPARE( ba, result );
1213 void tst_QVariant::toString_data()
1215 QTest::addColumn<QVariant>("value");
1216 QTest::addColumn<QString>("result");
1218 QTest::newRow( "qstring" ) << QVariant( QString( "Test" ) ) << QString( "Test" );
1219 QTest::newRow( "charstar" ) << QVariant(QLatin1String("Test")) << QString("Test");
1220 QTest::newRow( "qbytearray") << QVariant( QByteArray( "Test\0" ) ) << QString( "Test" );
1221 QTest::newRow( "int" ) << QVariant( -123 ) << QString( "-123" );
1222 QTest::newRow( "uint" ) << QVariant( (uint)123 ) << QString( "123" );
1223 QTest::newRow( "double" ) << QVariant( 123.456 ) << QString( "123.456" );
1224 QTest::newRow( "float" ) << QVariant( 123.456f ) << QString( "123.456" );
1225 QTest::newRow( "bool" ) << QVariant( true ) << QString( "true" );
1226 QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 1, 1 ) ) << QString( "2002-01-01" );
1227 QTest::newRow( "qtime" ) << QVariant( QTime( 12, 34, 56 ) ) << QString( "12:34:56" );
1228 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 1, 1 ), QTime( 12, 34, 56 ) ) ) << QString( "2002-01-01T12:34:56" );
1229 QTest::newRow( "qkeysequence" ) << qVariantFromValue( QKeySequence( Qt::CTRL + Qt::Key_A ) )
1231 << QString( "Ctrl+A" );
1233 << QString(QChar(0x2318)) + "A";
1236 QFont font( "times", 12 );
1237 QTest::newRow( "qfont" ) << qVariantFromValue( font ) << QString("times,12,-1,5,50,0,0,0,0,0");
1238 QTest::newRow( "qcolor" ) << qVariantFromValue( QColor( 10, 10, 10 ) ) << QString( "#0a0a0a" );
1239 QTest::newRow( "llong" ) << QVariant( (qlonglong)Q_INT64_C(123456789012) ) <<
1240 QString( "123456789012" );
1243 void tst_QVariant::toString()
1245 QFETCH( QVariant, value );
1246 QFETCH( QString, result );
1247 QVERIFY( value.isValid() );
1248 QVERIFY( value.canConvert( QVariant::String ) );
1249 QString str = value.toString();
1250 QCOMPARE( str, result );
1253 void tst_QVariant::toDate_data()
1255 QTest::addColumn<QVariant>("value");
1256 QTest::addColumn<QDate>("result");
1258 QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 10, 10 ) ) << QDate( 2002, 10, 10 );
1259 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) ) ) << QDate( 2002, 10, 10 );
1260 QTest::newRow( "qstring" ) << QVariant( QString( "2002-10-10" ) ) << QDate( 2002, 10, 10 );
1263 void tst_QVariant::toDate()
1265 QFETCH( QVariant, value );
1266 QFETCH( QDate, result );
1267 QVERIFY( value.isValid() );
1268 QVERIFY( value.canConvert( QVariant::Date ) );
1269 QCOMPARE( value.toDate(), result );
1272 void tst_QVariant::toTime_data()
1274 QTest::addColumn<QVariant>("value");
1275 QTest::addColumn<QTime>("result");
1277 QTest::newRow( "qtime" ) << QVariant( QTime( 12, 34, 56 ) ) << QTime( 12, 34, 56 );
1278 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) ) ) << QTime( 12, 34, 56 );
1279 QTest::newRow( "qstring" ) << QVariant( QString( "12:34:56" ) ) << QTime( 12, 34, 56 );
1282 void tst_QVariant::toTime()
1284 QFETCH( QVariant, value );
1285 QFETCH( QTime, result );
1286 QVERIFY( value.isValid() );
1287 QVERIFY( value.canConvert( QVariant::Time ) );
1288 QCOMPARE( value.toTime(), result );
1291 void tst_QVariant::toDateTime_data()
1293 QTest::addColumn<QVariant>("value");
1294 QTest::addColumn<QDateTime>("result");
1296 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) ) )
1297 << QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) );
1298 QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 10, 10 ) ) << QDateTime( QDate( 2002, 10, 10 ), QTime( 0, 0, 0 ) );
1299 QTest::newRow( "qstring" ) << QVariant( QString( "2002-10-10T12:34:56" ) ) << QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) );
1302 void tst_QVariant::toDateTime()
1304 QFETCH( QVariant, value );
1305 QFETCH( QDateTime, result );
1306 QVERIFY( value.isValid() );
1307 QVERIFY( value.canConvert( QVariant::DateTime ) );
1308 QCOMPARE( value.toDateTime(), result );
1311 void tst_QVariant::toLocale()
1314 QLocale loc = variant.toLocale();
1315 variant = QLocale::system();
1316 loc = variant.toLocale();
1319 void tst_QVariant::toRegExp()
1322 QRegExp rx = variant.toRegExp();
1323 variant = QRegExp("foo");
1324 rx = variant.toRegExp();
1327 void tst_QVariant::toRegularExpression()
1330 QRegularExpression re = variant.toRegularExpression();
1331 QCOMPARE(re, QRegularExpression());
1333 variant = QRegularExpression("abc.*def");
1334 re = variant.toRegularExpression();
1335 QCOMPARE(re, QRegularExpression("abc.*def"));
1337 variant = QVariant::fromValue(QRegularExpression("[ab]\\w+"));
1338 re = variant.value<QRegularExpression>();
1339 QCOMPARE(re, QRegularExpression("[ab]\\w+"));
1342 void tst_QVariant::matrix()
1345 QMatrix matrix = qVariantValue<QMatrix>(variant);
1346 QVERIFY(matrix.isIdentity());
1347 qVariantSetValue(variant, QMatrix().rotate(90));
1348 QCOMPARE(QMatrix().rotate(90), qVariantValue<QMatrix>(variant));
1350 void *mmatrix = QMetaType::create(QVariant::Matrix, 0);
1352 QMetaType::destroy(QVariant::Matrix, mmatrix);
1355 void tst_QVariant::matrix4x4()
1358 QMatrix4x4 matrix = qVariantValue<QMatrix4x4>(variant);
1359 QVERIFY(matrix.isIdentity());
1362 qVariantSetValue(variant, m);
1363 QCOMPARE(m, qVariantValue<QMatrix4x4>(variant));
1365 void *mmatrix = QMetaType::create(QVariant::Matrix4x4, 0);
1367 QMetaType::destroy(QVariant::Matrix4x4, mmatrix);
1370 void tst_QVariant::transform()
1373 QTransform matrix = qVariantValue<QTransform>(variant);
1374 QVERIFY(matrix.isIdentity());
1375 qVariantSetValue(variant, QTransform().rotate(90));
1376 QCOMPARE(QTransform().rotate(90), qVariantValue<QTransform>(variant));
1378 void *mmatrix = QMetaType::create(QVariant::Transform, 0);
1380 QMetaType::destroy(QVariant::Transform, mmatrix);
1384 void tst_QVariant::vector2D()
1387 QVector2D vector = qVariantValue<QVector2D>(variant);
1388 QVERIFY(vector.isNull());
1389 qVariantSetValue(variant, QVector2D(0.1, 0.2));
1390 QCOMPARE(QVector2D(0.1, 0.2), qVariantValue<QVector2D>(variant));
1392 void *pvector = QMetaType::create(QVariant::Vector2D, 0);
1394 QMetaType::destroy(QVariant::Vector2D, pvector);
1397 void tst_QVariant::vector3D()
1400 QVector3D vector = qVariantValue<QVector3D>(variant);
1401 QVERIFY(vector.isNull());
1402 qVariantSetValue(variant, QVector3D(0.1, 0.2, 0.3));
1403 QCOMPARE(QVector3D(0.1, 0.2, 0.3), qVariantValue<QVector3D>(variant));
1405 void *pvector = QMetaType::create(QVariant::Vector3D, 0);
1407 QMetaType::destroy(QVariant::Vector3D, pvector);
1410 void tst_QVariant::vector4D()
1413 QVector4D vector = qVariantValue<QVector4D>(variant);
1414 QVERIFY(vector.isNull());
1415 qVariantSetValue(variant, QVector4D(0.1, 0.2, 0.3, 0.4));
1416 QCOMPARE(QVector4D(0.1, 0.2, 0.3, 0.4), qVariantValue<QVector4D>(variant));
1418 void *pvector = QMetaType::create(QVariant::Vector4D, 0);
1420 QMetaType::destroy(QVariant::Vector4D, pvector);
1423 void tst_QVariant::quaternion()
1426 QQuaternion quaternion = qVariantValue<QQuaternion>(variant);
1427 QVERIFY(quaternion.isIdentity());
1428 qVariantSetValue(variant, QQuaternion(0.1, 0.2, 0.3, 0.4));
1429 QCOMPARE(QQuaternion(0.1, 0.2, 0.3, 0.4), qVariantValue<QQuaternion>(variant));
1431 void *pquaternion = QMetaType::create(QVariant::Quaternion, 0);
1432 QVERIFY(pquaternion);
1433 QMetaType::destroy(QVariant::Quaternion, pquaternion);
1436 struct CustomStreamableClass
1439 bool operator==(const CustomStreamableClass& other) const
1441 return i == other.i;
1444 Q_DECLARE_METATYPE(CustomStreamableClass);
1446 QDataStream &operator<<(QDataStream &out, const CustomStreamableClass &myObj)
1448 return out << myObj.i;
1451 QDataStream &operator>>(QDataStream &in, CustomStreamableClass &myObj)
1453 return in >> myObj.i;
1456 void tst_QVariant::writeToReadFromDataStream_data()
1458 qRegisterMetaTypeStreamOperators<CustomStreamableClass>();
1460 QTest::addColumn<QVariant>("writeVariant");
1461 QTest::addColumn<bool>("isNull");
1463 typedef QList<QVariant> variantsList;
1464 variantsList valuelist;
1465 valuelist << QVariant( 1 ) << QVariant( QString("Two") ) << QVariant( 3.45 );
1466 QVariant var(valuelist);
1467 QTest::newRow( "list_valid" ) << var << false;
1470 QTest::newRow( "invalid" ) << QVariant() << true;
1471 QTest::newRow( "bitarray_invalid" ) << QVariant( QBitArray() ) << true;
1472 QBitArray bitarray( 3 );
1476 QTest::newRow( "bitarray_valid" ) << QVariant( bitarray ) << false;
1477 QTest::newRow( "bytearray_invalid" ) << QVariant( QByteArray() ) << true;
1478 QTest::newRow( "int_invalid") << QVariant(QVariant::Int) << true;
1479 QByteArray bytearray(5, ' ');
1484 bytearray[4] = '\0';
1485 QTest::newRow( "bytearray_valid" ) << QVariant( bytearray ) << false;
1486 QTest::newRow( "bitmap_invalid" ) << qVariantFromValue( QBitmap() ) << true;
1487 QBitmap bitmap( 10, 10 );
1488 bitmap.fill( Qt::red );
1489 QTest::newRow( "bitmap_valid" ) << qVariantFromValue( bitmap ) << false;
1490 QTest::newRow( "brush_valid" ) << qVariantFromValue( QBrush( Qt::red ) ) << false;
1491 QTest::newRow( "color_valid" ) << qVariantFromValue( QColor( Qt::red ) ) << false;
1492 #ifndef QT_NO_CURSOR
1493 QTest::newRow( "cursor_valid" ) << qVariantFromValue( QCursor( Qt::PointingHandCursor ) ) << false;
1495 QTest::newRow( "date_invalid" ) << QVariant( QDate() ) << true;
1496 QTest::newRow( "date_valid" ) << QVariant( QDate( 2002, 07, 06 ) ) << false;
1497 QTest::newRow( "datetime_invalid" ) << QVariant( QDateTime() ) << true;
1498 QTest::newRow( "datetime_valid" ) << QVariant( QDateTime( QDate( 2002, 07, 06 ), QTime( 14, 0, 0 ) ) ) << false;
1499 QTest::newRow( "double_valid" ) << QVariant( 123.456 ) << false;
1500 QTest::newRow( "float_valid" ) << QVariant( 123.456f ) << false;
1501 QTest::newRow( "font_valid" ) << qVariantFromValue( QFont( "times", 12 ) ) << false;
1502 QTest::newRow( "pixmap_invalid" ) << qVariantFromValue( QPixmap() ) << true;
1503 QPixmap pixmap( 10, 10 );
1504 pixmap.fill( Qt::red );
1505 QTest::newRow( "pixmap_valid" ) << qVariantFromValue( pixmap ) << false;
1506 // QTest::newRow( "iconset_valid" ) << QVariant( QIcon( pixmap ) ) << false;
1507 QTest::newRow( "image_invalid" ) << qVariantFromValue( QImage() ) << true;
1508 QTest::newRow( "keysequence_valid" ) << qVariantFromValue( QKeySequence( Qt::CTRL + Qt::Key_A ) ) << false;
1509 QTest::newRow( "int_valid" ) << QVariant( -123 ) << false;
1510 typedef QMap<QString, QVariant> variantsMap;
1512 vMap.insert( "int", QVariant( 1 ) );
1513 vMap.insert( "string", QVariant( QString("Two") ) );
1514 vMap.insert( "double", QVariant( 3.45 ) );
1515 vMap.insert( "float", QVariant( 3.45f ) );
1516 QTest::newRow( "map_valid" ) << QVariant( vMap ) << false;
1517 QTest::newRow( "palette_valid" ) << qVariantFromValue(QPalette(QColor("turquoise"))) << false;
1518 QTest::newRow( "pen_valid" ) << qVariantFromValue( QPen( Qt::red ) ) << false;
1519 QTest::newRow( "pointarray_invalid" ) << qVariantFromValue( QPolygon() ) << true;
1520 QTest::newRow( "pointarray_valid" ) << qVariantFromValue( QPolygon( QRect( 10, 10, 20, 20 ) ) ) << false;
1521 QTest::newRow( "region_invalid" ) << qVariantFromValue( QRegion() ) << true;
1522 QTest::newRow( "region_valid" ) << qVariantFromValue( QRegion( 10, 10, 20, 20 ) ) << false;
1523 QTest::newRow( "sizepolicy_valid" ) << qVariantFromValue( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) ) << false;
1524 QTest::newRow( "point_invalid" ) << qVariantFromValue( QPoint() ) << true;
1525 QTest::newRow( "point_valid" ) << qVariantFromValue( QPoint( 10, 10 ) ) << false;
1526 QTest::newRow( "rect_invalid" ) << QVariant( QRect() ) << true;
1527 QTest::newRow( "rect_valid" ) << QVariant( QRect( 10, 10, 20, 20 ) ) << false;
1528 QTest::newRow( "size_invalid" ) << QVariant( QSize( 0, 0 ) ) << true;
1529 QTest::newRow( "size_valid" ) << QVariant( QSize( 10, 10 ) ) << false;
1530 QTest::newRow( "string_invalid" ) << QVariant( QString() ) << true;
1531 QTest::newRow( "string_valid" ) << QVariant( QString( "Test" ) ) << false;
1532 QStringList stringlist;
1533 stringlist << "One" << "Two" << "Three";
1534 QTest::newRow( "stringlist_valid" ) << QVariant( stringlist ) << false;
1535 QTest::newRow( "time_invalid" ) << QVariant( QTime() ) << true;
1536 QTest::newRow( "time_valid" ) << QVariant( QTime( 14, 0, 0 ) ) << false;
1537 QTest::newRow( "uint_valid" ) << QVariant( (uint)123 ) << false;
1538 QTest::newRow( "qchar" ) << QVariant(QChar('a')) << false;
1539 QTest::newRow( "qchar_null" ) << QVariant(QChar(0)) << true;
1540 QTest::newRow( "regexp" ) << QVariant(QRegExp("foo", Qt::CaseInsensitive)) << false;
1541 QTest::newRow( "regexp_empty" ) << QVariant(QRegExp()) << false;
1542 QTest::newRow( "regularexpression" ) << QVariant(QRegularExpression("abc.*def")) << false;
1543 QTest::newRow( "regularexpression_empty" ) << QVariant(QRegularExpression()) << false;
1545 // types known to QMetaType, but not part of QVariant::Type
1546 QTest::newRow("QMetaType::Long invalid") << QVariant(QMetaType::Long, (void *) 0) << false;
1548 QTest::newRow("QMetaType::Long") << QVariant(QMetaType::Long, &longInt) << false;
1549 QTest::newRow("QMetaType::Short invalid") << QVariant(QMetaType::Short, (void *) 0) << false;
1551 QTest::newRow("QMetaType::Short") << QVariant(QMetaType::Short, &shortInt) << false;
1552 QTest::newRow("QMetaType::Char invalid") << QVariant(QMetaType::Char, (void *) 0) << false;
1554 QTest::newRow("QMetaType::Char") << QVariant(QMetaType::Char, &ch) << false;
1555 QTest::newRow("QMetaType::ULong invalid") << QVariant(QMetaType::ULong, (void *) 0) << false;
1556 ulong ulongInt = 1ul;
1557 QTest::newRow("QMetaType::ULong") << QVariant(QMetaType::ULong, &ulongInt) << false;
1558 QTest::newRow("QMetaType::UShort invalid") << QVariant(QMetaType::UShort, (void *) 0) << false;
1559 ushort ushortInt = 1u;
1560 QTest::newRow("QMetaType::UShort") << QVariant(QMetaType::UShort, &ushortInt) << false;
1561 QTest::newRow("QMetaType::UChar invalid") << QVariant(QMetaType::UChar, (void *) 0) << false;
1563 QTest::newRow("QMetaType::UChar") << QVariant(QMetaType::UChar, &uch) << false;
1564 QTest::newRow("QMetaType::Float invalid") << QVariant(QMetaType::Float, (void *) 0) << false;
1566 QTest::newRow("QMetaType::Float") << QVariant(QMetaType::Float, &f) << false;
1567 CustomStreamableClass custom = {123};
1568 QTest::newRow("Custom type") << QVariant::fromValue(custom) << false;
1571 void tst_QVariant::writeToReadFromDataStream()
1573 QFETCH( QVariant, writeVariant );
1574 QFETCH( bool, isNull );
1577 QDataStream writeStream( &data, QIODevice::WriteOnly );
1578 writeStream << writeVariant;
1580 QVariant readVariant;
1581 QDataStream readStream( &data, QIODevice::ReadOnly );
1582 readStream >> readVariant;
1583 QVERIFY( readVariant.isNull() == isNull );
1584 // Best way to confirm the readVariant contains the same data?
1585 // Since only a few won't match since the serial numbers are different
1586 // I won't bother adding another bool in the data test.
1587 const int writeType = writeVariant.userType();
1588 if (writeType == qMetaTypeId<CustomStreamableClass>())
1589 QCOMPARE(qvariant_cast<CustomStreamableClass>(readVariant), qvariant_cast<CustomStreamableClass>(writeVariant));
1590 else if ( writeType != QVariant::Invalid && writeType != QVariant::Bitmap && writeType != QVariant::Pixmap
1591 && writeType != QVariant::Image) {
1592 switch (writeType) {
1594 QCOMPARE( readVariant, writeVariant );
1597 // compare types know by QMetaType but not QVariant (QVariant::operator==() knows nothing about them)
1598 case QMetaType::Long:
1599 QCOMPARE(qvariant_cast<long>(readVariant), qvariant_cast<long>(writeVariant));
1601 case QMetaType::ULong:
1602 QCOMPARE(qvariant_cast<ulong>(readVariant), qvariant_cast<ulong>(writeVariant));
1604 case QMetaType::Short:
1605 QCOMPARE(qvariant_cast<short>(readVariant), qvariant_cast<short>(writeVariant));
1607 case QMetaType::UShort:
1608 QCOMPARE(qvariant_cast<ushort>(readVariant), qvariant_cast<ushort>(writeVariant));
1610 case QMetaType::Char:
1611 QCOMPARE(qvariant_cast<char>(readVariant), qvariant_cast<char>(writeVariant));
1613 case QMetaType::UChar:
1614 QCOMPARE(qvariant_cast<uchar>(readVariant), qvariant_cast<uchar>(writeVariant));
1616 case QMetaType::Float:
1618 // the uninitialized float can be NaN (observed on Windows Mobile 5 ARMv4i)
1619 float readFloat = qvariant_cast<float>(readVariant);
1620 float writtenFloat = qvariant_cast<float>(writeVariant);
1621 QVERIFY(qIsNaN(readFloat) == qIsNaN(writtenFloat));
1622 if (!qIsNaN(readFloat))
1623 QVERIFY(readFloat == writtenFloat);
1630 void tst_QVariant::writeToReadFromOldDataStream()
1632 QVariant writeVariant = QString("hello");
1635 QDataStream writeStream(&data, QIODevice::WriteOnly);
1636 writeStream.setVersion(QDataStream::Qt_2_1);
1637 writeStream << writeVariant;
1639 QVariant readVariant;
1640 QDataStream readStream(&data, QIODevice::ReadOnly);
1641 readStream.setVersion(QDataStream::Qt_2_1);
1642 readStream >> readVariant;
1644 QCOMPARE(writeVariant.userType(), readVariant.userType());
1645 QCOMPARE(writeVariant, readVariant);
1648 void tst_QVariant::checkDataStream()
1650 QTest::ignoreMessage(QtWarningMsg, "Trying to construct an instance of an invalid type, type id: 49");
1651 const QByteArray settingsHex("00000031ffffffffff");
1652 const QByteArray settings = QByteArray::fromHex(settingsHex);
1653 QDataStream in(settings);
1656 // the line below has been left out for now since the data stream
1657 // is not necessarily considered corrupt when an invalid QVariant is
1658 // constructed. However, it might be worth considering changing that behavior
1660 // QCOMPARE(in.status(), QDataStream::ReadCorruptData);
1661 QCOMPARE(v.type(), QVariant::Invalid);
1664 void tst_QVariant::operator_eq_eq_data()
1666 QTest::addColumn<QVariant>("left");
1667 QTest::addColumn<QVariant>("right");
1668 QTest::addColumn<bool>("equal"); // left == right ?
1671 QVariant i0( int(0) );
1672 QVariant i1( int(1) );
1674 QTest::newRow( "invinv" ) << inv << inv << true;
1676 QTest::newRow( "int1int1" ) << i1 << i1 << true;
1677 QTest::newRow( "int1int0" ) << i1 << i0 << false;
1678 QTest::newRow( "nullint" ) << i0 << QVariant(QVariant::Int) << false;
1680 // LongLong and ULongLong
1681 QVariant ll1( (qlonglong)1 );
1682 QVariant lln2( (qlonglong)-2 );
1683 QVariant ull1( (qulonglong)1 );
1684 QVariant ull3( (qulonglong)3 );
1685 QTest::newRow( "ll1ll1" ) << ll1 << ll1 << true;
1686 QTest::newRow( "ll1lln2" ) << ll1 << lln2 << false;
1687 QTest::newRow( "ll1ull1" ) << ull1 << ull1 << true;
1688 QTest::newRow( "ll1i1" ) << ull1 << i1 << true;
1689 QTest::newRow( "ull1ull1" ) << ull1 << ull1 << true;
1690 QTest::newRow( "ull1i1" ) << ull1 << ull1 << true;
1693 QVariant mIntString(QByteArray("-42"));
1694 QVariant mIntQString(QString("-42"));
1696 QVariant mUInt(42u);
1697 QVariant mUIntString(QByteArray("42"));
1698 QVariant mUIntQString(QString("42"));
1700 QVariant mDouble(42.11);
1701 QVariant mDoubleString(QByteArray("42.11"));
1702 QVariant mDoubleQString(QString("42.11"));
1704 QVariant mFloat(42.11f);
1705 QVariant mFloatString(QByteArray("42.11"));
1706 QVariant mFloatQString(QString("42.11"));
1708 QVariant mLongLong((qlonglong)-42);
1709 QVariant mLongLongString(QByteArray("-42"));
1710 QVariant mLongLongQString(QString("-42"));
1712 QVariant mULongLong((qulonglong)42);
1713 QVariant mULongLongString(QByteArray("42"));
1714 QVariant mULongLongQString(QString("42"));
1716 QVariant mBool(false);
1717 QVariant mBoolString(QByteArray("false"));
1718 QVariant mBoolQString(QString("false"));
1720 QTest::newRow( "double_int" ) << QVariant(42.0) << QVariant(42) << true;
1721 QTest::newRow( "float_int" ) << QVariant(42.f) << QVariant(42) << true;
1722 QTest::newRow( "mInt_mIntString" ) << mInt << mIntString << true;
1723 QTest::newRow( "mIntString_mInt" ) << mIntString << mInt << true;
1724 QTest::newRow( "mInt_mIntQString" ) << mInt << mIntQString << true;
1725 QTest::newRow( "mIntQString_mInt" ) << mIntQString << mInt << true;
1727 QTest::newRow( "mUInt_mUIntString" ) << mUInt << mUIntString << true;
1728 QTest::newRow( "mUIntString_mUInt" ) << mUIntString << mUInt << true;
1729 QTest::newRow( "mUInt_mUIntQString" ) << mUInt << mUIntQString << true;
1730 QTest::newRow( "mUIntQString_mUInt" ) << mUIntQString << mUInt << true;
1732 QTest::newRow( "mDouble_mDoubleString" ) << mDouble << mDoubleString << true;
1733 QTest::newRow( "mDoubleString_mDouble" ) << mDoubleString << mDouble << true;
1734 QTest::newRow( "mDouble_mDoubleQString" ) << mDouble << mDoubleQString << true;
1735 QTest::newRow( "mDoubleQString_mDouble" ) << mDoubleQString << mDouble << true;
1737 QTest::newRow( "mFloat_mFloatString" ) << mFloat << mFloatString << true;
1738 QTest::newRow( "mFloatString_mFloat" ) << mFloatString << mFloat << true;
1739 QTest::newRow( "mFloat_mFloatQString" ) << mFloat << mFloatQString << true;
1740 QTest::newRow( "mFloatQString_mFloat" ) << mFloatQString << mFloat << true;
1742 QTest::newRow( "mLongLong_mLongLongString" ) << mLongLong << mLongLongString << true;
1743 QTest::newRow( "mLongLongString_mLongLong" ) << mLongLongString << mLongLong << true;
1744 QTest::newRow( "mLongLong_mLongLongQString" ) << mLongLong << mLongLongQString << true;
1745 QTest::newRow( "mLongLongQString_mLongLong" ) << mLongLongQString << mLongLong << true;
1747 QTest::newRow( "mULongLong_mULongLongString" ) << mULongLong << mULongLongString << true;
1748 QTest::newRow( "mULongLongString_mULongLong" ) << mULongLongString << mULongLong << true;
1749 QTest::newRow( "mULongLong_mULongLongQString" ) << mULongLong << mULongLongQString << true;
1750 QTest::newRow( "mULongLongQString_mULongLong" ) << mULongLongQString << mULongLong << true;
1752 QTest::newRow( "mBool_mBoolString" ) << mBool << mBoolString << true;
1753 QTest::newRow( "mBoolString_mBool" ) << mBoolString << mBool << true;
1754 QTest::newRow( "mBool_mBoolQString" ) << mBool << mBoolQString << true;
1755 QTest::newRow( "mBoolQString_mBool" ) << mBoolQString << mBool << true;
1757 QTest::newRow("ba2qstring") << QVariant(QLatin1String("hallo")) << QVariant(QString("hallo")) << true;
1758 QTest::newRow("qstring2ba") << QVariant(QString("hallo")) << QVariant(QLatin1String("hallo")) << true;
1759 QTest::newRow("char_char") << QVariant(QChar('a')) << QVariant(QChar('a')) << true;
1760 QTest::newRow("char_char2") << QVariant(QChar('a')) << QVariant(QChar('b')) << false;
1762 QTest::newRow("invalidConversion") << QVariant(QString("bubu")) << QVariant(0) << false;
1763 QTest::newRow("invalidConversionR") << QVariant(0) << QVariant(QString("bubu")) << false;
1764 // ### many other combinations missing
1767 QMap<QString, QVariant> map1;
1768 map1.insert( "X", 1 );
1770 QMap<QString, QVariant> map2;
1771 map2.insert( "Y", 1 );
1773 QTest::newRow("TwoItemsInEqual") << QVariant(map1) << QVariant(map2) << false;
1778 QMap<QString, QVariant> map1;
1779 map1.insert( "X", 1 );
1781 QMap<QString, QVariant> map2;
1782 map2.insert( "X", 1 );
1784 QTest::newRow("TwoItemsEqual") << QVariant(map1) << QVariant(map2) << true;
1788 QMap<QString, QVariant> map1;
1789 map1.insert( "X", 1 );
1791 QMap<QString, QVariant> map2;
1793 QTest::newRow("PopulatedEmptyMap") << QVariant(map1) << QVariant(map2) << false;
1797 QMap<QString, QVariant> map1;
1799 QMap<QString, QVariant> map2;
1800 map2.insert( "X", 1 );
1802 QTest::newRow("EmptyPopulatedMap") << QVariant(map1) << QVariant(map2) << false;
1806 QMap<QString, QVariant> map1;
1807 map1.insert( "X", 1 );
1808 map1.insert( "Y", 1 );
1810 QMap<QString, QVariant> map2;
1811 map2.insert( "X", 1 );
1813 QTest::newRow("FirstLarger") << QVariant(map1) << QVariant(map2) << false;
1817 QMap<QString, QVariant> map1;
1818 map1.insert( "X", 1 );
1820 QMap<QString, QVariant> map2;
1821 map2.insert( "X", 1 );
1822 map2.insert( "Y", 1 );
1824 QTest::newRow("SecondLarger") << QVariant(map1) << QVariant(map2) << false;
1827 // same thing with hash
1829 QHash<QString, QVariant> hash1;
1830 hash1.insert( "X", 1 );
1832 QHash<QString, QVariant> hash2;
1833 hash2.insert( "Y", 1 );
1835 QTest::newRow("HashTwoItemsInEqual") << QVariant(hash1) << QVariant(hash2) << false;
1840 QHash<QString, QVariant> hash1;
1841 hash1.insert( "X", 1 );
1843 QHash<QString, QVariant> hash2;
1844 hash2.insert( "X", 1 );
1846 QTest::newRow("HashTwoItemsEqual") << QVariant(hash1) << QVariant(hash2) << true;
1850 QHash<QString, QVariant> hash1;
1851 hash1.insert( "X", 1 );
1853 QHash<QString, QVariant> hash2;
1855 QTest::newRow("HashPopulatedEmptyHash") << QVariant(hash1) << QVariant(hash2) << false;
1859 QHash<QString, QVariant> hash1;
1861 QHash<QString, QVariant> hash2;
1862 hash2.insert( "X", 1 );
1864 QTest::newRow("EmptyPopulatedHash") << QVariant(hash1) << QVariant(hash2) << false;
1868 QHash<QString, QVariant> hash1;
1869 hash1.insert( "X", 1 );
1870 hash1.insert( "Y", 1 );
1872 QHash<QString, QVariant> hash2;
1873 hash2.insert( "X", 1 );
1875 QTest::newRow("HashFirstLarger") << QVariant(hash1) << QVariant(hash2) << false;
1879 QHash<QString, QVariant> hash1;
1880 hash1.insert( "X", 1 );
1882 QHash<QString, QVariant> hash2;
1883 hash2.insert( "X", 1 );
1884 hash2.insert( "Y", 1 );
1886 QTest::newRow("HashSecondLarger") << QVariant(hash1) << QVariant(hash2) << false;
1890 void tst_QVariant::operator_eq_eq()
1892 QFETCH( QVariant, left );
1893 QFETCH( QVariant, right );
1894 QFETCH( bool, equal );
1895 QEXPECT_FAIL("nullint", "See QTBUG-22933", Continue);
1896 QCOMPARE( left == right, equal );
1899 void tst_QVariant::operator_eq_eq_rhs()
1907 /* This should _not_ compile */
1911 QVERIFY(dt == list);
1915 void tst_QVariant::typeName_data()
1917 QTest::addColumn<int>("type");
1918 QTest::addColumn<QByteArray>("res");
1919 QTest::newRow("0") << int(QVariant::Invalid) << QByteArray("");
1920 QTest::newRow("1") << int(QVariant::Map) << QByteArray("QVariantMap");
1921 QTest::newRow("2") << int(QVariant::List) << QByteArray("QVariantList");
1922 QTest::newRow("3") << int(QVariant::String) << QByteArray("QString");
1923 QTest::newRow("4") << int(QVariant::StringList) << QByteArray("QStringList");
1924 QTest::newRow("5") << int(QVariant::Font) << QByteArray("QFont");
1925 QTest::newRow("6") << int(QVariant::Pixmap) << QByteArray("QPixmap");
1926 QTest::newRow("7") << int(QVariant::Brush) << QByteArray("QBrush");
1927 QTest::newRow("8") << int(QVariant::Rect) << QByteArray("QRect");
1928 QTest::newRow("9") << int(QVariant::Size) << QByteArray("QSize");
1929 QTest::newRow("10") << int(QVariant::Color) << QByteArray("QColor");
1930 QTest::newRow("11") << int(QVariant::Palette) << QByteArray("QPalette");
1931 QTest::newRow("12") << int(QVariant::Point) << QByteArray("QPoint");
1932 QTest::newRow("13") << int(QVariant::Image) << QByteArray("QImage");
1933 QTest::newRow("14") << int(QVariant::Int) << QByteArray("int");
1934 QTest::newRow("15") << int(QVariant::UInt) << QByteArray("uint");
1935 QTest::newRow("16") << int(QVariant::Bool) << QByteArray("bool");
1936 QTest::newRow("17") << int(QVariant::Double) << QByteArray("double");
1937 QTest::newRow("18") << int(QMetaType::Float) << QByteArray("float");
1938 QTest::newRow("19") << int(QVariant::Polygon) << QByteArray("QPolygon");
1939 QTest::newRow("20") << int(QVariant::Region) << QByteArray("QRegion");
1940 QTest::newRow("21") << int(QVariant::Bitmap) << QByteArray("QBitmap");
1941 QTest::newRow("22") << int(QVariant::Cursor) << QByteArray("QCursor");
1942 QTest::newRow("23") << int(QVariant::SizePolicy) << QByteArray("QSizePolicy");
1943 QTest::newRow("24") << int(QVariant::Date) << QByteArray("QDate");
1944 QTest::newRow("25") << int(QVariant::Time) << QByteArray("QTime");
1945 QTest::newRow("26") << int(QVariant::DateTime) << QByteArray("QDateTime");
1946 QTest::newRow("27") << int(QVariant::ByteArray) << QByteArray("QByteArray");
1947 QTest::newRow("28") << int(QVariant::BitArray) << QByteArray("QBitArray");
1948 QTest::newRow("29") << int(QVariant::KeySequence) << QByteArray("QKeySequence");
1949 QTest::newRow("30") << int(QVariant::Pen) << QByteArray("QPen");
1950 QTest::newRow("31") << int(QVariant::LongLong) << QByteArray("qlonglong");
1951 QTest::newRow("32") << int(QVariant::ULongLong) << QByteArray("qulonglong");
1952 QTest::newRow("33") << int(QVariant::Char) << QByteArray("QChar");
1953 QTest::newRow("34") << int(QVariant::Url) << QByteArray("QUrl");
1954 QTest::newRow("35") << int(QVariant::TextLength) << QByteArray("QTextLength");
1955 QTest::newRow("36") << int(QVariant::TextFormat) << QByteArray("QTextFormat");
1956 QTest::newRow("37") << int(QVariant::Locale) << QByteArray("QLocale");
1957 QTest::newRow("38") << int(QVariant::LineF) << QByteArray("QLineF");
1958 QTest::newRow("39") << int(QVariant::RectF) << QByteArray("QRectF");
1959 QTest::newRow("40") << int(QVariant::PointF) << QByteArray("QPointF");
1960 QTest::newRow("41") << int(QVariant::RegExp) << QByteArray("QRegExp");
1961 QTest::newRow("43") << int(QVariant::Matrix) << QByteArray("QMatrix");
1962 QTest::newRow("44") << int(QVariant::Transform) << QByteArray("QTransform");
1963 QTest::newRow("45") << int(QVariant::Hash) << QByteArray("QVariantHash");
1964 QTest::newRow("46") << int(QVariant::Matrix4x4) << QByteArray("QMatrix4x4");
1965 QTest::newRow("47") << int(QVariant::Vector2D) << QByteArray("QVector2D");
1966 QTest::newRow("48") << int(QVariant::Vector3D) << QByteArray("QVector3D");
1967 QTest::newRow("49") << int(QVariant::Vector4D) << QByteArray("QVector4D");
1968 QTest::newRow("50") << int(QVariant::Quaternion) << QByteArray("QQuaternion");
1969 QTest::newRow("51") << int(QVariant::RegularExpression) << QByteArray("QRegularExpression");
1972 void tst_QVariant::typeName()
1974 QFETCH( int, type );
1975 QFETCH( QByteArray, res );
1976 QCOMPARE(QString::fromLatin1(QVariant::typeToName((QVariant::Type)type)),
1977 QString::fromLatin1(res.constData()));
1980 // test nameToType as well
1981 void tst_QVariant::typeToName()
1984 QCOMPARE( QVariant::typeToName( v.type() ), (const char*)0 ); // Invalid
1985 // assumes that QVariant::Type contains consecutive values
1987 int max = QVariant::LastGuiType;
1988 for ( int t = 1; t <= max; t++ ) {
1989 const char *n = QVariant::typeToName( (QVariant::Type)t );
1991 QCOMPARE( int(QVariant::nameToType( n )), t );
1994 QCOMPARE(QVariant::typeToName(QVariant::Int), "int");
1995 // not documented but we return 0 if the type is out of range
1996 // by testing this we catch cases where QVariant is extended
1997 // but type_map is not updated accordingly
1998 QCOMPARE( QVariant::typeToName( QVariant::Type(max+1) ), (char*)0 );
1999 // invalid type names
2000 QVERIFY( QVariant::nameToType( 0 ) == QVariant::Invalid );
2001 QVERIFY( QVariant::nameToType( "" ) == QVariant::Invalid );
2002 QVERIFY( QVariant::nameToType( "foo" ) == QVariant::Invalid );
2004 QCOMPARE(QVariant::nameToType("UserType"), QVariant::Invalid);
2006 // We don't support these old (Qt3) types anymore.
2007 QCOMPARE(QVariant::nameToType("QIconSet"), QVariant::Invalid);
2008 QCOMPARE(QVariant::nameToType("Q3CString"), QVariant::Invalid);
2009 QCOMPARE(QVariant::nameToType("Q_LLONG"), QVariant::Invalid);
2010 QCOMPARE(QVariant::nameToType("Q_ULLONG"), QVariant::Invalid);
2013 void tst_QVariant::streamInvalidVariant()
2019 QVariant writeVariant;
2020 QVariant readVariant;
2022 QVERIFY( writeVariant.type() == QVariant::Invalid );
2025 QDataStream writeStream( &data, QIODevice::WriteOnly );
2026 writeStream << writeX << writeVariant << writeY;
2028 QDataStream readStream( &data, QIODevice::ReadOnly );
2029 readStream >> readX >> readVariant >> readY;
2031 QVERIFY( readX == writeX );
2032 // Two invalid QVariant's aren't necessarily the same, so == will
2033 // return false if one is invalid, so check the type() instead
2034 QVERIFY( readVariant.type() == QVariant::Invalid );
2035 QVERIFY( readY == writeY );
2038 static int instanceCount = 0;
2042 MyType(int n = 0, const char *t=0): number(n), text(t)
2046 MyType(const MyType &other)
2047 : number(other.number), text(other.text)
2059 Q_DECLARE_METATYPE(MyType)
2060 Q_DECLARE_METATYPE(MyType*)
2062 void tst_QVariant::userType()
2065 MyType data(1, "eins");
2066 MyType data2(2, "zwei");
2070 qVariantSetValue(userVar, data);
2072 QCOMPARE(userVar.type(), QVariant::UserType);
2073 QCOMPARE(userVar.userType(), qMetaTypeId<MyType>());
2074 QCOMPARE(userVar.typeName(), "MyType");
2075 QVERIFY(!userVar.isNull());
2076 QVERIFY(!userVar.canConvert(QVariant::String));
2078 QVariant userVar2(userVar);
2079 QVERIFY(userVar == userVar2);
2081 qVariantSetValue(userVar2, data2);
2082 QVERIFY(userVar != userVar2);
2084 const MyType *varData = static_cast<const MyType *>(userVar.constData());
2086 QCOMPARE(varData->number, data.number);
2087 QCOMPARE(varData->text, data.text);
2090 qVariantSetValue(userVar3, data2);
2092 userVar3 = userVar2;
2093 QVERIFY(userVar2 == userVar3);
2095 // At this point all QVariants got destroyed but we have 2 MyType instances.
2096 QCOMPARE(instanceCount, 2);
2099 qVariantSetValue(userVar, &data);
2101 QCOMPARE(userVar.type(), QVariant::UserType);
2102 QCOMPARE(userVar.userType(), qMetaTypeId<MyType*>());
2103 QCOMPARE(userVar.typeName(), "MyType*");
2104 QVERIFY(!userVar.isNull());
2105 QVERIFY(!userVar.canConvert(QVariant::String));
2107 QVariant userVar2(userVar);
2108 QVERIFY(userVar == userVar2);
2110 qVariantSetValue(userVar2, &data2);
2111 QVERIFY(userVar != userVar2);
2113 MyType * const*varData = reinterpret_cast<MyType *const *>(userVar.constData());
2115 QCOMPARE(*varData, &data);
2118 qVariantSetValue(userVar3, &data2);
2120 /* This check is correct now. userVar2 contains a pointer to data2 and so
2122 QVERIFY(userVar2 == userVar3);
2124 userVar3 = userVar2;
2125 QVERIFY(userVar2 == userVar3);
2128 QCOMPARE(instanceCount, 2);
2130 qVariantSetValue(myCarrier, data);
2131 QCOMPARE(instanceCount, 3);
2133 QVariant second = myCarrier;
2134 QCOMPARE(instanceCount, 3);
2136 QCOMPARE(instanceCount, 4);
2138 QCOMPARE(instanceCount, 3);
2140 MyType data3(0, "null");
2141 data3 = qVariantValue<MyType>(myCarrier);
2142 QCOMPARE(data3.number, 1);
2143 QCOMPARE(data3.text, (const char *)"eins");
2145 QCOMPARE(instanceCount, 4);
2151 const MyType data(3, "drei");
2154 qVariantSetValue(myCarrier, data);
2155 QCOMPARE(myCarrier.typeName(), "MyType");
2157 const MyType data2 = qvariant_cast<MyType>(myCarrier);
2158 QCOMPARE(data2.number, 3);
2159 QCOMPARE(data2.text, (const char *)"drei");
2166 qVariantSetValue(myCarrier, s);
2167 QCOMPARE((int)qvariant_cast<short>(myCarrier), 42);
2171 qlonglong ll = Q_INT64_C(42);
2174 qVariantSetValue(myCarrier, ll);
2175 QCOMPARE(qvariant_cast<int>(myCarrier), 42);
2178 // At this point all QVariants got destroyed and MyType objects too.
2179 QCOMPARE(instanceCount, 0);
2187 Q_DECLARE_METATYPE(MyTypePOD)
2189 void tst_QVariant::podUserType()
2195 QVariant pod_as_variant = qVariantFromValue(pod);
2196 MyTypePOD pod2 = qvariant_cast<MyTypePOD>(pod_as_variant);
2198 QCOMPARE(pod.a, pod2.a);
2199 QCOMPARE(pod.b, pod2.b);
2201 qVariantSetValue(pod_as_variant, pod);
2202 pod2 = qVariantValue<MyTypePOD>(pod_as_variant);
2204 QCOMPARE(pod.a, pod2.a);
2205 QCOMPARE(pod.b, pod2.b);
2208 void tst_QVariant::basicUserType()
2213 v = QVariant(QMetaType::Int, &i);
2215 QCOMPARE(v.type(), QVariant::Int);
2216 QCOMPARE(v.toInt(), 7);
2220 v = QVariant(QMetaType::QString, &s);
2222 QCOMPARE(v.type(), QVariant::String);
2223 QCOMPARE(v.toString(), QString("foo"));
2227 v = QVariant(QMetaType::Double, &d);
2229 QCOMPARE(v.type(), QVariant::Double);
2230 QCOMPARE(v.toDouble(), 4.4);
2234 v = QVariant(QMetaType::Float, &f);
2236 QCOMPARE(v.userType(), int(QMetaType::Float));
2237 QCOMPARE(v.toDouble(), 4.5);
2240 QByteArray ba("bar");
2241 v = QVariant(QMetaType::QByteArray, &ba);
2243 QCOMPARE(v.type(), QVariant::ByteArray);
2244 QCOMPARE(v.toByteArray(), QByteArray("bar"));
2247 void tst_QVariant::data()
2254 QVariant ll = (qlonglong)2;
2255 QVariant ull = (qulonglong)3;
2256 QVariant s(QString("hallo"));
2257 QVariant r(QRect(1,2,3,4));
2261 QCOMPARE(*static_cast<int *>(v.data()), i.toInt());
2265 QCOMPARE(*static_cast<double *>(v.data()), d.toDouble());
2269 QCOMPARE(*static_cast<float *>(v.data()), qVariantValue<float>(v));
2273 QCOMPARE(*static_cast<qlonglong *>(v.data()), ll.toLongLong());
2277 QCOMPARE(*static_cast<qulonglong *>(v.data()), ull.toULongLong());
2281 QCOMPARE(*static_cast<QString *>(v.data()), s.toString());
2285 QCOMPARE(*static_cast<QRect *>(v.data()), r.toRect());
2288 void tst_QVariant::constData()
2301 QVERIFY(v.constData());
2302 QCOMPARE(*static_cast<const int *>(v.constData()), i);
2305 QVERIFY(v.constData());
2306 QCOMPARE(*static_cast<const double *>(v.constData()), d);
2309 QVERIFY(v.constData());
2310 QCOMPARE(*static_cast<const float *>(v.constData()), f);
2313 QVERIFY(v.constData());
2314 QCOMPARE(*static_cast<const qlonglong *>(v.constData()), ll);
2317 QVERIFY(v.constData());
2318 QCOMPARE(*static_cast<const qulonglong *>(v.constData()), ull);
2321 QVERIFY(v.constData());
2322 QCOMPARE(*static_cast<const QString *>(v.constData()), s);
2325 QVERIFY(v.constData());
2326 QCOMPARE(*static_cast<const QRect *>(v.constData()), r);
2335 Q_DECLARE_METATYPE(Foo)
2337 void tst_QVariant::variant_to()
2346 sl << QLatin1String("blah");
2348 qVariantSetValue(v3, sl);
2353 qVariantSetValue(v4, foo);
2355 QCOMPARE(qvariant_cast<double>(v1), 4.2);
2356 QCOMPARE(qvariant_cast<float>(v1), 4.2f);
2357 QCOMPARE(qvariant_cast<int>(v2), 5);
2358 QCOMPARE(qvariant_cast<QStringList>(v3), sl);
2359 QCOMPARE(qvariant_cast<QString>(v3), QString::fromLatin1("blah"));
2361 QCOMPARE(qvariant_cast<Foo>(v4).i, 42);
2364 QCOMPARE(qvariant_cast<Foo>(v5).i, 0);
2366 QCOMPARE(qvariant_cast<int>(v1), 4);
2368 QVariant n = qVariantFromValue<short>(42);
2369 QCOMPARE(qvariant_cast<int>(n), 42);
2370 QCOMPARE(qvariant_cast<uint>(n), 42u);
2371 QCOMPARE(qvariant_cast<double>(n), 42.0);
2372 QCOMPARE(qvariant_cast<float>(n), 42.f);
2373 QCOMPARE(qvariant_cast<short>(n), short(42));
2374 QCOMPARE(qvariant_cast<ushort>(n), ushort(42));
2376 n = qVariantFromValue(43l);
2377 QCOMPARE(qvariant_cast<int>(n), 43);
2378 QCOMPARE(qvariant_cast<uint>(n), 43u);
2379 QCOMPARE(qvariant_cast<double>(n), 43.0);
2380 QCOMPARE(qvariant_cast<float>(n), 43.f);
2381 QCOMPARE(qvariant_cast<long>(n), 43l);
2383 n = QLatin1String("44");
2384 QCOMPARE(qvariant_cast<int>(n), 44);
2385 QCOMPARE(qvariant_cast<ulong>(n), 44ul);
2386 QCOMPARE(qvariant_cast<float>(n), 44.0f);
2388 QCOMPARE(qVariantFromValue(0.25f).toDouble(), 0.25);
2391 struct Blah { int i; };
2393 QDataStream& operator>>(QDataStream& s, Blah& c)
2394 { return (s >> c.i); }
2396 QDataStream& operator<<(QDataStream& s, const Blah& c)
2397 { return (s << c.i); }
2399 void tst_QVariant::saveLoadCustomTypes()
2404 int tp = qRegisterMetaType<Blah>("Blah");
2405 QVariant v = QVariant(tp, &i);
2407 qRegisterMetaTypeStreamOperators<Blah>("Blah");
2409 QCOMPARE(v.userType(), tp);
2410 QVERIFY(v.type() == QVariant::UserType);
2412 QDataStream stream(&data, QIODevice::WriteOnly);
2419 QDataStream stream(data);
2423 QCOMPARE(int(v.userType()), QMetaType::type("Blah"));
2424 int value = *(int*)v.constData();
2425 QCOMPARE(value, 42);
2428 void tst_QVariant::url()
2430 QString str("http://qt.nokia.com");
2433 QVariant v(url); //built with a QUrl
2437 QVariant v3(str); //built with a QString
2439 QCOMPARE(v2.toUrl(), url);
2440 QVERIFY(qVariantCanConvert<QUrl>(v3));
2441 QCOMPARE(v2.toUrl(), v3.toUrl());
2443 QVERIFY(qVariantCanConvert<QString>(v2));
2444 QCOMPARE(v2.toString(), str);
2445 QCOMPARE(v3.toString(), str);
2448 void tst_QVariant::variantMap()
2450 QMap<QString, QVariant> map;
2454 QVariantMap map2 = qvariant_cast<QVariantMap>(v);
2456 QCOMPARE(map2.value("test").toInt(), 42);
2458 QVariant v2 = QVariant(QMetaType::type("QVariantMap"), &map);
2459 QCOMPARE(qvariant_cast<QVariantMap>(v2).value("test").toInt(), 42);
2461 QVariant v3 = QVariant(QMetaType::type("QMap<QString, QVariant>"), &map);
2462 QCOMPARE(qvariant_cast<QVariantMap>(v3).value("test").toInt(), 42);
2465 void tst_QVariant::variantHash()
2467 QHash<QString, QVariant> hash;
2471 QVariantHash hash2 = qvariant_cast<QVariantHash>(v);
2473 QCOMPARE(hash2.value("test").toInt(), 42);
2475 QVariant v2 = QVariant(QMetaType::type("QVariantHash"), &hash);
2476 QCOMPARE(qvariant_cast<QVariantHash>(v2).value("test").toInt(), 42);
2478 QVariant v3 = QVariant(QMetaType::type("QHash<QString, QVariant>"), &hash);
2479 QCOMPARE(qvariant_cast<QVariantHash>(v3).value("test").toInt(), 42);
2482 void tst_QVariant::invalidQColor() const
2484 QVariant va("An invalid QColor::name() value.");
2485 QVERIFY(va.canConvert(QVariant::Color));
2487 QVERIFY(!va.convert(QVariant::Color));
2489 QVERIFY(!qvariant_cast<QColor>(va).isValid());
2492 class CustomQObject : public QObject {
2495 CustomQObject(QObject *parent = 0) : QObject(parent) {}
2497 class CustomQWidget : public QWidget {
2500 CustomQWidget(QWidget *parent = 0) : QWidget(parent) {}
2502 Q_DECLARE_METATYPE(CustomQObject*)
2503 Q_DECLARE_METATYPE(CustomQWidget*)
2505 class CustomNonQObject { };
2506 Q_DECLARE_METATYPE(CustomNonQObject)
2507 Q_DECLARE_METATYPE(CustomNonQObject*)
2509 void tst_QVariant::cleanupTestCase()
2511 delete customNonQObjectPointer;
2512 qDeleteAll(objectPointerTestData);
2515 void tst_QVariant::qvariant_cast_QObject_data()
2517 QTest::addColumn<QVariant>("data");
2518 QTest::addColumn<bool>("success");
2519 QObject *obj = new QObject;
2520 obj->setObjectName(QString::fromLatin1("Hello"));
2521 QTest::newRow("from QObject") << QVariant(QMetaType::QObjectStar, &obj) << true;
2522 QTest::newRow("from QObject2") << QVariant::fromValue(obj) << true;
2523 QTest::newRow("from String") << QVariant(QLatin1String("1, 2, 3")) << false;
2524 QTest::newRow("from int") << QVariant((int) 123) << false;
2525 QWidget *widget = new QWidget;
2526 widget->setObjectName(QString::fromLatin1("Hello"));
2527 QTest::newRow("from QWidget") << QVariant::fromValue(widget) << true;
2528 CustomQObject *customObject = new CustomQObject(this);
2529 customObject->setObjectName(QString::fromLatin1("Hello"));
2530 QTest::newRow("from Derived QObject") << QVariant::fromValue(customObject) << true;
2531 CustomQWidget *customWidget = new CustomQWidget;
2532 customWidget->setObjectName(QString::fromLatin1("Hello"));
2533 QTest::newRow("from Derived QWidget") << QVariant::fromValue(customWidget) << true;
2534 QTest::newRow("from custom Object") << QVariant::fromValue(CustomNonQObject()) << false;
2536 // Deleted in cleanupTestCase.
2537 customNonQObjectPointer = new CustomNonQObject;
2538 QTest::newRow("from custom ObjectStar") << QVariant::fromValue(customNonQObjectPointer) << false;
2540 // Deleted in cleanupTestCase.
2541 objectPointerTestData.push_back(obj);
2542 objectPointerTestData.push_back(widget);
2543 objectPointerTestData.push_back(customObject);
2544 objectPointerTestData.push_back(customWidget);
2547 void tst_QVariant::qvariant_cast_QObject()
2549 QFETCH(QVariant, data);
2550 QFETCH(bool, success);
2552 QObject *o = qvariant_cast<QObject *>(data);
2553 QCOMPARE(o != 0, success);
2555 QCOMPARE(o->objectName(), QString::fromLatin1("Hello"));
2559 class CustomQObjectDerived : public CustomQObject {
2562 CustomQObjectDerived(QObject *parent = 0) : CustomQObject(parent) {}
2564 Q_DECLARE_METATYPE(CustomQObjectDerived*)
2566 class CustomQObjectDerivedNoMetaType : public CustomQObject {
2569 CustomQObjectDerivedNoMetaType(QObject *parent = 0) : CustomQObject(parent) {}
2572 void tst_QVariant::qvariant_cast_QObject_derived()
2575 CustomQObjectDerivedNoMetaType *object = new CustomQObjectDerivedNoMetaType(this);
2576 QVariant data = QVariant::fromValue(object);
2577 QVERIFY(data.userType() == qMetaTypeId<CustomQObjectDerivedNoMetaType*>());
2578 QCOMPARE(data.value<QObject *>(), object);
2579 QCOMPARE(data.value<CustomQObjectDerivedNoMetaType *>(), object);
2580 QCOMPARE(data.value<CustomQObject *>(), object);
2581 QVERIFY(data.value<CustomQWidget*>() == 0);
2584 CustomQObjectDerived *object = new CustomQObjectDerived(this);
2585 QVariant data = QVariant::fromValue(object);
2587 QVERIFY(data.userType() == qMetaTypeId<CustomQObjectDerived*>());
2589 QCOMPARE(data.value<QObject *>(), object);
2590 QCOMPARE(data.value<CustomQObjectDerived *>(), object);
2591 QCOMPARE(data.value<CustomQObject *>(), object);
2592 QVERIFY(data.value<CustomQWidget*>() == 0);
2595 CustomQWidget customWidget;
2596 QWidget *widget = &customWidget;
2597 QVariant data = QVariant::fromValue(widget);
2598 QVERIFY(data.userType() == QMetaType::QWidgetStar);
2600 QCOMPARE(data.value<QObject*>(), widget);
2601 QCOMPARE(data.value<QWidget*>(), widget);
2602 QCOMPARE(data.value<CustomQWidget*>(), widget);
2606 Q_DECLARE_METATYPE(qint8);
2608 void tst_QVariant::convertToQUint8() const
2612 const qint8 anInt = 32;
2614 /* QVariant(int) gets invoked here so the QVariant has nothing with qint8 to do.
2615 * It's of type QVariant::Int. */
2616 const QVariant v0 = anInt;
2618 QVERIFY(qVariantCanConvert<qint8>(v0));
2619 QCOMPARE(int(qvariant_cast<qint8>(v0)), 32);
2620 QCOMPARE(int(v0.toInt()), 32);
2621 QCOMPARE(v0.toString(), QString("32"));
2623 QCOMPARE(int(qvariant_cast<qlonglong>(v0)), 32);
2624 QCOMPARE(int(qvariant_cast<char>(v0)), 32);
2625 QCOMPARE(int(qvariant_cast<short>(v0)), 32);
2626 QCOMPARE(int(qvariant_cast<long>(v0)), 32);
2627 QCOMPARE(int(qvariant_cast<float>(v0)), 32);
2628 QCOMPARE(int(qvariant_cast<double>(v0)), 32);
2633 const quint8 anInt = 32;
2634 const QVariant v0 = anInt;
2636 QVERIFY(qVariantCanConvert<quint8>(v0));
2637 QCOMPARE(int(qvariant_cast<quint8>(v0)), 32);
2638 QCOMPARE(int(v0.toUInt()), 32);
2639 QCOMPARE(v0.toString(), QString("32"));
2644 const qint16 anInt = 32;
2645 const QVariant v0 = anInt;
2647 QVERIFY(qVariantCanConvert<qint16>(v0));
2648 QCOMPARE(int(qvariant_cast<qint16>(v0)), 32);
2649 QCOMPARE(int(v0.toInt()), 32);
2650 QCOMPARE(v0.toString(), QString("32"));
2655 const quint16 anInt = 32;
2656 const QVariant v0 = anInt;
2658 QVERIFY(qVariantCanConvert<quint16>(v0));
2659 QCOMPARE(int(qvariant_cast<quint16>(v0)), 32);
2660 QCOMPARE(int(v0.toUInt()), 32);
2661 QCOMPARE(v0.toString(), QString("32"));
2665 void tst_QVariant::comparePointers() const
2673 QVariant v = qVariantFromValue<void *>(&myClass);
2674 QVariant v2 = qVariantFromValue<void *>(&myClass);
2680 Q_DECLARE_METATYPE(Data*)
2682 void tst_QVariant::voidStar() const
2689 v1 = qVariantFromValue(p1);
2693 v2 = qVariantFromValue(p2);
2697 v2 = qVariantFromValue(p2);
2701 void tst_QVariant::dataStar() const
2703 qRegisterMetaType<Data*>();
2704 Data *p1 = new Data;
2706 QVariant v1 = qVariantFromValue(p1);
2707 QCOMPARE(v1.userType(), qMetaTypeId<Data*>());
2708 QCOMPARE(qvariant_cast<Data*>(v1), p1);
2713 v2 = qVariantFromValue(p1);
2718 void tst_QVariant::canConvertQStringList() const
2720 QFETCH(bool, canConvert);
2721 QFETCH(QStringList, input);
2722 QFETCH(QString, result);
2726 QCOMPARE(v.canConvert(QVariant::String), canConvert);
2727 QCOMPARE(v.toString(), result);
2730 void tst_QVariant::canConvertQStringList_data() const
2732 QTest::addColumn<bool>("canConvert");
2733 QTest::addColumn<QStringList>("input");
2734 QTest::addColumn<QString>("result");
2736 QTest::newRow("An empty list") << false << QStringList() << QString();
2737 QTest::newRow("A single item") << true << QStringList(QLatin1String("foo")) << QString::fromLatin1("foo");
2738 QTest::newRow("A single, but empty item") << true << QStringList(QString()) << QString();
2743 QTest::newRow("Two items") << false << l << QString();
2746 QTest::newRow("Three items") << false << l << QString();
2749 template<typename T> void convertMetaType()
2751 QVERIFY(qVariantFromValue<T>(10).isValid());
2752 QVERIFY(qVariantFromValue<T>(10).canConvert(QVariant::Int));
2753 QCOMPARE(qVariantFromValue<T>(10).toInt(), 10);
2754 QCOMPARE(qVariantFromValue<T>(10), qVariantFromValue<T>(10));
2757 #define CONVERT_META_TYPE(Type) \
2758 convertMetaType<Type>(); \
2759 if (QTest::currentTestFailed()) \
2760 QFAIL("convertMetaType<" #Type "> failed");
2762 void tst_QVariant::canConvertMetaTypeToInt() const
2764 CONVERT_META_TYPE(long);
2765 CONVERT_META_TYPE(short);
2766 CONVERT_META_TYPE(short);
2767 CONVERT_META_TYPE(unsigned short);
2768 CONVERT_META_TYPE(ushort);
2769 CONVERT_META_TYPE(ulong);
2770 CONVERT_META_TYPE(unsigned long);
2771 CONVERT_META_TYPE(uchar);
2772 CONVERT_META_TYPE(unsigned char);
2773 CONVERT_META_TYPE(char);
2774 CONVERT_META_TYPE(uint);
2775 CONVERT_META_TYPE(unsigned int);
2778 #undef CONVERT_META_TYPE
2781 These calls should not produce any warnings.
2783 void tst_QVariant::variantToDateTimeWithoutWarnings() const
2786 const QVariant variant(QLatin1String("An invalid QDateTime string"));
2787 const QDateTime dateTime(variant.toDateTime());
2788 QVERIFY(!dateTime.isValid());
2792 QVariant v1(QLatin1String("xyz"));
2793 v1.convert(QVariant::DateTime);
2795 QVariant v2(QLatin1String("xyz"));
2796 QDateTime dt1(v2.toDateTime());
2798 const QVariant v3(QLatin1String("xyz"));
2799 const QDateTime dt2(v3.toDateTime());
2803 void tst_QVariant::invalidDateTime() const
2805 QVariant variant(QString::fromLatin1("Invalid date time string"));
2806 QVERIFY(!variant.toDateTime().isValid());
2807 QVERIFY(!variant.convert(QVariant::DateTime));
2812 MyClass() : myValue(0) {}
2816 Q_DECLARE_METATYPE( MyClass )
2818 void tst_QVariant::loadUnknownUserType()
2820 qRegisterMetaType<MyClass>("MyClass");
2821 char data[] = {0, 0, QMetaType::User >> 8 , char(QMetaType::User), 0, 0, 0, 0, 8, 'M', 'y', 'C', 'l', 'a', 's', 's', 0};
2823 QByteArray ba(data, sizeof(data));
2824 QDataStream ds(&ba, QIODevice::ReadOnly);
2827 QCOMPARE(ds.status(), QDataStream::ReadCorruptData);
2830 void tst_QVariant::loadBrokenUserType()
2832 char data[] = {0, 0, 0, 127, 0, 112 };
2834 QByteArray ba(data, sizeof(data));
2835 QDataStream ds(&ba, QIODevice::ReadOnly);
2838 QCOMPARE(ds.status(), QDataStream::ReadPastEnd);
2841 void tst_QVariant::invalidDate() const
2843 QString foo("Hello");
2844 QVariant variant(foo);
2845 QVERIFY(!variant.convert(QVariant::Date));
2848 QVERIFY(!variant.convert(QVariant::DateTime));
2851 QVERIFY(!variant.convert(QVariant::Time));
2854 QVERIFY(!variant.convert(QVariant::Int));
2857 QVERIFY(!variant.convert(QVariant::Double));
2860 QVERIFY(!variant.convert(QVariant::Type(QMetaType::Float)));
2865 int x,y,z,q,w,e,r,t;
2867 Q_DECLARE_METATYPE(WontCompare);
2869 void tst_QVariant::compareCustomTypes() const
2871 qRegisterMetaType<WontCompare>("WontCompare");
2875 const QVariant variant1(qVariantFromValue(f1));
2879 const QVariant variant2(qVariantFromValue(f2));
2881 /* We compare pointers. */
2882 QVERIFY(variant1 != variant2);
2883 QVERIFY(variant1 == variant1);
2884 QVERIFY(variant2 == variant2);
2887 void tst_QVariant::timeToDateTime() const
2889 const QVariant val(QTime::currentTime());
2890 QVERIFY(!val.canConvert(QVariant::DateTime));
2891 QVERIFY(!val.toDateTime().isValid());
2894 Q_DECLARE_METATYPE(QHostAddress)
2896 void tst_QVariant::copyingUserTypes() const
2900 const QHostAddress ha("127.0.0.1");
2901 qVariantSetValue(var, ha);
2904 QCOMPARE(qVariantValue<QHostAddress>(var3), ha);
2907 void tst_QVariant::convertBoolToByteArray() const
2909 QFETCH(QByteArray, input);
2910 QFETCH(bool, canConvert);
2911 QFETCH(bool, value);
2913 const QVariant variant(input);
2915 QCOMPARE(qVariantCanConvert<bool>(variant), canConvert);
2918 /* Just call this function so we run the code path. */
2919 QCOMPARE(variant.toBool(), value);
2923 void tst_QVariant::convertBoolToByteArray_data() const
2925 QTest::addColumn<QByteArray>("input");
2926 QTest::addColumn<bool>("canConvert");
2927 QTest::addColumn<bool>("value");
2929 QTest::newRow("false")
2930 << QByteArray("false")
2934 QTest::newRow("FALSE")
2935 << QByteArray("FALSE")
2939 QTest::newRow("falSE")
2940 << QByteArray("FALSE")
2949 QTest::newRow("null QByteArray")
2954 QTest::newRow("any-content")
2955 << QByteArray("any-content")
2959 QTest::newRow("true")
2960 << QByteArray("true")
2964 QTest::newRow("TRUE")
2965 << QByteArray("TRUE")
2969 QTest::newRow("trUE")
2970 << QByteArray("trUE")
2975 void tst_QVariant::convertByteArrayToBool() const
2977 QFETCH(bool, input);
2978 QFETCH(QByteArray, output);
2980 const QVariant variant(input);
2981 QCOMPARE(variant.type(), QVariant::Bool);
2982 QCOMPARE(variant.toBool(), input);
2983 QVERIFY(qVariantCanConvert<bool>(variant));
2985 QCOMPARE(variant.toByteArray(), output);
2988 void tst_QVariant::convertByteArrayToBool_data() const
2990 QTest::addColumn<bool>("input");
2991 QTest::addColumn<QByteArray>("output");
2993 QTest::newRow("false")
2995 << QByteArray("false");
2997 QTest::newRow("true")
2999 << QByteArray("true");
3004 1. Converting the string "9.9" to int fails. This is the behavior of
3005 toLongLong() and hence also QVariant, since it uses it.
3006 2. Converting the QVariant containing the double 9.9 to int works.
3008 Rationale: "9.9" is not a valid int. However, doubles are by definition not
3009 ints and therefore it makes more sense to perform conversion for those.
3011 void tst_QVariant::toIntFromQString() const
3013 QVariant first("9.9");
3015 QCOMPARE(first.toInt(&ok), 0);
3018 QCOMPARE(QString("9.9").toLongLong(&ok), qlonglong(0));
3022 QCOMPARE(v.toInt(&ok), 10);
3028 1. Conversion from (64 bit) double to int works (no overflow).
3029 2. Same conversion works for QVariant::convert.
3031 Rationale: if 2147483630 is set in float and then converted to int,
3032 there will be overflow and the result will be -2147483648.
3034 void tst_QVariant::toIntFromDouble() const
3036 double d = 2147483630; // max int 2147483647
3037 QVERIFY((int)d == 2147483630);
3040 QVERIFY( var.canConvert( QVariant::Int ) );
3043 int result = var.toInt(&ok);
3045 QVERIFY( ok == true );
3046 QCOMPARE(result, 2147483630);
3049 void tst_QVariant::setValue()
3051 QTransform t; //we just take a value so that we're sure that it will be shared
3053 QVERIFY( v1.isDetached() );
3055 QVERIFY( !v1.isDetached() );
3056 QVERIFY( !v2.isDetached() );
3058 qVariantSetValue(v2, 3); //set an integer value
3060 QVERIFY( v1.isDetached() );
3061 QVERIFY( v2.isDetached() );
3064 void tst_QVariant::numericalConvert()
3066 #if defined(Q_OS_LINUX) && defined(Q_CC_GNU) && !defined(__x86_64__)
3067 QSKIP("Known to fail due to a GCC bug on at least Ubuntu 10.04 32-bit - check QTBUG-8959");
3069 QVariant vfloat(float(5.3));
3070 QVariant vdouble(double(5.3));
3071 QVariant vreal(qreal(5.3));
3072 QVariant vint(int(5));
3073 QVariant vuint(uint(5));
3074 QVariant vshort(short(5));
3075 QVariant vlonglong(quint64(5));
3076 QVariant vstringint(QString::fromLatin1("5"));
3077 QVariant vstring(QString::fromLatin1("5.3"));
3079 QVector<QVariant *> vect;
3080 vect << &vfloat << &vdouble << &vreal << &vint << &vuint << &vshort<< &vlonglong << &vstringint << &vstring;
3082 for(int i = 0; i < vect.size(); i++) {
3084 if (i >= 3 && i <= 7)
3086 QVariant *v = vect.at(i);
3087 QCOMPARE(v->toFloat() , float(num));
3088 QCOMPARE(float(v->toReal()) , float(num));
3089 QCOMPARE(float(v->toDouble()) , float(num));
3091 QCOMPARE(v->toInt() , int(num));
3092 QCOMPARE(v->toUInt() , uint(num));
3093 QCOMPARE(v->toULongLong() , quint64(num));
3095 QCOMPARE(v->toString() , QString::number(num));
3100 template<class T> void playWithVariant(const T &orig, bool isNull, const QString &toString, double toDouble, bool toBool)
3102 QVariant v = QVariant::fromValue(orig);
3103 QVERIFY(v.isValid());
3104 QCOMPARE(v.isNull(), isNull);
3105 QCOMPARE(v.toString(), toString);
3106 QCOMPARE(v.toDouble(), toDouble);
3107 QCOMPARE(v.toBool(), toBool);
3108 QCOMPARE(qvariant_cast<T>(v), orig);
3112 if (!(QTypeInfo<T>::isStatic && QTypeInfo<T>::isComplex)) {
3113 // Type is movable so standard comparison algorithm in QVariant should work
3114 // In a custom type QVariant is not aware of ==operator so it won't be called,
3115 // which may cause problems especially visible when using a not-movable type
3118 QVERIFY(v2.isValid());
3119 QCOMPARE(v2.isNull(), isNull);
3120 QCOMPARE(v2.toString(), toString);
3121 QCOMPARE(v2.toDouble(), toDouble);
3122 QCOMPARE(v2.toBool(), toBool);
3123 QCOMPARE(qvariant_cast<T>(v2), orig);
3129 if (!(QTypeInfo<T>::isStatic && QTypeInfo<T>::isComplex)) {
3130 // Type is movable so standard comparison algorithm in QVariant should work
3131 // In a custom type QVariant is not aware of ==operator so it won't be called,
3132 // which may cause problems especially visible when using a not-movable type
3135 QCOMPARE(qvariant_cast<T>(v2), qvariant_cast<T>(v));
3136 QCOMPARE(v2.toString(), toString);
3137 v3 = qVariantFromValue(orig);
3139 QVERIFY(v3.isValid());
3140 QCOMPARE(v3.isNull(), isNull);
3141 QCOMPARE(v3.toString(), toString);
3142 QCOMPARE(v3.toDouble(), toDouble);
3143 QCOMPARE(v3.toBool(), toBool);
3144 QCOMPARE(qvariant_cast<T>(v3), qvariant_cast<T>(v));
3147 QVERIFY(v.isValid());
3148 QCOMPARE(v.isNull(), isNull);
3149 QCOMPARE(v.toString(), toString);
3150 QCOMPARE(v.toDouble(), toDouble);
3151 QCOMPARE(v.toBool(), toBool);
3152 QCOMPARE(qvariant_cast<T>(v), orig);
3154 if (qMetaTypeId<T>() != qMetaTypeId<QVariant>()) {
3155 QCOMPARE(v.userType(), qMetaTypeId<T>());
3156 QCOMPARE(QVariant::typeToName(QVariant::Type(v.userType())), QMetaType::typeName(qMetaTypeId<T>()));
3160 #define PLAY_WITH_VARIANT(Orig, IsNull, ToString, ToDouble, ToBool) \
3161 playWithVariant(Orig, IsNull, ToString, ToDouble, ToBool);\
3162 if (QTest::currentTestFailed())\
3163 QFAIL("playWithVariant failed");
3168 bool operator==(const MyPrimitive &o) const
3170 return x == o.x && y == o.y;
3175 Q_DECLARE_TYPEINFO(MyPrimitive, Q_PRIMITIVE_TYPE);
3181 MyData() : ptr(this) {}
3184 if (ptr != this) qWarning("%s: object has moved", Q_FUNC_INFO);
3186 MyData(const MyData& o) : ptr(this)
3188 if (o.ptr != &o) qWarning("%s: other object has moved", Q_FUNC_INFO);
3190 MyData &operator=(const MyData &o)
3192 if (ptr != this) qWarning("%s: object has moved", Q_FUNC_INFO);
3193 if (o.ptr != &o) qWarning("%s: other object has moved", Q_FUNC_INFO);
3196 bool operator==(const MyData &o) const
3198 if (ptr != this) qWarning("%s: object has moved", Q_FUNC_INFO);
3199 if (o.ptr != &o) qWarning("%s: other object has moved", Q_FUNC_INFO);
3208 MyMovable() { v = count++; }
3209 ~MyMovable() { count--; }
3210 MyMovable(const MyMovable &o) : v(o.v) { count++; }
3212 bool operator==(const MyMovable &o) const
3218 int MyMovable::count = 0;
3224 MyNotMovable() : that(this) { count++; }
3225 ~MyNotMovable() { QCOMPARE(that, this); count--; }
3226 MyNotMovable(const MyNotMovable &o) : that(this) { QCOMPARE(o.that, &o); count++; }
3227 MyNotMovable &operator=(const MyNotMovable &o) {
3228 bool ok = that == this && o.that == &o;
3229 if (!ok) qFatal("MyNotMovable has been moved");
3233 //PLAY_WITH_VARIANT test that they are equal, but never that they are not equal
3234 // so it would be fine just to always return true
3235 bool operator==(const MyNotMovable &o) const
3237 bool ok = that == this && o.that == &o;
3238 if (!ok) qFatal("MyNotMovable has been moved");
3243 int MyNotMovable::count = 0;
3245 struct MyShared : QSharedData {
3250 Q_DECLARE_TYPEINFO(MyMovable, Q_MOVABLE_TYPE);
3253 Q_DECLARE_METATYPE(QList<QSize>)
3254 Q_DECLARE_METATYPE(MyPrimitive)
3255 Q_DECLARE_METATYPE(MyData)
3256 Q_DECLARE_METATYPE(MyMovable)
3257 Q_DECLARE_METATYPE(MyNotMovable)
3258 Q_DECLARE_METATYPE(QList<MyPrimitive>)
3259 Q_DECLARE_METATYPE(QList<MyData>)
3260 Q_DECLARE_METATYPE(QList<MyMovable>)
3261 Q_DECLARE_METATYPE(QList<MyNotMovable>)
3262 Q_DECLARE_METATYPE(MyPrimitive *)
3263 Q_DECLARE_METATYPE(MyData *)
3264 Q_DECLARE_METATYPE(MyMovable *)
3265 Q_DECLARE_METATYPE(MyNotMovable *)
3266 Q_DECLARE_METATYPE(QSharedDataPointer<MyShared>)
3269 void tst_QVariant::moreCustomTypes()
3272 QList<QSize> listSize;
3273 PLAY_WITH_VARIANT(listSize, false, QString(), 0, false);
3274 listSize << QSize(4,5) << QSize(89,23) << QSize(5,6);
3275 PLAY_WITH_VARIANT(listSize, false, QString(), 0, false);
3280 PLAY_WITH_VARIANT(str, true, QString(), 0, false);
3281 str = QString::fromLatin1("123456789.123");
3282 PLAY_WITH_VARIANT(str, false, str, 123456789.123, true);
3287 PLAY_WITH_VARIANT(size, false, QString(), 0, false);
3288 PLAY_WITH_VARIANT(QSize(45,78), false, QString(), 0, false);
3293 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3294 PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3296 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3297 l << MyData() << MyData();
3298 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3302 MyPrimitive d = { 4, 5 };
3303 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3304 PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3305 QList<MyPrimitive> l;
3306 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3308 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3313 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3314 PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3316 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3317 l << MyMovable() << d;
3318 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3320 QCOMPARE(MyMovable::count, 0);
3322 QCOMPARE(MyNotMovable::count, 0);
3325 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3326 PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3327 QList<MyNotMovable> l;
3328 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3329 l << MyNotMovable() << d;
3330 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3332 QCOMPARE(MyNotMovable::count, 0);
3335 PLAY_WITH_VARIANT(12.12, false, "12.12", 12.12, true);
3336 PLAY_WITH_VARIANT(12.12f, false, "12.12", 12.12f, true);
3337 PLAY_WITH_VARIANT('a', false, "a", 'a', true);
3338 PLAY_WITH_VARIANT((unsigned char)('a'), false, "a", 'a', true);
3339 PLAY_WITH_VARIANT( quint8(12), false, "\xc", 12, true);
3340 PLAY_WITH_VARIANT( qint8(13), false, "\xd", 13, true);
3341 PLAY_WITH_VARIANT(quint16(14), false, "14", 14, true);
3342 PLAY_WITH_VARIANT( qint16(15), false, "15", 15, true);
3343 PLAY_WITH_VARIANT(quint32(16), false, "16", 16, true);
3344 PLAY_WITH_VARIANT( qint32(17), false, "17", 17, true);
3345 PLAY_WITH_VARIANT(quint64(18), false, "18", 18, true);
3346 PLAY_WITH_VARIANT( qint64(19), false, "19", 19, true);
3347 PLAY_WITH_VARIANT( qint8(-12), false, "\xf4", -12, true);
3348 PLAY_WITH_VARIANT( qint16(-13), false, "-13", -13, true);
3349 PLAY_WITH_VARIANT( qint32(-14), false, "-14", -14, true);
3350 PLAY_WITH_VARIANT( qint64(-15), false, "-15", -15, true);
3351 PLAY_WITH_VARIANT(quint64(0), false, "0", 0, false);
3352 PLAY_WITH_VARIANT( true, false, "true", 1, true);
3353 PLAY_WITH_VARIANT( false, false, "false", 0, false);
3355 PLAY_WITH_VARIANT(QString("hello\n"), false, "hello\n", 0, true);
3360 PLAY_WITH_VARIANT((void *)(&i), false, QString(), 0, false);
3361 PLAY_WITH_VARIANT((void *)(0), false, QString(), 0, false);
3365 QVariant v1 = QVariant::fromValue(5);
3366 QVariant v2 = QVariant::fromValue(5.0);
3367 QVariant v3 = QVariant::fromValue(quint16(5));
3369 QVariant v5 = QVariant::fromValue(MyPrimitive());
3370 QVariant v6 = QVariant::fromValue(MyMovable());
3371 QVariant v7 = QVariant::fromValue(MyData());
3372 PLAY_WITH_VARIANT(v1, false, "5", 5, true);
3373 PLAY_WITH_VARIANT(v2, false, "5", 5, true);
3374 PLAY_WITH_VARIANT(v3, false, "5", 5, true);
3375 PLAY_WITH_VARIANT(v4, false, "5", 5, true);
3377 PLAY_WITH_VARIANT(v5, false, QString(), 0, false);
3380 QCOMPARE(MyMovable::count, 0);
3382 QSharedDataPointer<MyShared> d(new MyShared);
3383 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3385 QCOMPARE(MyMovable::count, 0);
3388 QList<QList<int> > data;
3389 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3390 data << (QList<int>() << 42);
3391 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3395 QList<QVector<int> > data;
3396 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3397 data << (QVector<int>() << 42);
3398 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3402 QList<QSet<int> > data;
3403 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3404 data << (QSet<int>() << 42);
3405 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3409 QList<QLinkedList<int> > data;
3410 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3411 data << (QLinkedList<int>() << 42);
3412 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3416 void tst_QVariant::movabilityTest()
3418 // This test checks if QVariant is movable even if an internal data is not movable.
3419 QVERIFY(!MyNotMovable::count);
3421 QVariant variant = QVariant::fromValue(MyNotMovable());
3422 QVERIFY(MyNotMovable::count);
3424 // prepare destination memory space to which variant will be moved
3426 QCOMPARE(buffer[0].type(), QVariant::Invalid);
3427 buffer[0].~QVariant();
3429 memcpy(buffer, &variant, sizeof(QVariant));
3430 QCOMPARE(buffer[0].type(), QVariant::UserType);
3431 QCOMPARE(buffer[0].userType(), qMetaTypeId<MyNotMovable>());
3432 MyNotMovable tmp(buffer[0].value<MyNotMovable>());
3434 new (&variant) QVariant();
3436 QVERIFY(!MyNotMovable::count);
3439 void tst_QVariant::variantInVariant()
3442 QCOMPARE(var1.type(), QVariant::Int);
3443 QVariant var2 = var1;
3444 QCOMPARE(var2, var1);
3445 QCOMPARE(var2.type(), QVariant::Int);
3446 QVariant var3 = QVariant::fromValue(var1);
3447 QCOMPARE(var3, var1);
3448 QCOMPARE(var3.type(), QVariant::Int);
3449 QVariant var4 = qvariant_cast<QVariant>(var1);
3450 QCOMPARE(var4, var1);
3451 QCOMPARE(var4.type(), QVariant::Int);
3454 QCOMPARE(var5, var1);
3455 QCOMPARE(var5.type(), QVariant::Int);
3457 var6.setValue(var1);
3458 QCOMPARE(var6, var1);
3459 QCOMPARE(var6.type(), QVariant::Int);
3461 QCOMPARE(QVariant::fromValue(var1), QVariant::fromValue(var2));
3462 QCOMPARE(qvariant_cast<QVariant>(var3), QVariant::fromValue(var4));
3463 QCOMPARE(qvariant_cast<QVariant>(var5), qvariant_cast<QVariant>(var6));
3465 QString str("hello");
3466 QVariant var8 = qvariant_cast<QVariant>(QVariant::fromValue(QVariant::fromValue(str)));
3467 QCOMPARE((int)var8.type(), (int)QVariant::String);
3468 QCOMPARE(qvariant_cast<QString>(QVariant(qvariant_cast<QVariant>(var8))), str);
3470 QVariant var9(qMetaTypeId<QVariant>(), &var1);
3471 QCOMPARE(var9.userType(), qMetaTypeId<QVariant>());
3472 QCOMPARE(qvariant_cast<QVariant>(var9), var1);
3475 void tst_QVariant::colorInteger()
3477 QVariant v = QColor(Qt::red);
3478 QCOMPARE(v.type(), QVariant::Color);
3479 QCOMPARE(v.value<QColor>(), QColor(Qt::red));
3482 QCOMPARE(v.type(), QVariant::Int);
3483 QCOMPARE(v.toInt(), 1000);
3485 v.setValue(QColor(Qt::yellow));
3486 QCOMPARE(v.type(), QVariant::Color);
3487 QCOMPARE(v.value<QColor>(), QColor(Qt::yellow));
3491 Q_DECLARE_OPAQUE_POINTER(Forward*)
3492 Q_DECLARE_METATYPE(Forward*)
3494 void tst_QVariant::forwardDeclare()
3497 QVariant v = QVariant::fromValue(f);
3498 QCOMPARE(qvariant_cast<Forward*>(v), f);
3501 void tst_QVariant::loadQt5Stream_data()
3503 dataStream_data(QDataStream::Qt_5_0);
3506 void tst_QVariant::loadQt5Stream()
3508 loadQVariantFromDataStream(QDataStream::Qt_5_0);
3511 void tst_QVariant::saveQt5Stream_data()
3513 dataStream_data(QDataStream::Qt_5_0);
3516 void tst_QVariant::saveQt5Stream()
3518 saveQVariantFromDataStream(QDataStream::Qt_5_0);
3521 void tst_QVariant::loadQt4Stream_data()
3523 dataStream_data(QDataStream::Qt_4_9);
3526 void tst_QVariant::loadQt4Stream()
3528 loadQVariantFromDataStream(QDataStream::Qt_4_9);
3531 void tst_QVariant::saveQt4Stream_data()
3533 dataStream_data(QDataStream::Qt_4_9);
3536 void tst_QVariant::saveQt4Stream()
3538 saveQVariantFromDataStream(QDataStream::Qt_4_9);
3541 void tst_QVariant::dataStream_data(QDataStream::Version version)
3543 QTest::addColumn<QString>("fileName");
3547 case QDataStream::Qt_4_9:
3548 path = QString::fromLatin1("qt4.9");
3550 case QDataStream::Qt_5_0:
3551 path = QString::fromLatin1("qt5.0");
3557 path = path.prepend(":/stream/").append("/");
3560 foreach (const QFileInfo &fileInfo, dir.entryInfoList(QStringList() << "*.bin")) {
3561 QTest::newRow((path + fileInfo.fileName()).toLatin1()) << fileInfo.filePath();
3567 void tst_QVariant::loadQVariantFromDataStream(QDataStream::Version version)
3569 QFETCH(QString, fileName);
3571 QFile file(fileName);
3572 QVERIFY(file.open(QIODevice::ReadOnly));
3574 QDataStream stream(&file);
3575 stream.setVersion(version);
3578 QVariant loadedVariant;
3579 stream >> typeName >> loadedVariant;
3581 const int id = QMetaType::type(typeName.toLatin1());
3582 if (id == QMetaType::Void) {
3583 // Void type is not supported by QVariant
3587 QVariant constructedVariant(static_cast<QVariant::Type>(id));
3588 QCOMPARE(constructedVariant.userType(), id);
3589 QCOMPARE(QMetaType::typeName(loadedVariant.userType()), typeName.toLatin1().constData());
3590 QCOMPARE(loadedVariant.userType(), constructedVariant.userType());
3593 void tst_QVariant::saveQVariantFromDataStream(QDataStream::Version version)
3595 QFETCH(QString, fileName);
3597 QFile file(fileName);
3598 QVERIFY(file.open(QIODevice::ReadOnly));
3599 QDataStream dataFileStream(&file);
3602 dataFileStream >> typeName;
3603 QByteArray data = file.readAll();
3604 const int id = QMetaType::type(typeName.toLatin1());
3605 if (id == QMetaType::Void) {
3606 // Void type is not supported by QVariant
3611 buffer.open(QIODevice::ReadWrite);
3612 QDataStream stream(&buffer);
3613 stream.setVersion(version);
3615 QVariant constructedVariant(static_cast<QVariant::Type>(id));
3616 QCOMPARE(constructedVariant.userType(), id);
3617 stream << constructedVariant;
3619 // We are testing QVariant there is no point in testing full array.
3620 QCOMPARE(buffer.data().left(5), data.left(5));
3623 QVariant recunstructedVariant;
3624 stream >> recunstructedVariant;
3625 QCOMPARE(recunstructedVariant.userType(), constructedVariant.userType());
3628 class MessageHandler {
3630 MessageHandler(const int typeId, QtMsgHandler msgHandler = handler)
3631 : oldMsgHandler(qInstallMsgHandler(msgHandler))
3638 qInstallMsgHandler(oldMsgHandler);
3641 bool testPassed() const
3646 static void handler(QtMsgType, const char *txt)
3648 QString msg = QString::fromLatin1(txt);
3649 // Format itself is not important, but basic data as a type name should be included in the output
3650 ok = msg.startsWith("QVariant(");
3651 QVERIFY2(ok, (QString::fromLatin1("Message is not started correctly: '") + msg + '\'').toLatin1().constData());
3652 ok &= (currentId == QMetaType::UnknownType
3653 ? msg.contains("Invalid")
3654 : msg.contains(QMetaType::typeName(currentId)));
3655 QVERIFY2(ok, (QString::fromLatin1("Message doesn't contain type name: '") + msg + '\'').toLatin1().constData());
3656 if (currentId == QMetaType::Char || currentId == QMetaType::QChar) {
3657 // Chars insert '\0' into the qdebug stream, it is not possible to find a real string length
3660 if (QMetaType::typeFlags(currentId) & QMetaType::PointerToQObject) {
3661 QByteArray currentName = QMetaType::typeName(currentId);
3662 currentName.chop(1);
3663 ok &= (msg.contains(", " + currentName) || msg.contains(", 0x0"));
3665 ok &= msg.endsWith(") ");
3666 QVERIFY2(ok, (QString::fromLatin1("Message is not correctly finished: '") + msg + '\'').toLatin1().constData());
3670 QtMsgHandler oldMsgHandler;
3671 static int currentId;
3674 bool MessageHandler::ok;
3675 int MessageHandler::currentId;
3677 void tst_QVariant::debugStream_data()
3679 QTest::addColumn<QVariant>("variant");
3680 QTest::addColumn<int>("typeId");
3681 for (int id = 0; id < QMetaType::User; ++id) {
3682 const char *tagName = QMetaType::typeName(id);
3685 if (id != QMetaType::Void) {
3686 QTest::newRow(tagName) << QVariant(static_cast<QVariant::Type>(id)) << id;
3689 QTest::newRow("QBitArray(111)") << QVariant(QBitArray(3, true)) << qMetaTypeId<QBitArray>();
3690 QTest::newRow("CustomStreamableClass") << QVariant(qMetaTypeId<CustomStreamableClass>(), 0) << qMetaTypeId<CustomStreamableClass>();
3691 QTest::newRow("MyClass") << QVariant(qMetaTypeId<MyClass>(), 0) << qMetaTypeId<MyClass>();
3692 QTest::newRow("InvalidVariant") << QVariant() << int(QMetaType::UnknownType);
3693 QTest::newRow("CustomQObject") << QVariant::fromValue(this) << qMetaTypeId<tst_QVariant*>();
3696 void tst_QVariant::debugStream()
3698 QFETCH(QVariant, variant);
3699 QFETCH(int, typeId);
3701 MessageHandler msgHandler(typeId);
3702 qDebug() << variant;
3703 QVERIFY(msgHandler.testPassed());
3706 struct MessageHandlerType : public MessageHandler
3708 MessageHandlerType(const int typeId)
3709 : MessageHandler(typeId, handler)
3711 static void handler(QtMsgType, const char *txt)
3713 QString msg = QString::fromLatin1(txt);
3714 // Format itself is not important, but basic data as a type name should be included in the output
3715 ok = msg.startsWith("QVariant::");
3716 QVERIFY2(ok, (QString::fromLatin1("Message is not started correctly: '") + msg + '\'').toLatin1().constData());
3717 ok &= (currentId == QMetaType::UnknownType
3718 ? msg.contains("Invalid")
3719 : msg.contains(QMetaType::typeName(currentId)));
3720 QVERIFY2(ok, (QString::fromLatin1("Message doesn't contain type name: '") + msg + '\'').toLatin1().constData());
3724 void tst_QVariant::debugStreamType_data()
3729 void tst_QVariant::debugStreamType()
3731 QFETCH(QVariant, variant);
3732 QFETCH(int, typeId);
3734 MessageHandlerType msgHandler(typeId);
3735 qDebug() << QVariant::Type(typeId);
3736 QVERIFY(msgHandler.testPassed());
3739 void tst_QVariant::guiVariantAtExit()
3741 // crash test, it should not crash at QGuiApplication exit
3742 static QVariant cursor = QCursor();
3743 static QVariant point = QPoint();
3744 static QVariant image = QImage();
3745 static QVariant pallete = QPalette();
3753 void tst_QVariant::widgetsVariantAtExit()
3755 // crash test, it should not crash at QGuiApplication exit
3756 static QVariant icon= QIcon();
3757 static QVariant sizePolicy = QSizePolicy();
3759 Q_UNUSED(sizePolicy);
3763 QTEST_MAIN(tst_QVariant)
3764 #include "tst_qvariant.moc"