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
4#include "qtestsupport_widgets.h"
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
17QT_BEGIN_NAMESPACE
18
19template <typename FunctorWindowGetter, typename FunctorPredicate>
20static bool qWaitForWidgetWindow(FunctorWindowGetter windowGetter, FunctorPredicate predicate, int 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
32/*!
33 \since 5.0
34
35 Returns \c true if \a widget is active within \a timeout milliseconds. Otherwise returns \c false.
36
37 The method is useful in tests that call QWidget::show() and rely on the widget actually being
38 active (i.e. being visible and having focus) before proceeding.
39
40 \note The method will time out and return \c false if another window prevents \a widget from
41 becoming active.
42
43 \note Since focus is an exclusive property, \a widget may loose its focus to another window at
44 any time - even after the method has returned \c true.
45
46 \sa qWaitForWindowExposed(), QWidget::isActiveWindow()
47*/
48Q_WIDGETS_EXPORT bool QTest::qWaitForWindowActive(QWidget *widget, int timeout)
49{
50 if (Q_UNLIKELY(!QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowActivation))) {
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.";
56 return qWaitForWindowExposed(widget, timeout);
57 }
58 return qWaitForWidgetWindow(windowGetter: [&]() { return widget->window()->windowHandle(); },
59 predicate: [&](QWindow *window) { return window->isActive(); },
60 timeout);
61}
62
63/*!
64 \since 5.0
65
66 Returns \c true if \a widget is exposed within \a timeout milliseconds. Otherwise returns \c false.
67
68 The method is useful in tests that call QWidget::show() and rely on the widget actually being
69 being visible before proceeding.
70
71 \note A window mapped to screen may still not be considered exposed, if the window client area is
72 not visible, e.g. because it is completely covered by other windows.
73 In such cases, the method will time out and return \c false.
74
75 \sa qWaitForWindowActive(), QWidget::isVisible(), QWindow::isExposed()
76*/
77Q_WIDGETS_EXPORT bool QTest::qWaitForWindowExposed(QWidget *widget, int timeout)
78{
79 return qWaitForWidgetWindow(windowGetter: [&]() { return widget->window()->windowHandle(); },
80 predicate: [&](QWindow *window) { return window->isExposed(); },
81 timeout);
82}
83
84namespace QTest {
85
86QTouchEventWidgetSequence::~QTouchEventWidgetSequence()
87{
88 if (commitWhenDestroyed)
89 QTouchEventWidgetSequence::commit();
90}
91
92QTouchEventWidgetSequence& QTouchEventWidgetSequence::press(int touchId, const QPoint &pt, QWidget *widget)
93{
94 auto &p = point(touchId);
95 QMutableEventPoint::setGlobalPosition(p, arg: mapToScreen(widget, pt));
96 QMutableEventPoint::setState(p, arg: QEventPoint::State::Pressed);
97 return *this;
98}
99QTouchEventWidgetSequence& QTouchEventWidgetSequence::move(int touchId, const QPoint &pt, QWidget *widget)
100{
101 auto &p = point(touchId);
102 QMutableEventPoint::setGlobalPosition(p, arg: mapToScreen(widget, pt));
103 QMutableEventPoint::setState(p, arg: QEventPoint::State::Updated);
104 return *this;
105}
106QTouchEventWidgetSequence& QTouchEventWidgetSequence::release(int touchId, const QPoint &pt, QWidget *widget)
107{
108 auto &p = point(touchId);
109 QMutableEventPoint::setGlobalPosition(p, arg: mapToScreen(widget, pt));
110 QMutableEventPoint::setState(p, arg: QEventPoint::State::Released);
111 return *this;
112}
113
114QTouchEventWidgetSequence& QTouchEventWidgetSequence::stationary(int touchId)
115{
116 auto &p = pointOrPreviousPoint(touchId);
117 QMutableEventPoint::setState(p, arg: QEventPoint::State::Stationary);
118 return *this;
119}
120
121bool QTouchEventWidgetSequence::commit(bool processEvents)
122{
123 bool ret = false;
124 if (points.isEmpty())
125 return ret;
126 QThread::sleep(nsec: std::chrono::milliseconds{1});
127 if (targetWindow) {
128 ret = qt_handleTouchEventv2(w: targetWindow, device, points: points.values());
129 } else if (targetWidget) {
130 ret = qt_handleTouchEventv2(w: targetWidget->windowHandle(), device, points: points.values());
131 }
132 if (processEvents)
133 QCoreApplication::processEvents();
134 previousPoints = points;
135 points.clear();
136 return ret;
137}
138
139QTest::QTouchEventWidgetSequence::QTouchEventWidgetSequence(QWidget *widget, QPointingDevice *aDevice, bool autoCommit)
140 : QTouchEventSequence(nullptr, aDevice, autoCommit), targetWidget(widget)
141{
142}
143
144QPoint QTouchEventWidgetSequence::mapToScreen(QWidget *widget, const QPoint &pt)
145{
146 if (widget)
147 return widget->mapToGlobal(pt);
148 return targetWidget ? targetWidget->mapToGlobal(pt) : pt;
149}
150
151} // namespace QTest
152
153QT_END_NAMESPACE
154

source code of qtbase/src/widgets/kernel/qtestsupport_widgets.cpp