KWin
Loading...
Searching...
No Matches
key_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_key_state)
19
20using namespace KWin::LibInput;
21
22class TestLibinputKeyEvent : public QObject
23{
24 Q_OBJECT
25private Q_SLOTS:
26 void init();
27 void cleanup();
28
29 void testCreate();
30 void testEvent_data();
31 void testEvent();
32
33private:
34 libinput_device *m_nativeDevice = nullptr;
35 Device *m_device = nullptr;
36};
37
38void TestLibinputKeyEvent::init()
39{
40 m_nativeDevice = new libinput_device;
41 m_nativeDevice->keyboard = true;
42 m_device = new Device(m_nativeDevice);
43}
44
45void TestLibinputKeyEvent::cleanup()
46{
47 delete m_device;
48 m_device = nullptr;
49
50 delete m_nativeDevice;
51 m_nativeDevice = nullptr;
52}
53
54void TestLibinputKeyEvent::testCreate()
55{
56 // this test verifies the initialisation of a KeyEvent and the parent Event class
58 keyEvent->device = m_nativeDevice;
59
60 std::unique_ptr<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.get()), keyEvent);
66 // verify it's a key event
67 QVERIFY(dynamic_cast<KeyEvent *>(event.get()));
68 QCOMPARE((libinput_event_keyboard *)(*dynamic_cast<KeyEvent *>(event.get())), keyEvent);
69
70 // verify that a nullptr passed to Event::create returns a nullptr
71 QVERIFY(!Event::create(nullptr));
72}
73
74void 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
85void TestLibinputKeyEvent::testEvent()
86{
87 // this test verifies the key press/release
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 = std::chrono::milliseconds(time);
96
97 std::unique_ptr<Event> event(Event::create(keyEvent));
98 auto ke = dynamic_cast<KeyEvent *>(event.get());
99 QVERIFY(ke);
100 QTEST(ke->state(), "expectedKeyState");
101 QCOMPARE(ke->key(), key);
102 QCOMPARE(ke->time(), keyEvent->time);
103}
104
105QTEST_GUILESS_MAIN(TestLibinputKeyEvent)
106#include "key_event_test.moc"
static std::unique_ptr< Event > create(libinput_event *event)
Definition events.cpp:19
Q_DECLARE_METATYPE(KWin::SwitchEvent::State)
libinput_key_state state
libinput_device * device
std::chrono::microseconds time