summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTor Arne Vestbø <[email protected]>2023-12-08 14:44:10 +0100
committerTor Arne Vestbø <[email protected]>2023-12-09 01:37:44 +0100
commit9eb06a2848257d054447777c645231afa87429c2 (patch)
tree41ebb71878e2dd30f9b577da3d345b05d20ab415
parent311f8896322bcd39d33369c8311a8c89ccdad449 (diff)
Add QTest::qWaitForWindowFocused for checking focus window
As opposed to QTest::qWaitForWindowActive, which checks if the window is active, which is not a guarantee that the window has focus. Task-number: QTBUG-119287 Change-Id: I9fe65b0474095389f6518ebaaf07c71143b6f459 Reviewed-by: Volker Hilsheimer <[email protected]>
-rw-r--r--src/gui/kernel/qtestsupport_gui.cpp23
-rw-r--r--src/gui/kernel/qtestsupport_gui.h1
-rw-r--r--src/widgets/kernel/qtestsupport_widgets.cpp26
-rw-r--r--src/widgets/kernel/qtestsupport_widgets.h1
4 files changed, 50 insertions, 1 deletions
diff --git a/src/gui/kernel/qtestsupport_gui.cpp b/src/gui/kernel/qtestsupport_gui.cpp
index ac58ab449c8..b43f7ab163e 100644
--- a/src/gui/kernel/qtestsupport_gui.cpp
+++ b/src/gui/kernel/qtestsupport_gui.cpp
@@ -29,7 +29,7 @@ QT_BEGIN_NAMESPACE
\note Since focus is an exclusive property, \a window may loose its focus to another window at
any time - even after the method has returned \c true.
- \sa qWaitForWindowExposed(), QWindow::isActive()
+ \sa qWaitForWindowExposed(), qWaitForWindowFocused(), QWindow::isActive()
*/
Q_GUI_EXPORT bool QTest::qWaitForWindowActive(QWindow *window, int timeout)
{
@@ -45,6 +45,27 @@ 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.
+
+ 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.
+
+ \note The method will time out and return \c false if another window prevents \a window from
+ becoming focused.
+
+ \note Since focus is an exclusive property, \a window may loose its focus to another window at
+ any time - even after the method has returned \c true.
+
+ \sa qWaitForWindowExposed(), qWaitForWindowActive(), QGuiApplication::focusWindow()
+*/
+Q_GUI_EXPORT bool QTest::qWaitForWindowFocused(QWindow *window, int timeout)
+{
+ return QTest::qWaitFor([&]() { return qGuiApp->focusWindow() == window; }, timeout);
+}
+
+/*!
\since 5.0
Returns \c true, if \a window is exposed within \a timeout milliseconds. Otherwise returns \c false.
diff --git a/src/gui/kernel/qtestsupport_gui.h b/src/gui/kernel/qtestsupport_gui.h
index e93fd520188..e676324bb0e 100644
--- a/src/gui/kernel/qtestsupport_gui.h
+++ b/src/gui/kernel/qtestsupport_gui.h
@@ -23,6 +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 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 3aa13b24167..226c56140d9 100644
--- a/src/widgets/kernel/qtestsupport_widgets.cpp
+++ b/src/widgets/kernel/qtestsupport_widgets.cpp
@@ -60,6 +60,32 @@ Q_WIDGETS_EXPORT bool QTest::qWaitForWindowActive(QWidget *widget, int timeout)
timeout);
}
+
+/*!
+ \since 6.7
+
+ Returns \c true, if \a widget is the focus window within \a timeout milliseconds. 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.
+
+ \sa qWaitForWindowExposed(), qWaitForWindowActive(), QGuiApplication::focusWindow()
+*/
+Q_WIDGETS_EXPORT bool QTest::qWaitForWindowFocused(QWidget *widget, int timeout)
+{
+ return qWaitForWidgetWindow([&]() {
+ return widget->window()->windowHandle();
+ }, [&](QWindow *window) {
+ return qGuiApp->focusWindow() == window;
+ }, timeout);
+}
+
/*!
\since 5.0
diff --git a/src/widgets/kernel/qtestsupport_widgets.h b/src/widgets/kernel/qtestsupport_widgets.h
index 84b529ae316..cb8ab21069a 100644
--- a/src/widgets/kernel/qtestsupport_widgets.h
+++ b/src/widgets/kernel/qtestsupport_widgets.h
@@ -15,6 +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 qWaitForWindowExposed(QWidget *widget, int timeout = 5000);
class Q_WIDGETS_EXPORT QTouchEventWidgetSequence : public QTouchEventSequence