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