1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtTest module of the Qt Toolkit.
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
47 #pragma qt_no_master_include
50 #include <QtTest/qtest_global.h>
51 #include <QtTest/qtestassert.h>
52 #include <QtTest/qtestsystem.h>
53 #include <QtTest/qtestspontaneevent.h>
54 #include <QtCore/qpoint.h>
55 #include <QtCore/qstring.h>
56 #include <QtGui/qevent.h>
59 #include <QtWidgets/qapplication.h>
60 #include <QtWidgets/qwidget.h>
67 Q_GUI_EXPORT void qt_handleMouseEvent(QWindow *w, const QPointF & local, const QPointF & global, Qt::MouseButtons b, Qt::KeyboardModifiers mods = Qt::NoModifier);
71 enum MouseAction { MousePress, MouseRelease, MouseClick, MouseDClick, MouseMove };
73 static void waitForEvents()
78 qApp->processEvents();
82 static void mouseEvent(MouseAction action, QWindow *window, Qt::MouseButton button,
83 Qt::KeyboardModifiers stateKey, QPoint pos, int delay=-1)
86 extern int Q_TESTLIB_EXPORT defaultMouseDelay();
88 static Qt::MouseButton lastButton = Qt::NoButton;
90 if (delay == -1 || delay < defaultMouseDelay())
91 delay = defaultMouseDelay();
96 pos = window->geometry().center();
98 if (action == MouseClick) {
99 mouseEvent(MousePress, window, button, stateKey, pos);
100 mouseEvent(MouseRelease, window, button, stateKey, pos);
103 QTEST_ASSERT(stateKey == 0 || stateKey & Qt::KeyboardModifierMask);
105 stateKey &= static_cast<unsigned int>(Qt::KeyboardModifierMask);
111 qt_handleMouseEvent(window,pos,window->mapToGlobal(pos),button,stateKey);
115 qt_handleMouseEvent(window,pos,window->mapToGlobal(pos),Qt::NoButton,stateKey);
116 lastButton = Qt::NoButton;
119 qt_handleMouseEvent(window,pos,window->mapToGlobal(pos),button,stateKey);
121 qt_handleMouseEvent(window,pos,window->mapToGlobal(pos),Qt::NoButton,stateKey);
123 qt_handleMouseEvent(window,pos,window->mapToGlobal(pos),button,stateKey);
125 qt_handleMouseEvent(window,pos,window->mapToGlobal(pos),Qt::NoButton,stateKey);
128 qt_handleMouseEvent(window,pos,window->mapToGlobal(pos),lastButton,stateKey);
129 // No QCursor::setPos() call here. That could potentially result in mouse events sent by the windowing system
130 // which is highly undesired here. Tests must avoid relying on QCursor.
138 inline void mousePress(QWindow *window, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = 0,
139 QPoint pos = QPoint(), int delay=-1)
140 { mouseEvent(MousePress, window, button, stateKey, pos, delay); }
141 inline void mouseRelease(QWindow *window, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = 0,
142 QPoint pos = QPoint(), int delay=-1)
143 { mouseEvent(MouseRelease, window, button, stateKey, pos, delay); }
144 inline void mouseClick(QWindow *window, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = 0,
145 QPoint pos = QPoint(), int delay=-1)
146 { mouseEvent(MouseClick, window, button, stateKey, pos, delay); }
147 inline void mouseDClick(QWindow *window, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = 0,
148 QPoint pos = QPoint(), int delay=-1)
149 { mouseEvent(MouseDClick, window, button, stateKey, pos, delay); }
150 inline void mouseMove(QWindow *window, QPoint pos = QPoint(), int delay=-1)
151 { mouseEvent(MouseMove, window, Qt::NoButton, 0, pos, delay); }
153 #ifdef QT_WIDGETS_LIB
154 static void mouseEvent(MouseAction action, QWidget *widget, Qt::MouseButton button,
155 Qt::KeyboardModifiers stateKey, QPoint pos, int delay=-1)
157 QTEST_ASSERT(widget);
158 extern int Q_TESTLIB_EXPORT defaultMouseDelay();
160 if (delay == -1 || delay < defaultMouseDelay())
161 delay = defaultMouseDelay();
166 pos = widget->rect().center();
168 if (action == MouseClick) {
169 mouseEvent(MousePress, widget, button, stateKey, pos);
170 mouseEvent(MouseRelease, widget, button, stateKey, pos);
174 QTEST_ASSERT(stateKey == 0 || stateKey & Qt::KeyboardModifierMask);
176 stateKey &= static_cast<unsigned int>(Qt::KeyboardModifierMask);
178 QMouseEvent me(QEvent::User, QPoint(), Qt::LeftButton, button, stateKey);
182 me = QMouseEvent(QEvent::MouseButtonPress, pos, widget->mapToGlobal(pos), button, button, stateKey);
185 me = QMouseEvent(QEvent::MouseButtonRelease, pos, widget->mapToGlobal(pos), button, 0, stateKey);
188 me = QMouseEvent(QEvent::MouseButtonDblClick, pos, widget->mapToGlobal(pos), button, button, stateKey);
191 QCursor::setPos(widget->mapToGlobal(pos));
195 qApp->processEvents();
201 QSpontaneKeyEvent::setSpontaneous(&me);
202 if (!qApp->notify(widget, &me)) {
203 static const char *mouseActionNames[] =
204 { "MousePress", "MouseRelease", "MouseClick", "MouseDClick", "MouseMove" };
205 QString warning = QString::fromLatin1("Mouse event \"%1\" not accepted by receiving widget");
206 QTest::qWarn(warning.arg(QString::fromLatin1(mouseActionNames[static_cast<int>(action)])).toLatin1().data());
211 inline void mousePress(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = 0,
212 QPoint pos = QPoint(), int delay=-1)
213 { mouseEvent(MousePress, widget, button, stateKey, pos, delay); }
214 inline void mouseRelease(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = 0,
215 QPoint pos = QPoint(), int delay=-1)
216 { mouseEvent(MouseRelease, widget, button, stateKey, pos, delay); }
217 inline void mouseClick(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = 0,
218 QPoint pos = QPoint(), int delay=-1)
219 { mouseEvent(MouseClick, widget, button, stateKey, pos, delay); }
220 inline void mouseDClick(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = 0,
221 QPoint pos = QPoint(), int delay=-1)
222 { mouseEvent(MouseDClick, widget, button, stateKey, pos, delay); }
223 inline void mouseMove(QWidget *widget, QPoint pos = QPoint(), int delay=-1)
224 { mouseEvent(MouseMove, widget, Qt::NoButton, 0, pos, delay); }
225 #endif // QT_WIDGETS_LIB
232 #endif // QTESTMOUSE_H