diff options
author | Andrew den Exter <andrew.den-exter@nokia.com> | 2012-05-11 17:37:07 +1000 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-05-24 05:52:32 +0200 |
commit | 4709f30b26042427b225dd164648e3f5907c9d33 (patch) | |
tree | 3edac8d14d3fb77406ef93aa2c34c42131109625 /tests/auto/qml/qqmllanguage | |
parent | 2542778d4837143a61dfcf143c32683acc8b998a (diff) |
Enable binding to properties of type QJSValue.
This allows javascript objects of all types to be bound to properties
declared in c++. Compared to a QVariant the primary benefit this offers
is a type which functions and objects with functions can be bound to.
Change-Id: Idb3313e7ff1d616ab12d44f616083c8296201f3a
Reviewed-by: Kent Hansen <kent.hansen@nokia.com>
Diffstat (limited to 'tests/auto/qml/qqmllanguage')
-rw-r--r-- | tests/auto/qml/qqmllanguage/data/assignLiteralToJSValue.qml | 109 | ||||
-rw-r--r-- | tests/auto/qml/qqmllanguage/data/bindTypeToJSValue.qml | 60 | ||||
-rw-r--r-- | tests/auto/qml/qqmllanguage/testtypes.h | 106 | ||||
-rw-r--r-- | tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp | 367 |
4 files changed, 618 insertions, 24 deletions
diff --git a/tests/auto/qml/qqmllanguage/data/assignLiteralToJSValue.qml b/tests/auto/qml/qqmllanguage/data/assignLiteralToJSValue.qml new file mode 100644 index 0000000000..fce248a381 --- /dev/null +++ b/tests/auto/qml/qqmllanguage/data/assignLiteralToJSValue.qml @@ -0,0 +1,109 @@ +// This tests assigning literals to QJSValue properties. +// These properties store JavaScript object references. + +import QtQuick 2.0 +import Test 1.0 + +QtObject { + property list<QtObject> resources: [ + MyQmlObject { id: testObj1; objectName: "test1"; qjsvalue: 1 }, + MyQmlObject { id: testObj2; objectName: "test2"; qjsvalue: 1.7 }, + MyQmlObject { id: testObj3; objectName: "test3"; qjsvalue: "Hello world!" }, + MyQmlObject { id: testObj4; objectName: "test4"; qjsvalue: "#FF008800" }, + MyQmlObject { id: testObj5; objectName: "test5"; qjsvalue: "10,10,10x10" }, + MyQmlObject { id: testObj6; objectName: "test6"; qjsvalue: "10,10" }, + MyQmlObject { id: testObj7; objectName: "test7"; qjsvalue: "10x10" }, + MyQmlObject { id: testObj8; objectName: "test8"; qjsvalue: "100,100,100" }, + MyQmlObject { id: testObj9; objectName: "test9"; qjsvalue: String("#FF008800") }, + MyQmlObject { id: testObj10; objectName: "test10"; qjsvalue: true }, + MyQmlObject { id: testObj11; objectName: "test11"; qjsvalue: false }, + MyQmlObject { id: testObj12; objectName: "test12"; qjsvalue: Qt.rgba(0.2, 0.3, 0.4, 0.5) }, + MyQmlObject { id: testObj13; objectName: "test13"; qjsvalue: Qt.rect(10, 10, 10, 10) }, + MyQmlObject { id: testObj14; objectName: "test14"; qjsvalue: Qt.point(10, 10) }, + MyQmlObject { id: testObj15; objectName: "test15"; qjsvalue: Qt.size(10, 10) }, + MyQmlObject { id: testObj16; objectName: "test16"; qjsvalue: Qt.vector3d(100, 100, 100); }, + MyQmlObject { id: testObj17; objectName: "test17"; qjsvalue: "1" }, + MyQmlObject { id: testObj18; objectName: "test18"; qjsvalue: "1.7" }, + MyQmlObject { id: testObj19; objectName: "test19"; qjsvalue: "true" }, + MyQmlObject { id: testObj20; objectName: "test20"; qjsvalue: function(val) { return val * 3; } }, + MyQmlObject { id: testObj21; objectName: "test21"; qjsvalue: undefined }, + MyQmlObject { id: testObj22; objectName: "test22"; qjsvalue: null }, + MyQmlObject { id: testObj1Bound; objectName: "test1Bound"; qjsvalue: testObj1.qjsvalue + 4 }, // 1 + 4 + 4 = 9 + MyQmlObject { id: testObj20Bound; objectName: "test20Bound"; qjsvalue: testObj20.qjsvalue(testObj1Bound.qjsvalue) }, // 9 * 3 = 27 + QtObject { + id: varProperties + objectName: "varProperties" + property var test1: testObj1.qjsvalue + property var test2: testObj2.qjsvalue + property var test3: testObj3.qjsvalue + property var test4: testObj4.qjsvalue + property var test5: testObj5.qjsvalue + property var test6: testObj6.qjsvalue + property var test7: testObj7.qjsvalue + property var test8: testObj8.qjsvalue + property var test9: testObj9.qjsvalue + property var test10: testObj10.qjsvalue + property var test11: testObj11.qjsvalue + property var test12: testObj12.qjsvalue + property var test13: testObj13.qjsvalue + property var test14: testObj14.qjsvalue + property var test15: testObj15.qjsvalue + property var test16: testObj16.qjsvalue + property var test20: testObj20.qjsvalue + + property var test1Bound: testObj1.qjsvalue + 4 // 1 + 4 + 4 = 9 + property var test20Bound: testObj20.qjsvalue(test1Bound) // 9 * 3 = 27 + }, + QtObject { + id: variantProperties + objectName: "variantProperties" + property variant test1: testObj1.qjsvalue + property variant test2: testObj2.qjsvalue + property variant test3: testObj3.qjsvalue + property variant test4: testObj4.qjsvalue + property variant test5: testObj5.qjsvalue + property variant test6: testObj6.qjsvalue + property variant test7: testObj7.qjsvalue + property variant test8: testObj8.qjsvalue + property variant test9: testObj9.qjsvalue + property variant test10: testObj10.qjsvalue + property variant test11: testObj11.qjsvalue + property variant test12: testObj12.qjsvalue + property variant test13: testObj13.qjsvalue + property variant test14: testObj14.qjsvalue + property variant test15: testObj15.qjsvalue + property variant test16: testObj16.qjsvalue + + property variant test1Bound: testObj1.qjsvalue + 4 // 1 + 4 + 4 = 9 + property variant test20Bound: testObj20.qjsvalue(test1Bound) // 9 * 3 = 27 + }, + MyTypeObject { + objectName: "typedProperties" + intProperty: testObj1.qjsvalue + doubleProperty: testObj2.qjsvalue + stringProperty: testObj3.qjsvalue + boolProperty: testObj10.qjsvalue + colorProperty: testObj12.qjsvalue + rectFProperty: testObj13.qjsvalue + pointFProperty: testObj14.qjsvalue + sizeFProperty: testObj15.qjsvalue + vectorProperty: testObj16.qjsvalue + }, + MyTypeObject { + objectName: "stringProperties" + intProperty: testObj17.qjsvalue + doubleProperty: testObj18.qjsvalue + stringProperty: testObj3.qjsvalue + boolProperty: testObj19.qjsvalue + colorProperty: testObj4.qjsvalue + rectFProperty: testObj5.qjsvalue + pointFProperty: testObj6.qjsvalue + sizeFProperty: testObj7.qjsvalue + vectorProperty: testObj8.qjsvalue + } + ] + + Component.onCompleted: { + testObj1.qjsvalue = testObj1.qjsvalue + 4 + } +} diff --git a/tests/auto/qml/qqmllanguage/data/bindTypeToJSValue.qml b/tests/auto/qml/qqmllanguage/data/bindTypeToJSValue.qml new file mode 100644 index 0000000000..ff724a4162 --- /dev/null +++ b/tests/auto/qml/qqmllanguage/data/bindTypeToJSValue.qml @@ -0,0 +1,60 @@ +import Test 1.0 +import QtQuick 2.0 + +MyTypeObject { + flagProperty: "FlagVal1 | FlagVal3" + enumProperty: "EnumVal2" + stringProperty: "Hello World!" + uintProperty: 10 + intProperty: -19 + realProperty: 23.2 + doubleProperty: -19.7 + floatProperty: 8.5 + colorProperty: "red" + dateProperty: "1982-11-25" + timeProperty: "11:11:32" + dateTimeProperty: "2009-05-12T13:22:01" + pointProperty: "99,13" + pointFProperty: "-10.1,12.3" + sizeProperty: "99x13" + sizeFProperty: "0.1x0.2" + rectProperty: "9,7,100x200" + rectFProperty: "1000.1,-10.9,400x90.99" + boolProperty: true + variantProperty: "Hello World!" + vectorProperty: "10,1,2.2" + vector4Property: "10,1,2.2,2.3" + urlProperty: "main.qml?with%3cencoded%3edata" + + objectProperty: MyTypeObject {} + + property var varProperty: "Hello World!" + + property list<MyQmlObject> resources: [ + MyQmlObject { objectName: "flagProperty"; qjsvalue: flagProperty }, + MyQmlObject { objectName: "enumProperty"; qjsvalue: enumProperty }, + MyQmlObject { objectName: "stringProperty"; qjsvalue: stringProperty }, + MyQmlObject { objectName: "uintProperty"; qjsvalue: uintProperty }, + MyQmlObject { objectName: "intProperty"; qjsvalue: intProperty }, + MyQmlObject { objectName: "realProperty"; qjsvalue: realProperty }, + MyQmlObject { objectName: "doubleProperty"; qjsvalue: doubleProperty }, + MyQmlObject { objectName: "floatProperty"; qjsvalue: floatProperty }, + MyQmlObject { objectName: "colorProperty"; qjsvalue: colorProperty }, + MyQmlObject { objectName: "dateProperty"; qjsvalue: dateProperty }, + MyQmlObject { objectName: "timeProperty"; qjsvalue: timeProperty }, + MyQmlObject { objectName: "dateTimeProperty"; qjsvalue: dateTimeProperty }, + MyQmlObject { objectName: "pointProperty"; qjsvalue: pointProperty }, + MyQmlObject { objectName: "pointFProperty"; qjsvalue: pointFProperty }, + MyQmlObject { objectName: "sizeProperty"; qjsvalue: sizeProperty }, + MyQmlObject { objectName: "sizeFProperty"; qjsvalue: sizeFProperty }, + MyQmlObject { objectName: "rectProperty"; qjsvalue: rectProperty }, + MyQmlObject { objectName: "rectFProperty"; qjsvalue: rectFProperty }, + MyQmlObject { objectName: "boolProperty"; qjsvalue: boolProperty }, + MyQmlObject { objectName: "variantProperty"; qjsvalue: variantProperty }, + MyQmlObject { objectName: "vectorProperty"; qjsvalue: vectorProperty }, + MyQmlObject { objectName: "vector4Property"; qjsvalue: vector4Property }, + MyQmlObject { objectName: "urlProperty"; qjsvalue: urlProperty }, + MyQmlObject { objectName: "objectProperty"; qjsvalue: objectProperty }, + MyQmlObject { objectName: "varProperty"; qjsvalue: varProperty } + ] +} diff --git a/tests/auto/qml/qqmllanguage/testtypes.h b/tests/auto/qml/qqmllanguage/testtypes.h index a359a34ef5..b601a545de 100644 --- a/tests/auto/qml/qqmllanguage/testtypes.h +++ b/tests/auto/qml/qqmllanguage/testtypes.h @@ -66,6 +66,8 @@ public: int id; }; +Q_DECLARE_METATYPE(QJSValue) + QT_BEGIN_NAMESPACE #define MyInterface_iid "org.qt-project.Qt.Test.MyInterface" Q_DECLARE_INTERFACE(MyInterface, MyInterface_iid); @@ -115,6 +117,7 @@ class MyQmlObject : public QObject, public MyInterface Q_PROPERTY(MyQmlObject *qmlobjectProperty READ qmlobject WRITE setQmlobject) Q_PROPERTY(int propertyWithNotify READ propertyWithNotify WRITE setPropertyWithNotify NOTIFY oddlyNamedNotifySignal) Q_PROPERTY(int nonScriptable READ nonScriptable WRITE setNonScriptable SCRIPTABLE false) + Q_PROPERTY(QJSValue qjsvalue READ qjsvalue WRITE setQJSValue NOTIFY qjsvalueChanged) Q_INTERFACES(MyInterface) public: @@ -156,15 +159,21 @@ public: int nonScriptable() const { return 0; } void setNonScriptable(int) {} + + QJSValue qjsvalue() const { return m_qjsvalue; } + void setQJSValue(const QJSValue &value) { m_qjsvalue = value; emit qjsvalueChanged(); } + public slots: void basicSlot() { qWarning("MyQmlObject::basicSlot"); } void basicSlotWithArgs(int v) { qWarning("MyQmlObject::basicSlotWithArgs(%d)", v); } + void qjsvalueMethod(const QJSValue &v) { m_qjsvalue = v; } signals: void basicSignal(); void basicParameterizedSignal(int parameter); void oddlyNamedNotifySignal(); void signalWithDefaultArg(int parameter = 5); + void qjsvalueChanged(); private: friend class tst_qqmllanguage; @@ -173,6 +182,7 @@ private: MyQmlObject *m_qmlobject; MyCustomVariantType m_custom; int m_propertyWithNotify; + QJSValue m_qjsvalue; }; QML_DECLARE_TYPE(MyQmlObject) QML_DECLARE_TYPEINFO(MyQmlObject, QML_HAS_ATTACHED_PROPERTIES) @@ -202,33 +212,33 @@ class MyTypeObject : public QObject Q_FLAGS(MyFlags) Q_PROPERTY(QString id READ id WRITE setId) - Q_PROPERTY(QObject *objectProperty READ objectProperty WRITE setObjectProperty) + Q_PROPERTY(QObject *objectProperty READ objectProperty WRITE setObjectProperty NOTIFY objectPropertyChanged) Q_PROPERTY(QQmlComponent *componentProperty READ componentProperty WRITE setComponentProperty) - Q_PROPERTY(MyFlags flagProperty READ flagProperty WRITE setFlagProperty) - Q_PROPERTY(MyEnum enumProperty READ enumProperty WRITE setEnumProperty) + Q_PROPERTY(MyFlags flagProperty READ flagProperty WRITE setFlagProperty NOTIFY flagPropertyChanged) + Q_PROPERTY(MyEnum enumProperty READ enumProperty WRITE setEnumProperty NOTIFY enumPropertyChanged) Q_PROPERTY(MyEnum readOnlyEnumProperty READ readOnlyEnumProperty) - Q_PROPERTY(QString stringProperty READ stringProperty WRITE setStringProperty) - Q_PROPERTY(uint uintProperty READ uintProperty WRITE setUintProperty) - Q_PROPERTY(int intProperty READ intProperty WRITE setIntProperty) - Q_PROPERTY(qreal realProperty READ realProperty WRITE setRealProperty) - Q_PROPERTY(double doubleProperty READ doubleProperty WRITE setDoubleProperty) - Q_PROPERTY(float floatProperty READ floatProperty WRITE setFloatProperty) - Q_PROPERTY(QColor colorProperty READ colorProperty WRITE setColorProperty) - Q_PROPERTY(QDate dateProperty READ dateProperty WRITE setDateProperty) - Q_PROPERTY(QTime timeProperty READ timeProperty WRITE setTimeProperty) - Q_PROPERTY(QDateTime dateTimeProperty READ dateTimeProperty WRITE setDateTimeProperty) - Q_PROPERTY(QPoint pointProperty READ pointProperty WRITE setPointProperty) - Q_PROPERTY(QPointF pointFProperty READ pointFProperty WRITE setPointFProperty) - Q_PROPERTY(QSize sizeProperty READ sizeProperty WRITE setSizeProperty) - Q_PROPERTY(QSizeF sizeFProperty READ sizeFProperty WRITE setSizeFProperty) + Q_PROPERTY(QString stringProperty READ stringProperty WRITE setStringProperty NOTIFY stringPropertyChanged) + Q_PROPERTY(uint uintProperty READ uintProperty WRITE setUintProperty NOTIFY uintPropertyChanged) + Q_PROPERTY(int intProperty READ intProperty WRITE setIntProperty NOTIFY intPropertyChanged) + Q_PROPERTY(qreal realProperty READ realProperty WRITE setRealProperty NOTIFY realPropertyChanged) + Q_PROPERTY(double doubleProperty READ doubleProperty WRITE setDoubleProperty NOTIFY doublePropertyChanged) + Q_PROPERTY(float floatProperty READ floatProperty WRITE setFloatProperty NOTIFY floatPropertyChanged) + Q_PROPERTY(QColor colorProperty READ colorProperty WRITE setColorProperty NOTIFY colorPropertyChanged) + Q_PROPERTY(QDate dateProperty READ dateProperty WRITE setDateProperty NOTIFY datePropertyChanged) + Q_PROPERTY(QTime timeProperty READ timeProperty WRITE setTimeProperty NOTIFY timePropertyChanged) + Q_PROPERTY(QDateTime dateTimeProperty READ dateTimeProperty WRITE setDateTimeProperty NOTIFY dateTimePropertyChanged) + Q_PROPERTY(QPoint pointProperty READ pointProperty WRITE setPointProperty NOTIFY pointPropertyChanged) + Q_PROPERTY(QPointF pointFProperty READ pointFProperty WRITE setPointFProperty NOTIFY pointFPropertyChanged) + Q_PROPERTY(QSize sizeProperty READ sizeProperty WRITE setSizeProperty NOTIFY sizePropertyChanged) + Q_PROPERTY(QSizeF sizeFProperty READ sizeFProperty WRITE setSizeFProperty NOTIFY sizeFPropertyChanged) Q_PROPERTY(QRect rectProperty READ rectProperty WRITE setRectProperty NOTIFY rectPropertyChanged) - Q_PROPERTY(QRect rectProperty2 READ rectProperty2 WRITE setRectProperty2) - Q_PROPERTY(QRectF rectFProperty READ rectFProperty WRITE setRectFProperty) - Q_PROPERTY(bool boolProperty READ boolProperty WRITE setBoolProperty) - Q_PROPERTY(QVariant variantProperty READ variantProperty WRITE setVariantProperty) - Q_PROPERTY(QVector3D vectorProperty READ vectorProperty WRITE setVectorProperty) - Q_PROPERTY(QVector4D vector4Property READ vector4Property WRITE setVector4Property) - Q_PROPERTY(QUrl urlProperty READ urlProperty WRITE setUrlProperty) + Q_PROPERTY(QRect rectProperty2 READ rectProperty2 WRITE setRectProperty2 ) + Q_PROPERTY(QRectF rectFProperty READ rectFProperty WRITE setRectFProperty NOTIFY rectFPropertyChanged) + Q_PROPERTY(bool boolProperty READ boolProperty WRITE setBoolProperty NOTIFY boolPropertyChanged) + Q_PROPERTY(QVariant variantProperty READ variantProperty WRITE setVariantProperty NOTIFY variantPropertyChanged) + Q_PROPERTY(QVector3D vectorProperty READ vectorProperty WRITE setVectorProperty NOTIFY vectorPropertyChanged) + Q_PROPERTY(QVector4D vector4Property READ vector4Property WRITE setVector4Property NOTIFY vector4PropertyChanged) + Q_PROPERTY(QUrl urlProperty READ urlProperty WRITE setUrlProperty NOTIFY urlPropertyChanged) Q_PROPERTY(QQmlScriptString scriptProperty READ scriptProperty WRITE setScriptProperty) Q_PROPERTY(MyGroupedObject *grouped READ grouped CONSTANT) @@ -252,6 +262,7 @@ public: } void setObjectProperty(QObject *v) { objectPropertyValue = v; + emit objectPropertyChanged(); } QQmlComponent *componentPropertyValue; @@ -270,6 +281,7 @@ public: } void setFlagProperty(MyFlags v) { flagPropertyValue = v; + emit flagPropertyChanged(); } enum MyEnum { EnumVal1, EnumVal2 }; @@ -279,6 +291,7 @@ public: } void setEnumProperty(MyEnum v) { enumPropertyValue = v; + emit enumPropertyChanged(); } MyEnum readOnlyEnumProperty() const { @@ -291,6 +304,7 @@ public: } void setStringProperty(const QString &v) { stringPropertyValue = v; + emit stringPropertyChanged(); } uint uintPropertyValue; @@ -299,6 +313,7 @@ public: } void setUintProperty(const uint &v) { uintPropertyValue = v; + emit uintPropertyChanged(); } int intPropertyValue; @@ -307,6 +322,7 @@ public: } void setIntProperty(const int &v) { intPropertyValue = v; + emit intPropertyChanged(); } qreal realPropertyValue; @@ -315,6 +331,7 @@ public: } void setRealProperty(const qreal &v) { realPropertyValue = v; + emit realPropertyChanged(); } double doublePropertyValue; @@ -323,6 +340,7 @@ public: } void setDoubleProperty(const double &v) { doublePropertyValue = v; + emit doublePropertyChanged(); } float floatPropertyValue; @@ -331,6 +349,7 @@ public: } void setFloatProperty(const float &v) { floatPropertyValue = v; + emit floatPropertyChanged(); } QColor colorPropertyValue; @@ -339,6 +358,7 @@ public: } void setColorProperty(const QColor &v) { colorPropertyValue = v; + emit colorPropertyChanged(); } QDate datePropertyValue; @@ -347,6 +367,7 @@ public: } void setDateProperty(const QDate &v) { datePropertyValue = v; + emit datePropertyChanged(); } QTime timePropertyValue; @@ -355,6 +376,7 @@ public: } void setTimeProperty(const QTime &v) { timePropertyValue = v; + emit timePropertyChanged(); } QDateTime dateTimePropertyValue; @@ -363,6 +385,7 @@ public: } void setDateTimeProperty(const QDateTime &v) { dateTimePropertyValue = v; + emit dateTimePropertyChanged(); } QPoint pointPropertyValue; @@ -371,6 +394,7 @@ public: } void setPointProperty(const QPoint &v) { pointPropertyValue = v; + emit pointPropertyChanged(); } QPointF pointFPropertyValue; @@ -379,6 +403,7 @@ public: } void setPointFProperty(const QPointF &v) { pointFPropertyValue = v; + emit pointFPropertyChanged(); } QSize sizePropertyValue; @@ -387,6 +412,7 @@ public: } void setSizeProperty(const QSize &v) { sizePropertyValue = v; + emit sizePropertyChanged(); } QSizeF sizeFPropertyValue; @@ -395,6 +421,7 @@ public: } void setSizeFProperty(const QSizeF &v) { sizeFPropertyValue = v; + emit sizeFPropertyChanged(); } QRect rectPropertyValue; @@ -420,6 +447,7 @@ public: } void setRectFProperty(const QRectF &v) { rectFPropertyValue = v; + emit rectFPropertyChanged(); } bool boolPropertyValue; @@ -428,6 +456,7 @@ public: } void setBoolProperty(const bool &v) { boolPropertyValue = v; + emit boolPropertyChanged(); } QVariant variantPropertyValue; @@ -436,6 +465,7 @@ public: } void setVariantProperty(const QVariant &v) { variantPropertyValue = v; + emit variantPropertyChanged(); } QVector3D vectorPropertyValue; @@ -444,6 +474,7 @@ public: } void setVectorProperty(const QVector3D &v) { vectorPropertyValue = v; + emit vectorPropertyChanged(); } QVector4D vector4PropertyValue; @@ -452,6 +483,7 @@ public: } void setVector4Property(const QVector4D &v) { vector4PropertyValue = v; + emit vector4PropertyChanged(); } QUrl urlPropertyValue; @@ -460,6 +492,7 @@ public: } void setUrlProperty(const QUrl &v) { urlPropertyValue = v; + emit urlPropertyChanged(); } QQmlScriptString scriptPropertyValue; @@ -478,7 +511,32 @@ public: void doAction() { emit action(); } signals: void action(); + + void objectPropertyChanged(); + void flagPropertyChanged(); + void enumPropertyChanged(); + void stringPropertyChanged(); + void uintPropertyChanged(); + void intPropertyChanged(); + void realPropertyChanged(); + void doublePropertyChanged(); + void floatPropertyChanged(); + void colorPropertyChanged(); + void datePropertyChanged(); + void timePropertyChanged(); + void dateTimePropertyChanged(); + void pointPropertyChanged(); + void pointFPropertyChanged(); + void sizePropertyChanged(); + void sizeFPropertyChanged(); void rectPropertyChanged(); + void rectFPropertyChanged(); + void boolPropertyChanged(); + void variantPropertyChanged(); + void vectorPropertyChanged(); + void vector4PropertyChanged(); + void urlPropertyChanged(); + }; Q_DECLARE_OPERATORS_FOR_FLAGS(MyTypeObject::MyFlags) diff --git a/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp b/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp index 05029b9489..c6b0dd66cc 100644 --- a/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp +++ b/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp @@ -109,6 +109,11 @@ private slots: void assignCompositeToType(); void assignLiteralToVariant(); void assignLiteralToVar(); + void assignLiteralToJSValue(); + void bindJSValueToVar(); + void bindJSValueToVariant(); + void bindJSValueToType(); + void bindTypeToJSValue(); void customParserTypes(); void rootAsQmlComponent(); void inlineQmlComponents(); @@ -711,6 +716,368 @@ void tst_qqmllanguage::assignLiteralToVar() delete object; } +void tst_qqmllanguage::assignLiteralToJSValue() +{ + QQmlComponent component(&engine, TEST_FILE("assignLiteralToJSValue.qml")); + VERIFY_ERRORS(0); + QObject *root = component.create(); + QVERIFY(root != 0); + + { + MyQmlObject *object = root->findChild<MyQmlObject *>("test1"); + QJSValue value = object->qjsvalue(); + QVERIFY(value.isNumber()); + QCOMPARE(value.toNumber(), qreal(5)); + } { + MyQmlObject *object = root->findChild<MyQmlObject *>("test2"); + QJSValue value = object->qjsvalue(); + QVERIFY(value.isNumber()); + QCOMPARE(value.toNumber(), qreal(1.7)); + } { + MyQmlObject *object = root->findChild<MyQmlObject *>("test3"); + QJSValue value = object->qjsvalue(); + QVERIFY(value.isString()); + QCOMPARE(value.toString(), QString(QLatin1String("Hello world!"))); + }{ + MyQmlObject *object = root->findChild<MyQmlObject *>("test4"); + QJSValue value = object->qjsvalue(); + QVERIFY(value.isString()); + QCOMPARE(value.toString(), QString(QLatin1String("#FF008800"))); + } { + MyQmlObject *object = root->findChild<MyQmlObject *>("test5"); + QJSValue value = object->qjsvalue(); + QVERIFY(value.isString()); + QCOMPARE(value.toString(), QString(QLatin1String("10,10,10x10"))); + } { + MyQmlObject *object = root->findChild<MyQmlObject *>("test6"); + QJSValue value = object->qjsvalue(); + QVERIFY(value.isString()); + QCOMPARE(value.toString(), QString(QLatin1String("10,10"))); + } { + MyQmlObject *object = root->findChild<MyQmlObject *>("test7"); + QJSValue value = object->qjsvalue(); + QVERIFY(value.isString()); + QCOMPARE(value.toString(), QString(QLatin1String("10x10"))); + } { + MyQmlObject *object = root->findChild<MyQmlObject *>("test8"); + QJSValue value = object->qjsvalue(); + QVERIFY(value.isString()); + QCOMPARE(value.toString(), QString(QLatin1String("100,100,100"))); + } { + MyQmlObject *object = root->findChild<MyQmlObject *>("test9"); + QJSValue value = object->qjsvalue(); + QVERIFY(value.isString()); + QCOMPARE(value.toString(), QString(QLatin1String("#FF008800"))); + } { + MyQmlObject *object = root->findChild<MyQmlObject *>("test10"); + QJSValue value = object->qjsvalue(); + QVERIFY(value.isBool()); + QCOMPARE(value.toBool(), true); + } { + MyQmlObject *object = root->findChild<MyQmlObject *>("test11"); + QJSValue value = object->qjsvalue(); + QVERIFY(value.isBool()); + QCOMPARE(value.toBool(), false); + } { + MyQmlObject *object = root->findChild<MyQmlObject *>("test20"); + QJSValue value = object->qjsvalue(); + QVERIFY(value.isCallable()); + QCOMPARE(value.call(QList<QJSValue> () << QJSValue(4)).toInt(), 12); + } { + MyQmlObject *object = root->findChild<MyQmlObject *>("test21"); + QJSValue value = object->qjsvalue(); + QVERIFY(value.isUndefined()); + } { + MyQmlObject *object = root->findChild<MyQmlObject *>("test22"); + QJSValue value = object->qjsvalue(); + QVERIFY(value.isNull()); + } { + MyQmlObject *object = root->findChild<MyQmlObject *>("test1Bound"); + QJSValue value = object->qjsvalue(); + QVERIFY(value.isNumber()); + QCOMPARE(value.toNumber(), qreal(9)); + } { + MyQmlObject *object = root->findChild<MyQmlObject *>("test20Bound"); + QJSValue value = object->qjsvalue(); + QVERIFY(value.isNumber()); + QCOMPARE(value.toNumber(), qreal(27)); + } +} + +void tst_qqmllanguage::bindJSValueToVar() +{ + QQmlComponent component(&engine, TEST_FILE("assignLiteralToJSValue.qml")); + + VERIFY_ERRORS(0); + QObject *root = component.create(); + QVERIFY(root != 0); + + QObject *object = root->findChild<QObject *>("varProperties"); + + QCOMPARE(object->property("test1").userType(), (int)QMetaType::Int); + QCOMPARE(object->property("test2").userType(), (int)QMetaType::Double); + QCOMPARE(object->property("test3").userType(), (int)QVariant::String); + QCOMPARE(object->property("test4").userType(), (int)QVariant::String); + QCOMPARE(object->property("test5").userType(), (int)QVariant::String); + QCOMPARE(object->property("test6").userType(), (int)QVariant::String); + QCOMPARE(object->property("test7").userType(), (int)QVariant::String); + QCOMPARE(object->property("test8").userType(), (int)QVariant::String); + QCOMPARE(object->property("test9").userType(), (int)QVariant::String); + QCOMPARE(object->property("test10").userType(), (int)QVariant::Bool); + QCOMPARE(object->property("test11").userType(), (int)QVariant::Bool); + QCOMPARE(object->property("test12").userType(), (int)QVariant::Color); + QCOMPARE(object->property("test13").userType(), (int)QVariant::RectF); + QCOMPARE(object->property("test14").userType(), (int)QVariant::PointF); + QCOMPARE(object->property("test15").userType(), (int)QVariant::SizeF); + QCOMPARE(object->property("test16").userType(), (int)QVariant::Vector3D); + QCOMPARE(object->property("test1Bound").userType(), (int)QVariant::Int); + QCOMPARE(object->property("test20Bound").userType(), (int)QVariant::Int); + + QCOMPARE(object->property("test1"), QVariant(5)); + QCOMPARE(object->property("test2"), QVariant((double)1.7)); + QCOMPARE(object->property("test3"), QVariant(QString(QLatin1String("Hello world!")))); + QCOMPARE(object->property("test4"), QVariant(QString(QLatin1String("#FF008800")))); + QCOMPARE(object->property("test5"), QVariant(QString(QLatin1String("10,10,10x10")))); + QCOMPARE(object->property("test6"), QVariant(QString(QLatin1String("10,10")))); + QCOMPARE(object->property("test7"), QVariant(QString(QLatin1String("10x10")))); + QCOMPARE(object->property("test8"), QVariant(QString(QLatin1String("100,100,100")))); + QCOMPARE(object->property("test9"), QVariant(QString(QLatin1String("#FF008800")))); + QCOMPARE(object->property("test10"), QVariant(bool(true))); + QCOMPARE(object->property("test11"), QVariant(bool(false))); + QCOMPARE(object->property("test12"), QVariant(QColor::fromRgbF(0.2, 0.3, 0.4, 0.5))); + QCOMPARE(object->property("test13"), QVariant(QRectF(10, 10, 10, 10))); + QCOMPARE(object->property("test14"), QVariant(QPointF(10, 10))); + QCOMPARE(object->property("test15"), QVariant(QSizeF(10, 10))); + QCOMPARE(object->property("test16"), QVariant(QVector3D(100, 100, 100))); + QCOMPARE(object->property("test1Bound"), QVariant(9)); + QCOMPARE(object->property("test20Bound"), QVariant(27)); +} + +void tst_qqmllanguage::bindJSValueToVariant() +{ + QQmlComponent component(&engine, TEST_FILE("assignLiteralToJSValue.qml")); + + VERIFY_ERRORS(0); + QObject *root = component.create(); + QVERIFY(root != 0); + + QObject *object = root->findChild<QObject *>("variantProperties"); + + QCOMPARE(object->property("test1").userType(), (int)QMetaType::Int); + QCOMPARE(object->property("test2").userType(), (int)QMetaType::Double); + QCOMPARE(object->property("test3").userType(), (int)QVariant::String); + QCOMPARE(object->property("test4").userType(), (int)QVariant::String); + QCOMPARE(object->property("test5").userType(), (int)QVariant::String); + QCOMPARE(object->property("test6").userType(), (int)QVariant::String); + QCOMPARE(object->property("test7").userType(), (int)QVariant::String); + QCOMPARE(object->property("test8").userType(), (int)QVariant::String); + QCOMPARE(object->property("test9").userType(), (int)QVariant::String); + QCOMPARE(object->property("test10").userType(), (int)QVariant::Bool); + QCOMPARE(object->property("test11").userType(), (int)QVariant::Bool); + QCOMPARE(object->property("test12").userType(), (int)QVariant::Color); + QCOMPARE(object->property("test13").userType(), (int)QVariant::RectF); + QCOMPARE(object->property("test14").userType(), (int)QVariant::PointF); + QCOMPARE(object->property("test15").userType(), (int)QVariant::SizeF); + QCOMPARE(object->property("test16").userType(), (int)QVariant::Vector3D); + QCOMPARE(object->property("test1Bound").userType(), (int)QVariant::Int); + QCOMPARE(object->property("test20Bound").userType(), (int)QVariant::Int); + + QCOMPARE(object->property("test1"), QVariant(5)); + QCOMPARE(object->property("test2"), QVariant((double)1.7)); + QCOMPARE(object->property("test3"), QVariant(QString(QLatin1String("Hello world!")))); + QCOMPARE(object->property("test4"), QVariant(QString(QLatin1String("#FF008800")))); + QCOMPARE(object->property("test5"), QVariant(QString(QLatin1String("10,10,10x10")))); + QCOMPARE(object->property("test6"), QVariant(QString(QLatin1String("10,10")))); + QCOMPARE(object->property("test7"), QVariant(QString(QLatin1String("10x10")))); + QCOMPARE(object->property("test8"), QVariant(QString(QLatin1String("100,100,100")))); + QCOMPARE(object->property("test9"), QVariant(QString(QLatin1String("#FF008800")))); + QCOMPARE(object->property("test10"), QVariant(bool(true))); + QCOMPARE(object->property("test11"), QVariant(bool(false))); + QCOMPARE(object->property("test12"), QVariant(QColor::fromRgbF(0.2, 0.3, 0.4, 0.5))); + QCOMPARE(object->property("test13"), QVariant(QRectF(10, 10, 10, 10))); + QCOMPARE(object->property("test14"), QVariant(QPointF(10, 10))); + QCOMPARE(object->property("test15"), QVariant(QSizeF(10, 10))); + QCOMPARE(object->property("test16"), QVariant(QVector3D(100, 100, 100))); + QCOMPARE(object->property("test1Bound"), QVariant(9)); + QCOMPARE(object->property("test20Bound"), QVariant(27)); +} + +void tst_qqmllanguage::bindJSValueToType() +{ + QQmlComponent component(&engine, TEST_FILE("assignLiteralToJSValue.qml")); + + VERIFY_ERRORS(0); + QObject *root = component.create(); + QVERIFY(root != 0); + + { + MyTypeObject *object = root->findChild<MyTypeObject *>("typedProperties"); + + QCOMPARE(object->intProperty(), 5); + QCOMPARE(object->doubleProperty(), double(1.7)); + QCOMPARE(object->stringProperty(), QString(QLatin1String("Hello world!"))); + QCOMPARE(object->boolProperty(), true); + QCOMPARE(object->colorProperty(), QColor::fromRgbF(0.2, 0.3, 0.4, 0.5)); + QCOMPARE(object->rectFProperty(), QRectF(10, 10, 10, 10)); + QCOMPARE(object->pointFProperty(), QPointF(10, 10)); + QCOMPARE(object->sizeFProperty(), QSizeF(10, 10)); + QCOMPARE(object->vectorProperty(), QVector3D(100, 100, 100)); + } { + MyTypeObject *object = root->findChild<MyTypeObject *>("stringProperties"); + + QCOMPARE(object->intProperty(), 1); + QCOMPARE(object->doubleProperty(), double(1.7)); + QCOMPARE(object->stringProperty(), QString(QLatin1String("Hello world!"))); + QCOMPARE(object->boolProperty(), true); + QCOMPARE(object->colorProperty(), QColor::fromRgb(0x00, 0x88, 0x00, 0xFF)); + QCOMPARE(object->rectFProperty(), QRectF(10, 10, 10, 10)); + QCOMPARE(object->pointFProperty(), QPointF(10, 10)); + QCOMPARE(object->sizeFProperty(), QSizeF(10, 10)); + QCOMPARE(object->vectorProperty(), QVector3D(100, 100, 100)); + } +} + +void tst_qqmllanguage::bindTypeToJSValue() +{ + QQmlComponent component(&engine, TEST_FILE("bindTypeToJSValue.qml")); + + VERIFY_ERRORS(0); + QObject *root = component.create(); + QVERIFY(root != 0); + + { + MyQmlObject *object = root->findChild<MyQmlObject *>("flagProperty"); + QVERIFY(object); + QJSValue value = object->qjsvalue(); + QVERIFY(value.isNumber()); + QCOMPARE(value.toNumber(), qreal(MyTypeObject::FlagVal1 | MyTypeObject::FlagVal3)); + } { + MyQmlObject *object = root->findChild<MyQmlObject *>("enumProperty"); + QJSValue value = object->qjsvalue(); + QVERIFY(value.isNumber()); + QCOMPARE(value.toNumber(), qreal(MyTypeObject::EnumVal2)); + } { + MyQmlObject *object = root->findChild<MyQmlObject *>("stringProperty"); + QJSValue value = object->qjsvalue(); + QVERIFY(value.isString()); + QCOMPARE(value.toString(), QString(QLatin1String("Hello World!"))); + } { + MyQmlObject *object = root->findChild<MyQmlObject *>("uintProperty"); + QJSValue value = object->qjsvalue(); + QVERIFY(value.isNumber()); + QCOMPARE(value.toNumber(), qreal(10)); + } { + MyQmlObject *object = root->findChild<MyQmlObject *>("intProperty"); + QJSValue value = object->qjsvalue(); + QVERIFY(value.isNumber()); + QCOMPARE(value.toNumber(), qreal(-19)); + } { + MyQmlObject *object = root->findChild<MyQmlObject *>("realProperty"); + QJSValue value = object->qjsvalue(); + QVERIFY(value.isNumber()); + QCOMPARE(value.toNumber(), qreal(23.2)); + } { + MyQmlObject *object = root->findChild<MyQmlObject *>("doubleProperty"); + QJSValue value = object->qjsvalue(); + QVERIFY(value.isNumber()); + QCOMPARE(value.toNumber(), qreal(-19.7)); + } { + MyQmlObject *object = root->findChild<MyQmlObject *>("floatProperty"); + QJSValue value = object->qjsvalue(); + QVERIFY(value.isNumber()); + QCOMPARE(value.toNumber(), qreal(8.5)); + } { + MyQmlObject *object = root->findChild<MyQmlObject *>("colorProperty"); + QJSValue value = object->qjsvalue(); + QVERIFY(value.isObject()); + QCOMPARE(value.property(QLatin1String("r")).toNumber(), qreal(1.0)); + QCOMPARE(value.property(QLatin1String("g")).toNumber(), qreal(0.0)); + QCOMPARE(value.property(QLatin1String("b")).toNumber(), qreal(0.0)); + } { + MyQmlObject *object = root->findChild<MyQmlObject *>("dateProperty"); + QJSValue value = object->qjsvalue(); + QCOMPARE(value.toDateTime().isValid(), true); + } { + MyQmlObject *object = root->findChild<MyQmlObject *>("timeProperty"); + QJSValue value = object->qjsvalue(); + QCOMPARE(value.toDateTime().isValid(), true); + } { + MyQmlObject *object = root->findChild<MyQmlObject *>("dateTimeProperty"); + QJSValue value = object->qjsvalue(); + QCOMPARE(value.toDateTime().isValid(), true); + } { + MyQmlObject *object = root->findChild<MyQmlObject *>("pointProperty"); + QJSValue value = object->qjsvalue(); + QVERIFY(value.isObject()); + QCOMPARE(value.property(QLatin1String("x")).toNumber(), qreal(99)); + QCOMPARE(value.property(QLatin1String("y")).toNumber(), qreal(13)); + } { + MyQmlObject *object = root->findChild<MyQmlObject *>("pointFProperty"); + QJSValue value = object->qjsvalue(); + QVERIFY(value.isObject()); + QCOMPARE(value.property(QLatin1String("x")).toNumber(), qreal(-10.1)); + QCOMPARE(value.property(QLatin1String("y")).toNumber(), qreal(12.3)); + } { + MyQmlObject *object = root->findChild<MyQmlObject *>("rectProperty"); + QJSValue value = object->qjsvalue(); + QVERIFY(value.isObject()); + QCOMPARE(value.property(QLatin1String("x")).toNumber(), qreal(9)); + QCOMPARE(value.property(QLatin1String("y")).toNumber(), qreal(7)); + QCOMPARE(value.property(QLatin1String("width")).toNumber(), qreal(100)); + QCOMPARE(value.property(QLatin1String("height")).toNumber(), qreal(200)); + } { + MyQmlObject *object = root->findChild<MyQmlObject *>("rectFProperty"); + QJSValue value = object->qjsvalue(); + QVERIFY(value.isObject()); + QCOMPARE(value.property(QLatin1String("x")).toNumber(), qreal(1000.1)); + QCOMPARE(value.property(QLatin1String("y")).toNumber(), qreal(-10.9)); + QCOMPARE(value.property(QLatin1String("width")).toNumber(), qreal(400)); + QCOMPARE(value.property(QLatin1String("height")).toNumber(), qreal(90.99)); + } { + MyQmlObject *object = root->findChild<MyQmlObject *>("boolProperty"); + QJSValue value = object->qjsvalue(); + QVERIFY(value.isBool()); + QCOMPARE(value.toBool(), true); + } { + MyQmlObject *object = root->findChild<MyQmlObject *>("variantProperty"); + QJSValue value = object->qjsvalue(); + QVERIFY(value.isString()); + QCOMPARE(value.toString(), QString(QLatin1String("Hello World!"))); + } { + MyQmlObject *object = root->findChild<MyQmlObject *>("vectorProperty"); + QJSValue value = object->qjsvalue(); + QVERIFY(value.isObject()); + QCOMPARE(value.property(QLatin1String("x")).toNumber(), qreal(10.0f)); + QCOMPARE(value.property(QLatin1String("y")).toNumber(), qreal(1.0f)); + QCOMPARE(value.property(QLatin1String("z")).toNumber(), qreal(2.2f)); + } { + MyQmlObject *object = root->findChild<MyQmlObject *>("vector4Property"); + QJSValue value = object->qjsvalue(); + QVERIFY(value.isObject()); + QCOMPARE(value.property(QLatin1String("x")).toNumber(), qreal(10.0f)); + QCOMPARE(value.property(QLatin1String("y")).toNumber(), qreal(1.0f)); + QCOMPARE(value.property(QLatin1String("z")).toNumber(), qreal(2.2f)); + QCOMPARE(value.property(QLatin1String("w")).toNumber(), qreal(2.3f)); + } { + MyQmlObject *object = root->findChild<MyQmlObject *>("urlProperty"); + QJSValue value = object->qjsvalue(); + QVERIFY(value.isString()); + QUrl encoded; + encoded.setEncodedUrl("main.qml?with%3cencoded%3edata", QUrl::TolerantMode); + QCOMPARE(value.toString(), component.url().resolved(encoded).toString()); + } { + MyQmlObject *object = root->findChild<MyQmlObject *>("objectProperty"); + QJSValue value = object->qjsvalue(); + QVERIFY(value.isQObject()); + QVERIFY(qobject_cast<MyTypeObject *>(value.toQObject())); + } { + MyQmlObject *object = root->findChild<MyQmlObject *>("varProperty"); + QJSValue value = object->qjsvalue(); + QVERIFY(value.isString()); + QCOMPARE(value.toString(), QString(QLatin1String("Hello World!"))); + } +} + // Tests that custom parser types can be instantiated void tst_qqmllanguage::customParserTypes() { |