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