diff options
Diffstat (limited to 'tests/auto/qml/qqmlproperty')
-rw-r--r-- | tests/auto/qml/qqmlproperty/data/aliasToBinding.qml | 23 | ||||
-rw-r--r-- | tests/auto/qml/qqmlproperty/data/assignEmptyVariantMap.qml | 1 | ||||
-rw-r--r-- | tests/auto/qml/qqmlproperty/data/interfaceBinding2.qml | 27 | ||||
-rw-r--r-- | tests/auto/qml/qqmlproperty/interfaces.h | 161 | ||||
-rw-r--r-- | tests/auto/qml/qqmlproperty/qqmlproperty.pro | 8 | ||||
-rw-r--r-- | tests/auto/qml/qqmlproperty/tst_qqmlproperty.cpp | 158 |
6 files changed, 278 insertions, 100 deletions
diff --git a/tests/auto/qml/qqmlproperty/data/aliasToBinding.qml b/tests/auto/qml/qqmlproperty/data/aliasToBinding.qml new file mode 100644 index 0000000000..54f9e3f944 --- /dev/null +++ b/tests/auto/qml/qqmlproperty/data/aliasToBinding.qml @@ -0,0 +1,23 @@ +import QtQuick 2.7 + +Item { + id: _window + property bool userFontStrikeout: true + + Component.onCompleted: { + _box.font.strikeout = Qt.binding(function() { return _window.userFontStrikeout; }); + } + + Rectangle { + id: _box + width: 100 + height: 100 + property alias font: _text.font + + Text { + id: _text + anchors.fill: parent + text: "Text" + } + } +} diff --git a/tests/auto/qml/qqmlproperty/data/assignEmptyVariantMap.qml b/tests/auto/qml/qqmlproperty/data/assignEmptyVariantMap.qml index a9e51c1255..440f07ac87 100644 --- a/tests/auto/qml/qqmlproperty/data/assignEmptyVariantMap.qml +++ b/tests/auto/qml/qqmlproperty/data/assignEmptyVariantMap.qml @@ -1,5 +1,6 @@ import QtQuick 2.0 Item { + required property QtObject o Component.onCompleted: { o.variantMap = {}; } } diff --git a/tests/auto/qml/qqmlproperty/data/interfaceBinding2.qml b/tests/auto/qml/qqmlproperty/data/interfaceBinding2.qml new file mode 100644 index 0000000000..e7c5dc7344 --- /dev/null +++ b/tests/auto/qml/qqmlproperty/data/interfaceBinding2.qml @@ -0,0 +1,27 @@ +import QtQuick 2.12 +import io.qt.bugreports 2.0 +Item { + InterfaceConsumer2 { + objectName: "a1" + i: A2 { + property int i: 42 + } + } + + InterfaceConsumer2 { + objectName: "a2" + property A2 a: A2 { + property int i: 43 + } + i: a + } + + InterfaceConsumer2 { + objectName: "a3" + property A2 a: A2 { + id : aa + property int i: 44 + } + i: aa + } +} diff --git a/tests/auto/qml/qqmlproperty/interfaces.h b/tests/auto/qml/qqmlproperty/interfaces.h new file mode 100644 index 0000000000..2c06c5f594 --- /dev/null +++ b/tests/auto/qml/qqmlproperty/interfaces.h @@ -0,0 +1,161 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef INTERFACES_H +#define INTERFACES_H + +#include <QtQml/qqml.h> + +struct Interface { +}; + +QT_BEGIN_NAMESPACE +#define MyInterface_iid "io.qt.bugreports.Interface" +Q_DECLARE_INTERFACE(Interface, MyInterface_iid); +QT_END_NAMESPACE + +class A : public QObject, Interface { + Q_OBJECT + Q_INTERFACES(Interface) +}; + +class B : public QObject, Interface { + Q_OBJECT + Q_INTERFACES(Interface) +}; + +class C : public QObject { + Q_OBJECT +}; + +struct Interface2 +{ + Q_GADGET + QML_INTERFACE +}; + +QT_BEGIN_NAMESPACE +#define MyInterface2_iid "io.qt.bugreports.Interface2" +Q_DECLARE_INTERFACE(Interface2, MyInterface2_iid); +QT_END_NAMESPACE + +class A2 : public QObject, Interface2 { + Q_OBJECT + QML_ELEMENT + Q_INTERFACES(Interface2) +}; + +class B2 : public QObject, Interface2 { + Q_OBJECT + QML_ELEMENT + Q_INTERFACES(Interface2) +}; + +class C2 : public QObject { + Q_OBJECT + QML_ELEMENT +}; + +class InterfaceConsumer : public QObject { + Q_OBJECT + Q_PROPERTY(Interface *i READ interface WRITE setInterface NOTIFY interfaceChanged) + Q_PROPERTY(int testValue READ testValue NOTIFY testValueChanged) + +public: + + Interface* interface() const + { + return m_interface; + } + void setInterface(Interface* interface) + { + QObject* object = reinterpret_cast<QObject*>(interface); + m_testValue = object->property("i").toInt(); + emit testValueChanged(); + if (m_interface == interface) + return; + + m_interface = interface; + emit interfaceChanged(); + } + + int testValue() { + return m_testValue; + } + +signals: + void interfaceChanged(); + void testValueChanged(); + +private: + Interface* m_interface = nullptr; + int m_testValue = 0; +}; + + +class InterfaceConsumer2 : public QObject +{ + Q_OBJECT + + Q_PROPERTY(Interface2 *i READ interface WRITE setInterface NOTIFY interfaceChanged) + Q_PROPERTY(int testValue READ testValue NOTIFY testValueChanged) + + QML_ELEMENT + +public: + + Interface2* interface() const + { + return m_interface; + } + void setInterface(Interface2* interface2) + { + QObject* object = reinterpret_cast<QObject*>(interface2); + m_testValue = object->property("i").toInt(); + emit testValueChanged(); + if (m_interface == interface2) + return; + + m_interface = interface2; + emit interfaceChanged(); + } + + int testValue() { + return m_testValue; + } + +signals: + void interfaceChanged(); + void testValueChanged(); + +private: + Interface2 *m_interface = nullptr; + int m_testValue = 0; +}; + +#endif // INTERFACES_H diff --git a/tests/auto/qml/qqmlproperty/qqmlproperty.pro b/tests/auto/qml/qqmlproperty/qqmlproperty.pro index b1bcf1f17d..4d42975369 100644 --- a/tests/auto/qml/qqmlproperty/qqmlproperty.pro +++ b/tests/auto/qml/qqmlproperty/qqmlproperty.pro @@ -1,7 +1,10 @@ -CONFIG += testcase +CONFIG += testcase qmltypes TARGET = tst_qqmlproperty macx:CONFIG -= app_bundle +QML_IMPORT_NAME = io.qt.bugreports +QML_IMPORT_VERSION = 2.0 + SOURCES += tst_qqmlproperty.cpp include (../../shared/util.pri) @@ -9,3 +12,6 @@ include (../../shared/util.pri) TESTDATA = data/* QT += core-private gui-private qml-private testlib + +HEADERS += \ + interfaces.h diff --git a/tests/auto/qml/qqmlproperty/tst_qqmlproperty.cpp b/tests/auto/qml/qqmlproperty/tst_qqmlproperty.cpp index 864a47e998..8a96fc52c5 100644 --- a/tests/auto/qml/qqmlproperty/tst_qqmlproperty.cpp +++ b/tests/auto/qml/qqmlproperty/tst_qqmlproperty.cpp @@ -25,6 +25,8 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ + +#include "interfaces.h" #include <qtest.h> #include <QtQml/qqmlengine.h> #include <QtQml/qqmlcomponent.h> @@ -157,6 +159,8 @@ private slots: void copy(); + void bindingToAlias(); + void nestedQQmlPropertyMap(); void underscorePropertyChangeHandler(); @@ -1224,10 +1228,10 @@ void tst_qqmlproperty::read() } { QQmlComponent component(&engine, testFileUrl("readSynthesizedObject.qml")); - QObject *object = component.create(); + QScopedPointer<QObject> object(component.create()); QVERIFY(object != nullptr); - QQmlProperty p(object, "test", &engine); + QQmlProperty p(object.data(), "test", &engine); QCOMPARE(p.propertyTypeCategory(), QQmlProperty::Object); QVERIFY(p.propertyType() != QMetaType::QObjectStar); @@ -1239,10 +1243,10 @@ void tst_qqmlproperty::read() } { // static QQmlComponent component(&engine, testFileUrl("readSynthesizedObject.qml")); - QObject *object = component.create(); + QScopedPointer<QObject> object(component.create()); QVERIFY(object != nullptr); - QVariant v = QQmlProperty::read(object, "test", &engine); + QVariant v = QQmlProperty::read(object.data(), "test", &engine); QCOMPARE(v.userType(), int(QMetaType::QObjectStar)); QCOMPARE(qvariant_cast<QObject *>(v)->property("a").toInt(), 10); QCOMPARE(qvariant_cast<QObject *>(v)->property("b").toInt(), 19); @@ -1252,41 +1256,38 @@ void tst_qqmlproperty::read() { QQmlComponent component(&engine); component.setData("import Test 1.0\nMyContainer { }", QUrl()); - QObject *object = component.create(); + QScopedPointer<QObject> object(component.create()); QVERIFY(object != nullptr); - QQmlProperty p(object, "MyContainer.foo", qmlContext(object)); + QQmlProperty p(object.data(), "MyContainer.foo", qmlContext(object.data())); QCOMPARE(p.read(), QVariant(13)); - delete object; } { QQmlComponent component(&engine); component.setData("import Test 1.0\nMyContainer { MyContainer.foo: 10 }", QUrl()); - QObject *object = component.create(); + QScopedPointer<QObject> object(component.create()); QVERIFY(object != nullptr); - QQmlProperty p(object, "MyContainer.foo", qmlContext(object)); + QQmlProperty p(object.data(), "MyContainer.foo", qmlContext(object.data())); QCOMPARE(p.read(), QVariant(10)); - delete object; } { QQmlComponent component(&engine); component.setData("import Test 1.0 as Foo\nFoo.MyContainer { Foo.MyContainer.foo: 10 }", QUrl()); - QObject *object = component.create(); + QScopedPointer<QObject> object(component.create()); QVERIFY(object != nullptr); - QQmlProperty p(object, "Foo.MyContainer.foo", qmlContext(object)); + QQmlProperty p(object.data(), "Foo.MyContainer.foo", qmlContext(object.data())); QCOMPARE(p.read(), QVariant(10)); - delete object; } { // static QQmlComponent component(&engine); component.setData("import Test 1.0 as Foo\nFoo.MyContainer { Foo.MyContainer.foo: 10 }", QUrl()); - QObject *object = component.create(); + QScopedPointer<QObject> object(component.create()); QVERIFY(object != nullptr); - QCOMPARE(QQmlProperty::read(object, "Foo.MyContainer.foo", qmlContext(object)), QVariant(10)); - delete object; + QCOMPARE(QQmlProperty::read(object.data(), "Foo.MyContainer.foo", + qmlContext(object.data())), QVariant(10)); } } @@ -1447,11 +1448,12 @@ void tst_qqmlproperty::write() { // QChar -> QString QQmlComponent component(&engine); component.setData("import Test 1.0\nPropertyObject { stringProperty: constQChar }", QUrl()); - PropertyObject *obj = qobject_cast<PropertyObject*>(component.create()); - QVERIFY(obj != nullptr); - if (obj) { - QQmlProperty stringProperty(obj, "stringProperty"); - QCOMPARE(stringProperty.read(), QVariant(QString(obj->constQChar()))); + QScopedPointer<QObject> object(component.create()); + PropertyObject *propertyObject = qobject_cast<PropertyObject*>(object.data()); + QVERIFY(propertyObject != nullptr); + if (propertyObject) { + QQmlProperty stringProperty(propertyObject, "stringProperty"); + QCOMPARE(stringProperty.read(), QVariant(QString(propertyObject->constQChar()))); } } @@ -1628,29 +1630,32 @@ void tst_qqmlproperty::writeObjectToList() { QQmlComponent containerComponent(&engine); containerComponent.setData("import Test 1.0\nMyContainer { children: MyQmlObject {} }", QUrl()); - MyContainer *container = qobject_cast<MyContainer*>(containerComponent.create()); + QScopedPointer<QObject> object(containerComponent.create()); + MyContainer *container = qobject_cast<MyContainer*>(object.data()); QVERIFY(container != nullptr); QQmlListReference list(container, "children"); QCOMPARE(list.count(), 1); - MyQmlObject *object = new MyQmlObject; + QScopedPointer<MyQmlObject> childObject(new MyQmlObject); QQmlProperty prop(container, "children"); - prop.write(QVariant::fromValue(object)); + prop.write(QVariant::fromValue(childObject.data())); QCOMPARE(list.count(), 1); - QCOMPARE(list.at(0), qobject_cast<QObject*>(object)); + QCOMPARE(list.at(0), qobject_cast<QObject*>(childObject.data())); } void tst_qqmlproperty::writeListToList() { QQmlComponent containerComponent(&engine); containerComponent.setData("import Test 1.0\nMyContainer { children: MyQmlObject {} }", QUrl()); - MyContainer *container = qobject_cast<MyContainer*>(containerComponent.create()); + QScopedPointer<QObject> object(containerComponent.create()); + MyContainer *container = qobject_cast<MyContainer*>(object.data()); QVERIFY(container != nullptr); QQmlListReference list(container, "children"); QCOMPARE(list.count(), 1); QList<QObject*> objList; - objList << new MyQmlObject() << new MyQmlObject() << new MyQmlObject() << new MyQmlObject(); + objList << new MyQmlObject(this) << new MyQmlObject(this) + << new MyQmlObject(this) << new MyQmlObject(this); QQmlProperty prop(container, "children"); prop.write(QVariant::fromValue(objList)); QCOMPARE(list.count(), 4); @@ -1828,10 +1833,11 @@ void tst_qqmlproperty::crashOnValueProperty() QQmlComponent component(engine); component.setData("import Test 1.0\nPropertyObject { wrectProperty.x: 10 }", QUrl()); - PropertyObject *obj = qobject_cast<PropertyObject*>(component.create()); - QVERIFY(obj != nullptr); + QScopedPointer<QObject> object(component.create()); + PropertyObject *propertyObject = qobject_cast<PropertyObject*>(object.data()); + QVERIFY(propertyObject != nullptr); - QQmlProperty p(obj, "wrectProperty.x", qmlContext(obj)); + QQmlProperty p(propertyObject, "wrectProperty.x", qmlContext(propertyObject)); QCOMPARE(p.name(), QString("wrectProperty.x")); QCOMPARE(p.read(), QVariant(10)); @@ -1999,11 +2005,9 @@ void tst_qqmlproperty::assignEmptyVariantMap() QCOMPARE(o.variantMap().count(), 1); QCOMPARE(o.variantMap().isEmpty(), false); - QQmlContext context(&engine); - context.setContextProperty("o", &o); QQmlComponent component(&engine, testFileUrl("assignEmptyVariantMap.qml")); - QObject *obj = component.create(&context); + QObject *obj = component.createWithInitialProperties({{"o", QVariant::fromValue(&o)}}); QVERIFY(obj); QCOMPARE(o.variantMap().count(), 0); @@ -2090,74 +2094,20 @@ void tst_qqmlproperty::nullPropertyBinding() QMetaObject::invokeMethod(root.get(), "tog"); } -struct Interface { -}; - -QT_BEGIN_NAMESPACE -#define MyInterface_iid "io.qt.bugreports.Interface" -Q_DECLARE_INTERFACE(Interface, MyInterface_iid); -QT_END_NAMESPACE - -class A : public QObject, Interface { - Q_OBJECT - Q_INTERFACES(Interface) -}; - -class B : public QObject, Interface { - Q_OBJECT - Q_INTERFACES(Interface) -}; - -class C : public QObject { - Q_OBJECT -}; - -class InterfaceConsumer : public QObject { - Q_OBJECT - Q_PROPERTY(Interface* i READ interface WRITE setInterface NOTIFY interfaceChanged) - Q_PROPERTY(int testValue READ testValue NOTIFY testValueChanged) - - -public: - - Interface* interface() const - { - return m_interface; - } - void setInterface(Interface* interface) - { - QObject* object = reinterpret_cast<QObject*>(interface); - m_testValue = object->property("i").toInt(); - emit testValueChanged(); - if (m_interface == interface) - return; - - m_interface = interface; - emit interfaceChanged(); - } - - int testValue() { - return m_testValue; - } - -signals: - void interfaceChanged(); - void testValueChanged(); - -private: - Interface* m_interface = nullptr; - int m_testValue = 0; -}; void tst_qqmlproperty::interfaceBinding() { - - qmlRegisterInterface<Interface>("Interface"); - qmlRegisterType<A>("io.qt.bugreports", 1, 0, "A"); - qmlRegisterType<B>("io.qt.bugreports", 1, 0, "B"); - qmlRegisterType<C>("io.qt.bugreports", 1, 0, "C"); - qmlRegisterType<InterfaceConsumer>("io.qt.bugreports", 1, 0, "InterfaceConsumer"); - - const QUrl url = testFileUrl("interfaceBinding.qml"); + qmlRegisterInterface<Interface>("Interface"); + qmlRegisterType<A>("io.qt.bugreports", 1, 0, "A"); + qmlRegisterType<B>("io.qt.bugreports", 1, 0, "B"); + qmlRegisterType<C>("io.qt.bugreports", 1, 0, "C"); + qmlRegisterType<InterfaceConsumer>("io.qt.bugreports", 1, 0, "InterfaceConsumer"); + + const QVector<QUrl> urls = { + testFileUrl("interfaceBinding.qml"), + testFileUrl("interfaceBinding2.qml") + }; + + for (const QUrl &url : urls) { QQmlEngine engine; QQmlComponent component(&engine, url); QScopedPointer<QObject> root(component.create()); @@ -2165,6 +2115,7 @@ void tst_qqmlproperty::interfaceBinding() QCOMPARE(root->findChild<QObject*>("a1")->property("testValue").toInt(), 42); QCOMPARE(root->findChild<QObject*>("a2")->property("testValue").toInt(), 43); QCOMPARE(root->findChild<QObject*>("a3")->property("testValue").toInt(), 44); + } } void tst_qqmlproperty::floatToStringPrecision_data() @@ -2214,6 +2165,15 @@ void tst_qqmlproperty::initTestCase() qmlRegisterType<MyContainer>("Test",1,0,"MyContainer"); } +// QTBUG-60908 +void tst_qqmlproperty::bindingToAlias() +{ + QQmlEngine engine; + QQmlComponent component(&engine, testFileUrl("aliasToBinding.qml")); + QScopedPointer<QObject> o(component.create()); + QVERIFY(!o.isNull()); +} + void tst_qqmlproperty::nestedQQmlPropertyMap() { QQmlPropertyMap mainPropertyMap; |