1/****************************************************************************
2**
3** Copyright (C) 2016 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#ifndef QTESTMOUSE_H
41#define QTESTMOUSE_H
42
43#if 0
44// inform syncqt
45#pragma qt_no_master_include
46#endif
47
48#include <QtTest/qttestglobal.h>
49#include <QtTest/qtestassert.h>
50#include <QtTest/qtestsystem.h>
51#include <QtTest/qtestspontaneevent.h>
52#include <QtCore/qpoint.h>
53#include <QtCore/qstring.h>
54#include <QtCore/qpointer.h>
55#include <QtGui/qevent.h>
56#include <QtGui/qwindow.h>
57
58#ifdef QT_WIDGETS_LIB
59#include <QtWidgets/qapplication.h>
60#include <QtWidgets/qwidget.h>
61#endif
62
63#include <QtCore/QDebug>
64
65QT_BEGIN_NAMESPACE
66
67Q_GUI_EXPORT void qt_handleMouseEvent(QWindow *window, const QPointF &local, const QPointF &global,
68 Qt::MouseButtons state, Qt::MouseButton button,
69 QEvent::Type type, Qt::KeyboardModifiers mods, int timestamp);
70
71namespace QTestPrivate
72{
73 extern Q_TESTLIB_EXPORT Qt::MouseButtons qtestMouseButtons;
74}
75
76namespace QTest
77{
78 enum MouseAction { MousePress, MouseRelease, MouseClick, MouseDClick, MouseMove };
79
80 extern Q_TESTLIB_EXPORT int lastMouseTimestamp;
81
82 // This value is used to emulate timestamps to avoid creating double clicks by mistake.
83 // Use this constant instead of QStyleHints::mouseDoubleClickInterval property to avoid tests
84 // to depend on platform themes.
85 static const int mouseDoubleClickInterval = 500;
86
87 /*! \internal
88 This function creates a QPA mouse event of type specified by \a action
89 and calls QWindowSystemInterface::handleMouseEvent(), simulating the
90 windowing system and bypassing the platform plugin. \a delay is the
91 amount of time to be added to the simulated clock so that
92 QInputEvent::timestamp() will be greater than that of the previous
93 event. We expect all event-handling code to rely on the event
94 timestamps, not the system clock; therefore tests can be run faster
95 than real-time.
96 */
97 static void mouseEvent(MouseAction action, QWindow *window, Qt::MouseButton button,
98 Qt::KeyboardModifiers stateKey, QPoint pos, int delay=-1)
99 {
100 QTEST_ASSERT(window);
101 extern int Q_TESTLIB_EXPORT defaultMouseDelay();
102
103 // pos is in window local coordinates
104 const QSize windowSize = window->geometry().size();
105 if (windowSize.width() <= pos.x() || windowSize.height() <= pos.y()) {
106 QTest::qWarn(qPrintable(QString::fromLatin1("Mouse event at %1, %2 occurs outside of target window (%3x%4).")
107 .arg(pos.x()).arg(pos.y()).arg(windowSize.width()).arg(windowSize.height())));
108 }
109
110 if (delay == -1 || delay < defaultMouseDelay())
111 delay = defaultMouseDelay();
112 lastMouseTimestamp += qMax(1, delay);
113
114 if (pos.isNull())
115 pos = QPoint(window->width() / 2, window->height() / 2);
116
117 QTEST_ASSERT(uint(stateKey) == 0 || stateKey & Qt::KeyboardModifierMask);
118
119 stateKey &= static_cast<unsigned int>(Qt::KeyboardModifierMask);
120
121 QPointF global = window->mapToGlobal(pos);
122 QPointer<QWindow> w(window);
123
124 using namespace QTestPrivate;
125 switch (action)
126 {
127 case MouseDClick:
128 qtestMouseButtons.setFlag(button, true);
129 qt_handleMouseEvent(w, pos, global, qtestMouseButtons, button, QEvent::MouseButtonPress,
130 stateKey, lastMouseTimestamp);
131 qtestMouseButtons.setFlag(button, false);
132 qt_handleMouseEvent(w, pos, global, qtestMouseButtons, button, QEvent::MouseButtonRelease,
133 stateKey, lastMouseTimestamp);
134 Q_FALLTHROUGH();
135 case MousePress:
136 case MouseClick:
137 qtestMouseButtons.setFlag(button, true);
138 qt_handleMouseEvent(w, pos, global, qtestMouseButtons, button, QEvent::MouseButtonPress,
139 stateKey, lastMouseTimestamp);
140 if (action == MousePress)
141 break;
142 Q_FALLTHROUGH();
143 case MouseRelease:
144 qtestMouseButtons.setFlag(button, false);
145 qt_handleMouseEvent(w, pos, global, qtestMouseButtons, button, QEvent::MouseButtonRelease,
146 stateKey, lastMouseTimestamp);
147 lastMouseTimestamp += mouseDoubleClickInterval; // avoid double clicks being generated
148 break;
149 case MouseMove:
150 qt_handleMouseEvent(w, pos, global, qtestMouseButtons, Qt::NoButton, QEvent::MouseMove,
151 stateKey, lastMouseTimestamp);
152 break;
153 default:
154 QTEST_ASSERT(false);
155 }
156 qApp->processEvents();
157 }
158
159 inline void mousePress(QWindow *window, Qt::MouseButton button,
160 Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers(),
161 QPoint pos = QPoint(), int delay=-1)
162 { mouseEvent(MousePress, window, button, stateKey, pos, delay); }
163 inline void mouseRelease(QWindow *window, Qt::MouseButton button,
164 Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers(),
165 QPoint pos = QPoint(), int delay=-1)
166 { mouseEvent(MouseRelease, window, button, stateKey, pos, delay); }
167 inline void mouseClick(QWindow *window, Qt::MouseButton button,
168 Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers(),
169 QPoint pos = QPoint(), int delay=-1)
170 { mouseEvent(MouseClick, window, button, stateKey, pos, delay); }
171 inline void mouseDClick(QWindow *window, Qt::MouseButton button,
172 Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers(),
173 QPoint pos = QPoint(), int delay=-1)
174 { mouseEvent(MouseDClick, window, button, stateKey, pos, delay); }
175 inline void mouseMove(QWindow *window, QPoint pos = QPoint(), int delay=-1)
176 { mouseEvent(MouseMove, window, Qt::NoButton, Qt::KeyboardModifiers(), pos, delay); }
177
178#ifdef QT_WIDGETS_LIB
179 static void mouseEvent(MouseAction action, QWidget *widget, Qt::MouseButton button,
180 Qt::KeyboardModifiers stateKey, QPoint pos, int delay=-1)
181 {
182 QTEST_ASSERT(widget);
183
184 if (pos.isNull())
185 pos = widget->rect().center();
186
187#ifdef QTEST_QPA_MOUSE_HANDLING
188 QWindow *w = widget->window()->windowHandle();
189 QTEST_ASSERT(w);
190 mouseEvent(action, w, button, stateKey, w->mapFromGlobal(widget->mapToGlobal(pos)), delay);
191#else
192 extern int Q_TESTLIB_EXPORT defaultMouseDelay();
193
194 if (delay == -1 || delay < defaultMouseDelay())
195 delay = defaultMouseDelay();
196 lastMouseTimestamp += qMax(1, delay);
197
198 if (action == MouseClick) {
199 mouseEvent(MousePress, widget, button, stateKey, pos);
200 mouseEvent(MouseRelease, widget, button, stateKey, pos);
201 return;
202 }
203
204 QTEST_ASSERT(stateKey == 0 || stateKey & Qt::KeyboardModifierMask);
205
206 stateKey &= static_cast<unsigned int>(Qt::KeyboardModifierMask);
207
208 QEvent::Type meType;
209 Qt::MouseButton meButton;
210 switch (action)
211 {
212 case MousePress:
213 meType = QEvent::MouseButtonPress;
214 meButton = button;
215 break;
216 case MouseRelease:
217 meType = QEvent::MouseButtonRelease;
218 meButton = Qt::MouseButton();
219 break;
220 case MouseDClick:
221 meType = QEvent::MouseButtonDblClick;
222 meButton = button;
223 break;
224 case MouseMove:
225 QCursor::setPos(widget->mapToGlobal(pos));
226 qApp->processEvents();
227 return;
228 default:
229 QTEST_ASSERT(false);
230 }
231 QMouseEvent me(meType, pos, widget->mapToGlobal(pos), button, meButton, stateKey, QPointingDevice::primaryPointingDevice());
232 me.setTimestamp(lastMouseTimestamp);
233 if (action == MouseRelease) // avoid double clicks being generated
234 lastMouseTimestamp += mouseDoubleClickInterval;
235
236 QSpontaneKeyEvent::setSpontaneous(&me);
237 if (!qApp->notify(widget, &me)) {
238 static const char *const mouseActionNames[] =
239 { "MousePress", "MouseRelease", "MouseClick", "MouseDClick", "MouseMove" };
240 QString warning = QString::fromLatin1("Mouse event \"%1\" not accepted by receiving widget");
241 QTest::qWarn(warning.arg(QString::fromLatin1(mouseActionNames[static_cast<int>(action)])).toLatin1().data());
242 }
243#endif
244 }
245
246 inline void mousePress(QWidget *widget, Qt::MouseButton button,
247 Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers(),
248 QPoint pos = QPoint(), int delay=-1)
249 { mouseEvent(MousePress, widget, button, stateKey, pos, delay); }
250 inline void mouseRelease(QWidget *widget, Qt::MouseButton button,
251 Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers(),
252 QPoint pos = QPoint(), int delay=-1)
253 { mouseEvent(MouseRelease, widget, button, stateKey, pos, delay); }
254 inline void mouseClick(QWidget *widget, Qt::MouseButton button,
255 Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers(),
256 QPoint pos = QPoint(), int delay=-1)
257 { mouseEvent(MouseClick, widget, button, stateKey, pos, delay); }
258 inline void mouseDClick(QWidget *widget, Qt::MouseButton button,
259 Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers(),
260 QPoint pos = QPoint(), int delay=-1)
261 { mouseEvent(MouseDClick, widget, button, stateKey, pos, delay); }
262 inline void mouseMove(QWidget *widget, QPoint pos = QPoint(), int delay=-1)
263 { mouseEvent(MouseMove, widget, Qt::NoButton, Qt::KeyboardModifiers(), pos, delay); }
264#endif // QT_WIDGETS_LIB
265}
266
267QT_END_NAMESPACE
268
269#endif // QTESTMOUSE_H
270