From 2d9ff54e688db04c0369db136eb0d1ad2b8aeb14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Gr=C3=A4=C3=9Flin?= Date: Thu, 2 Jun 2016 07:46:09 +0200 Subject: [PATCH] [autotest/libinput] Add a basic test for Context This test mocks part of KWin::Udev, udev and libinput. The test itself is still rather limited and only verifies whether libinput is valid or not and that assignSeat works. Most of the interaction is not yet tested, though to a large degree doesn't make sense and should be rather tested in the context of LibInput::Connection. --- autotests/libinput/CMakeLists.txt | 25 ++++++++ autotests/libinput/context_test.cpp | 90 ++++++++++++++++++++++++++++ autotests/libinput/mock_libinput.cpp | 69 +++++++++++++++++++++ autotests/libinput/mock_libinput.h | 6 ++ autotests/libinput/mock_udev.cpp | 37 ++++++++++++ autotests/libinput/mock_udev.h | 28 +++++++++ 6 files changed, 255 insertions(+) create mode 100644 autotests/libinput/context_test.cpp create mode 100644 autotests/libinput/mock_udev.cpp create mode 100644 autotests/libinput/mock_udev.h diff --git a/autotests/libinput/CMakeLists.txt b/autotests/libinput/CMakeLists.txt index 973d288d64..bd92fdf0a7 100644 --- a/autotests/libinput/CMakeLists.txt +++ b/autotests/libinput/CMakeLists.txt @@ -1,4 +1,5 @@ include_directories(${Libinput_INCLUDE_DIRS}) +include_directories(${UDEV_INCLUDE_DIR}) ######################################################## # Test Devices ######################################################## @@ -50,6 +51,30 @@ target_link_libraries( testLibinputTouchEvent Qt5::Test Qt5::Widgets KF5::Config add_test(kwin-testLibinputTouchEvent testLibinputTouchEvent) ecm_mark_as_test(testLibinputTouchEvent) +######################################################## +# Test Context +######################################################## +set( testLibinputContext_SRCS + context_test.cpp + mock_libinput.cpp + mock_udev.cpp + ../../libinput/context.cpp + ../../libinput/device.cpp + ../../libinput/events.cpp + ../../libinput/libinput_logging.cpp + ../../logind.cpp + ) +add_executable(testLibinputContext ${testLibinputContext_SRCS}) +target_link_libraries( testLibinputContext + Qt5::DBus + Qt5::Test + Qt5::Widgets + KF5::ConfigCore + KF5::WindowSystem + ) +add_test(kwin-testLibinputContext testLibinputContext) +ecm_mark_as_test(testLibinputContext) + ######################################################## # Test Input Events ######################################################## diff --git a/autotests/libinput/context_test.cpp b/autotests/libinput/context_test.cpp new file mode 100644 index 0000000000..180bf63012 --- /dev/null +++ b/autotests/libinput/context_test.cpp @@ -0,0 +1,90 @@ +/******************************************************************** +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 "mock_udev.h" +#include "../../libinput/context.h" +#include "../../udev.h" +#include +Q_LOGGING_CATEGORY(KWIN_CORE, "kwin_core", QtCriticalMsg) + +using namespace KWin; +using namespace KWin::LibInput; + +class TestContext : public QObject +{ + Q_OBJECT +private Q_SLOTS: + void cleanup(); + void testCreateFailUdev(); + void testAssignSeat_data(); + void testAssignSeat(); +}; + +void TestContext::cleanup() +{ + delete udev::s_mockUdev; + udev::s_mockUdev = nullptr; +} + +void TestContext::testCreateFailUdev() +{ + // this test verifies that isValid is false if the setup fails + // we create an Udev without a mockUdev + Udev u; + QVERIFY(!(udev*)(u)); + Context context(u); + QVERIFY(!context.isValid()); + // should not have a valid libinput + libinput *libinput = context; + QVERIFY(!libinput); +} + +void TestContext::testAssignSeat_data() +{ + QTest::addColumn("assignShouldFail"); + QTest::addColumn("expectedValue"); + + QTest::newRow("succeeds") << false << true; + QTest::newRow("fails") << true << false; +} + +void TestContext::testAssignSeat() +{ + // this test verifies the behavior of assignSeat + // setup udev so that we can create a context + udev::s_mockUdev = new udev; + QVERIFY(udev::s_mockUdev); + Udev u; + QVERIFY((udev*)(u)); + Context context(u); + QVERIFY(context.isValid()); + // this should give as a libinput + libinput *libinput = context; + QVERIFY(libinput); + // and now we can assign it + QFETCH(bool, assignShouldFail); + libinput->assignSeatRetVal = assignShouldFail; + QTEST(context.assignSeat("testSeat"), "expectedValue"); + // of course it's not suspended + QVERIFY(!context.isSuspended()); +} + +QTEST_GUILESS_MAIN(TestContext) +#include "context_test.moc" diff --git a/autotests/libinput/mock_libinput.cpp b/autotests/libinput/mock_libinput.cpp index bed39924a0..a042a29a22 100644 --- a/autotests/libinput/mock_libinput.cpp +++ b/autotests/libinput/mock_libinput.cpp @@ -380,3 +380,72 @@ int32_t libinput_event_touch_get_slot(struct libinput_event_touch *event) { return event->slot; } + +struct libinput *libinput_udev_create_context(const struct libinput_interface *interface, void *user_data, struct udev *udev) +{ + if (!udev) { + return nullptr; + } + Q_UNUSED(interface) + Q_UNUSED(user_data) + return new libinput; +} + +void libinput_log_set_priority(struct libinput *libinput, enum libinput_log_priority priority) +{ + Q_UNUSED(libinput) + Q_UNUSED(priority) +} + +void libinput_log_set_handler(struct libinput *libinput, libinput_log_handler log_handler) +{ + Q_UNUSED(libinput) + Q_UNUSED(log_handler) +} + +struct libinput *libinput_unref(struct libinput *libinput) +{ + libinput->refCount--; + if (libinput->refCount == 0) { + delete libinput; + return nullptr; + } + return libinput; +} + +int libinput_udev_assign_seat(struct libinput *libinput, const char *seat_id) +{ + if (libinput->assignSeatRetVal == 0) { + libinput->seat = QByteArray(seat_id); + } + return libinput->assignSeatRetVal; +} + +int libinput_get_fd(struct libinput *libinput) +{ + Q_UNUSED(libinput) + return -1; +} + +int libinput_dispatch(struct libinput *libinput) +{ + Q_UNUSED(libinput) + return 0; +} + +struct libinput_event *libinput_get_event(struct libinput *libinput) +{ + Q_UNUSED(libinput) + return nullptr; +} + +void libinput_suspend(struct libinput *libinput) +{ + Q_UNUSED(libinput) +} + +int libinput_resume(struct libinput *libinput) +{ + Q_UNUSED(libinput) + return 0; +} diff --git a/autotests/libinput/mock_libinput.h b/autotests/libinput/mock_libinput.h index b614f63beb..a6cfedc43d 100644 --- a/autotests/libinput/mock_libinput.h +++ b/autotests/libinput/mock_libinput.h @@ -86,4 +86,10 @@ struct libinput_event_touch : libinput_event { QPointF absolutePos; }; +struct libinput { + int refCount = 1; + QByteArray seat; + int assignSeatRetVal = 0; +}; + #endif diff --git a/autotests/libinput/mock_udev.cpp b/autotests/libinput/mock_udev.cpp new file mode 100644 index 0000000000..fc8e228d55 --- /dev/null +++ b/autotests/libinput/mock_udev.cpp @@ -0,0 +1,37 @@ +/******************************************************************** +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 "../../udev.h" +#include "mock_udev.h" + +udev *udev::s_mockUdev = nullptr; + +namespace KWin +{ + +Udev::Udev() + : m_udev(udev::s_mockUdev) +{ +} + +Udev::~Udev() +{ +} + +} diff --git a/autotests/libinput/mock_udev.h b/autotests/libinput/mock_udev.h new file mode 100644 index 0000000000..bfd6455a9b --- /dev/null +++ b/autotests/libinput/mock_udev.h @@ -0,0 +1,28 @@ +/******************************************************************** +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 . +*********************************************************************/ +#ifndef MOCK_UDEV_H +#define MOCK_UDEV_H + +struct udev { + static udev *s_mockUdev; +}; + +#endif +