diff options
author | Caroline Chao <caroline.chao@theqtcompany.com> | 2015-02-27 12:11:58 +0100 |
---|---|---|
committer | Caroline Chao <caroline.chao@theqtcompany.com> | 2015-03-02 11:45:35 +0000 |
commit | 726eaaeeeede086ed341899b8ee0c1570e6bf6f5 (patch) | |
tree | b3880ee109108fb11299a70ac701e6bf1915b3be | |
parent | 09e903e0743b3d3d58c31621b368ea317140b0f2 (diff) |
testlib: Introduce MouseDoubleClickSequence() method
The existing method MouseDoubleClick() emulates the mouse
double click event only.
The added method MouseDoubleClickSequence() emulates the full
sequence of mouse events a physical double-click would generate:
Press-Release-Press-DoubleClick-Release
Introducing a new method in order to provide convenience when a
test requires to simulate a complete double-click action without
changing the behavior of MouseDoubleClick() and risking to break
existing tests.
Add autotest.
Task-number: QTBUG-42185
Change-Id: I1cdddd9e21d3b1d8a818f6d4e3717b06b7d70e08
Reviewed-by: Simon Hausmann <simon.hausmann@theqtcompany.com>
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
Reviewed-by: Laszlo Agocs <laszlo.agocs@theqtcompany.com>
-rw-r--r-- | src/imports/testlib/TestCase.qml | 54 | ||||
-rw-r--r-- | src/qmltest/quicktestevent.cpp | 27 | ||||
-rw-r--r-- | src/qmltest/quicktestevent_p.h | 2 | ||||
-rw-r--r-- | tests/auto/qmltest/events/tst_events.qml | 35 |
4 files changed, 108 insertions, 10 deletions
diff --git a/src/imports/testlib/TestCase.qml b/src/imports/testlib/TestCase.qml index caa50fb6d7..9f527b66f1 100644 --- a/src/imports/testlib/TestCase.qml +++ b/src/imports/testlib/TestCase.qml @@ -191,7 +191,7 @@ import Qt.test.qtestroot 1.0 } \endcode - The mousePress(), mouseRelease(), mouseClick(), mouseDoubleClick(), + The mousePress(), mouseRelease(), mouseClick(), mouseDoubleClick(), mouseDoubleClickSequence() and mouseMove() methods can be used to simulate mouse events in a similar fashion. @@ -947,7 +947,7 @@ Item { If \a item is obscured by another item, or a child of \a item occupies that position, then the event will be delivered to the other item instead. - \sa mouseRelease(), mouseClick(), mouseDoubleClick(), mouseMove(), mouseDrag(), mouseWheel() + \sa mouseRelease(), mouseClick(), mouseDoubleClick(), mouseDoubleClickSequence(), mouseMove(), mouseDrag(), mouseWheel() */ function mousePress(item, x, y, button, modifiers, delay) { if (button === undefined) @@ -978,7 +978,7 @@ Item { If \a item is obscured by another item, or a child of \a item occupies that position, then the event will be delivered to the other item instead. - \sa mousePress(), mouseClick(), mouseDoubleClick(), mouseMove(), mouseDrag(), mouseWheel() + \sa mousePress(), mouseClick(), mouseDoubleClick(), mouseDoubleClickSequence(), mouseMove(), mouseDrag(), mouseWheel() */ function mouseRelease(item, x, y, button, modifiers, delay) { if (button === undefined) @@ -1011,7 +1011,7 @@ Item { Note: this method does not imply a drop action, to make a drop, an additional mouseRelease(item, x + dx, y + dy) is needed. - \sa mousePress(), mouseClick(), mouseDoubleClick(), mouseMove(), mouseRelease(), mouseWheel() + \sa mousePress(), mouseClick(), mouseDoubleClick(), mouseDoubleClickSequence(), mouseMove(), mouseRelease(), mouseWheel() */ function mouseDrag(item, x, y, dx, dy, button, modifiers, delay) { if (item.x === undefined || item.y === undefined) @@ -1058,7 +1058,7 @@ Item { If \a item is obscured by another item, or a child of \a item occupies that position, then the event will be delivered to the other item instead. - \sa mousePress(), mouseRelease(), mouseDoubleClick(), mouseMove(), mouseDrag(), mouseWheel() + \sa mousePress(), mouseRelease(), mouseDoubleClick(), mouseDoubleClickSequence(), mouseMove(), mouseDrag(), mouseWheel() */ function mouseClick(item, x, y, button, modifiers, delay) { if (button === undefined) @@ -1089,7 +1089,7 @@ Item { If \a item is obscured by another item, or a child of \a item occupies that position, then the event will be delivered to the other item instead. - \sa mousePress(), mouseRelease(), mouseClick(), mouseMove(), mouseDrag(), mouseWheel() + \sa mouseDoubleClickSequence(), mousePress(), mouseRelease(), mouseClick(), mouseMove(), mouseDrag(), mouseWheel() */ function mouseDoubleClick(item, x, y, button, modifiers, delay) { if (button === undefined) @@ -1107,6 +1107,44 @@ Item { } /*! + \qmlmethod TestCase::mouseDoubleClickSequence(item, x = item.width / 2, y = item.height / 2, button = Qt.LeftButton, modifiers = Qt.NoModifier, delay = -1) + + Simulates the full sequence of events generated by double-clicking a mouse + \a button with an optional \a modifier on an \a item. + + This method reproduces the sequence of mouse events generated when a user makes + a double click: Press-Release-Press-DoubleClick-Release. + + The position of the click is defined by \a x and \a y. + If \a x and \a y are not defined the position will be the center of \a item. + If \a delay is specified, the test will wait for the specified amount of + milliseconds before pressing and before releasing the button. + + The position given by \a x and \a y is transformed from the co-ordinate + system of \a item into window co-ordinates and then delivered. + If \a item is obscured by another item, or a child of \a item occupies + that position, then the event will be delivered to the other item instead. + + This QML method was introduced in Qt 5.5. + + \sa mouseDoubleClick(), mousePress(), mouseRelease(), mouseClick(), mouseMove(), mouseDrag(), mouseWheel() + */ + function mouseDoubleClickSequence(item, x, y, button, modifiers, delay) { + if (button === undefined) + button = Qt.LeftButton + if (modifiers === undefined) + modifiers = Qt.NoModifier + if (delay == undefined) + delay = -1 + if (x === undefined) + x = item.width / 2 + if (y === undefined) + y = item.height / 2 + if (!qtest_events.mouseDoubleClickSequence(item, x, y, button, modifiers, delay)) + qtest_fail("window not shown", 2) + } + + /*! \qmlmethod TestCase::mouseMove(item, x, y, delay = -1) Moves the mouse pointer to the position given by \a x and \a y within @@ -1118,7 +1156,7 @@ Item { If \a item is obscured by another item, or a child of \a item occupies that position, then the event will be delivered to the other item instead. - \sa mousePress(), mouseRelease(), mouseClick(), mouseDoubleClick(), mouseDrag(), mouseWheel() + \sa mousePress(), mouseRelease(), mouseClick(), mouseDoubleClick(), mouseDoubleClickSequence(), mouseDrag(), mouseWheel() */ function mouseMove(item, x, y, delay, buttons) { if (delay == undefined) @@ -1143,7 +1181,7 @@ Item { The \a xDelta and \a yDelta contain the wheel rotation distance in eighths of a degree. see \l QWheelEvent::angleDelta() for more details. - \sa mousePress(), mouseClick(), mouseDoubleClick(), mouseMove(), mouseRelease(), mouseDrag(), QWheelEvent::angleDelta() + \sa mousePress(), mouseClick(), mouseDoubleClick(), mouseDoubleClickSequence(), mouseMove(), mouseRelease(), mouseDrag(), QWheelEvent::angleDelta() */ function mouseWheel(item, x, y, xDelta, yDelta, buttons, modifiers, delay) { if (delay == undefined) diff --git a/src/qmltest/quicktestevent.cpp b/src/qmltest/quicktestevent.cpp index 2101d6e7aa..df8de14c14 100644 --- a/src/qmltest/quicktestevent.cpp +++ b/src/qmltest/quicktestevent.cpp @@ -111,7 +111,7 @@ namespace QTest { namespace QtQuickTest { - enum MouseAction { MousePress, MouseRelease, MouseClick, MouseDoubleClick, MouseMove }; + enum MouseAction { MousePress, MouseRelease, MouseClick, MouseDoubleClick, MouseMove, MouseDoubleClickSequence }; static void mouseEvent(MouseAction action, QWindow *window, QObject *item, Qt::MouseButton button, @@ -131,6 +131,15 @@ namespace QtQuickTest return; } + if (action == MouseDoubleClickSequence) { + mouseEvent(MousePress, window, item, button, stateKey, _pos); + mouseEvent(MouseRelease, window, item, button, stateKey, _pos); + mouseEvent(MousePress, window, item, button, stateKey, _pos); + mouseEvent(MouseDoubleClick, window, item, button, stateKey, _pos); + mouseEvent(MouseRelease, window, item, button, stateKey, _pos); + return; + } + QPoint pos; QQuickItem *sgitem = qobject_cast<QQuickItem *>(item); if (sgitem) @@ -162,7 +171,7 @@ namespace QtQuickTest QSpontaneKeyEvent::setSpontaneous(&me); if (!qApp->notify(window, &me)) { static const char *mouseActionNames[] = - { "MousePress", "MouseRelease", "MouseClick", "MouseDoubleClick", "MouseMove" }; + { "MousePress", "MouseRelease", "MouseClick", "MouseDoubleClick", "MouseMove", "MouseDoubleClickSequence" }; QString warning = QString::fromLatin1("Mouse event \"%1\" not accepted by receiving window"); QWARN(warning.arg(QString::fromLatin1(mouseActionNames[static_cast<int>(action)])).toLatin1().data()); } @@ -269,6 +278,20 @@ bool QuickTestEvent::mouseDoubleClick return true; } +bool QuickTestEvent::mouseDoubleClickSequence + (QObject *item, qreal x, qreal y, int button, + int modifiers, int delay) +{ + QWindow *view = eventWindow(); + if (!view) + return false; + QtQuickTest::mouseEvent(QtQuickTest::MouseDoubleClickSequence, view, item, + Qt::MouseButton(button), + Qt::KeyboardModifiers(modifiers), + QPointF(x, y), delay); + return true; +} + bool QuickTestEvent::mouseMove (QObject *item, qreal x, qreal y, int delay, int buttons) { diff --git a/src/qmltest/quicktestevent_p.h b/src/qmltest/quicktestevent_p.h index 3005d84128..b7f8f3eda2 100644 --- a/src/qmltest/quicktestevent_p.h +++ b/src/qmltest/quicktestevent_p.h @@ -63,6 +63,8 @@ public Q_SLOTS: int modifiers, int delay); bool mouseDoubleClick(QObject *item, qreal x, qreal y, int button, int modifiers, int delay); + bool mouseDoubleClickSequence(QObject *item, qreal x, qreal y, int button, + int modifiers, int delay); bool mouseMove(QObject *item, qreal x, qreal y, int delay, int buttons); #ifndef QT_NO_WHEELEVENT diff --git a/tests/auto/qmltest/events/tst_events.qml b/tests/auto/qmltest/events/tst_events.qml index b9bc78be21..8a08d7dd2b 100644 --- a/tests/auto/qmltest/events/tst_events.qml +++ b/tests/auto/qmltest/events/tst_events.qml @@ -53,10 +53,28 @@ Rectangle { property bool mouseHasBeenClicked: false + signal doubleClickSignalHelper(string eventType) + + SignalSpy { + id: doubleClickSpy + target: top + signalName: "doubleClickSignalHelper" + } + MouseArea { anchors.fill: parent onClicked: { mouseHasBeenClicked = true + doubleClickSignalHelper("clicked") + } + onPressed: { + doubleClickSignalHelper("pressed") + } + onReleased: { + doubleClickSignalHelper("released") + } + onDoubleClicked: { + doubleClickSignalHelper("doubleClick") } } @@ -75,5 +93,22 @@ Rectangle { mouseClick(top, 25, 30) tryCompare(top, "mouseHasBeenClicked", true, 10000) } + + function test_mouse_doubleclick() { + doubleClickSpy.clear() + mouseDoubleClickSequence(top, 25, 30) + compare(doubleClickSpy.count, 6) + compare(doubleClickSpy.signalArguments[0][0], "pressed") + compare(doubleClickSpy.signalArguments[1][0], "released") + compare(doubleClickSpy.signalArguments[2][0], "clicked") + compare(doubleClickSpy.signalArguments[3][0], "pressed") + compare(doubleClickSpy.signalArguments[4][0], "doubleClick") + compare(doubleClickSpy.signalArguments[5][0], "released") + + doubleClickSpy.clear() + mouseDoubleClick(top, 25, 30) + compare(doubleClickSpy.count, 1) + compare(doubleClickSpy.signalArguments[0][0], "doubleClick") + } } } |