1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the test suite of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "quicktestevent_p.h"
43 #include <QtTest/qtestkeyboard.h>
44 #include <QtDeclarative/qdeclarative.h>
45 #include <QtQuick1/qdeclarativeitem.h>
46 #include <QtQuick1/qdeclarativeview.h>
47 #if defined(QML_VERSION) && QML_VERSION >= 0x020000
48 #include <QtQuick/qquickitem.h>
49 #include <QtQuick/qquickcanvas.h>
50 #define QUICK_TEST_SCENEGRAPH 1
52 #include <QtWidgets/qgraphicsscene.h>
56 QuickTestEvent::QuickTestEvent(QObject *parent)
61 QuickTestEvent::~QuickTestEvent()
65 bool QuickTestEvent::keyPress(int key, int modifiers, int delay)
67 QWindow *window = eventWindow();
70 QTest::keyPress(window, Qt::Key(key), Qt::KeyboardModifiers(modifiers), delay);
74 bool QuickTestEvent::keyRelease(int key, int modifiers, int delay)
76 QWindow *window = eventWindow();
79 QTest::keyRelease(window, Qt::Key(key), Qt::KeyboardModifiers(modifiers), delay);
83 bool QuickTestEvent::keyClick(int key, int modifiers, int delay)
85 QWindow *window = eventWindow();
88 QTest::keyClick(window, Qt::Key(key), Qt::KeyboardModifiers(modifiers), delay);
93 extern int Q_TESTLIB_EXPORT defaultMouseDelay();
98 enum MouseAction { MousePress, MouseRelease, MouseClick, MouseDoubleClick, MouseMove };
100 static void mouseEvent(MouseAction action, QWindow *window,
101 QObject *item, Qt::MouseButton button,
102 Qt::KeyboardModifiers stateKey, QPointF _pos, int delay=-1)
104 QTEST_ASSERT(window);
107 if (delay == -1 || delay < QTest::defaultMouseDelay())
108 delay = QTest::defaultMouseDelay();
112 if (action == MouseClick) {
113 mouseEvent(MousePress, window, item, button, stateKey, _pos);
114 mouseEvent(MouseRelease, window, item, button, stateKey, _pos);
119 QDeclarativeView *view = qobject_cast<QDeclarativeView *>(window);
120 QWindow *eventWindow = window;
121 #ifdef QUICK_TEST_SCENEGRAPH
122 QQuickItem *sgitem = qobject_cast<QQuickItem *>(item);
124 pos = sgitem->mapToScene(_pos).toPoint();
128 QDeclarativeItem *ditem = qobject_cast<QDeclarativeItem *>(item);
130 qWarning("Mouse event target is not an Item");
133 pos = view->mapFromScene(ditem->mapToScene(_pos));
134 eventWindow = view->viewport()->windowHandle();
136 QTEST_ASSERT(button == Qt::NoButton || button & Qt::MouseButtonMask);
137 QTEST_ASSERT(stateKey == 0 || stateKey & Qt::KeyboardModifierMask);
139 stateKey &= static_cast<unsigned int>(Qt::KeyboardModifierMask);
141 QMouseEvent me(QEvent::User, QPoint(), Qt::LeftButton, button, stateKey);
145 me = QMouseEvent(QEvent::MouseButtonPress, pos, window->mapToGlobal(pos), button, button, stateKey);
148 me = QMouseEvent(QEvent::MouseButtonRelease, pos, window->mapToGlobal(pos), button, 0, stateKey);
150 case MouseDoubleClick:
151 me = QMouseEvent(QEvent::MouseButtonDblClick, pos, window->mapToGlobal(pos), button, button, stateKey);
154 // with move event the button is NoButton, but 'buttons' holds the currently pressed buttons
155 me = QMouseEvent(QEvent::MouseMove, pos, window->mapToGlobal(pos), Qt::NoButton, button, stateKey);
160 QSpontaneKeyEvent::setSpontaneous(&me);
161 if (!qApp->notify(eventWindow, &me)) {
162 static const char *mouseActionNames[] =
163 { "MousePress", "MouseRelease", "MouseClick", "MouseDoubleClick", "MouseMove" };
164 QString warning = QString::fromLatin1("Mouse event \"%1\" not accepted by receiving window");
165 QWARN(warning.arg(QString::fromLatin1(mouseActionNames[static_cast<int>(action)])).toAscii().data());
169 static void mouseWheel(QWindow* window, QObject* item, Qt::MouseButtons buttons,
170 Qt::KeyboardModifiers stateKey,
171 QPointF _pos, int delta, int delay = -1, Qt::Orientation orientation = Qt::Vertical)
173 QTEST_ASSERT(window);
175 if (delay == -1 || delay < QTest::defaultMouseDelay())
176 delay = QTest::defaultMouseDelay();
181 QDeclarativeView *view = qobject_cast<QDeclarativeView *>(window);
182 QWindow *eventWindow = window;
183 #ifdef QUICK_TEST_SCENEGRAPH
184 QQuickItem *sgitem = qobject_cast<QQuickItem *>(item);
186 pos = sgitem->mapToScene(_pos).toPoint();
190 QDeclarativeItem *ditem = qobject_cast<QDeclarativeItem *>(item);
192 qWarning("Mouse event target is not an Item");
195 pos = view->mapFromScene(ditem->mapToScene(_pos));
196 eventWindow = view->viewport()->windowHandle();
198 QTEST_ASSERT(buttons == Qt::NoButton || buttons & Qt::MouseButtonMask);
199 QTEST_ASSERT(stateKey == 0 || stateKey & Qt::KeyboardModifierMask);
201 stateKey &= static_cast<unsigned int>(Qt::KeyboardModifierMask);
202 QWheelEvent we(pos, window->mapToGlobal(pos), delta, buttons, stateKey, orientation);
204 QSpontaneKeyEvent::setSpontaneous(&we); // hmmmm
205 if (!qApp->notify(eventWindow, &we))
206 QTest::qWarn("Wheel event not accepted by receiving window");
210 bool QuickTestEvent::mousePress
211 (QObject *item, qreal x, qreal y, int button,
212 int modifiers, int delay)
214 QWindow *view = eventWindow();
217 QtQuickTest::mouseEvent(QtQuickTest::MousePress, view, item,
218 Qt::MouseButton(button),
219 Qt::KeyboardModifiers(modifiers),
220 QPointF(x, y), delay);
224 bool QuickTestEvent::mouseWheel(
225 QObject *item, qreal x, qreal y, int buttons,
226 int modifiers, int delta, int delay, int orientation)
228 QWindow *view = eventWindow();
231 QtQuickTest::mouseWheel(view, item, Qt::MouseButtons(buttons),
232 Qt::KeyboardModifiers(modifiers),
233 QPointF(x, y), delta, delay, Qt::Orientation(orientation));
237 bool QuickTestEvent::mouseRelease
238 (QObject *item, qreal x, qreal y, int button,
239 int modifiers, int delay)
241 QWindow *view = eventWindow();
244 QtQuickTest::mouseEvent(QtQuickTest::MouseRelease, view, item,
245 Qt::MouseButton(button),
246 Qt::KeyboardModifiers(modifiers),
247 QPointF(x, y), delay);
251 bool QuickTestEvent::mouseClick
252 (QObject *item, qreal x, qreal y, int button,
253 int modifiers, int delay)
255 QWindow *view = eventWindow();
258 QtQuickTest::mouseEvent(QtQuickTest::MouseClick, view, item,
259 Qt::MouseButton(button),
260 Qt::KeyboardModifiers(modifiers),
261 QPointF(x, y), delay);
265 bool QuickTestEvent::mouseDoubleClick
266 (QObject *item, qreal x, qreal y, int button,
267 int modifiers, int delay)
269 QWindow *view = eventWindow();
272 QtQuickTest::mouseEvent(QtQuickTest::MouseDoubleClick, view, item,
273 Qt::MouseButton(button),
274 Qt::KeyboardModifiers(modifiers),
275 QPointF(x, y), delay);
279 bool QuickTestEvent::mouseMove
280 (QObject *item, qreal x, qreal y, int delay, int buttons)
282 QWindow *view = eventWindow();
285 QtQuickTest::mouseEvent(QtQuickTest::MouseMove, view, item,
286 Qt::MouseButton(buttons), Qt::NoModifier,
287 QPointF(x, y), delay);
291 QWindow *QuickTestEvent::eventWindow()
293 #ifdef QUICK_TEST_SCENEGRAPH
294 QQuickItem *sgitem = qobject_cast<QQuickItem *>(parent());
296 return sgitem->canvas();
298 QDeclarativeItem *item = qobject_cast<QDeclarativeItem *>(parent());
301 QGraphicsScene *s = item->scene();
304 QList<QGraphicsView *> views = s->views();
307 return views.at(0)->windowHandle();