1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "quicktestevent_p.h"
43 #include <QtTest/qtestkeyboard.h>
44 #include <QtQml/qqml.h>
45 #if defined(QML_VERSION) && QML_VERSION >= 0x020000
46 #include <QtQuick/qquickitem.h>
47 #include <QtQuick/qquickcanvas.h>
48 #define QUICK_TEST_SCENEGRAPH 1
50 #include <QtWidgets/qgraphicsscene.h>
54 QuickTestEvent::QuickTestEvent(QObject *parent)
59 QuickTestEvent::~QuickTestEvent()
63 bool QuickTestEvent::keyPress(int key, int modifiers, int delay)
65 QWindow *window = eventWindow();
68 QTest::keyPress(window, Qt::Key(key), Qt::KeyboardModifiers(modifiers), delay);
72 bool QuickTestEvent::keyRelease(int key, int modifiers, int delay)
74 QWindow *window = eventWindow();
77 QTest::keyRelease(window, Qt::Key(key), Qt::KeyboardModifiers(modifiers), delay);
81 bool QuickTestEvent::keyClick(int key, int modifiers, int delay)
83 QWindow *window = eventWindow();
86 QTest::keyClick(window, Qt::Key(key), Qt::KeyboardModifiers(modifiers), delay);
91 extern int Q_TESTLIB_EXPORT defaultMouseDelay();
96 enum MouseAction { MousePress, MouseRelease, MouseClick, MouseDoubleClick, MouseMove };
98 static void mouseEvent(MouseAction action, QWindow *window,
99 QObject *item, Qt::MouseButton button,
100 Qt::KeyboardModifiers stateKey, QPointF _pos, int delay=-1)
102 QTEST_ASSERT(window);
105 if (delay == -1 || delay < QTest::defaultMouseDelay())
106 delay = QTest::defaultMouseDelay();
110 if (action == MouseClick) {
111 mouseEvent(MousePress, window, item, button, stateKey, _pos);
112 mouseEvent(MouseRelease, window, item, button, stateKey, _pos);
117 #ifdef QUICK_TEST_SCENEGRAPH
118 QQuickItem *sgitem = qobject_cast<QQuickItem *>(item);
120 pos = sgitem->mapToScene(_pos).toPoint();
124 qWarning("No suitable QtQuick1 implementation is available!");
126 QTEST_ASSERT(button == Qt::NoButton || button & Qt::MouseButtonMask);
127 QTEST_ASSERT(stateKey == 0 || stateKey & Qt::KeyboardModifierMask);
129 stateKey &= static_cast<unsigned int>(Qt::KeyboardModifierMask);
131 QMouseEvent me(QEvent::User, QPoint(), Qt::LeftButton, button, stateKey);
135 me = QMouseEvent(QEvent::MouseButtonPress, pos, window->mapToGlobal(pos), button, button, stateKey);
138 me = QMouseEvent(QEvent::MouseButtonRelease, pos, window->mapToGlobal(pos), button, 0, stateKey);
140 case MouseDoubleClick:
141 me = QMouseEvent(QEvent::MouseButtonDblClick, pos, window->mapToGlobal(pos), button, button, stateKey);
144 // with move event the button is NoButton, but 'buttons' holds the currently pressed buttons
145 me = QMouseEvent(QEvent::MouseMove, pos, window->mapToGlobal(pos), Qt::NoButton, button, stateKey);
150 QSpontaneKeyEvent::setSpontaneous(&me);
151 if (!qApp->notify(window, &me)) {
152 static const char *mouseActionNames[] =
153 { "MousePress", "MouseRelease", "MouseClick", "MouseDoubleClick", "MouseMove" };
154 QString warning = QString::fromLatin1("Mouse event \"%1\" not accepted by receiving window");
155 QWARN(warning.arg(QString::fromLatin1(mouseActionNames[static_cast<int>(action)])).toAscii().data());
159 static void mouseWheel(QWindow* window, QObject* item, Qt::MouseButtons buttons,
160 Qt::KeyboardModifiers stateKey,
161 QPointF _pos, int delta, int delay = -1, Qt::Orientation orientation = Qt::Vertical)
163 QTEST_ASSERT(window);
165 if (delay == -1 || delay < QTest::defaultMouseDelay())
166 delay = QTest::defaultMouseDelay();
171 #ifdef QUICK_TEST_SCENEGRAPH
172 QQuickItem *sgitem = qobject_cast<QQuickItem *>(item);
174 pos = sgitem->mapToScene(_pos).toPoint();
178 qWarning("No suitable QtQuick1 implementation is available!");
180 QTEST_ASSERT(buttons == Qt::NoButton || buttons & Qt::MouseButtonMask);
181 QTEST_ASSERT(stateKey == 0 || stateKey & Qt::KeyboardModifierMask);
183 stateKey &= static_cast<unsigned int>(Qt::KeyboardModifierMask);
184 QWheelEvent we(pos, window->mapToGlobal(pos), delta, buttons, stateKey, orientation);
186 QSpontaneKeyEvent::setSpontaneous(&we); // hmmmm
187 if (!qApp->notify(window, &we))
188 QTest::qWarn("Wheel event not accepted by receiving window");
192 bool QuickTestEvent::mousePress
193 (QObject *item, qreal x, qreal y, int button,
194 int modifiers, int delay)
196 QWindow *view = eventWindow();
199 QtQuickTest::mouseEvent(QtQuickTest::MousePress, view, item,
200 Qt::MouseButton(button),
201 Qt::KeyboardModifiers(modifiers),
202 QPointF(x, y), delay);
206 bool QuickTestEvent::mouseWheel(
207 QObject *item, qreal x, qreal y, int buttons,
208 int modifiers, int delta, int delay, int orientation)
210 QWindow *view = eventWindow();
213 QtQuickTest::mouseWheel(view, item, Qt::MouseButtons(buttons),
214 Qt::KeyboardModifiers(modifiers),
215 QPointF(x, y), delta, delay, Qt::Orientation(orientation));
219 bool QuickTestEvent::mouseRelease
220 (QObject *item, qreal x, qreal y, int button,
221 int modifiers, int delay)
223 QWindow *view = eventWindow();
226 QtQuickTest::mouseEvent(QtQuickTest::MouseRelease, view, item,
227 Qt::MouseButton(button),
228 Qt::KeyboardModifiers(modifiers),
229 QPointF(x, y), delay);
233 bool QuickTestEvent::mouseClick
234 (QObject *item, qreal x, qreal y, int button,
235 int modifiers, int delay)
237 QWindow *view = eventWindow();
240 QtQuickTest::mouseEvent(QtQuickTest::MouseClick, view, item,
241 Qt::MouseButton(button),
242 Qt::KeyboardModifiers(modifiers),
243 QPointF(x, y), delay);
247 bool QuickTestEvent::mouseDoubleClick
248 (QObject *item, qreal x, qreal y, int button,
249 int modifiers, int delay)
251 QWindow *view = eventWindow();
254 QtQuickTest::mouseEvent(QtQuickTest::MouseDoubleClick, view, item,
255 Qt::MouseButton(button),
256 Qt::KeyboardModifiers(modifiers),
257 QPointF(x, y), delay);
261 bool QuickTestEvent::mouseMove
262 (QObject *item, qreal x, qreal y, int delay, int buttons)
264 QWindow *view = eventWindow();
267 QtQuickTest::mouseEvent(QtQuickTest::MouseMove, view, item,
268 Qt::MouseButton(buttons), Qt::NoModifier,
269 QPointF(x, y), delay);
273 QWindow *QuickTestEvent::eventWindow()
275 #ifdef QUICK_TEST_SCENEGRAPH
276 QQuickItem *sgitem = qobject_cast<QQuickItem *>(parent());
278 return sgitem->canvas();
282 QQuickItem *item = qobject_cast<QQuickItem *>(parent());
285 QGraphicsScene *s = item->scene();
288 QList<QGraphicsView *> views = s->views();
291 return views.at(0)->windowHandle();