1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <qtest.h>
#include <QtQuick>
#include <QtQuick/private/qquickmousearea_p.h>
#include <QDebug>
#include <QtQuickTestUtils/private/qmlutils_p.h>
#include <QtQuickTestUtils/private/viewtestutils_p.h>
class TestView : public QQuickView
{
public:
void handleEvent(QEvent *ev) { event(ev); }
};
class tst_events : public QQmlDataTest
{
Q_OBJECT
public:
tst_events();
private slots:
void mousePressRelease();
void mouseMove();
void touchToMousePressRelease();
void touchToMousePressMove();
public slots:
void initTestCase() override {
QQmlDataTest::initTestCase();
window.setBaseSize(QSize(400, 400));
window.setSource(testFileUrl("mouseevent.qml"));
window.show();
QVERIFY(QTest::qWaitForWindowExposed(&window));
}
private:
TestView window;
};
tst_events::tst_events()
: QQmlDataTest(QT_QMLTEST_DATADIR)
{
}
void tst_events::mousePressRelease()
{
QQuickMouseArea *mouseArea = window.rootObject()->findChild<QQuickMouseArea *>("mouseArea");
QCOMPARE(mouseArea->isPressed(), false);
const QPoint localPos(100, 100);
const QPoint globalPos = window.mapToGlobal(localPos);
QBENCHMARK {
QMouseEvent pressEvent(QEvent::MouseButtonPress, localPos, globalPos, Qt::LeftButton, Qt::LeftButton, {});
window.handleEvent(&pressEvent);
QCOMPARE(mouseArea->isPressed(), true);
QMouseEvent releaseEvent(QEvent::MouseButtonRelease, localPos, globalPos, Qt::LeftButton, Qt::LeftButton, {});
window.handleEvent(&releaseEvent);
}
QCOMPARE(mouseArea->isPressed(), false);
}
void tst_events::mouseMove()
{
QQuickMouseArea *mouseArea = window.rootObject()->findChild<QQuickMouseArea *>("mouseArea");
const QPoint localPos1(100, 100);
const QPoint globalPos1 = window.mapToGlobal(localPos1);
const QPoint localPos2(101, 100);
const QPoint globalPos2 = window.mapToGlobal(localPos2);
QCOMPARE(mouseArea->isPressed(), false);
QMouseEvent pressEvent(QEvent::MouseButtonPress, localPos1, globalPos1, Qt::LeftButton, Qt::LeftButton, {});
window.handleEvent(&pressEvent);
QCOMPARE(mouseArea->isPressed(), true);
QMouseEvent moveEvent1(QEvent::MouseMove, localPos2, globalPos2, Qt::LeftButton, Qt::LeftButton, {});
QMouseEvent moveEvent2(QEvent::MouseMove, localPos1, globalPos1, Qt::LeftButton, Qt::LeftButton, {});
QBENCHMARK {
window.handleEvent(&moveEvent1);
window.handleEvent(&moveEvent2);
}
QCOMPARE(mouseArea->isPressed(), true);
QMouseEvent releaseEvent(QEvent::MouseButtonRelease, localPos1, globalPos1, Qt::LeftButton, Qt::LeftButton, {});
window.handleEvent(&releaseEvent);
QCOMPARE(mouseArea->isPressed(), false);
}
void tst_events::touchToMousePressRelease()
{
QQuickMouseArea *mouseArea = window.rootObject()->findChild<QQuickMouseArea *>("mouseArea");
QCOMPARE(mouseArea->isPressed(), false);
auto device = QTest::createTouchDevice();
auto p = QPoint(80, 80);
QBENCHMARK {
QTest::touchEvent(&window, device).press(0, p, &window).commit();
QCOMPARE(mouseArea->isPressed(), true);
QTest::touchEvent(&window, device).release(0, p, &window).commit();
}
QCOMPARE(mouseArea->isPressed(), false);
}
void tst_events::touchToMousePressMove()
{
QQuickMouseArea *mouseArea = window.rootObject()->findChild<QQuickMouseArea *>("mouseArea");
QCOMPARE(mouseArea->isPressed(), false);
auto device = QTest::createTouchDevice();
auto p = QPoint(80, 80);
auto p2 = QPoint(81, 80);
QTest::touchEvent(&window, device).press(0, p, &window).commit();
QQuickTouchUtils::flush(&window);
QCOMPARE(mouseArea->isPressed(), true);
QBENCHMARK {
QTest::touchEvent(&window, device).move(0, p, &window).commit();
QCOMPARE(mouseArea->isPressed(), true);
QTest::touchEvent(&window, device).move(0, p2, &window).commit();
}
QCOMPARE(mouseArea->isPressed(), true);
QTest::touchEvent(&window, device).release(0, p, &window).commit();
QQuickTouchUtils::flush(&window);
QCOMPARE(mouseArea->isPressed(), false);
}
QTEST_MAIN(tst_events)
#include "tst_events.moc"
|