diff options
Diffstat (limited to 'tests/auto')
3 files changed, 115 insertions, 4 deletions
diff --git a/tests/auto/quick/pointerhandlers/qquickpointerhandler/data/dynamicallyCreated.qml b/tests/auto/quick/pointerhandlers/qquickpointerhandler/data/dynamicallyCreated.qml new file mode 100644 index 0000000000..8f774a7ec3 --- /dev/null +++ b/tests/auto/quick/pointerhandlers/qquickpointerhandler/data/dynamicallyCreated.qml @@ -0,0 +1,34 @@ +import QtQuick 2.12 +import Qt.test 1.0 + +Item { + id: root + objectName: "root Item" + width: 320 + height: 480 + + Rectangle { + objectName: "eventItem's bounds" + anchors.fill: eventItem + color: "lightsteelblue" + } + + EventItem { + id: eventItem + objectName: "eventItem1" + x: 5 + y: 5 + height: 30 + width: 30 + + Component.onCompleted: handlerComponent.createObject(eventItem) + + Component { + id: handlerComponent + + EventHandler { + objectName: "eventHandler" + } + } + } +} diff --git a/tests/auto/quick/pointerhandlers/qquickpointerhandler/data/dynamicallyCreatedInWindow.qml b/tests/auto/quick/pointerhandlers/qquickpointerhandler/data/dynamicallyCreatedInWindow.qml new file mode 100644 index 0000000000..058726b267 --- /dev/null +++ b/tests/auto/quick/pointerhandlers/qquickpointerhandler/data/dynamicallyCreatedInWindow.qml @@ -0,0 +1,20 @@ +import QtQuick 2.12 +import QtQuick.Window 2.12 +import Qt.test 1.0 + +Window { + id: root + objectName: "root Window" + width: 320 + height: 480 + + Component.onCompleted: handlerComponent.createObject(root) + + Component { + id: handlerComponent + + EventHandler { + objectName: "eventHandler" + } + } +} diff --git a/tests/auto/quick/pointerhandlers/qquickpointerhandler/tst_qquickpointerhandler.cpp b/tests/auto/quick/pointerhandlers/qquickpointerhandler/tst_qquickpointerhandler.cpp index e8b076052a..906cbf63e4 100644 --- a/tests/auto/quick/pointerhandlers/qquickpointerhandler/tst_qquickpointerhandler.cpp +++ b/tests/auto/quick/pointerhandlers/qquickpointerhandler/tst_qquickpointerhandler.cpp @@ -182,12 +182,18 @@ public: class EventHandler : public QQuickPointerHandler { +public: void handlePointerEventImpl(QQuickPointerEvent *event) override { QQuickPointerHandler::handlePointerEventImpl(event); if (!enabled()) return; - EventItem *item = static_cast<EventItem *>(target()); + ++eventCount; + EventItem *item = qmlobject_cast<EventItem *>(target()); + if (!item) { + event->point(0)->setGrabberPointerHandler(this); + return; + } qCDebug(lcPointerTests) << item->objectName() << event; int c = event->pointCount(); for (int i = 0; i < c; ++i) { @@ -206,10 +212,13 @@ class EventHandler : public QQuickPointerHandler void onGrabChanged(QQuickPointerHandler *, QQuickEventPoint::GrabTransition stateChange, QQuickEventPoint *point) override { - EventItem *item = static_cast<EventItem *>(target()); - item->eventList.append(Event(Event::HandlerDestination, QEvent::None, - static_cast<Qt::TouchPointState>(point->state()), stateChange, eventPos(point), point->scenePosition())); + EventItem *item = qmlobject_cast<EventItem *>(target()); + if (item) + item->eventList.append(Event(Event::HandlerDestination, QEvent::None, + static_cast<Qt::TouchPointState>(point->state()), stateChange, eventPos(point), point->scenePosition())); } + + int eventCount = 0; }; class tst_PointerHandlers : public QQmlDataTest @@ -228,6 +237,8 @@ private slots: void mouseEventDelivery(); void touchReleaseOutside_data(); void touchReleaseOutside(); + void dynamicCreation(); + void dynamicCreationInWindow(); protected: bool eventFilter(QObject *, QEvent *event) @@ -593,6 +604,52 @@ void tst_PointerHandlers::touchReleaseOutside() QCOMPARE_EVENT(endIndexToTest, endDestination, endType, endState, endGrabState); } +void tst_PointerHandlers::dynamicCreation() +{ + QScopedPointer<QQuickView> windowPtr; + createView(windowPtr, "dynamicallyCreated.qml"); + QQuickView * window = windowPtr.data(); + + EventItem *eventItem1 = window->rootObject()->findChild<EventItem*>("eventItem1"); + QVERIFY(eventItem1); + EventHandler *handler = window->rootObject()->findChild<EventHandler*>("eventHandler"); + QVERIFY(handler); + + QCOMPARE(handler->parentItem(), eventItem1); + QCOMPARE(handler->target(), eventItem1); + + QPoint p1(20, 20); + QTest::mousePress(window, Qt::LeftButton, Qt::NoModifier, p1); + QTRY_COMPARE(eventItem1->eventList.size(), 2); + QCOMPARE_EVENT(0, Event::HandlerDestination, QEvent::Pointer, Qt::TouchPointPressed, NoGrab); + QCOMPARE_EVENT(1, Event::MouseDestination, QEvent::MouseButtonPress, Qt::TouchPointPressed, NoGrab); + QTest::mouseRelease(window, Qt::LeftButton, Qt::NoModifier, p1); +} + +void tst_PointerHandlers::dynamicCreationInWindow() +{ + QQmlEngine engine; + QQmlComponent component(&engine); + component.loadUrl(testFileUrl("dynamicallyCreatedInWindow.qml")); + QQuickWindow *window = qobject_cast<QQuickWindow*>(component.create()); + QScopedPointer<QQuickWindow> cleanup(window); + QVERIFY(window); + window->show(); + QVERIFY(QTest::qWaitForWindowExposed(window)); + + EventHandler *handler = window->contentItem()->findChild<EventHandler*>("eventHandler"); + QVERIFY(handler); + + QCOMPARE(handler->parentItem(), window->contentItem()); + QCOMPARE(handler->target(), window->contentItem()); + + QPoint p1(20, 20); + QTest::mousePress(window, Qt::LeftButton, Qt::NoModifier, p1); + QTRY_COMPARE(handler->eventCount, 1); + QTest::mouseRelease(window, Qt::LeftButton, Qt::NoModifier, p1); + QTRY_COMPARE(handler->eventCount, 2); +} + QTEST_MAIN(tst_PointerHandlers) #include "tst_qquickpointerhandler.moc" |