4#include <private/qguiapplication_p.h>
5#include <private/qeventpoint_p.h>
7#include <qpa/qplatformintegration.h>
12#include <QtCore/qtestsupport_core.h>
13#include <QtCore/qthread.h>
14#include <QtCore/QDebug>
16#if QT_CONFIG(test_gui)
17#include <QtCore/qloggingcategory.h>
18#include <private/qinputdevicemanager_p.h>
19#include <private/qeventpoint_p.h>
20#include <private/qhighdpiscaling_p.h>
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
43 if (Q_UNLIKELY(!QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowActivation))) {
44 qWarning() <<
"qWaitForWindowActive was called on a platform that doesn't support window"
45 <<
"activation. This means there is an error in the test and it should either"
46 <<
"check for the WindowActivation platform capability before calling"
47 <<
"qWaitForWindowActivate, use qWaitForWindowExposed instead, or skip the test."
48 <<
"Falling back to qWaitForWindowExposed.";
49 return qWaitForWindowExposed(window, timeout);
51 return QTest::qWaitFor([&]() {
return window->isActive(); }, timeout);
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
72 return QTest::qWaitFor([&]() {
return qGuiApp->focusWindow() == window; }, timeout);
76
77
78
79
80
81
82
83
84
85
86
87
88
91 return QTest::qWaitFor([&]() {
return window->isExposed(); }, timeout);
186#if QT_CONFIG(test_gui)
187Q_STATIC_LOGGING_CATEGORY(lcQtGuiTest,
"qt.gui.test");
188#define deb qCDebug(lcQtGuiTest)
191
192
193
194
195static QInputDeviceManager *inputDeviceManager()
197 if (
auto *idm = QGuiApplicationPrivate::inputDeviceManager())
200 deb <<
"No input device manager present.";
205
206
207
208
209void QtGuiTest::setKeyboardModifiers(Qt::KeyboardModifiers modifiers)
211 auto *idm = inputDeviceManager();
212 if (Q_UNLIKELY(!idm))
215 idm->setKeyboardModifiers(modifiers);
216 deb <<
"Keyboard modifiers synthesized:" << modifiers;
220
221
222
223
224void QtGuiTest::setCursorPosition(
const QPoint &position)
226 auto *idm = inputDeviceManager();
227 if (Q_UNLIKELY(!idm))
230 idm->setCursorPos(position);
231 deb <<
"Mouse curser set to" << position;
235
236
237
238
239
240
241
242
243
244void QtGuiTest::synthesizeExtendedKeyEvent(QEvent::Type type,
int key, Qt::KeyboardModifiers modifiers,
245 quint32 nativeScanCode, quint32 nativeVirtualKey,
248 Q_ASSERT_X((type == QEvent::KeyPress
249 || type == QEvent::KeyRelease),
251 "called with invalid QEvent type");
253 deb <<
"Synthesizing key event:" << type << Qt::Key(key) << modifiers << text;
255 if (QWindowSystemInterface::handleExtendedKeyEvent(
nullptr, type, key, modifiers,
256 nativeScanCode, nativeVirtualKey,
257 modifiers, text,
false,
262 QCoreApplication::sendPostedEvents();
270
271
272
273
274
275
276
277
278
279bool QtGuiTest::synthesizeKeyEvent(QWindow *window, QEvent::Type t,
int k, Qt::KeyboardModifiers mods,
280 const QString & text,
bool autorep,
283 Q_ASSERT_X((t == QEvent::KeyPress
284 || t == QEvent::KeyRelease),
286 "called with invalid QEvent type");
288 deb <<
"Synthesizing key event:" << t << Qt::Key(k) << mods << text;
290 bool result = QWindowSystemInterface::handleKeyEvent(window, t, k, mods, text, autorep, count);
294 QCoreApplication::sendPostedEvents();
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318void QtGuiTest::synthesizeMouseEvent(
const QPointF &position, Qt::MouseButtons state,
319 Qt::MouseButton button, QEvent::Type type,
320 Qt::KeyboardModifiers modifiers)
322 Q_ASSERT_X((type == QEvent::MouseButtonPress
323 || type == QEvent::MouseButtonRelease
324 || type == QEvent::MouseMove),
326 "called with invalid QEvent type");
328 deb <<
"Synthesizing mouse event:" << type << position << button << modifiers;
330 if (QWindowSystemInterface::handleMouseEvent(
nullptr, position, position, state, button,
331 type, modifiers, Qt::MouseEventNotSynthesized)) {
334 QCoreApplication::processEvents();
335 QCoreApplication::sendPostedEvents();
344
345
346
347
348
349
350
351
352void QtGuiTest::synthesizeWheelEvent(
int rollCount, Qt::KeyboardModifiers modifiers)
354 deb <<
"Synthesizing wheel event:" << rollCount << modifiers;
356 QPoint position = QCursor::pos();
357 if (QWindowSystemInterface::handleWheelEvent(
nullptr, position, position,
358 QPoint(), QPoint(0, -rollCount), modifiers)) {
362 QCoreApplication::sendPostedEvents();
370
371
372
373
374qint64 QtGuiTest::eventTimeElapsed()
376 return QWindowSystemInterfacePrivate::eventTime.elapsed();
380
381
382
383
384void QtGuiTest::postFakeWindowActivation(QWindow *window)
388 "called with nullptr");
390 deb <<
"Posting fake window activation:" << window;
392 QWindowSystemInterfacePrivate::FocusWindowEvent e(window, Qt::OtherFocusReason);
393 QGuiApplicationPrivate::processWindowSystemEvent(&e);
394 QWindowSystemInterface::handleFocusWindowChanged(window);
398
399
400
401QPoint QtGuiTest::toNativePixels(
const QPoint &value,
const QWindow *window)
405 "called with nullptr");
407 deb <<
"Calculating native pixels: " << value << window;
408 return QHighDpi::toNativePixels<QPoint, QWindow>(value, window);
412
413
414
415QRect QtGuiTest::toNativePixels(
const QRect &value,
const QWindow *window)
419 "called with nullptr");
421 deb <<
"Calculating native pixels: " << value << window;
422 return QHighDpi::toNativePixels<QRect, QWindow>(value, window);
426
427
428
429qreal QtGuiTest::scaleFactor(
const QWindow *window)
433 "called with nullptr");
435 deb <<
"Calculating scaling factor: " << window;
436 return QHighDpiScaling::factor(window);
440
441
442
443void QtGuiTest::setEventPointId(QEventPoint &p,
int arg)
445 QMutableEventPoint::setId(p, arg);
449
450
451
452void QtGuiTest::setEventPointPressure(QEventPoint &p, qreal arg)
454 QMutableEventPoint::setPressure(p, arg);
458
459
460
461void QtGuiTest::setEventPointState(QEventPoint &p, QEventPoint::State arg)
463 QMutableEventPoint::setState(p, arg);
467
468
469
470void QtGuiTest::setEventPointPosition(QEventPoint &p, QPointF arg)
472 QMutableEventPoint::setPosition(p, arg);
476
477
478
479void QtGuiTest::setEventPointGlobalPosition(QEventPoint &p, QPointF arg)
481 QMutableEventPoint::setGlobalPosition(p, arg);
485
486
487
488void QtGuiTest::setEventPointScenePosition(QEventPoint &p, QPointF arg)
490 QMutableEventPoint::setScenePosition(p, arg);
494
495
496
497void QtGuiTest::setEventPointEllipseDiameters(QEventPoint &p, QSizeF arg)
499 QMutableEventPoint::setEllipseDiameters(p, arg);
Combined button and popup list for selecting options.
Q_GUI_EXPORT bool qWaitForWindowFocused(QWindow *window, QDeadlineTimer timeout=std::chrono::seconds{5})
Q_GUI_EXPORT bool qWaitForWindowActive(QWindow *window, int timeout=5000)
Q_GUI_EXPORT bool qWaitForWindowExposed(QWindow *window, int timeout=5000)