KWin
Loading...
Searching...
No Matches
input_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
12#include "input_event.h"
13
14#include <QTest>
15
17
18using namespace KWin;
19using namespace KWin::LibInput;
20using namespace std::literals;
21
22class InputEventsTest : public QObject
23{
24 Q_OBJECT
25private Q_SLOTS:
26 void testInitMouseEvent_data();
27 void testInitMouseEvent();
28 void testInitKeyEvent_data();
29 void testInitKeyEvent();
30 void testInitWheelEvent_data();
31 void testInitWheelEvent();
32 void testInitSwitchEvent_data();
33 void testInitSwitchEvent();
34};
35
36void InputEventsTest::testInitMouseEvent_data()
37{
38 QTest::addColumn<QEvent::Type>("type");
39
40 QTest::newRow("Press") << QEvent::MouseButtonPress;
41 QTest::newRow("Release") << QEvent::MouseButtonRelease;
42 QTest::newRow("Move") << QEvent::MouseMove;
43}
44
45void InputEventsTest::testInitMouseEvent()
46{
47 // this test verifies that a MouseEvent is constructed correctly
48
49 // first create the test LibInput::Device
50 libinput_device device;
51 Device d(&device);
52
53 QFETCH(QEvent::Type, type);
54 // now create our own event
55 MouseEvent event(type, QPointF(100, 200), Qt::LeftButton, Qt::LeftButton | Qt::RightButton,
56 Qt::ShiftModifier | Qt::ControlModifier, 300ms, QPointF(1, 2), QPointF(3, 4), &d);
57 // and verify the contract of QMouseEvent
58 QCOMPARE(event.type(), type);
59 QCOMPARE(event.globalPos(), QPoint(100, 200));
60 QCOMPARE(event.screenPos(), QPointF(100, 200));
61 QCOMPARE(event.localPos(), QPointF(100, 200));
62 QCOMPARE(event.button(), Qt::LeftButton);
63 QCOMPARE(event.buttons(), Qt::LeftButton | Qt::RightButton);
64 QCOMPARE(event.modifiers(), Qt::ShiftModifier | Qt::ControlModifier);
65 QCOMPARE(event.timestamp(), 300ms);
66 // and our custom argument
67 QCOMPARE(event.device(), &d);
68 QCOMPARE(event.delta(), QPointF(1, 2));
69 QCOMPARE(event.deltaUnaccelerated(), QPointF(3, 4));
70}
71
72void InputEventsTest::testInitKeyEvent_data()
73{
74 QTest::addColumn<QEvent::Type>("type");
75 QTest::addColumn<bool>("autorepeat");
76
77 QTest::newRow("Press") << QEvent::KeyPress << false;
78 QTest::newRow("Repeat") << QEvent::KeyPress << true;
79 QTest::newRow("Release") << QEvent::KeyRelease << false;
80}
81
82void InputEventsTest::testInitKeyEvent()
83{
84 // this test verifies that a KeyEvent is constructed correctly
85
86 // first create the test LibInput::Device
87 libinput_device device;
88 Device d(&device);
89
90 // setup event
91 QFETCH(QEvent::Type, type);
92 QFETCH(bool, autorepeat);
93 KeyEvent event(type, Qt::Key_Space, Qt::ShiftModifier | Qt::ControlModifier, 200, 300,
94 QStringLiteral(" "), autorepeat, 400ms, &d);
95 // and verify the contract of QKeyEvent
96 QCOMPARE(event.type(), type);
97 QCOMPARE(event.isAutoRepeat(), autorepeat);
98 QCOMPARE(event.key(), int(Qt::Key_Space));
99 QCOMPARE(event.nativeScanCode(), 200u);
100 QCOMPARE(event.nativeVirtualKey(), 300u);
101 QCOMPARE(event.text(), QStringLiteral(" "));
102 QCOMPARE(event.count(), 1);
103 QCOMPARE(event.nativeModifiers(), 0u);
104 QCOMPARE(event.modifiers(), Qt::ShiftModifier | Qt::ControlModifier);
105 QCOMPARE(event.timestamp(), 400ms);
106 // and our custom argument
107 QCOMPARE(event.device(), &d);
108}
109
110void InputEventsTest::testInitWheelEvent_data()
111{
112 QTest::addColumn<Qt::Orientation>("orientation");
113 QTest::addColumn<qreal>("delta");
114 QTest::addColumn<qint32>("deltaV120");
115 QTest::addColumn<QPoint>("expectedAngleDelta");
116
117 QTest::newRow("horiz") << Qt::Horizontal << 3.3 << 1 << QPoint(3, 0);
118 QTest::newRow("vert") << Qt::Vertical << 2.4 << 2 << QPoint(0, 2);
119}
120
121void InputEventsTest::testInitWheelEvent()
122{
123 // this test verifies that a WheelEvent is constructed correctly
124
125 // first create the test LibInput::Device
126 libinput_device device;
127 Device d(&device);
128
129 // setup event
130 QFETCH(Qt::Orientation, orientation);
131 QFETCH(qreal, delta);
132 QFETCH(qint32, deltaV120);
133 WheelEvent event(QPointF(100, 200), delta, deltaV120, orientation, Qt::LeftButton | Qt::RightButton,
134 Qt::ShiftModifier | Qt::ControlModifier, InputRedirection::PointerAxisSourceWheel, 300ms, &d);
135 // compare QWheelEvent contract
136 QCOMPARE(event.type(), QEvent::Wheel);
137 QCOMPARE(event.position(), QPointF(100, 200));
138 QCOMPARE(event.globalPosition(), QPointF(100, 200));
139 QCOMPARE(event.buttons(), Qt::LeftButton | Qt::RightButton);
140 QCOMPARE(event.modifiers(), Qt::ShiftModifier | Qt::ControlModifier);
141 QCOMPARE(event.timestamp(), 300ms);
142 QTEST(event.angleDelta(), "expectedAngleDelta");
143 QTEST(event.orientation(), "orientation");
144 QTEST(event.delta(), "delta");
145 QTEST(event.deltaV120(), "deltaV120");
146 QCOMPARE(event.axisSource(), InputRedirection::PointerAxisSourceWheel);
147 // and our custom argument
148 QCOMPARE(event.device(), &d);
149}
150
151void InputEventsTest::testInitSwitchEvent_data()
152{
153 QTest::addColumn<KWin::SwitchEvent::State>("state");
154 QTest::addColumn<quint64>("timestamp");
155
156 QTest::newRow("on") << SwitchEvent::State::On << quint64{23456790};
157 QTest::newRow("off") << SwitchEvent::State::Off << quint64{45689235987};
158}
159
160void InputEventsTest::testInitSwitchEvent()
161{
162 // this test verifies that a SwitchEvent is constructed correctly
163 libinput_device device;
164 Device d(&device);
165
166 QFETCH(SwitchEvent::State, state);
167 QFETCH(quint64, timestamp);
168 SwitchEvent event(state, std::chrono::microseconds(timestamp), &d);
169
170 QCOMPARE(event.state(), state);
171 QCOMPARE(event.timestamp(), std::chrono::microseconds(timestamp));
172 QCOMPARE(event.device(), &d);
173}
174
175QTEST_GUILESS_MAIN(InputEventsTest)
176#include "input_event_test.moc"
Q_DECLARE_METATYPE(KWin::SwitchEvent::State)
Session::Type type
Definition session.cpp:17