File indexing completed on 2024-04-28 16:46:47

0001 /*
0002     KWin - the KDE window manager
0003     This file is part of the KDE project.
0004 
0005     SPDX-FileCopyrightText: 2017 Martin Gräßlin <mgraesslin@kde.org>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #include <QTest>
0011 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
0012 #include <private/qtx11extras_p.h>
0013 #else
0014 #include <QX11Info>
0015 #endif
0016 
0017 #include "main.h"
0018 #include "utils/common.h"
0019 
0020 namespace KWin
0021 {
0022 
0023 class X11TestApplication : public Application
0024 {
0025     Q_OBJECT
0026 public:
0027     X11TestApplication(int &argc, char **argv);
0028     ~X11TestApplication() override;
0029 
0030 protected:
0031     void performStartup() override;
0032 };
0033 
0034 X11TestApplication::X11TestApplication(int &argc, char **argv)
0035     : Application(OperationModeX11, argc, argv)
0036 {
0037     setX11Connection(QX11Info::connection());
0038     setX11RootWindow(QX11Info::appRootWindow());
0039 }
0040 
0041 X11TestApplication::~X11TestApplication()
0042 {
0043 }
0044 
0045 void X11TestApplication::performStartup()
0046 {
0047 }
0048 
0049 }
0050 
0051 class X11TimestampUpdateTest : public QObject
0052 {
0053     Q_OBJECT
0054 private Q_SLOTS:
0055     void testGrabAfterServerTime();
0056     void testBeforeLastGrabTime();
0057 };
0058 
0059 void X11TimestampUpdateTest::testGrabAfterServerTime()
0060 {
0061     // this test tries to grab the X keyboard with a timestamp in future
0062     // that should fail, but after updating the X11 timestamp, it should
0063     // work again
0064     KWin::kwinApp()->updateXTime();
0065     QCOMPARE(KWin::grabXKeyboard(), true);
0066     KWin::ungrabXKeyboard();
0067 
0068     // now let's change the timestamp
0069     KWin::kwinApp()->setX11Time(KWin::xTime() + 5 * 60 * 1000);
0070 
0071     // now grab keyboard should fail
0072     QCOMPARE(KWin::grabXKeyboard(), false);
0073 
0074     // let's update timestamp, now it should work again
0075     KWin::kwinApp()->updateXTime();
0076     QCOMPARE(KWin::grabXKeyboard(), true);
0077     KWin::ungrabXKeyboard();
0078 }
0079 
0080 void X11TimestampUpdateTest::testBeforeLastGrabTime()
0081 {
0082     // this test tries to grab the X keyboard with a timestamp before the
0083     // last grab time on the server. That should fail, but after updating the X11
0084     // timestamp it should work again
0085 
0086     // first set the grab timestamp
0087     KWin::kwinApp()->updateXTime();
0088     QCOMPARE(KWin::grabXKeyboard(), true);
0089     KWin::ungrabXKeyboard();
0090 
0091     // now go to past
0092     const auto timestamp = KWin::xTime();
0093     KWin::kwinApp()->setX11Time(KWin::xTime() - 5 * 60 * 1000, KWin::Application::TimestampUpdate::Always);
0094     QCOMPARE(KWin::xTime(), timestamp - 5 * 60 * 1000);
0095 
0096     // now grab keyboard should fail
0097     QCOMPARE(KWin::grabXKeyboard(), false);
0098 
0099     // let's update timestamp, now it should work again
0100     KWin::kwinApp()->updateXTime();
0101     QVERIFY(KWin::xTime() >= timestamp);
0102     QCOMPARE(KWin::grabXKeyboard(), true);
0103     KWin::ungrabXKeyboard();
0104 }
0105 
0106 int main(int argc, char *argv[])
0107 {
0108     setenv("QT_QPA_PLATFORM", "xcb", true);
0109     KWin::X11TestApplication app(argc, argv);
0110     app.setAttribute(Qt::AA_Use96Dpi, true);
0111     X11TimestampUpdateTest tc;
0112     return QTest::qExec(&tc, argc, argv);
0113 }
0114 
0115 #include "test_x11_timestamp_update.moc"