Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qtestsupport_widgets.cpp
Go to the documentation of this file.
1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
6
7#include "qwidget.h"
8
9#include <QtGui/qwindow.h>
10#include <QtCore/qtestsupport_core.h>
11#include <QtCore/qthread.h>
12#include <QtGui/qtestsupport_gui.h>
13#include <QtGui/private/qevent_p.h>
14#include <QtGui/private/qeventpoint_p.h>
15#include <private/qguiapplication_p.h>
16#include <qpa/qplatformintegration.h>
17
19
20template <typename Predicate>
21static bool qWaitForWidgetWindow(QWidget *w, Predicate predicate, QDeadlineTimer timeout)
22{
23 if (!w->window()->windowHandle())
24 return false;
25
26 return QTest::qWaitFor([&, wp = QPointer(w)]() {
27 using QTest::Internal::WaitForResult;
28 if (QWidget *widget = wp.data(); !widget)
29 return WaitForResult::Failed;
30 else if (QWindow *window = widget->window()->windowHandle(); window && predicate(window))
31 return WaitForResult::Done;
32 return WaitForResult::NotYet;
33 }, timeout);
34}
35
36/*!
37 \since 5.0
38 \overload
39
40 The \a timeout is in milliseconds.
41*/
42bool QTest::qWaitForWindowActive(QWidget *widget, int timeout)
43{
44 return qWaitForWindowActive(widget, QDeadlineTimer{timeout, Qt::TimerType::PreciseTimer});
45}
46
47/*!
48 \since 6.10
49
50 Returns \c true if \a widget is active within \a timeout milliseconds. Otherwise returns \c false.
51
52 The method is useful in tests that call QWidget::show() and rely on the widget actually being
53 active (i.e. being visible and having focus) before proceeding.
54
55 \note The method will time out and return \c false if another window prevents \a widget from
56 becoming active.
57
58 \note Since focus is an exclusive property, \a widget may loose its focus to another window at
59 any time - even after the method has returned \c true.
60
61 \sa qWaitForWindowExposed(), QWidget::isActiveWindow()
62*/
63bool QTest::qWaitForWindowActive(QWidget *widget, QDeadlineTimer timeout)
64{
65 if (Q_UNLIKELY(!QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowActivation))) {
66 qWarning() << "qWaitForWindowActive was called on a platform that doesn't support window"
67 << "activation. This means there is an error in the test and it should either"
68 << "check for the WindowActivation platform capability before calling"
69 << "qWaitForWindowActivate, use qWaitForWindowExposed instead, or skip the test."
70 << "Falling back to qWaitForWindowExposed.";
71 return qWaitForWindowExposed(widget, timeout);
72 }
73 return qWaitForWidgetWindow(widget,
74 [&](QWindow *window) { return window->isActive(); },
75 timeout);
76}
77
78/*!
79 \since 6.10
80 \overload
81
82 This function uses the default timeout of 5 seconds.
83*/
85{
86 return qWaitForWindowActive(widget, defaultTryTimeout.load(std::memory_order_relaxed));
87}
88
89/*!
90 \since 6.7
91
92 Returns \c true, if \a widget is the focus window within \a timeout. Otherwise returns \c false.
93
94 The method is useful in tests that call QWidget::show() and rely on the widget
95 having focus (for receiving keyboard events e.g.) before proceeding.
96
97 \note The method will time out and return \c false if another window prevents \a widget from
98 becoming focused.
99
100 \note Since focus is an exclusive property, \a widget may loose its focus to another window at
101 any time - even after the method has returned \c true.
102
103 \sa qWaitForWindowExposed(), qWaitForWindowActive(), QGuiApplication::focusWindow()
104*/
105Q_WIDGETS_EXPORT bool QTest::qWaitForWindowFocused(QWidget *widget, QDeadlineTimer timeout)
106{
107 return qWaitForWidgetWindow(widget,
108 [&](QWindow *window) {
109 return qGuiApp->focusWindow() == window;
110 }, timeout);
111}
112
113/*!
114 \since 6.10
115 \overload
116
117 This function uses the default timeout of 5 seconds.
118*/
120{
121 return qWaitForWindowFocused(widget, defaultTryTimeout.load(std::memory_order_relaxed));
122}
123
124/*!
125 \since 5.0
126 \overload
127
128 The \a timeout is in milliseconds.
129*/
130bool QTest::qWaitForWindowExposed(QWidget *widget, int timeout)
131{
132 return qWaitForWindowExposed(widget, std::chrono::milliseconds(timeout));
133}
134
135
136/*!
137 \since 6.10
138
139 Returns \c true if \a widget is exposed within \a timeout milliseconds. Otherwise returns \c false.
140
141 The method is useful in tests that call QWidget::show() and rely on the widget actually being
142 being visible before proceeding.
143
144 \note A window mapped to screen may still not be considered exposed, if the window client area is
145 not visible, e.g. because it is completely covered by other windows.
146 In such cases, the method will time out and return \c false.
147
148 \sa qWaitForWindowActive(), QWidget::isVisible(), QWindow::isExposed()
149*/
150bool QTest::qWaitForWindowExposed(QWidget *widget, QDeadlineTimer timeout)
151{
152 return qWaitForWidgetWindow(widget,
153 [&](QWindow *window) { return window->isExposed(); },
154 timeout);
155}
156
157/*!
158 \since 6.10
159 \overload
160
161 This function uses the default timeout of 5 seconds.
162*/
164{
165 return qWaitForWindowExposed(widget, defaultTryTimeout.load(std::memory_order_relaxed));
166}
167
168namespace QTest {
169
175
197
204
206{
207 bool ret = false;
208 if (points.isEmpty())
209 return ret;
211 if (targetWindow) {
213 } else if (targetWidget) {
215 }
216 if (processEvents)
219 points.clear();
220 return ret;
221}
222
225{
226}
227
229{
230 if (widget)
231 return widget->mapToGlobal(pt);
233}
234
235} // namespace QTest
236
237QT_END_NAMESPACE
friend class QWidget
Definition qpainter.h:431
Q_WIDGETS_EXPORT bool qWaitForWindowExposed(QWidget *widget)
Q_WIDGETS_EXPORT bool qWaitForWindowFocused(QWidget *widget, QDeadlineTimer timeout)
Q_WIDGETS_EXPORT bool qWaitForWindowFocused(QWidget *widget)
Q_WIDGETS_EXPORT bool qWaitForWindowExposed(QWidget *widget, int timeout)
Q_WIDGETS_EXPORT bool qWaitForWindowActive(QWidget *widget)
Q_WIDGETS_EXPORT bool qWaitForWindowActive(QWidget *widget, int timeout)