Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qtestmouse.h
Go to the documentation of this file.
1// Copyright (C) 2021 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#ifndef QTESTMOUSE_H
5#define QTESTMOUSE_H
6
7#if 0
8// inform syncqt
9#pragma qt_no_master_include
10#endif
11
12#include <QtTest/qttestglobal.h>
13#include <QtTest/qtestassert.h>
14#include <QtTest/qtestsystem.h>
15#include <QtTest/qtestspontaneevent.h>
16#include <QtCore/qpoint.h>
17#include <QtCore/qstring.h>
18#include <QtCore/qpointer.h>
19#include <QtGui/qevent.h>
20#include <QtGui/qwindow.h>
21
22#ifdef QT_WIDGETS_LIB
23#include <QtWidgets/qapplication.h>
24#include <QtWidgets/qwidget.h>
25#endif
26
28
29Q_GUI_EXPORT void qt_handleMouseEvent(QWindow *window, const QPointF &local, const QPointF &global,
30 Qt::MouseButtons state, Qt::MouseButton button,
31 QEvent::Type type, Qt::KeyboardModifiers mods, int timestamp);
32
33namespace QTestPrivate
34{
36}
37
38namespace QTest
39{
41
43
44 // This value is used to emulate timestamps to avoid creating double clicks by mistake.
45 // Use this constant instead of QStyleHints::mouseDoubleClickInterval property to avoid tests
46 // to depend on platform themes.
47 static const int mouseDoubleClickInterval = 500;
48
49 /*! \internal
50 This function creates a QPA mouse event of type specified by \a action
51 and calls QWindowSystemInterface::handleMouseEvent(), simulating the
52 windowing system and bypassing the platform plugin. \a delay is the
53 amount of time to be added to the simulated clock so that
54 QInputEvent::timestamp() will be greater than that of the previous
55 event. We expect all event-handling code to rely on the event
56 timestamps, not the system clock; therefore tests can be run faster
57 than real-time.
58
59 If \a delay is not given, a default minimum mouse delay is used, and
60 unintended double-click events are prevented by incrementing the
61 timestamp by 500ms after each mouse release. Therefore, to test
62 double-clicks, it's necessary to give a realistic \a delay value (for
63 example, 10ms).
64 */
65 static void mouseEvent(MouseAction action, QWindow *window, Qt::MouseButton button,
66 Qt::KeyboardModifiers stateKey, QPoint pos, int delay=-1)
67 {
68 QTEST_ASSERT(window);
69 extern int Q_TESTLIB_EXPORT defaultMouseDelay();
70
71 // pos is in window local coordinates
72 const QSize windowSize = window->geometry().size();
73 if (windowSize.width() <= pos.x() || windowSize.height() <= pos.y()) {
74 qWarning("Mouse event at %d, %d occurs outside target window (%dx%d).",
75 pos.x(), pos.y(), windowSize.width(), windowSize.height());
76 }
77
78 int actualDelay = qMax(1, (delay == -1 || delay < defaultMouseDelay()) ? defaultMouseDelay() : delay);
79
80 if (pos.isNull())
81 pos = QPoint(window->width() / 2, window->height() / 2);
82
83 QTEST_ASSERT(!stateKey || stateKey & Qt::KeyboardModifierMask);
84
85 stateKey &= Qt::KeyboardModifierMask;
86
87 QPointF global = window->mapToGlobal(pos);
88 QPointer<QWindow> w(window);
89
90 using namespace QTestPrivate;
91 switch (action)
92 {
93 case MouseDClick:
94 qtestMouseButtons.setFlag(button, true);
95 lastMouseTimestamp += actualDelay;
96 qt_handleMouseEvent(w, pos, global, qtestMouseButtons, button, QEvent::MouseButtonPress,
97 stateKey, lastMouseTimestamp);
98 qtestMouseButtons.setFlag(button, false);
99 lastMouseTimestamp += actualDelay;
100 qt_handleMouseEvent(w, pos, global, qtestMouseButtons, button, QEvent::MouseButtonRelease,
101 stateKey, lastMouseTimestamp);
102 Q_FALLTHROUGH();
103 case MousePress:
104 case MouseClick:
105 qtestMouseButtons.setFlag(button, true);
106 lastMouseTimestamp += actualDelay;
107 qt_handleMouseEvent(w, pos, global, qtestMouseButtons, button, QEvent::MouseButtonPress,
108 stateKey, lastMouseTimestamp);
109 if (action == MousePress)
110 break;
111 Q_FALLTHROUGH();
112 case MouseRelease:
113 qtestMouseButtons.setFlag(button, false);
114 lastMouseTimestamp += actualDelay;
115 qt_handleMouseEvent(w, pos, global, qtestMouseButtons, button, QEvent::MouseButtonRelease,
116 stateKey, lastMouseTimestamp);
117 if (delay == -1)
118 lastMouseTimestamp += mouseDoubleClickInterval; // avoid double clicks being generated
119 break;
120 case MouseMove:
121 lastMouseTimestamp += actualDelay;
122 qt_handleMouseEvent(w, pos, global, qtestMouseButtons, Qt::NoButton, QEvent::MouseMove,
123 stateKey, lastMouseTimestamp);
124 break;
125 default:
126 QTEST_ASSERT(false);
127 }
128 qApp->processEvents();
129 }
130
131 inline void mousePress(QWindow *window, Qt::MouseButton button,
132 Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers(),
133 QPoint pos = QPoint(), int delay=-1)
134 { mouseEvent(MousePress, window, button, stateKey, pos, delay); }
135 inline void mouseRelease(QWindow *window, Qt::MouseButton button,
136 Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers(),
137 QPoint pos = QPoint(), int delay=-1)
138 { mouseEvent(MouseRelease, window, button, stateKey, pos, delay); }
139 inline void mouseClick(QWindow *window, Qt::MouseButton button,
140 Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers(),
141 QPoint pos = QPoint(), int delay=-1)
142 { mouseEvent(MouseClick, window, button, stateKey, pos, delay); }
143 inline void mouseDClick(QWindow *window, Qt::MouseButton button,
144 Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers(),
145 QPoint pos = QPoint(), int delay=-1)
146 { mouseEvent(MouseDClick, window, button, stateKey, pos, delay); }
147 inline void mouseMove(QWindow *window, QPoint pos = QPoint(), int delay=-1)
148 { mouseEvent(MouseMove, window, Qt::NoButton, Qt::KeyboardModifiers(), pos, delay); }
149
150#ifdef QT_WIDGETS_LIB
153 {
155
156 if (pos.isNull())
157 pos = widget->rect().center();
158
159#ifdef QTEST_QPA_MOUSE_HANDLING
163#else
165
166 if (delay == -1 || delay < defaultMouseDelay())
169
170 if (action == MouseClick) {
173 return;
174 }
175
177
179
181 using namespace QTestPrivate;
182 switch (action)
183 {
184 case MousePress:
187 break;
188 case MouseRelease:
191 break;
192 case MouseDClick:
195 break;
196 case MouseMove:
197 // ### Qt 7: compatibility with < Qt 6.3, we should not rely on QCursor::setPos
198 // for generating mouse move events, and code that depends on QCursor::pos should
199 // be tested using QCursor::setPos explicitly.
200 if (qtestMouseButtons == Qt::NoButton) {
203 return;
204 }
206 break;
207 default:
208 QTEST_ASSERT(false);
209 }
212 if (action == MouseRelease) // avoid double clicks being generated
214
216 if (!qApp->notify(widget, &me)) {
217 static const char *const mouseActionNames[] =
218 { "MousePress", "MouseRelease", "MouseClick", "MouseDClick", "MouseMove" };
219 qWarning("Mouse event \"%s\" not accepted by receiving widget",
220 mouseActionNames[static_cast<int>(action)]);
221 }
222#endif
223 }
224
227 QPoint pos = QPoint(), int delay=-1)
231 QPoint pos = QPoint(), int delay=-1)
235 QPoint pos = QPoint(), int delay=-1)
239 QPoint pos = QPoint(), int delay=-1)
241 inline void mouseMove(QWidget *widget, QPoint pos = QPoint(), int delay=-1)
243#endif // QT_WIDGETS_LIB
244}
245
246QT_END_NAMESPACE
247
248#endif // QTESTMOUSE_H
Q_TESTLIB_EXPORT int lastMouseTimestamp
MouseAction
Definition qtestmouse.h:40
@ MouseMove
Definition qtestmouse.h:40
@ MousePress
Definition qtestmouse.h:40
@ MouseDClick
Definition qtestmouse.h:40
@ MouseClick
Definition qtestmouse.h:40
@ MouseRelease
Definition qtestmouse.h:40
static void mouseEvent(MouseAction action, QWindow *window, Qt::MouseButton button, Qt::KeyboardModifiers stateKey, QPoint pos, int delay=-1)
Definition qtestmouse.h:65
void mouseMove(QWindow *window, QPoint pos=QPoint(), int delay=-1)
Definition qtestmouse.h:147
static const int mouseDoubleClickInterval
Definition qtestmouse.h:47
void mouseDClick(QWindow *window, Qt::MouseButton button, Qt::KeyboardModifiers stateKey=Qt::KeyboardModifiers(), QPoint pos=QPoint(), int delay=-1)
Definition qtestmouse.h:143
void mouseRelease(QWindow *window, Qt::MouseButton button, Qt::KeyboardModifiers stateKey=Qt::KeyboardModifiers(), QPoint pos=QPoint(), int delay=-1)
Definition qtestmouse.h:135
void mouseClick(QWindow *window, Qt::MouseButton button, Qt::KeyboardModifiers stateKey=Qt::KeyboardModifiers(), QPoint pos=QPoint(), int delay=-1)
Definition qtestmouse.h:139
void mousePress(QWindow *window, Qt::MouseButton button, Qt::KeyboardModifiers stateKey=Qt::KeyboardModifiers(), QPoint pos=QPoint(), int delay=-1)
Definition qtestmouse.h:131
QDebug Q_GUI_EXPORT & operator<<(QDebug &s, const QVectorPath &path)
Q_GUI_EXPORT void qt_handleMouseEvent(QWindow *window, const QPointF &local, const QPointF &global, Qt::MouseButtons state, Qt::MouseButton button, QEvent::Type type, Qt::KeyboardModifiers mods, int timestamp)