2014-09-29 14:34:05 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2016-01-15 12:36:27 +00:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2014-09-29 14:34:05 +00:00
|
|
|
**
|
|
|
|
** This file is part of the test suite of the Qt Toolkit.
|
|
|
|
**
|
2016-01-15 12:36:27 +00:00
|
|
|
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
|
2014-09-29 14:34:05 +00:00
|
|
|
** 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
|
2015-01-28 08:44:43 +00:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
2016-01-15 12:36:27 +00:00
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2014-09-29 14:34:05 +00:00
|
|
|
**
|
2016-01-15 12:36:27 +00:00
|
|
|
** 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.
|
2014-09-29 14:34:05 +00:00
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
#include <QtTest/QTest>
|
|
|
|
#include <QtWidgets/QApplication>
|
|
|
|
#include <QtWidgets/QWidget>
|
|
|
|
#include <QtWidgets/QGestureEvent>
|
|
|
|
#include <QtGui/QScreen>
|
Introduce QInputDevice hierarchy; replace QTouchDevice
We have seen during the Qt 5 series that QMouseEvent::source() does
not provide enough information: if it is synthesized, it could have
come from any device for which mouse events are synthesized, not only
from a touchscreen. By providing in every QInputEvent as complete
information about the actual source device as possible, we will enable
very fine-tuned behavior in the object that handles each event.
Further, we would like to support multiple keyboards, pointing devices,
and named groups of devices that are known as "seats" in Wayland.
In Qt 5, QPA plugins registered each touchscreen as it was discovered.
Now we extend this pattern to all input devices. This new requirement
can be implemented gradually; for now, if a QTWSI input event is
received wtihout a device pointer, a default "core" device will be
created on-the-fly, and a warning emitted.
In Qt 5, QTouchEvent::TouchPoint::id() was forced to be unique even when
multiple devices were in use simultaneously. Now that each event
identifies the device it came from, this hack is no longer needed.
A stub of the new QPointerEvent is added; it will be developed further
in subsequent patches.
[ChangeLog][QtGui][QInputEvent] Every QInputEvent now carries a pointer
to an instance of QInputDevice, or the subclass QPointingDevice in case
of mouse, touch and tablet events. Each platform plugin is expected to
create the device instances, register them, and provide valid pointers
with all input events. If this is not done, warnings are emitted and
default devices are created as necessary. When the device has accurate
information, it provides the opportunity to fine-tune behavior depending
on device type and capabilities: for example if a QMouseEvent is
synthesized from a touchscreen, the recipient can see which touchscreen
it came from. Each device also has a seatName to distinguish users on
multi-user windowing systems. Touchpoint IDs are no longer unique on
their own, but the combination of ID and device is.
Fixes: QTBUG-46412
Fixes: QTBUG-72167
Task-number: QTBUG-69433
Task-number: QTBUG-52430
Change-Id: I933fb2b86182efa722037b7a33e404c5daf5292a
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
2019-05-31 06:38:16 +00:00
|
|
|
#include <QtGui/QPointingDevice>
|
2020-06-22 14:15:32 +00:00
|
|
|
#include <QtCore/QList>
|
2014-09-29 14:34:05 +00:00
|
|
|
#include <QtCore/QString>
|
|
|
|
#include <QtCore/QHash>
|
|
|
|
#include <QtCore/QDebug>
|
|
|
|
|
|
|
|
class tst_QGestureRecognizer : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
tst_QGestureRecognizer();
|
|
|
|
|
|
|
|
private Q_SLOTS:
|
|
|
|
void initTestCase();
|
|
|
|
#ifndef QT_NO_GESTURES
|
|
|
|
void panGesture_data();
|
|
|
|
void panGesture();
|
|
|
|
void pinchGesture_data();
|
|
|
|
void pinchGesture();
|
|
|
|
void swipeGesture_data();
|
|
|
|
void swipeGesture();
|
|
|
|
#endif // !QT_NO_GESTURES
|
|
|
|
|
|
|
|
private:
|
|
|
|
const int m_fingerDistance;
|
Introduce QInputDevice hierarchy; replace QTouchDevice
We have seen during the Qt 5 series that QMouseEvent::source() does
not provide enough information: if it is synthesized, it could have
come from any device for which mouse events are synthesized, not only
from a touchscreen. By providing in every QInputEvent as complete
information about the actual source device as possible, we will enable
very fine-tuned behavior in the object that handles each event.
Further, we would like to support multiple keyboards, pointing devices,
and named groups of devices that are known as "seats" in Wayland.
In Qt 5, QPA plugins registered each touchscreen as it was discovered.
Now we extend this pattern to all input devices. This new requirement
can be implemented gradually; for now, if a QTWSI input event is
received wtihout a device pointer, a default "core" device will be
created on-the-fly, and a warning emitted.
In Qt 5, QTouchEvent::TouchPoint::id() was forced to be unique even when
multiple devices were in use simultaneously. Now that each event
identifies the device it came from, this hack is no longer needed.
A stub of the new QPointerEvent is added; it will be developed further
in subsequent patches.
[ChangeLog][QtGui][QInputEvent] Every QInputEvent now carries a pointer
to an instance of QInputDevice, or the subclass QPointingDevice in case
of mouse, touch and tablet events. Each platform plugin is expected to
create the device instances, register them, and provide valid pointers
with all input events. If this is not done, warnings are emitted and
default devices are created as necessary. When the device has accurate
information, it provides the opportunity to fine-tune behavior depending
on device type and capabilities: for example if a QMouseEvent is
synthesized from a touchscreen, the recipient can see which touchscreen
it came from. Each device also has a seatName to distinguish users on
multi-user windowing systems. Touchpoint IDs are no longer unique on
their own, but the combination of ID and device is.
Fixes: QTBUG-46412
Fixes: QTBUG-72167
Task-number: QTBUG-69433
Task-number: QTBUG-52430
Change-Id: I933fb2b86182efa722037b7a33e404c5daf5292a
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
2019-05-31 06:38:16 +00:00
|
|
|
QPointingDevice *m_touchDevice;
|
2014-09-29 14:34:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
tst_QGestureRecognizer::tst_QGestureRecognizer()
|
|
|
|
: m_fingerDistance(qRound(QGuiApplication::primaryScreen()->physicalDotsPerInch() / 2.0))
|
2016-05-03 16:27:12 +00:00
|
|
|
, m_touchDevice(QTest::createTouchDevice())
|
2014-09-29 14:34:05 +00:00
|
|
|
{
|
2014-10-30 13:43:51 +00:00
|
|
|
qputenv("QT_PAN_TOUCHPOINTS", "2"); // Prevent device detection of pan touch point count.
|
2014-09-29 14:34:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QGestureRecognizer::initTestCase()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef QT_NO_GESTURES
|
|
|
|
|
2020-06-22 14:15:32 +00:00
|
|
|
typedef QList<Qt::GestureType> GestureTypeVector;
|
2014-09-29 14:34:05 +00:00
|
|
|
|
|
|
|
class TestWidget : public QWidget
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit TestWidget(const GestureTypeVector &gestureTypes);
|
|
|
|
|
|
|
|
bool gestureReceived(Qt::GestureType gestureType) const
|
|
|
|
{ return m_receivedGestures.value(gestureType); }
|
|
|
|
|
|
|
|
protected:
|
2017-09-18 08:36:49 +00:00
|
|
|
bool event(QEvent * event) override;
|
2014-09-29 14:34:05 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
typedef QHash<Qt::GestureType, bool> GestureTypeHash;
|
|
|
|
GestureTypeHash m_receivedGestures;
|
|
|
|
};
|
|
|
|
|
|
|
|
TestWidget::TestWidget(const GestureTypeVector &gestureTypes)
|
|
|
|
{
|
|
|
|
setAttribute(Qt::WA_AcceptTouchEvents);
|
|
|
|
|
|
|
|
foreach (Qt::GestureType gestureType, gestureTypes) {
|
|
|
|
grabGesture(gestureType);
|
|
|
|
m_receivedGestures.insert(gestureType, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
const QRect geometry = QGuiApplication::primaryScreen()->availableGeometry();
|
|
|
|
const QSize size = geometry.size() / 2;
|
|
|
|
resize(size);
|
|
|
|
move(geometry.center() - QPoint(size.width() / 2, size.height() / 2));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TestWidget::event(QEvent * event)
|
|
|
|
{
|
|
|
|
switch (event->type()) {
|
|
|
|
case QEvent::Gesture: {
|
|
|
|
const QGestureEvent *gestureEvent = static_cast<QGestureEvent *>(event);
|
|
|
|
const GestureTypeHash::iterator hend = m_receivedGestures.end();
|
|
|
|
for (GestureTypeHash::iterator it = m_receivedGestures.begin(); it != hend; ++it) {
|
|
|
|
if (const QGesture *gesture = gestureEvent->gesture(it.key())) {
|
|
|
|
if (gesture->state() == Qt::GestureFinished)
|
|
|
|
it.value() = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return QWidget::event(event);
|
|
|
|
}
|
|
|
|
|
2020-06-22 14:15:32 +00:00
|
|
|
static void pressSequence(QTest::QTouchEventSequence &sequence, QList<QPoint> &points,
|
2014-09-29 14:34:05 +00:00
|
|
|
QWidget *widget)
|
|
|
|
{
|
|
|
|
const int pointCount = points.size();
|
|
|
|
for (int p = 0; p < pointCount; ++p)
|
|
|
|
sequence.press(p, points.at(p), widget);
|
|
|
|
sequence.commit();
|
|
|
|
}
|
|
|
|
|
2020-06-22 14:15:32 +00:00
|
|
|
static void linearSequence(int n, const QPoint &delta, QTest::QTouchEventSequence &sequence,
|
|
|
|
QList<QPoint> &points, QWidget *widget)
|
2014-09-29 14:34:05 +00:00
|
|
|
{
|
|
|
|
const int pointCount = points.size();
|
|
|
|
for (int s = 0; s < n; ++s) {
|
|
|
|
for (int p = 0; p < pointCount; ++p) {
|
|
|
|
points[p] += delta;
|
|
|
|
sequence.move(p, points[p], widget);
|
|
|
|
}
|
|
|
|
sequence.commit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 14:15:32 +00:00
|
|
|
static void releaseSequence(QTest::QTouchEventSequence &sequence, QList<QPoint> &points,
|
2014-09-29 14:34:05 +00:00
|
|
|
QWidget *widget)
|
|
|
|
{
|
|
|
|
const int pointCount = points.size();
|
|
|
|
for (int p = 0; p < pointCount; ++p)
|
|
|
|
sequence.release(p, points[p], widget);
|
|
|
|
sequence.commit();
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- Pan
|
|
|
|
|
|
|
|
enum PanSubTest {
|
|
|
|
TwoFingerPanSubTest
|
|
|
|
};
|
|
|
|
|
|
|
|
void tst_QGestureRecognizer::panGesture_data()
|
|
|
|
{
|
|
|
|
QTest::addColumn<int>("panSubTest");
|
|
|
|
QTest::addColumn<bool>("gestureExpected");
|
|
|
|
QTest::newRow("Two finger") << int(TwoFingerPanSubTest) << true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QGestureRecognizer::panGesture()
|
|
|
|
{
|
|
|
|
QFETCH(int, panSubTest);
|
|
|
|
QFETCH(bool, gestureExpected);
|
|
|
|
|
2020-06-27 12:18:09 +00:00
|
|
|
Q_UNUSED(panSubTest); // Single finger pan will be added later.
|
2014-09-29 14:34:05 +00:00
|
|
|
|
|
|
|
const int panPoints = 2;
|
|
|
|
const Qt::GestureType gestureType = Qt::PanGesture;
|
|
|
|
TestWidget widget(GestureTypeVector(1, gestureType));
|
|
|
|
widget.setWindowTitle(QTest::currentTestFunction());
|
|
|
|
widget.show();
|
|
|
|
QVERIFY(QTest::qWaitForWindowExposed(&widget));
|
|
|
|
|
2020-06-22 14:15:32 +00:00
|
|
|
QList<QPoint> points;
|
2014-09-29 14:34:05 +00:00
|
|
|
for (int i = 0; i < panPoints; ++i)
|
|
|
|
points.append(QPoint(10 + i *20, 10 + i *20));
|
|
|
|
|
|
|
|
QTest::QTouchEventSequence panSequence = QTest::touchEvent(&widget, m_touchDevice);
|
|
|
|
pressSequence(panSequence, points, &widget);
|
|
|
|
linearSequence(5, QPoint(20, 20), panSequence, points, &widget);
|
|
|
|
releaseSequence(panSequence, points, &widget);
|
|
|
|
|
|
|
|
if (gestureExpected) {
|
|
|
|
QTRY_VERIFY(widget.gestureReceived(gestureType));
|
|
|
|
} else {
|
|
|
|
QCoreApplication::processEvents();
|
|
|
|
QVERIFY(!widget.gestureReceived(gestureType));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- Pinch
|
|
|
|
|
|
|
|
enum PinchSubTest {
|
|
|
|
StandardPinchSubTest
|
|
|
|
};
|
|
|
|
|
|
|
|
void tst_QGestureRecognizer::pinchGesture_data()
|
|
|
|
{
|
|
|
|
QTest::addColumn<int>("pinchSubTest");
|
|
|
|
QTest::addColumn<bool>("gestureExpected");
|
|
|
|
QTest::newRow("Standard") << int(StandardPinchSubTest) << true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QGestureRecognizer::pinchGesture()
|
|
|
|
{
|
|
|
|
QFETCH(int, pinchSubTest);
|
|
|
|
QFETCH(bool, gestureExpected);
|
|
|
|
|
2020-06-27 12:18:09 +00:00
|
|
|
Q_UNUSED(pinchSubTest);
|
2014-09-29 14:34:05 +00:00
|
|
|
|
|
|
|
const Qt::GestureType gestureType = Qt::PinchGesture;
|
|
|
|
TestWidget widget(GestureTypeVector(1, gestureType));
|
|
|
|
widget.setWindowTitle(QTest::currentTestFunction());
|
|
|
|
widget.show();
|
|
|
|
QVERIFY(QTest::qWaitForWindowExposed(&widget));
|
|
|
|
|
2020-06-22 14:15:32 +00:00
|
|
|
QList<QPoint> points;
|
2014-09-29 14:34:05 +00:00
|
|
|
points.append(widget.rect().center());
|
|
|
|
points.append(points.front() + QPoint(0, 20));
|
|
|
|
|
|
|
|
QTest::QTouchEventSequence pinchSequence = QTest::touchEvent(&widget, m_touchDevice);
|
|
|
|
pressSequence(pinchSequence, points, &widget);
|
|
|
|
|
|
|
|
for (int s = 0; s < 5; ++s) {
|
|
|
|
points[0] += QPoint(5, 30);
|
|
|
|
pinchSequence.move(0, points[0], &widget);
|
|
|
|
points[1] += QPoint(5, -30);
|
|
|
|
pinchSequence.move(1, points[1], &widget);
|
|
|
|
pinchSequence.commit();
|
|
|
|
}
|
|
|
|
|
|
|
|
releaseSequence(pinchSequence, points, &widget);
|
|
|
|
|
|
|
|
if (gestureExpected) {
|
|
|
|
QTRY_VERIFY(widget.gestureReceived(gestureType));
|
|
|
|
} else {
|
|
|
|
QCoreApplication::processEvents();
|
|
|
|
QVERIFY(!widget.gestureReceived(gestureType));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- Swipe
|
|
|
|
|
|
|
|
enum SwipeSubTest {
|
|
|
|
SwipeLineSubTest,
|
2015-05-29 12:02:03 +00:00
|
|
|
SwipeDirectionChangeSubTest,
|
|
|
|
SwipeSmallDirectionChangeSubTest
|
2014-09-29 14:34:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void tst_QGestureRecognizer::swipeGesture_data()
|
|
|
|
{
|
|
|
|
QTest::addColumn<int>("swipeSubTest");
|
|
|
|
QTest::addColumn<bool>("gestureExpected");
|
|
|
|
QTest::newRow("Line") << int(SwipeLineSubTest) << true;
|
2015-05-29 12:02:03 +00:00
|
|
|
QTest::newRow("DirectionChange") << int(SwipeDirectionChangeSubTest) << false;
|
|
|
|
QTest::newRow("SmallDirectionChange") << int(SwipeSmallDirectionChangeSubTest) << true;
|
2014-09-29 14:34:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QGestureRecognizer::swipeGesture()
|
|
|
|
{
|
|
|
|
enum { swipePoints = 3 };
|
|
|
|
|
|
|
|
QFETCH(int, swipeSubTest);
|
|
|
|
QFETCH(bool, gestureExpected);
|
|
|
|
|
|
|
|
const Qt::GestureType gestureType = Qt::SwipeGesture;
|
|
|
|
TestWidget widget(GestureTypeVector(1, gestureType));
|
|
|
|
widget.setWindowTitle(QTest::currentTestFunction());
|
|
|
|
widget.show();
|
|
|
|
QVERIFY(QTest::qWaitForWindowExposed(&widget));
|
|
|
|
|
|
|
|
// Start a swipe sequence with 2 points (QTBUG-15768)
|
|
|
|
const QPoint fingerDistance(m_fingerDistance, m_fingerDistance);
|
2020-06-22 14:15:32 +00:00
|
|
|
QList<QPoint> points;
|
2014-09-29 14:34:05 +00:00
|
|
|
for (int i = 0; i < swipePoints - 1; ++i)
|
|
|
|
points.append(fingerDistance + i * fingerDistance);
|
|
|
|
|
|
|
|
QTest::QTouchEventSequence swipeSequence = QTest::touchEvent(&widget, m_touchDevice);
|
|
|
|
pressSequence(swipeSequence, points, &widget);
|
|
|
|
|
|
|
|
// Press point #3
|
|
|
|
points.append(points.last() + fingerDistance);
|
|
|
|
swipeSequence.press(points.size() - 1, points.last(), &widget);
|
|
|
|
swipeSequence.commit();
|
|
|
|
Q_ASSERT(points.size() == swipePoints);
|
|
|
|
|
|
|
|
// Move.
|
|
|
|
const QPoint moveDelta(60, 20);
|
|
|
|
switch (swipeSubTest) {
|
|
|
|
case SwipeLineSubTest:
|
|
|
|
linearSequence(5, moveDelta, swipeSequence, points, &widget);
|
|
|
|
break;
|
2015-05-29 12:02:03 +00:00
|
|
|
case SwipeDirectionChangeSubTest:
|
2014-10-21 15:31:55 +00:00
|
|
|
linearSequence(5, moveDelta, swipeSequence, points, &widget);
|
|
|
|
linearSequence(3, QPoint(-moveDelta.x(), moveDelta.y()), swipeSequence, points, &widget);
|
|
|
|
break;
|
2015-05-29 12:02:03 +00:00
|
|
|
case SwipeSmallDirectionChangeSubTest: { // QTBUG-46195, small changes in direction should not cause the gesture to be canceled.
|
|
|
|
const QPoint smallChangeMoveDelta(50, 1);
|
|
|
|
linearSequence(5, smallChangeMoveDelta, swipeSequence, points, &widget);
|
|
|
|
linearSequence(1, QPoint(smallChangeMoveDelta.x(), -3), swipeSequence, points, &widget);
|
|
|
|
linearSequence(5, smallChangeMoveDelta, swipeSequence, points, &widget);
|
|
|
|
}
|
|
|
|
break;
|
2014-09-29 14:34:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
releaseSequence(swipeSequence, points, &widget);
|
|
|
|
|
|
|
|
if (gestureExpected) {
|
|
|
|
QTRY_VERIFY(widget.gestureReceived(gestureType));
|
|
|
|
} else {
|
|
|
|
QCoreApplication::processEvents();
|
|
|
|
QVERIFY(!widget.gestureReceived(gestureType));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // !QT_NO_GESTURES
|
|
|
|
|
|
|
|
QTEST_MAIN(tst_QGestureRecognizer)
|
|
|
|
|
|
|
|
#include "tst_qgesturerecognizer.moc"
|