diff options
| author | Mitch Curtis <mitch.curtis@qt.io> | 2023-06-23 16:26:01 +0800 |
|---|---|---|
| committer | Shawn Rutledge <shawn.rutledge@qt.io> | 2023-06-27 21:41:13 +0000 |
| commit | 2adaa51d6fa7952013c592d2a8a75814112ee247 (patch) | |
| tree | c76b1c9decba6e217e5984241f6a6352fdd964fd /src/quicktestutils/quick/visualtestutils.cpp | |
| parent | 7d450891a2565f2e91430b1415f320ff01fc5914 (diff) | |
Add PointLerper to QQuickVisualTestUtils
This is a convenience class to linearly interpolate between two
move points:
PointLerper pointLerper(window);
// Lerps from {0, 0} to {15, 15}.
pointLerper.move(15, 15);
QVERIFY(parentButton->isHovered());
// Lerps from {15, 15} to {25, 25}.
pointLerper.move(25, 25);
QVERIFY(childButton->isHovered());
This allows testing mouse interaction in a more realistic manner,
as opposed to immediately moving to a new position with no intermediate
move events.
More importantly, it fixes flakiness in tst_qquickpopup hover tests.
Includes an attempt to stabilize tst_QQuickDrawer::multiple() which
seems to crash due to events arriving after window destruction.
Task-number: QTBUG-114718
Pick-to: 6.5 6.6
Change-Id: I4b466d820cec4113a9d266685c4575a3692e5202
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
Diffstat (limited to 'src/quicktestutils/quick/visualtestutils.cpp')
| -rw-r--r-- | src/quicktestutils/quick/visualtestutils.cpp | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/quicktestutils/quick/visualtestutils.cpp b/src/quicktestutils/quick/visualtestutils.cpp index 7684109070..3435be5353 100644 --- a/src/quicktestutils/quick/visualtestutils.cpp +++ b/src/quicktestutils/quick/visualtestutils.cpp @@ -4,12 +4,14 @@ #include "visualtestutils_p.h" #include <QtCore/QCoreApplication> +#include <QtCore/private/qvariantanimation_p.h> #include <QtCore/QDebug> #include <QtQuick/QQuickItem> #if QT_CONFIG(quick_itemview) #include <QtQuick/private/qquickitemview_p.h> #endif #include <QtQuickTest/QtQuickTest> +#include <QtQuickTestUtils/private/viewtestutils_p.h> QT_BEGIN_NAMESPACE @@ -60,6 +62,55 @@ void QQuickVisualTestUtils::centerOnScreen(QQuickWindow *window) window->setFramePosition(screenGeometry.center() - offset); } +QPoint QQuickVisualTestUtils::lerpPoints(const QPoint &point1, const QPoint &point2, qreal t) +{ + return QPoint(_q_interpolate(point1.x(), point2.x(), t), _q_interpolate(point1.y(), point2.y(), t)); +}; + +/*! + \internal + + Convenience class to linearly interpolate between two pointer move points. + + \code + PointLerper pointLerper(window); + // Lerps from {0, 0} to {15, 15}. + pointLerper.move(15, 15); + QVERIFY(parentButton->isHovered()); + + // Lerps from {15, 15} to {25, 25}. + pointLerper.move(25, 25); + QVERIFY(childButton->isHovered()); + \endcode +*/ +QQuickVisualTestUtils::PointLerper::PointLerper(QQuickWindow *window, const QPointingDevice *pointingDevice) + : mWindow(window) + , mPointingDevice(pointingDevice) +{ +} + +/*! + \internal + + Moves from the last pos (or {0, 0} if there have been no calls + to this function yet) to \a pos using linear interpolation + over 10 (default value) steps with 1 ms (default value) delays + between each step. +*/ +void QQuickVisualTestUtils::PointLerper::move(const QPoint &pos, int steps, int delayInMilliseconds) +{ + forEachStep(steps, [&](qreal progress) { + QQuickTest::pointerMove(mPointingDevice, mWindow, 0, lerpPoints(mFrom, pos, progress)); + QTest::qWait(delayInMilliseconds); + }); + mFrom = pos; +}; + +void QQuickVisualTestUtils::PointLerper::move(int x, int y, int steps, int delayInMilliseconds) +{ + move(QPoint(x, y), steps, delayInMilliseconds); +}; + bool QQuickVisualTestUtils::delegateVisible(QQuickItem *item) { return item->isVisible() && !QQuickItemPrivate::get(item)->culled; |
