Debian Codegraph @

tests/auto/gui/platform/qx11info/tst_qx11info.cpp

raw
1 // Copyright (C) 2016 The Qt Company Ltd.
2 // Copyright (C) 2016 David Faure <[email protected]>
3 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
4
5 #include <QtTest/qtest.h>
6
7 #include <QtGui/qguiapplication.h>
8
9 #include <QtGui/private/qtx11extras_p.h>
10
11 using namespace Qt::StringLiterals;
12
13 class tst_QX11Info : public QObject
14 {
15 Q_OBJECT
16
17 private slots:
18 void staticFunctionsBeforeQGuiApplication();
19 void startupId();
20 void isPlatformX11();
21 void appTime();
22 void peeker();
23 void isCompositingManagerRunning();
24 };
25
26 void tst_QX11Info::staticFunctionsBeforeQGuiApplication()
27 {
28 QVERIFY(!QGuiApplication::instance());
29
30 // none of these functions should crash if QGuiApplication hasn't
31 // been constructed
32
33 Display *display = QX11Info::display();
34 QCOMPARE(display, (Display *)0);
35
36 #if 0
37 const char *appClass = QX11Info::appClass();
38 QCOMPARE(appClass, (const char *)0);
39 #endif
40 int appScreen = QX11Info::appScreen();
41 QCOMPARE(appScreen, 0);
42 #if 0
43 int appDepth = QX11Info::appDepth();
44 QCOMPARE(appDepth, 32);
45 int appCells = QX11Info::appCells();
46 QCOMPARE(appCells, 0);
47 Qt::HANDLE appColormap = QX11Info::appColormap();
48 QCOMPARE(appColormap, static_cast<Qt::HANDLE>(0));
49 void *appVisual = QX11Info::appVisual();
50 QCOMPARE(appVisual, static_cast<void *>(0));
51 #endif
52 unsigned long appRootWindow = QX11Info::appRootWindow();
53 QCOMPARE(appRootWindow, static_cast<unsigned long>(0));
54
55 #if 0
56 bool appDefaultColormap = QX11Info::appDefaultColormap();
57 QCOMPARE(appDefaultColormap, true);
58 bool appDefaultVisual = QX11Info::appDefaultVisual();
59 QCOMPARE(appDefaultVisual, true);
60 #endif
61
62 int appDpiX = QX11Info::appDpiX();
63 int appDpiY = QX11Info::appDpiY();
64 QCOMPARE(appDpiX, 75);
65 QCOMPARE(appDpiY, 75);
66
67 unsigned long appTime = QX11Info::appTime();
68 unsigned long appUserTime = QX11Info::appUserTime();
69 QCOMPARE(appTime, 0ul);
70 QCOMPARE(appUserTime, 0ul);
71 // setApp*Time do nothing without QGuiApplication
72 QX11Info::setAppTime(1234);
73 QX11Info::setAppUserTime(5678);
74 appTime = QX11Info::appTime();
75 appUserTime = QX11Info::appUserTime();
76 QCOMPARE(appTime, 0ul);
77 QCOMPARE(appTime, 0ul);
78
79 QX11Info::isCompositingManagerRunning();
80 }
81
82 static const char idFromEnv[] = "startupid_TIME123456";
83 void initialize()
84 {
85 qputenv("DESKTOP_STARTUP_ID", idFromEnv);
86 }
87 Q_CONSTRUCTOR_FUNCTION(initialize)
88
89 static QString checkSkip() {
90 if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive))
91 return "This test is only for X11, not Wayland."_L1;
92 if (QGuiApplication::platformName().startsWith(QLatin1String("offscreen"), Qt::CaseInsensitive))
93 return "This test is only for X11, not offscreen."_L1;
94 return ""_L1;
95 }
96
97 void tst_QX11Info::startupId()
98 {
99 int argc = 0;
100 QGuiApplication app(argc, 0);
101 QString result = checkSkip();
102 if (!result.isEmpty())
103 QSKIP(result.toUtf8());
104
105 // This relies on the fact that no widget was shown yet,
106 // so please make sure this method is always the first test.
107 QCOMPARE(QString(QX11Info::nextStartupId()), QString(idFromEnv));
108 QWindow w;
109 w.show();
110 QVERIFY(QX11Info::nextStartupId().isEmpty());
111
112 QByteArray idSecondWindow = "startupid2_TIME234567";
113 QX11Info::setNextStartupId(idSecondWindow);
114 QCOMPARE(QX11Info::nextStartupId(), idSecondWindow);
115
116 QWindow w2;
117 w2.show();
118 QVERIFY(QX11Info::nextStartupId().isEmpty());
119 }
120
121 void tst_QX11Info::isPlatformX11()
122 {
123 int argc = 0;
124 QGuiApplication app(argc, 0);
125 QString result = checkSkip();
126 if (!result.isEmpty())
127 QSKIP(result.toUtf8());
128
129 QVERIFY(QX11Info::isPlatformX11());
130 }
131
132 void tst_QX11Info::appTime()
133 {
134 int argc = 0;
135 QGuiApplication app(argc, 0);
136 QString result = checkSkip();
137 if (!result.isEmpty())
138 QSKIP(result.toUtf8());
139
140 // No X11 event received yet
141 QCOMPARE(QX11Info::appTime(), 0ul);
142 QCOMPARE(QX11Info::appUserTime(), 0ul);
143
144 // Trigger some X11 events
145 QWindow window;
146 window.show();
147 QTest::qWait(100);
148 QVERIFY(QX11Info::appTime() > 0);
149
150 unsigned long t0 = QX11Info::appTime();
151 unsigned long t1 = t0 + 1;
152 QX11Info::setAppTime(t1);
153 QCOMPARE(QX11Info::appTime(), t1);
154
155 unsigned long u0 = QX11Info::appUserTime();
156 unsigned long u1 = u0 + 1;
157 QX11Info::setAppUserTime(u1);
158 QCOMPARE(QX11Info::appUserTime(), u1);
159 }
160
161 class PeekerTest : public QWindow
162 {
163 public:
164 PeekerTest()
165 {
166 setGeometry(100, 100, 400, 400);
167 m_peekerFirstId = QX11Info::generatePeekerId();
168 QVERIFY(m_peekerFirstId >= 0);
169 m_peekerSecondId = QX11Info::generatePeekerId();
170 QVERIFY(m_peekerSecondId == m_peekerFirstId + 1);
171 // Get X atoms
172 xcb_connection_t *c = QX11Info::connection();
173 const char *first = "QT_XCB_PEEKER_TEST_FIRST";
174 const char *second = "QT_XCB_PEEKER_TEST_SECOND";
175 xcb_intern_atom_reply_t *reply =
176 xcb_intern_atom_reply(c, xcb_intern_atom(c, false, strlen(first), first), 0);
177 QVERIFY2(reply != nullptr, first);
178 m_atomFirst = reply->atom;
179 free(reply);
180 reply = xcb_intern_atom_reply(c, xcb_intern_atom(c, false, strlen(second), second), 0);
181 QVERIFY2(reply != nullptr, second);
182 m_atomSecond = reply->atom;
183 free(reply);
184 }
185
186 protected:
187 void triggerPropertyNotifyEvents()
188 {
189 xcb_window_t rootWindow = QX11Info::appRootWindow();
190 xcb_connection_t *connection = QX11Info::connection();
191 xcb_change_property(connection, XCB_PROP_MODE_APPEND, rootWindow, m_atomFirst,
192 XCB_ATOM_INTEGER, 32, 0, nullptr);
193 xcb_change_property(connection, XCB_PROP_MODE_APPEND, rootWindow, m_atomSecond,
194 XCB_ATOM_INTEGER, 32, 0, nullptr);
195 xcb_flush(connection);
196 }
197
198 static bool checkForPropertyNotifyByAtom(xcb_generic_event_t *event, void *data)
199 {
200 bool isPropertyNotify = (event->response_type & ~0x80) == XCB_PROPERTY_NOTIFY;
201 if (isPropertyNotify) {
202 xcb_property_notify_event_t *pn =
203 reinterpret_cast<xcb_property_notify_event_t *>(event);
204 xcb_atom_t *atom = static_cast<xcb_atom_t *>(data);
205 if (pn->atom == *atom)
206 return true;
207 }
208 return false;
209 }
210
211 bool sanityCheckPeeking(xcb_generic_event_t *event)
212 {
213 m_countWithCaching++;
214 bool isPropertyNotify = (event->response_type & ~0x80) == XCB_PROPERTY_NOTIFY;
215 if (isPropertyNotify) {
216 xcb_property_notify_event_t *pn =
217 reinterpret_cast<xcb_property_notify_event_t *>(event);
218 if (pn->atom == m_atomFirst) {
219 if (m_indexFirst == -1) {
220 m_indexFirst = m_countWithCaching;
221 // continue peeking, maybe the second event is already in the queue
222 return false;
223 }
224 m_foundFirstEventAgain = true;
225 // Return so we can fail the test with QVERIFY2, for more details
226 // see QTBUG-62354
227 return true;
228 }
229 // Let it peek to the end, even when the second event was found
230 if (pn->atom == m_atomSecond) {
231 m_indexSecond = m_countWithCaching;
232 if (m_indexFirst == -1) {
233 m_foundSecondBeforeFirst = true;
234 // Same as above, see QTBUG-62354
235 return true;
236 }
237 }
238 }
239 return false;
240 }
241
242 void exposeEvent(QExposeEvent *) override
243 {
244 if (m_ignoreSubsequentExpose)
245 return;
246 // We don't want to execute this handler again in case there are more expose
247 // events after calling QCoreApplication::processEvents() later in this test
248 m_ignoreSubsequentExpose = true;
249
250 xcb_flush(QX11Info::connection());
251 bool found = QX11Info::peekEventQueue(checkForPropertyNotifyByAtom, &m_atomFirst);
252 QVERIFY2(!found, "Found m_atomFirst which should not be in the queue yet");
253 found = QX11Info::peekEventQueue(checkForPropertyNotifyByAtom, &m_atomSecond);
254 QVERIFY2(!found, "Found m_atomSecond which should not be in the queue yet");
255
256 triggerPropertyNotifyEvents();
257
258 bool earlyExit = false;
259 while (!earlyExit && (m_indexFirst == -1 || m_indexSecond == -1)) {
260
261 earlyExit = QX11Info::peekEventQueue([](xcb_generic_event_t *event, void *data) {
262 return static_cast<PeekerTest *>(data)->sanityCheckPeeking(event);
263 }, this, QX11Info::PeekFromCachedIndex, m_peekerFirstId);
264
265 if (m_countWithCaching == -1)
266 QVERIFY2(!earlyExit, "Unexpected return value for an empty queue");
267 }
268
269 QVERIFY2(!m_foundFirstEventAgain,
270 "Found the same notify event twice, maybe broken index cache?");
271 QVERIFY2(!m_foundSecondBeforeFirst, "Found second event before first");
272 QVERIFY2(!earlyExit,
273 "Unexpected return value for a peeker that always scans to the end of the queue");
274
275 found = QX11Info::peekEventQueue(checkForPropertyNotifyByAtom, &m_atomFirst,
276 QX11Info::PeekDefault, m_peekerFirstId);
277 QVERIFY2(found, "Failed to find m_atomFirst, when peeking from start and ignoring "
278 "the cached index associated with the passed in peeker id");
279 // The above call updated index cache for m_peekerFirstId to the position of
280 // event(m_atomFirst) + 1
281 found = QX11Info::peekEventQueue(checkForPropertyNotifyByAtom, &m_atomSecond,
282 QX11Info::PeekFromCachedIndex, m_peekerFirstId);
283 QVERIFY2(found, "Unexpectedly failed to find m_atomSecond");
284
285 QVERIFY(m_indexFirst <= m_countWithCaching);
286 QVERIFY(m_indexSecond <= m_countWithCaching);
287 QVERIFY(m_indexFirst < m_indexSecond);
288 QX11Info::peekEventQueue([](xcb_generic_event_t *, void *data) {
289 static_cast<PeekerTest *>(data)->m_countFromStart++;
290 return false;
291 }, this);
292 QVERIFY(m_countWithCaching <= m_countFromStart);
293 found = QX11Info::peekEventQueue(checkForPropertyNotifyByAtom, &m_atomFirst,
294 QX11Info::PeekFromCachedIndex, m_peekerSecondId);
295 QVERIFY2(found, "m_peekerSecondId failed to find the event");
296
297 // Remove peeker id from within the peeker while using it for peeking
298 QX11Info::peekEventQueue([](xcb_generic_event_t *, void *data) {
299 PeekerTest *obj = static_cast<PeekerTest *>(data);
300 QX11Info::removePeekerId(obj->m_peekerSecondId);
301 return true;
302 }, this, QX11Info::PeekFromCachedIndex, m_peekerSecondId);
303 // Check that it really has been removed from the cache
304 bool ok = QX11Info::peekEventQueue([](xcb_generic_event_t *, void *) {
305 return true;
306 }, nullptr, QX11Info::PeekFromCachedIndex, m_peekerSecondId);
307 QVERIFY2(!ok, "Unexpected return value when attempting to peek from cached "
308 "index when peeker id has been removed from the cache");
309
310 // Sanity check other input combinations
311 QVERIFY(!QX11Info::removePeekerId(-99));
312 ok = QX11Info::peekEventQueue([](xcb_generic_event_t *, void *) {
313 return true;
314 }, nullptr, QX11Info::PeekFromCachedIndex, -100);
315 QVERIFY2(!ok, "PeekFromCachedIndex with invalid peeker id unexpectedly succeeded");
316 ok = QX11Info::peekEventQueue([](xcb_generic_event_t *, void *) {
317 return true;
318 }, nullptr, QX11Info::PeekDefault, -100);
319 QVERIFY2(!ok, "PeekDefault with invalid peeker id unexpectedly succeeded");
320 ok = QX11Info::peekEventQueue([](xcb_generic_event_t *, void *) {
321 return true;
322 }, nullptr, QX11Info::PeekFromCachedIndex);
323 QVERIFY2(!ok, "PeekFromCachedIndex without peeker id unexpectedly succeeded");
324 ok = QX11Info::peekEventQueue([](xcb_generic_event_t *, void *) {
325 return true;
326 }, nullptr, QX11Info::PeekDefault);
327 QVERIFY2(ok, "PeekDefault without peeker id unexpectedly failed");
328
329 QCoreApplication::processEvents(); // Flush buffered events
330
331 found = QX11Info::peekEventQueue(checkForPropertyNotifyByAtom, &m_atomFirst);
332 QVERIFY2(!found, "Found m_atomFirst in the queue after flushing");
333 found = QX11Info::peekEventQueue(checkForPropertyNotifyByAtom, &m_atomSecond);
334 QVERIFY2(!found, "Found m_atomSecond in the queue after flushing");
335
336 QVERIFY(QX11Info::removePeekerId(m_peekerFirstId));
337 QVERIFY2(!QX11Info::removePeekerId(m_peekerFirstId),
338 "Removing the same peeker id twice unexpectedly succeeded");
339 #if 0
340 qDebug() << "Buffered event queue size (caching) : " << m_countWithCaching + 1;
341 qDebug() << "Buffered event queue size (from start) : " << m_countFromStart + 1;
342 qDebug() << "PropertyNotify[FIRST] at : " << m_indexFirst;
343 qDebug() << "PropertyNotify[SECOND] at : " << m_indexSecond;
344 #endif
345 }
346
347 private:
348 xcb_atom_t m_atomFirst = -1;
349 xcb_atom_t m_atomSecond = -1;
350 qint32 m_peekerFirstId = -1;
351 qint32 m_peekerSecondId = -1;
352 qint32 m_indexFirst = -1;
353 qint32 m_indexSecond = -1;
354 bool m_foundFirstEventAgain = false;
355 qint32 m_countWithCaching = -1;
356 qint32 m_countFromStart = -1;
357 bool m_ignoreSubsequentExpose = false;
358 bool m_foundSecondBeforeFirst = false;
359 };
360
361 void tst_QX11Info::peeker()
362 {
363 int argc = 0;
364 QGuiApplication app(argc, 0);
365 QString result = checkSkip();
366 if (!result.isEmpty())
367 QSKIP(result.toUtf8());
368
369 PeekerTest test;
370 test.show();
371
372 QVERIFY(QTest::qWaitForWindowExposed(&test));
373 }
374
375 void tst_QX11Info::isCompositingManagerRunning()
376 {
377 int argc = 0;
378 QGuiApplication app(argc, 0);
379 QString result = checkSkip();
380 if (!result.isEmpty())
381 QSKIP(result.toUtf8());
382 const bool b = QX11Info::isCompositingManagerRunning();
383 Q_UNUSED(b);
384 const bool b2 = QX11Info::isCompositingManagerRunning(0);
385 Q_UNUSED(b2);
386 // just check that it didn't crash (QTBUG-91913)
387 }
388
389 QTEST_APPLESS_MAIN(tst_QX11Info)
390
391 #include "tst_qx11info.moc"