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
33 return qWaitForWindowActive(window, QDeadlineTimer{timeout, Qt::TimerType::PreciseTimer});
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
54 using Internal::WaitForResult;
55 if (Q_UNLIKELY(!QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowActivation))) {
56 qWarning() <<
"qWaitForWindowActive was called on a platform that doesn't support window"
57 <<
"activation. This means there is an error in the test and it should either"
58 <<
"check for the WindowActivation platform capability before calling"
59 <<
"qWaitForWindowActivate, use qWaitForWindowExposed instead, or skip the test."
60 <<
"Falling back to qWaitForWindowExposed.";
61 return qWaitForWindowExposed(window, timeout);
63 return QTest::qWaitFor([wp = QPointer(window)]() {
64 if (QWindow *w = wp.data(); !w)
65 return WaitForResult::Failed;
67 return w->isActive() ? WaitForResult::Done : WaitForResult::NotYet;
72
73
74
75
76
79 return qWaitForWindowActive(window, defaultTryTimeout.load(std::memory_order_relaxed));
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
100 using Internal::WaitForResult;
101 return QTest::qWaitFor([wp = QPointer(window)]() {
102 if (QWindow *w = wp.data(); !w)
103 return WaitForResult::Failed;
105 return qGuiApp->focusWindow() == w ? WaitForResult::Done : WaitForResult::NotYet;
110
111
112
113
114
117 return qWaitForWindowFocused(window, defaultTryTimeout.load(std::memory_order_relaxed));
121
122
123
124
125
128 return qWaitForWindowExposed(window, std::chrono::milliseconds(timeout));
132
133
134
135
136
137
138
139
140
141
142
143
144
147 using Internal::WaitForResult;
148 return QTest::qWaitFor([wp = QPointer(window)]() {
149 if (QWindow *w = wp.data(); !w)
150 return WaitForResult::Failed;
152 return w->isExposed() ? WaitForResult::Done : WaitForResult::NotYet;
157
158
159
160
161
164 return qWaitForWindowExposed(window, defaultTryTimeout.load(std::memory_order_relaxed));
259#if QT_CONFIG(test_gui)
260Q_STATIC_LOGGING_CATEGORY(lcQtGuiTest,
"qt.gui.test");
261#define deb qCDebug(lcQtGuiTest)
264
265
266
267
268static QInputDeviceManager *inputDeviceManager()
270 if (
auto *idm = QGuiApplicationPrivate::inputDeviceManager())
273 deb <<
"No input device manager present.";
278
279
280
281
282void QtGuiTest::setKeyboardModifiers(Qt::KeyboardModifiers modifiers)
284 auto *idm = inputDeviceManager();
285 if (Q_UNLIKELY(!idm))
288 idm->setKeyboardModifiers(modifiers);
289 deb <<
"Keyboard modifiers synthesized:" << modifiers;
293
294
295
296
297void QtGuiTest::setCursorPosition(
const QPoint &position)
299 auto *idm = inputDeviceManager();
300 if (Q_UNLIKELY(!idm))
303 idm->setCursorPos(position);
304 deb <<
"Mouse curser set to" << position;
308
309
310
311
312
313
314
315
316
317void QtGuiTest::synthesizeExtendedKeyEvent(QEvent::Type type,
int key, Qt::KeyboardModifiers modifiers,
318 quint32 nativeScanCode, quint32 nativeVirtualKey,
321 Q_ASSERT_X((type == QEvent::KeyPress
322 || type == QEvent::KeyRelease),
324 "called with invalid QEvent type");
326 deb <<
"Synthesizing key event:" << type << Qt::Key(key) << modifiers << text;
328 if (QWindowSystemInterface::handleExtendedKeyEvent(
nullptr, type, key, modifiers,
329 nativeScanCode, nativeVirtualKey,
330 modifiers, text,
false,
335 QCoreApplication::sendPostedEvents();
343
344
345
346
347
348
349
350
351
352bool QtGuiTest::synthesizeKeyEvent(QWindow *window, QEvent::Type t,
int k, Qt::KeyboardModifiers mods,
353 const QString & text,
bool autorep,
356 Q_ASSERT_X((t == QEvent::KeyPress
357 || t == QEvent::KeyRelease),
359 "called with invalid QEvent type");
361 deb <<
"Synthesizing key event:" << t << Qt::Key(k) << mods << text;
363 bool result = QWindowSystemInterface::handleKeyEvent(window, t, k, mods, text, autorep, count);
367 QCoreApplication::sendPostedEvents();
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391void QtGuiTest::synthesizeMouseEvent(
const QPointF &position, Qt::MouseButtons state,
392 Qt::MouseButton button, QEvent::Type type,
393 Qt::KeyboardModifiers modifiers)
395 Q_ASSERT_X((type == QEvent::MouseButtonPress
396 || type == QEvent::MouseButtonRelease
397 || type == QEvent::MouseMove),
399 "called with invalid QEvent type");
401 deb <<
"Synthesizing mouse event:" << type << position << button << modifiers;
403 if (QWindowSystemInterface::handleMouseEvent(
nullptr, position, position, state, button,
404 type, modifiers, Qt::MouseEventNotSynthesized)) {
407 QCoreApplication::processEvents();
408 QCoreApplication::sendPostedEvents();
417
418
419
420
421
422
423
424
425void QtGuiTest::synthesizeWheelEvent(
int rollCount, Qt::KeyboardModifiers modifiers)
427 deb <<
"Synthesizing wheel event:" << rollCount << modifiers;
429 QPoint position = QCursor::pos();
430 if (QWindowSystemInterface::handleWheelEvent(
nullptr, position, position,
431 QPoint(), QPoint(0, -rollCount), modifiers)) {
435 QCoreApplication::sendPostedEvents();
443
444
445
446
447qint64 QtGuiTest::eventTimeElapsed()
449 return QWindowSystemInterfacePrivate::eventTime.elapsed();
453
454
455
456
457void QtGuiTest::postFakeWindowActivation(QWindow *window)
461 "called with nullptr");
463 deb <<
"Posting fake window activation:" << window;
465 QWindowSystemInterfacePrivate::FocusWindowEvent e(window, Qt::OtherFocusReason);
466 QGuiApplicationPrivate::processWindowSystemEvent(&e);
467 QWindowSystemInterface::handleFocusWindowChanged(window);
471
472
473
474QPoint QtGuiTest::toNativePixels(
const QPoint &value,
const QWindow *window)
478 "called with nullptr");
480 deb <<
"Calculating native pixels: " << value << window;
481 return QHighDpi::toNativePixels<QPoint, QWindow>(value, window);
485
486
487
488QRect QtGuiTest::toNativePixels(
const QRect &value,
const QWindow *window)
492 "called with nullptr");
494 deb <<
"Calculating native pixels: " << value << window;
495 return QHighDpi::toNativePixels<QRect, QWindow>(value, window);
499
500
501
502qreal QtGuiTest::scaleFactor(
const QWindow *window)
506 "called with nullptr");
508 deb <<
"Calculating scaling factor: " << window;
509 return QHighDpiScaling::factor(window);
513
514
515
516void QtGuiTest::setEventPointId(QEventPoint &p,
int arg)
518 QMutableEventPoint::setId(p, arg);
522
523
524
525void QtGuiTest::setEventPointPressure(QEventPoint &p, qreal arg)
527 QMutableEventPoint::setPressure(p, arg);
531
532
533
534void QtGuiTest::setEventPointState(QEventPoint &p, QEventPoint::State arg)
536 QMutableEventPoint::setState(p, arg);
540
541
542
543void QtGuiTest::setEventPointPosition(QEventPoint &p, QPointF arg)
545 QMutableEventPoint::setPosition(p, arg);
549
550
551
552void QtGuiTest::setEventPointGlobalPosition(QEventPoint &p, QPointF arg)
554 QMutableEventPoint::setGlobalPosition(p, arg);
558
559
560
561void QtGuiTest::setEventPointScenePosition(QEventPoint &p, QPointF arg)
563 QMutableEventPoint::setScenePosition(p, arg);
567
568
569
570void QtGuiTest::setEventPointEllipseDiameters(QEventPoint &p, QSizeF arg)
572 QMutableEventPoint::setEllipseDiameters(p, arg);
576
577
578
579
580bool QtGuiTest::platformSupportsMultipleWindows()
582 const auto *platformIntegration = QGuiApplicationPrivate::platformIntegration();
583 return platformIntegration->hasCapability(QPlatformIntegration::Capability::MultipleWindows);
Q_GUI_EXPORT bool qWaitForWindowExposed(QWindow *window)
Q_GUI_EXPORT bool qWaitForWindowFocused(QWindow *window)
Q_GUI_EXPORT bool qWaitForWindowActive(QWindow *window, int timeout)
Q_GUI_EXPORT bool qWaitForWindowFocused(QWindow *window, QDeadlineTimer timeout)
Q_GUI_EXPORT bool qWaitForWindowExposed(QWindow *window, int timeout)
Q_GUI_EXPORT bool qWaitForWindowActive(QWindow *window)