From f5bb807d6938fbd39ea1f99ed45c817d538e4287 Mon Sep 17 00:00:00 2001 From: Aaron Kennedy Date: Thu, 25 Feb 2010 20:01:25 +1000 Subject: Rename QDeclarativeMetaProperty -> QDeclarativeProperty There's nothing meta about our properties. --- tests/auto/declarative/declarative.pro | 2 +- .../qdeclarativedebug/tst_qdeclarativedebug.cpp | 6 +- .../declarative/qdeclarativelanguage/testtypes.h | 6 +- .../tst_qdeclarativelanguage.cpp | 10 +- .../tst_qdeclarativelistreference.cpp | 12 +- .../qdeclarativemetaproperty.pro | 5 - .../tst_qdeclarativemetaproperty.cpp | 1186 -------------------- .../tst_qdeclarativemetatype.cpp | 4 +- .../qdeclarativeproperty/qdeclarativeproperty.pro | 5 + .../tst_qdeclarativeproperty.cpp | 1186 ++++++++++++++++++++ .../declarative/qdeclarativevaluetypes/testtypes.h | 12 +- 11 files changed, 1217 insertions(+), 1217 deletions(-) delete mode 100644 tests/auto/declarative/qdeclarativemetaproperty/qdeclarativemetaproperty.pro delete mode 100644 tests/auto/declarative/qdeclarativemetaproperty/tst_qdeclarativemetaproperty.cpp create mode 100644 tests/auto/declarative/qdeclarativeproperty/qdeclarativeproperty.pro create mode 100644 tests/auto/declarative/qdeclarativeproperty/tst_qdeclarativeproperty.cpp (limited to 'tests/auto') diff --git a/tests/auto/declarative/declarative.pro b/tests/auto/declarative/declarative.pro index 669213ccb0..42ff523d8f 100644 --- a/tests/auto/declarative/declarative.pro +++ b/tests/auto/declarative/declarative.pro @@ -41,7 +41,7 @@ SUBDIRS += \ qdeclarativelanguage \ # Cover qdeclarativelistreference \ # Cover qdeclarativelistmodel \ # Cover - qdeclarativemetaproperty \ # Cover + qdeclarativeproperty \ # Cover qdeclarativemetatype \ # Cover qdeclarativemoduleplugin \ # Cover qdeclarativenumberformatter \ # Cover diff --git a/tests/auto/declarative/qdeclarativedebug/tst_qdeclarativedebug.cpp b/tests/auto/declarative/qdeclarativedebug/tst_qdeclarativedebug.cpp index 8d5a1f65a7..0c3ca765bd 100644 --- a/tests/auto/declarative/qdeclarativedebug/tst_qdeclarativedebug.cpp +++ b/tests/auto/declarative/qdeclarativedebug/tst_qdeclarativedebug.cpp @@ -49,7 +49,7 @@ #include #include #include -#include +#include #include #include @@ -58,7 +58,7 @@ #include #include #include -#include +#include #include "../shared/debugutil_p.h" @@ -218,7 +218,7 @@ void tst_QDeclarativeDebug::recursiveObjectTest(QObject *o, const QDeclarativeDe QCOMPARE(p.valueTypeName(), QString::fromUtf8(pmeta.typeName())); QDeclarativeAbstractBinding *binding = - QDeclarativeMetaPropertyPrivate::binding(QDeclarativeMetaProperty(o, p.name())); + QDeclarativePropertyPrivate::binding(QDeclarativeProperty(o, p.name())); if (binding) QCOMPARE(binding->expression(), p.binding()); diff --git a/tests/auto/declarative/qdeclarativelanguage/testtypes.h b/tests/auto/declarative/qdeclarativelanguage/testtypes.h index fc1ede750e..8ac7aa6e60 100644 --- a/tests/auto/declarative/qdeclarativelanguage/testtypes.h +++ b/tests/auto/declarative/qdeclarativelanguage/testtypes.h @@ -52,7 +52,7 @@ #include #include #include -#include +#include QVariant myCustomVariantTypeConverter(const QString &data); @@ -480,8 +480,8 @@ public: MyPropertyValueSource() : QDeclarativePropertyValueSource() {} - QDeclarativeMetaProperty prop; - virtual void setTarget(const QDeclarativeMetaProperty &p) + QDeclarativeProperty prop; + virtual void setTarget(const QDeclarativeProperty &p) { prop = p; } diff --git a/tests/auto/declarative/qdeclarativelanguage/tst_qdeclarativelanguage.cpp b/tests/auto/declarative/qdeclarativelanguage/tst_qdeclarativelanguage.cpp index 29077579f2..1ba4454d73 100644 --- a/tests/auto/declarative/qdeclarativelanguage/tst_qdeclarativelanguage.cpp +++ b/tests/auto/declarative/qdeclarativelanguage/tst_qdeclarativelanguage.cpp @@ -46,7 +46,7 @@ #include #include -#include +#include #include #include "testtypes.h" @@ -747,14 +747,14 @@ void tst_qdeclarativelanguage::valueTypes() // ### #if 0 - QDeclarativeMetaProperty p(object, "rectProperty.x"); + QDeclarativeProperty p(object, "rectProperty.x"); QCOMPARE(p.read(), QVariant(12)); p.write(13); QCOMPARE(p.read(), QVariant(13)); - quint32 r = QDeclarativeMetaPropertyPrivate::saveValueType(p.coreIndex(), p.valueTypeCoreIndex()); - QDeclarativeMetaProperty p2; - QDeclarativeMetaPropertyPrivate::restore(p2, r, object); + quint32 r = QDeclarativePropertyPrivate::saveValueType(p.coreIndex(), p.valueTypeCoreIndex()); + QDeclarativeProperty p2; + QDeclarativePropertyPrivate::restore(p2, r, object); QCOMPARE(p2.read(), QVariant(13)); #endif } diff --git a/tests/auto/declarative/qdeclarativelistreference/tst_qdeclarativelistreference.cpp b/tests/auto/declarative/qdeclarativelistreference/tst_qdeclarativelistreference.cpp index fdcb98a860..1b7af19c48 100644 --- a/tests/auto/declarative/qdeclarativelistreference/tst_qdeclarativelistreference.cpp +++ b/tests/auto/declarative/qdeclarativelistreference/tst_qdeclarativelistreference.cpp @@ -47,7 +47,7 @@ #include #include #include -#include +#include #include inline QUrl TEST_FILE(const QString &filename) @@ -530,7 +530,7 @@ void tst_qdeclarativelistreference::qmlmetaproperty() tt.data.append(0); tt.data.append(&tt); - QDeclarativeMetaProperty prop(&tt, QLatin1String("data")); + QDeclarativeProperty prop(&tt, QLatin1String("data")); QVariant v = prop.read(); QVERIFY(v.userType() == qMetaTypeId()); QDeclarativeListReference ref = qvariant_cast(v); @@ -546,11 +546,11 @@ void tst_qdeclarativelistreference::engineTypes() QObject *o = component.create(); QVERIFY(o); - QDeclarativeMetaProperty p1(o, QLatin1String("myList")); - QVERIFY(p1.propertyTypeCategory() == QDeclarativeMetaProperty::Normal); + QDeclarativeProperty p1(o, QLatin1String("myList")); + QVERIFY(p1.propertyTypeCategory() == QDeclarativeProperty::Normal); - QDeclarativeMetaProperty p2(o, QLatin1String("myList"), engine.rootContext()); - QVERIFY(p2.propertyTypeCategory() == QDeclarativeMetaProperty::List); + QDeclarativeProperty p2(o, QLatin1String("myList"), engine.rootContext()); + QVERIFY(p2.propertyTypeCategory() == QDeclarativeProperty::List); QVariant v = p2.read(); QVERIFY(v.userType() == qMetaTypeId()); QDeclarativeListReference ref = qvariant_cast(v); diff --git a/tests/auto/declarative/qdeclarativemetaproperty/qdeclarativemetaproperty.pro b/tests/auto/declarative/qdeclarativemetaproperty/qdeclarativemetaproperty.pro deleted file mode 100644 index 7170469331..0000000000 --- a/tests/auto/declarative/qdeclarativemetaproperty/qdeclarativemetaproperty.pro +++ /dev/null @@ -1,5 +0,0 @@ -load(qttest_p4) -contains(QT_CONFIG,declarative): QT += declarative -macx:CONFIG -= app_bundle - -SOURCES += tst_qdeclarativemetaproperty.cpp diff --git a/tests/auto/declarative/qdeclarativemetaproperty/tst_qdeclarativemetaproperty.cpp b/tests/auto/declarative/qdeclarativemetaproperty/tst_qdeclarativemetaproperty.cpp deleted file mode 100644 index a7e9d0d57b..0000000000 --- a/tests/auto/declarative/qdeclarativemetaproperty/tst_qdeclarativemetaproperty.cpp +++ /dev/null @@ -1,1186 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). -** All rights reserved. -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** No Commercial Usage -** This file contains pre-release code and may not be distributed. -** You may use this file in accordance with the terms and conditions -** contained in the Technology Preview License Agreement accompanying -** this package. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -** -** -** -** -** -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ -#include -#include -#include -#include -#include -#include -#include -#include - -class MyQmlObject : public QObject -{ - Q_OBJECT -public: - MyQmlObject() {} -}; - -QML_DECLARE_TYPE(MyQmlObject); - -class MyAttached : public QObject -{ - Q_OBJECT - Q_PROPERTY(int foo READ foo WRITE setFoo) -public: - MyAttached(QObject *parent) : QObject(parent), m_foo(13) {} - - int foo() const { return m_foo; } - void setFoo(int f) { m_foo = f; } - -private: - int m_foo; -}; - -class MyContainer : public QObject -{ - Q_OBJECT - Q_PROPERTY(QDeclarativeListProperty children READ children) -public: - MyContainer() {} - - QDeclarativeListProperty children() { return QDeclarativeListProperty(this, m_children); } - - static MyAttached *qmlAttachedProperties(QObject *o) { - return new MyAttached(o); - } - -private: - QList m_children; -}; - -QML_DECLARE_TYPE(MyContainer); -QML_DECLARE_TYPEINFO(MyContainer, QML_HAS_ATTACHED_PROPERTIES) - -class tst_qdeclarativemetaproperty : public QObject -{ - Q_OBJECT -public: - tst_qdeclarativemetaproperty() {} - -private slots: - void initTestCase(); - - // Constructors - void qmlmetaproperty(); - void qmlmetaproperty_object(); - void qmlmetaproperty_object_string(); - void qmlmetaproperty_object_context(); - void qmlmetaproperty_object_string_context(); - - // Methods - void name(); - void read(); - void write(); - void reset(); - - // Functionality - void writeObjectToList(); - void writeListToList(); - - //writeToReadOnly(); - - // Bugs - void crashOnValueProperty(); - - void copy(); -private: - QDeclarativeEngine engine; -}; - -void tst_qdeclarativemetaproperty::qmlmetaproperty() -{ - QDeclarativeMetaProperty prop; - - QGuard binding(new QDeclarativeBinding(QLatin1String("null"), 0, engine.rootContext())); - QVERIFY(binding != 0); - QGuard expression(new QDeclarativeExpression()); - QVERIFY(expression != 0); - - QObject *obj = new QObject; - - QCOMPARE(prop.name(), QString()); - QCOMPARE(prop.read(), QVariant()); - QCOMPARE(prop.write(QVariant()), false); - QCOMPARE(prop.hasChangedNotifier(), false); - QCOMPARE(prop.needsChangedNotifier(), false); - QCOMPARE(prop.connectNotifier(0, SLOT(deleteLater())), false); - QCOMPARE(prop.connectNotifier(obj, SLOT(deleteLater())), false); - QCOMPARE(prop.connectNotifier(obj, 0), false); - QCOMPARE(prop.connectNotifier(0, obj->metaObject()->indexOfMethod("deleteLater()")), false); - QCOMPARE(prop.connectNotifier(obj, obj->metaObject()->indexOfMethod("deleteLater()")), false); - QCOMPARE(prop.connectNotifier(obj, -1), false); - QVERIFY(prop.method().signature() == 0); - QCOMPARE(prop.type(), QDeclarativeMetaProperty::Invalid); - QCOMPARE(prop.isProperty(), false); - QCOMPARE(prop.isDefault(), false); - QCOMPARE(prop.isWritable(), false); - QCOMPARE(prop.isDesignable(), false); - QCOMPARE(prop.isResettable(), false); - QCOMPARE(prop.isValid(), false); - QCOMPARE(prop.object(), (QObject *)0); - QCOMPARE(prop.propertyTypeCategory(), QDeclarativeMetaProperty::InvalidCategory); - QCOMPARE(prop.propertyType(), 0); - QCOMPARE(prop.propertyTypeName(), (const char *)0); - QVERIFY(prop.property().name() == 0); - QVERIFY(QDeclarativeMetaPropertyPrivate::binding(prop) == 0); - QVERIFY(QDeclarativeMetaPropertyPrivate::setBinding(prop, binding) == 0); - QVERIFY(binding == 0); - QVERIFY(QDeclarativeMetaPropertyPrivate::signalExpression(prop) == 0); - QVERIFY(QDeclarativeMetaPropertyPrivate::setSignalExpression(prop, expression) == 0); - QVERIFY(expression == 0); - QCOMPARE(prop.coreIndex(), -1); - QCOMPARE(QDeclarativeMetaPropertyPrivate::valueTypeCoreIndex(prop), -1); - - delete obj; -} - -class PropertyObject : public QObject -{ - Q_OBJECT - Q_PROPERTY(int defaultProperty READ defaultProperty); - Q_PROPERTY(QRect rectProperty READ rectProperty); - Q_PROPERTY(QRect wrectProperty READ wrectProperty WRITE setWRectProperty); - Q_PROPERTY(QUrl url READ url WRITE setUrl); - Q_PROPERTY(int resettableProperty READ resettableProperty WRITE setResettableProperty RESET resetProperty); - - Q_CLASSINFO("DefaultProperty", "defaultProperty"); -public: - PropertyObject() : m_resetProperty(9) {} - - int defaultProperty() { return 10; } - QRect rectProperty() { return QRect(10, 10, 1, 209); } - - QRect wrectProperty() { return m_rect; } - void setWRectProperty(const QRect &r) { m_rect = r; } - - QUrl url() { return m_url; } - void setUrl(const QUrl &u) { m_url = u; } - - int resettableProperty() const { return m_resetProperty; } - void setResettableProperty(int r) { m_resetProperty = r; } - void resetProperty() { m_resetProperty = 9; } - -signals: - void clicked(); - -private: - int m_resetProperty; - QRect m_rect; - QUrl m_url; -}; - -QML_DECLARE_TYPE(PropertyObject); - -void tst_qdeclarativemetaproperty::qmlmetaproperty_object() -{ - QObject object; // Has no default property - PropertyObject dobject; // Has default property - - { - QDeclarativeMetaProperty prop(&object); - - QGuard binding(new QDeclarativeBinding(QLatin1String("null"), 0, engine.rootContext())); - QVERIFY(binding != 0); - QGuard expression(new QDeclarativeExpression()); - QVERIFY(expression != 0); - - QObject *obj = new QObject; - - QCOMPARE(prop.name(), QString()); - QCOMPARE(prop.read(), QVariant()); - QCOMPARE(prop.write(QVariant()), false); - QCOMPARE(prop.hasChangedNotifier(), false); - QCOMPARE(prop.needsChangedNotifier(), false); - QCOMPARE(prop.connectNotifier(0, SLOT(deleteLater())), false); - QCOMPARE(prop.connectNotifier(obj, SLOT(deleteLater())), false); - QCOMPARE(prop.connectNotifier(obj, 0), false); - QCOMPARE(prop.connectNotifier(0, obj->metaObject()->indexOfMethod("deleteLater()")), false); - QCOMPARE(prop.connectNotifier(obj, obj->metaObject()->indexOfMethod("deleteLater()")), false); - QCOMPARE(prop.connectNotifier(obj, -1), false); - QVERIFY(prop.method().signature() == 0); - QCOMPARE(prop.type(), QDeclarativeMetaProperty::Invalid); - QCOMPARE(prop.isProperty(), false); - QCOMPARE(prop.isDefault(), false); - QCOMPARE(prop.isWritable(), false); - QCOMPARE(prop.isDesignable(), false); - QCOMPARE(prop.isResettable(), false); - QCOMPARE(prop.isValid(), false); - QCOMPARE(prop.object(), (QObject *)0); - QCOMPARE(prop.propertyTypeCategory(), QDeclarativeMetaProperty::InvalidCategory); - QCOMPARE(prop.propertyType(), 0); - QCOMPARE(prop.propertyTypeName(), (const char *)0); - QVERIFY(prop.property().name() == 0); - QVERIFY(QDeclarativeMetaPropertyPrivate::binding(prop) == 0); - QVERIFY(QDeclarativeMetaPropertyPrivate::setBinding(prop, binding) == 0); - QVERIFY(binding == 0); - QVERIFY(QDeclarativeMetaPropertyPrivate::signalExpression(prop) == 0); - QVERIFY(QDeclarativeMetaPropertyPrivate::setSignalExpression(prop, expression) == 0); - QVERIFY(expression == 0); - QCOMPARE(prop.coreIndex(), -1); - QCOMPARE(QDeclarativeMetaPropertyPrivate::valueTypeCoreIndex(prop), -1); - - delete obj; - } - - { - QDeclarativeMetaProperty prop(&dobject); - - QGuard binding(new QDeclarativeBinding(QLatin1String("null"), 0, engine.rootContext())); - binding->setTarget(prop); - QVERIFY(binding != 0); - QGuard expression(new QDeclarativeExpression()); - QVERIFY(expression != 0); - - QObject *obj = new QObject; - - QCOMPARE(prop.name(), QString("defaultProperty")); - QCOMPARE(prop.read(), QVariant(10)); - QCOMPARE(prop.write(QVariant()), false); - QCOMPARE(prop.hasChangedNotifier(), false); - QCOMPARE(prop.needsChangedNotifier(), true); - QCOMPARE(prop.connectNotifier(0, SLOT(deleteLater())), false); - QCOMPARE(prop.connectNotifier(obj, SLOT(deleteLater())), false); - QCOMPARE(prop.connectNotifier(obj, 0), false); - QCOMPARE(prop.connectNotifier(0, obj->metaObject()->indexOfMethod("deleteLater()")), false); - QCOMPARE(prop.connectNotifier(obj, obj->metaObject()->indexOfMethod("deleteLater()")), false); - QCOMPARE(prop.connectNotifier(obj, -1), false); - QVERIFY(prop.method().signature() == 0); - QCOMPARE(prop.type(), (QDeclarativeMetaProperty::Type)(QDeclarativeMetaProperty::Property | QDeclarativeMetaProperty::Default)); - QCOMPARE(prop.isProperty(), true); - QCOMPARE(prop.isDefault(), true); - QCOMPARE(prop.isWritable(), false); - QCOMPARE(prop.isDesignable(), true); - QCOMPARE(prop.isResettable(), false); - QCOMPARE(prop.isValid(), true); - QCOMPARE(prop.object(), &dobject); - QCOMPARE(prop.propertyTypeCategory(), QDeclarativeMetaProperty::Normal); - QCOMPARE(prop.propertyType(), (int)QVariant::Int); - QCOMPARE(prop.propertyTypeName(), "int"); - QCOMPARE(QString(prop.property().name()), QString("defaultProperty")); - QVERIFY(QDeclarativeMetaPropertyPrivate::binding(prop) == 0); - QTest::ignoreMessage(QtWarningMsg, ":-1: Unable to assign null to int"); - QVERIFY(QDeclarativeMetaPropertyPrivate::setBinding(prop, binding) == 0); - QVERIFY(binding != 0); - QVERIFY(QDeclarativeMetaPropertyPrivate::binding(prop) == binding); - QVERIFY(QDeclarativeMetaPropertyPrivate::signalExpression(prop) == 0); - QVERIFY(QDeclarativeMetaPropertyPrivate::setSignalExpression(prop, expression) == 0); - QVERIFY(expression == 0); - QCOMPARE(prop.coreIndex(), dobject.metaObject()->indexOfProperty("defaultProperty")); - QCOMPARE(QDeclarativeMetaPropertyPrivate::valueTypeCoreIndex(prop), -1); - - delete obj; - } -} - -void tst_qdeclarativemetaproperty::qmlmetaproperty_object_string() -{ - QObject object; - PropertyObject dobject; - - { - QDeclarativeMetaProperty prop(&object, QString("defaultProperty")); - - QGuard binding(new QDeclarativeBinding(QLatin1String("null"), 0, engine.rootContext())); - QVERIFY(binding != 0); - QGuard expression(new QDeclarativeExpression()); - QVERIFY(expression != 0); - - QObject *obj = new QObject; - - QCOMPARE(prop.name(), QString()); - QCOMPARE(prop.read(), QVariant()); - QCOMPARE(prop.write(QVariant()), false); - QCOMPARE(prop.hasChangedNotifier(), false); - QCOMPARE(prop.needsChangedNotifier(), false); - QCOMPARE(prop.connectNotifier(0, SLOT(deleteLater())), false); - QCOMPARE(prop.connectNotifier(obj, SLOT(deleteLater())), false); - QCOMPARE(prop.connectNotifier(obj, 0), false); - QCOMPARE(prop.connectNotifier(0, obj->metaObject()->indexOfMethod("deleteLater()")), false); - QCOMPARE(prop.connectNotifier(obj, obj->metaObject()->indexOfMethod("deleteLater()")), false); - QCOMPARE(prop.connectNotifier(obj, -1), false); - QVERIFY(prop.method().signature() == 0); - QCOMPARE(prop.type(), QDeclarativeMetaProperty::Invalid); - QCOMPARE(prop.isProperty(), false); - QCOMPARE(prop.isDefault(), false); - QCOMPARE(prop.isWritable(), false); - QCOMPARE(prop.isDesignable(), false); - QCOMPARE(prop.isResettable(), false); - QCOMPARE(prop.isValid(), false); - QCOMPARE(prop.object(), (QObject *)0); - QCOMPARE(prop.propertyTypeCategory(), QDeclarativeMetaProperty::InvalidCategory); - QCOMPARE(prop.propertyType(), 0); - QCOMPARE(prop.propertyTypeName(), (const char *)0); - QVERIFY(prop.property().name() == 0); - QVERIFY(QDeclarativeMetaPropertyPrivate::binding(prop) == 0); - QVERIFY(QDeclarativeMetaPropertyPrivate::setBinding(prop, binding) == 0); - QVERIFY(binding == 0); - QVERIFY(QDeclarativeMetaPropertyPrivate::signalExpression(prop) == 0); - QVERIFY(QDeclarativeMetaPropertyPrivate::setSignalExpression(prop, expression) == 0); - QVERIFY(expression == 0); - QCOMPARE(prop.coreIndex(), -1); - QCOMPARE(QDeclarativeMetaPropertyPrivate::valueTypeCoreIndex(prop), -1); - - delete obj; - } - - { - QDeclarativeMetaProperty prop(&dobject, QString("defaultProperty")); - - QGuard binding(new QDeclarativeBinding(QLatin1String("null"), 0, engine.rootContext())); - binding->setTarget(prop); - QVERIFY(binding != 0); - QGuard expression(new QDeclarativeExpression()); - QVERIFY(expression != 0); - - QObject *obj = new QObject; - - QCOMPARE(prop.name(), QString("defaultProperty")); - QCOMPARE(prop.read(), QVariant(10)); - QCOMPARE(prop.write(QVariant()), false); - QCOMPARE(prop.hasChangedNotifier(), false); - QCOMPARE(prop.needsChangedNotifier(), true); - QCOMPARE(prop.connectNotifier(0, SLOT(deleteLater())), false); - QCOMPARE(prop.connectNotifier(obj, SLOT(deleteLater())), false); - QCOMPARE(prop.connectNotifier(obj, 0), false); - QCOMPARE(prop.connectNotifier(0, obj->metaObject()->indexOfMethod("deleteLater()")), false); - QCOMPARE(prop.connectNotifier(obj, obj->metaObject()->indexOfMethod("deleteLater()")), false); - QCOMPARE(prop.connectNotifier(obj, -1), false); - QVERIFY(prop.method().signature() == 0); - QCOMPARE(prop.type(), QDeclarativeMetaProperty::Property); - QCOMPARE(prop.isProperty(), true); - QCOMPARE(prop.isDefault(), false); - QCOMPARE(prop.isWritable(), false); - QCOMPARE(prop.isDesignable(), true); - QCOMPARE(prop.isResettable(), false); - QCOMPARE(prop.isValid(), true); - QCOMPARE(prop.object(), &dobject); - QCOMPARE(prop.propertyTypeCategory(), QDeclarativeMetaProperty::Normal); - QCOMPARE(prop.propertyType(), (int)QVariant::Int); - QCOMPARE(prop.propertyTypeName(), "int"); - QCOMPARE(QString(prop.property().name()), QString("defaultProperty")); - QVERIFY(QDeclarativeMetaPropertyPrivate::binding(prop) == 0); - QTest::ignoreMessage(QtWarningMsg, ":-1: Unable to assign null to int"); - QVERIFY(QDeclarativeMetaPropertyPrivate::setBinding(prop, binding) == 0); - QVERIFY(binding != 0); - QVERIFY(QDeclarativeMetaPropertyPrivate::binding(prop) == binding); - QVERIFY(QDeclarativeMetaPropertyPrivate::signalExpression(prop) == 0); - QVERIFY(QDeclarativeMetaPropertyPrivate::setSignalExpression(prop, expression) == 0); - QVERIFY(expression == 0); - QCOMPARE(prop.coreIndex(), dobject.metaObject()->indexOfProperty("defaultProperty")); - QCOMPARE(QDeclarativeMetaPropertyPrivate::valueTypeCoreIndex(prop), -1); - - delete obj; - } - - { - QDeclarativeMetaProperty prop(&dobject, QString("onClicked")); - - QGuard binding(new QDeclarativeBinding(QLatin1String("null"), 0, engine.rootContext())); - binding->setTarget(prop); - QVERIFY(binding != 0); - QGuard expression(new QDeclarativeExpression()); - QVERIFY(expression != 0); - - QObject *obj = new QObject; - - QCOMPARE(prop.name(), QString("onClicked")); - QCOMPARE(prop.read(), QVariant()); - QCOMPARE(prop.write(QVariant("Hello")), false); - QCOMPARE(prop.hasChangedNotifier(), false); - QCOMPARE(prop.needsChangedNotifier(), false); - QCOMPARE(prop.connectNotifier(0, SLOT(deleteLater())), false); - QCOMPARE(prop.connectNotifier(obj, SLOT(deleteLater())), false); - QCOMPARE(prop.connectNotifier(obj, 0), false); - QCOMPARE(prop.connectNotifier(0, obj->metaObject()->indexOfMethod("deleteLater()")), false); - QCOMPARE(prop.connectNotifier(obj, obj->metaObject()->indexOfMethod("deleteLater()")), false); - QCOMPARE(prop.connectNotifier(obj, -1), false); - QCOMPARE(QString(prop.method().signature()), QString("clicked()")); - QCOMPARE(prop.type(), QDeclarativeMetaProperty::SignalProperty); - QCOMPARE(prop.isProperty(), false); - QCOMPARE(prop.isDefault(), false); - QCOMPARE(prop.isWritable(), false); - QCOMPARE(prop.isDesignable(), false); - QCOMPARE(prop.isResettable(), false); - QCOMPARE(prop.isValid(), true); - QCOMPARE(prop.object(), &dobject); - QCOMPARE(prop.propertyTypeCategory(), QDeclarativeMetaProperty::InvalidCategory); - QCOMPARE(prop.propertyType(), 0); - QCOMPARE(prop.propertyTypeName(), (const char *)0); - QCOMPARE(prop.property().name(), (const char *)0); - QVERIFY(QDeclarativeMetaPropertyPrivate::binding(prop) == 0); - QVERIFY(QDeclarativeMetaPropertyPrivate::setBinding(prop, binding) == 0); - QVERIFY(binding == 0); - QVERIFY(QDeclarativeMetaPropertyPrivate::signalExpression(prop) == 0); - QVERIFY(QDeclarativeMetaPropertyPrivate::setSignalExpression(prop, expression) == 0); - QVERIFY(expression != 0); - QVERIFY(QDeclarativeMetaPropertyPrivate::signalExpression(prop) == expression); - QCOMPARE(prop.coreIndex(), dobject.metaObject()->indexOfMethod("clicked()")); - QCOMPARE(QDeclarativeMetaPropertyPrivate::valueTypeCoreIndex(prop), -1); - - delete obj; - } -} - -void tst_qdeclarativemetaproperty::qmlmetaproperty_object_context() -{ - QObject object; // Has no default property - PropertyObject dobject; // Has default property - - { - QDeclarativeMetaProperty prop(&object, engine.rootContext()); - - QGuard binding(new QDeclarativeBinding(QLatin1String("null"), 0, engine.rootContext())); - QVERIFY(binding != 0); - QGuard expression(new QDeclarativeExpression()); - QVERIFY(expression != 0); - - QObject *obj = new QObject; - - QCOMPARE(prop.name(), QString()); - QCOMPARE(prop.read(), QVariant()); - QCOMPARE(prop.write(QVariant()), false); - QCOMPARE(prop.hasChangedNotifier(), false); - QCOMPARE(prop.needsChangedNotifier(), false); - QCOMPARE(prop.connectNotifier(0, SLOT(deleteLater())), false); - QCOMPARE(prop.connectNotifier(obj, SLOT(deleteLater())), false); - QCOMPARE(prop.connectNotifier(obj, 0), false); - QCOMPARE(prop.connectNotifier(0, obj->metaObject()->indexOfMethod("deleteLater()")), false); - QCOMPARE(prop.connectNotifier(obj, obj->metaObject()->indexOfMethod("deleteLater()")), false); - QCOMPARE(prop.connectNotifier(obj, -1), false); - QVERIFY(prop.method().signature() == 0); - QCOMPARE(prop.type(), QDeclarativeMetaProperty::Invalid); - QCOMPARE(prop.isProperty(), false); - QCOMPARE(prop.isDefault(), false); - QCOMPARE(prop.isWritable(), false); - QCOMPARE(prop.isDesignable(), false); - QCOMPARE(prop.isResettable(), false); - QCOMPARE(prop.isValid(), false); - QCOMPARE(prop.object(), (QObject *)0); - QCOMPARE(prop.propertyTypeCategory(), QDeclarativeMetaProperty::InvalidCategory); - QCOMPARE(prop.propertyType(), 0); - QCOMPARE(prop.propertyTypeName(), (const char *)0); - QVERIFY(prop.property().name() == 0); - QVERIFY(QDeclarativeMetaPropertyPrivate::binding(prop) == 0); - QVERIFY(QDeclarativeMetaPropertyPrivate::setBinding(prop, binding) == 0); - QVERIFY(binding == 0); - QVERIFY(QDeclarativeMetaPropertyPrivate::signalExpression(prop) == 0); - QVERIFY(QDeclarativeMetaPropertyPrivate::setSignalExpression(prop, expression) == 0); - QVERIFY(expression == 0); - QCOMPARE(prop.coreIndex(), -1); - QCOMPARE(QDeclarativeMetaPropertyPrivate::valueTypeCoreIndex(prop), -1); - - delete obj; - } - - { - QDeclarativeMetaProperty prop(&dobject, engine.rootContext()); - - QGuard binding(new QDeclarativeBinding(QLatin1String("null"), 0, engine.rootContext())); - binding->setTarget(prop); - QVERIFY(binding != 0); - QGuard expression(new QDeclarativeExpression()); - QVERIFY(expression != 0); - - QObject *obj = new QObject; - - QCOMPARE(prop.name(), QString("defaultProperty")); - QCOMPARE(prop.read(), QVariant(10)); - QCOMPARE(prop.write(QVariant()), false); - QCOMPARE(prop.hasChangedNotifier(), false); - QCOMPARE(prop.needsChangedNotifier(), true); - QCOMPARE(prop.connectNotifier(0, SLOT(deleteLater())), false); - QCOMPARE(prop.connectNotifier(obj, SLOT(deleteLater())), false); - QCOMPARE(prop.connectNotifier(obj, 0), false); - QCOMPARE(prop.connectNotifier(0, obj->metaObject()->indexOfMethod("deleteLater()")), false); - QCOMPARE(prop.connectNotifier(obj, obj->metaObject()->indexOfMethod("deleteLater()")), false); - QCOMPARE(prop.connectNotifier(obj, -1), false); - QVERIFY(prop.method().signature() == 0); - QCOMPARE(prop.type(), (QDeclarativeMetaProperty::Type)(QDeclarativeMetaProperty::Property | QDeclarativeMetaProperty::Default)); - QCOMPARE(prop.isProperty(), true); - QCOMPARE(prop.isDefault(), true); - QCOMPARE(prop.isWritable(), false); - QCOMPARE(prop.isDesignable(), true); - QCOMPARE(prop.isResettable(), false); - QCOMPARE(prop.isValid(), true); - QCOMPARE(prop.object(), &dobject); - QCOMPARE(prop.propertyTypeCategory(), QDeclarativeMetaProperty::Normal); - QCOMPARE(prop.propertyType(), (int)QVariant::Int); - QCOMPARE(prop.propertyTypeName(), "int"); - QCOMPARE(QString(prop.property().name()), QString("defaultProperty")); - QVERIFY(QDeclarativeMetaPropertyPrivate::binding(prop) == 0); - QTest::ignoreMessage(QtWarningMsg, ":-1: Unable to assign null to int"); - QVERIFY(QDeclarativeMetaPropertyPrivate::setBinding(prop, binding) == 0); - QVERIFY(binding != 0); - QVERIFY(QDeclarativeMetaPropertyPrivate::binding(prop) == binding); - QVERIFY(QDeclarativeMetaPropertyPrivate::signalExpression(prop) == 0); - QVERIFY(QDeclarativeMetaPropertyPrivate::setSignalExpression(prop, expression) == 0); - QVERIFY(expression == 0); - QCOMPARE(prop.coreIndex(), dobject.metaObject()->indexOfProperty("defaultProperty")); - QCOMPARE(QDeclarativeMetaPropertyPrivate::valueTypeCoreIndex(prop), -1); - - delete obj; - } -} - -void tst_qdeclarativemetaproperty::qmlmetaproperty_object_string_context() -{ - QObject object; - PropertyObject dobject; - - { - QDeclarativeMetaProperty prop(&object, QString("defaultProperty"), engine.rootContext()); - - QGuard binding(new QDeclarativeBinding(QLatin1String("null"), 0, engine.rootContext())); - QVERIFY(binding != 0); - QGuard expression(new QDeclarativeExpression()); - QVERIFY(expression != 0); - - QObject *obj = new QObject; - - QCOMPARE(prop.name(), QString()); - QCOMPARE(prop.read(), QVariant()); - QCOMPARE(prop.write(QVariant()), false); - QCOMPARE(prop.hasChangedNotifier(), false); - QCOMPARE(prop.needsChangedNotifier(), false); - QCOMPARE(prop.connectNotifier(0, SLOT(deleteLater())), false); - QCOMPARE(prop.connectNotifier(obj, SLOT(deleteLater())), false); - QCOMPARE(prop.connectNotifier(obj, 0), false); - QCOMPARE(prop.connectNotifier(0, obj->metaObject()->indexOfMethod("deleteLater()")), false); - QCOMPARE(prop.connectNotifier(obj, obj->metaObject()->indexOfMethod("deleteLater()")), false); - QCOMPARE(prop.connectNotifier(obj, -1), false); - QVERIFY(prop.method().signature() == 0); - QCOMPARE(prop.type(), QDeclarativeMetaProperty::Invalid); - QCOMPARE(prop.isProperty(), false); - QCOMPARE(prop.isDefault(), false); - QCOMPARE(prop.isWritable(), false); - QCOMPARE(prop.isDesignable(), false); - QCOMPARE(prop.isResettable(), false); - QCOMPARE(prop.isValid(), false); - QCOMPARE(prop.object(), (QObject *)0); - QCOMPARE(prop.propertyTypeCategory(), QDeclarativeMetaProperty::InvalidCategory); - QCOMPARE(prop.propertyType(), 0); - QCOMPARE(prop.propertyTypeName(), (const char *)0); - QVERIFY(prop.property().name() == 0); - QVERIFY(QDeclarativeMetaPropertyPrivate::binding(prop) == 0); - QVERIFY(QDeclarativeMetaPropertyPrivate::setBinding(prop, binding) == 0); - QVERIFY(binding == 0); - QVERIFY(QDeclarativeMetaPropertyPrivate::signalExpression(prop) == 0); - QVERIFY(QDeclarativeMetaPropertyPrivate::setSignalExpression(prop, expression) == 0); - QVERIFY(expression == 0); - QCOMPARE(prop.coreIndex(), -1); - QCOMPARE(QDeclarativeMetaPropertyPrivate::valueTypeCoreIndex(prop), -1); - - delete obj; - } - - { - QDeclarativeMetaProperty prop(&dobject, QString("defaultProperty"), engine.rootContext()); - - QGuard binding(new QDeclarativeBinding(QLatin1String("null"), 0, engine.rootContext())); - binding->setTarget(prop); - QVERIFY(binding != 0); - QGuard expression(new QDeclarativeExpression()); - QVERIFY(expression != 0); - - QObject *obj = new QObject; - - QCOMPARE(prop.name(), QString("defaultProperty")); - QCOMPARE(prop.read(), QVariant(10)); - QCOMPARE(prop.write(QVariant()), false); - QCOMPARE(prop.hasChangedNotifier(), false); - QCOMPARE(prop.needsChangedNotifier(), true); - QCOMPARE(prop.connectNotifier(0, SLOT(deleteLater())), false); - QCOMPARE(prop.connectNotifier(obj, SLOT(deleteLater())), false); - QCOMPARE(prop.connectNotifier(obj, 0), false); - QCOMPARE(prop.connectNotifier(0, obj->metaObject()->indexOfMethod("deleteLater()")), false); - QCOMPARE(prop.connectNotifier(obj, obj->metaObject()->indexOfMethod("deleteLater()")), false); - QCOMPARE(prop.connectNotifier(obj, -1), false); - QVERIFY(prop.method().signature() == 0); - QCOMPARE(prop.type(), QDeclarativeMetaProperty::Property); - QCOMPARE(prop.isProperty(), true); - QCOMPARE(prop.isDefault(), false); - QCOMPARE(prop.isWritable(), false); - QCOMPARE(prop.isDesignable(), true); - QCOMPARE(prop.isResettable(), false); - QCOMPARE(prop.isValid(), true); - QCOMPARE(prop.object(), &dobject); - QCOMPARE(prop.propertyTypeCategory(), QDeclarativeMetaProperty::Normal); - QCOMPARE(prop.propertyType(), (int)QVariant::Int); - QCOMPARE(prop.propertyTypeName(), "int"); - QCOMPARE(QString(prop.property().name()), QString("defaultProperty")); - QVERIFY(QDeclarativeMetaPropertyPrivate::binding(prop) == 0); - QTest::ignoreMessage(QtWarningMsg, ":-1: Unable to assign null to int"); - QVERIFY(QDeclarativeMetaPropertyPrivate::setBinding(prop, binding) == 0); - QVERIFY(binding != 0); - QVERIFY(QDeclarativeMetaPropertyPrivate::binding(prop) == binding); - QVERIFY(QDeclarativeMetaPropertyPrivate::signalExpression(prop) == 0); - QVERIFY(QDeclarativeMetaPropertyPrivate::setSignalExpression(prop, expression) == 0); - QVERIFY(expression == 0); - QCOMPARE(prop.coreIndex(), dobject.metaObject()->indexOfProperty("defaultProperty")); - QCOMPARE(QDeclarativeMetaPropertyPrivate::valueTypeCoreIndex(prop), -1); - - delete obj; - } - - { - QDeclarativeMetaProperty prop(&dobject, QString("onClicked"), engine.rootContext()); - - QGuard binding(new QDeclarativeBinding(QLatin1String("null"), 0, engine.rootContext())); - binding->setTarget(prop); - QVERIFY(binding != 0); - QGuard expression(new QDeclarativeExpression()); - QVERIFY(expression != 0); - - QObject *obj = new QObject; - - QCOMPARE(prop.name(), QString("onClicked")); - QCOMPARE(prop.read(), QVariant()); - QCOMPARE(prop.write(QVariant("Hello")), false); - QCOMPARE(prop.hasChangedNotifier(), false); - QCOMPARE(prop.needsChangedNotifier(), false); - QCOMPARE(prop.connectNotifier(0, SLOT(deleteLater())), false); - QCOMPARE(prop.connectNotifier(obj, SLOT(deleteLater())), false); - QCOMPARE(prop.connectNotifier(obj, 0), false); - QCOMPARE(prop.connectNotifier(0, obj->metaObject()->indexOfMethod("deleteLater()")), false); - QCOMPARE(prop.connectNotifier(obj, obj->metaObject()->indexOfMethod("deleteLater()")), false); - QCOMPARE(prop.connectNotifier(obj, -1), false); - QCOMPARE(QString(prop.method().signature()), QString("clicked()")); - QCOMPARE(prop.type(), QDeclarativeMetaProperty::SignalProperty); - QCOMPARE(prop.isProperty(), false); - QCOMPARE(prop.isDefault(), false); - QCOMPARE(prop.isWritable(), false); - QCOMPARE(prop.isDesignable(), false); - QCOMPARE(prop.isResettable(), false); - QCOMPARE(prop.isValid(), true); - QCOMPARE(prop.object(), &dobject); - QCOMPARE(prop.propertyTypeCategory(), QDeclarativeMetaProperty::InvalidCategory); - QCOMPARE(prop.propertyType(), 0); - QCOMPARE(prop.propertyTypeName(), (const char *)0); - QCOMPARE(prop.property().name(), (const char *)0); - QVERIFY(QDeclarativeMetaPropertyPrivate::binding(prop) == 0); - QVERIFY(QDeclarativeMetaPropertyPrivate::setBinding(prop, binding) == 0); - QVERIFY(binding == 0); - QVERIFY(QDeclarativeMetaPropertyPrivate::signalExpression(prop) == 0); - QVERIFY(QDeclarativeMetaPropertyPrivate::setSignalExpression(prop, expression) == 0); - QVERIFY(expression != 0); - QVERIFY(QDeclarativeMetaPropertyPrivate::signalExpression(prop) == expression); - QCOMPARE(prop.coreIndex(), dobject.metaObject()->indexOfMethod("clicked()")); - QCOMPARE(QDeclarativeMetaPropertyPrivate::valueTypeCoreIndex(prop), -1); - - delete obj; - } -} - -void tst_qdeclarativemetaproperty::name() -{ - { - QDeclarativeMetaProperty p; - QCOMPARE(p.name(), QString()); - } - - { - PropertyObject o; - QDeclarativeMetaProperty p(&o); - QCOMPARE(p.name(), QString("defaultProperty")); - } - - { - QObject o; - QDeclarativeMetaProperty p(&o, QString("objectName")); - QCOMPARE(p.name(), QString("objectName")); - } - - { - PropertyObject o; - QDeclarativeMetaProperty p(&o, "onClicked"); - QCOMPARE(p.name(), QString("onClicked")); - } - - { - QObject o; - QDeclarativeMetaProperty p(&o, "onClicked"); - QCOMPARE(p.name(), QString()); - } - - { - QObject o; - QDeclarativeMetaProperty p(&o, "foo"); - QCOMPARE(p.name(), QString()); - } - - { - QDeclarativeMetaProperty p(0, "foo"); - QCOMPARE(p.name(), QString()); - } - - { - PropertyObject o; - QDeclarativeMetaProperty p(&o, "rectProperty"); - QCOMPARE(p.name(), QString("rectProperty")); - } - - { - PropertyObject o; - QDeclarativeMetaProperty p(&o, "rectProperty.x"); - QCOMPARE(p.name(), QString("rectProperty.x")); - } - - { - PropertyObject o; - QDeclarativeMetaProperty p(&o, "rectProperty.foo"); - QCOMPARE(p.name(), QString()); - } -} - -void tst_qdeclarativemetaproperty::read() -{ - // Invalid - { - QDeclarativeMetaProperty p; - QCOMPARE(p.read(), QVariant()); - } - - // Default prop - { - PropertyObject o; - QDeclarativeMetaProperty p(&o); - QCOMPARE(p.read(), QVariant(10)); - } - - // Invalid default prop - { - QObject o; - QDeclarativeMetaProperty p(&o); - QCOMPARE(p.read(), QVariant()); - } - - // Value prop by name - { - QObject o; - - QDeclarativeMetaProperty p(&o, "objectName"); - QCOMPARE(p.read(), QVariant(QString())); - - o.setObjectName("myName"); - - QCOMPARE(p.read(), QVariant("myName")); - } - - // Value-type prop - { - PropertyObject o; - QDeclarativeMetaProperty p(&o, "rectProperty.x"); - QCOMPARE(p.read(), QVariant(10)); - } - - // Invalid value-type prop - { - PropertyObject o; - QDeclarativeMetaProperty p(&o, "rectProperty.foo"); - QCOMPARE(p.read(), QVariant()); - } - - // Signal property - { - PropertyObject o; - QDeclarativeMetaProperty p(&o, "onClicked"); - QCOMPARE(p.read(), QVariant()); - - QVERIFY(0 == QDeclarativeMetaPropertyPrivate::setSignalExpression(p, new QDeclarativeExpression())); - QVERIFY(0 != QDeclarativeMetaPropertyPrivate::signalExpression(p)); - - QCOMPARE(p.read(), QVariant()); - } - - // Deleted object - { - PropertyObject *o = new PropertyObject; - QDeclarativeMetaProperty p(o, "rectProperty.x"); - QCOMPARE(p.read(), QVariant(10)); - delete o; - QCOMPARE(p.read(), QVariant()); - } - - // Attached property - { - QDeclarativeComponent component(&engine); - component.setData("import Test 1.0\nMyContainer { }", QUrl()); - QObject *object = component.create(); - QVERIFY(object != 0); - - QDeclarativeMetaProperty p(object, "MyContainer.foo", qmlContext(object)); - QCOMPARE(p.read(), QVariant(13)); - delete object; - } - { - QDeclarativeComponent component(&engine); - component.setData("import Test 1.0\nMyContainer { MyContainer.foo: 10 }", QUrl()); - QObject *object = component.create(); - QVERIFY(object != 0); - - QDeclarativeMetaProperty p(object, "MyContainer.foo", qmlContext(object)); - QCOMPARE(p.read(), QVariant(10)); - delete object; - } - { - QDeclarativeComponent component(&engine); - component.setData("import Test 1.0 as Foo\nFoo.MyContainer { Foo.MyContainer.foo: 10 }", QUrl()); - QObject *object = component.create(); - QVERIFY(object != 0); - - QDeclarativeMetaProperty p(object, "Foo.MyContainer.foo", qmlContext(object)); - QCOMPARE(p.read(), QVariant(10)); - delete object; - } -} - -void tst_qdeclarativemetaproperty::write() -{ - // Invalid - { - QDeclarativeMetaProperty p; - QCOMPARE(p.write(QVariant(10)), false); - } - - // Read-only default prop - { - PropertyObject o; - QDeclarativeMetaProperty p(&o); - QCOMPARE(p.write(QVariant(10)), false); - } - - // Invalid default prop - { - QObject o; - QDeclarativeMetaProperty p(&o); - QCOMPARE(p.write(QVariant(10)), false); - } - - // Read-only prop by name - { - PropertyObject o; - QDeclarativeMetaProperty p(&o, QString("defaultProperty")); - QCOMPARE(p.write(QVariant(10)), false); - } - - // Writable prop by name - { - PropertyObject o; - QDeclarativeMetaProperty p(&o, QString("objectName")); - QCOMPARE(o.objectName(), QString()); - QCOMPARE(p.write(QVariant(QString("myName"))), true); - QCOMPARE(o.objectName(), QString("myName")); - } - - // Deleted object - { - PropertyObject *o = new PropertyObject; - QDeclarativeMetaProperty p(o, QString("objectName")); - QCOMPARE(p.write(QVariant(QString("myName"))), true); - QCOMPARE(o->objectName(), QString("myName")); - - delete o; - - QCOMPARE(p.write(QVariant(QString("myName"))), false); - } - - // Signal property - { - PropertyObject o; - QDeclarativeMetaProperty p(&o, "onClicked"); - QCOMPARE(p.write(QVariant("console.log(1921)")), false); - - QVERIFY(0 == QDeclarativeMetaPropertyPrivate::setSignalExpression(p, new QDeclarativeExpression())); - QVERIFY(0 != QDeclarativeMetaPropertyPrivate::signalExpression(p)); - - QCOMPARE(p.write(QVariant("console.log(1921)")), false); - - QVERIFY(0 != QDeclarativeMetaPropertyPrivate::signalExpression(p)); - } - - // Value-type property - { - PropertyObject o; - QDeclarativeMetaProperty p(&o, "wrectProperty"); - - QCOMPARE(o.wrectProperty(), QRect()); - QCOMPARE(p.write(QRect(1, 13, 99, 8)), true); - QCOMPARE(o.wrectProperty(), QRect(1, 13, 99, 8)); - - QDeclarativeMetaProperty p2(&o, "wrectProperty.x"); - QCOMPARE(p2.read(), QVariant(1)); - QCOMPARE(p2.write(QVariant(6)), true); - QCOMPARE(p2.read(), QVariant(6)); - QCOMPARE(o.wrectProperty(), QRect(6, 13, 99, 8)); - } - - // URL-property - { - PropertyObject o; - QDeclarativeMetaProperty p(&o, "url"); - - QCOMPARE(p.write(QUrl("main.qml")), true); - QCOMPARE(o.url(), QUrl("main.qml")); - - QDeclarativeMetaProperty p2(&o, "url", engine.rootContext()); - - QUrl result = engine.baseUrl().resolved(QUrl("main.qml")); - QVERIFY(result != QUrl("main.qml")); - - QCOMPARE(p2.write(QUrl("main.qml")), true); - QCOMPARE(o.url(), result); - } - - // Attached property - { - QDeclarativeComponent component(&engine); - component.setData("import Test 1.0\nMyContainer { }", QUrl()); - QObject *object = component.create(); - QVERIFY(object != 0); - - QDeclarativeMetaProperty p(object, "MyContainer.foo", qmlContext(object)); - p.write(QVariant(99)); - QCOMPARE(p.read(), QVariant(99)); - delete object; - } - { - QDeclarativeComponent component(&engine); - component.setData("import Test 1.0 as Foo\nFoo.MyContainer { Foo.MyContainer.foo: 10 }", QUrl()); - QObject *object = component.create(); - QVERIFY(object != 0); - - QDeclarativeMetaProperty p(object, "Foo.MyContainer.foo", qmlContext(object)); - p.write(QVariant(99)); - QCOMPARE(p.read(), QVariant(99)); - delete object; - } -} - -void tst_qdeclarativemetaproperty::reset() -{ - // Invalid - { - QDeclarativeMetaProperty p; - QCOMPARE(p.isResettable(), false); - QCOMPARE(p.reset(), false); - } - - // Read-only default prop - { - PropertyObject o; - QDeclarativeMetaProperty p(&o); - QCOMPARE(p.isResettable(), false); - QCOMPARE(p.reset(), false); - } - - // Invalid default prop - { - QObject o; - QDeclarativeMetaProperty p(&o); - QCOMPARE(p.isResettable(), false); - QCOMPARE(p.reset(), false); - } - - // Non-resettable-only prop by name - { - PropertyObject o; - QDeclarativeMetaProperty p(&o, QString("defaultProperty")); - QCOMPARE(p.isResettable(), false); - QCOMPARE(p.reset(), false); - } - - // Resettable prop by name - { - PropertyObject o; - QDeclarativeMetaProperty p(&o, QString("resettableProperty")); - - QCOMPARE(p.read(), QVariant(9)); - QCOMPARE(p.write(QVariant(11)), true); - QCOMPARE(p.read(), QVariant(11)); - - QCOMPARE(p.isResettable(), true); - QCOMPARE(p.reset(), true); - - QCOMPARE(p.read(), QVariant(9)); - } - - // Deleted object - { - PropertyObject *o = new PropertyObject; - - QDeclarativeMetaProperty p(o, QString("resettableProperty")); - - QCOMPARE(p.isResettable(), true); - QCOMPARE(p.reset(), true); - - delete o; - - QCOMPARE(p.isResettable(), false); - QCOMPARE(p.reset(), false); - } - - // Signal property - { - PropertyObject o; - QDeclarativeMetaProperty p(&o, "onClicked"); - - QCOMPARE(p.isResettable(), false); - QCOMPARE(p.reset(), false); - } -} - -void tst_qdeclarativemetaproperty::writeObjectToList() -{ - QDeclarativeComponent containerComponent(&engine); - containerComponent.setData("import Test 1.0\nMyContainer { children: MyQmlObject {} }", QUrl()); - MyContainer *container = qobject_cast(containerComponent.create()); - QVERIFY(container != 0); - QDeclarativeListReference list(container, "children"); - QVERIFY(list.count() == 1); - - MyQmlObject *object = new MyQmlObject; - QDeclarativeMetaProperty prop(container, "children"); - prop.write(qVariantFromValue(object)); - QCOMPARE(list.count(), 1); - QCOMPARE(list.at(0), object); -} - -Q_DECLARE_METATYPE(QList); -void tst_qdeclarativemetaproperty::writeListToList() -{ - QDeclarativeComponent containerComponent(&engine); - containerComponent.setData("import Test 1.0\nMyContainer { children: MyQmlObject {} }", QUrl()); - MyContainer *container = qobject_cast(containerComponent.create()); - QVERIFY(container != 0); - QDeclarativeListReference list(container, "children"); - QVERIFY(list.count() == 1); - - QList objList; - objList << new MyQmlObject() << new MyQmlObject() << new MyQmlObject() << new MyQmlObject(); - QDeclarativeMetaProperty prop(container, "children"); - prop.write(qVariantFromValue(objList)); - QCOMPARE(list.count(), 4); - - //XXX need to try this with read/write prop (for read-only it correctly doesn't write) - /*QList typedObjList; - typedObjList << new MyQmlObject(); - prop.write(qVariantFromValue(&typedObjList)); - QCOMPARE(container->children()->size(), 1);*/ -} - -void tst_qdeclarativemetaproperty::crashOnValueProperty() -{ - QDeclarativeEngine *engine = new QDeclarativeEngine; - QDeclarativeComponent component(engine); - - component.setData("import Test 1.0\nPropertyObject { wrectProperty.x: 10 }", QUrl()); - PropertyObject *obj = qobject_cast(component.create()); - QVERIFY(obj != 0); - - QDeclarativeMetaProperty p(obj, "wrectProperty.x", qmlContext(obj)); - QCOMPARE(p.name(), QString("wrectProperty.x")); - - QCOMPARE(p.read(), QVariant(10)); - - //don't crash once the engine is deleted - delete engine; - engine = 0; - - QCOMPARE(p.propertyTypeName(), "int"); - QCOMPARE(p.read(), QVariant(10)); - p.write(QVariant(20)); - QCOMPARE(p.read(), QVariant(20)); -} - -void tst_qdeclarativemetaproperty::copy() -{ - PropertyObject object; - - QDeclarativeMetaProperty *property = new QDeclarativeMetaProperty(&object, QLatin1String("defaultProperty")); - QCOMPARE(property->name(), QString("defaultProperty")); - QCOMPARE(property->read(), QVariant(10)); - QCOMPARE(property->type(), QDeclarativeMetaProperty::Property); - QCOMPARE(property->propertyTypeCategory(), QDeclarativeMetaProperty::Normal); - QCOMPARE(property->propertyType(), (int)QVariant::Int); - - QDeclarativeMetaProperty p1(*property); - QCOMPARE(p1.name(), QString("defaultProperty")); - QCOMPARE(p1.read(), QVariant(10)); - QCOMPARE(p1.type(), QDeclarativeMetaProperty::Property); - QCOMPARE(p1.propertyTypeCategory(), QDeclarativeMetaProperty::Normal); - QCOMPARE(p1.propertyType(), (int)QVariant::Int); - - QDeclarativeMetaProperty p2(&object, QLatin1String("url")); - QCOMPARE(p2.name(), QString("url")); - p2 = *property; - QCOMPARE(p2.name(), QString("defaultProperty")); - QCOMPARE(p2.read(), QVariant(10)); - QCOMPARE(p2.type(), QDeclarativeMetaProperty::Property); - QCOMPARE(p2.propertyTypeCategory(), QDeclarativeMetaProperty::Normal); - QCOMPARE(p2.propertyType(), (int)QVariant::Int); - - delete property; property = 0; - - QCOMPARE(p1.name(), QString("defaultProperty")); - QCOMPARE(p1.read(), QVariant(10)); - QCOMPARE(p1.type(), QDeclarativeMetaProperty::Property); - QCOMPARE(p1.propertyTypeCategory(), QDeclarativeMetaProperty::Normal); - QCOMPARE(p1.propertyType(), (int)QVariant::Int); - - QCOMPARE(p2.name(), QString("defaultProperty")); - QCOMPARE(p2.read(), QVariant(10)); - QCOMPARE(p2.type(), QDeclarativeMetaProperty::Property); - QCOMPARE(p2.propertyTypeCategory(), QDeclarativeMetaProperty::Normal); - QCOMPARE(p2.propertyType(), (int)QVariant::Int); -} - -void tst_qdeclarativemetaproperty::initTestCase() -{ - QML_REGISTER_TYPE(Test,1,0,MyQmlObject,MyQmlObject); - QML_REGISTER_TYPE(Test,1,0,PropertyObject,PropertyObject); - QML_REGISTER_TYPE(Test,1,0,MyContainer,MyContainer); -} - - -QTEST_MAIN(tst_qdeclarativemetaproperty) - -#include "tst_qdeclarativemetaproperty.moc" diff --git a/tests/auto/declarative/qdeclarativemetatype/tst_qdeclarativemetatype.cpp b/tests/auto/declarative/qdeclarativemetatype/tst_qdeclarativemetatype.cpp index 4333e02bfc..818f1089ec 100644 --- a/tests/auto/declarative/qdeclarativemetatype/tst_qdeclarativemetatype.cpp +++ b/tests/auto/declarative/qdeclarativemetatype/tst_qdeclarativemetatype.cpp @@ -97,7 +97,7 @@ class ValueSourceTestType : public QObject, public QDeclarativePropertyValueSour Q_OBJECT Q_INTERFACES(QDeclarativePropertyValueSource) public: - virtual void setTarget(const QDeclarativeMetaProperty &) {} + virtual void setTarget(const QDeclarativeProperty &) {} }; QML_DECLARE_TYPE(ValueSourceTestType); @@ -106,7 +106,7 @@ class ValueInterceptorTestType : public QObject, public QDeclarativePropertyValu Q_OBJECT Q_INTERFACES(QDeclarativePropertyValueInterceptor) public: - virtual void setTarget(const QDeclarativeMetaProperty &) {} + virtual void setTarget(const QDeclarativeProperty &) {} virtual void write(const QVariant &) {} }; QML_DECLARE_TYPE(ValueInterceptorTestType); diff --git a/tests/auto/declarative/qdeclarativeproperty/qdeclarativeproperty.pro b/tests/auto/declarative/qdeclarativeproperty/qdeclarativeproperty.pro new file mode 100644 index 0000000000..22e50cc558 --- /dev/null +++ b/tests/auto/declarative/qdeclarativeproperty/qdeclarativeproperty.pro @@ -0,0 +1,5 @@ +load(qttest_p4) +contains(QT_CONFIG,declarative): QT += declarative +macx:CONFIG -= app_bundle + +SOURCES += tst_qdeclarativeproperty.cpp diff --git a/tests/auto/declarative/qdeclarativeproperty/tst_qdeclarativeproperty.cpp b/tests/auto/declarative/qdeclarativeproperty/tst_qdeclarativeproperty.cpp new file mode 100644 index 0000000000..d4b0808504 --- /dev/null +++ b/tests/auto/declarative/qdeclarativeproperty/tst_qdeclarativeproperty.cpp @@ -0,0 +1,1186 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include + +class MyQmlObject : public QObject +{ + Q_OBJECT +public: + MyQmlObject() {} +}; + +QML_DECLARE_TYPE(MyQmlObject); + +class MyAttached : public QObject +{ + Q_OBJECT + Q_PROPERTY(int foo READ foo WRITE setFoo) +public: + MyAttached(QObject *parent) : QObject(parent), m_foo(13) {} + + int foo() const { return m_foo; } + void setFoo(int f) { m_foo = f; } + +private: + int m_foo; +}; + +class MyContainer : public QObject +{ + Q_OBJECT + Q_PROPERTY(QDeclarativeListProperty children READ children) +public: + MyContainer() {} + + QDeclarativeListProperty children() { return QDeclarativeListProperty(this, m_children); } + + static MyAttached *qmlAttachedProperties(QObject *o) { + return new MyAttached(o); + } + +private: + QList m_children; +}; + +QML_DECLARE_TYPE(MyContainer); +QML_DECLARE_TYPEINFO(MyContainer, QML_HAS_ATTACHED_PROPERTIES) + +class tst_qdeclarativeproperty : public QObject +{ + Q_OBJECT +public: + tst_qdeclarativeproperty() {} + +private slots: + void initTestCase(); + + // Constructors + void qmlmetaproperty(); + void qmlmetaproperty_object(); + void qmlmetaproperty_object_string(); + void qmlmetaproperty_object_context(); + void qmlmetaproperty_object_string_context(); + + // Methods + void name(); + void read(); + void write(); + void reset(); + + // Functionality + void writeObjectToList(); + void writeListToList(); + + //writeToReadOnly(); + + // Bugs + void crashOnValueProperty(); + + void copy(); +private: + QDeclarativeEngine engine; +}; + +void tst_qdeclarativeproperty::qmlmetaproperty() +{ + QDeclarativeProperty prop; + + QGuard binding(new QDeclarativeBinding(QLatin1String("null"), 0, engine.rootContext())); + QVERIFY(binding != 0); + QGuard expression(new QDeclarativeExpression()); + QVERIFY(expression != 0); + + QObject *obj = new QObject; + + QCOMPARE(prop.name(), QString()); + QCOMPARE(prop.read(), QVariant()); + QCOMPARE(prop.write(QVariant()), false); + QCOMPARE(prop.hasChangedNotifier(), false); + QCOMPARE(prop.needsChangedNotifier(), false); + QCOMPARE(prop.connectNotifier(0, SLOT(deleteLater())), false); + QCOMPARE(prop.connectNotifier(obj, SLOT(deleteLater())), false); + QCOMPARE(prop.connectNotifier(obj, 0), false); + QCOMPARE(prop.connectNotifier(0, obj->metaObject()->indexOfMethod("deleteLater()")), false); + QCOMPARE(prop.connectNotifier(obj, obj->metaObject()->indexOfMethod("deleteLater()")), false); + QCOMPARE(prop.connectNotifier(obj, -1), false); + QVERIFY(prop.method().signature() == 0); + QCOMPARE(prop.type(), QDeclarativeProperty::Invalid); + QCOMPARE(prop.isProperty(), false); + QCOMPARE(prop.isDefault(), false); + QCOMPARE(prop.isWritable(), false); + QCOMPARE(prop.isDesignable(), false); + QCOMPARE(prop.isResettable(), false); + QCOMPARE(prop.isValid(), false); + QCOMPARE(prop.object(), (QObject *)0); + QCOMPARE(prop.propertyTypeCategory(), QDeclarativeProperty::InvalidCategory); + QCOMPARE(prop.propertyType(), 0); + QCOMPARE(prop.propertyTypeName(), (const char *)0); + QVERIFY(prop.property().name() == 0); + QVERIFY(QDeclarativePropertyPrivate::binding(prop) == 0); + QVERIFY(QDeclarativePropertyPrivate::setBinding(prop, binding) == 0); + QVERIFY(binding == 0); + QVERIFY(QDeclarativePropertyPrivate::signalExpression(prop) == 0); + QVERIFY(QDeclarativePropertyPrivate::setSignalExpression(prop, expression) == 0); + QVERIFY(expression == 0); + QCOMPARE(prop.coreIndex(), -1); + QCOMPARE(QDeclarativePropertyPrivate::valueTypeCoreIndex(prop), -1); + + delete obj; +} + +class PropertyObject : public QObject +{ + Q_OBJECT + Q_PROPERTY(int defaultProperty READ defaultProperty); + Q_PROPERTY(QRect rectProperty READ rectProperty); + Q_PROPERTY(QRect wrectProperty READ wrectProperty WRITE setWRectProperty); + Q_PROPERTY(QUrl url READ url WRITE setUrl); + Q_PROPERTY(int resettableProperty READ resettableProperty WRITE setResettableProperty RESET resetProperty); + + Q_CLASSINFO("DefaultProperty", "defaultProperty"); +public: + PropertyObject() : m_resetProperty(9) {} + + int defaultProperty() { return 10; } + QRect rectProperty() { return QRect(10, 10, 1, 209); } + + QRect wrectProperty() { return m_rect; } + void setWRectProperty(const QRect &r) { m_rect = r; } + + QUrl url() { return m_url; } + void setUrl(const QUrl &u) { m_url = u; } + + int resettableProperty() const { return m_resetProperty; } + void setResettableProperty(int r) { m_resetProperty = r; } + void resetProperty() { m_resetProperty = 9; } + +signals: + void clicked(); + +private: + int m_resetProperty; + QRect m_rect; + QUrl m_url; +}; + +QML_DECLARE_TYPE(PropertyObject); + +void tst_qdeclarativeproperty::qmlmetaproperty_object() +{ + QObject object; // Has no default property + PropertyObject dobject; // Has default property + + { + QDeclarativeProperty prop(&object); + + QGuard binding(new QDeclarativeBinding(QLatin1String("null"), 0, engine.rootContext())); + QVERIFY(binding != 0); + QGuard expression(new QDeclarativeExpression()); + QVERIFY(expression != 0); + + QObject *obj = new QObject; + + QCOMPARE(prop.name(), QString()); + QCOMPARE(prop.read(), QVariant()); + QCOMPARE(prop.write(QVariant()), false); + QCOMPARE(prop.hasChangedNotifier(), false); + QCOMPARE(prop.needsChangedNotifier(), false); + QCOMPARE(prop.connectNotifier(0, SLOT(deleteLater())), false); + QCOMPARE(prop.connectNotifier(obj, SLOT(deleteLater())), false); + QCOMPARE(prop.connectNotifier(obj, 0), false); + QCOMPARE(prop.connectNotifier(0, obj->metaObject()->indexOfMethod("deleteLater()")), false); + QCOMPARE(prop.connectNotifier(obj, obj->metaObject()->indexOfMethod("deleteLater()")), false); + QCOMPARE(prop.connectNotifier(obj, -1), false); + QVERIFY(prop.method().signature() == 0); + QCOMPARE(prop.type(), QDeclarativeProperty::Invalid); + QCOMPARE(prop.isProperty(), false); + QCOMPARE(prop.isDefault(), false); + QCOMPARE(prop.isWritable(), false); + QCOMPARE(prop.isDesignable(), false); + QCOMPARE(prop.isResettable(), false); + QCOMPARE(prop.isValid(), false); + QCOMPARE(prop.object(), (QObject *)0); + QCOMPARE(prop.propertyTypeCategory(), QDeclarativeProperty::InvalidCategory); + QCOMPARE(prop.propertyType(), 0); + QCOMPARE(prop.propertyTypeName(), (const char *)0); + QVERIFY(prop.property().name() == 0); + QVERIFY(QDeclarativePropertyPrivate::binding(prop) == 0); + QVERIFY(QDeclarativePropertyPrivate::setBinding(prop, binding) == 0); + QVERIFY(binding == 0); + QVERIFY(QDeclarativePropertyPrivate::signalExpression(prop) == 0); + QVERIFY(QDeclarativePropertyPrivate::setSignalExpression(prop, expression) == 0); + QVERIFY(expression == 0); + QCOMPARE(prop.coreIndex(), -1); + QCOMPARE(QDeclarativePropertyPrivate::valueTypeCoreIndex(prop), -1); + + delete obj; + } + + { + QDeclarativeProperty prop(&dobject); + + QGuard binding(new QDeclarativeBinding(QLatin1String("null"), 0, engine.rootContext())); + binding->setTarget(prop); + QVERIFY(binding != 0); + QGuard expression(new QDeclarativeExpression()); + QVERIFY(expression != 0); + + QObject *obj = new QObject; + + QCOMPARE(prop.name(), QString("defaultProperty")); + QCOMPARE(prop.read(), QVariant(10)); + QCOMPARE(prop.write(QVariant()), false); + QCOMPARE(prop.hasChangedNotifier(), false); + QCOMPARE(prop.needsChangedNotifier(), true); + QCOMPARE(prop.connectNotifier(0, SLOT(deleteLater())), false); + QCOMPARE(prop.connectNotifier(obj, SLOT(deleteLater())), false); + QCOMPARE(prop.connectNotifier(obj, 0), false); + QCOMPARE(prop.connectNotifier(0, obj->metaObject()->indexOfMethod("deleteLater()")), false); + QCOMPARE(prop.connectNotifier(obj, obj->metaObject()->indexOfMethod("deleteLater()")), false); + QCOMPARE(prop.connectNotifier(obj, -1), false); + QVERIFY(prop.method().signature() == 0); + QCOMPARE(prop.type(), (QDeclarativeProperty::Type)(QDeclarativeProperty::Property | QDeclarativeProperty::Default)); + QCOMPARE(prop.isProperty(), true); + QCOMPARE(prop.isDefault(), true); + QCOMPARE(prop.isWritable(), false); + QCOMPARE(prop.isDesignable(), true); + QCOMPARE(prop.isResettable(), false); + QCOMPARE(prop.isValid(), true); + QCOMPARE(prop.object(), &dobject); + QCOMPARE(prop.propertyTypeCategory(), QDeclarativeProperty::Normal); + QCOMPARE(prop.propertyType(), (int)QVariant::Int); + QCOMPARE(prop.propertyTypeName(), "int"); + QCOMPARE(QString(prop.property().name()), QString("defaultProperty")); + QVERIFY(QDeclarativePropertyPrivate::binding(prop) == 0); + QTest::ignoreMessage(QtWarningMsg, ":-1: Unable to assign null to int"); + QVERIFY(QDeclarativePropertyPrivate::setBinding(prop, binding) == 0); + QVERIFY(binding != 0); + QVERIFY(QDeclarativePropertyPrivate::binding(prop) == binding); + QVERIFY(QDeclarativePropertyPrivate::signalExpression(prop) == 0); + QVERIFY(QDeclarativePropertyPrivate::setSignalExpression(prop, expression) == 0); + QVERIFY(expression == 0); + QCOMPARE(prop.coreIndex(), dobject.metaObject()->indexOfProperty("defaultProperty")); + QCOMPARE(QDeclarativePropertyPrivate::valueTypeCoreIndex(prop), -1); + + delete obj; + } +} + +void tst_qdeclarativeproperty::qmlmetaproperty_object_string() +{ + QObject object; + PropertyObject dobject; + + { + QDeclarativeProperty prop(&object, QString("defaultProperty")); + + QGuard binding(new QDeclarativeBinding(QLatin1String("null"), 0, engine.rootContext())); + QVERIFY(binding != 0); + QGuard expression(new QDeclarativeExpression()); + QVERIFY(expression != 0); + + QObject *obj = new QObject; + + QCOMPARE(prop.name(), QString()); + QCOMPARE(prop.read(), QVariant()); + QCOMPARE(prop.write(QVariant()), false); + QCOMPARE(prop.hasChangedNotifier(), false); + QCOMPARE(prop.needsChangedNotifier(), false); + QCOMPARE(prop.connectNotifier(0, SLOT(deleteLater())), false); + QCOMPARE(prop.connectNotifier(obj, SLOT(deleteLater())), false); + QCOMPARE(prop.connectNotifier(obj, 0), false); + QCOMPARE(prop.connectNotifier(0, obj->metaObject()->indexOfMethod("deleteLater()")), false); + QCOMPARE(prop.connectNotifier(obj, obj->metaObject()->indexOfMethod("deleteLater()")), false); + QCOMPARE(prop.connectNotifier(obj, -1), false); + QVERIFY(prop.method().signature() == 0); + QCOMPARE(prop.type(), QDeclarativeProperty::Invalid); + QCOMPARE(prop.isProperty(), false); + QCOMPARE(prop.isDefault(), false); + QCOMPARE(prop.isWritable(), false); + QCOMPARE(prop.isDesignable(), false); + QCOMPARE(prop.isResettable(), false); + QCOMPARE(prop.isValid(), false); + QCOMPARE(prop.object(), (QObject *)0); + QCOMPARE(prop.propertyTypeCategory(), QDeclarativeProperty::InvalidCategory); + QCOMPARE(prop.propertyType(), 0); + QCOMPARE(prop.propertyTypeName(), (const char *)0); + QVERIFY(prop.property().name() == 0); + QVERIFY(QDeclarativePropertyPrivate::binding(prop) == 0); + QVERIFY(QDeclarativePropertyPrivate::setBinding(prop, binding) == 0); + QVERIFY(binding == 0); + QVERIFY(QDeclarativePropertyPrivate::signalExpression(prop) == 0); + QVERIFY(QDeclarativePropertyPrivate::setSignalExpression(prop, expression) == 0); + QVERIFY(expression == 0); + QCOMPARE(prop.coreIndex(), -1); + QCOMPARE(QDeclarativePropertyPrivate::valueTypeCoreIndex(prop), -1); + + delete obj; + } + + { + QDeclarativeProperty prop(&dobject, QString("defaultProperty")); + + QGuard binding(new QDeclarativeBinding(QLatin1String("null"), 0, engine.rootContext())); + binding->setTarget(prop); + QVERIFY(binding != 0); + QGuard expression(new QDeclarativeExpression()); + QVERIFY(expression != 0); + + QObject *obj = new QObject; + + QCOMPARE(prop.name(), QString("defaultProperty")); + QCOMPARE(prop.read(), QVariant(10)); + QCOMPARE(prop.write(QVariant()), false); + QCOMPARE(prop.hasChangedNotifier(), false); + QCOMPARE(prop.needsChangedNotifier(), true); + QCOMPARE(prop.connectNotifier(0, SLOT(deleteLater())), false); + QCOMPARE(prop.connectNotifier(obj, SLOT(deleteLater())), false); + QCOMPARE(prop.connectNotifier(obj, 0), false); + QCOMPARE(prop.connectNotifier(0, obj->metaObject()->indexOfMethod("deleteLater()")), false); + QCOMPARE(prop.connectNotifier(obj, obj->metaObject()->indexOfMethod("deleteLater()")), false); + QCOMPARE(prop.connectNotifier(obj, -1), false); + QVERIFY(prop.method().signature() == 0); + QCOMPARE(prop.type(), QDeclarativeProperty::Property); + QCOMPARE(prop.isProperty(), true); + QCOMPARE(prop.isDefault(), false); + QCOMPARE(prop.isWritable(), false); + QCOMPARE(prop.isDesignable(), true); + QCOMPARE(prop.isResettable(), false); + QCOMPARE(prop.isValid(), true); + QCOMPARE(prop.object(), &dobject); + QCOMPARE(prop.propertyTypeCategory(), QDeclarativeProperty::Normal); + QCOMPARE(prop.propertyType(), (int)QVariant::Int); + QCOMPARE(prop.propertyTypeName(), "int"); + QCOMPARE(QString(prop.property().name()), QString("defaultProperty")); + QVERIFY(QDeclarativePropertyPrivate::binding(prop) == 0); + QTest::ignoreMessage(QtWarningMsg, ":-1: Unable to assign null to int"); + QVERIFY(QDeclarativePropertyPrivate::setBinding(prop, binding) == 0); + QVERIFY(binding != 0); + QVERIFY(QDeclarativePropertyPrivate::binding(prop) == binding); + QVERIFY(QDeclarativePropertyPrivate::signalExpression(prop) == 0); + QVERIFY(QDeclarativePropertyPrivate::setSignalExpression(prop, expression) == 0); + QVERIFY(expression == 0); + QCOMPARE(prop.coreIndex(), dobject.metaObject()->indexOfProperty("defaultProperty")); + QCOMPARE(QDeclarativePropertyPrivate::valueTypeCoreIndex(prop), -1); + + delete obj; + } + + { + QDeclarativeProperty prop(&dobject, QString("onClicked")); + + QGuard binding(new QDeclarativeBinding(QLatin1String("null"), 0, engine.rootContext())); + binding->setTarget(prop); + QVERIFY(binding != 0); + QGuard expression(new QDeclarativeExpression()); + QVERIFY(expression != 0); + + QObject *obj = new QObject; + + QCOMPARE(prop.name(), QString("onClicked")); + QCOMPARE(prop.read(), QVariant()); + QCOMPARE(prop.write(QVariant("Hello")), false); + QCOMPARE(prop.hasChangedNotifier(), false); + QCOMPARE(prop.needsChangedNotifier(), false); + QCOMPARE(prop.connectNotifier(0, SLOT(deleteLater())), false); + QCOMPARE(prop.connectNotifier(obj, SLOT(deleteLater())), false); + QCOMPARE(prop.connectNotifier(obj, 0), false); + QCOMPARE(prop.connectNotifier(0, obj->metaObject()->indexOfMethod("deleteLater()")), false); + QCOMPARE(prop.connectNotifier(obj, obj->metaObject()->indexOfMethod("deleteLater()")), false); + QCOMPARE(prop.connectNotifier(obj, -1), false); + QCOMPARE(QString(prop.method().signature()), QString("clicked()")); + QCOMPARE(prop.type(), QDeclarativeProperty::SignalProperty); + QCOMPARE(prop.isProperty(), false); + QCOMPARE(prop.isDefault(), false); + QCOMPARE(prop.isWritable(), false); + QCOMPARE(prop.isDesignable(), false); + QCOMPARE(prop.isResettable(), false); + QCOMPARE(prop.isValid(), true); + QCOMPARE(prop.object(), &dobject); + QCOMPARE(prop.propertyTypeCategory(), QDeclarativeProperty::InvalidCategory); + QCOMPARE(prop.propertyType(), 0); + QCOMPARE(prop.propertyTypeName(), (const char *)0); + QCOMPARE(prop.property().name(), (const char *)0); + QVERIFY(QDeclarativePropertyPrivate::binding(prop) == 0); + QVERIFY(QDeclarativePropertyPrivate::setBinding(prop, binding) == 0); + QVERIFY(binding == 0); + QVERIFY(QDeclarativePropertyPrivate::signalExpression(prop) == 0); + QVERIFY(QDeclarativePropertyPrivate::setSignalExpression(prop, expression) == 0); + QVERIFY(expression != 0); + QVERIFY(QDeclarativePropertyPrivate::signalExpression(prop) == expression); + QCOMPARE(prop.coreIndex(), dobject.metaObject()->indexOfMethod("clicked()")); + QCOMPARE(QDeclarativePropertyPrivate::valueTypeCoreIndex(prop), -1); + + delete obj; + } +} + +void tst_qdeclarativeproperty::qmlmetaproperty_object_context() +{ + QObject object; // Has no default property + PropertyObject dobject; // Has default property + + { + QDeclarativeProperty prop(&object, engine.rootContext()); + + QGuard binding(new QDeclarativeBinding(QLatin1String("null"), 0, engine.rootContext())); + QVERIFY(binding != 0); + QGuard expression(new QDeclarativeExpression()); + QVERIFY(expression != 0); + + QObject *obj = new QObject; + + QCOMPARE(prop.name(), QString()); + QCOMPARE(prop.read(), QVariant()); + QCOMPARE(prop.write(QVariant()), false); + QCOMPARE(prop.hasChangedNotifier(), false); + QCOMPARE(prop.needsChangedNotifier(), false); + QCOMPARE(prop.connectNotifier(0, SLOT(deleteLater())), false); + QCOMPARE(prop.connectNotifier(obj, SLOT(deleteLater())), false); + QCOMPARE(prop.connectNotifier(obj, 0), false); + QCOMPARE(prop.connectNotifier(0, obj->metaObject()->indexOfMethod("deleteLater()")), false); + QCOMPARE(prop.connectNotifier(obj, obj->metaObject()->indexOfMethod("deleteLater()")), false); + QCOMPARE(prop.connectNotifier(obj, -1), false); + QVERIFY(prop.method().signature() == 0); + QCOMPARE(prop.type(), QDeclarativeProperty::Invalid); + QCOMPARE(prop.isProperty(), false); + QCOMPARE(prop.isDefault(), false); + QCOMPARE(prop.isWritable(), false); + QCOMPARE(prop.isDesignable(), false); + QCOMPARE(prop.isResettable(), false); + QCOMPARE(prop.isValid(), false); + QCOMPARE(prop.object(), (QObject *)0); + QCOMPARE(prop.propertyTypeCategory(), QDeclarativeProperty::InvalidCategory); + QCOMPARE(prop.propertyType(), 0); + QCOMPARE(prop.propertyTypeName(), (const char *)0); + QVERIFY(prop.property().name() == 0); + QVERIFY(QDeclarativePropertyPrivate::binding(prop) == 0); + QVERIFY(QDeclarativePropertyPrivate::setBinding(prop, binding) == 0); + QVERIFY(binding == 0); + QVERIFY(QDeclarativePropertyPrivate::signalExpression(prop) == 0); + QVERIFY(QDeclarativePropertyPrivate::setSignalExpression(prop, expression) == 0); + QVERIFY(expression == 0); + QCOMPARE(prop.coreIndex(), -1); + QCOMPARE(QDeclarativePropertyPrivate::valueTypeCoreIndex(prop), -1); + + delete obj; + } + + { + QDeclarativeProperty prop(&dobject, engine.rootContext()); + + QGuard binding(new QDeclarativeBinding(QLatin1String("null"), 0, engine.rootContext())); + binding->setTarget(prop); + QVERIFY(binding != 0); + QGuard expression(new QDeclarativeExpression()); + QVERIFY(expression != 0); + + QObject *obj = new QObject; + + QCOMPARE(prop.name(), QString("defaultProperty")); + QCOMPARE(prop.read(), QVariant(10)); + QCOMPARE(prop.write(QVariant()), false); + QCOMPARE(prop.hasChangedNotifier(), false); + QCOMPARE(prop.needsChangedNotifier(), true); + QCOMPARE(prop.connectNotifier(0, SLOT(deleteLater())), false); + QCOMPARE(prop.connectNotifier(obj, SLOT(deleteLater())), false); + QCOMPARE(prop.connectNotifier(obj, 0), false); + QCOMPARE(prop.connectNotifier(0, obj->metaObject()->indexOfMethod("deleteLater()")), false); + QCOMPARE(prop.connectNotifier(obj, obj->metaObject()->indexOfMethod("deleteLater()")), false); + QCOMPARE(prop.connectNotifier(obj, -1), false); + QVERIFY(prop.method().signature() == 0); + QCOMPARE(prop.type(), (QDeclarativeProperty::Type)(QDeclarativeProperty::Property | QDeclarativeProperty::Default)); + QCOMPARE(prop.isProperty(), true); + QCOMPARE(prop.isDefault(), true); + QCOMPARE(prop.isWritable(), false); + QCOMPARE(prop.isDesignable(), true); + QCOMPARE(prop.isResettable(), false); + QCOMPARE(prop.isValid(), true); + QCOMPARE(prop.object(), &dobject); + QCOMPARE(prop.propertyTypeCategory(), QDeclarativeProperty::Normal); + QCOMPARE(prop.propertyType(), (int)QVariant::Int); + QCOMPARE(prop.propertyTypeName(), "int"); + QCOMPARE(QString(prop.property().name()), QString("defaultProperty")); + QVERIFY(QDeclarativePropertyPrivate::binding(prop) == 0); + QTest::ignoreMessage(QtWarningMsg, ":-1: Unable to assign null to int"); + QVERIFY(QDeclarativePropertyPrivate::setBinding(prop, binding) == 0); + QVERIFY(binding != 0); + QVERIFY(QDeclarativePropertyPrivate::binding(prop) == binding); + QVERIFY(QDeclarativePropertyPrivate::signalExpression(prop) == 0); + QVERIFY(QDeclarativePropertyPrivate::setSignalExpression(prop, expression) == 0); + QVERIFY(expression == 0); + QCOMPARE(prop.coreIndex(), dobject.metaObject()->indexOfProperty("defaultProperty")); + QCOMPARE(QDeclarativePropertyPrivate::valueTypeCoreIndex(prop), -1); + + delete obj; + } +} + +void tst_qdeclarativeproperty::qmlmetaproperty_object_string_context() +{ + QObject object; + PropertyObject dobject; + + { + QDeclarativeProperty prop(&object, QString("defaultProperty"), engine.rootContext()); + + QGuard binding(new QDeclarativeBinding(QLatin1String("null"), 0, engine.rootContext())); + QVERIFY(binding != 0); + QGuard expression(new QDeclarativeExpression()); + QVERIFY(expression != 0); + + QObject *obj = new QObject; + + QCOMPARE(prop.name(), QString()); + QCOMPARE(prop.read(), QVariant()); + QCOMPARE(prop.write(QVariant()), false); + QCOMPARE(prop.hasChangedNotifier(), false); + QCOMPARE(prop.needsChangedNotifier(), false); + QCOMPARE(prop.connectNotifier(0, SLOT(deleteLater())), false); + QCOMPARE(prop.connectNotifier(obj, SLOT(deleteLater())), false); + QCOMPARE(prop.connectNotifier(obj, 0), false); + QCOMPARE(prop.connectNotifier(0, obj->metaObject()->indexOfMethod("deleteLater()")), false); + QCOMPARE(prop.connectNotifier(obj, obj->metaObject()->indexOfMethod("deleteLater()")), false); + QCOMPARE(prop.connectNotifier(obj, -1), false); + QVERIFY(prop.method().signature() == 0); + QCOMPARE(prop.type(), QDeclarativeProperty::Invalid); + QCOMPARE(prop.isProperty(), false); + QCOMPARE(prop.isDefault(), false); + QCOMPARE(prop.isWritable(), false); + QCOMPARE(prop.isDesignable(), false); + QCOMPARE(prop.isResettable(), false); + QCOMPARE(prop.isValid(), false); + QCOMPARE(prop.object(), (QObject *)0); + QCOMPARE(prop.propertyTypeCategory(), QDeclarativeProperty::InvalidCategory); + QCOMPARE(prop.propertyType(), 0); + QCOMPARE(prop.propertyTypeName(), (const char *)0); + QVERIFY(prop.property().name() == 0); + QVERIFY(QDeclarativePropertyPrivate::binding(prop) == 0); + QVERIFY(QDeclarativePropertyPrivate::setBinding(prop, binding) == 0); + QVERIFY(binding == 0); + QVERIFY(QDeclarativePropertyPrivate::signalExpression(prop) == 0); + QVERIFY(QDeclarativePropertyPrivate::setSignalExpression(prop, expression) == 0); + QVERIFY(expression == 0); + QCOMPARE(prop.coreIndex(), -1); + QCOMPARE(QDeclarativePropertyPrivate::valueTypeCoreIndex(prop), -1); + + delete obj; + } + + { + QDeclarativeProperty prop(&dobject, QString("defaultProperty"), engine.rootContext()); + + QGuard binding(new QDeclarativeBinding(QLatin1String("null"), 0, engine.rootContext())); + binding->setTarget(prop); + QVERIFY(binding != 0); + QGuard expression(new QDeclarativeExpression()); + QVERIFY(expression != 0); + + QObject *obj = new QObject; + + QCOMPARE(prop.name(), QString("defaultProperty")); + QCOMPARE(prop.read(), QVariant(10)); + QCOMPARE(prop.write(QVariant()), false); + QCOMPARE(prop.hasChangedNotifier(), false); + QCOMPARE(prop.needsChangedNotifier(), true); + QCOMPARE(prop.connectNotifier(0, SLOT(deleteLater())), false); + QCOMPARE(prop.connectNotifier(obj, SLOT(deleteLater())), false); + QCOMPARE(prop.connectNotifier(obj, 0), false); + QCOMPARE(prop.connectNotifier(0, obj->metaObject()->indexOfMethod("deleteLater()")), false); + QCOMPARE(prop.connectNotifier(obj, obj->metaObject()->indexOfMethod("deleteLater()")), false); + QCOMPARE(prop.connectNotifier(obj, -1), false); + QVERIFY(prop.method().signature() == 0); + QCOMPARE(prop.type(), QDeclarativeProperty::Property); + QCOMPARE(prop.isProperty(), true); + QCOMPARE(prop.isDefault(), false); + QCOMPARE(prop.isWritable(), false); + QCOMPARE(prop.isDesignable(), true); + QCOMPARE(prop.isResettable(), false); + QCOMPARE(prop.isValid(), true); + QCOMPARE(prop.object(), &dobject); + QCOMPARE(prop.propertyTypeCategory(), QDeclarativeProperty::Normal); + QCOMPARE(prop.propertyType(), (int)QVariant::Int); + QCOMPARE(prop.propertyTypeName(), "int"); + QCOMPARE(QString(prop.property().name()), QString("defaultProperty")); + QVERIFY(QDeclarativePropertyPrivate::binding(prop) == 0); + QTest::ignoreMessage(QtWarningMsg, ":-1: Unable to assign null to int"); + QVERIFY(QDeclarativePropertyPrivate::setBinding(prop, binding) == 0); + QVERIFY(binding != 0); + QVERIFY(QDeclarativePropertyPrivate::binding(prop) == binding); + QVERIFY(QDeclarativePropertyPrivate::signalExpression(prop) == 0); + QVERIFY(QDeclarativePropertyPrivate::setSignalExpression(prop, expression) == 0); + QVERIFY(expression == 0); + QCOMPARE(prop.coreIndex(), dobject.metaObject()->indexOfProperty("defaultProperty")); + QCOMPARE(QDeclarativePropertyPrivate::valueTypeCoreIndex(prop), -1); + + delete obj; + } + + { + QDeclarativeProperty prop(&dobject, QString("onClicked"), engine.rootContext()); + + QGuard binding(new QDeclarativeBinding(QLatin1String("null"), 0, engine.rootContext())); + binding->setTarget(prop); + QVERIFY(binding != 0); + QGuard expression(new QDeclarativeExpression()); + QVERIFY(expression != 0); + + QObject *obj = new QObject; + + QCOMPARE(prop.name(), QString("onClicked")); + QCOMPARE(prop.read(), QVariant()); + QCOMPARE(prop.write(QVariant("Hello")), false); + QCOMPARE(prop.hasChangedNotifier(), false); + QCOMPARE(prop.needsChangedNotifier(), false); + QCOMPARE(prop.connectNotifier(0, SLOT(deleteLater())), false); + QCOMPARE(prop.connectNotifier(obj, SLOT(deleteLater())), false); + QCOMPARE(prop.connectNotifier(obj, 0), false); + QCOMPARE(prop.connectNotifier(0, obj->metaObject()->indexOfMethod("deleteLater()")), false); + QCOMPARE(prop.connectNotifier(obj, obj->metaObject()->indexOfMethod("deleteLater()")), false); + QCOMPARE(prop.connectNotifier(obj, -1), false); + QCOMPARE(QString(prop.method().signature()), QString("clicked()")); + QCOMPARE(prop.type(), QDeclarativeProperty::SignalProperty); + QCOMPARE(prop.isProperty(), false); + QCOMPARE(prop.isDefault(), false); + QCOMPARE(prop.isWritable(), false); + QCOMPARE(prop.isDesignable(), false); + QCOMPARE(prop.isResettable(), false); + QCOMPARE(prop.isValid(), true); + QCOMPARE(prop.object(), &dobject); + QCOMPARE(prop.propertyTypeCategory(), QDeclarativeProperty::InvalidCategory); + QCOMPARE(prop.propertyType(), 0); + QCOMPARE(prop.propertyTypeName(), (const char *)0); + QCOMPARE(prop.property().name(), (const char *)0); + QVERIFY(QDeclarativePropertyPrivate::binding(prop) == 0); + QVERIFY(QDeclarativePropertyPrivate::setBinding(prop, binding) == 0); + QVERIFY(binding == 0); + QVERIFY(QDeclarativePropertyPrivate::signalExpression(prop) == 0); + QVERIFY(QDeclarativePropertyPrivate::setSignalExpression(prop, expression) == 0); + QVERIFY(expression != 0); + QVERIFY(QDeclarativePropertyPrivate::signalExpression(prop) == expression); + QCOMPARE(prop.coreIndex(), dobject.metaObject()->indexOfMethod("clicked()")); + QCOMPARE(QDeclarativePropertyPrivate::valueTypeCoreIndex(prop), -1); + + delete obj; + } +} + +void tst_qdeclarativeproperty::name() +{ + { + QDeclarativeProperty p; + QCOMPARE(p.name(), QString()); + } + + { + PropertyObject o; + QDeclarativeProperty p(&o); + QCOMPARE(p.name(), QString("defaultProperty")); + } + + { + QObject o; + QDeclarativeProperty p(&o, QString("objectName")); + QCOMPARE(p.name(), QString("objectName")); + } + + { + PropertyObject o; + QDeclarativeProperty p(&o, "onClicked"); + QCOMPARE(p.name(), QString("onClicked")); + } + + { + QObject o; + QDeclarativeProperty p(&o, "onClicked"); + QCOMPARE(p.name(), QString()); + } + + { + QObject o; + QDeclarativeProperty p(&o, "foo"); + QCOMPARE(p.name(), QString()); + } + + { + QDeclarativeProperty p(0, "foo"); + QCOMPARE(p.name(), QString()); + } + + { + PropertyObject o; + QDeclarativeProperty p(&o, "rectProperty"); + QCOMPARE(p.name(), QString("rectProperty")); + } + + { + PropertyObject o; + QDeclarativeProperty p(&o, "rectProperty.x"); + QCOMPARE(p.name(), QString("rectProperty.x")); + } + + { + PropertyObject o; + QDeclarativeProperty p(&o, "rectProperty.foo"); + QCOMPARE(p.name(), QString()); + } +} + +void tst_qdeclarativeproperty::read() +{ + // Invalid + { + QDeclarativeProperty p; + QCOMPARE(p.read(), QVariant()); + } + + // Default prop + { + PropertyObject o; + QDeclarativeProperty p(&o); + QCOMPARE(p.read(), QVariant(10)); + } + + // Invalid default prop + { + QObject o; + QDeclarativeProperty p(&o); + QCOMPARE(p.read(), QVariant()); + } + + // Value prop by name + { + QObject o; + + QDeclarativeProperty p(&o, "objectName"); + QCOMPARE(p.read(), QVariant(QString())); + + o.setObjectName("myName"); + + QCOMPARE(p.read(), QVariant("myName")); + } + + // Value-type prop + { + PropertyObject o; + QDeclarativeProperty p(&o, "rectProperty.x"); + QCOMPARE(p.read(), QVariant(10)); + } + + // Invalid value-type prop + { + PropertyObject o; + QDeclarativeProperty p(&o, "rectProperty.foo"); + QCOMPARE(p.read(), QVariant()); + } + + // Signal property + { + PropertyObject o; + QDeclarativeProperty p(&o, "onClicked"); + QCOMPARE(p.read(), QVariant()); + + QVERIFY(0 == QDeclarativePropertyPrivate::setSignalExpression(p, new QDeclarativeExpression())); + QVERIFY(0 != QDeclarativePropertyPrivate::signalExpression(p)); + + QCOMPARE(p.read(), QVariant()); + } + + // Deleted object + { + PropertyObject *o = new PropertyObject; + QDeclarativeProperty p(o, "rectProperty.x"); + QCOMPARE(p.read(), QVariant(10)); + delete o; + QCOMPARE(p.read(), QVariant()); + } + + // Attached property + { + QDeclarativeComponent component(&engine); + component.setData("import Test 1.0\nMyContainer { }", QUrl()); + QObject *object = component.create(); + QVERIFY(object != 0); + + QDeclarativeProperty p(object, "MyContainer.foo", qmlContext(object)); + QCOMPARE(p.read(), QVariant(13)); + delete object; + } + { + QDeclarativeComponent component(&engine); + component.setData("import Test 1.0\nMyContainer { MyContainer.foo: 10 }", QUrl()); + QObject *object = component.create(); + QVERIFY(object != 0); + + QDeclarativeProperty p(object, "MyContainer.foo", qmlContext(object)); + QCOMPARE(p.read(), QVariant(10)); + delete object; + } + { + QDeclarativeComponent component(&engine); + component.setData("import Test 1.0 as Foo\nFoo.MyContainer { Foo.MyContainer.foo: 10 }", QUrl()); + QObject *object = component.create(); + QVERIFY(object != 0); + + QDeclarativeProperty p(object, "Foo.MyContainer.foo", qmlContext(object)); + QCOMPARE(p.read(), QVariant(10)); + delete object; + } +} + +void tst_qdeclarativeproperty::write() +{ + // Invalid + { + QDeclarativeProperty p; + QCOMPARE(p.write(QVariant(10)), false); + } + + // Read-only default prop + { + PropertyObject o; + QDeclarativeProperty p(&o); + QCOMPARE(p.write(QVariant(10)), false); + } + + // Invalid default prop + { + QObject o; + QDeclarativeProperty p(&o); + QCOMPARE(p.write(QVariant(10)), false); + } + + // Read-only prop by name + { + PropertyObject o; + QDeclarativeProperty p(&o, QString("defaultProperty")); + QCOMPARE(p.write(QVariant(10)), false); + } + + // Writable prop by name + { + PropertyObject o; + QDeclarativeProperty p(&o, QString("objectName")); + QCOMPARE(o.objectName(), QString()); + QCOMPARE(p.write(QVariant(QString("myName"))), true); + QCOMPARE(o.objectName(), QString("myName")); + } + + // Deleted object + { + PropertyObject *o = new PropertyObject; + QDeclarativeProperty p(o, QString("objectName")); + QCOMPARE(p.write(QVariant(QString("myName"))), true); + QCOMPARE(o->objectName(), QString("myName")); + + delete o; + + QCOMPARE(p.write(QVariant(QString("myName"))), false); + } + + // Signal property + { + PropertyObject o; + QDeclarativeProperty p(&o, "onClicked"); + QCOMPARE(p.write(QVariant("console.log(1921)")), false); + + QVERIFY(0 == QDeclarativePropertyPrivate::setSignalExpression(p, new QDeclarativeExpression())); + QVERIFY(0 != QDeclarativePropertyPrivate::signalExpression(p)); + + QCOMPARE(p.write(QVariant("console.log(1921)")), false); + + QVERIFY(0 != QDeclarativePropertyPrivate::signalExpression(p)); + } + + // Value-type property + { + PropertyObject o; + QDeclarativeProperty p(&o, "wrectProperty"); + + QCOMPARE(o.wrectProperty(), QRect()); + QCOMPARE(p.write(QRect(1, 13, 99, 8)), true); + QCOMPARE(o.wrectProperty(), QRect(1, 13, 99, 8)); + + QDeclarativeProperty p2(&o, "wrectProperty.x"); + QCOMPARE(p2.read(), QVariant(1)); + QCOMPARE(p2.write(QVariant(6)), true); + QCOMPARE(p2.read(), QVariant(6)); + QCOMPARE(o.wrectProperty(), QRect(6, 13, 99, 8)); + } + + // URL-property + { + PropertyObject o; + QDeclarativeProperty p(&o, "url"); + + QCOMPARE(p.write(QUrl("main.qml")), true); + QCOMPARE(o.url(), QUrl("main.qml")); + + QDeclarativeProperty p2(&o, "url", engine.rootContext()); + + QUrl result = engine.baseUrl().resolved(QUrl("main.qml")); + QVERIFY(result != QUrl("main.qml")); + + QCOMPARE(p2.write(QUrl("main.qml")), true); + QCOMPARE(o.url(), result); + } + + // Attached property + { + QDeclarativeComponent component(&engine); + component.setData("import Test 1.0\nMyContainer { }", QUrl()); + QObject *object = component.create(); + QVERIFY(object != 0); + + QDeclarativeProperty p(object, "MyContainer.foo", qmlContext(object)); + p.write(QVariant(99)); + QCOMPARE(p.read(), QVariant(99)); + delete object; + } + { + QDeclarativeComponent component(&engine); + component.setData("import Test 1.0 as Foo\nFoo.MyContainer { Foo.MyContainer.foo: 10 }", QUrl()); + QObject *object = component.create(); + QVERIFY(object != 0); + + QDeclarativeProperty p(object, "Foo.MyContainer.foo", qmlContext(object)); + p.write(QVariant(99)); + QCOMPARE(p.read(), QVariant(99)); + delete object; + } +} + +void tst_qdeclarativeproperty::reset() +{ + // Invalid + { + QDeclarativeProperty p; + QCOMPARE(p.isResettable(), false); + QCOMPARE(p.reset(), false); + } + + // Read-only default prop + { + PropertyObject o; + QDeclarativeProperty p(&o); + QCOMPARE(p.isResettable(), false); + QCOMPARE(p.reset(), false); + } + + // Invalid default prop + { + QObject o; + QDeclarativeProperty p(&o); + QCOMPARE(p.isResettable(), false); + QCOMPARE(p.reset(), false); + } + + // Non-resettable-only prop by name + { + PropertyObject o; + QDeclarativeProperty p(&o, QString("defaultProperty")); + QCOMPARE(p.isResettable(), false); + QCOMPARE(p.reset(), false); + } + + // Resettable prop by name + { + PropertyObject o; + QDeclarativeProperty p(&o, QString("resettableProperty")); + + QCOMPARE(p.read(), QVariant(9)); + QCOMPARE(p.write(QVariant(11)), true); + QCOMPARE(p.read(), QVariant(11)); + + QCOMPARE(p.isResettable(), true); + QCOMPARE(p.reset(), true); + + QCOMPARE(p.read(), QVariant(9)); + } + + // Deleted object + { + PropertyObject *o = new PropertyObject; + + QDeclarativeProperty p(o, QString("resettableProperty")); + + QCOMPARE(p.isResettable(), true); + QCOMPARE(p.reset(), true); + + delete o; + + QCOMPARE(p.isResettable(), false); + QCOMPARE(p.reset(), false); + } + + // Signal property + { + PropertyObject o; + QDeclarativeProperty p(&o, "onClicked"); + + QCOMPARE(p.isResettable(), false); + QCOMPARE(p.reset(), false); + } +} + +void tst_qdeclarativeproperty::writeObjectToList() +{ + QDeclarativeComponent containerComponent(&engine); + containerComponent.setData("import Test 1.0\nMyContainer { children: MyQmlObject {} }", QUrl()); + MyContainer *container = qobject_cast(containerComponent.create()); + QVERIFY(container != 0); + QDeclarativeListReference list(container, "children"); + QVERIFY(list.count() == 1); + + MyQmlObject *object = new MyQmlObject; + QDeclarativeProperty prop(container, "children"); + prop.write(qVariantFromValue(object)); + QCOMPARE(list.count(), 1); + QCOMPARE(list.at(0), object); +} + +Q_DECLARE_METATYPE(QList); +void tst_qdeclarativeproperty::writeListToList() +{ + QDeclarativeComponent containerComponent(&engine); + containerComponent.setData("import Test 1.0\nMyContainer { children: MyQmlObject {} }", QUrl()); + MyContainer *container = qobject_cast(containerComponent.create()); + QVERIFY(container != 0); + QDeclarativeListReference list(container, "children"); + QVERIFY(list.count() == 1); + + QList objList; + objList << new MyQmlObject() << new MyQmlObject() << new MyQmlObject() << new MyQmlObject(); + QDeclarativeProperty prop(container, "children"); + prop.write(qVariantFromValue(objList)); + QCOMPARE(list.count(), 4); + + //XXX need to try this with read/write prop (for read-only it correctly doesn't write) + /*QList typedObjList; + typedObjList << new MyQmlObject(); + prop.write(qVariantFromValue(&typedObjList)); + QCOMPARE(container->children()->size(), 1);*/ +} + +void tst_qdeclarativeproperty::crashOnValueProperty() +{ + QDeclarativeEngine *engine = new QDeclarativeEngine; + QDeclarativeComponent component(engine); + + component.setData("import Test 1.0\nPropertyObject { wrectProperty.x: 10 }", QUrl()); + PropertyObject *obj = qobject_cast(component.create()); + QVERIFY(obj != 0); + + QDeclarativeProperty p(obj, "wrectProperty.x", qmlContext(obj)); + QCOMPARE(p.name(), QString("wrectProperty.x")); + + QCOMPARE(p.read(), QVariant(10)); + + //don't crash once the engine is deleted + delete engine; + engine = 0; + + QCOMPARE(p.propertyTypeName(), "int"); + QCOMPARE(p.read(), QVariant(10)); + p.write(QVariant(20)); + QCOMPARE(p.read(), QVariant(20)); +} + +void tst_qdeclarativeproperty::copy() +{ + PropertyObject object; + + QDeclarativeProperty *property = new QDeclarativeProperty(&object, QLatin1String("defaultProperty")); + QCOMPARE(property->name(), QString("defaultProperty")); + QCOMPARE(property->read(), QVariant(10)); + QCOMPARE(property->type(), QDeclarativeProperty::Property); + QCOMPARE(property->propertyTypeCategory(), QDeclarativeProperty::Normal); + QCOMPARE(property->propertyType(), (int)QVariant::Int); + + QDeclarativeProperty p1(*property); + QCOMPARE(p1.name(), QString("defaultProperty")); + QCOMPARE(p1.read(), QVariant(10)); + QCOMPARE(p1.type(), QDeclarativeProperty::Property); + QCOMPARE(p1.propertyTypeCategory(), QDeclarativeProperty::Normal); + QCOMPARE(p1.propertyType(), (int)QVariant::Int); + + QDeclarativeProperty p2(&object, QLatin1String("url")); + QCOMPARE(p2.name(), QString("url")); + p2 = *property; + QCOMPARE(p2.name(), QString("defaultProperty")); + QCOMPARE(p2.read(), QVariant(10)); + QCOMPARE(p2.type(), QDeclarativeProperty::Property); + QCOMPARE(p2.propertyTypeCategory(), QDeclarativeProperty::Normal); + QCOMPARE(p2.propertyType(), (int)QVariant::Int); + + delete property; property = 0; + + QCOMPARE(p1.name(), QString("defaultProperty")); + QCOMPARE(p1.read(), QVariant(10)); + QCOMPARE(p1.type(), QDeclarativeProperty::Property); + QCOMPARE(p1.propertyTypeCategory(), QDeclarativeProperty::Normal); + QCOMPARE(p1.propertyType(), (int)QVariant::Int); + + QCOMPARE(p2.name(), QString("defaultProperty")); + QCOMPARE(p2.read(), QVariant(10)); + QCOMPARE(p2.type(), QDeclarativeProperty::Property); + QCOMPARE(p2.propertyTypeCategory(), QDeclarativeProperty::Normal); + QCOMPARE(p2.propertyType(), (int)QVariant::Int); +} + +void tst_qdeclarativeproperty::initTestCase() +{ + QML_REGISTER_TYPE(Test,1,0,MyQmlObject,MyQmlObject); + QML_REGISTER_TYPE(Test,1,0,PropertyObject,PropertyObject); + QML_REGISTER_TYPE(Test,1,0,MyContainer,MyContainer); +} + + +QTEST_MAIN(tst_qdeclarativeproperty) + +#include "tst_qdeclarativeproperty.moc" diff --git a/tests/auto/declarative/qdeclarativevaluetypes/testtypes.h b/tests/auto/declarative/qdeclarativevaluetypes/testtypes.h index 82a9c8a2e3..dfc28297eb 100644 --- a/tests/auto/declarative/qdeclarativevaluetypes/testtypes.h +++ b/tests/auto/declarative/qdeclarativevaluetypes/testtypes.h @@ -52,8 +52,8 @@ #include #include #include -#include -#include +#include +#include class MyTypeObject : public QObject { @@ -135,7 +135,7 @@ class MyConstantValueSource : public QObject, public QDeclarativePropertyValueSo { Q_OBJECT public: - virtual void setTarget(const QDeclarativeMetaProperty &p) { p.write(3345); } + virtual void setTarget(const QDeclarativeProperty &p) { p.write(3345); } }; QML_DECLARE_TYPE(MyConstantValueSource); @@ -143,11 +143,11 @@ class MyOffsetValueInterceptor : public QObject, public QDeclarativePropertyValu { Q_OBJECT public: - virtual void setTarget(const QDeclarativeMetaProperty &p) { prop = p; } - virtual void write(const QVariant &value) { QDeclarativeMetaPropertyPrivate::write(prop, value.toInt() + 13, QDeclarativeMetaPropertyPrivate::BypassInterceptor); } + virtual void setTarget(const QDeclarativeProperty &p) { prop = p; } + virtual void write(const QVariant &value) { QDeclarativePropertyPrivate::write(prop, value.toInt() + 13, QDeclarativePropertyPrivate::BypassInterceptor); } private: - QDeclarativeMetaProperty prop; + QDeclarativeProperty prop; }; QML_DECLARE_TYPE(MyOffsetValueInterceptor); -- cgit v1.2.1