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