KWin
Loading...
Searching...
No Matches
pointer_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)
19Q_DECLARE_METATYPE(libinput_button_state)
20
21using namespace KWin::LibInput;
22using namespace std::literals;
23
24class TestLibinputPointerEvent : public QObject
25{
26 Q_OBJECT
27private Q_SLOTS:
28 void init();
29 void cleanup();
30
31 void testType_data();
32 void testType();
33 void testButton_data();
34 void testButton();
35 void testScrollWheel_data();
36 void testScrollWheel();
37 void testScrollFinger_data();
38 void testScrollFinger();
39 void testScrollContinuous_data();
40 void testScrollContinuous();
41 void testMotion();
42 void testAbsoluteMotion();
43
44private:
45 libinput_device *m_nativeDevice = nullptr;
46 Device *m_device = nullptr;
47};
48
49void TestLibinputPointerEvent::init()
50{
51 m_nativeDevice = new libinput_device;
52 m_nativeDevice->pointer = true;
53 m_nativeDevice->deviceSize = QSizeF(12.5, 13.8);
54 m_device = new Device(m_nativeDevice);
55}
56
57void TestLibinputPointerEvent::cleanup()
58{
59 delete m_device;
60 m_device = nullptr;
61
62 delete m_nativeDevice;
63 m_nativeDevice = nullptr;
64}
65
66void TestLibinputPointerEvent::testType_data()
67{
68 QTest::addColumn<libinput_event_type>("type");
69
70 QTest::newRow("motion") << LIBINPUT_EVENT_POINTER_MOTION;
71 QTest::newRow("absolute motion") << LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE;
72 QTest::newRow("button") << LIBINPUT_EVENT_POINTER_BUTTON;
73 QTest::newRow("scroll wheel") << LIBINPUT_EVENT_POINTER_SCROLL_WHEEL;
74 QTest::newRow("scroll finger") << LIBINPUT_EVENT_POINTER_SCROLL_FINGER;
75 QTest::newRow("scroll continuous") << LIBINPUT_EVENT_POINTER_SCROLL_CONTINUOUS;
76}
77
78void TestLibinputPointerEvent::testType()
79{
80 // this test verifies the initialization of a PointerEvent and the parent Event class
82 QFETCH(libinput_event_type, type);
83 pointerEvent->type = type;
84 pointerEvent->device = m_nativeDevice;
85
86 std::unique_ptr<Event> event(Event::create(pointerEvent));
87 // API of event
88 QCOMPARE(event->type(), type);
89 QCOMPARE(event->device(), m_device);
90 QCOMPARE(event->nativeDevice(), m_nativeDevice);
91 QCOMPARE((libinput_event *)(*event.get()), pointerEvent);
92 // verify it's a pointer event
93 QVERIFY(dynamic_cast<PointerEvent *>(event.get()));
94 QCOMPARE((libinput_event_pointer *)(*dynamic_cast<PointerEvent *>(event.get())), pointerEvent);
95}
96
97void TestLibinputPointerEvent::testButton_data()
98{
99 QTest::addColumn<libinput_button_state>("buttonState");
100 QTest::addColumn<KWin::InputRedirection::PointerButtonState>("expectedButtonState");
101 QTest::addColumn<quint32>("button");
102 QTest::addColumn<quint32>("time");
103
104 QTest::newRow("pressed") << LIBINPUT_BUTTON_STATE_RELEASED << KWin::InputRedirection::PointerButtonReleased << quint32(BTN_RIGHT) << 100u;
105 QTest::newRow("released") << LIBINPUT_BUTTON_STATE_PRESSED << KWin::InputRedirection::PointerButtonPressed << quint32(BTN_LEFT) << 200u;
106}
107
108void TestLibinputPointerEvent::testButton()
109{
110 // this test verifies the button press/release
112 pointerEvent->device = m_nativeDevice;
113 pointerEvent->type = LIBINPUT_EVENT_POINTER_BUTTON;
114 QFETCH(libinput_button_state, buttonState);
115 pointerEvent->buttonState = buttonState;
116 QFETCH(quint32, button);
117 pointerEvent->button = button;
118 QFETCH(quint32, time);
119 pointerEvent->time = std::chrono::milliseconds(time);
120
121 std::unique_ptr<Event> event(Event::create(pointerEvent));
122 auto pe = dynamic_cast<PointerEvent *>(event.get());
123 QVERIFY(pe);
124 QCOMPARE(pe->type(), LIBINPUT_EVENT_POINTER_BUTTON);
125 QTEST(pe->buttonState(), "expectedButtonState");
126 QCOMPARE(pe->button(), button);
127 QCOMPARE(pe->time(), pointerEvent->time);
128}
129
130void TestLibinputPointerEvent::testScrollWheel_data()
131{
132 QTest::addColumn<bool>("horizontal");
133 QTest::addColumn<bool>("vertical");
134 QTest::addColumn<QPointF>("value");
135 QTest::addColumn<QPoint>("valueV120");
136 QTest::addColumn<quint32>("time");
137
138 QTest::newRow("wheel/horizontal") << true << false << QPointF(3.0, 0.0) << QPoint(120, 0) << 100u;
139 QTest::newRow("wheel/vertical") << false << true << QPointF(0.0, 2.5) << QPoint(0, 120) << 200u;
140 QTest::newRow("wheel/both") << true << true << QPointF(1.1, 4.2) << QPoint(120, 120) << 300u;
141}
142
143void TestLibinputPointerEvent::testScrollWheel()
144{
145 // this test verifies pointer axis functionality
147 pointerEvent->device = m_nativeDevice;
148 pointerEvent->type = LIBINPUT_EVENT_POINTER_SCROLL_WHEEL;
149 QFETCH(bool, horizontal);
150 QFETCH(bool, vertical);
151 QFETCH(QPointF, value);
152 QFETCH(QPoint, valueV120);
153 QFETCH(quint32, time);
154 pointerEvent->horizontalAxis = horizontal;
155 pointerEvent->verticalAxis = vertical;
156 pointerEvent->horizontalScrollValue = value.x();
157 pointerEvent->verticalScrollValue = value.y();
158 pointerEvent->horizontalScrollValueV120 = valueV120.x();
159 pointerEvent->verticalScrollValueV120 = valueV120.y();
160 pointerEvent->time = std::chrono::milliseconds(time);
161
162 std::unique_ptr<Event> event(Event::create(pointerEvent));
163 auto pe = dynamic_cast<PointerEvent *>(event.get());
164 QVERIFY(pe);
165 QCOMPARE(pe->type(), LIBINPUT_EVENT_POINTER_SCROLL_WHEEL);
166 QCOMPARE(pe->axis().contains(KWin::InputRedirection::PointerAxisHorizontal), horizontal);
167 QCOMPARE(pe->axis().contains(KWin::InputRedirection::PointerAxisVertical), vertical);
168 QCOMPARE(pe->scrollValue(KWin::InputRedirection::PointerAxisHorizontal), value.x());
169 QCOMPARE(pe->scrollValue(KWin::InputRedirection::PointerAxisVertical), value.y());
170 QCOMPARE(pe->scrollValueV120(KWin::InputRedirection::PointerAxisHorizontal), valueV120.x());
171 QCOMPARE(pe->scrollValueV120(KWin::InputRedirection::PointerAxisVertical), valueV120.y());
172 QCOMPARE(pe->time(), pointerEvent->time);
173}
174
175void TestLibinputPointerEvent::testScrollFinger_data()
176{
177 QTest::addColumn<bool>("horizontal");
178 QTest::addColumn<bool>("vertical");
179 QTest::addColumn<QPointF>("value");
180 QTest::addColumn<quint32>("time");
181
182 QTest::newRow("finger/horizontal") << true << false << QPointF(3.0, 0.0) << 400u;
183 QTest::newRow("stop finger/horizontal") << true << false << QPointF(0.0, 0.0) << 500u;
184 QTest::newRow("finger/vertical") << false << true << QPointF(0.0, 2.5) << 600u;
185 QTest::newRow("stop finger/vertical") << false << true << QPointF(0.0, 0.0) << 700u;
186 QTest::newRow("finger/both") << true << true << QPointF(1.1, 4.2) << 800u;
187 QTest::newRow("stop finger/both") << true << true << QPointF(0.0, 0.0) << 900u;
188}
189
190void TestLibinputPointerEvent::testScrollFinger()
191{
192 // this test verifies pointer axis functionality
194 pointerEvent->device = m_nativeDevice;
195 pointerEvent->type = LIBINPUT_EVENT_POINTER_SCROLL_FINGER;
196 QFETCH(bool, horizontal);
197 QFETCH(bool, vertical);
198 QFETCH(QPointF, value);
199 QFETCH(quint32, time);
200 pointerEvent->horizontalAxis = horizontal;
201 pointerEvent->verticalAxis = vertical;
202 pointerEvent->horizontalScrollValue = value.x();
203 pointerEvent->verticalScrollValue = value.y();
204 pointerEvent->time = std::chrono::milliseconds(time);
205
206 std::unique_ptr<Event> event(Event::create(pointerEvent));
207 auto pe = dynamic_cast<PointerEvent *>(event.get());
208 QVERIFY(pe);
209 QCOMPARE(pe->type(), LIBINPUT_EVENT_POINTER_SCROLL_FINGER);
210 QCOMPARE(pe->axis().contains(KWin::InputRedirection::PointerAxisHorizontal), horizontal);
211 QCOMPARE(pe->axis().contains(KWin::InputRedirection::PointerAxisVertical), vertical);
212 QCOMPARE(pe->scrollValue(KWin::InputRedirection::PointerAxisHorizontal), value.x());
213 QCOMPARE(pe->scrollValue(KWin::InputRedirection::PointerAxisVertical), value.y());
214 QCOMPARE(pe->time(), pointerEvent->time);
215}
216
217void TestLibinputPointerEvent::testScrollContinuous_data()
218{
219 QTest::addColumn<bool>("horizontal");
220 QTest::addColumn<bool>("vertical");
221 QTest::addColumn<QPointF>("value");
222 QTest::addColumn<quint32>("time");
223
224 QTest::newRow("continuous/horizontal") << true << false << QPointF(3.0, 0.0) << 1000u;
225 QTest::newRow("continuous/vertical") << false << true << QPointF(0.0, 2.5) << 1100u;
226 QTest::newRow("continuous/both") << true << true << QPointF(1.1, 4.2) << 1200u;
227}
228
229void TestLibinputPointerEvent::testScrollContinuous()
230{
231 // this test verifies pointer axis functionality
233 pointerEvent->device = m_nativeDevice;
234 pointerEvent->type = LIBINPUT_EVENT_POINTER_SCROLL_CONTINUOUS;
235 QFETCH(bool, horizontal);
236 QFETCH(bool, vertical);
237 QFETCH(QPointF, value);
238 QFETCH(quint32, time);
239 pointerEvent->horizontalAxis = horizontal;
240 pointerEvent->verticalAxis = vertical;
241 pointerEvent->horizontalScrollValue = value.x();
242 pointerEvent->verticalScrollValue = value.y();
243 pointerEvent->time = std::chrono::milliseconds(time);
244
245 std::unique_ptr<Event> event(Event::create(pointerEvent));
246 auto pe = dynamic_cast<PointerEvent *>(event.get());
247 QVERIFY(pe);
248 QCOMPARE(pe->type(), LIBINPUT_EVENT_POINTER_SCROLL_CONTINUOUS);
249 QCOMPARE(pe->axis().contains(KWin::InputRedirection::PointerAxisHorizontal), horizontal);
250 QCOMPARE(pe->axis().contains(KWin::InputRedirection::PointerAxisVertical), vertical);
251 QCOMPARE(pe->scrollValue(KWin::InputRedirection::PointerAxisHorizontal), value.x());
252 QCOMPARE(pe->scrollValue(KWin::InputRedirection::PointerAxisVertical), value.y());
253 QCOMPARE(pe->time(), pointerEvent->time);
254}
255
256void TestLibinputPointerEvent::testMotion()
257{
258 // this test verifies pointer motion (delta)
260 pointerEvent->device = m_nativeDevice;
261 pointerEvent->type = LIBINPUT_EVENT_POINTER_MOTION;
262 pointerEvent->delta = QPointF(2.1, 4.5);
263 pointerEvent->time = 500ms;
264
265 std::unique_ptr<Event> event(Event::create(pointerEvent));
266 auto pe = dynamic_cast<PointerEvent *>(event.get());
267 QVERIFY(pe);
268 QCOMPARE(pe->type(), LIBINPUT_EVENT_POINTER_MOTION);
269 QCOMPARE(pe->time(), pointerEvent->time);
270 QCOMPARE(pe->delta(), QPointF(2.1, 4.5));
271}
272
273void TestLibinputPointerEvent::testAbsoluteMotion()
274{
275 // this test verifies absolute pointer motion
277 pointerEvent->device = m_nativeDevice;
278 pointerEvent->type = LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE;
279 pointerEvent->absolutePos = QPointF(6.25, 6.9);
280 pointerEvent->time = 500ms;
281
282 std::unique_ptr<Event> event(Event::create(pointerEvent));
283 auto pe = dynamic_cast<PointerEvent *>(event.get());
284 QVERIFY(pe);
285 QCOMPARE(pe->type(), LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE);
286 QCOMPARE(pe->time(), pointerEvent->time);
287 QCOMPARE(pe->absolutePos(), QPointF(6.25, 6.9));
288 QCOMPARE(pe->absolutePos(QSize(1280, 1024)), QPointF(640, 512));
289}
290
291QTEST_GUILESS_MAIN(TestLibinputPointerEvent)
292#include "pointer_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_button_state buttonState
libinput_device * device
std::chrono::microseconds time
libinput_event_type type