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();
243 void convertToQUint8() const;
244 void invalidQColor() const;
245 void comparePointers() const;
246 void voidStar() const;
247 void dataStar() const;
248 void canConvertQStringList() const;
249 void canConvertQStringList_data() const;
250 void canConvertMetaTypeToInt() const;
251 void variantToDateTimeWithoutWarnings() const;
252 void invalidDateTime() const;
254 void loadUnknownUserType();
255 void loadBrokenUserType();
257 void invalidDate() const;
258 void compareCustomTypes() const;
259 void timeToDateTime() const;
260 void copyingUserTypes() const;
261 void convertBoolToByteArray() const;
262 void convertBoolToByteArray_data() const;
263 void convertByteArrayToBool() const;
264 void convertByteArrayToBool_data() const;
265 void toIntFromQString() const;
266 void toIntFromDouble() const;
269 void numericalConvert();
270 void moreCustomTypes();
271 void movabilityTest();
272 void variantInVariant();
276 void forwardDeclare();
277 void debugStream_data();
279 void debugStreamType_data();
280 void debugStreamType();
282 void loadQt4Stream_data();
283 void loadQt4Stream();
284 void saveQt4Stream_data();
285 void saveQt4Stream();
286 void loadQt5Stream_data();
287 void loadQt5Stream();
288 void saveQt5Stream_data();
289 void saveQt5Stream();
291 void guiVariantAtExit();
292 void widgetsVariantAtExit();
294 void dataStream_data(QDataStream::Version version);
295 void loadQVariantFromDataStream(QDataStream::Version version);
296 void saveQVariantFromDataStream(QDataStream::Version version);
298 CustomNonQObject *customNonQObjectPointer;
299 QVector<QObject*> objectPointerTestData;
302 Q_DECLARE_METATYPE(QDate)
303 Q_DECLARE_METATYPE(QTime)
304 Q_DECLARE_METATYPE(QDateTime)
305 Q_DECLARE_METATYPE(QVariant)
307 const qlonglong intMax1 = (qlonglong)INT_MAX + 1;
308 const qulonglong uintMax1 = (qulonglong)UINT_MAX + 1;
310 void tst_QVariant::constructor()
313 QVERIFY( !variant.isValid() );
314 QVERIFY( variant.isNull() );
316 QVariant var2(variant);
317 QVERIFY( !var2.isValid() );
318 QVERIFY( variant.isNull() );
320 QVariant varll(intMax1);
321 QVariant varll2(varll);
322 QCOMPARE(varll2, varll);
324 QVariant var3(QVariant::String);
325 QCOMPARE(var3.typeName(), "QString");
326 QVERIFY(var3.isNull());
327 QVERIFY(var3.isValid());
329 QVariant var4(QVariant::Invalid);
330 QCOMPARE(var4.type(), QVariant::Invalid);
331 QVERIFY(var4.isNull());
332 QVERIFY(!var4.isValid());
334 QVariant var5(QLatin1String("hallo"));
335 QCOMPARE(var5.type(), QVariant::String);
336 QCOMPARE(var5.typeName(), "QString");
338 QVariant var6(qlonglong(0));
339 QCOMPARE(var6.type(), QVariant::LongLong);
340 QCOMPARE(var6.typeName(), "qlonglong");
343 QVERIFY(var7.isValid());
344 QVERIFY(!var7.isNull());
346 var8.setValue<int>(5);
347 QVERIFY(var8.isValid());
348 QVERIFY(!var8.isNull());
351 void tst_QVariant::copy_constructor()
353 QVariant var7(QVariant::Int);
355 QCOMPARE(var8.type(), QVariant::Int);
356 QVERIFY(var8.isNull());
359 void tst_QVariant::isNull()
362 QVERIFY( var.isNull() );
365 QVariant var1( str1 );
366 QVERIFY( var1.isNull() );
368 QVariant var2( QString::null );
369 QVERIFY( var2.isNull() );
371 QVariant var3( QString( "blah" ) );
372 QVERIFY( !var3.isNull() );
375 QVERIFY( !var4.isNull() );
377 QVariant var5 = QString();
378 QVERIFY( var5.isNull() );
380 QVariant var6( QString( "blah" ) );
381 QVERIFY( !var6.isNull() );
383 QVERIFY( var6.isNull() );
384 var6.convert( QVariant::String );
385 QVERIFY( var6.isNull() );
386 QVariant varLL( (qlonglong)0 );
387 QVERIFY( !varLL.isNull() );
388 QVariant var7(QString::null);
389 QVERIFY(var7.isNull());
392 void tst_QVariant::swap()
394 QVariant v1 = 1, v2 = 2.0;
396 QCOMPARE(v1.type(),QVariant::Double);
397 QCOMPARE(v1.toDouble(),2.0);
398 QCOMPARE(v2.type(),QVariant::Int);
399 QCOMPARE(v2.toInt(),1);
402 void tst_QVariant::canConvert_data()
404 QTest::addColumn<QVariant>("val");
405 QTest::addColumn<bool>("BitArrayCast");
406 QTest::addColumn<bool>("BitmapCast");
407 QTest::addColumn<bool>("BoolCast");
408 QTest::addColumn<bool>("BrushCast");
409 QTest::addColumn<bool>("ByteArrayCast");
410 QTest::addColumn<bool>("ColorCast");
411 QTest::addColumn<bool>("CursorCast");
412 QTest::addColumn<bool>("DateCast");
413 QTest::addColumn<bool>("DateTimeCast");
414 QTest::addColumn<bool>("DoubleCast");
415 QTest::addColumn<bool>("FontCast");
416 QTest::addColumn<bool>("ImageCast");
417 QTest::addColumn<bool>("IntCast");
418 QTest::addColumn<bool>("InvalidCast");
419 QTest::addColumn<bool>("KeySequenceCast");
420 QTest::addColumn<bool>("ListCast");
421 QTest::addColumn<bool>("LongLongCast");
422 QTest::addColumn<bool>("MapCast");
423 QTest::addColumn<bool>("PaletteCast");
424 QTest::addColumn<bool>("PenCast");
425 QTest::addColumn<bool>("PixmapCast");
426 QTest::addColumn<bool>("PointCast");
427 QTest::addColumn<bool>("RectCast");
428 QTest::addColumn<bool>("RegionCast");
429 QTest::addColumn<bool>("SizeCast");
430 QTest::addColumn<bool>("SizePolicyCast");
431 QTest::addColumn<bool>("StringCast");
432 QTest::addColumn<bool>("StringListCast");
433 QTest::addColumn<bool>("TimeCast");
434 QTest::addColumn<bool>("UIntCast");
435 QTest::addColumn<bool>("ULongLongCast");
446 // 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
449 QVariant var(QBitArray(0));
450 QTest::newRow("BitArray")
451 << 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;
452 var = qVariantFromValue(QBitmap());
453 QTest::newRow("Bitmap")
454 << 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;
455 var = qVariantFromValue(QBrush());
456 QTest::newRow("Brush")
457 << 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;
458 var = QVariant(QByteArray());
459 QTest::newRow("ByteArray")
460 << 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;
461 var = qVariantFromValue(QColor());
462 QTest::newRow("Color")
463 << 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;
465 var = qVariantFromValue(QCursor());
466 QTest::newRow("Cursor")
467 << 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;
469 var = QVariant(QDate());
470 QTest::newRow("Date")
471 << 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;
472 var = QVariant(QDateTime());
473 QTest::newRow("DateTime")
474 << 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;
475 var = QVariant((double)0.1);
476 QTest::newRow("Double")
477 << 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;
478 var = QVariant(0.1f);
479 QTest::newRow("Float")
480 << 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;
481 var = qVariantFromValue(QFont());
482 QTest::newRow("Font")
483 << 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;
484 var = qVariantFromValue(QIcon());
485 QTest::newRow("Icon")
486 << 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;
487 var = qVariantFromValue(QImage());
488 QTest::newRow("Image")
489 << 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;
490 var = QVariant((int)1);
492 << 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;
494 QTest::newRow("Invalid")
495 << 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;
496 var = qVariantFromValue(QKeySequence());
497 QTest::newRow("KeySequence")
498 << 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;
499 var = QVariant(QList<QVariant>());
500 QTest::newRow("List")
501 << 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;
502 var = QVariant((qlonglong)1);
503 QTest::newRow("LongLong")
504 << 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;
505 var = QVariant(QMap<QString,QVariant>());
507 << 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;
508 var = qVariantFromValue(QPalette());
509 QTest::newRow("Palette")
510 << 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;
511 var = qVariantFromValue(QPen());
513 << 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;
514 var = qVariantFromValue(QPixmap());
515 QTest::newRow("Pixmap")
516 << 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;
517 var = qVariantFromValue(QPolygon());
518 QTest::newRow("PointArray")
519 << 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;
520 var = QVariant(QPoint());
521 QTest::newRow("Point")
522 << 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;
523 var = QVariant(QRect());
524 QTest::newRow("Rect")
525 << 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;
526 var = qVariantFromValue(QRegion());
527 QTest::newRow("Region")
528 << 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;
529 var = QVariant(QSize());
530 QTest::newRow("Size")
531 << 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;
532 var = qVariantFromValue(QSizePolicy());
533 QTest::newRow("SizePolicy")
534 << 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;
535 var = QVariant(QString());
536 QTest::newRow("String")
537 << 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;
538 var = QVariant(QStringList("entry"));
539 QTest::newRow("StringList")
540 << 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;
541 var = QVariant(QTime());
542 QTest::newRow("Time")
543 << 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;
544 var = QVariant((uint)1);
545 QTest::newRow("UInt")
546 << 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;
547 var = QVariant((qulonglong)1);
548 QTest::newRow("ULongLong")
549 << 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;
555 void tst_QVariant::canConvert()
557 QFETCH(QVariant, val);
558 QFETCH(bool, BitArrayCast);
559 QFETCH(bool, BitmapCast);
560 QFETCH(bool, BoolCast);
561 QFETCH(bool, BrushCast);
562 QFETCH(bool, ByteArrayCast);
563 QFETCH(bool, ColorCast);
564 QFETCH(bool, CursorCast);
565 QFETCH(bool, DateCast);
566 QFETCH(bool, DateTimeCast);
567 QFETCH(bool, DoubleCast);
568 QFETCH(bool, FontCast);
569 QFETCH(bool, ImageCast);
570 QFETCH(bool, IntCast);
571 QFETCH(bool, InvalidCast);
572 QFETCH(bool, KeySequenceCast);
573 QFETCH(bool, ListCast);
574 QFETCH(bool, LongLongCast);
575 QFETCH(bool, MapCast);
576 QFETCH(bool, PaletteCast);
577 QFETCH(bool, PenCast);
578 QFETCH(bool, PixmapCast);
579 QFETCH(bool, PointCast);
580 QFETCH(bool, RectCast);
581 QFETCH(bool, RegionCast);
582 QFETCH(bool, SizeCast);
583 QFETCH(bool, SizePolicyCast);
584 QFETCH(bool, StringCast);
585 QFETCH(bool, StringListCast);
586 QFETCH(bool, TimeCast);
587 QFETCH(bool, UIntCast);
588 QFETCH(bool, ULongLongCast);
590 QCOMPARE(val.canConvert(QVariant::BitArray), BitArrayCast);
591 QCOMPARE(val.canConvert(QVariant::Bitmap), BitmapCast);
592 QCOMPARE(val.canConvert(QVariant::Bool), BoolCast);
593 QCOMPARE(val.canConvert(QVariant::Brush), BrushCast);
594 QCOMPARE(val.canConvert(QVariant::ByteArray), ByteArrayCast);
595 QCOMPARE(val.canConvert(QVariant::Color), ColorCast);
596 QCOMPARE(val.canConvert(QVariant::Cursor), CursorCast);
597 QCOMPARE(val.canConvert(QVariant::Date), DateCast);
598 QCOMPARE(val.canConvert(QVariant::DateTime), DateTimeCast);
599 QCOMPARE(val.canConvert(QVariant::Double), DoubleCast);
600 QCOMPARE(val.canConvert(QVariant::Type(QMetaType::Float)), DoubleCast);
601 QCOMPARE(val.canConvert(QVariant::Font), FontCast);
602 QCOMPARE(val.canConvert(QVariant::Image), ImageCast);
603 QCOMPARE(val.canConvert(QVariant::Int), IntCast);
604 QCOMPARE(val.canConvert(QVariant::Invalid), InvalidCast);
605 QCOMPARE(val.canConvert(QVariant::KeySequence), KeySequenceCast);
606 QCOMPARE(val.canConvert(QVariant::List), ListCast);
607 QCOMPARE(val.canConvert(QVariant::LongLong), LongLongCast);
608 QCOMPARE(val.canConvert(QVariant::Map), MapCast);
609 QCOMPARE(val.canConvert(QVariant::Palette), PaletteCast);
610 QCOMPARE(val.canConvert(QVariant::Pen), PenCast);
611 QCOMPARE(val.canConvert(QVariant::Pixmap), PixmapCast);
612 QCOMPARE(val.canConvert(QVariant::Point), PointCast);
613 QCOMPARE(val.canConvert(QVariant::Rect), RectCast);
614 QCOMPARE(val.canConvert(QVariant::Region), RegionCast);
615 QCOMPARE(val.canConvert(QVariant::Size), SizeCast);
616 QCOMPARE(val.canConvert(QVariant::SizePolicy), SizePolicyCast);
617 QCOMPARE(val.canConvert(QVariant::String), StringCast);
618 QCOMPARE(val.canConvert(QVariant::StringList), StringListCast);
619 QCOMPARE(val.canConvert(QVariant::Time), TimeCast);
620 QCOMPARE(val.canConvert(QVariant::UInt), UIntCast);
621 QCOMPARE(val.canConvert(QVariant::ULongLong), ULongLongCast);
624 QCOMPARE(val.canConvert(-1), false);
625 QCOMPARE(val.canConvert(-23), false);
626 QCOMPARE(val.canConvert(-23876), false);
627 QCOMPARE(val.canConvert(23876), false);
630 void tst_QVariant::toInt_data()
632 QTest::addColumn<QVariant>("value");
633 QTest::addColumn<int>("result");
634 QTest::addColumn<bool>("valueOK");
636 QTest::newRow( "invalid" ) << QVariant() << 0 << false;
637 QTest::newRow( "int" ) << QVariant( 123 ) << 123 << true;
638 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << 3 << true;
639 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << 3 << true;
640 QTest::newRow( "uint" ) << QVariant( 123u ) << 123 << true;
641 QTest::newRow( "int-string" ) << QVariant( QString("123") ) << 123 << true;
642 QTest::newRow( "string" ) << QVariant( QString("Unicode String") ) << 0 << false;
643 QTest::newRow( "longlong0" ) << QVariant( (qlonglong)34 ) << 34 << true;
644 QTest::newRow( "longlong1" ) << QVariant( intMax1 ) << (int)INT_MIN << true;
645 QTest::newRow( "ulonglong0" ) << QVariant( (qulonglong)34 ) << 34 << true;
646 QTest::newRow( "ulonglong1" ) << QVariant( uintMax1 ) << 0 << true;
647 QTest::newRow( "signedint" ) << QVariant( -123 ) << -123 << true;
648 QTest::newRow( "signeddouble" ) << QVariant( -3.1415927 ) << -3 << true;
649 QTest::newRow( "signedfloat" ) << QVariant( -3.1415927f ) << -3 << true;
650 QTest::newRow( "signedint-string" ) << QVariant( QString("-123") ) << -123 << true;
651 QTest::newRow( "signedlonglong0" ) << QVariant( (qlonglong)-34 ) << -34 << true;
652 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << int('a') << true;
653 QTest::newRow( "keysequence" ) << qVariantFromValue( QKeySequence( Qt::Key_A ) ) << 65 << true;
654 QByteArray bytearray(4, ' ');
659 QTest::newRow( "QByteArray1" ) << QVariant( bytearray ) << 0 << false;
664 QTest::newRow( "QByteArray2" ) << QVariant( bytearray ) << 4500 << true;
667 void tst_QVariant::toInt()
669 QFETCH( QVariant, value );
670 QFETCH( int, result );
671 QFETCH( bool, valueOK );
672 QVERIFY( value.isValid() == value.canConvert( QVariant::Int ) );
674 int i = value.toInt( &ok );
675 QCOMPARE( i, result );
676 QVERIFY( ok == valueOK );
679 void tst_QVariant::toUInt_data()
681 QTest::addColumn<QVariant>("value");
682 QTest::addColumn<uint>("result");
683 QTest::addColumn<bool>("valueOK");
685 QTest::newRow( "int" ) << QVariant( 123 ) << (uint)123 << true;
686 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (uint)3 << true;
687 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (uint)3 << true;
688 QTest::newRow( "uint" ) << QVariant( 123u ) << (uint)123 << true;
689 QTest::newRow( "int-string" ) << QVariant( QString("123") ) << (uint)123 << true;
690 QTest::newRow( "string" ) << QVariant( QString("Unicode String") ) << (uint)0 << false;
691 QTest::newRow( "string2" ) << QVariant( QString("4") ) << (uint)4 << true;
692 QTest::newRow( "longlong0" ) << QVariant( (qlonglong)34 ) << (uint)34 << true;
693 QTest::newRow( "longlong1" ) << QVariant( intMax1 ) << (uint)INT_MIN << true;
694 QTest::newRow( "ulonglong0" ) << QVariant( (qulonglong)34 ) << (uint)34 << true;
695 QTest::newRow( "ulonglong1" ) << QVariant( uintMax1 ) << (uint)0 << true;
696 QTest::newRow( "negativeint" ) << QVariant( -123 ) << (uint)-123 << true;
697 QTest::newRow( "negativedouble" ) << QVariant( -3.1415927 ) << (uint)-3 << true;
698 QTest::newRow( "negativefloat" ) << QVariant( -3.1415927f ) << (uint)-3 << true;
699 QTest::newRow( "negativeint-string" ) << QVariant( QString("-123") ) << (uint)0 << false;
700 QTest::newRow( "negativelonglong0" ) << QVariant( (qlonglong)-34 ) << (uint)-34 << true;
701 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << uint('a') << true;
702 QByteArray bytearray(4, ' ');
707 QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (uint)4321 << true;
710 void tst_QVariant::toUInt()
712 QFETCH( QVariant, value );
713 QFETCH( uint, result );
714 QFETCH( bool, valueOK );
715 QVERIFY( value.isValid() );
716 QVERIFY( value.canConvert( QVariant::UInt ) );
719 uint i = value.toUInt( &ok );
720 QVERIFY( ok == valueOK );
721 QCOMPARE( i, result );
725 void tst_QVariant::toSize_data()
727 QTest::addColumn<QVariant>("value");
728 QTest::addColumn<QSize>("result");
729 QTest::newRow( "qsizef4" ) << QVariant( QSizeF(4, 2) ) << QSize(4, 2);
730 QTest::newRow( "qsizef1" ) << QVariant( QSizeF(0, 0) ) << QSize(0, 0);
731 QTest::newRow( "qsizef2" ) << QVariant( QSizeF(-5, -1) ) << QSize(-5, -1);
732 QTest::newRow( "qsizef3" ) << QVariant( QSizeF() ) << QSize();
735 void tst_QVariant::toSize()
737 QFETCH( QVariant, value );
738 QFETCH( QSize, result );
739 QVERIFY( value.isValid() );
740 QVERIFY( value.canConvert( QVariant::Size ) );
742 QSize i = value.toSize();
743 QCOMPARE( i, result );
746 void tst_QVariant::toSizeF_data()
748 QTest::addColumn<QVariant>("value");
749 QTest::addColumn<QSizeF>("result");
750 QTest::newRow( "qsize1" ) << QVariant( QSize(0, 0) ) << QSizeF(0, 0);
751 QTest::newRow( "qsize2" ) << QVariant( QSize(-5, -1) ) << QSizeF(-5, -1);
752 QTest::newRow( "qsize3" ) << QVariant( QSize() ) << QSizeF();
753 QTest::newRow( "qsize4" ) << QVariant(QSize(4,2)) << QSizeF(4,2);
756 void tst_QVariant::toSizeF()
758 QFETCH( QVariant, value );
759 QFETCH( QSizeF, result );
760 QVERIFY( value.isValid() );
761 QVERIFY( value.canConvert( QVariant::SizeF ) );
763 QSizeF i = value.toSizeF();
764 QCOMPARE( i, result );
767 void tst_QVariant::toLine_data()
769 QTest::addColumn<QVariant>("value");
770 QTest::addColumn<QLine>("result");
771 QTest::newRow( "linef1" ) << QVariant( QLineF(1, 2, 3, 4) ) << QLine(1, 2, 3, 4);
772 QTest::newRow( "linef2" ) << QVariant( QLineF(-1, -2, -3, -4) ) << QLine(-1, -2, -3, -4);
773 QTest::newRow( "linef3" ) << QVariant( QLineF(0, 0, 0, 0) ) << QLine(0, 0, 0, 0);
774 QTest::newRow( "linef4" ) << QVariant( QLineF() ) << QLine();
777 void tst_QVariant::toLine()
779 QFETCH( QVariant, value );
780 QFETCH( QLine, result );
781 QVERIFY( value.isValid() );
782 QVERIFY( value.canConvert( QVariant::Line ) );
784 QLine i = value.toLine();
785 QCOMPARE( i, result );
788 void tst_QVariant::toLineF_data()
790 QTest::addColumn<QVariant>("value");
791 QTest::addColumn<QLineF>("result");
792 QTest::newRow( "line1" ) << QVariant( QLine(-1, -2, -3, -4) ) << QLineF(-1, -2, -3, -4);
793 QTest::newRow( "line2" ) << QVariant( QLine(0, 0, 0, 0) ) << QLineF(0, 0, 0, 0);
794 QTest::newRow( "line3" ) << QVariant( QLine() ) << QLineF();
795 QTest::newRow( "line4" ) << QVariant( QLine(1, 2, 3, 4) ) << QLineF(1, 2, 3, 4);
798 void tst_QVariant::toLineF()
800 QFETCH( QVariant, value );
801 QFETCH( QLineF, result );
802 QVERIFY( value.isValid() );
803 QVERIFY( value.canConvert( QVariant::LineF ) );
805 QLineF i = value.toLineF();
806 QCOMPARE( i, result );
809 void tst_QVariant::toPoint_data()
811 QTest::addColumn<QVariant>("value");
812 QTest::addColumn<QPoint>("result");
813 QTest::newRow( "pointf1" ) << QVariant( QPointF(4, 2) ) << QPoint(4, 2);
814 QTest::newRow( "pointf2" ) << QVariant( QPointF(0, 0) ) << QPoint(0, 0);
815 QTest::newRow( "pointf3" ) << QVariant( QPointF(-4, -2) ) << QPoint(-4, -2);
816 QTest::newRow( "pointf4" ) << QVariant( QPointF() ) << QPoint();
817 QTest::newRow( "pointf5" ) << QVariant( QPointF(-4.2f, -2.3f) ) << QPoint(-4, -2);
820 void tst_QVariant::toPoint()
822 QFETCH( QVariant, value );
823 QFETCH( QPoint, result );
824 QVERIFY( value.isValid() );
825 QVERIFY( value.canConvert( QVariant::Point ) );
826 QPoint i = value.toPoint();
827 QCOMPARE( i, result );
830 void tst_QVariant::toRect_data()
832 QTest::addColumn<QVariant>("value");
833 QTest::addColumn<QRect>("result");
834 QTest::newRow( "rectf1" ) << QVariant(QRectF(1, 2, 3, 4)) << QRect(1, 2, 3, 4);
835 QTest::newRow( "rectf2" ) << QVariant(QRectF(0, 0, 0, 0)) << QRect(0, 0, 0, 0);
836 QTest::newRow( "rectf3" ) << QVariant(QRectF(-1, -2, -3, -4)) << QRect(-1, -2, -3, -4);
837 QTest::newRow( "rectf4" ) << QVariant(QRectF(-1.3f, 0, 3.9f, -4.0)) << QRect(-1, 0, 4, -4);
838 QTest::newRow( "rectf5" ) << QVariant(QRectF()) << QRect();
841 void tst_QVariant::toRect()
843 QFETCH( QVariant, value );
844 QFETCH( QRect, result );
845 QVERIFY( value.isValid() );
846 QVERIFY( value.canConvert( QVariant::Rect ) );
847 QRect i = value.toRect();
848 QCOMPARE( i, result );
851 void tst_QVariant::toChar_data()
853 QTest::addColumn<QVariant>("value");
854 QTest::addColumn<QChar>("result");
855 QTest::newRow( "longlong" ) << QVariant(qlonglong('6')) << QChar('6');
856 QTest::newRow( "ulonglong" ) << QVariant(qulonglong('7')) << QChar('7');
859 void tst_QVariant::toChar()
861 QFETCH( QVariant, value );
862 QFETCH( QChar, result );
863 QVERIFY( value.isValid() );
864 QVERIFY( value.canConvert( QVariant::Char ) );
866 QChar i = value.toChar();
867 QCOMPARE( i, result );
870 void tst_QVariant::toBool_data()
872 QTest::addColumn<QVariant>("value");
873 QTest::addColumn<bool>("result");
875 QTest::newRow( "int0" ) << QVariant( 0 ) << false;
876 QTest::newRow( "int1" ) << QVariant( 123 ) << true;
877 QTest::newRow( "uint0" ) << QVariant( 0u ) << false;
878 QTest::newRow( "uint1" ) << QVariant( 123u ) << true;
879 QTest::newRow( "double0" ) << QVariant( 0.0 ) << false;
880 QTest::newRow( "float0" ) << QVariant( 0.0f ) << false;
881 QTest::newRow( "double1" ) << QVariant( 3.1415927 ) << true;
882 QTest::newRow( "float1" ) << QVariant( 3.1415927f ) << true;
883 QTest::newRow( "string0" ) << QVariant( QString("3") ) << true;
884 QTest::newRow( "string1" ) << QVariant( QString("true") ) << true;
885 QTest::newRow( "string2" ) << QVariant( QString("0") ) << false;
886 QTest::newRow( "string3" ) << QVariant( QString("fAlSe") ) << false;
887 QTest::newRow( "longlong0" ) << QVariant( (qlonglong)0 ) << false;
888 QTest::newRow( "longlong1" ) << QVariant( (qlonglong)1 ) << true;
889 QTest::newRow( "ulonglong0" ) << QVariant( (qulonglong)0 ) << false;
890 QTest::newRow( "ulonglong1" ) << QVariant( (qulonglong)1 ) << true;
891 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << true;
892 QTest::newRow( "Null_QChar" ) << QVariant(QChar(0)) << false;
895 void tst_QVariant::toBool()
897 QFETCH( QVariant, value );
898 QFETCH( bool, result );
899 QVERIFY( value.isValid() );
900 QVERIFY( value.canConvert( QVariant::Bool ) );
902 bool i = value.toBool();
903 QCOMPARE( i, result );
906 void tst_QVariant::toPointF_data()
908 QTest::addColumn<QVariant>("value");
909 QTest::addColumn<QPointF>("result");
911 QTest::newRow( "QPoint" ) << QVariant( QPointF( 19, 84) ) << QPointF( 19, 84 );
914 void tst_QVariant::toPointF()
916 QFETCH( QVariant, value );
917 QFETCH( QPointF, result );
918 QVERIFY( value.isValid() );
919 QVERIFY( value.canConvert( QVariant::PointF ) );
920 QPointF d = value.toPointF();
921 QCOMPARE( d, result );
924 void tst_QVariant::toRectF_data()
926 QTest::addColumn<QVariant>("value");
927 QTest::addColumn<QRectF>("result");
929 QRect r( 1, 9, 8, 4 );
930 QRectF rf( 1.0, 9.0, 8.0, 4.0 );
931 QTest::newRow( "QRect" ) << QVariant( r ) << rf;
934 void tst_QVariant::toRectF()
936 QFETCH( QVariant, value );
937 QFETCH( QRectF, result );
938 QVERIFY( value.isValid() );
939 QVERIFY( value.canConvert( QVariant::RectF ) );
940 QRectF d = value.toRectF();
941 QCOMPARE( d, result );
944 void tst_QVariant::toColor_data()
946 QTest::addColumn<QVariant>("value");
947 QTest::addColumn<QColor>("result");
950 QTest::newRow( "string" ) << QVariant( QString( "red" ) ) << c;
951 QTest::newRow( "solid brush" ) << QVariant( QBrush(c) ) << c;
954 void tst_QVariant::toColor()
956 QFETCH( QVariant, value );
957 QFETCH( QColor, result );
958 QVERIFY( value.isValid() );
959 QVERIFY( value.canConvert( QVariant::Color ) );
960 QColor d = qVariantValue<QColor>(value);
961 QCOMPARE( d, result );
964 void tst_QVariant::toPixmap_data()
966 QTest::addColumn<QVariant>("value");
967 QTest::addColumn<QPixmap>("result");
971 QTest::newRow( "image" ) << QVariant( pm ) << pm;
975 QTest::newRow( "bitmap" ) << QVariant( bm ) << QPixmap(bm);
978 void tst_QVariant::toPixmap()
980 QFETCH( QVariant, value );
981 QFETCH( QPixmap, result );
982 QVERIFY( value.isValid() );
983 QVERIFY( value.canConvert( QVariant::Pixmap ) );
984 QPixmap d = qVariantValue<QPixmap>(value);
985 QCOMPARE( d, result );
988 void tst_QVariant::toImage_data()
990 QTest::addColumn<QVariant>("value");
991 QTest::addColumn<QImage>("result");
993 QImage im(30, 30, QImage::Format_ARGB32);
995 QTest::newRow( "image" ) << QVariant( im ) << im;
998 void tst_QVariant::toImage()
1000 QFETCH( QVariant, value );
1001 QFETCH( QImage, result );
1002 QVERIFY( value.isValid() );
1003 QVERIFY( value.canConvert( QVariant::Image ) );
1004 QImage d = qVariantValue<QImage>(value);
1005 QCOMPARE( d, result );
1008 void tst_QVariant::toBrush_data()
1010 QTest::addColumn<QVariant>("value");
1011 QTest::addColumn<QBrush>("result");
1014 QTest::newRow( "color" ) << QVariant( c ) << QBrush(c);
1017 QTest::newRow( "pixmap" ) << QVariant( pm ) << QBrush(pm);
1020 void tst_QVariant::toBrush()
1022 QFETCH( QVariant, value );
1023 QFETCH( QBrush, result );
1024 QVERIFY( value.isValid() );
1025 QVERIFY( value.canConvert( QVariant::Brush ) );
1026 QBrush d = qVariantValue<QBrush>(value);
1027 QCOMPARE( d, result );
1030 void tst_QVariant::toFont_data()
1032 QTest::addColumn<QVariant>("value");
1033 QTest::addColumn<QFont>("result");
1035 QFont f("times",12,-1,false);
1036 QTest::newRow( "string" ) << QVariant( QString( "times,12,-1,5,50,0,0,0,0,0" ) ) << f;
1039 void tst_QVariant::toFont()
1041 QFETCH( QVariant, value );
1042 QFETCH( QFont, result );
1043 QVERIFY( value.isValid() );
1044 QVERIFY( value.canConvert( QVariant::Font ) );
1045 QFont d = qVariantValue<QFont>(value);
1046 QCOMPARE( d, result );
1049 void tst_QVariant::toKeySequence_data()
1051 QTest::addColumn<QVariant>("value");
1052 QTest::addColumn<QKeySequence>("result");
1055 QTest::newRow( "int" ) << QVariant( 67108929 ) << QKeySequence( Qt::CTRL + Qt::Key_A );
1058 QTest::newRow( "qstring" )
1059 << QVariant( QString( "Ctrl+A" ) )
1060 << QKeySequence( Qt::CTRL + Qt::Key_A );
1063 void tst_QVariant::toKeySequence()
1065 QFETCH( QVariant, value );
1066 QFETCH( QKeySequence, result );
1067 QVERIFY( value.isValid() );
1068 QVERIFY( value.canConvert( QVariant::KeySequence ) );
1069 QKeySequence d = qVariantValue<QKeySequence>(value);
1070 QCOMPARE( d, result );
1073 void tst_QVariant::toDouble_data()
1075 QTest::addColumn<QVariant>("value");
1076 QTest::addColumn<double>("result");
1077 QTest::addColumn<bool>("valueOK");
1079 QByteArray bytearray(4, ' ');
1084 QTest::newRow( "bytearray" ) << QVariant( bytearray ) << 32.1 << true;
1087 void tst_QVariant::toDouble()
1089 QFETCH( QVariant, value );
1090 QFETCH( double, result );
1091 QFETCH( bool, valueOK );
1092 QVERIFY( value.isValid() );
1093 QVERIFY( value.canConvert( QVariant::Double ) );
1095 double d = value.toDouble( &ok );
1096 QCOMPARE( d, result );
1097 QVERIFY( ok == valueOK );
1100 void tst_QVariant::toLongLong_data()
1102 QTest::addColumn<QVariant>("value");
1103 QTest::addColumn<qlonglong>("result");
1104 QTest::addColumn<bool>("valueOK");
1106 QTest::newRow( "int0" ) << QVariant( 123 ) << (qlonglong)123 << true;
1107 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (qlonglong)3 << true;
1108 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (qlonglong)3 << true;
1109 QTest::newRow( "uint" ) << QVariant( 123u ) << (qlonglong)123 << true;
1110 QTest::newRow( "int-string" ) << QVariant( QString("123") )
1111 << (qlonglong)123 << true;
1112 QTest::newRow( "string" ) << QVariant( QString("Unicode fun") ) << (qlonglong)0
1114 QTest::newRow( "longlong" ) << QVariant( intMax1 ) << intMax1 << true;
1115 QTest::newRow( "ulonglong" ) << QVariant( uintMax1 ) << (qlonglong)uintMax1 << true;
1116 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << qlonglong('a') << true;
1117 QByteArray bytearray(4, ' ');
1122 QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (qlonglong) 3200 << true;
1125 void tst_QVariant::toLongLong()
1127 QFETCH( QVariant, value );
1128 QFETCH( qlonglong, result );
1129 QFETCH( bool, valueOK );
1130 QVERIFY( value.isValid() );
1131 QVERIFY( value.canConvert( QVariant::LongLong ) );
1133 qlonglong ll = value.toLongLong( &ok );
1134 QCOMPARE( ll, result );
1135 QVERIFY( ok == valueOK );
1138 void tst_QVariant::toULongLong_data()
1140 QTest::addColumn<QVariant>("value");
1141 QTest::addColumn<qulonglong>("result");
1142 QTest::addColumn<bool>("valueOK");
1144 QTest::newRow( "int0" ) << QVariant( 123 ) << (qulonglong)123 << true;
1145 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (qulonglong)3 << true;
1146 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (qulonglong)3 << true;
1147 QTest::newRow( "uint" ) << QVariant( 123u ) << (qulonglong)123 << true;
1148 QTest::newRow( "int-string" ) << QVariant( QString("123") )
1149 << (qulonglong)123 << true;
1150 QTest::newRow( "string" ) << QVariant( QString("Unicode fun") ) << (qulonglong)0
1152 QTest::newRow( "ulonglong-string" ) << QVariant( QString("18446744073709551615") )
1153 << Q_UINT64_C(18446744073709551615)
1155 QTest::newRow( "bytaa-string" ) << QVariant( QString("18446744073709551615") )
1156 << Q_UINT64_C(18446744073709551615)
1158 QTest::newRow( "longlong" ) << QVariant( intMax1 ) << (qulonglong)intMax1 << true;
1159 QTest::newRow( "ulonglong" ) << QVariant( uintMax1 ) << uintMax1 << true;
1160 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << qulonglong('a') << true;
1161 QByteArray bytearray(4, ' ');
1166 QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (qulonglong) 3201 << true;
1169 void tst_QVariant::toULongLong()
1171 QFETCH( QVariant, value );
1172 QFETCH( qulonglong, result );
1173 QFETCH( bool, valueOK );
1174 QVERIFY( value.isValid() );
1175 QVERIFY( value.canConvert( QVariant::ULongLong ) );
1177 qulonglong ll = value.toULongLong( &ok );
1178 QCOMPARE( ll, result );
1179 QVERIFY( ok == valueOK );
1182 void tst_QVariant::toByteArray_data()
1184 QTest::addColumn<QVariant>("value");
1185 QTest::addColumn<QByteArray>("result");
1187 QByteArray ba(5, ' ');
1194 QByteArray variantBa = ba;
1196 QTest::newRow( "qbytearray" ) << QVariant( variantBa ) << ba;
1197 QTest::newRow( "int" ) << QVariant( -123 ) << QByteArray( "-123" );
1198 QTest::newRow( "uint" ) << QVariant( (uint)123 ) << QByteArray( "123" );
1199 QTest::newRow( "double" ) << QVariant( 123.456 ) << QByteArray( "123.456" );
1200 QTest::newRow( "float" ) << QVariant( 123.456f ) << QByteArray( "123.456" );
1201 QTest::newRow( "longlong" ) << QVariant( (qlonglong)34 ) << QByteArray( "34" );
1202 QTest::newRow( "ulonglong" ) << QVariant( (qulonglong)34 ) << QByteArray( "34" );
1205 void tst_QVariant::toByteArray()
1207 QFETCH( QVariant, value );
1208 QFETCH( QByteArray, result );
1209 QVERIFY( value.isValid() );
1210 QVERIFY( value.canConvert( QVariant::ByteArray ) );
1211 QByteArray ba = value.toByteArray();
1212 QCOMPARE( ba, result );
1215 void tst_QVariant::toString_data()
1217 QTest::addColumn<QVariant>("value");
1218 QTest::addColumn<QString>("result");
1220 QTest::newRow( "qstring" ) << QVariant( QString( "Test" ) ) << QString( "Test" );
1221 QTest::newRow( "charstar" ) << QVariant(QLatin1String("Test")) << QString("Test");
1222 QTest::newRow( "qbytearray") << QVariant( QByteArray( "Test\0" ) ) << QString( "Test" );
1223 QTest::newRow( "int" ) << QVariant( -123 ) << QString( "-123" );
1224 QTest::newRow( "uint" ) << QVariant( (uint)123 ) << QString( "123" );
1225 QTest::newRow( "double" ) << QVariant( 123.456 ) << QString( "123.456" );
1226 QTest::newRow( "float" ) << QVariant( 123.456f ) << QString( "123.456" );
1227 QTest::newRow( "bool" ) << QVariant( true ) << QString( "true" );
1228 QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 1, 1 ) ) << QString( "2002-01-01" );
1229 QTest::newRow( "qtime" ) << QVariant( QTime( 12, 34, 56 ) ) << QString( "12:34:56" );
1230 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 1, 1 ), QTime( 12, 34, 56 ) ) ) << QString( "2002-01-01T12:34:56" );
1231 QTest::newRow( "qkeysequence" ) << qVariantFromValue( QKeySequence( Qt::CTRL + Qt::Key_A ) )
1233 << QString( "Ctrl+A" );
1235 << QString(QChar(0x2318)) + "A";
1238 QFont font( "times", 12 );
1239 QTest::newRow( "qfont" ) << qVariantFromValue( font ) << QString("times,12,-1,5,50,0,0,0,0,0");
1240 QTest::newRow( "qcolor" ) << qVariantFromValue( QColor( 10, 10, 10 ) ) << QString( "#0a0a0a" );
1241 QTest::newRow( "llong" ) << QVariant( (qlonglong)Q_INT64_C(123456789012) ) <<
1242 QString( "123456789012" );
1245 void tst_QVariant::toString()
1247 QFETCH( QVariant, value );
1248 QFETCH( QString, result );
1249 QVERIFY( value.isValid() );
1250 QVERIFY( value.canConvert( QVariant::String ) );
1251 QString str = value.toString();
1252 QCOMPARE( str, result );
1255 void tst_QVariant::toDate_data()
1257 QTest::addColumn<QVariant>("value");
1258 QTest::addColumn<QDate>("result");
1260 QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 10, 10 ) ) << QDate( 2002, 10, 10 );
1261 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) ) ) << QDate( 2002, 10, 10 );
1262 QTest::newRow( "qstring" ) << QVariant( QString( "2002-10-10" ) ) << QDate( 2002, 10, 10 );
1265 void tst_QVariant::toDate()
1267 QFETCH( QVariant, value );
1268 QFETCH( QDate, result );
1269 QVERIFY( value.isValid() );
1270 QVERIFY( value.canConvert( QVariant::Date ) );
1271 QCOMPARE( value.toDate(), result );
1274 void tst_QVariant::toTime_data()
1276 QTest::addColumn<QVariant>("value");
1277 QTest::addColumn<QTime>("result");
1279 QTest::newRow( "qtime" ) << QVariant( QTime( 12, 34, 56 ) ) << QTime( 12, 34, 56 );
1280 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) ) ) << QTime( 12, 34, 56 );
1281 QTest::newRow( "qstring" ) << QVariant( QString( "12:34:56" ) ) << QTime( 12, 34, 56 );
1284 void tst_QVariant::toTime()
1286 QFETCH( QVariant, value );
1287 QFETCH( QTime, result );
1288 QVERIFY( value.isValid() );
1289 QVERIFY( value.canConvert( QVariant::Time ) );
1290 QCOMPARE( value.toTime(), result );
1293 void tst_QVariant::toDateTime_data()
1295 QTest::addColumn<QVariant>("value");
1296 QTest::addColumn<QDateTime>("result");
1298 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) ) )
1299 << QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) );
1300 QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 10, 10 ) ) << QDateTime( QDate( 2002, 10, 10 ), QTime( 0, 0, 0 ) );
1301 QTest::newRow( "qstring" ) << QVariant( QString( "2002-10-10T12:34:56" ) ) << QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) );
1304 void tst_QVariant::toDateTime()
1306 QFETCH( QVariant, value );
1307 QFETCH( QDateTime, result );
1308 QVERIFY( value.isValid() );
1309 QVERIFY( value.canConvert( QVariant::DateTime ) );
1310 QCOMPARE( value.toDateTime(), result );
1313 void tst_QVariant::toLocale()
1316 QLocale loc = variant.toLocale();
1317 variant = QLocale::system();
1318 loc = variant.toLocale();
1321 void tst_QVariant::toRegExp()
1324 QRegExp rx = variant.toRegExp();
1325 variant = QRegExp("foo");
1326 rx = variant.toRegExp();
1329 void tst_QVariant::toRegularExpression()
1332 QRegularExpression re = variant.toRegularExpression();
1333 QCOMPARE(re, QRegularExpression());
1335 variant = QRegularExpression("abc.*def");
1336 re = variant.toRegularExpression();
1337 QCOMPARE(re, QRegularExpression("abc.*def"));
1339 variant = QVariant::fromValue(QRegularExpression("[ab]\\w+"));
1340 re = variant.value<QRegularExpression>();
1341 QCOMPARE(re, QRegularExpression("[ab]\\w+"));
1344 void tst_QVariant::matrix()
1347 QMatrix matrix = qVariantValue<QMatrix>(variant);
1348 QVERIFY(matrix.isIdentity());
1349 qVariantSetValue(variant, QMatrix().rotate(90));
1350 QCOMPARE(QMatrix().rotate(90), qVariantValue<QMatrix>(variant));
1352 void *mmatrix = QMetaType::create(QVariant::Matrix, 0);
1354 QMetaType::destroy(QVariant::Matrix, mmatrix);
1357 void tst_QVariant::matrix4x4()
1360 QMatrix4x4 matrix = qVariantValue<QMatrix4x4>(variant);
1361 QVERIFY(matrix.isIdentity());
1364 qVariantSetValue(variant, m);
1365 QCOMPARE(m, qVariantValue<QMatrix4x4>(variant));
1367 void *mmatrix = QMetaType::create(QVariant::Matrix4x4, 0);
1369 QMetaType::destroy(QVariant::Matrix4x4, mmatrix);
1372 void tst_QVariant::transform()
1375 QTransform matrix = qVariantValue<QTransform>(variant);
1376 QVERIFY(matrix.isIdentity());
1377 qVariantSetValue(variant, QTransform().rotate(90));
1378 QCOMPARE(QTransform().rotate(90), qVariantValue<QTransform>(variant));
1380 void *mmatrix = QMetaType::create(QVariant::Transform, 0);
1382 QMetaType::destroy(QVariant::Transform, mmatrix);
1386 void tst_QVariant::vector2D()
1389 QVector2D vector = qVariantValue<QVector2D>(variant);
1390 QVERIFY(vector.isNull());
1391 qVariantSetValue(variant, QVector2D(0.1, 0.2));
1392 QCOMPARE(QVector2D(0.1, 0.2), qVariantValue<QVector2D>(variant));
1394 void *pvector = QMetaType::create(QVariant::Vector2D, 0);
1396 QMetaType::destroy(QVariant::Vector2D, pvector);
1399 void tst_QVariant::vector3D()
1402 QVector3D vector = qVariantValue<QVector3D>(variant);
1403 QVERIFY(vector.isNull());
1404 qVariantSetValue(variant, QVector3D(0.1, 0.2, 0.3));
1405 QCOMPARE(QVector3D(0.1, 0.2, 0.3), qVariantValue<QVector3D>(variant));
1407 void *pvector = QMetaType::create(QVariant::Vector3D, 0);
1409 QMetaType::destroy(QVariant::Vector3D, pvector);
1412 void tst_QVariant::vector4D()
1415 QVector4D vector = qVariantValue<QVector4D>(variant);
1416 QVERIFY(vector.isNull());
1417 qVariantSetValue(variant, QVector4D(0.1, 0.2, 0.3, 0.4));
1418 QCOMPARE(QVector4D(0.1, 0.2, 0.3, 0.4), qVariantValue<QVector4D>(variant));
1420 void *pvector = QMetaType::create(QVariant::Vector4D, 0);
1422 QMetaType::destroy(QVariant::Vector4D, pvector);
1425 void tst_QVariant::quaternion()
1428 QQuaternion quaternion = qVariantValue<QQuaternion>(variant);
1429 QVERIFY(quaternion.isIdentity());
1430 qVariantSetValue(variant, QQuaternion(0.1, 0.2, 0.3, 0.4));
1431 QCOMPARE(QQuaternion(0.1, 0.2, 0.3, 0.4), qVariantValue<QQuaternion>(variant));
1433 void *pquaternion = QMetaType::create(QVariant::Quaternion, 0);
1434 QVERIFY(pquaternion);
1435 QMetaType::destroy(QVariant::Quaternion, pquaternion);
1438 struct CustomStreamableClass
1441 bool operator==(const CustomStreamableClass& other) const
1443 return i == other.i;
1446 Q_DECLARE_METATYPE(CustomStreamableClass);
1448 QDataStream &operator<<(QDataStream &out, const CustomStreamableClass &myObj)
1450 return out << myObj.i;
1453 QDataStream &operator>>(QDataStream &in, CustomStreamableClass &myObj)
1455 return in >> myObj.i;
1458 void tst_QVariant::writeToReadFromDataStream_data()
1460 qRegisterMetaTypeStreamOperators<CustomStreamableClass>();
1462 QTest::addColumn<QVariant>("writeVariant");
1463 QTest::addColumn<bool>("isNull");
1465 typedef QList<QVariant> variantsList;
1466 variantsList valuelist;
1467 valuelist << QVariant( 1 ) << QVariant( QString("Two") ) << QVariant( 3.45 );
1468 QVariant var(valuelist);
1469 QTest::newRow( "list_valid" ) << var << false;
1472 QTest::newRow( "invalid" ) << QVariant() << true;
1473 QTest::newRow( "bitarray_invalid" ) << QVariant( QBitArray() ) << true;
1474 QBitArray bitarray( 3 );
1478 QTest::newRow( "bitarray_valid" ) << QVariant( bitarray ) << false;
1479 QTest::newRow( "bytearray_invalid" ) << QVariant( QByteArray() ) << true;
1480 QTest::newRow( "int_invalid") << QVariant(QVariant::Int) << true;
1481 QByteArray bytearray(5, ' ');
1486 bytearray[4] = '\0';
1487 QTest::newRow( "bytearray_valid" ) << QVariant( bytearray ) << false;
1488 QTest::newRow( "bitmap_invalid" ) << qVariantFromValue( QBitmap() ) << true;
1489 QBitmap bitmap( 10, 10 );
1490 bitmap.fill( Qt::red );
1491 QTest::newRow( "bitmap_valid" ) << qVariantFromValue( bitmap ) << false;
1492 QTest::newRow( "brush_valid" ) << qVariantFromValue( QBrush( Qt::red ) ) << false;
1493 QTest::newRow( "color_valid" ) << qVariantFromValue( QColor( Qt::red ) ) << false;
1494 #ifndef QT_NO_CURSOR
1495 QTest::newRow( "cursor_valid" ) << qVariantFromValue( QCursor( Qt::PointingHandCursor ) ) << false;
1497 QTest::newRow( "date_invalid" ) << QVariant( QDate() ) << true;
1498 QTest::newRow( "date_valid" ) << QVariant( QDate( 2002, 07, 06 ) ) << false;
1499 QTest::newRow( "datetime_invalid" ) << QVariant( QDateTime() ) << true;
1500 QTest::newRow( "datetime_valid" ) << QVariant( QDateTime( QDate( 2002, 07, 06 ), QTime( 14, 0, 0 ) ) ) << false;
1501 QTest::newRow( "double_valid" ) << QVariant( 123.456 ) << false;
1502 QTest::newRow( "float_valid" ) << QVariant( 123.456f ) << false;
1503 QTest::newRow( "font_valid" ) << qVariantFromValue( QFont( "times", 12 ) ) << false;
1504 QTest::newRow( "pixmap_invalid" ) << qVariantFromValue( QPixmap() ) << true;
1505 QPixmap pixmap( 10, 10 );
1506 pixmap.fill( Qt::red );
1507 QTest::newRow( "pixmap_valid" ) << qVariantFromValue( pixmap ) << false;
1508 // QTest::newRow( "iconset_valid" ) << QVariant( QIcon( pixmap ) ) << false;
1509 QTest::newRow( "image_invalid" ) << qVariantFromValue( QImage() ) << true;
1510 QTest::newRow( "keysequence_valid" ) << qVariantFromValue( QKeySequence( Qt::CTRL + Qt::Key_A ) ) << false;
1511 QTest::newRow( "int_valid" ) << QVariant( -123 ) << false;
1512 typedef QMap<QString, QVariant> variantsMap;
1514 vMap.insert( "int", QVariant( 1 ) );
1515 vMap.insert( "string", QVariant( QString("Two") ) );
1516 vMap.insert( "double", QVariant( 3.45 ) );
1517 vMap.insert( "float", QVariant( 3.45f ) );
1518 QTest::newRow( "map_valid" ) << QVariant( vMap ) << false;
1519 QTest::newRow( "palette_valid" ) << qVariantFromValue(QPalette(QColor("turquoise"))) << false;
1520 QTest::newRow( "pen_valid" ) << qVariantFromValue( QPen( Qt::red ) ) << false;
1521 QTest::newRow( "pointarray_invalid" ) << qVariantFromValue( QPolygon() ) << true;
1522 QTest::newRow( "pointarray_valid" ) << qVariantFromValue( QPolygon( QRect( 10, 10, 20, 20 ) ) ) << false;
1523 QTest::newRow( "region_invalid" ) << qVariantFromValue( QRegion() ) << true;
1524 QTest::newRow( "region_valid" ) << qVariantFromValue( QRegion( 10, 10, 20, 20 ) ) << false;
1525 QTest::newRow( "sizepolicy_valid" ) << qVariantFromValue( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) ) << false;
1526 QTest::newRow( "point_invalid" ) << qVariantFromValue( QPoint() ) << true;
1527 QTest::newRow( "point_valid" ) << qVariantFromValue( QPoint( 10, 10 ) ) << false;
1528 QTest::newRow( "rect_invalid" ) << QVariant( QRect() ) << true;
1529 QTest::newRow( "rect_valid" ) << QVariant( QRect( 10, 10, 20, 20 ) ) << false;
1530 QTest::newRow( "size_invalid" ) << QVariant( QSize( 0, 0 ) ) << true;
1531 QTest::newRow( "size_valid" ) << QVariant( QSize( 10, 10 ) ) << false;
1532 QTest::newRow( "string_invalid" ) << QVariant( QString() ) << true;
1533 QTest::newRow( "string_valid" ) << QVariant( QString( "Test" ) ) << false;
1534 QStringList stringlist;
1535 stringlist << "One" << "Two" << "Three";
1536 QTest::newRow( "stringlist_valid" ) << QVariant( stringlist ) << false;
1537 QTest::newRow( "time_invalid" ) << QVariant( QTime() ) << true;
1538 QTest::newRow( "time_valid" ) << QVariant( QTime( 14, 0, 0 ) ) << false;
1539 QTest::newRow( "uint_valid" ) << QVariant( (uint)123 ) << false;
1540 QTest::newRow( "qchar" ) << QVariant(QChar('a')) << false;
1541 QTest::newRow( "qchar_null" ) << QVariant(QChar(0)) << true;
1542 QTest::newRow( "regexp" ) << QVariant(QRegExp("foo", Qt::CaseInsensitive)) << false;
1543 QTest::newRow( "regexp_empty" ) << QVariant(QRegExp()) << false;
1544 QTest::newRow( "regularexpression" ) << QVariant(QRegularExpression("abc.*def")) << false;
1545 QTest::newRow( "regularexpression_empty" ) << QVariant(QRegularExpression()) << false;
1547 // types known to QMetaType, but not part of QVariant::Type
1548 QTest::newRow("QMetaType::Long invalid") << QVariant(QMetaType::Long, (void *) 0) << false;
1550 QTest::newRow("QMetaType::Long") << QVariant(QMetaType::Long, &longInt) << false;
1551 QTest::newRow("QMetaType::Short invalid") << QVariant(QMetaType::Short, (void *) 0) << false;
1553 QTest::newRow("QMetaType::Short") << QVariant(QMetaType::Short, &shortInt) << false;
1554 QTest::newRow("QMetaType::Char invalid") << QVariant(QMetaType::Char, (void *) 0) << false;
1556 QTest::newRow("QMetaType::Char") << QVariant(QMetaType::Char, &ch) << false;
1557 QTest::newRow("QMetaType::ULong invalid") << QVariant(QMetaType::ULong, (void *) 0) << false;
1558 ulong ulongInt = 1ul;
1559 QTest::newRow("QMetaType::ULong") << QVariant(QMetaType::ULong, &ulongInt) << false;
1560 QTest::newRow("QMetaType::UShort invalid") << QVariant(QMetaType::UShort, (void *) 0) << false;
1561 ushort ushortInt = 1u;
1562 QTest::newRow("QMetaType::UShort") << QVariant(QMetaType::UShort, &ushortInt) << false;
1563 QTest::newRow("QMetaType::UChar invalid") << QVariant(QMetaType::UChar, (void *) 0) << false;
1565 QTest::newRow("QMetaType::UChar") << QVariant(QMetaType::UChar, &uch) << false;
1566 QTest::newRow("QMetaType::Float invalid") << QVariant(QMetaType::Float, (void *) 0) << false;
1568 QTest::newRow("QMetaType::Float") << QVariant(QMetaType::Float, &f) << false;
1569 CustomStreamableClass custom = {123};
1570 QTest::newRow("Custom type") << QVariant::fromValue(custom) << false;
1573 void tst_QVariant::writeToReadFromDataStream()
1575 QFETCH( QVariant, writeVariant );
1576 QFETCH( bool, isNull );
1579 QDataStream writeStream( &data, QIODevice::WriteOnly );
1580 writeStream << writeVariant;
1582 QVariant readVariant;
1583 QDataStream readStream( &data, QIODevice::ReadOnly );
1584 readStream >> readVariant;
1585 QVERIFY( readVariant.isNull() == isNull );
1586 // Best way to confirm the readVariant contains the same data?
1587 // Since only a few won't match since the serial numbers are different
1588 // I won't bother adding another bool in the data test.
1589 const int writeType = writeVariant.userType();
1590 if (writeType == qMetaTypeId<CustomStreamableClass>())
1591 QCOMPARE(qvariant_cast<CustomStreamableClass>(readVariant), qvariant_cast<CustomStreamableClass>(writeVariant));
1592 else if ( writeType != QVariant::Invalid && writeType != QVariant::Bitmap && writeType != QVariant::Pixmap
1593 && writeType != QVariant::Image) {
1594 switch (writeType) {
1596 QCOMPARE( readVariant, writeVariant );
1599 // compare types know by QMetaType but not QVariant (QVariant::operator==() knows nothing about them)
1600 case QMetaType::Long:
1601 QCOMPARE(qvariant_cast<long>(readVariant), qvariant_cast<long>(writeVariant));
1603 case QMetaType::ULong:
1604 QCOMPARE(qvariant_cast<ulong>(readVariant), qvariant_cast<ulong>(writeVariant));
1606 case QMetaType::Short:
1607 QCOMPARE(qvariant_cast<short>(readVariant), qvariant_cast<short>(writeVariant));
1609 case QMetaType::UShort:
1610 QCOMPARE(qvariant_cast<ushort>(readVariant), qvariant_cast<ushort>(writeVariant));
1612 case QMetaType::Char:
1613 QCOMPARE(qvariant_cast<char>(readVariant), qvariant_cast<char>(writeVariant));
1615 case QMetaType::UChar:
1616 QCOMPARE(qvariant_cast<uchar>(readVariant), qvariant_cast<uchar>(writeVariant));
1618 case QMetaType::Float:
1620 // the uninitialized float can be NaN (observed on Windows Mobile 5 ARMv4i)
1621 float readFloat = qvariant_cast<float>(readVariant);
1622 float writtenFloat = qvariant_cast<float>(writeVariant);
1623 QVERIFY(qIsNaN(readFloat) == qIsNaN(writtenFloat));
1624 if (!qIsNaN(readFloat))
1625 QVERIFY(readFloat == writtenFloat);
1632 void tst_QVariant::writeToReadFromOldDataStream()
1634 QVariant writeVariant = QString("hello");
1637 QDataStream writeStream(&data, QIODevice::WriteOnly);
1638 writeStream.setVersion(QDataStream::Qt_2_1);
1639 writeStream << writeVariant;
1641 QVariant readVariant;
1642 QDataStream readStream(&data, QIODevice::ReadOnly);
1643 readStream.setVersion(QDataStream::Qt_2_1);
1644 readStream >> readVariant;
1646 QCOMPARE(writeVariant.userType(), readVariant.userType());
1647 QCOMPARE(writeVariant, readVariant);
1650 void tst_QVariant::checkDataStream()
1652 QTest::ignoreMessage(QtWarningMsg, "Trying to construct an instance of an invalid type, type id: 49");
1653 const QByteArray settingsHex("00000031ffffffffff");
1654 const QByteArray settings = QByteArray::fromHex(settingsHex);
1655 QDataStream in(settings);
1658 // the line below has been left out for now since the data stream
1659 // is not necessarily considered corrupt when an invalid QVariant is
1660 // constructed. However, it might be worth considering changing that behavior
1662 // QCOMPARE(in.status(), QDataStream::ReadCorruptData);
1663 QCOMPARE(v.type(), QVariant::Invalid);
1666 void tst_QVariant::operator_eq_eq_data()
1668 QTest::addColumn<QVariant>("left");
1669 QTest::addColumn<QVariant>("right");
1670 QTest::addColumn<bool>("equal"); // left == right ?
1673 QVariant i0( int(0) );
1674 QVariant i1( int(1) );
1676 QTest::newRow( "invinv" ) << inv << inv << true;
1678 QTest::newRow( "int1int1" ) << i1 << i1 << true;
1679 QTest::newRow( "int1int0" ) << i1 << i0 << false;
1680 QTest::newRow( "nullint" ) << i0 << QVariant(QVariant::Int) << false;
1682 // LongLong and ULongLong
1683 QVariant ll1( (qlonglong)1 );
1684 QVariant lln2( (qlonglong)-2 );
1685 QVariant ull1( (qulonglong)1 );
1686 QVariant ull3( (qulonglong)3 );
1687 QTest::newRow( "ll1ll1" ) << ll1 << ll1 << true;
1688 QTest::newRow( "ll1lln2" ) << ll1 << lln2 << false;
1689 QTest::newRow( "ll1ull1" ) << ull1 << ull1 << true;
1690 QTest::newRow( "ll1i1" ) << ull1 << i1 << true;
1691 QTest::newRow( "ull1ull1" ) << ull1 << ull1 << true;
1692 QTest::newRow( "ull1i1" ) << ull1 << ull1 << true;
1695 QVariant mIntString(QByteArray("-42"));
1696 QVariant mIntQString(QString("-42"));
1698 QVariant mUInt(42u);
1699 QVariant mUIntString(QByteArray("42"));
1700 QVariant mUIntQString(QString("42"));
1702 QVariant mDouble(42.11);
1703 QVariant mDoubleString(QByteArray("42.11"));
1704 QVariant mDoubleQString(QString("42.11"));
1706 QVariant mFloat(42.11f);
1707 QVariant mFloatString(QByteArray("42.11"));
1708 QVariant mFloatQString(QString("42.11"));
1710 QVariant mLongLong((qlonglong)-42);
1711 QVariant mLongLongString(QByteArray("-42"));
1712 QVariant mLongLongQString(QString("-42"));
1714 QVariant mULongLong((qulonglong)42);
1715 QVariant mULongLongString(QByteArray("42"));
1716 QVariant mULongLongQString(QString("42"));
1718 QVariant mBool(false);
1719 QVariant mBoolString(QByteArray("false"));
1720 QVariant mBoolQString(QString("false"));
1722 QTest::newRow( "double_int" ) << QVariant(42.0) << QVariant(42) << true;
1723 QTest::newRow( "float_int" ) << QVariant(42.f) << QVariant(42) << true;
1724 QTest::newRow( "mInt_mIntString" ) << mInt << mIntString << true;
1725 QTest::newRow( "mIntString_mInt" ) << mIntString << mInt << true;
1726 QTest::newRow( "mInt_mIntQString" ) << mInt << mIntQString << true;
1727 QTest::newRow( "mIntQString_mInt" ) << mIntQString << mInt << true;
1729 QTest::newRow( "mUInt_mUIntString" ) << mUInt << mUIntString << true;
1730 QTest::newRow( "mUIntString_mUInt" ) << mUIntString << mUInt << true;
1731 QTest::newRow( "mUInt_mUIntQString" ) << mUInt << mUIntQString << true;
1732 QTest::newRow( "mUIntQString_mUInt" ) << mUIntQString << mUInt << true;
1734 QTest::newRow( "mDouble_mDoubleString" ) << mDouble << mDoubleString << true;
1735 QTest::newRow( "mDoubleString_mDouble" ) << mDoubleString << mDouble << true;
1736 QTest::newRow( "mDouble_mDoubleQString" ) << mDouble << mDoubleQString << true;
1737 QTest::newRow( "mDoubleQString_mDouble" ) << mDoubleQString << mDouble << true;
1739 QTest::newRow( "mFloat_mFloatString" ) << mFloat << mFloatString << true;
1740 QTest::newRow( "mFloatString_mFloat" ) << mFloatString << mFloat << true;
1741 QTest::newRow( "mFloat_mFloatQString" ) << mFloat << mFloatQString << true;
1742 QTest::newRow( "mFloatQString_mFloat" ) << mFloatQString << mFloat << true;
1744 QTest::newRow( "mLongLong_mLongLongString" ) << mLongLong << mLongLongString << true;
1745 QTest::newRow( "mLongLongString_mLongLong" ) << mLongLongString << mLongLong << true;
1746 QTest::newRow( "mLongLong_mLongLongQString" ) << mLongLong << mLongLongQString << true;
1747 QTest::newRow( "mLongLongQString_mLongLong" ) << mLongLongQString << mLongLong << true;
1749 QTest::newRow( "mULongLong_mULongLongString" ) << mULongLong << mULongLongString << true;
1750 QTest::newRow( "mULongLongString_mULongLong" ) << mULongLongString << mULongLong << true;
1751 QTest::newRow( "mULongLong_mULongLongQString" ) << mULongLong << mULongLongQString << true;
1752 QTest::newRow( "mULongLongQString_mULongLong" ) << mULongLongQString << mULongLong << true;
1754 QTest::newRow( "mBool_mBoolString" ) << mBool << mBoolString << true;
1755 QTest::newRow( "mBoolString_mBool" ) << mBoolString << mBool << true;
1756 QTest::newRow( "mBool_mBoolQString" ) << mBool << mBoolQString << true;
1757 QTest::newRow( "mBoolQString_mBool" ) << mBoolQString << mBool << true;
1759 QTest::newRow("ba2qstring") << QVariant(QLatin1String("hallo")) << QVariant(QString("hallo")) << true;
1760 QTest::newRow("qstring2ba") << QVariant(QString("hallo")) << QVariant(QLatin1String("hallo")) << true;
1761 QTest::newRow("char_char") << QVariant(QChar('a')) << QVariant(QChar('a')) << true;
1762 QTest::newRow("char_char2") << QVariant(QChar('a')) << QVariant(QChar('b')) << false;
1764 QTest::newRow("invalidConversion") << QVariant(QString("bubu")) << QVariant(0) << false;
1765 QTest::newRow("invalidConversionR") << QVariant(0) << QVariant(QString("bubu")) << false;
1766 // ### many other combinations missing
1769 QMap<QString, QVariant> map1;
1770 map1.insert( "X", 1 );
1772 QMap<QString, QVariant> map2;
1773 map2.insert( "Y", 1 );
1775 QTest::newRow("TwoItemsInEqual") << QVariant(map1) << QVariant(map2) << false;
1780 QMap<QString, QVariant> map1;
1781 map1.insert( "X", 1 );
1783 QMap<QString, QVariant> map2;
1784 map2.insert( "X", 1 );
1786 QTest::newRow("TwoItemsEqual") << QVariant(map1) << QVariant(map2) << true;
1790 QMap<QString, QVariant> map1;
1791 map1.insert( "X", 1 );
1793 QMap<QString, QVariant> map2;
1795 QTest::newRow("PopulatedEmptyMap") << QVariant(map1) << QVariant(map2) << false;
1799 QMap<QString, QVariant> map1;
1801 QMap<QString, QVariant> map2;
1802 map2.insert( "X", 1 );
1804 QTest::newRow("EmptyPopulatedMap") << QVariant(map1) << QVariant(map2) << false;
1808 QMap<QString, QVariant> map1;
1809 map1.insert( "X", 1 );
1810 map1.insert( "Y", 1 );
1812 QMap<QString, QVariant> map2;
1813 map2.insert( "X", 1 );
1815 QTest::newRow("FirstLarger") << QVariant(map1) << QVariant(map2) << false;
1819 QMap<QString, QVariant> map1;
1820 map1.insert( "X", 1 );
1822 QMap<QString, QVariant> map2;
1823 map2.insert( "X", 1 );
1824 map2.insert( "Y", 1 );
1826 QTest::newRow("SecondLarger") << QVariant(map1) << QVariant(map2) << false;
1829 // same thing with hash
1831 QHash<QString, QVariant> hash1;
1832 hash1.insert( "X", 1 );
1834 QHash<QString, QVariant> hash2;
1835 hash2.insert( "Y", 1 );
1837 QTest::newRow("HashTwoItemsInEqual") << QVariant(hash1) << QVariant(hash2) << false;
1842 QHash<QString, QVariant> hash1;
1843 hash1.insert( "X", 1 );
1845 QHash<QString, QVariant> hash2;
1846 hash2.insert( "X", 1 );
1848 QTest::newRow("HashTwoItemsEqual") << QVariant(hash1) << QVariant(hash2) << true;
1852 QHash<QString, QVariant> hash1;
1853 hash1.insert( "X", 1 );
1855 QHash<QString, QVariant> hash2;
1857 QTest::newRow("HashPopulatedEmptyHash") << QVariant(hash1) << QVariant(hash2) << false;
1861 QHash<QString, QVariant> hash1;
1863 QHash<QString, QVariant> hash2;
1864 hash2.insert( "X", 1 );
1866 QTest::newRow("EmptyPopulatedHash") << QVariant(hash1) << QVariant(hash2) << false;
1870 QHash<QString, QVariant> hash1;
1871 hash1.insert( "X", 1 );
1872 hash1.insert( "Y", 1 );
1874 QHash<QString, QVariant> hash2;
1875 hash2.insert( "X", 1 );
1877 QTest::newRow("HashFirstLarger") << QVariant(hash1) << QVariant(hash2) << false;
1881 QHash<QString, QVariant> hash1;
1882 hash1.insert( "X", 1 );
1884 QHash<QString, QVariant> hash2;
1885 hash2.insert( "X", 1 );
1886 hash2.insert( "Y", 1 );
1888 QTest::newRow("HashSecondLarger") << QVariant(hash1) << QVariant(hash2) << false;
1892 void tst_QVariant::operator_eq_eq()
1894 QFETCH( QVariant, left );
1895 QFETCH( QVariant, right );
1896 QFETCH( bool, equal );
1897 QEXPECT_FAIL("nullint", "See QTBUG-22933", Continue);
1898 QCOMPARE( left == right, equal );
1901 void tst_QVariant::operator_eq_eq_rhs()
1909 /* This should _not_ compile */
1913 QVERIFY(dt == list);
1917 void tst_QVariant::typeName_data()
1919 QTest::addColumn<int>("type");
1920 QTest::addColumn<QByteArray>("res");
1921 QTest::newRow("0") << int(QVariant::Invalid) << QByteArray("");
1922 QTest::newRow("1") << int(QVariant::Map) << QByteArray("QVariantMap");
1923 QTest::newRow("2") << int(QVariant::List) << QByteArray("QVariantList");
1924 QTest::newRow("3") << int(QVariant::String) << QByteArray("QString");
1925 QTest::newRow("4") << int(QVariant::StringList) << QByteArray("QStringList");
1926 QTest::newRow("5") << int(QVariant::Font) << QByteArray("QFont");
1927 QTest::newRow("6") << int(QVariant::Pixmap) << QByteArray("QPixmap");
1928 QTest::newRow("7") << int(QVariant::Brush) << QByteArray("QBrush");
1929 QTest::newRow("8") << int(QVariant::Rect) << QByteArray("QRect");
1930 QTest::newRow("9") << int(QVariant::Size) << QByteArray("QSize");
1931 QTest::newRow("10") << int(QVariant::Color) << QByteArray("QColor");
1932 QTest::newRow("11") << int(QVariant::Palette) << QByteArray("QPalette");
1933 QTest::newRow("12") << int(QVariant::Point) << QByteArray("QPoint");
1934 QTest::newRow("13") << int(QVariant::Image) << QByteArray("QImage");
1935 QTest::newRow("14") << int(QVariant::Int) << QByteArray("int");
1936 QTest::newRow("15") << int(QVariant::UInt) << QByteArray("uint");
1937 QTest::newRow("16") << int(QVariant::Bool) << QByteArray("bool");
1938 QTest::newRow("17") << int(QVariant::Double) << QByteArray("double");
1939 QTest::newRow("18") << int(QMetaType::Float) << QByteArray("float");
1940 QTest::newRow("19") << int(QVariant::Polygon) << QByteArray("QPolygon");
1941 QTest::newRow("20") << int(QVariant::Region) << QByteArray("QRegion");
1942 QTest::newRow("21") << int(QVariant::Bitmap) << QByteArray("QBitmap");
1943 QTest::newRow("22") << int(QVariant::Cursor) << QByteArray("QCursor");
1944 QTest::newRow("23") << int(QVariant::SizePolicy) << QByteArray("QSizePolicy");
1945 QTest::newRow("24") << int(QVariant::Date) << QByteArray("QDate");
1946 QTest::newRow("25") << int(QVariant::Time) << QByteArray("QTime");
1947 QTest::newRow("26") << int(QVariant::DateTime) << QByteArray("QDateTime");
1948 QTest::newRow("27") << int(QVariant::ByteArray) << QByteArray("QByteArray");
1949 QTest::newRow("28") << int(QVariant::BitArray) << QByteArray("QBitArray");
1950 QTest::newRow("29") << int(QVariant::KeySequence) << QByteArray("QKeySequence");
1951 QTest::newRow("30") << int(QVariant::Pen) << QByteArray("QPen");
1952 QTest::newRow("31") << int(QVariant::LongLong) << QByteArray("qlonglong");
1953 QTest::newRow("32") << int(QVariant::ULongLong) << QByteArray("qulonglong");
1954 QTest::newRow("33") << int(QVariant::Char) << QByteArray("QChar");
1955 QTest::newRow("34") << int(QVariant::Url) << QByteArray("QUrl");
1956 QTest::newRow("35") << int(QVariant::TextLength) << QByteArray("QTextLength");
1957 QTest::newRow("36") << int(QVariant::TextFormat) << QByteArray("QTextFormat");
1958 QTest::newRow("37") << int(QVariant::Locale) << QByteArray("QLocale");
1959 QTest::newRow("38") << int(QVariant::LineF) << QByteArray("QLineF");
1960 QTest::newRow("39") << int(QVariant::RectF) << QByteArray("QRectF");
1961 QTest::newRow("40") << int(QVariant::PointF) << QByteArray("QPointF");
1962 QTest::newRow("41") << int(QVariant::RegExp) << QByteArray("QRegExp");
1963 QTest::newRow("43") << int(QVariant::Matrix) << QByteArray("QMatrix");
1964 QTest::newRow("44") << int(QVariant::Transform) << QByteArray("QTransform");
1965 QTest::newRow("45") << int(QVariant::Hash) << QByteArray("QVariantHash");
1966 QTest::newRow("46") << int(QVariant::Matrix4x4) << QByteArray("QMatrix4x4");
1967 QTest::newRow("47") << int(QVariant::Vector2D) << QByteArray("QVector2D");
1968 QTest::newRow("48") << int(QVariant::Vector3D) << QByteArray("QVector3D");
1969 QTest::newRow("49") << int(QVariant::Vector4D) << QByteArray("QVector4D");
1970 QTest::newRow("50") << int(QVariant::Quaternion) << QByteArray("QQuaternion");
1971 QTest::newRow("51") << int(QVariant::RegularExpression) << QByteArray("QRegularExpression");
1974 void tst_QVariant::typeName()
1976 QFETCH( int, type );
1977 QFETCH( QByteArray, res );
1978 QCOMPARE(QString::fromLatin1(QVariant::typeToName((QVariant::Type)type)),
1979 QString::fromLatin1(res.constData()));
1982 // test nameToType as well
1983 void tst_QVariant::typeToName()
1986 QCOMPARE( QVariant::typeToName( v.type() ), (const char*)0 ); // Invalid
1987 // assumes that QVariant::Type contains consecutive values
1989 int max = QVariant::LastGuiType;
1990 for ( int t = 1; t <= max; t++ ) {
1991 const char *n = QVariant::typeToName( (QVariant::Type)t );
1993 QCOMPARE( int(QVariant::nameToType( n )), t );
1996 QCOMPARE(QVariant::typeToName(QVariant::Int), "int");
1997 // not documented but we return 0 if the type is out of range
1998 // by testing this we catch cases where QVariant is extended
1999 // but type_map is not updated accordingly
2000 QCOMPARE( QVariant::typeToName( QVariant::Type(max+1) ), (char*)0 );
2001 // invalid type names
2002 QVERIFY( QVariant::nameToType( 0 ) == QVariant::Invalid );
2003 QVERIFY( QVariant::nameToType( "" ) == QVariant::Invalid );
2004 QVERIFY( QVariant::nameToType( "foo" ) == QVariant::Invalid );
2006 QCOMPARE(QVariant::nameToType("UserType"), QVariant::Invalid);
2008 // We don't support these old (Qt3) types anymore.
2009 QCOMPARE(QVariant::nameToType("QIconSet"), QVariant::Invalid);
2010 QCOMPARE(QVariant::nameToType("Q3CString"), QVariant::Invalid);
2011 QCOMPARE(QVariant::nameToType("Q_LLONG"), QVariant::Invalid);
2012 QCOMPARE(QVariant::nameToType("Q_ULLONG"), QVariant::Invalid);
2015 void tst_QVariant::streamInvalidVariant()
2021 QVariant writeVariant;
2022 QVariant readVariant;
2024 QVERIFY( writeVariant.type() == QVariant::Invalid );
2027 QDataStream writeStream( &data, QIODevice::WriteOnly );
2028 writeStream << writeX << writeVariant << writeY;
2030 QDataStream readStream( &data, QIODevice::ReadOnly );
2031 readStream >> readX >> readVariant >> readY;
2033 QVERIFY( readX == writeX );
2034 // Two invalid QVariant's aren't necessarily the same, so == will
2035 // return false if one is invalid, so check the type() instead
2036 QVERIFY( readVariant.type() == QVariant::Invalid );
2037 QVERIFY( readY == writeY );
2040 static int instanceCount = 0;
2044 MyType(int n = 0, const char *t=0): number(n), text(t)
2048 MyType(const MyType &other)
2049 : number(other.number), text(other.text)
2061 Q_DECLARE_METATYPE(MyType)
2062 Q_DECLARE_METATYPE(MyType*)
2064 void tst_QVariant::userType()
2067 MyType data(1, "eins");
2068 MyType data2(2, "zwei");
2072 qVariantSetValue(userVar, data);
2074 QCOMPARE(userVar.type(), QVariant::UserType);
2075 QCOMPARE(userVar.userType(), qMetaTypeId<MyType>());
2076 QCOMPARE(userVar.typeName(), "MyType");
2077 QVERIFY(!userVar.isNull());
2078 QVERIFY(!userVar.canConvert(QVariant::String));
2080 QVariant userVar2(userVar);
2081 QVERIFY(userVar == userVar2);
2083 qVariantSetValue(userVar2, data2);
2084 QVERIFY(userVar != userVar2);
2086 const MyType *varData = static_cast<const MyType *>(userVar.constData());
2088 QCOMPARE(varData->number, data.number);
2089 QCOMPARE(varData->text, data.text);
2092 qVariantSetValue(userVar3, data2);
2094 userVar3 = userVar2;
2095 QVERIFY(userVar2 == userVar3);
2097 // At this point all QVariants got destroyed but we have 2 MyType instances.
2098 QCOMPARE(instanceCount, 2);
2101 qVariantSetValue(userVar, &data);
2103 QCOMPARE(userVar.type(), QVariant::UserType);
2104 QCOMPARE(userVar.userType(), qMetaTypeId<MyType*>());
2105 QCOMPARE(userVar.typeName(), "MyType*");
2106 QVERIFY(!userVar.isNull());
2107 QVERIFY(!userVar.canConvert(QVariant::String));
2109 QVariant userVar2(userVar);
2110 QVERIFY(userVar == userVar2);
2112 qVariantSetValue(userVar2, &data2);
2113 QVERIFY(userVar != userVar2);
2115 MyType * const*varData = reinterpret_cast<MyType *const *>(userVar.constData());
2117 QCOMPARE(*varData, &data);
2120 qVariantSetValue(userVar3, &data2);
2122 /* This check is correct now. userVar2 contains a pointer to data2 and so
2124 QVERIFY(userVar2 == userVar3);
2126 userVar3 = userVar2;
2127 QVERIFY(userVar2 == userVar3);
2130 QCOMPARE(instanceCount, 2);
2132 qVariantSetValue(myCarrier, data);
2133 QCOMPARE(instanceCount, 3);
2135 QVariant second = myCarrier;
2136 QCOMPARE(instanceCount, 3);
2138 QCOMPARE(instanceCount, 4);
2140 QCOMPARE(instanceCount, 3);
2142 MyType data3(0, "null");
2143 data3 = qVariantValue<MyType>(myCarrier);
2144 QCOMPARE(data3.number, 1);
2145 QCOMPARE(data3.text, (const char *)"eins");
2147 QCOMPARE(instanceCount, 4);
2153 const MyType data(3, "drei");
2156 qVariantSetValue(myCarrier, data);
2157 QCOMPARE(myCarrier.typeName(), "MyType");
2159 const MyType data2 = qvariant_cast<MyType>(myCarrier);
2160 QCOMPARE(data2.number, 3);
2161 QCOMPARE(data2.text, (const char *)"drei");
2168 qVariantSetValue(myCarrier, s);
2169 QCOMPARE((int)qvariant_cast<short>(myCarrier), 42);
2173 qlonglong ll = Q_INT64_C(42);
2176 qVariantSetValue(myCarrier, ll);
2177 QCOMPARE(qvariant_cast<int>(myCarrier), 42);
2180 // At this point all QVariants got destroyed and MyType objects too.
2181 QCOMPARE(instanceCount, 0);
2189 Q_DECLARE_METATYPE(MyTypePOD)
2191 void tst_QVariant::podUserType()
2197 QVariant pod_as_variant = qVariantFromValue(pod);
2198 MyTypePOD pod2 = qvariant_cast<MyTypePOD>(pod_as_variant);
2200 QCOMPARE(pod.a, pod2.a);
2201 QCOMPARE(pod.b, pod2.b);
2203 qVariantSetValue(pod_as_variant, pod);
2204 pod2 = qVariantValue<MyTypePOD>(pod_as_variant);
2206 QCOMPARE(pod.a, pod2.a);
2207 QCOMPARE(pod.b, pod2.b);
2210 void tst_QVariant::basicUserType()
2215 v = QVariant(QMetaType::Int, &i);
2217 QCOMPARE(v.type(), QVariant::Int);
2218 QCOMPARE(v.toInt(), 7);
2222 v = QVariant(QMetaType::QString, &s);
2224 QCOMPARE(v.type(), QVariant::String);
2225 QCOMPARE(v.toString(), QString("foo"));
2229 v = QVariant(QMetaType::Double, &d);
2231 QCOMPARE(v.type(), QVariant::Double);
2232 QCOMPARE(v.toDouble(), 4.4);
2236 v = QVariant(QMetaType::Float, &f);
2238 QCOMPARE(v.userType(), int(QMetaType::Float));
2239 QCOMPARE(v.toDouble(), 4.5);
2242 QByteArray ba("bar");
2243 v = QVariant(QMetaType::QByteArray, &ba);
2245 QCOMPARE(v.type(), QVariant::ByteArray);
2246 QCOMPARE(v.toByteArray(), QByteArray("bar"));
2249 void tst_QVariant::data()
2256 QVariant ll = (qlonglong)2;
2257 QVariant ull = (qulonglong)3;
2258 QVariant s(QString("hallo"));
2259 QVariant r(QRect(1,2,3,4));
2263 QCOMPARE(*static_cast<int *>(v.data()), i.toInt());
2267 QCOMPARE(*static_cast<double *>(v.data()), d.toDouble());
2271 QCOMPARE(*static_cast<float *>(v.data()), qVariantValue<float>(v));
2275 QCOMPARE(*static_cast<qlonglong *>(v.data()), ll.toLongLong());
2279 QCOMPARE(*static_cast<qulonglong *>(v.data()), ull.toULongLong());
2283 QCOMPARE(*static_cast<QString *>(v.data()), s.toString());
2287 QCOMPARE(*static_cast<QRect *>(v.data()), r.toRect());
2290 void tst_QVariant::constData()
2303 QVERIFY(v.constData());
2304 QCOMPARE(*static_cast<const int *>(v.constData()), i);
2307 QVERIFY(v.constData());
2308 QCOMPARE(*static_cast<const double *>(v.constData()), d);
2311 QVERIFY(v.constData());
2312 QCOMPARE(*static_cast<const float *>(v.constData()), f);
2315 QVERIFY(v.constData());
2316 QCOMPARE(*static_cast<const qlonglong *>(v.constData()), ll);
2319 QVERIFY(v.constData());
2320 QCOMPARE(*static_cast<const qulonglong *>(v.constData()), ull);
2323 QVERIFY(v.constData());
2324 QCOMPARE(*static_cast<const QString *>(v.constData()), s);
2327 QVERIFY(v.constData());
2328 QCOMPARE(*static_cast<const QRect *>(v.constData()), r);
2337 Q_DECLARE_METATYPE(Foo)
2339 void tst_QVariant::variant_to()
2348 sl << QLatin1String("blah");
2350 qVariantSetValue(v3, sl);
2355 qVariantSetValue(v4, foo);
2357 QCOMPARE(qvariant_cast<double>(v1), 4.2);
2358 QCOMPARE(qvariant_cast<float>(v1), 4.2f);
2359 QCOMPARE(qvariant_cast<int>(v2), 5);
2360 QCOMPARE(qvariant_cast<QStringList>(v3), sl);
2361 QCOMPARE(qvariant_cast<QString>(v3), QString::fromLatin1("blah"));
2363 QCOMPARE(qvariant_cast<Foo>(v4).i, 42);
2366 QCOMPARE(qvariant_cast<Foo>(v5).i, 0);
2368 QCOMPARE(qvariant_cast<int>(v1), 4);
2370 QVariant n = qVariantFromValue<short>(42);
2371 QCOMPARE(qvariant_cast<int>(n), 42);
2372 QCOMPARE(qvariant_cast<uint>(n), 42u);
2373 QCOMPARE(qvariant_cast<double>(n), 42.0);
2374 QCOMPARE(qvariant_cast<float>(n), 42.f);
2375 QCOMPARE(qvariant_cast<short>(n), short(42));
2376 QCOMPARE(qvariant_cast<ushort>(n), ushort(42));
2378 n = qVariantFromValue(43l);
2379 QCOMPARE(qvariant_cast<int>(n), 43);
2380 QCOMPARE(qvariant_cast<uint>(n), 43u);
2381 QCOMPARE(qvariant_cast<double>(n), 43.0);
2382 QCOMPARE(qvariant_cast<float>(n), 43.f);
2383 QCOMPARE(qvariant_cast<long>(n), 43l);
2385 n = QLatin1String("44");
2386 QCOMPARE(qvariant_cast<int>(n), 44);
2387 QCOMPARE(qvariant_cast<ulong>(n), 44ul);
2388 QCOMPARE(qvariant_cast<float>(n), 44.0f);
2390 QCOMPARE(qVariantFromValue(0.25f).toDouble(), 0.25);
2393 struct Blah { int i; };
2395 QDataStream& operator>>(QDataStream& s, Blah& c)
2396 { return (s >> c.i); }
2398 QDataStream& operator<<(QDataStream& s, const Blah& c)
2399 { return (s << c.i); }
2401 void tst_QVariant::saveLoadCustomTypes()
2406 int tp = qRegisterMetaType<Blah>("Blah");
2407 QVariant v = QVariant(tp, &i);
2409 qRegisterMetaTypeStreamOperators<Blah>("Blah");
2411 QCOMPARE(v.userType(), tp);
2412 QVERIFY(v.type() == QVariant::UserType);
2414 QDataStream stream(&data, QIODevice::WriteOnly);
2421 QDataStream stream(data);
2425 QCOMPARE(int(v.userType()), QMetaType::type("Blah"));
2426 int value = *(int*)v.constData();
2427 QCOMPARE(value, 42);
2430 void tst_QVariant::url()
2432 QString str("http://qt.nokia.com");
2435 QVariant v(url); //built with a QUrl
2439 QVariant v3(str); //built with a QString
2441 QCOMPARE(v2.toUrl(), url);
2442 QVERIFY(qVariantCanConvert<QUrl>(v3));
2443 QCOMPARE(v2.toUrl(), v3.toUrl());
2445 QVERIFY(qVariantCanConvert<QString>(v2));
2446 QCOMPARE(v2.toString(), str);
2447 QCOMPARE(v3.toString(), str);
2450 void tst_QVariant::globalColor()
2452 QVariant variant(Qt::blue);
2453 QVERIFY(variant.type() == QVariant::Color);
2454 QVERIFY(qVariantValue<QColor>(variant) == QColor(Qt::blue));
2457 void tst_QVariant::variantMap()
2459 QMap<QString, QVariant> map;
2463 QVariantMap map2 = qvariant_cast<QVariantMap>(v);
2465 QCOMPARE(map2.value("test").toInt(), 42);
2467 QVariant v2 = QVariant(QMetaType::type("QVariantMap"), &map);
2468 QCOMPARE(qvariant_cast<QVariantMap>(v2).value("test").toInt(), 42);
2470 QVariant v3 = QVariant(QMetaType::type("QMap<QString, QVariant>"), &map);
2471 QCOMPARE(qvariant_cast<QVariantMap>(v3).value("test").toInt(), 42);
2474 void tst_QVariant::variantHash()
2476 QHash<QString, QVariant> hash;
2480 QVariantHash hash2 = qvariant_cast<QVariantHash>(v);
2482 QCOMPARE(hash2.value("test").toInt(), 42);
2484 QVariant v2 = QVariant(QMetaType::type("QVariantHash"), &hash);
2485 QCOMPARE(qvariant_cast<QVariantHash>(v2).value("test").toInt(), 42);
2487 QVariant v3 = QVariant(QMetaType::type("QHash<QString, QVariant>"), &hash);
2488 QCOMPARE(qvariant_cast<QVariantHash>(v3).value("test").toInt(), 42);
2491 void tst_QVariant::invalidQColor() const
2493 QVariant va("An invalid QColor::name() value.");
2494 QVERIFY(va.canConvert(QVariant::Color));
2496 QVERIFY(!va.convert(QVariant::Color));
2498 QVERIFY(!qvariant_cast<QColor>(va).isValid());
2501 class CustomQObject : public QObject {
2504 CustomQObject(QObject *parent = 0) : QObject(parent) {}
2506 class CustomQWidget : public QWidget {
2509 CustomQWidget(QWidget *parent = 0) : QWidget(parent) {}
2511 Q_DECLARE_METATYPE(CustomQObject*)
2512 Q_DECLARE_METATYPE(CustomQWidget*)
2514 class CustomNonQObject { };
2515 Q_DECLARE_METATYPE(CustomNonQObject)
2516 Q_DECLARE_METATYPE(CustomNonQObject*)
2518 void tst_QVariant::cleanupTestCase()
2520 delete customNonQObjectPointer;
2521 qDeleteAll(objectPointerTestData);
2524 void tst_QVariant::qvariant_cast_QObject_data()
2526 QTest::addColumn<QVariant>("data");
2527 QTest::addColumn<bool>("success");
2528 QObject *obj = new QObject;
2529 obj->setObjectName(QString::fromLatin1("Hello"));
2530 QTest::newRow("from QObject") << QVariant(QMetaType::QObjectStar, &obj) << true;
2531 QTest::newRow("from QObject2") << QVariant::fromValue(obj) << true;
2532 QTest::newRow("from String") << QVariant(QLatin1String("1, 2, 3")) << false;
2533 QTest::newRow("from int") << QVariant((int) 123) << false;
2534 QWidget *widget = new QWidget;
2535 widget->setObjectName(QString::fromLatin1("Hello"));
2536 QTest::newRow("from QWidget") << QVariant::fromValue(widget) << true;
2537 CustomQObject *customObject = new CustomQObject(this);
2538 customObject->setObjectName(QString::fromLatin1("Hello"));
2539 QTest::newRow("from Derived QObject") << QVariant::fromValue(customObject) << true;
2540 CustomQWidget *customWidget = new CustomQWidget;
2541 customWidget->setObjectName(QString::fromLatin1("Hello"));
2542 QTest::newRow("from Derived QWidget") << QVariant::fromValue(customWidget) << true;
2543 QTest::newRow("from custom Object") << QVariant::fromValue(CustomNonQObject()) << false;
2545 // Deleted in cleanupTestCase.
2546 customNonQObjectPointer = new CustomNonQObject;
2547 QTest::newRow("from custom ObjectStar") << QVariant::fromValue(customNonQObjectPointer) << false;
2549 // Deleted in cleanupTestCase.
2550 objectPointerTestData.push_back(obj);
2551 objectPointerTestData.push_back(widget);
2552 objectPointerTestData.push_back(customObject);
2553 objectPointerTestData.push_back(customWidget);
2556 void tst_QVariant::qvariant_cast_QObject()
2558 QFETCH(QVariant, data);
2559 QFETCH(bool, success);
2561 QObject *o = qvariant_cast<QObject *>(data);
2562 QCOMPARE(o != 0, success);
2564 QCOMPARE(o->objectName(), QString::fromLatin1("Hello"));
2568 class CustomQObjectDerived : public CustomQObject {
2571 CustomQObjectDerived(QObject *parent = 0) : CustomQObject(parent) {}
2573 Q_DECLARE_METATYPE(CustomQObjectDerived*)
2575 class CustomQObjectDerivedNoMetaType : public CustomQObject {
2578 CustomQObjectDerivedNoMetaType(QObject *parent = 0) : CustomQObject(parent) {}
2581 void tst_QVariant::qvariant_cast_QObject_derived()
2584 CustomQObjectDerivedNoMetaType *object = new CustomQObjectDerivedNoMetaType(this);
2585 QVariant data = QVariant::fromValue(object);
2586 QVERIFY(data.userType() == qMetaTypeId<CustomQObjectDerivedNoMetaType*>());
2587 QCOMPARE(data.value<QObject *>(), object);
2588 QCOMPARE(data.value<CustomQObjectDerivedNoMetaType *>(), object);
2589 QCOMPARE(data.value<CustomQObject *>(), object);
2590 QVERIFY(data.value<CustomQWidget*>() == 0);
2593 CustomQObjectDerived *object = new CustomQObjectDerived(this);
2594 QVariant data = QVariant::fromValue(object);
2596 QVERIFY(data.userType() == qMetaTypeId<CustomQObjectDerived*>());
2598 QCOMPARE(data.value<QObject *>(), object);
2599 QCOMPARE(data.value<CustomQObjectDerived *>(), object);
2600 QCOMPARE(data.value<CustomQObject *>(), object);
2601 QVERIFY(data.value<CustomQWidget*>() == 0);
2604 CustomQWidget customWidget;
2605 QWidget *widget = &customWidget;
2606 QVariant data = QVariant::fromValue(widget);
2607 QVERIFY(data.userType() == QMetaType::QWidgetStar);
2609 QCOMPARE(data.value<QObject*>(), widget);
2610 QCOMPARE(data.value<QWidget*>(), widget);
2611 QCOMPARE(data.value<CustomQWidget*>(), widget);
2615 Q_DECLARE_METATYPE(qint8);
2617 void tst_QVariant::convertToQUint8() const
2621 const qint8 anInt = 32;
2623 /* QVariant(int) gets invoked here so the QVariant has nothing with qint8 to do.
2624 * It's of type QVariant::Int. */
2625 const QVariant v0 = anInt;
2627 QVERIFY(qVariantCanConvert<qint8>(v0));
2628 QCOMPARE(int(qvariant_cast<qint8>(v0)), 32);
2629 QCOMPARE(int(v0.toInt()), 32);
2630 QCOMPARE(v0.toString(), QString("32"));
2632 QCOMPARE(int(qvariant_cast<qlonglong>(v0)), 32);
2633 QCOMPARE(int(qvariant_cast<char>(v0)), 32);
2634 QCOMPARE(int(qvariant_cast<short>(v0)), 32);
2635 QCOMPARE(int(qvariant_cast<long>(v0)), 32);
2636 QCOMPARE(int(qvariant_cast<float>(v0)), 32);
2637 QCOMPARE(int(qvariant_cast<double>(v0)), 32);
2642 const quint8 anInt = 32;
2643 const QVariant v0 = anInt;
2645 QVERIFY(qVariantCanConvert<quint8>(v0));
2646 QCOMPARE(int(qvariant_cast<quint8>(v0)), 32);
2647 QCOMPARE(int(v0.toUInt()), 32);
2648 QCOMPARE(v0.toString(), QString("32"));
2653 const qint16 anInt = 32;
2654 const QVariant v0 = anInt;
2656 QVERIFY(qVariantCanConvert<qint16>(v0));
2657 QCOMPARE(int(qvariant_cast<qint16>(v0)), 32);
2658 QCOMPARE(int(v0.toInt()), 32);
2659 QCOMPARE(v0.toString(), QString("32"));
2664 const quint16 anInt = 32;
2665 const QVariant v0 = anInt;
2667 QVERIFY(qVariantCanConvert<quint16>(v0));
2668 QCOMPARE(int(qvariant_cast<quint16>(v0)), 32);
2669 QCOMPARE(int(v0.toUInt()), 32);
2670 QCOMPARE(v0.toString(), QString("32"));
2674 void tst_QVariant::comparePointers() const
2682 QVariant v = qVariantFromValue<void *>(&myClass);
2683 QVariant v2 = qVariantFromValue<void *>(&myClass);
2689 Q_DECLARE_METATYPE(Data*)
2691 void tst_QVariant::voidStar() const
2698 v1 = qVariantFromValue(p1);
2702 v2 = qVariantFromValue(p2);
2706 v2 = qVariantFromValue(p2);
2710 void tst_QVariant::dataStar() const
2712 qRegisterMetaType<Data*>();
2713 Data *p1 = new Data;
2715 QVariant v1 = qVariantFromValue(p1);
2716 QCOMPARE(v1.userType(), qMetaTypeId<Data*>());
2717 QCOMPARE(qvariant_cast<Data*>(v1), p1);
2722 v2 = qVariantFromValue(p1);
2727 void tst_QVariant::canConvertQStringList() const
2729 QFETCH(bool, canConvert);
2730 QFETCH(QStringList, input);
2731 QFETCH(QString, result);
2735 QCOMPARE(v.canConvert(QVariant::String), canConvert);
2736 QCOMPARE(v.toString(), result);
2739 void tst_QVariant::canConvertQStringList_data() const
2741 QTest::addColumn<bool>("canConvert");
2742 QTest::addColumn<QStringList>("input");
2743 QTest::addColumn<QString>("result");
2745 QTest::newRow("An empty list") << false << QStringList() << QString();
2746 QTest::newRow("A single item") << true << QStringList(QLatin1String("foo")) << QString::fromLatin1("foo");
2747 QTest::newRow("A single, but empty item") << true << QStringList(QString()) << QString();
2752 QTest::newRow("Two items") << false << l << QString();
2755 QTest::newRow("Three items") << false << l << QString();
2758 template<typename T> void convertMetaType()
2760 QVERIFY(qVariantFromValue<T>(10).isValid());
2761 QVERIFY(qVariantFromValue<T>(10).canConvert(QVariant::Int));
2762 QCOMPARE(qVariantFromValue<T>(10).toInt(), 10);
2763 QCOMPARE(qVariantFromValue<T>(10), qVariantFromValue<T>(10));
2766 #define CONVERT_META_TYPE(Type) \
2767 convertMetaType<Type>(); \
2768 if (QTest::currentTestFailed()) \
2769 QFAIL("convertMetaType<" #Type "> failed");
2771 void tst_QVariant::canConvertMetaTypeToInt() const
2773 CONVERT_META_TYPE(long);
2774 CONVERT_META_TYPE(short);
2775 CONVERT_META_TYPE(short);
2776 CONVERT_META_TYPE(unsigned short);
2777 CONVERT_META_TYPE(ushort);
2778 CONVERT_META_TYPE(ulong);
2779 CONVERT_META_TYPE(unsigned long);
2780 CONVERT_META_TYPE(uchar);
2781 CONVERT_META_TYPE(unsigned char);
2782 CONVERT_META_TYPE(char);
2783 CONVERT_META_TYPE(uint);
2784 CONVERT_META_TYPE(unsigned int);
2787 #undef CONVERT_META_TYPE
2790 These calls should not produce any warnings.
2792 void tst_QVariant::variantToDateTimeWithoutWarnings() const
2795 const QVariant variant(QLatin1String("An invalid QDateTime string"));
2796 const QDateTime dateTime(variant.toDateTime());
2797 QVERIFY(!dateTime.isValid());
2801 QVariant v1(QLatin1String("xyz"));
2802 v1.convert(QVariant::DateTime);
2804 QVariant v2(QLatin1String("xyz"));
2805 QDateTime dt1(v2.toDateTime());
2807 const QVariant v3(QLatin1String("xyz"));
2808 const QDateTime dt2(v3.toDateTime());
2812 void tst_QVariant::invalidDateTime() const
2814 QVariant variant(QString::fromLatin1("Invalid date time string"));
2815 QVERIFY(!variant.toDateTime().isValid());
2816 QVERIFY(!variant.convert(QVariant::DateTime));
2821 MyClass() : myValue(0) {}
2825 Q_DECLARE_METATYPE( MyClass )
2827 void tst_QVariant::loadUnknownUserType()
2829 qRegisterMetaType<MyClass>("MyClass");
2830 char data[] = {0, 0, QMetaType::User >> 8 , char(QMetaType::User), 0, 0, 0, 0, 8, 'M', 'y', 'C', 'l', 'a', 's', 's', 0};
2832 QByteArray ba(data, sizeof(data));
2833 QDataStream ds(&ba, QIODevice::ReadOnly);
2836 QCOMPARE(ds.status(), QDataStream::ReadCorruptData);
2839 void tst_QVariant::loadBrokenUserType()
2841 char data[] = {0, 0, 0, 127, 0, 112 };
2843 QByteArray ba(data, sizeof(data));
2844 QDataStream ds(&ba, QIODevice::ReadOnly);
2847 QCOMPARE(ds.status(), QDataStream::ReadPastEnd);
2850 void tst_QVariant::invalidDate() const
2852 QString foo("Hello");
2853 QVariant variant(foo);
2854 QVERIFY(!variant.convert(QVariant::Date));
2857 QVERIFY(!variant.convert(QVariant::DateTime));
2860 QVERIFY(!variant.convert(QVariant::Time));
2863 QVERIFY(!variant.convert(QVariant::Int));
2866 QVERIFY(!variant.convert(QVariant::Double));
2869 QVERIFY(!variant.convert(QVariant::Type(QMetaType::Float)));
2874 int x,y,z,q,w,e,r,t;
2876 Q_DECLARE_METATYPE(WontCompare);
2878 void tst_QVariant::compareCustomTypes() const
2880 qRegisterMetaType<WontCompare>("WontCompare");
2884 const QVariant variant1(qVariantFromValue(f1));
2888 const QVariant variant2(qVariantFromValue(f2));
2890 /* We compare pointers. */
2891 QVERIFY(variant1 != variant2);
2892 QVERIFY(variant1 == variant1);
2893 QVERIFY(variant2 == variant2);
2896 void tst_QVariant::timeToDateTime() const
2898 const QVariant val(QTime::currentTime());
2899 QVERIFY(!val.canConvert(QVariant::DateTime));
2900 QVERIFY(!val.toDateTime().isValid());
2903 Q_DECLARE_METATYPE(QHostAddress)
2905 void tst_QVariant::copyingUserTypes() const
2909 const QHostAddress ha("127.0.0.1");
2910 qVariantSetValue(var, ha);
2913 QCOMPARE(qVariantValue<QHostAddress>(var3), ha);
2916 void tst_QVariant::convertBoolToByteArray() const
2918 QFETCH(QByteArray, input);
2919 QFETCH(bool, canConvert);
2920 QFETCH(bool, value);
2922 const QVariant variant(input);
2924 QCOMPARE(qVariantCanConvert<bool>(variant), canConvert);
2927 /* Just call this function so we run the code path. */
2928 QCOMPARE(variant.toBool(), value);
2932 void tst_QVariant::convertBoolToByteArray_data() const
2934 QTest::addColumn<QByteArray>("input");
2935 QTest::addColumn<bool>("canConvert");
2936 QTest::addColumn<bool>("value");
2938 QTest::newRow("false")
2939 << QByteArray("false")
2943 QTest::newRow("FALSE")
2944 << QByteArray("FALSE")
2948 QTest::newRow("falSE")
2949 << QByteArray("FALSE")
2958 QTest::newRow("null QByteArray")
2963 QTest::newRow("any-content")
2964 << QByteArray("any-content")
2968 QTest::newRow("true")
2969 << QByteArray("true")
2973 QTest::newRow("TRUE")
2974 << QByteArray("TRUE")
2978 QTest::newRow("trUE")
2979 << QByteArray("trUE")
2984 void tst_QVariant::convertByteArrayToBool() const
2986 QFETCH(bool, input);
2987 QFETCH(QByteArray, output);
2989 const QVariant variant(input);
2990 QCOMPARE(variant.type(), QVariant::Bool);
2991 QCOMPARE(variant.toBool(), input);
2992 QVERIFY(qVariantCanConvert<bool>(variant));
2994 QCOMPARE(variant.toByteArray(), output);
2997 void tst_QVariant::convertByteArrayToBool_data() const
2999 QTest::addColumn<bool>("input");
3000 QTest::addColumn<QByteArray>("output");
3002 QTest::newRow("false")
3004 << QByteArray("false");
3006 QTest::newRow("true")
3008 << QByteArray("true");
3013 1. Converting the string "9.9" to int fails. This is the behavior of
3014 toLongLong() and hence also QVariant, since it uses it.
3015 2. Converting the QVariant containing the double 9.9 to int works.
3017 Rationale: "9.9" is not a valid int. However, doubles are by definition not
3018 ints and therefore it makes more sense to perform conversion for those.
3020 void tst_QVariant::toIntFromQString() const
3022 QVariant first("9.9");
3024 QCOMPARE(first.toInt(&ok), 0);
3027 QCOMPARE(QString("9.9").toLongLong(&ok), qlonglong(0));
3031 QCOMPARE(v.toInt(&ok), 10);
3037 1. Conversion from (64 bit) double to int works (no overflow).
3038 2. Same conversion works for QVariant::convert.
3040 Rationale: if 2147483630 is set in float and then converted to int,
3041 there will be overflow and the result will be -2147483648.
3043 void tst_QVariant::toIntFromDouble() const
3045 double d = 2147483630; // max int 2147483647
3046 QVERIFY((int)d == 2147483630);
3049 QVERIFY( var.canConvert( QVariant::Int ) );
3052 int result = var.toInt(&ok);
3054 QVERIFY( ok == true );
3055 QCOMPARE(result, 2147483630);
3058 void tst_QVariant::setValue()
3060 QTransform t; //we just take a value so that we're sure that it will be shared
3062 QVERIFY( v1.isDetached() );
3064 QVERIFY( !v1.isDetached() );
3065 QVERIFY( !v2.isDetached() );
3067 qVariantSetValue(v2, 3); //set an integer value
3069 QVERIFY( v1.isDetached() );
3070 QVERIFY( v2.isDetached() );
3073 void tst_QVariant::numericalConvert()
3075 #if defined(Q_OS_LINUX) && defined(Q_CC_GNU) && !defined(__x86_64__)
3076 QSKIP("Known to fail due to a GCC bug on at least Ubuntu 10.04 32-bit - check QTBUG-8959");
3078 QVariant vfloat(float(5.3));
3079 QVariant vdouble(double(5.3));
3080 QVariant vreal(qreal(5.3));
3081 QVariant vint(int(5));
3082 QVariant vuint(uint(5));
3083 QVariant vshort(short(5));
3084 QVariant vlonglong(quint64(5));
3085 QVariant vstringint(QString::fromLatin1("5"));
3086 QVariant vstring(QString::fromLatin1("5.3"));
3088 QVector<QVariant *> vect;
3089 vect << &vfloat << &vdouble << &vreal << &vint << &vuint << &vshort<< &vlonglong << &vstringint << &vstring;
3091 for(int i = 0; i < vect.size(); i++) {
3093 if (i >= 3 && i <= 7)
3095 QVariant *v = vect.at(i);
3096 QCOMPARE(v->toFloat() , float(num));
3097 QCOMPARE(float(v->toReal()) , float(num));
3098 QCOMPARE(float(v->toDouble()) , float(num));
3100 QCOMPARE(v->toInt() , int(num));
3101 QCOMPARE(v->toUInt() , uint(num));
3102 QCOMPARE(v->toULongLong() , quint64(num));
3104 QCOMPARE(v->toString() , QString::number(num));
3109 template<class T> void playWithVariant(const T &orig, bool isNull, const QString &toString, double toDouble, bool toBool)
3111 QVariant v = QVariant::fromValue(orig);
3112 QVERIFY(v.isValid());
3113 QCOMPARE(v.isNull(), isNull);
3114 QCOMPARE(v.toString(), toString);
3115 QCOMPARE(v.toDouble(), toDouble);
3116 QCOMPARE(v.toBool(), toBool);
3117 QCOMPARE(qvariant_cast<T>(v), orig);
3121 if (!(QTypeInfo<T>::isStatic && QTypeInfo<T>::isComplex)) {
3122 // Type is movable so standard comparison algorithm in QVariant should work
3123 // In a custom type QVariant is not aware of ==operator so it won't be called,
3124 // which may cause problems especially visible when using a not-movable type
3127 QVERIFY(v2.isValid());
3128 QCOMPARE(v2.isNull(), isNull);
3129 QCOMPARE(v2.toString(), toString);
3130 QCOMPARE(v2.toDouble(), toDouble);
3131 QCOMPARE(v2.toBool(), toBool);
3132 QCOMPARE(qvariant_cast<T>(v2), orig);
3138 if (!(QTypeInfo<T>::isStatic && QTypeInfo<T>::isComplex)) {
3139 // Type is movable so standard comparison algorithm in QVariant should work
3140 // In a custom type QVariant is not aware of ==operator so it won't be called,
3141 // which may cause problems especially visible when using a not-movable type
3144 QCOMPARE(qvariant_cast<T>(v2), qvariant_cast<T>(v));
3145 QCOMPARE(v2.toString(), toString);
3146 v3 = qVariantFromValue(orig);
3148 QVERIFY(v3.isValid());
3149 QCOMPARE(v3.isNull(), isNull);
3150 QCOMPARE(v3.toString(), toString);
3151 QCOMPARE(v3.toDouble(), toDouble);
3152 QCOMPARE(v3.toBool(), toBool);
3153 QCOMPARE(qvariant_cast<T>(v3), qvariant_cast<T>(v));
3156 QVERIFY(v.isValid());
3157 QCOMPARE(v.isNull(), isNull);
3158 QCOMPARE(v.toString(), toString);
3159 QCOMPARE(v.toDouble(), toDouble);
3160 QCOMPARE(v.toBool(), toBool);
3161 QCOMPARE(qvariant_cast<T>(v), orig);
3163 if (qMetaTypeId<T>() != qMetaTypeId<QVariant>()) {
3164 QCOMPARE(v.userType(), qMetaTypeId<T>());
3165 QCOMPARE(QVariant::typeToName(QVariant::Type(v.userType())), QMetaType::typeName(qMetaTypeId<T>()));
3169 #define PLAY_WITH_VARIANT(Orig, IsNull, ToString, ToDouble, ToBool) \
3170 playWithVariant(Orig, IsNull, ToString, ToDouble, ToBool);\
3171 if (QTest::currentTestFailed())\
3172 QFAIL("playWithVariant failed");
3177 bool operator==(const MyPrimitive &o) const
3179 return x == o.x && y == o.y;
3184 Q_DECLARE_TYPEINFO(MyPrimitive, Q_PRIMITIVE_TYPE);
3190 MyData() : ptr(this) {}
3193 if (ptr != this) qWarning("%s: object has moved", Q_FUNC_INFO);
3195 MyData(const MyData& o) : ptr(this)
3197 if (o.ptr != &o) qWarning("%s: other object has moved", Q_FUNC_INFO);
3199 MyData &operator=(const MyData &o)
3201 if (ptr != this) qWarning("%s: object has moved", Q_FUNC_INFO);
3202 if (o.ptr != &o) qWarning("%s: other object has moved", Q_FUNC_INFO);
3205 bool operator==(const MyData &o) const
3207 if (ptr != this) qWarning("%s: object has moved", Q_FUNC_INFO);
3208 if (o.ptr != &o) qWarning("%s: other object has moved", Q_FUNC_INFO);
3217 MyMovable() { v = count++; }
3218 ~MyMovable() { count--; }
3219 MyMovable(const MyMovable &o) : v(o.v) { count++; }
3221 bool operator==(const MyMovable &o) const
3227 int MyMovable::count = 0;
3233 MyNotMovable() : that(this) { count++; }
3234 ~MyNotMovable() { QCOMPARE(that, this); count--; }
3235 MyNotMovable(const MyNotMovable &o) : that(this) { QCOMPARE(o.that, &o); count++; }
3236 MyNotMovable &operator=(const MyNotMovable &o) {
3237 bool ok = that == this && o.that == &o;
3238 if (!ok) qFatal("MyNotMovable has been moved");
3242 //PLAY_WITH_VARIANT test that they are equal, but never that they are not equal
3243 // so it would be fine just to always return true
3244 bool operator==(const MyNotMovable &o) const
3246 bool ok = that == this && o.that == &o;
3247 if (!ok) qFatal("MyNotMovable has been moved");
3252 int MyNotMovable::count = 0;
3254 struct MyShared : QSharedData {
3259 Q_DECLARE_TYPEINFO(MyMovable, Q_MOVABLE_TYPE);
3262 Q_DECLARE_METATYPE(QList<QSize>)
3263 Q_DECLARE_METATYPE(MyPrimitive)
3264 Q_DECLARE_METATYPE(MyData)
3265 Q_DECLARE_METATYPE(MyMovable)
3266 Q_DECLARE_METATYPE(MyNotMovable)
3267 Q_DECLARE_METATYPE(QList<MyPrimitive>)
3268 Q_DECLARE_METATYPE(QList<MyData>)
3269 Q_DECLARE_METATYPE(QList<MyMovable>)
3270 Q_DECLARE_METATYPE(QList<MyNotMovable>)
3271 Q_DECLARE_METATYPE(MyPrimitive *)
3272 Q_DECLARE_METATYPE(MyData *)
3273 Q_DECLARE_METATYPE(MyMovable *)
3274 Q_DECLARE_METATYPE(MyNotMovable *)
3275 Q_DECLARE_METATYPE(QSharedDataPointer<MyShared>)
3278 void tst_QVariant::moreCustomTypes()
3281 QList<QSize> listSize;
3282 PLAY_WITH_VARIANT(listSize, false, QString(), 0, false);
3283 listSize << QSize(4,5) << QSize(89,23) << QSize(5,6);
3284 PLAY_WITH_VARIANT(listSize, false, QString(), 0, false);
3289 PLAY_WITH_VARIANT(str, true, QString(), 0, false);
3290 str = QString::fromLatin1("123456789.123");
3291 PLAY_WITH_VARIANT(str, false, str, 123456789.123, true);
3296 PLAY_WITH_VARIANT(size, false, QString(), 0, false);
3297 PLAY_WITH_VARIANT(QSize(45,78), false, QString(), 0, false);
3302 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3303 PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3305 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3306 l << MyData() << MyData();
3307 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3311 MyPrimitive d = { 4, 5 };
3312 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3313 PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3314 QList<MyPrimitive> l;
3315 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3317 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3322 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3323 PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3325 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3326 l << MyMovable() << d;
3327 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3329 QCOMPARE(MyMovable::count, 0);
3331 QCOMPARE(MyNotMovable::count, 0);
3334 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3335 PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3336 QList<MyNotMovable> l;
3337 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3338 l << MyNotMovable() << d;
3339 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3341 QCOMPARE(MyNotMovable::count, 0);
3344 PLAY_WITH_VARIANT(12.12, false, "12.12", 12.12, true);
3345 PLAY_WITH_VARIANT(12.12f, false, "12.12", 12.12f, true);
3346 PLAY_WITH_VARIANT('a', false, "a", 'a', true);
3347 PLAY_WITH_VARIANT((unsigned char)('a'), false, "a", 'a', true);
3348 PLAY_WITH_VARIANT( quint8(12), false, "\xc", 12, true);
3349 PLAY_WITH_VARIANT( qint8(13), false, "\xd", 13, true);
3350 PLAY_WITH_VARIANT(quint16(14), false, "14", 14, true);
3351 PLAY_WITH_VARIANT( qint16(15), false, "15", 15, true);
3352 PLAY_WITH_VARIANT(quint32(16), false, "16", 16, true);
3353 PLAY_WITH_VARIANT( qint32(17), false, "17", 17, true);
3354 PLAY_WITH_VARIANT(quint64(18), false, "18", 18, true);
3355 PLAY_WITH_VARIANT( qint64(19), false, "19", 19, true);
3356 PLAY_WITH_VARIANT( qint8(-12), false, "\xf4", -12, true);
3357 PLAY_WITH_VARIANT( qint16(-13), false, "-13", -13, true);
3358 PLAY_WITH_VARIANT( qint32(-14), false, "-14", -14, true);
3359 PLAY_WITH_VARIANT( qint64(-15), false, "-15", -15, true);
3360 PLAY_WITH_VARIANT(quint64(0), false, "0", 0, false);
3361 PLAY_WITH_VARIANT( true, false, "true", 1, true);
3362 PLAY_WITH_VARIANT( false, false, "false", 0, false);
3364 PLAY_WITH_VARIANT(QString("hello\n"), false, "hello\n", 0, true);
3369 PLAY_WITH_VARIANT((void *)(&i), false, QString(), 0, false);
3370 PLAY_WITH_VARIANT((void *)(0), false, QString(), 0, false);
3374 QVariant v1 = QVariant::fromValue(5);
3375 QVariant v2 = QVariant::fromValue(5.0);
3376 QVariant v3 = QVariant::fromValue(quint16(5));
3378 QVariant v5 = QVariant::fromValue(MyPrimitive());
3379 QVariant v6 = QVariant::fromValue(MyMovable());
3380 QVariant v7 = QVariant::fromValue(MyData());
3381 PLAY_WITH_VARIANT(v1, false, "5", 5, true);
3382 PLAY_WITH_VARIANT(v2, false, "5", 5, true);
3383 PLAY_WITH_VARIANT(v3, false, "5", 5, true);
3384 PLAY_WITH_VARIANT(v4, false, "5", 5, true);
3386 PLAY_WITH_VARIANT(v5, false, QString(), 0, false);
3389 QCOMPARE(MyMovable::count, 0);
3391 QSharedDataPointer<MyShared> d(new MyShared);
3392 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3394 QCOMPARE(MyMovable::count, 0);
3397 QList<QList<int> > data;
3398 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3399 data << (QList<int>() << 42);
3400 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3404 QList<QVector<int> > data;
3405 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3406 data << (QVector<int>() << 42);
3407 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3411 QList<QSet<int> > data;
3412 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3413 data << (QSet<int>() << 42);
3414 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3418 QList<QLinkedList<int> > data;
3419 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3420 data << (QLinkedList<int>() << 42);
3421 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3425 void tst_QVariant::movabilityTest()
3427 // This test checks if QVariant is movable even if an internal data is not movable.
3428 QVERIFY(!MyNotMovable::count);
3430 QVariant variant = QVariant::fromValue(MyNotMovable());
3431 QVERIFY(MyNotMovable::count);
3433 // prepare destination memory space to which variant will be moved
3435 QCOMPARE(buffer[0].type(), QVariant::Invalid);
3436 buffer[0].~QVariant();
3438 memcpy(buffer, &variant, sizeof(QVariant));
3439 QCOMPARE(buffer[0].type(), QVariant::UserType);
3440 QCOMPARE(buffer[0].userType(), qMetaTypeId<MyNotMovable>());
3441 MyNotMovable tmp(buffer[0].value<MyNotMovable>());
3443 new (&variant) QVariant();
3445 QVERIFY(!MyNotMovable::count);
3448 void tst_QVariant::variantInVariant()
3451 QCOMPARE(var1.type(), QVariant::Int);
3452 QVariant var2 = var1;
3453 QCOMPARE(var2, var1);
3454 QCOMPARE(var2.type(), QVariant::Int);
3455 QVariant var3 = QVariant::fromValue(var1);
3456 QCOMPARE(var3, var1);
3457 QCOMPARE(var3.type(), QVariant::Int);
3458 QVariant var4 = qvariant_cast<QVariant>(var1);
3459 QCOMPARE(var4, var1);
3460 QCOMPARE(var4.type(), QVariant::Int);
3463 QCOMPARE(var5, var1);
3464 QCOMPARE(var5.type(), QVariant::Int);
3466 var6.setValue(var1);
3467 QCOMPARE(var6, var1);
3468 QCOMPARE(var6.type(), QVariant::Int);
3470 QCOMPARE(QVariant::fromValue(var1), QVariant::fromValue(var2));
3471 QCOMPARE(qvariant_cast<QVariant>(var3), QVariant::fromValue(var4));
3472 QCOMPARE(qvariant_cast<QVariant>(var5), qvariant_cast<QVariant>(var6));
3474 QString str("hello");
3475 QVariant var8 = qvariant_cast<QVariant>(QVariant::fromValue(QVariant::fromValue(str)));
3476 QCOMPARE((int)var8.type(), (int)QVariant::String);
3477 QCOMPARE(qvariant_cast<QString>(QVariant(qvariant_cast<QVariant>(var8))), str);
3479 QVariant var9(qMetaTypeId<QVariant>(), &var1);
3480 QCOMPARE(var9.userType(), qMetaTypeId<QVariant>());
3481 QCOMPARE(qvariant_cast<QVariant>(var9), var1);
3484 void tst_QVariant::colorInteger()
3486 QVariant v = QColor(Qt::red);
3487 QCOMPARE(v.type(), QVariant::Color);
3488 QCOMPARE(v.value<QColor>(), QColor(Qt::red));
3491 QCOMPARE(v.type(), QVariant::Int);
3492 QCOMPARE(v.toInt(), 1000);
3494 v.setValue(QColor(Qt::yellow));
3495 QCOMPARE(v.type(), QVariant::Color);
3496 QCOMPARE(v.value<QColor>(), QColor(Qt::yellow));
3500 Q_DECLARE_OPAQUE_POINTER(Forward*)
3501 Q_DECLARE_METATYPE(Forward*)
3503 void tst_QVariant::forwardDeclare()
3506 QVariant v = QVariant::fromValue(f);
3507 QCOMPARE(qvariant_cast<Forward*>(v), f);
3510 void tst_QVariant::loadQt5Stream_data()
3512 dataStream_data(QDataStream::Qt_5_0);
3515 void tst_QVariant::loadQt5Stream()
3517 loadQVariantFromDataStream(QDataStream::Qt_5_0);
3520 void tst_QVariant::saveQt5Stream_data()
3522 dataStream_data(QDataStream::Qt_5_0);
3525 void tst_QVariant::saveQt5Stream()
3527 saveQVariantFromDataStream(QDataStream::Qt_5_0);
3530 void tst_QVariant::loadQt4Stream_data()
3532 dataStream_data(QDataStream::Qt_4_9);
3535 void tst_QVariant::loadQt4Stream()
3537 loadQVariantFromDataStream(QDataStream::Qt_4_9);
3540 void tst_QVariant::saveQt4Stream_data()
3542 dataStream_data(QDataStream::Qt_4_9);
3545 void tst_QVariant::saveQt4Stream()
3547 saveQVariantFromDataStream(QDataStream::Qt_4_9);
3550 void tst_QVariant::dataStream_data(QDataStream::Version version)
3552 QTest::addColumn<QString>("fileName");
3556 case QDataStream::Qt_4_9:
3557 path = QString::fromLatin1("qt4.9");
3559 case QDataStream::Qt_5_0:
3560 path = QString::fromLatin1("qt5.0");
3566 path = path.prepend(":/stream/").append("/");
3569 foreach (const QFileInfo &fileInfo, dir.entryInfoList(QStringList() << "*.bin")) {
3570 QTest::newRow((path + fileInfo.fileName()).toLatin1()) << fileInfo.filePath();
3576 void tst_QVariant::loadQVariantFromDataStream(QDataStream::Version version)
3578 QFETCH(QString, fileName);
3580 QFile file(fileName);
3581 QVERIFY(file.open(QIODevice::ReadOnly));
3583 QDataStream stream(&file);
3584 stream.setVersion(version);
3587 QVariant loadedVariant;
3588 stream >> typeName >> loadedVariant;
3590 const int id = QMetaType::type(typeName.toLatin1());
3591 if (id == QMetaType::Void) {
3592 // Void type is not supported by QVariant
3596 QVariant constructedVariant(static_cast<QVariant::Type>(id));
3597 QCOMPARE(constructedVariant.userType(), id);
3598 QCOMPARE(QMetaType::typeName(loadedVariant.userType()), typeName.toLatin1().constData());
3599 QCOMPARE(loadedVariant.userType(), constructedVariant.userType());
3602 void tst_QVariant::saveQVariantFromDataStream(QDataStream::Version version)
3604 QFETCH(QString, fileName);
3606 QFile file(fileName);
3607 QVERIFY(file.open(QIODevice::ReadOnly));
3608 QDataStream dataFileStream(&file);
3611 dataFileStream >> typeName;
3612 QByteArray data = file.readAll();
3613 const int id = QMetaType::type(typeName.toLatin1());
3614 if (id == QMetaType::Void) {
3615 // Void type is not supported by QVariant
3620 buffer.open(QIODevice::ReadWrite);
3621 QDataStream stream(&buffer);
3622 stream.setVersion(version);
3624 QVariant constructedVariant(static_cast<QVariant::Type>(id));
3625 QCOMPARE(constructedVariant.userType(), id);
3626 stream << constructedVariant;
3628 // We are testing QVariant there is no point in testing full array.
3629 QCOMPARE(buffer.data().left(5), data.left(5));
3632 QVariant recunstructedVariant;
3633 stream >> recunstructedVariant;
3634 QCOMPARE(recunstructedVariant.userType(), constructedVariant.userType());
3637 class MessageHandler {
3639 MessageHandler(const int typeId, QtMsgHandler msgHandler = handler)
3640 : oldMsgHandler(qInstallMsgHandler(msgHandler))
3647 qInstallMsgHandler(oldMsgHandler);
3650 bool testPassed() const
3655 static void handler(QtMsgType, const char *txt)
3657 QString msg = QString::fromLatin1(txt);
3658 // Format itself is not important, but basic data as a type name should be included in the output
3659 ok = msg.startsWith("QVariant(");
3660 QVERIFY2(ok, (QString::fromLatin1("Message is not started correctly: '") + msg + '\'').toLatin1().constData());
3661 ok &= (currentId == QMetaType::UnknownType
3662 ? msg.contains("Invalid")
3663 : msg.contains(QMetaType::typeName(currentId)));
3664 QVERIFY2(ok, (QString::fromLatin1("Message doesn't contain type name: '") + msg + '\'').toLatin1().constData());
3665 if (currentId == QMetaType::Char || currentId == QMetaType::QChar) {
3666 // Chars insert '\0' into the qdebug stream, it is not possible to find a real string length
3669 if (QMetaType::typeFlags(currentId) & QMetaType::PointerToQObject) {
3670 QByteArray currentName = QMetaType::typeName(currentId);
3671 currentName.chop(1);
3672 ok &= (msg.contains(", " + currentName) || msg.contains(", 0x0"));
3674 ok &= msg.endsWith(") ");
3675 QVERIFY2(ok, (QString::fromLatin1("Message is not correctly finished: '") + msg + '\'').toLatin1().constData());
3679 QtMsgHandler oldMsgHandler;
3680 static int currentId;
3683 bool MessageHandler::ok;
3684 int MessageHandler::currentId;
3686 void tst_QVariant::debugStream_data()
3688 QTest::addColumn<QVariant>("variant");
3689 QTest::addColumn<int>("typeId");
3690 for (int id = 0; id < QMetaType::User; ++id) {
3691 const char *tagName = QMetaType::typeName(id);
3694 if (id != QMetaType::Void) {
3695 QTest::newRow(tagName) << QVariant(static_cast<QVariant::Type>(id)) << id;
3698 QTest::newRow("QBitArray(111)") << QVariant(QBitArray(3, true)) << qMetaTypeId<QBitArray>();
3699 QTest::newRow("CustomStreamableClass") << QVariant(qMetaTypeId<CustomStreamableClass>(), 0) << qMetaTypeId<CustomStreamableClass>();
3700 QTest::newRow("MyClass") << QVariant(qMetaTypeId<MyClass>(), 0) << qMetaTypeId<MyClass>();
3701 QTest::newRow("InvalidVariant") << QVariant() << int(QMetaType::UnknownType);
3702 QTest::newRow("CustomQObject") << QVariant::fromValue(this) << qMetaTypeId<tst_QVariant*>();
3705 void tst_QVariant::debugStream()
3707 QFETCH(QVariant, variant);
3708 QFETCH(int, typeId);
3710 MessageHandler msgHandler(typeId);
3711 qDebug() << variant;
3712 QVERIFY(msgHandler.testPassed());
3715 struct MessageHandlerType : public MessageHandler
3717 MessageHandlerType(const int typeId)
3718 : MessageHandler(typeId, handler)
3720 static void handler(QtMsgType, const char *txt)
3722 QString msg = QString::fromLatin1(txt);
3723 // Format itself is not important, but basic data as a type name should be included in the output
3724 ok = msg.startsWith("QVariant::");
3725 QVERIFY2(ok, (QString::fromLatin1("Message is not started correctly: '") + msg + '\'').toLatin1().constData());
3726 ok &= (currentId == QMetaType::UnknownType
3727 ? msg.contains("Invalid")
3728 : msg.contains(QMetaType::typeName(currentId)));
3729 QVERIFY2(ok, (QString::fromLatin1("Message doesn't contain type name: '") + msg + '\'').toLatin1().constData());
3733 void tst_QVariant::debugStreamType_data()
3738 void tst_QVariant::debugStreamType()
3740 QFETCH(QVariant, variant);
3741 QFETCH(int, typeId);
3743 MessageHandlerType msgHandler(typeId);
3744 qDebug() << QVariant::Type(typeId);
3745 QVERIFY(msgHandler.testPassed());
3748 void tst_QVariant::guiVariantAtExit()
3750 // crash test, it should not crash at QGuiApplication exit
3751 static QVariant cursor = QCursor();
3752 static QVariant point = QPoint();
3753 static QVariant image = QImage();
3754 static QVariant pallete = QPalette();
3762 void tst_QVariant::widgetsVariantAtExit()
3764 // crash test, it should not crash at QGuiApplication exit
3765 static QVariant icon= QIcon();
3766 static QVariant sizePolicy = QSizePolicy();
3768 Q_UNUSED(sizePolicy);
3772 QTEST_MAIN(tst_QVariant)
3773 #include "tst_qvariant.moc"