[autotest/libinput] Add a mocked test for Libinput::KeyEvent
This new test includes everything used in events.cpp to the mocked functionality of libinput. Only key event is implemented so far, the referenced pointer and touch functions are mocked with default values. The test verifies that a KeyEvent gets created and the key press/release works as expected.
This commit is contained in:
parent
2e4db5c2b8
commit
ac227e57c7
4 changed files with 301 additions and 0 deletions
|
@ -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
|
||||
########################################################
|
||||
|
|
113
autotests/libinput/key_event_test.cpp
Normal file
113
autotests/libinput/key_event_test.cpp
Normal file
|
@ -0,0 +1,113 @@
|
|||
/********************************************************************
|
||||
KWin - the KDE window manager
|
||||
This file is part of the KDE project.
|
||||
|
||||
Copyright (C) 2016 Martin Gräßlin <mgraesslin@kde.org>
|
||||
|
||||
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 <http://www.gnu.org/licenses/>.
|
||||
*********************************************************************/
|
||||
#include "mock_libinput.h"
|
||||
#include "../../libinput/device.h"
|
||||
#include "../../libinput/events.h"
|
||||
|
||||
#include <QtTest/QtTest>
|
||||
|
||||
#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;
|
||||
|
||||
QScopedPointer<Event> 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<KeyEvent*>(event.data()));
|
||||
QCOMPARE((libinput_event_keyboard*)(*dynamic_cast<KeyEvent*>(event.data())), keyEvent);
|
||||
}
|
||||
|
||||
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);
|
||||
keyEvent->time = time;
|
||||
|
||||
QScopedPointer<Event> event(Event::create(keyEvent));
|
||||
auto ke = dynamic_cast<KeyEvent*>(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"
|
|
@ -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<libinput_event_keyboard *>(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;
|
||||
}
|
||||
|
|
|
@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||
*********************************************************************/
|
||||
#ifndef MOCK_LIBINPUT_H
|
||||
#define MOCK_LIBINPUT_H
|
||||
#include <libinput.h>
|
||||
|
||||
#include <QByteArray>
|
||||
#include <QSizeF>
|
||||
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue