File indexing completed on 2024-11-10 04:56:14

0001 /*
0002     KWin - the KDE window manager
0003     This file is part of the KDE project.
0004 
0005     SPDX-FileCopyrightText: 2017 Martin Flöser <mgraesslin@kde.org>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 #include "mock_libinput.h"
0010 
0011 #include "backends/libinput/device.h"
0012 #include "backends/libinput/events.h"
0013 
0014 #include <QTest>
0015 
0016 #include <memory>
0017 
0018 Q_DECLARE_METATYPE(KWin::LibInput::SwitchEvent::State)
0019 
0020 using namespace KWin::LibInput;
0021 using namespace std::literals;
0022 
0023 class TestLibinputSwitchEvent : public QObject
0024 {
0025     Q_OBJECT
0026 private Q_SLOTS:
0027     void init();
0028     void cleanup();
0029 
0030     void testToggled_data();
0031     void testToggled();
0032 
0033 private:
0034     std::unique_ptr<libinput_device> m_nativeDevice;
0035     std::unique_ptr<Device> m_device;
0036 };
0037 
0038 void TestLibinputSwitchEvent::init()
0039 {
0040     m_nativeDevice = std::make_unique<libinput_device>();
0041     m_nativeDevice->switchDevice = true;
0042     m_device = std::make_unique<Device>(m_nativeDevice.get());
0043 }
0044 
0045 void TestLibinputSwitchEvent::cleanup()
0046 {
0047     m_device.reset();
0048     m_nativeDevice.reset();
0049 }
0050 
0051 void TestLibinputSwitchEvent::testToggled_data()
0052 {
0053     QTest::addColumn<KWin::LibInput::SwitchEvent::State>("state");
0054 
0055     QTest::newRow("on") << KWin::LibInput::SwitchEvent::State::On;
0056     QTest::newRow("off") << KWin::LibInput::SwitchEvent::State::Off;
0057 }
0058 
0059 void TestLibinputSwitchEvent::testToggled()
0060 {
0061     libinput_event_switch *nativeEvent = new libinput_event_switch;
0062     nativeEvent->type = LIBINPUT_EVENT_SWITCH_TOGGLE;
0063     nativeEvent->device = m_nativeDevice.get();
0064     QFETCH(KWin::LibInput::SwitchEvent::State, state);
0065     switch (state) {
0066     case SwitchEvent::State::Off:
0067         nativeEvent->state = libinput_event_switch::State::Off;
0068         break;
0069     case SwitchEvent::State::On:
0070         nativeEvent->state = libinput_event_switch::State::On;
0071         break;
0072     default:
0073         Q_UNREACHABLE();
0074     }
0075     nativeEvent->time = 23456789us;
0076 
0077     std::unique_ptr<Event> event(Event::create(nativeEvent));
0078     auto se = dynamic_cast<SwitchEvent *>(event.get());
0079     QVERIFY(se);
0080     QCOMPARE(se->device(), m_device.get());
0081     QCOMPARE(se->nativeDevice(), m_nativeDevice.get());
0082     QCOMPARE(se->type(), LIBINPUT_EVENT_SWITCH_TOGGLE);
0083     QCOMPARE(se->state(), state);
0084     QCOMPARE(se->time(), 23456789us);
0085 }
0086 
0087 QTEST_GUILESS_MAIN(TestLibinputSwitchEvent)
0088 #include "switch_event_test.moc"