5#include <private/qguiapplication_p.h>
6#include <private/qeventpoint_p.h>
8#include <qpa/qplatformintegration.h>
13#include <QtCore/qtestsupport_core.h>
14#include <QtCore/qthread.h>
15#include <QtCore/QDebug>
17#if QT_CONFIG(test_gui)
18#include <QtCore/qloggingcategory.h>
19#include <private/qinputdevicemanager_p.h>
20#include <private/qeventpoint_p.h>
21#include <private/qhighdpiscaling_p.h>
27
28
29
30
31
34 return qWaitForWindowActive(window, QDeadlineTimer{timeout, Qt::TimerType::PreciseTimer});
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
55 using Internal::WaitForResult;
56 if (Q_UNLIKELY(!QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowActivation))) {
57 qWarning() <<
"qWaitForWindowActive was called on a platform that doesn't support window"
58 <<
"activation. This means there is an error in the test and it should either"
59 <<
"check for the WindowActivation platform capability before calling"
60 <<
"qWaitForWindowActivate, use qWaitForWindowExposed instead, or skip the test."
61 <<
"Falling back to qWaitForWindowExposed.";
62 return qWaitForWindowExposed(window, timeout);
64 return QTest::qWaitFor([wp = QPointer(window)]() {
65 if (QWindow *w = wp.data(); !w)
66 return WaitForResult::Failed;
68 return w->isActive() ? WaitForResult::Done : WaitForResult::NotYet;
73
74
75
76
77
80 return qWaitForWindowActive(window, defaultTryTimeout.load(std::memory_order_relaxed));
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
101 using Internal::WaitForResult;
102 return QTest::qWaitFor([wp = QPointer(window)]() {
103 if (QWindow *w = wp.data(); !w)
104 return WaitForResult::Failed;
106 return qGuiApp->focusWindow() == w ? WaitForResult::Done : WaitForResult::NotYet;
111
112
113
114
115
118 return qWaitForWindowFocused(window, defaultTryTimeout.load(std::memory_order_relaxed));
122
123
124
125
126
129 return qWaitForWindowExposed(window, std::chrono::milliseconds(timeout));
133
134
135
136
137
138
139
140
141
142
143
144
145
148 using Internal::WaitForResult;
149 return QTest::qWaitFor([wp = QPointer(window)]() {
150 if (QWindow *w = wp.data(); !w)
151 return WaitForResult::Failed;
153 return w->isExposed() ? WaitForResult::Done : WaitForResult::NotYet;
158
159
160
161
162
165 return qWaitForWindowExposed(window, defaultTryTimeout.load(std::memory_order_relaxed));
260#if QT_CONFIG(test_gui)
261Q_STATIC_LOGGING_CATEGORY(lcQtGuiTest,
"qt.gui.test");
262#define deb qCDebug(lcQtGuiTest)
265
266
267
268
269static QInputDeviceManager *inputDeviceManager()
271 if (
auto *idm = QGuiApplicationPrivate::inputDeviceManager())
274 deb <<
"No input device manager present.";
279
280
281
282
283void QtGuiTest::setKeyboardModifiers(Qt::KeyboardModifiers modifiers)
285 auto *idm = inputDeviceManager();
286 if (Q_UNLIKELY(!idm))
289 idm->setKeyboardModifiers(modifiers);
290 deb <<
"Keyboard modifiers synthesized:" << modifiers;
294
295
296
297
298void QtGuiTest::setCursorPosition(
const QPoint &position)
300 auto *idm = inputDeviceManager();
301 if (Q_UNLIKELY(!idm))
304 idm->setCursorPos(position);
305 deb <<
"Mouse curser set to" << position;
309
310
311
312
313
314
315
316
317
318void QtGuiTest::synthesizeExtendedKeyEvent(QEvent::Type type,
int key, Qt::KeyboardModifiers modifiers,
319 quint32 nativeScanCode, quint32 nativeVirtualKey,
322 Q_ASSERT_X((type == QEvent::KeyPress
323 || type == QEvent::KeyRelease),
325 "called with invalid QEvent type");
327 deb <<
"Synthesizing key event:" << type << Qt::Key(key) << modifiers << text;
329 if (QWindowSystemInterface::handleExtendedKeyEvent(
nullptr, type, key, modifiers,
330 nativeScanCode, nativeVirtualKey,
331 modifiers, text,
false,
336 QCoreApplication::sendPostedEvents();
344
345
346
347
348
349
350
351
352
353bool QtGuiTest::synthesizeKeyEvent(QWindow *window, QEvent::Type t,
int k, Qt::KeyboardModifiers mods,
354 const QString & text,
bool autorep,
357 Q_ASSERT_X((t == QEvent::KeyPress
358 || t == QEvent::KeyRelease),
360 "called with invalid QEvent type");
362 deb <<
"Synthesizing key event:" << t << Qt::Key(k) << mods << text;
364 bool result = QWindowSystemInterface::handleKeyEvent(window, t, k, mods, text, autorep, count);
368 QCoreApplication::sendPostedEvents();
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392void QtGuiTest::synthesizeMouseEvent(
const QPointF &position, Qt::MouseButtons state,
393 Qt::MouseButton button, QEvent::Type type,
394 Qt::KeyboardModifiers modifiers)
396 Q_ASSERT_X((type == QEvent::MouseButtonPress
397 || type == QEvent::MouseButtonRelease
398 || type == QEvent::MouseMove),
400 "called with invalid QEvent type");
402 deb <<
"Synthesizing mouse event:" << type << position << button << modifiers;
404 if (QWindowSystemInterface::handleMouseEvent(
nullptr, position, position, state, button,
405 type, modifiers, Qt::MouseEventNotSynthesized)) {
408 QCoreApplication::processEvents();
409 QCoreApplication::sendPostedEvents();
418
419
420
421
422
423
424
425
426void QtGuiTest::synthesizeWheelEvent(
int rollCount, Qt::KeyboardModifiers modifiers)
428 deb <<
"Synthesizing wheel event:" << rollCount << modifiers;
430 QPoint position = QCursor::pos();
431 if (QWindowSystemInterface::handleWheelEvent(
nullptr, position, position,
432 QPoint(), QPoint(0, -rollCount), modifiers)) {
436 QCoreApplication::sendPostedEvents();
444
445
446
447
448qint64 QtGuiTest::eventTimeElapsed()
450 return QWindowSystemInterfacePrivate::eventTime.elapsed();
454
455
456
457
458void QtGuiTest::postFakeWindowActivation(QWindow *window)
462 "called with nullptr");
464 deb <<
"Posting fake window activation:" << window;
466 QWindowSystemInterfacePrivate::FocusWindowEvent e(window, Qt::OtherFocusReason);
467 QGuiApplicationPrivate::processWindowSystemEvent(&e);
468 QWindowSystemInterface::handleFocusWindowChanged(window);
472
473
474
475QPoint QtGuiTest::toNativePixels(
const QPoint &value,
const QWindow *window)
479 "called with nullptr");
481 deb <<
"Calculating native pixels: " << value << window;
482 return QHighDpi::toNativePixels<QPoint, QWindow>(value, window);
486
487
488
489QRect QtGuiTest::toNativePixels(
const QRect &value,
const QWindow *window)
493 "called with nullptr");
495 deb <<
"Calculating native pixels: " << value << window;
496 return QHighDpi::toNativePixels<QRect, QWindow>(value, window);
500
501
502
503qreal QtGuiTest::scaleFactor(
const QWindow *window)
507 "called with nullptr");
509 deb <<
"Calculating scaling factor: " << window;
510 return QHighDpiScaling::factor(window);
514
515
516
517void QtGuiTest::setEventPointId(QEventPoint &p,
int arg)
519 QMutableEventPoint::setId(p, arg);
523
524
525
526void QtGuiTest::setEventPointPressure(QEventPoint &p, qreal arg)
528 QMutableEventPoint::setPressure(p, arg);
532
533
534
535void QtGuiTest::setEventPointState(QEventPoint &p, QEventPoint::State arg)
537 QMutableEventPoint::setState(p, arg);
541
542
543
544void QtGuiTest::setEventPointPosition(QEventPoint &p, QPointF arg)
546 QMutableEventPoint::setPosition(p, arg);
550
551
552
553void QtGuiTest::setEventPointGlobalPosition(QEventPoint &p, QPointF arg)
555 QMutableEventPoint::setGlobalPosition(p, arg);
559
560
561
562void QtGuiTest::setEventPointScenePosition(QEventPoint &p, QPointF arg)
564 QMutableEventPoint::setScenePosition(p, arg);
568
569
570
571void QtGuiTest::setEventPointEllipseDiameters(QEventPoint &p, QSizeF arg)
573 QMutableEventPoint::setEllipseDiameters(p, arg);
577
578
579
580
581bool QtGuiTest::platformSupportsMultipleWindows()
583 const auto *platformIntegration = QGuiApplicationPrivate::platformIntegration();
584 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)