summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/testlib/doc/snippets/code/src_qtestlib_qtestcase_snippet.cpp13
-rw-r--r--src/testlib/qtestcase.qdoc22
-rw-r--r--src/testlib/qtestmouse.h14
-rw-r--r--tests/auto/testlib/selftests/expected_mouse.junitxml3
-rw-r--r--tests/auto/testlib/selftests/expected_mouse.lightxml4
-rw-r--r--tests/auto/testlib/selftests/expected_mouse.tap9
-rw-r--r--tests/auto/testlib/selftests/expected_mouse.teamcity2
-rw-r--r--tests/auto/testlib/selftests/expected_mouse.txt3
-rw-r--r--tests/auto/testlib/selftests/expected_mouse.xml4
-rw-r--r--tests/auto/testlib/selftests/mouse/tst_mouse.cpp49
10 files changed, 111 insertions, 12 deletions
diff --git a/src/testlib/doc/snippets/code/src_qtestlib_qtestcase_snippet.cpp b/src/testlib/doc/snippets/code/src_qtestlib_qtestcase_snippet.cpp
index e64e9daecf..aa24746522 100644
--- a/src/testlib/doc/snippets/code/src_qtestlib_qtestcase_snippet.cpp
+++ b/src/testlib/doc/snippets/code/src_qtestlib_qtestcase_snippet.cpp
@@ -119,3 +119,16 @@ char *toString(const MyType &t)
return repr;
}
//! [34]
+
+//! [35]
+QSignalSpy doubleClickSpy(target, &TargetClass::doubleClicked);
+const QPoint p(1, 2);
+QTest::mousePress(&myWindow, Qt::LeftButton, Qt::NoModifier, p);
+QVERIFY(target.isPressed());
+QTest::mouseRelease(&myWindow, Qt::LeftButton, Qt::NoModifier, p, 10);
+QCOMPARE(target.isPressed(), false);
+QTest::mousePress(&myWindow, Qt::LeftButton, Qt::NoModifier, p, 10);
+QCOMPARE(target.pressCount(), 2);
+QTest::mouseRelease(&myWindow, Qt::LeftButton, Qt::NoModifier, p, 10);
+QCOMPARE(doubleClickSpy.count(), 1);
+//! [35]
diff --git a/src/testlib/qtestcase.qdoc b/src/testlib/qtestcase.qdoc
index 654687c880..e2181c816b 100644
--- a/src/testlib/qtestcase.qdoc
+++ b/src/testlib/qtestcase.qdoc
@@ -1260,7 +1260,16 @@
on a \a widget. The position of the release is defined by \a pos;
the default position is the center of the widget. If \a delay is
specified, the test will wait for the specified amount of
- milliseconds before releasing the button.
+ milliseconds before releasing the button; otherwise, it will wait for a
+ default amount of time (1 ms), which can be overridden via
+ \l {Testing Options}{command-line arguments}.
+
+ \note If you wish to test a double-click by sending events individually,
+ specify a short delay, greater than the default, on both mouse release events.
+ The total of the delays for the press, release, press and release must be
+ less than QStyleHints::mouseDoubleClickInterval(). But if you don't need
+ to check state between events, it's better to use QTest::mouseDClick().
+ \snippet code/src_qtestlib_qtestcase_snippet.cpp 35
\sa QTest::mousePress(), QTest::mouseClick()
*/
@@ -1273,7 +1282,16 @@
on a \a window. The position of the release is defined by \a pos;
the default position is the center of the window. If \a delay is
specified, the test will wait for the specified amount of
- milliseconds before releasing the button.
+ milliseconds before releasing the button; otherwise, it will wait for a
+ default amount of time (1 ms), which can be overridden via
+ \l {Testing Options}{command-line arguments}.
+
+ \note If you wish to test a double-click by sending events individually,
+ specify a short delay, greater than the default, on both mouse release events.
+ The total of the delays for the press, release, press and release must be
+ less than QStyleHints::mouseDoubleClickInterval(). But if you don't need
+ to check state between events, it's better to use QTest::mouseDClick().
+ \snippet code/src_qtestlib_qtestcase_snippet.cpp 35
\sa QTest::mousePress(), QTest::mouseClick()
*/
diff --git a/src/testlib/qtestmouse.h b/src/testlib/qtestmouse.h
index 37e8d4eab5..01a3c9c6b5 100644
--- a/src/testlib/qtestmouse.h
+++ b/src/testlib/qtestmouse.h
@@ -57,6 +57,12 @@ namespace QTest
event. We expect all event-handling code to rely on the event
timestamps, not the system clock; therefore tests can be run faster
than real-time.
+
+ If \a delay is not given, a default minimum mouse delay is used, and
+ unintended double-click events are prevented by incrementing the
+ timestamp by 500ms after each mouse release. Therefore, to test
+ double-clicks, it's necessary to give a realistic \a delay value (for
+ example, 10ms).
*/
static void mouseEvent(MouseAction action, QWindow *window, Qt::MouseButton button,
Qt::KeyboardModifiers stateKey, QPoint pos, int delay=-1)
@@ -71,9 +77,8 @@ namespace QTest
pos.x(), pos.y(), windowSize.width(), windowSize.height());
}
- if (delay == -1 || delay < defaultMouseDelay())
- delay = defaultMouseDelay();
- lastMouseTimestamp += qMax(1, delay);
+ int actualDelay = (delay == -1 || delay < defaultMouseDelay()) ? defaultMouseDelay() : delay;
+ lastMouseTimestamp += qMax(1, actualDelay);
if (pos.isNull())
pos = QPoint(window->width() / 2, window->height() / 2);
@@ -108,7 +113,8 @@ namespace QTest
qtestMouseButtons.setFlag(button, false);
qt_handleMouseEvent(w, pos, global, qtestMouseButtons, button, QEvent::MouseButtonRelease,
stateKey, lastMouseTimestamp);
- lastMouseTimestamp += mouseDoubleClickInterval; // avoid double clicks being generated
+ if (delay == -1)
+ lastMouseTimestamp += mouseDoubleClickInterval; // avoid double clicks being generated
break;
case MouseMove:
qt_handleMouseEvent(w, pos, global, qtestMouseButtons, Qt::NoButton, QEvent::MouseMove,
diff --git a/tests/auto/testlib/selftests/expected_mouse.junitxml b/tests/auto/testlib/selftests/expected_mouse.junitxml
index 0664ab3a34..8b0b7b2da5 100644
--- a/tests/auto/testlib/selftests/expected_mouse.junitxml
+++ b/tests/auto/testlib/selftests/expected_mouse.junitxml
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
-<testsuite name="tst_Mouse" timestamp="@TEST_START_TIME@" hostname="@HOSTNAME@" tests="13" failures="0" errors="0" skipped="3" time="@TEST_DURATION@">
+<testsuite name="tst_Mouse" timestamp="@TEST_START_TIME@" hostname="@HOSTNAME@" tests="14" failures="0" errors="0" skipped="3" time="@TEST_DURATION@">
<properties>
<property name="QTestVersion" value="@INSERT_QT_VERSION_HERE@"/>
<property name="QtVersion" value="@INSERT_QT_VERSION_HERE@"/>
@@ -23,5 +23,6 @@
<testcase name="deterministicEvents(first&#x002D;run&#x002D;false)" classname="tst_Mouse" time="@TEST_DURATION@">
<skipped message="Not implemented!"/>
</testcase>
+ <testcase name="doubleClick" classname="tst_Mouse" time="@TEST_DURATION@"/>
<testcase name="cleanupTestCase" classname="tst_Mouse" time="@TEST_DURATION@"/>
</testsuite>
diff --git a/tests/auto/testlib/selftests/expected_mouse.lightxml b/tests/auto/testlib/selftests/expected_mouse.lightxml
index ed125d9b68..fd6b9a8218 100644
--- a/tests/auto/testlib/selftests/expected_mouse.lightxml
+++ b/tests/auto/testlib/selftests/expected_mouse.lightxml
@@ -54,6 +54,10 @@
</Incident>
<Duration msecs="0"/>
</TestFunction>
+ <TestFunction name="doubleClick">
+ <Incident type="pass" file="" line="0" />
+ <Duration msecs="0"/>
+ </TestFunction>
<TestFunction name="cleanupTestCase">
<Incident type="pass" file="" line="0" />
<Duration msecs="0"/>
diff --git a/tests/auto/testlib/selftests/expected_mouse.tap b/tests/auto/testlib/selftests/expected_mouse.tap
index a91b5b4da2..a2ba671bf4 100644
--- a/tests/auto/testlib/selftests/expected_mouse.tap
+++ b/tests/auto/testlib/selftests/expected_mouse.tap
@@ -12,8 +12,9 @@ ok 9 - stateHandlingPart1(dummy-2)
ok 10 - stateHandlingPart2() # SKIP Not implemented beyond this point!
ok 11 - deterministicEvents(first-run-true) # SKIP Not implemented!
ok 12 - deterministicEvents(first-run-false) # SKIP Not implemented!
-ok 13 - cleanupTestCase()
-1..13
-# tests 13
-# pass 10
+ok 13 - doubleClick()
+ok 14 - cleanupTestCase()
+1..14
+# tests 14
+# pass 11
# fail 0
diff --git a/tests/auto/testlib/selftests/expected_mouse.teamcity b/tests/auto/testlib/selftests/expected_mouse.teamcity
index 371bd49626..44c03ce6d7 100644
--- a/tests/auto/testlib/selftests/expected_mouse.teamcity
+++ b/tests/auto/testlib/selftests/expected_mouse.teamcity
@@ -26,6 +26,8 @@
##teamcity[testStarted name='deterministicEvents(first-run-false)' flowId='tst_Mouse']
##teamcity[testIgnored name='deterministicEvents(first-run-false)' message='Not implemented! |[Loc: qtbase/tests/auto/testlib/selftests/mouse/tst_mouse.cpp(0)|]' flowId='tst_Mouse']
##teamcity[testFinished name='deterministicEvents(first-run-false)' flowId='tst_Mouse']
+##teamcity[testStarted name='doubleClick()' flowId='tst_Mouse']
+##teamcity[testFinished name='doubleClick()' flowId='tst_Mouse']
##teamcity[testStarted name='cleanupTestCase()' flowId='tst_Mouse']
##teamcity[testFinished name='cleanupTestCase()' flowId='tst_Mouse']
##teamcity[testSuiteFinished name='tst_Mouse' flowId='tst_Mouse']
diff --git a/tests/auto/testlib/selftests/expected_mouse.txt b/tests/auto/testlib/selftests/expected_mouse.txt
index 660b4f47a4..9fe927062d 100644
--- a/tests/auto/testlib/selftests/expected_mouse.txt
+++ b/tests/auto/testlib/selftests/expected_mouse.txt
@@ -15,6 +15,7 @@ SKIP : tst_Mouse::deterministicEvents(first-run-true) Not implemented!
Loc: [qtbase/tests/auto/testlib/selftests/mouse/tst_mouse.cpp(0)]
SKIP : tst_Mouse::deterministicEvents(first-run-false) Not implemented!
Loc: [qtbase/tests/auto/testlib/selftests/mouse/tst_mouse.cpp(0)]
+PASS : tst_Mouse::doubleClick()
PASS : tst_Mouse::cleanupTestCase()
-Totals: 10 passed, 0 failed, 3 skipped, 0 blacklisted, 0ms
+Totals: 11 passed, 0 failed, 3 skipped, 0 blacklisted, 0ms
********* Finished testing of tst_Mouse *********
diff --git a/tests/auto/testlib/selftests/expected_mouse.xml b/tests/auto/testlib/selftests/expected_mouse.xml
index 26bde614ce..b532534049 100644
--- a/tests/auto/testlib/selftests/expected_mouse.xml
+++ b/tests/auto/testlib/selftests/expected_mouse.xml
@@ -56,6 +56,10 @@
</Incident>
<Duration msecs="0"/>
</TestFunction>
+ <TestFunction name="doubleClick">
+ <Incident type="pass" file="" line="0" />
+ <Duration msecs="0"/>
+ </TestFunction>
<TestFunction name="cleanupTestCase">
<Incident type="pass" file="" line="0" />
<Duration msecs="0"/>
diff --git a/tests/auto/testlib/selftests/mouse/tst_mouse.cpp b/tests/auto/testlib/selftests/mouse/tst_mouse.cpp
index b2e9c61baf..c9793da553 100644
--- a/tests/auto/testlib/selftests/mouse/tst_mouse.cpp
+++ b/tests/auto/testlib/selftests/mouse/tst_mouse.cpp
@@ -24,6 +24,7 @@ private slots:
void stateHandlingPart2();
void deterministicEvents_data();
void deterministicEvents();
+ void doubleClick();
};
class MouseWindow : public QWindow
@@ -251,5 +252,53 @@ void tst_Mouse::deterministicEvents()
QCOMPARE(w.moveCount, 1);
}
+void tst_Mouse::doubleClick()
+{
+ MouseWindow w;
+ w.show();
+ w.setGeometry(100, 100, 200, 200);
+ QVERIFY(QTest::qWaitForWindowActive(&w));
+
+ // click
+ QPoint point(10, 10);
+ QCOMPARE(w.pressCount, 0);
+ QTest::mousePress(&w, Qt::LeftButton, { }, point);
+ QCOMPARE(w.pressCount, 1);
+ // give a delay of 10ms
+ auto ts = w.lastTimeStamp;
+ QTest::mouseRelease(&w, Qt::LeftButton, { }, point, 10);
+ QCOMPARE(w.lastTimeStamp, ts + 10);
+ QCOMPARE(w.doubleClickCount, 0);
+
+ // click again within a short time to generate double-click
+ QTest::mousePress(&w, Qt::LeftButton, { }, point, 10);
+ QCOMPARE(w.pressCount, 2);
+ QCOMPARE(w.lastTimeStamp, ts + 20);
+ // this time, let some virtual time elapse, because we're going to test double-click again afterwards
+ QTest::mouseRelease(&w, Qt::LeftButton, { }, point);
+ QCOMPARE_GT(w.lastTimeStamp, ts + 20);
+ QCOMPARE(w.doubleClickCount, 1);
+
+ // use the mouseClick function to generate another double-click
+ 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);
+ QCOMPARE(w.doubleClickCount, 2);
+
+ // 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.doubleClickCount, 3);
+
+ // use the mouseClick function with default delay to avoid double-click
+ ts = w.lastTimeStamp;
+ QTest::mouseClick(&w, Qt::LeftButton, {}, point);
+ QCOMPARE_GE(w.lastTimeStamp, ts + 500); // because the last release had a default delay
+ QTest::mouseClick(&w, Qt::LeftButton, {}, point);
+ QCOMPARE(w.doubleClickCount, 3);
+}
+
QTEST_MAIN(tst_Mouse)
#include "tst_mouse.moc"