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>
61 #include <qtransform.h>
62 #include <qvector2d.h>
63 #include <qvector3d.h>
64 #include <qvector4d.h>
65 #include <qquaternion.h>
70 Q_DECLARE_METATYPE(qlonglong)
71 Q_DECLARE_METATYPE(qulonglong)
72 Q_DECLARE_METATYPE(QPointF)
73 Q_DECLARE_METATYPE(QRectF)
74 Q_DECLARE_METATYPE(QSize)
75 Q_DECLARE_METATYPE(QSizeF)
76 Q_DECLARE_METATYPE(QLine)
77 Q_DECLARE_METATYPE(QLineF)
78 Q_DECLARE_METATYPE(QPoint)
79 Q_DECLARE_METATYPE(QRect)
80 Q_DECLARE_METATYPE(QPixmap)
81 Q_DECLARE_METATYPE(QBrush)
82 Q_DECLARE_METATYPE(QFont)
83 Q_DECLARE_METATYPE(QColor)
84 Q_DECLARE_METATYPE(QKeySequence)
86 class CustomNonQObject;
88 class tst_QVariant : public QObject
93 tst_QVariant(QObject *parent = 0)
94 : QObject(parent), customNonQObjectPointer(0)
100 void cleanupTestCase();
103 void copy_constructor();
107 void canConvert_data();
140 void toLongLong_data();
143 void toULongLong_data();
146 void toByteArray_data();
149 void toString_data();
158 void toDateTime_data();
161 void toDouble_data();
164 void toPointF_data();
170 void toKeySequence_data();
171 void toKeySequence();
179 void toPixmap_data();
188 void qvariant_cast_QObject_data();
189 void qvariant_cast_QObject();
190 void qvariant_cast_QObject_derived();
209 void basicUserType();
213 void writeToReadFromDataStream_data();
214 void writeToReadFromDataStream();
215 void writeToReadFromOldDataStream();
216 void checkDataStream();
218 void operator_eq_eq_data();
219 void operator_eq_eq();
221 void operator_eq_eq_rhs();
223 void typeName_data();
227 void streamInvalidVariant();
231 void data_(); // data is virtual function in QtTestCase
234 void saveLoadCustomTypes();
241 void convertToQUint8() const;
242 void invalidQColor() const;
243 void comparePointers() const;
244 void voidStar() const;
245 void dataStar() const;
246 void canConvertQStringList() const;
247 void canConvertQStringList_data() const;
248 void canConvertMetaTypeToInt() const;
249 void variantToDateTimeWithoutWarnings() const;
250 void invalidDateTime() const;
252 void loadUnknownUserType();
253 void loadBrokenUserType();
255 void invalidDate() const;
256 void compareCustomTypes() const;
257 void timeToDateTime() const;
258 void copyingUserTypes() const;
259 void convertBoolToByteArray() const;
260 void convertBoolToByteArray_data() const;
261 void convertByteArrayToBool() const;
262 void convertByteArrayToBool_data() const;
263 void toIntFromQString() const;
264 void toIntFromDouble() const;
267 void numericalConvert();
268 void moreCustomTypes();
269 void movabilityTest();
270 void variantInVariant();
274 void forwardDeclare();
275 void debugStream_data();
278 void loadQt4Stream_data();
279 void loadQt4Stream();
280 void saveQt4Stream_data();
281 void saveQt4Stream();
282 void loadQt5Stream_data();
283 void loadQt5Stream();
284 void saveQt5Stream_data();
285 void saveQt5Stream();
287 void dataStream_data(QDataStream::Version version);
288 void loadQVariantFromDataStream(QDataStream::Version version);
289 void saveQVariantFromDataStream(QDataStream::Version version);
291 CustomNonQObject *customNonQObjectPointer;
292 QVector<QObject*> objectPointerTestData;
295 Q_DECLARE_METATYPE(QDate)
296 Q_DECLARE_METATYPE(QTime)
297 Q_DECLARE_METATYPE(QDateTime)
298 Q_DECLARE_METATYPE(QVariant)
300 const qlonglong intMax1 = (qlonglong)INT_MAX + 1;
301 const qulonglong uintMax1 = (qulonglong)UINT_MAX + 1;
303 void tst_QVariant::constructor()
306 QVERIFY( !variant.isValid() );
307 QVERIFY( variant.isNull() );
309 QVariant var2(variant);
310 QVERIFY( !var2.isValid() );
311 QVERIFY( variant.isNull() );
313 QVariant varll(intMax1);
314 QVariant varll2(varll);
315 QCOMPARE(varll2, varll);
317 QVariant var3(QVariant::String);
318 QCOMPARE(var3.typeName(), "QString");
319 QVERIFY(var3.isNull());
320 QVERIFY(var3.isValid());
322 QVariant var4(QVariant::Invalid);
323 QCOMPARE(var4.type(), QVariant::Invalid);
324 QVERIFY(var4.isNull());
325 QVERIFY(!var4.isValid());
327 QVariant var5(QLatin1String("hallo"));
328 QCOMPARE(var5.type(), QVariant::String);
329 QCOMPARE(var5.typeName(), "QString");
331 QVariant var6(qlonglong(0));
332 QCOMPARE(var6.type(), QVariant::LongLong);
333 QCOMPARE(var6.typeName(), "qlonglong");
336 QVERIFY(var7.isValid());
337 QVERIFY(!var7.isNull());
339 var8.setValue<int>(5);
340 QVERIFY(var8.isValid());
341 QVERIFY(!var8.isNull());
344 void tst_QVariant::copy_constructor()
346 QVariant var7(QVariant::Int);
348 QCOMPARE(var8.type(), QVariant::Int);
349 QVERIFY(var8.isNull());
352 void tst_QVariant::isNull()
355 QVERIFY( var.isNull() );
358 QVariant var1( str1 );
359 QVERIFY( var1.isNull() );
361 QVariant var2( QString::null );
362 QVERIFY( var2.isNull() );
364 QVariant var3( QString( "blah" ) );
365 QVERIFY( !var3.isNull() );
368 QVERIFY( !var4.isNull() );
370 QVariant var5 = QString();
371 QVERIFY( var5.isNull() );
373 QVariant var6( QString( "blah" ) );
374 QVERIFY( !var6.isNull() );
376 QVERIFY( var6.isNull() );
377 var6.convert( QVariant::String );
378 QVERIFY( var6.isNull() );
379 QVariant varLL( (qlonglong)0 );
380 QVERIFY( !varLL.isNull() );
381 QVariant var7(QString::null);
382 QVERIFY(var7.isNull());
385 void tst_QVariant::swap()
387 QVariant v1 = 1, v2 = 2.0;
389 QCOMPARE(v1.type(),QVariant::Double);
390 QCOMPARE(v1.toDouble(),2.0);
391 QCOMPARE(v2.type(),QVariant::Int);
392 QCOMPARE(v2.toInt(),1);
395 void tst_QVariant::canConvert_data()
397 QTest::addColumn<QVariant>("val");
398 QTest::addColumn<bool>("BitArrayCast");
399 QTest::addColumn<bool>("BitmapCast");
400 QTest::addColumn<bool>("BoolCast");
401 QTest::addColumn<bool>("BrushCast");
402 QTest::addColumn<bool>("ByteArrayCast");
403 QTest::addColumn<bool>("ColorCast");
404 QTest::addColumn<bool>("CursorCast");
405 QTest::addColumn<bool>("DateCast");
406 QTest::addColumn<bool>("DateTimeCast");
407 QTest::addColumn<bool>("DoubleCast");
408 QTest::addColumn<bool>("FontCast");
409 QTest::addColumn<bool>("ImageCast");
410 QTest::addColumn<bool>("IntCast");
411 QTest::addColumn<bool>("InvalidCast");
412 QTest::addColumn<bool>("KeySequenceCast");
413 QTest::addColumn<bool>("ListCast");
414 QTest::addColumn<bool>("LongLongCast");
415 QTest::addColumn<bool>("MapCast");
416 QTest::addColumn<bool>("PaletteCast");
417 QTest::addColumn<bool>("PenCast");
418 QTest::addColumn<bool>("PixmapCast");
419 QTest::addColumn<bool>("PointCast");
420 QTest::addColumn<bool>("RectCast");
421 QTest::addColumn<bool>("RegionCast");
422 QTest::addColumn<bool>("SizeCast");
423 QTest::addColumn<bool>("SizePolicyCast");
424 QTest::addColumn<bool>("StringCast");
425 QTest::addColumn<bool>("StringListCast");
426 QTest::addColumn<bool>("TimeCast");
427 QTest::addColumn<bool>("UIntCast");
428 QTest::addColumn<bool>("ULongLongCast");
439 // 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
442 QVariant var(QBitArray(0));
443 QTest::newRow("BitArray")
444 << 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;
445 var = qVariantFromValue(QBitmap());
446 QTest::newRow("Bitmap")
447 << 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;
448 var = qVariantFromValue(QBrush());
449 QTest::newRow("Brush")
450 << 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;
451 var = QVariant(QByteArray());
452 QTest::newRow("ByteArray")
453 << 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;
454 var = qVariantFromValue(QColor());
455 QTest::newRow("Color")
456 << 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;
458 var = qVariantFromValue(QCursor());
459 QTest::newRow("Cursor")
460 << 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;
462 var = QVariant(QDate());
463 QTest::newRow("Date")
464 << 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;
465 var = QVariant(QDateTime());
466 QTest::newRow("DateTime")
467 << 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;
468 var = QVariant((double)0.1);
469 QTest::newRow("Double")
470 << 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;
471 var = QVariant(0.1f);
472 QTest::newRow("Float")
473 << 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;
474 var = qVariantFromValue(QFont());
475 QTest::newRow("Font")
476 << 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;
477 var = qVariantFromValue(QIcon());
478 QTest::newRow("Icon")
479 << 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;
480 var = qVariantFromValue(QImage());
481 QTest::newRow("Image")
482 << 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;
483 var = QVariant((int)1);
485 << 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;
487 QTest::newRow("Invalid")
488 << 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;
489 var = qVariantFromValue(QKeySequence());
490 QTest::newRow("KeySequence")
491 << 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;
492 var = QVariant(QList<QVariant>());
493 QTest::newRow("List")
494 << 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;
495 var = QVariant((qlonglong)1);
496 QTest::newRow("LongLong")
497 << 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;
498 var = QVariant(QMap<QString,QVariant>());
500 << 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;
501 var = qVariantFromValue(QPalette());
502 QTest::newRow("Palette")
503 << 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;
504 var = qVariantFromValue(QPen());
506 << 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;
507 var = qVariantFromValue(QPixmap());
508 QTest::newRow("Pixmap")
509 << 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;
510 var = qVariantFromValue(QPolygon());
511 QTest::newRow("PointArray")
512 << 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;
513 var = QVariant(QPoint());
514 QTest::newRow("Point")
515 << 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;
516 var = QVariant(QRect());
517 QTest::newRow("Rect")
518 << 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;
519 var = qVariantFromValue(QRegion());
520 QTest::newRow("Region")
521 << 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;
522 var = QVariant(QSize());
523 QTest::newRow("Size")
524 << 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;
525 var = qVariantFromValue(QSizePolicy());
526 QTest::newRow("SizePolicy")
527 << 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;
528 var = QVariant(QString());
529 QTest::newRow("String")
530 << 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;
531 var = QVariant(QStringList("entry"));
532 QTest::newRow("StringList")
533 << 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;
534 var = QVariant(QTime());
535 QTest::newRow("Time")
536 << 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;
537 var = QVariant((uint)1);
538 QTest::newRow("UInt")
539 << var << N << N << Y << N << Y << N << N << N << N << Y << N << N << Y << N << N << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y;
540 var = QVariant((qulonglong)1);
541 QTest::newRow("ULongLong")
542 << var << N << N << Y << N << Y << N << N << N << N << Y << N << N << Y << N << N << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y;
548 void tst_QVariant::canConvert()
550 QFETCH(QVariant, val);
551 QFETCH(bool, BitArrayCast);
552 QFETCH(bool, BitmapCast);
553 QFETCH(bool, BoolCast);
554 QFETCH(bool, BrushCast);
555 QFETCH(bool, ByteArrayCast);
556 QFETCH(bool, ColorCast);
557 QFETCH(bool, CursorCast);
558 QFETCH(bool, DateCast);
559 QFETCH(bool, DateTimeCast);
560 QFETCH(bool, DoubleCast);
561 QFETCH(bool, FontCast);
562 QFETCH(bool, ImageCast);
563 QFETCH(bool, IntCast);
564 QFETCH(bool, InvalidCast);
565 QFETCH(bool, KeySequenceCast);
566 QFETCH(bool, ListCast);
567 QFETCH(bool, LongLongCast);
568 QFETCH(bool, MapCast);
569 QFETCH(bool, PaletteCast);
570 QFETCH(bool, PenCast);
571 QFETCH(bool, PixmapCast);
572 QFETCH(bool, PointCast);
573 QFETCH(bool, RectCast);
574 QFETCH(bool, RegionCast);
575 QFETCH(bool, SizeCast);
576 QFETCH(bool, SizePolicyCast);
577 QFETCH(bool, StringCast);
578 QFETCH(bool, StringListCast);
579 QFETCH(bool, TimeCast);
580 QFETCH(bool, UIntCast);
581 QFETCH(bool, ULongLongCast);
583 QCOMPARE(val.canConvert(QVariant::BitArray), BitArrayCast);
584 QCOMPARE(val.canConvert(QVariant::Bitmap), BitmapCast);
585 QCOMPARE(val.canConvert(QVariant::Bool), BoolCast);
586 QCOMPARE(val.canConvert(QVariant::Brush), BrushCast);
587 QCOMPARE(val.canConvert(QVariant::ByteArray), ByteArrayCast);
588 QCOMPARE(val.canConvert(QVariant::Color), ColorCast);
589 QCOMPARE(val.canConvert(QVariant::Cursor), CursorCast);
590 QCOMPARE(val.canConvert(QVariant::Date), DateCast);
591 QCOMPARE(val.canConvert(QVariant::DateTime), DateTimeCast);
592 QCOMPARE(val.canConvert(QVariant::Double), DoubleCast);
593 QCOMPARE(val.canConvert(QVariant::Type(QMetaType::Float)), DoubleCast);
594 QCOMPARE(val.canConvert(QVariant::Font), FontCast);
595 QCOMPARE(val.canConvert(QVariant::Image), ImageCast);
596 QCOMPARE(val.canConvert(QVariant::Int), IntCast);
597 QCOMPARE(val.canConvert(QVariant::Invalid), InvalidCast);
598 QCOMPARE(val.canConvert(QVariant::KeySequence), KeySequenceCast);
599 QCOMPARE(val.canConvert(QVariant::List), ListCast);
600 QCOMPARE(val.canConvert(QVariant::LongLong), LongLongCast);
601 QCOMPARE(val.canConvert(QVariant::Map), MapCast);
602 QCOMPARE(val.canConvert(QVariant::Palette), PaletteCast);
603 QCOMPARE(val.canConvert(QVariant::Pen), PenCast);
604 QCOMPARE(val.canConvert(QVariant::Pixmap), PixmapCast);
605 QCOMPARE(val.canConvert(QVariant::Point), PointCast);
606 QCOMPARE(val.canConvert(QVariant::Rect), RectCast);
607 QCOMPARE(val.canConvert(QVariant::Region), RegionCast);
608 QCOMPARE(val.canConvert(QVariant::Size), SizeCast);
609 QCOMPARE(val.canConvert(QVariant::SizePolicy), SizePolicyCast);
610 QCOMPARE(val.canConvert(QVariant::String), StringCast);
611 QCOMPARE(val.canConvert(QVariant::StringList), StringListCast);
612 QCOMPARE(val.canConvert(QVariant::Time), TimeCast);
613 QCOMPARE(val.canConvert(QVariant::UInt), UIntCast);
614 QCOMPARE(val.canConvert(QVariant::ULongLong), ULongLongCast);
617 void tst_QVariant::toInt_data()
619 QTest::addColumn<QVariant>("value");
620 QTest::addColumn<int>("result");
621 QTest::addColumn<bool>("valueOK");
623 QTest::newRow( "invalid" ) << QVariant() << 0 << false;
624 QTest::newRow( "int" ) << QVariant( 123 ) << 123 << true;
625 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << 3 << true;
626 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << 3 << true;
627 QTest::newRow( "uint" ) << QVariant( 123u ) << 123 << true;
628 QTest::newRow( "int-string" ) << QVariant( QString("123") ) << 123 << true;
629 QTest::newRow( "string" ) << QVariant( QString("Unicode String") ) << 0 << false;
630 QTest::newRow( "longlong0" ) << QVariant( (qlonglong)34 ) << 34 << true;
631 QTest::newRow( "longlong1" ) << QVariant( intMax1 ) << (int)INT_MIN << true;
632 QTest::newRow( "ulonglong0" ) << QVariant( (qulonglong)34 ) << 34 << true;
633 QTest::newRow( "ulonglong1" ) << QVariant( uintMax1 ) << 0 << true;
634 QTest::newRow( "signedint" ) << QVariant( -123 ) << -123 << true;
635 QTest::newRow( "signeddouble" ) << QVariant( -3.1415927 ) << -3 << true;
636 QTest::newRow( "signedfloat" ) << QVariant( -3.1415927f ) << -3 << true;
637 QTest::newRow( "signedint-string" ) << QVariant( QString("-123") ) << -123 << true;
638 QTest::newRow( "signedlonglong0" ) << QVariant( (qlonglong)-34 ) << -34 << true;
639 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << int('a') << true;
640 QTest::newRow( "keysequence" ) << qVariantFromValue( QKeySequence( Qt::Key_A ) ) << 65 << true;
641 QByteArray bytearray(4, ' ');
646 QTest::newRow( "QByteArray1" ) << QVariant( bytearray ) << 0 << false;
651 QTest::newRow( "QByteArray2" ) << QVariant( bytearray ) << 4500 << true;
654 void tst_QVariant::toInt()
656 QFETCH( QVariant, value );
657 QFETCH( int, result );
658 QFETCH( bool, valueOK );
659 QVERIFY( value.isValid() == value.canConvert( QVariant::Int ) );
661 int i = value.toInt( &ok );
662 QCOMPARE( i, result );
663 QVERIFY( ok == valueOK );
666 void tst_QVariant::toUInt_data()
668 QTest::addColumn<QVariant>("value");
669 QTest::addColumn<uint>("result");
670 QTest::addColumn<bool>("valueOK");
672 QTest::newRow( "int" ) << QVariant( 123 ) << (uint)123 << true;
673 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (uint)3 << true;
674 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (uint)3 << true;
675 QTest::newRow( "uint" ) << QVariant( 123u ) << (uint)123 << true;
676 QTest::newRow( "int-string" ) << QVariant( QString("123") ) << (uint)123 << true;
677 QTest::newRow( "string" ) << QVariant( QString("Unicode String") ) << (uint)0 << false;
678 QTest::newRow( "string2" ) << QVariant( QString("4") ) << (uint)4 << true;
679 QTest::newRow( "longlong0" ) << QVariant( (qlonglong)34 ) << (uint)34 << true;
680 QTest::newRow( "longlong1" ) << QVariant( intMax1 ) << (uint)INT_MIN << true;
681 QTest::newRow( "ulonglong0" ) << QVariant( (qulonglong)34 ) << (uint)34 << true;
682 QTest::newRow( "ulonglong1" ) << QVariant( uintMax1 ) << (uint)0 << true;
683 QTest::newRow( "negativeint" ) << QVariant( -123 ) << (uint)-123 << true;
684 QTest::newRow( "negativedouble" ) << QVariant( -3.1415927 ) << (uint)-3 << true;
685 QTest::newRow( "negativefloat" ) << QVariant( -3.1415927f ) << (uint)-3 << true;
686 QTest::newRow( "negativeint-string" ) << QVariant( QString("-123") ) << (uint)0 << false;
687 QTest::newRow( "negativelonglong0" ) << QVariant( (qlonglong)-34 ) << (uint)-34 << true;
688 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << uint('a') << true;
689 QByteArray bytearray(4, ' ');
694 QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (uint)4321 << true;
697 void tst_QVariant::toUInt()
699 QFETCH( QVariant, value );
700 QFETCH( uint, result );
701 QFETCH( bool, valueOK );
702 QVERIFY( value.isValid() );
703 QVERIFY( value.canConvert( QVariant::UInt ) );
706 uint i = value.toUInt( &ok );
707 QVERIFY( ok == valueOK );
708 QCOMPARE( i, result );
712 void tst_QVariant::toSize_data()
714 QTest::addColumn<QVariant>("value");
715 QTest::addColumn<QSize>("result");
716 QTest::newRow( "qsizef4" ) << QVariant( QSizeF(4, 2) ) << QSize(4, 2);
717 QTest::newRow( "qsizef1" ) << QVariant( QSizeF(0, 0) ) << QSize(0, 0);
718 QTest::newRow( "qsizef2" ) << QVariant( QSizeF(-5, -1) ) << QSize(-5, -1);
719 QTest::newRow( "qsizef3" ) << QVariant( QSizeF() ) << QSize();
722 void tst_QVariant::toSize()
724 QFETCH( QVariant, value );
725 QFETCH( QSize, result );
726 QVERIFY( value.isValid() );
727 QVERIFY( value.canConvert( QVariant::Size ) );
729 QSize i = value.toSize();
730 QCOMPARE( i, result );
733 void tst_QVariant::toSizeF_data()
735 QTest::addColumn<QVariant>("value");
736 QTest::addColumn<QSizeF>("result");
737 QTest::newRow( "qsize1" ) << QVariant( QSize(0, 0) ) << QSizeF(0, 0);
738 QTest::newRow( "qsize2" ) << QVariant( QSize(-5, -1) ) << QSizeF(-5, -1);
739 QTest::newRow( "qsize3" ) << QVariant( QSize() ) << QSizeF();
740 QTest::newRow( "qsize4" ) << QVariant(QSize(4,2)) << QSizeF(4,2);
743 void tst_QVariant::toSizeF()
745 QFETCH( QVariant, value );
746 QFETCH( QSizeF, result );
747 QVERIFY( value.isValid() );
748 QVERIFY( value.canConvert( QVariant::SizeF ) );
750 QSizeF i = value.toSizeF();
751 QCOMPARE( i, result );
754 void tst_QVariant::toLine_data()
756 QTest::addColumn<QVariant>("value");
757 QTest::addColumn<QLine>("result");
758 QTest::newRow( "linef1" ) << QVariant( QLineF(1, 2, 3, 4) ) << QLine(1, 2, 3, 4);
759 QTest::newRow( "linef2" ) << QVariant( QLineF(-1, -2, -3, -4) ) << QLine(-1, -2, -3, -4);
760 QTest::newRow( "linef3" ) << QVariant( QLineF(0, 0, 0, 0) ) << QLine(0, 0, 0, 0);
761 QTest::newRow( "linef4" ) << QVariant( QLineF() ) << QLine();
764 void tst_QVariant::toLine()
766 QFETCH( QVariant, value );
767 QFETCH( QLine, result );
768 QVERIFY( value.isValid() );
769 QVERIFY( value.canConvert( QVariant::Line ) );
771 QLine i = value.toLine();
772 QCOMPARE( i, result );
775 void tst_QVariant::toLineF_data()
777 QTest::addColumn<QVariant>("value");
778 QTest::addColumn<QLineF>("result");
779 QTest::newRow( "line1" ) << QVariant( QLine(-1, -2, -3, -4) ) << QLineF(-1, -2, -3, -4);
780 QTest::newRow( "line2" ) << QVariant( QLine(0, 0, 0, 0) ) << QLineF(0, 0, 0, 0);
781 QTest::newRow( "line3" ) << QVariant( QLine() ) << QLineF();
782 QTest::newRow( "line4" ) << QVariant( QLine(1, 2, 3, 4) ) << QLineF(1, 2, 3, 4);
785 void tst_QVariant::toLineF()
787 QFETCH( QVariant, value );
788 QFETCH( QLineF, result );
789 QVERIFY( value.isValid() );
790 QVERIFY( value.canConvert( QVariant::LineF ) );
792 QLineF i = value.toLineF();
793 QCOMPARE( i, result );
796 void tst_QVariant::toPoint_data()
798 QTest::addColumn<QVariant>("value");
799 QTest::addColumn<QPoint>("result");
800 QTest::newRow( "pointf1" ) << QVariant( QPointF(4, 2) ) << QPoint(4, 2);
801 QTest::newRow( "pointf2" ) << QVariant( QPointF(0, 0) ) << QPoint(0, 0);
802 QTest::newRow( "pointf3" ) << QVariant( QPointF(-4, -2) ) << QPoint(-4, -2);
803 QTest::newRow( "pointf4" ) << QVariant( QPointF() ) << QPoint();
804 QTest::newRow( "pointf5" ) << QVariant( QPointF(-4.2f, -2.3f) ) << QPoint(-4, -2);
807 void tst_QVariant::toPoint()
809 QFETCH( QVariant, value );
810 QFETCH( QPoint, result );
811 QVERIFY( value.isValid() );
812 QVERIFY( value.canConvert( QVariant::Point ) );
813 QPoint i = value.toPoint();
814 QCOMPARE( i, result );
817 void tst_QVariant::toRect_data()
819 QTest::addColumn<QVariant>("value");
820 QTest::addColumn<QRect>("result");
821 QTest::newRow( "rectf1" ) << QVariant(QRectF(1, 2, 3, 4)) << QRect(1, 2, 3, 4);
822 QTest::newRow( "rectf2" ) << QVariant(QRectF(0, 0, 0, 0)) << QRect(0, 0, 0, 0);
823 QTest::newRow( "rectf3" ) << QVariant(QRectF(-1, -2, -3, -4)) << QRect(-1, -2, -3, -4);
824 QTest::newRow( "rectf4" ) << QVariant(QRectF(-1.3f, 0, 3.9f, -4.0)) << QRect(-1, 0, 4, -4);
825 QTest::newRow( "rectf5" ) << QVariant(QRectF()) << QRect();
828 void tst_QVariant::toRect()
830 QFETCH( QVariant, value );
831 QFETCH( QRect, result );
832 QVERIFY( value.isValid() );
833 QVERIFY( value.canConvert( QVariant::Rect ) );
834 QRect i = value.toRect();
835 QCOMPARE( i, result );
838 void tst_QVariant::toChar_data()
840 QTest::addColumn<QVariant>("value");
841 QTest::addColumn<QChar>("result");
842 QTest::newRow( "longlong" ) << QVariant(qlonglong('6')) << QChar('6');
843 QTest::newRow( "ulonglong" ) << QVariant(qulonglong('7')) << QChar('7');
846 void tst_QVariant::toChar()
848 QFETCH( QVariant, value );
849 QFETCH( QChar, result );
850 QVERIFY( value.isValid() );
851 QVERIFY( value.canConvert( QVariant::Char ) );
853 QChar i = value.toChar();
854 QCOMPARE( i, result );
857 void tst_QVariant::toBool_data()
859 QTest::addColumn<QVariant>("value");
860 QTest::addColumn<bool>("result");
862 QTest::newRow( "int0" ) << QVariant( 0 ) << false;
863 QTest::newRow( "int1" ) << QVariant( 123 ) << true;
864 QTest::newRow( "uint0" ) << QVariant( 0u ) << false;
865 QTest::newRow( "uint1" ) << QVariant( 123u ) << true;
866 QTest::newRow( "double0" ) << QVariant( 0.0 ) << false;
867 QTest::newRow( "float0" ) << QVariant( 0.0f ) << false;
868 QTest::newRow( "double1" ) << QVariant( 3.1415927 ) << true;
869 QTest::newRow( "float1" ) << QVariant( 3.1415927f ) << true;
870 QTest::newRow( "string0" ) << QVariant( QString("3") ) << true;
871 QTest::newRow( "string1" ) << QVariant( QString("true") ) << true;
872 QTest::newRow( "string2" ) << QVariant( QString("0") ) << false;
873 QTest::newRow( "string3" ) << QVariant( QString("fAlSe") ) << false;
874 QTest::newRow( "longlong0" ) << QVariant( (qlonglong)0 ) << false;
875 QTest::newRow( "longlong1" ) << QVariant( (qlonglong)1 ) << true;
876 QTest::newRow( "ulonglong0" ) << QVariant( (qulonglong)0 ) << false;
877 QTest::newRow( "ulonglong1" ) << QVariant( (qulonglong)1 ) << true;
878 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << true;
879 QTest::newRow( "Null_QChar" ) << QVariant(QChar(0)) << false;
882 void tst_QVariant::toBool()
884 QFETCH( QVariant, value );
885 QFETCH( bool, result );
886 QVERIFY( value.isValid() );
887 QVERIFY( value.canConvert( QVariant::Bool ) );
889 bool i = value.toBool();
890 QCOMPARE( i, result );
893 void tst_QVariant::toPointF_data()
895 QTest::addColumn<QVariant>("value");
896 QTest::addColumn<QPointF>("result");
898 QTest::newRow( "QPoint" ) << QVariant( QPointF( 19, 84) ) << QPointF( 19, 84 );
901 void tst_QVariant::toPointF()
903 QFETCH( QVariant, value );
904 QFETCH( QPointF, result );
905 QVERIFY( value.isValid() );
906 QVERIFY( value.canConvert( QVariant::PointF ) );
907 QPointF d = value.toPointF();
908 QCOMPARE( d, result );
911 void tst_QVariant::toRectF_data()
913 QTest::addColumn<QVariant>("value");
914 QTest::addColumn<QRectF>("result");
916 QRect r( 1, 9, 8, 4 );
917 QRectF rf( 1.0, 9.0, 8.0, 4.0 );
918 QTest::newRow( "QRect" ) << QVariant( r ) << rf;
921 void tst_QVariant::toRectF()
923 QFETCH( QVariant, value );
924 QFETCH( QRectF, result );
925 QVERIFY( value.isValid() );
926 QVERIFY( value.canConvert( QVariant::RectF ) );
927 QRectF d = value.toRectF();
928 QCOMPARE( d, result );
931 void tst_QVariant::toColor_data()
933 QTest::addColumn<QVariant>("value");
934 QTest::addColumn<QColor>("result");
937 QTest::newRow( "string" ) << QVariant( QString( "red" ) ) << c;
938 QTest::newRow( "solid brush" ) << QVariant( QBrush(c) ) << c;
941 void tst_QVariant::toColor()
943 QFETCH( QVariant, value );
944 QFETCH( QColor, result );
945 QVERIFY( value.isValid() );
946 QVERIFY( value.canConvert( QVariant::Color ) );
947 QColor d = qVariantValue<QColor>(value);
948 QCOMPARE( d, result );
951 void tst_QVariant::toPixmap_data()
953 QTest::addColumn<QVariant>("value");
954 QTest::addColumn<QPixmap>("result");
958 QTest::newRow( "image" ) << QVariant( pm ) << pm;
962 QTest::newRow( "bitmap" ) << QVariant( bm ) << QPixmap(bm);
965 void tst_QVariant::toPixmap()
967 QFETCH( QVariant, value );
968 QFETCH( QPixmap, result );
969 QVERIFY( value.isValid() );
970 QVERIFY( value.canConvert( QVariant::Pixmap ) );
971 QPixmap d = qVariantValue<QPixmap>(value);
972 QCOMPARE( d, result );
975 void tst_QVariant::toImage_data()
977 QTest::addColumn<QVariant>("value");
978 QTest::addColumn<QImage>("result");
980 QImage im(30, 30, QImage::Format_ARGB32);
982 QTest::newRow( "image" ) << QVariant( im ) << im;
985 void tst_QVariant::toImage()
987 QFETCH( QVariant, value );
988 QFETCH( QImage, result );
989 QVERIFY( value.isValid() );
990 QVERIFY( value.canConvert( QVariant::Image ) );
991 QImage d = qVariantValue<QImage>(value);
992 QCOMPARE( d, result );
995 void tst_QVariant::toBrush_data()
997 QTest::addColumn<QVariant>("value");
998 QTest::addColumn<QBrush>("result");
1001 QTest::newRow( "color" ) << QVariant( c ) << QBrush(c);
1004 QTest::newRow( "pixmap" ) << QVariant( pm ) << QBrush(pm);
1007 void tst_QVariant::toBrush()
1009 QFETCH( QVariant, value );
1010 QFETCH( QBrush, result );
1011 QVERIFY( value.isValid() );
1012 QVERIFY( value.canConvert( QVariant::Brush ) );
1013 QBrush d = qVariantValue<QBrush>(value);
1014 QCOMPARE( d, result );
1017 void tst_QVariant::toFont_data()
1019 QTest::addColumn<QVariant>("value");
1020 QTest::addColumn<QFont>("result");
1022 QFont f("times",12,-1,false);
1023 QTest::newRow( "string" ) << QVariant( QString( "times,12,-1,5,50,0,0,0,0,0" ) ) << f;
1026 void tst_QVariant::toFont()
1028 QFETCH( QVariant, value );
1029 QFETCH( QFont, result );
1030 QVERIFY( value.isValid() );
1031 QVERIFY( value.canConvert( QVariant::Font ) );
1032 QFont d = qVariantValue<QFont>(value);
1033 QCOMPARE( d, result );
1036 void tst_QVariant::toKeySequence_data()
1038 QTest::addColumn<QVariant>("value");
1039 QTest::addColumn<QKeySequence>("result");
1042 QTest::newRow( "int" ) << QVariant( 67108929 ) << QKeySequence( Qt::CTRL + Qt::Key_A );
1045 QTest::newRow( "qstring" )
1046 << QVariant( QString( "Ctrl+A" ) )
1047 << QKeySequence( Qt::CTRL + Qt::Key_A );
1050 void tst_QVariant::toKeySequence()
1052 QFETCH( QVariant, value );
1053 QFETCH( QKeySequence, result );
1054 QVERIFY( value.isValid() );
1055 QVERIFY( value.canConvert( QVariant::KeySequence ) );
1056 QKeySequence d = qVariantValue<QKeySequence>(value);
1057 QCOMPARE( d, result );
1060 void tst_QVariant::toDouble_data()
1062 QTest::addColumn<QVariant>("value");
1063 QTest::addColumn<double>("result");
1064 QTest::addColumn<bool>("valueOK");
1066 QByteArray bytearray(4, ' ');
1071 QTest::newRow( "bytearray" ) << QVariant( bytearray ) << 32.1 << true;
1074 void tst_QVariant::toDouble()
1076 QFETCH( QVariant, value );
1077 QFETCH( double, result );
1078 QFETCH( bool, valueOK );
1079 QVERIFY( value.isValid() );
1080 QVERIFY( value.canConvert( QVariant::Double ) );
1082 double d = value.toDouble( &ok );
1083 QCOMPARE( d, result );
1084 QVERIFY( ok == valueOK );
1087 void tst_QVariant::toLongLong_data()
1089 QTest::addColumn<QVariant>("value");
1090 QTest::addColumn<qlonglong>("result");
1091 QTest::addColumn<bool>("valueOK");
1093 QTest::newRow( "int0" ) << QVariant( 123 ) << (qlonglong)123 << true;
1094 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (qlonglong)3 << true;
1095 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (qlonglong)3 << true;
1096 QTest::newRow( "uint" ) << QVariant( 123u ) << (qlonglong)123 << true;
1097 QTest::newRow( "int-string" ) << QVariant( QString("123") )
1098 << (qlonglong)123 << true;
1099 QTest::newRow( "string" ) << QVariant( QString("Unicode fun") ) << (qlonglong)0
1101 QTest::newRow( "longlong" ) << QVariant( intMax1 ) << intMax1 << true;
1102 QTest::newRow( "ulonglong" ) << QVariant( uintMax1 ) << (qlonglong)uintMax1 << true;
1103 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << qlonglong('a') << true;
1104 QByteArray bytearray(4, ' ');
1109 QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (qlonglong) 3200 << true;
1112 void tst_QVariant::toLongLong()
1114 QFETCH( QVariant, value );
1115 QFETCH( qlonglong, result );
1116 QFETCH( bool, valueOK );
1117 QVERIFY( value.isValid() );
1118 QVERIFY( value.canConvert( QVariant::LongLong ) );
1120 qlonglong ll = value.toLongLong( &ok );
1121 QCOMPARE( ll, result );
1122 QVERIFY( ok == valueOK );
1125 void tst_QVariant::toULongLong_data()
1127 QTest::addColumn<QVariant>("value");
1128 QTest::addColumn<qulonglong>("result");
1129 QTest::addColumn<bool>("valueOK");
1131 QTest::newRow( "int0" ) << QVariant( 123 ) << (qulonglong)123 << true;
1132 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (qulonglong)3 << true;
1133 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (qulonglong)3 << true;
1134 QTest::newRow( "uint" ) << QVariant( 123u ) << (qulonglong)123 << true;
1135 QTest::newRow( "int-string" ) << QVariant( QString("123") )
1136 << (qulonglong)123 << true;
1137 QTest::newRow( "string" ) << QVariant( QString("Unicode fun") ) << (qulonglong)0
1139 QTest::newRow( "ulonglong-string" ) << QVariant( QString("18446744073709551615") )
1140 << Q_UINT64_C(18446744073709551615)
1142 QTest::newRow( "bytaa-string" ) << QVariant( QString("18446744073709551615") )
1143 << Q_UINT64_C(18446744073709551615)
1145 QTest::newRow( "longlong" ) << QVariant( intMax1 ) << (qulonglong)intMax1 << true;
1146 QTest::newRow( "ulonglong" ) << QVariant( uintMax1 ) << uintMax1 << true;
1147 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << qulonglong('a') << true;
1148 QByteArray bytearray(4, ' ');
1153 QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (qulonglong) 3201 << true;
1156 void tst_QVariant::toULongLong()
1158 QFETCH( QVariant, value );
1159 QFETCH( qulonglong, result );
1160 QFETCH( bool, valueOK );
1161 QVERIFY( value.isValid() );
1162 QVERIFY( value.canConvert( QVariant::ULongLong ) );
1164 qulonglong ll = value.toULongLong( &ok );
1165 QCOMPARE( ll, result );
1166 QVERIFY( ok == valueOK );
1169 void tst_QVariant::toByteArray_data()
1171 QTest::addColumn<QVariant>("value");
1172 QTest::addColumn<QByteArray>("result");
1174 QByteArray ba(5, ' ');
1181 QByteArray variantBa = ba;
1183 QTest::newRow( "qbytearray" ) << QVariant( variantBa ) << ba;
1184 QTest::newRow( "int" ) << QVariant( -123 ) << QByteArray( "-123" );
1185 QTest::newRow( "uint" ) << QVariant( (uint)123 ) << QByteArray( "123" );
1186 QTest::newRow( "double" ) << QVariant( 123.456 ) << QByteArray( "123.456" );
1187 QTest::newRow( "float" ) << QVariant( 123.456f ) << QByteArray( "123.456" );
1188 QTest::newRow( "longlong" ) << QVariant( (qlonglong)34 ) << QByteArray( "34" );
1189 QTest::newRow( "ulonglong" ) << QVariant( (qulonglong)34 ) << QByteArray( "34" );
1192 void tst_QVariant::toByteArray()
1194 QFETCH( QVariant, value );
1195 QFETCH( QByteArray, result );
1196 QVERIFY( value.isValid() );
1197 QVERIFY( value.canConvert( QVariant::ByteArray ) );
1198 QByteArray ba = value.toByteArray();
1199 QCOMPARE( ba, result );
1202 void tst_QVariant::toString_data()
1204 QTest::addColumn<QVariant>("value");
1205 QTest::addColumn<QString>("result");
1207 QTest::newRow( "qstring" ) << QVariant( QString( "Test" ) ) << QString( "Test" );
1208 QTest::newRow( "charstar" ) << QVariant(QLatin1String("Test")) << QString("Test");
1209 QTest::newRow( "qbytearray") << QVariant( QByteArray( "Test\0" ) ) << QString( "Test" );
1210 QTest::newRow( "int" ) << QVariant( -123 ) << QString( "-123" );
1211 QTest::newRow( "uint" ) << QVariant( (uint)123 ) << QString( "123" );
1212 QTest::newRow( "double" ) << QVariant( 123.456 ) << QString( "123.456" );
1213 QTest::newRow( "float" ) << QVariant( 123.456f ) << QString( "123.456" );
1214 QTest::newRow( "bool" ) << QVariant( true ) << QString( "true" );
1215 QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 1, 1 ) ) << QString( "2002-01-01" );
1216 QTest::newRow( "qtime" ) << QVariant( QTime( 12, 34, 56 ) ) << QString( "12:34:56" );
1217 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 1, 1 ), QTime( 12, 34, 56 ) ) ) << QString( "2002-01-01T12:34:56" );
1218 QTest::newRow( "qkeysequence" ) << qVariantFromValue( QKeySequence( Qt::CTRL + Qt::Key_A ) )
1220 << QString( "Ctrl+A" );
1222 << QString(QChar(0x2318)) + "A";
1225 QFont font( "times", 12 );
1226 QTest::newRow( "qfont" ) << qVariantFromValue( font ) << QString("times,12,-1,5,50,0,0,0,0,0");
1227 QTest::newRow( "qcolor" ) << qVariantFromValue( QColor( 10, 10, 10 ) ) << QString( "#0a0a0a" );
1228 QTest::newRow( "llong" ) << QVariant( (qlonglong)Q_INT64_C(123456789012) ) <<
1229 QString( "123456789012" );
1232 void tst_QVariant::toString()
1234 QFETCH( QVariant, value );
1235 QFETCH( QString, result );
1236 QVERIFY( value.isValid() );
1237 QVERIFY( value.canConvert( QVariant::String ) );
1238 QString str = value.toString();
1239 QCOMPARE( str, result );
1242 void tst_QVariant::toDate_data()
1244 QTest::addColumn<QVariant>("value");
1245 QTest::addColumn<QDate>("result");
1247 QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 10, 10 ) ) << QDate( 2002, 10, 10 );
1248 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) ) ) << QDate( 2002, 10, 10 );
1249 QTest::newRow( "qstring" ) << QVariant( QString( "2002-10-10" ) ) << QDate( 2002, 10, 10 );
1252 void tst_QVariant::toDate()
1254 QFETCH( QVariant, value );
1255 QFETCH( QDate, result );
1256 QVERIFY( value.isValid() );
1257 QVERIFY( value.canConvert( QVariant::Date ) );
1258 QCOMPARE( value.toDate(), result );
1261 void tst_QVariant::toTime_data()
1263 QTest::addColumn<QVariant>("value");
1264 QTest::addColumn<QTime>("result");
1266 QTest::newRow( "qtime" ) << QVariant( QTime( 12, 34, 56 ) ) << QTime( 12, 34, 56 );
1267 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) ) ) << QTime( 12, 34, 56 );
1268 QTest::newRow( "qstring" ) << QVariant( QString( "12:34:56" ) ) << QTime( 12, 34, 56 );
1271 void tst_QVariant::toTime()
1273 QFETCH( QVariant, value );
1274 QFETCH( QTime, result );
1275 QVERIFY( value.isValid() );
1276 QVERIFY( value.canConvert( QVariant::Time ) );
1277 QCOMPARE( value.toTime(), result );
1280 void tst_QVariant::toDateTime_data()
1282 QTest::addColumn<QVariant>("value");
1283 QTest::addColumn<QDateTime>("result");
1285 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) ) )
1286 << QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) );
1287 QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 10, 10 ) ) << QDateTime( QDate( 2002, 10, 10 ), QTime( 0, 0, 0 ) );
1288 QTest::newRow( "qstring" ) << QVariant( QString( "2002-10-10T12:34:56" ) ) << QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) );
1291 void tst_QVariant::toDateTime()
1293 QFETCH( QVariant, value );
1294 QFETCH( QDateTime, result );
1295 QVERIFY( value.isValid() );
1296 QVERIFY( value.canConvert( QVariant::DateTime ) );
1297 QCOMPARE( value.toDateTime(), result );
1300 void tst_QVariant::toLocale()
1303 QLocale loc = variant.toLocale();
1304 variant = QLocale::system();
1305 loc = variant.toLocale();
1308 void tst_QVariant::toRegExp()
1311 QRegExp rx = variant.toRegExp();
1312 variant = QRegExp("foo");
1313 rx = variant.toRegExp();
1316 void tst_QVariant::matrix()
1319 QMatrix matrix = qVariantValue<QMatrix>(variant);
1320 QVERIFY(matrix.isIdentity());
1321 qVariantSetValue(variant, QMatrix().rotate(90));
1322 QCOMPARE(QMatrix().rotate(90), qVariantValue<QMatrix>(variant));
1324 void *mmatrix = QMetaType::create(QVariant::Matrix, 0);
1326 QMetaType::destroy(QVariant::Matrix, mmatrix);
1329 void tst_QVariant::matrix4x4()
1332 QMatrix4x4 matrix = qVariantValue<QMatrix4x4>(variant);
1333 QVERIFY(matrix.isIdentity());
1336 qVariantSetValue(variant, m);
1337 QCOMPARE(m, qVariantValue<QMatrix4x4>(variant));
1339 void *mmatrix = QMetaType::create(QVariant::Matrix4x4, 0);
1341 QMetaType::destroy(QVariant::Matrix4x4, mmatrix);
1344 void tst_QVariant::transform()
1347 QTransform matrix = qVariantValue<QTransform>(variant);
1348 QVERIFY(matrix.isIdentity());
1349 qVariantSetValue(variant, QTransform().rotate(90));
1350 QCOMPARE(QTransform().rotate(90), qVariantValue<QTransform>(variant));
1352 void *mmatrix = QMetaType::create(QVariant::Transform, 0);
1354 QMetaType::destroy(QVariant::Transform, mmatrix);
1358 void tst_QVariant::vector2D()
1361 QVector2D vector = qVariantValue<QVector2D>(variant);
1362 QVERIFY(vector.isNull());
1363 qVariantSetValue(variant, QVector2D(0.1, 0.2));
1364 QCOMPARE(QVector2D(0.1, 0.2), qVariantValue<QVector2D>(variant));
1366 void *pvector = QMetaType::create(QVariant::Vector2D, 0);
1368 QMetaType::destroy(QVariant::Vector2D, pvector);
1371 void tst_QVariant::vector3D()
1374 QVector3D vector = qVariantValue<QVector3D>(variant);
1375 QVERIFY(vector.isNull());
1376 qVariantSetValue(variant, QVector3D(0.1, 0.2, 0.3));
1377 QCOMPARE(QVector3D(0.1, 0.2, 0.3), qVariantValue<QVector3D>(variant));
1379 void *pvector = QMetaType::create(QVariant::Vector3D, 0);
1381 QMetaType::destroy(QVariant::Vector3D, pvector);
1384 void tst_QVariant::vector4D()
1387 QVector4D vector = qVariantValue<QVector4D>(variant);
1388 QVERIFY(vector.isNull());
1389 qVariantSetValue(variant, QVector4D(0.1, 0.2, 0.3, 0.4));
1390 QCOMPARE(QVector4D(0.1, 0.2, 0.3, 0.4), qVariantValue<QVector4D>(variant));
1392 void *pvector = QMetaType::create(QVariant::Vector4D, 0);
1394 QMetaType::destroy(QVariant::Vector4D, pvector);
1397 void tst_QVariant::quaternion()
1400 QQuaternion quaternion = qVariantValue<QQuaternion>(variant);
1401 QVERIFY(quaternion.isIdentity());
1402 qVariantSetValue(variant, QQuaternion(0.1, 0.2, 0.3, 0.4));
1403 QCOMPARE(QQuaternion(0.1, 0.2, 0.3, 0.4), qVariantValue<QQuaternion>(variant));
1405 void *pquaternion = QMetaType::create(QVariant::Quaternion, 0);
1406 QVERIFY(pquaternion);
1407 QMetaType::destroy(QVariant::Quaternion, pquaternion);
1410 struct CustomStreamableClass
1413 bool operator==(const CustomStreamableClass& other) const
1415 return i == other.i;
1418 Q_DECLARE_METATYPE(CustomStreamableClass);
1420 QDataStream &operator<<(QDataStream &out, const CustomStreamableClass &myObj)
1422 return out << myObj.i;
1425 QDataStream &operator>>(QDataStream &in, CustomStreamableClass &myObj)
1427 return in >> myObj.i;
1430 void tst_QVariant::writeToReadFromDataStream_data()
1432 qRegisterMetaTypeStreamOperators<CustomStreamableClass>();
1434 QTest::addColumn<QVariant>("writeVariant");
1435 QTest::addColumn<bool>("isNull");
1437 typedef QList<QVariant> variantsList;
1438 variantsList valuelist;
1439 valuelist << QVariant( 1 ) << QVariant( QString("Two") ) << QVariant( 3.45 );
1440 QVariant var(valuelist);
1441 QTest::newRow( "list_valid" ) << var << false;
1444 QTest::newRow( "invalid" ) << QVariant() << true;
1445 QTest::newRow( "bitarray_invalid" ) << QVariant( QBitArray() ) << true;
1446 QBitArray bitarray( 3 );
1450 QTest::newRow( "bitarray_valid" ) << QVariant( bitarray ) << false;
1451 QTest::newRow( "bytearray_invalid" ) << QVariant( QByteArray() ) << true;
1452 QTest::newRow( "int_invalid") << QVariant(QVariant::Int) << true;
1453 QByteArray bytearray(5, ' ');
1458 bytearray[4] = '\0';
1459 QTest::newRow( "bytearray_valid" ) << QVariant( bytearray ) << false;
1460 QTest::newRow( "bitmap_invalid" ) << qVariantFromValue( QBitmap() ) << true;
1461 QBitmap bitmap( 10, 10 );
1462 bitmap.fill( Qt::red );
1463 QTest::newRow( "bitmap_valid" ) << qVariantFromValue( bitmap ) << false;
1464 QTest::newRow( "brush_valid" ) << qVariantFromValue( QBrush( Qt::red ) ) << false;
1465 QTest::newRow( "color_valid" ) << qVariantFromValue( QColor( Qt::red ) ) << false;
1466 #ifndef QT_NO_CURSOR
1467 QTest::newRow( "cursor_valid" ) << qVariantFromValue( QCursor( Qt::PointingHandCursor ) ) << false;
1469 QTest::newRow( "date_invalid" ) << QVariant( QDate() ) << true;
1470 QTest::newRow( "date_valid" ) << QVariant( QDate( 2002, 07, 06 ) ) << false;
1471 QTest::newRow( "datetime_invalid" ) << QVariant( QDateTime() ) << true;
1472 QTest::newRow( "datetime_valid" ) << QVariant( QDateTime( QDate( 2002, 07, 06 ), QTime( 14, 0, 0 ) ) ) << false;
1473 QTest::newRow( "double_valid" ) << QVariant( 123.456 ) << false;
1474 QTest::newRow( "float_valid" ) << QVariant( 123.456f ) << false;
1475 QTest::newRow( "font_valid" ) << qVariantFromValue( QFont( "times", 12 ) ) << false;
1476 QTest::newRow( "pixmap_invalid" ) << qVariantFromValue( QPixmap() ) << true;
1477 QPixmap pixmap( 10, 10 );
1478 pixmap.fill( Qt::red );
1479 QTest::newRow( "pixmap_valid" ) << qVariantFromValue( pixmap ) << false;
1480 // QTest::newRow( "iconset_valid" ) << QVariant( QIcon( pixmap ) ) << false;
1481 QTest::newRow( "image_invalid" ) << qVariantFromValue( QImage() ) << true;
1482 QTest::newRow( "keysequence_valid" ) << qVariantFromValue( QKeySequence( Qt::CTRL + Qt::Key_A ) ) << false;
1483 QTest::newRow( "int_valid" ) << QVariant( -123 ) << false;
1484 typedef QMap<QString, QVariant> variantsMap;
1486 vMap.insert( "int", QVariant( 1 ) );
1487 vMap.insert( "string", QVariant( QString("Two") ) );
1488 vMap.insert( "double", QVariant( 3.45 ) );
1489 vMap.insert( "float", QVariant( 3.45f ) );
1490 QTest::newRow( "map_valid" ) << QVariant( vMap ) << false;
1491 QTest::newRow( "palette_valid" ) << qVariantFromValue(QPalette(QColor("turquoise"))) << false;
1492 QTest::newRow( "pen_valid" ) << qVariantFromValue( QPen( Qt::red ) ) << false;
1493 QTest::newRow( "pointarray_invalid" ) << qVariantFromValue( QPolygon() ) << true;
1494 QTest::newRow( "pointarray_valid" ) << qVariantFromValue( QPolygon( QRect( 10, 10, 20, 20 ) ) ) << false;
1495 QTest::newRow( "region_invalid" ) << qVariantFromValue( QRegion() ) << true;
1496 QTest::newRow( "region_valid" ) << qVariantFromValue( QRegion( 10, 10, 20, 20 ) ) << false;
1497 QTest::newRow( "sizepolicy_valid" ) << qVariantFromValue( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) ) << false;
1498 QTest::newRow( "point_invalid" ) << qVariantFromValue( QPoint() ) << true;
1499 QTest::newRow( "point_valid" ) << qVariantFromValue( QPoint( 10, 10 ) ) << false;
1500 QTest::newRow( "rect_invalid" ) << QVariant( QRect() ) << true;
1501 QTest::newRow( "rect_valid" ) << QVariant( QRect( 10, 10, 20, 20 ) ) << false;
1502 QTest::newRow( "size_invalid" ) << QVariant( QSize( 0, 0 ) ) << true;
1503 QTest::newRow( "size_valid" ) << QVariant( QSize( 10, 10 ) ) << false;
1504 QTest::newRow( "string_invalid" ) << QVariant( QString() ) << true;
1505 QTest::newRow( "string_valid" ) << QVariant( QString( "Test" ) ) << false;
1506 QStringList stringlist;
1507 stringlist << "One" << "Two" << "Three";
1508 QTest::newRow( "stringlist_valid" ) << QVariant( stringlist ) << false;
1509 QTest::newRow( "time_invalid" ) << QVariant( QTime() ) << true;
1510 QTest::newRow( "time_valid" ) << QVariant( QTime( 14, 0, 0 ) ) << false;
1511 QTest::newRow( "uint_valid" ) << QVariant( (uint)123 ) << false;
1512 QTest::newRow( "qchar" ) << QVariant(QChar('a')) << false;
1513 QTest::newRow( "qchar_null" ) << QVariant(QChar(0)) << true;
1514 QTest::newRow( "regexp" ) << QVariant(QRegExp("foo", Qt::CaseInsensitive)) << false;
1515 QTest::newRow( "regexp_empty" ) << QVariant(QRegExp()) << false;
1517 // types known to QMetaType, but not part of QVariant::Type
1518 QTest::newRow("QMetaType::Long invalid") << QVariant(QMetaType::Long, (void *) 0) << false;
1520 QTest::newRow("QMetaType::Long") << QVariant(QMetaType::Long, &longInt) << false;
1521 QTest::newRow("QMetaType::Short invalid") << QVariant(QMetaType::Short, (void *) 0) << false;
1523 QTest::newRow("QMetaType::Short") << QVariant(QMetaType::Short, &shortInt) << false;
1524 QTest::newRow("QMetaType::Char invalid") << QVariant(QMetaType::Char, (void *) 0) << false;
1526 QTest::newRow("QMetaType::Char") << QVariant(QMetaType::Char, &ch) << false;
1527 QTest::newRow("QMetaType::ULong invalid") << QVariant(QMetaType::ULong, (void *) 0) << false;
1528 ulong ulongInt = 1ul;
1529 QTest::newRow("QMetaType::ULong") << QVariant(QMetaType::ULong, &ulongInt) << false;
1530 QTest::newRow("QMetaType::UShort invalid") << QVariant(QMetaType::UShort, (void *) 0) << false;
1531 ushort ushortInt = 1u;
1532 QTest::newRow("QMetaType::UShort") << QVariant(QMetaType::UShort, &ushortInt) << false;
1533 QTest::newRow("QMetaType::UChar invalid") << QVariant(QMetaType::UChar, (void *) 0) << false;
1535 QTest::newRow("QMetaType::UChar") << QVariant(QMetaType::UChar, &uch) << false;
1536 QTest::newRow("QMetaType::Float invalid") << QVariant(QMetaType::Float, (void *) 0) << false;
1538 QTest::newRow("QMetaType::Float") << QVariant(QMetaType::Float, &f) << false;
1539 CustomStreamableClass custom = {123};
1540 QTest::newRow("Custom type") << QVariant::fromValue(custom) << false;
1543 void tst_QVariant::writeToReadFromDataStream()
1545 QFETCH( QVariant, writeVariant );
1546 QFETCH( bool, isNull );
1549 QDataStream writeStream( &data, QIODevice::WriteOnly );
1550 writeStream << writeVariant;
1552 QVariant readVariant;
1553 QDataStream readStream( &data, QIODevice::ReadOnly );
1554 readStream >> readVariant;
1555 QVERIFY( readVariant.isNull() == isNull );
1556 // Best way to confirm the readVariant contains the same data?
1557 // Since only a few won't match since the serial numbers are different
1558 // I won't bother adding another bool in the data test.
1559 const int writeType = writeVariant.userType();
1560 if (writeType == qMetaTypeId<CustomStreamableClass>())
1561 QCOMPARE(qvariant_cast<CustomStreamableClass>(readVariant), qvariant_cast<CustomStreamableClass>(writeVariant));
1562 else if ( writeType != QVariant::Invalid && writeType != QVariant::Bitmap && writeType != QVariant::Pixmap
1563 && writeType != QVariant::Image) {
1564 switch (writeType) {
1566 QCOMPARE( readVariant, writeVariant );
1569 // compare types know by QMetaType but not QVariant (QVariant::operator==() knows nothing about them)
1570 case QMetaType::Long:
1571 QCOMPARE(qvariant_cast<long>(readVariant), qvariant_cast<long>(writeVariant));
1573 case QMetaType::ULong:
1574 QCOMPARE(qvariant_cast<ulong>(readVariant), qvariant_cast<ulong>(writeVariant));
1576 case QMetaType::Short:
1577 QCOMPARE(qvariant_cast<short>(readVariant), qvariant_cast<short>(writeVariant));
1579 case QMetaType::UShort:
1580 QCOMPARE(qvariant_cast<ushort>(readVariant), qvariant_cast<ushort>(writeVariant));
1582 case QMetaType::Char:
1583 QCOMPARE(qvariant_cast<char>(readVariant), qvariant_cast<char>(writeVariant));
1585 case QMetaType::UChar:
1586 QCOMPARE(qvariant_cast<uchar>(readVariant), qvariant_cast<uchar>(writeVariant));
1588 case QMetaType::Float:
1590 // the uninitialized float can be NaN (observed on Windows Mobile 5 ARMv4i)
1591 float readFloat = qvariant_cast<float>(readVariant);
1592 float writtenFloat = qvariant_cast<float>(writeVariant);
1593 QVERIFY(qIsNaN(readFloat) == qIsNaN(writtenFloat));
1594 if (!qIsNaN(readFloat))
1595 QVERIFY(readFloat == writtenFloat);
1602 void tst_QVariant::writeToReadFromOldDataStream()
1604 QVariant writeVariant = QString("hello");
1607 QDataStream writeStream(&data, QIODevice::WriteOnly);
1608 writeStream.setVersion(QDataStream::Qt_2_1);
1609 writeStream << writeVariant;
1611 QVariant readVariant;
1612 QDataStream readStream(&data, QIODevice::ReadOnly);
1613 readStream.setVersion(QDataStream::Qt_2_1);
1614 readStream >> readVariant;
1616 QCOMPARE(writeVariant.userType(), readVariant.userType());
1617 QCOMPARE(writeVariant, readVariant);
1620 void tst_QVariant::checkDataStream()
1622 QTest::ignoreMessage(QtWarningMsg, "Trying to construct an instance of an invalid type, type id: 46");
1623 const QByteArray settingsHex("0000002effffffffff");
1624 const QByteArray settings = QByteArray::fromHex(settingsHex);
1625 QDataStream in(settings);
1628 // the line below has been left out for now since the data stream
1629 // is not necessarily considered corrupt when an invalid QVariant is
1630 // constructed. However, it might be worth considering changing that behavior
1632 // QCOMPARE(in.status(), QDataStream::ReadCorruptData);
1633 QCOMPARE(v.type(), QVariant::Invalid);
1636 void tst_QVariant::operator_eq_eq_data()
1638 QTest::addColumn<QVariant>("left");
1639 QTest::addColumn<QVariant>("right");
1640 QTest::addColumn<bool>("equal"); // left == right ?
1643 QVariant i0( int(0) );
1644 QVariant i1( int(1) );
1646 QTest::newRow( "invinv" ) << inv << inv << true;
1648 QTest::newRow( "int1int1" ) << i1 << i1 << true;
1649 QTest::newRow( "int1int0" ) << i1 << i0 << false;
1650 QTest::newRow( "nullint" ) << i0 << QVariant(QVariant::Int) << false;
1652 // LongLong and ULongLong
1653 QVariant ll1( (qlonglong)1 );
1654 QVariant lln2( (qlonglong)-2 );
1655 QVariant ull1( (qulonglong)1 );
1656 QVariant ull3( (qulonglong)3 );
1657 QTest::newRow( "ll1ll1" ) << ll1 << ll1 << true;
1658 QTest::newRow( "ll1lln2" ) << ll1 << lln2 << false;
1659 QTest::newRow( "ll1ull1" ) << ull1 << ull1 << true;
1660 QTest::newRow( "ll1i1" ) << ull1 << i1 << true;
1661 QTest::newRow( "ull1ull1" ) << ull1 << ull1 << true;
1662 QTest::newRow( "ull1i1" ) << ull1 << ull1 << true;
1665 QVariant mIntString(QByteArray("-42"));
1666 QVariant mIntQString(QString("-42"));
1668 QVariant mUInt(42u);
1669 QVariant mUIntString(QByteArray("42"));
1670 QVariant mUIntQString(QString("42"));
1672 QVariant mDouble(42.11);
1673 QVariant mDoubleString(QByteArray("42.11"));
1674 QVariant mDoubleQString(QString("42.11"));
1676 QVariant mFloat(42.11f);
1677 QVariant mFloatString(QByteArray("42.11"));
1678 QVariant mFloatQString(QString("42.11"));
1680 QVariant mLongLong((qlonglong)-42);
1681 QVariant mLongLongString(QByteArray("-42"));
1682 QVariant mLongLongQString(QString("-42"));
1684 QVariant mULongLong((qulonglong)42);
1685 QVariant mULongLongString(QByteArray("42"));
1686 QVariant mULongLongQString(QString("42"));
1688 QVariant mBool(false);
1689 QVariant mBoolString(QByteArray("false"));
1690 QVariant mBoolQString(QString("false"));
1692 QTest::newRow( "double_int" ) << QVariant(42.0) << QVariant(42) << true;
1693 QTest::newRow( "float_int" ) << QVariant(42.f) << QVariant(42) << true;
1694 QTest::newRow( "mInt_mIntString" ) << mInt << mIntString << true;
1695 QTest::newRow( "mIntString_mInt" ) << mIntString << mInt << true;
1696 QTest::newRow( "mInt_mIntQString" ) << mInt << mIntQString << true;
1697 QTest::newRow( "mIntQString_mInt" ) << mIntQString << mInt << true;
1699 QTest::newRow( "mUInt_mUIntString" ) << mUInt << mUIntString << true;
1700 QTest::newRow( "mUIntString_mUInt" ) << mUIntString << mUInt << true;
1701 QTest::newRow( "mUInt_mUIntQString" ) << mUInt << mUIntQString << true;
1702 QTest::newRow( "mUIntQString_mUInt" ) << mUIntQString << mUInt << true;
1704 QTest::newRow( "mDouble_mDoubleString" ) << mDouble << mDoubleString << true;
1705 QTest::newRow( "mDoubleString_mDouble" ) << mDoubleString << mDouble << true;
1706 QTest::newRow( "mDouble_mDoubleQString" ) << mDouble << mDoubleQString << true;
1707 QTest::newRow( "mDoubleQString_mDouble" ) << mDoubleQString << mDouble << true;
1709 QTest::newRow( "mFloat_mFloatString" ) << mFloat << mFloatString << true;
1710 QTest::newRow( "mFloatString_mFloat" ) << mFloatString << mFloat << true;
1711 QTest::newRow( "mFloat_mFloatQString" ) << mFloat << mFloatQString << true;
1712 QTest::newRow( "mFloatQString_mFloat" ) << mFloatQString << mFloat << true;
1714 QTest::newRow( "mLongLong_mLongLongString" ) << mLongLong << mLongLongString << true;
1715 QTest::newRow( "mLongLongString_mLongLong" ) << mLongLongString << mLongLong << true;
1716 QTest::newRow( "mLongLong_mLongLongQString" ) << mLongLong << mLongLongQString << true;
1717 QTest::newRow( "mLongLongQString_mLongLong" ) << mLongLongQString << mLongLong << true;
1719 QTest::newRow( "mULongLong_mULongLongString" ) << mULongLong << mULongLongString << true;
1720 QTest::newRow( "mULongLongString_mULongLong" ) << mULongLongString << mULongLong << true;
1721 QTest::newRow( "mULongLong_mULongLongQString" ) << mULongLong << mULongLongQString << true;
1722 QTest::newRow( "mULongLongQString_mULongLong" ) << mULongLongQString << mULongLong << true;
1724 QTest::newRow( "mBool_mBoolString" ) << mBool << mBoolString << true;
1725 QTest::newRow( "mBoolString_mBool" ) << mBoolString << mBool << true;
1726 QTest::newRow( "mBool_mBoolQString" ) << mBool << mBoolQString << true;
1727 QTest::newRow( "mBoolQString_mBool" ) << mBoolQString << mBool << true;
1729 QTest::newRow("ba2qstring") << QVariant(QLatin1String("hallo")) << QVariant(QString("hallo")) << true;
1730 QTest::newRow("qstring2ba") << QVariant(QString("hallo")) << QVariant(QLatin1String("hallo")) << true;
1731 QTest::newRow("char_char") << QVariant(QChar('a')) << QVariant(QChar('a')) << true;
1732 QTest::newRow("char_char2") << QVariant(QChar('a')) << QVariant(QChar('b')) << false;
1734 QTest::newRow("invalidConversion") << QVariant(QString("bubu")) << QVariant(0) << false;
1735 QTest::newRow("invalidConversionR") << QVariant(0) << QVariant(QString("bubu")) << false;
1736 // ### many other combinations missing
1739 QMap<QString, QVariant> map1;
1740 map1.insert( "X", 1 );
1742 QMap<QString, QVariant> map2;
1743 map2.insert( "Y", 1 );
1745 QTest::newRow("TwoItemsInEqual") << QVariant(map1) << QVariant(map2) << false;
1750 QMap<QString, QVariant> map1;
1751 map1.insert( "X", 1 );
1753 QMap<QString, QVariant> map2;
1754 map2.insert( "X", 1 );
1756 QTest::newRow("TwoItemsEqual") << QVariant(map1) << QVariant(map2) << true;
1760 QMap<QString, QVariant> map1;
1761 map1.insert( "X", 1 );
1763 QMap<QString, QVariant> map2;
1765 QTest::newRow("PopulatedEmptyMap") << QVariant(map1) << QVariant(map2) << false;
1769 QMap<QString, QVariant> map1;
1771 QMap<QString, QVariant> map2;
1772 map2.insert( "X", 1 );
1774 QTest::newRow("EmptyPopulatedMap") << QVariant(map1) << QVariant(map2) << false;
1778 QMap<QString, QVariant> map1;
1779 map1.insert( "X", 1 );
1780 map1.insert( "Y", 1 );
1782 QMap<QString, QVariant> map2;
1783 map2.insert( "X", 1 );
1785 QTest::newRow("FirstLarger") << QVariant(map1) << QVariant(map2) << false;
1789 QMap<QString, QVariant> map1;
1790 map1.insert( "X", 1 );
1792 QMap<QString, QVariant> map2;
1793 map2.insert( "X", 1 );
1794 map2.insert( "Y", 1 );
1796 QTest::newRow("SecondLarger") << QVariant(map1) << QVariant(map2) << false;
1799 // same thing with hash
1801 QHash<QString, QVariant> hash1;
1802 hash1.insert( "X", 1 );
1804 QHash<QString, QVariant> hash2;
1805 hash2.insert( "Y", 1 );
1807 QTest::newRow("HashTwoItemsInEqual") << QVariant(hash1) << QVariant(hash2) << false;
1812 QHash<QString, QVariant> hash1;
1813 hash1.insert( "X", 1 );
1815 QHash<QString, QVariant> hash2;
1816 hash2.insert( "X", 1 );
1818 QTest::newRow("HashTwoItemsEqual") << QVariant(hash1) << QVariant(hash2) << true;
1822 QHash<QString, QVariant> hash1;
1823 hash1.insert( "X", 1 );
1825 QHash<QString, QVariant> hash2;
1827 QTest::newRow("HashPopulatedEmptyHash") << QVariant(hash1) << QVariant(hash2) << false;
1831 QHash<QString, QVariant> hash1;
1833 QHash<QString, QVariant> hash2;
1834 hash2.insert( "X", 1 );
1836 QTest::newRow("EmptyPopulatedHash") << QVariant(hash1) << QVariant(hash2) << false;
1840 QHash<QString, QVariant> hash1;
1841 hash1.insert( "X", 1 );
1842 hash1.insert( "Y", 1 );
1844 QHash<QString, QVariant> hash2;
1845 hash2.insert( "X", 1 );
1847 QTest::newRow("HashFirstLarger") << QVariant(hash1) << QVariant(hash2) << false;
1851 QHash<QString, QVariant> hash1;
1852 hash1.insert( "X", 1 );
1854 QHash<QString, QVariant> hash2;
1855 hash2.insert( "X", 1 );
1856 hash2.insert( "Y", 1 );
1858 QTest::newRow("HashSecondLarger") << QVariant(hash1) << QVariant(hash2) << false;
1862 void tst_QVariant::operator_eq_eq()
1864 QFETCH( QVariant, left );
1865 QFETCH( QVariant, right );
1866 QFETCH( bool, equal );
1867 QEXPECT_FAIL("nullint", "See QTBUG-22933", Continue);
1868 QCOMPARE( left == right, equal );
1871 void tst_QVariant::operator_eq_eq_rhs()
1879 /* This should _not_ compile */
1883 QVERIFY(dt == list);
1887 void tst_QVariant::typeName_data()
1889 QTest::addColumn<int>("type");
1890 QTest::addColumn<QByteArray>("res");
1891 QTest::newRow("0") << int(QVariant::Invalid) << QByteArray("");
1892 QTest::newRow("1") << int(QVariant::Map) << QByteArray("QVariantMap");
1893 QTest::newRow("2") << int(QVariant::List) << QByteArray("QVariantList");
1894 QTest::newRow("3") << int(QVariant::String) << QByteArray("QString");
1895 QTest::newRow("4") << int(QVariant::StringList) << QByteArray("QStringList");
1896 QTest::newRow("5") << int(QVariant::Font) << QByteArray("QFont");
1897 QTest::newRow("6") << int(QVariant::Pixmap) << QByteArray("QPixmap");
1898 QTest::newRow("7") << int(QVariant::Brush) << QByteArray("QBrush");
1899 QTest::newRow("8") << int(QVariant::Rect) << QByteArray("QRect");
1900 QTest::newRow("9") << int(QVariant::Size) << QByteArray("QSize");
1901 QTest::newRow("10") << int(QVariant::Color) << QByteArray("QColor");
1902 QTest::newRow("11") << int(QVariant::Palette) << QByteArray("QPalette");
1903 QTest::newRow("12") << int(QVariant::Point) << QByteArray("QPoint");
1904 QTest::newRow("13") << int(QVariant::Image) << QByteArray("QImage");
1905 QTest::newRow("14") << int(QVariant::Int) << QByteArray("int");
1906 QTest::newRow("15") << int(QVariant::UInt) << QByteArray("uint");
1907 QTest::newRow("16") << int(QVariant::Bool) << QByteArray("bool");
1908 QTest::newRow("17") << int(QVariant::Double) << QByteArray("double");
1909 QTest::newRow("18") << int(QMetaType::Float) << QByteArray("float");
1910 QTest::newRow("19") << int(QVariant::Polygon) << QByteArray("QPolygon");
1911 QTest::newRow("20") << int(QVariant::Region) << QByteArray("QRegion");
1912 QTest::newRow("21") << int(QVariant::Bitmap) << QByteArray("QBitmap");
1913 QTest::newRow("22") << int(QVariant::Cursor) << QByteArray("QCursor");
1914 QTest::newRow("23") << int(QVariant::SizePolicy) << QByteArray("QSizePolicy");
1915 QTest::newRow("24") << int(QVariant::Date) << QByteArray("QDate");
1916 QTest::newRow("25") << int(QVariant::Time) << QByteArray("QTime");
1917 QTest::newRow("26") << int(QVariant::DateTime) << QByteArray("QDateTime");
1918 QTest::newRow("27") << int(QVariant::ByteArray) << QByteArray("QByteArray");
1919 QTest::newRow("28") << int(QVariant::BitArray) << QByteArray("QBitArray");
1920 QTest::newRow("29") << int(QVariant::KeySequence) << QByteArray("QKeySequence");
1921 QTest::newRow("30") << int(QVariant::Pen) << QByteArray("QPen");
1922 QTest::newRow("31") << int(QVariant::LongLong) << QByteArray("qlonglong");
1923 QTest::newRow("32") << int(QVariant::ULongLong) << QByteArray("qulonglong");
1924 QTest::newRow("33") << int(QVariant::Char) << QByteArray("QChar");
1925 QTest::newRow("34") << int(QVariant::Url) << QByteArray("QUrl");
1926 QTest::newRow("35") << int(QVariant::TextLength) << QByteArray("QTextLength");
1927 QTest::newRow("36") << int(QVariant::TextFormat) << QByteArray("QTextFormat");
1928 QTest::newRow("37") << int(QVariant::Locale) << QByteArray("QLocale");
1929 QTest::newRow("38") << int(QVariant::LineF) << QByteArray("QLineF");
1930 QTest::newRow("39") << int(QVariant::RectF) << QByteArray("QRectF");
1931 QTest::newRow("40") << int(QVariant::PointF) << QByteArray("QPointF");
1932 QTest::newRow("41") << int(QVariant::RegExp) << QByteArray("QRegExp");
1933 QTest::newRow("43") << int(QVariant::Matrix) << QByteArray("QMatrix");
1934 QTest::newRow("44") << int(QVariant::Transform) << QByteArray("QTransform");
1935 QTest::newRow("45") << int(QVariant::Hash) << QByteArray("QVariantHash");
1936 QTest::newRow("46") << int(QVariant::Matrix4x4) << QByteArray("QMatrix4x4");
1937 QTest::newRow("47") << int(QVariant::Vector2D) << QByteArray("QVector2D");
1938 QTest::newRow("48") << int(QVariant::Vector3D) << QByteArray("QVector3D");
1939 QTest::newRow("49") << int(QVariant::Vector4D) << QByteArray("QVector4D");
1940 QTest::newRow("50") << int(QVariant::Quaternion) << QByteArray("QQuaternion");
1943 void tst_QVariant::typeName()
1945 QFETCH( int, type );
1946 QFETCH( QByteArray, res );
1947 QCOMPARE(QString::fromLatin1(QVariant::typeToName((QVariant::Type)type)),
1948 QString::fromLatin1(res.constData()));
1951 // test nameToType as well
1952 void tst_QVariant::typeToName()
1955 QCOMPARE( QVariant::typeToName( v.type() ), (const char*)0 ); // Invalid
1956 // assumes that QVariant::Type contains consecutive values
1958 int max = QVariant::LastGuiType;
1959 for ( int t = 1; t <= max; t++ ) {
1960 const char *n = QVariant::typeToName( (QVariant::Type)t );
1962 QCOMPARE( int(QVariant::nameToType( n )), t );
1965 QCOMPARE(QVariant::typeToName(QVariant::Int), "int");
1966 // not documented but we return 0 if the type is out of range
1967 // by testing this we catch cases where QVariant is extended
1968 // but type_map is not updated accordingly
1969 QCOMPARE( QVariant::typeToName( QVariant::Type(max+1) ), (char*)0 );
1970 // invalid type names
1971 QVERIFY( QVariant::nameToType( 0 ) == QVariant::Invalid );
1972 QVERIFY( QVariant::nameToType( "" ) == QVariant::Invalid );
1973 QVERIFY( QVariant::nameToType( "foo" ) == QVariant::Invalid );
1975 QCOMPARE(QVariant::nameToType("UserType"), QVariant::Invalid);
1977 // We don't support these old (Qt3) types anymore.
1978 QCOMPARE(QVariant::nameToType("QIconSet"), QVariant::Invalid);
1979 QCOMPARE(QVariant::nameToType("Q3CString"), QVariant::Invalid);
1980 QCOMPARE(QVariant::nameToType("Q_LLONG"), QVariant::Invalid);
1981 QCOMPARE(QVariant::nameToType("Q_ULLONG"), QVariant::Invalid);
1984 void tst_QVariant::streamInvalidVariant()
1990 QVariant writeVariant;
1991 QVariant readVariant;
1993 QVERIFY( writeVariant.type() == QVariant::Invalid );
1996 QDataStream writeStream( &data, QIODevice::WriteOnly );
1997 writeStream << writeX << writeVariant << writeY;
1999 QDataStream readStream( &data, QIODevice::ReadOnly );
2000 readStream >> readX >> readVariant >> readY;
2002 QVERIFY( readX == writeX );
2003 // Two invalid QVariant's aren't necessarily the same, so == will
2004 // return false if one is invalid, so check the type() instead
2005 QVERIFY( readVariant.type() == QVariant::Invalid );
2006 QVERIFY( readY == writeY );
2009 static int instanceCount = 0;
2013 MyType(int n = 0, const char *t=0): number(n), text(t)
2017 MyType(const MyType &other)
2018 : number(other.number), text(other.text)
2030 Q_DECLARE_METATYPE(MyType)
2031 Q_DECLARE_METATYPE(MyType*)
2033 void tst_QVariant::userType()
2036 MyType data(1, "eins");
2037 MyType data2(2, "zwei");
2041 qVariantSetValue(userVar, data);
2043 QCOMPARE(userVar.type(), QVariant::UserType);
2044 QCOMPARE(userVar.userType(), qMetaTypeId<MyType>());
2045 QCOMPARE(userVar.typeName(), "MyType");
2046 QVERIFY(!userVar.isNull());
2047 QVERIFY(!userVar.canConvert(QVariant::String));
2049 QVariant userVar2(userVar);
2050 QVERIFY(userVar == userVar2);
2052 qVariantSetValue(userVar2, data2);
2053 QVERIFY(userVar != userVar2);
2055 const MyType *varData = static_cast<const MyType *>(userVar.constData());
2057 QCOMPARE(varData->number, data.number);
2058 QCOMPARE(varData->text, data.text);
2061 qVariantSetValue(userVar3, data2);
2063 userVar3 = userVar2;
2064 QVERIFY(userVar2 == userVar3);
2066 // At this point all QVariants got destroyed but we have 2 MyType instances.
2067 QCOMPARE(instanceCount, 2);
2070 qVariantSetValue(userVar, &data);
2072 QCOMPARE(userVar.type(), QVariant::UserType);
2073 QCOMPARE(userVar.userType(), qMetaTypeId<MyType*>());
2074 QCOMPARE(userVar.typeName(), "MyType*");
2075 QVERIFY(!userVar.isNull());
2076 QVERIFY(!userVar.canConvert(QVariant::String));
2078 QVariant userVar2(userVar);
2079 QVERIFY(userVar == userVar2);
2081 qVariantSetValue(userVar2, &data2);
2082 QVERIFY(userVar != userVar2);
2084 MyType * const*varData = reinterpret_cast<MyType *const *>(userVar.constData());
2086 QCOMPARE(*varData, &data);
2089 qVariantSetValue(userVar3, &data2);
2091 /* This check is correct now. userVar2 contains a pointer to data2 and so
2093 QVERIFY(userVar2 == userVar3);
2095 userVar3 = userVar2;
2096 QVERIFY(userVar2 == userVar3);
2099 QCOMPARE(instanceCount, 2);
2101 qVariantSetValue(myCarrier, data);
2102 QCOMPARE(instanceCount, 3);
2104 QVariant second = myCarrier;
2105 QCOMPARE(instanceCount, 3);
2107 QCOMPARE(instanceCount, 4);
2109 QCOMPARE(instanceCount, 3);
2111 MyType data3(0, "null");
2112 data3 = qVariantValue<MyType>(myCarrier);
2113 QCOMPARE(data3.number, 1);
2114 QCOMPARE(data3.text, (const char *)"eins");
2116 QCOMPARE(instanceCount, 4);
2122 const MyType data(3, "drei");
2125 qVariantSetValue(myCarrier, data);
2126 QCOMPARE(myCarrier.typeName(), "MyType");
2128 const MyType data2 = qvariant_cast<MyType>(myCarrier);
2129 QCOMPARE(data2.number, 3);
2130 QCOMPARE(data2.text, (const char *)"drei");
2137 qVariantSetValue(myCarrier, s);
2138 QCOMPARE((int)qvariant_cast<short>(myCarrier), 42);
2142 qlonglong ll = Q_INT64_C(42);
2145 qVariantSetValue(myCarrier, ll);
2146 QCOMPARE(qvariant_cast<int>(myCarrier), 42);
2149 // At this point all QVariants got destroyed and MyType objects too.
2150 QCOMPARE(instanceCount, 0);
2158 Q_DECLARE_METATYPE(MyTypePOD)
2160 void tst_QVariant::podUserType()
2166 QVariant pod_as_variant = qVariantFromValue(pod);
2167 MyTypePOD pod2 = qvariant_cast<MyTypePOD>(pod_as_variant);
2169 QCOMPARE(pod.a, pod2.a);
2170 QCOMPARE(pod.b, pod2.b);
2172 qVariantSetValue(pod_as_variant, pod);
2173 pod2 = qVariantValue<MyTypePOD>(pod_as_variant);
2175 QCOMPARE(pod.a, pod2.a);
2176 QCOMPARE(pod.b, pod2.b);
2179 void tst_QVariant::basicUserType()
2184 v = QVariant(QMetaType::Int, &i);
2186 QCOMPARE(v.type(), QVariant::Int);
2187 QCOMPARE(v.toInt(), 7);
2191 v = QVariant(QMetaType::QString, &s);
2193 QCOMPARE(v.type(), QVariant::String);
2194 QCOMPARE(v.toString(), QString("foo"));
2198 v = QVariant(QMetaType::Double, &d);
2200 QCOMPARE(v.type(), QVariant::Double);
2201 QCOMPARE(v.toDouble(), 4.4);
2205 v = QVariant(QMetaType::Float, &f);
2207 QCOMPARE(v.userType(), int(QMetaType::Float));
2208 QCOMPARE(v.toDouble(), 4.5);
2211 QByteArray ba("bar");
2212 v = QVariant(QMetaType::QByteArray, &ba);
2214 QCOMPARE(v.type(), QVariant::ByteArray);
2215 QCOMPARE(v.toByteArray(), QByteArray("bar"));
2218 void tst_QVariant::data_()
2225 QVariant ll = (qlonglong)2;
2226 QVariant ull = (qulonglong)3;
2227 QVariant s(QString("hallo"));
2228 QVariant r(QRect(1,2,3,4));
2232 QCOMPARE(*static_cast<int *>(v.data()), i.toInt());
2236 QCOMPARE(*static_cast<double *>(v.data()), d.toDouble());
2240 QCOMPARE(*static_cast<float *>(v.data()), qVariantValue<float>(v));
2244 QCOMPARE(*static_cast<qlonglong *>(v.data()), ll.toLongLong());
2248 QCOMPARE(*static_cast<qulonglong *>(v.data()), ull.toULongLong());
2252 QCOMPARE(*static_cast<QString *>(v.data()), s.toString());
2256 QCOMPARE(*static_cast<QRect *>(v.data()), r.toRect());
2259 void tst_QVariant::constData()
2272 QVERIFY(v.constData());
2273 QCOMPARE(*static_cast<const int *>(v.constData()), i);
2276 QVERIFY(v.constData());
2277 QCOMPARE(*static_cast<const double *>(v.constData()), d);
2280 QVERIFY(v.constData());
2281 QCOMPARE(*static_cast<const float *>(v.constData()), f);
2284 QVERIFY(v.constData());
2285 QCOMPARE(*static_cast<const qlonglong *>(v.constData()), ll);
2288 QVERIFY(v.constData());
2289 QCOMPARE(*static_cast<const qulonglong *>(v.constData()), ull);
2292 QVERIFY(v.constData());
2293 QCOMPARE(*static_cast<const QString *>(v.constData()), s);
2296 QVERIFY(v.constData());
2297 QCOMPARE(*static_cast<const QRect *>(v.constData()), r);
2306 Q_DECLARE_METATYPE(Foo)
2308 void tst_QVariant::variant_to()
2317 sl << QLatin1String("blah");
2319 qVariantSetValue(v3, sl);
2324 qVariantSetValue(v4, foo);
2326 QCOMPARE(qvariant_cast<double>(v1), 4.2);
2327 QCOMPARE(qvariant_cast<float>(v1), 4.2f);
2328 QCOMPARE(qvariant_cast<int>(v2), 5);
2329 QCOMPARE(qvariant_cast<QStringList>(v3), sl);
2330 QCOMPARE(qvariant_cast<QString>(v3), QString::fromLatin1("blah"));
2332 QCOMPARE(qvariant_cast<Foo>(v4).i, 42);
2335 QCOMPARE(qvariant_cast<Foo>(v5).i, 0);
2337 QCOMPARE(qvariant_cast<int>(v1), 4);
2339 QVariant n = qVariantFromValue<short>(42);
2340 QCOMPARE(qvariant_cast<int>(n), 42);
2341 QCOMPARE(qvariant_cast<uint>(n), 42u);
2342 QCOMPARE(qvariant_cast<double>(n), 42.0);
2343 QCOMPARE(qvariant_cast<float>(n), 42.f);
2344 QCOMPARE(qvariant_cast<short>(n), short(42));
2345 QCOMPARE(qvariant_cast<ushort>(n), ushort(42));
2347 n = qVariantFromValue(43l);
2348 QCOMPARE(qvariant_cast<int>(n), 43);
2349 QCOMPARE(qvariant_cast<uint>(n), 43u);
2350 QCOMPARE(qvariant_cast<double>(n), 43.0);
2351 QCOMPARE(qvariant_cast<float>(n), 43.f);
2352 QCOMPARE(qvariant_cast<long>(n), 43l);
2354 n = QLatin1String("44");
2355 QCOMPARE(qvariant_cast<int>(n), 44);
2356 QCOMPARE(qvariant_cast<ulong>(n), 44ul);
2357 QCOMPARE(qvariant_cast<float>(n), 44.0f);
2359 QCOMPARE(qVariantFromValue(0.25f).toDouble(), 0.25);
2362 struct Blah { int i; };
2364 QDataStream& operator>>(QDataStream& s, Blah& c)
2365 { return (s >> c.i); }
2367 QDataStream& operator<<(QDataStream& s, const Blah& c)
2368 { return (s << c.i); }
2370 void tst_QVariant::saveLoadCustomTypes()
2375 int tp = qRegisterMetaType<Blah>("Blah");
2376 QVariant v = QVariant(tp, &i);
2378 qRegisterMetaTypeStreamOperators<Blah>("Blah");
2380 QCOMPARE(v.userType(), tp);
2381 QVERIFY(v.type() == QVariant::UserType);
2383 QDataStream stream(&data, QIODevice::WriteOnly);
2390 QDataStream stream(data);
2394 QCOMPARE(int(v.userType()), QMetaType::type("Blah"));
2395 int value = *(int*)v.constData();
2396 QCOMPARE(value, 42);
2399 void tst_QVariant::url()
2401 QString str("http://qt.nokia.com");
2404 QVariant v(url); //built with a QUrl
2408 QVariant v3(str); //built with a QString
2410 QCOMPARE(v2.toUrl(), url);
2411 QVERIFY(qVariantCanConvert<QUrl>(v3));
2412 QCOMPARE(v2.toUrl(), v3.toUrl());
2414 QVERIFY(qVariantCanConvert<QString>(v2));
2415 QCOMPARE(v2.toString(), str);
2416 QCOMPARE(v3.toString(), str);
2419 void tst_QVariant::globalColor()
2421 QVariant variant(Qt::blue);
2422 QVERIFY(variant.type() == QVariant::Color);
2423 QVERIFY(qVariantValue<QColor>(variant) == QColor(Qt::blue));
2426 void tst_QVariant::variantMap()
2428 QMap<QString, QVariant> map;
2432 QVariantMap map2 = qvariant_cast<QVariantMap>(v);
2434 QCOMPARE(map2.value("test").toInt(), 42);
2436 QVariant v2 = QVariant(QMetaType::type("QVariantMap"), &map);
2437 QCOMPARE(qvariant_cast<QVariantMap>(v2).value("test").toInt(), 42);
2439 QVariant v3 = QVariant(QMetaType::type("QMap<QString, QVariant>"), &map);
2440 QCOMPARE(qvariant_cast<QVariantMap>(v3).value("test").toInt(), 42);
2443 void tst_QVariant::variantHash()
2445 QHash<QString, QVariant> hash;
2449 QVariantHash hash2 = qvariant_cast<QVariantHash>(v);
2451 QCOMPARE(hash2.value("test").toInt(), 42);
2453 QVariant v2 = QVariant(QMetaType::type("QVariantHash"), &hash);
2454 QCOMPARE(qvariant_cast<QVariantHash>(v2).value("test").toInt(), 42);
2456 QVariant v3 = QVariant(QMetaType::type("QHash<QString, QVariant>"), &hash);
2457 QCOMPARE(qvariant_cast<QVariantHash>(v3).value("test").toInt(), 42);
2460 void tst_QVariant::invalidQColor() const
2462 QVariant va("An invalid QColor::name() value.");
2463 QVERIFY(va.canConvert(QVariant::Color));
2465 QVERIFY(!va.convert(QVariant::Color));
2467 QVERIFY(!qvariant_cast<QColor>(va).isValid());
2470 class CustomQObject : public QObject {
2473 CustomQObject(QObject *parent = 0) : QObject(parent) {}
2475 class CustomQWidget : public QWidget {
2478 CustomQWidget(QWidget *parent = 0) : QWidget(parent) {}
2480 Q_DECLARE_METATYPE(CustomQObject*)
2481 Q_DECLARE_METATYPE(CustomQWidget*)
2483 class CustomNonQObject { };
2484 Q_DECLARE_METATYPE(CustomNonQObject)
2485 Q_DECLARE_METATYPE(CustomNonQObject*)
2487 void tst_QVariant::cleanupTestCase()
2489 delete customNonQObjectPointer;
2490 qDeleteAll(objectPointerTestData);
2493 void tst_QVariant::qvariant_cast_QObject_data()
2495 QTest::addColumn<QVariant>("data");
2496 QTest::addColumn<bool>("success");
2497 QObject *obj = new QObject;
2498 obj->setObjectName(QString::fromLatin1("Hello"));
2499 QTest::newRow("from QObject") << QVariant(QMetaType::QObjectStar, &obj) << true;
2500 QTest::newRow("from QObject2") << QVariant::fromValue(obj) << true;
2501 QTest::newRow("from String") << QVariant(QLatin1String("1, 2, 3")) << false;
2502 QTest::newRow("from int") << QVariant((int) 123) << false;
2503 QWidget *widget = new QWidget;
2504 widget->setObjectName(QString::fromLatin1("Hello"));
2505 QTest::newRow("from QWidget") << QVariant::fromValue(widget) << true;
2506 CustomQObject *customObject = new CustomQObject(this);
2507 customObject->setObjectName(QString::fromLatin1("Hello"));
2508 QTest::newRow("from Derived QObject") << QVariant::fromValue(customObject) << true;
2509 CustomQWidget *customWidget = new CustomQWidget;
2510 customWidget->setObjectName(QString::fromLatin1("Hello"));
2511 QTest::newRow("from Derived QWidget") << QVariant::fromValue(customWidget) << true;
2512 QTest::newRow("from custom Object") << QVariant::fromValue(CustomNonQObject()) << false;
2514 // Deleted in cleanupTestCase.
2515 customNonQObjectPointer = new CustomNonQObject;
2516 QTest::newRow("from custom ObjectStar") << QVariant::fromValue(customNonQObjectPointer) << false;
2518 // Deleted in cleanupTestCase.
2519 objectPointerTestData.push_back(obj);
2520 objectPointerTestData.push_back(widget);
2521 objectPointerTestData.push_back(customObject);
2522 objectPointerTestData.push_back(customWidget);
2525 void tst_QVariant::qvariant_cast_QObject()
2527 QFETCH(QVariant, data);
2528 QFETCH(bool, success);
2530 QObject *o = qvariant_cast<QObject *>(data);
2531 QCOMPARE(o != 0, success);
2533 QCOMPARE(o->objectName(), QString::fromLatin1("Hello"));
2537 class CustomQObjectDerived : public CustomQObject {
2540 CustomQObjectDerived(QObject *parent = 0) : CustomQObject(parent) {}
2542 Q_DECLARE_METATYPE(CustomQObjectDerived*)
2544 void tst_QVariant::qvariant_cast_QObject_derived()
2547 CustomQObjectDerived *object = new CustomQObjectDerived(this);
2548 QVariant data = QVariant::fromValue(object);
2550 QVERIFY(data.userType() == qMetaTypeId<CustomQObjectDerived*>());
2552 QCOMPARE(data.value<QObject *>(), object);
2553 QCOMPARE(data.value<CustomQObjectDerived *>(), object);
2554 QCOMPARE(data.value<CustomQObject *>(), object);
2555 QVERIFY(data.value<CustomQWidget*>() == 0);
2558 CustomQWidget customWidget;
2559 QWidget *widget = &customWidget;
2560 QVariant data = QVariant::fromValue(widget);
2561 QVERIFY(data.userType() == QMetaType::QWidgetStar);
2563 QCOMPARE(data.value<QObject*>(), widget);
2564 QCOMPARE(data.value<QWidget*>(), widget);
2565 QCOMPARE(data.value<CustomQWidget*>(), widget);
2569 Q_DECLARE_METATYPE(qint8);
2571 void tst_QVariant::convertToQUint8() const
2575 const qint8 anInt = 32;
2577 /* QVariant(int) gets invoked here so the QVariant has nothing with qint8 to do.
2578 * It's of type QVariant::Int. */
2579 const QVariant v0 = anInt;
2581 QVERIFY(qVariantCanConvert<qint8>(v0));
2582 QCOMPARE(int(qvariant_cast<qint8>(v0)), 32);
2583 QCOMPARE(int(v0.toInt()), 32);
2584 QCOMPARE(v0.toString(), QString("32"));
2586 QCOMPARE(int(qvariant_cast<qlonglong>(v0)), 32);
2587 QCOMPARE(int(qvariant_cast<char>(v0)), 32);
2588 QCOMPARE(int(qvariant_cast<short>(v0)), 32);
2589 QCOMPARE(int(qvariant_cast<long>(v0)), 32);
2590 QCOMPARE(int(qvariant_cast<float>(v0)), 32);
2591 QCOMPARE(int(qvariant_cast<double>(v0)), 32);
2596 const quint8 anInt = 32;
2597 const QVariant v0 = anInt;
2599 QVERIFY(qVariantCanConvert<quint8>(v0));
2600 QCOMPARE(int(qvariant_cast<quint8>(v0)), 32);
2601 QCOMPARE(int(v0.toUInt()), 32);
2602 QCOMPARE(v0.toString(), QString("32"));
2607 const qint16 anInt = 32;
2608 const QVariant v0 = anInt;
2610 QVERIFY(qVariantCanConvert<qint16>(v0));
2611 QCOMPARE(int(qvariant_cast<qint16>(v0)), 32);
2612 QCOMPARE(int(v0.toInt()), 32);
2613 QCOMPARE(v0.toString(), QString("32"));
2618 const quint16 anInt = 32;
2619 const QVariant v0 = anInt;
2621 QVERIFY(qVariantCanConvert<quint16>(v0));
2622 QCOMPARE(int(qvariant_cast<quint16>(v0)), 32);
2623 QCOMPARE(int(v0.toUInt()), 32);
2624 QCOMPARE(v0.toString(), QString("32"));
2628 void tst_QVariant::comparePointers() const
2636 QVariant v = qVariantFromValue<void *>(&myClass);
2637 QVariant v2 = qVariantFromValue<void *>(&myClass);
2643 Q_DECLARE_METATYPE(Data*)
2645 void tst_QVariant::voidStar() const
2652 v1 = qVariantFromValue(p1);
2656 v2 = qVariantFromValue(p2);
2660 v2 = qVariantFromValue(p2);
2664 void tst_QVariant::dataStar() const
2666 qRegisterMetaType<Data*>();
2667 Data *p1 = new Data;
2669 QVariant v1 = qVariantFromValue(p1);
2670 QCOMPARE(v1.userType(), qMetaTypeId<Data*>());
2671 QCOMPARE(qvariant_cast<Data*>(v1), p1);
2676 v2 = qVariantFromValue(p1);
2681 void tst_QVariant::canConvertQStringList() const
2683 QFETCH(bool, canConvert);
2684 QFETCH(QStringList, input);
2685 QFETCH(QString, result);
2689 QCOMPARE(v.canConvert(QVariant::String), canConvert);
2690 QCOMPARE(v.toString(), result);
2693 void tst_QVariant::canConvertQStringList_data() const
2695 QTest::addColumn<bool>("canConvert");
2696 QTest::addColumn<QStringList>("input");
2697 QTest::addColumn<QString>("result");
2699 QTest::newRow("An empty list") << false << QStringList() << QString();
2700 QTest::newRow("A single item") << true << QStringList(QLatin1String("foo")) << QString::fromLatin1("foo");
2701 QTest::newRow("A single, but empty item") << true << QStringList(QString()) << QString();
2706 QTest::newRow("Two items") << false << l << QString();
2709 QTest::newRow("Three items") << false << l << QString();
2712 template<typename T> void convertMetaType()
2714 QVERIFY(qVariantFromValue<T>(10).isValid());
2715 QVERIFY(qVariantFromValue<T>(10).canConvert(QVariant::Int));
2716 QCOMPARE(qVariantFromValue<T>(10).toInt(), 10);
2717 QCOMPARE(qVariantFromValue<T>(10), qVariantFromValue<T>(10));
2720 #define CONVERT_META_TYPE(Type) \
2721 convertMetaType<Type>(); \
2722 if (QTest::currentTestFailed()) \
2723 QFAIL("convertMetaType<" #Type "> failed");
2725 void tst_QVariant::canConvertMetaTypeToInt() const
2727 CONVERT_META_TYPE(long);
2728 CONVERT_META_TYPE(short);
2729 CONVERT_META_TYPE(short);
2730 CONVERT_META_TYPE(unsigned short);
2731 CONVERT_META_TYPE(ushort);
2732 CONVERT_META_TYPE(ulong);
2733 CONVERT_META_TYPE(unsigned long);
2734 CONVERT_META_TYPE(uchar);
2735 CONVERT_META_TYPE(unsigned char);
2736 CONVERT_META_TYPE(char);
2737 CONVERT_META_TYPE(uint);
2738 CONVERT_META_TYPE(unsigned int);
2741 #undef CONVERT_META_TYPE
2744 These calls should not produce any warnings.
2746 void tst_QVariant::variantToDateTimeWithoutWarnings() const
2749 const QVariant variant(QLatin1String("An invalid QDateTime string"));
2750 const QDateTime dateTime(variant.toDateTime());
2751 QVERIFY(!dateTime.isValid());
2755 QVariant v1(QLatin1String("xyz"));
2756 v1.convert(QVariant::DateTime);
2758 QVariant v2(QLatin1String("xyz"));
2759 QDateTime dt1(v2.toDateTime());
2761 const QVariant v3(QLatin1String("xyz"));
2762 const QDateTime dt2(v3.toDateTime());
2766 void tst_QVariant::invalidDateTime() const
2768 QVariant variant(QString::fromLatin1("Invalid date time string"));
2769 QVERIFY(!variant.toDateTime().isValid());
2770 QVERIFY(!variant.convert(QVariant::DateTime));
2775 MyClass() : myValue(0) {}
2779 Q_DECLARE_METATYPE( MyClass )
2781 void tst_QVariant::loadUnknownUserType()
2783 qRegisterMetaType<MyClass>("MyClass");
2784 char data[] = {0, 0, 1, 0, 0, 0, 0, 0, 8, 77, 121, 67, 108, 97, 115, 115, 0};
2786 QByteArray ba(data, sizeof(data));
2787 QDataStream ds(&ba, QIODevice::ReadOnly);
2790 QCOMPARE(ds.status(), QDataStream::ReadCorruptData);
2793 void tst_QVariant::loadBrokenUserType()
2795 char data[] = {0, 0, 0, 127, 0, 112 };
2797 QByteArray ba(data, sizeof(data));
2798 QDataStream ds(&ba, QIODevice::ReadOnly);
2801 QCOMPARE(ds.status(), QDataStream::ReadPastEnd);
2804 void tst_QVariant::invalidDate() const
2806 QString foo("Hello");
2807 QVariant variant(foo);
2808 QVERIFY(!variant.convert(QVariant::Date));
2811 QVERIFY(!variant.convert(QVariant::DateTime));
2814 QVERIFY(!variant.convert(QVariant::Time));
2817 QVERIFY(!variant.convert(QVariant::Int));
2820 QVERIFY(!variant.convert(QVariant::Double));
2823 QVERIFY(!variant.convert(QVariant::Type(QMetaType::Float)));
2828 int x,y,z,q,w,e,r,t;
2830 Q_DECLARE_METATYPE(WontCompare);
2832 void tst_QVariant::compareCustomTypes() const
2834 qRegisterMetaType<WontCompare>("WontCompare");
2838 const QVariant variant1(qVariantFromValue(f1));
2842 const QVariant variant2(qVariantFromValue(f2));
2844 /* We compare pointers. */
2845 QVERIFY(variant1 != variant2);
2846 QVERIFY(variant1 == variant1);
2847 QVERIFY(variant2 == variant2);
2850 void tst_QVariant::timeToDateTime() const
2852 const QVariant val(QTime::currentTime());
2853 QVERIFY(!val.canConvert(QVariant::DateTime));
2854 QVERIFY(!val.toDateTime().isValid());
2857 Q_DECLARE_METATYPE(QHostAddress)
2859 void tst_QVariant::copyingUserTypes() const
2863 const QHostAddress ha("127.0.0.1");
2864 qVariantSetValue(var, ha);
2867 QCOMPARE(qVariantValue<QHostAddress>(var3), ha);
2870 void tst_QVariant::convertBoolToByteArray() const
2872 QFETCH(QByteArray, input);
2873 QFETCH(bool, canConvert);
2874 QFETCH(bool, value);
2876 const QVariant variant(input);
2878 QCOMPARE(qVariantCanConvert<bool>(variant), canConvert);
2881 /* Just call this function so we run the code path. */
2882 QCOMPARE(variant.toBool(), value);
2886 void tst_QVariant::convertBoolToByteArray_data() const
2888 QTest::addColumn<QByteArray>("input");
2889 QTest::addColumn<bool>("canConvert");
2890 QTest::addColumn<bool>("value");
2892 QTest::newRow("false")
2893 << QByteArray("false")
2897 QTest::newRow("FALSE")
2898 << QByteArray("FALSE")
2902 QTest::newRow("falSE")
2903 << QByteArray("FALSE")
2912 QTest::newRow("null QByteArray")
2917 QTest::newRow("any-content")
2918 << QByteArray("any-content")
2922 QTest::newRow("true")
2923 << QByteArray("true")
2927 QTest::newRow("TRUE")
2928 << QByteArray("TRUE")
2932 QTest::newRow("trUE")
2933 << QByteArray("trUE")
2938 void tst_QVariant::convertByteArrayToBool() const
2940 QFETCH(bool, input);
2941 QFETCH(QByteArray, output);
2943 const QVariant variant(input);
2944 QCOMPARE(variant.type(), QVariant::Bool);
2945 QCOMPARE(variant.toBool(), input);
2946 QVERIFY(qVariantCanConvert<bool>(variant));
2948 QCOMPARE(variant.toByteArray(), output);
2951 void tst_QVariant::convertByteArrayToBool_data() const
2953 QTest::addColumn<bool>("input");
2954 QTest::addColumn<QByteArray>("output");
2956 QTest::newRow("false")
2958 << QByteArray("false");
2960 QTest::newRow("true")
2962 << QByteArray("true");
2967 1. Converting the string "9.9" to int fails. This is the behavior of
2968 toLongLong() and hence also QVariant, since it uses it.
2969 2. Converting the QVariant containing the double 9.9 to int works.
2971 Rationale: "9.9" is not a valid int. However, doubles are by definition not
2972 ints and therefore it makes more sense to perform conversion for those.
2974 void tst_QVariant::toIntFromQString() const
2976 QVariant first("9.9");
2978 QCOMPARE(first.toInt(&ok), 0);
2981 QCOMPARE(QString("9.9").toLongLong(&ok), qlonglong(0));
2985 QCOMPARE(v.toInt(&ok), 10);
2991 1. Conversion from (64 bit) double to int works (no overflow).
2992 2. Same conversion works for QVariant::convert.
2994 Rationale: if 2147483630 is set in float and then converted to int,
2995 there will be overflow and the result will be -2147483648.
2997 void tst_QVariant::toIntFromDouble() const
2999 double d = 2147483630; // max int 2147483647
3000 QVERIFY((int)d == 2147483630);
3003 QVERIFY( var.canConvert( QVariant::Int ) );
3006 int result = var.toInt(&ok);
3008 QVERIFY( ok == true );
3009 QCOMPARE(result, 2147483630);
3012 void tst_QVariant::setValue()
3014 QTransform t; //we just take a value so that we're sure that it will be shared
3016 QVERIFY( v1.isDetached() );
3018 QVERIFY( !v1.isDetached() );
3019 QVERIFY( !v2.isDetached() );
3021 qVariantSetValue(v2, 3); //set an integer value
3023 QVERIFY( v1.isDetached() );
3024 QVERIFY( v2.isDetached() );
3027 void tst_QVariant::numericalConvert()
3029 #if defined(Q_OS_LINUX) && defined(Q_CC_GNU) && !defined(__x86_64__)
3030 QSKIP("Known to fail due to a GCC bug on at least Ubuntu 10.04 32-bit - check QTBUG-8959");
3032 QVariant vfloat(float(5.3));
3033 QVariant vdouble(double(5.3));
3034 QVariant vreal(qreal(5.3));
3035 QVariant vint(int(5));
3036 QVariant vuint(uint(5));
3037 QVariant vshort(short(5));
3038 QVariant vlonglong(quint64(5));
3039 QVariant vstringint(QString::fromLatin1("5"));
3040 QVariant vstring(QString::fromLatin1("5.3"));
3042 QVector<QVariant *> vect;
3043 vect << &vfloat << &vdouble << &vreal << &vint << &vuint << &vshort<< &vlonglong << &vstringint << &vstring;
3045 for(int i = 0; i < vect.size(); i++) {
3047 if (i >= 3 && i <= 7)
3049 QVariant *v = vect.at(i);
3050 QCOMPARE(v->toFloat() , float(num));
3051 QCOMPARE(float(v->toReal()) , float(num));
3052 QCOMPARE(float(v->toDouble()) , float(num));
3054 QCOMPARE(v->toInt() , int(num));
3055 QCOMPARE(v->toUInt() , uint(num));
3056 QCOMPARE(v->toULongLong() , quint64(num));
3058 QCOMPARE(v->toString() , QString::number(num));
3063 template<class T> void playWithVariant(const T &orig, bool isNull, const QString &toString, double toDouble, bool toBool)
3065 QVariant v = QVariant::fromValue(orig);
3066 QVERIFY(v.isValid());
3067 QCOMPARE(v.isNull(), isNull);
3068 QCOMPARE(v.toString(), toString);
3069 QCOMPARE(v.toDouble(), toDouble);
3070 QCOMPARE(v.toBool(), toBool);
3071 QCOMPARE(qvariant_cast<T>(v), orig);
3075 if (!(QTypeInfo<T>::isStatic && QTypeInfo<T>::isComplex)) {
3076 // Type is movable so standard comparison algorithm in QVariant should work
3077 // In a custom type QVariant is not aware of ==operator so it won't be called,
3078 // which may cause problems especially visible when using a not-movable type
3081 QVERIFY(v2.isValid());
3082 QCOMPARE(v2.isNull(), isNull);
3083 QCOMPARE(v2.toString(), toString);
3084 QCOMPARE(v2.toDouble(), toDouble);
3085 QCOMPARE(v2.toBool(), toBool);
3086 QCOMPARE(qvariant_cast<T>(v2), orig);
3092 if (!(QTypeInfo<T>::isStatic && QTypeInfo<T>::isComplex)) {
3093 // Type is movable so standard comparison algorithm in QVariant should work
3094 // In a custom type QVariant is not aware of ==operator so it won't be called,
3095 // which may cause problems especially visible when using a not-movable type
3098 QCOMPARE(qvariant_cast<T>(v2), qvariant_cast<T>(v));
3099 QCOMPARE(v2.toString(), toString);
3100 v3 = qVariantFromValue(orig);
3102 QVERIFY(v3.isValid());
3103 QCOMPARE(v3.isNull(), isNull);
3104 QCOMPARE(v3.toString(), toString);
3105 QCOMPARE(v3.toDouble(), toDouble);
3106 QCOMPARE(v3.toBool(), toBool);
3107 QCOMPARE(qvariant_cast<T>(v3), qvariant_cast<T>(v));
3110 QVERIFY(v.isValid());
3111 QCOMPARE(v.isNull(), isNull);
3112 QCOMPARE(v.toString(), toString);
3113 QCOMPARE(v.toDouble(), toDouble);
3114 QCOMPARE(v.toBool(), toBool);
3115 QCOMPARE(qvariant_cast<T>(v), orig);
3117 if (qMetaTypeId<T>() != qMetaTypeId<QVariant>()) {
3118 QCOMPARE(v.userType(), qMetaTypeId<T>());
3119 QCOMPARE(QVariant::typeToName(QVariant::Type(v.userType())), QMetaType::typeName(qMetaTypeId<T>()));
3123 #define PLAY_WITH_VARIANT(Orig, IsNull, ToString, ToDouble, ToBool) \
3124 playWithVariant(Orig, IsNull, ToString, ToDouble, ToBool);\
3125 if (QTest::currentTestFailed())\
3126 QFAIL("playWithVariant failed");
3131 bool operator==(const MyPrimitive &o) const
3133 return x == o.x && y == o.y;
3138 Q_DECLARE_TYPEINFO(MyPrimitive, Q_PRIMITIVE_TYPE);
3144 MyData() : ptr(this) {}
3147 if (ptr != this) qWarning("%s: object has moved", Q_FUNC_INFO);
3149 MyData(const MyData& o) : ptr(this)
3151 if (o.ptr != &o) qWarning("%s: other object has moved", Q_FUNC_INFO);
3153 MyData &operator=(const MyData &o)
3155 if (ptr != this) qWarning("%s: object has moved", Q_FUNC_INFO);
3156 if (o.ptr != &o) qWarning("%s: other object has moved", Q_FUNC_INFO);
3159 bool operator==(const MyData &o) const
3161 if (ptr != this) qWarning("%s: object has moved", Q_FUNC_INFO);
3162 if (o.ptr != &o) qWarning("%s: other object has moved", Q_FUNC_INFO);
3171 MyMovable() { v = count++; }
3172 ~MyMovable() { count--; }
3173 MyMovable(const MyMovable &o) : v(o.v) { count++; }
3175 bool operator==(const MyMovable &o) const
3181 int MyMovable::count = 0;
3187 MyNotMovable() : that(this) { count++; }
3188 ~MyNotMovable() { QCOMPARE(that, this); count--; }
3189 MyNotMovable(const MyNotMovable &o) : that(this) { QCOMPARE(o.that, &o); count++; }
3190 MyNotMovable &operator=(const MyNotMovable &o) {
3191 bool ok = that == this && o.that == &o;
3192 if (!ok) qFatal("MyNotMovable has been moved");
3196 //PLAY_WITH_VARIANT test that they are equal, but never that they are not equal
3197 // so it would be fine just to always return true
3198 bool operator==(const MyNotMovable &o) const
3200 bool ok = that == this && o.that == &o;
3201 if (!ok) qFatal("MyNotMovable has been moved");
3206 int MyNotMovable::count = 0;
3208 struct MyShared : QSharedData {
3213 Q_DECLARE_TYPEINFO(MyMovable, Q_MOVABLE_TYPE);
3216 Q_DECLARE_METATYPE(QList<QSize>)
3217 Q_DECLARE_METATYPE(MyPrimitive)
3218 Q_DECLARE_METATYPE(MyData)
3219 Q_DECLARE_METATYPE(MyMovable)
3220 Q_DECLARE_METATYPE(MyNotMovable)
3221 Q_DECLARE_METATYPE(QList<MyPrimitive>)
3222 Q_DECLARE_METATYPE(QList<MyData>)
3223 Q_DECLARE_METATYPE(QList<MyMovable>)
3224 Q_DECLARE_METATYPE(QList<MyNotMovable>)
3225 Q_DECLARE_METATYPE(MyPrimitive *)
3226 Q_DECLARE_METATYPE(MyData *)
3227 Q_DECLARE_METATYPE(MyMovable *)
3228 Q_DECLARE_METATYPE(MyNotMovable *)
3229 Q_DECLARE_METATYPE(QSharedDataPointer<MyShared>)
3232 void tst_QVariant::moreCustomTypes()
3235 QList<QSize> listSize;
3236 PLAY_WITH_VARIANT(listSize, false, QString(), 0, false);
3237 listSize << QSize(4,5) << QSize(89,23) << QSize(5,6);
3238 PLAY_WITH_VARIANT(listSize, false, QString(), 0, false);
3243 PLAY_WITH_VARIANT(str, true, QString(), 0, false);
3244 str = QString::fromLatin1("123456789.123");
3245 PLAY_WITH_VARIANT(str, false, str, 123456789.123, true);
3250 PLAY_WITH_VARIANT(size, false, QString(), 0, false);
3251 PLAY_WITH_VARIANT(QSize(45,78), false, QString(), 0, false);
3256 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3257 PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3259 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3260 l << MyData() << MyData();
3261 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3265 MyPrimitive d = { 4, 5 };
3266 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3267 PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3268 QList<MyPrimitive> l;
3269 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3271 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3276 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3277 PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3279 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3280 l << MyMovable() << d;
3281 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3283 QCOMPARE(MyMovable::count, 0);
3285 QCOMPARE(MyNotMovable::count, 0);
3288 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3289 PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3290 QList<MyNotMovable> l;
3291 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3292 l << MyNotMovable() << d;
3293 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3295 QCOMPARE(MyNotMovable::count, 0);
3298 PLAY_WITH_VARIANT(12.12, false, "12.12", 12.12, true);
3299 PLAY_WITH_VARIANT(12.12f, false, "12.12", 12.12f, true);
3300 PLAY_WITH_VARIANT('a', false, "a", 'a', true);
3301 PLAY_WITH_VARIANT((unsigned char)('a'), false, "a", 'a', true);
3302 PLAY_WITH_VARIANT( quint8(12), false, "\xc", 12, true);
3303 PLAY_WITH_VARIANT( qint8(13), false, "\xd", 13, true);
3304 PLAY_WITH_VARIANT(quint16(14), false, "14", 14, true);
3305 PLAY_WITH_VARIANT( qint16(15), false, "15", 15, true);
3306 PLAY_WITH_VARIANT(quint32(16), false, "16", 16, true);
3307 PLAY_WITH_VARIANT( qint32(17), false, "17", 17, true);
3308 PLAY_WITH_VARIANT(quint64(18), false, "18", 18, true);
3309 PLAY_WITH_VARIANT( qint64(19), false, "19", 19, true);
3310 PLAY_WITH_VARIANT( qint8(-12), false, "\xf4", -12, true);
3311 PLAY_WITH_VARIANT( qint16(-13), false, "-13", -13, true);
3312 PLAY_WITH_VARIANT( qint32(-14), false, "-14", -14, true);
3313 PLAY_WITH_VARIANT( qint64(-15), false, "-15", -15, true);
3314 PLAY_WITH_VARIANT(quint64(0), false, "0", 0, false);
3315 PLAY_WITH_VARIANT( true, false, "true", 1, true);
3316 PLAY_WITH_VARIANT( false, false, "false", 0, false);
3318 PLAY_WITH_VARIANT(QString("hello\n"), false, "hello\n", 0, true);
3323 PLAY_WITH_VARIANT((void *)(&i), false, QString(), 0, false);
3324 PLAY_WITH_VARIANT((void *)(0), false, QString(), 0, false);
3328 QVariant v1 = QVariant::fromValue(5);
3329 QVariant v2 = QVariant::fromValue(5.0);
3330 QVariant v3 = QVariant::fromValue(quint16(5));
3332 QVariant v5 = QVariant::fromValue(MyPrimitive());
3333 QVariant v6 = QVariant::fromValue(MyMovable());
3334 QVariant v7 = QVariant::fromValue(MyData());
3335 PLAY_WITH_VARIANT(v1, false, "5", 5, true);
3336 PLAY_WITH_VARIANT(v2, false, "5", 5, true);
3337 PLAY_WITH_VARIANT(v3, false, "5", 5, true);
3338 PLAY_WITH_VARIANT(v4, false, "5", 5, true);
3340 PLAY_WITH_VARIANT(v5, false, QString(), 0, false);
3343 QCOMPARE(MyMovable::count, 0);
3345 QSharedDataPointer<MyShared> d(new MyShared);
3346 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3348 QCOMPARE(MyMovable::count, 0);
3351 QList<QList<int> > data;
3352 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3353 data << (QList<int>() << 42);
3354 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3358 QList<QVector<int> > data;
3359 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3360 data << (QVector<int>() << 42);
3361 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3365 QList<QSet<int> > data;
3366 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3367 data << (QSet<int>() << 42);
3368 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3372 QList<QLinkedList<int> > data;
3373 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3374 data << (QLinkedList<int>() << 42);
3375 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3379 void tst_QVariant::movabilityTest()
3381 // This test checks if QVariant is movable even if an internal data is not movable.
3382 QVERIFY(!MyNotMovable::count);
3384 QVariant variant = QVariant::fromValue(MyNotMovable());
3385 QVERIFY(MyNotMovable::count);
3387 // prepare destination memory space to which variant will be moved
3389 QCOMPARE(buffer[0].type(), QVariant::Invalid);
3390 buffer[0].~QVariant();
3392 memcpy(buffer, &variant, sizeof(QVariant));
3393 QCOMPARE(buffer[0].type(), QVariant::UserType);
3394 QCOMPARE(buffer[0].userType(), qMetaTypeId<MyNotMovable>());
3395 MyNotMovable tmp(buffer[0].value<MyNotMovable>());
3397 new (&variant) QVariant();
3399 QVERIFY(!MyNotMovable::count);
3402 void tst_QVariant::variantInVariant()
3405 QCOMPARE(var1.type(), QVariant::Int);
3406 QVariant var2 = var1;
3407 QCOMPARE(var2, var1);
3408 QCOMPARE(var2.type(), QVariant::Int);
3409 QVariant var3 = QVariant::fromValue(var1);
3410 QCOMPARE(var3, var1);
3411 QCOMPARE(var3.type(), QVariant::Int);
3412 QVariant var4 = qvariant_cast<QVariant>(var1);
3413 QCOMPARE(var4, var1);
3414 QCOMPARE(var4.type(), QVariant::Int);
3417 QCOMPARE(var5, var1);
3418 QCOMPARE(var5.type(), QVariant::Int);
3420 var6.setValue(var1);
3421 QCOMPARE(var6, var1);
3422 QCOMPARE(var6.type(), QVariant::Int);
3424 QCOMPARE(QVariant::fromValue(var1), QVariant::fromValue(var2));
3425 QCOMPARE(qvariant_cast<QVariant>(var3), QVariant::fromValue(var4));
3426 QCOMPARE(qvariant_cast<QVariant>(var5), qvariant_cast<QVariant>(var6));
3428 QString str("hello");
3429 QVariant var8 = qvariant_cast<QVariant>(QVariant::fromValue(QVariant::fromValue(str)));
3430 QCOMPARE((int)var8.type(), (int)QVariant::String);
3431 QCOMPARE(qvariant_cast<QString>(QVariant(qvariant_cast<QVariant>(var8))), str);
3433 QVariant var9(qMetaTypeId<QVariant>(), &var1);
3434 QCOMPARE(var9.userType(), qMetaTypeId<QVariant>());
3435 QCOMPARE(qvariant_cast<QVariant>(var9), var1);
3438 void tst_QVariant::colorInteger()
3440 QVariant v = QColor(Qt::red);
3441 QCOMPARE(v.type(), QVariant::Color);
3442 QCOMPARE(v.value<QColor>(), QColor(Qt::red));
3445 QCOMPARE(v.type(), QVariant::Int);
3446 QCOMPARE(v.toInt(), 1000);
3448 v.setValue(QColor(Qt::yellow));
3449 QCOMPARE(v.type(), QVariant::Color);
3450 QCOMPARE(v.value<QColor>(), QColor(Qt::yellow));
3454 Q_DECLARE_OPAQUE_POINTER(Forward*)
3455 Q_DECLARE_METATYPE(Forward*)
3457 void tst_QVariant::forwardDeclare()
3460 QVariant v = QVariant::fromValue(f);
3461 QCOMPARE(qvariant_cast<Forward*>(v), f);
3464 void tst_QVariant::loadQt5Stream_data()
3466 dataStream_data(QDataStream::Qt_5_0);
3469 void tst_QVariant::loadQt5Stream()
3471 loadQVariantFromDataStream(QDataStream::Qt_5_0);
3474 void tst_QVariant::saveQt5Stream_data()
3476 dataStream_data(QDataStream::Qt_5_0);
3479 void tst_QVariant::saveQt5Stream()
3481 saveQVariantFromDataStream(QDataStream::Qt_5_0);
3484 void tst_QVariant::loadQt4Stream_data()
3486 dataStream_data(QDataStream::Qt_4_9);
3489 void tst_QVariant::loadQt4Stream()
3491 loadQVariantFromDataStream(QDataStream::Qt_4_9);
3494 void tst_QVariant::saveQt4Stream_data()
3496 dataStream_data(QDataStream::Qt_4_9);
3499 void tst_QVariant::saveQt4Stream()
3501 saveQVariantFromDataStream(QDataStream::Qt_4_9);
3504 void tst_QVariant::dataStream_data(QDataStream::Version version)
3506 QTest::addColumn<QString>("fileName");
3510 case QDataStream::Qt_4_9:
3511 path = QString::fromLatin1("qt4.9");
3513 case QDataStream::Qt_5_0:
3514 path = QString::fromLatin1("qt5.0");
3520 path = path.prepend(":/stream/").append("/");
3523 foreach (const QFileInfo &fileInfo, dir.entryInfoList(QStringList() << "*.bin")) {
3524 QTest::newRow((path + fileInfo.fileName()).toLatin1()) << fileInfo.filePath();
3530 void tst_QVariant::loadQVariantFromDataStream(QDataStream::Version version)
3532 QFETCH(QString, fileName);
3534 QFile file(fileName);
3535 QVERIFY(file.open(QIODevice::ReadOnly));
3537 QDataStream stream(&file);
3538 stream.setVersion(version);
3541 QVariant loadedVariant;
3542 stream >> typeName >> loadedVariant;
3544 const int id = QMetaType::type(typeName.toLatin1());
3545 if (id == QMetaType::Void) {
3546 // Void type is not supported by QVariant
3550 QVariant constructedVariant(static_cast<QVariant::Type>(id));
3551 QCOMPARE(constructedVariant.userType(), id);
3552 QCOMPARE(QMetaType::typeName(loadedVariant.userType()), typeName.toLatin1().constData());
3553 QCOMPARE(loadedVariant.userType(), constructedVariant.userType());
3556 void tst_QVariant::saveQVariantFromDataStream(QDataStream::Version version)
3558 QFETCH(QString, fileName);
3560 QFile file(fileName);
3561 QVERIFY(file.open(QIODevice::ReadOnly));
3562 QDataStream dataFileStream(&file);
3565 dataFileStream >> typeName;
3566 QByteArray data = file.readAll();
3567 const int id = QMetaType::type(typeName.toLatin1());
3568 if (id == QMetaType::Void) {
3569 // Void type is not supported by QVariant
3574 buffer.open(QIODevice::ReadWrite);
3575 QDataStream stream(&buffer);
3576 stream.setVersion(version);
3578 QVariant constructedVariant(static_cast<QVariant::Type>(id));
3579 QCOMPARE(constructedVariant.userType(), id);
3580 stream << constructedVariant;
3582 // We are testing QVariant there is no point in testing full array.
3583 QCOMPARE(buffer.data().left(5), data.left(5));
3586 QVariant recunstructedVariant;
3587 stream >> recunstructedVariant;
3588 QCOMPARE(recunstructedVariant.userType(), constructedVariant.userType());
3591 class MessageHandler {
3593 MessageHandler(const int typeId)
3594 : oldMsgHandler(qInstallMsgHandler(handler))
3601 qInstallMsgHandler(oldMsgHandler);
3604 bool testPassed() const
3609 static void handler(QtMsgType, const char *txt)
3611 QString msg = QString::fromLatin1(txt);
3612 // Format itself is not important, but basic data as a type name should be included in the output
3613 ok = msg.startsWith("QVariant(") + QMetaType::typeName(currentId);
3614 QVERIFY2(ok, (QString::fromLatin1("Message is not valid: '") + msg + '\'').toLatin1().constData());
3617 QtMsgHandler oldMsgHandler;
3618 static int currentId;
3621 bool MessageHandler::ok;
3622 int MessageHandler::currentId;
3624 void tst_QVariant::debugStream_data()
3626 QTest::addColumn<QVariant>("variant");
3627 QTest::addColumn<int>("typeId");
3628 for (int id = 0; id < QMetaType::User; ++id) {
3629 const char *tagName = QMetaType::typeName(id);
3632 if (id != QMetaType::Void) {
3633 QTest::newRow(tagName) << QVariant(static_cast<QVariant::Type>(id)) << id;
3636 QTest::newRow("QBitArray(111)") << QVariant(QBitArray(3, true)) << qMetaTypeId<QBitArray>();
3637 QTest::newRow("CustomStreamableClass") << QVariant(qMetaTypeId<CustomStreamableClass>(), 0) << qMetaTypeId<CustomStreamableClass>();
3638 QTest::newRow("MyClass") << QVariant(qMetaTypeId<MyClass>(), 0) << qMetaTypeId<MyClass>();
3641 void tst_QVariant::debugStream()
3643 QFETCH(QVariant, variant);
3644 QFETCH(int, typeId);
3646 MessageHandler msgHandler(typeId);
3647 qDebug() << variant;
3648 QVERIFY(msgHandler.testPassed());
3651 QTEST_MAIN(tst_QVariant)
3652 #include "tst_qvariant.moc"