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') 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