Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
5
6#include "qwidget.h"
7
8#include <QtGui/qwindow.h>
9#include <QtCore/qtestsupport_core.h>
10#include <QtCore/qthread.h>
11#include <QtGui/qtestsupport_gui.h>
12#include <QtGui/private/qevent_p.h>
13#include <QtGui/private/qeventpoint_p.h>
14#include <private/qguiapplication_p.h>
15#include <qpa/qplatformintegration.h>
16
18
19template <typename FunctorWindowGetter, typename FunctorPredicate, typename Timeout>
20static bool qWaitForWidgetWindow(FunctorWindowGetter windowGetter, FunctorPredicate predicate, Timeout timeout)
21{
22 if (!windowGetter())
23 return false;
24
25 return QTest::qWaitFor([&]() {
26 if (QWindow *window = windowGetter())
27 return predicate(window);
28 return false;
29 }, timeout);
30}
31
49{
51 qWarning() << "qWaitForWindowActive was called on a platform that doesn't support window"
52 << "activation. This means there is an error in the test and it should either"
53 << "check for the WindowActivation platform capability before calling"
54 << "qWaitForWindowActivate, use qWaitForWindowExposed instead, or skip the test."
55 << "Falling back to qWaitForWindowExposed.";
57 }
58 return qWaitForWidgetWindow([&]() { return widget->window()->windowHandle(); },
59 [&](QWindow *window) { return window->isActive(); },
60 timeout);
61}
62
63
81{
82 return qWaitForWidgetWindow([&]() {
83 return widget->window()->windowHandle();
84 }, [&](QWindow *window) {
85 return qGuiApp->focusWindow() == window;
86 }, timeout);
87}
88
104{
105 return qWaitForWidgetWindow([&]() { return widget->window()->windowHandle(); },
106 [&](QWindow *window) { return window->isExposed(); },
107 timeout);
108}
109
110namespace QTest {
111
117
119{
120 auto &p = point(touchId);
121 QMutableEventPoint::setGlobalPosition(p, mapToScreen(widget, pt));
122 QMutableEventPoint::setState(p, QEventPoint::State::Pressed);
123 return *this;
124}
126{
127 auto &p = point(touchId);
128 QMutableEventPoint::setGlobalPosition(p, mapToScreen(widget, pt));
129 QMutableEventPoint::setState(p, QEventPoint::State::Updated);
130 return *this;
131}
133{
134 auto &p = point(touchId);
135 QMutableEventPoint::setGlobalPosition(p, mapToScreen(widget, pt));
136 QMutableEventPoint::setState(p, QEventPoint::State::Released);
137 return *this;
138}
139
141{
142 auto &p = pointOrPreviousPoint(touchId);
143 QMutableEventPoint::setState(p, QEventPoint::State::Stationary);
144 return *this;
145}
146
147bool QTouchEventWidgetSequence::commit(bool processEvents)
148{
149 bool ret = false;
150 if (points.isEmpty())
151 return ret;
152 QThread::sleep(std::chrono::milliseconds{1});
153 if (targetWindow) {
155 } else if (targetWidget) {
156 ret = qt_handleTouchEventv2(targetWidget->windowHandle(), device, points.values());
157 }
158 if (processEvents)
161 points.clear();
162 return ret;
163}
164
165QTest::QTouchEventWidgetSequence::QTouchEventWidgetSequence(QWidget *widget, QPointingDevice *aDevice, bool autoCommit)
166 : QTouchEventSequence(nullptr, aDevice, autoCommit), targetWidget(widget)
167{
168}
169
170QPoint QTouchEventWidgetSequence::mapToScreen(QWidget *widget, const QPoint &pt)
171{
172 if (widget)
173 return widget->mapToGlobal(pt);
174 return targetWidget ? targetWidget->mapToGlobal(pt) : pt;
175}
176
177} // namespace QTest
178
static void processEvents(QEventLoop::ProcessEventsFlags flags=QEventLoop::AllEvents)
Processes some pending events for the calling thread according to the specified flags.
\inmodule QtCore
static QPlatformIntegration * platformIntegration()
void clear()
Definition qmap.h:289
\inmodule QtCore\reentrant
Definition qpoint.h:25
The QPointingDevice class describes a device from which mouse, touch or tablet events originate.
QMap< int, QEventPoint > previousPoints
QEventPoint & pointOrPreviousPoint(int touchId)
QEventPoint & point(int touchId)
QMap< int, QEventPoint > points
bool commit(bool processEvents=true) override
QTouchEventWidgetSequence & press(int touchId, const QPoint &pt, QWidget *widget=nullptr)
QTouchEventWidgetSequence & stationary(int touchId) override
QTouchEventWidgetSequence & move(int touchId, const QPoint &pt, QWidget *widget=nullptr)
QTouchEventWidgetSequence & release(int touchId, const QPoint &pt, QWidget *widget=nullptr)
static void sleep(unsigned long)
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
QWidget * window() const
Returns the window for this widget, i.e.
Definition qwidget.cpp:4313
QPointF mapToGlobal(const QPointF &) const
Translates the widget coordinate pos to global screen coordinates.
QWindow * windowHandle() const
If this is a native widget, return the associated QWindow.
Definition qwidget.cpp:2483
\inmodule QtGui
Definition qwindow.h:63
QOpenGLWidget * widget
[1]
const auto predicate
Combined button and popup list for selecting options.
bool qWaitFor(Functor predicate, QDeadlineTimer deadline=QDeadlineTimer(std::chrono::seconds{5}))
Q_GUI_EXPORT bool qWaitForWindowActive(QWindow *window, int timeout=5000)
Q_GUI_EXPORT bool qWaitForWindowExposed(QWindow *window, int timeout=5000)
Q_GUI_EXPORT bool qWaitForWindowFocused(QWindow *widget, QDeadlineTimer timeout=std::chrono::seconds{5})
#define Q_UNLIKELY(x)
#define qGuiApp
#define qWarning
Definition qlogging.h:166
return ret
GLbitfield GLuint64 timeout
[4]
GLfixed GLfixed GLint GLint GLfixed points
GLfloat GLfloat p
[1]
Q_GUI_EXPORT bool qt_handleTouchEventv2(QWindow *w, const QPointingDevice *device, const QList< QEventPoint > &points, Qt::KeyboardModifiers mods=Qt::NoModifier)
static QT_BEGIN_NAMESPACE bool qWaitForWidgetWindow(FunctorWindowGetter windowGetter, FunctorPredicate predicate, Timeout timeout)
QObject::connect nullptr
aWidget window() -> setWindowTitle("New Window Title")
[2]