From 8c6c4df3e83776d821f357582a717dbfbeb1c3ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Arve=20S=C3=A6ther?= Date: Tue, 4 Feb 2020 11:32:00 +0100 Subject: [PATCH] Maintain at least 500ms timestamp distance between each test function If we had one test function that just did tst_Mouse::f1() { QTest::mouseMove(w, QPoint(0,0)); } and another test function that did tst_Mouse::f2() { QTest::mouseMove(w, QPoint(500,500)); } their corresponding event timestamps were only 1 apart from each other. This meant that any code that tried to estimate the velocity of a mouse cursor would get a really high velocity estimate inside f2(). This would come as a surprise to most people. So to avoid this, we add a 500 ms timestamp delay between each test function call. In theory this could also prevent generating a mouseDoubleClickEvent when a pair of test functions containing a press-release sequence was run, but there is a separate pre-existing mechanism to handle that case. Change-Id: Icd4fc35853c09f080466d22411208c7b5c4174b5 Reviewed-by: Shawn Rutledge --- src/testlib/qtestcase.cpp | 3 + .../testlib/selftests/mouse/tst_mouse.cpp | 67 ++++++++++++++++++- 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp index 6be623d7fc..6805d17b6e 100644 --- a/src/testlib/qtestcase.cpp +++ b/src/testlib/qtestcase.cpp @@ -296,6 +296,8 @@ namespace QTestPrivate namespace QTest { +extern Q_TESTLIB_EXPORT int lastMouseTimestamp; + class WatchDog; static QObject *currentTestObject = nullptr; @@ -1173,6 +1175,7 @@ bool TestMethods::invokeTest(int index, const char *data, WatchDog *watchDog) co QTestPrivate::qtestMouseButtons = Qt::NoButton; if (watchDog) watchDog->beginTest(); + QTest::lastMouseTimestamp += 500; // Maintain at least 500ms mouse event timestamps between each test function call invokeTestOnData(index); if (watchDog) watchDog->testFinished(); diff --git a/tests/auto/testlib/selftests/mouse/tst_mouse.cpp b/tests/auto/testlib/selftests/mouse/tst_mouse.cpp index d097027e7e..43dda31daf 100644 --- a/tests/auto/testlib/selftests/mouse/tst_mouse.cpp +++ b/tests/auto/testlib/selftests/mouse/tst_mouse.cpp @@ -42,6 +42,8 @@ class tst_Mouse : public QObject Q_OBJECT private slots: + void timestampBetweenTestFunction_data(); + void timestampBetweenTestFunction(); void stateHandlingPart1_data(); void stateHandlingPart1(); void stateHandlingPart2(); @@ -55,20 +57,83 @@ public: Qt::MouseButtons stateInMouseMove = Qt::NoButton; int moveCount = 0; int pressCount = 0; + int doubleClickCount = 0; + ulong lastTimeStamp = 0; protected: - void mousePressEvent(QMouseEvent *) + void mousePressEvent(QMouseEvent *e) { pressCount++; + processEvent(e); } void mouseMoveEvent(QMouseEvent *e) { moveCount++; stateInMouseMove = e->buttons(); + processEvent(e); } + + void mouseReleaseEvent(QMouseEvent *e) + { + processEvent(e); + } + + void mouseDoubleClickEvent(QMouseEvent *e) + { + doubleClickCount++; + processEvent(e); + } + + void processEvent(QMouseEvent *e) + { + lastTimeStamp = e->timestamp(); + } + }; +static ulong lastTimeStampInPreviousTestFunction = 0; + +void tst_Mouse::timestampBetweenTestFunction_data() +{ + QTest::addColumn("hoverLast"); + QTest::addColumn("pressAndRelease"); + QTest::newRow("press, release") << true << false; + QTest::newRow("press, release, hover") << true << true; + QTest::newRow("hover") << false << true; + QTest::newRow("hover #2") << false << true; + QTest::newRow("press, release #2") << true << false; + QTest::newRow("press, release, hover #2") << true << true; +} + +void tst_Mouse::timestampBetweenTestFunction() +{ + QFETCH(bool, hoverLast); + QFETCH(bool, pressAndRelease); + + MouseWindow w; + w.show(); + w.setGeometry(100, 100, 200, 200); + QVERIFY(QTest::qWaitForWindowActive(&w)); + + QPoint point(10, 10); + QCOMPARE(w.pressCount, 0); + if (pressAndRelease) { + QTest::mousePress(&w, Qt::LeftButton, { }, point); + QVERIFY(w.lastTimeStamp - lastTimeStampInPreviousTestFunction > 500); // Should be at least 500 ms timestamp between each test case + QCOMPARE(w.pressCount, 1); + QTest::mouseRelease(&w, Qt::LeftButton, { }, point); + } + QCOMPARE(w.doubleClickCount, 0); + if (hoverLast) { + static int xMove = 0; + xMove += 5; // Just make sure we generate different hover coordinates + point.rx() += xMove; + QTest::mouseMove(&w, point); // a hover move. This doesn't generate a timestamp delay of 500 ms + } + lastTimeStampInPreviousTestFunction = w.lastTimeStamp; +} + void tst_Mouse::stateHandlingPart1_data() { QTest::addColumn("dummy");