5#include <private/qguiapplication_p.h>
6#include <private/qeventpoint_p.h>
8#include <qpa/qplatformintegration.h>
9#include <qpa/qwindowsysteminterface.h>
14#include <QtCore/qtestsupport_core.h>
15#include <QtCore/qthread.h>
16#include <QtCore/QDebug>
18#if QT_CONFIG(test_gui)
19#include <QtCore/qloggingcategory.h>
20#include <private/qinputdevicemanager_p.h>
21#include <private/qeventpoint_p.h>
22#include <private/qhighdpiscaling_p.h>
28
29
30
31
32
35 return qWaitForWindowActive(window, QDeadlineTimer{timeout, Qt::TimerType::PreciseTimer});
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
56 using Internal::WaitForResult;
57 if (Q_UNLIKELY(!QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowActivation))) {
58 qWarning() <<
"qWaitForWindowActive was called on a platform that doesn't support window"
59 <<
"activation. This means there is an error in the test and it should either"
60 <<
"check for the WindowActivation platform capability before calling"
61 <<
"qWaitForWindowActivate, use qWaitForWindowExposed instead, or skip the test."
62 <<
"Falling back to qWaitForWindowExposed.";
63 return qWaitForWindowExposed(window, timeout);
65 return QTest::qWaitFor([wp = QPointer(window)]() {
66 if (QWindow *w = wp.data(); !w)
67 return WaitForResult::Failed;
69 return w->isActive() ? WaitForResult::Done : WaitForResult::NotYet;
74
75
76
77
78
81 return qWaitForWindowActive(window, defaultTryTimeout.load(std::memory_order_relaxed));
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
102 using Internal::WaitForResult;
103 return QTest::qWaitFor([wp = QPointer(window)]() {
104 if (QWindow *w = wp.data(); !w)
105 return WaitForResult::Failed;
107 return qGuiApp->focusWindow() == w ? WaitForResult::Done : WaitForResult::NotYet;
112
113
114
115
116
119 return qWaitForWindowFocused(window, defaultTryTimeout.load(std::memory_order_relaxed));
123
124
125
126
127
130 return qWaitForWindowExposed(window, std::chrono::milliseconds(timeout));
134
135
136
137
138
139
140
141
142
143
144
145
146
149 using Internal::WaitForResult;
150 return QTest::qWaitFor([wp = QPointer(window)]() {
151 if (QWindow *w = wp.data(); !w)
152 return WaitForResult::Failed;
154 return w->isExposed() ? WaitForResult::Done : WaitForResult::NotYet;
159
160
161
162
163
166 return qWaitForWindowExposed(window, defaultTryTimeout.load(std::memory_order_relaxed));
277#if QT_CONFIG(test_gui)
278Q_STATIC_LOGGING_CATEGORY(lcQtGuiTest,
"qt.gui.test");
279#define deb qCDebug(lcQtGuiTest)
282
283
284
285
286static QInputDeviceManager *inputDeviceManager()
288 if (
auto *idm = QGuiApplicationPrivate::inputDeviceManager())
291 deb <<
"No input device manager present.";
296
297
298
299
300void QtGuiTest::setKeyboardModifiers(Qt::KeyboardModifiers modifiers)
302 auto *idm = inputDeviceManager();
303 if (Q_UNLIKELY(!idm))
306 idm->setKeyboardModifiers(modifiers);
307 deb <<
"Keyboard modifiers synthesized:" << modifiers;
311
312
313
314
315void QtGuiTest::setCursorPosition(
const QPoint &position)
317 auto *idm = inputDeviceManager();
318 if (Q_UNLIKELY(!idm))
321 idm->setCursorPos(position);
322 deb <<
"Mouse curser set to" << position;
326
327
328
329
330
331
332
333
334
335void QtGuiTest::synthesizeExtendedKeyEvent(QEvent::Type type,
int key, Qt::KeyboardModifiers modifiers,
336 quint32 nativeScanCode, quint32 nativeVirtualKey,
339 Q_ASSERT_X((type == QEvent::KeyPress
340 || type == QEvent::KeyRelease),
342 "called with invalid QEvent type");
344 deb <<
"Synthesizing key event:" << type << Qt::Key(key) << modifiers << text;
346 if (QWindowSystemInterface::handleExtendedKeyEvent(
nullptr, type, key, modifiers,
347 nativeScanCode, nativeVirtualKey,
348 modifiers, text,
false,
353 QCoreApplication::sendPostedEvents();
361
362
363
364
365
366
367
368
369
370bool QtGuiTest::synthesizeKeyEvent(QWindow *window, QEvent::Type t,
int k, Qt::KeyboardModifiers mods,
371 const QString & text,
bool autorep,
374 Q_ASSERT_X((t == QEvent::KeyPress
375 || t == QEvent::KeyRelease),
377 "called with invalid QEvent type");
379 deb <<
"Synthesizing key event:" << t << Qt::Key(k) << mods << text;
381 bool result = QWindowSystemInterface::handleKeyEvent(window, t, k, mods, text, autorep, count);
385 QCoreApplication::sendPostedEvents();
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409void QtGuiTest::synthesizeMouseEvent(
const QPointF &position, Qt::MouseButtons state,
410 Qt::MouseButton button, QEvent::Type type,
411 Qt::KeyboardModifiers modifiers)
413 Q_ASSERT_X((type == QEvent::MouseButtonPress
414 || type == QEvent::MouseButtonRelease
415 || type == QEvent::MouseMove),
417 "called with invalid QEvent type");
419 deb <<
"Synthesizing mouse event:" << type << position << button << modifiers;
421 if (QWindowSystemInterface::handleMouseEvent(
nullptr, position, position, state, button,
422 type, modifiers, Qt::MouseEventNotSynthesized)) {
425 QCoreApplication::processEvents();
426 QCoreApplication::sendPostedEvents();
435
436
437
438
439
440
441
442
443void QtGuiTest::synthesizeWheelEvent(
int rollCount, Qt::KeyboardModifiers modifiers)
445 deb <<
"Synthesizing wheel event:" << rollCount << modifiers;
447 QPoint position = QCursor::pos();
448 if (QWindowSystemInterface::handleWheelEvent(
nullptr, position, position,
449 QPoint(), QPoint(0, -rollCount), modifiers)) {
453 QCoreApplication::sendPostedEvents();
461
462
463
464
465qint64 QtGuiTest::eventTimeElapsed()
467 return QWindowSystemInterfacePrivate::eventTime.elapsed();
471
472
473
474
475void QtGuiTest::postFakeWindowActivation(QWindow *window)
479 "called with nullptr");
481 deb <<
"Posting fake window activation:" << window;
483 QWindowSystemInterfacePrivate::FocusWindowEvent e(window, Qt::OtherFocusReason);
484 QGuiApplicationPrivate::processWindowSystemEvent(&e);
485 QWindowSystemInterface::handleFocusWindowChanged(window);
489
490
491
492QPoint QtGuiTest::toNativePixels(
const QPoint &value,
const QWindow *window)
496 "called with nullptr");
498 deb <<
"Calculating native pixels: " << value << window;
499 return QHighDpi::toNativePixels<QPoint, QWindow>(value, window);
503
504
505
506QRect QtGuiTest::toNativePixels(
const QRect &value,
const QWindow *window)
510 "called with nullptr");
512 deb <<
"Calculating native pixels: " << value << window;
513 return QHighDpi::toNativePixels<QRect, QWindow>(value, window);
517
518
519
520qreal QtGuiTest::scaleFactor(
const QWindow *window)
524 "called with nullptr");
526 deb <<
"Calculating scaling factor: " << window;
527 return QHighDpiScaling::factor(window);
531
532
533
534void QtGuiTest::setEventPointId(QEventPoint &p,
int arg)
536 QMutableEventPoint::setId(p, arg);
540
541
542
543void QtGuiTest::setEventPointPressure(QEventPoint &p, qreal arg)
545 QMutableEventPoint::setPressure(p, arg);
549
550
551
552void QtGuiTest::setEventPointState(QEventPoint &p, QEventPoint::State arg)
554 QMutableEventPoint::setState(p, arg);
558
559
560
561void QtGuiTest::setEventPointPosition(QEventPoint &p, QPointF arg)
563 QMutableEventPoint::setPosition(p, arg);
567
568
569
570void QtGuiTest::setEventPointGlobalPosition(QEventPoint &p, QPointF arg)
572 QMutableEventPoint::setGlobalPosition(p, arg);
576
577
578
579void QtGuiTest::setEventPointScenePosition(QEventPoint &p, QPointF arg)
581 QMutableEventPoint::setScenePosition(p, arg);
585
586
587
588void QtGuiTest::setEventPointEllipseDiameters(QEventPoint &p, QSizeF arg)
590 QMutableEventPoint::setEllipseDiameters(p, arg);
594
595
596
597
598bool QtGuiTest::platformSupportsMultipleWindows()
600 const auto *platformIntegration = QGuiApplicationPrivate::platformIntegration();
601 return platformIntegration->hasCapability(QPlatformIntegration::Capability::MultipleWindows);
Combined button and popup list for selecting options.
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)