From 7100a74def3a82bc52c93b800cec973bf312b1b3 Mon Sep 17 00:00:00 2001 From: juhvu Date: Wed, 21 Sep 2011 13:04:11 +1000 Subject: [PATCH] Wheel event support in qmltestlib + few fixes. added unit test and orientaion support for mouseWheel Change-Id: I9c26dc762281bc32965769c151414ac0e177ad0f Reviewed-on: http://codereview.qt-project.org/5272 Reviewed-by: Qt Sanity Bot Reviewed-by: Juha Vuolle Reviewed-by: Charles Yin --- src/imports/testlib/TestCase.qml | 24 +++++++++++-- src/qmltest/quicktestevent.cpp | 64 +++++++++++++++++++++++++++++---- src/qmltest/quicktestevent_p.h | 7 ++-- tests/auto/qmltest/events/tst_wheel.qml | 47 ++++++++++++++++++++++++ 4 files changed, 131 insertions(+), 11 deletions(-) create mode 100644 tests/auto/qmltest/events/tst_wheel.qml diff --git a/src/imports/testlib/TestCase.qml b/src/imports/testlib/TestCase.qml index ed8bf55..020f934 100644 --- a/src/imports/testlib/TestCase.qml +++ b/src/imports/testlib/TestCase.qml @@ -435,13 +435,31 @@ Item { qtest_fail("window not shown", 2) } - function mouseMove(item, x, y, delay) { + function mouseMove(item, x, y, delay, buttons) { if (delay == undefined) delay = -1 - if (!qtest_events.mouseMove(item, x, y, delay)) + if (buttons == undefined) + buttons = Qt.NoButton + if (!qtest_events.mouseMove(item, x, y, delay, buttons)) qtest_fail("window not shown", 2) } + function mouseWheel(item, x, y, delta, buttons, modifiers, delay, orientation) { + if (delay == undefined) + delay = -1 + if (buttons == undefined) + buttons = Qt.NoButton + if (modifiers === undefined) + modifiers = Qt.NoModifier + if (delta == undefined) + delta = 0 + if (orientation == undefined) + orientation = Qt.Vertical + if (!qtest_events.mouseWheel(item, x, y, buttons, modifiers, delta, delay, orientation)) + qtest_fail("window not shown", 2) + } + + // Functions that can be overridden in subclasses for init/cleanup duties. function initTestCase() {} function cleanupTestCase() {} @@ -692,4 +710,4 @@ Item { if (when && !completed && !running) qtest_run() } -} \ No newline at end of file +} diff --git a/src/qmltest/quicktestevent.cpp b/src/qmltest/quicktestevent.cpp index 9d1e5d1..f573899 100644 --- a/src/qmltest/quicktestevent.cpp +++ b/src/qmltest/quicktestevent.cpp @@ -133,7 +133,6 @@ namespace QtQuickTest pos = view->mapFromScene(ditem->mapToScene(_pos)); eventWindow = view->viewport()->windowHandle(); } - QTEST_ASSERT(button == Qt::NoButton || button & Qt::MouseButtonMask); QTEST_ASSERT(stateKey == 0 || stateKey & Qt::KeyboardModifierMask); @@ -152,9 +151,9 @@ namespace QtQuickTest me = QMouseEvent(QEvent::MouseButtonDblClick, pos, window->mapToGlobal(pos), button, button, stateKey); break; case MouseMove: - QCursor::setPos(window->mapToGlobal(pos)); - qApp->processEvents(); - return; + // with move event the button is NoButton, but 'buttons' holds the currently pressed buttons + me = QMouseEvent(QEvent::MouseMove, pos, window->mapToGlobal(pos), Qt::NoButton, button, stateKey); + break; default: QTEST_ASSERT(false); } @@ -166,6 +165,46 @@ namespace QtQuickTest QTest::qWarn(warning.arg(QString::fromLatin1(mouseActionNames[static_cast(action)])).toAscii().data()); } } + + static void mouseWheel(QWindow* window, QObject* item, Qt::MouseButtons buttons, + Qt::KeyboardModifiers stateKey, + QPointF _pos, int delta, int delay = -1, Qt::Orientation orientation = Qt::Vertical) + { + QTEST_ASSERT(window); + QTEST_ASSERT(item); + if (delay == -1 || delay < QTest::defaultMouseDelay()) + delay = QTest::defaultMouseDelay(); + if (delay > 0) + QTest::qWait(delay); + + QPoint pos; + QDeclarativeView *view = qobject_cast(window); + QWindow *eventWindow = window; +#ifdef QUICK_TEST_SCENEGRAPH + QSGItem *sgitem = qobject_cast(item); + if (sgitem) { + pos = sgitem->mapToScene(_pos).toPoint(); + } else +#endif + { + QDeclarativeItem *ditem = qobject_cast(item); + if (!ditem) { + qWarning("Mouse event target is not an Item"); + return; + } + pos = view->mapFromScene(ditem->mapToScene(_pos)); + eventWindow = view->viewport()->windowHandle(); + } + QTEST_ASSERT(buttons == Qt::NoButton || buttons & Qt::MouseButtonMask); + QTEST_ASSERT(stateKey == 0 || stateKey & Qt::KeyboardModifierMask); + + stateKey &= static_cast(Qt::KeyboardModifierMask); + QWheelEvent we(pos, window->mapToGlobal(pos), delta, buttons, stateKey, orientation); + + QSpontaneKeyEvent::setSpontaneous(&we); // hmmmm + if (!qApp->notify(eventWindow, &we)) + QTest::qWarn("Wheel event not accepted by receiving window"); + } }; bool QuickTestEvent::mousePress @@ -182,6 +221,19 @@ bool QuickTestEvent::mousePress return true; } +bool QuickTestEvent::mouseWheel( + QObject *item, qreal x, qreal y, int buttons, + int modifiers, int delta, int delay, int orientation) +{ + QWindow *view = eventWindow(); + if (!view) + return false; + QtQuickTest::mouseWheel(view, item, Qt::MouseButtons(buttons), + Qt::KeyboardModifiers(modifiers), + QPointF(x, y), delta, delay, Qt::Orientation(orientation)); + return true; +} + bool QuickTestEvent::mouseRelease (QObject *item, qreal x, qreal y, int button, int modifiers, int delay) @@ -225,13 +277,13 @@ bool QuickTestEvent::mouseDoubleClick } bool QuickTestEvent::mouseMove - (QObject *item, qreal x, qreal y, int delay) + (QObject *item, qreal x, qreal y, int delay, int buttons) { QWindow *view = eventWindow(); if (!view) return false; QtQuickTest::mouseEvent(QtQuickTest::MouseMove, view, item, - Qt::NoButton, Qt::NoModifier, + Qt::MouseButton(buttons), Qt::NoModifier, QPointF(x, y), delay); return true; } diff --git a/src/qmltest/quicktestevent_p.h b/src/qmltest/quicktestevent_p.h index d9439a6..31f0d11 100644 --- a/src/qmltest/quicktestevent_p.h +++ b/src/qmltest/quicktestevent_p.h @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). ** All rights reserved. ** Contact: Nokia Corporation (qt-info@nokia.com) ** @@ -67,7 +67,10 @@ public Q_SLOTS: int modifiers, int delay); bool mouseDoubleClick(QObject *item, qreal x, qreal y, int button, int modifiers, int delay); - bool mouseMove(QObject *item, qreal x, qreal y, int delay); + bool mouseMove(QObject *item, qreal x, qreal y, int delay, int buttons); + + bool mouseWheel(QObject *item, qreal x, qreal y, int buttons, + int modifiers, int delta, int delay, int orientation); private: QWindow *eventWindow(); diff --git a/tests/auto/qmltest/events/tst_wheel.qml b/tests/auto/qmltest/events/tst_wheel.qml new file mode 100644 index 0000000..243b932 --- /dev/null +++ b/tests/auto/qmltest/events/tst_wheel.qml @@ -0,0 +1,47 @@ +import QtQuick 2.0 +import QtTest 1.0 + +Rectangle { + id:top + width: 400 + height: 400 + color: "gray" + + Flickable { + id: flick + objectName: "flick" + anchors.fill: parent + contentWidth: 800 + contentHeight: 800 + + Rectangle { + width: flick.contentWidth + height: flick.contentHeight + color: "red" + Rectangle { + width: 50; height: 50; color: "blue" + anchors.centerIn: parent + } + } + } + TestCase { + name: "WheelEvents" + when: windowShown // Must have this line for events to work. + + function test_wheel() { + //mouseWheel(item, x, y, delta, buttons = Qt.NoButton, modifiers = Qt.NoModifier, delay = -1, orientation = Qt.Vertical) + mouseWheel(flick, 200, 200, -120, Qt.NoButton, Qt.NoModifier, -1, Qt.Vertical); + wait(1000); + verify(flick.contentY > 0); + verify(flick.contentX == 0); + flick.contentY = 0; + verify(flick.contentY == 0); + mouseWheel(flick, 200, 200, -120, Qt.NoButton, Qt.NoModifier, -1, Qt.Horizontal); + wait(1000); + verify(flick.contentX > 0); + verify(flick.contentY == 0); + } + + } + +} \ No newline at end of file -- 2.7.4