Changeset View
Changeset View
Standalone View
Standalone View
autotests/test_x11_timestamp_update.cpp
- This file was added.
1 | /******************************************************************** | ||||
---|---|---|---|---|---|
2 | KWin - the KDE window manager | ||||
3 | This file is part of the KDE project. | ||||
4 | | ||||
5 | Copyright (C) 2017 Martin Gräßlin <mgraesslin@kde.org> | ||||
6 | | ||||
7 | This program is free software; you can redistribute it and/or modify | ||||
8 | it under the terms of the GNU General Public License as published by | ||||
9 | the Free Software Foundation; either version 2 of the License, or | ||||
10 | (at your option) any later version. | ||||
11 | | ||||
12 | This program is distributed in the hope that it will be useful, | ||||
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
15 | GNU General Public License for more details. | ||||
16 | | ||||
17 | You should have received a copy of the GNU General Public License | ||||
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | *********************************************************************/ | ||||
20 | | ||||
21 | #include <QTest> | ||||
22 | #include <QX11Info> | ||||
23 | | ||||
24 | #include "main.h" | ||||
25 | #include "utils.h" | ||||
26 | | ||||
27 | namespace KWin | ||||
28 | { | ||||
29 | | ||||
30 | class X11TestApplication : public Application | ||||
31 | { | ||||
32 | Q_OBJECT | ||||
33 | public: | ||||
34 | X11TestApplication(int &argc, char **argv); | ||||
35 | virtual ~X11TestApplication(); | ||||
36 | | ||||
37 | protected: | ||||
38 | void performStartup() override; | ||||
39 | | ||||
40 | }; | ||||
41 | | ||||
42 | X11TestApplication::X11TestApplication(int &argc, char **argv) | ||||
43 | : Application(OperationModeX11, argc, argv) | ||||
44 | { | ||||
45 | setX11Connection(QX11Info::connection()); | ||||
46 | setX11RootWindow(QX11Info::appRootWindow()); | ||||
47 | } | ||||
48 | | ||||
49 | X11TestApplication::~X11TestApplication() | ||||
50 | { | ||||
51 | } | ||||
52 | | ||||
53 | void X11TestApplication::performStartup() | ||||
54 | { | ||||
55 | } | ||||
56 | | ||||
57 | } | ||||
58 | | ||||
59 | class X11TimestampUpdateTest : public QObject | ||||
60 | { | ||||
61 | Q_OBJECT | ||||
62 | private Q_SLOTS: | ||||
63 | void testGrabAfterServerTime(); | ||||
64 | void testBeforeLastGrabTime(); | ||||
65 | }; | ||||
66 | | ||||
67 | void X11TimestampUpdateTest::testGrabAfterServerTime() | ||||
68 | { | ||||
69 | // this test tries to grab the X keyboard with a timestamp in future | ||||
70 | // that should fail, but after updating the X11 timestamp, it should | ||||
71 | // work again | ||||
72 | KWin::updateXTime(); | ||||
73 | QCOMPARE(KWin::grabXKeyboard(), true); | ||||
74 | KWin::ungrabXKeyboard(); | ||||
75 | | ||||
76 | // now let's change the timestamp | ||||
77 | KWin::kwinApp()->setX11Time(KWin::xTime() + 5 * 60 * 1000); | ||||
78 | | ||||
79 | // now grab keyboard should fail | ||||
80 | QCOMPARE(KWin::grabXKeyboard(), false); | ||||
81 | | ||||
82 | // let's update timestamp, now it should work again | ||||
83 | KWin::updateXTime(); | ||||
84 | QCOMPARE(KWin::grabXKeyboard(), true); | ||||
85 | KWin::ungrabXKeyboard(); | ||||
86 | } | ||||
87 | | ||||
88 | void X11TimestampUpdateTest::testBeforeLastGrabTime() | ||||
89 | { | ||||
90 | // this test tries to grab the X keyboard with a timestamp before the | ||||
91 | // last grab time on the server. That should fail, but after updating the X11 | ||||
92 | // timestamp it should work again | ||||
93 | | ||||
94 | // first set the grab timestamp | ||||
95 | KWin::updateXTime(); | ||||
96 | QCOMPARE(KWin::grabXKeyboard(), true); | ||||
97 | KWin::ungrabXKeyboard(); | ||||
98 | | ||||
99 | // now go to past | ||||
100 | const auto timestamp = KWin::xTime(); | ||||
101 | KWin::kwinApp()->setX11Time(KWin::xTime() - 5 * 60 * 1000, KWin::Application::TimestampUpdate::Always); | ||||
102 | QCOMPARE(KWin::xTime(), timestamp - 5 * 60 * 1000); | ||||
103 | | ||||
104 | // now grab keyboard should fail | ||||
105 | QCOMPARE(KWin::grabXKeyboard(), false); | ||||
106 | | ||||
107 | // let's update timestamp, now it should work again | ||||
108 | KWin::updateXTime(); | ||||
109 | QVERIFY(KWin::xTime() >= timestamp); | ||||
110 | QCOMPARE(KWin::grabXKeyboard(), true); | ||||
111 | KWin::ungrabXKeyboard(); | ||||
112 | } | ||||
113 | | ||||
114 | int main(int argc, char *argv[]) | ||||
115 | { | ||||
116 | setenv("QT_QPA_PLATFORM", "xcb", true); | ||||
117 | KWin::X11TestApplication app(argc, argv); | ||||
118 | app.setAttribute(Qt::AA_Use96Dpi, true); | ||||
119 | X11TimestampUpdateTest tc; | ||||
120 | return QTest::qExec(&tc, argc, argv); | ||||
121 | } | ||||
122 | | ||||
123 | #include "test_x11_timestamp_update.moc" |