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 <qdatetime.h>
48 #include <qiodevice.h>
51 #include <qkeysequence.h>
57 #include <qmatrix4x4.h>
61 #include <qtransform.h>
62 #include <qvector2d.h>
63 #include <qvector3d.h>
64 #include <qvector4d.h>
65 #include <qquaternion.h>
70 #include <QLinkedList>
71 #include <QRegularExpression>
77 #include "tst_qvariant_common.h"
79 class CustomNonQObject;
81 class tst_QVariant : public QObject
86 tst_QVariant(QObject *parent = 0)
87 : QObject(parent), customNonQObjectPointer(0)
93 void cleanupTestCase();
96 void copy_constructor();
97 void constructor_invalid_data();
98 void constructor_invalid();
102 void canConvert_data();
135 void toLongLong_data();
138 void toULongLong_data();
141 void toByteArray_data();
144 void toString_data();
153 void toDateTime_data();
156 void toDouble_data();
159 void toPointF_data();
165 void toKeySequence_data();
166 void toKeySequence();
174 void toPixmap_data();
183 void qvariant_cast_QObject_data();
184 void qvariant_cast_QObject();
185 void qvariant_cast_QObject_derived();
190 void toRegularExpression();
205 void basicUserType();
209 void writeToReadFromDataStream_data();
210 void writeToReadFromDataStream();
211 void writeToReadFromOldDataStream();
212 void checkDataStream();
214 void operator_eq_eq_data();
215 void operator_eq_eq();
217 void operator_eq_eq_rhs();
219 void typeName_data();
223 void streamInvalidVariant();
230 void saveLoadCustomTypes();
235 void convertToQUint8() const;
236 void invalidQColor() const;
237 void comparePointers() const;
238 void voidStar() const;
239 void dataStar() const;
240 void canConvertQStringList() const;
241 void canConvertQStringList_data() const;
242 void canConvertMetaTypeToInt() const;
243 void variantToDateTimeWithoutWarnings() const;
244 void invalidDateTime() const;
246 void loadUnknownUserType();
247 void loadBrokenUserType();
249 void invalidDate() const;
250 void compareCustomTypes() const;
251 void timeToDateTime() const;
252 void copyingUserTypes() const;
253 void convertBoolToByteArray() const;
254 void convertBoolToByteArray_data() const;
255 void convertByteArrayToBool() const;
256 void convertByteArrayToBool_data() const;
257 void toIntFromQString() const;
258 void toIntFromDouble() const;
261 void numericalConvert();
262 void moreCustomTypes();
263 void movabilityTest();
264 void variantInVariant();
268 void forwardDeclare();
269 void debugStream_data();
271 void debugStreamType_data();
272 void debugStreamType();
274 void loadQt4Stream_data();
275 void loadQt4Stream();
276 void saveQt4Stream_data();
277 void saveQt4Stream();
278 void loadQt5Stream_data();
279 void loadQt5Stream();
280 void saveQt5Stream_data();
281 void saveQt5Stream();
283 void guiVariantAtExit();
285 void dataStream_data(QDataStream::Version version);
286 void loadQVariantFromDataStream(QDataStream::Version version);
287 void saveQVariantFromDataStream(QDataStream::Version version);
289 CustomNonQObject *customNonQObjectPointer;
290 QVector<QObject*> objectPointerTestData;
293 const qlonglong intMax1 = (qlonglong)INT_MAX + 1;
294 const qulonglong uintMax1 = (qulonglong)UINT_MAX + 1;
296 void tst_QVariant::constructor()
299 QVERIFY( !variant.isValid() );
300 QVERIFY( variant.isNull() );
302 QVariant var2(variant);
303 QVERIFY( !var2.isValid() );
304 QVERIFY( variant.isNull() );
306 QVariant varll(intMax1);
307 QVariant varll2(varll);
308 QCOMPARE(varll2, varll);
310 QVariant var3(QVariant::String);
311 QCOMPARE(var3.typeName(), "QString");
312 QVERIFY(var3.isNull());
313 QVERIFY(var3.isValid());
315 QVariant var4(QVariant::Invalid);
316 QCOMPARE(var4.type(), QVariant::Invalid);
317 QVERIFY(var4.isNull());
318 QVERIFY(!var4.isValid());
320 QVariant var5(QLatin1String("hallo"));
321 QCOMPARE(var5.type(), QVariant::String);
322 QCOMPARE(var5.typeName(), "QString");
324 QVariant var6(qlonglong(0));
325 QCOMPARE(var6.type(), QVariant::LongLong);
326 QCOMPARE(var6.typeName(), "qlonglong");
329 QVERIFY(var7.isValid());
330 QVERIFY(!var7.isNull());
332 var8.setValue<int>(5);
333 QVERIFY(var8.isValid());
334 QVERIFY(!var8.isNull());
337 void tst_QVariant::constructor_invalid_data()
339 QTest::addColumn<uint>("typeId");
341 QTest::newRow("-1") << uint(-1);
342 QTest::newRow("-122234567") << uint(-122234567);
343 QTest::newRow("0xfffffffff") << uint(0xfffffffff);
344 QTest::newRow("LastCoreType + 1") << uint(QMetaType::LastCoreType + 1);
345 QVERIFY(!QMetaType::isRegistered(QMetaType::LastCoreType + 1));
348 void tst_QVariant::constructor_invalid()
351 QFETCH(uint, typeId);
353 MessageHandlerInvalidType msg;
354 QVariant variant(static_cast<QVariant::Type>(typeId));
355 QVERIFY(!variant.isValid());
356 QVERIFY(variant.userType() == QMetaType::UnknownType);
360 MessageHandlerInvalidType msg;
361 QVariant variant(typeId, /* copy */ 0);
362 QVERIFY(!variant.isValid());
363 QVERIFY(variant.userType() == QMetaType::UnknownType);
368 void tst_QVariant::copy_constructor()
370 QVariant var7(QVariant::Int);
372 QCOMPARE(var8.type(), QVariant::Int);
373 QVERIFY(var8.isNull());
376 void tst_QVariant::isNull()
379 QVERIFY( var.isNull() );
382 QVariant var1( str1 );
383 QVERIFY( var1.isNull() );
385 QVariant var2( QString::null );
386 QVERIFY( var2.isNull() );
388 QVariant var3( QString( "blah" ) );
389 QVERIFY( !var3.isNull() );
392 QVERIFY( !var4.isNull() );
394 QVariant var5 = QString();
395 QVERIFY( var5.isNull() );
397 QVariant var6( QString( "blah" ) );
398 QVERIFY( !var6.isNull() );
400 QVERIFY( var6.isNull() );
401 var6.convert( QVariant::String );
402 QVERIFY( var6.isNull() );
403 QVariant varLL( (qlonglong)0 );
404 QVERIFY( !varLL.isNull() );
405 QVariant var7(QString::null);
406 QVERIFY(var7.isNull());
409 void tst_QVariant::swap()
411 QVariant v1 = 1, v2 = 2.0;
413 QCOMPARE(v1.type(),QVariant::Double);
414 QCOMPARE(v1.toDouble(),2.0);
415 QCOMPARE(v2.type(),QVariant::Int);
416 QCOMPARE(v2.toInt(),1);
419 void tst_QVariant::canConvert_data()
421 TST_QVARIANT_CANCONVERT_DATATABLE_HEADERS
432 // 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
435 QVariant var(QBitArray(0));
436 QTest::newRow("BitArray")
437 << 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;
438 var = QVariant::fromValue(QBitmap());
439 QTest::newRow("Bitmap")
440 << 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;
441 var = QVariant::fromValue(QBrush());
442 QTest::newRow("Brush")
443 << 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;
444 var = QVariant(QByteArray());
445 QTest::newRow("ByteArray")
446 << 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;
447 var = QVariant::fromValue(QColor());
448 QTest::newRow("Color")
449 << 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;
451 var = QVariant::fromValue(QCursor());
452 QTest::newRow("Cursor")
453 << 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;
455 var = QVariant(QDate());
456 QTest::newRow("Date")
457 << 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;
458 var = QVariant(QDateTime());
459 QTest::newRow("DateTime")
460 << 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;
461 var = QVariant((double)0.1);
462 QTest::newRow("Double")
463 << 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;
464 var = QVariant(0.1f);
465 QTest::newRow("Float")
466 << 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;
467 var = QVariant::fromValue(QFont());
468 QTest::newRow("Font")
469 << 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;
470 var = QVariant::fromValue(QIcon());
471 QTest::newRow("Icon")
472 << 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;
473 var = QVariant::fromValue(QImage());
474 QTest::newRow("Image")
475 << 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;
476 var = QVariant((int)1);
478 << 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;
480 QTest::newRow("Invalid")
481 << 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;
482 var = QVariant::fromValue(QKeySequence());
483 QTest::newRow("KeySequence")
484 << 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;
485 var = QVariant(QList<QVariant>());
486 QTest::newRow("List")
487 << 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;
488 var = QVariant((qlonglong)1);
489 QTest::newRow("LongLong")
490 << 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;
491 var = QVariant(QMap<QString,QVariant>());
493 << 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;
494 var = QVariant::fromValue(QPalette());
495 QTest::newRow("Palette")
496 << 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;
497 var = QVariant::fromValue(QPen());
499 << 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;
500 var = QVariant::fromValue(QPixmap());
501 QTest::newRow("Pixmap")
502 << 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;
503 var = QVariant::fromValue(QPolygon());
504 QTest::newRow("PointArray")
505 << 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;
506 var = QVariant(QPoint());
507 QTest::newRow("Point")
508 << 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;
509 var = QVariant(QRect());
510 QTest::newRow("Rect")
511 << 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;
512 var = QVariant::fromValue(QRegion());
513 QTest::newRow("Region")
514 << 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;
515 var = QVariant(QSize());
516 QTest::newRow("Size")
517 << var << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N;
518 var = QVariant(QString());
519 QTest::newRow("String")
520 << 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;
521 var = QVariant(QStringList("entry"));
522 QTest::newRow("StringList")
523 << 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;
524 var = QVariant(QTime());
525 QTest::newRow("Time")
526 << var << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << Y << N << N;
527 var = QVariant((uint)1);
528 QTest::newRow("UInt")
529 << 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;
530 var = QVariant((int)1);
532 << 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;
533 var = QVariant((qulonglong)1);
534 QTest::newRow("ULongLong")
535 << 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;
536 var = QVariant::fromValue('a');
537 QTest::newRow("Char")
538 << 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;
539 var = QVariant::fromValue<signed char>(-1);
540 QTest::newRow("SChar")
541 << var << N << N << Y << N << Y << N << N << N << N << Y << N << N << Y << N << N << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y;
547 void tst_QVariant::canConvert()
549 TST_QVARIANT_CANCONVERT_FETCH_DATA
551 TST_QVARIANT_CANCONVERT_COMPARE_DATA
554 QCOMPARE(val.canConvert(-1), false);
555 QCOMPARE(val.canConvert(-23), false);
556 QCOMPARE(val.canConvert(-23876), false);
557 QCOMPARE(val.canConvert(23876), false);
560 void tst_QVariant::toInt_data()
562 QTest::addColumn<QVariant>("value");
563 QTest::addColumn<int>("result");
564 QTest::addColumn<bool>("valueOK");
566 QTest::newRow( "invalid" ) << QVariant() << 0 << false;
567 QTest::newRow( "int" ) << QVariant( 123 ) << 123 << true;
568 QTest::newRow( "char" ) << QVariant::fromValue('a') << int('a') << true;
569 signed char signedChar = -13;
570 QTest::newRow( "signed char" ) << QVariant::fromValue(signedChar) << -13 << true;
571 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << 3 << true;
572 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << 3 << true;
573 QTest::newRow( "uint" ) << QVariant( 123u ) << 123 << true;
574 QTest::newRow( "int-string" ) << QVariant( QString("123") ) << 123 << true;
575 QTest::newRow( "string" ) << QVariant( QString("Unicode String") ) << 0 << false;
576 QTest::newRow( "longlong0" ) << QVariant( (qlonglong)34 ) << 34 << true;
577 QTest::newRow( "longlong1" ) << QVariant( intMax1 ) << (int)INT_MIN << true;
578 QTest::newRow( "ulonglong0" ) << QVariant( (qulonglong)34 ) << 34 << true;
579 QTest::newRow( "ulonglong1" ) << QVariant( uintMax1 ) << 0 << true;
580 QTest::newRow( "signedint" ) << QVariant( -123 ) << -123 << true;
581 QTest::newRow( "signeddouble" ) << QVariant( -3.1415927 ) << -3 << true;
582 QTest::newRow( "signedfloat" ) << QVariant( -3.1415927f ) << -3 << true;
583 QTest::newRow( "signedint-string" ) << QVariant( QString("-123") ) << -123 << true;
584 QTest::newRow( "signedlonglong0" ) << QVariant( (qlonglong)-34 ) << -34 << true;
585 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << int('a') << true;
586 QTest::newRow( "keysequence" ) << QVariant::fromValue( QKeySequence( Qt::Key_A ) ) << 65 << true;
587 QByteArray bytearray(4, ' ');
592 QTest::newRow( "QByteArray1" ) << QVariant( bytearray ) << 0 << false;
597 QTest::newRow( "QByteArray2" ) << QVariant( bytearray ) << 4500 << true;
600 void tst_QVariant::toInt()
602 QFETCH( QVariant, value );
603 QFETCH( int, result );
604 QFETCH( bool, valueOK );
605 QVERIFY( value.isValid() == value.canConvert( QVariant::Int ) );
607 int i = value.toInt( &ok );
608 QCOMPARE( i, result );
609 QVERIFY( ok == valueOK );
612 void tst_QVariant::toUInt_data()
614 QTest::addColumn<QVariant>("value");
615 QTest::addColumn<uint>("result");
616 QTest::addColumn<bool>("valueOK");
618 QTest::newRow( "int" ) << QVariant( 123 ) << (uint)123 << true;
619 QTest::newRow( "char" ) << QVariant::fromValue('a') << uint('a') << true;
620 signed char signedChar = 12;
621 QTest::newRow( "signed char" ) << QVariant::fromValue(signedChar) << uint(12) << true;
622 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (uint)3 << true;
623 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (uint)3 << true;
624 QTest::newRow( "uint" ) << QVariant( 123u ) << (uint)123 << true;
625 QTest::newRow( "int-string" ) << QVariant( QString("123") ) << (uint)123 << true;
626 QTest::newRow( "string" ) << QVariant( QString("Unicode String") ) << (uint)0 << false;
627 QTest::newRow( "string2" ) << QVariant( QString("4") ) << (uint)4 << true;
628 QTest::newRow( "longlong0" ) << QVariant( (qlonglong)34 ) << (uint)34 << true;
629 QTest::newRow( "longlong1" ) << QVariant( intMax1 ) << (uint)INT_MIN << true;
630 QTest::newRow( "ulonglong0" ) << QVariant( (qulonglong)34 ) << (uint)34 << true;
631 QTest::newRow( "ulonglong1" ) << QVariant( uintMax1 ) << (uint)0 << true;
632 QTest::newRow( "negativeint" ) << QVariant( -123 ) << (uint)-123 << true;
633 QTest::newRow( "negativedouble" ) << QVariant( -3.1415927 ) << (uint)-3 << true;
634 QTest::newRow( "negativefloat" ) << QVariant( -3.1415927f ) << (uint)-3 << true;
635 QTest::newRow( "negativeint-string" ) << QVariant( QString("-123") ) << (uint)0 << false;
636 QTest::newRow( "negativelonglong0" ) << QVariant( (qlonglong)-34 ) << (uint)-34 << true;
637 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << uint('a') << true;
638 QByteArray bytearray(4, ' ');
643 QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (uint)4321 << true;
646 void tst_QVariant::toUInt()
648 QFETCH( QVariant, value );
649 QFETCH( uint, result );
650 QFETCH( bool, valueOK );
651 QVERIFY( value.isValid() );
652 QVERIFY( value.canConvert( QVariant::UInt ) );
655 uint i = value.toUInt( &ok );
656 QVERIFY( ok == valueOK );
657 QCOMPARE( i, result );
661 void tst_QVariant::toSize_data()
663 QTest::addColumn<QVariant>("value");
664 QTest::addColumn<QSize>("result");
665 QTest::newRow( "qsizef4" ) << QVariant( QSizeF(4, 2) ) << QSize(4, 2);
666 QTest::newRow( "qsizef1" ) << QVariant( QSizeF(0, 0) ) << QSize(0, 0);
667 QTest::newRow( "qsizef2" ) << QVariant( QSizeF(-5, -1) ) << QSize(-5, -1);
668 QTest::newRow( "qsizef3" ) << QVariant( QSizeF() ) << QSize();
671 void tst_QVariant::toSize()
673 QFETCH( QVariant, value );
674 QFETCH( QSize, result );
675 QVERIFY( value.isValid() );
676 QVERIFY( value.canConvert( QVariant::Size ) );
678 QSize i = value.toSize();
679 QCOMPARE( i, result );
682 void tst_QVariant::toSizeF_data()
684 QTest::addColumn<QVariant>("value");
685 QTest::addColumn<QSizeF>("result");
686 QTest::newRow( "qsize1" ) << QVariant( QSize(0, 0) ) << QSizeF(0, 0);
687 QTest::newRow( "qsize2" ) << QVariant( QSize(-5, -1) ) << QSizeF(-5, -1);
688 QTest::newRow( "qsize3" ) << QVariant( QSize() ) << QSizeF();
689 QTest::newRow( "qsize4" ) << QVariant(QSize(4,2)) << QSizeF(4,2);
692 void tst_QVariant::toSizeF()
694 QFETCH( QVariant, value );
695 QFETCH( QSizeF, result );
696 QVERIFY( value.isValid() );
697 QVERIFY( value.canConvert( QVariant::SizeF ) );
699 QSizeF i = value.toSizeF();
700 QCOMPARE( i, result );
703 void tst_QVariant::toLine_data()
705 QTest::addColumn<QVariant>("value");
706 QTest::addColumn<QLine>("result");
707 QTest::newRow( "linef1" ) << QVariant( QLineF(1, 2, 3, 4) ) << QLine(1, 2, 3, 4);
708 QTest::newRow( "linef2" ) << QVariant( QLineF(-1, -2, -3, -4) ) << QLine(-1, -2, -3, -4);
709 QTest::newRow( "linef3" ) << QVariant( QLineF(0, 0, 0, 0) ) << QLine(0, 0, 0, 0);
710 QTest::newRow( "linef4" ) << QVariant( QLineF() ) << QLine();
713 void tst_QVariant::toLine()
715 QFETCH( QVariant, value );
716 QFETCH( QLine, result );
717 QVERIFY( value.isValid() );
718 QVERIFY( value.canConvert( QVariant::Line ) );
720 QLine i = value.toLine();
721 QCOMPARE( i, result );
724 void tst_QVariant::toLineF_data()
726 QTest::addColumn<QVariant>("value");
727 QTest::addColumn<QLineF>("result");
728 QTest::newRow( "line1" ) << QVariant( QLine(-1, -2, -3, -4) ) << QLineF(-1, -2, -3, -4);
729 QTest::newRow( "line2" ) << QVariant( QLine(0, 0, 0, 0) ) << QLineF(0, 0, 0, 0);
730 QTest::newRow( "line3" ) << QVariant( QLine() ) << QLineF();
731 QTest::newRow( "line4" ) << QVariant( QLine(1, 2, 3, 4) ) << QLineF(1, 2, 3, 4);
734 void tst_QVariant::toLineF()
736 QFETCH( QVariant, value );
737 QFETCH( QLineF, result );
738 QVERIFY( value.isValid() );
739 QVERIFY( value.canConvert( QVariant::LineF ) );
741 QLineF i = value.toLineF();
742 QCOMPARE( i, result );
745 void tst_QVariant::toPoint_data()
747 QTest::addColumn<QVariant>("value");
748 QTest::addColumn<QPoint>("result");
749 QTest::newRow( "pointf1" ) << QVariant( QPointF(4, 2) ) << QPoint(4, 2);
750 QTest::newRow( "pointf2" ) << QVariant( QPointF(0, 0) ) << QPoint(0, 0);
751 QTest::newRow( "pointf3" ) << QVariant( QPointF(-4, -2) ) << QPoint(-4, -2);
752 QTest::newRow( "pointf4" ) << QVariant( QPointF() ) << QPoint();
753 QTest::newRow( "pointf5" ) << QVariant( QPointF(-4.2f, -2.3f) ) << QPoint(-4, -2);
756 void tst_QVariant::toPoint()
758 QFETCH( QVariant, value );
759 QFETCH( QPoint, result );
760 QVERIFY( value.isValid() );
761 QVERIFY( value.canConvert( QVariant::Point ) );
762 QPoint i = value.toPoint();
763 QCOMPARE( i, result );
766 void tst_QVariant::toRect_data()
768 QTest::addColumn<QVariant>("value");
769 QTest::addColumn<QRect>("result");
770 QTest::newRow( "rectf1" ) << QVariant(QRectF(1, 2, 3, 4)) << QRect(1, 2, 3, 4);
771 QTest::newRow( "rectf2" ) << QVariant(QRectF(0, 0, 0, 0)) << QRect(0, 0, 0, 0);
772 QTest::newRow( "rectf3" ) << QVariant(QRectF(-1, -2, -3, -4)) << QRect(-1, -2, -3, -4);
773 QTest::newRow( "rectf4" ) << QVariant(QRectF(-1.3f, 0, 3.9f, -4.0)) << QRect(-1, 0, 4, -4);
774 QTest::newRow( "rectf5" ) << QVariant(QRectF()) << QRect();
777 void tst_QVariant::toRect()
779 QFETCH( QVariant, value );
780 QFETCH( QRect, result );
781 QVERIFY( value.isValid() );
782 QVERIFY( value.canConvert( QVariant::Rect ) );
783 QRect i = value.toRect();
784 QCOMPARE( i, result );
787 void tst_QVariant::toChar_data()
789 QTest::addColumn<QVariant>("value");
790 QTest::addColumn<QChar>("result");
791 QTest::newRow( "longlong" ) << QVariant(qlonglong('6')) << QChar('6');
792 QTest::newRow( "ulonglong" ) << QVariant(qulonglong('7')) << QChar('7');
795 void tst_QVariant::toChar()
797 QFETCH( QVariant, value );
798 QFETCH( QChar, result );
799 QVERIFY( value.isValid() );
800 QVERIFY( value.canConvert( QVariant::Char ) );
802 QChar i = value.toChar();
803 QCOMPARE( i, result );
806 void tst_QVariant::toBool_data()
808 QTest::addColumn<QVariant>("value");
809 QTest::addColumn<bool>("result");
811 QTest::newRow( "int0" ) << QVariant( 0 ) << false;
812 QTest::newRow( "int1" ) << QVariant( 123 ) << true;
813 QTest::newRow( "uint0" ) << QVariant( 0u ) << false;
814 QTest::newRow( "uint1" ) << QVariant( 123u ) << true;
815 QTest::newRow( "double0" ) << QVariant( 0.0 ) << false;
816 QTest::newRow( "float0" ) << QVariant( 0.0f ) << false;
817 QTest::newRow( "double1" ) << QVariant( 3.1415927 ) << true;
818 QTest::newRow( "float1" ) << QVariant( 3.1415927f ) << true;
819 QTest::newRow( "string0" ) << QVariant( QString("3") ) << true;
820 QTest::newRow( "string1" ) << QVariant( QString("true") ) << true;
821 QTest::newRow( "string2" ) << QVariant( QString("0") ) << false;
822 QTest::newRow( "string3" ) << QVariant( QString("fAlSe") ) << false;
823 QTest::newRow( "longlong0" ) << QVariant( (qlonglong)0 ) << false;
824 QTest::newRow( "longlong1" ) << QVariant( (qlonglong)1 ) << true;
825 QTest::newRow( "ulonglong0" ) << QVariant( (qulonglong)0 ) << false;
826 QTest::newRow( "ulonglong1" ) << QVariant( (qulonglong)1 ) << true;
827 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << true;
828 QTest::newRow( "Null_QChar" ) << QVariant(QChar(0)) << false;
831 void tst_QVariant::toBool()
833 QFETCH( QVariant, value );
834 QFETCH( bool, result );
835 QVERIFY( value.isValid() );
836 QVERIFY( value.canConvert( QVariant::Bool ) );
838 bool i = value.toBool();
839 QCOMPARE( i, result );
842 void tst_QVariant::toPointF_data()
844 QTest::addColumn<QVariant>("value");
845 QTest::addColumn<QPointF>("result");
847 QTest::newRow( "QPoint" ) << QVariant( QPointF( 19, 84) ) << QPointF( 19, 84 );
850 void tst_QVariant::toPointF()
852 QFETCH( QVariant, value );
853 QFETCH( QPointF, result );
854 QVERIFY( value.isValid() );
855 QVERIFY( value.canConvert( QVariant::PointF ) );
856 QPointF d = value.toPointF();
857 QCOMPARE( d, result );
860 void tst_QVariant::toRectF_data()
862 QTest::addColumn<QVariant>("value");
863 QTest::addColumn<QRectF>("result");
865 QRect r( 1, 9, 8, 4 );
866 QRectF rf( 1.0, 9.0, 8.0, 4.0 );
867 QTest::newRow( "QRect" ) << QVariant( r ) << rf;
870 void tst_QVariant::toRectF()
872 QFETCH( QVariant, value );
873 QFETCH( QRectF, result );
874 QVERIFY( value.isValid() );
875 QVERIFY( value.canConvert( QVariant::RectF ) );
876 QRectF d = value.toRectF();
877 QCOMPARE( d, result );
880 void tst_QVariant::toColor_data()
882 QTest::addColumn<QVariant>("value");
883 QTest::addColumn<QColor>("result");
886 QTest::newRow( "string" ) << QVariant( QString( "red" ) ) << c;
887 QTest::newRow( "solid brush" ) << QVariant( QBrush(c) ) << c;
890 void tst_QVariant::toColor()
892 QFETCH( QVariant, value );
893 QFETCH( QColor, result );
894 QVERIFY( value.isValid() );
895 QVERIFY( value.canConvert( QVariant::Color ) );
896 QColor d = qvariant_cast<QColor>(value);
897 QCOMPARE( d, result );
900 void tst_QVariant::toPixmap_data()
902 QTest::addColumn<QVariant>("value");
903 QTest::addColumn<QPixmap>("result");
907 QTest::newRow( "image" ) << QVariant( pm ) << pm;
911 QTest::newRow( "bitmap" ) << QVariant( bm ) << QPixmap(bm);
914 void tst_QVariant::toPixmap()
916 QFETCH( QVariant, value );
917 QFETCH( QPixmap, result );
918 QVERIFY( value.isValid() );
919 QVERIFY( value.canConvert( QVariant::Pixmap ) );
920 QPixmap d = qvariant_cast<QPixmap>(value);
921 QCOMPARE( d, result );
924 void tst_QVariant::toImage_data()
926 QTest::addColumn<QVariant>("value");
927 QTest::addColumn<QImage>("result");
929 QImage im(30, 30, QImage::Format_ARGB32);
931 QTest::newRow( "image" ) << QVariant( im ) << im;
934 void tst_QVariant::toImage()
936 QFETCH( QVariant, value );
937 QFETCH( QImage, result );
938 QVERIFY( value.isValid() );
939 QVERIFY( value.canConvert( QVariant::Image ) );
940 QImage d = qvariant_cast<QImage>(value);
941 QCOMPARE( d, result );
944 void tst_QVariant::toBrush_data()
946 QTest::addColumn<QVariant>("value");
947 QTest::addColumn<QBrush>("result");
950 QTest::newRow( "color" ) << QVariant( c ) << QBrush(c);
953 QTest::newRow( "pixmap" ) << QVariant( pm ) << QBrush(pm);
956 void tst_QVariant::toBrush()
958 QFETCH( QVariant, value );
959 QFETCH( QBrush, result );
960 QVERIFY( value.isValid() );
961 QVERIFY( value.canConvert( QVariant::Brush ) );
962 QBrush d = qvariant_cast<QBrush>(value);
963 QCOMPARE( d, result );
966 void tst_QVariant::toFont_data()
968 QTest::addColumn<QVariant>("value");
969 QTest::addColumn<QFont>("result");
971 QFont f("times",12,-1,false);
972 QTest::newRow( "string" ) << QVariant( QString( "times,12,-1,5,50,0,0,0,0,0" ) ) << f;
975 void tst_QVariant::toFont()
977 QFETCH( QVariant, value );
978 QFETCH( QFont, result );
979 QVERIFY( value.isValid() );
980 QVERIFY( value.canConvert( QVariant::Font ) );
981 QFont d = qvariant_cast<QFont>(value);
982 QCOMPARE( d, result );
985 void tst_QVariant::toKeySequence_data()
987 QTest::addColumn<QVariant>("value");
988 QTest::addColumn<QKeySequence>("result");
991 QTest::newRow( "int" ) << QVariant( 67108929 ) << QKeySequence( Qt::CTRL + Qt::Key_A );
994 QTest::newRow( "qstring" )
995 << QVariant( QString( "Ctrl+A" ) )
996 << QKeySequence( Qt::CTRL + Qt::Key_A );
999 void tst_QVariant::toKeySequence()
1001 QFETCH( QVariant, value );
1002 QFETCH( QKeySequence, result );
1003 QVERIFY( value.isValid() );
1004 QVERIFY( value.canConvert( QVariant::KeySequence ) );
1005 QKeySequence d = qvariant_cast<QKeySequence>(value);
1006 QCOMPARE( d, result );
1009 void tst_QVariant::toDouble_data()
1011 QTest::addColumn<QVariant>("value");
1012 QTest::addColumn<double>("result");
1013 QTest::addColumn<bool>("valueOK");
1015 QByteArray bytearray(4, ' ');
1020 QTest::newRow( "bytearray" ) << QVariant( bytearray ) << 32.1 << true;
1023 void tst_QVariant::toDouble()
1025 QFETCH( QVariant, value );
1026 QFETCH( double, result );
1027 QFETCH( bool, valueOK );
1028 QVERIFY( value.isValid() );
1029 QVERIFY( value.canConvert( QVariant::Double ) );
1031 double d = value.toDouble( &ok );
1032 QCOMPARE( d, result );
1033 QVERIFY( ok == valueOK );
1036 void tst_QVariant::toLongLong_data()
1038 QTest::addColumn<QVariant>("value");
1039 QTest::addColumn<qlonglong>("result");
1040 QTest::addColumn<bool>("valueOK");
1042 QTest::newRow( "int0" ) << QVariant( 123 ) << (qlonglong)123 << true;
1043 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (qlonglong)3 << true;
1044 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (qlonglong)3 << true;
1045 QTest::newRow( "uint" ) << QVariant( 123u ) << (qlonglong)123 << true;
1046 QTest::newRow( "int-string" ) << QVariant( QString("123") )
1047 << (qlonglong)123 << true;
1048 QTest::newRow( "string" ) << QVariant( QString("Unicode fun") ) << (qlonglong)0
1050 QTest::newRow( "longlong" ) << QVariant( intMax1 ) << intMax1 << true;
1051 QTest::newRow( "ulonglong" ) << QVariant( uintMax1 ) << (qlonglong)uintMax1 << true;
1052 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << qlonglong('a') << true;
1053 QByteArray bytearray(4, ' ');
1058 QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (qlonglong) 3200 << true;
1061 void tst_QVariant::toLongLong()
1063 QFETCH( QVariant, value );
1064 QFETCH( qlonglong, result );
1065 QFETCH( bool, valueOK );
1066 QVERIFY( value.isValid() );
1067 QVERIFY( value.canConvert( QVariant::LongLong ) );
1069 qlonglong ll = value.toLongLong( &ok );
1070 QCOMPARE( ll, result );
1071 QVERIFY( ok == valueOK );
1074 void tst_QVariant::toULongLong_data()
1076 QTest::addColumn<QVariant>("value");
1077 QTest::addColumn<qulonglong>("result");
1078 QTest::addColumn<bool>("valueOK");
1080 QTest::newRow( "int0" ) << QVariant( 123 ) << (qulonglong)123 << true;
1081 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (qulonglong)3 << true;
1082 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (qulonglong)3 << true;
1083 QTest::newRow( "uint" ) << QVariant( 123u ) << (qulonglong)123 << true;
1084 QTest::newRow( "int-string" ) << QVariant( QString("123") )
1085 << (qulonglong)123 << true;
1086 QTest::newRow( "string" ) << QVariant( QString("Unicode fun") ) << (qulonglong)0
1088 QTest::newRow( "ulonglong-string" ) << QVariant( QString("18446744073709551615") )
1089 << Q_UINT64_C(18446744073709551615)
1091 QTest::newRow( "bytaa-string" ) << QVariant( QString("18446744073709551615") )
1092 << Q_UINT64_C(18446744073709551615)
1094 QTest::newRow( "longlong" ) << QVariant( intMax1 ) << (qulonglong)intMax1 << true;
1095 QTest::newRow( "ulonglong" ) << QVariant( uintMax1 ) << uintMax1 << true;
1096 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << qulonglong('a') << true;
1097 QByteArray bytearray(4, ' ');
1102 QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (qulonglong) 3201 << true;
1105 void tst_QVariant::toULongLong()
1107 QFETCH( QVariant, value );
1108 QFETCH( qulonglong, result );
1109 QFETCH( bool, valueOK );
1110 QVERIFY( value.isValid() );
1111 QVERIFY( value.canConvert( QVariant::ULongLong ) );
1113 qulonglong ll = value.toULongLong( &ok );
1114 QCOMPARE( ll, result );
1115 QVERIFY( ok == valueOK );
1118 void tst_QVariant::toByteArray_data()
1120 QTest::addColumn<QVariant>("value");
1121 QTest::addColumn<QByteArray>("result");
1123 QByteArray ba(5, ' ');
1130 QByteArray variantBa = ba;
1132 QTest::newRow( "qbytearray" ) << QVariant( variantBa ) << ba;
1133 QTest::newRow( "int" ) << QVariant( -123 ) << QByteArray( "-123" );
1134 QTest::newRow( "uint" ) << QVariant( (uint)123 ) << QByteArray( "123" );
1135 QTest::newRow( "double" ) << QVariant( 123.456 ) << QByteArray( "123.456" );
1136 QTest::newRow( "float" ) << QVariant( 123.456f ) << QByteArray( "123.456" );
1137 QTest::newRow( "longlong" ) << QVariant( (qlonglong)34 ) << QByteArray( "34" );
1138 QTest::newRow( "ulonglong" ) << QVariant( (qulonglong)34 ) << QByteArray( "34" );
1141 void tst_QVariant::toByteArray()
1143 QFETCH( QVariant, value );
1144 QFETCH( QByteArray, result );
1145 QVERIFY( value.isValid() );
1146 QVERIFY( value.canConvert( QVariant::ByteArray ) );
1147 QByteArray ba = value.toByteArray();
1148 QCOMPARE( ba, result );
1151 void tst_QVariant::toString_data()
1153 QTest::addColumn<QVariant>("value");
1154 QTest::addColumn<QString>("result");
1156 QTest::newRow( "qstring" ) << QVariant( QString( "Test" ) ) << QString( "Test" );
1157 QTest::newRow( "charstar" ) << QVariant(QLatin1String("Test")) << QString("Test");
1158 QTest::newRow( "qbytearray") << QVariant( QByteArray( "Test\0" ) ) << QString( "Test" );
1159 QTest::newRow( "int" ) << QVariant( -123 ) << QString( "-123" );
1160 QTest::newRow( "uint" ) << QVariant( (uint)123 ) << QString( "123" );
1161 QTest::newRow( "double" ) << QVariant( 123.456 ) << QString( "123.456" );
1162 QTest::newRow( "float" ) << QVariant( 123.456f ) << QString( "123.456" );
1163 QTest::newRow( "bool" ) << QVariant( true ) << QString( "true" );
1164 QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 1, 1 ) ) << QString( "2002-01-01" );
1165 QTest::newRow( "qtime" ) << QVariant( QTime( 12, 34, 56 ) ) << QString( "12:34:56" );
1166 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 1, 1 ), QTime( 12, 34, 56 ) ) ) << QString( "2002-01-01T12:34:56" );
1167 QTest::newRow( "qkeysequence" ) << QVariant::fromValue( QKeySequence( Qt::CTRL + Qt::Key_A ) )
1169 << QString( "Ctrl+A" );
1171 << QString(QChar(0x2318)) + "A";
1174 QFont font( "times", 12 );
1175 QTest::newRow( "qfont" ) << QVariant::fromValue( font ) << QString("times,12,-1,5,50,0,0,0,0,0");
1176 QTest::newRow( "qcolor" ) << QVariant::fromValue( QColor( 10, 10, 10 ) ) << QString( "#0a0a0a" );
1177 QTest::newRow( "llong" ) << QVariant( (qlonglong)Q_INT64_C(123456789012) ) <<
1178 QString( "123456789012" );
1181 void tst_QVariant::toString()
1183 QFETCH( QVariant, value );
1184 QFETCH( QString, result );
1185 QVERIFY( value.isValid() );
1186 QVERIFY( value.canConvert( QVariant::String ) );
1187 QString str = value.toString();
1188 QCOMPARE( str, result );
1191 void tst_QVariant::toDate_data()
1193 QTest::addColumn<QVariant>("value");
1194 QTest::addColumn<QDate>("result");
1196 QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 10, 10 ) ) << QDate( 2002, 10, 10 );
1197 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) ) ) << QDate( 2002, 10, 10 );
1198 QTest::newRow( "qstring" ) << QVariant( QString( "2002-10-10" ) ) << QDate( 2002, 10, 10 );
1201 void tst_QVariant::toDate()
1203 QFETCH( QVariant, value );
1204 QFETCH( QDate, result );
1205 QVERIFY( value.isValid() );
1206 QVERIFY( value.canConvert( QVariant::Date ) );
1207 QCOMPARE( value.toDate(), result );
1210 void tst_QVariant::toTime_data()
1212 QTest::addColumn<QVariant>("value");
1213 QTest::addColumn<QTime>("result");
1215 QTest::newRow( "qtime" ) << QVariant( QTime( 12, 34, 56 ) ) << QTime( 12, 34, 56 );
1216 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) ) ) << QTime( 12, 34, 56 );
1217 QTest::newRow( "qstring" ) << QVariant( QString( "12:34:56" ) ) << QTime( 12, 34, 56 );
1220 void tst_QVariant::toTime()
1222 QFETCH( QVariant, value );
1223 QFETCH( QTime, result );
1224 QVERIFY( value.isValid() );
1225 QVERIFY( value.canConvert( QVariant::Time ) );
1226 QCOMPARE( value.toTime(), result );
1229 void tst_QVariant::toDateTime_data()
1231 QTest::addColumn<QVariant>("value");
1232 QTest::addColumn<QDateTime>("result");
1234 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) ) )
1235 << QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) );
1236 QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 10, 10 ) ) << QDateTime( QDate( 2002, 10, 10 ), QTime( 0, 0, 0 ) );
1237 QTest::newRow( "qstring" ) << QVariant( QString( "2002-10-10T12:34:56" ) ) << QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) );
1240 void tst_QVariant::toDateTime()
1242 QFETCH( QVariant, value );
1243 QFETCH( QDateTime, result );
1244 QVERIFY( value.isValid() );
1245 QVERIFY( value.canConvert( QVariant::DateTime ) );
1246 QCOMPARE( value.toDateTime(), result );
1249 void tst_QVariant::toLocale()
1252 QLocale loc = variant.toLocale();
1253 variant = QLocale::system();
1254 loc = variant.toLocale();
1257 void tst_QVariant::toRegExp()
1260 QRegExp rx = variant.toRegExp();
1261 variant = QRegExp("foo");
1262 rx = variant.toRegExp();
1265 void tst_QVariant::toRegularExpression()
1268 QRegularExpression re = variant.toRegularExpression();
1269 QCOMPARE(re, QRegularExpression());
1271 variant = QRegularExpression("abc.*def");
1272 re = variant.toRegularExpression();
1273 QCOMPARE(re, QRegularExpression("abc.*def"));
1275 variant = QVariant::fromValue(QRegularExpression("[ab]\\w+"));
1276 re = variant.value<QRegularExpression>();
1277 QCOMPARE(re, QRegularExpression("[ab]\\w+"));
1280 void tst_QVariant::matrix()
1283 QMatrix matrix = qvariant_cast<QMatrix>(variant);
1284 QVERIFY(matrix.isIdentity());
1285 variant.setValue(QMatrix().rotate(90));
1286 QCOMPARE(QMatrix().rotate(90), qvariant_cast<QMatrix>(variant));
1288 void *mmatrix = QMetaType::create(QVariant::Matrix, 0);
1290 QMetaType::destroy(QVariant::Matrix, mmatrix);
1293 void tst_QVariant::matrix4x4()
1296 QMatrix4x4 matrix = qvariant_cast<QMatrix4x4>(variant);
1297 QVERIFY(matrix.isIdentity());
1300 variant.setValue(m);
1301 QCOMPARE(m, qvariant_cast<QMatrix4x4>(variant));
1303 void *mmatrix = QMetaType::create(QVariant::Matrix4x4, 0);
1305 QMetaType::destroy(QVariant::Matrix4x4, mmatrix);
1308 void tst_QVariant::transform()
1311 QTransform matrix = qvariant_cast<QTransform>(variant);
1312 QVERIFY(matrix.isIdentity());
1313 variant.setValue(QTransform().rotate(90));
1314 QCOMPARE(QTransform().rotate(90), qvariant_cast<QTransform>(variant));
1316 void *mmatrix = QMetaType::create(QVariant::Transform, 0);
1318 QMetaType::destroy(QVariant::Transform, mmatrix);
1322 void tst_QVariant::vector2D()
1325 QVector2D vector = qvariant_cast<QVector2D>(variant);
1326 QVERIFY(vector.isNull());
1327 variant.setValue(QVector2D(0.1, 0.2));
1328 QCOMPARE(QVector2D(0.1, 0.2), qvariant_cast<QVector2D>(variant));
1330 void *pvector = QMetaType::create(QVariant::Vector2D, 0);
1332 QMetaType::destroy(QVariant::Vector2D, pvector);
1335 void tst_QVariant::vector3D()
1338 QVector3D vector = qvariant_cast<QVector3D>(variant);
1339 QVERIFY(vector.isNull());
1340 variant.setValue(QVector3D(0.1, 0.2, 0.3));
1341 QCOMPARE(QVector3D(0.1, 0.2, 0.3), qvariant_cast<QVector3D>(variant));
1343 void *pvector = QMetaType::create(QVariant::Vector3D, 0);
1345 QMetaType::destroy(QVariant::Vector3D, pvector);
1348 void tst_QVariant::vector4D()
1351 QVector4D vector = qvariant_cast<QVector4D>(variant);
1352 QVERIFY(vector.isNull());
1353 variant.setValue(QVector4D(0.1, 0.2, 0.3, 0.4));
1354 QCOMPARE(QVector4D(0.1, 0.2, 0.3, 0.4), qvariant_cast<QVector4D>(variant));
1356 void *pvector = QMetaType::create(QVariant::Vector4D, 0);
1358 QMetaType::destroy(QVariant::Vector4D, pvector);
1361 void tst_QVariant::quaternion()
1364 QQuaternion quaternion = qvariant_cast<QQuaternion>(variant);
1365 QVERIFY(quaternion.isIdentity());
1366 variant.setValue(QQuaternion(0.1, 0.2, 0.3, 0.4));
1367 QCOMPARE(QQuaternion(0.1, 0.2, 0.3, 0.4), qvariant_cast<QQuaternion>(variant));
1369 void *pquaternion = QMetaType::create(QVariant::Quaternion, 0);
1370 QVERIFY(pquaternion);
1371 QMetaType::destroy(QVariant::Quaternion, pquaternion);
1374 struct CustomStreamableClass
1377 bool operator==(const CustomStreamableClass& other) const
1379 return i == other.i;
1382 Q_DECLARE_METATYPE(CustomStreamableClass);
1384 QDataStream &operator<<(QDataStream &out, const CustomStreamableClass &myObj)
1386 return out << myObj.i;
1389 QDataStream &operator>>(QDataStream &in, CustomStreamableClass &myObj)
1391 return in >> myObj.i;
1394 void tst_QVariant::writeToReadFromDataStream_data()
1396 qRegisterMetaTypeStreamOperators<CustomStreamableClass>();
1398 QTest::addColumn<QVariant>("writeVariant");
1399 QTest::addColumn<bool>("isNull");
1401 QVariantList valuelist;
1402 valuelist << QVariant( 1 ) << QVariant( QString("Two") ) << QVariant( 3.45 );
1403 QVariant var(valuelist);
1404 QTest::newRow( "list_valid" ) << var << false;
1407 QTest::newRow( "invalid" ) << QVariant() << true;
1408 QTest::newRow( "bitarray_invalid" ) << QVariant( QBitArray() ) << true;
1409 QBitArray bitarray( 3 );
1413 QTest::newRow( "bitarray_valid" ) << QVariant( bitarray ) << false;
1414 QTest::newRow( "bytearray_invalid" ) << QVariant( QByteArray() ) << true;
1415 QTest::newRow( "int_invalid") << QVariant(QVariant::Int) << true;
1416 QByteArray bytearray(5, ' ');
1421 bytearray[4] = '\0';
1422 QTest::newRow( "bytearray_valid" ) << QVariant( bytearray ) << false;
1423 QTest::newRow( "bitmap_invalid" ) << QVariant::fromValue( QBitmap() ) << true;
1424 QBitmap bitmap( 10, 10 );
1425 bitmap.fill( Qt::red );
1426 QTest::newRow( "bitmap_valid" ) << QVariant::fromValue( bitmap ) << false;
1427 QTest::newRow( "brush_valid" ) << QVariant::fromValue( QBrush( Qt::red ) ) << false;
1428 QTest::newRow( "color_valid" ) << QVariant::fromValue( QColor( Qt::red ) ) << false;
1429 #ifndef QT_NO_CURSOR
1430 QTest::newRow( "cursor_valid" ) << QVariant::fromValue( QCursor( Qt::PointingHandCursor ) ) << false;
1432 QTest::newRow( "date_invalid" ) << QVariant( QDate() ) << true;
1433 QTest::newRow( "date_valid" ) << QVariant( QDate( 2002, 07, 06 ) ) << false;
1434 QTest::newRow( "datetime_invalid" ) << QVariant( QDateTime() ) << true;
1435 QTest::newRow( "datetime_valid" ) << QVariant( QDateTime( QDate( 2002, 07, 06 ), QTime( 14, 0, 0 ) ) ) << false;
1436 QTest::newRow( "double_valid" ) << QVariant( 123.456 ) << false;
1437 QTest::newRow( "float_valid" ) << QVariant( 123.456f ) << false;
1438 QTest::newRow( "font_valid" ) << QVariant::fromValue( QFont( "times", 12 ) ) << false;
1439 QTest::newRow( "pixmap_invalid" ) << QVariant::fromValue( QPixmap() ) << true;
1440 QPixmap pixmap( 10, 10 );
1441 pixmap.fill( Qt::red );
1442 QTest::newRow( "pixmap_valid" ) << QVariant::fromValue( pixmap ) << false;
1443 // QTest::newRow( "iconset_valid" ) << QVariant( QIcon( pixmap ) ) << false;
1444 QTest::newRow( "image_invalid" ) << QVariant::fromValue( QImage() ) << true;
1445 QTest::newRow( "keysequence_valid" ) << QVariant::fromValue( QKeySequence( Qt::CTRL + Qt::Key_A ) ) << false;
1446 QTest::newRow( "int_valid" ) << QVariant( -123 ) << false;
1448 vMap.insert( "int", QVariant( 1 ) );
1449 vMap.insert( "string", QVariant( QString("Two") ) );
1450 vMap.insert( "double", QVariant( 3.45 ) );
1451 vMap.insert( "float", QVariant( 3.45f ) );
1452 QTest::newRow( "map_valid" ) << QVariant( vMap ) << false;
1453 QTest::newRow( "palette_valid" ) << QVariant::fromValue(QPalette(QColor("turquoise"))) << false;
1454 QTest::newRow( "pen_valid" ) << QVariant::fromValue( QPen( Qt::red ) ) << false;
1455 QTest::newRow( "pointarray_invalid" ) << QVariant::fromValue( QPolygon() ) << true;
1456 QTest::newRow( "pointarray_valid" ) << QVariant::fromValue( QPolygon( QRect( 10, 10, 20, 20 ) ) ) << false;
1457 QTest::newRow( "region_invalid" ) << QVariant::fromValue( QRegion() ) << true;
1458 QTest::newRow( "region_valid" ) << QVariant::fromValue( QRegion( 10, 10, 20, 20 ) ) << false;
1459 QTest::newRow( "point_invalid" ) << QVariant::fromValue( QPoint() ) << true;
1460 QTest::newRow( "point_valid" ) << QVariant::fromValue( QPoint( 10, 10 ) ) << false;
1461 QTest::newRow( "rect_invalid" ) << QVariant( QRect() ) << true;
1462 QTest::newRow( "rect_valid" ) << QVariant( QRect( 10, 10, 20, 20 ) ) << false;
1463 QTest::newRow( "size_invalid" ) << QVariant( QSize( 0, 0 ) ) << true;
1464 QTest::newRow( "size_valid" ) << QVariant( QSize( 10, 10 ) ) << false;
1465 QTest::newRow( "string_invalid" ) << QVariant( QString() ) << true;
1466 QTest::newRow( "string_valid" ) << QVariant( QString( "Test" ) ) << false;
1467 QStringList stringlist;
1468 stringlist << "One" << "Two" << "Three";
1469 QTest::newRow( "stringlist_valid" ) << QVariant( stringlist ) << false;
1470 QTest::newRow( "time_invalid" ) << QVariant( QTime() ) << true;
1471 QTest::newRow( "time_valid" ) << QVariant( QTime( 14, 0, 0 ) ) << false;
1472 QTest::newRow( "uint_valid" ) << QVariant( (uint)123 ) << false;
1473 QTest::newRow( "qchar" ) << QVariant(QChar('a')) << false;
1474 QTest::newRow( "qchar_null" ) << QVariant(QChar(0)) << true;
1475 QTest::newRow( "regexp" ) << QVariant(QRegExp("foo", Qt::CaseInsensitive)) << false;
1476 QTest::newRow( "regexp_empty" ) << QVariant(QRegExp()) << false;
1477 QTest::newRow( "regularexpression" ) << QVariant(QRegularExpression("abc.*def")) << false;
1478 QTest::newRow( "regularexpression_empty" ) << QVariant(QRegularExpression()) << false;
1480 // types known to QMetaType, but not part of QVariant::Type
1481 QTest::newRow("QMetaType::Long invalid") << QVariant(QMetaType::Long, (void *) 0) << false;
1483 QTest::newRow("QMetaType::Long") << QVariant(QMetaType::Long, &longInt) << false;
1484 QTest::newRow("QMetaType::Short invalid") << QVariant(QMetaType::Short, (void *) 0) << false;
1486 QTest::newRow("QMetaType::Short") << QVariant(QMetaType::Short, &shortInt) << false;
1487 QTest::newRow("QMetaType::Char invalid") << QVariant(QMetaType::Char, (void *) 0) << false;
1489 QTest::newRow("QMetaType::Char") << QVariant(QMetaType::Char, &ch) << false;
1490 QTest::newRow("QMetaType::ULong invalid") << QVariant(QMetaType::ULong, (void *) 0) << false;
1491 ulong ulongInt = 1ul;
1492 QTest::newRow("QMetaType::ULong") << QVariant(QMetaType::ULong, &ulongInt) << false;
1493 QTest::newRow("QMetaType::UShort invalid") << QVariant(QMetaType::UShort, (void *) 0) << false;
1494 ushort ushortInt = 1u;
1495 QTest::newRow("QMetaType::UShort") << QVariant(QMetaType::UShort, &ushortInt) << false;
1496 QTest::newRow("QMetaType::UChar invalid") << QVariant(QMetaType::UChar, (void *) 0) << false;
1498 QTest::newRow("QMetaType::UChar") << QVariant(QMetaType::UChar, &uch) << false;
1499 QTest::newRow("QMetaType::Float invalid") << QVariant(QMetaType::Float, (void *) 0) << false;
1501 QTest::newRow("QMetaType::Float") << QVariant(QMetaType::Float, &f) << false;
1502 CustomStreamableClass custom = {123};
1503 QTest::newRow("Custom type") << QVariant::fromValue(custom) << false;
1506 void tst_QVariant::writeToReadFromDataStream()
1508 QFETCH( QVariant, writeVariant );
1509 QFETCH( bool, isNull );
1512 QDataStream writeStream( &data, QIODevice::WriteOnly );
1513 writeStream << writeVariant;
1515 QVariant readVariant;
1516 QDataStream readStream( &data, QIODevice::ReadOnly );
1517 readStream >> readVariant;
1518 QVERIFY( readVariant.isNull() == isNull );
1519 // Best way to confirm the readVariant contains the same data?
1520 // Since only a few won't match since the serial numbers are different
1521 // I won't bother adding another bool in the data test.
1522 const int writeType = writeVariant.userType();
1523 if (writeType == qMetaTypeId<CustomStreamableClass>())
1524 QCOMPARE(qvariant_cast<CustomStreamableClass>(readVariant), qvariant_cast<CustomStreamableClass>(writeVariant));
1525 else if ( writeType != QVariant::Invalid && writeType != QVariant::Bitmap && writeType != QVariant::Pixmap
1526 && writeType != QVariant::Image) {
1527 switch (writeType) {
1529 QCOMPARE( readVariant, writeVariant );
1532 // compare types know by QMetaType but not QVariant (QVariant::operator==() knows nothing about them)
1533 case QMetaType::Long:
1534 QCOMPARE(qvariant_cast<long>(readVariant), qvariant_cast<long>(writeVariant));
1536 case QMetaType::ULong:
1537 QCOMPARE(qvariant_cast<ulong>(readVariant), qvariant_cast<ulong>(writeVariant));
1539 case QMetaType::Short:
1540 QCOMPARE(qvariant_cast<short>(readVariant), qvariant_cast<short>(writeVariant));
1542 case QMetaType::UShort:
1543 QCOMPARE(qvariant_cast<ushort>(readVariant), qvariant_cast<ushort>(writeVariant));
1545 case QMetaType::Char:
1546 QCOMPARE(qvariant_cast<char>(readVariant), qvariant_cast<char>(writeVariant));
1548 case QMetaType::UChar:
1549 QCOMPARE(qvariant_cast<uchar>(readVariant), qvariant_cast<uchar>(writeVariant));
1551 case QMetaType::Float:
1553 // the uninitialized float can be NaN (observed on Windows Mobile 5 ARMv4i)
1554 float readFloat = qvariant_cast<float>(readVariant);
1555 float writtenFloat = qvariant_cast<float>(writeVariant);
1556 QVERIFY(qIsNaN(readFloat) == qIsNaN(writtenFloat));
1557 if (!qIsNaN(readFloat))
1558 QVERIFY(readFloat == writtenFloat);
1565 void tst_QVariant::writeToReadFromOldDataStream()
1567 QVariant writeVariant = QString("hello");
1570 QDataStream writeStream(&data, QIODevice::WriteOnly);
1571 writeStream.setVersion(QDataStream::Qt_2_1);
1572 writeStream << writeVariant;
1574 QVariant readVariant;
1575 QDataStream readStream(&data, QIODevice::ReadOnly);
1576 readStream.setVersion(QDataStream::Qt_2_1);
1577 readStream >> readVariant;
1579 QCOMPARE(writeVariant.userType(), readVariant.userType());
1580 QCOMPARE(writeVariant, readVariant);
1583 void tst_QVariant::checkDataStream()
1585 const int typeId = QMetaType::LastCoreType + 1;
1586 QVERIFY(!QMetaType::isRegistered(typeId));
1588 QByteArray errorMessage("Trying to construct an instance of an invalid type, type id: ");
1589 errorMessage.append(QString::number(typeId, 10));
1591 QTest::ignoreMessage(QtWarningMsg, errorMessage.constData());
1592 QByteArray settingsHex("000000");
1593 settingsHex.append(QString::number(typeId, 16));
1594 settingsHex.append("ffffffffff");
1595 const QByteArray settings = QByteArray::fromHex(settingsHex);
1596 QDataStream in(settings);
1599 // the line below has been left out for now since the data stream
1600 // is not necessarily considered corrupt when an invalid QVariant is
1601 // constructed. However, it might be worth considering changing that behavior
1603 // QCOMPARE(in.status(), QDataStream::ReadCorruptData);
1604 QCOMPARE(v.type(), QVariant::Invalid);
1607 void tst_QVariant::operator_eq_eq_data()
1609 QTest::addColumn<QVariant>("left");
1610 QTest::addColumn<QVariant>("right");
1611 QTest::addColumn<bool>("equal"); // left == right ?
1614 QVariant i0( int(0) );
1615 QVariant i1( int(1) );
1617 QTest::newRow( "invinv" ) << inv << inv << true;
1619 QTest::newRow( "int1int1" ) << i1 << i1 << true;
1620 QTest::newRow( "int1int0" ) << i1 << i0 << false;
1621 QTest::newRow( "nullint" ) << i0 << QVariant(QVariant::Int) << false;
1623 // LongLong and ULongLong
1624 QVariant ll1( (qlonglong)1 );
1625 QVariant lln2( (qlonglong)-2 );
1626 QVariant ull1( (qulonglong)1 );
1627 QVariant ull3( (qulonglong)3 );
1628 QTest::newRow( "ll1ll1" ) << ll1 << ll1 << true;
1629 QTest::newRow( "ll1lln2" ) << ll1 << lln2 << false;
1630 QTest::newRow( "ll1ull1" ) << ull1 << ull1 << true;
1631 QTest::newRow( "ll1i1" ) << ull1 << i1 << true;
1632 QTest::newRow( "ull1ull1" ) << ull1 << ull1 << true;
1633 QTest::newRow( "ull1i1" ) << ull1 << ull1 << true;
1636 QVariant mIntString(QByteArray("-42"));
1637 QVariant mIntQString(QString("-42"));
1639 QVariant mUInt(42u);
1640 QVariant mUIntString(QByteArray("42"));
1641 QVariant mUIntQString(QString("42"));
1643 QVariant mDouble(42.11);
1644 QVariant mDoubleString(QByteArray("42.11"));
1645 QVariant mDoubleQString(QString("42.11"));
1647 QVariant mFloat(42.11f);
1648 QVariant mFloatString(QByteArray("42.11"));
1649 QVariant mFloatQString(QString("42.11"));
1651 QVariant mLongLong((qlonglong)-42);
1652 QVariant mLongLongString(QByteArray("-42"));
1653 QVariant mLongLongQString(QString("-42"));
1655 QVariant mULongLong((qulonglong)42);
1656 QVariant mULongLongString(QByteArray("42"));
1657 QVariant mULongLongQString(QString("42"));
1659 QVariant mBool(false);
1660 QVariant mBoolString(QByteArray("false"));
1661 QVariant mBoolQString(QString("false"));
1663 QTest::newRow( "double_int" ) << QVariant(42.0) << QVariant(42) << true;
1664 QTest::newRow( "float_int" ) << QVariant(42.f) << QVariant(42) << true;
1665 QTest::newRow( "mInt_mIntString" ) << mInt << mIntString << true;
1666 QTest::newRow( "mIntString_mInt" ) << mIntString << mInt << true;
1667 QTest::newRow( "mInt_mIntQString" ) << mInt << mIntQString << true;
1668 QTest::newRow( "mIntQString_mInt" ) << mIntQString << mInt << true;
1670 QTest::newRow( "mUInt_mUIntString" ) << mUInt << mUIntString << true;
1671 QTest::newRow( "mUIntString_mUInt" ) << mUIntString << mUInt << true;
1672 QTest::newRow( "mUInt_mUIntQString" ) << mUInt << mUIntQString << true;
1673 QTest::newRow( "mUIntQString_mUInt" ) << mUIntQString << mUInt << true;
1675 QTest::newRow( "mDouble_mDoubleString" ) << mDouble << mDoubleString << true;
1676 QTest::newRow( "mDoubleString_mDouble" ) << mDoubleString << mDouble << true;
1677 QTest::newRow( "mDouble_mDoubleQString" ) << mDouble << mDoubleQString << true;
1678 QTest::newRow( "mDoubleQString_mDouble" ) << mDoubleQString << mDouble << true;
1680 QTest::newRow( "mFloat_mFloatString" ) << mFloat << mFloatString << true;
1681 QTest::newRow( "mFloatString_mFloat" ) << mFloatString << mFloat << true;
1682 QTest::newRow( "mFloat_mFloatQString" ) << mFloat << mFloatQString << true;
1683 QTest::newRow( "mFloatQString_mFloat" ) << mFloatQString << mFloat << true;
1685 QTest::newRow( "mLongLong_mLongLongString" ) << mLongLong << mLongLongString << true;
1686 QTest::newRow( "mLongLongString_mLongLong" ) << mLongLongString << mLongLong << true;
1687 QTest::newRow( "mLongLong_mLongLongQString" ) << mLongLong << mLongLongQString << true;
1688 QTest::newRow( "mLongLongQString_mLongLong" ) << mLongLongQString << mLongLong << true;
1690 QTest::newRow( "mULongLong_mULongLongString" ) << mULongLong << mULongLongString << true;
1691 QTest::newRow( "mULongLongString_mULongLong" ) << mULongLongString << mULongLong << true;
1692 QTest::newRow( "mULongLong_mULongLongQString" ) << mULongLong << mULongLongQString << true;
1693 QTest::newRow( "mULongLongQString_mULongLong" ) << mULongLongQString << mULongLong << true;
1695 QTest::newRow( "mBool_mBoolString" ) << mBool << mBoolString << true;
1696 QTest::newRow( "mBoolString_mBool" ) << mBoolString << mBool << true;
1697 QTest::newRow( "mBool_mBoolQString" ) << mBool << mBoolQString << true;
1698 QTest::newRow( "mBoolQString_mBool" ) << mBoolQString << mBool << true;
1700 QTest::newRow("ba2qstring") << QVariant(QLatin1String("hallo")) << QVariant(QString("hallo")) << true;
1701 QTest::newRow("qstring2ba") << QVariant(QString("hallo")) << QVariant(QLatin1String("hallo")) << true;
1702 QTest::newRow("char_char") << QVariant(QChar('a')) << QVariant(QChar('a')) << true;
1703 QTest::newRow("char_char2") << QVariant(QChar('a')) << QVariant(QChar('b')) << false;
1705 QTest::newRow("invalidConversion") << QVariant(QString("bubu")) << QVariant(0) << false;
1706 QTest::newRow("invalidConversionR") << QVariant(0) << QVariant(QString("bubu")) << false;
1707 // ### many other combinations missing
1710 QMap<QString, QVariant> map1;
1711 map1.insert( "X", 1 );
1713 QMap<QString, QVariant> map2;
1714 map2.insert( "Y", 1 );
1716 QTest::newRow("TwoItemsInEqual") << QVariant(map1) << QVariant(map2) << false;
1721 QMap<QString, QVariant> map1;
1722 map1.insert( "X", 1 );
1724 QMap<QString, QVariant> map2;
1725 map2.insert( "X", 1 );
1727 QTest::newRow("TwoItemsEqual") << QVariant(map1) << QVariant(map2) << true;
1731 QMap<QString, QVariant> map1;
1732 map1.insert( "X", 1 );
1734 QMap<QString, QVariant> map2;
1736 QTest::newRow("PopulatedEmptyMap") << QVariant(map1) << QVariant(map2) << false;
1740 QMap<QString, QVariant> map1;
1742 QMap<QString, QVariant> map2;
1743 map2.insert( "X", 1 );
1745 QTest::newRow("EmptyPopulatedMap") << QVariant(map1) << QVariant(map2) << false;
1749 QMap<QString, QVariant> map1;
1750 map1.insert( "X", 1 );
1751 map1.insert( "Y", 1 );
1753 QMap<QString, QVariant> map2;
1754 map2.insert( "X", 1 );
1756 QTest::newRow("FirstLarger") << QVariant(map1) << QVariant(map2) << false;
1760 QMap<QString, QVariant> map1;
1761 map1.insert( "X", 1 );
1763 QMap<QString, QVariant> map2;
1764 map2.insert( "X", 1 );
1765 map2.insert( "Y", 1 );
1767 QTest::newRow("SecondLarger") << QVariant(map1) << QVariant(map2) << false;
1770 // same thing with hash
1772 QHash<QString, QVariant> hash1;
1773 hash1.insert( "X", 1 );
1775 QHash<QString, QVariant> hash2;
1776 hash2.insert( "Y", 1 );
1778 QTest::newRow("HashTwoItemsInEqual") << QVariant(hash1) << QVariant(hash2) << false;
1783 QHash<QString, QVariant> hash1;
1784 hash1.insert( "X", 1 );
1786 QHash<QString, QVariant> hash2;
1787 hash2.insert( "X", 1 );
1789 QTest::newRow("HashTwoItemsEqual") << QVariant(hash1) << QVariant(hash2) << true;
1793 QHash<QString, QVariant> hash1;
1794 hash1.insert( "X", 1 );
1796 QHash<QString, QVariant> hash2;
1798 QTest::newRow("HashPopulatedEmptyHash") << QVariant(hash1) << QVariant(hash2) << false;
1802 QHash<QString, QVariant> hash1;
1804 QHash<QString, QVariant> hash2;
1805 hash2.insert( "X", 1 );
1807 QTest::newRow("EmptyPopulatedHash") << QVariant(hash1) << QVariant(hash2) << false;
1811 QHash<QString, QVariant> hash1;
1812 hash1.insert( "X", 1 );
1813 hash1.insert( "Y", 1 );
1815 QHash<QString, QVariant> hash2;
1816 hash2.insert( "X", 1 );
1818 QTest::newRow("HashFirstLarger") << QVariant(hash1) << QVariant(hash2) << false;
1822 QHash<QString, QVariant> hash1;
1823 hash1.insert( "X", 1 );
1825 QHash<QString, QVariant> hash2;
1826 hash2.insert( "X", 1 );
1827 hash2.insert( "Y", 1 );
1829 QTest::newRow("HashSecondLarger") << QVariant(hash1) << QVariant(hash2) << false;
1833 void tst_QVariant::operator_eq_eq()
1835 QFETCH( QVariant, left );
1836 QFETCH( QVariant, right );
1837 QFETCH( bool, equal );
1838 QEXPECT_FAIL("nullint", "See QTBUG-22933", Continue);
1839 QCOMPARE( left == right, equal );
1842 void tst_QVariant::operator_eq_eq_rhs()
1850 /* This should _not_ compile */
1854 QVERIFY(dt == list);
1858 void tst_QVariant::typeName_data()
1860 QTest::addColumn<int>("type");
1861 QTest::addColumn<QByteArray>("res");
1862 QTest::newRow("0") << int(QVariant::Invalid) << QByteArray("");
1863 QTest::newRow("1") << int(QVariant::Map) << QByteArray("QVariantMap");
1864 QTest::newRow("2") << int(QVariant::List) << QByteArray("QVariantList");
1865 QTest::newRow("3") << int(QVariant::String) << QByteArray("QString");
1866 QTest::newRow("4") << int(QVariant::StringList) << QByteArray("QStringList");
1867 QTest::newRow("5") << int(QVariant::Font) << QByteArray("QFont");
1868 QTest::newRow("6") << int(QVariant::Pixmap) << QByteArray("QPixmap");
1869 QTest::newRow("7") << int(QVariant::Brush) << QByteArray("QBrush");
1870 QTest::newRow("8") << int(QVariant::Rect) << QByteArray("QRect");
1871 QTest::newRow("9") << int(QVariant::Size) << QByteArray("QSize");
1872 QTest::newRow("10") << int(QVariant::Color) << QByteArray("QColor");
1873 QTest::newRow("11") << int(QVariant::Palette) << QByteArray("QPalette");
1874 QTest::newRow("12") << int(QVariant::Point) << QByteArray("QPoint");
1875 QTest::newRow("13") << int(QVariant::Image) << QByteArray("QImage");
1876 QTest::newRow("14") << int(QVariant::Int) << QByteArray("int");
1877 QTest::newRow("15") << int(QVariant::UInt) << QByteArray("uint");
1878 QTest::newRow("16") << int(QVariant::Bool) << QByteArray("bool");
1879 QTest::newRow("17") << int(QVariant::Double) << QByteArray("double");
1880 QTest::newRow("18") << int(QMetaType::Float) << QByteArray("float");
1881 QTest::newRow("19") << int(QVariant::Polygon) << QByteArray("QPolygon");
1882 QTest::newRow("20") << int(QVariant::Region) << QByteArray("QRegion");
1883 QTest::newRow("21") << int(QVariant::Bitmap) << QByteArray("QBitmap");
1884 QTest::newRow("22") << int(QVariant::Cursor) << QByteArray("QCursor");
1885 QTest::newRow("23") << int(QVariant::SizePolicy) << QByteArray("QSizePolicy");
1886 QTest::newRow("24") << int(QVariant::Date) << QByteArray("QDate");
1887 QTest::newRow("25") << int(QVariant::Time) << QByteArray("QTime");
1888 QTest::newRow("26") << int(QVariant::DateTime) << QByteArray("QDateTime");
1889 QTest::newRow("27") << int(QVariant::ByteArray) << QByteArray("QByteArray");
1890 QTest::newRow("28") << int(QVariant::BitArray) << QByteArray("QBitArray");
1891 QTest::newRow("29") << int(QVariant::KeySequence) << QByteArray("QKeySequence");
1892 QTest::newRow("30") << int(QVariant::Pen) << QByteArray("QPen");
1893 QTest::newRow("31") << int(QVariant::LongLong) << QByteArray("qlonglong");
1894 QTest::newRow("32") << int(QVariant::ULongLong) << QByteArray("qulonglong");
1895 QTest::newRow("33") << int(QVariant::Char) << QByteArray("QChar");
1896 QTest::newRow("34") << int(QVariant::Url) << QByteArray("QUrl");
1897 QTest::newRow("35") << int(QVariant::TextLength) << QByteArray("QTextLength");
1898 QTest::newRow("36") << int(QVariant::TextFormat) << QByteArray("QTextFormat");
1899 QTest::newRow("37") << int(QVariant::Locale) << QByteArray("QLocale");
1900 QTest::newRow("38") << int(QVariant::LineF) << QByteArray("QLineF");
1901 QTest::newRow("39") << int(QVariant::RectF) << QByteArray("QRectF");
1902 QTest::newRow("40") << int(QVariant::PointF) << QByteArray("QPointF");
1903 QTest::newRow("41") << int(QVariant::RegExp) << QByteArray("QRegExp");
1904 QTest::newRow("43") << int(QVariant::Matrix) << QByteArray("QMatrix");
1905 QTest::newRow("44") << int(QVariant::Transform) << QByteArray("QTransform");
1906 QTest::newRow("45") << int(QVariant::Hash) << QByteArray("QVariantHash");
1907 QTest::newRow("46") << int(QVariant::Matrix4x4) << QByteArray("QMatrix4x4");
1908 QTest::newRow("47") << int(QVariant::Vector2D) << QByteArray("QVector2D");
1909 QTest::newRow("48") << int(QVariant::Vector3D) << QByteArray("QVector3D");
1910 QTest::newRow("49") << int(QVariant::Vector4D) << QByteArray("QVector4D");
1911 QTest::newRow("50") << int(QVariant::Quaternion) << QByteArray("QQuaternion");
1912 QTest::newRow("51") << int(QVariant::RegularExpression) << QByteArray("QRegularExpression");
1915 void tst_QVariant::typeName()
1917 QFETCH( int, type );
1918 QFETCH( QByteArray, res );
1919 QCOMPARE(QString::fromLatin1(QVariant::typeToName((QVariant::Type)type)),
1920 QString::fromLatin1(res.constData()));
1923 // test nameToType as well
1924 void tst_QVariant::typeToName()
1927 QCOMPARE( QVariant::typeToName( v.type() ), (const char*)0 ); // Invalid
1928 // assumes that QVariant::Type contains consecutive values
1930 int max = QVariant::LastGuiType;
1931 for ( int t = 1; t <= max; t++ ) {
1932 const char *n = QVariant::typeToName( (QVariant::Type)t );
1934 QCOMPARE( int(QVariant::nameToType( n )), t );
1937 QCOMPARE(QVariant::typeToName(QVariant::Int), "int");
1938 // not documented but we return 0 if the type is out of range
1939 // by testing this we catch cases where QVariant is extended
1940 // but type_map is not updated accordingly
1941 QCOMPARE( QVariant::typeToName( QVariant::Type(max+1) ), (char*)0 );
1942 // invalid type names
1943 QVERIFY( QVariant::nameToType( 0 ) == QVariant::Invalid );
1944 QVERIFY( QVariant::nameToType( "" ) == QVariant::Invalid );
1945 QVERIFY( QVariant::nameToType( "foo" ) == QVariant::Invalid );
1947 QCOMPARE(QVariant::nameToType("UserType"), QVariant::Invalid);
1949 // We don't support these old (Qt3) types anymore.
1950 QCOMPARE(QVariant::nameToType("QIconSet"), QVariant::Invalid);
1951 QCOMPARE(QVariant::nameToType("Q3CString"), QVariant::Invalid);
1952 QCOMPARE(QVariant::nameToType("Q_LLONG"), QVariant::Invalid);
1953 QCOMPARE(QVariant::nameToType("Q_ULLONG"), QVariant::Invalid);
1956 void tst_QVariant::streamInvalidVariant()
1962 QVariant writeVariant;
1963 QVariant readVariant;
1965 QVERIFY( writeVariant.type() == QVariant::Invalid );
1968 QDataStream writeStream( &data, QIODevice::WriteOnly );
1969 writeStream << writeX << writeVariant << writeY;
1971 QDataStream readStream( &data, QIODevice::ReadOnly );
1972 readStream >> readX >> readVariant >> readY;
1974 QVERIFY( readX == writeX );
1975 // Two invalid QVariant's aren't necessarily the same, so == will
1976 // return false if one is invalid, so check the type() instead
1977 QVERIFY( readVariant.type() == QVariant::Invalid );
1978 QVERIFY( readY == writeY );
1981 static int instanceCount = 0;
1985 MyType(int n = 0, const char *t=0): number(n), text(t)
1989 MyType(const MyType &other)
1990 : number(other.number), text(other.text)
2002 Q_DECLARE_METATYPE(MyType)
2003 Q_DECLARE_METATYPE(MyType*)
2005 void tst_QVariant::userType()
2008 MyType data(1, "eins");
2009 MyType data2(2, "zwei");
2013 userVar.setValue(data);
2015 QCOMPARE(userVar.type(), QVariant::UserType);
2016 QCOMPARE(userVar.userType(), qMetaTypeId<MyType>());
2017 QCOMPARE(userVar.typeName(), "MyType");
2018 QVERIFY(!userVar.isNull());
2019 QVERIFY(!userVar.canConvert(QVariant::String));
2021 QVariant userVar2(userVar);
2022 QVERIFY(userVar == userVar2);
2024 userVar2.setValue(data2);
2025 QVERIFY(userVar != userVar2);
2027 const MyType *varData = static_cast<const MyType *>(userVar.constData());
2029 QCOMPARE(varData->number, data.number);
2030 QCOMPARE(varData->text, data.text);
2033 userVar3.setValue(data2);
2035 userVar3 = userVar2;
2036 QVERIFY(userVar2 == userVar3);
2038 // At this point all QVariants got destroyed but we have 2 MyType instances.
2039 QCOMPARE(instanceCount, 2);
2042 userVar.setValue(&data);
2044 QCOMPARE(userVar.type(), QVariant::UserType);
2045 QCOMPARE(userVar.userType(), qMetaTypeId<MyType*>());
2046 QCOMPARE(userVar.typeName(), "MyType*");
2047 QVERIFY(!userVar.isNull());
2048 QVERIFY(!userVar.canConvert(QVariant::String));
2050 QVariant userVar2(userVar);
2051 QVERIFY(userVar == userVar2);
2053 userVar2.setValue(&data2);
2054 QVERIFY(userVar != userVar2);
2056 MyType * const*varData = reinterpret_cast<MyType *const *>(userVar.constData());
2058 QCOMPARE(*varData, &data);
2061 userVar3.setValue(&data2);
2063 /* This check is correct now. userVar2 contains a pointer to data2 and so
2065 QVERIFY(userVar2 == userVar3);
2067 userVar3 = userVar2;
2068 QVERIFY(userVar2 == userVar3);
2071 QCOMPARE(instanceCount, 2);
2073 myCarrier.setValue(data);
2074 QCOMPARE(instanceCount, 3);
2076 QVariant second = myCarrier;
2077 QCOMPARE(instanceCount, 3);
2079 QCOMPARE(instanceCount, 4);
2081 QCOMPARE(instanceCount, 3);
2083 MyType data3(0, "null");
2084 data3 = qvariant_cast<MyType>(myCarrier);
2085 QCOMPARE(data3.number, 1);
2086 QCOMPARE(data3.text, (const char *)"eins");
2088 QCOMPARE(instanceCount, 4);
2094 const MyType data(3, "drei");
2097 myCarrier.setValue(data);
2098 QCOMPARE(myCarrier.typeName(), "MyType");
2100 const MyType data2 = qvariant_cast<MyType>(myCarrier);
2101 QCOMPARE(data2.number, 3);
2102 QCOMPARE(data2.text, (const char *)"drei");
2109 myCarrier.setValue(s);
2110 QCOMPARE((int)qvariant_cast<short>(myCarrier), 42);
2114 qlonglong ll = Q_INT64_C(42);
2117 myCarrier.setValue(ll);
2118 QCOMPARE(qvariant_cast<int>(myCarrier), 42);
2121 // At this point all QVariants got destroyed and MyType objects too.
2122 QCOMPARE(instanceCount, 0);
2130 Q_DECLARE_METATYPE(MyTypePOD)
2132 void tst_QVariant::podUserType()
2138 QVariant pod_as_variant = QVariant::fromValue(pod);
2139 MyTypePOD pod2 = qvariant_cast<MyTypePOD>(pod_as_variant);
2141 QCOMPARE(pod.a, pod2.a);
2142 QCOMPARE(pod.b, pod2.b);
2144 pod_as_variant.setValue(pod);
2145 pod2 = qvariant_cast<MyTypePOD>(pod_as_variant);
2147 QCOMPARE(pod.a, pod2.a);
2148 QCOMPARE(pod.b, pod2.b);
2151 void tst_QVariant::basicUserType()
2156 v = QVariant(QMetaType::Int, &i);
2158 QCOMPARE(v.type(), QVariant::Int);
2159 QCOMPARE(v.toInt(), 7);
2163 v = QVariant(QMetaType::QString, &s);
2165 QCOMPARE(v.type(), QVariant::String);
2166 QCOMPARE(v.toString(), QString("foo"));
2170 v = QVariant(QMetaType::Double, &d);
2172 QCOMPARE(v.type(), QVariant::Double);
2173 QCOMPARE(v.toDouble(), 4.4);
2177 v = QVariant(QMetaType::Float, &f);
2179 QCOMPARE(v.userType(), int(QMetaType::Float));
2180 QCOMPARE(v.toDouble(), 4.5);
2183 QByteArray ba("bar");
2184 v = QVariant(QMetaType::QByteArray, &ba);
2186 QCOMPARE(v.type(), QVariant::ByteArray);
2187 QCOMPARE(v.toByteArray(), QByteArray("bar"));
2190 void tst_QVariant::data()
2197 QVariant ll = (qlonglong)2;
2198 QVariant ull = (qulonglong)3;
2199 QVariant s(QString("hallo"));
2200 QVariant r(QRect(1,2,3,4));
2204 QCOMPARE(*static_cast<int *>(v.data()), i.toInt());
2208 QCOMPARE(*static_cast<double *>(v.data()), d.toDouble());
2212 QCOMPARE(*static_cast<float *>(v.data()), qvariant_cast<float>(v));
2216 QCOMPARE(*static_cast<qlonglong *>(v.data()), ll.toLongLong());
2220 QCOMPARE(*static_cast<qulonglong *>(v.data()), ull.toULongLong());
2224 QCOMPARE(*static_cast<QString *>(v.data()), s.toString());
2228 QCOMPARE(*static_cast<QRect *>(v.data()), r.toRect());
2231 void tst_QVariant::constData()
2244 QVERIFY(v.constData());
2245 QCOMPARE(*static_cast<const int *>(v.constData()), i);
2248 QVERIFY(v.constData());
2249 QCOMPARE(*static_cast<const double *>(v.constData()), d);
2252 QVERIFY(v.constData());
2253 QCOMPARE(*static_cast<const float *>(v.constData()), f);
2256 QVERIFY(v.constData());
2257 QCOMPARE(*static_cast<const qlonglong *>(v.constData()), ll);
2260 QVERIFY(v.constData());
2261 QCOMPARE(*static_cast<const qulonglong *>(v.constData()), ull);
2264 QVERIFY(v.constData());
2265 QCOMPARE(*static_cast<const QString *>(v.constData()), s);
2268 QVERIFY(v.constData());
2269 QCOMPARE(*static_cast<const QRect *>(v.constData()), r);
2278 Q_DECLARE_METATYPE(Foo)
2280 void tst_QVariant::variant_to()
2289 sl << QLatin1String("blah");
2298 QCOMPARE(qvariant_cast<double>(v1), 4.2);
2299 QCOMPARE(qvariant_cast<float>(v1), 4.2f);
2300 QCOMPARE(qvariant_cast<int>(v2), 5);
2301 QCOMPARE(qvariant_cast<QStringList>(v3), sl);
2302 QCOMPARE(qvariant_cast<QString>(v3), QString::fromLatin1("blah"));
2304 QCOMPARE(qvariant_cast<Foo>(v4).i, 42);
2307 QCOMPARE(qvariant_cast<Foo>(v5).i, 0);
2309 QCOMPARE(qvariant_cast<int>(v1), 4);
2311 QVariant n = QVariant::fromValue<short>(42);
2312 QCOMPARE(qvariant_cast<int>(n), 42);
2313 QCOMPARE(qvariant_cast<uint>(n), 42u);
2314 QCOMPARE(qvariant_cast<double>(n), 42.0);
2315 QCOMPARE(qvariant_cast<float>(n), 42.f);
2316 QCOMPARE(qvariant_cast<short>(n), short(42));
2317 QCOMPARE(qvariant_cast<ushort>(n), ushort(42));
2319 n = QVariant::fromValue(43l);
2320 QCOMPARE(qvariant_cast<int>(n), 43);
2321 QCOMPARE(qvariant_cast<uint>(n), 43u);
2322 QCOMPARE(qvariant_cast<double>(n), 43.0);
2323 QCOMPARE(qvariant_cast<float>(n), 43.f);
2324 QCOMPARE(qvariant_cast<long>(n), 43l);
2326 n = QLatin1String("44");
2327 QCOMPARE(qvariant_cast<int>(n), 44);
2328 QCOMPARE(qvariant_cast<ulong>(n), 44ul);
2329 QCOMPARE(qvariant_cast<float>(n), 44.0f);
2331 QCOMPARE(QVariant::fromValue(0.25f).toDouble(), 0.25);
2334 struct Blah { int i; };
2336 QDataStream& operator>>(QDataStream& s, Blah& c)
2337 { return (s >> c.i); }
2339 QDataStream& operator<<(QDataStream& s, const Blah& c)
2340 { return (s << c.i); }
2342 void tst_QVariant::saveLoadCustomTypes()
2347 int tp = qRegisterMetaType<Blah>("Blah");
2348 QVariant v = QVariant(tp, &i);
2350 qRegisterMetaTypeStreamOperators<Blah>("Blah");
2352 QCOMPARE(v.userType(), tp);
2353 QVERIFY(v.type() == QVariant::UserType);
2355 QDataStream stream(&data, QIODevice::WriteOnly);
2362 QDataStream stream(data);
2366 QCOMPARE(int(v.userType()), QMetaType::type("Blah"));
2367 int value = *(int*)v.constData();
2368 QCOMPARE(value, 42);
2371 void tst_QVariant::url()
2373 QString str("http://qt.nokia.com");
2376 QVariant v(url); //built with a QUrl
2380 QVariant v3(str); //built with a QString
2382 QCOMPARE(v2.toUrl(), url);
2383 QVERIFY(v3.canConvert<QUrl>());
2384 QCOMPARE(v2.toUrl(), v3.toUrl());
2386 QVERIFY(v2.canConvert<QString>());
2387 QCOMPARE(v2.toString(), str);
2388 QCOMPARE(v3.toString(), str);
2391 void tst_QVariant::variantMap()
2393 QMap<QString, QVariant> map;
2397 QVariantMap map2 = qvariant_cast<QVariantMap>(v);
2399 QCOMPARE(map2.value("test").toInt(), 42);
2401 QVariant v2 = QVariant(QMetaType::type("QVariantMap"), &map);
2402 QCOMPARE(qvariant_cast<QVariantMap>(v2).value("test").toInt(), 42);
2404 QVariant v3 = QVariant(QMetaType::type("QMap<QString, QVariant>"), &map);
2405 QCOMPARE(qvariant_cast<QVariantMap>(v3).value("test").toInt(), 42);
2408 void tst_QVariant::variantHash()
2410 QHash<QString, QVariant> hash;
2414 QVariantHash hash2 = qvariant_cast<QVariantHash>(v);
2416 QCOMPARE(hash2.value("test").toInt(), 42);
2418 QVariant v2 = QVariant(QMetaType::type("QVariantHash"), &hash);
2419 QCOMPARE(qvariant_cast<QVariantHash>(v2).value("test").toInt(), 42);
2421 QVariant v3 = QVariant(QMetaType::type("QHash<QString, QVariant>"), &hash);
2422 QCOMPARE(qvariant_cast<QVariantHash>(v3).value("test").toInt(), 42);
2425 void tst_QVariant::invalidQColor() const
2427 QVariant va("An invalid QColor::name() value.");
2428 QVERIFY(va.canConvert(QVariant::Color));
2430 QVERIFY(!va.convert(QVariant::Color));
2432 QVERIFY(!qvariant_cast<QColor>(va).isValid());
2435 class CustomQObject : public QObject {
2438 CustomQObject(QObject *parent = 0) : QObject(parent) {}
2440 Q_DECLARE_METATYPE(CustomQObject*)
2442 class CustomNonQObject { };
2443 Q_DECLARE_METATYPE(CustomNonQObject)
2444 Q_DECLARE_METATYPE(CustomNonQObject*)
2446 void tst_QVariant::cleanupTestCase()
2448 delete customNonQObjectPointer;
2449 qDeleteAll(objectPointerTestData);
2452 void tst_QVariant::qvariant_cast_QObject_data()
2454 QTest::addColumn<QVariant>("data");
2455 QTest::addColumn<bool>("success");
2456 QObject *obj = new QObject;
2457 obj->setObjectName(QString::fromLatin1("Hello"));
2458 QTest::newRow("from QObject") << QVariant(QMetaType::QObjectStar, &obj) << true;
2459 QTest::newRow("from QObject2") << QVariant::fromValue(obj) << true;
2460 QTest::newRow("from String") << QVariant(QLatin1String("1, 2, 3")) << false;
2461 QTest::newRow("from int") << QVariant((int) 123) << false;
2462 CustomQObject *customObject = new CustomQObject(this);
2463 customObject->setObjectName(QString::fromLatin1("Hello"));
2464 QTest::newRow("from Derived QObject") << QVariant::fromValue(customObject) << true;
2465 QTest::newRow("from custom Object") << QVariant::fromValue(CustomNonQObject()) << false;
2467 // Deleted in cleanupTestCase.
2468 customNonQObjectPointer = new CustomNonQObject;
2469 QTest::newRow("from custom ObjectStar") << QVariant::fromValue(customNonQObjectPointer) << false;
2471 // Deleted in cleanupTestCase.
2472 objectPointerTestData.push_back(obj);
2473 objectPointerTestData.push_back(customObject);
2476 void tst_QVariant::qvariant_cast_QObject()
2478 QFETCH(QVariant, data);
2479 QFETCH(bool, success);
2481 QObject *o = qvariant_cast<QObject *>(data);
2482 QCOMPARE(o != 0, success);
2484 QCOMPARE(o->objectName(), QString::fromLatin1("Hello"));
2485 QVERIFY(data.canConvert<QObject*>());
2486 QVERIFY(data.canConvert(QMetaType::QObjectStar));
2487 QVERIFY(data.canConvert(::qMetaTypeId<QObject*>()));
2488 QVERIFY(data.value<QObject*>());
2489 QVERIFY(data.convert(QMetaType::QObjectStar));
2490 QCOMPARE(data.userType(), int(QMetaType::QObjectStar));
2492 QVERIFY(!data.canConvert<QObject*>());
2493 QVERIFY(!data.canConvert(QMetaType::QObjectStar));
2494 QVERIFY(!data.canConvert(::qMetaTypeId<QObject*>()));
2495 QVERIFY(!data.value<QObject*>());
2496 QVERIFY(!data.convert(QMetaType::QObjectStar));
2497 QVERIFY(data.userType() != QMetaType::QObjectStar);
2501 class CustomQObjectDerived : public CustomQObject {
2504 CustomQObjectDerived(QObject *parent = 0) : CustomQObject(parent) {}
2506 Q_DECLARE_METATYPE(CustomQObjectDerived*)
2508 class CustomQObjectDerivedNoMetaType : public CustomQObject {
2511 CustomQObjectDerivedNoMetaType(QObject *parent = 0) : CustomQObject(parent) {}
2514 void tst_QVariant::qvariant_cast_QObject_derived()
2517 CustomQObjectDerivedNoMetaType *object = new CustomQObjectDerivedNoMetaType(this);
2518 QVariant data = QVariant::fromValue(object);
2519 QVERIFY(data.userType() == qMetaTypeId<CustomQObjectDerivedNoMetaType*>());
2520 QCOMPARE(data.value<QObject *>(), object);
2521 QCOMPARE(data.value<CustomQObjectDerivedNoMetaType *>(), object);
2522 QCOMPARE(data.value<CustomQObject *>(), object);
2525 CustomQObjectDerived *object = new CustomQObjectDerived(this);
2526 QVariant data = QVariant::fromValue(object);
2528 QVERIFY(data.userType() == qMetaTypeId<CustomQObjectDerived*>());
2530 QCOMPARE(data.value<QObject *>(), object);
2531 QCOMPARE(data.value<CustomQObjectDerived *>(), object);
2532 QCOMPARE(data.value<CustomQObject *>(), object);
2536 void tst_QVariant::convertToQUint8() const
2540 const qint8 anInt = 32;
2542 /* QVariant(int) gets invoked here so the QVariant has nothing with qint8 to do.
2543 * It's of type QVariant::Int. */
2544 const QVariant v0 = anInt;
2546 QVERIFY(v0.canConvert<qint8>());
2547 QCOMPARE(int(qvariant_cast<qint8>(v0)), 32);
2548 QCOMPARE(int(v0.toInt()), 32);
2549 QCOMPARE(v0.toString(), QString("32"));
2551 QCOMPARE(int(qvariant_cast<qlonglong>(v0)), 32);
2552 QCOMPARE(int(qvariant_cast<char>(v0)), 32);
2553 QCOMPARE(int(qvariant_cast<short>(v0)), 32);
2554 QCOMPARE(int(qvariant_cast<long>(v0)), 32);
2555 QCOMPARE(int(qvariant_cast<float>(v0)), 32);
2556 QCOMPARE(int(qvariant_cast<double>(v0)), 32);
2561 const quint8 anInt = 32;
2562 const QVariant v0 = anInt;
2564 QVERIFY(v0.canConvert<quint8>());
2565 QCOMPARE(int(qvariant_cast<quint8>(v0)), 32);
2566 QCOMPARE(int(v0.toUInt()), 32);
2567 QCOMPARE(v0.toString(), QString("32"));
2572 const qint16 anInt = 32;
2573 const QVariant v0 = anInt;
2575 QVERIFY(v0.canConvert<qint16>());
2576 QCOMPARE(int(qvariant_cast<qint16>(v0)), 32);
2577 QCOMPARE(int(v0.toInt()), 32);
2578 QCOMPARE(v0.toString(), QString("32"));
2583 const quint16 anInt = 32;
2584 const QVariant v0 = anInt;
2586 QVERIFY(v0.canConvert<quint16>());
2587 QCOMPARE(int(qvariant_cast<quint16>(v0)), 32);
2588 QCOMPARE(int(v0.toUInt()), 32);
2589 QCOMPARE(v0.toString(), QString("32"));
2593 void tst_QVariant::comparePointers() const
2601 QVariant v = QVariant::fromValue<void *>(&myClass);
2602 QVariant v2 = QVariant::fromValue<void *>(&myClass);
2608 Q_DECLARE_METATYPE(Data*)
2610 void tst_QVariant::voidStar() const
2617 v1 = QVariant::fromValue(p1);
2621 v2 = QVariant::fromValue(p2);
2625 v2 = QVariant::fromValue(p2);
2629 void tst_QVariant::dataStar() const
2631 qRegisterMetaType<Data*>();
2632 Data *p1 = new Data;
2634 QVariant v1 = QVariant::fromValue(p1);
2635 QCOMPARE(v1.userType(), qMetaTypeId<Data*>());
2636 QCOMPARE(qvariant_cast<Data*>(v1), p1);
2641 v2 = QVariant::fromValue(p1);
2646 void tst_QVariant::canConvertQStringList() const
2648 QFETCH(bool, canConvert);
2649 QFETCH(QStringList, input);
2650 QFETCH(QString, result);
2654 QCOMPARE(v.canConvert(QVariant::String), canConvert);
2655 QCOMPARE(v.toString(), result);
2658 void tst_QVariant::canConvertQStringList_data() const
2660 QTest::addColumn<bool>("canConvert");
2661 QTest::addColumn<QStringList>("input");
2662 QTest::addColumn<QString>("result");
2664 QTest::newRow("An empty list") << false << QStringList() << QString();
2665 QTest::newRow("A single item") << true << QStringList(QLatin1String("foo")) << QString::fromLatin1("foo");
2666 QTest::newRow("A single, but empty item") << true << QStringList(QString()) << QString();
2671 QTest::newRow("Two items") << false << l << QString();
2674 QTest::newRow("Three items") << false << l << QString();
2677 template<typename T> void convertMetaType()
2679 QVERIFY(QVariant::fromValue<T>(10).isValid());
2680 QVERIFY(QVariant::fromValue<T>(10).canConvert(QVariant::Int));
2681 QCOMPARE(QVariant::fromValue<T>(10).toInt(), 10);
2682 QCOMPARE(QVariant::fromValue<T>(10), QVariant::fromValue<T>(10));
2685 #define CONVERT_META_TYPE(Type) \
2686 convertMetaType<Type>(); \
2687 if (QTest::currentTestFailed()) \
2688 QFAIL("convertMetaType<" #Type "> failed");
2690 void tst_QVariant::canConvertMetaTypeToInt() const
2692 CONVERT_META_TYPE(long);
2693 CONVERT_META_TYPE(short);
2694 CONVERT_META_TYPE(short);
2695 CONVERT_META_TYPE(unsigned short);
2696 CONVERT_META_TYPE(ushort);
2697 CONVERT_META_TYPE(ulong);
2698 CONVERT_META_TYPE(unsigned long);
2699 CONVERT_META_TYPE(uchar);
2700 CONVERT_META_TYPE(unsigned char);
2701 CONVERT_META_TYPE(char);
2702 CONVERT_META_TYPE(uint);
2703 CONVERT_META_TYPE(unsigned int);
2706 #undef CONVERT_META_TYPE
2709 These calls should not produce any warnings.
2711 void tst_QVariant::variantToDateTimeWithoutWarnings() const
2714 const QVariant variant(QLatin1String("An invalid QDateTime string"));
2715 const QDateTime dateTime(variant.toDateTime());
2716 QVERIFY(!dateTime.isValid());
2720 QVariant v1(QLatin1String("xyz"));
2721 v1.convert(QVariant::DateTime);
2723 QVariant v2(QLatin1String("xyz"));
2724 QDateTime dt1(v2.toDateTime());
2726 const QVariant v3(QLatin1String("xyz"));
2727 const QDateTime dt2(v3.toDateTime());
2731 void tst_QVariant::invalidDateTime() const
2733 QVariant variant(QString::fromLatin1("Invalid date time string"));
2734 QVERIFY(!variant.toDateTime().isValid());
2735 QVERIFY(!variant.convert(QVariant::DateTime));
2740 MyClass() : myValue(0) {}
2744 Q_DECLARE_METATYPE( MyClass )
2746 void tst_QVariant::loadUnknownUserType()
2748 qRegisterMetaType<MyClass>("MyClass");
2749 QTest::ignoreMessage(QtWarningMsg, "QVariant::load: unable to load type "
2750 + QByteArray::number(qMetaTypeId<MyClass>()) +".");
2751 char data[] = {0, 0, QMetaType::User >> 8 , char(QMetaType::User), 0, 0, 0, 0, 8, 'M', 'y', 'C', 'l', 'a', 's', 's', 0};
2753 QByteArray ba(data, sizeof(data));
2754 QDataStream ds(&ba, QIODevice::ReadOnly);
2757 QCOMPARE(ds.status(), QDataStream::ReadCorruptData);
2760 void tst_QVariant::loadBrokenUserType()
2762 QTest::ignoreMessage(QtWarningMsg, "Trying to construct an instance of an invalid type, type id: 127");
2763 char data[] = {0, 0, 0, 127, 0 };
2765 QByteArray ba(data, sizeof(data));
2766 QDataStream ds(&ba, QIODevice::ReadOnly);
2769 QCOMPARE(ds.status(), QDataStream::Ok);
2772 void tst_QVariant::invalidDate() const
2774 QString foo("Hello");
2775 QVariant variant(foo);
2776 QVERIFY(!variant.convert(QVariant::Date));
2779 QVERIFY(!variant.convert(QVariant::DateTime));
2782 QVERIFY(!variant.convert(QVariant::Time));
2785 QVERIFY(!variant.convert(QVariant::Int));
2788 QVERIFY(!variant.convert(QVariant::Double));
2791 QVERIFY(!variant.convert(QVariant::Type(QMetaType::Float)));
2796 int x,y,z,q,w,e,r,t;
2798 Q_DECLARE_METATYPE(WontCompare);
2800 void tst_QVariant::compareCustomTypes() const
2802 qRegisterMetaType<WontCompare>("WontCompare");
2806 const QVariant variant1(QVariant::fromValue(f1));
2810 const QVariant variant2(QVariant::fromValue(f2));
2812 /* We compare pointers. */
2813 QVERIFY(variant1 != variant2);
2814 QVERIFY(variant1 == variant1);
2815 QVERIFY(variant2 == variant2);
2818 void tst_QVariant::timeToDateTime() const
2820 const QVariant val(QTime::currentTime());
2821 QVERIFY(!val.canConvert(QVariant::DateTime));
2822 QVERIFY(!val.toDateTime().isValid());
2825 struct CustomComparable
2827 CustomComparable(int value = 0) : myValue(value) {}
2830 bool operator==(const CustomComparable &other) const
2831 { return other.myValue == myValue; }
2834 Q_DECLARE_METATYPE(CustomComparable)
2836 void tst_QVariant::copyingUserTypes() const
2840 const CustomComparable userType = CustomComparable(42);
2841 var.setValue(userType);
2844 const CustomComparable copiedType = qvariant_cast<CustomComparable>(varCopy);
2845 QCOMPARE(copiedType, userType);
2846 QCOMPARE(copiedType.myValue, 42);
2849 void tst_QVariant::convertBoolToByteArray() const
2851 QFETCH(QByteArray, input);
2852 QFETCH(bool, canConvert);
2853 QFETCH(bool, value);
2855 const QVariant variant(input);
2857 QCOMPARE(variant.canConvert<bool>(), canConvert);
2860 /* Just call this function so we run the code path. */
2861 QCOMPARE(variant.toBool(), value);
2865 void tst_QVariant::convertBoolToByteArray_data() const
2867 QTest::addColumn<QByteArray>("input");
2868 QTest::addColumn<bool>("canConvert");
2869 QTest::addColumn<bool>("value");
2871 QTest::newRow("false")
2872 << QByteArray("false")
2876 QTest::newRow("FALSE")
2877 << QByteArray("FALSE")
2881 QTest::newRow("falSE")
2882 << QByteArray("FALSE")
2891 QTest::newRow("null QByteArray")
2896 QTest::newRow("any-content")
2897 << QByteArray("any-content")
2901 QTest::newRow("true")
2902 << QByteArray("true")
2906 QTest::newRow("TRUE")
2907 << QByteArray("TRUE")
2911 QTest::newRow("trUE")
2912 << QByteArray("trUE")
2917 void tst_QVariant::convertByteArrayToBool() const
2919 QFETCH(bool, input);
2920 QFETCH(QByteArray, output);
2922 const QVariant variant(input);
2923 QCOMPARE(variant.type(), QVariant::Bool);
2924 QCOMPARE(variant.toBool(), input);
2925 QVERIFY(variant.canConvert<bool>());
2927 QCOMPARE(variant.toByteArray(), output);
2930 void tst_QVariant::convertByteArrayToBool_data() const
2932 QTest::addColumn<bool>("input");
2933 QTest::addColumn<QByteArray>("output");
2935 QTest::newRow("false")
2937 << QByteArray("false");
2939 QTest::newRow("true")
2941 << QByteArray("true");
2946 1. Converting the string "9.9" to int fails. This is the behavior of
2947 toLongLong() and hence also QVariant, since it uses it.
2948 2. Converting the QVariant containing the double 9.9 to int works.
2950 Rationale: "9.9" is not a valid int. However, doubles are by definition not
2951 ints and therefore it makes more sense to perform conversion for those.
2953 void tst_QVariant::toIntFromQString() const
2955 QVariant first("9.9");
2957 QCOMPARE(first.toInt(&ok), 0);
2960 QCOMPARE(QString("9.9").toLongLong(&ok), qlonglong(0));
2964 QCOMPARE(v.toInt(&ok), 10);
2970 1. Conversion from (64 bit) double to int works (no overflow).
2971 2. Same conversion works for QVariant::convert.
2973 Rationale: if 2147483630 is set in float and then converted to int,
2974 there will be overflow and the result will be -2147483648.
2976 void tst_QVariant::toIntFromDouble() const
2978 double d = 2147483630; // max int 2147483647
2979 QVERIFY((int)d == 2147483630);
2982 QVERIFY( var.canConvert( QVariant::Int ) );
2985 int result = var.toInt(&ok);
2987 QVERIFY( ok == true );
2988 QCOMPARE(result, 2147483630);
2991 void tst_QVariant::setValue()
2993 QTransform t; //we just take a value so that we're sure that it will be shared
2995 QVERIFY( v1.isDetached() );
2997 QVERIFY( !v1.isDetached() );
2998 QVERIFY( !v2.isDetached() );
3000 v2.setValue(3); //set an integer value
3002 QVERIFY( v1.isDetached() );
3003 QVERIFY( v2.isDetached() );
3006 void tst_QVariant::numericalConvert()
3008 #if defined(Q_OS_LINUX) && defined(Q_CC_GNU) && !defined(__x86_64__)
3009 QSKIP("Known to fail due to a GCC bug on at least Ubuntu 10.04 32-bit - check QTBUG-8959");
3011 QVariant vfloat(float(5.3));
3012 QVariant vdouble(double(5.3));
3013 QVariant vreal(qreal(5.3));
3014 QVariant vint(int(5));
3015 QVariant vuint(uint(5));
3016 QVariant vshort(short(5));
3017 QVariant vlonglong(quint64(5));
3018 QVariant vstringint(QString::fromLatin1("5"));
3019 QVariant vstring(QString::fromLatin1("5.3"));
3021 QVector<QVariant *> vect;
3022 vect << &vfloat << &vdouble << &vreal << &vint << &vuint << &vshort<< &vlonglong << &vstringint << &vstring;
3024 for(int i = 0; i < vect.size(); i++) {
3026 if (i >= 3 && i <= 7)
3028 QVariant *v = vect.at(i);
3029 QCOMPARE(v->toFloat() , float(num));
3030 QCOMPARE(float(v->toReal()) , float(num));
3031 QCOMPARE(float(v->toDouble()) , float(num));
3033 QCOMPARE(v->toInt() , int(num));
3034 QCOMPARE(v->toUInt() , uint(num));
3035 QCOMPARE(v->toULongLong() , quint64(num));
3037 QCOMPARE(v->toString() , QString::number(num));
3042 template<class T> void playWithVariant(const T &orig, bool isNull, const QString &toString, double toDouble, bool toBool)
3044 QVariant v = QVariant::fromValue(orig);
3045 QVERIFY(v.isValid());
3046 QCOMPARE(v.isNull(), isNull);
3047 QCOMPARE(v.toString(), toString);
3048 QCOMPARE(v.toDouble(), toDouble);
3049 QCOMPARE(v.toBool(), toBool);
3050 QCOMPARE(qvariant_cast<T>(v), orig);
3054 if (!(QTypeInfo<T>::isStatic && QTypeInfo<T>::isComplex)) {
3055 // Type is movable so standard comparison algorithm in QVariant should work
3056 // In a custom type QVariant is not aware of ==operator so it won't be called,
3057 // which may cause problems especially visible when using a not-movable type
3060 QVERIFY(v2.isValid());
3061 QCOMPARE(v2.isNull(), isNull);
3062 QCOMPARE(v2.toString(), toString);
3063 QCOMPARE(v2.toDouble(), toDouble);
3064 QCOMPARE(v2.toBool(), toBool);
3065 QCOMPARE(qvariant_cast<T>(v2), orig);
3071 if (!(QTypeInfo<T>::isStatic && QTypeInfo<T>::isComplex)) {
3072 // Type is movable so standard comparison algorithm in QVariant should work
3073 // In a custom type QVariant is not aware of ==operator so it won't be called,
3074 // which may cause problems especially visible when using a not-movable type
3077 QCOMPARE(qvariant_cast<T>(v2), qvariant_cast<T>(v));
3078 QCOMPARE(v2.toString(), toString);
3079 v3 = QVariant::fromValue(orig);
3081 QVERIFY(v3.isValid());
3082 QCOMPARE(v3.isNull(), isNull);
3083 QCOMPARE(v3.toString(), toString);
3084 QCOMPARE(v3.toDouble(), toDouble);
3085 QCOMPARE(v3.toBool(), toBool);
3086 QCOMPARE(qvariant_cast<T>(v3), qvariant_cast<T>(v));
3089 QVERIFY(v.isValid());
3090 QCOMPARE(v.isNull(), isNull);
3091 QCOMPARE(v.toString(), toString);
3092 QCOMPARE(v.toDouble(), toDouble);
3093 QCOMPARE(v.toBool(), toBool);
3094 QCOMPARE(qvariant_cast<T>(v), orig);
3096 if (qMetaTypeId<T>() != qMetaTypeId<QVariant>()) {
3097 QCOMPARE(v.userType(), qMetaTypeId<T>());
3098 QCOMPARE(QVariant::typeToName(QVariant::Type(v.userType())), QMetaType::typeName(qMetaTypeId<T>()));
3102 #define PLAY_WITH_VARIANT(Orig, IsNull, ToString, ToDouble, ToBool) \
3103 playWithVariant(Orig, IsNull, ToString, ToDouble, ToBool);\
3104 if (QTest::currentTestFailed())\
3105 QFAIL("playWithVariant failed");
3110 bool operator==(const MyPrimitive &o) const
3112 return x == o.x && y == o.y;
3117 Q_DECLARE_TYPEINFO(MyPrimitive, Q_PRIMITIVE_TYPE);
3123 MyData() : ptr(this) {}
3126 if (ptr != this) qWarning("%s: object has moved", Q_FUNC_INFO);
3128 MyData(const MyData& o) : ptr(this)
3130 if (o.ptr != &o) qWarning("%s: other object has moved", Q_FUNC_INFO);
3132 MyData &operator=(const MyData &o)
3134 if (ptr != this) qWarning("%s: object has moved", Q_FUNC_INFO);
3135 if (o.ptr != &o) qWarning("%s: other object has moved", Q_FUNC_INFO);
3138 bool operator==(const MyData &o) const
3140 if (ptr != this) qWarning("%s: object has moved", Q_FUNC_INFO);
3141 if (o.ptr != &o) qWarning("%s: other object has moved", Q_FUNC_INFO);
3150 MyMovable() { v = count++; }
3151 ~MyMovable() { count--; }
3152 MyMovable(const MyMovable &o) : v(o.v) { count++; }
3154 bool operator==(const MyMovable &o) const
3160 int MyMovable::count = 0;
3166 MyNotMovable() : that(this) { count++; }
3167 ~MyNotMovable() { QCOMPARE(that, this); count--; }
3168 MyNotMovable(const MyNotMovable &o) : that(this) { QCOMPARE(o.that, &o); count++; }
3169 MyNotMovable &operator=(const MyNotMovable &o) {
3170 bool ok = that == this && o.that == &o;
3171 if (!ok) qFatal("MyNotMovable has been moved");
3175 //PLAY_WITH_VARIANT test that they are equal, but never that they are not equal
3176 // so it would be fine just to always return true
3177 bool operator==(const MyNotMovable &o) const
3179 bool ok = that == this && o.that == &o;
3180 if (!ok) qFatal("MyNotMovable has been moved");
3185 int MyNotMovable::count = 0;
3187 struct MyShared : QSharedData {
3192 Q_DECLARE_TYPEINFO(MyMovable, Q_MOVABLE_TYPE);
3195 Q_DECLARE_METATYPE(QList<QSize>)
3196 Q_DECLARE_METATYPE(MyPrimitive)
3197 Q_DECLARE_METATYPE(MyData)
3198 Q_DECLARE_METATYPE(MyMovable)
3199 Q_DECLARE_METATYPE(MyNotMovable)
3200 Q_DECLARE_METATYPE(QList<MyPrimitive>)
3201 Q_DECLARE_METATYPE(QList<MyData>)
3202 Q_DECLARE_METATYPE(QList<MyMovable>)
3203 Q_DECLARE_METATYPE(QList<MyNotMovable>)
3204 Q_DECLARE_METATYPE(MyPrimitive *)
3205 Q_DECLARE_METATYPE(MyData *)
3206 Q_DECLARE_METATYPE(MyMovable *)
3207 Q_DECLARE_METATYPE(MyNotMovable *)
3208 Q_DECLARE_METATYPE(QSharedDataPointer<MyShared>)
3211 void tst_QVariant::moreCustomTypes()
3214 QList<QSize> listSize;
3215 PLAY_WITH_VARIANT(listSize, false, QString(), 0, false);
3216 listSize << QSize(4,5) << QSize(89,23) << QSize(5,6);
3217 PLAY_WITH_VARIANT(listSize, false, QString(), 0, false);
3222 PLAY_WITH_VARIANT(str, true, QString(), 0, false);
3223 str = QString::fromLatin1("123456789.123");
3224 PLAY_WITH_VARIANT(str, false, str, 123456789.123, true);
3229 PLAY_WITH_VARIANT(size, false, QString(), 0, false);
3230 PLAY_WITH_VARIANT(QSize(45,78), false, QString(), 0, false);
3235 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3236 PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3238 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3239 l << MyData() << MyData();
3240 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3244 MyPrimitive d = { 4, 5 };
3245 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3246 PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3247 QList<MyPrimitive> l;
3248 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3250 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3255 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3256 PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3258 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3259 l << MyMovable() << d;
3260 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3262 QCOMPARE(MyMovable::count, 0);
3264 QCOMPARE(MyNotMovable::count, 0);
3267 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3268 PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3269 QList<MyNotMovable> l;
3270 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3271 l << MyNotMovable() << d;
3272 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3274 QCOMPARE(MyNotMovable::count, 0);
3277 PLAY_WITH_VARIANT(12.12, false, "12.12", 12.12, true);
3278 PLAY_WITH_VARIANT(12.12f, false, "12.12", 12.12f, true);
3279 PLAY_WITH_VARIANT('a', false, "a", 'a', true);
3280 PLAY_WITH_VARIANT((unsigned char)('a'), false, "a", 'a', true);
3281 PLAY_WITH_VARIANT( quint8(12), false, "\xc", 12, true);
3282 PLAY_WITH_VARIANT( qint8(13), false, "\xd", 13, true);
3283 PLAY_WITH_VARIANT(quint16(14), false, "14", 14, true);
3284 PLAY_WITH_VARIANT( qint16(15), false, "15", 15, true);
3285 PLAY_WITH_VARIANT(quint32(16), false, "16", 16, true);
3286 PLAY_WITH_VARIANT( qint32(17), false, "17", 17, true);
3287 PLAY_WITH_VARIANT(quint64(18), false, "18", 18, true);
3288 PLAY_WITH_VARIANT( qint64(19), false, "19", 19, true);
3289 PLAY_WITH_VARIANT( qint8(-12), false, QLatin1String("\xf4"), -12, true); // qint8 is char, so it gets converted via QChar
3290 PLAY_WITH_VARIANT( qint16(-13), false, "-13", -13, true);
3291 PLAY_WITH_VARIANT( qint32(-14), false, "-14", -14, true);
3292 PLAY_WITH_VARIANT( qint64(-15), false, "-15", -15, true);
3293 PLAY_WITH_VARIANT(quint64(0), false, "0", 0, false);
3294 PLAY_WITH_VARIANT( true, false, "true", 1, true);
3295 PLAY_WITH_VARIANT( false, false, "false", 0, false);
3297 PLAY_WITH_VARIANT(QString("hello\n"), false, "hello\n", 0, true);
3302 PLAY_WITH_VARIANT((void *)(&i), false, QString(), 0, false);
3303 PLAY_WITH_VARIANT((void *)(0), false, QString(), 0, false);
3307 QVariant v1 = QVariant::fromValue(5);
3308 QVariant v2 = QVariant::fromValue(5.0);
3309 QVariant v3 = QVariant::fromValue(quint16(5));
3311 QVariant v5 = QVariant::fromValue(MyPrimitive());
3312 QVariant v6 = QVariant::fromValue(MyMovable());
3313 QVariant v7 = QVariant::fromValue(MyData());
3314 PLAY_WITH_VARIANT(v1, false, "5", 5, true);
3315 PLAY_WITH_VARIANT(v2, false, "5", 5, true);
3316 PLAY_WITH_VARIANT(v3, false, "5", 5, true);
3317 PLAY_WITH_VARIANT(v4, false, "5", 5, true);
3319 PLAY_WITH_VARIANT(v5, false, QString(), 0, false);
3322 QCOMPARE(MyMovable::count, 0);
3324 QSharedDataPointer<MyShared> d(new MyShared);
3325 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3327 QCOMPARE(MyMovable::count, 0);
3330 QList<QList<int> > data;
3331 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3332 data << (QList<int>() << 42);
3333 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3337 QList<QVector<int> > data;
3338 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3339 data << (QVector<int>() << 42);
3340 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3344 QList<QSet<int> > data;
3345 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3346 data << (QSet<int>() << 42);
3347 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3351 QList<QLinkedList<int> > data;
3352 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3353 data << (QLinkedList<int>() << 42);
3354 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3358 void tst_QVariant::movabilityTest()
3360 // This test checks if QVariant is movable even if an internal data is not movable.
3361 QVERIFY(!MyNotMovable::count);
3363 QVariant variant = QVariant::fromValue(MyNotMovable());
3364 QVERIFY(MyNotMovable::count);
3366 // prepare destination memory space to which variant will be moved
3368 QCOMPARE(buffer[0].type(), QVariant::Invalid);
3369 buffer[0].~QVariant();
3371 memcpy(buffer, &variant, sizeof(QVariant));
3372 QCOMPARE(buffer[0].type(), QVariant::UserType);
3373 QCOMPARE(buffer[0].userType(), qMetaTypeId<MyNotMovable>());
3374 MyNotMovable tmp(buffer[0].value<MyNotMovable>());
3376 new (&variant) QVariant();
3378 QVERIFY(!MyNotMovable::count);
3381 void tst_QVariant::variantInVariant()
3384 QCOMPARE(var1.type(), QVariant::Int);
3385 QVariant var2 = var1;
3386 QCOMPARE(var2, var1);
3387 QCOMPARE(var2.type(), QVariant::Int);
3388 QVariant var3 = QVariant::fromValue(var1);
3389 QCOMPARE(var3, var1);
3390 QCOMPARE(var3.type(), QVariant::Int);
3391 QVariant var4 = qvariant_cast<QVariant>(var1);
3392 QCOMPARE(var4, var1);
3393 QCOMPARE(var4.type(), QVariant::Int);
3396 QCOMPARE(var5, var1);
3397 QCOMPARE(var5.type(), QVariant::Int);
3399 var6.setValue(var1);
3400 QCOMPARE(var6, var1);
3401 QCOMPARE(var6.type(), QVariant::Int);
3403 QCOMPARE(QVariant::fromValue(var1), QVariant::fromValue(var2));
3404 QCOMPARE(qvariant_cast<QVariant>(var3), QVariant::fromValue(var4));
3405 QCOMPARE(qvariant_cast<QVariant>(var5), qvariant_cast<QVariant>(var6));
3407 QString str("hello");
3408 QVariant var8 = qvariant_cast<QVariant>(QVariant::fromValue(QVariant::fromValue(str)));
3409 QCOMPARE((int)var8.type(), (int)QVariant::String);
3410 QCOMPARE(qvariant_cast<QString>(QVariant(qvariant_cast<QVariant>(var8))), str);
3412 QVariant var9(qMetaTypeId<QVariant>(), &var1);
3413 QCOMPARE(var9.userType(), qMetaTypeId<QVariant>());
3414 QCOMPARE(qvariant_cast<QVariant>(var9), var1);
3417 void tst_QVariant::colorInteger()
3419 QVariant v = QColor(Qt::red);
3420 QCOMPARE(v.type(), QVariant::Color);
3421 QCOMPARE(v.value<QColor>(), QColor(Qt::red));
3424 QCOMPARE(v.type(), QVariant::Int);
3425 QCOMPARE(v.toInt(), 1000);
3427 v.setValue(QColor(Qt::yellow));
3428 QCOMPARE(v.type(), QVariant::Color);
3429 QCOMPARE(v.value<QColor>(), QColor(Qt::yellow));
3433 Q_DECLARE_OPAQUE_POINTER(Forward*)
3434 Q_DECLARE_METATYPE(Forward*)
3436 void tst_QVariant::forwardDeclare()
3439 QVariant v = QVariant::fromValue(f);
3440 QCOMPARE(qvariant_cast<Forward*>(v), f);
3443 void tst_QVariant::loadQt5Stream_data()
3445 dataStream_data(QDataStream::Qt_5_0);
3448 void tst_QVariant::loadQt5Stream()
3450 loadQVariantFromDataStream(QDataStream::Qt_5_0);
3453 void tst_QVariant::saveQt5Stream_data()
3455 dataStream_data(QDataStream::Qt_5_0);
3458 void tst_QVariant::saveQt5Stream()
3460 saveQVariantFromDataStream(QDataStream::Qt_5_0);
3463 void tst_QVariant::loadQt4Stream_data()
3465 dataStream_data(QDataStream::Qt_4_9);
3468 void tst_QVariant::loadQt4Stream()
3470 loadQVariantFromDataStream(QDataStream::Qt_4_9);
3473 void tst_QVariant::saveQt4Stream_data()
3475 dataStream_data(QDataStream::Qt_4_9);
3478 void tst_QVariant::saveQt4Stream()
3480 saveQVariantFromDataStream(QDataStream::Qt_4_9);
3483 void tst_QVariant::dataStream_data(QDataStream::Version version)
3485 QTest::addColumn<QString>("fileName");
3489 case QDataStream::Qt_4_9:
3490 path = QString::fromLatin1("qt4.9");
3492 case QDataStream::Qt_5_0:
3493 path = QString::fromLatin1("qt5.0");
3499 path = path.prepend(":/stream/").append("/");
3502 foreach (const QFileInfo &fileInfo, dir.entryInfoList(QStringList() << "*.bin")) {
3503 QTest::newRow((path + fileInfo.fileName()).toLatin1()) << fileInfo.filePath();
3509 void tst_QVariant::loadQVariantFromDataStream(QDataStream::Version version)
3511 QFETCH(QString, fileName);
3513 QFile file(fileName);
3514 QVERIFY(file.open(QIODevice::ReadOnly));
3516 QDataStream stream(&file);
3517 stream.setVersion(version);
3520 QVariant loadedVariant;
3521 stream >> typeName >> loadedVariant;
3523 const int id = QMetaType::type(typeName.toLatin1());
3524 if (id == QMetaType::Void) {
3525 // Void type is not supported by QVariant
3529 QVariant constructedVariant(static_cast<QVariant::Type>(id));
3530 QCOMPARE(constructedVariant.userType(), id);
3531 QCOMPARE(QMetaType::typeName(loadedVariant.userType()), typeName.toLatin1().constData());
3532 QCOMPARE(loadedVariant.userType(), constructedVariant.userType());
3535 void tst_QVariant::saveQVariantFromDataStream(QDataStream::Version version)
3537 QFETCH(QString, fileName);
3539 QFile file(fileName);
3540 QVERIFY(file.open(QIODevice::ReadOnly));
3541 QDataStream dataFileStream(&file);
3544 dataFileStream >> typeName;
3545 QByteArray data = file.readAll();
3546 const int id = QMetaType::type(typeName.toLatin1());
3547 if (id == QMetaType::Void) {
3548 // Void type is not supported by QVariant
3553 buffer.open(QIODevice::ReadWrite);
3554 QDataStream stream(&buffer);
3555 stream.setVersion(version);
3557 QVariant constructedVariant(static_cast<QVariant::Type>(id));
3558 QCOMPARE(constructedVariant.userType(), id);
3559 stream << constructedVariant;
3561 // We are testing QVariant there is no point in testing full array.
3562 QCOMPARE(buffer.data().left(5), data.left(5));
3565 QVariant recunstructedVariant;
3566 stream >> recunstructedVariant;
3567 QCOMPARE(recunstructedVariant.userType(), constructedVariant.userType());
3570 void tst_QVariant::debugStream_data()
3572 QTest::addColumn<QVariant>("variant");
3573 QTest::addColumn<int>("typeId");
3574 for (int id = 0; id < QMetaType::LastGuiType + 1; ++id) {
3575 const char *tagName = QMetaType::typeName(id);
3578 if (id != QMetaType::Void) {
3579 QTest::newRow(tagName) << QVariant(static_cast<QVariant::Type>(id)) << id;
3582 QTest::newRow("QBitArray(111)") << QVariant(QBitArray(3, true)) << qMetaTypeId<QBitArray>();
3583 QTest::newRow("CustomStreamableClass") << QVariant(qMetaTypeId<CustomStreamableClass>(), 0) << qMetaTypeId<CustomStreamableClass>();
3584 QTest::newRow("MyClass") << QVariant(qMetaTypeId<MyClass>(), 0) << qMetaTypeId<MyClass>();
3585 QTest::newRow("InvalidVariant") << QVariant() << int(QMetaType::UnknownType);
3586 QTest::newRow("CustomQObject") << QVariant::fromValue(this) << qMetaTypeId<tst_QVariant*>();
3589 void tst_QVariant::debugStream()
3591 QFETCH(QVariant, variant);
3592 QFETCH(int, typeId);
3594 MessageHandler msgHandler(typeId);
3595 qDebug() << variant;
3596 QVERIFY(msgHandler.testPassed());
3599 struct MessageHandlerType : public MessageHandler
3601 MessageHandlerType(const int typeId)
3602 : MessageHandler(typeId, handler)
3604 static void handler(QtMsgType, const char *txt)
3606 QString msg = QString::fromLatin1(txt);
3607 // Format itself is not important, but basic data as a type name should be included in the output
3608 ok = msg.startsWith("QVariant::");
3609 QVERIFY2(ok, (QString::fromLatin1("Message is not started correctly: '") + msg + '\'').toLatin1().constData());
3610 ok &= (currentId == QMetaType::UnknownType
3611 ? msg.contains("Invalid")
3612 : msg.contains(QMetaType::typeName(currentId)));
3613 QVERIFY2(ok, (QString::fromLatin1("Message doesn't contain type name: '") + msg + '\'').toLatin1().constData());
3617 void tst_QVariant::debugStreamType_data()
3622 void tst_QVariant::debugStreamType()
3624 QFETCH(QVariant, variant);
3625 QFETCH(int, typeId);
3627 MessageHandlerType msgHandler(typeId);
3628 qDebug() << QVariant::Type(typeId);
3629 QVERIFY(msgHandler.testPassed());
3632 void tst_QVariant::guiVariantAtExit()
3634 // crash test, it should not crash at QGuiApplication exit
3635 static QVariant cursor = QCursor();
3636 static QVariant point = QPoint();
3637 static QVariant icon = QIcon();
3638 static QVariant image = QImage();
3639 static QVariant palette = QPalette();
3648 QTEST_MAIN(tst_QVariant)
3649 #include "tst_qvariant.moc"