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