From 8e94e7d95f7fbc142a1a5faf3347aa165ff2426e Mon Sep 17 00:00:00 2001 From: Aaron Kennedy Date: Fri, 6 Nov 2009 12:44:19 +1000 Subject: Update valuetype tests Add a new test, and make the old variantCopy pass --- src/declarative/qml/qmlengine.cpp | 17 +--- src/declarative/qml/qmlvaluetypescriptclass.cpp | 101 ++++++++++----------- src/declarative/qml/qmlvaluetypescriptclass_p.h | 18 ++-- .../valuetypes/data/bindingVariantCopy.qml | 13 +++ .../valuetypes/data/scriptVariantCopy.qml | 14 +++ .../declarative/valuetypes/data/variantCopy.qml | 13 --- .../auto/declarative/valuetypes/tst_valuetypes.cpp | 26 +++++- 7 files changed, 105 insertions(+), 97 deletions(-) create mode 100644 tests/auto/declarative/valuetypes/data/bindingVariantCopy.qml create mode 100644 tests/auto/declarative/valuetypes/data/scriptVariantCopy.qml delete mode 100644 tests/auto/declarative/valuetypes/data/variantCopy.qml diff --git a/src/declarative/qml/qmlengine.cpp b/src/declarative/qml/qmlengine.cpp index e46205d..177818f 100644 --- a/src/declarative/qml/qmlengine.cpp +++ b/src/declarative/qml/qmlengine.cpp @@ -908,7 +908,7 @@ QVariant QmlEnginePrivate::scriptValueToVariant(const QScriptValue &val) else if (dc == contextClass) return QVariant(); - QScriptClass *sc = val.scriptClass(); + QScriptDeclarativeClass *sc = QScriptDeclarativeClass::scriptClass(val); if (!sc) { return val.toVariant(); } else if (sc == valueTypeClass) { @@ -929,20 +929,7 @@ QVariant QmlScriptClass::toVariant(QmlEngine *engine, const QScriptValue &val) QmlEnginePrivate *ep = static_cast(QObjectPrivate::get(engine)); - QScriptDeclarativeClass *dc = QScriptDeclarativeClass::scriptClass(val); - if (dc == ep->objectClass) - return QVariant::fromValue(ep->objectClass->toQObject(val)); - else if (dc == ep->contextClass) - return QVariant(); - - QScriptClass *sc = val.scriptClass(); - if (!sc) { - return val.toVariant(); - } else if (sc == ep->valueTypeClass) { - return ep->valueTypeClass->toVariant(val); - } - - return QVariant(); + return ep->scriptValueToVariant(val); } // XXX this beyonds in QUrl::toLocalFile() diff --git a/src/declarative/qml/qmlvaluetypescriptclass.cpp b/src/declarative/qml/qmlvaluetypescriptclass.cpp index 0c30992..e939e80 100644 --- a/src/declarative/qml/qmlvaluetypescriptclass.cpp +++ b/src/declarative/qml/qmlvaluetypescriptclass.cpp @@ -44,15 +44,14 @@ QT_BEGIN_NAMESPACE -struct QmlValueTypeReference { +struct QmlValueTypeReference : public QScriptDeclarativeClass::Object { QmlValueType *type; QGuard object; int property; }; -Q_DECLARE_METATYPE(QmlValueTypeReference); QmlValueTypeScriptClass::QmlValueTypeScriptClass(QmlEngine *bindEngine) -: QScriptClass(QmlEnginePrivate::getScriptEngine(bindEngine)), engine(bindEngine) +: QScriptDeclarativeClass(QmlEnginePrivate::getScriptEngine(bindEngine)), engine(bindEngine) { } @@ -62,89 +61,83 @@ QmlValueTypeScriptClass::~QmlValueTypeScriptClass() QScriptValue QmlValueTypeScriptClass::newObject(QObject *object, int coreIndex, QmlValueType *type) { - QmlValueTypeReference ref = { type, object, coreIndex }; - QScriptEngine *scriptEngine = QmlEnginePrivate::getScriptEngine(engine); - return scriptEngine->newObject(this, scriptEngine->newVariant(qVariantFromValue(ref))); + QmlValueTypeReference *ref = new QmlValueTypeReference; + ref->type = type; + ref->object = object; + ref->property = coreIndex; + return QScriptDeclarativeClass::newObject(QmlEnginePrivate::getScriptEngine(engine), this, ref); } -QmlValueTypeScriptClass::QueryFlags -QmlValueTypeScriptClass::queryProperty(const QScriptValue &object, - const QScriptString &name, - QueryFlags flags, uint *id) +QScriptClass::QueryFlags +QmlValueTypeScriptClass::queryProperty(Object *obj, const Identifier &name, + QScriptClass::QueryFlags) { - Q_UNUSED(flags); - QmlValueTypeReference ref = - qvariant_cast(object.data().toVariant()); + QmlValueTypeReference *ref = static_cast(obj); - if (!ref.object) + m_lastIndex = -1; + + if (!ref->object) return 0; - QByteArray propName = name.toString().toUtf8(); + QByteArray propName = toString(name).toUtf8(); - int idx = ref.type->metaObject()->indexOfProperty(propName.constData()); - if (idx == -1) + m_lastIndex = ref->type->metaObject()->indexOfProperty(propName.constData()); + if (m_lastIndex == -1) return 0; - *id = idx; - QMetaProperty prop = ref.object->metaObject()->property(idx); + QMetaProperty prop = ref->object->metaObject()->property(m_lastIndex); - QmlValueTypeScriptClass::QueryFlags rv = - QmlValueTypeScriptClass::HandlesReadAccess; + QScriptClass::QueryFlags rv = + QScriptClass::HandlesReadAccess; if (prop.isWritable()) - rv |= QmlValueTypeScriptClass::HandlesWriteAccess; + rv |= QScriptClass::HandlesWriteAccess; return rv; } -QScriptValue QmlValueTypeScriptClass::property(const QScriptValue &object, - const QScriptString &name, - uint id) +QScriptValue QmlValueTypeScriptClass::property(Object *obj, const Identifier &) { - Q_UNUSED(name); - QmlValueTypeReference ref = - qvariant_cast(object.data().toVariant()); - - if (!ref.object) - return QScriptValue(); + QmlValueTypeReference *ref = static_cast(obj); - ref.type->read(ref.object, ref.property); - - QMetaProperty p = ref.type->metaObject()->property(id); - QVariant rv = p.read(ref.type); + QMetaProperty p = ref->type->metaObject()->property(m_lastIndex); + ref->type->read(ref->object, ref->property); + QVariant rv = p.read(ref->type); return static_cast(QObjectPrivate::get(engine))->scriptValueFromVariant(rv); } -void QmlValueTypeScriptClass::setProperty(QScriptValue &object, - const QScriptString &name, - uint id, +void QmlValueTypeScriptClass::setProperty(Object *obj, const Identifier &, const QScriptValue &value) { - Q_UNUSED(name); - QmlValueTypeReference ref = - qvariant_cast(object.data().toVariant()); - - if (!ref.object) - return; + QmlValueTypeReference *ref = static_cast(obj); QVariant v = QmlScriptClass::toVariant(engine, value); - ref.type->read(ref.object, ref.property); - QMetaProperty p = ref.type->metaObject()->property(id); - p.write(ref.type, v); - ref.type->write(ref.object, ref.property, 0); + ref->type->read(ref->object, ref->property); + QMetaProperty p = ref->type->metaObject()->property(m_lastIndex); + p.write(ref->type, v); + ref->type->write(ref->object, ref->property, 0); } -QVariant QmlValueTypeScriptClass::toVariant(const QScriptValue &val) +QVariant QmlValueTypeScriptClass::toVariant(Object *obj, bool *ok) { - QmlValueTypeReference ref = - qvariant_cast(val.data().toVariant()); + QmlValueTypeReference *ref = static_cast(obj); - if (!ref.object) + if (ok) *ok = true; + + if (ref->object) { + ref->type->read(ref->object, ref->property); + return ref->type->value(); + } else { return QVariant(); + } +} + +QVariant QmlValueTypeScriptClass::toVariant(const QScriptValue &value) +{ + Q_ASSERT(scriptClass(value) == this); - QMetaProperty p = ref.object->metaObject()->property(ref.property); - return p.read(ref.object); + return toVariant(object(value), 0); } QT_END_NAMESPACE diff --git a/src/declarative/qml/qmlvaluetypescriptclass_p.h b/src/declarative/qml/qmlvaluetypescriptclass_p.h index bd31ec1..19020b2 100644 --- a/src/declarative/qml/qmlvaluetypescriptclass_p.h +++ b/src/declarative/qml/qmlvaluetypescriptclass_p.h @@ -59,7 +59,7 @@ QT_BEGIN_NAMESPACE class QmlEngine; class QmlValueType; -class QmlValueTypeScriptClass : public QScriptClass +class QmlValueTypeScriptClass : public QScriptDeclarativeClass { public: QmlValueTypeScriptClass(QmlEngine *); @@ -67,20 +67,16 @@ public: QScriptValue newObject(QObject *object, int coreIndex, QmlValueType *); - virtual QueryFlags queryProperty(const QScriptValue &object, - const QScriptString &name, - QueryFlags flags, uint *id); - virtual QScriptValue property(const QScriptValue &object, - const QScriptString &name, - uint id); - virtual void setProperty(QScriptValue &object, - const QScriptString &name, - uint id, - const QScriptValue &value); + virtual QScriptClass::QueryFlags queryProperty(Object *, const Identifier &, + QScriptClass::QueryFlags flags); + virtual QScriptValue property(Object *, const Identifier &); + virtual void setProperty(Object *, const Identifier &name, const QScriptValue &); + virtual QVariant toVariant(Object *, bool *ok = 0); QVariant toVariant(const QScriptValue &); private: QmlEngine *engine; + int m_lastIndex; }; QT_END_NAMESPACE diff --git a/tests/auto/declarative/valuetypes/data/bindingVariantCopy.qml b/tests/auto/declarative/valuetypes/data/bindingVariantCopy.qml new file mode 100644 index 0000000..691a56c --- /dev/null +++ b/tests/auto/declarative/valuetypes/data/bindingVariantCopy.qml @@ -0,0 +1,13 @@ +import Test 1.0 + +MyTypeObject { + property var object + object: MyTypeObject { + rect.x: 19 + rect.y: 33 + rect.width: 5 + rect.height: 99 + } + + rect: object.rect +} diff --git a/tests/auto/declarative/valuetypes/data/scriptVariantCopy.qml b/tests/auto/declarative/valuetypes/data/scriptVariantCopy.qml new file mode 100644 index 0000000..29157e8 --- /dev/null +++ b/tests/auto/declarative/valuetypes/data/scriptVariantCopy.qml @@ -0,0 +1,14 @@ +import Test 1.0 + +MyTypeObject { + property var object + object: MyTypeObject { + rect.x: 19 + rect.y: 33 + rect.width: 5 + rect.height: 99 + } + + onRunScript: rect = object.rect +} + diff --git a/tests/auto/declarative/valuetypes/data/variantCopy.qml b/tests/auto/declarative/valuetypes/data/variantCopy.qml deleted file mode 100644 index 691a56c..0000000 --- a/tests/auto/declarative/valuetypes/data/variantCopy.qml +++ /dev/null @@ -1,13 +0,0 @@ -import Test 1.0 - -MyTypeObject { - property var object - object: MyTypeObject { - rect.x: 19 - rect.y: 33 - rect.width: 5 - rect.height: 99 - } - - rect: object.rect -} diff --git a/tests/auto/declarative/valuetypes/tst_valuetypes.cpp b/tests/auto/declarative/valuetypes/tst_valuetypes.cpp index 9821a01..d42bfc5 100644 --- a/tests/auto/declarative/valuetypes/tst_valuetypes.cpp +++ b/tests/auto/declarative/valuetypes/tst_valuetypes.cpp @@ -71,7 +71,8 @@ private slots: void valueInterceptors(); void bindingConflict(); void deletedObject(); - void variantCopy(); + void bindingVariantCopy(); + void scriptVariantCopy(); void cppClasses(); private: @@ -524,10 +525,10 @@ void tst_valuetypes::deletedObject() delete object; } -// Test that value types can be assigned to another value type property -void tst_valuetypes::variantCopy() +// Test that value types can be assigned to another value type property in a binding +void tst_valuetypes::bindingVariantCopy() { - QmlComponent component(&engine, TEST_FILE("variantCopy.qml")); + QmlComponent component(&engine, TEST_FILE("bindingVariantCopy.qml")); MyTypeObject *object = qobject_cast(component.create()); QVERIFY(object != 0); @@ -536,6 +537,23 @@ void tst_valuetypes::variantCopy() delete object; } +// Test that value types can be assigned to another value type property in script +void tst_valuetypes::scriptVariantCopy() +{ + QmlComponent component(&engine, TEST_FILE("scriptVariantCopy.qml")); + MyTypeObject *object = qobject_cast(component.create()); + QVERIFY(object != 0); + + QCOMPARE(object->rect(), QRect(2, 3, 109, 102)); + + object->emitRunScript(); + + QCOMPARE(object->rect(), QRect(19, 33, 5, 99)); + + delete object; +} + + // Test that the value type classes can be used manually void tst_valuetypes::cppClasses() { -- cgit v0.12