Merge remote-tracking branch 'origin/api_changes'
[profile/ivi/qtbase.git] / tests / auto / corelib / kernel / qvariant / tst_qvariant.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <QtTest/QtTest>
43
44 #include <qvariant.h>
45 #include <qbitarray.h>
46 #include <qhostaddress.h>
47 #include <qdatetime.h>
48 #include <qmap.h>
49 #include <qiodevice.h>
50 #include <qurl.h>
51 #include <qlocale.h>
52 #include <qkeysequence.h>
53 #include <qbitmap.h>
54 #include <qcursor.h>
55 #include <qimage.h>
56 #include <qicon.h>
57 #include <qmatrix.h>
58 #include <qmatrix4x4.h>
59 #include <qpen.h>
60 #include <qpolygon.h>
61 #include <qpalette.h>
62 #include <qtransform.h>
63 #include <qvector2d.h>
64 #include <qvector3d.h>
65 #include <qvector4d.h>
66 #include <qquaternion.h>
67 #include <qdebug.h>
68
69 #include <limits.h>
70
71 Q_DECLARE_METATYPE(qlonglong)
72 Q_DECLARE_METATYPE(qulonglong)
73 Q_DECLARE_METATYPE(QPointF)
74 Q_DECLARE_METATYPE(QRectF)
75 Q_DECLARE_METATYPE(QSize)
76 Q_DECLARE_METATYPE(QSizeF)
77 Q_DECLARE_METATYPE(QLine)
78 Q_DECLARE_METATYPE(QLineF)
79 Q_DECLARE_METATYPE(QPoint)
80 Q_DECLARE_METATYPE(QRect)
81 Q_DECLARE_METATYPE(QPixmap)
82 Q_DECLARE_METATYPE(QBrush)
83 Q_DECLARE_METATYPE(QFont)
84 Q_DECLARE_METATYPE(QColor)
85 Q_DECLARE_METATYPE(QKeySequence)
86
87 class CustomNonQObject;
88
89 class tst_QVariant : public QObject
90 {
91     Q_OBJECT
92
93 public:
94     tst_QVariant(QObject *parent = 0)
95       : QObject(parent), customNonQObjectPointer(0)
96     {
97
98     }
99
100 private slots:
101     void cleanupTestCase();
102
103     void constructor();
104     void copy_constructor();
105     void constructor_invalid_data();
106     void constructor_invalid();
107     void isNull();
108     void swap();
109
110     void canConvert_data();
111     void canConvert();
112
113     void toSize_data();
114     void toSize();
115
116     void toSizeF_data();
117     void toSizeF();
118
119     void toPoint_data();
120     void toPoint();
121
122     void toRect_data();
123     void toRect();
124
125     void toChar_data();
126     void toChar();
127
128     void toLine_data();
129     void toLine();
130
131     void toLineF_data();
132     void toLineF();
133
134     void toInt_data();
135     void toInt();
136
137     void toUInt_data();
138     void toUInt();
139
140     void toBool_data();
141     void toBool();
142
143     void toLongLong_data();
144     void toLongLong();
145
146     void toULongLong_data();
147     void toULongLong();
148
149     void toByteArray_data();
150     void toByteArray();
151
152     void toString_data();
153     void toString();
154
155     void toDate_data();
156     void toDate();
157
158     void toTime_data();
159     void toTime();
160
161     void toDateTime_data();
162     void toDateTime();
163
164     void toDouble_data();
165     void toDouble();
166
167     void toPointF_data();
168     void toPointF();
169
170     void toFont_data();
171     void toFont();
172
173     void toKeySequence_data();
174     void toKeySequence();
175
176     void toRectF_data();
177     void toRectF();
178
179     void toColor_data();
180     void toColor();
181
182     void toPixmap_data();
183     void toPixmap();
184
185     void toImage_data();
186     void toImage();
187
188     void toBrush_data();
189     void toBrush();
190
191     void qvariant_cast_QObject_data();
192     void qvariant_cast_QObject();
193     void qvariant_cast_QObject_derived();
194
195     void toLocale();
196
197     void toRegExp();
198     void toRegularExpression();
199
200     void matrix();
201
202     void transform();
203
204     void matrix4x4();
205     void vector2D();
206     void vector3D();
207     void vector4D();
208     void quaternion();
209
210     void url();
211
212     void userType();
213     void basicUserType();
214
215     void variant_to();
216
217     void writeToReadFromDataStream_data();
218     void writeToReadFromDataStream();
219     void writeToReadFromOldDataStream();
220     void checkDataStream();
221
222     void operator_eq_eq_data();
223     void operator_eq_eq();
224
225     void operator_eq_eq_rhs();
226
227     void typeName_data();
228     void typeName();
229     void typeToName();
230
231     void streamInvalidVariant();
232
233     void podUserType();
234
235     void data();
236     void constData();
237
238     void saveLoadCustomTypes();
239
240     void variantMap();
241     void variantHash();
242
243     void convertToQUint8() const;
244     void invalidQColor() const;
245     void comparePointers() const;
246     void voidStar() const;
247     void dataStar() const;
248     void canConvertQStringList() const;
249     void canConvertQStringList_data() const;
250     void canConvertMetaTypeToInt() const;
251     void variantToDateTimeWithoutWarnings() const;
252     void invalidDateTime() const;
253
254     void loadUnknownUserType();
255     void loadBrokenUserType();
256
257     void invalidDate() const;
258     void compareCustomTypes() const;
259     void timeToDateTime() const;
260     void copyingUserTypes() const;
261     void convertBoolToByteArray() const;
262     void convertBoolToByteArray_data() const;
263     void convertByteArrayToBool() const;
264     void convertByteArrayToBool_data() const;
265     void toIntFromQString() const;
266     void toIntFromDouble() const;
267     void setValue();
268
269     void numericalConvert();
270     void moreCustomTypes();
271     void movabilityTest();
272     void variantInVariant();
273
274     void colorInteger();
275
276     void forwardDeclare();
277     void debugStream_data();
278     void debugStream();
279     void debugStreamType_data();
280     void debugStreamType();
281
282     void loadQt4Stream_data();
283     void loadQt4Stream();
284     void saveQt4Stream_data();
285     void saveQt4Stream();
286     void loadQt5Stream_data();
287     void loadQt5Stream();
288     void saveQt5Stream_data();
289     void saveQt5Stream();
290
291     void guiVariantAtExit();
292     void widgetsVariantAtExit();
293 private:
294     void dataStream_data(QDataStream::Version version);
295     void loadQVariantFromDataStream(QDataStream::Version version);
296     void saveQVariantFromDataStream(QDataStream::Version version);
297
298     CustomNonQObject *customNonQObjectPointer;
299     QVector<QObject*> objectPointerTestData;
300 };
301
302 Q_DECLARE_METATYPE(QDate)
303 Q_DECLARE_METATYPE(QTime)
304 Q_DECLARE_METATYPE(QDateTime)
305 Q_DECLARE_METATYPE(QVariant)
306
307 const qlonglong intMax1 = (qlonglong)INT_MAX + 1;
308 const qulonglong uintMax1 = (qulonglong)UINT_MAX + 1;
309
310 void tst_QVariant::constructor()
311 {
312     QVariant variant;
313     QVERIFY( !variant.isValid() );
314     QVERIFY( variant.isNull() );
315
316     QVariant var2(variant);
317     QVERIFY( !var2.isValid() );
318     QVERIFY( variant.isNull() );
319
320     QVariant varll(intMax1);
321     QVariant varll2(varll);
322     QCOMPARE(varll2, varll);
323
324     QVariant var3(QVariant::String);
325     QCOMPARE(var3.typeName(), "QString");
326     QVERIFY(var3.isNull());
327     QVERIFY(var3.isValid());
328
329     QVariant var4(QVariant::Invalid);
330     QCOMPARE(var4.type(), QVariant::Invalid);
331     QVERIFY(var4.isNull());
332     QVERIFY(!var4.isValid());
333
334     QVariant var5(QLatin1String("hallo"));
335     QCOMPARE(var5.type(), QVariant::String);
336     QCOMPARE(var5.typeName(), "QString");
337
338     QVariant var6(qlonglong(0));
339     QCOMPARE(var6.type(), QVariant::LongLong);
340     QCOMPARE(var6.typeName(), "qlonglong");
341
342     QVariant var7 = 5;
343     QVERIFY(var7.isValid());
344     QVERIFY(!var7.isNull());
345     QVariant var8;
346     var8.setValue<int>(5);
347     QVERIFY(var8.isValid());
348     QVERIFY(!var8.isNull());
349 }
350
351 void tst_QVariant::constructor_invalid_data()
352 {
353     QTest::addColumn<uint>("typeId");
354
355     QTest::newRow("-1") << uint(-1);
356     QTest::newRow("-122234567") << uint(-122234567);
357     QTest::newRow("0xfffffffff") << uint(0xfffffffff);
358     QTest::newRow("LastCoreType + 1") << uint(QMetaType::LastCoreType + 1);
359     QVERIFY(!QMetaType::isRegistered(QMetaType::LastCoreType + 1));
360     QTest::newRow("LastGuiType + 1") << uint(QMetaType::LastGuiType + 1);
361     QVERIFY(!QMetaType::isRegistered(QMetaType::LastGuiType + 1));
362     QTest::newRow("LastWidgetsType + 1") << uint(QMetaType::LastWidgetsType + 1);
363     QVERIFY(!QMetaType::isRegistered(QMetaType::LastWidgetsType + 1));
364 }
365
366 struct MessageHandlerInvalidType
367 {
368     MessageHandlerInvalidType()
369         : oldMsgHandler(qInstallMsgHandler(handler))
370     {
371         ok = false;
372     }
373
374     ~MessageHandlerInvalidType()
375     {
376         qInstallMsgHandler(oldMsgHandler);
377     }
378
379     QtMsgHandler oldMsgHandler;
380
381     static void handler(QtMsgType type, const char *txt)
382     {
383         QString msg = QString::fromLatin1(txt);
384         // uint(-1) can be platform dependent so we check only beginning of the message.
385         ok = msg.startsWith("Trying to construct an instance of an invalid type, type id:");
386         QVERIFY2(ok, (QString::fromLatin1("Message is not started correctly: '") + msg + '\'').toLatin1().constData());
387     }
388     static bool ok;
389 };
390 bool MessageHandlerInvalidType::ok;
391
392 void tst_QVariant::constructor_invalid()
393 {
394
395     QFETCH(uint, typeId);
396     {
397         MessageHandlerInvalidType msg;
398         QVariant variant(static_cast<QVariant::Type>(typeId));
399         QVERIFY(!variant.isValid());
400         QVERIFY(variant.userType() == QMetaType::UnknownType);
401         QVERIFY(msg.ok);
402     }
403     {
404         MessageHandlerInvalidType msg;
405         QVariant variant(typeId, /* copy */ 0);
406         QVERIFY(!variant.isValid());
407         QVERIFY(variant.userType() == QMetaType::UnknownType);
408         QVERIFY(msg.ok);
409     }
410 }
411
412 void tst_QVariant::copy_constructor()
413 {
414     QVariant var7(QVariant::Int);
415     QVariant var8(var7);
416     QCOMPARE(var8.type(), QVariant::Int);
417     QVERIFY(var8.isNull());
418 }
419
420 void tst_QVariant::isNull()
421 {
422     QVariant var;
423     QVERIFY( var.isNull() );
424
425     QString str1;
426     QVariant var1( str1 );
427     QVERIFY( var1.isNull() );
428
429     QVariant var2( QString::null );
430     QVERIFY( var2.isNull() );
431
432     QVariant var3( QString( "blah" ) );
433     QVERIFY( !var3.isNull() );
434
435     QVariant var4( 0 );
436     QVERIFY( !var4.isNull() );
437
438     QVariant var5 = QString();
439     QVERIFY( var5.isNull() );
440
441     QVariant var6( QString( "blah" ) );
442     QVERIFY( !var6.isNull() );
443     var6 = QVariant();
444     QVERIFY( var6.isNull() );
445     var6.convert( QVariant::String );
446     QVERIFY( var6.isNull() );
447     QVariant varLL( (qlonglong)0 );
448     QVERIFY( !varLL.isNull() );
449     QVariant var7(QString::null);
450     QVERIFY(var7.isNull());
451 }
452
453 void tst_QVariant::swap()
454 {
455     QVariant v1 = 1, v2 = 2.0;
456     v1.swap(v2);
457     QCOMPARE(v1.type(),QVariant::Double);
458     QCOMPARE(v1.toDouble(),2.0);
459     QCOMPARE(v2.type(),QVariant::Int);
460     QCOMPARE(v2.toInt(),1);
461 }
462
463 void tst_QVariant::canConvert_data()
464 {
465     QTest::addColumn<QVariant>("val");
466     QTest::addColumn<bool>("BitArrayCast");
467     QTest::addColumn<bool>("BitmapCast");
468     QTest::addColumn<bool>("BoolCast");
469     QTest::addColumn<bool>("BrushCast");
470     QTest::addColumn<bool>("ByteArrayCast");
471     QTest::addColumn<bool>("ColorCast");
472     QTest::addColumn<bool>("CursorCast");
473     QTest::addColumn<bool>("DateCast");
474     QTest::addColumn<bool>("DateTimeCast");
475     QTest::addColumn<bool>("DoubleCast");
476     QTest::addColumn<bool>("FontCast");
477     QTest::addColumn<bool>("ImageCast");
478     QTest::addColumn<bool>("IntCast");
479     QTest::addColumn<bool>("InvalidCast");
480     QTest::addColumn<bool>("KeySequenceCast");
481     QTest::addColumn<bool>("ListCast");
482     QTest::addColumn<bool>("LongLongCast");
483     QTest::addColumn<bool>("MapCast");
484     QTest::addColumn<bool>("PaletteCast");
485     QTest::addColumn<bool>("PenCast");
486     QTest::addColumn<bool>("PixmapCast");
487     QTest::addColumn<bool>("PointCast");
488     QTest::addColumn<bool>("RectCast");
489     QTest::addColumn<bool>("RegionCast");
490     QTest::addColumn<bool>("SizeCast");
491     QTest::addColumn<bool>("SizePolicyCast");
492     QTest::addColumn<bool>("StringCast");
493     QTest::addColumn<bool>("StringListCast");
494     QTest::addColumn<bool>("TimeCast");
495     QTest::addColumn<bool>("UIntCast");
496     QTest::addColumn<bool>("ULongLongCast");
497
498
499 #ifdef Y
500 #undef Y
501 #endif
502 #ifdef N
503 #undef N
504 #endif
505 #define Y true
506 #define N false
507     //            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
508
509
510     QVariant var(QBitArray(0));
511     QTest::newRow("BitArray")
512         << 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;
513     var = QVariant::fromValue(QBitmap());
514     QTest::newRow("Bitmap")
515         << 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;
516     var = QVariant::fromValue(QBrush());
517     QTest::newRow("Brush")
518         << 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;
519     var = QVariant(QByteArray());
520     QTest::newRow("ByteArray")
521         << 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;
522     var = QVariant::fromValue(QColor());
523     QTest::newRow("Color")
524         << 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;
525 #ifndef QT_NO_CURSOR
526     var = QVariant::fromValue(QCursor());
527     QTest::newRow("Cursor")
528         << 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;
529 #endif
530     var = QVariant(QDate());
531     QTest::newRow("Date")
532         << 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;
533     var = QVariant(QDateTime());
534     QTest::newRow("DateTime")
535         << 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;
536     var = QVariant((double)0.1);
537     QTest::newRow("Double")
538         << var << N << N << Y << N << Y << N << N << N << N << Y << N << N << Y << N << N << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y;
539     var = QVariant(0.1f);
540     QTest::newRow("Float")
541         << var << N << N << Y << N << Y << N << N << N << N << Y << N << N << Y << N << N << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y;
542     var = QVariant::fromValue(QFont());
543     QTest::newRow("Font")
544         << 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;
545     var = QVariant::fromValue(QIcon());
546     QTest::newRow("Icon")
547         << 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;
548     var = QVariant::fromValue(QImage());
549     QTest::newRow("Image")
550         << 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;
551     var = QVariant((int)1);
552     QTest::newRow("Int")
553         << 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;
554     var = QVariant();
555     QTest::newRow("Invalid")
556         << 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;
557     var = QVariant::fromValue(QKeySequence());
558     QTest::newRow("KeySequence")
559         << 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;
560     var = QVariant(QList<QVariant>());
561     QTest::newRow("List")
562         << 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;
563     var = QVariant((qlonglong)1);
564     QTest::newRow("LongLong")
565         << 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;
566     var = QVariant(QMap<QString,QVariant>());
567     QTest::newRow("Map")
568         << 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;
569     var = QVariant::fromValue(QPalette());
570     QTest::newRow("Palette")
571         << 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;
572     var = QVariant::fromValue(QPen());
573     QTest::newRow("Pen")
574         << 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;
575     var = QVariant::fromValue(QPixmap());
576     QTest::newRow("Pixmap")
577         << 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;
578     var = QVariant::fromValue(QPolygon());
579     QTest::newRow("PointArray")
580         << 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;
581     var = QVariant(QPoint());
582     QTest::newRow("Point")
583         << 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;
584     var = QVariant(QRect());
585     QTest::newRow("Rect")
586         << 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;
587     var = QVariant::fromValue(QRegion());
588     QTest::newRow("Region")
589         << 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;
590     var = QVariant(QSize());
591     QTest::newRow("Size")
592         << 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;
593     var = QVariant::fromValue(QSizePolicy());
594     QTest::newRow("SizePolicy")
595         << 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;
596     var = QVariant(QString());
597     QTest::newRow("String")
598         << 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;
599    var = QVariant(QStringList("entry"));
600     QTest::newRow("StringList")
601         << 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;
602     var = QVariant(QTime());
603     QTest::newRow("Time")
604         << 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;
605     var = QVariant((uint)1);
606     QTest::newRow("UInt")
607         << 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;
608     var = QVariant((qulonglong)1);
609     QTest::newRow("ULongLong")
610         << 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;
611
612 #undef N
613 #undef Y
614 }
615
616 void tst_QVariant::canConvert()
617 {
618     QFETCH(QVariant, val);
619     QFETCH(bool, BitArrayCast);
620     QFETCH(bool, BitmapCast);
621     QFETCH(bool, BoolCast);
622     QFETCH(bool, BrushCast);
623     QFETCH(bool, ByteArrayCast);
624     QFETCH(bool, ColorCast);
625     QFETCH(bool, CursorCast);
626     QFETCH(bool, DateCast);
627     QFETCH(bool, DateTimeCast);
628     QFETCH(bool, DoubleCast);
629     QFETCH(bool, FontCast);
630     QFETCH(bool, ImageCast);
631     QFETCH(bool, IntCast);
632     QFETCH(bool, InvalidCast);
633     QFETCH(bool, KeySequenceCast);
634     QFETCH(bool, ListCast);
635     QFETCH(bool, LongLongCast);
636     QFETCH(bool, MapCast);
637     QFETCH(bool, PaletteCast);
638     QFETCH(bool, PenCast);
639     QFETCH(bool, PixmapCast);
640     QFETCH(bool, PointCast);
641     QFETCH(bool, RectCast);
642     QFETCH(bool, RegionCast);
643     QFETCH(bool, SizeCast);
644     QFETCH(bool, SizePolicyCast);
645     QFETCH(bool, StringCast);
646     QFETCH(bool, StringListCast);
647     QFETCH(bool, TimeCast);
648     QFETCH(bool, UIntCast);
649     QFETCH(bool, ULongLongCast);
650
651     QCOMPARE(val.canConvert(QVariant::BitArray), BitArrayCast);
652     QCOMPARE(val.canConvert(QVariant::Bitmap), BitmapCast);
653     QCOMPARE(val.canConvert(QVariant::Bool), BoolCast);
654     QCOMPARE(val.canConvert(QVariant::Brush), BrushCast);
655     QCOMPARE(val.canConvert(QVariant::ByteArray), ByteArrayCast);
656     QCOMPARE(val.canConvert(QVariant::Color), ColorCast);
657     QCOMPARE(val.canConvert(QVariant::Cursor), CursorCast);
658     QCOMPARE(val.canConvert(QVariant::Date), DateCast);
659     QCOMPARE(val.canConvert(QVariant::DateTime), DateTimeCast);
660     QCOMPARE(val.canConvert(QVariant::Double), DoubleCast);
661     QCOMPARE(val.canConvert(QVariant::Type(QMetaType::Float)), DoubleCast);
662     QCOMPARE(val.canConvert(QVariant::Font), FontCast);
663     QCOMPARE(val.canConvert(QVariant::Image), ImageCast);
664     QCOMPARE(val.canConvert(QVariant::Int), IntCast);
665     QCOMPARE(val.canConvert(QVariant::Invalid), InvalidCast);
666     QCOMPARE(val.canConvert(QVariant::KeySequence), KeySequenceCast);
667     QCOMPARE(val.canConvert(QVariant::List), ListCast);
668     QCOMPARE(val.canConvert(QVariant::LongLong), LongLongCast);
669     QCOMPARE(val.canConvert(QVariant::Map), MapCast);
670     QCOMPARE(val.canConvert(QVariant::Palette), PaletteCast);
671     QCOMPARE(val.canConvert(QVariant::Pen), PenCast);
672     QCOMPARE(val.canConvert(QVariant::Pixmap), PixmapCast);
673     QCOMPARE(val.canConvert(QVariant::Point), PointCast);
674     QCOMPARE(val.canConvert(QVariant::Rect), RectCast);
675     QCOMPARE(val.canConvert(QVariant::Region), RegionCast);
676     QCOMPARE(val.canConvert(QVariant::Size), SizeCast);
677     QCOMPARE(val.canConvert(QVariant::SizePolicy), SizePolicyCast);
678     QCOMPARE(val.canConvert(QVariant::String), StringCast);
679     QCOMPARE(val.canConvert(QVariant::StringList), StringListCast);
680     QCOMPARE(val.canConvert(QVariant::Time), TimeCast);
681     QCOMPARE(val.canConvert(QVariant::UInt), UIntCast);
682     QCOMPARE(val.canConvert(QVariant::ULongLong), ULongLongCast);
683
684     // Invalid type ids
685     QCOMPARE(val.canConvert(-1), false);
686     QCOMPARE(val.canConvert(-23), false);
687     QCOMPARE(val.canConvert(-23876), false);
688     QCOMPARE(val.canConvert(23876), false);
689 }
690
691 void tst_QVariant::toInt_data()
692 {
693     QTest::addColumn<QVariant>("value");
694     QTest::addColumn<int>("result");
695     QTest::addColumn<bool>("valueOK");
696
697     QTest::newRow( "invalid" ) << QVariant()  << 0 << false;
698     QTest::newRow( "int" ) << QVariant( 123 ) << 123 << true;
699     QTest::newRow( "double" ) << QVariant( 3.1415927 ) << 3 << true;
700     QTest::newRow( "float" ) << QVariant( 3.1415927f ) << 3 << true;
701     QTest::newRow( "uint" ) << QVariant( 123u ) << 123 << true;
702     QTest::newRow( "int-string" ) << QVariant( QString("123") ) << 123 << true;
703     QTest::newRow( "string" ) << QVariant( QString("Unicode String") ) << 0 << false;
704     QTest::newRow( "longlong0" ) << QVariant( (qlonglong)34 ) << 34 << true;
705     QTest::newRow( "longlong1" ) << QVariant( intMax1 ) << (int)INT_MIN << true;
706     QTest::newRow( "ulonglong0" ) << QVariant( (qulonglong)34 ) << 34 << true;
707     QTest::newRow( "ulonglong1" ) << QVariant( uintMax1 ) << 0 << true;
708     QTest::newRow( "signedint" ) << QVariant( -123 ) << -123 << true;
709     QTest::newRow( "signeddouble" ) << QVariant( -3.1415927 ) << -3 << true;
710     QTest::newRow( "signedfloat" ) << QVariant( -3.1415927f ) << -3 << true;
711     QTest::newRow( "signedint-string" ) << QVariant( QString("-123") ) << -123 << true;
712     QTest::newRow( "signedlonglong0" ) << QVariant( (qlonglong)-34 ) << -34 << true;
713     QTest::newRow( "QChar" ) << QVariant(QChar('a')) << int('a') << true;
714     QTest::newRow( "keysequence" ) << QVariant::fromValue( QKeySequence( Qt::Key_A ) ) << 65 << true;
715     QByteArray bytearray(4, ' ');
716     bytearray[0] = 'T';
717     bytearray[1] = 'e';
718     bytearray[2] = 's';
719     bytearray[3] = 't';
720     QTest::newRow( "QByteArray1" ) << QVariant( bytearray ) << 0 << false;
721     bytearray[0] = '4';
722     bytearray[1] = '5';
723     bytearray[2] = '0';
724     bytearray[3] = '0';
725     QTest::newRow( "QByteArray2" ) << QVariant( bytearray ) << 4500 << true;
726 }
727
728 void tst_QVariant::toInt()
729 {
730     QFETCH( QVariant, value );
731     QFETCH( int, result );
732     QFETCH( bool, valueOK );
733     QVERIFY( value.isValid() == value.canConvert( QVariant::Int ) );
734     bool ok;
735     int i = value.toInt( &ok );
736     QCOMPARE( i, result );
737     QVERIFY( ok == valueOK );
738 }
739
740 void tst_QVariant::toUInt_data()
741 {
742     QTest::addColumn<QVariant>("value");
743     QTest::addColumn<uint>("result");
744     QTest::addColumn<bool>("valueOK");
745
746     QTest::newRow( "int" ) << QVariant( 123 ) << (uint)123 << true;
747     QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (uint)3 << true;
748     QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (uint)3 << true;
749     QTest::newRow( "uint" ) << QVariant( 123u ) << (uint)123 << true;
750     QTest::newRow( "int-string" ) << QVariant( QString("123") ) << (uint)123 << true;
751     QTest::newRow( "string" ) << QVariant( QString("Unicode String") ) << (uint)0 << false;
752     QTest::newRow( "string2" ) << QVariant( QString("4") ) << (uint)4 << true;
753     QTest::newRow( "longlong0" ) << QVariant( (qlonglong)34 ) << (uint)34 << true;
754     QTest::newRow( "longlong1" ) << QVariant( intMax1 ) << (uint)INT_MIN << true;
755     QTest::newRow( "ulonglong0" ) << QVariant( (qulonglong)34 ) << (uint)34 << true;
756     QTest::newRow( "ulonglong1" ) << QVariant( uintMax1 ) << (uint)0 << true;
757     QTest::newRow( "negativeint" ) << QVariant( -123 ) << (uint)-123 << true;
758     QTest::newRow( "negativedouble" ) << QVariant( -3.1415927 ) << (uint)-3 << true;
759     QTest::newRow( "negativefloat" ) << QVariant( -3.1415927f ) << (uint)-3 << true;
760     QTest::newRow( "negativeint-string" ) << QVariant( QString("-123") ) << (uint)0 << false;
761     QTest::newRow( "negativelonglong0" ) << QVariant( (qlonglong)-34 ) << (uint)-34 << true;
762     QTest::newRow( "QChar" ) << QVariant(QChar('a')) << uint('a') << true;
763     QByteArray bytearray(4, ' ');
764     bytearray[0] = '4';
765     bytearray[1] = '3';
766     bytearray[2] = '2';
767     bytearray[3] = '1';
768     QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (uint)4321 << true;
769 }
770
771 void tst_QVariant::toUInt()
772 {
773     QFETCH( QVariant, value );
774     QFETCH( uint, result );
775     QFETCH( bool, valueOK );
776     QVERIFY( value.isValid() );
777     QVERIFY( value.canConvert( QVariant::UInt ) );
778
779     bool ok;
780     uint i = value.toUInt( &ok );
781     QVERIFY( ok == valueOK );
782     QCOMPARE( i, result );
783 }
784
785
786 void tst_QVariant::toSize_data()
787 {
788     QTest::addColumn<QVariant>("value");
789     QTest::addColumn<QSize>("result");
790     QTest::newRow( "qsizef4" ) << QVariant( QSizeF(4, 2) ) << QSize(4, 2);
791     QTest::newRow( "qsizef1" ) << QVariant( QSizeF(0, 0) ) << QSize(0, 0);
792     QTest::newRow( "qsizef2" ) << QVariant( QSizeF(-5, -1) ) << QSize(-5, -1);
793     QTest::newRow( "qsizef3" ) << QVariant( QSizeF() ) << QSize();
794 }
795
796 void tst_QVariant::toSize()
797 {
798     QFETCH( QVariant, value );
799     QFETCH( QSize, result );
800     QVERIFY( value.isValid() );
801     QVERIFY( value.canConvert( QVariant::Size ) );
802
803     QSize i = value.toSize();
804     QCOMPARE( i, result );
805 }
806
807 void tst_QVariant::toSizeF_data()
808 {
809     QTest::addColumn<QVariant>("value");
810     QTest::addColumn<QSizeF>("result");
811     QTest::newRow( "qsize1" ) << QVariant( QSize(0, 0) ) << QSizeF(0, 0);
812     QTest::newRow( "qsize2" ) << QVariant( QSize(-5, -1) ) << QSizeF(-5, -1);
813      QTest::newRow( "qsize3" ) << QVariant( QSize() ) << QSizeF();
814     QTest::newRow( "qsize4" ) << QVariant(QSize(4,2)) << QSizeF(4,2);
815 }
816
817 void tst_QVariant::toSizeF()
818 {
819     QFETCH( QVariant, value );
820     QFETCH( QSizeF, result );
821     QVERIFY( value.isValid() );
822     QVERIFY( value.canConvert( QVariant::SizeF ) );
823
824     QSizeF i = value.toSizeF();
825     QCOMPARE( i, result );
826 }
827
828 void tst_QVariant::toLine_data()
829 {
830     QTest::addColumn<QVariant>("value");
831     QTest::addColumn<QLine>("result");
832     QTest::newRow( "linef1" ) << QVariant( QLineF(1, 2, 3, 4) ) << QLine(1, 2, 3, 4);
833     QTest::newRow( "linef2" ) << QVariant( QLineF(-1, -2, -3, -4) ) << QLine(-1, -2, -3, -4);
834     QTest::newRow( "linef3" ) << QVariant( QLineF(0, 0, 0, 0) ) << QLine(0, 0, 0, 0);
835     QTest::newRow( "linef4" ) << QVariant( QLineF() ) << QLine();
836 }
837
838 void tst_QVariant::toLine()
839 {
840     QFETCH( QVariant, value );
841     QFETCH( QLine, result );
842     QVERIFY( value.isValid() );
843     QVERIFY( value.canConvert( QVariant::Line ) );
844
845     QLine i = value.toLine();
846     QCOMPARE( i, result );
847 }
848
849 void tst_QVariant::toLineF_data()
850 {
851     QTest::addColumn<QVariant>("value");
852     QTest::addColumn<QLineF>("result");
853     QTest::newRow( "line1" ) << QVariant( QLine(-1, -2, -3, -4) ) << QLineF(-1, -2, -3, -4);
854     QTest::newRow( "line2" ) << QVariant( QLine(0, 0, 0, 0) ) << QLineF(0, 0, 0, 0);
855     QTest::newRow( "line3" ) << QVariant( QLine() ) << QLineF();
856     QTest::newRow( "line4" ) << QVariant( QLine(1, 2, 3, 4) ) << QLineF(1, 2, 3, 4);
857 }
858
859 void tst_QVariant::toLineF()
860 {
861     QFETCH( QVariant, value );
862     QFETCH( QLineF, result );
863     QVERIFY( value.isValid() );
864     QVERIFY( value.canConvert( QVariant::LineF ) );
865
866     QLineF i = value.toLineF();
867     QCOMPARE( i, result );
868 }
869
870 void tst_QVariant::toPoint_data()
871 {
872     QTest::addColumn<QVariant>("value");
873     QTest::addColumn<QPoint>("result");
874     QTest::newRow( "pointf1" ) << QVariant( QPointF(4, 2) ) << QPoint(4, 2);
875     QTest::newRow( "pointf2" ) << QVariant( QPointF(0, 0) ) << QPoint(0, 0);
876     QTest::newRow( "pointf3" ) << QVariant( QPointF(-4, -2) ) << QPoint(-4, -2);
877     QTest::newRow( "pointf4" ) << QVariant( QPointF() ) << QPoint();
878     QTest::newRow( "pointf5" ) << QVariant( QPointF(-4.2f, -2.3f) ) << QPoint(-4, -2);
879 }
880
881 void tst_QVariant::toPoint()
882 {
883     QFETCH( QVariant, value );
884     QFETCH( QPoint, result );
885     QVERIFY( value.isValid() );
886     QVERIFY( value.canConvert( QVariant::Point ) );
887     QPoint i = value.toPoint();
888     QCOMPARE( i, result );
889 }
890
891 void tst_QVariant::toRect_data()
892 {
893     QTest::addColumn<QVariant>("value");
894     QTest::addColumn<QRect>("result");
895     QTest::newRow( "rectf1" ) << QVariant(QRectF(1, 2, 3, 4)) << QRect(1, 2, 3, 4);
896     QTest::newRow( "rectf2" ) << QVariant(QRectF(0, 0, 0, 0)) << QRect(0, 0, 0, 0);
897     QTest::newRow( "rectf3" ) << QVariant(QRectF(-1, -2, -3, -4)) << QRect(-1, -2, -3, -4);
898     QTest::newRow( "rectf4" ) << QVariant(QRectF(-1.3f, 0, 3.9f, -4.0)) << QRect(-1, 0, 4, -4);
899     QTest::newRow( "rectf5" ) << QVariant(QRectF()) << QRect();
900 }
901
902 void tst_QVariant::toRect()
903 {
904     QFETCH( QVariant, value );
905     QFETCH( QRect, result );
906     QVERIFY( value.isValid() );
907     QVERIFY( value.canConvert( QVariant::Rect ) );
908     QRect i = value.toRect();
909     QCOMPARE( i, result );
910 }
911
912 void tst_QVariant::toChar_data()
913 {
914     QTest::addColumn<QVariant>("value");
915     QTest::addColumn<QChar>("result");
916     QTest::newRow( "longlong" ) << QVariant(qlonglong('6')) << QChar('6');
917     QTest::newRow( "ulonglong" ) << QVariant(qulonglong('7')) << QChar('7');
918 }
919
920 void tst_QVariant::toChar()
921 {
922     QFETCH( QVariant, value );
923     QFETCH( QChar, result );
924     QVERIFY( value.isValid() );
925     QVERIFY( value.canConvert( QVariant::Char ) );
926
927     QChar i = value.toChar();
928     QCOMPARE( i, result );
929 }
930
931 void tst_QVariant::toBool_data()
932 {
933     QTest::addColumn<QVariant>("value");
934     QTest::addColumn<bool>("result");
935
936     QTest::newRow( "int0" ) << QVariant( 0 ) << false;
937     QTest::newRow( "int1" ) << QVariant( 123 ) << true;
938     QTest::newRow( "uint0" ) << QVariant( 0u ) << false;
939     QTest::newRow( "uint1" ) << QVariant( 123u ) << true;
940     QTest::newRow( "double0" ) << QVariant( 0.0 ) << false;
941     QTest::newRow( "float0" ) << QVariant( 0.0f ) << false;
942     QTest::newRow( "double1" ) << QVariant( 3.1415927 ) << true;
943     QTest::newRow( "float1" ) << QVariant( 3.1415927f ) << true;
944     QTest::newRow( "string0" ) << QVariant( QString("3") ) << true;
945     QTest::newRow( "string1" ) << QVariant( QString("true") ) << true;
946     QTest::newRow( "string2" ) << QVariant( QString("0") ) << false;
947     QTest::newRow( "string3" ) << QVariant( QString("fAlSe") ) << false;
948     QTest::newRow( "longlong0" ) << QVariant( (qlonglong)0 ) << false;
949     QTest::newRow( "longlong1" ) << QVariant( (qlonglong)1 ) << true;
950     QTest::newRow( "ulonglong0" ) << QVariant( (qulonglong)0 ) << false;
951     QTest::newRow( "ulonglong1" ) << QVariant( (qulonglong)1 ) << true;
952     QTest::newRow( "QChar" ) << QVariant(QChar('a')) << true;
953     QTest::newRow( "Null_QChar" ) << QVariant(QChar(0)) << false;
954 }
955
956 void tst_QVariant::toBool()
957 {
958     QFETCH( QVariant, value );
959     QFETCH( bool, result );
960     QVERIFY( value.isValid() );
961     QVERIFY( value.canConvert( QVariant::Bool ) );
962
963     bool i = value.toBool();
964     QCOMPARE( i, result );
965 }
966
967 void tst_QVariant::toPointF_data()
968 {
969     QTest::addColumn<QVariant>("value");
970     QTest::addColumn<QPointF>("result");
971
972     QTest::newRow( "QPoint" ) << QVariant( QPointF( 19, 84) ) << QPointF( 19, 84 );
973 }
974
975 void tst_QVariant::toPointF()
976 {
977     QFETCH( QVariant, value );
978     QFETCH( QPointF, result );
979     QVERIFY( value.isValid() );
980     QVERIFY( value.canConvert( QVariant::PointF ) );
981     QPointF d = value.toPointF();
982     QCOMPARE( d, result );
983 }
984
985 void tst_QVariant::toRectF_data()
986 {
987     QTest::addColumn<QVariant>("value");
988     QTest::addColumn<QRectF>("result");
989
990     QRect r( 1, 9, 8, 4 );
991     QRectF rf( 1.0, 9.0, 8.0, 4.0 );
992     QTest::newRow( "QRect" ) << QVariant( r ) << rf;
993 }
994
995 void tst_QVariant::toRectF()
996 {
997     QFETCH( QVariant, value );
998     QFETCH( QRectF, result );
999     QVERIFY( value.isValid() );
1000     QVERIFY( value.canConvert( QVariant::RectF ) );
1001     QRectF d = value.toRectF();
1002     QCOMPARE( d, result );
1003 }
1004
1005 void tst_QVariant::toColor_data()
1006 {
1007     QTest::addColumn<QVariant>("value");
1008     QTest::addColumn<QColor>("result");
1009
1010     QColor c("red");
1011     QTest::newRow( "string" ) << QVariant( QString( "red" ) ) << c;
1012     QTest::newRow( "solid brush" ) << QVariant( QBrush(c) ) << c;
1013 }
1014
1015 void tst_QVariant::toColor()
1016 {
1017     QFETCH( QVariant, value );
1018     QFETCH( QColor, result );
1019     QVERIFY( value.isValid() );
1020     QVERIFY( value.canConvert( QVariant::Color ) );
1021     QColor d = qvariant_cast<QColor>(value);
1022     QCOMPARE( d, result );
1023 }
1024
1025 void tst_QVariant::toPixmap_data()
1026 {
1027     QTest::addColumn<QVariant>("value");
1028     QTest::addColumn<QPixmap>("result");
1029
1030     QPixmap pm(30, 30);
1031     pm.fill(Qt::red);
1032     QTest::newRow( "image" ) << QVariant( pm ) << pm;
1033
1034     QBitmap bm(30, 30);
1035     bm.fill(Qt::color1);
1036     QTest::newRow( "bitmap" ) << QVariant( bm ) << QPixmap(bm);
1037 }
1038
1039 void tst_QVariant::toPixmap()
1040 {
1041     QFETCH( QVariant, value );
1042     QFETCH( QPixmap, result );
1043     QVERIFY( value.isValid() );
1044     QVERIFY( value.canConvert( QVariant::Pixmap ) );
1045     QPixmap d = qvariant_cast<QPixmap>(value);
1046     QCOMPARE( d, result );
1047 }
1048
1049 void tst_QVariant::toImage_data()
1050 {
1051     QTest::addColumn<QVariant>("value");
1052     QTest::addColumn<QImage>("result");
1053
1054     QImage im(30, 30, QImage::Format_ARGB32);
1055     im.fill(0x7fff0000);
1056     QTest::newRow( "image" ) << QVariant( im ) << im;
1057 }
1058
1059 void tst_QVariant::toImage()
1060 {
1061     QFETCH( QVariant, value );
1062     QFETCH( QImage, result );
1063     QVERIFY( value.isValid() );
1064     QVERIFY( value.canConvert( QVariant::Image ) );
1065     QImage d = qvariant_cast<QImage>(value);
1066     QCOMPARE( d, result );
1067 }
1068
1069 void tst_QVariant::toBrush_data()
1070 {
1071     QTest::addColumn<QVariant>("value");
1072     QTest::addColumn<QBrush>("result");
1073
1074     QColor c(Qt::red);
1075     QTest::newRow( "color" ) << QVariant( c ) << QBrush(c);
1076     QPixmap pm(30, 30);
1077     pm.fill(c);
1078     QTest::newRow( "pixmap" ) << QVariant( pm ) << QBrush(pm);
1079 }
1080
1081 void tst_QVariant::toBrush()
1082 {
1083     QFETCH( QVariant, value );
1084     QFETCH( QBrush, result );
1085     QVERIFY( value.isValid() );
1086     QVERIFY( value.canConvert( QVariant::Brush ) );
1087     QBrush d = qvariant_cast<QBrush>(value);
1088     QCOMPARE( d, result );
1089 }
1090
1091 void tst_QVariant::toFont_data()
1092 {
1093     QTest::addColumn<QVariant>("value");
1094     QTest::addColumn<QFont>("result");
1095
1096     QFont f("times",12,-1,false);
1097     QTest::newRow( "string" ) << QVariant( QString( "times,12,-1,5,50,0,0,0,0,0" ) ) << f;
1098 }
1099
1100 void tst_QVariant::toFont()
1101 {
1102     QFETCH( QVariant, value );
1103     QFETCH( QFont, result );
1104     QVERIFY( value.isValid() );
1105     QVERIFY( value.canConvert( QVariant::Font ) );
1106     QFont d = qvariant_cast<QFont>(value);
1107     QCOMPARE( d, result );
1108 }
1109
1110 void tst_QVariant::toKeySequence_data()
1111 {
1112     QTest::addColumn<QVariant>("value");
1113     QTest::addColumn<QKeySequence>("result");
1114
1115
1116     QTest::newRow( "int" ) << QVariant( 67108929 ) << QKeySequence( Qt::CTRL + Qt::Key_A );
1117
1118
1119     QTest::newRow( "qstring" )
1120         << QVariant( QString( "Ctrl+A" ) )
1121         << QKeySequence( Qt::CTRL + Qt::Key_A );
1122 }
1123
1124 void tst_QVariant::toKeySequence()
1125 {
1126     QFETCH( QVariant, value );
1127     QFETCH( QKeySequence, result );
1128     QVERIFY( value.isValid() );
1129     QVERIFY( value.canConvert( QVariant::KeySequence ) );
1130     QKeySequence d = qvariant_cast<QKeySequence>(value);
1131     QCOMPARE( d, result );
1132 }
1133
1134 void tst_QVariant::toDouble_data()
1135 {
1136     QTest::addColumn<QVariant>("value");
1137     QTest::addColumn<double>("result");
1138     QTest::addColumn<bool>("valueOK");
1139
1140     QByteArray bytearray(4, ' ');
1141     bytearray[0] = '3';
1142     bytearray[1] = '2';
1143     bytearray[2] = '.';
1144     bytearray[3] = '1';
1145     QTest::newRow( "bytearray" ) << QVariant( bytearray ) << 32.1 << true;
1146 }
1147
1148 void tst_QVariant::toDouble()
1149 {
1150     QFETCH( QVariant, value );
1151     QFETCH( double, result );
1152     QFETCH( bool, valueOK );
1153     QVERIFY( value.isValid() );
1154     QVERIFY( value.canConvert( QVariant::Double ) );
1155     bool ok;
1156     double d = value.toDouble( &ok );
1157     QCOMPARE( d, result );
1158     QVERIFY( ok == valueOK );
1159 }
1160
1161 void tst_QVariant::toLongLong_data()
1162 {
1163     QTest::addColumn<QVariant>("value");
1164     QTest::addColumn<qlonglong>("result");
1165     QTest::addColumn<bool>("valueOK");
1166
1167     QTest::newRow( "int0" ) << QVariant( 123 ) << (qlonglong)123 << true;
1168     QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (qlonglong)3 << true;
1169     QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (qlonglong)3 << true;
1170     QTest::newRow( "uint" ) << QVariant( 123u ) << (qlonglong)123 << true;
1171     QTest::newRow( "int-string" ) << QVariant( QString("123") )
1172                                << (qlonglong)123 << true;
1173     QTest::newRow( "string" ) << QVariant( QString("Unicode fun") ) << (qlonglong)0
1174                            << false;
1175     QTest::newRow( "longlong" ) << QVariant( intMax1 ) << intMax1 << true;
1176     QTest::newRow( "ulonglong" ) << QVariant( uintMax1 ) << (qlonglong)uintMax1 << true;
1177     QTest::newRow( "QChar" ) << QVariant(QChar('a')) << qlonglong('a') << true;
1178     QByteArray bytearray(4, ' ');
1179     bytearray[0] = '3';
1180     bytearray[1] = '2';
1181     bytearray[2] = '0';
1182     bytearray[3] = '0';
1183     QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (qlonglong) 3200 << true;
1184 }
1185
1186 void tst_QVariant::toLongLong()
1187 {
1188     QFETCH( QVariant, value );
1189     QFETCH( qlonglong, result );
1190     QFETCH( bool, valueOK );
1191     QVERIFY( value.isValid() );
1192     QVERIFY( value.canConvert( QVariant::LongLong ) );
1193     bool ok;
1194     qlonglong ll = value.toLongLong( &ok );
1195     QCOMPARE( ll, result );
1196     QVERIFY( ok == valueOK );
1197 }
1198
1199 void tst_QVariant::toULongLong_data()
1200 {
1201     QTest::addColumn<QVariant>("value");
1202     QTest::addColumn<qulonglong>("result");
1203     QTest::addColumn<bool>("valueOK");
1204
1205     QTest::newRow( "int0" ) << QVariant( 123 ) << (qulonglong)123 << true;
1206     QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (qulonglong)3 << true;
1207     QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (qulonglong)3 << true;
1208     QTest::newRow( "uint" ) << QVariant( 123u ) << (qulonglong)123 << true;
1209     QTest::newRow( "int-string" ) << QVariant( QString("123") )
1210                                << (qulonglong)123 << true;
1211     QTest::newRow( "string" ) << QVariant( QString("Unicode fun") ) << (qulonglong)0
1212                            << false;
1213     QTest::newRow( "ulonglong-string" ) << QVariant( QString("18446744073709551615") )
1214                                      << Q_UINT64_C(18446744073709551615)
1215                                      << true;
1216     QTest::newRow( "bytaa-string" ) << QVariant( QString("18446744073709551615") )
1217                                      << Q_UINT64_C(18446744073709551615)
1218                                      << true;
1219     QTest::newRow( "longlong" ) << QVariant( intMax1 ) << (qulonglong)intMax1 << true;
1220     QTest::newRow( "ulonglong" ) << QVariant( uintMax1 ) << uintMax1 << true;
1221     QTest::newRow( "QChar" ) << QVariant(QChar('a')) << qulonglong('a') << true;
1222     QByteArray bytearray(4, ' ');
1223     bytearray[0] = '3';
1224     bytearray[1] = '2';
1225     bytearray[2] = '0';
1226     bytearray[3] = '1';
1227     QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (qulonglong) 3201 << true;
1228 }
1229
1230 void tst_QVariant::toULongLong()
1231 {
1232     QFETCH( QVariant, value );
1233     QFETCH( qulonglong, result );
1234     QFETCH( bool, valueOK );
1235     QVERIFY( value.isValid() );
1236     QVERIFY( value.canConvert( QVariant::ULongLong ) );
1237     bool ok;
1238     qulonglong ll = value.toULongLong( &ok );
1239     QCOMPARE( ll, result );
1240     QVERIFY( ok == valueOK );
1241 }
1242
1243 void tst_QVariant::toByteArray_data()
1244 {
1245     QTest::addColumn<QVariant>("value");
1246     QTest::addColumn<QByteArray>("result");
1247
1248     QByteArray ba(5, ' ');
1249     ba[0] = 'T';
1250     ba[1] = 'e';
1251     ba[2] = 's';
1252     ba[3] = 't';
1253     ba[4] = '\0';
1254
1255     QByteArray variantBa = ba;
1256
1257     QTest::newRow( "qbytearray" ) << QVariant( variantBa ) << ba;
1258     QTest::newRow( "int" ) << QVariant( -123 ) << QByteArray( "-123" );
1259     QTest::newRow( "uint" ) << QVariant( (uint)123 ) << QByteArray( "123" );
1260     QTest::newRow( "double" ) << QVariant( 123.456 ) << QByteArray( "123.456" );
1261     QTest::newRow( "float" ) << QVariant( 123.456f ) << QByteArray( "123.456" );
1262     QTest::newRow( "longlong" ) << QVariant( (qlonglong)34 ) << QByteArray( "34" );
1263     QTest::newRow( "ulonglong" ) << QVariant( (qulonglong)34 ) << QByteArray( "34" );
1264 }
1265
1266 void tst_QVariant::toByteArray()
1267 {
1268     QFETCH( QVariant, value );
1269     QFETCH( QByteArray, result );
1270     QVERIFY( value.isValid() );
1271     QVERIFY( value.canConvert( QVariant::ByteArray ) );
1272     QByteArray ba = value.toByteArray();
1273     QCOMPARE( ba, result );
1274 }
1275
1276 void tst_QVariant::toString_data()
1277 {
1278     QTest::addColumn<QVariant>("value");
1279     QTest::addColumn<QString>("result");
1280
1281     QTest::newRow( "qstring" ) << QVariant( QString( "Test" ) ) << QString( "Test" );
1282     QTest::newRow( "charstar" ) << QVariant(QLatin1String("Test")) << QString("Test");
1283     QTest::newRow( "qbytearray") << QVariant( QByteArray( "Test\0" ) ) << QString( "Test" );
1284     QTest::newRow( "int" ) << QVariant( -123 ) << QString( "-123" );
1285     QTest::newRow( "uint" ) << QVariant( (uint)123 ) << QString( "123" );
1286     QTest::newRow( "double" ) << QVariant( 123.456 ) << QString( "123.456" );
1287     QTest::newRow( "float" ) << QVariant( 123.456f ) << QString( "123.456" );
1288     QTest::newRow( "bool" ) << QVariant( true ) << QString( "true" );
1289     QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 1, 1 ) ) << QString( "2002-01-01" );
1290     QTest::newRow( "qtime" ) << QVariant( QTime( 12, 34, 56 ) ) << QString( "12:34:56" );
1291     QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 1, 1 ), QTime( 12, 34, 56 ) ) ) << QString( "2002-01-01T12:34:56" );
1292     QTest::newRow( "qkeysequence" ) << QVariant::fromValue( QKeySequence( Qt::CTRL + Qt::Key_A ) )
1293 #ifndef Q_OS_MAC
1294         << QString( "Ctrl+A" );
1295 #else
1296         << QString(QChar(0x2318)) + "A";
1297 #endif
1298
1299     QFont font( "times", 12 );
1300     QTest::newRow( "qfont" ) << QVariant::fromValue( font ) << QString("times,12,-1,5,50,0,0,0,0,0");
1301     QTest::newRow( "qcolor" ) << QVariant::fromValue( QColor( 10, 10, 10 ) ) << QString( "#0a0a0a" );
1302     QTest::newRow( "llong" ) << QVariant( (qlonglong)Q_INT64_C(123456789012) ) <<
1303         QString( "123456789012" );
1304 }
1305
1306 void tst_QVariant::toString()
1307 {
1308     QFETCH( QVariant, value );
1309     QFETCH( QString, result );
1310     QVERIFY( value.isValid() );
1311     QVERIFY( value.canConvert( QVariant::String ) );
1312     QString str = value.toString();
1313     QCOMPARE( str, result );
1314 }
1315
1316 void tst_QVariant::toDate_data()
1317 {
1318     QTest::addColumn<QVariant>("value");
1319     QTest::addColumn<QDate>("result");
1320
1321     QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 10, 10 ) ) << QDate( 2002, 10, 10 );
1322     QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) ) ) << QDate( 2002, 10, 10 );
1323     QTest::newRow( "qstring" ) << QVariant( QString( "2002-10-10" ) ) << QDate( 2002, 10, 10 );
1324 }
1325
1326 void tst_QVariant::toDate()
1327 {
1328     QFETCH( QVariant, value );
1329     QFETCH( QDate, result );
1330     QVERIFY( value.isValid() );
1331     QVERIFY( value.canConvert( QVariant::Date ) );
1332     QCOMPARE( value.toDate(), result );
1333 }
1334
1335 void tst_QVariant::toTime_data()
1336 {
1337     QTest::addColumn<QVariant>("value");
1338     QTest::addColumn<QTime>("result");
1339
1340     QTest::newRow( "qtime" ) << QVariant( QTime( 12, 34, 56 ) ) << QTime( 12, 34, 56 );
1341     QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) ) ) << QTime( 12, 34, 56 );
1342     QTest::newRow( "qstring" ) << QVariant( QString( "12:34:56" ) ) << QTime( 12, 34, 56 );
1343 }
1344
1345 void tst_QVariant::toTime()
1346 {
1347     QFETCH( QVariant, value );
1348     QFETCH( QTime, result );
1349     QVERIFY( value.isValid() );
1350     QVERIFY( value.canConvert( QVariant::Time ) );
1351     QCOMPARE( value.toTime(), result );
1352 }
1353
1354 void tst_QVariant::toDateTime_data()
1355 {
1356     QTest::addColumn<QVariant>("value");
1357     QTest::addColumn<QDateTime>("result");
1358
1359     QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) ) )
1360         << QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) );
1361     QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 10, 10 ) ) << QDateTime( QDate( 2002, 10, 10 ), QTime( 0, 0, 0 ) );
1362     QTest::newRow( "qstring" ) << QVariant( QString( "2002-10-10T12:34:56" ) ) << QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) );
1363 }
1364
1365 void tst_QVariant::toDateTime()
1366 {
1367     QFETCH( QVariant, value );
1368     QFETCH( QDateTime, result );
1369     QVERIFY( value.isValid() );
1370     QVERIFY( value.canConvert( QVariant::DateTime ) );
1371     QCOMPARE( value.toDateTime(), result );
1372 }
1373
1374 void tst_QVariant::toLocale()
1375 {
1376     QVariant variant;
1377     QLocale loc = variant.toLocale();
1378     variant = QLocale::system();
1379     loc = variant.toLocale();
1380 }
1381
1382 void tst_QVariant::toRegExp()
1383 {
1384     QVariant variant;
1385     QRegExp rx = variant.toRegExp();
1386     variant = QRegExp("foo");
1387     rx = variant.toRegExp();
1388 }
1389
1390 void tst_QVariant::toRegularExpression()
1391 {
1392     QVariant variant;
1393     QRegularExpression re = variant.toRegularExpression();
1394     QCOMPARE(re, QRegularExpression());
1395
1396     variant = QRegularExpression("abc.*def");
1397     re = variant.toRegularExpression();
1398     QCOMPARE(re, QRegularExpression("abc.*def"));
1399
1400     variant = QVariant::fromValue(QRegularExpression("[ab]\\w+"));
1401     re = variant.value<QRegularExpression>();
1402     QCOMPARE(re, QRegularExpression("[ab]\\w+"));
1403 }
1404
1405 void tst_QVariant::matrix()
1406 {
1407     QVariant variant;
1408     QMatrix matrix = qvariant_cast<QMatrix>(variant);
1409     QVERIFY(matrix.isIdentity());
1410     variant.setValue(QMatrix().rotate(90));
1411     QCOMPARE(QMatrix().rotate(90), qvariant_cast<QMatrix>(variant));
1412
1413     void *mmatrix = QMetaType::create(QVariant::Matrix, 0);
1414     QVERIFY(mmatrix);
1415     QMetaType::destroy(QVariant::Matrix, mmatrix);
1416 }
1417
1418 void tst_QVariant::matrix4x4()
1419 {
1420     QVariant variant;
1421     QMatrix4x4 matrix = qvariant_cast<QMatrix4x4>(variant);
1422     QVERIFY(matrix.isIdentity());
1423     QMatrix4x4 m;
1424     m.scale(2.0f);
1425     variant.setValue(m);
1426     QCOMPARE(m, qvariant_cast<QMatrix4x4>(variant));
1427
1428     void *mmatrix = QMetaType::create(QVariant::Matrix4x4, 0);
1429     QVERIFY(mmatrix);
1430     QMetaType::destroy(QVariant::Matrix4x4, mmatrix);
1431 }
1432
1433 void tst_QVariant::transform()
1434 {
1435     QVariant variant;
1436     QTransform matrix = qvariant_cast<QTransform>(variant);
1437     QVERIFY(matrix.isIdentity());
1438     variant.setValue(QTransform().rotate(90));
1439     QCOMPARE(QTransform().rotate(90), qvariant_cast<QTransform>(variant));
1440
1441     void *mmatrix = QMetaType::create(QVariant::Transform, 0);
1442     QVERIFY(mmatrix);
1443     QMetaType::destroy(QVariant::Transform, mmatrix);
1444 }
1445
1446
1447 void tst_QVariant::vector2D()
1448 {
1449     QVariant variant;
1450     QVector2D vector = qvariant_cast<QVector2D>(variant);
1451     QVERIFY(vector.isNull());
1452     variant.setValue(QVector2D(0.1, 0.2));
1453     QCOMPARE(QVector2D(0.1, 0.2), qvariant_cast<QVector2D>(variant));
1454
1455     void *pvector = QMetaType::create(QVariant::Vector2D, 0);
1456     QVERIFY(pvector);
1457     QMetaType::destroy(QVariant::Vector2D, pvector);
1458 }
1459
1460 void tst_QVariant::vector3D()
1461 {
1462     QVariant variant;
1463     QVector3D vector = qvariant_cast<QVector3D>(variant);
1464     QVERIFY(vector.isNull());
1465     variant.setValue(QVector3D(0.1, 0.2, 0.3));
1466     QCOMPARE(QVector3D(0.1, 0.2, 0.3), qvariant_cast<QVector3D>(variant));
1467
1468     void *pvector = QMetaType::create(QVariant::Vector3D, 0);
1469     QVERIFY(pvector);
1470     QMetaType::destroy(QVariant::Vector3D, pvector);
1471 }
1472
1473 void tst_QVariant::vector4D()
1474 {
1475     QVariant variant;
1476     QVector4D vector = qvariant_cast<QVector4D>(variant);
1477     QVERIFY(vector.isNull());
1478     variant.setValue(QVector4D(0.1, 0.2, 0.3, 0.4));
1479     QCOMPARE(QVector4D(0.1, 0.2, 0.3, 0.4), qvariant_cast<QVector4D>(variant));
1480
1481     void *pvector = QMetaType::create(QVariant::Vector4D, 0);
1482     QVERIFY(pvector);
1483     QMetaType::destroy(QVariant::Vector4D, pvector);
1484 }
1485
1486 void tst_QVariant::quaternion()
1487 {
1488     QVariant variant;
1489     QQuaternion quaternion = qvariant_cast<QQuaternion>(variant);
1490     QVERIFY(quaternion.isIdentity());
1491     variant.setValue(QQuaternion(0.1, 0.2, 0.3, 0.4));
1492     QCOMPARE(QQuaternion(0.1, 0.2, 0.3, 0.4), qvariant_cast<QQuaternion>(variant));
1493
1494     void *pquaternion = QMetaType::create(QVariant::Quaternion, 0);
1495     QVERIFY(pquaternion);
1496     QMetaType::destroy(QVariant::Quaternion, pquaternion);
1497 }
1498
1499 struct CustomStreamableClass
1500 {
1501     int i;
1502     bool operator==(const CustomStreamableClass& other) const
1503     {
1504         return i == other.i;
1505     }
1506 };
1507 Q_DECLARE_METATYPE(CustomStreamableClass);
1508
1509 QDataStream &operator<<(QDataStream &out, const CustomStreamableClass &myObj)
1510 {
1511     return out << myObj.i;
1512 }
1513
1514 QDataStream &operator>>(QDataStream &in, CustomStreamableClass &myObj)
1515 {
1516     return in >> myObj.i;
1517 }
1518
1519 void tst_QVariant::writeToReadFromDataStream_data()
1520 {
1521     qRegisterMetaTypeStreamOperators<CustomStreamableClass>();
1522
1523     QTest::addColumn<QVariant>("writeVariant");
1524     QTest::addColumn<bool>("isNull");
1525     {
1526         typedef QList<QVariant> variantsList;
1527         variantsList valuelist;
1528         valuelist << QVariant( 1 ) << QVariant( QString("Two") ) << QVariant( 3.45 );
1529         QVariant var(valuelist);
1530         QTest::newRow( "list_valid" ) << var << false;
1531     }
1532
1533     QTest::newRow( "invalid" ) << QVariant() << true;
1534     QTest::newRow( "bitarray_invalid" ) << QVariant( QBitArray() ) << true;
1535     QBitArray bitarray( 3 );
1536     bitarray[0] = 0;
1537     bitarray[1] = 1;
1538     bitarray[2] = 0;
1539     QTest::newRow( "bitarray_valid" ) << QVariant( bitarray ) << false;
1540     QTest::newRow( "bytearray_invalid" ) << QVariant( QByteArray() ) << true;
1541     QTest::newRow( "int_invalid") << QVariant(QVariant::Int) << true;
1542     QByteArray bytearray(5, ' ');
1543     bytearray[0] = 'T';
1544     bytearray[1] = 'e';
1545     bytearray[2] = 's';
1546     bytearray[3] = 't';
1547     bytearray[4] = '\0';
1548     QTest::newRow( "bytearray_valid" ) << QVariant( bytearray ) << false;
1549     QTest::newRow( "bitmap_invalid" ) << QVariant::fromValue( QBitmap() ) << true;
1550     QBitmap bitmap( 10, 10 );
1551     bitmap.fill( Qt::red );
1552     QTest::newRow( "bitmap_valid" ) << QVariant::fromValue( bitmap ) << false;
1553     QTest::newRow( "brush_valid" ) << QVariant::fromValue( QBrush( Qt::red ) ) << false;
1554     QTest::newRow( "color_valid" ) << QVariant::fromValue( QColor( Qt::red ) ) << false;
1555 #ifndef QT_NO_CURSOR
1556     QTest::newRow( "cursor_valid" ) << QVariant::fromValue( QCursor( Qt::PointingHandCursor ) ) << false;
1557 #endif
1558     QTest::newRow( "date_invalid" ) << QVariant( QDate() ) << true;
1559     QTest::newRow( "date_valid" ) << QVariant( QDate( 2002, 07, 06 ) ) << false;
1560     QTest::newRow( "datetime_invalid" ) << QVariant( QDateTime() ) << true;
1561     QTest::newRow( "datetime_valid" ) << QVariant( QDateTime( QDate( 2002, 07, 06 ), QTime( 14, 0, 0 ) ) ) << false;
1562     QTest::newRow( "double_valid" ) << QVariant( 123.456 ) << false;
1563     QTest::newRow( "float_valid" ) << QVariant( 123.456f ) << false;
1564     QTest::newRow( "font_valid" ) << QVariant::fromValue( QFont( "times", 12 ) ) << false;
1565     QTest::newRow( "pixmap_invalid" ) << QVariant::fromValue( QPixmap() ) << true;
1566     QPixmap pixmap( 10, 10 );
1567     pixmap.fill( Qt::red );
1568     QTest::newRow( "pixmap_valid" ) << QVariant::fromValue( pixmap ) << false;
1569 //    QTest::newRow( "iconset_valid" ) << QVariant( QIcon( pixmap ) ) << false;
1570     QTest::newRow( "image_invalid" ) << QVariant::fromValue( QImage() ) << true;
1571     QTest::newRow( "keysequence_valid" ) << QVariant::fromValue( QKeySequence( Qt::CTRL + Qt::Key_A ) ) << false;
1572     QTest::newRow( "int_valid" ) << QVariant( -123 ) << false;
1573     typedef QMap<QString, QVariant> variantsMap;
1574     variantsMap vMap;
1575     vMap.insert( "int", QVariant( 1 ) );
1576     vMap.insert( "string", QVariant( QString("Two") ) );
1577     vMap.insert( "double", QVariant( 3.45 ) );
1578     vMap.insert( "float", QVariant( 3.45f ) );
1579     QTest::newRow( "map_valid" ) << QVariant( vMap ) << false;
1580     QTest::newRow( "palette_valid" ) << QVariant::fromValue(QPalette(QColor("turquoise"))) << false;
1581     QTest::newRow( "pen_valid" ) << QVariant::fromValue( QPen( Qt::red ) ) << false;
1582     QTest::newRow( "pointarray_invalid" ) << QVariant::fromValue( QPolygon() ) << true;
1583     QTest::newRow( "pointarray_valid" ) << QVariant::fromValue( QPolygon( QRect( 10, 10, 20, 20 ) ) ) << false;
1584     QTest::newRow( "region_invalid" ) << QVariant::fromValue( QRegion() ) << true;
1585     QTest::newRow( "region_valid" ) << QVariant::fromValue( QRegion( 10, 10, 20, 20 ) ) << false;
1586     QTest::newRow( "sizepolicy_valid" ) << QVariant::fromValue( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) ) << false;
1587     QTest::newRow( "point_invalid" ) << QVariant::fromValue( QPoint() ) << true;
1588     QTest::newRow( "point_valid" ) << QVariant::fromValue( QPoint( 10, 10 ) ) << false;
1589     QTest::newRow( "rect_invalid" ) << QVariant( QRect() ) << true;
1590     QTest::newRow( "rect_valid" ) << QVariant( QRect( 10, 10, 20, 20 ) ) << false;
1591     QTest::newRow( "size_invalid" ) << QVariant( QSize( 0, 0 ) ) << true;
1592     QTest::newRow( "size_valid" ) << QVariant( QSize( 10, 10 ) ) << false;
1593     QTest::newRow( "string_invalid" ) << QVariant( QString() ) << true;
1594     QTest::newRow( "string_valid" ) << QVariant( QString( "Test" ) ) << false;
1595     QStringList stringlist;
1596     stringlist << "One" << "Two" << "Three";
1597     QTest::newRow( "stringlist_valid" ) << QVariant( stringlist ) << false;
1598     QTest::newRow( "time_invalid" ) << QVariant( QTime() ) << true;
1599     QTest::newRow( "time_valid" ) << QVariant( QTime( 14, 0, 0 ) ) << false;
1600     QTest::newRow( "uint_valid" ) << QVariant( (uint)123 ) << false;
1601     QTest::newRow( "qchar" ) << QVariant(QChar('a')) << false;
1602     QTest::newRow( "qchar_null" ) << QVariant(QChar(0)) << true;
1603     QTest::newRow( "regexp" ) << QVariant(QRegExp("foo", Qt::CaseInsensitive)) << false;
1604     QTest::newRow( "regexp_empty" ) << QVariant(QRegExp()) << false;
1605     QTest::newRow( "regularexpression" ) << QVariant(QRegularExpression("abc.*def")) << false;
1606     QTest::newRow( "regularexpression_empty" ) << QVariant(QRegularExpression()) << false;
1607
1608     // types known to QMetaType, but not part of QVariant::Type
1609     QTest::newRow("QMetaType::Long invalid") << QVariant(QMetaType::Long, (void *) 0) << false;
1610     long longInt = -1l;
1611     QTest::newRow("QMetaType::Long") << QVariant(QMetaType::Long, &longInt) << false;
1612     QTest::newRow("QMetaType::Short invalid") << QVariant(QMetaType::Short, (void *) 0) << false;
1613     short shortInt = 1;
1614     QTest::newRow("QMetaType::Short") << QVariant(QMetaType::Short, &shortInt) << false;
1615     QTest::newRow("QMetaType::Char invalid") << QVariant(QMetaType::Char, (void *) 0) << false;
1616     char ch = 'c';
1617     QTest::newRow("QMetaType::Char") << QVariant(QMetaType::Char, &ch) << false;
1618     QTest::newRow("QMetaType::ULong invalid") << QVariant(QMetaType::ULong, (void *) 0) << false;
1619     ulong ulongInt = 1ul;
1620     QTest::newRow("QMetaType::ULong") << QVariant(QMetaType::ULong, &ulongInt) << false;
1621     QTest::newRow("QMetaType::UShort invalid") << QVariant(QMetaType::UShort, (void *) 0) << false;
1622     ushort ushortInt = 1u;
1623     QTest::newRow("QMetaType::UShort") << QVariant(QMetaType::UShort, &ushortInt) << false;
1624     QTest::newRow("QMetaType::UChar invalid") << QVariant(QMetaType::UChar, (void *) 0) << false;
1625     uchar uch = 0xf0;
1626     QTest::newRow("QMetaType::UChar") << QVariant(QMetaType::UChar, &uch) << false;
1627     QTest::newRow("QMetaType::Float invalid") << QVariant(QMetaType::Float, (void *) 0) << false;
1628     float f = 1.234f;
1629     QTest::newRow("QMetaType::Float") << QVariant(QMetaType::Float, &f) << false;
1630     CustomStreamableClass custom = {123};
1631     QTest::newRow("Custom type") << QVariant::fromValue(custom) << false;
1632 }
1633
1634 void tst_QVariant::writeToReadFromDataStream()
1635 {
1636     QFETCH( QVariant, writeVariant );
1637     QFETCH( bool, isNull );
1638     QByteArray data;
1639
1640     QDataStream writeStream( &data, QIODevice::WriteOnly );
1641     writeStream << writeVariant;
1642
1643     QVariant readVariant;
1644     QDataStream readStream( &data, QIODevice::ReadOnly );
1645     readStream >> readVariant;
1646     QVERIFY( readVariant.isNull() == isNull );
1647     // Best way to confirm the readVariant contains the same data?
1648     // Since only a few won't match since the serial numbers are different
1649     // I won't bother adding another bool in the data test.
1650     const int writeType = writeVariant.userType();
1651     if (writeType == qMetaTypeId<CustomStreamableClass>())
1652         QCOMPARE(qvariant_cast<CustomStreamableClass>(readVariant), qvariant_cast<CustomStreamableClass>(writeVariant));
1653     else if ( writeType != QVariant::Invalid && writeType != QVariant::Bitmap && writeType != QVariant::Pixmap
1654         && writeType != QVariant::Image) {
1655         switch (writeType) {
1656         default:
1657             QCOMPARE( readVariant, writeVariant );
1658             break;
1659
1660         // compare types know by QMetaType but not QVariant (QVariant::operator==() knows nothing about them)
1661         case QMetaType::Long:
1662             QCOMPARE(qvariant_cast<long>(readVariant), qvariant_cast<long>(writeVariant));
1663             break;
1664         case QMetaType::ULong:
1665             QCOMPARE(qvariant_cast<ulong>(readVariant), qvariant_cast<ulong>(writeVariant));
1666             break;
1667         case QMetaType::Short:
1668             QCOMPARE(qvariant_cast<short>(readVariant), qvariant_cast<short>(writeVariant));
1669             break;
1670         case QMetaType::UShort:
1671             QCOMPARE(qvariant_cast<ushort>(readVariant), qvariant_cast<ushort>(writeVariant));
1672             break;
1673         case QMetaType::Char:
1674             QCOMPARE(qvariant_cast<char>(readVariant), qvariant_cast<char>(writeVariant));
1675             break;
1676         case QMetaType::UChar:
1677             QCOMPARE(qvariant_cast<uchar>(readVariant), qvariant_cast<uchar>(writeVariant));
1678             break;
1679         case QMetaType::Float:
1680             {
1681                 // the uninitialized float can be NaN (observed on Windows Mobile 5 ARMv4i)
1682                 float readFloat = qvariant_cast<float>(readVariant);
1683                 float writtenFloat = qvariant_cast<float>(writeVariant);
1684                 QVERIFY(qIsNaN(readFloat) == qIsNaN(writtenFloat));
1685                 if (!qIsNaN(readFloat))
1686                     QVERIFY(readFloat == writtenFloat);
1687             }
1688             break;
1689         }
1690     }
1691 }
1692
1693 void tst_QVariant::writeToReadFromOldDataStream()
1694 {
1695     QVariant writeVariant = QString("hello");
1696     QByteArray data;
1697
1698     QDataStream writeStream(&data, QIODevice::WriteOnly);
1699     writeStream.setVersion(QDataStream::Qt_2_1);
1700     writeStream << writeVariant;
1701
1702     QVariant readVariant;
1703     QDataStream readStream(&data, QIODevice::ReadOnly);
1704     readStream.setVersion(QDataStream::Qt_2_1);
1705     readStream >> readVariant;
1706
1707     QCOMPARE(writeVariant.userType(), readVariant.userType());
1708     QCOMPARE(writeVariant, readVariant);
1709 }
1710
1711 void tst_QVariant::checkDataStream()
1712 {
1713     QTest::ignoreMessage(QtWarningMsg, "Trying to construct an instance of an invalid type, type id: 49");
1714     const QByteArray settingsHex("00000031ffffffffff");
1715     const QByteArray settings = QByteArray::fromHex(settingsHex);
1716     QDataStream in(settings);
1717     QVariant v;
1718     in >> v;
1719     // the line below has been left out for now since the data stream
1720     // is not necessarily considered corrupt when an invalid QVariant is
1721     // constructed. However, it might be worth considering changing that behavior
1722     // in the future.
1723 //    QCOMPARE(in.status(), QDataStream::ReadCorruptData);
1724     QCOMPARE(v.type(), QVariant::Invalid);
1725 }
1726
1727 void tst_QVariant::operator_eq_eq_data()
1728 {
1729     QTest::addColumn<QVariant>("left");
1730     QTest::addColumn<QVariant>("right");
1731     QTest::addColumn<bool>("equal"); // left == right ?
1732
1733     QVariant inv;
1734     QVariant i0( int(0) );
1735     QVariant i1( int(1) );
1736     // Invalid
1737     QTest::newRow( "invinv" ) << inv << inv << true;
1738     // Int
1739     QTest::newRow( "int1int1" ) << i1 << i1 << true;
1740     QTest::newRow( "int1int0" ) << i1 << i0 << false;
1741     QTest::newRow( "nullint" ) << i0 << QVariant(QVariant::Int) << false;
1742
1743     // LongLong and ULongLong
1744     QVariant ll1( (qlonglong)1 );
1745     QVariant lln2( (qlonglong)-2 );
1746     QVariant ull1( (qulonglong)1 );
1747     QVariant ull3( (qulonglong)3 );
1748     QTest::newRow( "ll1ll1" ) << ll1 << ll1 << true;
1749     QTest::newRow( "ll1lln2" ) << ll1 << lln2 << false;
1750     QTest::newRow( "ll1ull1" ) << ull1 << ull1 << true;
1751     QTest::newRow( "ll1i1" ) << ull1 << i1 << true;
1752     QTest::newRow( "ull1ull1" ) << ull1 << ull1 << true;
1753     QTest::newRow( "ull1i1" ) << ull1 << ull1 << true;
1754
1755     QVariant mInt(-42);
1756     QVariant mIntString(QByteArray("-42"));
1757     QVariant mIntQString(QString("-42"));
1758
1759     QVariant mUInt(42u);
1760     QVariant mUIntString(QByteArray("42"));
1761     QVariant mUIntQString(QString("42"));
1762
1763     QVariant mDouble(42.11);
1764     QVariant mDoubleString(QByteArray("42.11"));
1765     QVariant mDoubleQString(QString("42.11"));
1766
1767     QVariant mFloat(42.11f);
1768     QVariant mFloatString(QByteArray("42.11"));
1769     QVariant mFloatQString(QString("42.11"));
1770
1771     QVariant mLongLong((qlonglong)-42);
1772     QVariant mLongLongString(QByteArray("-42"));
1773     QVariant mLongLongQString(QString("-42"));
1774
1775     QVariant mULongLong((qulonglong)42);
1776     QVariant mULongLongString(QByteArray("42"));
1777     QVariant mULongLongQString(QString("42"));
1778
1779     QVariant mBool(false);
1780     QVariant mBoolString(QByteArray("false"));
1781     QVariant mBoolQString(QString("false"));
1782
1783     QTest::newRow( "double_int" ) << QVariant(42.0) << QVariant(42) << true;
1784     QTest::newRow( "float_int" ) << QVariant(42.f) << QVariant(42) << true;
1785     QTest::newRow( "mInt_mIntString" ) << mInt << mIntString << true;
1786     QTest::newRow( "mIntString_mInt" ) << mIntString << mInt << true;
1787     QTest::newRow( "mInt_mIntQString" ) << mInt << mIntQString << true;
1788     QTest::newRow( "mIntQString_mInt" ) << mIntQString << mInt << true;
1789
1790     QTest::newRow( "mUInt_mUIntString" ) << mUInt << mUIntString << true;
1791     QTest::newRow( "mUIntString_mUInt" ) << mUIntString << mUInt << true;
1792     QTest::newRow( "mUInt_mUIntQString" ) << mUInt << mUIntQString << true;
1793     QTest::newRow( "mUIntQString_mUInt" ) << mUIntQString << mUInt << true;
1794
1795     QTest::newRow( "mDouble_mDoubleString" ) << mDouble << mDoubleString << true;
1796     QTest::newRow( "mDoubleString_mDouble" ) << mDoubleString << mDouble << true;
1797     QTest::newRow( "mDouble_mDoubleQString" ) << mDouble << mDoubleQString << true;
1798     QTest::newRow( "mDoubleQString_mDouble" ) << mDoubleQString << mDouble << true;
1799
1800     QTest::newRow( "mFloat_mFloatString" ) << mFloat << mFloatString << true;
1801     QTest::newRow( "mFloatString_mFloat" ) << mFloatString << mFloat << true;
1802     QTest::newRow( "mFloat_mFloatQString" ) << mFloat << mFloatQString << true;
1803     QTest::newRow( "mFloatQString_mFloat" ) << mFloatQString << mFloat << true;
1804
1805     QTest::newRow( "mLongLong_mLongLongString" ) << mLongLong << mLongLongString << true;
1806     QTest::newRow( "mLongLongString_mLongLong" ) << mLongLongString << mLongLong << true;
1807     QTest::newRow( "mLongLong_mLongLongQString" ) << mLongLong << mLongLongQString << true;
1808     QTest::newRow( "mLongLongQString_mLongLong" ) << mLongLongQString << mLongLong << true;
1809
1810     QTest::newRow( "mULongLong_mULongLongString" ) << mULongLong << mULongLongString << true;
1811     QTest::newRow( "mULongLongString_mULongLong" ) << mULongLongString << mULongLong << true;
1812     QTest::newRow( "mULongLong_mULongLongQString" ) << mULongLong << mULongLongQString << true;
1813     QTest::newRow( "mULongLongQString_mULongLong" ) << mULongLongQString << mULongLong << true;
1814
1815     QTest::newRow( "mBool_mBoolString" ) << mBool << mBoolString << true;
1816     QTest::newRow( "mBoolString_mBool" ) << mBoolString << mBool << true;
1817     QTest::newRow( "mBool_mBoolQString" ) << mBool << mBoolQString << true;
1818     QTest::newRow( "mBoolQString_mBool" ) << mBoolQString << mBool << true;
1819
1820     QTest::newRow("ba2qstring") << QVariant(QLatin1String("hallo")) << QVariant(QString("hallo")) << true;
1821     QTest::newRow("qstring2ba") << QVariant(QString("hallo")) << QVariant(QLatin1String("hallo")) << true;
1822     QTest::newRow("char_char") << QVariant(QChar('a')) << QVariant(QChar('a')) << true;
1823     QTest::newRow("char_char2") << QVariant(QChar('a')) << QVariant(QChar('b')) << false;
1824
1825     QTest::newRow("invalidConversion") << QVariant(QString("bubu")) << QVariant(0) << false;
1826     QTest::newRow("invalidConversionR") << QVariant(0) << QVariant(QString("bubu")) << false;
1827     // ### many other combinations missing
1828
1829     {
1830         QMap<QString, QVariant> map1;
1831         map1.insert( "X", 1 );
1832
1833         QMap<QString, QVariant> map2;
1834         map2.insert( "Y", 1 );
1835
1836         QTest::newRow("TwoItemsInEqual") << QVariant(map1) << QVariant(map2) << false;
1837
1838     }
1839
1840     {
1841         QMap<QString, QVariant> map1;
1842         map1.insert( "X", 1 );
1843
1844         QMap<QString, QVariant> map2;
1845         map2.insert( "X", 1 );
1846
1847         QTest::newRow("TwoItemsEqual") << QVariant(map1) << QVariant(map2) << true;
1848     }
1849
1850     {
1851         QMap<QString, QVariant> map1;
1852         map1.insert( "X", 1 );
1853
1854         QMap<QString, QVariant> map2;
1855
1856         QTest::newRow("PopulatedEmptyMap") << QVariant(map1) << QVariant(map2) << false;
1857     }
1858
1859     {
1860         QMap<QString, QVariant> map1;
1861
1862         QMap<QString, QVariant> map2;
1863         map2.insert( "X", 1 );
1864
1865         QTest::newRow("EmptyPopulatedMap") << QVariant(map1) << QVariant(map2) << false;
1866     }
1867
1868     {
1869         QMap<QString, QVariant> map1;
1870         map1.insert( "X", 1 );
1871         map1.insert( "Y", 1 );
1872
1873         QMap<QString, QVariant> map2;
1874         map2.insert( "X", 1 );
1875
1876         QTest::newRow("FirstLarger") << QVariant(map1) << QVariant(map2) << false;
1877     }
1878
1879     {
1880         QMap<QString, QVariant> map1;
1881         map1.insert( "X", 1 );
1882
1883         QMap<QString, QVariant> map2;
1884         map2.insert( "X", 1 );
1885         map2.insert( "Y", 1 );
1886
1887         QTest::newRow("SecondLarger") << QVariant(map1) << QVariant(map2) << false;
1888     }
1889
1890     // same thing with hash
1891     {
1892         QHash<QString, QVariant> hash1;
1893         hash1.insert( "X", 1 );
1894
1895         QHash<QString, QVariant> hash2;
1896         hash2.insert( "Y", 1 );
1897
1898         QTest::newRow("HashTwoItemsInEqual") << QVariant(hash1) << QVariant(hash2) << false;
1899
1900     }
1901
1902     {
1903         QHash<QString, QVariant> hash1;
1904         hash1.insert( "X", 1 );
1905
1906         QHash<QString, QVariant> hash2;
1907         hash2.insert( "X", 1 );
1908
1909         QTest::newRow("HashTwoItemsEqual") << QVariant(hash1) << QVariant(hash2) << true;
1910     }
1911
1912     {
1913         QHash<QString, QVariant> hash1;
1914         hash1.insert( "X", 1 );
1915
1916         QHash<QString, QVariant> hash2;
1917
1918         QTest::newRow("HashPopulatedEmptyHash") << QVariant(hash1) << QVariant(hash2) << false;
1919     }
1920
1921     {
1922         QHash<QString, QVariant> hash1;
1923
1924         QHash<QString, QVariant> hash2;
1925         hash2.insert( "X", 1 );
1926
1927         QTest::newRow("EmptyPopulatedHash") << QVariant(hash1) << QVariant(hash2) << false;
1928     }
1929
1930     {
1931         QHash<QString, QVariant> hash1;
1932         hash1.insert( "X", 1 );
1933         hash1.insert( "Y", 1 );
1934
1935         QHash<QString, QVariant> hash2;
1936         hash2.insert( "X", 1 );
1937
1938         QTest::newRow("HashFirstLarger") << QVariant(hash1) << QVariant(hash2) << false;
1939     }
1940
1941     {
1942         QHash<QString, QVariant> hash1;
1943         hash1.insert( "X", 1 );
1944
1945         QHash<QString, QVariant> hash2;
1946         hash2.insert( "X", 1 );
1947         hash2.insert( "Y", 1 );
1948
1949         QTest::newRow("HashSecondLarger") << QVariant(hash1) << QVariant(hash2) << false;
1950     }
1951 }
1952
1953 void tst_QVariant::operator_eq_eq()
1954 {
1955     QFETCH( QVariant, left );
1956     QFETCH( QVariant, right );
1957     QFETCH( bool, equal );
1958     QEXPECT_FAIL("nullint", "See QTBUG-22933", Continue);
1959     QCOMPARE( left == right, equal );
1960 }
1961
1962 void tst_QVariant::operator_eq_eq_rhs()
1963 {
1964     QVariant v = 42;
1965
1966     QVERIFY(v == 42);
1967     QVERIFY(42 == v);
1968
1969 #if 0
1970     /* This should _not_ compile */
1971     QStringList list;
1972     QDateTime dt;
1973
1974     QVERIFY(dt == list);
1975 #endif
1976 }
1977
1978 void tst_QVariant::typeName_data()
1979 {
1980     QTest::addColumn<int>("type");
1981     QTest::addColumn<QByteArray>("res");
1982     QTest::newRow("0") << int(QVariant::Invalid) << QByteArray("");
1983     QTest::newRow("1") << int(QVariant::Map) << QByteArray("QVariantMap");
1984     QTest::newRow("2") << int(QVariant::List) << QByteArray("QVariantList");
1985     QTest::newRow("3") << int(QVariant::String) << QByteArray("QString");
1986     QTest::newRow("4") << int(QVariant::StringList) << QByteArray("QStringList");
1987     QTest::newRow("5") << int(QVariant::Font) << QByteArray("QFont");
1988     QTest::newRow("6") << int(QVariant::Pixmap) << QByteArray("QPixmap");
1989     QTest::newRow("7") << int(QVariant::Brush) << QByteArray("QBrush");
1990     QTest::newRow("8") << int(QVariant::Rect) << QByteArray("QRect");
1991     QTest::newRow("9") << int(QVariant::Size) << QByteArray("QSize");
1992     QTest::newRow("10") << int(QVariant::Color) << QByteArray("QColor");
1993     QTest::newRow("11") << int(QVariant::Palette) << QByteArray("QPalette");
1994     QTest::newRow("12") << int(QVariant::Point) << QByteArray("QPoint");
1995     QTest::newRow("13") << int(QVariant::Image) << QByteArray("QImage");
1996     QTest::newRow("14") << int(QVariant::Int) << QByteArray("int");
1997     QTest::newRow("15") << int(QVariant::UInt) << QByteArray("uint");
1998     QTest::newRow("16") << int(QVariant::Bool) << QByteArray("bool");
1999     QTest::newRow("17") << int(QVariant::Double) << QByteArray("double");
2000     QTest::newRow("18") << int(QMetaType::Float) << QByteArray("float");
2001     QTest::newRow("19") << int(QVariant::Polygon) << QByteArray("QPolygon");
2002     QTest::newRow("20") << int(QVariant::Region) << QByteArray("QRegion");
2003     QTest::newRow("21") << int(QVariant::Bitmap) << QByteArray("QBitmap");
2004     QTest::newRow("22") << int(QVariant::Cursor) << QByteArray("QCursor");
2005     QTest::newRow("23") << int(QVariant::SizePolicy) << QByteArray("QSizePolicy");
2006     QTest::newRow("24") << int(QVariant::Date) << QByteArray("QDate");
2007     QTest::newRow("25") << int(QVariant::Time) << QByteArray("QTime");
2008     QTest::newRow("26") << int(QVariant::DateTime) << QByteArray("QDateTime");
2009     QTest::newRow("27") << int(QVariant::ByteArray) << QByteArray("QByteArray");
2010     QTest::newRow("28") << int(QVariant::BitArray) << QByteArray("QBitArray");
2011     QTest::newRow("29") << int(QVariant::KeySequence) << QByteArray("QKeySequence");
2012     QTest::newRow("30") << int(QVariant::Pen) << QByteArray("QPen");
2013     QTest::newRow("31") << int(QVariant::LongLong) << QByteArray("qlonglong");
2014     QTest::newRow("32") << int(QVariant::ULongLong) << QByteArray("qulonglong");
2015     QTest::newRow("33") << int(QVariant::Char) << QByteArray("QChar");
2016     QTest::newRow("34") << int(QVariant::Url) << QByteArray("QUrl");
2017     QTest::newRow("35") << int(QVariant::TextLength) << QByteArray("QTextLength");
2018     QTest::newRow("36") << int(QVariant::TextFormat) << QByteArray("QTextFormat");
2019     QTest::newRow("37") << int(QVariant::Locale) << QByteArray("QLocale");
2020     QTest::newRow("38") << int(QVariant::LineF) << QByteArray("QLineF");
2021     QTest::newRow("39") << int(QVariant::RectF) << QByteArray("QRectF");
2022     QTest::newRow("40") << int(QVariant::PointF) << QByteArray("QPointF");
2023     QTest::newRow("41") << int(QVariant::RegExp) << QByteArray("QRegExp");
2024     QTest::newRow("43") << int(QVariant::Matrix) << QByteArray("QMatrix");
2025     QTest::newRow("44") << int(QVariant::Transform) << QByteArray("QTransform");
2026     QTest::newRow("45") << int(QVariant::Hash) << QByteArray("QVariantHash");
2027     QTest::newRow("46") << int(QVariant::Matrix4x4) << QByteArray("QMatrix4x4");
2028     QTest::newRow("47") << int(QVariant::Vector2D) << QByteArray("QVector2D");
2029     QTest::newRow("48") << int(QVariant::Vector3D) << QByteArray("QVector3D");
2030     QTest::newRow("49") << int(QVariant::Vector4D) << QByteArray("QVector4D");
2031     QTest::newRow("50") << int(QVariant::Quaternion) << QByteArray("QQuaternion");
2032     QTest::newRow("51") << int(QVariant::RegularExpression) << QByteArray("QRegularExpression");
2033 }
2034
2035 void tst_QVariant::typeName()
2036 {
2037     QFETCH( int, type );
2038     QFETCH( QByteArray, res );
2039     QCOMPARE(QString::fromLatin1(QVariant::typeToName((QVariant::Type)type)),
2040             QString::fromLatin1(res.constData()));
2041 }
2042
2043 // test nameToType as well
2044 void tst_QVariant::typeToName()
2045 {
2046     QVariant v;
2047     QCOMPARE( QVariant::typeToName( v.type() ), (const char*)0 ); // Invalid
2048     // assumes that QVariant::Type contains consecutive values
2049
2050     int max = QVariant::LastGuiType;
2051     for ( int t = 1; t <= max; t++ ) {
2052         const char *n = QVariant::typeToName( (QVariant::Type)t );
2053         if (n)
2054             QCOMPARE( int(QVariant::nameToType( n )), t );
2055
2056     }
2057     QCOMPARE(QVariant::typeToName(QVariant::Int), "int");
2058     // not documented but we return 0 if the type is out of range
2059     // by testing this we catch cases where QVariant is extended
2060     // but type_map is not updated accordingly
2061     QCOMPARE( QVariant::typeToName( QVariant::Type(max+1) ), (char*)0 );
2062     // invalid type names
2063     QVERIFY( QVariant::nameToType( 0 ) == QVariant::Invalid );
2064     QVERIFY( QVariant::nameToType( "" ) == QVariant::Invalid );
2065     QVERIFY( QVariant::nameToType( "foo" ) == QVariant::Invalid );
2066
2067     QCOMPARE(QVariant::nameToType("UserType"), QVariant::Invalid);
2068
2069     // We don't support these old (Qt3) types anymore.
2070     QCOMPARE(QVariant::nameToType("QIconSet"), QVariant::Invalid);
2071     QCOMPARE(QVariant::nameToType("Q3CString"), QVariant::Invalid);
2072     QCOMPARE(QVariant::nameToType("Q_LLONG"), QVariant::Invalid);
2073     QCOMPARE(QVariant::nameToType("Q_ULLONG"), QVariant::Invalid);
2074 }
2075
2076 void tst_QVariant::streamInvalidVariant()
2077 {
2078     int writeX = 1;
2079     int writeY = 2;
2080     int readX;
2081     int readY;
2082     QVariant writeVariant;
2083     QVariant readVariant;
2084
2085     QVERIFY( writeVariant.type() == QVariant::Invalid );
2086
2087     QByteArray data;
2088     QDataStream writeStream( &data, QIODevice::WriteOnly );
2089     writeStream << writeX << writeVariant << writeY;
2090
2091     QDataStream readStream( &data, QIODevice::ReadOnly );
2092     readStream >> readX >> readVariant >> readY;
2093
2094     QVERIFY( readX == writeX );
2095     // Two invalid QVariant's aren't necessarily the same, so == will
2096     // return false if one is invalid, so check the type() instead
2097     QVERIFY( readVariant.type() == QVariant::Invalid );
2098     QVERIFY( readY == writeY );
2099 }
2100
2101 static int instanceCount = 0;
2102
2103 struct MyType
2104 {
2105     MyType(int n = 0, const char *t=0): number(n), text(t)
2106     {
2107         ++instanceCount;
2108     }
2109     MyType(const MyType &other)
2110         : number(other.number), text(other.text)
2111     {
2112         ++instanceCount;
2113     }
2114     ~MyType()
2115     {
2116         --instanceCount;
2117     }
2118     int number;
2119     const char *text;
2120 };
2121
2122 Q_DECLARE_METATYPE(MyType)
2123 Q_DECLARE_METATYPE(MyType*)
2124
2125 void tst_QVariant::userType()
2126 {
2127     {
2128         MyType data(1, "eins");
2129         MyType data2(2, "zwei");
2130
2131         {
2132             QVariant userVar;
2133             userVar.setValue(data);
2134
2135             QCOMPARE(userVar.type(), QVariant::UserType);
2136             QCOMPARE(userVar.userType(), qMetaTypeId<MyType>());
2137             QCOMPARE(userVar.typeName(), "MyType");
2138             QVERIFY(!userVar.isNull());
2139             QVERIFY(!userVar.canConvert(QVariant::String));
2140
2141             QVariant userVar2(userVar);
2142             QVERIFY(userVar == userVar2);
2143
2144             userVar2.setValue(data2);
2145             QVERIFY(userVar != userVar2);
2146
2147             const MyType *varData = static_cast<const MyType *>(userVar.constData());
2148             QVERIFY(varData);
2149             QCOMPARE(varData->number, data.number);
2150             QCOMPARE(varData->text, data.text);
2151
2152             QVariant userVar3;
2153             userVar3.setValue(data2);
2154
2155             userVar3 = userVar2;
2156             QVERIFY(userVar2 == userVar3);
2157         }
2158         // At this point all QVariants got destroyed but we have 2 MyType instances.
2159         QCOMPARE(instanceCount, 2);
2160         {
2161             QVariant userVar;
2162             userVar.setValue(&data);
2163
2164             QCOMPARE(userVar.type(), QVariant::UserType);
2165             QCOMPARE(userVar.userType(), qMetaTypeId<MyType*>());
2166             QCOMPARE(userVar.typeName(), "MyType*");
2167             QVERIFY(!userVar.isNull());
2168             QVERIFY(!userVar.canConvert(QVariant::String));
2169
2170             QVariant userVar2(userVar);
2171             QVERIFY(userVar == userVar2);
2172
2173             userVar2.setValue(&data2);
2174             QVERIFY(userVar != userVar2);
2175
2176             MyType * const*varData = reinterpret_cast<MyType *const *>(userVar.constData());
2177             QVERIFY(varData);
2178             QCOMPARE(*varData, &data);
2179
2180             QVariant userVar3;
2181             userVar3.setValue(&data2);
2182
2183             /* This check is correct now. userVar2 contains a pointer to data2 and so
2184              * does userVar3. */
2185             QVERIFY(userVar2 == userVar3);
2186
2187             userVar3 = userVar2;
2188             QVERIFY(userVar2 == userVar3);
2189         }
2190
2191         QCOMPARE(instanceCount, 2);
2192         QVariant myCarrier;
2193         myCarrier.setValue(data);
2194         QCOMPARE(instanceCount, 3);
2195         {
2196             QVariant second = myCarrier;
2197             QCOMPARE(instanceCount, 3);
2198             second.detach();
2199             QCOMPARE(instanceCount, 4);
2200         }
2201         QCOMPARE(instanceCount, 3);
2202
2203         MyType data3(0, "null");
2204         data3 = qvariant_cast<MyType>(myCarrier);
2205         QCOMPARE(data3.number, 1);
2206         QCOMPARE(data3.text, (const char *)"eins");
2207 #ifndef Q_CC_SUN
2208         QCOMPARE(instanceCount, 4);
2209 #endif
2210
2211     }
2212
2213     {
2214         const MyType data(3, "drei");
2215         QVariant myCarrier;
2216
2217         myCarrier.setValue(data);
2218         QCOMPARE(myCarrier.typeName(), "MyType");
2219
2220         const MyType data2 = qvariant_cast<MyType>(myCarrier);
2221         QCOMPARE(data2.number, 3);
2222         QCOMPARE(data2.text, (const char *)"drei");
2223     }
2224
2225     {
2226         short s = 42;
2227         QVariant myCarrier;
2228
2229         myCarrier.setValue(s);
2230         QCOMPARE((int)qvariant_cast<short>(myCarrier), 42);
2231     }
2232
2233     {
2234         qlonglong ll = Q_INT64_C(42);
2235         QVariant myCarrier;
2236
2237         myCarrier.setValue(ll);
2238         QCOMPARE(qvariant_cast<int>(myCarrier), 42);
2239     }
2240
2241     // At this point all QVariants got destroyed and MyType objects too.
2242     QCOMPARE(instanceCount, 0);
2243 }
2244
2245 struct MyTypePOD
2246 {
2247     int a;
2248     int b;
2249 };
2250 Q_DECLARE_METATYPE(MyTypePOD)
2251
2252 void tst_QVariant::podUserType()
2253 {
2254     MyTypePOD pod;
2255     pod.a = 10;
2256     pod.b = 20;
2257
2258     QVariant pod_as_variant = QVariant::fromValue(pod);
2259     MyTypePOD pod2 = qvariant_cast<MyTypePOD>(pod_as_variant);
2260
2261     QCOMPARE(pod.a, pod2.a);
2262     QCOMPARE(pod.b, pod2.b);
2263
2264     pod_as_variant.setValue(pod);
2265     pod2 = qvariant_cast<MyTypePOD>(pod_as_variant);
2266
2267     QCOMPARE(pod.a, pod2.a);
2268     QCOMPARE(pod.b, pod2.b);
2269 }
2270
2271 void tst_QVariant::basicUserType()
2272 {
2273     QVariant v;
2274     {
2275         int i = 7;
2276         v = QVariant(QMetaType::Int, &i);
2277     }
2278     QCOMPARE(v.type(), QVariant::Int);
2279     QCOMPARE(v.toInt(), 7);
2280
2281     {
2282         QString s("foo");
2283         v = QVariant(QMetaType::QString, &s);
2284     }
2285     QCOMPARE(v.type(), QVariant::String);
2286     QCOMPARE(v.toString(), QString("foo"));
2287
2288     {
2289         double d = 4.4;
2290         v = QVariant(QMetaType::Double, &d);
2291     }
2292     QCOMPARE(v.type(), QVariant::Double);
2293     QCOMPARE(v.toDouble(), 4.4);
2294
2295     {
2296         float f = 4.5f;
2297         v = QVariant(QMetaType::Float, &f);
2298     }
2299     QCOMPARE(v.userType(), int(QMetaType::Float));
2300     QCOMPARE(v.toDouble(), 4.5);
2301
2302     {
2303         QByteArray ba("bar");
2304         v = QVariant(QMetaType::QByteArray, &ba);
2305     }
2306     QCOMPARE(v.type(), QVariant::ByteArray);
2307     QCOMPARE(v.toByteArray(), QByteArray("bar"));
2308 }
2309
2310 void tst_QVariant::data()
2311 {
2312     QVariant v;
2313
2314     QVariant i = 1;
2315     QVariant d = 1.12;
2316     QVariant f = 1.12f;
2317     QVariant ll = (qlonglong)2;
2318     QVariant ull = (qulonglong)3;
2319     QVariant s(QString("hallo"));
2320     QVariant r(QRect(1,2,3,4));
2321
2322     v = i;
2323     QVERIFY(v.data());
2324     QCOMPARE(*static_cast<int *>(v.data()), i.toInt());
2325
2326     v = d;
2327     QVERIFY(v.data());
2328     QCOMPARE(*static_cast<double *>(v.data()), d.toDouble());
2329
2330     v = f;
2331     QVERIFY(v.data());
2332     QCOMPARE(*static_cast<float *>(v.data()), qvariant_cast<float>(v));
2333
2334     v = ll;
2335     QVERIFY(v.data());
2336     QCOMPARE(*static_cast<qlonglong *>(v.data()), ll.toLongLong());
2337
2338     v = ull;
2339     QVERIFY(v.data());
2340     QCOMPARE(*static_cast<qulonglong *>(v.data()), ull.toULongLong());
2341
2342     v = s;
2343     QVERIFY(v.data());
2344     QCOMPARE(*static_cast<QString *>(v.data()), s.toString());
2345
2346     v = r;
2347     QVERIFY(v.data());
2348     QCOMPARE(*static_cast<QRect *>(v.data()), r.toRect());
2349 }
2350
2351 void tst_QVariant::constData()
2352 {
2353     QVariant v;
2354
2355     int i = 1;
2356     double d = 1.12;
2357     float f = 1.12f;
2358     qlonglong ll = 2;
2359     qulonglong ull = 3;
2360     QString s("hallo");
2361     QRect r(1,2,3,4);
2362
2363     v = QVariant(i);
2364     QVERIFY(v.constData());
2365     QCOMPARE(*static_cast<const int *>(v.constData()), i);
2366
2367     v = QVariant(d);
2368     QVERIFY(v.constData());
2369     QCOMPARE(*static_cast<const double *>(v.constData()), d);
2370
2371     v = QVariant(f);
2372     QVERIFY(v.constData());
2373     QCOMPARE(*static_cast<const float *>(v.constData()), f);
2374
2375     v = QVariant(ll);
2376     QVERIFY(v.constData());
2377     QCOMPARE(*static_cast<const qlonglong *>(v.constData()), ll);
2378
2379     v = QVariant(ull);
2380     QVERIFY(v.constData());
2381     QCOMPARE(*static_cast<const qulonglong *>(v.constData()), ull);
2382
2383     v = QVariant(s);
2384     QVERIFY(v.constData());
2385     QCOMPARE(*static_cast<const QString *>(v.constData()), s);
2386
2387     v = QVariant(r);
2388     QVERIFY(v.constData());
2389     QCOMPARE(*static_cast<const QRect *>(v.constData()), r);
2390 }
2391
2392 struct Foo
2393 {
2394     Foo(): i(0) {}
2395     int i;
2396 };
2397
2398 Q_DECLARE_METATYPE(Foo)
2399
2400 void tst_QVariant::variant_to()
2401 {
2402     QVariant v1(4.2);
2403     QVariant v2(5);
2404
2405     QVariant v3;
2406     QVariant v4;
2407
2408     QStringList sl;
2409     sl << QLatin1String("blah");
2410
2411     v3.setValue(sl);
2412
2413     Foo foo;
2414     foo.i = 42;
2415
2416     v4.setValue(foo);
2417
2418     QCOMPARE(qvariant_cast<double>(v1), 4.2);
2419     QCOMPARE(qvariant_cast<float>(v1), 4.2f);
2420     QCOMPARE(qvariant_cast<int>(v2), 5);
2421     QCOMPARE(qvariant_cast<QStringList>(v3), sl);
2422     QCOMPARE(qvariant_cast<QString>(v3), QString::fromLatin1("blah"));
2423
2424     QCOMPARE(qvariant_cast<Foo>(v4).i, 42);
2425
2426     QVariant v5;
2427     QCOMPARE(qvariant_cast<Foo>(v5).i, 0);
2428
2429     QCOMPARE(qvariant_cast<int>(v1), 4);
2430
2431     QVariant n = QVariant::fromValue<short>(42);
2432     QCOMPARE(qvariant_cast<int>(n), 42);
2433     QCOMPARE(qvariant_cast<uint>(n), 42u);
2434     QCOMPARE(qvariant_cast<double>(n), 42.0);
2435     QCOMPARE(qvariant_cast<float>(n), 42.f);
2436     QCOMPARE(qvariant_cast<short>(n), short(42));
2437     QCOMPARE(qvariant_cast<ushort>(n), ushort(42));
2438
2439     n = QVariant::fromValue(43l);
2440     QCOMPARE(qvariant_cast<int>(n), 43);
2441     QCOMPARE(qvariant_cast<uint>(n), 43u);
2442     QCOMPARE(qvariant_cast<double>(n), 43.0);
2443     QCOMPARE(qvariant_cast<float>(n), 43.f);
2444     QCOMPARE(qvariant_cast<long>(n), 43l);
2445
2446     n = QLatin1String("44");
2447     QCOMPARE(qvariant_cast<int>(n), 44);
2448     QCOMPARE(qvariant_cast<ulong>(n), 44ul);
2449     QCOMPARE(qvariant_cast<float>(n), 44.0f);
2450
2451     QCOMPARE(QVariant::fromValue(0.25f).toDouble(), 0.25);
2452 }
2453
2454 struct Blah { int i; };
2455
2456 QDataStream& operator>>(QDataStream& s, Blah& c)
2457 { return (s >> c.i); }
2458
2459 QDataStream& operator<<(QDataStream& s, const Blah& c)
2460 { return (s << c.i); }
2461
2462 void tst_QVariant::saveLoadCustomTypes()
2463 {
2464     QByteArray data;
2465
2466     Blah i = { 42 };
2467     int tp = qRegisterMetaType<Blah>("Blah");
2468     QVariant v = QVariant(tp, &i);
2469
2470     qRegisterMetaTypeStreamOperators<Blah>("Blah");
2471
2472     QCOMPARE(v.userType(), tp);
2473     QVERIFY(v.type() == QVariant::UserType);
2474     {
2475         QDataStream stream(&data, QIODevice::WriteOnly);
2476         stream << v;
2477     }
2478
2479     v = QVariant();
2480
2481     {
2482         QDataStream stream(data);
2483         stream >> v;
2484     }
2485
2486     QCOMPARE(int(v.userType()), QMetaType::type("Blah"));
2487     int value = *(int*)v.constData();
2488     QCOMPARE(value, 42);
2489 }
2490
2491 void tst_QVariant::url()
2492 {
2493     QString str("http://qt.nokia.com");
2494     QUrl url(str);
2495
2496     QVariant v(url); //built with a QUrl
2497
2498     QVariant v2 = v;
2499
2500     QVariant v3(str); //built with a QString
2501
2502     QCOMPARE(v2.toUrl(), url);
2503     QVERIFY(v3.canConvert<QUrl>());
2504     QCOMPARE(v2.toUrl(), v3.toUrl());
2505
2506     QVERIFY(v2.canConvert<QString>());
2507     QCOMPARE(v2.toString(), str);
2508     QCOMPARE(v3.toString(), str);
2509 }
2510
2511 void tst_QVariant::variantMap()
2512 {
2513     QMap<QString, QVariant> map;
2514     map["test"] = 42;
2515
2516     QVariant v = map;
2517     QVariantMap map2 = qvariant_cast<QVariantMap>(v);
2518
2519     QCOMPARE(map2.value("test").toInt(), 42);
2520
2521     QVariant v2 = QVariant(QMetaType::type("QVariantMap"), &map);
2522     QCOMPARE(qvariant_cast<QVariantMap>(v2).value("test").toInt(), 42);
2523
2524     QVariant v3 = QVariant(QMetaType::type("QMap<QString, QVariant>"), &map);
2525     QCOMPARE(qvariant_cast<QVariantMap>(v3).value("test").toInt(), 42);
2526 }
2527
2528 void tst_QVariant::variantHash()
2529 {
2530     QHash<QString, QVariant> hash;
2531     hash["test"] = 42;
2532
2533     QVariant v = hash;
2534     QVariantHash hash2 = qvariant_cast<QVariantHash>(v);
2535
2536     QCOMPARE(hash2.value("test").toInt(), 42);
2537
2538     QVariant v2 = QVariant(QMetaType::type("QVariantHash"), &hash);
2539     QCOMPARE(qvariant_cast<QVariantHash>(v2).value("test").toInt(), 42);
2540
2541     QVariant v3 = QVariant(QMetaType::type("QHash<QString, QVariant>"), &hash);
2542     QCOMPARE(qvariant_cast<QVariantHash>(v3).value("test").toInt(), 42);
2543 }
2544
2545 void tst_QVariant::invalidQColor() const
2546 {
2547     QVariant va("An invalid QColor::name() value.");
2548     QVERIFY(va.canConvert(QVariant::Color));
2549
2550     QVERIFY(!va.convert(QVariant::Color));
2551
2552     QVERIFY(!qvariant_cast<QColor>(va).isValid());
2553 }
2554
2555 class CustomQObject : public QObject {
2556     Q_OBJECT
2557 public:
2558     CustomQObject(QObject *parent = 0) : QObject(parent) {}
2559 };
2560 class CustomQWidget : public QWidget {
2561     Q_OBJECT
2562 public:
2563     CustomQWidget(QWidget *parent = 0) : QWidget(parent) {}
2564 };
2565 Q_DECLARE_METATYPE(CustomQObject*)
2566 Q_DECLARE_METATYPE(CustomQWidget*)
2567
2568 class CustomNonQObject { };
2569 Q_DECLARE_METATYPE(CustomNonQObject)
2570 Q_DECLARE_METATYPE(CustomNonQObject*)
2571
2572 void tst_QVariant::cleanupTestCase()
2573 {
2574     delete customNonQObjectPointer;
2575     qDeleteAll(objectPointerTestData);
2576 }
2577
2578 void tst_QVariant::qvariant_cast_QObject_data()
2579 {
2580     QTest::addColumn<QVariant>("data");
2581     QTest::addColumn<bool>("success");
2582     QObject *obj = new QObject;
2583     obj->setObjectName(QString::fromLatin1("Hello"));
2584     QTest::newRow("from QObject") << QVariant(QMetaType::QObjectStar, &obj) << true;
2585     QTest::newRow("from QObject2") << QVariant::fromValue(obj) << true;
2586     QTest::newRow("from String") << QVariant(QLatin1String("1, 2, 3")) << false;
2587     QTest::newRow("from int") << QVariant((int) 123) << false;
2588     QWidget *widget = new QWidget;
2589     widget->setObjectName(QString::fromLatin1("Hello"));
2590     QTest::newRow("from QWidget") << QVariant::fromValue(widget) << true;
2591     CustomQObject *customObject = new CustomQObject(this);
2592     customObject->setObjectName(QString::fromLatin1("Hello"));
2593     QTest::newRow("from Derived QObject") << QVariant::fromValue(customObject) << true;
2594     CustomQWidget *customWidget = new CustomQWidget;
2595     customWidget->setObjectName(QString::fromLatin1("Hello"));
2596     QTest::newRow("from Derived QWidget") << QVariant::fromValue(customWidget) << true;
2597     QTest::newRow("from custom Object") << QVariant::fromValue(CustomNonQObject()) << false;
2598
2599     // Deleted in cleanupTestCase.
2600     customNonQObjectPointer = new CustomNonQObject;
2601     QTest::newRow("from custom ObjectStar") << QVariant::fromValue(customNonQObjectPointer) << false;
2602
2603     // Deleted in cleanupTestCase.
2604     objectPointerTestData.push_back(obj);
2605     objectPointerTestData.push_back(widget);
2606     objectPointerTestData.push_back(customObject);
2607     objectPointerTestData.push_back(customWidget);
2608 }
2609
2610 void tst_QVariant::qvariant_cast_QObject()
2611 {
2612     QFETCH(QVariant, data);
2613     QFETCH(bool, success);
2614
2615     QObject *o = qvariant_cast<QObject *>(data);
2616     QCOMPARE(o != 0, success);
2617     if (success) {
2618         QCOMPARE(o->objectName(), QString::fromLatin1("Hello"));
2619     }
2620 }
2621
2622 class CustomQObjectDerived : public CustomQObject {
2623     Q_OBJECT
2624 public:
2625     CustomQObjectDerived(QObject *parent = 0) : CustomQObject(parent) {}
2626 };
2627 Q_DECLARE_METATYPE(CustomQObjectDerived*)
2628
2629 class CustomQObjectDerivedNoMetaType : public CustomQObject {
2630     Q_OBJECT
2631 public:
2632     CustomQObjectDerivedNoMetaType(QObject *parent = 0) : CustomQObject(parent) {}
2633 };
2634
2635 void tst_QVariant::qvariant_cast_QObject_derived()
2636 {
2637     {
2638         CustomQObjectDerivedNoMetaType *object = new CustomQObjectDerivedNoMetaType(this);
2639         QVariant data = QVariant::fromValue(object);
2640         QVERIFY(data.userType() == qMetaTypeId<CustomQObjectDerivedNoMetaType*>());
2641         QCOMPARE(data.value<QObject *>(), object);
2642         QCOMPARE(data.value<CustomQObjectDerivedNoMetaType *>(), object);
2643         QCOMPARE(data.value<CustomQObject *>(), object);
2644         QVERIFY(data.value<CustomQWidget*>() == 0);
2645     }
2646     {
2647         CustomQObjectDerived *object = new CustomQObjectDerived(this);
2648         QVariant data = QVariant::fromValue(object);
2649
2650         QVERIFY(data.userType() == qMetaTypeId<CustomQObjectDerived*>());
2651
2652         QCOMPARE(data.value<QObject *>(), object);
2653         QCOMPARE(data.value<CustomQObjectDerived *>(), object);
2654         QCOMPARE(data.value<CustomQObject *>(), object);
2655         QVERIFY(data.value<CustomQWidget*>() == 0);
2656     }
2657     {
2658         CustomQWidget customWidget;
2659         QWidget *widget = &customWidget;
2660         QVariant data = QVariant::fromValue(widget);
2661         QVERIFY(data.userType() == QMetaType::QWidgetStar);
2662
2663         QCOMPARE(data.value<QObject*>(), widget);
2664         QCOMPARE(data.value<QWidget*>(), widget);
2665         QCOMPARE(data.value<CustomQWidget*>(), widget);
2666     }
2667 }
2668
2669 Q_DECLARE_METATYPE(qint8);
2670
2671 void tst_QVariant::convertToQUint8() const
2672 {
2673     /* qint8. */
2674     {
2675         const qint8 anInt = 32;
2676
2677         /* QVariant(int) gets invoked here so the QVariant has nothing with qint8 to do.
2678          * It's of type QVariant::Int. */
2679         const QVariant v0 = anInt;
2680
2681         QVERIFY(v0.canConvert<qint8>());
2682         QCOMPARE(int(qvariant_cast<qint8>(v0)), 32);
2683         QCOMPARE(int(v0.toInt()), 32);
2684         QCOMPARE(v0.toString(), QString("32"));
2685
2686         QCOMPARE(int(qvariant_cast<qlonglong>(v0)), 32);
2687         QCOMPARE(int(qvariant_cast<char>(v0)),      32);
2688         QCOMPARE(int(qvariant_cast<short>(v0)),     32);
2689         QCOMPARE(int(qvariant_cast<long>(v0)),      32);
2690         QCOMPARE(int(qvariant_cast<float>(v0)),     32);
2691         QCOMPARE(int(qvariant_cast<double>(v0)),    32);
2692     }
2693
2694     /* quint8. */
2695     {
2696         const quint8 anInt = 32;
2697         const QVariant v0 = anInt;
2698
2699         QVERIFY(v0.canConvert<quint8>());
2700         QCOMPARE(int(qvariant_cast<quint8>(v0)), 32);
2701         QCOMPARE(int(v0.toUInt()), 32);
2702         QCOMPARE(v0.toString(), QString("32"));
2703     }
2704
2705     /* qint16. */
2706     {
2707         const qint16 anInt = 32;
2708         const QVariant v0 = anInt;
2709
2710         QVERIFY(v0.canConvert<qint16>());
2711         QCOMPARE(int(qvariant_cast<qint16>(v0)), 32);
2712         QCOMPARE(int(v0.toInt()), 32);
2713         QCOMPARE(v0.toString(), QString("32"));
2714     }
2715
2716     /* quint16. */
2717     {
2718         const quint16 anInt = 32;
2719         const QVariant v0 = anInt;
2720
2721         QVERIFY(v0.canConvert<quint16>());
2722         QCOMPARE(int(qvariant_cast<quint16>(v0)), 32);
2723         QCOMPARE(int(v0.toUInt()), 32);
2724         QCOMPARE(v0.toString(), QString("32"));
2725     }
2726 }
2727
2728 void tst_QVariant::comparePointers() const
2729 {
2730     class MyClass
2731     {
2732     };
2733
2734     MyClass myClass;
2735
2736     QVariant v  = QVariant::fromValue<void *>(&myClass);
2737     QVariant v2 = QVariant::fromValue<void *>(&myClass);
2738
2739     QCOMPARE(v, v2);
2740 }
2741
2742 struct Data {};
2743 Q_DECLARE_METATYPE(Data*)
2744
2745 void tst_QVariant::voidStar() const
2746 {
2747     char c;
2748     void *p1 = &c;
2749     void *p2 = p1;
2750
2751     QVariant v1, v2;
2752     v1 = QVariant::fromValue(p1);
2753     v2 = v1;
2754     QVERIFY(v1 == v2);
2755
2756     v2 = QVariant::fromValue(p2);
2757     QVERIFY(v1 == v2);
2758
2759     p2 = 0;
2760     v2 = QVariant::fromValue(p2);
2761     QVERIFY(v1 != v2);
2762 }
2763
2764 void tst_QVariant::dataStar() const
2765 {
2766     qRegisterMetaType<Data*>();
2767     Data *p1 = new Data;
2768
2769     QVariant v1 = QVariant::fromValue(p1);
2770     QCOMPARE(v1.userType(), qMetaTypeId<Data*>());
2771     QCOMPARE(qvariant_cast<Data*>(v1), p1);
2772
2773     QVariant v2 = v1;
2774     QVERIFY(v1 == v2);
2775
2776     v2 = QVariant::fromValue(p1);
2777     QVERIFY(v1 == v2);
2778     delete p1;
2779 }
2780
2781 void tst_QVariant::canConvertQStringList() const
2782 {
2783     QFETCH(bool, canConvert);
2784     QFETCH(QStringList, input);
2785     QFETCH(QString, result);
2786
2787     QVariant v(input);
2788
2789     QCOMPARE(v.canConvert(QVariant::String), canConvert);
2790     QCOMPARE(v.toString(), result);
2791 }
2792
2793 void tst_QVariant::canConvertQStringList_data() const
2794 {
2795     QTest::addColumn<bool>("canConvert");
2796     QTest::addColumn<QStringList>("input");
2797     QTest::addColumn<QString>("result");
2798
2799     QTest::newRow("An empty list") << false << QStringList() << QString();
2800     QTest::newRow("A single item") << true << QStringList(QLatin1String("foo")) << QString::fromLatin1("foo");
2801     QTest::newRow("A single, but empty item") << true << QStringList(QString()) << QString();
2802
2803     QStringList l;
2804     l << "a" << "b";
2805
2806     QTest::newRow("Two items") << false << l << QString();
2807
2808     l << "c";
2809     QTest::newRow("Three items") << false << l << QString();
2810 }
2811
2812 template<typename T> void convertMetaType()
2813 {
2814     QVERIFY(QVariant::fromValue<T>(10).isValid());
2815     QVERIFY(QVariant::fromValue<T>(10).canConvert(QVariant::Int));
2816     QCOMPARE(QVariant::fromValue<T>(10).toInt(), 10);
2817     QCOMPARE(QVariant::fromValue<T>(10), QVariant::fromValue<T>(10));
2818 }
2819
2820 #define CONVERT_META_TYPE(Type) \
2821     convertMetaType<Type>(); \
2822     if (QTest::currentTestFailed()) \
2823         QFAIL("convertMetaType<" #Type "> failed");
2824
2825 void tst_QVariant::canConvertMetaTypeToInt() const
2826 {
2827     CONVERT_META_TYPE(long);
2828     CONVERT_META_TYPE(short);
2829     CONVERT_META_TYPE(short);
2830     CONVERT_META_TYPE(unsigned short);
2831     CONVERT_META_TYPE(ushort);
2832     CONVERT_META_TYPE(ulong);
2833     CONVERT_META_TYPE(unsigned long);
2834     CONVERT_META_TYPE(uchar);
2835     CONVERT_META_TYPE(unsigned char);
2836     CONVERT_META_TYPE(char);
2837     CONVERT_META_TYPE(uint);
2838     CONVERT_META_TYPE(unsigned int);
2839 }
2840
2841 #undef CONVERT_META_TYPE
2842
2843 /*!
2844  These calls should not produce any warnings.
2845  */
2846 void tst_QVariant::variantToDateTimeWithoutWarnings() const
2847 {
2848     {
2849         const QVariant variant(QLatin1String("An invalid QDateTime string"));
2850         const QDateTime dateTime(variant.toDateTime());
2851         QVERIFY(!dateTime.isValid());
2852     }
2853
2854     {
2855         QVariant v1(QLatin1String("xyz"));
2856         v1.convert(QVariant::DateTime);
2857
2858         QVariant v2(QLatin1String("xyz"));
2859         QDateTime dt1(v2.toDateTime());
2860
2861         const QVariant v3(QLatin1String("xyz"));
2862         const QDateTime dt2(v3.toDateTime());
2863     }
2864 }
2865
2866 void tst_QVariant::invalidDateTime() const
2867 {
2868     QVariant variant(QString::fromLatin1("Invalid date time string"));
2869     QVERIFY(!variant.toDateTime().isValid());
2870     QVERIFY(!variant.convert(QVariant::DateTime));
2871 }
2872
2873 struct MyClass
2874 {
2875     MyClass() : myValue(0) {}
2876     int myValue;
2877 };
2878
2879 Q_DECLARE_METATYPE( MyClass )
2880
2881 void tst_QVariant::loadUnknownUserType()
2882 {
2883     qRegisterMetaType<MyClass>("MyClass");
2884     QTest::ignoreMessage(QtWarningMsg, "QVariant::load: unable to load type "
2885                          + QByteArray::number(qMetaTypeId<MyClass>()) +".");
2886     char data[] = {0, 0, QMetaType::User >> 8 , char(QMetaType::User), 0, 0, 0, 0, 8, 'M', 'y', 'C', 'l', 'a', 's', 's', 0};
2887
2888     QByteArray ba(data, sizeof(data));
2889     QDataStream ds(&ba, QIODevice::ReadOnly);
2890     QVariant var;
2891     var.load(ds);
2892     QCOMPARE(ds.status(), QDataStream::ReadCorruptData);
2893 }
2894
2895 void tst_QVariant::loadBrokenUserType()
2896 {
2897     QTest::ignoreMessage(QtWarningMsg, "Trying to construct an instance of an invalid type, type id: 127");
2898     char data[] = {0, 0, 0, 127, 0 };
2899
2900     QByteArray ba(data, sizeof(data));
2901     QDataStream ds(&ba, QIODevice::ReadOnly);
2902     QVariant var;
2903     var.load(ds);
2904     QCOMPARE(ds.status(), QDataStream::Ok);
2905 }
2906
2907 void tst_QVariant::invalidDate() const
2908 {
2909     QString foo("Hello");
2910     QVariant variant(foo);
2911     QVERIFY(!variant.convert(QVariant::Date));
2912
2913     variant = foo;
2914     QVERIFY(!variant.convert(QVariant::DateTime));
2915
2916     variant = foo;
2917     QVERIFY(!variant.convert(QVariant::Time));
2918
2919     variant = foo;
2920     QVERIFY(!variant.convert(QVariant::Int));
2921
2922     variant = foo;
2923     QVERIFY(!variant.convert(QVariant::Double));
2924
2925     variant = foo;
2926     QVERIFY(!variant.convert(QVariant::Type(QMetaType::Float)));
2927 }
2928
2929 struct WontCompare
2930 {
2931     int x,y,z,q,w,e,r,t;
2932 };
2933 Q_DECLARE_METATYPE(WontCompare);
2934
2935 void tst_QVariant::compareCustomTypes() const
2936 {
2937     qRegisterMetaType<WontCompare>("WontCompare");
2938
2939     WontCompare f1;
2940     f1.x = 0;
2941     const QVariant variant1(QVariant::fromValue(f1));
2942
2943     WontCompare f2;
2944     f2.x = 0;
2945     const QVariant variant2(QVariant::fromValue(f2));
2946
2947     /* We compare pointers. */
2948     QVERIFY(variant1 != variant2);
2949     QVERIFY(variant1 == variant1);
2950     QVERIFY(variant2 == variant2);
2951 }
2952
2953 void tst_QVariant::timeToDateTime() const
2954 {
2955     const QVariant val(QTime::currentTime());
2956     QVERIFY(!val.canConvert(QVariant::DateTime));
2957     QVERIFY(!val.toDateTime().isValid());
2958 }
2959
2960 Q_DECLARE_METATYPE(QHostAddress)
2961
2962 void tst_QVariant::copyingUserTypes() const
2963 {
2964     QVariant var;
2965     QVariant var3;
2966     const QHostAddress ha("127.0.0.1");
2967     var.setValue(ha);
2968     var3 = var;
2969
2970     QCOMPARE(qvariant_cast<QHostAddress>(var3), ha);
2971 }
2972
2973 void tst_QVariant::convertBoolToByteArray() const
2974 {
2975     QFETCH(QByteArray, input);
2976     QFETCH(bool, canConvert);
2977     QFETCH(bool, value);
2978
2979     const QVariant variant(input);
2980
2981     QCOMPARE(variant.canConvert<bool>(), canConvert);
2982
2983     if(canConvert) {
2984         /* Just call this function so we run the code path. */
2985         QCOMPARE(variant.toBool(), value);
2986     }
2987 }
2988
2989 void tst_QVariant::convertBoolToByteArray_data() const
2990 {
2991     QTest::addColumn<QByteArray>("input");
2992     QTest::addColumn<bool>("canConvert");
2993     QTest::addColumn<bool>("value");
2994
2995     QTest::newRow("false")
2996         << QByteArray("false")
2997         << true
2998         << false;
2999
3000     QTest::newRow("FALSE")
3001         << QByteArray("FALSE")
3002         << true
3003         << false;
3004
3005     QTest::newRow("falSE")
3006         << QByteArray("FALSE")
3007         << true
3008         << false;
3009
3010     QTest::newRow("")
3011         << QByteArray("")
3012         << true
3013         << false;
3014
3015     QTest::newRow("null QByteArray")
3016         << QByteArray()
3017         << true
3018         << false;
3019
3020     QTest::newRow("any-content")
3021         << QByteArray("any-content")
3022         << true
3023         << true;
3024
3025     QTest::newRow("true")
3026         << QByteArray("true")
3027         << true
3028         << true;
3029
3030     QTest::newRow("TRUE")
3031         << QByteArray("TRUE")
3032         << true
3033         << true;
3034
3035     QTest::newRow("trUE")
3036         << QByteArray("trUE")
3037         << true
3038         << true;
3039 }
3040
3041 void tst_QVariant::convertByteArrayToBool() const
3042 {
3043     QFETCH(bool, input);
3044     QFETCH(QByteArray, output);
3045
3046     const QVariant variant(input);
3047     QCOMPARE(variant.type(), QVariant::Bool);
3048     QCOMPARE(variant.toBool(), input);
3049     QVERIFY(variant.canConvert<bool>());
3050
3051     QCOMPARE(variant.toByteArray(), output);
3052 }
3053
3054 void tst_QVariant::convertByteArrayToBool_data() const
3055 {
3056     QTest::addColumn<bool>("input");
3057     QTest::addColumn<QByteArray>("output");
3058
3059     QTest::newRow("false")
3060         << false
3061         << QByteArray("false");
3062
3063     QTest::newRow("true")
3064         << true
3065         << QByteArray("true");
3066 }
3067
3068 /*!
3069   We verify that:
3070     1. Converting the string "9.9" to int fails. This is the behavior of
3071        toLongLong() and hence also QVariant, since it uses it.
3072     2. Converting the QVariant containing the double 9.9 to int works.
3073
3074   Rationale: "9.9" is not a valid int. However, doubles are by definition not
3075   ints and therefore it makes more sense to perform conversion for those.
3076 */
3077 void tst_QVariant::toIntFromQString() const
3078 {
3079     QVariant first("9.9");
3080     bool ok;
3081     QCOMPARE(first.toInt(&ok), 0);
3082     QVERIFY(!ok);
3083
3084     QCOMPARE(QString("9.9").toLongLong(&ok), qlonglong(0));
3085     QVERIFY(!ok);
3086
3087     QVariant v(9.9);
3088     QCOMPARE(v.toInt(&ok), 10);
3089     QVERIFY(ok);
3090 }
3091
3092 /*!
3093   We verify that:
3094     1. Conversion from (64 bit) double to int works (no overflow).
3095     2. Same conversion works for QVariant::convert.
3096
3097   Rationale: if 2147483630 is set in float and then converted to int,
3098   there will be overflow and the result will be -2147483648.
3099 */
3100 void tst_QVariant::toIntFromDouble() const
3101 {
3102     double d = 2147483630;  // max int 2147483647
3103     QVERIFY((int)d == 2147483630);
3104
3105     QVariant var(d);
3106     QVERIFY( var.canConvert( QVariant::Int ) );
3107
3108     bool ok;
3109     int result = var.toInt(&ok);
3110
3111     QVERIFY( ok == true );
3112     QCOMPARE(result, 2147483630);
3113 }
3114
3115 void tst_QVariant::setValue()
3116 {
3117     QTransform t; //we just take a value so that we're sure that it will be shared
3118     QVariant v1 = t;
3119     QVERIFY( v1.isDetached() );
3120     QVariant v2 = v1;
3121     QVERIFY( !v1.isDetached() );
3122     QVERIFY( !v2.isDetached() );
3123
3124     v2.setValue(3); //set an integer value
3125
3126     QVERIFY( v1.isDetached() );
3127     QVERIFY( v2.isDetached() );
3128 }
3129
3130 void tst_QVariant::numericalConvert()
3131 {
3132 #if defined(Q_OS_LINUX) && defined(Q_CC_GNU) && !defined(__x86_64__)
3133     QSKIP("Known to fail due to a GCC bug on at least Ubuntu 10.04 32-bit - check QTBUG-8959");
3134 #endif
3135     QVariant vfloat(float(5.3));
3136     QVariant vdouble(double(5.3));
3137     QVariant vreal(qreal(5.3));
3138     QVariant vint(int(5));
3139     QVariant vuint(uint(5));
3140     QVariant vshort(short(5));
3141     QVariant vlonglong(quint64(5));
3142     QVariant vstringint(QString::fromLatin1("5"));
3143     QVariant vstring(QString::fromLatin1("5.3"));
3144
3145     QVector<QVariant *> vect;
3146     vect << &vfloat << &vdouble << &vreal << &vint << &vuint << &vshort<< &vlonglong << &vstringint << &vstring;
3147
3148     for(int i = 0; i < vect.size(); i++) {
3149         double num = 5.3;
3150         if (i >= 3 && i <= 7)
3151             num = 5;
3152         QVariant *v = vect.at(i);
3153         QCOMPARE(v->toFloat() , float(num));
3154         QCOMPARE(float(v->toReal()) , float(num));
3155         QCOMPARE(float(v->toDouble()) , float(num));
3156         if(i != 8) {
3157             QCOMPARE(v->toInt() , int(num));
3158             QCOMPARE(v->toUInt() , uint(num));
3159             QCOMPARE(v->toULongLong() , quint64(num));
3160         }
3161         QCOMPARE(v->toString() , QString::number(num));
3162     }
3163 }
3164
3165
3166 template<class T> void playWithVariant(const T &orig, bool isNull, const QString &toString, double toDouble, bool toBool)
3167 {
3168     QVariant v = QVariant::fromValue(orig);
3169     QVERIFY(v.isValid());
3170     QCOMPARE(v.isNull(), isNull);
3171     QCOMPARE(v.toString(), toString);
3172     QCOMPARE(v.toDouble(), toDouble);
3173     QCOMPARE(v.toBool(), toBool);
3174     QCOMPARE(qvariant_cast<T>(v), orig);
3175
3176     {
3177         QVariant v2 = v;
3178         if (!(QTypeInfo<T>::isStatic && QTypeInfo<T>::isComplex)) {
3179             // Type is movable so standard comparison algorithm in QVariant should work
3180             // In a custom type QVariant is not aware of ==operator so it won't be called,
3181             // which may cause problems especially visible when using a not-movable type
3182             QCOMPARE(v2, v);
3183         }
3184         QVERIFY(v2.isValid());
3185         QCOMPARE(v2.isNull(), isNull);
3186         QCOMPARE(v2.toString(), toString);
3187         QCOMPARE(v2.toDouble(), toDouble);
3188         QCOMPARE(v2.toBool(), toBool);
3189         QCOMPARE(qvariant_cast<T>(v2), orig);
3190
3191         QVariant v3;
3192         v = QVariant();
3193         QCOMPARE(v3, v);
3194         v = v2;
3195         if (!(QTypeInfo<T>::isStatic && QTypeInfo<T>::isComplex)) {
3196             // Type is movable so standard comparison algorithm in QVariant should work
3197             // In a custom type QVariant is not aware of ==operator so it won't be called,
3198             // which may cause problems especially visible when using a not-movable type
3199             QCOMPARE(v2, v);
3200         }
3201         QCOMPARE(qvariant_cast<T>(v2), qvariant_cast<T>(v));
3202         QCOMPARE(v2.toString(), toString);
3203         v3 = QVariant::fromValue(orig);
3204
3205         QVERIFY(v3.isValid());
3206         QCOMPARE(v3.isNull(), isNull);
3207         QCOMPARE(v3.toString(), toString);
3208         QCOMPARE(v3.toDouble(), toDouble);
3209         QCOMPARE(v3.toBool(), toBool);
3210         QCOMPARE(qvariant_cast<T>(v3), qvariant_cast<T>(v));
3211     }
3212
3213     QVERIFY(v.isValid());
3214     QCOMPARE(v.isNull(), isNull);
3215     QCOMPARE(v.toString(), toString);
3216     QCOMPARE(v.toDouble(), toDouble);
3217     QCOMPARE(v.toBool(), toBool);
3218     QCOMPARE(qvariant_cast<T>(v), orig);
3219
3220     if (qMetaTypeId<T>() != qMetaTypeId<QVariant>()) {
3221         QCOMPARE(v.userType(), qMetaTypeId<T>());
3222         QCOMPARE(QVariant::typeToName(QVariant::Type(v.userType())), QMetaType::typeName(qMetaTypeId<T>()));
3223     }
3224 }
3225
3226 #define PLAY_WITH_VARIANT(Orig, IsNull, ToString, ToDouble, ToBool) \
3227     playWithVariant(Orig, IsNull, ToString, ToDouble, ToBool);\
3228     if (QTest::currentTestFailed())\
3229         QFAIL("playWithVariant failed");
3230
3231 struct MyPrimitive
3232 {
3233     char x, y;
3234     bool operator==(const MyPrimitive &o) const
3235     {
3236         return x == o.x && y == o.y;
3237     }
3238 };
3239
3240 QT_BEGIN_NAMESPACE
3241 Q_DECLARE_TYPEINFO(MyPrimitive, Q_PRIMITIVE_TYPE);
3242 QT_END_NAMESPACE
3243
3244 struct MyData
3245 {
3246     void *ptr;
3247     MyData() : ptr(this) {}
3248     ~MyData()
3249     {
3250         if (ptr != this) qWarning("%s: object has moved", Q_FUNC_INFO);
3251     }
3252     MyData(const MyData& o) : ptr(this)
3253     {
3254         if (o.ptr != &o) qWarning("%s: other object has moved", Q_FUNC_INFO);
3255     }
3256     MyData &operator=(const MyData &o)
3257     {
3258         if (ptr != this) qWarning("%s: object has moved", Q_FUNC_INFO);
3259         if (o.ptr != &o) qWarning("%s: other object has moved", Q_FUNC_INFO);
3260         return *this;
3261     }
3262     bool operator==(const MyData &o) const
3263     {
3264         if (ptr != this) qWarning("%s: object has moved", Q_FUNC_INFO);
3265         if (o.ptr != &o) qWarning("%s: other object has moved", Q_FUNC_INFO);
3266         return true;
3267     }
3268 };
3269
3270 struct MyMovable
3271 {
3272     static int count;
3273     int v;
3274     MyMovable() { v = count++; }
3275     ~MyMovable() { count--; }
3276     MyMovable(const MyMovable &o) : v(o.v) { count++; }
3277
3278     bool operator==(const MyMovable &o) const
3279     {
3280         return v == o.v;
3281     }
3282 };
3283
3284 int MyMovable::count  = 0;
3285
3286 struct MyNotMovable
3287 {
3288     static int count;
3289     MyNotMovable *that;
3290     MyNotMovable() : that(this) { count++; }
3291     ~MyNotMovable() { QCOMPARE(that, this);  count--; }
3292     MyNotMovable(const MyNotMovable &o) : that(this) { QCOMPARE(o.that, &o); count++; }
3293     MyNotMovable &operator=(const MyNotMovable &o) {
3294         bool ok = that == this && o.that == &o;
3295         if (!ok) qFatal("MyNotMovable has been moved");
3296         return *this;
3297     }
3298
3299     //PLAY_WITH_VARIANT test that they are equal, but never that they are not equal
3300     // so it would be fine just to always return true
3301     bool operator==(const MyNotMovable &o) const
3302     {
3303         bool ok = that == this && o.that == &o;
3304         if (!ok) qFatal("MyNotMovable has been moved");
3305         return ok;
3306     }
3307 };
3308
3309 int MyNotMovable::count  = 0;
3310
3311 struct MyShared : QSharedData {
3312     MyMovable movable;
3313 };
3314
3315 QT_BEGIN_NAMESPACE
3316 Q_DECLARE_TYPEINFO(MyMovable, Q_MOVABLE_TYPE);
3317 QT_END_NAMESPACE
3318
3319 Q_DECLARE_METATYPE(QList<QSize>)
3320 Q_DECLARE_METATYPE(MyPrimitive)
3321 Q_DECLARE_METATYPE(MyData)
3322 Q_DECLARE_METATYPE(MyMovable)
3323 Q_DECLARE_METATYPE(MyNotMovable)
3324 Q_DECLARE_METATYPE(QList<MyPrimitive>)
3325 Q_DECLARE_METATYPE(QList<MyData>)
3326 Q_DECLARE_METATYPE(QList<MyMovable>)
3327 Q_DECLARE_METATYPE(QList<MyNotMovable>)
3328 Q_DECLARE_METATYPE(MyPrimitive *)
3329 Q_DECLARE_METATYPE(MyData *)
3330 Q_DECLARE_METATYPE(MyMovable *)
3331 Q_DECLARE_METATYPE(MyNotMovable *)
3332 Q_DECLARE_METATYPE(QSharedDataPointer<MyShared>)
3333
3334
3335 void tst_QVariant::moreCustomTypes()
3336 {
3337     {
3338         QList<QSize> listSize;
3339         PLAY_WITH_VARIANT(listSize, false, QString(), 0, false);
3340         listSize << QSize(4,5) << QSize(89,23) << QSize(5,6);
3341         PLAY_WITH_VARIANT(listSize, false, QString(), 0, false);
3342     }
3343
3344     {
3345         QString str;
3346         PLAY_WITH_VARIANT(str, true, QString(), 0, false);
3347         str = QString::fromLatin1("123456789.123");
3348         PLAY_WITH_VARIANT(str, false, str, 123456789.123, true);
3349     }
3350
3351     {
3352         QSize size;
3353         PLAY_WITH_VARIANT(size, false, QString(), 0, false);
3354         PLAY_WITH_VARIANT(QSize(45,78), false, QString(), 0, false);
3355     }
3356
3357     {
3358         MyData d;
3359         PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3360         PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3361         QList<MyData> l;
3362         PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3363         l << MyData() << MyData();
3364         PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3365     }
3366
3367     {
3368         MyPrimitive d = { 4, 5 };
3369         PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3370         PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3371         QList<MyPrimitive> l;
3372         PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3373         l << d;
3374         PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3375     }
3376
3377     {
3378         MyMovable d;
3379         PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3380         PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3381         QList<MyMovable> l;
3382         PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3383         l << MyMovable() << d;
3384         PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3385     }
3386     QCOMPARE(MyMovable::count, 0);
3387
3388     QCOMPARE(MyNotMovable::count, 0);
3389     {
3390         MyNotMovable d;
3391         PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3392         PLAY_WITH_VARIANT(&d, false, QString(), 0, false);
3393         QList<MyNotMovable> l;
3394         PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3395         l << MyNotMovable() << d;
3396         PLAY_WITH_VARIANT(l, false, QString(), 0, false);
3397     }
3398     QCOMPARE(MyNotMovable::count, 0);
3399
3400     {
3401         PLAY_WITH_VARIANT(12.12, false, "12.12", 12.12, true);
3402         PLAY_WITH_VARIANT(12.12f, false, "12.12", 12.12f, true);
3403         PLAY_WITH_VARIANT('a', false, "a", 'a', true);
3404         PLAY_WITH_VARIANT((unsigned char)('a'), false, "a", 'a', true);
3405         PLAY_WITH_VARIANT( quint8(12), false, "\xc", 12, true);
3406         PLAY_WITH_VARIANT(  qint8(13), false, "\xd", 13, true);
3407         PLAY_WITH_VARIANT(quint16(14), false, "14", 14, true);
3408         PLAY_WITH_VARIANT( qint16(15), false, "15", 15, true);
3409         PLAY_WITH_VARIANT(quint32(16), false, "16", 16, true);
3410         PLAY_WITH_VARIANT( qint32(17), false, "17", 17, true);
3411         PLAY_WITH_VARIANT(quint64(18), false, "18", 18, true);
3412         PLAY_WITH_VARIANT( qint64(19), false, "19", 19, true);
3413         PLAY_WITH_VARIANT(  qint8(-12), false, QLatin1String("\xf4"), -12, true); // qint8 is char, so it gets converted via QChar
3414         PLAY_WITH_VARIANT( qint16(-13), false, "-13", -13, true);
3415         PLAY_WITH_VARIANT( qint32(-14), false, "-14", -14, true);
3416         PLAY_WITH_VARIANT( qint64(-15), false, "-15", -15, true);
3417         PLAY_WITH_VARIANT(quint64(0), false, "0", 0, false);
3418         PLAY_WITH_VARIANT( true, false, "true", 1, true);
3419         PLAY_WITH_VARIANT( false, false, "false", 0, false);
3420
3421         PLAY_WITH_VARIANT(QString("hello\n"), false, "hello\n", 0, true);
3422     }
3423
3424     {
3425         int i = 5;
3426         PLAY_WITH_VARIANT((void *)(&i), false, QString(), 0, false);
3427         PLAY_WITH_VARIANT((void *)(0), false, QString(), 0, false);
3428     }
3429
3430     {
3431         QVariant v1 = QVariant::fromValue(5);
3432         QVariant v2 = QVariant::fromValue(5.0);
3433         QVariant v3 = QVariant::fromValue(quint16(5));
3434         QVariant v4 = 5;
3435         QVariant v5 = QVariant::fromValue(MyPrimitive());
3436         QVariant v6 = QVariant::fromValue(MyMovable());
3437         QVariant v7 = QVariant::fromValue(MyData());
3438         PLAY_WITH_VARIANT(v1, false, "5", 5, true);
3439         PLAY_WITH_VARIANT(v2, false, "5", 5, true);
3440         PLAY_WITH_VARIANT(v3, false, "5", 5, true);
3441         PLAY_WITH_VARIANT(v4, false, "5", 5, true);
3442
3443         PLAY_WITH_VARIANT(v5, false, QString(), 0, false);
3444     }
3445
3446     QCOMPARE(MyMovable::count, 0);
3447     {
3448         QSharedDataPointer<MyShared> d(new MyShared);
3449         PLAY_WITH_VARIANT(d, false, QString(), 0, false);
3450     }
3451     QCOMPARE(MyMovable::count, 0);
3452
3453     {
3454         QList<QList<int> > data;
3455         PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3456         data << (QList<int>() << 42);
3457         PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3458     }
3459
3460     {
3461         QList<QVector<int> > data;
3462         PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3463         data << (QVector<int>() << 42);
3464         PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3465     }
3466
3467     {
3468         QList<QSet<int> > data;
3469         PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3470         data << (QSet<int>() << 42);
3471         PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3472     }
3473
3474     {
3475         QList<QLinkedList<int> > data;
3476         PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3477         data << (QLinkedList<int>() << 42);
3478         PLAY_WITH_VARIANT(data, false, QString(), 0, false);
3479     }
3480 }
3481
3482 void tst_QVariant::movabilityTest()
3483 {
3484     // This test checks if QVariant is movable even if an internal data is not movable.
3485     QVERIFY(!MyNotMovable::count);
3486     {
3487         QVariant variant = QVariant::fromValue(MyNotMovable());
3488         QVERIFY(MyNotMovable::count);
3489
3490         // prepare destination memory space to which variant will be moved
3491         QVariant buffer[1];
3492         QCOMPARE(buffer[0].type(), QVariant::Invalid);
3493         buffer[0].~QVariant();
3494
3495         memcpy(buffer, &variant, sizeof(QVariant));
3496         QCOMPARE(buffer[0].type(), QVariant::UserType);
3497         QCOMPARE(buffer[0].userType(), qMetaTypeId<MyNotMovable>());
3498         MyNotMovable tmp(buffer[0].value<MyNotMovable>());
3499
3500         new (&variant) QVariant();
3501     }
3502     QVERIFY(!MyNotMovable::count);
3503 }
3504
3505 void tst_QVariant::variantInVariant()
3506 {
3507     QVariant var1 = 5;
3508     QCOMPARE(var1.type(), QVariant::Int);
3509     QVariant var2 = var1;
3510     QCOMPARE(var2, var1);
3511     QCOMPARE(var2.type(), QVariant::Int);
3512     QVariant var3 = QVariant::fromValue(var1);
3513     QCOMPARE(var3, var1);
3514     QCOMPARE(var3.type(), QVariant::Int);
3515     QVariant var4 = qvariant_cast<QVariant>(var1);
3516     QCOMPARE(var4, var1);
3517     QCOMPARE(var4.type(), QVariant::Int);
3518     QVariant var5;
3519     var5 = var1;
3520     QCOMPARE(var5, var1);
3521     QCOMPARE(var5.type(), QVariant::Int);
3522     QVariant var6;
3523     var6.setValue(var1);
3524     QCOMPARE(var6, var1);
3525     QCOMPARE(var6.type(), QVariant::Int);
3526
3527     QCOMPARE(QVariant::fromValue(var1), QVariant::fromValue(var2));
3528     QCOMPARE(qvariant_cast<QVariant>(var3), QVariant::fromValue(var4));
3529     QCOMPARE(qvariant_cast<QVariant>(var5), qvariant_cast<QVariant>(var6));
3530
3531     QString str("hello");
3532     QVariant var8 = qvariant_cast<QVariant>(QVariant::fromValue(QVariant::fromValue(str)));
3533     QCOMPARE((int)var8.type(), (int)QVariant::String);
3534     QCOMPARE(qvariant_cast<QString>(QVariant(qvariant_cast<QVariant>(var8))), str);
3535
3536     QVariant var9(qMetaTypeId<QVariant>(), &var1);
3537     QCOMPARE(var9.userType(), qMetaTypeId<QVariant>());
3538     QCOMPARE(qvariant_cast<QVariant>(var9), var1);
3539 }
3540
3541 void tst_QVariant::colorInteger()
3542 {
3543     QVariant v = QColor(Qt::red);
3544     QCOMPARE(v.type(), QVariant::Color);
3545     QCOMPARE(v.value<QColor>(), QColor(Qt::red));
3546
3547     v.setValue(1000);
3548     QCOMPARE(v.type(), QVariant::Int);
3549     QCOMPARE(v.toInt(), 1000);
3550
3551     v.setValue(QColor(Qt::yellow));
3552     QCOMPARE(v.type(), QVariant::Color);
3553     QCOMPARE(v.value<QColor>(), QColor(Qt::yellow));
3554 }
3555
3556 class Forward;
3557 Q_DECLARE_OPAQUE_POINTER(Forward*)
3558 Q_DECLARE_METATYPE(Forward*)
3559
3560 void tst_QVariant::forwardDeclare()
3561 {
3562     Forward *f = 0;
3563     QVariant v = QVariant::fromValue(f);
3564     QCOMPARE(qvariant_cast<Forward*>(v), f);
3565 }
3566
3567 void tst_QVariant::loadQt5Stream_data()
3568 {
3569     dataStream_data(QDataStream::Qt_5_0);
3570 }
3571
3572 void tst_QVariant::loadQt5Stream()
3573 {
3574     loadQVariantFromDataStream(QDataStream::Qt_5_0);
3575 }
3576
3577 void tst_QVariant::saveQt5Stream_data()
3578 {
3579     dataStream_data(QDataStream::Qt_5_0);
3580 }
3581
3582 void tst_QVariant::saveQt5Stream()
3583 {
3584     saveQVariantFromDataStream(QDataStream::Qt_5_0);
3585 }
3586
3587 void tst_QVariant::loadQt4Stream_data()
3588 {
3589     dataStream_data(QDataStream::Qt_4_9);
3590 }
3591
3592 void tst_QVariant::loadQt4Stream()
3593 {
3594     loadQVariantFromDataStream(QDataStream::Qt_4_9);
3595 }
3596
3597 void tst_QVariant::saveQt4Stream_data()
3598 {
3599     dataStream_data(QDataStream::Qt_4_9);
3600 }
3601
3602 void tst_QVariant::saveQt4Stream()
3603 {
3604     saveQVariantFromDataStream(QDataStream::Qt_4_9);
3605 }
3606
3607 void tst_QVariant::dataStream_data(QDataStream::Version version)
3608 {
3609     QTest::addColumn<QString>("fileName");
3610
3611     QString path;
3612     switch (version) {
3613     case QDataStream::Qt_4_9:
3614         path = QString::fromLatin1("qt4.9");
3615         break;
3616     case QDataStream::Qt_5_0:
3617         path = QString::fromLatin1("qt5.0");
3618         break;
3619     default:
3620         Q_UNIMPLEMENTED();
3621     }
3622
3623     path = path.prepend(":/stream/").append("/");
3624     QDir dir(path);
3625     uint i = 0;
3626     foreach (const QFileInfo &fileInfo, dir.entryInfoList(QStringList() << "*.bin")) {
3627         QTest::newRow((path + fileInfo.fileName()).toLatin1()) << fileInfo.filePath();
3628         i += 1;
3629     }
3630     QVERIFY(i > 10);
3631 }
3632
3633 void tst_QVariant::loadQVariantFromDataStream(QDataStream::Version version)
3634 {
3635     QFETCH(QString, fileName);
3636
3637     QFile file(fileName);
3638     QVERIFY(file.open(QIODevice::ReadOnly));
3639
3640     QDataStream stream(&file);
3641     stream.setVersion(version);
3642
3643     QString typeName;
3644     QVariant loadedVariant;
3645     stream >> typeName >> loadedVariant;
3646
3647     const int id = QMetaType::type(typeName.toLatin1());
3648     if (id == QMetaType::Void) {
3649         // Void type is not supported by QVariant
3650         return;
3651     }
3652
3653     QVariant constructedVariant(static_cast<QVariant::Type>(id));
3654     QCOMPARE(constructedVariant.userType(), id);
3655     QCOMPARE(QMetaType::typeName(loadedVariant.userType()), typeName.toLatin1().constData());
3656     QCOMPARE(loadedVariant.userType(), constructedVariant.userType());
3657 }
3658
3659 void tst_QVariant::saveQVariantFromDataStream(QDataStream::Version version)
3660 {
3661     QFETCH(QString, fileName);
3662
3663     QFile file(fileName);
3664     QVERIFY(file.open(QIODevice::ReadOnly));
3665     QDataStream dataFileStream(&file);
3666
3667     QString typeName;
3668     dataFileStream >> typeName;
3669     QByteArray data = file.readAll();
3670     const int id = QMetaType::type(typeName.toLatin1());
3671     if (id == QMetaType::Void) {
3672         // Void type is not supported by QVariant
3673         return;
3674     }
3675
3676     QBuffer buffer;
3677     buffer.open(QIODevice::ReadWrite);
3678     QDataStream stream(&buffer);
3679     stream.setVersion(version);
3680
3681     QVariant constructedVariant(static_cast<QVariant::Type>(id));
3682     QCOMPARE(constructedVariant.userType(), id);
3683     stream << constructedVariant;
3684
3685     // We are testing QVariant there is no point in testing full array.
3686     QCOMPARE(buffer.data().left(5), data.left(5));
3687
3688     buffer.seek(0);
3689     QVariant recunstructedVariant;
3690     stream >> recunstructedVariant;
3691     QCOMPARE(recunstructedVariant.userType(), constructedVariant.userType());
3692 }
3693
3694 class MessageHandler {
3695 public:
3696     MessageHandler(const int typeId, QtMsgHandler msgHandler = handler)
3697         : oldMsgHandler(qInstallMsgHandler(msgHandler))
3698     {
3699         currentId = typeId;
3700     }
3701
3702     ~MessageHandler()
3703     {
3704         qInstallMsgHandler(oldMsgHandler);
3705     }
3706
3707     bool testPassed() const
3708     {
3709         return ok;
3710     }
3711 protected:
3712     static void handler(QtMsgType, const char *txt)
3713     {
3714         QString msg = QString::fromLatin1(txt);
3715         // Format itself is not important, but basic data as a type name should be included in the output
3716         ok = msg.startsWith("QVariant(");
3717         QVERIFY2(ok, (QString::fromLatin1("Message is not started correctly: '") + msg + '\'').toLatin1().constData());
3718         ok &= (currentId == QMetaType::UnknownType
3719              ? msg.contains("Invalid")
3720              : msg.contains(QMetaType::typeName(currentId)));
3721         QVERIFY2(ok, (QString::fromLatin1("Message doesn't contain type name: '") + msg + '\'').toLatin1().constData());
3722         if (currentId == QMetaType::Char || currentId == QMetaType::QChar) {
3723             // Chars insert '\0' into the qdebug stream, it is not possible to find a real string length
3724             return;
3725         }
3726         if (QMetaType::typeFlags(currentId) & QMetaType::PointerToQObject) {
3727             QByteArray currentName = QMetaType::typeName(currentId);
3728             currentName.chop(1);
3729             ok &= (msg.contains(", " + currentName) || msg.contains(", 0x0"));
3730         }
3731         ok &= msg.endsWith(") ");
3732         QVERIFY2(ok, (QString::fromLatin1("Message is not correctly finished: '") + msg + '\'').toLatin1().constData());
3733
3734     }
3735
3736     QtMsgHandler oldMsgHandler;
3737     static int currentId;
3738     static bool ok;
3739 };
3740 bool MessageHandler::ok;
3741 int MessageHandler::currentId;
3742
3743 void tst_QVariant::debugStream_data()
3744 {
3745     QTest::addColumn<QVariant>("variant");
3746     QTest::addColumn<int>("typeId");
3747     for (int id = 0; id < QMetaType::User; ++id) {
3748         const char *tagName = QMetaType::typeName(id);
3749         if (!tagName)
3750             continue;
3751         if (id != QMetaType::Void) {
3752             QTest::newRow(tagName) << QVariant(static_cast<QVariant::Type>(id)) << id;
3753         }
3754     }
3755     QTest::newRow("QBitArray(111)") << QVariant(QBitArray(3, true)) << qMetaTypeId<QBitArray>();
3756     QTest::newRow("CustomStreamableClass") << QVariant(qMetaTypeId<CustomStreamableClass>(), 0) << qMetaTypeId<CustomStreamableClass>();
3757     QTest::newRow("MyClass") << QVariant(qMetaTypeId<MyClass>(), 0) << qMetaTypeId<MyClass>();
3758     QTest::newRow("InvalidVariant") << QVariant() << int(QMetaType::UnknownType);
3759     QTest::newRow("CustomQObject") << QVariant::fromValue(this) << qMetaTypeId<tst_QVariant*>();
3760 }
3761
3762 void tst_QVariant::debugStream()
3763 {
3764     QFETCH(QVariant, variant);
3765     QFETCH(int, typeId);
3766
3767     MessageHandler msgHandler(typeId);
3768     qDebug() << variant;
3769     QVERIFY(msgHandler.testPassed());
3770 }
3771
3772 struct MessageHandlerType : public MessageHandler
3773 {
3774     MessageHandlerType(const int typeId)
3775         : MessageHandler(typeId, handler)
3776     {}
3777     static void handler(QtMsgType, const char *txt)
3778     {
3779         QString msg = QString::fromLatin1(txt);
3780         // Format itself is not important, but basic data as a type name should be included in the output
3781         ok = msg.startsWith("QVariant::");
3782         QVERIFY2(ok, (QString::fromLatin1("Message is not started correctly: '") + msg + '\'').toLatin1().constData());
3783         ok &= (currentId == QMetaType::UnknownType
3784                 ? msg.contains("Invalid")
3785                 : msg.contains(QMetaType::typeName(currentId)));
3786         QVERIFY2(ok, (QString::fromLatin1("Message doesn't contain type name: '") + msg + '\'').toLatin1().constData());
3787     }
3788 };
3789
3790 void tst_QVariant::debugStreamType_data()
3791 {
3792     debugStream_data();
3793 }
3794
3795 void tst_QVariant::debugStreamType()
3796 {
3797     QFETCH(QVariant, variant);
3798     QFETCH(int, typeId);
3799
3800     MessageHandlerType msgHandler(typeId);
3801     qDebug() << QVariant::Type(typeId);
3802     QVERIFY(msgHandler.testPassed());
3803 }
3804
3805 void tst_QVariant::guiVariantAtExit()
3806 {
3807     // crash test, it should not crash at QGuiApplication exit
3808     static QVariant cursor = QCursor();
3809     static QVariant point = QPoint();
3810     static QVariant image = QImage();
3811     static QVariant pallete = QPalette();
3812     Q_UNUSED(cursor);
3813     Q_UNUSED(point);
3814     Q_UNUSED(image);
3815     Q_UNUSED(pallete);
3816     QVERIFY(true);
3817 }
3818
3819 void tst_QVariant::widgetsVariantAtExit()
3820 {
3821     // crash test, it should not crash at QGuiApplication exit
3822     static QVariant icon= QIcon();
3823     static QVariant sizePolicy = QSizePolicy();
3824     Q_UNUSED(icon);
3825     Q_UNUSED(sizePolicy);
3826     QVERIFY(true);
3827 }
3828
3829 QTEST_MAIN(tst_QVariant)
3830 #include "tst_qvariant.moc"