diff options
author | Fabian Kosmale <fabian.kosmale@qt.io> | 2023-11-06 15:21:24 +0100 |
---|---|---|
committer | Sami Varanka <sami.varanka@qt.io> | 2023-11-15 18:54:42 +0200 |
commit | 9331947a9de4e500de7db56b096618713c947419 (patch) | |
tree | 3685546c086f530a8c6c52476c40ba827d92b85d /tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp | |
parent | a1a0241946906b6ae81e5a3926c6af925d8b8073 (diff) |
Detect slot object if explicitly provided
If explicitly context is given to function when connecting, it is used
as the receiver. Then when the context is destroyed, the connection
will also be cleared.
Pick-to: 6.6
Fixes: QTBUG-29676
Change-Id: Iec9318132245094603f71e3e6279d65c8021cb7e
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp')
-rw-r--r-- | tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp index a45efc793c..787bac1520 100644 --- a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp +++ b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp @@ -3862,6 +3862,64 @@ void tst_qqmlecmascript::scriptConnect() QScopedPointer<QObject> root { component.create() }; QVERIFY2(root, qPrintable(component.errorString())); } + + { + QQmlComponent component(&engine, testFileUrl("scriptConnect.8.qml")); + + QScopedPointer<QObject> obj(component.create()); + QVERIFY2(obj, qPrintable(component.errorString())); + QVERIFY(obj.data() != nullptr); + + QCOMPARE(obj.data()->property("count"), 0); + + QMetaObject::invokeMethod(obj.data(), "someSignal"); + QCOMPARE(obj.data()->property("count"), 1); + + QMetaObject::invokeMethod(obj.data(), "itemDestroy"); + QCoreApplication::sendPostedEvents(nullptr, QEvent::DeferredDelete); + QCoreApplication::processEvents(); + + QMetaObject::invokeMethod(obj.data(), "someSignal"); + QCOMPARE(obj.data()->property("count"), 1); + } + + { + QQmlComponent component(&engine, testFileUrl("scriptConnect.9.qml")); + + QScopedPointer<QObject> obj(component.create()); + QVERIFY2(obj, qPrintable(component.errorString())); + QVERIFY(obj.data() != nullptr); + + MyQmlObject *object = qobject_cast<MyQmlObject *>(obj.data()); + + QCOMPARE(object->property("a"), 0); + + QMetaObject::invokeMethod(object, "someSignal"); + QCOMPARE(object->property("a"), 1); + + QMetaObject::invokeMethod(object, "destroyObj", Qt::DirectConnection); + QApplication::sendPostedEvents(nullptr, QEvent::DeferredDelete); + QApplication::processEvents(); + + QMetaObject::invokeMethod(object, "someSignal"); + + QCOMPARE(object->property("a"), 1); + } + + { + QQmlComponent component(&engine, testFileUrl("scriptConnectSingleton.qml")); + + QScopedPointer<QObject> obj(component.create()); + QVERIFY2(obj, qPrintable(component.errorString())); + QVERIFY(obj.data() != nullptr); + + QMetaObject::invokeMethod(obj.data(), "mySignal", Qt::DirectConnection); + QCOMPARE(obj.data()->property("a").toInt(), 1); + engine.clearSingletons(); + QMetaObject::invokeMethod(obj.data(), "mySignal", Qt::DirectConnection); + QCOMPARE(obj.data()->property("a").toInt(), 1); + + } } void tst_qqmlecmascript::scriptDisconnect() @@ -3942,6 +4000,60 @@ void tst_qqmlecmascript::scriptDisconnect() emit object->argumentSignal(19, "Hello world!", 10.25, MyQmlObject::EnumValue4, Qt::RightButton); QCOMPARE(object->property("test").toInt(), 3); } + + { + QQmlComponent component(&engine, testFileUrl("scriptDisconnect.5.qml")); + + QScopedPointer<QObject> obj(component.create()); + QVERIFY2(obj, qPrintable(component.errorString())); + QVERIFY(obj.data() != nullptr); + + QCOMPARE(obj.data()->property("count"), 0); + + QMetaObject::invokeMethod(obj.data(), "someSignal"); + QCOMPARE(obj.data()->property("count"), 1); + + QMetaObject::invokeMethod(obj.data(), "disconnectSignal"); + + QMetaObject::invokeMethod(obj.data(), "someSignal"); + QCOMPARE(obj.data()->property("count"), 1); + } + + { + QQmlComponent component(&engine, testFileUrl("scriptConnect.9.qml")); + + QScopedPointer<QObject> obj(component.create()); + QVERIFY2(obj, qPrintable(component.errorString())); + QVERIFY(obj.data() != nullptr); + + MyQmlObject *object = qobject_cast<MyQmlObject *>(obj.data()); + + QCOMPARE(object->property("a"), 0); + + QMetaObject::invokeMethod(object, "someSignal"); + QCOMPARE(object->property("a"), 1); + + QMetaObject::invokeMethod(object, "disconnectSignal", Qt::DirectConnection); + + QMetaObject::invokeMethod(object, "someSignal"); + + QCOMPARE(object->property("a"), 1); + } + + { + QQmlComponent component(&engine, testFileUrl("scriptConnectSingleton.qml")); + + QScopedPointer<QObject> obj(component.create()); + QVERIFY2(obj, qPrintable(component.errorString())); + QVERIFY(obj.data() != nullptr); + + QMetaObject::invokeMethod(obj.data(), "mySignal", Qt::DirectConnection); + QCOMPARE(obj.data()->property("a").toInt(), 1); + + QMetaObject::invokeMethod(obj.data(), "disconnectSingleton", Qt::DirectConnection); + QMetaObject::invokeMethod(obj.data(), "mySignal", Qt::DirectConnection); + QCOMPARE(obj.data()->property("a").toInt(), 1); + } } class OwnershipObject : public QObject |