Skip to content

Commit

Permalink
QTestLib: increment timestamp by specified delay before each event
Browse files Browse the repository at this point in the history
Some test functions generate series of mouse events, and the docs have
always said that the specified delay applies to each event, not just
to the first one. This brings reality into agreement with the docs.

We're still keeping 0f94430 : the
delay cannot be 0, because if we never increment the timestamp at all,
that would never be a realistic simulation of any real-world scenario.
Checking elapsed time is increasingly useful in Qt, and unrealistic
timestamps can cause strange behavior.

[ChangeLog][QTestLib] QTest::mouseClick() and mouseDClick() now apply
the given delay to the timestamps of each event in the sequence, as it
has been documented all along. As before, the delay is never allowed
to be less than 1 ms.

Fixes: QTBUG-129794
Change-Id: I03de902b57582de239fc9491b95fb406b879f7e3
Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
  • Loading branch information
ec1oud committed Oct 9, 2024
1 parent fbe61de commit 0698580
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/testlib/qtestmouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ namespace QTest
pos.x(), pos.y(), windowSize.width(), windowSize.height());
}

int actualDelay = (delay == -1 || delay < defaultMouseDelay()) ? defaultMouseDelay() : delay;
lastMouseTimestamp += qMax(1, actualDelay);
int actualDelay = qMax(1, (delay == -1 || delay < defaultMouseDelay()) ? defaultMouseDelay() : delay);

if (pos.isNull())
pos = QPoint(window->width() / 2, window->height() / 2);
Expand All @@ -95,28 +94,33 @@ namespace QTest
{
case MouseDClick:
qtestMouseButtons.setFlag(button, true);
lastMouseTimestamp += actualDelay;
qt_handleMouseEvent(w, pos, global, qtestMouseButtons, button, QEvent::MouseButtonPress,
stateKey, lastMouseTimestamp);
qtestMouseButtons.setFlag(button, false);
lastMouseTimestamp += actualDelay;
qt_handleMouseEvent(w, pos, global, qtestMouseButtons, button, QEvent::MouseButtonRelease,
stateKey, lastMouseTimestamp);
Q_FALLTHROUGH();
case MousePress:
case MouseClick:
qtestMouseButtons.setFlag(button, true);
lastMouseTimestamp += actualDelay;
qt_handleMouseEvent(w, pos, global, qtestMouseButtons, button, QEvent::MouseButtonPress,
stateKey, lastMouseTimestamp);
if (action == MousePress)
break;
Q_FALLTHROUGH();
case MouseRelease:
qtestMouseButtons.setFlag(button, false);
lastMouseTimestamp += actualDelay;
qt_handleMouseEvent(w, pos, global, qtestMouseButtons, button, QEvent::MouseButtonRelease,
stateKey, lastMouseTimestamp);
if (delay == -1)
lastMouseTimestamp += mouseDoubleClickInterval; // avoid double clicks being generated
break;
case MouseMove:
lastMouseTimestamp += actualDelay;
qt_handleMouseEvent(w, pos, global, qtestMouseButtons, Qt::NoButton, QEvent::MouseMove,
stateKey, lastMouseTimestamp);
break;
Expand Down
6 changes: 4 additions & 2 deletions tests/auto/testlib/selftests/mouse/tst_mouse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,13 +283,15 @@ void tst_Mouse::doubleClick()
ts = w.lastTimeStamp;
QTest::mouseClick(&w, Qt::LeftButton, {}, point, 10);
QCOMPARE_GE(w.lastTimeStamp, ts + 500); // because the last release had a default delay
QTest::mouseClick(&w, Qt::LeftButton, {}, point);
ts = w.lastTimeStamp;
QTest::mouseClick(&w, Qt::LeftButton, {}, point, 10); // 10 ms before press, 10 ms before release
QCOMPARE(w.doubleClickCount, 2);
QCOMPARE(w.lastTimeStamp, ts + 20);

// use the mouseDClick function to generate another double-click
ts = w.lastTimeStamp;
QTest::mouseDClick(&w, Qt::LeftButton, {}, point);
QCOMPARE_GE(w.lastTimeStamp, ts + 500); // because the last release had a default delay
QCOMPARE(w.lastTimeStamp, ts + 4); // 1 ms before each press and release
QCOMPARE(w.doubleClickCount, 3);

// use the mouseClick function with default delay to avoid double-click
Expand Down

0 comments on commit 0698580

Please sign in to comment.