Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
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
5
#
include
"qtestsupport_widgets.h"
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
18
QT_BEGIN_NAMESPACE
19
20
template
<
typename
Predicate>
21
static
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
*/
42
bool
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
*/
63
bool
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
*/
84
bool
QTest
::
qWaitForWindowActive
(
QWidget
*widget)
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
*/
105
Q_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
*/
119
bool
QTest
::
qWaitForWindowFocused
(
QWidget
*widget)
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
*/
130
bool
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
*/
150
bool
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
*/
163
bool
QTest
::
qWaitForWindowExposed
(
QWidget
*widget)
164
{
165
return
qWaitForWindowExposed(widget, defaultTryTimeout.load(std::memory_order_relaxed));
166
}
167
168
namespace
QTest
{
169
170
QTouchEventWidgetSequence
::~
QTouchEventWidgetSequence
()
171
{
172
if
(
commitWhenDestroyed
)
173
QTouchEventWidgetSequence
::
commit
();
174
}
175
176
QTouchEventWidgetSequence
&
QTouchEventWidgetSequence
::
press
(
int
touchId
,
const
QPoint
&
pt
,
QWidget
*
widget
)
177
{
178
auto
&
p
=
point
(
touchId
);
179
QMutableEventPoint
::
setGlobalPosition
(
p
,
mapToScreen
(
widget
,
pt
));
180
QMutableEventPoint
::
setState
(
p
,
QEventPoint
::
State
::
Pressed
);
181
return
*
this
;
182
}
183
QTouchEventWidgetSequence
&
QTouchEventWidgetSequence
::
move
(
int
touchId
,
const
QPoint
&
pt
,
QWidget
*
widget
)
184
{
185
auto
&
p
=
point
(
touchId
);
186
QMutableEventPoint
::
setGlobalPosition
(
p
,
mapToScreen
(
widget
,
pt
));
187
QMutableEventPoint
::
setState
(
p
,
QEventPoint
::
State
::
Updated
);
188
return
*
this
;
189
}
190
QTouchEventWidgetSequence
&
QTouchEventWidgetSequence
::
release
(
int
touchId
,
const
QPoint
&
pt
,
QWidget
*
widget
)
191
{
192
auto
&
p
=
point
(
touchId
);
193
QMutableEventPoint
::
setGlobalPosition
(
p
,
mapToScreen
(
widget
,
pt
));
194
QMutableEventPoint
::
setState
(
p
,
QEventPoint
::
State
::
Released
);
195
return
*
this
;
196
}
197
198
QTouchEventWidgetSequence
&
QTouchEventWidgetSequence
::
stationary
(
int
touchId
)
199
{
200
auto
&
p
=
pointOrPreviousPoint
(
touchId
);
201
QMutableEventPoint
::
setState
(
p
,
QEventPoint
::
State
::
Stationary
);
202
return
*
this
;
203
}
204
205
bool
QTouchEventWidgetSequence
::
commit
(
bool
processEvents
)
206
{
207
bool
ret
=
false
;
208
if
(
points
.
isEmpty
())
209
return
ret
;
210
QThread
::
sleep
(
std
::
chrono
::
milliseconds
{1});
211
if
(
targetWindow
) {
212
ret
=
qt_handleTouchEventv2
(
targetWindow
,
device
,
points
.
values
());
213
}
else
if
(
targetWidget
) {
214
ret
=
qt_handleTouchEventv2
(
targetWidget
->
windowHandle
(),
device
,
points
.
values
());
215
}
216
if
(
processEvents
)
217
QCoreApplication
::
processEvents
();
218
previousPoints
=
points
;
219
points
.
clear
();
220
return
ret
;
221
}
222
223
QTest
::
QTouchEventWidgetSequence
::
QTouchEventWidgetSequence
(
QWidget
*
widget
,
QPointingDevice
*
aDevice
,
bool
autoCommit
)
224
:
QTouchEventSequence
(
nullptr
,
aDevice
,
autoCommit
),
targetWidget
(
widget
)
225
{
226
}
227
228
QPointF
QTouchEventWidgetSequence
::
mapToScreen
(
QWidget
*
widget
,
const
QPoint
&
pt
)
229
{
230
if
(
widget
)
231
return
widget
->
mapToGlobal
(
QPointF
(
pt
));
232
return
targetWidget
?
targetWidget
->
mapToGlobal
(
QPointF
(
pt
)) :
QPointF
(
pt
);
233
}
234
235
}
// namespace QTest
236
237
QT_END_NAMESPACE
QPainter::QWidget
friend class QWidget
Definition
qpainter.h:432
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
QTest
[15]
Definition
qtestsupport_core.h:15
QTest::qWaitForWindowExposed
Q_WIDGETS_EXPORT bool qWaitForWindowExposed(QWidget *widget)
Definition
qtestsupport_widgets.cpp:163
QTest::qWaitForWindowFocused
Q_WIDGETS_EXPORT bool qWaitForWindowFocused(QWidget *widget, QDeadlineTimer timeout)
Definition
qtestsupport_widgets.cpp:105
QTest::qWaitForWindowFocused
Q_WIDGETS_EXPORT bool qWaitForWindowFocused(QWidget *widget)
Definition
qtestsupport_widgets.cpp:119
QTest::qWaitForWindowExposed
Q_WIDGETS_EXPORT bool qWaitForWindowExposed(QWidget *widget, int timeout)
Definition
qtestsupport_widgets.cpp:130
QTest::qWaitForWindowActive
Q_WIDGETS_EXPORT bool qWaitForWindowActive(QWidget *widget)
Definition
qtestsupport_widgets.cpp:84
QTest::qWaitForWindowActive
Q_WIDGETS_EXPORT bool qWaitForWindowActive(QWidget *widget, int timeout)
Definition
qtestsupport_widgets.cpp:42
qtbase
src
widgets
kernel
qtestsupport_widgets.cpp
Generated on
for Qt by
1.18.0