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