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 #include <QtQuick/qquickitem.h>
46 #include <QtQuick/qquickcanvas.h>
50 QuickTestEvent::QuickTestEvent(QObject *parent)
55 QuickTestEvent::~QuickTestEvent()
59 bool QuickTestEvent::keyPress(int key, int modifiers, int delay)
61 QWindow *window = eventWindow();
64 QTest::keyPress(window, Qt::Key(key), Qt::KeyboardModifiers(modifiers), delay);
68 bool QuickTestEvent::keyRelease(int key, int modifiers, int delay)
70 QWindow *window = eventWindow();
73 QTest::keyRelease(window, Qt::Key(key), Qt::KeyboardModifiers(modifiers), delay);
77 bool QuickTestEvent::keyClick(int key, int modifiers, int delay)
79 QWindow *window = eventWindow();
82 QTest::keyClick(window, Qt::Key(key), Qt::KeyboardModifiers(modifiers), delay);
87 extern int Q_TESTLIB_EXPORT defaultMouseDelay();
92 enum MouseAction { MousePress, MouseRelease, MouseClick, MouseDoubleClick, MouseMove };
94 static void mouseEvent(MouseAction action, QWindow *window,
95 QObject *item, Qt::MouseButton button,
96 Qt::KeyboardModifiers stateKey, QPointF _pos, int delay=-1)
101 if (delay == -1 || delay < QTest::defaultMouseDelay())
102 delay = QTest::defaultMouseDelay();
106 if (action == MouseClick) {
107 mouseEvent(MousePress, window, item, button, stateKey, _pos);
108 mouseEvent(MouseRelease, window, item, button, stateKey, _pos);
113 QQuickItem *sgitem = qobject_cast<QQuickItem *>(item);
115 pos = sgitem->mapToScene(_pos).toPoint();
116 QTEST_ASSERT(button == Qt::NoButton || button & Qt::MouseButtonMask);
117 QTEST_ASSERT(stateKey == 0 || stateKey & Qt::KeyboardModifierMask);
119 stateKey &= static_cast<unsigned int>(Qt::KeyboardModifierMask);
121 QMouseEvent me(QEvent::User, QPoint(), Qt::LeftButton, button, stateKey);
125 me = QMouseEvent(QEvent::MouseButtonPress, pos, window->mapToGlobal(pos), button, button, stateKey);
128 me = QMouseEvent(QEvent::MouseButtonRelease, pos, window->mapToGlobal(pos), button, 0, stateKey);
130 case MouseDoubleClick:
131 me = QMouseEvent(QEvent::MouseButtonDblClick, pos, window->mapToGlobal(pos), button, button, stateKey);
134 // with move event the button is NoButton, but 'buttons' holds the currently pressed buttons
135 me = QMouseEvent(QEvent::MouseMove, pos, window->mapToGlobal(pos), Qt::NoButton, button, stateKey);
140 QSpontaneKeyEvent::setSpontaneous(&me);
141 if (!qApp->notify(window, &me)) {
142 static const char *mouseActionNames[] =
143 { "MousePress", "MouseRelease", "MouseClick", "MouseDoubleClick", "MouseMove" };
144 QString warning = QString::fromLatin1("Mouse event \"%1\" not accepted by receiving window");
145 QWARN(warning.arg(QString::fromLatin1(mouseActionNames[static_cast<int>(action)])).toAscii().data());
149 static void mouseWheel(QWindow* window, QObject* item, Qt::MouseButtons buttons,
150 Qt::KeyboardModifiers stateKey,
151 QPointF _pos, int delta, int delay = -1, Qt::Orientation orientation = Qt::Vertical)
153 QTEST_ASSERT(window);
155 if (delay == -1 || delay < QTest::defaultMouseDelay())
156 delay = QTest::defaultMouseDelay();
161 QQuickItem *sgitem = qobject_cast<QQuickItem *>(item);
163 pos = sgitem->mapToScene(_pos).toPoint();
165 QTEST_ASSERT(buttons == Qt::NoButton || buttons & Qt::MouseButtonMask);
166 QTEST_ASSERT(stateKey == 0 || stateKey & Qt::KeyboardModifierMask);
168 stateKey &= static_cast<unsigned int>(Qt::KeyboardModifierMask);
169 QWheelEvent we(pos, window->mapToGlobal(pos), delta, buttons, stateKey, orientation);
171 QSpontaneKeyEvent::setSpontaneous(&we); // hmmmm
172 if (!qApp->notify(window, &we))
173 QTest::qWarn("Wheel event not accepted by receiving window");
177 bool QuickTestEvent::mousePress
178 (QObject *item, qreal x, qreal y, int button,
179 int modifiers, int delay)
181 QWindow *view = eventWindow();
184 QtQuickTest::mouseEvent(QtQuickTest::MousePress, view, item,
185 Qt::MouseButton(button),
186 Qt::KeyboardModifiers(modifiers),
187 QPointF(x, y), delay);
191 bool QuickTestEvent::mouseWheel(
192 QObject *item, qreal x, qreal y, int buttons,
193 int modifiers, int delta, int delay, int orientation)
195 QWindow *view = eventWindow();
198 QtQuickTest::mouseWheel(view, item, Qt::MouseButtons(buttons),
199 Qt::KeyboardModifiers(modifiers),
200 QPointF(x, y), delta, delay, Qt::Orientation(orientation));
204 bool QuickTestEvent::mouseRelease
205 (QObject *item, qreal x, qreal y, int button,
206 int modifiers, int delay)
208 QWindow *view = eventWindow();
211 QtQuickTest::mouseEvent(QtQuickTest::MouseRelease, view, item,
212 Qt::MouseButton(button),
213 Qt::KeyboardModifiers(modifiers),
214 QPointF(x, y), delay);
218 bool QuickTestEvent::mouseClick
219 (QObject *item, qreal x, qreal y, int button,
220 int modifiers, int delay)
222 QWindow *view = eventWindow();
225 QtQuickTest::mouseEvent(QtQuickTest::MouseClick, view, item,
226 Qt::MouseButton(button),
227 Qt::KeyboardModifiers(modifiers),
228 QPointF(x, y), delay);
232 bool QuickTestEvent::mouseDoubleClick
233 (QObject *item, qreal x, qreal y, int button,
234 int modifiers, int delay)
236 QWindow *view = eventWindow();
239 QtQuickTest::mouseEvent(QtQuickTest::MouseDoubleClick, view, item,
240 Qt::MouseButton(button),
241 Qt::KeyboardModifiers(modifiers),
242 QPointF(x, y), delay);
246 bool QuickTestEvent::mouseMove
247 (QObject *item, qreal x, qreal y, int delay, int buttons)
249 QWindow *view = eventWindow();
252 QtQuickTest::mouseEvent(QtQuickTest::MouseMove, view, item,
253 Qt::MouseButton(buttons), Qt::NoModifier,
254 QPointF(x, y), delay);
258 QWindow *QuickTestEvent::eventWindow()
260 QQuickItem *sgitem = qobject_cast<QQuickItem *>(parent());
262 return sgitem->canvas();