1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
7 ** This file is part of the test suite of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include <QtTest/QtTest>
45 #include <qbitarray.h>
46 #include <qhostaddress.h>
47 #include <qdatetime.h>
49 #include <qiodevice.h>
52 #include <qkeysequence.h>
58 #include <qmatrix4x4.h>
61 #include <qtransform.h>
62 #include <qvector2d.h>
63 #include <qvector3d.h>
64 #include <qvector4d.h>
65 #include <qquaternion.h>
70 Q_DECLARE_METATYPE(qlonglong)
71 Q_DECLARE_METATYPE(qulonglong)
72 Q_DECLARE_METATYPE(QPointF)
73 Q_DECLARE_METATYPE(QRectF)
74 Q_DECLARE_METATYPE(QSize)
75 Q_DECLARE_METATYPE(QSizeF)
76 Q_DECLARE_METATYPE(QLine)
77 Q_DECLARE_METATYPE(QLineF)
78 Q_DECLARE_METATYPE(QPoint)
79 Q_DECLARE_METATYPE(QRect)
80 Q_DECLARE_METATYPE(QPixmap)
81 Q_DECLARE_METATYPE(QBrush)
82 Q_DECLARE_METATYPE(QFont)
83 Q_DECLARE_METATYPE(QColor)
84 Q_DECLARE_METATYPE(QKeySequence)
86 class tst_QVariant : public QObject
92 void copy_constructor();
96 void canConvert_data();
129 void toLongLong_data();
132 void toULongLong_data();
135 void toByteArray_data();
138 void toString_data();
147 void toDateTime_data();
150 void toDouble_data();
153 void toPointF_data();
159 void toKeySequence_data();
160 void toKeySequence();
168 void toPixmap_data();
177 void qvariant_cast_QObject_data();
178 void qvariant_cast_QObject();
197 void basicUserType();
201 void writeToReadFromDataStream_data();
202 void writeToReadFromDataStream();
203 void writeToReadFromOldDataStream();
204 void checkDataStream();
206 void operator_eq_eq_data();
207 void operator_eq_eq();
209 void operator_eq_eq_rhs();
211 void typeName_data();
215 void streamInvalidVariant();
219 void data_(); // data is virtual function in QtTestCase
222 void saveLoadCustomTypes();
229 void convertToQUint8() const;
230 void invalidQColor() const;
231 void comparePointers() const;
232 void voidStar() const;
233 void dataStar() const;
234 void canConvertQStringList() const;
235 void canConvertQStringList_data() const;
236 void canConvertMetaTypeToInt() const;
237 void variantToDateTimeWithoutWarnings() const;
238 void invalidDateTime() const;
240 void loadUnknownUserType();
241 void loadBrokenUserType();
243 void invalidDate() const;
244 void compareCustomTypes() const;
245 void timeToDateTime() const;
246 void copyingUserTypes() const;
247 void convertBoolToByteArray() const;
248 void convertBoolToByteArray_data() const;
249 void convertByteArrayToBool() const;
250 void convertByteArrayToBool_data() const;
251 void toIntFromQString() const;
252 void toIntFromDouble() const;
255 void numericalConvert();
256 void moreCustomTypes();
257 void movabilityTest();
258 void variantInVariant();
262 void forwardDeclare();
263 void debugStream_data();
266 void loadQt4Stream_data();
267 void loadQt4Stream();
268 void saveQt4Stream_data();
269 void saveQt4Stream();
270 void loadQt5Stream_data();
271 void loadQt5Stream();
272 void saveQt5Stream_data();
273 void saveQt5Stream();
275 void dataStream_data(QDataStream::Version version);
276 void loadQVariantFromDataStream(QDataStream::Version version);
277 void saveQVariantFromDataStream(QDataStream::Version version);
280 Q_DECLARE_METATYPE(QDate)
281 Q_DECLARE_METATYPE(QTime)
282 Q_DECLARE_METATYPE(QDateTime)
283 Q_DECLARE_METATYPE(QVariant)
285 const qlonglong intMax1 = (qlonglong)INT_MAX + 1;
286 const qulonglong uintMax1 = (qulonglong)UINT_MAX + 1;
288 void tst_QVariant::constructor()
291 QVERIFY( !variant.isValid() );
292 QVERIFY( variant.isNull() );
294 QVariant var2(variant);
295 QVERIFY( !var2.isValid() );
296 QVERIFY( variant.isNull() );
298 QVariant varll(intMax1);
299 QVariant varll2(varll);
300 QCOMPARE(varll2, varll);
302 QVariant var3(QVariant::String);
303 QCOMPARE(var3.typeName(), "QString");
304 QVERIFY(var3.isNull());
305 QVERIFY(var3.isValid());
307 QVariant var4(QVariant::Invalid);
308 QCOMPARE(var4.type(), QVariant::Invalid);
309 QVERIFY(var4.isNull());
310 QVERIFY(!var4.isValid());
312 QVariant var5(QLatin1String("hallo"));
313 QCOMPARE(var5.type(), QVariant::String);
314 QCOMPARE(var5.typeName(), "QString");
316 QVariant var6(qlonglong(0));
317 QCOMPARE(var6.type(), QVariant::LongLong);
318 QCOMPARE(var6.typeName(), "qlonglong");
321 QVERIFY(var7.isValid());
322 QVERIFY(!var7.isNull());
324 var8.setValue<int>(5);
325 QVERIFY(var8.isValid());
326 QVERIFY(!var8.isNull());
329 void tst_QVariant::copy_constructor()
331 QVariant var7(QVariant::Int);
333 QCOMPARE(var8.type(), QVariant::Int);
334 QVERIFY(var8.isNull());
337 void tst_QVariant::isNull()
340 QVERIFY( var.isNull() );
343 QVariant var1( str1 );
344 QVERIFY( var1.isNull() );
346 QVariant var2( QString::null );
347 QVERIFY( var2.isNull() );
349 QVariant var3( QString( "blah" ) );
350 QVERIFY( !var3.isNull() );
353 QVERIFY( !var4.isNull() );
355 QVariant var5 = QString();
356 QVERIFY( var5.isNull() );
358 QVariant var6( QString( "blah" ) );
359 QVERIFY( !var6.isNull() );
361 QVERIFY( var6.isNull() );
362 var6.convert( QVariant::String );
363 QVERIFY( var6.isNull() );
364 QVariant varLL( (qlonglong)0 );
365 QVERIFY( !varLL.isNull() );
366 QVariant var7(QString::null);
367 QVERIFY(var7.isNull());
370 void tst_QVariant::swap()
372 QVariant v1 = 1, v2 = 2.0;
374 QCOMPARE(v1.type(),QVariant::Double);
375 QCOMPARE(v1.toDouble(),2.0);
376 QCOMPARE(v2.type(),QVariant::Int);
377 QCOMPARE(v2.toInt(),1);
380 void tst_QVariant::canConvert_data()
382 QTest::addColumn<QVariant>("val");
383 QTest::addColumn<bool>("BitArrayCast");
384 QTest::addColumn<bool>("BitmapCast");
385 QTest::addColumn<bool>("BoolCast");
386 QTest::addColumn<bool>("BrushCast");
387 QTest::addColumn<bool>("ByteArrayCast");
388 QTest::addColumn<bool>("ColorCast");
389 QTest::addColumn<bool>("CursorCast");
390 QTest::addColumn<bool>("DateCast");
391 QTest::addColumn<bool>("DateTimeCast");
392 QTest::addColumn<bool>("DoubleCast");
393 QTest::addColumn<bool>("FontCast");
394 QTest::addColumn<bool>("ImageCast");
395 QTest::addColumn<bool>("IntCast");
396 QTest::addColumn<bool>("InvalidCast");
397 QTest::addColumn<bool>("KeySequenceCast");
398 QTest::addColumn<bool>("ListCast");
399 QTest::addColumn<bool>("LongLongCast");
400 QTest::addColumn<bool>("MapCast");
401 QTest::addColumn<bool>("PaletteCast");
402 QTest::addColumn<bool>("PenCast");
403 QTest::addColumn<bool>("PixmapCast");
404 QTest::addColumn<bool>("PointCast");
405 QTest::addColumn<bool>("RectCast");
406 QTest::addColumn<bool>("RegionCast");
407 QTest::addColumn<bool>("SizeCast");
408 QTest::addColumn<bool>("SizePolicyCast");
409 QTest::addColumn<bool>("StringCast");
410 QTest::addColumn<bool>("StringListCast");
411 QTest::addColumn<bool>("TimeCast");
412 QTest::addColumn<bool>("UIntCast");
413 QTest::addColumn<bool>("ULongLongCast");
424 // 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
427 QVariant var(QBitArray(0));
428 QTest::newRow("BitArray")
429 << 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;
430 var = qVariantFromValue(QBitmap());
431 QTest::newRow("Bitmap")
432 << 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;
433 var = qVariantFromValue(QBrush());
434 QTest::newRow("Brush")
435 << 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;
436 var = QVariant(QByteArray());
437 QTest::newRow("ByteArray")
438 << 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;
439 var = qVariantFromValue(QColor());
440 QTest::newRow("Color")
441 << 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;
443 var = qVariantFromValue(QCursor());
444 QTest::newRow("Cursor")
445 << 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;
447 var = QVariant(QDate());
448 QTest::newRow("Date")
449 << 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;
450 var = QVariant(QDateTime());
451 QTest::newRow("DateTime")
452 << 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;
453 var = QVariant((double)0.1);
454 QTest::newRow("Double")
455 << 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;
456 var = QVariant(0.1f);
457 QTest::newRow("Float")
458 << 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;
459 var = qVariantFromValue(QFont());
460 QTest::newRow("Font")
461 << 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;
462 var = qVariantFromValue(QIcon());
463 QTest::newRow("Icon")
464 << 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;
465 var = qVariantFromValue(QImage());
466 QTest::newRow("Image")
467 << 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;
468 var = QVariant((int)1);
470 << 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;
472 QTest::newRow("Invalid")
473 << 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;
474 var = qVariantFromValue(QKeySequence());
475 QTest::newRow("KeySequence")
476 << 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;
477 var = QVariant(QList<QVariant>());
478 QTest::newRow("List")
479 << 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;
480 var = QVariant((qlonglong)1);
481 QTest::newRow("LongLong")
482 << 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;
483 var = QVariant(QMap<QString,QVariant>());
485 << 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;
486 var = qVariantFromValue(QPalette());
487 QTest::newRow("Palette")
488 << 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;
489 var = qVariantFromValue(QPen());
491 << 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;
492 var = qVariantFromValue(QPixmap());
493 QTest::newRow("Pixmap")
494 << 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;
495 var = qVariantFromValue(QPolygon());
496 QTest::newRow("PointArray")
497 << 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;
498 var = QVariant(QPoint());
499 QTest::newRow("Point")
500 << 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;
501 var = QVariant(QRect());
502 QTest::newRow("Rect")
503 << 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;
504 var = qVariantFromValue(QRegion());
505 QTest::newRow("Region")
506 << 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;
507 var = QVariant(QSize());
508 QTest::newRow("Size")
509 << 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;
510 var = qVariantFromValue(QSizePolicy());
511 QTest::newRow("SizePolicy")
512 << var << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N;
513 var = QVariant(QString());
514 QTest::newRow("String")
515 << 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;
516 var = QVariant(QStringList("entry"));
517 QTest::newRow("StringList")
518 << 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;
519 var = QVariant(QTime());
520 QTest::newRow("Time")
521 << var << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << Y << N << N;
522 var = QVariant((uint)1);
523 QTest::newRow("UInt")
524 << 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;
525 var = QVariant((qulonglong)1);
526 QTest::newRow("ULongLong")
527 << 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;
533 void tst_QVariant::canConvert()
535 QFETCH(QVariant, val);
536 QFETCH(bool, BitArrayCast);
537 QFETCH(bool, BitmapCast);
538 QFETCH(bool, BoolCast);
539 QFETCH(bool, BrushCast);
540 QFETCH(bool, ByteArrayCast);
541 QFETCH(bool, ColorCast);
542 QFETCH(bool, CursorCast);
543 QFETCH(bool, DateCast);
544 QFETCH(bool, DateTimeCast);
545 QFETCH(bool, DoubleCast);
546 QFETCH(bool, FontCast);
547 QFETCH(bool, ImageCast);
548 QFETCH(bool, IntCast);
549 QFETCH(bool, InvalidCast);
550 QFETCH(bool, KeySequenceCast);
551 QFETCH(bool, ListCast);
552 QFETCH(bool, LongLongCast);
553 QFETCH(bool, MapCast);
554 QFETCH(bool, PaletteCast);
555 QFETCH(bool, PenCast);
556 QFETCH(bool, PixmapCast);
557 QFETCH(bool, PointCast);
558 QFETCH(bool, RectCast);
559 QFETCH(bool, RegionCast);
560 QFETCH(bool, SizeCast);
561 QFETCH(bool, SizePolicyCast);
562 QFETCH(bool, StringCast);
563 QFETCH(bool, StringListCast);
564 QFETCH(bool, TimeCast);
565 QFETCH(bool, UIntCast);
566 QFETCH(bool, ULongLongCast);
568 QCOMPARE(val.canConvert(QVariant::BitArray), BitArrayCast);
569 QCOMPARE(val.canConvert(QVariant::Bitmap), BitmapCast);
570 QCOMPARE(val.canConvert(QVariant::Bool), BoolCast);
571 QCOMPARE(val.canConvert(QVariant::Brush), BrushCast);
572 QCOMPARE(val.canConvert(QVariant::ByteArray), ByteArrayCast);
573 QCOMPARE(val.canConvert(QVariant::Color), ColorCast);
574 QCOMPARE(val.canConvert(QVariant::Cursor), CursorCast);
575 QCOMPARE(val.canConvert(QVariant::Date), DateCast);
576 QCOMPARE(val.canConvert(QVariant::DateTime), DateTimeCast);
577 QCOMPARE(val.canConvert(QVariant::Double), DoubleCast);
578 QCOMPARE(val.canConvert(QVariant::Type(QMetaType::Float)), DoubleCast);
579 QCOMPARE(val.canConvert(QVariant::Font), FontCast);
580 QCOMPARE(val.canConvert(QVariant::Image), ImageCast);
581 QCOMPARE(val.canConvert(QVariant::Int), IntCast);
582 QCOMPARE(val.canConvert(QVariant::Invalid), InvalidCast);
583 QCOMPARE(val.canConvert(QVariant::KeySequence), KeySequenceCast);
584 QCOMPARE(val.canConvert(QVariant::List), ListCast);
585 QCOMPARE(val.canConvert(QVariant::LongLong), LongLongCast);
586 QCOMPARE(val.canConvert(QVariant::Map), MapCast);
587 QCOMPARE(val.canConvert(QVariant::Palette), PaletteCast);
588 QCOMPARE(val.canConvert(QVariant::Pen), PenCast);
589 QCOMPARE(val.canConvert(QVariant::Pixmap), PixmapCast);
590 QCOMPARE(val.canConvert(QVariant::Point), PointCast);
591 QCOMPARE(val.canConvert(QVariant::Rect), RectCast);
592 QCOMPARE(val.canConvert(QVariant::Region), RegionCast);
593 QCOMPARE(val.canConvert(QVariant::Size), SizeCast);
594 QCOMPARE(val.canConvert(QVariant::SizePolicy), SizePolicyCast);
595 QCOMPARE(val.canConvert(QVariant::String), StringCast);
596 QCOMPARE(val.canConvert(QVariant::StringList), StringListCast);
597 QCOMPARE(val.canConvert(QVariant::Time), TimeCast);
598 QCOMPARE(val.canConvert(QVariant::UInt), UIntCast);
599 QCOMPARE(val.canConvert(QVariant::ULongLong), ULongLongCast);
602 void tst_QVariant::toInt_data()
604 QTest::addColumn<QVariant>("value");
605 QTest::addColumn<int>("result");
606 QTest::addColumn<bool>("valueOK");
608 QTest::newRow( "invalid" ) << QVariant() << 0 << false;
609 QTest::newRow( "int" ) << QVariant( 123 ) << 123 << true;
610 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << 3 << true;
611 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << 3 << true;
612 QTest::newRow( "uint" ) << QVariant( 123u ) << 123 << true;
613 QTest::newRow( "int-string" ) << QVariant( QString("123") ) << 123 << true;
614 QTest::newRow( "string" ) << QVariant( QString("Unicode String") ) << 0 << false;
615 QTest::newRow( "longlong0" ) << QVariant( (qlonglong)34 ) << 34 << true;
616 QTest::newRow( "longlong1" ) << QVariant( intMax1 ) << (int)INT_MIN << true;
617 QTest::newRow( "ulonglong0" ) << QVariant( (qulonglong)34 ) << 34 << true;
618 QTest::newRow( "ulonglong1" ) << QVariant( uintMax1 ) << 0 << true;
619 QTest::newRow( "signedint" ) << QVariant( -123 ) << -123 << true;
620 QTest::newRow( "signeddouble" ) << QVariant( -3.1415927 ) << -3 << true;
621 QTest::newRow( "signedfloat" ) << QVariant( -3.1415927f ) << -3 << true;
622 QTest::newRow( "signedint-string" ) << QVariant( QString("-123") ) << -123 << true;
623 QTest::newRow( "signedlonglong0" ) << QVariant( (qlonglong)-34 ) << -34 << true;
624 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << int('a') << true;
625 QTest::newRow( "keysequence" ) << qVariantFromValue( QKeySequence( Qt::Key_A ) ) << 65 << true;
626 QByteArray bytearray(4, ' ');
631 QTest::newRow( "QByteArray1" ) << QVariant( bytearray ) << 0 << false;
636 QTest::newRow( "QByteArray2" ) << QVariant( bytearray ) << 4500 << true;
639 void tst_QVariant::toInt()
641 QFETCH( QVariant, value );
642 QFETCH( int, result );
643 QFETCH( bool, valueOK );
644 QVERIFY( value.isValid() == value.canConvert( QVariant::Int ) );
646 int i = value.toInt( &ok );
647 QCOMPARE( i, result );
648 QVERIFY( ok == valueOK );
651 void tst_QVariant::toUInt_data()
653 QTest::addColumn<QVariant>("value");
654 QTest::addColumn<uint>("result");
655 QTest::addColumn<bool>("valueOK");
657 QTest::newRow( "int" ) << QVariant( 123 ) << (uint)123 << true;
658 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (uint)3 << true;
659 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (uint)3 << true;
660 QTest::newRow( "uint" ) << QVariant( 123u ) << (uint)123 << true;
661 QTest::newRow( "int-string" ) << QVariant( QString("123") ) << (uint)123 << true;
662 QTest::newRow( "string" ) << QVariant( QString("Unicode String") ) << (uint)0 << false;
663 QTest::newRow( "string2" ) << QVariant( QString("4") ) << (uint)4 << true;
664 QTest::newRow( "longlong0" ) << QVariant( (qlonglong)34 ) << (uint)34 << true;
665 QTest::newRow( "longlong1" ) << QVariant( intMax1 ) << (uint)INT_MIN << true;
666 QTest::newRow( "ulonglong0" ) << QVariant( (qulonglong)34 ) << (uint)34 << true;
667 QTest::newRow( "ulonglong1" ) << QVariant( uintMax1 ) << (uint)0 << true;
668 QTest::newRow( "negativeint" ) << QVariant( -123 ) << (uint)-123 << true;
669 QTest::newRow( "negativedouble" ) << QVariant( -3.1415927 ) << (uint)-3 << true;
670 QTest::newRow( "negativefloat" ) << QVariant( -3.1415927f ) << (uint)-3 << true;
671 QTest::newRow( "negativeint-string" ) << QVariant( QString("-123") ) << (uint)0 << false;
672 QTest::newRow( "negativelonglong0" ) << QVariant( (qlonglong)-34 ) << (uint)-34 << true;
673 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << uint('a') << true;
674 QByteArray bytearray(4, ' ');
679 QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (uint)4321 << true;
682 void tst_QVariant::toUInt()
684 QFETCH( QVariant, value );
685 QFETCH( uint, result );
686 QFETCH( bool, valueOK );
687 QVERIFY( value.isValid() );
688 QVERIFY( value.canConvert( QVariant::UInt ) );
691 uint i = value.toUInt( &ok );
692 QVERIFY( ok == valueOK );
693 QCOMPARE( i, result );
697 void tst_QVariant::toSize_data()
699 QTest::addColumn<QVariant>("value");
700 QTest::addColumn<QSize>("result");
701 QTest::newRow( "qsizef4" ) << QVariant( QSizeF(4, 2) ) << QSize(4, 2);
702 QTest::newRow( "qsizef1" ) << QVariant( QSizeF(0, 0) ) << QSize(0, 0);
703 QTest::newRow( "qsizef2" ) << QVariant( QSizeF(-5, -1) ) << QSize(-5, -1);
704 QTest::newRow( "qsizef3" ) << QVariant( QSizeF() ) << QSize();
707 void tst_QVariant::toSize()
709 QFETCH( QVariant, value );
710 QFETCH( QSize, result );
711 QVERIFY( value.isValid() );
712 QVERIFY( value.canConvert( QVariant::Size ) );
714 QSize i = value.toSize();
715 QCOMPARE( i, result );
718 void tst_QVariant::toSizeF_data()
720 QTest::addColumn<QVariant>("value");
721 QTest::addColumn<QSizeF>("result");
722 QTest::newRow( "qsize1" ) << QVariant( QSize(0, 0) ) << QSizeF(0, 0);
723 QTest::newRow( "qsize2" ) << QVariant( QSize(-5, -1) ) << QSizeF(-5, -1);
724 QTest::newRow( "qsize3" ) << QVariant( QSize() ) << QSizeF();
725 QTest::newRow( "qsize4" ) << QVariant(QSize(4,2)) << QSizeF(4,2);
728 void tst_QVariant::toSizeF()
730 QFETCH( QVariant, value );
731 QFETCH( QSizeF, result );
732 QVERIFY( value.isValid() );
733 QVERIFY( value.canConvert( QVariant::SizeF ) );
735 QSizeF i = value.toSizeF();
736 QCOMPARE( i, result );
739 void tst_QVariant::toLine_data()
741 QTest::addColumn<QVariant>("value");
742 QTest::addColumn<QLine>("result");
743 QTest::newRow( "linef1" ) << QVariant( QLineF(1, 2, 3, 4) ) << QLine(1, 2, 3, 4);
744 QTest::newRow( "linef2" ) << QVariant( QLineF(-1, -2, -3, -4) ) << QLine(-1, -2, -3, -4);
745 QTest::newRow( "linef3" ) << QVariant( QLineF(0, 0, 0, 0) ) << QLine(0, 0, 0, 0);
746 QTest::newRow( "linef4" ) << QVariant( QLineF() ) << QLine();
749 void tst_QVariant::toLine()
751 QFETCH( QVariant, value );
752 QFETCH( QLine, result );
753 QVERIFY( value.isValid() );
754 QVERIFY( value.canConvert( QVariant::Line ) );
756 QLine i = value.toLine();
757 QCOMPARE( i, result );
760 void tst_QVariant::toLineF_data()
762 QTest::addColumn<QVariant>("value");
763 QTest::addColumn<QLineF>("result");
764 QTest::newRow( "line1" ) << QVariant( QLine(-1, -2, -3, -4) ) << QLineF(-1, -2, -3, -4);
765 QTest::newRow( "line2" ) << QVariant( QLine(0, 0, 0, 0) ) << QLineF(0, 0, 0, 0);
766 QTest::newRow( "line3" ) << QVariant( QLine() ) << QLineF();
767 QTest::newRow( "line4" ) << QVariant( QLine(1, 2, 3, 4) ) << QLineF(1, 2, 3, 4);
770 void tst_QVariant::toLineF()
772 QFETCH( QVariant, value );
773 QFETCH( QLineF, result );
774 QVERIFY( value.isValid() );
775 QVERIFY( value.canConvert( QVariant::LineF ) );
777 QLineF i = value.toLineF();
778 QCOMPARE( i, result );
781 void tst_QVariant::toPoint_data()
783 QTest::addColumn<QVariant>("value");
784 QTest::addColumn<QPoint>("result");
785 QTest::newRow( "pointf1" ) << QVariant( QPointF(4, 2) ) << QPoint(4, 2);
786 QTest::newRow( "pointf2" ) << QVariant( QPointF(0, 0) ) << QPoint(0, 0);
787 QTest::newRow( "pointf3" ) << QVariant( QPointF(-4, -2) ) << QPoint(-4, -2);
788 QTest::newRow( "pointf4" ) << QVariant( QPointF() ) << QPoint();
789 QTest::newRow( "pointf5" ) << QVariant( QPointF(-4.2f, -2.3f) ) << QPoint(-4, -2);
792 void tst_QVariant::toPoint()
794 QFETCH( QVariant, value );
795 QFETCH( QPoint, result );
796 QVERIFY( value.isValid() );
797 QVERIFY( value.canConvert( QVariant::Point ) );
798 QPoint i = value.toPoint();
799 QCOMPARE( i, result );
802 void tst_QVariant::toRect_data()
804 QTest::addColumn<QVariant>("value");
805 QTest::addColumn<QRect>("result");
806 QTest::newRow( "rectf1" ) << QVariant(QRectF(1, 2, 3, 4)) << QRect(1, 2, 3, 4);
807 QTest::newRow( "rectf2" ) << QVariant(QRectF(0, 0, 0, 0)) << QRect(0, 0, 0, 0);
808 QTest::newRow( "rectf3" ) << QVariant(QRectF(-1, -2, -3, -4)) << QRect(-1, -2, -3, -4);
809 QTest::newRow( "rectf4" ) << QVariant(QRectF(-1.3f, 0, 3.9f, -4.0)) << QRect(-1, 0, 4, -4);
810 QTest::newRow( "rectf5" ) << QVariant(QRectF()) << QRect();
813 void tst_QVariant::toRect()
815 QFETCH( QVariant, value );
816 QFETCH( QRect, result );
817 QVERIFY( value.isValid() );
818 QVERIFY( value.canConvert( QVariant::Rect ) );
819 QRect i = value.toRect();
820 QCOMPARE( i, result );
823 void tst_QVariant::toChar_data()
825 QTest::addColumn<QVariant>("value");
826 QTest::addColumn<QChar>("result");
827 QTest::newRow( "longlong" ) << QVariant(qlonglong('6')) << QChar('6');
828 QTest::newRow( "ulonglong" ) << QVariant(qulonglong('7')) << QChar('7');
831 void tst_QVariant::toChar()
833 QFETCH( QVariant, value );
834 QFETCH( QChar, result );
835 QVERIFY( value.isValid() );
836 QVERIFY( value.canConvert( QVariant::Char ) );
838 QChar i = value.toChar();
839 QCOMPARE( i, result );
842 void tst_QVariant::toBool_data()
844 QTest::addColumn<QVariant>("value");
845 QTest::addColumn<bool>("result");
847 QTest::newRow( "int0" ) << QVariant( 0 ) << false;
848 QTest::newRow( "int1" ) << QVariant( 123 ) << true;
849 QTest::newRow( "uint0" ) << QVariant( 0u ) << false;
850 QTest::newRow( "uint1" ) << QVariant( 123u ) << true;
851 QTest::newRow( "double0" ) << QVariant( 0.0 ) << false;
852 QTest::newRow( "float0" ) << QVariant( 0.0f ) << false;
853 QTest::newRow( "double1" ) << QVariant( 3.1415927 ) << true;
854 QTest::newRow( "float1" ) << QVariant( 3.1415927f ) << true;
855 QTest::newRow( "string0" ) << QVariant( QString("3") ) << true;
856 QTest::newRow( "string1" ) << QVariant( QString("true") ) << true;
857 QTest::newRow( "string2" ) << QVariant( QString("0") ) << false;
858 QTest::newRow( "string3" ) << QVariant( QString("fAlSe") ) << false;
859 QTest::newRow( "longlong0" ) << QVariant( (qlonglong)0 ) << false;
860 QTest::newRow( "longlong1" ) << QVariant( (qlonglong)1 ) << true;
861 QTest::newRow( "ulonglong0" ) << QVariant( (qulonglong)0 ) << false;
862 QTest::newRow( "ulonglong1" ) << QVariant( (qulonglong)1 ) << true;
863 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << true;
864 QTest::newRow( "Null_QChar" ) << QVariant(QChar(0)) << false;
867 void tst_QVariant::toBool()
869 QFETCH( QVariant, value );
870 QFETCH( bool, result );
871 QVERIFY( value.isValid() );
872 QVERIFY( value.canConvert( QVariant::Bool ) );
874 bool i = value.toBool();
875 QCOMPARE( i, result );
878 void tst_QVariant::toPointF_data()
880 QTest::addColumn<QVariant>("value");
881 QTest::addColumn<QPointF>("result");
883 QTest::newRow( "QPoint" ) << QVariant( QPointF( 19, 84) ) << QPointF( 19, 84 );
886 void tst_QVariant::toPointF()
888 QFETCH( QVariant, value );
889 QFETCH( QPointF, result );
890 QVERIFY( value.isValid() );
891 QVERIFY( value.canConvert( QVariant::PointF ) );
892 QPointF d = value.toPointF();
893 QCOMPARE( d, result );
896 void tst_QVariant::toRectF_data()
898 QTest::addColumn<QVariant>("value");
899 QTest::addColumn<QRectF>("result");
901 QRect r( 1, 9, 8, 4 );
902 QRectF rf( 1.0, 9.0, 8.0, 4.0 );
903 QTest::newRow( "QRect" ) << QVariant( r ) << rf;
906 void tst_QVariant::toRectF()
908 QFETCH( QVariant, value );
909 QFETCH( QRectF, result );
910 QVERIFY( value.isValid() );
911 QVERIFY( value.canConvert( QVariant::RectF ) );
912 QRectF d = value.toRectF();
913 QCOMPARE( d, result );
916 void tst_QVariant::toColor_data()
918 QTest::addColumn<QVariant>("value");
919 QTest::addColumn<QColor>("result");
922 QTest::newRow( "string" ) << QVariant( QString( "red" ) ) << c;
923 QTest::newRow( "solid brush" ) << QVariant( QBrush(c) ) << c;
926 void tst_QVariant::toColor()
928 QFETCH( QVariant, value );
929 QFETCH( QColor, result );
930 QVERIFY( value.isValid() );
931 QVERIFY( value.canConvert( QVariant::Color ) );
932 QColor d = qVariantValue<QColor>(value);
933 QCOMPARE( d, result );
936 void tst_QVariant::toPixmap_data()
938 QTest::addColumn<QVariant>("value");
939 QTest::addColumn<QPixmap>("result");
943 QTest::newRow( "image" ) << QVariant( pm ) << pm;
947 QTest::newRow( "bitmap" ) << QVariant( bm ) << QPixmap(bm);
950 void tst_QVariant::toPixmap()
952 QFETCH( QVariant, value );
953 QFETCH( QPixmap, result );
954 QVERIFY( value.isValid() );
955 QVERIFY( value.canConvert( QVariant::Pixmap ) );
956 QPixmap d = qVariantValue<QPixmap>(value);
957 QCOMPARE( d, result );
960 void tst_QVariant::toImage_data()
962 QTest::addColumn<QVariant>("value");
963 QTest::addColumn<QImage>("result");
965 QImage im(30, 30, QImage::Format_ARGB32);
967 QTest::newRow( "image" ) << QVariant( im ) << im;
970 void tst_QVariant::toImage()
972 QFETCH( QVariant, value );
973 QFETCH( QImage, result );
974 QVERIFY( value.isValid() );
975 QVERIFY( value.canConvert( QVariant::Image ) );
976 QImage d = qVariantValue<QImage>(value);
977 QCOMPARE( d, result );
980 void tst_QVariant::toBrush_data()
982 QTest::addColumn<QVariant>("value");
983 QTest::addColumn<QBrush>("result");
986 QTest::newRow( "color" ) << QVariant( c ) << QBrush(c);
989 QTest::newRow( "pixmap" ) << QVariant( pm ) << QBrush(pm);
992 void tst_QVariant::toBrush()
994 QFETCH( QVariant, value );
995 QFETCH( QBrush, result );
996 QVERIFY( value.isValid() );
997 QVERIFY( value.canConvert( QVariant::Brush ) );
998 QBrush d = qVariantValue<QBrush>(value);
999 QCOMPARE( d, result );
1002 void tst_QVariant::toFont_data()
1004 QTest::addColumn<QVariant>("value");
1005 QTest::addColumn<QFont>("result");
1007 QFont f("times",12,-1,false);
1008 QTest::newRow( "string" ) << QVariant( QString( "times,12,-1,5,50,0,0,0,0,0" ) ) << f;
1011 void tst_QVariant::toFont()
1013 QFETCH( QVariant, value );
1014 QFETCH( QFont, result );
1015 QVERIFY( value.isValid() );
1016 QVERIFY( value.canConvert( QVariant::Font ) );
1017 QFont d = qVariantValue<QFont>(value);
1018 QCOMPARE( d, result );
1021 void tst_QVariant::toKeySequence_data()
1023 QTest::addColumn<QVariant>("value");
1024 QTest::addColumn<QKeySequence>("result");
1027 QTest::newRow( "int" ) << QVariant( 67108929 ) << QKeySequence( Qt::CTRL + Qt::Key_A );
1030 QTest::newRow( "qstring" )
1031 << QVariant( QString( "Ctrl+A" ) )
1032 << QKeySequence( Qt::CTRL + Qt::Key_A );
1035 void tst_QVariant::toKeySequence()
1037 QFETCH( QVariant, value );
1038 QFETCH( QKeySequence, result );
1039 QVERIFY( value.isValid() );
1040 QVERIFY( value.canConvert( QVariant::KeySequence ) );
1041 QKeySequence d = qVariantValue<QKeySequence>(value);
1042 QCOMPARE( d, result );
1045 void tst_QVariant::toDouble_data()
1047 QTest::addColumn<QVariant>("value");
1048 QTest::addColumn<double>("result");
1049 QTest::addColumn<bool>("valueOK");
1051 QByteArray bytearray(4, ' ');
1056 QTest::newRow( "bytearray" ) << QVariant( bytearray ) << 32.1 << true;
1059 void tst_QVariant::toDouble()
1061 QFETCH( QVariant, value );
1062 QFETCH( double, result );
1063 QFETCH( bool, valueOK );
1064 QVERIFY( value.isValid() );
1065 QVERIFY( value.canConvert( QVariant::Double ) );
1067 double d = value.toDouble( &ok );
1068 QCOMPARE( d, result );
1069 QVERIFY( ok == valueOK );
1072 void tst_QVariant::toLongLong_data()
1074 QTest::addColumn<QVariant>("value");
1075 QTest::addColumn<qlonglong>("result");
1076 QTest::addColumn<bool>("valueOK");
1078 QTest::newRow( "int0" ) << QVariant( 123 ) << (qlonglong)123 << true;
1079 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (qlonglong)3 << true;
1080 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (qlonglong)3 << true;
1081 QTest::newRow( "uint" ) << QVariant( 123u ) << (qlonglong)123 << true;
1082 QTest::newRow( "int-string" ) << QVariant( QString("123") )
1083 << (qlonglong)123 << true;
1084 QTest::newRow( "string" ) << QVariant( QString("Unicode fun") ) << (qlonglong)0
1086 QTest::newRow( "longlong" ) << QVariant( intMax1 ) << intMax1 << true;
1087 QTest::newRow( "ulonglong" ) << QVariant( uintMax1 ) << (qlonglong)uintMax1 << true;
1088 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << qlonglong('a') << true;
1089 QByteArray bytearray(4, ' ');
1094 QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (qlonglong) 3200 << true;
1097 void tst_QVariant::toLongLong()
1099 QFETCH( QVariant, value );
1100 QFETCH( qlonglong, result );
1101 QFETCH( bool, valueOK );
1102 QVERIFY( value.isValid() );
1103 QVERIFY( value.canConvert( QVariant::LongLong ) );
1105 qlonglong ll = value.toLongLong( &ok );
1106 QCOMPARE( ll, result );
1107 QVERIFY( ok == valueOK );
1110 void tst_QVariant::toULongLong_data()
1112 QTest::addColumn<QVariant>("value");
1113 QTest::addColumn<qulonglong>("result");
1114 QTest::addColumn<bool>("valueOK");
1116 QTest::newRow( "int0" ) << QVariant( 123 ) << (qulonglong)123 << true;
1117 QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (qulonglong)3 << true;
1118 QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (qulonglong)3 << true;
1119 QTest::newRow( "uint" ) << QVariant( 123u ) << (qulonglong)123 << true;
1120 QTest::newRow( "int-string" ) << QVariant( QString("123") )
1121 << (qulonglong)123 << true;
1122 QTest::newRow( "string" ) << QVariant( QString("Unicode fun") ) << (qulonglong)0
1124 QTest::newRow( "ulonglong-string" ) << QVariant( QString("18446744073709551615") )
1125 << Q_UINT64_C(18446744073709551615)
1127 QTest::newRow( "bytaa-string" ) << QVariant( QString("18446744073709551615") )
1128 << Q_UINT64_C(18446744073709551615)
1130 QTest::newRow( "longlong" ) << QVariant( intMax1 ) << (qulonglong)intMax1 << true;
1131 QTest::newRow( "ulonglong" ) << QVariant( uintMax1 ) << uintMax1 << true;
1132 QTest::newRow( "QChar" ) << QVariant(QChar('a')) << qulonglong('a') << true;
1133 QByteArray bytearray(4, ' ');
1138 QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (qulonglong) 3201 << true;
1141 void tst_QVariant::toULongLong()
1143 QFETCH( QVariant, value );
1144 QFETCH( qulonglong, result );
1145 QFETCH( bool, valueOK );
1146 QVERIFY( value.isValid() );
1147 QVERIFY( value.canConvert( QVariant::ULongLong ) );
1149 qulonglong ll = value.toULongLong( &ok );
1150 QCOMPARE( ll, result );
1151 QVERIFY( ok == valueOK );
1154 void tst_QVariant::toByteArray_data()
1156 QTest::addColumn<QVariant>("value");
1157 QTest::addColumn<QByteArray>("result");
1159 QByteArray ba(5, ' ');
1166 QByteArray variantBa = ba;
1168 QTest::newRow( "qbytearray" ) << QVariant( variantBa ) << ba;
1169 QTest::newRow( "int" ) << QVariant( -123 ) << QByteArray( "-123" );
1170 QTest::newRow( "uint" ) << QVariant( (uint)123 ) << QByteArray( "123" );
1171 QTest::newRow( "double" ) << QVariant( 123.456 ) << QByteArray( "123.456" );
1172 QTest::newRow( "float" ) << QVariant( 123.456f ) << QByteArray( "123.456" );
1173 QTest::newRow( "longlong" ) << QVariant( (qlonglong)34 ) << QByteArray( "34" );
1174 QTest::newRow( "ulonglong" ) << QVariant( (qulonglong)34 ) << QByteArray( "34" );
1177 void tst_QVariant::toByteArray()
1179 QFETCH( QVariant, value );
1180 QFETCH( QByteArray, result );
1181 QVERIFY( value.isValid() );
1182 QVERIFY( value.canConvert( QVariant::ByteArray ) );
1183 QByteArray ba = value.toByteArray();
1184 QCOMPARE( ba, result );
1187 void tst_QVariant::toString_data()
1189 QTest::addColumn<QVariant>("value");
1190 QTest::addColumn<QString>("result");
1192 QTest::newRow( "qstring" ) << QVariant( QString( "Test" ) ) << QString( "Test" );
1193 QTest::newRow( "charstar" ) << QVariant(QLatin1String("Test")) << QString("Test");
1194 QTest::newRow( "qbytearray") << QVariant( QByteArray( "Test\0" ) ) << QString( "Test" );
1195 QTest::newRow( "int" ) << QVariant( -123 ) << QString( "-123" );
1196 QTest::newRow( "uint" ) << QVariant( (uint)123 ) << QString( "123" );
1197 QTest::newRow( "double" ) << QVariant( 123.456 ) << QString( "123.456" );
1198 QTest::newRow( "float" ) << QVariant( 123.456f ) << QString( "123.456" );
1199 QTest::newRow( "bool" ) << QVariant( true ) << QString( "true" );
1200 QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 1, 1 ) ) << QString( "2002-01-01" );
1201 QTest::newRow( "qtime" ) << QVariant( QTime( 12, 34, 56 ) ) << QString( "12:34:56" );
1202 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 1, 1 ), QTime( 12, 34, 56 ) ) ) << QString( "2002-01-01T12:34:56" );
1203 QTest::newRow( "qkeysequence" ) << qVariantFromValue( QKeySequence( Qt::CTRL + Qt::Key_A ) )
1205 << QString( "Ctrl+A" );
1207 << QString(QChar(0x2318)) + "A";
1210 QFont font( "times", 12 );
1211 QTest::newRow( "qfont" ) << qVariantFromValue( font ) << QString("times,12,-1,5,50,0,0,0,0,0");
1212 QTest::newRow( "qcolor" ) << qVariantFromValue( QColor( 10, 10, 10 ) ) << QString( "#0a0a0a" );
1213 QTest::newRow( "llong" ) << QVariant( (qlonglong)Q_INT64_C(123456789012) ) <<
1214 QString( "123456789012" );
1217 void tst_QVariant::toString()
1219 QFETCH( QVariant, value );
1220 QFETCH( QString, result );
1221 QVERIFY( value.isValid() );
1222 QVERIFY( value.canConvert( QVariant::String ) );
1223 QString str = value.toString();
1224 QCOMPARE( str, result );
1227 void tst_QVariant::toDate_data()
1229 QTest::addColumn<QVariant>("value");
1230 QTest::addColumn<QDate>("result");
1232 QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 10, 10 ) ) << QDate( 2002, 10, 10 );
1233 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) ) ) << QDate( 2002, 10, 10 );
1234 QTest::newRow( "qstring" ) << QVariant( QString( "2002-10-10" ) ) << QDate( 2002, 10, 10 );
1237 void tst_QVariant::toDate()
1239 QFETCH( QVariant, value );
1240 QFETCH( QDate, result );
1241 QVERIFY( value.isValid() );
1242 QVERIFY( value.canConvert( QVariant::Date ) );
1243 QCOMPARE( value.toDate(), result );
1246 void tst_QVariant::toTime_data()
1248 QTest::addColumn<QVariant>("value");
1249 QTest::addColumn<QTime>("result");
1251 QTest::newRow( "qtime" ) << QVariant( QTime( 12, 34, 56 ) ) << QTime( 12, 34, 56 );
1252 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) ) ) << QTime( 12, 34, 56 );
1253 QTest::newRow( "qstring" ) << QVariant( QString( "12:34:56" ) ) << QTime( 12, 34, 56 );
1256 void tst_QVariant::toTime()
1258 QFETCH( QVariant, value );
1259 QFETCH( QTime, result );
1260 QVERIFY( value.isValid() );
1261 QVERIFY( value.canConvert( QVariant::Time ) );
1262 QCOMPARE( value.toTime(), result );
1265 void tst_QVariant::toDateTime_data()
1267 QTest::addColumn<QVariant>("value");
1268 QTest::addColumn<QDateTime>("result");
1270 QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) ) )
1271 << QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) );
1272 QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 10, 10 ) ) << QDateTime( QDate( 2002, 10, 10 ), QTime( 0, 0, 0 ) );
1273 QTest::newRow( "qstring" ) << QVariant( QString( "2002-10-10T12:34:56" ) ) << QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) );
1276 void tst_QVariant::toDateTime()
1278 QFETCH( QVariant, value );
1279 QFETCH( QDateTime, result );
1280 QVERIFY( value.isValid() );
1281 QVERIFY( value.canConvert( QVariant::DateTime ) );
1282 QCOMPARE( value.toDateTime(), result );
1285 void tst_QVariant::toLocale()
1288 QLocale loc = variant.toLocale();
1289 variant = QLocale::system();
1290 loc = variant.toLocale();
1293 void tst_QVariant::toRegExp()
1296 QRegExp rx = variant.toRegExp();
1297 variant = QRegExp("foo");
1298 rx = variant.toRegExp();
1301 void tst_QVariant::matrix()
1304 QMatrix matrix = qVariantValue<QMatrix>(variant);
1305 QVERIFY(matrix.isIdentity());
1306 qVariantSetValue(variant, QMatrix().rotate(90));
1307 QCOMPARE(QMatrix().rotate(90), qVariantValue<QMatrix>(variant));
1309 void *mmatrix = QMetaType::create(QVariant::Matrix, 0);
1311 QMetaType::destroy(QVariant::Matrix, mmatrix);
1314 void tst_QVariant::matrix4x4()
1317 QMatrix4x4 matrix = qVariantValue<QMatrix4x4>(variant);
1318 QVERIFY(matrix.isIdentity());
1321 qVariantSetValue(variant, m);
1322 QCOMPARE(m, qVariantValue<QMatrix4x4>(variant));
1324 void *mmatrix = QMetaType::create(QVariant::Matrix4x4, 0);
1326 QMetaType::destroy(QVariant::Matrix4x4, mmatrix);
1329 void tst_QVariant::transform()
1332 QTransform matrix = qVariantValue<QTransform>(variant);
1333 QVERIFY(matrix.isIdentity());
1334 qVariantSetValue(variant, QTransform().rotate(90));
1335 QCOMPARE(QTransform().rotate(90), qVariantValue<QTransform>(variant));
1337 void *mmatrix = QMetaType::create(QVariant::Transform, 0);
1339 QMetaType::destroy(QVariant::Transform, mmatrix);
1343 void tst_QVariant::vector2D()
1346 QVector2D vector = qVariantValue<QVector2D>(variant);
1347 QVERIFY(vector.isNull());
1348 qVariantSetValue(variant, QVector2D(0.1, 0.2));
1349 QCOMPARE(QVector2D(0.1, 0.2), qVariantValue<QVector2D>(variant));
1351 void *pvector = QMetaType::create(QVariant::Vector2D, 0);
1353 QMetaType::destroy(QVariant::Vector2D, pvector);
1356 void tst_QVariant::vector3D()
1359 QVector3D vector = qVariantValue<QVector3D>(variant);
1360 QVERIFY(vector.isNull());
1361 qVariantSetValue(variant, QVector3D(0.1, 0.2, 0.3));
1362 QCOMPARE(QVector3D(0.1, 0.2, 0.3), qVariantValue<QVector3D>(variant));
1364 void *pvector = QMetaType::create(QVariant::Vector3D, 0);
1366 QMetaType::destroy(QVariant::Vector3D, pvector);
1369 void tst_QVariant::vector4D()
1372 QVector4D vector = qVariantValue<QVector4D>(variant);
1373 QVERIFY(vector.isNull());
1374 qVariantSetValue(variant, QVector4D(0.1, 0.2, 0.3, 0.4));
1375 QCOMPARE(QVector4D(0.1, 0.2, 0.3, 0.4), qVariantValue<QVector4D>(variant));
1377 void *pvector = QMetaType::create(QVariant::Vector4D, 0);
1379 QMetaType::destroy(QVariant::Vector4D, pvector);
1382 void tst_QVariant::quaternion()
1385 QQuaternion quaternion = qVariantValue<QQuaternion>(variant);
1386 QVERIFY(quaternion.isIdentity());
1387 qVariantSetValue(variant, QQuaternion(0.1, 0.2, 0.3, 0.4));
1388 QCOMPARE(QQuaternion(0.1, 0.2, 0.3, 0.4), qVariantValue<QQuaternion>(variant));
1390 void *pquaternion = QMetaType::create(QVariant::Quaternion, 0);
1391 QVERIFY(pquaternion);
1392 QMetaType::destroy(QVariant::Quaternion, pquaternion);
1395 struct CustomStreamableClass
1398 bool operator==(const CustomStreamableClass& other) const
1400 return i == other.i;
1403 Q_DECLARE_METATYPE(CustomStreamableClass);
1405 QDataStream &operator<<(QDataStream &out, const CustomStreamableClass &myObj)
1407 return out << myObj.i;
1410 QDataStream &operator>>(QDataStream &in, CustomStreamableClass &myObj)
1412 return in >> myObj.i;
1415 void tst_QVariant::writeToReadFromDataStream_data()
1417 qRegisterMetaTypeStreamOperators<CustomStreamableClass>();
1419 QTest::addColumn<QVariant>("writeVariant");
1420 QTest::addColumn<bool>("isNull");
1422 typedef QList<QVariant> variantsList;
1423 variantsList valuelist;
1424 valuelist << QVariant( 1 ) << QVariant( QString("Two") ) << QVariant( 3.45 );
1425 QVariant var(valuelist);
1426 QTest::newRow( "list_valid" ) << var << false;
1429 QTest::newRow( "invalid" ) << QVariant() << true;
1430 QTest::newRow( "bitarray_invalid" ) << QVariant( QBitArray() ) << true;
1431 QBitArray bitarray( 3 );
1435 QTest::newRow( "bitarray_valid" ) << QVariant( bitarray ) << false;
1436 QTest::newRow( "bytearray_invalid" ) << QVariant( QByteArray() ) << true;
1437 QTest::newRow( "int_invalid") << QVariant(QVariant::Int) << true;
1438 QByteArray bytearray(5, ' ');
1443 bytearray[4] = '\0';
1444 QTest::newRow( "bytearray_valid" ) << QVariant( bytearray ) << false;
1445 QTest::newRow( "bitmap_invalid" ) << qVariantFromValue( QBitmap() ) << true;
1446 QBitmap bitmap( 10, 10 );
1447 bitmap.fill( Qt::red );
1448 QTest::newRow( "bitmap_valid" ) << qVariantFromValue( bitmap ) << false;
1449 QTest::newRow( "brush_valid" ) << qVariantFromValue( QBrush( Qt::red ) ) << false;
1450 QTest::newRow( "color_valid" ) << qVariantFromValue( QColor( Qt::red ) ) << false;
1451 #ifndef QT_NO_CURSOR
1452 QTest::newRow( "cursor_valid" ) << qVariantFromValue( QCursor( Qt::PointingHandCursor ) ) << false;
1454 QTest::newRow( "date_invalid" ) << QVariant( QDate() ) << true;
1455 QTest::newRow( "date_valid" ) << QVariant( QDate( 2002, 07, 06 ) ) << false;
1456 QTest::newRow( "datetime_invalid" ) << QVariant( QDateTime() ) << true;
1457 QTest::newRow( "datetime_valid" ) << QVariant( QDateTime( QDate( 2002, 07, 06 ), QTime( 14, 0, 0 ) ) ) << false;
1458 QTest::newRow( "double_valid" ) << QVariant( 123.456 ) << false;
1459 QTest::newRow( "float_valid" ) << QVariant( 123.456f ) << false;
1460 QTest::newRow( "font_valid" ) << qVariantFromValue( QFont( "times", 12 ) ) << false;
1461 QTest::newRow( "pixmap_invalid" ) << qVariantFromValue( QPixmap() ) << true;
1462 QPixmap pixmap( 10, 10 );
1463 pixmap.fill( Qt::red );
1464 QTest::newRow( "pixmap_valid" ) << qVariantFromValue( pixmap ) << false;
1465 // QTest::newRow( "iconset_valid" ) << QVariant( QIcon( pixmap ) ) << false;
1466 QTest::newRow( "image_invalid" ) << qVariantFromValue( QImage() ) << true;
1467 QTest::newRow( "keysequence_valid" ) << qVariantFromValue( QKeySequence( Qt::CTRL + Qt::Key_A ) ) << false;
1468 QTest::newRow( "int_valid" ) << QVariant( -123 ) << false;
1469 typedef QMap<QString, QVariant> variantsMap;
1471 vMap.insert( "int", QVariant( 1 ) );
1472 vMap.insert( "string", QVariant( QString("Two") ) );
1473 vMap.insert( "double", QVariant( 3.45 ) );
1474 vMap.insert( "float", QVariant( 3.45f ) );
1475 QTest::newRow( "map_valid" ) << QVariant( vMap ) << false;
1476 QTest::newRow( "palette_valid" ) << qVariantFromValue(QPalette(QColor("turquoise"))) << false;
1477 QTest::newRow( "pen_valid" ) << qVariantFromValue( QPen( Qt::red ) ) << false;
1478 QTest::newRow( "pointarray_invalid" ) << qVariantFromValue( QPolygon() ) << true;
1479 QTest::newRow( "pointarray_valid" ) << qVariantFromValue( QPolygon( QRect( 10, 10, 20, 20 ) ) ) << false;
1480 QTest::newRow( "region_invalid" ) << qVariantFromValue( QRegion() ) << true;
1481 QTest::newRow( "region_valid" ) << qVariantFromValue( QRegion( 10, 10, 20, 20 ) ) << false;
1482 QTest::newRow( "sizepolicy_valid" ) << qVariantFromValue( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) ) << false;
1483 QTest::newRow( "point_invalid" ) << qVariantFromValue( QPoint() ) << true;
1484 QTest::newRow( "point_valid" ) << qVariantFromValue( QPoint( 10, 10 ) ) << false;
1485 QTest::newRow( "rect_invalid" ) << QVariant( QRect() ) << true;
1486 QTest::newRow( "rect_valid" ) << QVariant( QRect( 10, 10, 20, 20 ) ) << false;
1487 QTest::newRow( "size_invalid" ) << QVariant( QSize( 0, 0 ) ) << true;
1488 QTest::newRow( "size_valid" ) << QVariant( QSize( 10, 10 ) ) << false;
1489 QTest::newRow( "string_invalid" ) << QVariant( QString() ) << true;
1490 QTest::newRow( "string_valid" ) << QVariant( QString( "Test" ) ) << false;
1491 QStringList stringlist;
1492 stringlist << "One" << "Two" << "Three";
1493 QTest::newRow( "stringlist_valid" ) << QVariant( stringlist ) << false;
1494 QTest::newRow( "time_invalid" ) << QVariant( QTime() ) << true;
1495 QTest::newRow( "time_valid" ) << QVariant( QTime( 14, 0, 0 ) ) << false;
1496 QTest::newRow( "uint_valid" ) << QVariant( (uint)123 ) << false;
1497 QTest::newRow( "qchar" ) << QVariant(QChar('a')) << false;
1498 QTest::newRow( "qchar_null" ) << QVariant(QChar(0)) << true;
1499 QTest::newRow( "regexp" ) << QVariant(QRegExp("foo", Qt::CaseInsensitive)) << false;
1500 QTest::newRow( "regexp_empty" ) << QVariant(QRegExp()) << false;
1502 // types known to QMetaType, but not part of QVariant::Type
1503 QTest::newRow("QMetaType::Long invalid") << QVariant(QMetaType::Long, (void *) 0) << false;
1505 QTest::newRow("QMetaType::Long") << QVariant(QMetaType::Long, &longInt) << false;
1506 QTest::newRow("QMetaType::Short invalid") << QVariant(QMetaType::Short, (void *) 0) << false;
1508 QTest::newRow("QMetaType::Short") << QVariant(QMetaType::Short, &shortInt) << false;
1509 QTest::newRow("QMetaType::Char invalid") << QVariant(QMetaType::Char, (void *) 0) << false;
1511 QTest::newRow("QMetaType::Char") << QVariant(QMetaType::Char, &ch) << false;
1512 QTest::newRow("QMetaType::ULong invalid") << QVariant(QMetaType::ULong, (void *) 0) << false;
1513 ulong ulongInt = 1ul;
1514 QTest::newRow("QMetaType::ULong") << QVariant(QMetaType::ULong, &ulongInt) << false;
1515 QTest::newRow("QMetaType::UShort invalid") << QVariant(QMetaType::UShort, (void *) 0) << false;
1516 ushort ushortInt = 1u;
1517 QTest::newRow("QMetaType::UShort") << QVariant(QMetaType::UShort, &ushortInt) << false;
1518 QTest::newRow("QMetaType::UChar invalid") << QVariant(QMetaType::UChar, (void *) 0) << false;
1520 QTest::newRow("QMetaType::UChar") << QVariant(QMetaType::UChar, &uch) << false;
1521 QTest::newRow("QMetaType::Float invalid") << QVariant(QMetaType::Float, (void *) 0) << false;
1523 QTest::newRow("QMetaType::Float") << QVariant(QMetaType::Float, &f) << false;
1524 CustomStreamableClass custom = {123};
1525 QTest::newRow("Custom type") << QVariant::fromValue(custom) << false;
1528 void tst_QVariant::writeToReadFromDataStream()
1530 QFETCH( QVariant, writeVariant );
1531 QFETCH( bool, isNull );
1534 QDataStream writeStream( &data, QIODevice::WriteOnly );
1535 writeStream << writeVariant;
1537 QVariant readVariant;
1538 QDataStream readStream( &data, QIODevice::ReadOnly );
1539 readStream >> readVariant;
1540 QVERIFY( readVariant.isNull() == isNull );
1541 // Best way to confirm the readVariant contains the same data?
1542 // Since only a few won't match since the serial numbers are different
1543 // I won't bother adding another bool in the data test.
1544 const int writeType = writeVariant.userType();
1545 if (writeType == qMetaTypeId<CustomStreamableClass>())
1546 QCOMPARE(qvariant_cast<CustomStreamableClass>(readVariant), qvariant_cast<CustomStreamableClass>(writeVariant));
1547 else if ( writeType != QVariant::Invalid && writeType != QVariant::Bitmap && writeType != QVariant::Pixmap
1548 && writeType != QVariant::Image) {
1549 switch (writeType) {
1551 QCOMPARE( readVariant, writeVariant );
1554 // compare types know by QMetaType but not QVariant (QVariant::operator==() knows nothing about them)
1555 case QMetaType::Long:
1556 QCOMPARE(qvariant_cast<long>(readVariant), qvariant_cast<long>(writeVariant));
1558 case QMetaType::ULong:
1559 QCOMPARE(qvariant_cast<ulong>(readVariant), qvariant_cast<ulong>(writeVariant));
1561 case QMetaType::Short:
1562 QCOMPARE(qvariant_cast<short>(readVariant), qvariant_cast<short>(writeVariant));
1564 case QMetaType::UShort:
1565 QCOMPARE(qvariant_cast<ushort>(readVariant), qvariant_cast<ushort>(writeVariant));
1567 case QMetaType::Char:
1568 QCOMPARE(qvariant_cast<char>(readVariant), qvariant_cast<char>(writeVariant));
1570 case QMetaType::UChar:
1571 QCOMPARE(qvariant_cast<uchar>(readVariant), qvariant_cast<uchar>(writeVariant));
1573 case QMetaType::Float:
1575 // the uninitialized float can be NaN (observed on Windows Mobile 5 ARMv4i)
1576 float readFloat = qvariant_cast<float>(readVariant);
1577 float writtenFloat = qvariant_cast<float>(writeVariant);
1578 QVERIFY(qIsNaN(readFloat) == qIsNaN(writtenFloat));
1579 if (!qIsNaN(readFloat))
1580 QVERIFY(readFloat == writtenFloat);
1587 void tst_QVariant::writeToReadFromOldDataStream()
1589 QVariant writeVariant = QString("hello");
1592 QDataStream writeStream(&data, QIODevice::WriteOnly);
1593 writeStream.setVersion(QDataStream::Qt_2_1);
1594 writeStream << writeVariant;
1596 QVariant readVariant;
1597 QDataStream readStream(&data, QIODevice::ReadOnly);
1598 readStream.setVersion(QDataStream::Qt_2_1);
1599 readStream >> readVariant;
1601 QCOMPARE(writeVariant.userType(), readVariant.userType());
1602 QCOMPARE(writeVariant, readVariant);
1605 void tst_QVariant::checkDataStream()
1607 QTest::ignoreMessage(QtWarningMsg, "Trying to construct an instance of an invalid type, type id: 46");
1608 const QByteArray settingsHex("0000002effffffffff");
1609 const QByteArray settings = QByteArray::fromHex(settingsHex);
1610 QDataStream in(settings);
1613 // the line below has been left out for now since the data stream
1614 // is not necessarily considered corrupt when an invalid QVariant is
1615 // constructed. However, it might be worth considering changing that behavior
1617 // QCOMPARE(in.status(), QDataStream::ReadCorruptData);
1618 QCOMPARE(v.type(), QVariant::Invalid);
1621 void tst_QVariant::operator_eq_eq_data()
1623 QTest::addColumn<QVariant>("left");
1624 QTest::addColumn<QVariant>("right");
1625 QTest::addColumn<bool>("equal"); // left == right ?
1628 QVariant i0( int(0) );
1629 QVariant i1( int(1) );
1631 QTest::newRow( "invinv" ) << inv << inv << true;
1633 QTest::newRow( "int1int1" ) << i1 << i1 << true;
1634 QTest::newRow( "int1int0" ) << i1 << i0 << false;
1635 QTest::newRow( "nullint" ) << i0 << QVariant(QVariant::Int) << false;
1637 // LongLong and ULongLong
1638 QVariant ll1( (qlonglong)1 );
1639 QVariant lln2( (qlonglong)-2 );
1640 QVariant ull1( (qulonglong)1 );
1641 QVariant ull3( (qulonglong)3 );
1642 QTest::newRow( "ll1ll1" ) << ll1 << ll1 << true;
1643 QTest::newRow( "ll1lln2" ) << ll1 << lln2 << false;
1644 QTest::newRow( "ll1ull1" ) << ull1 << ull1 << true;
1645 QTest::newRow( "ll1i1" ) << ull1 << i1 << true;
1646 QTest::newRow( "ull1ull1" ) << ull1 << ull1 << true;
1647 QTest::newRow( "ull1i1" ) << ull1 << ull1 << true;
1650 QVariant mIntString(QByteArray("-42"));
1651 QVariant mIntQString(QString("-42"));
1653 QVariant mUInt(42u);
1654 QVariant mUIntString(QByteArray("42"));
1655 QVariant mUIntQString(QString("42"));
1657 QVariant mDouble(42.11);
1658 QVariant mDoubleString(QByteArray("42.11"));
1659 QVariant mDoubleQString(QString("42.11"));
1661 QVariant mFloat(42.11f);
1662 QVariant mFloatString(QByteArray("42.11"));
1663 QVariant mFloatQString(QString("42.11"));
1665 QVariant mLongLong((qlonglong)-42);
1666 QVariant mLongLongString(QByteArray("-42"));
1667 QVariant mLongLongQString(QString("-42"));
1669 QVariant mULongLong((qulonglong)42);
1670 QVariant mULongLongString(QByteArray("42"));
1671 QVariant mULongLongQString(QString("42"));
1673 QVariant mBool(false);
1674 QVariant mBoolString(QByteArray("false"));
1675 QVariant mBoolQString(QString("false"));
1677 QTest::newRow( "double_int" ) << QVariant(42.0) << QVariant(42) << true;
1678 QTest::newRow( "float_int" ) << QVariant(42.f) << QVariant(42) << true;
1679 QTest::newRow( "mInt_mIntString" ) << mInt << mIntString << true;
1680 QTest::newRow( "mIntString_mInt" ) << mIntString << mInt << true;
1681 QTest::newRow( "mInt_mIntQString" ) << mInt << mIntQString << true;
1682 QTest::newRow( "mIntQString_mInt" ) << mIntQString << mInt << true;
1684 QTest::newRow( "mUInt_mUIntString" ) << mUInt << mUIntString << true;
1685 QTest::newRow( "mUIntString_mUInt" ) << mUIntString << mUInt << true;
1686 QTest::newRow( "mUInt_mUIntQString" ) << mUInt << mUIntQString << true;
1687 QTest::newRow( "mUIntQString_mUInt" ) << mUIntQString << mUInt << true;
1689 QTest::newRow( "mDouble_mDoubleString" ) << mDouble << mDoubleString << true;
1690 QTest::newRow( "mDoubleString_mDouble" ) << mDoubleString << mDouble << true;
1691 QTest::newRow( "mDouble_mDoubleQString" ) << mDouble << mDoubleQString << true;
1692 QTest::newRow( "mDoubleQString_mDouble" ) << mDoubleQString << mDouble << true;
1694 QTest::newRow( "mFloat_mFloatString" ) << mFloat << mFloatString << true;
1695 QTest::newRow( "mFloatString_mFloat" ) << mFloatString << mFloat << true;
1696 QTest::newRow( "mFloat_mFloatQString" ) << mFloat << mFloatQString << true;
1697 QTest::newRow( "mFloatQString_mFloat" ) << mFloatQString << mFloat << true;
1699 QTest::newRow( "mLongLong_mLongLongString" ) << mLongLong << mLongLongString << true;
1700 QTest::newRow( "mLongLongString_mLongLong" ) << mLongLongString << mLongLong << true;
1701 QTest::newRow( "mLongLong_mLongLongQString" ) << mLongLong << mLongLongQString << true;
1702 QTest::newRow( "mLongLongQString_mLongLong" ) << mLongLongQString << mLongLong << true;
1704 QTest::newRow( "mULongLong_mULongLongString" ) << mULongLong << mULongLongString << true;
1705 QTest::newRow( "mULongLongString_mULongLong" ) << mULongLongString << mULongLong << true;
1706 QTest::newRow( "mULongLong_mULongLongQString" ) << mULongLong << mULongLongQString << true;
1707 QTest::newRow( "mULongLongQString_mULongLong" ) << mULongLongQString << mULongLong << true;
1709 QTest::newRow( "mBool_mBoolString" ) << mBool << mBoolString << true;
1710 QTest::newRow( "mBoolString_mBool" ) << mBoolString << mBool << true;
1711 QTest::newRow( "mBool_mBoolQString" ) << mBool << mBoolQString << true;
1712 QTest::newRow( "mBoolQString_mBool" ) << mBoolQString << mBool << true;
1714 QTest::newRow("ba2qstring") << QVariant(QLatin1String("hallo")) << QVariant(QString("hallo")) << true;
1715 QTest::newRow("qstring2ba") << QVariant(QString("hallo")) << QVariant(QLatin1String("hallo")) << true;
1716 QTest::newRow("char_char") << QVariant(QChar('a')) << QVariant(QChar('a')) << true;
1717 QTest::newRow("char_char2") << QVariant(QChar('a')) << QVariant(QChar('b')) << false;
1719 QTest::newRow("invalidConversion") << QVariant(QString("bubu")) << QVariant(0) << false;
1720 QTest::newRow("invalidConversionR") << QVariant(0) << QVariant(QString("bubu")) << false;
1721 // ### many other combinations missing
1724 QMap<QString, QVariant> map1;
1725 map1.insert( "X", 1 );
1727 QMap<QString, QVariant> map2;
1728 map2.insert( "Y", 1 );
1730 QTest::newRow("TwoItemsInEqual") << QVariant(map1) << QVariant(map2) << false;
1735 QMap<QString, QVariant> map1;
1736 map1.insert( "X", 1 );
1738 QMap<QString, QVariant> map2;
1739 map2.insert( "X", 1 );
1741 QTest::newRow("TwoItemsEqual") << QVariant(map1) << QVariant(map2) << true;
1745 QMap<QString, QVariant> map1;
1746 map1.insert( "X", 1 );
1748 QMap<QString, QVariant> map2;
1750 QTest::newRow("PopulatedEmptyMap") << QVariant(map1) << QVariant(map2) << false;
1754 QMap<QString, QVariant> map1;
1756 QMap<QString, QVariant> map2;
1757 map2.insert( "X", 1 );
1759 QTest::newRow("EmptyPopulatedMap") << QVariant(map1) << QVariant(map2) << false;
1763 QMap<QString, QVariant> map1;
1764 map1.insert( "X", 1 );
1765 map1.insert( "Y", 1 );
1767 QMap<QString, QVariant> map2;
1768 map2.insert( "X", 1 );
1770 QTest::newRow("FirstLarger") << QVariant(map1) << QVariant(map2) << false;
1774 QMap<QString, QVariant> map1;
1775 map1.insert( "X", 1 );
1777 QMap<QString, QVariant> map2;
1778 map2.insert( "X", 1 );
1779 map2.insert( "Y", 1 );
1781 QTest::newRow("SecondLarger") << QVariant(map1) << QVariant(map2) << false;
1784 // same thing with hash
1786 QHash<QString, QVariant> hash1;
1787 hash1.insert( "X", 1 );
1789 QHash<QString, QVariant> hash2;
1790 hash2.insert( "Y", 1 );
1792 QTest::newRow("HashTwoItemsInEqual") << QVariant(hash1) << QVariant(hash2) << false;
1797 QHash<QString, QVariant> hash1;
1798 hash1.insert( "X", 1 );
1800 QHash<QString, QVariant> hash2;
1801 hash2.insert( "X", 1 );
1803 QTest::newRow("HashTwoItemsEqual") << QVariant(hash1) << QVariant(hash2) << true;
1807 QHash<QString, QVariant> hash1;
1808 hash1.insert( "X", 1 );
1810 QHash<QString, QVariant> hash2;
1812 QTest::newRow("HashPopulatedEmptyHash") << QVariant(hash1) << QVariant(hash2) << false;
1816 QHash<QString, QVariant> hash1;
1818 QHash<QString, QVariant> hash2;
1819 hash2.insert( "X", 1 );
1821 QTest::newRow("EmptyPopulatedHash") << QVariant(hash1) << QVariant(hash2) << false;
1825 QHash<QString, QVariant> hash1;
1826 hash1.insert( "X", 1 );
1827 hash1.insert( "Y", 1 );
1829 QHash<QString, QVariant> hash2;
1830 hash2.insert( "X", 1 );
1832 QTest::newRow("HashFirstLarger") << QVariant(hash1) << QVariant(hash2) << false;
1836 QHash<QString, QVariant> hash1;
1837 hash1.insert( "X", 1 );
1839 QHash<QString, QVariant> hash2;
1840 hash2.insert( "X", 1 );
1841 hash2.insert( "Y", 1 );
1843 QTest::newRow("HashSecondLarger") << QVariant(hash1) << QVariant(hash2) << false;
1846 QTest::newRow( "UserType" ) << QVariant(QVariant::UserType) << QVariant(QVariant::UserType) << true;
1847 QVariant mUserType(QVariant::UserType);
1848 QTest::newRow( "Shared UserType" ) << mUserType << mUserType << true;
1851 void tst_QVariant::operator_eq_eq()
1853 QFETCH( QVariant, left );
1854 QFETCH( QVariant, right );
1855 QFETCH( bool, equal );
1856 QEXPECT_FAIL("nullint", "See QTBUG-22933", Continue);
1857 QCOMPARE( left == right, equal );
1860 void tst_QVariant::operator_eq_eq_rhs()
1868 /* This should _not_ compile */
1872 QVERIFY(dt == list);
1876 void tst_QVariant::typeName_data()
1878 QTest::addColumn<int>("type");
1879 QTest::addColumn<QByteArray>("res");
1880 QTest::newRow("0") << int(QVariant::Invalid) << QByteArray("");
1881 QTest::newRow("1") << int(QVariant::Map) << QByteArray("QVariantMap");
1882 QTest::newRow("2") << int(QVariant::List) << QByteArray("QVariantList");
1883 QTest::newRow("3") << int(QVariant::String) << QByteArray("QString");
1884 QTest::newRow("4") << int(QVariant::StringList) << QByteArray("QStringList");
1885 QTest::newRow("5") << int(QVariant::Font) << QByteArray("QFont");
1886 QTest::newRow("6") << int(QVariant::Pixmap) << QByteArray("QPixmap");
1887 QTest::newRow("7") << int(QVariant::Brush) << QByteArray("QBrush");
1888 QTest::newRow("8") << int(QVariant::Rect) << QByteArray("QRect");
1889 QTest::newRow("9") << int(QVariant::Size) << QByteArray("QSize");
1890 QTest::newRow("10") << int(QVariant::Color) << QByteArray("QColor");
1891 QTest::newRow("11") << int(QVariant::Palette) << QByteArray("QPalette");
1892 QTest::newRow("12") << int(QVariant::Point) << QByteArray("QPoint");
1893 QTest::newRow("13") << int(QVariant::Image) << QByteArray("QImage");
1894 QTest::newRow("14") << int(QVariant::Int) << QByteArray("int");
1895 QTest::newRow("15") << int(QVariant::UInt) << QByteArray("uint");
1896 QTest::newRow("16") << int(QVariant::Bool) << QByteArray("bool");
1897 QTest::newRow("17") << int(QVariant::Double) << QByteArray("double");
1898 QTest::newRow("18") << int(QMetaType::Float) << QByteArray("float");
1899 QTest::newRow("19") << int(QVariant::Polygon) << QByteArray("QPolygon");
1900 QTest::newRow("20") << int(QVariant::Region) << QByteArray("QRegion");
1901 QTest::newRow("21") << int(QVariant::Bitmap) << QByteArray("QBitmap");
1902 QTest::newRow("22") << int(QVariant::Cursor) << QByteArray("QCursor");
1903 QTest::newRow("23") << int(QVariant::SizePolicy) << QByteArray("QSizePolicy");
1904 QTest::newRow("24") << int(QVariant::Date) << QByteArray("QDate");
1905 QTest::newRow("25") << int(QVariant::Time) << QByteArray("QTime");
1906 QTest::newRow("26") << int(QVariant::DateTime) << QByteArray("QDateTime");
1907 QTest::newRow("27") << int(QVariant::ByteArray) << QByteArray("QByteArray");
1908 QTest::newRow("28") << int(QVariant::BitArray) << QByteArray("QBitArray");
1909 QTest::newRow("29") << int(QVariant::KeySequence) << QByteArray("QKeySequence");
1910 QTest::newRow("30") << int(QVariant::Pen) << QByteArray("QPen");
1911 QTest::newRow("31") << int(QVariant::LongLong) << QByteArray("qlonglong");
1912 QTest::newRow("32") << int(QVariant::ULongLong) << QByteArray("qulonglong");
1913 QTest::newRow("33") << int(QVariant::Char) << QByteArray("QChar");
1914 QTest::newRow("34") << int(QVariant::Url) << QByteArray("QUrl");
1915 QTest::newRow("35") << int(QVariant::TextLength) << QByteArray("QTextLength");
1916 QTest::newRow("36") << int(QVariant::TextFormat) << QByteArray("QTextFormat");
1917 QTest::newRow("37") << int(QVariant::Locale) << QByteArray("QLocale");
1918 QTest::newRow("38") << int(QVariant::LineF) << QByteArray("QLineF");
1919 QTest::newRow("39") << int(QVariant::RectF) << QByteArray("QRectF");
1920 QTest::newRow("40") << int(QVariant::PointF) << QByteArray("QPointF");
1921 QTest::newRow("41") << int(QVariant::RegExp) << QByteArray("QRegExp");
1922 QTest::newRow("42") << int(QVariant::UserType) << QByteArray("UserType");
1923 QTest::newRow("43") << int(QVariant::Matrix) << QByteArray("QMatrix");
1924 QTest::newRow("44") << int(QVariant::Transform) << QByteArray("QTransform");
1925 QTest::newRow("45") << int(QVariant::Hash) << QByteArray("QVariantHash");
1926 QTest::newRow("46") << int(QVariant::Matrix4x4) << QByteArray("QMatrix4x4");
1927 QTest::newRow("47") << int(QVariant::Vector2D) << QByteArray("QVector2D");
1928 QTest::newRow("48") << int(QVariant::Vector3D) << QByteArray("QVector3D");
1929 QTest::newRow("49") << int(QVariant::Vector4D) << QByteArray("QVector4D");
1930 QTest::newRow("50") << int(QVariant::Quaternion) << QByteArray("QQuaternion");
1933 void tst_QVariant::typeName()
1935 QFETCH( int, type );
1936 QFETCH( QByteArray, res );
1937 QCOMPARE(QString::fromLatin1(QVariant::typeToName((QVariant::Type)type)),
1938 QString::fromLatin1(res.constData()));
1941 // test nameToType as well
1942 void tst_QVariant::typeToName()
1945 QCOMPARE( QVariant::typeToName( v.type() ), (const char*)0 ); // Invalid
1946 // assumes that QVariant::Type contains consecutive values
1948 int max = QVariant::LastGuiType;
1949 for ( int t = 1; t <= max; t++ ) {
1950 const char *n = QVariant::typeToName( (QVariant::Type)t );
1952 QCOMPARE( int(QVariant::nameToType( n )), t );
1955 QCOMPARE(QVariant::typeToName(QVariant::Int), "int");
1956 // not documented but we return 0 if the type is out of range
1957 // by testing this we catch cases where QVariant is extended
1958 // but type_map is not updated accordingly
1959 QCOMPARE( QVariant::typeToName( QVariant::Type(max+1) ), (char*)0 );
1960 // invalid type names
1961 QVERIFY( QVariant::nameToType( 0 ) == QVariant::Invalid );
1962 QVERIFY( QVariant::nameToType( "" ) == QVariant::Invalid );
1963 QVERIFY( QVariant::nameToType( "foo" ) == QVariant::Invalid );
1965 // We don't support these old (Qt3) types anymore.
1966 QCOMPARE(QVariant::nameToType("QIconSet"), QVariant::Invalid);
1967 QCOMPARE(QVariant::nameToType("Q3CString"), QVariant::Invalid);
1968 QCOMPARE(QVariant::nameToType("Q_LLONG"), QVariant::Invalid);
1969 QCOMPARE(QVariant::nameToType("Q_ULLONG"), QVariant::Invalid);
1972 void tst_QVariant::streamInvalidVariant()
1978 QVariant writeVariant;
1979 QVariant readVariant;
1981 QVERIFY( writeVariant.type() == QVariant::Invalid );
1984 QDataStream writeStream( &data, QIODevice::WriteOnly );
1985 writeStream << writeX << writeVariant << writeY;
1987 QDataStream readStream( &data, QIODevice::ReadOnly );
1988 readStream >> readX >> readVariant >> readY;
1990 QVERIFY( readX == writeX );
1991 // Two invalid QVariant's aren't necessarily the same, so == will
1992 // return false if one is invalid, so check the type() instead
1993 QVERIFY( readVariant.type() == QVariant::Invalid );
1994 QVERIFY( readY == writeY );
1997 static int instanceCount = 0;
2001 MyType(int n = 0, const char *t=0): number(n), text(t)
2005 MyType(const MyType &other)
2006 : number(other.number), text(other.text)
2018 Q_DECLARE_METATYPE(MyType)
2019 Q_DECLARE_METATYPE(MyType*)
2021 void tst_QVariant::userType()
2024 MyType data(1, "eins");
2025 MyType data2(2, "zwei");
2029 qVariantSetValue(userVar, data);
2031 QCOMPARE(userVar.type(), QVariant::UserType);
2032 QCOMPARE(userVar.typeName(), "MyType");
2033 QVERIFY(!userVar.isNull());
2034 QVERIFY(!userVar.canConvert(QVariant::String));
2035 QVERIFY(!userVar.canConvert(QVariant::UserType));
2037 QVariant userVar2(userVar);
2038 QVERIFY(userVar == userVar2);
2040 qVariantSetValue(userVar2, data2);
2041 QVERIFY(userVar != userVar2);
2043 const MyType *varData = static_cast<const MyType *>(userVar.constData());
2045 QCOMPARE(varData->number, data.number);
2046 QCOMPARE(varData->text, data.text);
2049 qVariantSetValue(userVar3, data2);
2051 userVar3 = userVar2;
2052 QVERIFY(userVar2 == userVar3);
2054 // At this point all QVariants got destroyed but we have 2 MyType instances.
2055 QCOMPARE(instanceCount, 2);
2058 qVariantSetValue(userVar, &data);
2060 QCOMPARE(userVar.type(), QVariant::UserType);
2061 QCOMPARE(userVar.typeName(), "MyType*");
2062 QVERIFY(!userVar.isNull());
2063 QVERIFY(!userVar.canConvert(QVariant::String));
2064 QVERIFY(!userVar.canConvert(QVariant::UserType));
2066 QVariant userVar2(userVar);
2067 QVERIFY(userVar == userVar2);
2069 qVariantSetValue(userVar2, &data2);
2070 QVERIFY(userVar != userVar2);
2072 MyType * const*varData = reinterpret_cast<MyType *const *>(userVar.constData());
2074 QCOMPARE(*varData, &data);
2077 qVariantSetValue(userVar3, &data2);
2079 /* This check is correct now. userVar2 contains a pointer to data2 and so
2081 QVERIFY(userVar2 == userVar3);
2083 userVar3 = userVar2;
2084 QVERIFY(userVar2 == userVar3);
2087 QCOMPARE(instanceCount, 2);
2089 qVariantSetValue(myCarrier, data);
2090 QCOMPARE(instanceCount, 3);
2092 QVariant second = myCarrier;
2093 QCOMPARE(instanceCount, 3);
2095 QCOMPARE(instanceCount, 4);
2097 QCOMPARE(instanceCount, 3);
2099 MyType data3(0, "null");
2100 data3 = qVariantValue<MyType>(myCarrier);
2101 QCOMPARE(data3.number, 1);
2102 QCOMPARE(data3.text, (const char *)"eins");
2104 QCOMPARE(instanceCount, 4);
2110 const MyType data(3, "drei");
2113 qVariantSetValue(myCarrier, data);
2114 QCOMPARE(myCarrier.typeName(), "MyType");
2116 const MyType data2 = qvariant_cast<MyType>(myCarrier);
2117 QCOMPARE(data2.number, 3);
2118 QCOMPARE(data2.text, (const char *)"drei");
2125 qVariantSetValue(myCarrier, s);
2126 QCOMPARE((int)qvariant_cast<short>(myCarrier), 42);
2130 qlonglong ll = Q_INT64_C(42);
2133 qVariantSetValue(myCarrier, ll);
2134 QCOMPARE(qvariant_cast<int>(myCarrier), 42);
2137 // At this point all QVariants got destroyed and MyType objects too.
2138 QCOMPARE(instanceCount, 0);
2146 Q_DECLARE_METATYPE(MyTypePOD)
2148 void tst_QVariant::podUserType()
2154 QVariant pod_as_variant = qVariantFromValue(pod);
2155 MyTypePOD pod2 = qvariant_cast<MyTypePOD>(pod_as_variant);
2157 QCOMPARE(pod.a, pod2.a);
2158 QCOMPARE(pod.b, pod2.b);
2160 qVariantSetValue(pod_as_variant, pod);
2161 pod2 = qVariantValue<MyTypePOD>(pod_as_variant);
2163 QCOMPARE(pod.a, pod2.a);
2164 QCOMPARE(pod.b, pod2.b);
2167 void tst_QVariant::basicUserType()
2172 v = QVariant(QMetaType::Int, &i);
2174 QCOMPARE(v.type(), QVariant::Int);
2175 QCOMPARE(v.toInt(), 7);
2179 v = QVariant(QMetaType::QString, &s);
2181 QCOMPARE(v.type(), QVariant::String);
2182 QCOMPARE(v.toString(), QString("foo"));
2186 v = QVariant(QMetaType::Double, &d);
2188 QCOMPARE(v.type(), QVariant::Double);
2189 QCOMPARE(v.toDouble(), 4.4);
2193 v = QVariant(QMetaType::Float, &f);
2195 QCOMPARE(v.userType(), int(QMetaType::Float));
2196 QCOMPARE(v.toDouble(), 4.5);
2199 QByteArray ba("bar");
2200 v = QVariant(QMetaType::QByteArray, &ba);
2202 QCOMPARE(v.type(), QVariant::ByteArray);
2203 QCOMPARE(v.toByteArray(), QByteArray("bar"));
2206 void tst_QVariant::data_()
2213 QVariant ll = (qlonglong)2;
2214 QVariant ull = (qulonglong)3;
2215 QVariant s(QString("hallo"));
2216 QVariant r(QRect(1,2,3,4));
2220 QCOMPARE(*static_cast<int *>(v.data()), i.toInt());
2224 QCOMPARE(*static_cast<double *>(v.data()), d.toDouble());
2228 QCOMPARE(*static_cast<float *>(v.data()), qVariantValue<float>(v));
2232 QCOMPARE(*static_cast<qlonglong *>(v.data()), ll.toLongLong());
2236 QCOMPARE(*static_cast<qulonglong *>(v.data()), ull.toULongLong());
2240 QCOMPARE(*static_cast<QString *>(v.data()), s.toString());
2244 QCOMPARE(*static_cast<QRect *>(v.data()), r.toRect());
2247 void tst_QVariant::constData()
2260 QVERIFY(v.constData());
2261 QCOMPARE(*static_cast<const int *>(v.constData()), i);
2264 QVERIFY(v.constData());
2265 QCOMPARE(*static_cast<const double *>(v.constData()), d);
2268 QVERIFY(v.constData());
2269 QCOMPARE(*static_cast<const float *>(v.constData()), f);
2272 QVERIFY(v.constData());
2273 QCOMPARE(*static_cast<const qlonglong *>(v.constData()), ll);
2276 QVERIFY(v.constData());
2277 QCOMPARE(*static_cast<const qulonglong *>(v.constData()), ull);
2280 QVERIFY(v.constData());
2281 QCOMPARE(*static_cast<const QString *>(v.constData()), s);
2284 QVERIFY(v.constData());
2285 QCOMPARE(*static_cast<const QRect *>(v.constData()), r);
2294 Q_DECLARE_METATYPE(Foo)
2296 void tst_QVariant::variant_to()
2305 sl << QLatin1String("blah");
2307 qVariantSetValue(v3, sl);
2312 qVariantSetValue(v4, foo);
2314 QCOMPARE(qvariant_cast<double>(v1), 4.2);
2315 QCOMPARE(qvariant_cast<float>(v1), 4.2f);
2316 QCOMPARE(qvariant_cast<int>(v2), 5);
2317 QCOMPARE(qvariant_cast<QStringList>(v3), sl);
2318 QCOMPARE(qvariant_cast<QString>(v3), QString::fromLatin1("blah"));
2320 QCOMPARE(qvariant_cast<Foo>(v4).i, 42);
2323 QCOMPARE(qvariant_cast<Foo>(v5).i, 0);
2325 QCOMPARE(qvariant_cast<int>(v1), 4);
2327 QVariant n = qVariantFromValue<short>(42);
2328 QCOMPARE(qvariant_cast<int>(n), 42);
2329 QCOMPARE(qvariant_cast<uint>(n), 42u);
2330 QCOMPARE(qvariant_cast<double>(n), 42.0);
2331 QCOMPARE(qvariant_cast<float>(n), 42.f);
2332 QCOMPARE(qvariant_cast<short>(n), short(42));
2333 QCOMPARE(qvariant_cast<ushort>(n), ushort(42));
2335 n = qVariantFromValue(43l);
2336 QCOMPARE(qvariant_cast<int>(n), 43);
2337 QCOMPARE(qvariant_cast<uint>(n), 43u);
2338 QCOMPARE(qvariant_cast<double>(n), 43.0);
2339 QCOMPARE(qvariant_cast<float>(n), 43.f);
2340 QCOMPARE(qvariant_cast<long>(n), 43l);
2342 n = QLatin1String("44");
2343 QCOMPARE(qvariant_cast<int>(n), 44);
2344 QCOMPARE(qvariant_cast<ulong>(n), 44ul);
2345 QCOMPARE(qvariant_cast<float>(n), 44.0f);
2347 QCOMPARE(qVariantFromValue(0.25f).toDouble(), 0.25);
2350 struct Blah { int i; };
2352 QDataStream& operator>>(QDataStream& s, Blah& c)
2353 { return (s >> c.i); }
2355 QDataStream& operator<<(QDataStream& s, const Blah& c)
2356 { return (s << c.i); }
2358 void tst_QVariant::saveLoadCustomTypes()
2363 int tp = qRegisterMetaType<Blah>("Blah");
2364 QVariant v = QVariant(tp, &i);
2366 qRegisterMetaTypeStreamOperators<Blah>("Blah");
2368 QCOMPARE(v.userType(), tp);
2369 QVERIFY(v.type() == QVariant::UserType);
2371 QDataStream stream(&data, QIODevice::WriteOnly);
2378 QDataStream stream(data);
2382 QCOMPARE(int(v.userType()), QMetaType::type("Blah"));
2383 int value = *(int*)v.constData();
2384 QCOMPARE(value, 42);
2387 void tst_QVariant::url()
2389 QString str("http://qt.nokia.com");
2392 QVariant v(url); //built with a QUrl
2396 QVariant v3(str); //built with a QString
2398 QCOMPARE(v2.toUrl(), url);
2399 QVERIFY(qVariantCanConvert<QUrl>(v3));
2400 QCOMPARE(v2.toUrl(), v3.toUrl());
2402 QVERIFY(qVariantCanConvert<QString>(v2));
2403 QCOMPARE(v2.toString(), str);
2404 QCOMPARE(v3.toString(), str);
2407 void tst_QVariant::globalColor()
2409 QVariant variant(Qt::blue);
2410 QVERIFY(variant.type() == QVariant::Color);
2411 QVERIFY(qVariantValue<QColor>(variant) == QColor(Qt::blue));
2414 void tst_QVariant::variantMap()
2416 QMap<QString, QVariant> map;
2420 QVariantMap map2 = qvariant_cast<QVariantMap>(v);
2422 QCOMPARE(map2.value("test").toInt(), 42);
2424 QVariant v2 = QVariant(QMetaType::type("QVariantMap"), &map);
2425 QCOMPARE(qvariant_cast<QVariantMap>(v2).value("test").toInt(), 42);
2427 QVariant v3 = QVariant(QMetaType::type("QMap<QString, QVariant>"), &map);
2428 QCOMPARE(qvariant_cast<QVariantMap>(v3).value("test").toInt(), 42);
2431 void tst_QVariant::variantHash()
2433 QHash<QString, QVariant> hash;
2437 QVariantHash hash2 = qvariant_cast<QVariantHash>(v);
2439 QCOMPARE(hash2.value("test").toInt(), 42);
2441 QVariant v2 = QVariant(QMetaType::type("QVariantHash"), &hash);
2442 QCOMPARE(qvariant_cast<QVariantHash>(v2).value("test").toInt(), 42);
2444 QVariant v3 = QVariant(QMetaType::type("QHash<QString, QVariant>"), &hash);
2445 QCOMPARE(qvariant_cast<QVariantHash>(v3).value("test").toInt(), 42);
2448 void tst_QVariant::invalidQColor() const
2450 QVariant va("An invalid QColor::name() value.");
2451 QVERIFY(va.canConvert(QVariant::Color));
2453 QVERIFY(!va.convert(QVariant::Color));
2455 QVERIFY(!qvariant_cast<QColor>(va).isValid());
2458 void tst_QVariant::qvariant_cast_QObject_data() {
2460 QTest::addColumn<QVariant>("data");
2461 QTest::addColumn<bool>("success");
2462 QObject *obj = new QObject(this);
2463 obj->setObjectName(QString::fromLatin1("Hello"));
2464 QTest::newRow("from QObject") << QVariant(QMetaType::QObjectStar, &obj) << true;
2465 QTest::newRow("from QObject2") << QVariant::fromValue(obj) << true;
2466 QTest::newRow("from String") << QVariant(QLatin1String("1, 2, 3")) << false;
2467 QTest::newRow("from int") << QVariant((int) 123) << false;
2471 void tst_QVariant::qvariant_cast_QObject() {
2472 QFETCH(QVariant, data);
2473 QFETCH(bool, success);
2475 QObject *o = qvariant_cast<QObject *>(data);
2476 QCOMPARE(o != 0, success);
2478 QCOMPARE(o->objectName(), QString::fromLatin1("Hello"));
2482 Q_DECLARE_METATYPE(qint8);
2484 void tst_QVariant::convertToQUint8() const
2488 const qint8 anInt = 32;
2490 /* QVariant(int) gets invoked here so the QVariant has nothing with qint8 to do.
2491 * It's of type QVariant::Int. */
2492 const QVariant v0 = anInt;
2494 QVERIFY(qVariantCanConvert<qint8>(v0));
2495 QCOMPARE(int(qvariant_cast<qint8>(v0)), 32);
2496 QCOMPARE(int(v0.toInt()), 32);
2497 QCOMPARE(v0.toString(), QString("32"));
2499 QCOMPARE(int(qvariant_cast<qlonglong>(v0)), 32);
2500 QCOMPARE(int(qvariant_cast<char>(v0)), 32);
2501 QCOMPARE(int(qvariant_cast<short>(v0)), 32);
2502 QCOMPARE(int(qvariant_cast<long>(v0)), 32);
2503 QCOMPARE(int(qvariant_cast<float>(v0)), 32);
2504 QCOMPARE(int(qvariant_cast<double>(v0)), 32);
2509 const quint8 anInt = 32;
2510 const QVariant v0 = anInt;
2512 QVERIFY(qVariantCanConvert<quint8>(v0));
2513 QCOMPARE(int(qvariant_cast<quint8>(v0)), 32);
2514 QCOMPARE(int(v0.toUInt()), 32);
2515 QCOMPARE(v0.toString(), QString("32"));
2520 const qint16 anInt = 32;
2521 const QVariant v0 = anInt;
2523 QVERIFY(qVariantCanConvert<qint16>(v0));
2524 QCOMPARE(int(qvariant_cast<qint16>(v0)), 32);
2525 QCOMPARE(int(v0.toInt()), 32);
2526 QCOMPARE(v0.toString(), QString("32"));
2531 const quint16 anInt = 32;
2532 const QVariant v0 = anInt;
2534 QVERIFY(qVariantCanConvert<quint16>(v0));
2535 QCOMPARE(int(qvariant_cast<quint16>(v0)), 32);
2536 QCOMPARE(int(v0.toUInt()), 32);
2537 QCOMPARE(v0.toString(), QString("32"));
2541 void tst_QVariant::comparePointers() const
2549 QVariant v = qVariantFromValue<void *>(&myClass);
2550 QVariant v2 = qVariantFromValue<void *>(&myClass);
2556 Q_DECLARE_METATYPE(Data*)
2558 void tst_QVariant::voidStar() const
2565 v1 = qVariantFromValue(p1);
2569 v2 = qVariantFromValue(p2);
2573 v2 = qVariantFromValue(p2);
2577 void tst_QVariant::dataStar() const
2579 qRegisterMetaType<Data*>();
2580 Data *p1 = new Data;
2582 QVariant v1 = qVariantFromValue(p1);
2583 QCOMPARE(v1.userType(), qMetaTypeId<Data*>());
2584 QCOMPARE(qvariant_cast<Data*>(v1), p1);
2589 v2 = qVariantFromValue(p1);
2594 void tst_QVariant::canConvertQStringList() const
2596 QFETCH(bool, canConvert);
2597 QFETCH(QStringList, input);
2598 QFETCH(QString, result);
2602 QCOMPARE(v.canConvert(QVariant::String), canConvert);
2603 QCOMPARE(v.toString(), result);
2606 void tst_QVariant::canConvertQStringList_data() const
2608 QTest::addColumn<bool>("canConvert");
2609 QTest::addColumn<QStringList>("input");
2610 QTest::addColumn<QString>("result");
2612 QTest::newRow("An empty list") << false << QStringList() << QString();
2613 QTest::newRow("A single item") << true << QStringList(QLatin1String("foo")) << QString::fromLatin1("foo");
2614 QTest::newRow("A single, but empty item") << true << QStringList(QString()) << QString();
2619 QTest::newRow("Two items") << false << l << QString();
2622 QTest::newRow("Three items") << false << l << QString();
2625 template<typename T> void convertMetaType()
2627 QVERIFY(qVariantFromValue<T>(10).isValid());
2628 QVERIFY(qVariantFromValue<T>(10).canConvert(QVariant::Int));
2629 QCOMPARE(qVariantFromValue<T>(10).toInt(), 10);
2630 QCOMPARE(qVariantFromValue<T>(10), qVariantFromValue<T>(10));
2633 #define CONVERT_META_TYPE(Type) \
2634 convertMetaType<Type>(); \
2635 if (QTest::currentTestFailed()) \
2636 QFAIL("convertMetaType<" #Type "> failed");
2638 void tst_QVariant::canConvertMetaTypeToInt() const
2640 CONVERT_META_TYPE(long);
2641 CONVERT_META_TYPE(short);
2642 CONVERT_META_TYPE(short);
2643 CONVERT_META_TYPE(unsigned short);
2644 CONVERT_META_TYPE(ushort);
2645 CONVERT_META_TYPE(ulong);
2646 CONVERT_META_TYPE(unsigned long);
2647 CONVERT_META_TYPE(uchar);
2648 CONVERT_META_TYPE(unsigned char);
2649 CONVERT_META_TYPE(char);
2650 CONVERT_META_TYPE(uint);
2651 CONVERT_META_TYPE(unsigned int);
2654 #undef CONVERT_META_TYPE
2657 These calls should not produce any warnings.
2659 void tst_QVariant::variantToDateTimeWithoutWarnings() const
2662 const QVariant variant(QLatin1String("An invalid QDateTime string"));
2663 const QDateTime dateTime(variant.toDateTime());
2664 QVERIFY(!dateTime.isValid());
2668 QVariant v1(QLatin1String("xyz"));
2669 v1.convert(QVariant::DateTime);
2671 QVariant v2(QLatin1String("xyz"));
2672 QDateTime dt1(v2.toDateTime());
2674 const QVariant v3(QLatin1String("xyz"));
2675 const QDateTime dt2(v3.toDateTime());
2679 void tst_QVariant::invalidDateTime() const
2681 QVariant variant(QString::fromLatin1("Invalid date time string"));
2682 QVERIFY(!variant.toDateTime().isValid());
2683 QVERIFY(!variant.convert(QVariant::DateTime));
2688 MyClass() : myValue(0) {}
2692 Q_DECLARE_METATYPE( MyClass )
2694 void tst_QVariant::loadUnknownUserType()
2696 qRegisterMetaType<MyClass>("MyClass");
2697 char data[] = {0, 0, 0, 127, 0, 0, 0, 0, 8, 77, 121, 67, 108, 97, 115, 115, 0};
2699 QByteArray ba(data, sizeof(data));
2700 QDataStream ds(&ba, QIODevice::ReadOnly);
2703 QCOMPARE(ds.status(), QDataStream::ReadCorruptData);
2706 void tst_QVariant::loadBrokenUserType()
2708 char data[] = {0, 0, 0, 127, 0, 112 };
2710 QByteArray ba(data, sizeof(data));
2711 QDataStream ds(&ba, QIODevice::ReadOnly);
2714 QCOMPARE(ds.status(), QDataStream::ReadPastEnd);
2717 void tst_QVariant::invalidDate() const
2719 QString foo("Hello");
2720 QVariant variant(foo);
2721 QVERIFY(!variant.convert(QVariant::Date));
2724 QVERIFY(!variant.convert(QVariant::DateTime));
2727 QVERIFY(!variant.convert(QVariant::Time));
2730 QVERIFY(!variant.convert(QVariant::Int));
2733 QVERIFY(!variant.convert(QVariant::Double));
2736 QVERIFY(!variant.convert(QVariant::Type(QMetaType::Float)));
2741 int x,y,z,q,w,e,r,t;
2743 Q_DECLARE_METATYPE(WontCompare);
2745 void tst_QVariant::compareCustomTypes() const
2747 qRegisterMetaType<WontCompare>("WontCompare");
2751 const QVariant variant1(qVariantFromValue(f1));
2755 const QVariant variant2(qVariantFromValue(f2));
2757 /* We compare pointers. */
2758 QVERIFY(variant1 != variant2);
2759 QVERIFY(variant1 == variant1);
2760 QVERIFY(variant2 == variant2);
2763 void tst_QVariant::timeToDateTime() const
2765 const QVariant val(QTime::currentTime());
2766 QVERIFY(!val.canConvert(QVariant::DateTime));
2767 QVERIFY(!val.toDateTime().isValid());
2770 Q_DECLARE_METATYPE(QHostAddress)
2772 void tst_QVariant::copyingUserTypes() const
2776 const QHostAddress ha("127.0.0.1");
2777 qVariantSetValue(var, ha);
2780 QCOMPARE(qVariantValue<QHostAddress>(var3), ha);
2783 void tst_QVariant::convertBoolToByteArray() const
2785 QFETCH(QByteArray, input);
2786 QFETCH(bool, canConvert);
2787 QFETCH(bool, value);
2789 const QVariant variant(input);
2791 QCOMPARE(qVariantCanConvert<bool>(variant), canConvert);
2794 /* Just call this function so we run the code path. */
2795 QCOMPARE(variant.toBool(), value);
2799 void tst_QVariant::convertBoolToByteArray_data() const
2801 QTest::addColumn<QByteArray>("input");
2802 QTest::addColumn<bool>("canConvert");
2803 QTest::addColumn<bool>("value");
2805 QTest::newRow("false")
2806 << QByteArray("false")
2810 QTest::newRow("FALSE")
2811 << QByteArray("FALSE")
2815 QTest::newRow("falSE")
2816 << QByteArray("FALSE")
2825 QTest::newRow("null QByteArray")
2830 QTest::newRow("any-content")
2831 << QByteArray("any-content")
2835 QTest::newRow("true")
2836 << QByteArray("true")
2840 QTest::newRow("TRUE")
2841 << QByteArray("TRUE")
2845 QTest::newRow("trUE")
2846 << QByteArray("trUE")
2851 void tst_QVariant::convertByteArrayToBool() const
2853 QFETCH(bool, input);
2854 QFETCH(QByteArray, output);
2856 const QVariant variant(input);
2857 QCOMPARE(variant.type(), QVariant::Bool);
2858 QCOMPARE(variant.toBool(), input);
2859 QVERIFY(qVariantCanConvert<bool>(variant));
2861 QCOMPARE(variant.toByteArray(), output);
2864 void tst_QVariant::convertByteArrayToBool_data() const
2866 QTest::addColumn<bool>("input");
2867 QTest::addColumn<QByteArray>("output");
2869 QTest::newRow("false")
2871 << QByteArray("false");
2873 QTest::newRow("true")
2875 << QByteArray("true");
2880 1. Converting the string "9.9" to int fails. This is the behavior of
2881 toLongLong() and hence also QVariant, since it uses it.
2882 2. Converting the QVariant containing the double 9.9 to int works.
2884 Rationale: "9.9" is not a valid int. However, doubles are by definition not
2885 ints and therefore it makes more sense to perform conversion for those.
2887 void tst_QVariant::toIntFromQString() const
2889 QVariant first("9.9");
2891 QCOMPARE(first.toInt(&ok), 0);
2894 QCOMPARE(QString("9.9").toLongLong(&ok), qlonglong(0));
2898 QCOMPARE(v.toInt(&ok), 10);
2904 1. Conversion from (64 bit) double to int works (no overflow).
2905 2. Same conversion works for QVariant::convert.
2907 Rationale: if 2147483630 is set in float and then converted to int,
2908 there will be overflow and the result will be -2147483648.
2910 void tst_QVariant::toIntFromDouble() const
2912 double d = 2147483630; // max int 2147483647
2913 QVERIFY((int)d == 2147483630);
2916 QVERIFY( var.canConvert( QVariant::Int ) );
2919 int result = var.toInt(&ok);
2921 QVERIFY( ok == true );
2922 QCOMPARE(result, 2147483630);
2925 void tst_QVariant::setValue()
2927 QTransform t; //we just take a value so that we're sure that it will be shared
2929 QVERIFY( v1.isDetached() );
2931 QVERIFY( !v1.isDetached() );
2932 QVERIFY( !v2.isDetached() );
2934 qVariantSetValue(v2, 3); //set an integer value
2936 QVERIFY( v1.isDetached() );
2937 QVERIFY( v2.isDetached() );
2940 void tst_QVariant::numericalConvert()
2942 #if defined(Q_OS_LINUX) && defined(Q_CC_GNU) && !defined(__x86_64__)
2943 QSKIP("Known to fail due to a GCC bug on at least Ubuntu 10.04 32-bit - check QTBUG-8959");
2945 QVariant vfloat(float(5.3));
2946 QVariant vdouble(double(5.3));
2947 QVariant vreal(qreal(5.3));
2948 QVariant vint(int(5));
2949 QVariant vuint(uint(5));
2950 QVariant vshort(short(5));
2951 QVariant vlonglong(quint64(5));
2952 QVariant vstringint(QString::fromLatin1("5"));
2953 QVariant vstring(QString::fromLatin1("5.3"));
2955 QVector<QVariant *> vect;
2956 vect << &vfloat << &vdouble << &vreal << &vint << &vuint << &vshort<< &vlonglong << &vstringint << &vstring;
2958 for(int i = 0; i < vect.size(); i++) {
2960 if (i >= 3 && i <= 7)
2962 QVariant *v = vect.at(i);
2963 QCOMPARE(v->toFloat() , float(num));
2964 QCOMPARE(float(v->toReal()) , float(num));
2965 QCOMPARE(float(v->toDouble()) , float(num));
2967 QCOMPARE(v->toInt() , int(num));
2968 QCOMPARE(v->toUInt() , uint(num));
2969 QCOMPARE(v->toULongLong() , quint64(num));
2971 QCOMPARE(v->toString() , QString::number(num));
2976 template<class T> void playWithVariant(const T &orig, bool isNull, const QString &toString, double toDouble, bool toBool)
2978 QVariant v = QVariant::fromValue(orig);
2979 QVERIFY(v.isValid());
2980 QCOMPARE(v.isNull(), isNull);
2981 QCOMPARE(v.toString(), toString);
2982 QCOMPARE(v.toDouble(), toDouble);
2983 QCOMPARE(v.toBool(), toBool);
2984 QCOMPARE(qvariant_cast<T>(v), orig);
2988 if (!(QTypeInfo<T>::isStatic && QTypeInfo<T>::isComplex)) {
2989 // Type is movable so standard comparison algorithm in QVariant should work
2990 // In a custom type QVariant is not aware of ==operator so it won't be called,
2991 // which may cause problems especially visible when using a not-movable type
2994 QVERIFY(v2.isValid());
2995 QCOMPARE(v2.isNull(), isNull);
2996 QCOMPARE(v2.toString(), toString);
2997 QCOMPARE(v2.toDouble(), toDouble);
2998 QCOMPARE(v2.toBool(), toBool);
2999 QCOMPARE(qvariant_cast<T>(v2), orig);
3005 if (!(QTypeInfo<T>::isStatic && QTypeInfo<T>::isComplex)) {
3006 // Type is movable so standard comparison algorithm in QVariant should work
3007 // In a custom type QVariant is not aware of ==operator so it won't be called,
3008 // which may cause problems especially visible when using a not-movable type
3011 QCOMPARE(qvariant_cast<T>(v2), qvariant_cast<T>(v));
3012 QCOMPARE(v2.toString(), toString);
3013 v3 = qVariantFromValue(orig);
3015 QVERIFY(v3.isValid());
3016 QCOMPARE(v3.isNull(), isNull);
3017 QCOMPARE(v3.toString(), toString);
3018 QCOMPARE(v3.toDouble(), toDouble);
3019 QCOMPARE(v3.toBool(), toBool);
3020 QCOMPARE(qvariant_cast<T>(v3), qvariant_cast<T>(v));
3023 QVERIFY(v.isValid());
3024 QCOMPARE(v.isNull(), isNull);
3025 QCOMPARE(v.toString(), toString);
3026 QCOMPARE(v.toDouble(), toDouble);
3027 QCOMPARE(v.toBool(), toBool);
3028 QCOMPARE(qvariant_cast<T>(v), orig);
3030 if (qMetaTypeId<T>() != qMetaTypeId<QVariant>()) {
3031 QCOMPARE(v.userType(), qMetaTypeId<T>());
3032 QCOMPARE(QVariant::typeToName(QVariant::Type(v.userType())), QMetaType::typeName(qMetaTypeId<T>()));
3036 #define PLAY_WITH_VARIANT(Orig, IsNull, ToString, ToDouble, ToBool) \
3037 playWithVariant(Orig, IsNull, ToString, ToDouble, ToBool);\
3038 if (QTest::currentTestFailed())\
3039 QFAIL("playWithVariant failed");
3044 bool operator==(const MyPrimitive &o) const
3046 return x == o.x && y == o.y;
3051 Q_DECLARE_TYPEINFO(MyPrimitive, Q_PRIMITIVE_TYPE);
3057 MyData() : ptr(this) {}
3060 if (ptr != this) qWarning("%s: object has moved", Q_FUNC_INFO);
3062 MyData(const MyData& o) : ptr(this)
3064 if (o.ptr != &o) qWarning("%s: other object has moved", Q_FUNC_INFO);
3066 MyData &operator=(const MyData &o)
3068 if (ptr != this) qWarning("%s: object has moved", Q_FUNC_INFO);
3069 if (o.ptr != &o) qWarning("%s: other object has moved", Q_FUNC_INFO);
3072 bool operator==(const MyData &o) const
3074 if (ptr != this) qWarning("%s: object has moved", Q_FUNC_INFO);
3075 if (o.ptr != &o) qWarning("%s: other object has moved", Q_FUNC_INFO);
3084 MyMovable() { v = count++; }
3085 ~MyMovable() { count--; }
3086 MyMovable(const MyMovable &o) : v(o.v) { count++; }
3088 bool operator==(const MyMovable &o) const
3094 int MyMovable::count = 0;
3100 MyNotMovable() : that(this) { count++; }
3101 ~MyNotMovable() { QCOMPARE(that, this); count--; }
3102 MyNotMovable(const MyNotMovable &o) : that(this) { QCOMPARE(o.that, &o); count++; }
3103 MyNotMovable &operator=(const MyNotMovable &o) {
3104 bool ok = that == this && o.that == &o;
3105 if (!ok) qFatal("MyNotMovable has been moved");
3109 //PLAY_WITH_VARIANT test that they are equal, but never that they are not equal
3110 // so it would be fine just to always return true
3111 bool operator==(const MyNotMovable &o) const
3113 bool ok = that == this && o.that == &o;
3114 if (!ok) qFatal("MyNotMovable has been moved");
3119 int MyNotMovable::count = 0;
3121 struct MyShared : QSharedData {
3126 Q_DECLARE_TYPEINFO(MyMovable, Q_MOVABLE_TYPE);
3129 Q_DECLARE_METATYPE(QList<QSize>)
3130 Q_DECLARE_METATYPE(MyPrimitive)
3131 Q_DECLARE_METATYPE(MyData)
3132 Q_DECLARE_METATYPE(MyMovable)
3133 Q_DECLARE_METATYPE(MyNotMovable)
3134 Q_DECLARE_METATYPE(QList<MyPrimitive>)
3135 Q_DECLARE_METATYPE(QList<MyData>)
3136 Q_DECLARE_METATYPE(QList<MyMovable>)
3137 Q_DECLARE_METATYPE(QList<MyNotMovable>)
3138 Q_DECLARE_METATYPE(MyPrimitive *)
3139 Q_DECLARE_METATYPE(MyData *)
3140 Q_DECLARE_METATYPE(MyMovable *)
3141 Q_DECLARE_METATYPE(MyNotMovable *)
3142 Q_DECLARE_METATYPE(QSharedDataPointer<MyShared>)
3145 void tst_QVariant::moreCustomTypes()
3148 QList<QSize> listSize;
3149 PLAY_WITH_VARIANT(listSize, false, QString(), 0, false);
3150 listSize << QSize(4,5) << QSize(89,23) << QSize(5,6);
3151 PLAY_WITH_VARIANT(listSize, false, QString(), 0, false);
3156 PLAY_WITH_VARIANT(str, true, QString(), 0, false);
3157 str = QString::fromLatin1("123456789.123");
3158 PLAY_WITH_VARIANT(str, false, str, 123456789.123, true);
3163 PLAY_WITH_VARIANT(size, false, QString(), 0, false);
3164 PLAY_WITH_VARIANT(QSize(45,78), false, QString(), 0, false);
3169 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3170 PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3172 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3173 l << MyData() << MyData();
3174 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3178 MyPrimitive d = { 4, 5 };
3179 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3180 PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3181 QList<MyPrimitive> l;
3182 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3184 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3189 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3190 PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3192 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3193 l << MyMovable() << d;
3194 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3196 QCOMPARE(MyMovable::count, 0);
3198 QCOMPARE(MyNotMovable::count, 0);
3201 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3202 PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3203 QList<MyNotMovable> l;
3204 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3205 l << MyNotMovable() << d;
3206 PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3208 QCOMPARE(MyNotMovable::count, 0);
3211 PLAY_WITH_VARIANT(12.12, false, "12.12", 12.12, true);
3212 PLAY_WITH_VARIANT(12.12f, false, "12.12", 12.12f, true);
3213 PLAY_WITH_VARIANT('a', false, "a", 'a', true);
3214 PLAY_WITH_VARIANT((unsigned char)('a'), false, "a", 'a', true);
3215 PLAY_WITH_VARIANT( quint8(12), false, "\xc", 12, true);
3216 PLAY_WITH_VARIANT( qint8(13), false, "\xd", 13, true);
3217 PLAY_WITH_VARIANT(quint16(14), false, "14", 14, true);
3218 PLAY_WITH_VARIANT( qint16(15), false, "15", 15, true);
3219 PLAY_WITH_VARIANT(quint32(16), false, "16", 16, true);
3220 PLAY_WITH_VARIANT( qint32(17), false, "17", 17, true);
3221 PLAY_WITH_VARIANT(quint64(18), false, "18", 18, true);
3222 PLAY_WITH_VARIANT( qint64(19), false, "19", 19, true);
3223 PLAY_WITH_VARIANT( qint8(-12), false, "\xf4", -12, true);
3224 PLAY_WITH_VARIANT( qint16(-13), false, "-13", -13, true);
3225 PLAY_WITH_VARIANT( qint32(-14), false, "-14", -14, true);
3226 PLAY_WITH_VARIANT( qint64(-15), false, "-15", -15, true);
3227 PLAY_WITH_VARIANT(quint64(0), false, "0", 0, false);
3228 PLAY_WITH_VARIANT( true, false, "true", 1, true);
3229 PLAY_WITH_VARIANT( false, false, "false", 0, false);
3231 PLAY_WITH_VARIANT(QString("hello\n"), false, "hello\n", 0, true);
3236 PLAY_WITH_VARIANT((void *)(&i), false, QString(), 0, false);
3237 PLAY_WITH_VARIANT((void *)(0), false, QString(), 0, false);
3241 QVariant v1 = QVariant::fromValue(5);
3242 QVariant v2 = QVariant::fromValue(5.0);
3243 QVariant v3 = QVariant::fromValue(quint16(5));
3245 QVariant v5 = QVariant::fromValue(MyPrimitive());
3246 QVariant v6 = QVariant::fromValue(MyMovable());
3247 QVariant v7 = QVariant::fromValue(MyData());
3248 PLAY_WITH_VARIANT(v1, false, "5", 5, true);
3249 PLAY_WITH_VARIANT(v2, false, "5", 5, true);
3250 PLAY_WITH_VARIANT(v3, false, "5", 5, true);
3251 PLAY_WITH_VARIANT(v4, false, "5", 5, true);
3253 PLAY_WITH_VARIANT(v5, false, QString(), 0, false);
3256 QCOMPARE(MyMovable::count, 0);
3258 QSharedDataPointer<MyShared> d(new MyShared);
3259 PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3261 QCOMPARE(MyMovable::count, 0);
3264 QList<QList<int> > data;
3265 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3266 data << (QList<int>() << 42);
3267 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3271 QList<QVector<int> > data;
3272 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3273 data << (QVector<int>() << 42);
3274 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3278 QList<QSet<int> > data;
3279 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3280 data << (QSet<int>() << 42);
3281 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3285 QList<QLinkedList<int> > data;
3286 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3287 data << (QLinkedList<int>() << 42);
3288 PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3292 void tst_QVariant::movabilityTest()
3294 // This test checks if QVariant is movable even if an internal data is not movable.
3295 QVERIFY(!MyNotMovable::count);
3297 QVariant variant = QVariant::fromValue(MyNotMovable());
3298 QVERIFY(MyNotMovable::count);
3300 // prepare destination memory space to which variant will be moved
3302 QCOMPARE(buffer[0].type(), QVariant::Invalid);
3303 buffer[0].~QVariant();
3305 memcpy(buffer, &variant, sizeof(QVariant));
3306 QCOMPARE(buffer[0].type(), QVariant::UserType);
3307 MyNotMovable tmp(buffer[0].value<MyNotMovable>());
3309 new (&variant) QVariant();
3311 QVERIFY(!MyNotMovable::count);
3314 void tst_QVariant::variantInVariant()
3317 QCOMPARE(var1.type(), QVariant::Int);
3318 QVariant var2 = var1;
3319 QCOMPARE(var2, var1);
3320 QCOMPARE(var2.type(), QVariant::Int);
3321 QVariant var3 = QVariant::fromValue(var1);
3322 QCOMPARE(var3, var1);
3323 QCOMPARE(var3.type(), QVariant::Int);
3324 QVariant var4 = qvariant_cast<QVariant>(var1);
3325 QCOMPARE(var4, var1);
3326 QCOMPARE(var4.type(), QVariant::Int);
3329 QCOMPARE(var5, var1);
3330 QCOMPARE(var5.type(), QVariant::Int);
3332 var6.setValue(var1);
3333 QCOMPARE(var6, var1);
3334 QCOMPARE(var6.type(), QVariant::Int);
3336 QCOMPARE(QVariant::fromValue(var1), QVariant::fromValue(var2));
3337 QCOMPARE(qvariant_cast<QVariant>(var3), QVariant::fromValue(var4));
3338 QCOMPARE(qvariant_cast<QVariant>(var5), qvariant_cast<QVariant>(var6));
3340 QString str("hello");
3341 QVariant var8 = qvariant_cast<QVariant>(QVariant::fromValue(QVariant::fromValue(str)));
3342 QCOMPARE((int)var8.type(), (int)QVariant::String);
3343 QCOMPARE(qvariant_cast<QString>(QVariant(qvariant_cast<QVariant>(var8))), str);
3345 QVariant var9(qMetaTypeId<QVariant>(), &var1);
3346 QCOMPARE(var9.userType(), qMetaTypeId<QVariant>());
3347 QCOMPARE(qvariant_cast<QVariant>(var9), var1);
3350 void tst_QVariant::colorInteger()
3352 QVariant v = QColor(Qt::red);
3353 QCOMPARE(v.type(), QVariant::Color);
3354 QCOMPARE(v.value<QColor>(), QColor(Qt::red));
3357 QCOMPARE(v.type(), QVariant::Int);
3358 QCOMPARE(v.toInt(), 1000);
3360 v.setValue(QColor(Qt::yellow));
3361 QCOMPARE(v.type(), QVariant::Color);
3362 QCOMPARE(v.value<QColor>(), QColor(Qt::yellow));
3366 QT_BEGIN_NAMESPACE namespace QtPrivate {
3367 template <> struct IsPointerToTypeDerivedFromQObject<Forward*> {
3368 enum { Value = false };
3371 Q_DECLARE_METATYPE(Forward*);
3373 void tst_QVariant::forwardDeclare()
3376 QVariant v = QVariant::fromValue(f);
3377 QCOMPARE(qvariant_cast<Forward*>(v), f);
3380 void tst_QVariant::loadQt5Stream_data()
3382 dataStream_data(QDataStream::Qt_5_0);
3385 void tst_QVariant::loadQt5Stream()
3387 loadQVariantFromDataStream(QDataStream::Qt_5_0);
3390 void tst_QVariant::saveQt5Stream_data()
3392 dataStream_data(QDataStream::Qt_5_0);
3395 void tst_QVariant::saveQt5Stream()
3397 saveQVariantFromDataStream(QDataStream::Qt_5_0);
3400 void tst_QVariant::loadQt4Stream_data()
3402 dataStream_data(QDataStream::Qt_4_9);
3405 void tst_QVariant::loadQt4Stream()
3407 loadQVariantFromDataStream(QDataStream::Qt_4_9);
3410 void tst_QVariant::saveQt4Stream_data()
3412 dataStream_data(QDataStream::Qt_4_9);
3415 void tst_QVariant::saveQt4Stream()
3417 saveQVariantFromDataStream(QDataStream::Qt_4_9);
3420 void tst_QVariant::dataStream_data(QDataStream::Version version)
3422 QTest::addColumn<QString>("fileName");
3426 case QDataStream::Qt_4_9:
3427 path = QString::fromLatin1("qt4.9");
3429 case QDataStream::Qt_5_0:
3430 path = QString::fromLatin1("qt5.0");
3436 path = path.prepend(":/stream/").append("/");
3439 foreach (const QFileInfo &fileInfo, dir.entryInfoList(QStringList() << "*.bin")) {
3440 QTest::newRow((path + fileInfo.fileName()).toLatin1()) << fileInfo.filePath();
3446 void tst_QVariant::loadQVariantFromDataStream(QDataStream::Version version)
3448 QFETCH(QString, fileName);
3450 QFile file(fileName);
3451 QVERIFY(file.open(QIODevice::ReadOnly));
3453 QDataStream stream(&file);
3454 stream.setVersion(version);
3457 QVariant loadedVariant;
3458 stream >> typeName >> loadedVariant;
3460 const int id = QMetaType::type(typeName.toLatin1());
3461 QVariant constructedVariant(static_cast<QVariant::Type>(id));
3462 QCOMPARE(constructedVariant.userType(), id);
3463 QCOMPARE(QMetaType::typeName(loadedVariant.userType()), typeName.toLatin1().constData());
3464 QCOMPARE(loadedVariant.userType(), constructedVariant.userType());
3467 void tst_QVariant::saveQVariantFromDataStream(QDataStream::Version version)
3469 QFETCH(QString, fileName);
3471 QFile file(fileName);
3472 QVERIFY(file.open(QIODevice::ReadOnly));
3473 QDataStream dataFileStream(&file);
3476 dataFileStream >> typeName;
3477 QByteArray data = file.readAll();
3478 const int id = QMetaType::type(typeName.toLatin1());
3481 buffer.open(QIODevice::ReadWrite);
3482 QDataStream stream(&buffer);
3483 stream.setVersion(version);
3485 QVariant constructedVariant(static_cast<QVariant::Type>(id));
3486 QCOMPARE(constructedVariant.userType(), id);
3487 stream << constructedVariant;
3489 // We are testing QVariant there is no point in testing full array.
3490 QCOMPARE(buffer.data().left(5), data.left(5));
3493 QVariant recunstructedVariant;
3494 stream >> recunstructedVariant;
3495 QCOMPARE(recunstructedVariant.userType(), constructedVariant.userType());
3498 class MessageHandler {
3500 MessageHandler(const int typeId)
3501 : oldMsgHandler(qInstallMsgHandler(handler))
3508 qInstallMsgHandler(oldMsgHandler);
3511 bool testPassed() const
3516 static void handler(QtMsgType, const char *txt)
3518 QString msg = QString::fromLatin1(txt);
3519 // Format itself is not important, but basic data as a type name should be included in the output
3520 ok = msg.startsWith("QVariant(") + QMetaType::typeName(currentId);
3521 QVERIFY2(ok, (QString::fromLatin1("Message is not valid: '") + msg + '\'').toLatin1().constData());
3524 QtMsgHandler oldMsgHandler;
3525 static int currentId;
3528 bool MessageHandler::ok;
3529 int MessageHandler::currentId;
3531 void tst_QVariant::debugStream_data()
3533 QTest::addColumn<QVariant>("variant");
3534 QTest::addColumn<int>("typeId");
3535 for (int id = QMetaType::Void; id < QMetaType::User; ++id) {
3536 const char *tagName = QMetaType::typeName(id);
3539 QTest::newRow(tagName) << QVariant(static_cast<QVariant::Type>(id)) << id;
3541 QTest::newRow("QBitArray(111)") << QVariant(QBitArray(3, true)) << qMetaTypeId<QBitArray>();
3542 QTest::newRow("CustomStreamableClass") << QVariant(qMetaTypeId<CustomStreamableClass>(), 0) << qMetaTypeId<CustomStreamableClass>();
3543 QTest::newRow("MyClass") << QVariant(qMetaTypeId<MyClass>(), 0) << qMetaTypeId<MyClass>();
3546 void tst_QVariant::debugStream()
3548 QFETCH(QVariant, variant);
3549 QFETCH(int, typeId);
3551 MessageHandler msgHandler(typeId);
3552 qDebug() << variant;
3553 QVERIFY(msgHandler.testPassed());
3556 QTEST_MAIN(tst_QVariant)
3557 #include "tst_qvariant.moc"