KWin
Loading...
Searching...
No Matches
touch_event_test.cpp
Go to the documentation of this file.
1/*
2 KWin - the KDE window manager
3 This file is part of the KDE project.
4
5 SPDX-FileCopyrightText: 2016 Martin Gräßlin <mgraesslin@kde.org>
6
7 SPDX-License-Identifier: GPL-2.0-or-later
8*/
9#include "mock_libinput.h"
10
13
14#include <QTest>
15
16#include <linux/input.h>
17
18Q_DECLARE_METATYPE(libinput_event_type)
19
20using namespace KWin::LibInput;
21using namespace std::literals;
22
23class TestLibinputTouchEvent : public QObject
24{
25 Q_OBJECT
26private Q_SLOTS:
27 void init();
28 void cleanup();
29
30 void testType_data();
31 void testType();
32 void testAbsoluteMotion_data();
33 void testAbsoluteMotion();
34
35private:
36 libinput_device *m_nativeDevice = nullptr;
37 Device *m_device = nullptr;
38};
39
40void TestLibinputTouchEvent::init()
41{
42 m_nativeDevice = new libinput_device;
43 m_nativeDevice->touch = true;
44 m_nativeDevice->deviceSize = QSizeF(12.5, 13.8);
45 m_device = new Device(m_nativeDevice);
46}
47
48void TestLibinputTouchEvent::cleanup()
49{
50 delete m_device;
51 m_device = nullptr;
52
53 delete m_nativeDevice;
54 m_nativeDevice = nullptr;
55}
56
57void TestLibinputTouchEvent::testType_data()
58{
59 QTest::addColumn<libinput_event_type>("type");
60 QTest::addColumn<bool>("hasId");
61
62 QTest::newRow("down") << LIBINPUT_EVENT_TOUCH_DOWN << true;
63 QTest::newRow("up") << LIBINPUT_EVENT_TOUCH_UP << true;
64 QTest::newRow("motion") << LIBINPUT_EVENT_TOUCH_MOTION << true;
65 QTest::newRow("cancel") << LIBINPUT_EVENT_TOUCH_CANCEL << false;
66 QTest::newRow("frame") << LIBINPUT_EVENT_TOUCH_FRAME << false;
67}
68
69void TestLibinputTouchEvent::testType()
70{
71 // this test verifies the initialization of a PointerEvent and the parent Event class
73 QFETCH(libinput_event_type, type);
74 touchEvent->type = type;
75 touchEvent->device = m_nativeDevice;
76 touchEvent->slot = 0;
77
78 std::unique_ptr<Event> event(Event::create(touchEvent));
79 // API of event
80 QCOMPARE(event->type(), type);
81 QCOMPARE(event->device(), m_device);
82 QCOMPARE(event->nativeDevice(), m_nativeDevice);
83 QCOMPARE((libinput_event *)(*event.get()), touchEvent);
84 // verify it's a pointer event
85 QVERIFY(dynamic_cast<TouchEvent *>(event.get()));
86 QCOMPARE((libinput_event_touch *)(*dynamic_cast<TouchEvent *>(event.get())), touchEvent);
87 QFETCH(bool, hasId);
88 if (hasId) {
89 QCOMPARE(dynamic_cast<TouchEvent *>(event.get())->id(), 0);
90 }
91}
92
93void TestLibinputTouchEvent::testAbsoluteMotion_data()
94{
95 QTest::addColumn<libinput_event_type>("type");
96 QTest::newRow("down") << LIBINPUT_EVENT_TOUCH_DOWN;
97 QTest::newRow("motion") << LIBINPUT_EVENT_TOUCH_MOTION;
98}
99
100void TestLibinputTouchEvent::testAbsoluteMotion()
101{
102 // this test verifies absolute touch points (either down or motion)
104 touchEvent->device = m_nativeDevice;
105 QFETCH(libinput_event_type, type);
106 touchEvent->type = type;
107 touchEvent->absolutePos = QPointF(6.25, 6.9);
108 touchEvent->time = 500ms;
109 touchEvent->slot = 1;
110
111 std::unique_ptr<Event> event(Event::create(touchEvent));
112 auto te = dynamic_cast<TouchEvent *>(event.get());
113 QVERIFY(te);
114 QCOMPARE(te->type(), type);
115 QCOMPARE(te->time(), touchEvent->time);
116 QCOMPARE(te->absolutePos(), QPointF(6.25, 6.9));
117 QCOMPARE(te->absolutePos(QSize(1280, 1024)), QPointF(640, 512));
118}
119
120QTEST_GUILESS_MAIN(TestLibinputTouchEvent)
121#include "touch_event_test.moc"
static std::unique_ptr< Event > create(libinput_event *event)
Definition events.cpp:19
Q_DECLARE_METATYPE(KWin::SwitchEvent::State)
Session::Type type
Definition session.cpp:17
libinput_device * device
std::chrono::microseconds time
libinput_event_type type