summaryrefslogtreecommitdiffstats
path: root/src/widgets/kernel/qtestsupport_widgets.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/widgets/kernel/qtestsupport_widgets.cpp')
-rw-r--r--src/widgets/kernel/qtestsupport_widgets.cpp73
1 files changed, 52 insertions, 21 deletions
diff --git a/src/widgets/kernel/qtestsupport_widgets.cpp b/src/widgets/kernel/qtestsupport_widgets.cpp
index 490a40d431..f7b25b6643 100644
--- a/src/widgets/kernel/qtestsupport_widgets.cpp
+++ b/src/widgets/kernel/qtestsupport_widgets.cpp
@@ -16,8 +16,8 @@
QT_BEGIN_NAMESPACE
-template <typename FunctorWindowGetter, typename FunctorPredicate>
-static bool qWaitForWidgetWindow(FunctorWindowGetter windowGetter, FunctorPredicate predicate, int timeout)
+template <typename FunctorWindowGetter, typename FunctorPredicate, typename Timeout>
+static bool qWaitForWidgetWindow(FunctorWindowGetter windowGetter, FunctorPredicate predicate, Timeout timeout)
{
if (!windowGetter())
return false;
@@ -32,9 +32,16 @@ static bool qWaitForWidgetWindow(FunctorWindowGetter windowGetter, FunctorPredic
/*!
\since 5.0
- Waits for \a timeout milliseconds or until the \a widget's window is active.
+ Returns \c true if \a widget is active within \a timeout milliseconds. Otherwise returns \c false.
- Returns \c true if \c widget's window is active within \a timeout milliseconds, otherwise returns \c false.
+ The method is useful in tests that call QWidget::show() and rely on the widget actually being
+ active (i.e. being visible and having focus) before proceeding.
+
+ \note The method will time out and return \c false if another window prevents \a widget from
+ becoming active.
+
+ \note Since focus is an exclusive property, \a widget may loose its focus to another window at
+ any time - even after the method has returned \c true.
\sa qWaitForWindowExposed(), QWidget::isActiveWindow()
*/
@@ -53,23 +60,45 @@ Q_WIDGETS_EXPORT bool QTest::qWaitForWindowActive(QWidget *widget, int timeout)
timeout);
}
+
/*!
- \since 5.0
+ \since 6.7
+
+ Returns \c true, if \a widget is the focus window within \a timeout. Otherwise returns \c false.
+
+ The method is useful in tests that call QWidget::show() and rely on the widget
+ having focus (for receiving keyboard events e.g.) before proceeding.
+
+ \note The method will time out and return \c false if another window prevents \a widget from
+ becoming focused.
+
+ \note Since focus is an exclusive property, \a widget may loose its focus to another window at
+ any time - even after the method has returned \c true.
- Waits for \a timeout milliseconds or until the \a widget's window is exposed.
- Returns \c true if \c widget's window is exposed within \a timeout milliseconds, otherwise returns \c false.
+ \sa qWaitForWindowExposed(), qWaitForWindowActive(), QGuiApplication::focusWindow()
+*/
+Q_WIDGETS_EXPORT bool QTest::qWaitForWindowFocused(QWidget *widget, QDeadlineTimer timeout)
+{
+ return qWaitForWidgetWindow([&]() {
+ return widget->window()->windowHandle();
+ }, [&](QWindow *window) {
+ return qGuiApp->focusWindow() == window;
+ }, timeout);
+}
+
+/*!
+ \since 5.0
- This is mainly useful for asynchronous systems like X11, where a window will be mapped to screen some
- time after being asked to show itself on the screen.
+ Returns \c true if \a widget is exposed within \a timeout milliseconds. Otherwise returns \c false.
- Note that a window that is mapped to screen may still not be considered exposed if the window client
- area is completely covered by other windows, or if the window is otherwise not visible. This function
- will then time out when waiting for such a window.
+ The method is useful in tests that call QWidget::show() and rely on the widget actually being
+ being visible before proceeding.
- A specific configuration where this happens is when using QGLWidget as a viewport widget on macOS:
- The viewport widget gets the expose event, not the parent widget.
+ \note A window mapped to screen may still not be considered exposed, if the window client area is
+ not visible, e.g. because it is completely covered by other windows.
+ In such cases, the method will time out and return \c false.
- \sa qWaitForWindowActive()
+ \sa qWaitForWindowActive(), QWidget::isVisible(), QWindow::isExposed()
*/
Q_WIDGETS_EXPORT bool QTest::qWaitForWindowExposed(QWidget *widget, int timeout)
{
@@ -83,7 +112,7 @@ namespace QTest {
QTouchEventWidgetSequence::~QTouchEventWidgetSequence()
{
if (commitWhenDestroyed)
- commit();
+ QTouchEventWidgetSequence::commit();
}
QTouchEventWidgetSequence& QTouchEventWidgetSequence::press(int touchId, const QPoint &pt, QWidget *widget)
@@ -115,20 +144,22 @@ QTouchEventWidgetSequence& QTouchEventWidgetSequence::stationary(int touchId)
return *this;
}
-void QTouchEventWidgetSequence::commit(bool processEvents)
+bool QTouchEventWidgetSequence::commit(bool processEvents)
{
+ bool ret = false;
if (points.isEmpty())
- return;
- QThread::msleep(1);
+ return ret;
+ QThread::sleep(std::chrono::milliseconds{1});
if (targetWindow) {
- qt_handleTouchEvent(targetWindow, device, points.values());
+ ret = qt_handleTouchEventv2(targetWindow, device, points.values());
} else if (targetWidget) {
- qt_handleTouchEvent(targetWidget->windowHandle(), device, points.values());
+ ret = qt_handleTouchEventv2(targetWidget->windowHandle(), device, points.values());
}
if (processEvents)
QCoreApplication::processEvents();
previousPoints = points;
points.clear();
+ return ret;
}
QTest::QTouchEventWidgetSequence::QTouchEventWidgetSequence(QWidget *widget, QPointingDevice *aDevice, bool autoCommit)