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

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 TestLibinputTouchEvent : 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     void testAbsoluteMotion_data();
0033     void testAbsoluteMotion();
0034 
0035 private:
0036     libinput_device *m_nativeDevice = nullptr;
0037     Device *m_device = nullptr;
0038 };
0039 
0040 void TestLibinputTouchEvent::init()
0041 {
0042     m_nativeDevice = new libinput_device;
0043     m_nativeDevice->touch = true;
0044     m_nativeDevice->deviceSize = QSizeF(12.5, 13.8);
0045     m_device = new Device(m_nativeDevice);
0046 }
0047 
0048 void TestLibinputTouchEvent::cleanup()
0049 {
0050     delete m_device;
0051     m_device = nullptr;
0052 
0053     delete m_nativeDevice;
0054     m_nativeDevice = nullptr;
0055 }
0056 
0057 void TestLibinputTouchEvent::testType_data()
0058 {
0059     QTest::addColumn<libinput_event_type>("type");
0060     QTest::addColumn<bool>("hasId");
0061 
0062     QTest::newRow("down") << LIBINPUT_EVENT_TOUCH_DOWN << true;
0063     QTest::newRow("up") << LIBINPUT_EVENT_TOUCH_UP << true;
0064     QTest::newRow("motion") << LIBINPUT_EVENT_TOUCH_MOTION << true;
0065     QTest::newRow("cancel") << LIBINPUT_EVENT_TOUCH_CANCEL << false;
0066     QTest::newRow("frame") << LIBINPUT_EVENT_TOUCH_FRAME << false;
0067 }
0068 
0069 void TestLibinputTouchEvent::testType()
0070 {
0071     // this test verifies the initialization of a PointerEvent and the parent Event class
0072     libinput_event_touch *touchEvent = new libinput_event_touch;
0073     QFETCH(libinput_event_type, type);
0074     touchEvent->type = type;
0075     touchEvent->device = m_nativeDevice;
0076     touchEvent->slot = 0;
0077 
0078     std::unique_ptr<Event> event(Event::create(touchEvent));
0079     // API of event
0080     QCOMPARE(event->type(), type);
0081     QCOMPARE(event->device(), m_device);
0082     QCOMPARE(event->nativeDevice(), m_nativeDevice);
0083     QCOMPARE((libinput_event *)(*event.get()), touchEvent);
0084     // verify it's a pointer event
0085     QVERIFY(dynamic_cast<TouchEvent *>(event.get()));
0086     QCOMPARE((libinput_event_touch *)(*dynamic_cast<TouchEvent *>(event.get())), touchEvent);
0087     QFETCH(bool, hasId);
0088     if (hasId) {
0089         QCOMPARE(dynamic_cast<TouchEvent *>(event.get())->id(), 0);
0090     }
0091 }
0092 
0093 void TestLibinputTouchEvent::testAbsoluteMotion_data()
0094 {
0095     QTest::addColumn<libinput_event_type>("type");
0096     QTest::newRow("down") << LIBINPUT_EVENT_TOUCH_DOWN;
0097     QTest::newRow("motion") << LIBINPUT_EVENT_TOUCH_MOTION;
0098 }
0099 
0100 void TestLibinputTouchEvent::testAbsoluteMotion()
0101 {
0102     // this test verifies absolute touch points (either down or motion)
0103     libinput_event_touch *touchEvent = new libinput_event_touch;
0104     touchEvent->device = m_nativeDevice;
0105     QFETCH(libinput_event_type, type);
0106     touchEvent->type = type;
0107     touchEvent->absolutePos = QPointF(6.25, 6.9);
0108     touchEvent->time = 500ms;
0109     touchEvent->slot = 1;
0110 
0111     std::unique_ptr<Event> event(Event::create(touchEvent));
0112     auto te = dynamic_cast<TouchEvent *>(event.get());
0113     QVERIFY(te);
0114     QCOMPARE(te->type(), type);
0115     QCOMPARE(te->time(), touchEvent->time);
0116     QCOMPARE(te->absolutePos(), QPointF(6.25, 6.9));
0117     QCOMPARE(te->absolutePos(QSize(1280, 1024)), QPointF(640, 512));
0118 }
0119 
0120 QTEST_GUILESS_MAIN(TestLibinputTouchEvent)
0121 #include "touch_event_test.moc"