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
11 #include "libinput/device.h"
12 #include "libinput/events.h"
13
14 #include <QtTest>
15
16 #include <linux/input.h>
17
18 Q_DECLARE_METATYPE(libinput_key_state)
19
20 using namespace KWin::LibInput;
21
22 class TestLibinputKeyEvent : public QObject
23 {
24 Q_OBJECT
25 private Q_SLOTS:
26 void init();
27 void cleanup();
28
29 void testCreate();
30 void testEvent_data();
31 void testEvent();
32
33 private:
34 libinput_device *m_nativeDevice = nullptr;
35 Device *m_device = nullptr;
36 };
37
init()38 void TestLibinputKeyEvent::init()
39 {
40 m_nativeDevice = new libinput_device;
41 m_nativeDevice->keyboard = true;
42 m_device = new Device(m_nativeDevice);
43 }
44
cleanup()45 void TestLibinputKeyEvent::cleanup()
46 {
47 delete m_device;
48 m_device = nullptr;
49
50 delete m_nativeDevice;
51 m_nativeDevice = nullptr;
52 }
53
testCreate()54 void TestLibinputKeyEvent::testCreate()
55 {
56 // this test verifies the initialisation of a KeyEvent and the parent Event class
57 libinput_event_keyboard *keyEvent = new libinput_event_keyboard;
58 keyEvent->device = m_nativeDevice;
59
60 QScopedPointer<Event> event(Event::create(keyEvent));
61 // API of event
62 QCOMPARE(event->type(), LIBINPUT_EVENT_KEYBOARD_KEY);
63 QCOMPARE(event->device(), m_device);
64 QCOMPARE(event->nativeDevice(), m_nativeDevice);
65 QCOMPARE((libinput_event*)(*event.data()), keyEvent);
66 // verify it's a key event
67 QVERIFY(dynamic_cast<KeyEvent*>(event.data()));
68 QCOMPARE((libinput_event_keyboard*)(*dynamic_cast<KeyEvent*>(event.data())), keyEvent);
69
70 // verify that a nullptr passed to Event::create returns a nullptr
71 QVERIFY(!Event::create(nullptr));
72 }
73
testEvent_data()74 void TestLibinputKeyEvent::testEvent_data()
75 {
76 QTest::addColumn<libinput_key_state>("keyState");
77 QTest::addColumn<KWin::InputRedirection::KeyboardKeyState>("expectedKeyState");
78 QTest::addColumn<quint32>("key");
79 QTest::addColumn<quint32>("time");
80
81 QTest::newRow("pressed") << LIBINPUT_KEY_STATE_PRESSED << KWin::InputRedirection::KeyboardKeyPressed << quint32(KEY_A) << 100u;
82 QTest::newRow("released") << LIBINPUT_KEY_STATE_RELEASED << KWin::InputRedirection::KeyboardKeyReleased << quint32(KEY_B) << 200u;
83 }
84
testEvent()85 void TestLibinputKeyEvent::testEvent()
86 {
87 // this test verifies the key press/release
88 libinput_event_keyboard *keyEvent = new libinput_event_keyboard;
89 keyEvent->device = m_nativeDevice;
90 QFETCH(libinput_key_state, keyState);
91 keyEvent->state = keyState;
92 QFETCH(quint32, key);
93 keyEvent->key = key;
94 QFETCH(quint32, time);
95 keyEvent->time = time;
96
97 QScopedPointer<Event> event(Event::create(keyEvent));
98 auto ke = dynamic_cast<KeyEvent*>(event.data());
99 QVERIFY(ke);
100 QTEST(ke->state(), "expectedKeyState");
101 QCOMPARE(ke->key(), key);
102 QCOMPARE(ke->time(), time);
103 }
104
105 QTEST_GUILESS_MAIN(TestLibinputKeyEvent)
106 #include "key_event_test.moc"
107