diff options
Diffstat (limited to 'tests/auto/gui/kernel/qmouseevent/tst_qmouseevent.cpp')
-rw-r--r-- | tests/auto/gui/kernel/qmouseevent/tst_qmouseevent.cpp | 225 |
1 files changed, 170 insertions, 55 deletions
diff --git a/tests/auto/gui/kernel/qmouseevent/tst_qmouseevent.cpp b/tests/auto/gui/kernel/qmouseevent/tst_qmouseevent.cpp index 70e5169648..f703111384 100644 --- a/tests/auto/gui/kernel/qmouseevent/tst_qmouseevent.cpp +++ b/tests/auto/gui/kernel/qmouseevent/tst_qmouseevent.cpp @@ -1,42 +1,25 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - - -#include <QtTest/QtTest> +// Copyright (C) 2020 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + + +#include <QTest> #include <qevent.h> #include <qwindow.h> +#include <QtGui/private/qpointingdevice_p.h> + +#include <QtCore/qpointer.h> + +Q_LOGGING_CATEGORY(lcTests, "qt.gui.tests") class MouseEventWidget : public QWindow { public: - MouseEventWidget(QWindow *parent = 0) : QWindow(parent) + MouseEventWidget(QWindow *parent = nullptr) : QWindow(parent) { + setGeometry(100, 100, 100, 100); } + bool grabExclusive = false; + bool grabPassive = false; bool mousePressEventRecieved; bool mouseReleaseEventRecieved; int mousePressButton; @@ -45,22 +28,55 @@ public: int mouseReleaseButton; int mouseReleaseButtons; int mouseReleaseModifiers; + ulong timestamp; + ulong pressTimestamp; + ulong lastTimestamp; + QVector2D velocity; protected: - void mousePressEvent(QMouseEvent *e) + void mousePressEvent(QMouseEvent *e) override { + const auto &firstPoint = e->point(0); + qCDebug(lcTests) << e << firstPoint; + timestamp = firstPoint.timestamp(); + lastTimestamp = firstPoint.lastTimestamp(); + if (e->type() == QEvent::MouseButtonPress) { + auto firstPoint = e->points().first(); + QCOMPARE(e->exclusiveGrabber(firstPoint), nullptr); + QVERIFY(e->passiveGrabbers(firstPoint).isEmpty()); + QCOMPARE(firstPoint.timeHeld(), 0); + QCOMPARE(firstPoint.pressTimestamp(), e->timestamp()); + pressTimestamp = e->timestamp(); + } QWindow::mousePressEvent(e); mousePressButton = e->button(); mousePressButtons = e->buttons(); mousePressModifiers = e->modifiers(); mousePressEventRecieved = true; e->accept(); + // It's not normal for QWindow to be the grabber, but that's easier to test + // without needing to create child ojects. + if (grabExclusive) + e->setExclusiveGrabber(firstPoint, this); + if (grabPassive) + e->addPassiveGrabber(firstPoint, this); } - void mouseReleaseEvent(QMouseEvent *e) + void mouseMoveEvent(QMouseEvent *e) override { + qCDebug(lcTests) << e << e->points().first(); + timestamp = e->points().first().timestamp(); + lastTimestamp = e->points().first().lastTimestamp(); + velocity = e->points().first().velocity(); + } + void mouseReleaseEvent(QMouseEvent *e) override + { + qCDebug(lcTests) << e << e->points().first(); QWindow::mouseReleaseEvent(e); mouseReleaseButton = e->button(); mouseReleaseButtons = e->buttons(); mouseReleaseModifiers = e->modifiers(); + timestamp = e->points().first().timestamp(); + lastTimestamp = e->points().first().lastTimestamp(); + velocity = e->points().first().velocity(); mouseReleaseEventRecieved = true; e->accept(); } @@ -80,6 +96,10 @@ private slots: void checkMousePressEvent(); void checkMouseReleaseEvent_data(); void checkMouseReleaseEvent(); + void grabbers_data(); + void grabbers(); + void velocity(); + void clone(); private: MouseEventWidget* testMouseWidget; @@ -87,7 +107,7 @@ private: void tst_QMouseEvent::initTestCase() { - testMouseWidget = new MouseEventWidget(0); + testMouseWidget = new MouseEventWidget; testMouseWidget->show(); } @@ -115,26 +135,29 @@ void tst_QMouseEvent::mouseEventBasic() QPointF screen(300, 300); // Press left button QMouseEvent me(QEvent::MouseButtonPress, local, scene, screen, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier); + QVERIFY(me.isInputEvent()); + QVERIFY(me.isPointerEvent()); + QVERIFY(me.isSinglePointEvent()); QCOMPARE(me.isAccepted(), true); QCOMPARE(me.button(), Qt::LeftButton); QCOMPARE(me.buttons(), Qt::LeftButton); - QVERIFY(me.isPressEvent()); - QVERIFY(!me.isReleaseEvent()); + QVERIFY(me.isBeginEvent()); + QVERIFY(!me.isEndEvent()); QCOMPARE(me.position(), local); QCOMPARE(me.scenePosition(), scene); QCOMPARE(me.globalPosition(), screen); // Press right button while left is already pressed - me = QMouseEvent(QEvent::MouseButtonPress, local, scene, screen, Qt::RightButton, Qt::LeftButton | Qt::RightButton, Qt::NoModifier); - QVERIFY(me.isPressEvent()); - QVERIFY(!me.isReleaseEvent()); + QMouseEvent me2(QEvent::MouseButtonPress, local, scene, screen, Qt::RightButton, Qt::LeftButton | Qt::RightButton, Qt::NoModifier); + QVERIFY(me2.isBeginEvent()); + QVERIFY(!me2.isEndEvent()); // Release right button while left is still pressed - me = QMouseEvent(QEvent::MouseButtonRelease, local, scene, screen, Qt::RightButton, Qt::LeftButton, Qt::NoModifier); - QVERIFY(!me.isPressEvent()); - QVERIFY(me.isReleaseEvent()); + QMouseEvent me3 = QMouseEvent(QEvent::MouseButtonRelease, local, scene, screen, Qt::RightButton, Qt::LeftButton, Qt::NoModifier); + QVERIFY(!me3.isBeginEvent()); + QVERIFY(me3.isEndEvent()); // Release left button in the usual way - me = QMouseEvent(QEvent::MouseButtonRelease, local, scene, screen, Qt::LeftButton, Qt::NoButton, Qt::NoModifier); - QVERIFY(!me.isPressEvent()); - QVERIFY(me.isReleaseEvent()); + QMouseEvent me4 = QMouseEvent(QEvent::MouseButtonRelease, local, scene, screen, Qt::LeftButton, Qt::NoButton, Qt::NoModifier); + QVERIFY(!me4.isBeginEvent()); + QVERIFY(me4.isEndEvent()); } void tst_QMouseEvent::checkMousePressEvent_data() @@ -152,11 +175,11 @@ void tst_QMouseEvent::checkMousePressEvent_data() QTest::newRow("rightButton-controlkey") << int(Qt::RightButton) << int(Qt::ControlModifier); QTest::newRow("rightButton-altkey") << int(Qt::RightButton) << int(Qt::AltModifier); QTest::newRow("rightButton-metakey") << int(Qt::RightButton) << int(Qt::MetaModifier); - QTest::newRow("midButton-nokey") << int(Qt::MidButton) << int(Qt::NoButton); - QTest::newRow("midButton-shiftkey") << int(Qt::MidButton) << int(Qt::ShiftModifier); - QTest::newRow("midButton-controlkey") << int(Qt::MidButton) << int(Qt::ControlModifier); - QTest::newRow("midButton-altkey") << int(Qt::MidButton) << int(Qt::AltModifier); - QTest::newRow("midButton-metakey") << int(Qt::MidButton) << int(Qt::MetaModifier); + QTest::newRow("middleButton-nokey") << int(Qt::MiddleButton) << int(Qt::NoButton); + QTest::newRow("middleButton-shiftkey") << int(Qt::MiddleButton) << int(Qt::ShiftModifier); + QTest::newRow("middleButton-controlkey") << int(Qt::MiddleButton) << int(Qt::ControlModifier); + QTest::newRow("middleButton-altkey") << int(Qt::MiddleButton) << int(Qt::AltModifier); + QTest::newRow("middleButton-metakey") << int(Qt::MiddleButton) << int(Qt::MetaModifier); } void tst_QMouseEvent::checkMousePressEvent() @@ -193,11 +216,11 @@ void tst_QMouseEvent::checkMouseReleaseEvent_data() QTest::newRow("rightButton-controlkey") << int(Qt::RightButton) << int(Qt::ControlModifier); QTest::newRow("rightButton-altkey") << int(Qt::RightButton) << int(Qt::AltModifier); QTest::newRow("rightButton-metakey") << int(Qt::RightButton) << int(Qt::MetaModifier); - QTest::newRow("midButton-nokey") << int(Qt::MidButton) << int(Qt::NoButton); - QTest::newRow("midButton-shiftkey") << int(Qt::MidButton) << int(Qt::ShiftModifier); - QTest::newRow("midButton-controlkey") << int(Qt::MidButton) << int(Qt::ControlModifier); - QTest::newRow("midButton-altkey") << int(Qt::MidButton) << int(Qt::AltModifier); - QTest::newRow("midButton-metakey") << int(Qt::MidButton) << int(Qt::MetaModifier); + QTest::newRow("middleButton-nokey") << int(Qt::MiddleButton) << int(Qt::NoButton); + QTest::newRow("middleButton-shiftkey") << int(Qt::MiddleButton) << int(Qt::ShiftModifier); + QTest::newRow("middleButton-controlkey") << int(Qt::MiddleButton) << int(Qt::ControlModifier); + QTest::newRow("middleButton-altkey") << int(Qt::MiddleButton) << int(Qt::AltModifier); + QTest::newRow("middleButton-metakey") << int(Qt::MiddleButton) << int(Qt::MetaModifier); } void tst_QMouseEvent::checkMouseReleaseEvent() @@ -216,5 +239,97 @@ void tst_QMouseEvent::checkMouseReleaseEvent() QCOMPARE(testMouseWidget->mouseReleaseModifiers, modifiers); } +void tst_QMouseEvent::grabbers_data() +{ + QTest::addColumn<bool>("grabExclusive"); + QTest::addColumn<bool>("grabPassive"); + + QTest::newRow("no grab") << false << false; + QTest::newRow("exclusive") << true << false; + QTest::newRow("passive") << false << true; +} + +void tst_QMouseEvent::grabbers() +{ + QFETCH(bool, grabExclusive); + QFETCH(bool, grabPassive); + + testMouseWidget->grabExclusive = grabExclusive; + testMouseWidget->grabPassive = grabPassive; + + QTest::mousePress(testMouseWidget, Qt::LeftButton, Qt::KeyboardModifiers(), {10, 10}); + + auto devPriv = QPointingDevicePrivate::get(QPointingDevice::primaryPointingDevice()); + QCOMPARE(devPriv->activePoints.count(), 1); + + // Ensure that grabbers are persistent between events, within the stored touchpoints + auto firstEPD = devPriv->pointById(0); + QCOMPARE(firstEPD->eventPoint.pressTimestamp(), testMouseWidget->pressTimestamp); + QCOMPARE(firstEPD->exclusiveGrabber, grabExclusive ? testMouseWidget : nullptr); + QCOMPARE(firstEPD->passiveGrabbers.size(), grabPassive ? 1 : 0); + if (grabPassive) + QCOMPARE(firstEPD->passiveGrabbers.first(), testMouseWidget); + + // Ensure that grabbers are forgotten after release delivery + QTest::mouseRelease(testMouseWidget, Qt::LeftButton, Qt::KeyboardModifiers(), {10, 10}); + QTRY_COMPARE(firstEPD->exclusiveGrabber, nullptr); + QCOMPARE(firstEPD->passiveGrabbers.size(), 0); +} + +void tst_QMouseEvent::velocity() +{ + testMouseWidget->grabExclusive = true; + auto devPriv = QPointingDevicePrivate::get(const_cast<QPointingDevice *>(QPointingDevice::primaryPointingDevice())); + devPriv->activePoints.clear(); + + qCDebug(lcTests) << "sending mouse press event"; + QPoint pos(10, 10); + QTest::mousePress(testMouseWidget, Qt::LeftButton, Qt::KeyboardModifiers(), pos); + QCOMPARE(devPriv->activePoints.count(), 1); + QVERIFY(devPriv->activePoints.count() <= 2); + const auto &firstPoint = devPriv->pointById(0)->eventPoint; + QVERIFY(firstPoint.timestamp() > 0); + QCOMPARE(firstPoint.state(), QEventPoint::State::Pressed); + + ulong timestamp = firstPoint.timestamp(); + for (int i = 1; i < 4; ++i) { + qCDebug(lcTests) << "sending mouse move event" << i; + pos += {10, 10}; + QTest::mouseMove(testMouseWidget, pos, 1); + qApp->processEvents(); + qCDebug(lcTests) << firstPoint; + // currently we expect it to be updated in-place in devPriv->activePoints + QVERIFY(firstPoint.timestamp() > timestamp); + QVERIFY(testMouseWidget->timestamp > testMouseWidget->lastTimestamp); + QCOMPARE(testMouseWidget->timestamp, firstPoint.timestamp()); + timestamp = firstPoint.timestamp(); + QVERIFY(testMouseWidget->velocity.x() > 0); + QVERIFY(testMouseWidget->velocity.y() > 0); + } + QTest::mouseRelease(testMouseWidget, Qt::LeftButton, Qt::KeyboardModifiers(), pos, 1); + qCDebug(lcTests) << firstPoint; + QVERIFY(testMouseWidget->velocity.x() > 0); + QVERIFY(testMouseWidget->velocity.y() > 0); +} + +void tst_QMouseEvent::clone() +{ + const QPointF pos(10.0f, 10.0f); + + QMouseEvent originalMe(QEvent::MouseButtonPress, pos, pos, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier); + QVERIFY(!originalMe.allPointsAccepted()); + QVERIFY(!originalMe.points().first().isAccepted()); + + // create a clone of the original + std::unique_ptr<QMouseEvent> clonedMe(originalMe.clone()); + QVERIFY(!clonedMe->allPointsAccepted()); + QVERIFY(!clonedMe->points().first().isAccepted()); + + // now we alter originalMe, which should *not* change clonedMe + originalMe.setAccepted(true); + QVERIFY(!clonedMe->allPointsAccepted()); + QVERIFY(!clonedMe->points().first().isAccepted()); +} + QTEST_MAIN(tst_QMouseEvent) #include "tst_qmouseevent.moc" |