diff --git a/autotests/libinput/CMakeLists.txt b/autotests/libinput/CMakeLists.txt index be552c0e19..1cddcfee6b 100644 --- a/autotests/libinput/CMakeLists.txt +++ b/autotests/libinput/CMakeLists.txt @@ -7,6 +7,20 @@ target_link_libraries( testLibinputDevice Qt5::Test) add_test(kwin-testLibinputDevice testLibinputDevice) ecm_mark_as_test(testLibinputDevice) +######################################################## +# Test Key Event +######################################################## +set( testLibinputKeyEvent_SRCS + key_event_test.cpp + mock_libinput.cpp + ../../libinput/device.cpp + ../../libinput/events.cpp + ) +add_executable(testLibinputKeyEvent ${testLibinputKeyEvent_SRCS}) +target_link_libraries( testLibinputKeyEvent Qt5::Test Qt5::Widgets KF5::ConfigCore) +add_test(kwin-testLibinputKeyEvent testLibinputKeyEvent) +ecm_mark_as_test(testLibinputKeyEvent) + ######################################################## # Test Input Events ######################################################## diff --git a/autotests/libinput/key_event_test.cpp b/autotests/libinput/key_event_test.cpp new file mode 100644 index 0000000000..a5577d0aff --- /dev/null +++ b/autotests/libinput/key_event_test.cpp @@ -0,0 +1,113 @@ +/******************************************************************** +KWin - the KDE window manager +This file is part of the KDE project. + +Copyright (C) 2016 Martin Gräßlin + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*********************************************************************/ +#include "mock_libinput.h" +#include "../../libinput/device.h" +#include "../../libinput/events.h" + +#include + +#include + +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; + + QScopedPointer event(Event::create(keyEvent)); + // API of event + QCOMPARE(event->type(), LIBINPUT_EVENT_KEYBOARD_KEY); + QCOMPARE(event->device(), m_device); + QCOMPARE(event->nativeDevice(), m_nativeDevice); + QCOMPARE((libinput_event*)(*event.data()), keyEvent); + // verify it's a key event + QVERIFY(dynamic_cast(event.data())); + QCOMPARE((libinput_event_keyboard*)(*dynamic_cast(event.data())), keyEvent); +} + +void TestLibinputKeyEvent::testEvent_data() +{ + QTest::addColumn("keyState"); + QTest::addColumn("expectedKeyState"); + QTest::addColumn("key"); + QTest::addColumn("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); + keyEvent->time = time; + + QScopedPointer event(Event::create(keyEvent)); + auto ke = dynamic_cast(event.data()); + QVERIFY(ke); + QTEST(ke->state(), "expectedKeyState"); + QCOMPARE(ke->key(), key); + QCOMPARE(ke->time(), time); +} + +QTEST_GUILESS_MAIN(TestLibinputKeyEvent) +#include "key_event_test.moc" diff --git a/autotests/libinput/mock_libinput.cpp b/autotests/libinput/mock_libinput.cpp index 7b77ab1b99..cfb0167216 100644 --- a/autotests/libinput/mock_libinput.cpp +++ b/autotests/libinput/mock_libinput.cpp @@ -213,3 +213,162 @@ enum libinput_config_status libinput_device_config_send_events_set_mode(struct l } return LIBINPUT_CONFIG_STATUS_INVALID; } + + +enum libinput_event_type libinput_event_get_type(struct libinput_event *event) +{ + return event->type; +} + +struct libinput_device *libinput_event_get_device(struct libinput_event *event) +{ + return event->device; +} + +void libinput_event_destroy(struct libinput_event *event) +{ + delete event; +} + +struct libinput_event_keyboard *libinput_event_get_keyboard_event(struct libinput_event *event) +{ + if (event->type == LIBINPUT_EVENT_KEYBOARD_KEY) { + return reinterpret_cast(event); + } + return nullptr; +} + +struct libinput_event_pointer *libinput_event_get_pointer_event(struct libinput_event *event) +{ + Q_UNUSED(event) + return nullptr; +} + +struct libinput_event_touch *libinput_event_get_touch_event(struct libinput_event *event) +{ + Q_UNUSED(event) + return nullptr; +} + +uint32_t libinput_event_keyboard_get_key(struct libinput_event_keyboard *event) +{ + return event->key; +} + +enum libinput_key_state libinput_event_keyboard_get_key_state(struct libinput_event_keyboard *event) +{ + return event->state; +} + +uint32_t libinput_event_keyboard_get_time(struct libinput_event_keyboard *event) +{ + return event->time; +} + +double libinput_event_pointer_get_absolute_x(struct libinput_event_pointer *event) +{ + Q_UNUSED(event) + return 0.0; +} + +double libinput_event_pointer_get_absolute_y(struct libinput_event_pointer *event) +{ + Q_UNUSED(event) + return 0.0; +} + +double libinput_event_pointer_get_absolute_x_transformed(struct libinput_event_pointer *event, uint32_t width) +{ + Q_UNUSED(event) + Q_UNUSED(width) + return 0.0; +} + +double libinput_event_pointer_get_absolute_y_transformed(struct libinput_event_pointer *event, uint32_t height) +{ + Q_UNUSED(event) + Q_UNUSED(height) + return 0.0; +} + +double libinput_event_pointer_get_dx(struct libinput_event_pointer *event) +{ + Q_UNUSED(event) + return 0.0; +} + +double libinput_event_pointer_get_dy(struct libinput_event_pointer *event) +{ + Q_UNUSED(event) + return 0.0; +} + +uint32_t libinput_event_pointer_get_time(struct libinput_event_pointer *event) +{ + Q_UNUSED(event) + return 0; +} + +uint32_t libinput_event_pointer_get_button(struct libinput_event_pointer *event) +{ + Q_UNUSED(event) + return 0; +} + +enum libinput_button_state libinput_event_pointer_get_button_state(struct libinput_event_pointer *event) +{ + Q_UNUSED(event) + return LIBINPUT_BUTTON_STATE_RELEASED; +} + +int libinput_event_pointer_has_axis(struct libinput_event_pointer *event, enum libinput_pointer_axis axis) +{ + Q_UNUSED(event) + Q_UNUSED(axis) + return 0; +} + +double libinput_event_pointer_get_axis_value(struct libinput_event_pointer *event, enum libinput_pointer_axis axis) +{ + Q_UNUSED(event) + Q_UNUSED(axis) + return 0.0; +} + +uint32_t libinput_event_touch_get_time(struct libinput_event_touch *event) +{ + Q_UNUSED(time) + return 0; +} + +double libinput_event_touch_get_x(struct libinput_event_touch *event) +{ + Q_UNUSED(event) + return 0.0; +} + +double libinput_event_touch_get_y(struct libinput_event_touch *event) +{ + Q_UNUSED(event) + return 0.0; +} + +double libinput_event_touch_get_x_transformed(struct libinput_event_touch *event, uint32_t width) +{ + Q_UNUSED(event) + Q_UNUSED(width) + return 0.0; +} + +double libinput_event_touch_get_y_transformed(struct libinput_event_touch *event, uint32_t height) +{ + Q_UNUSED(event) + Q_UNUSED(height) + return 0.0; +} + +int32_t libinput_event_touch_get_slot(struct libinput_event_touch *event) +{ + Q_UNUSED(event) + return 0; +} diff --git a/autotests/libinput/mock_libinput.h b/autotests/libinput/mock_libinput.h index a635532c59..628f460f1c 100644 --- a/autotests/libinput/mock_libinput.h +++ b/autotests/libinput/mock_libinput.h @@ -19,6 +19,7 @@ along with this program. If not, see . *********************************************************************/ #ifndef MOCK_LIBINPUT_H #define MOCK_LIBINPUT_H +#include #include #include @@ -54,4 +55,18 @@ struct libinput_device { int setEnableModeReturnValue = 0; }; +struct libinput_event { + libinput_device *device = nullptr; + libinput_event_type type = LIBINPUT_EVENT_NONE; + quint32 time = 0; +}; + +struct libinput_event_keyboard : libinput_event { + libinput_event_keyboard() { + type = LIBINPUT_EVENT_KEYBOARD_KEY; + } + libinput_key_state state = LIBINPUT_KEY_STATE_RELEASED; + quint32 key = 0; +}; + #endif