From dbda879127a86d515a03fd4ea323883c4f0dffb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Thu, 25 Jan 2024 13:35:28 +0100 Subject: [PATCH] qWaitForWindowFocused: Pass timeout as QDeadlineTimer We should avoid int-based timeouts nowadays, and prefer std::chrono, or for timeouts where Forever is a valid state, QDeadlineTimer. Discovered during API header review. Change-Id: Ia56a67084c7a2f989951755fed5ffc161ed8f79e Reviewed-by: Marc Mutz (cherry picked from commit 3f2a9523a442e44ef52ebca30da9b5d3188df6bc) Reviewed-by: Qt Cherry-pick Bot --- src/gui/kernel/qtestsupport_gui.cpp | 4 ++-- src/gui/kernel/qtestsupport_gui.h | 2 +- src/widgets/kernel/qtestsupport_widgets.cpp | 8 ++++---- src/widgets/kernel/qtestsupport_widgets.h | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/gui/kernel/qtestsupport_gui.cpp b/src/gui/kernel/qtestsupport_gui.cpp index b43f7ab163e..869eddce494 100644 --- a/src/gui/kernel/qtestsupport_gui.cpp +++ b/src/gui/kernel/qtestsupport_gui.cpp @@ -47,7 +47,7 @@ Q_GUI_EXPORT bool QTest::qWaitForWindowActive(QWindow *window, int timeout) /*! \since 6.7 - Returns \c true, if \a window is the focus window within \a timeout milliseconds. Otherwise returns \c false. + Returns \c true, if \a window is the focus window within \a timeout. Otherwise returns \c false. The method is useful in tests that call QWindow::show() and rely on the window having focus (for receiving keyboard events e.g.) before proceeding. @@ -60,7 +60,7 @@ Q_GUI_EXPORT bool QTest::qWaitForWindowActive(QWindow *window, int timeout) \sa qWaitForWindowExposed(), qWaitForWindowActive(), QGuiApplication::focusWindow() */ -Q_GUI_EXPORT bool QTest::qWaitForWindowFocused(QWindow *window, int timeout) +Q_GUI_EXPORT bool QTest::qWaitForWindowFocused(QWindow *window, QDeadlineTimer timeout) { return QTest::qWaitFor([&]() { return qGuiApp->focusWindow() == window; }, timeout); } diff --git a/src/gui/kernel/qtestsupport_gui.h b/src/gui/kernel/qtestsupport_gui.h index e676324bb0e..e5b2a884558 100644 --- a/src/gui/kernel/qtestsupport_gui.h +++ b/src/gui/kernel/qtestsupport_gui.h @@ -23,7 +23,7 @@ Q_GUI_EXPORT bool qt_handleTouchEventv2(QWindow *w, const QPointingDevice *devic namespace QTest { [[nodiscard]] Q_GUI_EXPORT bool qWaitForWindowActive(QWindow *window, int timeout = 5000); -[[nodiscard]] Q_GUI_EXPORT bool qWaitForWindowFocused(QWindow *window, int timeout = 5000); +[[nodiscard]] Q_GUI_EXPORT bool qWaitForWindowFocused(QWindow *widget, QDeadlineTimer timeout = std::chrono::seconds{5}); [[nodiscard]] Q_GUI_EXPORT bool qWaitForWindowExposed(QWindow *window, int timeout = 5000); Q_GUI_EXPORT QPointingDevice * createTouchDevice(QInputDevice::DeviceType devType = QInputDevice::DeviceType::TouchScreen, diff --git a/src/widgets/kernel/qtestsupport_widgets.cpp b/src/widgets/kernel/qtestsupport_widgets.cpp index 226c56140d9..f7b25b6643b 100644 --- a/src/widgets/kernel/qtestsupport_widgets.cpp +++ b/src/widgets/kernel/qtestsupport_widgets.cpp @@ -16,8 +16,8 @@ QT_BEGIN_NAMESPACE -template -static bool qWaitForWidgetWindow(FunctorWindowGetter windowGetter, FunctorPredicate predicate, int timeout) +template +static bool qWaitForWidgetWindow(FunctorWindowGetter windowGetter, FunctorPredicate predicate, Timeout timeout) { if (!windowGetter()) return false; @@ -64,7 +64,7 @@ Q_WIDGETS_EXPORT bool QTest::qWaitForWindowActive(QWidget *widget, int timeout) /*! \since 6.7 - Returns \c true, if \a widget is the focus window within \a timeout milliseconds. Otherwise returns \c false. + 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. @@ -77,7 +77,7 @@ Q_WIDGETS_EXPORT bool QTest::qWaitForWindowActive(QWidget *widget, int timeout) \sa qWaitForWindowExposed(), qWaitForWindowActive(), QGuiApplication::focusWindow() */ -Q_WIDGETS_EXPORT bool QTest::qWaitForWindowFocused(QWidget *widget, int timeout) +Q_WIDGETS_EXPORT bool QTest::qWaitForWindowFocused(QWidget *widget, QDeadlineTimer timeout) { return qWaitForWidgetWindow([&]() { return widget->window()->windowHandle(); diff --git a/src/widgets/kernel/qtestsupport_widgets.h b/src/widgets/kernel/qtestsupport_widgets.h index cb8ab21069a..b49e68db651 100644 --- a/src/widgets/kernel/qtestsupport_widgets.h +++ b/src/widgets/kernel/qtestsupport_widgets.h @@ -15,7 +15,7 @@ class QWidget; namespace QTest { [[nodiscard]] Q_WIDGETS_EXPORT bool qWaitForWindowActive(QWidget *widget, int timeout = 5000); -[[nodiscard]] Q_WIDGETS_EXPORT bool qWaitForWindowFocused(QWidget *widget, int timeout = 5000); +[[nodiscard]] Q_WIDGETS_EXPORT bool qWaitForWindowFocused(QWidget *widget, QDeadlineTimer timeout = std::chrono::seconds{5}); [[nodiscard]] Q_WIDGETS_EXPORT bool qWaitForWindowExposed(QWidget *widget, int timeout = 5000); class Q_WIDGETS_EXPORT QTouchEventWidgetSequence : public QTouchEventSequence