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: 2016 Martin Gräßlin <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 "input_event.h"
0013 
0014 #include <QTest>
0015 
0016 Q_DECLARE_METATYPE(KWin::SwitchEvent::State);
0017 
0018 using namespace KWin;
0019 using namespace KWin::LibInput;
0020 using namespace std::literals;
0021 
0022 class InputEventsTest : public QObject
0023 {
0024     Q_OBJECT
0025 private Q_SLOTS:
0026     void testInitMouseEvent_data();
0027     void testInitMouseEvent();
0028     void testInitKeyEvent_data();
0029     void testInitKeyEvent();
0030     void testInitWheelEvent_data();
0031     void testInitWheelEvent();
0032     void testInitSwitchEvent_data();
0033     void testInitSwitchEvent();
0034 };
0035 
0036 void InputEventsTest::testInitMouseEvent_data()
0037 {
0038     QTest::addColumn<QEvent::Type>("type");
0039 
0040     QTest::newRow("Press") << QEvent::MouseButtonPress;
0041     QTest::newRow("Release") << QEvent::MouseButtonRelease;
0042     QTest::newRow("Move") << QEvent::MouseMove;
0043 }
0044 
0045 void InputEventsTest::testInitMouseEvent()
0046 {
0047     // this test verifies that a MouseEvent is constructed correctly
0048 
0049     // first create the test LibInput::Device
0050     libinput_device device;
0051     Device d(&device);
0052 
0053     QFETCH(QEvent::Type, type);
0054     // now create our own event
0055     MouseEvent event(type, QPointF(100, 200), Qt::LeftButton, Qt::LeftButton | Qt::RightButton,
0056                      Qt::ShiftModifier | Qt::ControlModifier, 300ms, QPointF(1, 2), QPointF(3, 4), &d);
0057     // and verify the contract of QMouseEvent
0058     QCOMPARE(event.type(), type);
0059     QCOMPARE(event.globalPos(), QPoint(100, 200));
0060     QCOMPARE(event.screenPos(), QPointF(100, 200));
0061     QCOMPARE(event.localPos(), QPointF(100, 200));
0062     QCOMPARE(event.button(), Qt::LeftButton);
0063     QCOMPARE(event.buttons(), Qt::LeftButton | Qt::RightButton);
0064     QCOMPARE(event.modifiers(), Qt::ShiftModifier | Qt::ControlModifier);
0065     QCOMPARE(event.timestamp(), 300ms);
0066     // and our custom argument
0067     QCOMPARE(event.device(), &d);
0068     QCOMPARE(event.delta(), QPointF(1, 2));
0069     QCOMPARE(event.deltaUnaccelerated(), QPointF(3, 4));
0070 }
0071 
0072 void InputEventsTest::testInitKeyEvent_data()
0073 {
0074     QTest::addColumn<QEvent::Type>("type");
0075     QTest::addColumn<bool>("autorepeat");
0076 
0077     QTest::newRow("Press") << QEvent::KeyPress << false;
0078     QTest::newRow("Repeat") << QEvent::KeyPress << true;
0079     QTest::newRow("Release") << QEvent::KeyRelease << false;
0080 }
0081 
0082 void InputEventsTest::testInitKeyEvent()
0083 {
0084     // this test verifies that a KeyEvent is constructed correctly
0085 
0086     // first create the test LibInput::Device
0087     libinput_device device;
0088     Device d(&device);
0089 
0090     // setup event
0091     QFETCH(QEvent::Type, type);
0092     QFETCH(bool, autorepeat);
0093     KeyEvent event(type, Qt::Key_Space, Qt::ShiftModifier | Qt::ControlModifier, 200, 300,
0094                    QStringLiteral(" "), autorepeat, 400ms, &d);
0095     // and verify the contract of QKeyEvent
0096     QCOMPARE(event.type(), type);
0097     QCOMPARE(event.isAutoRepeat(), autorepeat);
0098     QCOMPARE(event.key(), int(Qt::Key_Space));
0099     QCOMPARE(event.nativeScanCode(), 200u);
0100     QCOMPARE(event.nativeVirtualKey(), 300u);
0101     QCOMPARE(event.text(), QStringLiteral(" "));
0102     QCOMPARE(event.count(), 1);
0103     QCOMPARE(event.nativeModifiers(), 0u);
0104     QCOMPARE(event.modifiers(), Qt::ShiftModifier | Qt::ControlModifier);
0105     QCOMPARE(event.timestamp(), 400ms);
0106     // and our custom argument
0107     QCOMPARE(event.device(), &d);
0108 }
0109 
0110 void InputEventsTest::testInitWheelEvent_data()
0111 {
0112     QTest::addColumn<Qt::Orientation>("orientation");
0113     QTest::addColumn<qreal>("delta");
0114     QTest::addColumn<qint32>("deltaV120");
0115     QTest::addColumn<QPoint>("expectedAngleDelta");
0116 
0117     QTest::newRow("horiz") << Qt::Horizontal << 3.3 << 1 << QPoint(3, 0);
0118     QTest::newRow("vert") << Qt::Vertical << 2.4 << 2 << QPoint(0, 2);
0119 }
0120 
0121 void InputEventsTest::testInitWheelEvent()
0122 {
0123     // this test verifies that a WheelEvent is constructed correctly
0124 
0125     // first create the test LibInput::Device
0126     libinput_device device;
0127     Device d(&device);
0128 
0129     // setup event
0130     QFETCH(Qt::Orientation, orientation);
0131     QFETCH(qreal, delta);
0132     QFETCH(qint32, deltaV120);
0133     WheelEvent event(QPointF(100, 200), delta, deltaV120, orientation, Qt::LeftButton | Qt::RightButton,
0134                      Qt::ShiftModifier | Qt::ControlModifier, InputRedirection::PointerAxisSourceWheel, 300ms, &d);
0135     // compare QWheelEvent contract
0136     QCOMPARE(event.type(), QEvent::Wheel);
0137     QCOMPARE(event.position(), QPointF(100, 200));
0138     QCOMPARE(event.globalPosition(), QPointF(100, 200));
0139     QCOMPARE(event.buttons(), Qt::LeftButton | Qt::RightButton);
0140     QCOMPARE(event.modifiers(), Qt::ShiftModifier | Qt::ControlModifier);
0141     QCOMPARE(event.timestamp(), 300ms);
0142     QTEST(event.angleDelta(), "expectedAngleDelta");
0143     QTEST(event.orientation(), "orientation");
0144     QTEST(event.delta(), "delta");
0145     QTEST(event.deltaV120(), "deltaV120");
0146     QCOMPARE(event.axisSource(), InputRedirection::PointerAxisSourceWheel);
0147     // and our custom argument
0148     QCOMPARE(event.device(), &d);
0149 }
0150 
0151 void InputEventsTest::testInitSwitchEvent_data()
0152 {
0153     QTest::addColumn<KWin::SwitchEvent::State>("state");
0154     QTest::addColumn<quint64>("timestamp");
0155 
0156     QTest::newRow("on") << SwitchEvent::State::On << quint64{23456790};
0157     QTest::newRow("off") << SwitchEvent::State::Off << quint64{45689235987};
0158 }
0159 
0160 void InputEventsTest::testInitSwitchEvent()
0161 {
0162     // this test verifies that a SwitchEvent is constructed correctly
0163     libinput_device device;
0164     Device d(&device);
0165 
0166     QFETCH(SwitchEvent::State, state);
0167     QFETCH(quint64, timestamp);
0168     SwitchEvent event(state, std::chrono::microseconds(timestamp), &d);
0169 
0170     QCOMPARE(event.state(), state);
0171     QCOMPARE(event.timestamp(), std::chrono::microseconds(timestamp));
0172     QCOMPARE(event.device(), &d);
0173 }
0174 
0175 QTEST_GUILESS_MAIN(InputEventsTest)
0176 #include "input_event_test.moc"