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