File indexing completed on 2024-11-10 04:56:14

0001 /*
0002     KWin - the KDE window manager
0003     This file is part of the KDE project.
0004 
0005     SPDX-FileCopyrightText: 2016 Martin Gräßlin <mgraesslin@kde.org>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 #include "mock_libinput.h"
0010 
0011 #include "backends/libinput/device.h"
0012 #include "backends/libinput/events.h"
0013 
0014 #include <QTest>
0015 
0016 #include <linux/input.h>
0017 
0018 Q_DECLARE_METATYPE(libinput_key_state)
0019 
0020 using namespace KWin::LibInput;
0021 
0022 class TestLibinputKeyEvent : public QObject
0023 {
0024     Q_OBJECT
0025 private Q_SLOTS:
0026     void init();
0027     void cleanup();
0028 
0029     void testCreate();
0030     void testEvent_data();
0031     void testEvent();
0032 
0033 private:
0034     libinput_device *m_nativeDevice = nullptr;
0035     Device *m_device = nullptr;
0036 };
0037 
0038 void TestLibinputKeyEvent::init()
0039 {
0040     m_nativeDevice = new libinput_device;
0041     m_nativeDevice->keyboard = true;
0042     m_device = new Device(m_nativeDevice);
0043 }
0044 
0045 void TestLibinputKeyEvent::cleanup()
0046 {
0047     delete m_device;
0048     m_device = nullptr;
0049 
0050     delete m_nativeDevice;
0051     m_nativeDevice = nullptr;
0052 }
0053 
0054 void TestLibinputKeyEvent::testCreate()
0055 {
0056     // this test verifies the initialisation of a KeyEvent and the parent Event class
0057     libinput_event_keyboard *keyEvent = new libinput_event_keyboard;
0058     keyEvent->device = m_nativeDevice;
0059 
0060     std::unique_ptr<Event> event{Event::create(keyEvent)};
0061     // API of event
0062     QCOMPARE(event->type(), LIBINPUT_EVENT_KEYBOARD_KEY);
0063     QCOMPARE(event->device(), m_device);
0064     QCOMPARE(event->nativeDevice(), m_nativeDevice);
0065     QCOMPARE((libinput_event *)(*event.get()), keyEvent);
0066     // verify it's a key event
0067     QVERIFY(dynamic_cast<KeyEvent *>(event.get()));
0068     QCOMPARE((libinput_event_keyboard *)(*dynamic_cast<KeyEvent *>(event.get())), keyEvent);
0069 
0070     // verify that a nullptr passed to Event::create returns a nullptr
0071     QVERIFY(!Event::create(nullptr));
0072 }
0073 
0074 void TestLibinputKeyEvent::testEvent_data()
0075 {
0076     QTest::addColumn<libinput_key_state>("keyState");
0077     QTest::addColumn<KWin::InputRedirection::KeyboardKeyState>("expectedKeyState");
0078     QTest::addColumn<quint32>("key");
0079     QTest::addColumn<quint32>("time");
0080 
0081     QTest::newRow("pressed") << LIBINPUT_KEY_STATE_PRESSED << KWin::InputRedirection::KeyboardKeyPressed << quint32(KEY_A) << 100u;
0082     QTest::newRow("released") << LIBINPUT_KEY_STATE_RELEASED << KWin::InputRedirection::KeyboardKeyReleased << quint32(KEY_B) << 200u;
0083 }
0084 
0085 void TestLibinputKeyEvent::testEvent()
0086 {
0087     // this test verifies the key press/release
0088     libinput_event_keyboard *keyEvent = new libinput_event_keyboard;
0089     keyEvent->device = m_nativeDevice;
0090     QFETCH(libinput_key_state, keyState);
0091     keyEvent->state = keyState;
0092     QFETCH(quint32, key);
0093     keyEvent->key = key;
0094     QFETCH(quint32, time);
0095     keyEvent->time = std::chrono::milliseconds(time);
0096 
0097     std::unique_ptr<Event> event(Event::create(keyEvent));
0098     auto ke = dynamic_cast<KeyEvent *>(event.get());
0099     QVERIFY(ke);
0100     QTEST(ke->state(), "expectedKeyState");
0101     QCOMPARE(ke->key(), key);
0102     QCOMPARE(ke->time(), keyEvent->time);
0103 }
0104 
0105 QTEST_GUILESS_MAIN(TestLibinputKeyEvent)
0106 #include "key_event_test.moc"