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