diff options
Diffstat (limited to 'tests/auto')
-rw-r--r-- | tests/auto/quick/qquickitem/data/visualParentOwnership.qml | 14 | ||||
-rw-r--r-- | tests/auto/quick/qquickitem/tst_qquickitem.cpp | 65 |
2 files changed, 79 insertions, 0 deletions
diff --git a/tests/auto/quick/qquickitem/data/visualParentOwnership.qml b/tests/auto/quick/qquickitem/data/visualParentOwnership.qml new file mode 100644 index 0000000000..644d14ba43 --- /dev/null +++ b/tests/auto/quick/qquickitem/data/visualParentOwnership.qml @@ -0,0 +1,14 @@ +import QtQuick 2.0 + +Item { + Component { + id: factory + Item {} + } + + property Item keepAliveProperty; + + function createItemWithoutParent() { + return factory.createObject(/*parent*/ null); + } +} diff --git a/tests/auto/quick/qquickitem/tst_qquickitem.cpp b/tests/auto/quick/qquickitem/tst_qquickitem.cpp index ad3c4fc208..f4f2374183 100644 --- a/tests/auto/quick/qquickitem/tst_qquickitem.cpp +++ b/tests/auto/quick/qquickitem/tst_qquickitem.cpp @@ -49,6 +49,7 @@ #include <qpa/qwindowsysteminterface.h> #include <QDebug> #include <QTimer> +#include <QQmlEngine> #include "../../shared/util.h" class TestItem : public QQuickItem @@ -167,6 +168,8 @@ private slots: void acceptedMouseButtons(); + void visualParentOwnership(); + private: enum PaintOrderOp { @@ -1754,6 +1757,68 @@ void tst_qquickitem::acceptedMouseButtons() QCOMPARE(item.releaseCount, 3); } +static void gc(QQmlEngine &engine) +{ + engine.collectGarbage(); + QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete); + QCoreApplication::processEvents(); +} + +void tst_qquickitem::visualParentOwnership() +{ + QQuickView view; + view.setSource(testFileUrl("visualParentOwnership.qml")); + + QQuickItem *root = qobject_cast<QQuickItem*>(view.rootObject()); + QVERIFY(root); + + QVariant newObject; + { + QVERIFY(QMetaObject::invokeMethod(root, "createItemWithoutParent", Q_RETURN_ARG(QVariant, newObject))); + QPointer<QQuickItem> newItem = qvariant_cast<QQuickItem*>(newObject); + QVERIFY(!newItem.isNull()); + + QVERIFY(!newItem->parent()); + QVERIFY(!newItem->parentItem()); + + newItem->setParentItem(root); + + gc(*view.engine()); + + QVERIFY(!newItem.isNull()); + newItem->setParentItem(0); + + gc(*view.engine()); + QVERIFY(newItem.isNull()); + } + { + QVERIFY(QMetaObject::invokeMethod(root, "createItemWithoutParent", Q_RETURN_ARG(QVariant, newObject))); + QPointer<QQuickItem> firstItem = qvariant_cast<QQuickItem*>(newObject); + QVERIFY(!firstItem.isNull()); + + firstItem->setParentItem(root); + + QVERIFY(QMetaObject::invokeMethod(root, "createItemWithoutParent", Q_RETURN_ARG(QVariant, newObject))); + QPointer<QQuickItem> secondItem = qvariant_cast<QQuickItem*>(newObject); + QVERIFY(!firstItem.isNull()); + + secondItem->setParentItem(firstItem); + + gc(*view.engine()); + + delete firstItem; + + root->setProperty("keepAliveProperty", newObject); + + gc(*view.engine()); + QVERIFY(!secondItem.isNull()); + + root->setProperty("keepAliveProperty", QVariant()); + + gc(*view.engine()); + QVERIFY(secondItem.isNull()); + } +} QTEST_MAIN(tst_qquickitem) |