1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
7 ** This file is part of the test suite of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
43 #include <QDeclarativeEngine>
44 #include <QDeclarativeComponent>
46 #include <private/qdeclarativevaluetype_p.h>
47 #include "../../shared/util.h"
48 #include "testtypes.h"
51 extern int qt_defaultDpi();
54 class tst_qdeclarativevaluetypes : public QDeclarativeDataTest
58 tst_qdeclarativevaluetypes() {}
79 void bindingAssignment();
81 void staticAssignment();
83 void autoBindingRemoval();
85 void valueInterceptors();
86 void bindingConflict();
88 void bindingVariantCopy();
89 void scriptVariantCopy();
92 void conflictingBindings();
95 void bindingsSpliceCorrectly();
98 QDeclarativeEngine engine;
101 void tst_qdeclarativevaluetypes::initTestCase()
103 QDeclarativeDataTest::initTestCase();
107 void tst_qdeclarativevaluetypes::point()
110 QDeclarativeComponent component(&engine, testFileUrl("point_read.qml"));
111 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
112 QVERIFY(object != 0);
114 QCOMPARE(object->property("p_x").toInt(), 10);
115 QCOMPARE(object->property("p_y").toInt(), 4);
116 QCOMPARE(object->property("copy"), QVariant(QPoint(10, 4)));
122 QDeclarativeComponent component(&engine, testFileUrl("point_write.qml"));
123 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
124 QVERIFY(object != 0);
126 QCOMPARE(object->point(), QPoint(11, 12));
132 QDeclarativeComponent component(&engine, testFileUrl("point_compare.qml"));
133 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
134 QVERIFY(object != 0);
136 QString tostring = QLatin1String("QPoint(10, 4)");
137 QCOMPARE(object->property("tostring").toString(), tostring);
138 QCOMPARE(object->property("equalsString").toBool(), true);
139 QCOMPARE(object->property("equalsColor").toBool(), false);
140 QCOMPARE(object->property("equalsVector3d").toBool(), false);
141 QCOMPARE(object->property("equalsSize").toBool(), false);
142 QCOMPARE(object->property("equalsPoint").toBool(), true);
143 QCOMPARE(object->property("equalsRect").toBool(), false);
144 QCOMPARE(object->property("equalsSelf").toBool(), true);
145 QCOMPARE(object->property("equalsOther").toBool(), false);
146 QCOMPARE(object->property("pointEqualsPointf").toBool(), true);
152 void tst_qdeclarativevaluetypes::pointf()
155 QDeclarativeComponent component(&engine, testFileUrl("pointf_read.qml"));
156 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
157 QVERIFY(object != 0);
159 QCOMPARE(float(object->property("p_x").toDouble()), float(11.3));
160 QCOMPARE(float(object->property("p_y").toDouble()), float(-10.9));
161 QCOMPARE(object->property("copy"), QVariant(QPointF(11.3, -10.9)));
167 QDeclarativeComponent component(&engine, testFileUrl("pointf_write.qml"));
168 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
169 QVERIFY(object != 0);
171 QCOMPARE(object->pointf(), QPointF(6.8, 9.3));
177 QDeclarativeComponent component(&engine, testFileUrl("pointf_compare.qml"));
178 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
179 QVERIFY(object != 0);
181 QString tostring = QLatin1String("QPointF(11.3, -10.9)");
182 QCOMPARE(object->property("tostring").toString(), tostring);
183 QCOMPARE(object->property("equalsString").toBool(), true);
184 QCOMPARE(object->property("equalsColor").toBool(), false);
185 QCOMPARE(object->property("equalsVector3d").toBool(), false);
186 QCOMPARE(object->property("equalsSize").toBool(), false);
187 QCOMPARE(object->property("equalsPoint").toBool(), true);
188 QCOMPARE(object->property("equalsRect").toBool(), false);
189 QCOMPARE(object->property("equalsSelf").toBool(), true);
190 QCOMPARE(object->property("equalsOther").toBool(), false);
191 QCOMPARE(object->property("pointfEqualsPoint").toBool(), true);
197 void tst_qdeclarativevaluetypes::size()
200 QDeclarativeComponent component(&engine, testFileUrl("size_read.qml"));
201 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
202 QVERIFY(object != 0);
204 QCOMPARE(object->property("s_width").toInt(), 1912);
205 QCOMPARE(object->property("s_height").toInt(), 1913);
206 QCOMPARE(object->property("copy"), QVariant(QSize(1912, 1913)));
212 QDeclarativeComponent component(&engine, testFileUrl("size_write.qml"));
213 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
214 QVERIFY(object != 0);
216 QCOMPARE(object->size(), QSize(13, 88));
222 QDeclarativeComponent component(&engine, testFileUrl("size_compare.qml"));
223 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
224 QVERIFY(object != 0);
226 QString tostring = QLatin1String("QSize(1912, 1913)");
227 QCOMPARE(object->property("tostring").toString(), tostring);
228 QCOMPARE(object->property("equalsString").toBool(), true);
229 QCOMPARE(object->property("equalsColor").toBool(), false);
230 QCOMPARE(object->property("equalsVector3d").toBool(), false);
231 QCOMPARE(object->property("equalsSize").toBool(), true);
232 QCOMPARE(object->property("equalsPoint").toBool(), false);
233 QCOMPARE(object->property("equalsRect").toBool(), false);
234 QCOMPARE(object->property("equalsSelf").toBool(), true);
235 QCOMPARE(object->property("equalsOther").toBool(), false);
236 QCOMPARE(object->property("sizeEqualsSizef").toBool(), true);
242 void tst_qdeclarativevaluetypes::sizef()
245 QDeclarativeComponent component(&engine, testFileUrl("sizef_read.qml"));
246 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
247 QVERIFY(object != 0);
249 QCOMPARE(float(object->property("s_width").toDouble()), float(0.1));
250 QCOMPARE(float(object->property("s_height").toDouble()), float(100923.2));
251 QCOMPARE(object->property("copy"), QVariant(QSizeF(0.1, 100923.2)));
257 QDeclarativeComponent component(&engine, testFileUrl("sizef_write.qml"));
258 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
259 QVERIFY(object != 0);
261 QCOMPARE(object->sizef(), QSizeF(44.3, 92.8));
267 QDeclarativeComponent component(&engine, testFileUrl("sizef_compare.qml"));
268 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
269 QVERIFY(object != 0);
271 QString tostring = QLatin1String("QSizeF(0.1, 100923)");
272 QCOMPARE(object->property("tostring").toString(), tostring);
273 QCOMPARE(object->property("equalsString").toBool(), true);
274 QCOMPARE(object->property("equalsColor").toBool(), false);
275 QCOMPARE(object->property("equalsVector3d").toBool(), false);
276 QCOMPARE(object->property("equalsSize").toBool(), true);
277 QCOMPARE(object->property("equalsPoint").toBool(), false);
278 QCOMPARE(object->property("equalsRect").toBool(), false);
279 QCOMPARE(object->property("equalsSelf").toBool(), true);
280 QCOMPARE(object->property("equalsOther").toBool(), false);
281 QCOMPARE(object->property("sizefEqualsSize").toBool(), true);
287 void tst_qdeclarativevaluetypes::variant()
289 QDeclarativeComponent component(&engine, testFileUrl("variant_read.qml"));
290 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
291 QVERIFY(object != 0);
293 QCOMPARE(float(object->property("s_width").toDouble()), float(0.1));
294 QCOMPARE(float(object->property("s_height").toDouble()), float(100923.2));
295 QCOMPARE(object->property("copy"), QVariant(QSizeF(0.1, 100923.2)));
300 void tst_qdeclarativevaluetypes::sizereadonly()
303 QDeclarativeComponent component(&engine, testFileUrl("sizereadonly_read.qml"));
304 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
305 QVERIFY(object != 0);
307 QCOMPARE(object->property("s_width").toInt(), 1912);
308 QCOMPARE(object->property("s_height").toInt(), 1913);
309 QCOMPARE(object->property("copy"), QVariant(QSize(1912, 1913)));
315 QDeclarativeComponent component(&engine, testFileUrl("sizereadonly_writeerror.qml"));
316 QVERIFY(component.isError());
317 QCOMPARE(component.errors().at(0).description(), QLatin1String("Invalid property assignment: \"sizereadonly\" is a read-only property"));
321 QDeclarativeComponent component(&engine, testFileUrl("sizereadonly_writeerror2.qml"));
322 QVERIFY(component.isError());
323 QCOMPARE(component.errors().at(0).description(), QLatin1String("Invalid property assignment: \"sizereadonly\" is a read-only property"));
327 QDeclarativeComponent component(&engine, testFileUrl("sizereadonly_writeerror3.qml"));
328 QVERIFY(component.isError());
329 QCOMPARE(component.errors().at(0).description(), QLatin1String("Invalid property assignment: \"sizereadonly\" is a read-only property"));
333 QDeclarativeComponent component(&engine, testFileUrl("sizereadonly_writeerror4.qml"));
335 QObject *object = component.create();
338 QCOMPARE(object->property("sizereadonly").toSize(), QSize(1912, 1913));
344 void tst_qdeclarativevaluetypes::rect()
347 QDeclarativeComponent component(&engine, testFileUrl("rect_read.qml"));
348 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
349 QVERIFY(object != 0);
351 QCOMPARE(object->property("r_x").toInt(), 2);
352 QCOMPARE(object->property("r_y").toInt(), 3);
353 QCOMPARE(object->property("r_width").toInt(), 109);
354 QCOMPARE(object->property("r_height").toInt(), 102);
355 QCOMPARE(object->property("copy"), QVariant(QRect(2, 3, 109, 102)));
361 QDeclarativeComponent component(&engine, testFileUrl("rect_write.qml"));
362 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
363 QVERIFY(object != 0);
365 QCOMPARE(object->rect(), QRect(1234, 7, 56, 63));
371 QDeclarativeComponent component(&engine, testFileUrl("rect_compare.qml"));
372 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
373 QVERIFY(object != 0);
375 QString tostring = QLatin1String("QRect(2, 3, 109, 102)");
376 QCOMPARE(object->property("tostring").toString(), tostring);
377 QCOMPARE(object->property("equalsString").toBool(), true);
378 QCOMPARE(object->property("equalsColor").toBool(), false);
379 QCOMPARE(object->property("equalsVector3d").toBool(), false);
380 QCOMPARE(object->property("equalsSize").toBool(), false);
381 QCOMPARE(object->property("equalsPoint").toBool(), false);
382 QCOMPARE(object->property("equalsRect").toBool(), true);
383 QCOMPARE(object->property("equalsSelf").toBool(), true);
384 QCOMPARE(object->property("equalsOther").toBool(), false);
385 QCOMPARE(object->property("rectEqualsRectf").toBool(), true);
391 void tst_qdeclarativevaluetypes::rectf()
394 QDeclarativeComponent component(&engine, testFileUrl("rectf_read.qml"));
395 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
396 QVERIFY(object != 0);
398 QCOMPARE(float(object->property("r_x").toDouble()), float(103.8));
399 QCOMPARE(float(object->property("r_y").toDouble()), float(99.2));
400 QCOMPARE(float(object->property("r_width").toDouble()), float(88.1));
401 QCOMPARE(float(object->property("r_height").toDouble()), float(77.6));
402 QCOMPARE(object->property("copy"), QVariant(QRectF(103.8, 99.2, 88.1, 77.6)));
408 QDeclarativeComponent component(&engine, testFileUrl("rectf_write.qml"));
409 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
410 QVERIFY(object != 0);
412 QCOMPARE(object->rectf(), QRectF(70.1, -113.2, 80924.8, 99.2));
418 QDeclarativeComponent component(&engine, testFileUrl("rectf_compare.qml"));
419 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
420 QVERIFY(object != 0);
422 QString tostring = QLatin1String("QRectF(103.8, 99.2, 88.1, 77.6)");
423 QCOMPARE(object->property("tostring").toString(), tostring);
424 QCOMPARE(object->property("equalsString").toBool(), true);
425 QCOMPARE(object->property("equalsColor").toBool(), false);
426 QCOMPARE(object->property("equalsVector3d").toBool(), false);
427 QCOMPARE(object->property("equalsSize").toBool(), false);
428 QCOMPARE(object->property("equalsPoint").toBool(), false);
429 QCOMPARE(object->property("equalsRect").toBool(), true);
430 QCOMPARE(object->property("equalsSelf").toBool(), true);
431 QCOMPARE(object->property("equalsOther").toBool(), false);
432 QCOMPARE(object->property("rectfEqualsRect").toBool(), true);
438 void tst_qdeclarativevaluetypes::vector2d()
441 QDeclarativeComponent component(&engine, testFileUrl("vector2d_read.qml"));
442 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
443 QVERIFY(object != 0);
445 QCOMPARE((float)object->property("v_x").toDouble(), (float)32.88);
446 QCOMPARE((float)object->property("v_y").toDouble(), (float)1.3);
447 QCOMPARE(object->property("copy"), QVariant(QVector2D(32.88, 1.3)));
453 QDeclarativeComponent component(&engine, testFileUrl("vector2d_write.qml"));
454 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
455 QVERIFY(object != 0);
457 QCOMPARE(object->vector2(), QVector2D(-0.3, -12.9));
463 QDeclarativeComponent component(&engine, testFileUrl("vector2d_compare.qml"));
464 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
465 QVERIFY(object != 0);
467 QString tostring = QLatin1String("QVector2D(32.88, 1.3)");
468 QCOMPARE(object->property("tostring").toString(), tostring);
469 QCOMPARE(object->property("equalsString").toBool(), true);
470 QCOMPARE(object->property("equalsColor").toBool(), false);
471 QCOMPARE(object->property("equalsVector3d").toBool(), false);
472 QCOMPARE(object->property("equalsSize").toBool(), false);
473 QCOMPARE(object->property("equalsPoint").toBool(), false);
474 QCOMPARE(object->property("equalsRect").toBool(), false);
475 QCOMPARE(object->property("equalsSelf").toBool(), true);
481 void tst_qdeclarativevaluetypes::vector3d()
484 QDeclarativeComponent component(&engine, testFileUrl("vector3d_read.qml"));
485 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
486 QVERIFY(object != 0);
488 QCOMPARE((float)object->property("v_x").toDouble(), (float)23.88);
489 QCOMPARE((float)object->property("v_y").toDouble(), (float)3.1);
490 QCOMPARE((float)object->property("v_z").toDouble(), (float)4.3);
491 QCOMPARE(object->property("copy"), QVariant(QVector3D(23.88, 3.1, 4.3)));
497 QDeclarativeComponent component(&engine, testFileUrl("vector3d_write.qml"));
498 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
499 QVERIFY(object != 0);
501 QCOMPARE(object->vector(), QVector3D(-0.3, -12.9, 907.4));
507 QDeclarativeComponent component(&engine, testFileUrl("vector3d_compare.qml"));
508 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
509 QVERIFY(object != 0);
511 QString tostring = QLatin1String("QVector3D(23.88, 3.1, 4.3)");
512 QCOMPARE(object->property("tostring").toString(), tostring);
513 QCOMPARE(object->property("equalsString").toBool(), true);
514 QCOMPARE(object->property("equalsColor").toBool(), false);
515 QCOMPARE(object->property("equalsVector3d").toBool(), true);
516 QCOMPARE(object->property("equalsSize").toBool(), false);
517 QCOMPARE(object->property("equalsPoint").toBool(), false);
518 QCOMPARE(object->property("equalsRect").toBool(), false);
519 QCOMPARE(object->property("equalsSelf").toBool(), true);
520 QCOMPARE(object->property("equalsOther").toBool(), false);
526 void tst_qdeclarativevaluetypes::vector4d()
529 QDeclarativeComponent component(&engine, testFileUrl("vector4d_read.qml"));
530 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
531 QVERIFY(object != 0);
533 QCOMPARE((float)object->property("v_x").toDouble(), (float)54.2);
534 QCOMPARE((float)object->property("v_y").toDouble(), (float)23.88);
535 QCOMPARE((float)object->property("v_z").toDouble(), (float)3.1);
536 QCOMPARE((float)object->property("v_w").toDouble(), (float)4.3);
537 QCOMPARE(object->property("copy"), QVariant(QVector4D(54.2, 23.88, 3.1, 4.3)));
543 QDeclarativeComponent component(&engine, testFileUrl("vector4d_write.qml"));
544 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
545 QVERIFY(object != 0);
547 QCOMPARE(object->vector4(), QVector4D(-0.3, -12.9, 907.4, 88.5));
553 QDeclarativeComponent component(&engine, testFileUrl("vector4d_compare.qml"));
554 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
555 QVERIFY(object != 0);
557 QString tostring = QLatin1String("QVector4D(54.2, 23.88, 3.1, 4.3)");
558 QCOMPARE(object->property("tostring").toString(), tostring);
559 QCOMPARE(object->property("equalsString").toBool(), true);
560 QCOMPARE(object->property("equalsColor").toBool(), false);
561 QCOMPARE(object->property("equalsVector3d").toBool(), false);
562 QCOMPARE(object->property("equalsSize").toBool(), false);
563 QCOMPARE(object->property("equalsPoint").toBool(), false);
564 QCOMPARE(object->property("equalsRect").toBool(), false);
565 QCOMPARE(object->property("equalsSelf").toBool(), true);
571 void tst_qdeclarativevaluetypes::quaternion()
574 QDeclarativeComponent component(&engine, testFileUrl("quaternion_read.qml"));
575 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
576 QVERIFY(object != 0);
578 QCOMPARE((float)object->property("v_scalar").toDouble(), (float)4.3);
579 QCOMPARE((float)object->property("v_x").toDouble(), (float)54.2);
580 QCOMPARE((float)object->property("v_y").toDouble(), (float)23.88);
581 QCOMPARE((float)object->property("v_z").toDouble(), (float)3.1);
582 QCOMPARE(object->property("copy"), QVariant(QQuaternion(4.3, 54.2, 23.88, 3.1)));
588 QDeclarativeComponent component(&engine, testFileUrl("quaternion_write.qml"));
589 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
590 QVERIFY(object != 0);
592 QCOMPARE(object->quaternion(), QQuaternion(88.5, -0.3, -12.9, 907.4));
598 QDeclarativeComponent component(&engine, testFileUrl("quaternion_compare.qml"));
599 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
600 QVERIFY(object != 0);
602 QString tostring = QLatin1String("QQuaternion(4.3, 54.2, 23.88, 3.1)");
603 QCOMPARE(object->property("tostring").toString(), tostring);
604 QCOMPARE(object->property("equalsString").toBool(), true);
605 QCOMPARE(object->property("equalsColor").toBool(), false);
606 QCOMPARE(object->property("equalsVector3d").toBool(), false);
607 QCOMPARE(object->property("equalsSize").toBool(), false);
608 QCOMPARE(object->property("equalsPoint").toBool(), false);
609 QCOMPARE(object->property("equalsRect").toBool(), false);
610 QCOMPARE(object->property("equalsSelf").toBool(), true);
616 void tst_qdeclarativevaluetypes::matrix4x4()
619 QDeclarativeComponent component(&engine, testFileUrl("matrix4x4_read.qml"));
620 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
621 QVERIFY(object != 0);
623 QCOMPARE((float)object->property("v_m11").toDouble(), (float)1);
624 QCOMPARE((float)object->property("v_m12").toDouble(), (float)2);
625 QCOMPARE((float)object->property("v_m13").toDouble(), (float)3);
626 QCOMPARE((float)object->property("v_m14").toDouble(), (float)4);
627 QCOMPARE((float)object->property("v_m21").toDouble(), (float)5);
628 QCOMPARE((float)object->property("v_m22").toDouble(), (float)6);
629 QCOMPARE((float)object->property("v_m23").toDouble(), (float)7);
630 QCOMPARE((float)object->property("v_m24").toDouble(), (float)8);
631 QCOMPARE((float)object->property("v_m31").toDouble(), (float)9);
632 QCOMPARE((float)object->property("v_m32").toDouble(), (float)10);
633 QCOMPARE((float)object->property("v_m33").toDouble(), (float)11);
634 QCOMPARE((float)object->property("v_m34").toDouble(), (float)12);
635 QCOMPARE((float)object->property("v_m41").toDouble(), (float)13);
636 QCOMPARE((float)object->property("v_m42").toDouble(), (float)14);
637 QCOMPARE((float)object->property("v_m43").toDouble(), (float)15);
638 QCOMPARE((float)object->property("v_m44").toDouble(), (float)16);
639 QCOMPARE(object->property("copy"),
640 QVariant(QMatrix4x4(1, 2, 3, 4,
649 QDeclarativeComponent component(&engine, testFileUrl("matrix4x4_write.qml"));
650 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
651 QVERIFY(object != 0);
653 QCOMPARE(object->matrix(), QMatrix4x4(11, 12, 13, 14,
662 QDeclarativeComponent component(&engine, testFileUrl("matrix4x4_compare.qml"));
663 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
664 QVERIFY(object != 0);
666 QString tostring = QLatin1String("QMatrix4x4(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)");
667 QCOMPARE(object->property("tostring").toString(), tostring);
668 QCOMPARE(object->property("equalsString").toBool(), true);
669 QCOMPARE(object->property("equalsColor").toBool(), false);
670 QCOMPARE(object->property("equalsVector3d").toBool(), false);
671 QCOMPARE(object->property("equalsSize").toBool(), false);
672 QCOMPARE(object->property("equalsPoint").toBool(), false);
673 QCOMPARE(object->property("equalsRect").toBool(), false);
674 QCOMPARE(object->property("equalsSelf").toBool(), true);
680 void tst_qdeclarativevaluetypes::font()
683 QDeclarativeComponent component(&engine, testFileUrl("font_read.qml"));
684 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
685 QVERIFY(object != 0);
687 QCOMPARE(object->property("f_family").toString(), object->font().family());
688 QCOMPARE(object->property("f_bold").toBool(), object->font().bold());
689 QCOMPARE(object->property("f_weight").toInt(), object->font().weight());
690 QCOMPARE(object->property("f_italic").toBool(), object->font().italic());
691 QCOMPARE(object->property("f_underline").toBool(), object->font().underline());
692 QCOMPARE(object->property("f_overline").toBool(), object->font().overline());
693 QCOMPARE(object->property("f_strikeout").toBool(), object->font().strikeOut());
694 QCOMPARE(object->property("f_pointSize").toDouble(), object->font().pointSizeF());
695 QCOMPARE(object->property("f_pixelSize").toInt(), int((object->font().pointSizeF() * qt_defaultDpi()) / qreal(72.)));
696 QCOMPARE(object->property("f_capitalization").toInt(), (int)object->font().capitalization());
697 QCOMPARE(object->property("f_letterSpacing").toDouble(), object->font().letterSpacing());
698 QCOMPARE(object->property("f_wordSpacing").toDouble(), object->font().wordSpacing());
700 QCOMPARE(object->property("copy"), QVariant(object->font()));
706 QDeclarativeComponent component(&engine, testFileUrl("font_write.qml"));
707 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
708 QVERIFY(object != 0);
711 font.setFamily("Helvetica");
713 font.setWeight(QFont::Normal);
714 font.setItalic(false);
715 font.setUnderline(false);
716 font.setStrikeOut(false);
717 font.setPointSize(15);
718 font.setCapitalization(QFont::AllLowercase);
719 font.setLetterSpacing(QFont::AbsoluteSpacing, 9.7);
720 font.setWordSpacing(11.2);
722 QFont f = object->font();
723 QCOMPARE(f.family(), font.family());
724 QCOMPARE(f.bold(), font.bold());
725 QCOMPARE(f.weight(), font.weight());
726 QCOMPARE(f.italic(), font.italic());
727 QCOMPARE(f.underline(), font.underline());
728 QCOMPARE(f.strikeOut(), font.strikeOut());
729 QCOMPARE(f.pointSize(), font.pointSize());
730 QCOMPARE(f.capitalization(), font.capitalization());
731 QCOMPARE(f.letterSpacing(), font.letterSpacing());
732 QCOMPARE(f.wordSpacing(), font.wordSpacing());
739 QDeclarativeComponent component(&engine, testFileUrl("font_write.2.qml"));
740 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
741 QVERIFY(object != 0);
743 QCOMPARE(object->font().pixelSize(), 10);
748 // Test pixelSize and pointSize
750 QDeclarativeComponent component(&engine, testFileUrl("font_write.3.qml"));
751 QTest::ignoreMessage(QtWarningMsg, "Both point size and pixel size set. Using pixel size. ");
752 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
753 QVERIFY(object != 0);
755 QCOMPARE(object->font().pixelSize(), 10);
760 QDeclarativeComponent component(&engine, testFileUrl("font_write.4.qml"));
761 QTest::ignoreMessage(QtWarningMsg, "Both point size and pixel size set. Using pixel size. ");
762 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
763 QVERIFY(object != 0);
765 QCOMPARE(object->font().pixelSize(), 10);
770 QDeclarativeComponent component(&engine, testFileUrl("font_write.5.qml"));
771 QObject *object = qobject_cast<QObject *>(component.create());
772 QVERIFY(object != 0);
773 MyTypeObject *object1 = object->findChild<MyTypeObject *>("object1");
774 QVERIFY(object1 != 0);
775 MyTypeObject *object2 = object->findChild<MyTypeObject *>("object2");
776 QVERIFY(object2 != 0);
778 QCOMPARE(object1->font().pixelSize(), 19);
779 QCOMPARE(object2->font().pointSize(), 14);
785 QDeclarativeComponent component(&engine, testFileUrl("font_compare.qml"));
786 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
787 QVERIFY(object != 0);
789 QString tostring = QLatin1String("QFont(") + object->font().toString() + QLatin1Char(')');
790 QCOMPARE(object->property("tostring").toString(), tostring);
791 QCOMPARE(object->property("equalsString").toBool(), true);
792 QCOMPARE(object->property("equalsColor").toBool(), false);
793 QCOMPARE(object->property("equalsVector3d").toBool(), false);
794 QCOMPARE(object->property("equalsSize").toBool(), false);
795 QCOMPARE(object->property("equalsPoint").toBool(), false);
796 QCOMPARE(object->property("equalsRect").toBool(), false);
797 QCOMPARE(object->property("equalsSelf").toBool(), true);
803 void tst_qdeclarativevaluetypes::color()
806 QDeclarativeComponent component(&engine, testFileUrl("color_read.qml"));
807 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
808 QVERIFY(object != 0);
810 QCOMPARE((float)object->property("v_r").toDouble(), (float)0.2);
811 QCOMPARE((float)object->property("v_g").toDouble(), (float)0.88);
812 QCOMPARE((float)object->property("v_b").toDouble(), (float)0.6);
813 QCOMPARE((float)object->property("v_a").toDouble(), (float)0.34);
815 comparison.setRedF(0.2);
816 comparison.setGreenF(0.88);
817 comparison.setBlueF(0.6);
818 comparison.setAlphaF(0.34);
819 QCOMPARE(object->property("copy"), QVariant(comparison));
825 QDeclarativeComponent component(&engine, testFileUrl("color_write.qml"));
826 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
827 QVERIFY(object != 0);
830 newColor.setRedF(0.5);
831 newColor.setGreenF(0.38);
832 newColor.setBlueF(0.3);
833 newColor.setAlphaF(0.7);
834 QCOMPARE(object->color(), newColor);
840 QDeclarativeComponent component(&engine, testFileUrl("color_compare.qml"));
841 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
842 QVERIFY(object != 0);
843 QString colorString("#33e199");
844 QCOMPARE(object->property("colorToString").toString(), colorString);
845 QCOMPARE(object->property("colorEqualsIdenticalRgba").toBool(), true);
846 QCOMPARE(object->property("colorEqualsDifferentAlpha").toBool(), false);
847 QCOMPARE(object->property("colorEqualsDifferentRgba").toBool(), false);
848 QCOMPARE(object->property("colorToStringEqualsColorString").toBool(), true);
849 QCOMPARE(object->property("colorToStringEqualsDifferentAlphaString").toBool(), true);
850 QCOMPARE(object->property("colorToStringEqualsDifferentRgbaString").toBool(), false);
851 QCOMPARE(object->property("colorEqualsColorString").toBool(), true); // maintaining behaviour with QtQuick 1.0
852 QCOMPARE(object->property("colorEqualsDifferentAlphaString").toBool(), true); // maintaining behaviour with QtQuick 1.0
853 QCOMPARE(object->property("colorEqualsDifferentRgbaString").toBool(), false);
855 QCOMPARE(object->property("equalsColor").toBool(), true);
856 QCOMPARE(object->property("equalsVector3d").toBool(), false);
857 QCOMPARE(object->property("equalsSize").toBool(), false);
858 QCOMPARE(object->property("equalsPoint").toBool(), false);
859 QCOMPARE(object->property("equalsRect").toBool(), false);
861 // Color == Property and Property == Color should return the same result.
862 QCOMPARE(object->property("equalsColorRHS").toBool(), object->property("equalsColor").toBool());
863 QCOMPARE(object->property("colorEqualsCopy").toBool(), true);
864 QCOMPARE(object->property("copyEqualsColor").toBool(), object->property("colorEqualsCopy").toBool());
870 // Test bindings can write to value types
871 void tst_qdeclarativevaluetypes::bindingAssignment()
873 QDeclarativeComponent component(&engine, testFileUrl("bindingAssignment.qml"));
874 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
875 QVERIFY(object != 0);
877 QCOMPARE(object->rect().x(), 10);
879 object->setProperty("value", QVariant(92));
881 QCOMPARE(object->rect().x(), 92);
886 // Test bindings can read from value types
887 void tst_qdeclarativevaluetypes::bindingRead()
889 QDeclarativeComponent component(&engine, testFileUrl("bindingRead.qml"));
890 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
891 QVERIFY(object != 0);
893 QCOMPARE(object->property("value").toInt(), 2);
895 object->setRect(QRect(19, 3, 88, 2));
897 QCOMPARE(object->property("value").toInt(), 19);
902 // Test static values can assign to value types
903 void tst_qdeclarativevaluetypes::staticAssignment()
905 QDeclarativeComponent component(&engine, testFileUrl("staticAssignment.qml"));
906 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
907 QVERIFY(object != 0);
909 QCOMPARE(object->rect().x(), 9);
914 // Test scripts can read/write value types
915 void tst_qdeclarativevaluetypes::scriptAccess()
917 QDeclarativeComponent component(&engine, testFileUrl("scriptAccess.qml"));
918 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
919 QVERIFY(object != 0);
921 QCOMPARE(object->property("valuePre").toInt(), 2);
922 QCOMPARE(object->rect().x(), 19);
923 QCOMPARE(object->property("valuePost").toInt(), 19);
928 // Test that assigning a constant from script removes any binding
929 void tst_qdeclarativevaluetypes::autoBindingRemoval()
932 QDeclarativeComponent component(&engine, testFileUrl("autoBindingRemoval.qml"));
933 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
934 QVERIFY(object != 0);
936 QCOMPARE(object->rect().x(), 10);
938 object->setProperty("value", QVariant(13));
940 QCOMPARE(object->rect().x(), 13);
942 object->emitRunScript();
944 QCOMPARE(object->rect().x(), 42);
946 object->setProperty("value", QVariant(92));
948 QCOMPARE(object->rect().x(), 42);
955 QDeclarativeComponent component(&engine, testFileUrl("autoBindingRemoval.2.qml"));
956 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
957 QVERIFY(object != 0);
959 QCOMPARE(object->rect().x(), 10);
961 object->setProperty("value", QVariant(13));
963 QCOMPARE(object->rect().x(), 13);
965 object->emitRunScript();
967 QCOMPARE(object->rect(), QRect(10, 10, 10, 10));
969 object->setProperty("value", QVariant(92));
971 QCOMPARE(object->rect(), QRect(10, 10, 10, 10));
977 QDeclarativeComponent component(&engine, testFileUrl("autoBindingRemoval.3.qml"));
978 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
979 QVERIFY(object != 0);
981 object->setProperty("value", QVariant(QRect(9, 22, 33, 44)));
983 QCOMPARE(object->rect(), QRect(9, 22, 33, 44));
985 object->emitRunScript();
987 QCOMPARE(object->rect(), QRect(44, 22, 33, 44));
989 object->setProperty("value", QVariant(QRect(19, 3, 4, 8)));
991 QCOMPARE(object->rect(), QRect(44, 22, 33, 44));
998 // Test that property value sources assign to value types
999 void tst_qdeclarativevaluetypes::valueSources()
1001 QDeclarativeComponent component(&engine, testFileUrl("valueSources.qml"));
1002 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
1003 QVERIFY(object != 0);
1005 QCOMPARE(object->rect().x(), 3345);
1010 static void checkNoErrors(QDeclarativeComponent& component)
1012 QList<QDeclarativeError> errors = component.errors();
1013 if (errors.isEmpty())
1015 for (int ii = 0; ii < errors.count(); ++ii) {
1016 const QDeclarativeError &error = errors.at(ii);
1017 qWarning("%d:%d:%s",error.line(),error.column(),error.description().toUtf8().constData());
1021 // Test that property value interceptors can be applied to value types
1022 void tst_qdeclarativevaluetypes::valueInterceptors()
1024 QDeclarativeComponent component(&engine, testFileUrl("valueInterceptors.qml"));
1025 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
1026 checkNoErrors(component);
1027 QVERIFY(object != 0);
1029 QCOMPARE(object->rect().x(), 13);
1031 object->setProperty("value", 99);
1033 QCOMPARE(object->rect().x(), 112);
1038 // Test that you can't assign a binding to the "root" value type, and a sub-property
1039 void tst_qdeclarativevaluetypes::bindingConflict()
1041 QDeclarativeComponent component(&engine, testFileUrl("bindingConflict.qml"));
1042 QCOMPARE(component.isError(), true);
1045 #define CPP_TEST(type, v) \
1047 type *t = new type; \
1048 QVariant value(v); \
1049 t->setValue(value); \
1050 QCOMPARE(t->value(), value); \
1054 // Test that accessing a reference to a valuetype after the owning object is deleted
1056 void tst_qdeclarativevaluetypes::deletedObject()
1058 QDeclarativeComponent component(&engine, testFileUrl("deletedObject.qml"));
1059 QTest::ignoreMessage(QtDebugMsg, "Test: 2");
1060 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
1061 QVERIFY(object != 0);
1063 QObject *dObject = qvariant_cast<QObject *>(object->property("object"));
1064 QVERIFY(dObject != 0);
1067 QTest::ignoreMessage(QtDebugMsg, "Test: undefined");
1068 object->emitRunScript();
1073 // Test that value types can be assigned to another value type property in a binding
1074 void tst_qdeclarativevaluetypes::bindingVariantCopy()
1076 QDeclarativeComponent component(&engine, testFileUrl("bindingVariantCopy.qml"));
1077 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
1078 QVERIFY(object != 0);
1080 QCOMPARE(object->rect(), QRect(19, 33, 5, 99));
1085 // Test that value types can be assigned to another value type property in script
1086 void tst_qdeclarativevaluetypes::scriptVariantCopy()
1088 QDeclarativeComponent component(&engine, testFileUrl("scriptVariantCopy.qml"));
1089 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
1090 QVERIFY(object != 0);
1092 QCOMPARE(object->rect(), QRect(2, 3, 109, 102));
1094 object->emitRunScript();
1096 QCOMPARE(object->rect(), QRect(19, 33, 5, 99));
1102 // Test that the value type classes can be used manually
1103 void tst_qdeclarativevaluetypes::cppClasses()
1105 CPP_TEST(QDeclarativePointValueType, QPoint(19, 33));
1106 CPP_TEST(QDeclarativePointFValueType, QPointF(33.6, -23));
1107 CPP_TEST(QDeclarativeSizeValueType, QSize(-100, 18));
1108 CPP_TEST(QDeclarativeSizeFValueType, QSizeF(-100.7, 18.2));
1109 CPP_TEST(QDeclarativeRectValueType, QRect(13, 39, 10928, 88));
1110 CPP_TEST(QDeclarativeRectFValueType, QRectF(88.2, -90.1, 103.2, 118));
1111 CPP_TEST(QDeclarativeVector2DValueType, QVector2D(19.7, 1002));
1112 CPP_TEST(QDeclarativeVector3DValueType, QVector3D(18.2, 19.7, 1002));
1113 CPP_TEST(QDeclarativeVector4DValueType, QVector4D(18.2, 19.7, 1002, 54));
1114 CPP_TEST(QDeclarativeQuaternionValueType, QQuaternion(18.2, 19.7, 1002, 54));
1115 CPP_TEST(QDeclarativeMatrix4x4ValueType,
1116 QMatrix4x4(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16));
1117 CPP_TEST(QDeclarativeFontValueType, QFont("Helvetica"));
1121 void tst_qdeclarativevaluetypes::enums()
1124 QDeclarativeComponent component(&engine, testFileUrl("enums.1.qml"));
1125 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
1126 QVERIFY(object != 0);
1127 QVERIFY(object->font().capitalization() == QFont::AllUppercase);
1132 QDeclarativeComponent component(&engine, testFileUrl("enums.2.qml"));
1133 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
1134 QVERIFY(object != 0);
1135 QVERIFY(object->font().capitalization() == QFont::AllUppercase);
1140 QDeclarativeComponent component(&engine, testFileUrl("enums.3.qml"));
1141 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
1142 QVERIFY(object != 0);
1143 QVERIFY(object->font().capitalization() == QFont::AllUppercase);
1148 QDeclarativeComponent component(&engine, testFileUrl("enums.4.qml"));
1149 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
1150 QVERIFY(object != 0);
1151 QVERIFY(object->font().capitalization() == QFont::AllUppercase);
1156 QDeclarativeComponent component(&engine, testFileUrl("enums.5.qml"));
1157 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
1158 QVERIFY(object != 0);
1159 QVERIFY(object->font().capitalization() == QFont::AllUppercase);
1164 // Tests switching between "conflicting" bindings (eg. a binding on the core
1165 // property, to a binding on the value-type sub-property)
1166 void tst_qdeclarativevaluetypes::conflictingBindings()
1169 QDeclarativeComponent component(&engine, testFileUrl("conflicting.1.qml"));
1170 QObject *object = component.create();
1171 QVERIFY(object != 0);
1173 QCOMPARE(qvariant_cast<QFont>(object->property("font")).pixelSize(), 12);
1175 QMetaObject::invokeMethod(object, "toggle");
1177 QCOMPARE(qvariant_cast<QFont>(object->property("font")).pixelSize(), 6);
1179 QMetaObject::invokeMethod(object, "toggle");
1181 QCOMPARE(qvariant_cast<QFont>(object->property("font")).pixelSize(), 12);
1187 QDeclarativeComponent component(&engine, testFileUrl("conflicting.2.qml"));
1188 QObject *object = component.create();
1189 QVERIFY(object != 0);
1191 QCOMPARE(qvariant_cast<QFont>(object->property("font")).pixelSize(), 6);
1193 QMetaObject::invokeMethod(object, "toggle");
1195 QCOMPARE(qvariant_cast<QFont>(object->property("font")).pixelSize(), 12);
1197 QMetaObject::invokeMethod(object, "toggle");
1199 QCOMPARE(qvariant_cast<QFont>(object->property("font")).pixelSize(), 6);
1205 QDeclarativeComponent component(&engine, testFileUrl("conflicting.3.qml"));
1206 QObject *object = component.create();
1207 QVERIFY(object != 0);
1209 QCOMPARE(qvariant_cast<QFont>(object->property("font")).pixelSize(), 12);
1211 QMetaObject::invokeMethod(object, "toggle");
1213 QCOMPARE(qvariant_cast<QFont>(object->property("font")).pixelSize(), 24);
1215 QMetaObject::invokeMethod(object, "toggle");
1217 QCOMPARE(qvariant_cast<QFont>(object->property("font")).pixelSize(), 12);
1223 void tst_qdeclarativevaluetypes::returnValues()
1225 QDeclarativeComponent component(&engine, testFileUrl("returnValues.qml"));
1226 QObject *object = component.create();
1227 QVERIFY(object != 0);
1229 QCOMPARE(object->property("test1").toBool(), true);
1230 QCOMPARE(object->property("test2").toBool(), true);
1231 QCOMPARE(object->property("size").toSize(), QSize(13, 14));
1236 void tst_qdeclarativevaluetypes::varAssignment()
1238 QDeclarativeComponent component(&engine, testFileUrl("varAssignment.qml"));
1239 QObject *object = component.create();
1240 QVERIFY(object != 0);
1242 QCOMPARE(object->property("x").toInt(), 1);
1243 QCOMPARE(object->property("y").toInt(), 2);
1244 QCOMPARE(object->property("z").toInt(), 3);
1249 // Test bindings splice together correctly
1250 void tst_qdeclarativevaluetypes::bindingsSpliceCorrectly()
1253 QDeclarativeComponent component(&engine, testFileUrl("bindingsSpliceCorrectly.1.qml"));
1254 QObject *object = component.create();
1255 QVERIFY(object != 0);
1257 QCOMPARE(object->property("test").toBool(), true);
1263 QDeclarativeComponent component(&engine, testFileUrl("bindingsSpliceCorrectly.2.qml"));
1264 QObject *object = component.create();
1265 QVERIFY(object != 0);
1267 QCOMPARE(object->property("test").toBool(), true);
1274 QDeclarativeComponent component(&engine, testFileUrl("bindingsSpliceCorrectly.3.qml"));
1275 QObject *object = component.create();
1276 QVERIFY(object != 0);
1278 QCOMPARE(object->property("test").toBool(), true);
1284 QDeclarativeComponent component(&engine, testFileUrl("bindingsSpliceCorrectly.4.qml"));
1285 QObject *object = component.create();
1286 QVERIFY(object != 0);
1288 QCOMPARE(object->property("test").toBool(), true);
1294 QDeclarativeComponent component(&engine, testFileUrl("bindingsSpliceCorrectly.5.qml"));
1295 QObject *object = component.create();
1296 QVERIFY(object != 0);
1298 QCOMPARE(object->property("test").toBool(), true);
1304 QTEST_MAIN(tst_qdeclarativevaluetypes)
1306 #include "tst_qdeclarativevaluetypes.moc"