summaryrefslogtreecommitdiffstats
path: root/src/gui/kernel
diff options
context:
space:
mode:
authorShawn Rutledge <shawn.rutledge@qt.io>2020-09-03 13:32:55 +0200
committerShawn Rutledge <shawn.rutledge@qt.io>2020-09-08 08:02:15 +0200
commit0e475eeea65305efd94e38bff8fc806b7cfddd72 (patch)
tree2664f9326e9242a6c56adee5f4bc669ddf924b9b /src/gui/kernel
parent2f0f74498a3eb00950688750baccc846d4e11f6b (diff)
Refactor testlib touch functions into qtestsupport_gui and _widgets
Because we removed public setters from QTouchEvent and QEventPoint in 4e400369c08db251cd489fec1229398c224d02b4 and now it's proposed to give QEventPoint a d-pointer again, the implementation of QTouchEventSequence needs to start using QMutableEventPoint: being a friend will no longer be enough, because the member variables won't be accessible in the future. But because we have separate test libs for Gui and Widgets, it needs to be further refactored into two classes. Change-Id: I0bfc0978fc4187348ac872e1330d95259d557b69 Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'src/gui/kernel')
-rw-r--r--src/gui/kernel/qtestsupport_gui.cpp81
-rw-r--r--src/gui/kernel/qtestsupport_gui.h41
2 files changed, 121 insertions, 1 deletions
diff --git a/src/gui/kernel/qtestsupport_gui.cpp b/src/gui/kernel/qtestsupport_gui.cpp
index 79da26f2ca..ab48111fb5 100644
--- a/src/gui/kernel/qtestsupport_gui.cpp
+++ b/src/gui/kernel/qtestsupport_gui.cpp
@@ -45,6 +45,7 @@
#include "qwindow.h"
#include <QtCore/qtestsupport_core.h>
+#include <QtCore/qthread.h>
#include <QtCore/QDebug>
QT_BEGIN_NAMESPACE
@@ -91,4 +92,84 @@ Q_GUI_EXPORT bool QTest::qWaitForWindowExposed(QWindow *window, int timeout)
return QTest::qWaitFor([&]() { return window->isExposed(); }, timeout);
}
+namespace QTest {
+
+QTouchEventSequence::~QTouchEventSequence()
+{
+ if (commitWhenDestroyed)
+ commit();
+}
+QTouchEventSequence& QTouchEventSequence::press(int touchId, const QPoint &pt, QWindow *window)
+{
+ auto &p = QMutableEventPoint::from(point(touchId));
+ p.setGlobalPosition(mapToScreen(window, pt));
+ p.setState(QEventPoint::State::Pressed);
+ return *this;
+}
+QTouchEventSequence& QTouchEventSequence::move(int touchId, const QPoint &pt, QWindow *window)
+{
+ auto &p = QMutableEventPoint::from(point(touchId));
+ p.setGlobalPosition(mapToScreen(window, pt));
+ p.setState(QEventPoint::State::Updated);
+ return *this;
+}
+QTouchEventSequence& QTouchEventSequence::release(int touchId, const QPoint &pt, QWindow *window)
+{
+ auto &p = QMutableEventPoint::from(point(touchId));
+ p.setGlobalPosition(mapToScreen(window, pt));
+ p.setState(QEventPoint::State::Released);
+ return *this;
+}
+QTouchEventSequence& QTouchEventSequence::stationary(int touchId)
+{
+ auto &p = QMutableEventPoint::from(pointOrPreviousPoint(touchId));
+ p.setState(QEventPoint::State::Stationary);
+ return *this;
+}
+
+void QTouchEventSequence::commit(bool processEvents)
+{
+ if (points.isEmpty())
+ return;
+ QThread::msleep(1);
+ if (targetWindow)
+ qt_handleTouchEvent(targetWindow, device, points.values());
+ if (processEvents)
+ QCoreApplication::processEvents();
+ previousPoints = points;
+ points.clear();
+}
+
+QTouchEventSequence::QTouchEventSequence(QWindow *window, QPointingDevice *aDevice, bool autoCommit)
+ : targetWindow(window), device(aDevice), commitWhenDestroyed(autoCommit)
+{
+}
+
+QPoint QTouchEventSequence::mapToScreen(QWindow *window, const QPoint &pt)
+{
+ if (window)
+ return window->mapToGlobal(pt);
+ return targetWindow ? targetWindow->mapToGlobal(pt) : pt;
+}
+
+QEventPoint &QTouchEventSequence::point(int touchId)
+{
+ if (!points.contains(touchId))
+ points[touchId] = QEventPoint(touchId);
+ return points[touchId];
+}
+
+QEventPoint &QTouchEventSequence::pointOrPreviousPoint(int touchId)
+{
+ if (!points.contains(touchId)) {
+ if (previousPoints.contains(touchId))
+ points[touchId] = previousPoints.value(touchId);
+ else
+ points[touchId] = QEventPoint(touchId);
+ }
+ return points[touchId];
+}
+
+} // namespace QTest
+
QT_END_NAMESPACE
diff --git a/src/gui/kernel/qtestsupport_gui.h b/src/gui/kernel/qtestsupport_gui.h
index 86ff956ef8..5f1ac12936 100644
--- a/src/gui/kernel/qtestsupport_gui.h
+++ b/src/gui/kernel/qtestsupport_gui.h
@@ -41,15 +41,54 @@
#define QTESTSUPPORT_GUI_H
#include <QtGui/qtguiglobal.h>
+#include <QtGui/qevent.h>
+#include <QtCore/qmap.h>
QT_BEGIN_NAMESPACE
class QWindow;
+Q_GUI_EXPORT void qt_handleTouchEvent(QWindow *w, const QPointingDevice *device,
+ const QList<QEventPoint> &points,
+ Qt::KeyboardModifiers mods = Qt::NoModifier);
+
namespace QTest {
+
Q_REQUIRED_RESULT Q_GUI_EXPORT bool qWaitForWindowActive(QWindow *window, int timeout = 5000);
Q_REQUIRED_RESULT Q_GUI_EXPORT bool qWaitForWindowExposed(QWindow *window, int timeout = 5000);
-}
+
+Q_GUI_EXPORT QPointingDevice * createTouchDevice(QInputDevice::DeviceType devType = QInputDevice::DeviceType::TouchScreen,
+ QInputDevice::Capabilities caps = QInputDevice::Capability::Position);
+
+class Q_GUI_EXPORT QTouchEventSequence
+{
+public:
+ virtual ~QTouchEventSequence();
+ QTouchEventSequence& press(int touchId, const QPoint &pt, QWindow *window = nullptr);
+ QTouchEventSequence& move(int touchId, const QPoint &pt, QWindow *window = nullptr);
+ QTouchEventSequence& release(int touchId, const QPoint &pt, QWindow *window = nullptr);
+ virtual QTouchEventSequence& stationary(int touchId);
+
+ virtual void commit(bool processEvents = true);
+
+protected:
+ QTouchEventSequence(QWindow *window, QPointingDevice *aDevice, bool autoCommit);
+
+ QPoint mapToScreen(QWindow *window, const QPoint &pt);
+
+ QEventPoint &point(int touchId);
+
+ QEventPoint &pointOrPreviousPoint(int touchId);
+
+ QMap<int, QEventPoint> previousPoints;
+ QMap<int, QEventPoint> points;
+ QWindow *targetWindow;
+ QPointingDevice *device;
+ bool commitWhenDestroyed;
+ friend QTouchEventSequence touchEvent(QWindow *window, QPointingDevice *device, bool autoCommit);
+};
+
+} // namespace QTest
QT_END_NAMESPACE