File indexing completed on 2024-11-10 04:56:13

0001 /*
0002     KWin - the KDE window manager
0003     This file is part of the KDE project.
0004 
0005     SPDX-FileCopyrightText: 2016 Martin Gräßlin <mgraesslin@kde.org>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 #include "mock_libinput.h"
0010 
0011 #include "backends/libinput/device.h"
0012 #include "backends/libinput/events.h"
0013 
0014 #include <QTest>
0015 
0016 #include <linux/input.h>
0017 
0018 Q_DECLARE_METATYPE(libinput_event_type)
0019 
0020 using namespace KWin::LibInput;
0021 using namespace std::literals;
0022 
0023 class TestLibinputGestureEvent : public QObject
0024 {
0025     Q_OBJECT
0026 private Q_SLOTS:
0027     void init();
0028     void cleanup();
0029 
0030     void testType_data();
0031     void testType();
0032 
0033     void testStart_data();
0034     void testStart();
0035 
0036     void testSwipeUpdate();
0037     void testPinchUpdate();
0038 
0039     void testEnd_data();
0040     void testEnd();
0041 
0042 private:
0043     libinput_device *m_nativeDevice = nullptr;
0044     Device *m_device = nullptr;
0045 };
0046 
0047 void TestLibinputGestureEvent::init()
0048 {
0049     m_nativeDevice = new libinput_device;
0050     m_nativeDevice->pointer = true;
0051     m_nativeDevice->gestureSupported = true;
0052     m_nativeDevice->deviceSize = QSizeF(12.5, 13.8);
0053     m_device = new Device(m_nativeDevice);
0054 }
0055 
0056 void TestLibinputGestureEvent::cleanup()
0057 {
0058     delete m_device;
0059     m_device = nullptr;
0060 
0061     delete m_nativeDevice;
0062     m_nativeDevice = nullptr;
0063 }
0064 
0065 void TestLibinputGestureEvent::testType_data()
0066 {
0067     QTest::addColumn<libinput_event_type>("type");
0068 
0069     QTest::newRow("pinch-start") << LIBINPUT_EVENT_GESTURE_PINCH_BEGIN;
0070     QTest::newRow("pinch-update") << LIBINPUT_EVENT_GESTURE_PINCH_UPDATE;
0071     QTest::newRow("pinch-end") << LIBINPUT_EVENT_GESTURE_PINCH_END;
0072     QTest::newRow("swipe-start") << LIBINPUT_EVENT_GESTURE_SWIPE_BEGIN;
0073     QTest::newRow("swipe-update") << LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE;
0074     QTest::newRow("swipe-end") << LIBINPUT_EVENT_GESTURE_SWIPE_END;
0075 }
0076 
0077 void TestLibinputGestureEvent::testType()
0078 {
0079     // this test verifies the initialization of a PointerEvent and the parent Event class
0080     libinput_event_gesture *gestureEvent = new libinput_event_gesture;
0081     QFETCH(libinput_event_type, type);
0082     gestureEvent->type = type;
0083     gestureEvent->device = m_nativeDevice;
0084 
0085     std::unique_ptr<Event> event(Event::create(gestureEvent));
0086     // API of event
0087     QCOMPARE(event->type(), type);
0088     QCOMPARE(event->device(), m_device);
0089     QCOMPARE(event->nativeDevice(), m_nativeDevice);
0090     QCOMPARE((libinput_event *)(*event.get()), gestureEvent);
0091     // verify it's a pointer event
0092     QVERIFY(dynamic_cast<GestureEvent *>(event.get()));
0093     QCOMPARE((libinput_event_gesture *)(*dynamic_cast<GestureEvent *>(event.get())), gestureEvent);
0094 }
0095 
0096 void TestLibinputGestureEvent::testStart_data()
0097 {
0098     QTest::addColumn<libinput_event_type>("type");
0099 
0100     QTest::newRow("pinch") << LIBINPUT_EVENT_GESTURE_PINCH_BEGIN;
0101     QTest::newRow("swipe") << LIBINPUT_EVENT_GESTURE_SWIPE_BEGIN;
0102 }
0103 
0104 void TestLibinputGestureEvent::testStart()
0105 {
0106     libinput_event_gesture *gestureEvent = new libinput_event_gesture;
0107     gestureEvent->device = m_nativeDevice;
0108     QFETCH(libinput_event_type, type);
0109     gestureEvent->type = type;
0110     gestureEvent->fingerCount = 3;
0111     gestureEvent->time = 100ms;
0112 
0113     std::unique_ptr<Event> event(Event::create(gestureEvent));
0114     auto ge = dynamic_cast<GestureEvent *>(event.get());
0115     QVERIFY(ge);
0116     QCOMPARE(ge->fingerCount(), gestureEvent->fingerCount);
0117     QVERIFY(!ge->isCancelled());
0118     QCOMPARE(ge->time(), gestureEvent->time);
0119     QCOMPARE(ge->delta(), QPointF(0, 0));
0120     if (ge->type() == LIBINPUT_EVENT_GESTURE_PINCH_BEGIN) {
0121         auto pe = dynamic_cast<PinchGestureEvent *>(event.get());
0122         QCOMPARE(pe->scale(), 1.0);
0123         QCOMPARE(pe->angleDelta(), 0.0);
0124     }
0125 }
0126 
0127 void TestLibinputGestureEvent::testSwipeUpdate()
0128 {
0129     libinput_event_gesture *gestureEvent = new libinput_event_gesture;
0130     gestureEvent->device = m_nativeDevice;
0131     gestureEvent->type = LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE;
0132     gestureEvent->fingerCount = 2;
0133     gestureEvent->time = 200ms;
0134     gestureEvent->delta = QPointF(2, 3);
0135 
0136     std::unique_ptr<Event> event(Event::create(gestureEvent));
0137     auto se = dynamic_cast<SwipeGestureEvent *>(event.get());
0138     QVERIFY(se);
0139     QCOMPARE(se->fingerCount(), gestureEvent->fingerCount);
0140     QVERIFY(!se->isCancelled());
0141     QCOMPARE(se->time(), gestureEvent->time);
0142     QCOMPARE(se->delta(), QPointF(2, 3));
0143 }
0144 
0145 void TestLibinputGestureEvent::testPinchUpdate()
0146 {
0147     libinput_event_gesture *gestureEvent = new libinput_event_gesture;
0148     gestureEvent->device = m_nativeDevice;
0149     gestureEvent->type = LIBINPUT_EVENT_GESTURE_PINCH_UPDATE;
0150     gestureEvent->fingerCount = 4;
0151     gestureEvent->time = 600ms;
0152     gestureEvent->delta = QPointF(5, 4);
0153     gestureEvent->scale = 2;
0154     gestureEvent->angleDelta = -30;
0155 
0156     std::unique_ptr<Event> event(Event::create(gestureEvent));
0157     auto pe = dynamic_cast<PinchGestureEvent *>(event.get());
0158     QVERIFY(pe);
0159     QCOMPARE(pe->fingerCount(), gestureEvent->fingerCount);
0160     QVERIFY(!pe->isCancelled());
0161     QCOMPARE(pe->time(), gestureEvent->time);
0162     QCOMPARE(pe->delta(), QPointF(5, 4));
0163     QCOMPARE(pe->scale(), gestureEvent->scale);
0164     QCOMPARE(pe->angleDelta(), gestureEvent->angleDelta);
0165 }
0166 
0167 void TestLibinputGestureEvent::testEnd_data()
0168 {
0169     QTest::addColumn<libinput_event_type>("type");
0170     QTest::addColumn<bool>("cancelled");
0171 
0172     QTest::newRow("pinch/not cancelled") << LIBINPUT_EVENT_GESTURE_PINCH_END << false;
0173     QTest::newRow("pinch/cancelled") << LIBINPUT_EVENT_GESTURE_PINCH_END << true;
0174     QTest::newRow("swipe/not cancelled") << LIBINPUT_EVENT_GESTURE_SWIPE_END << false;
0175     QTest::newRow("swipe/cancelled") << LIBINPUT_EVENT_GESTURE_SWIPE_END << true;
0176 }
0177 
0178 void TestLibinputGestureEvent::testEnd()
0179 {
0180     libinput_event_gesture *gestureEvent = new libinput_event_gesture;
0181     gestureEvent->device = m_nativeDevice;
0182     QFETCH(libinput_event_type, type);
0183     gestureEvent->type = type;
0184     gestureEvent->fingerCount = 4;
0185     QFETCH(bool, cancelled);
0186     gestureEvent->cancelled = cancelled;
0187     gestureEvent->time = 300ms;
0188     gestureEvent->scale = 3;
0189 
0190     std::unique_ptr<Event> event(Event::create(gestureEvent));
0191     auto ge = dynamic_cast<GestureEvent *>(event.get());
0192     QVERIFY(ge);
0193     QCOMPARE(ge->fingerCount(), gestureEvent->fingerCount);
0194     QCOMPARE(ge->isCancelled(), cancelled);
0195     QCOMPARE(ge->time(), gestureEvent->time);
0196     QCOMPARE(ge->delta(), QPointF(0, 0));
0197     if (ge->type() == LIBINPUT_EVENT_GESTURE_PINCH_END) {
0198         auto pe = dynamic_cast<PinchGestureEvent *>(event.get());
0199         QCOMPARE(pe->scale(), gestureEvent->scale);
0200         QCOMPARE(pe->angleDelta(), 0.0);
0201     }
0202 }
0203 
0204 QTEST_GUILESS_MAIN(TestLibinputGestureEvent)
0205 #include "gesture_event_test.moc"