From 358a72b6ecd0fef69edf5355f299af10225a6c8b Mon Sep 17 00:00:00 2001 From: Shawn Rutledge Date: Wed, 12 Dec 2018 22:41:13 +0100 Subject: PointHandler: distribute simultaneous touch presses properly If multiple touchpoints are pressed simultaneously, each point can be grabbed by one PointHandler instance. Each PointHandler instance cannot grab more than one point, nor grab a point that is already chosen by another PointHandler. This was always the intention, but got broken (perhaps by 3523b676382db4aa39adeb9126d8bb2185e84403), and there was no test coverage of this case until now. Fixes: QTBUG-71431 Change-Id: I6d7614eb4767c677d929291f917cf62d9c03bd93 Reviewed-by: Andre de la Rocha --- .../qquickpointhandler/data/multiPointTracker.qml | 86 ++++++++++++++++++++++ .../qquickpointhandler/tst_qquickpointhandler.cpp | 79 ++++++++++++++++++++ 2 files changed, 165 insertions(+) create mode 100644 tests/auto/quick/pointerhandlers/qquickpointhandler/data/multiPointTracker.qml (limited to 'tests/auto/quick') diff --git a/tests/auto/quick/pointerhandlers/qquickpointhandler/data/multiPointTracker.qml b/tests/auto/quick/pointerhandlers/qquickpointhandler/data/multiPointTracker.qml new file mode 100644 index 0000000000..3f50d7c761 --- /dev/null +++ b/tests/auto/quick/pointerhandlers/qquickpointhandler/data/multiPointTracker.qml @@ -0,0 +1,86 @@ +/**************************************************************************** +** +** Copyright (C) 2018 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$ +** +****************************************************************************/ + +import QtQuick 2.12 + +Item { + id: root + width: 400; height: 400 + + PointHandler { + id: ph1 + objectName: "ph1" + + target: Rectangle { + parent: root + visible: ph1.active + x: ph1.point.position.x - width / 2 + y: ph1.point.position.y - height / 2 + width: 140 + height: width + radius: width / 2 + color: "orange" + opacity: 0.3 + } + } + + PointHandler { + id: ph2 + objectName: "ph2" + + target: Rectangle { + parent: root + visible: ph2.active + x: ph2.point.position.x - width / 2 + y: ph2.point.position.y - height / 2 + width: 140 + height: width + radius: width / 2 + color: "orange" + opacity: 0.3 + } + } + + PointHandler { + id: ph3 + objectName: "ph3" + + target: Rectangle { + parent: root + visible: ph3.active + x: ph3.point.position.x - width / 2 + y: ph3.point.position.y - height / 2 + width: 140 + height: width + radius: width / 2 + color: "orange" + opacity: 0.3 + } + } +} + diff --git a/tests/auto/quick/pointerhandlers/qquickpointhandler/tst_qquickpointhandler.cpp b/tests/auto/quick/pointerhandlers/qquickpointhandler/tst_qquickpointhandler.cpp index d91c89d833..d0b3bc3d36 100644 --- a/tests/auto/quick/pointerhandlers/qquickpointhandler/tst_qquickpointhandler.cpp +++ b/tests/auto/quick/pointerhandlers/qquickpointhandler/tst_qquickpointhandler.cpp @@ -55,6 +55,7 @@ private slots: void initTestCase(); void singleTouch(); + void simultaneousMultiTouch(); void pressedMultipleButtons_data(); void pressedMultipleButtons(); @@ -135,6 +136,84 @@ void tst_PointHandler::singleTouch() QCOMPARE(translationSpy.count(), 3); } +void tst_PointHandler::simultaneousMultiTouch() +{ + QScopedPointer windowPtr; + createView(windowPtr, "multiPointTracker.qml"); + QQuickView * window = windowPtr.data(); + QList handlers = window->rootObject()->findChildren(); + QCOMPARE(handlers.count(), 3); + + QVector activeSpies; + QVector pointSpies; + QVector translationSpies; + QVector points{{100,100}, {200, 200}, {100, 300}}; + QVector pressPoints = points; + for (auto h : handlers) { + activeSpies << new QSignalSpy(h, SIGNAL(activeChanged())); + pointSpies << new QSignalSpy(h, SIGNAL(pointChanged())); + translationSpies << new QSignalSpy(h, SIGNAL(translationChanged())); + } + + QTest::touchEvent(window, touchDevice).press(1, points[0], window).press(2, points[1], window).press(3, points[2], window); + QQuickTouchUtils::flush(window); + QVector pointIndexPerHandler; + int i = 0; + for (auto h : handlers) { + QTRY_COMPARE(h->active(), true); + QCOMPARE(activeSpies[i]->count(), 1); + QCOMPARE(pointSpies[i]->count(), 1); + int chosenPointIndex = points.indexOf(h->point().position().toPoint()); + QVERIFY(chosenPointIndex != -1); + // Verify that each handler chose a unique point + QVERIFY(!pointIndexPerHandler.contains(chosenPointIndex)); + pointIndexPerHandler.append(chosenPointIndex); + QPoint point = points[chosenPointIndex]; + QCOMPARE(h->point().scenePosition().toPoint(), point); + QCOMPARE(h->point().pressedButtons(), Qt::NoButton); + QCOMPARE(h->translation(), QVector2D()); + QCOMPARE(translationSpies[i]->count(), 1); + ++i; + } + + for (int i = 0; i < 3; ++i) + points[i] += QPoint(10 + 10 * i, 10 + 10 * i % 2); + QTest::touchEvent(window, touchDevice).move(1, points[0], window).move(2, points[1], window).move(3, points[2], window); + QQuickTouchUtils::flush(window); + i = 0; + for (auto h : handlers) { + QCOMPARE(h->active(), true); + QCOMPARE(activeSpies[i]->count(), 1); + QCOMPARE(pointSpies[i]->count(), 2); + QCOMPARE(h->point().position().toPoint(), points[pointIndexPerHandler[i]]); + QCOMPARE(h->point().scenePosition().toPoint(), points[pointIndexPerHandler[i]]); + QCOMPARE(h->point().pressPosition().toPoint(), pressPoints[pointIndexPerHandler[i]]); + QCOMPARE(h->point().scenePressPosition().toPoint(), pressPoints[pointIndexPerHandler[i]]); + QCOMPARE(h->point().pressedButtons(), Qt::NoButton); + QVERIFY(h->point().velocity().x() > 0); + QVERIFY(h->point().velocity().y() > 0); + QCOMPARE(h->translation(), QVector2D(10 + 10 * pointIndexPerHandler[i], 10 + 10 * pointIndexPerHandler[i] % 2)); + QCOMPARE(translationSpies[i]->count(), 2); + ++i; + } + + QTest::touchEvent(window, touchDevice).release(1, points[0], window).release(2, points[1], window).release(3, points[2], window); + QQuickTouchUtils::flush(window); + i = 0; + for (auto h : handlers) { + QTRY_COMPARE(h->active(), false); + QCOMPARE(activeSpies[i]->count(), 2); + QCOMPARE(pointSpies[i]->count(), 3); + QCOMPARE(h->translation(), QVector2D()); + QCOMPARE(translationSpies[i]->count(), 3); + ++i; + } + + qDeleteAll(activeSpies); + qDeleteAll(pointSpies); + qDeleteAll(translationSpies); +} + void tst_PointHandler::pressedMultipleButtons_data() { QTest::addColumn("accepted"); -- cgit v1.2.3 From 12f89fb8dda86c61277e3814912013873b1657e6 Mon Sep 17 00:00:00 2001 From: Shawn Rutledge Date: Wed, 7 Nov 2018 17:48:27 +0100 Subject: TapHandler: clean up when wantsEventPoint returns false We already emitted grabCanceled() to inform QML callbacks, but we didn't call reset(). It seems more proper to do everything that would normally be done when grab is canceled. TapHandler should not give up its passive grab yet though, because that prevents delivery to any parent Flickable that might be filtering events. A parent Flickable should be able to start flicking after the drag threshold is exceeded (it happens to be exactly when TapHandler gives up). Fixes: QTBUG-71466 Fixes: QTBUG-71970 Change-Id: Ibba1b0de92cfd88547eeb44edb095d019de76a94 Reviewed-by: Shawn Rutledge --- .../qquicktaphandler/data/Button.qml | 2 + .../qquicktaphandler/tst_qquicktaphandler.cpp | 54 ++++++++++++++++++++++ 2 files changed, 56 insertions(+) (limited to 'tests/auto/quick') diff --git a/tests/auto/quick/pointerhandlers/qquicktaphandler/data/Button.qml b/tests/auto/quick/pointerhandlers/qquicktaphandler/data/Button.qml index ba22e434ce..221e7df139 100644 --- a/tests/auto/quick/pointerhandlers/qquicktaphandler/data/Button.qml +++ b/tests/auto/quick/pointerhandlers/qquicktaphandler/data/Button.qml @@ -35,6 +35,7 @@ Rectangle { property bool checked: false property alias gesturePolicy: tap.gesturePolicy signal tapped + signal canceled width: label.implicitWidth * 1.5; height: label.implicitHeight * 2.0 border.color: "#9f9d9a"; border.width: 1; radius: height / 4; antialiasing: true @@ -52,6 +53,7 @@ Rectangle { tapFlash.start() root.tapped() } + onCanceled: root.canceled() } Text { diff --git a/tests/auto/quick/pointerhandlers/qquicktaphandler/tst_qquicktaphandler.cpp b/tests/auto/quick/pointerhandlers/qquicktaphandler/tst_qquicktaphandler.cpp index 467c964001..187ade58d5 100644 --- a/tests/auto/quick/pointerhandlers/qquicktaphandler/tst_qquicktaphandler.cpp +++ b/tests/auto/quick/pointerhandlers/qquicktaphandler/tst_qquicktaphandler.cpp @@ -59,6 +59,7 @@ private slots: void touchGesturePolicyDragThreshold(); void mouseGesturePolicyDragThreshold(); + void touchMouseGesturePolicyDragThreshold(); void touchGesturePolicyWithinBounds(); void mouseGesturePolicyWithinBounds(); void touchGesturePolicyReleaseWithinBounds(); @@ -179,6 +180,59 @@ void tst_TapHandler::mouseGesturePolicyDragThreshold() QCOMPARE(dragThresholdTappedSpy.count(), 0); } +void tst_TapHandler::touchMouseGesturePolicyDragThreshold() +{ + QScopedPointer windowPtr; + createView(windowPtr, "buttons.qml"); + QQuickView * window = windowPtr.data(); + + QQuickItem *buttonDragThreshold = window->rootObject()->findChild("DragThreshold"); + QVERIFY(buttonDragThreshold); + QSignalSpy tappedSpy(buttonDragThreshold, SIGNAL(tapped())); + QSignalSpy canceledSpy(buttonDragThreshold, SIGNAL(canceled())); + + // Press mouse, drag it outside the button, release + QPoint p1 = buttonDragThreshold->mapToScene(QPointF(20, 20)).toPoint(); + QPoint p2 = p1 + QPoint(int(buttonDragThreshold->height()), 0); + QTest::mousePress(window, Qt::LeftButton, Qt::NoModifier, p1); + QTRY_VERIFY(buttonDragThreshold->property("pressed").toBool()); + QTest::mouseMove(window, p2); + QTRY_COMPARE(canceledSpy.count(), 1); + QCOMPARE(tappedSpy.count(), 0); + QCOMPARE(buttonDragThreshold->property("pressed").toBool(), false); + QTest::mouseRelease(window, Qt::LeftButton, Qt::NoModifier, p2); + + // Press and release touch, verify that it still works (QTBUG-71466) + QTest::touchEvent(window, touchDevice).press(1, p1, window); + QQuickTouchUtils::flush(window); + QTRY_VERIFY(buttonDragThreshold->property("pressed").toBool()); + QTest::touchEvent(window, touchDevice).release(1, p1, window); + QQuickTouchUtils::flush(window); + QTRY_VERIFY(!buttonDragThreshold->property("pressed").toBool()); + QCOMPARE(tappedSpy.count(), 1); + + // Press touch, drag it outside the button, release + QTest::touchEvent(window, touchDevice).press(1, p1, window); + QQuickTouchUtils::flush(window); + QTRY_VERIFY(buttonDragThreshold->property("pressed").toBool()); + QTest::touchEvent(window, touchDevice).move(1, p2, window); + QQuickTouchUtils::flush(window); + QTRY_COMPARE(buttonDragThreshold->property("pressed").toBool(), false); + QTest::touchEvent(window, touchDevice).release(1, p2, window); + QQuickTouchUtils::flush(window); + QTRY_COMPARE(canceledSpy.count(), 2); + QCOMPARE(tappedSpy.count(), 1); // didn't increase + QCOMPARE(buttonDragThreshold->property("pressed").toBool(), false); + + // Press and release mouse, verify that it still works + QTest::mousePress(window, Qt::LeftButton, Qt::NoModifier, p1); + QTRY_VERIFY(buttonDragThreshold->property("pressed").toBool()); + QTest::mouseRelease(window, Qt::LeftButton, Qt::NoModifier, p1); + QTRY_COMPARE(tappedSpy.count(), 2); + QCOMPARE(canceledSpy.count(), 2); // didn't increase + QCOMPARE(buttonDragThreshold->property("pressed").toBool(), false); +} + void tst_TapHandler::touchGesturePolicyWithinBounds() { QScopedPointer windowPtr; -- cgit v1.2.3