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