1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
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 "testtypes.h"
50 // In Symbian OS test data is located in applications private dir
55 extern int qt_defaultDpi();
58 class tst_qdeclarativevaluetypes : public QObject
62 tst_qdeclarativevaluetypes() {}
83 void bindingAssignment();
85 void staticAssignment();
87 void autoBindingRemoval();
89 void valueInterceptors();
90 void bindingConflict();
92 void bindingVariantCopy();
93 void scriptVariantCopy();
96 void conflictingBindings();
99 void bindingsSpliceCorrectly();
102 QDeclarativeEngine engine;
105 void tst_qdeclarativevaluetypes::initTestCase()
110 inline QUrl TEST_FILE(const QString &filename)
112 return QUrl::fromLocalFile(QLatin1String(SRCDIR) + QLatin1String("/data/") + filename);
115 void tst_qdeclarativevaluetypes::point()
118 QDeclarativeComponent component(&engine, TEST_FILE("point_read.qml"));
119 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
120 QVERIFY(object != 0);
122 QCOMPARE(object->property("p_x").toInt(), 10);
123 QCOMPARE(object->property("p_y").toInt(), 4);
124 QCOMPARE(object->property("copy"), QVariant(QPoint(10, 4)));
130 QDeclarativeComponent component(&engine, TEST_FILE("point_write.qml"));
131 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
132 QVERIFY(object != 0);
134 QCOMPARE(object->point(), QPoint(11, 12));
140 QDeclarativeComponent component(&engine, TEST_FILE("point_compare.qml"));
141 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
142 QVERIFY(object != 0);
144 QString tostring = QLatin1String("QPoint(10, 4)");
145 QCOMPARE(object->property("tostring").toString(), tostring);
146 QCOMPARE(object->property("equalsString").toBool(), true);
147 QCOMPARE(object->property("equalsColor").toBool(), false);
148 QCOMPARE(object->property("equalsVector3d").toBool(), false);
149 QCOMPARE(object->property("equalsSize").toBool(), false);
150 QCOMPARE(object->property("equalsPoint").toBool(), true);
151 QCOMPARE(object->property("equalsRect").toBool(), false);
152 QCOMPARE(object->property("equalsSelf").toBool(), true);
153 QCOMPARE(object->property("equalsOther").toBool(), false);
154 QCOMPARE(object->property("pointEqualsPointf").toBool(), true);
160 void tst_qdeclarativevaluetypes::pointf()
163 QDeclarativeComponent component(&engine, TEST_FILE("pointf_read.qml"));
164 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
165 QVERIFY(object != 0);
167 QCOMPARE(float(object->property("p_x").toDouble()), float(11.3));
168 QCOMPARE(float(object->property("p_y").toDouble()), float(-10.9));
169 QCOMPARE(object->property("copy"), QVariant(QPointF(11.3, -10.9)));
175 QDeclarativeComponent component(&engine, TEST_FILE("pointf_write.qml"));
176 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
177 QVERIFY(object != 0);
179 QCOMPARE(object->pointf(), QPointF(6.8, 9.3));
185 QDeclarativeComponent component(&engine, TEST_FILE("pointf_compare.qml"));
186 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
187 QVERIFY(object != 0);
189 QString tostring = QLatin1String("QPointF(11.3, -10.9)");
190 QCOMPARE(object->property("tostring").toString(), tostring);
191 QCOMPARE(object->property("equalsString").toBool(), true);
192 QCOMPARE(object->property("equalsColor").toBool(), false);
193 QCOMPARE(object->property("equalsVector3d").toBool(), false);
194 QCOMPARE(object->property("equalsSize").toBool(), false);
195 QCOMPARE(object->property("equalsPoint").toBool(), true);
196 QCOMPARE(object->property("equalsRect").toBool(), false);
197 QCOMPARE(object->property("equalsSelf").toBool(), true);
198 QCOMPARE(object->property("equalsOther").toBool(), false);
199 QCOMPARE(object->property("pointfEqualsPoint").toBool(), true);
205 void tst_qdeclarativevaluetypes::size()
208 QDeclarativeComponent component(&engine, TEST_FILE("size_read.qml"));
209 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
210 QVERIFY(object != 0);
212 QCOMPARE(object->property("s_width").toInt(), 1912);
213 QCOMPARE(object->property("s_height").toInt(), 1913);
214 QCOMPARE(object->property("copy"), QVariant(QSize(1912, 1913)));
220 QDeclarativeComponent component(&engine, TEST_FILE("size_write.qml"));
221 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
222 QVERIFY(object != 0);
224 QCOMPARE(object->size(), QSize(13, 88));
230 QDeclarativeComponent component(&engine, TEST_FILE("size_compare.qml"));
231 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
232 QVERIFY(object != 0);
234 QString tostring = QLatin1String("QSize(1912, 1913)");
235 QCOMPARE(object->property("tostring").toString(), tostring);
236 QCOMPARE(object->property("equalsString").toBool(), true);
237 QCOMPARE(object->property("equalsColor").toBool(), false);
238 QCOMPARE(object->property("equalsVector3d").toBool(), false);
239 QCOMPARE(object->property("equalsSize").toBool(), true);
240 QCOMPARE(object->property("equalsPoint").toBool(), false);
241 QCOMPARE(object->property("equalsRect").toBool(), false);
242 QCOMPARE(object->property("equalsSelf").toBool(), true);
243 QCOMPARE(object->property("equalsOther").toBool(), false);
244 QCOMPARE(object->property("sizeEqualsSizef").toBool(), true);
250 void tst_qdeclarativevaluetypes::sizef()
253 QDeclarativeComponent component(&engine, TEST_FILE("sizef_read.qml"));
254 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
255 QVERIFY(object != 0);
257 QCOMPARE(float(object->property("s_width").toDouble()), float(0.1));
258 QCOMPARE(float(object->property("s_height").toDouble()), float(100923.2));
259 QCOMPARE(object->property("copy"), QVariant(QSizeF(0.1, 100923.2)));
265 QDeclarativeComponent component(&engine, TEST_FILE("sizef_write.qml"));
266 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
267 QVERIFY(object != 0);
269 QCOMPARE(object->sizef(), QSizeF(44.3, 92.8));
275 QDeclarativeComponent component(&engine, TEST_FILE("sizef_compare.qml"));
276 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
277 QVERIFY(object != 0);
279 QString tostring = QLatin1String("QSizeF(0.1, 100923)");
280 QCOMPARE(object->property("tostring").toString(), tostring);
281 QCOMPARE(object->property("equalsString").toBool(), true);
282 QCOMPARE(object->property("equalsColor").toBool(), false);
283 QCOMPARE(object->property("equalsVector3d").toBool(), false);
284 QCOMPARE(object->property("equalsSize").toBool(), true);
285 QCOMPARE(object->property("equalsPoint").toBool(), false);
286 QCOMPARE(object->property("equalsRect").toBool(), false);
287 QCOMPARE(object->property("equalsSelf").toBool(), true);
288 QCOMPARE(object->property("equalsOther").toBool(), false);
289 QCOMPARE(object->property("sizefEqualsSize").toBool(), true);
295 void tst_qdeclarativevaluetypes::variant()
297 QDeclarativeComponent component(&engine, TEST_FILE("variant_read.qml"));
298 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
299 QVERIFY(object != 0);
301 QCOMPARE(float(object->property("s_width").toDouble()), float(0.1));
302 QCOMPARE(float(object->property("s_height").toDouble()), float(100923.2));
303 QCOMPARE(object->property("copy"), QVariant(QSizeF(0.1, 100923.2)));
308 void tst_qdeclarativevaluetypes::sizereadonly()
311 QDeclarativeComponent component(&engine, TEST_FILE("sizereadonly_read.qml"));
312 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
313 QVERIFY(object != 0);
315 QCOMPARE(object->property("s_width").toInt(), 1912);
316 QCOMPARE(object->property("s_height").toInt(), 1913);
317 QCOMPARE(object->property("copy"), QVariant(QSize(1912, 1913)));
323 QDeclarativeComponent component(&engine, TEST_FILE("sizereadonly_writeerror.qml"));
324 QVERIFY(component.isError());
325 QCOMPARE(component.errors().at(0).description(), QLatin1String("Invalid property assignment: \"sizereadonly\" is a read-only property"));
329 QDeclarativeComponent component(&engine, TEST_FILE("sizereadonly_writeerror2.qml"));
330 QVERIFY(component.isError());
331 QCOMPARE(component.errors().at(0).description(), QLatin1String("Invalid property assignment: \"sizereadonly\" is a read-only property"));
335 QDeclarativeComponent component(&engine, TEST_FILE("sizereadonly_writeerror3.qml"));
336 QVERIFY(component.isError());
337 QCOMPARE(component.errors().at(0).description(), QLatin1String("Invalid property assignment: \"sizereadonly\" is a read-only property"));
341 QDeclarativeComponent component(&engine, TEST_FILE("sizereadonly_writeerror4.qml"));
343 QObject *object = component.create();
346 QCOMPARE(object->property("sizereadonly").toSize(), QSize(1912, 1913));
352 void tst_qdeclarativevaluetypes::rect()
355 QDeclarativeComponent component(&engine, TEST_FILE("rect_read.qml"));
356 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
357 QVERIFY(object != 0);
359 QCOMPARE(object->property("r_x").toInt(), 2);
360 QCOMPARE(object->property("r_y").toInt(), 3);
361 QCOMPARE(object->property("r_width").toInt(), 109);
362 QCOMPARE(object->property("r_height").toInt(), 102);
363 QCOMPARE(object->property("copy"), QVariant(QRect(2, 3, 109, 102)));
369 QDeclarativeComponent component(&engine, TEST_FILE("rect_write.qml"));
370 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
371 QVERIFY(object != 0);
373 QCOMPARE(object->rect(), QRect(1234, 7, 56, 63));
379 QDeclarativeComponent component(&engine, TEST_FILE("rect_compare.qml"));
380 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
381 QVERIFY(object != 0);
383 QString tostring = QLatin1String("QRect(2, 3, 109, 102)");
384 QCOMPARE(object->property("tostring").toString(), tostring);
385 QCOMPARE(object->property("equalsString").toBool(), true);
386 QCOMPARE(object->property("equalsColor").toBool(), false);
387 QCOMPARE(object->property("equalsVector3d").toBool(), false);
388 QCOMPARE(object->property("equalsSize").toBool(), false);
389 QCOMPARE(object->property("equalsPoint").toBool(), false);
390 QCOMPARE(object->property("equalsRect").toBool(), true);
391 QCOMPARE(object->property("equalsSelf").toBool(), true);
392 QCOMPARE(object->property("equalsOther").toBool(), false);
393 QCOMPARE(object->property("rectEqualsRectf").toBool(), true);
399 void tst_qdeclarativevaluetypes::rectf()
402 QDeclarativeComponent component(&engine, TEST_FILE("rectf_read.qml"));
403 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
404 QVERIFY(object != 0);
406 QCOMPARE(float(object->property("r_x").toDouble()), float(103.8));
407 QCOMPARE(float(object->property("r_y").toDouble()), float(99.2));
408 QCOMPARE(float(object->property("r_width").toDouble()), float(88.1));
409 QCOMPARE(float(object->property("r_height").toDouble()), float(77.6));
410 QCOMPARE(object->property("copy"), QVariant(QRectF(103.8, 99.2, 88.1, 77.6)));
416 QDeclarativeComponent component(&engine, TEST_FILE("rectf_write.qml"));
417 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
418 QVERIFY(object != 0);
420 QCOMPARE(object->rectf(), QRectF(70.1, -113.2, 80924.8, 99.2));
426 QDeclarativeComponent component(&engine, TEST_FILE("rectf_compare.qml"));
427 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
428 QVERIFY(object != 0);
430 QString tostring = QLatin1String("QRectF(103.8, 99.2, 88.1, 77.6)");
431 QCOMPARE(object->property("tostring").toString(), tostring);
432 QCOMPARE(object->property("equalsString").toBool(), true);
433 QCOMPARE(object->property("equalsColor").toBool(), false);
434 QCOMPARE(object->property("equalsVector3d").toBool(), false);
435 QCOMPARE(object->property("equalsSize").toBool(), false);
436 QCOMPARE(object->property("equalsPoint").toBool(), false);
437 QCOMPARE(object->property("equalsRect").toBool(), true);
438 QCOMPARE(object->property("equalsSelf").toBool(), true);
439 QCOMPARE(object->property("equalsOther").toBool(), false);
440 QCOMPARE(object->property("rectfEqualsRect").toBool(), true);
446 void tst_qdeclarativevaluetypes::vector2d()
449 QDeclarativeComponent component(&engine, TEST_FILE("vector2d_read.qml"));
450 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
451 QVERIFY(object != 0);
453 QCOMPARE((float)object->property("v_x").toDouble(), (float)32.88);
454 QCOMPARE((float)object->property("v_y").toDouble(), (float)1.3);
455 QCOMPARE(object->property("copy"), QVariant(QVector2D(32.88, 1.3)));
461 QDeclarativeComponent component(&engine, TEST_FILE("vector2d_write.qml"));
462 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
463 QVERIFY(object != 0);
465 QCOMPARE(object->vector2(), QVector2D(-0.3, -12.9));
471 QDeclarativeComponent component(&engine, TEST_FILE("vector2d_compare.qml"));
472 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
473 QVERIFY(object != 0);
475 QString tostring = QLatin1String("QVector2D(32.88, 1.3)");
476 QCOMPARE(object->property("tostring").toString(), tostring);
477 QCOMPARE(object->property("equalsString").toBool(), true);
478 QCOMPARE(object->property("equalsColor").toBool(), false);
479 QCOMPARE(object->property("equalsVector3d").toBool(), false);
480 QCOMPARE(object->property("equalsSize").toBool(), false);
481 QCOMPARE(object->property("equalsPoint").toBool(), false);
482 QCOMPARE(object->property("equalsRect").toBool(), false);
483 QCOMPARE(object->property("equalsSelf").toBool(), true);
489 void tst_qdeclarativevaluetypes::vector3d()
492 QDeclarativeComponent component(&engine, TEST_FILE("vector3d_read.qml"));
493 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
494 QVERIFY(object != 0);
496 QCOMPARE((float)object->property("v_x").toDouble(), (float)23.88);
497 QCOMPARE((float)object->property("v_y").toDouble(), (float)3.1);
498 QCOMPARE((float)object->property("v_z").toDouble(), (float)4.3);
499 QCOMPARE(object->property("copy"), QVariant(QVector3D(23.88, 3.1, 4.3)));
505 QDeclarativeComponent component(&engine, TEST_FILE("vector3d_write.qml"));
506 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
507 QVERIFY(object != 0);
509 QCOMPARE(object->vector(), QVector3D(-0.3, -12.9, 907.4));
515 QDeclarativeComponent component(&engine, TEST_FILE("vector3d_compare.qml"));
516 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
517 QVERIFY(object != 0);
519 QString tostring = QLatin1String("QVector3D(23.88, 3.1, 4.3)");
520 QCOMPARE(object->property("tostring").toString(), tostring);
521 QCOMPARE(object->property("equalsString").toBool(), true);
522 QCOMPARE(object->property("equalsColor").toBool(), false);
523 QCOMPARE(object->property("equalsVector3d").toBool(), true);
524 QCOMPARE(object->property("equalsSize").toBool(), false);
525 QCOMPARE(object->property("equalsPoint").toBool(), false);
526 QCOMPARE(object->property("equalsRect").toBool(), false);
527 QCOMPARE(object->property("equalsSelf").toBool(), true);
528 QCOMPARE(object->property("equalsOther").toBool(), false);
534 void tst_qdeclarativevaluetypes::vector4d()
537 QDeclarativeComponent component(&engine, TEST_FILE("vector4d_read.qml"));
538 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
539 QVERIFY(object != 0);
541 QCOMPARE((float)object->property("v_x").toDouble(), (float)54.2);
542 QCOMPARE((float)object->property("v_y").toDouble(), (float)23.88);
543 QCOMPARE((float)object->property("v_z").toDouble(), (float)3.1);
544 QCOMPARE((float)object->property("v_w").toDouble(), (float)4.3);
545 QCOMPARE(object->property("copy"), QVariant(QVector4D(54.2, 23.88, 3.1, 4.3)));
551 QDeclarativeComponent component(&engine, TEST_FILE("vector4d_write.qml"));
552 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
553 QVERIFY(object != 0);
555 QCOMPARE(object->vector4(), QVector4D(-0.3, -12.9, 907.4, 88.5));
561 QDeclarativeComponent component(&engine, TEST_FILE("vector4d_compare.qml"));
562 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
563 QVERIFY(object != 0);
565 QString tostring = QLatin1String("QVector4D(54.2, 23.88, 3.1, 4.3)");
566 QCOMPARE(object->property("tostring").toString(), tostring);
567 QCOMPARE(object->property("equalsString").toBool(), true);
568 QCOMPARE(object->property("equalsColor").toBool(), false);
569 QCOMPARE(object->property("equalsVector3d").toBool(), false);
570 QCOMPARE(object->property("equalsSize").toBool(), false);
571 QCOMPARE(object->property("equalsPoint").toBool(), false);
572 QCOMPARE(object->property("equalsRect").toBool(), false);
573 QCOMPARE(object->property("equalsSelf").toBool(), true);
579 void tst_qdeclarativevaluetypes::quaternion()
582 QDeclarativeComponent component(&engine, TEST_FILE("quaternion_read.qml"));
583 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
584 QVERIFY(object != 0);
586 QCOMPARE((float)object->property("v_scalar").toDouble(), (float)4.3);
587 QCOMPARE((float)object->property("v_x").toDouble(), (float)54.2);
588 QCOMPARE((float)object->property("v_y").toDouble(), (float)23.88);
589 QCOMPARE((float)object->property("v_z").toDouble(), (float)3.1);
590 QCOMPARE(object->property("copy"), QVariant(QQuaternion(4.3, 54.2, 23.88, 3.1)));
596 QDeclarativeComponent component(&engine, TEST_FILE("quaternion_write.qml"));
597 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
598 QVERIFY(object != 0);
600 QCOMPARE(object->quaternion(), QQuaternion(88.5, -0.3, -12.9, 907.4));
606 QDeclarativeComponent component(&engine, TEST_FILE("quaternion_compare.qml"));
607 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
608 QVERIFY(object != 0);
610 QString tostring = QLatin1String("QQuaternion(4.3, 54.2, 23.88, 3.1)");
611 QCOMPARE(object->property("tostring").toString(), tostring);
612 QCOMPARE(object->property("equalsString").toBool(), true);
613 QCOMPARE(object->property("equalsColor").toBool(), false);
614 QCOMPARE(object->property("equalsVector3d").toBool(), false);
615 QCOMPARE(object->property("equalsSize").toBool(), false);
616 QCOMPARE(object->property("equalsPoint").toBool(), false);
617 QCOMPARE(object->property("equalsRect").toBool(), false);
618 QCOMPARE(object->property("equalsSelf").toBool(), true);
624 void tst_qdeclarativevaluetypes::matrix4x4()
627 QDeclarativeComponent component(&engine, TEST_FILE("matrix4x4_read.qml"));
628 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
629 QVERIFY(object != 0);
631 QCOMPARE((float)object->property("v_m11").toDouble(), (float)1);
632 QCOMPARE((float)object->property("v_m12").toDouble(), (float)2);
633 QCOMPARE((float)object->property("v_m13").toDouble(), (float)3);
634 QCOMPARE((float)object->property("v_m14").toDouble(), (float)4);
635 QCOMPARE((float)object->property("v_m21").toDouble(), (float)5);
636 QCOMPARE((float)object->property("v_m22").toDouble(), (float)6);
637 QCOMPARE((float)object->property("v_m23").toDouble(), (float)7);
638 QCOMPARE((float)object->property("v_m24").toDouble(), (float)8);
639 QCOMPARE((float)object->property("v_m31").toDouble(), (float)9);
640 QCOMPARE((float)object->property("v_m32").toDouble(), (float)10);
641 QCOMPARE((float)object->property("v_m33").toDouble(), (float)11);
642 QCOMPARE((float)object->property("v_m34").toDouble(), (float)12);
643 QCOMPARE((float)object->property("v_m41").toDouble(), (float)13);
644 QCOMPARE((float)object->property("v_m42").toDouble(), (float)14);
645 QCOMPARE((float)object->property("v_m43").toDouble(), (float)15);
646 QCOMPARE((float)object->property("v_m44").toDouble(), (float)16);
647 QCOMPARE(object->property("copy"),
648 QVariant(QMatrix4x4(1, 2, 3, 4,
657 QDeclarativeComponent component(&engine, TEST_FILE("matrix4x4_write.qml"));
658 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
659 QVERIFY(object != 0);
661 QCOMPARE(object->matrix(), QMatrix4x4(11, 12, 13, 14,
670 QDeclarativeComponent component(&engine, TEST_FILE("matrix4x4_compare.qml"));
671 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
672 QVERIFY(object != 0);
674 QString tostring = QLatin1String("QMatrix4x4(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)");
675 QCOMPARE(object->property("tostring").toString(), tostring);
676 QCOMPARE(object->property("equalsString").toBool(), true);
677 QCOMPARE(object->property("equalsColor").toBool(), false);
678 QCOMPARE(object->property("equalsVector3d").toBool(), false);
679 QCOMPARE(object->property("equalsSize").toBool(), false);
680 QCOMPARE(object->property("equalsPoint").toBool(), false);
681 QCOMPARE(object->property("equalsRect").toBool(), false);
682 QCOMPARE(object->property("equalsSelf").toBool(), true);
688 void tst_qdeclarativevaluetypes::font()
691 QDeclarativeComponent component(&engine, TEST_FILE("font_read.qml"));
692 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
693 QVERIFY(object != 0);
695 QCOMPARE(object->property("f_family").toString(), object->font().family());
696 QCOMPARE(object->property("f_bold").toBool(), object->font().bold());
697 QCOMPARE(object->property("f_weight").toInt(), object->font().weight());
698 QCOMPARE(object->property("f_italic").toBool(), object->font().italic());
699 QCOMPARE(object->property("f_underline").toBool(), object->font().underline());
700 QCOMPARE(object->property("f_overline").toBool(), object->font().overline());
701 QCOMPARE(object->property("f_strikeout").toBool(), object->font().strikeOut());
702 QCOMPARE(object->property("f_pointSize").toDouble(), object->font().pointSizeF());
703 QCOMPARE(object->property("f_pixelSize").toInt(), int((object->font().pointSizeF() * qt_defaultDpi()) / qreal(72.)));
704 QCOMPARE(object->property("f_capitalization").toInt(), (int)object->font().capitalization());
705 QCOMPARE(object->property("f_letterSpacing").toDouble(), object->font().letterSpacing());
706 QCOMPARE(object->property("f_wordSpacing").toDouble(), object->font().wordSpacing());
708 QCOMPARE(object->property("copy"), QVariant(object->font()));
714 QDeclarativeComponent component(&engine, TEST_FILE("font_write.qml"));
715 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
716 QVERIFY(object != 0);
719 font.setFamily("Helvetica");
721 font.setWeight(QFont::Normal);
722 font.setItalic(false);
723 font.setUnderline(false);
724 font.setStrikeOut(false);
725 font.setPointSize(15);
726 font.setCapitalization(QFont::AllLowercase);
727 font.setLetterSpacing(QFont::AbsoluteSpacing, 9.7);
728 font.setWordSpacing(11.2);
730 QFont f = object->font();
731 QCOMPARE(f.family(), font.family());
732 QCOMPARE(f.bold(), font.bold());
733 QCOMPARE(f.weight(), font.weight());
734 QCOMPARE(f.italic(), font.italic());
735 QCOMPARE(f.underline(), font.underline());
736 QCOMPARE(f.strikeOut(), font.strikeOut());
737 QCOMPARE(f.pointSize(), font.pointSize());
738 QCOMPARE(f.capitalization(), font.capitalization());
739 QCOMPARE(f.letterSpacing(), font.letterSpacing());
740 QCOMPARE(f.wordSpacing(), font.wordSpacing());
747 QDeclarativeComponent component(&engine, TEST_FILE("font_write.2.qml"));
748 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
749 QVERIFY(object != 0);
751 QCOMPARE(object->font().pixelSize(), 10);
756 // Test pixelSize and pointSize
758 QDeclarativeComponent component(&engine, TEST_FILE("font_write.3.qml"));
759 QTest::ignoreMessage(QtWarningMsg, "Both point size and pixel size set. Using pixel size. ");
760 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
761 QVERIFY(object != 0);
763 QCOMPARE(object->font().pixelSize(), 10);
768 QDeclarativeComponent component(&engine, TEST_FILE("font_write.4.qml"));
769 QTest::ignoreMessage(QtWarningMsg, "Both point size and pixel size set. Using pixel size. ");
770 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
771 QVERIFY(object != 0);
773 QCOMPARE(object->font().pixelSize(), 10);
778 QDeclarativeComponent component(&engine, TEST_FILE("font_write.5.qml"));
779 QObject *object = qobject_cast<QObject *>(component.create());
780 QVERIFY(object != 0);
781 MyTypeObject *object1 = object->findChild<MyTypeObject *>("object1");
782 QVERIFY(object1 != 0);
783 MyTypeObject *object2 = object->findChild<MyTypeObject *>("object2");
784 QVERIFY(object2 != 0);
786 QCOMPARE(object1->font().pixelSize(), 19);
787 QCOMPARE(object2->font().pointSize(), 14);
793 QDeclarativeComponent component(&engine, TEST_FILE("font_compare.qml"));
794 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
795 QVERIFY(object != 0);
797 QString tostring = QLatin1String("QFont(") + object->font().toString() + QLatin1Char(')');
798 QCOMPARE(object->property("tostring").toString(), tostring);
799 QCOMPARE(object->property("equalsString").toBool(), true);
800 QCOMPARE(object->property("equalsColor").toBool(), false);
801 QCOMPARE(object->property("equalsVector3d").toBool(), false);
802 QCOMPARE(object->property("equalsSize").toBool(), false);
803 QCOMPARE(object->property("equalsPoint").toBool(), false);
804 QCOMPARE(object->property("equalsRect").toBool(), false);
805 QCOMPARE(object->property("equalsSelf").toBool(), true);
811 void tst_qdeclarativevaluetypes::color()
814 QDeclarativeComponent component(&engine, TEST_FILE("color_read.qml"));
815 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
816 QVERIFY(object != 0);
818 QCOMPARE((float)object->property("v_r").toDouble(), (float)0.2);
819 QCOMPARE((float)object->property("v_g").toDouble(), (float)0.88);
820 QCOMPARE((float)object->property("v_b").toDouble(), (float)0.6);
821 QCOMPARE((float)object->property("v_a").toDouble(), (float)0.34);
823 comparison.setRedF(0.2);
824 comparison.setGreenF(0.88);
825 comparison.setBlueF(0.6);
826 comparison.setAlphaF(0.34);
827 QCOMPARE(object->property("copy"), QVariant(comparison));
833 QDeclarativeComponent component(&engine, TEST_FILE("color_write.qml"));
834 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
835 QVERIFY(object != 0);
838 newColor.setRedF(0.5);
839 newColor.setGreenF(0.38);
840 newColor.setBlueF(0.3);
841 newColor.setAlphaF(0.7);
842 QCOMPARE(object->color(), newColor);
848 QDeclarativeComponent component(&engine, TEST_FILE("color_compare.qml"));
849 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
850 QVERIFY(object != 0);
851 QString colorString("#33e199");
852 QCOMPARE(object->property("colorToString").toString(), colorString);
853 QCOMPARE(object->property("colorEqualsIdenticalRgba").toBool(), true);
854 QCOMPARE(object->property("colorEqualsDifferentAlpha").toBool(), false);
855 QCOMPARE(object->property("colorEqualsDifferentRgba").toBool(), false);
856 QCOMPARE(object->property("colorToStringEqualsColorString").toBool(), true);
857 QCOMPARE(object->property("colorToStringEqualsDifferentAlphaString").toBool(), true);
858 QCOMPARE(object->property("colorToStringEqualsDifferentRgbaString").toBool(), false);
859 QCOMPARE(object->property("colorEqualsColorString").toBool(), true); // maintaining behaviour with QtQuick 1.0
860 QCOMPARE(object->property("colorEqualsDifferentAlphaString").toBool(), true); // maintaining behaviour with QtQuick 1.0
861 QCOMPARE(object->property("colorEqualsDifferentRgbaString").toBool(), false);
863 QCOMPARE(object->property("equalsColor").toBool(), true);
864 QCOMPARE(object->property("equalsVector3d").toBool(), false);
865 QCOMPARE(object->property("equalsSize").toBool(), false);
866 QCOMPARE(object->property("equalsPoint").toBool(), false);
867 QCOMPARE(object->property("equalsRect").toBool(), false);
869 // Color == Property and Property == Color should return the same result.
870 QCOMPARE(object->property("equalsColorRHS").toBool(), object->property("equalsColor").toBool());
871 QCOMPARE(object->property("colorEqualsCopy").toBool(), true);
872 QCOMPARE(object->property("copyEqualsColor").toBool(), object->property("colorEqualsCopy").toBool());
878 // Test bindings can write to value types
879 void tst_qdeclarativevaluetypes::bindingAssignment()
881 QDeclarativeComponent component(&engine, TEST_FILE("bindingAssignment.qml"));
882 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
883 QVERIFY(object != 0);
885 QCOMPARE(object->rect().x(), 10);
887 object->setProperty("value", QVariant(92));
889 QCOMPARE(object->rect().x(), 92);
894 // Test bindings can read from value types
895 void tst_qdeclarativevaluetypes::bindingRead()
897 QDeclarativeComponent component(&engine, TEST_FILE("bindingRead.qml"));
898 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
899 QVERIFY(object != 0);
901 QCOMPARE(object->property("value").toInt(), 2);
903 object->setRect(QRect(19, 3, 88, 2));
905 QCOMPARE(object->property("value").toInt(), 19);
910 // Test static values can assign to value types
911 void tst_qdeclarativevaluetypes::staticAssignment()
913 QDeclarativeComponent component(&engine, TEST_FILE("staticAssignment.qml"));
914 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
915 QVERIFY(object != 0);
917 QCOMPARE(object->rect().x(), 9);
922 // Test scripts can read/write value types
923 void tst_qdeclarativevaluetypes::scriptAccess()
925 QDeclarativeComponent component(&engine, TEST_FILE("scriptAccess.qml"));
926 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
927 QVERIFY(object != 0);
929 QCOMPARE(object->property("valuePre").toInt(), 2);
930 QCOMPARE(object->rect().x(), 19);
931 QCOMPARE(object->property("valuePost").toInt(), 19);
936 // Test that assigning a constant from script removes any binding
937 void tst_qdeclarativevaluetypes::autoBindingRemoval()
940 QDeclarativeComponent component(&engine, TEST_FILE("autoBindingRemoval.qml"));
941 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
942 QVERIFY(object != 0);
944 QCOMPARE(object->rect().x(), 10);
946 object->setProperty("value", QVariant(13));
948 QCOMPARE(object->rect().x(), 13);
950 object->emitRunScript();
952 QCOMPARE(object->rect().x(), 42);
954 object->setProperty("value", QVariant(92));
956 QCOMPARE(object->rect().x(), 42);
963 QDeclarativeComponent component(&engine, TEST_FILE("autoBindingRemoval.2.qml"));
964 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
965 QVERIFY(object != 0);
967 QCOMPARE(object->rect().x(), 10);
969 object->setProperty("value", QVariant(13));
971 QCOMPARE(object->rect().x(), 13);
973 object->emitRunScript();
975 QCOMPARE(object->rect(), QRect(10, 10, 10, 10));
977 object->setProperty("value", QVariant(92));
979 QCOMPARE(object->rect(), QRect(10, 10, 10, 10));
985 QDeclarativeComponent component(&engine, TEST_FILE("autoBindingRemoval.3.qml"));
986 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
987 QVERIFY(object != 0);
989 object->setProperty("value", QVariant(QRect(9, 22, 33, 44)));
991 QCOMPARE(object->rect(), QRect(9, 22, 33, 44));
993 object->emitRunScript();
995 QCOMPARE(object->rect(), QRect(44, 22, 33, 44));
997 object->setProperty("value", QVariant(QRect(19, 3, 4, 8)));
999 QCOMPARE(object->rect(), QRect(44, 22, 33, 44));
1006 // Test that property value sources assign to value types
1007 void tst_qdeclarativevaluetypes::valueSources()
1009 QDeclarativeComponent component(&engine, TEST_FILE("valueSources.qml"));
1010 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
1011 QVERIFY(object != 0);
1013 QCOMPARE(object->rect().x(), 3345);
1018 static void checkNoErrors(QDeclarativeComponent& component)
1020 QList<QDeclarativeError> errors = component.errors();
1021 if (errors.isEmpty())
1023 for (int ii = 0; ii < errors.count(); ++ii) {
1024 const QDeclarativeError &error = errors.at(ii);
1025 qWarning("%d:%d:%s",error.line(),error.column(),error.description().toUtf8().constData());
1029 // Test that property value interceptors can be applied to value types
1030 void tst_qdeclarativevaluetypes::valueInterceptors()
1032 QDeclarativeComponent component(&engine, TEST_FILE("valueInterceptors.qml"));
1033 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
1034 checkNoErrors(component);
1035 QVERIFY(object != 0);
1037 QCOMPARE(object->rect().x(), 13);
1039 object->setProperty("value", 99);
1041 QCOMPARE(object->rect().x(), 112);
1046 // Test that you can't assign a binding to the "root" value type, and a sub-property
1047 void tst_qdeclarativevaluetypes::bindingConflict()
1049 QDeclarativeComponent component(&engine, TEST_FILE("bindingConflict.qml"));
1050 QCOMPARE(component.isError(), true);
1053 #define CPP_TEST(type, v) \
1055 type *t = new type; \
1056 QVariant value(v); \
1057 t->setValue(value); \
1058 QCOMPARE(t->value(), value); \
1062 // Test that accessing a reference to a valuetype after the owning object is deleted
1064 void tst_qdeclarativevaluetypes::deletedObject()
1066 QDeclarativeComponent component(&engine, TEST_FILE("deletedObject.qml"));
1067 QTest::ignoreMessage(QtDebugMsg, "Test: 2");
1068 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
1069 QVERIFY(object != 0);
1071 QObject *dObject = qvariant_cast<QObject *>(object->property("object"));
1072 QVERIFY(dObject != 0);
1075 QTest::ignoreMessage(QtDebugMsg, "Test: undefined");
1076 object->emitRunScript();
1081 // Test that value types can be assigned to another value type property in a binding
1082 void tst_qdeclarativevaluetypes::bindingVariantCopy()
1084 QDeclarativeComponent component(&engine, TEST_FILE("bindingVariantCopy.qml"));
1085 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
1086 QVERIFY(object != 0);
1088 QCOMPARE(object->rect(), QRect(19, 33, 5, 99));
1093 // Test that value types can be assigned to another value type property in script
1094 void tst_qdeclarativevaluetypes::scriptVariantCopy()
1096 QDeclarativeComponent component(&engine, TEST_FILE("scriptVariantCopy.qml"));
1097 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
1098 QVERIFY(object != 0);
1100 QCOMPARE(object->rect(), QRect(2, 3, 109, 102));
1102 object->emitRunScript();
1104 QCOMPARE(object->rect(), QRect(19, 33, 5, 99));
1110 // Test that the value type classes can be used manually
1111 void tst_qdeclarativevaluetypes::cppClasses()
1113 CPP_TEST(QDeclarativePointValueType, QPoint(19, 33));
1114 CPP_TEST(QDeclarativePointFValueType, QPointF(33.6, -23));
1115 CPP_TEST(QDeclarativeSizeValueType, QSize(-100, 18));
1116 CPP_TEST(QDeclarativeSizeFValueType, QSizeF(-100.7, 18.2));
1117 CPP_TEST(QDeclarativeRectValueType, QRect(13, 39, 10928, 88));
1118 CPP_TEST(QDeclarativeRectFValueType, QRectF(88.2, -90.1, 103.2, 118));
1119 CPP_TEST(QDeclarativeVector2DValueType, QVector2D(19.7, 1002));
1120 CPP_TEST(QDeclarativeVector3DValueType, QVector3D(18.2, 19.7, 1002));
1121 CPP_TEST(QDeclarativeVector4DValueType, QVector4D(18.2, 19.7, 1002, 54));
1122 CPP_TEST(QDeclarativeQuaternionValueType, QQuaternion(18.2, 19.7, 1002, 54));
1123 CPP_TEST(QDeclarativeMatrix4x4ValueType,
1124 QMatrix4x4(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16));
1125 CPP_TEST(QDeclarativeFontValueType, QFont("Helvetica"));
1129 void tst_qdeclarativevaluetypes::enums()
1132 QDeclarativeComponent component(&engine, TEST_FILE("enums.1.qml"));
1133 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
1134 QVERIFY(object != 0);
1135 QVERIFY(object->font().capitalization() == QFont::AllUppercase);
1140 QDeclarativeComponent component(&engine, TEST_FILE("enums.2.qml"));
1141 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
1142 QVERIFY(object != 0);
1143 QVERIFY(object->font().capitalization() == QFont::AllUppercase);
1148 QDeclarativeComponent component(&engine, TEST_FILE("enums.3.qml"));
1149 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
1150 QVERIFY(object != 0);
1151 QVERIFY(object->font().capitalization() == QFont::AllUppercase);
1156 QDeclarativeComponent component(&engine, TEST_FILE("enums.4.qml"));
1157 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
1158 QVERIFY(object != 0);
1159 QVERIFY(object->font().capitalization() == QFont::AllUppercase);
1164 QDeclarativeComponent component(&engine, TEST_FILE("enums.5.qml"));
1165 MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
1166 QVERIFY(object != 0);
1167 QVERIFY(object->font().capitalization() == QFont::AllUppercase);
1172 // Tests switching between "conflicting" bindings (eg. a binding on the core
1173 // property, to a binding on the value-type sub-property)
1174 void tst_qdeclarativevaluetypes::conflictingBindings()
1177 QDeclarativeComponent component(&engine, TEST_FILE("conflicting.1.qml"));
1178 QObject *object = component.create();
1179 QVERIFY(object != 0);
1181 QCOMPARE(qvariant_cast<QFont>(object->property("font")).pixelSize(), 12);
1183 QMetaObject::invokeMethod(object, "toggle");
1185 QCOMPARE(qvariant_cast<QFont>(object->property("font")).pixelSize(), 6);
1187 QMetaObject::invokeMethod(object, "toggle");
1189 QCOMPARE(qvariant_cast<QFont>(object->property("font")).pixelSize(), 12);
1195 QDeclarativeComponent component(&engine, TEST_FILE("conflicting.2.qml"));
1196 QObject *object = component.create();
1197 QVERIFY(object != 0);
1199 QCOMPARE(qvariant_cast<QFont>(object->property("font")).pixelSize(), 6);
1201 QMetaObject::invokeMethod(object, "toggle");
1203 QCOMPARE(qvariant_cast<QFont>(object->property("font")).pixelSize(), 12);
1205 QMetaObject::invokeMethod(object, "toggle");
1207 QCOMPARE(qvariant_cast<QFont>(object->property("font")).pixelSize(), 6);
1213 QDeclarativeComponent component(&engine, TEST_FILE("conflicting.3.qml"));
1214 QObject *object = component.create();
1215 QVERIFY(object != 0);
1217 QCOMPARE(qvariant_cast<QFont>(object->property("font")).pixelSize(), 12);
1219 QMetaObject::invokeMethod(object, "toggle");
1221 QCOMPARE(qvariant_cast<QFont>(object->property("font")).pixelSize(), 24);
1223 QMetaObject::invokeMethod(object, "toggle");
1225 QCOMPARE(qvariant_cast<QFont>(object->property("font")).pixelSize(), 12);
1231 void tst_qdeclarativevaluetypes::returnValues()
1233 QDeclarativeComponent component(&engine, TEST_FILE("returnValues.qml"));
1234 QObject *object = component.create();
1235 QVERIFY(object != 0);
1237 QCOMPARE(object->property("test1").toBool(), true);
1238 QCOMPARE(object->property("test2").toBool(), true);
1239 QCOMPARE(object->property("size").toSize(), QSize(13, 14));
1244 void tst_qdeclarativevaluetypes::varAssignment()
1246 QDeclarativeComponent component(&engine, TEST_FILE("varAssignment.qml"));
1247 QObject *object = component.create();
1248 QVERIFY(object != 0);
1250 QCOMPARE(object->property("x").toInt(), 1);
1251 QCOMPARE(object->property("y").toInt(), 2);
1252 QCOMPARE(object->property("z").toInt(), 3);
1257 // Test bindings splice together correctly
1258 void tst_qdeclarativevaluetypes::bindingsSpliceCorrectly()
1261 QDeclarativeComponent component(&engine, TEST_FILE("bindingsSpliceCorrectly.1.qml"));
1262 QObject *object = component.create();
1263 QVERIFY(object != 0);
1265 QCOMPARE(object->property("test").toBool(), true);
1271 QDeclarativeComponent component(&engine, TEST_FILE("bindingsSpliceCorrectly.2.qml"));
1272 QObject *object = component.create();
1273 QVERIFY(object != 0);
1275 QCOMPARE(object->property("test").toBool(), true);
1282 QDeclarativeComponent component(&engine, TEST_FILE("bindingsSpliceCorrectly.3.qml"));
1283 QObject *object = component.create();
1284 QVERIFY(object != 0);
1286 QCOMPARE(object->property("test").toBool(), true);
1292 QDeclarativeComponent component(&engine, TEST_FILE("bindingsSpliceCorrectly.4.qml"));
1293 QObject *object = component.create();
1294 QVERIFY(object != 0);
1296 QCOMPARE(object->property("test").toBool(), true);
1302 QDeclarativeComponent component(&engine, TEST_FILE("bindingsSpliceCorrectly.5.qml"));
1303 QObject *object = component.create();
1304 QVERIFY(object != 0);
1306 QCOMPARE(object->property("test").toBool(), true);
1312 QTEST_MAIN(tst_qdeclarativevaluetypes)
1314 #include "tst_qdeclarativevaluetypes.moc"