File indexing completed on 2024-05-05 05:31:28

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