From 1e564989c9e5bd0d4eea9c93db0a47c886e35fb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Gr=C3=A4=C3=9Flin?= Date: Tue, 31 May 2016 10:14:28 +0200 Subject: [PATCH] [autotest/libinput] Add a mocked test for Libinput::TouchEvent --- autotests/libinput/CMakeLists.txt | 14 +++ autotests/libinput/mock_libinput.cpp | 34 ++++--- autotests/libinput/mock_libinput.h | 5 + autotests/libinput/touch_event_test.cpp | 130 ++++++++++++++++++++++++ 4 files changed, 168 insertions(+), 15 deletions(-) create mode 100644 autotests/libinput/touch_event_test.cpp diff --git a/autotests/libinput/CMakeLists.txt b/autotests/libinput/CMakeLists.txt index 16cf3ba524..973d288d64 100644 --- a/autotests/libinput/CMakeLists.txt +++ b/autotests/libinput/CMakeLists.txt @@ -36,6 +36,20 @@ target_link_libraries( testLibinputPointerEvent Qt5::Test Qt5::Widgets KF5::Conf add_test(kwin-testLibinputPointerEvent testLibinputPointerEvent) ecm_mark_as_test(testLibinputPointerEvent) +######################################################## +# Test Touch Event +######################################################## +set( testLibinputTouchEvent_SRCS + touch_event_test.cpp + mock_libinput.cpp + ../../libinput/device.cpp + ../../libinput/events.cpp + ) +add_executable(testLibinputTouchEvent ${testLibinputTouchEvent_SRCS}) +target_link_libraries( testLibinputTouchEvent Qt5::Test Qt5::Widgets KF5::ConfigCore) +add_test(kwin-testLibinputTouchEvent testLibinputTouchEvent) +ecm_mark_as_test(testLibinputTouchEvent) + ######################################################## # Test Input Events ######################################################## diff --git a/autotests/libinput/mock_libinput.cpp b/autotests/libinput/mock_libinput.cpp index aebddd2906..bed39924a0 100644 --- a/autotests/libinput/mock_libinput.cpp +++ b/autotests/libinput/mock_libinput.cpp @@ -251,7 +251,13 @@ struct libinput_event_pointer *libinput_event_get_pointer_event(struct libinput_ struct libinput_event_touch *libinput_event_get_touch_event(struct libinput_event *event) { - Q_UNUSED(event) + if (event->type == LIBINPUT_EVENT_TOUCH_DOWN || + event->type == LIBINPUT_EVENT_TOUCH_UP || + event->type == LIBINPUT_EVENT_TOUCH_MOTION || + event->type == LIBINPUT_EVENT_TOUCH_CANCEL || + event->type == LIBINPUT_EVENT_TOUCH_FRAME) { + return reinterpret_cast(event); + } return nullptr; } @@ -341,38 +347,36 @@ double libinput_event_pointer_get_axis_value(struct libinput_event_pointer *even uint32_t libinput_event_touch_get_time(struct libinput_event_touch *event) { - Q_UNUSED(time) - return 0; + return event->time; } double libinput_event_touch_get_x(struct libinput_event_touch *event) { - Q_UNUSED(event) - return 0.0; + return event->absolutePos.x(); } double libinput_event_touch_get_y(struct libinput_event_touch *event) { - Q_UNUSED(event) - return 0.0; + return event->absolutePos.y(); } 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 deviceWidth = 0.0; + double deviceHeight = 0.0; + libinput_device_get_size(event->device, &deviceWidth, &deviceHeight); + return event->absolutePos.x() / deviceWidth * width; } double libinput_event_touch_get_y_transformed(struct libinput_event_touch *event, uint32_t height) { - Q_UNUSED(event) - Q_UNUSED(height) - return 0.0; + double deviceWidth = 0.0; + double deviceHeight = 0.0; + libinput_device_get_size(event->device, &deviceWidth, &deviceHeight); + return event->absolutePos.y() / deviceHeight * height; } int32_t libinput_event_touch_get_slot(struct libinput_event_touch *event) { - Q_UNUSED(event) - return 0; + return event->slot; } diff --git a/autotests/libinput/mock_libinput.h b/autotests/libinput/mock_libinput.h index 371d0cf528..b614f63beb 100644 --- a/autotests/libinput/mock_libinput.h +++ b/autotests/libinput/mock_libinput.h @@ -81,4 +81,9 @@ struct libinput_event_pointer : libinput_event { QPointF absolutePos; }; +struct libinput_event_touch : libinput_event { + qint32 slot = -1; + QPointF absolutePos; +}; + #endif diff --git a/autotests/libinput/touch_event_test.cpp b/autotests/libinput/touch_event_test.cpp new file mode 100644 index 0000000000..52eefa37b3 --- /dev/null +++ b/autotests/libinput/touch_event_test.cpp @@ -0,0 +1,130 @@ +/******************************************************************** +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_event_type) + +using namespace KWin::LibInput; + +class TestLibinputTouchEvent : public QObject +{ + Q_OBJECT +private Q_SLOTS: + void init(); + void cleanup(); + + void testType_data(); + void testType(); + void testAbsoluteMotion_data(); + void testAbsoluteMotion(); + +private: + libinput_device *m_nativeDevice = nullptr; + Device *m_device = nullptr; +}; + +void TestLibinputTouchEvent::init() +{ + m_nativeDevice = new libinput_device; + m_nativeDevice->touch = true; + m_nativeDevice->deviceSize = QSizeF(12.5, 13.8); + m_device = new Device(m_nativeDevice); +} + +void TestLibinputTouchEvent::cleanup() +{ + delete m_device; + m_device = nullptr; + + delete m_nativeDevice; + m_nativeDevice = nullptr; +} + +void TestLibinputTouchEvent::testType_data() +{ + QTest::addColumn("type"); + QTest::addColumn("hasId"); + + QTest::newRow("down") << LIBINPUT_EVENT_TOUCH_DOWN << true; + QTest::newRow("up") << LIBINPUT_EVENT_TOUCH_UP << true; + QTest::newRow("motion") << LIBINPUT_EVENT_TOUCH_MOTION << true; + QTest::newRow("cancel") << LIBINPUT_EVENT_TOUCH_CANCEL << false; + QTest::newRow("frame") << LIBINPUT_EVENT_TOUCH_FRAME << false; +} + +void TestLibinputTouchEvent::testType() +{ + // this test verifies the initialization of a PointerEvent and the parent Event class + libinput_event_touch *touchEvent = new libinput_event_touch; + QFETCH(libinput_event_type, type); + touchEvent->type = type; + touchEvent->device = m_nativeDevice; + touchEvent->slot = 0; + + QScopedPointer event(Event::create(touchEvent)); + // API of event + QCOMPARE(event->type(), type); + QCOMPARE(event->device(), m_device); + QCOMPARE(event->nativeDevice(), m_nativeDevice); + QCOMPARE((libinput_event*)(*event.data()), touchEvent); + // verify it's a pointer event + QVERIFY(dynamic_cast(event.data())); + QCOMPARE((libinput_event_touch*)(*dynamic_cast(event.data())), touchEvent); + QFETCH(bool, hasId); + if (hasId) { + QCOMPARE(dynamic_cast(event.data())->id(), 0); + } +} + +void TestLibinputTouchEvent::testAbsoluteMotion_data() +{ + QTest::addColumn("type"); + QTest::newRow("down") << LIBINPUT_EVENT_TOUCH_DOWN; + QTest::newRow("motion") << LIBINPUT_EVENT_TOUCH_MOTION; +} + +void TestLibinputTouchEvent::testAbsoluteMotion() +{ + // this test verifies absolute touch points (either down or motion) + libinput_event_touch *touchEvent = new libinput_event_touch; + touchEvent->device = m_nativeDevice; + QFETCH(libinput_event_type, type); + touchEvent->type = type; + touchEvent->absolutePos = QPointF(6.25, 6.9); + touchEvent->time = 500u; + touchEvent->slot = 1; + + QScopedPointer event(Event::create(touchEvent)); + auto te = dynamic_cast(event.data()); + QVERIFY(te); + QCOMPARE(te->type(), type); + QCOMPARE(te->time(), 500u); + QCOMPARE(te->absolutePos(), QPointF(6.25, 6.9)); + QCOMPARE(te->absolutePos(QSize(1280, 1024)), QPointF(640, 512)); +} + +QTEST_GUILESS_MAIN(TestLibinputTouchEvent) +#include "touch_event_test.moc"