2020-08-02 22:22:19 +00:00
|
|
|
/*
|
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
2016-05-30 15:46:53 +00:00
|
|
|
|
2020-08-02 22:22:19 +00:00
|
|
|
SPDX-FileCopyrightText: 2016 Martin Gräßlin <mgraesslin@kde.org>
|
2016-05-30 15:46:53 +00:00
|
|
|
|
2020-08-02 22:22:19 +00:00
|
|
|
SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
2016-05-30 15:46:53 +00:00
|
|
|
#include "mock_libinput.h"
|
2021-02-09 18:18:36 +00:00
|
|
|
|
2021-10-27 09:13:09 +00:00
|
|
|
#include "backends/libinput/device.h"
|
|
|
|
#include "backends/libinput/events.h"
|
2016-05-30 15:46:53 +00:00
|
|
|
|
2018-06-05 10:52:57 +00:00
|
|
|
#include <QtTest>
|
2016-05-30 15:46:53 +00:00
|
|
|
|
|
|
|
#include <linux/input.h>
|
|
|
|
|
|
|
|
Q_DECLARE_METATYPE(libinput_key_state)
|
|
|
|
|
|
|
|
using namespace KWin::LibInput;
|
|
|
|
|
|
|
|
class TestLibinputKeyEvent : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
private Q_SLOTS:
|
|
|
|
void init();
|
|
|
|
void cleanup();
|
|
|
|
|
|
|
|
void testCreate();
|
|
|
|
void testEvent_data();
|
|
|
|
void testEvent();
|
|
|
|
|
|
|
|
private:
|
|
|
|
libinput_device *m_nativeDevice = nullptr;
|
|
|
|
Device *m_device = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
void TestLibinputKeyEvent::init()
|
|
|
|
{
|
|
|
|
m_nativeDevice = new libinput_device;
|
|
|
|
m_nativeDevice->keyboard = true;
|
|
|
|
m_device = new Device(m_nativeDevice);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestLibinputKeyEvent::cleanup()
|
|
|
|
{
|
|
|
|
delete m_device;
|
|
|
|
m_device = nullptr;
|
|
|
|
|
|
|
|
delete m_nativeDevice;
|
|
|
|
m_nativeDevice = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestLibinputKeyEvent::testCreate()
|
|
|
|
{
|
|
|
|
// this test verifies the initialisation of a KeyEvent and the parent Event class
|
|
|
|
libinput_event_keyboard *keyEvent = new libinput_event_keyboard;
|
|
|
|
keyEvent->device = m_nativeDevice;
|
|
|
|
|
2022-06-01 10:13:34 +00:00
|
|
|
std::unique_ptr<Event> event{Event::create(keyEvent)};
|
2016-05-30 15:46:53 +00:00
|
|
|
// API of event
|
|
|
|
QCOMPARE(event->type(), LIBINPUT_EVENT_KEYBOARD_KEY);
|
|
|
|
QCOMPARE(event->device(), m_device);
|
|
|
|
QCOMPARE(event->nativeDevice(), m_nativeDevice);
|
2022-06-01 10:13:34 +00:00
|
|
|
QCOMPARE((libinput_event *)(*event.get()), keyEvent);
|
2016-05-30 15:46:53 +00:00
|
|
|
// verify it's a key event
|
2022-06-01 10:13:34 +00:00
|
|
|
QVERIFY(dynamic_cast<KeyEvent *>(event.get()));
|
|
|
|
QCOMPARE((libinput_event_keyboard *)(*dynamic_cast<KeyEvent *>(event.get())), keyEvent);
|
2016-05-31 08:15:44 +00:00
|
|
|
|
|
|
|
// verify that a nullptr passed to Event::create returns a nullptr
|
|
|
|
QVERIFY(!Event::create(nullptr));
|
2016-05-30 15:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestLibinputKeyEvent::testEvent_data()
|
|
|
|
{
|
|
|
|
QTest::addColumn<libinput_key_state>("keyState");
|
|
|
|
QTest::addColumn<KWin::InputRedirection::KeyboardKeyState>("expectedKeyState");
|
|
|
|
QTest::addColumn<quint32>("key");
|
|
|
|
QTest::addColumn<quint32>("time");
|
|
|
|
|
|
|
|
QTest::newRow("pressed") << LIBINPUT_KEY_STATE_PRESSED << KWin::InputRedirection::KeyboardKeyPressed << quint32(KEY_A) << 100u;
|
|
|
|
QTest::newRow("released") << LIBINPUT_KEY_STATE_RELEASED << KWin::InputRedirection::KeyboardKeyReleased << quint32(KEY_B) << 200u;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestLibinputKeyEvent::testEvent()
|
|
|
|
{
|
|
|
|
// this test verifies the key press/release
|
|
|
|
libinput_event_keyboard *keyEvent = new libinput_event_keyboard;
|
|
|
|
keyEvent->device = m_nativeDevice;
|
|
|
|
QFETCH(libinput_key_state, keyState);
|
|
|
|
keyEvent->state = keyState;
|
|
|
|
QFETCH(quint32, key);
|
|
|
|
keyEvent->key = key;
|
|
|
|
QFETCH(quint32, time);
|
2022-12-21 00:55:20 +00:00
|
|
|
keyEvent->time = std::chrono::milliseconds(time);
|
2016-05-30 15:46:53 +00:00
|
|
|
|
2022-06-01 10:13:34 +00:00
|
|
|
std::unique_ptr<Event> event(Event::create(keyEvent));
|
|
|
|
auto ke = dynamic_cast<KeyEvent *>(event.get());
|
2016-05-30 15:46:53 +00:00
|
|
|
QVERIFY(ke);
|
|
|
|
QTEST(ke->state(), "expectedKeyState");
|
|
|
|
QCOMPARE(ke->key(), key);
|
2022-12-21 00:55:20 +00:00
|
|
|
QCOMPARE(ke->time(), keyEvent->time);
|
2016-05-30 15:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QTEST_GUILESS_MAIN(TestLibinputKeyEvent)
|
|
|
|
#include "key_event_test.moc"
|