1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2018 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtTest module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include "qtestsupport_widgets.h" |
41 | |
42 | #include "qwidget.h" |
43 | |
44 | #include <QtGui/qwindow.h> |
45 | #include <QtCore/qtestsupport_core.h> |
46 | #include <QtCore/qthread.h> |
47 | #include <QtGui/qtestsupport_gui.h> |
48 | #include <QtGui/private/qevent_p.h> |
49 | |
50 | QT_BEGIN_NAMESPACE |
51 | |
52 | /*! |
53 | \since 5.0 |
54 | |
55 | Waits for \a timeout milliseconds or until the \a widget's window is active. |
56 | |
57 | Returns \c true if \c widget's window is active within \a timeout milliseconds, otherwise returns \c false. |
58 | |
59 | \sa qWaitForWindowExposed(), QWidget::isActiveWindow() |
60 | */ |
61 | Q_WIDGETS_EXPORT bool QTest::qWaitForWindowActive(QWidget *widget, int timeout) |
62 | { |
63 | if (QWindow *window = widget->window()->windowHandle()) |
64 | return QTest::qWaitForWindowActive(window, timeout); |
65 | return false; |
66 | } |
67 | |
68 | /*! |
69 | \since 5.0 |
70 | |
71 | Waits for \a timeout milliseconds or until the \a widget's window is exposed. |
72 | Returns \c true if \c widget's window is exposed within \a timeout milliseconds, otherwise returns \c false. |
73 | |
74 | This is mainly useful for asynchronous systems like X11, where a window will be mapped to screen some |
75 | time after being asked to show itself on the screen. |
76 | |
77 | Note that a window that is mapped to screen may still not be considered exposed if the window client |
78 | area is completely covered by other windows, or if the window is otherwise not visible. This function |
79 | will then time out when waiting for such a window. |
80 | |
81 | A specific configuration where this happens is when using QGLWidget as a viewport widget on macOS: |
82 | The viewport widget gets the expose event, not the parent widget. |
83 | |
84 | \sa qWaitForWindowActive() |
85 | */ |
86 | Q_WIDGETS_EXPORT bool QTest::qWaitForWindowExposed(QWidget *widget, int timeout) |
87 | { |
88 | if (QWindow *window = widget->window()->windowHandle()) |
89 | return QTest::qWaitForWindowExposed(window, timeout); |
90 | return false; |
91 | } |
92 | |
93 | namespace QTest { |
94 | |
95 | QTouchEventWidgetSequence::~QTouchEventWidgetSequence() |
96 | { |
97 | if (commitWhenDestroyed) |
98 | commit(); |
99 | } |
100 | |
101 | QTouchEventWidgetSequence& QTouchEventWidgetSequence::press(int touchId, const QPoint &pt, QWidget *widget) |
102 | { |
103 | auto &p = QMutableEventPoint::from(point(touchId)); |
104 | p.setGlobalPosition(mapToScreen(widget, pt)); |
105 | p.setState(QEventPoint::State::Pressed); |
106 | return *this; |
107 | } |
108 | QTouchEventWidgetSequence& QTouchEventWidgetSequence::move(int touchId, const QPoint &pt, QWidget *widget) |
109 | { |
110 | auto &p = QMutableEventPoint::from(point(touchId)); |
111 | p.setGlobalPosition(mapToScreen(widget, pt)); |
112 | p.setState(QEventPoint::State::Updated); |
113 | return *this; |
114 | } |
115 | QTouchEventWidgetSequence& QTouchEventWidgetSequence::release(int touchId, const QPoint &pt, QWidget *widget) |
116 | { |
117 | auto &p = QMutableEventPoint::from(point(touchId)); |
118 | p.setGlobalPosition(mapToScreen(widget, pt)); |
119 | p.setState(QEventPoint::State::Released); |
120 | return *this; |
121 | } |
122 | |
123 | QTouchEventWidgetSequence& QTouchEventWidgetSequence::stationary(int touchId) |
124 | { |
125 | auto &p = QMutableEventPoint::from(pointOrPreviousPoint(touchId)); |
126 | p.setState(QEventPoint::State::Stationary); |
127 | return *this; |
128 | } |
129 | |
130 | void QTouchEventWidgetSequence::commit(bool processEvents) |
131 | { |
132 | if (points.isEmpty()) |
133 | return; |
134 | QThread::msleep(1); |
135 | if (targetWindow) { |
136 | qt_handleTouchEvent(targetWindow, device, points.values()); |
137 | } else if (targetWidget) { |
138 | qt_handleTouchEvent(targetWidget->windowHandle(), device, points.values()); |
139 | } |
140 | if (processEvents) |
141 | QCoreApplication::processEvents(); |
142 | previousPoints = points; |
143 | points.clear(); |
144 | } |
145 | |
146 | QTest::QTouchEventWidgetSequence::QTouchEventWidgetSequence(QWidget *widget, QPointingDevice *aDevice, bool autoCommit) |
147 | : QTouchEventSequence(nullptr, aDevice, autoCommit), targetWidget(widget) |
148 | { |
149 | } |
150 | |
151 | QPoint QTouchEventWidgetSequence::mapToScreen(QWidget *widget, const QPoint &pt) |
152 | { |
153 | if (widget) |
154 | return widget->mapToGlobal(pt); |
155 | return targetWidget ? targetWidget->mapToGlobal(pt) : pt; |
156 | } |
157 | |
158 | } // namespace QTest |
159 | |
160 | QT_END_NAMESPACE |
161 | |