f996adbeca5630025f486d308b5ba77762e51b1f
[profile/ivi/qtdeclarative.git] / src / qmltest / quicktestevent.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
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
49 #endif
50 #include <QtWidgets/qgraphicsscene.h>
51
52 QT_BEGIN_NAMESPACE
53
54 QuickTestEvent::QuickTestEvent(QObject *parent)
55     : QObject(parent)
56 {
57 }
58
59 QuickTestEvent::~QuickTestEvent()
60 {
61 }
62
63 bool QuickTestEvent::keyPress(int key, int modifiers, int delay)
64 {
65     QWindow *window = eventWindow();
66     if (!window)
67         return false;
68     QTest::keyPress(window, Qt::Key(key), Qt::KeyboardModifiers(modifiers), delay);
69     return true;
70 }
71
72 bool QuickTestEvent::keyRelease(int key, int modifiers, int delay)
73 {
74     QWindow *window = eventWindow();
75     if (!window)
76         return false;
77     QTest::keyRelease(window, Qt::Key(key), Qt::KeyboardModifiers(modifiers), delay);
78     return true;
79 }
80
81 bool QuickTestEvent::keyClick(int key, int modifiers, int delay)
82 {
83     QWindow *window = eventWindow();
84     if (!window)
85         return false;
86     QTest::keyClick(window, Qt::Key(key), Qt::KeyboardModifiers(modifiers), delay);
87     return true;
88 }
89
90 namespace QTest {
91     extern int Q_TESTLIB_EXPORT defaultMouseDelay();
92 };
93
94 namespace QtQuickTest
95 {
96     enum MouseAction { MousePress, MouseRelease, MouseClick, MouseDoubleClick, MouseMove };
97
98     static void mouseEvent(MouseAction action, QWindow *window,
99                            QObject *item, Qt::MouseButton button,
100                            Qt::KeyboardModifiers stateKey, QPointF _pos, int delay=-1)
101     {
102         QTEST_ASSERT(window);
103         QTEST_ASSERT(item);
104
105         if (delay == -1 || delay < QTest::defaultMouseDelay())
106             delay = QTest::defaultMouseDelay();
107         if (delay > 0)
108             QTest::qWait(delay);
109
110         if (action == MouseClick) {
111             mouseEvent(MousePress, window, item, button, stateKey, _pos);
112             mouseEvent(MouseRelease, window, item, button, stateKey, _pos);
113             return;
114         }
115
116         QPoint pos;
117 #ifdef QUICK_TEST_SCENEGRAPH
118         QQuickItem *sgitem = qobject_cast<QQuickItem *>(item);
119         if (sgitem) {
120             pos = sgitem->mapToScene(_pos).toPoint();
121         } else
122 #endif
123         {
124             qWarning("No suitable QtQuick1 implementation is available!");
125         }
126         QTEST_ASSERT(button == Qt::NoButton || button & Qt::MouseButtonMask);
127         QTEST_ASSERT(stateKey == 0 || stateKey & Qt::KeyboardModifierMask);
128
129         stateKey &= static_cast<unsigned int>(Qt::KeyboardModifierMask);
130
131         QMouseEvent me(QEvent::User, QPoint(), Qt::LeftButton, button, stateKey);
132         switch (action)
133         {
134             case MousePress:
135                 me = QMouseEvent(QEvent::MouseButtonPress, pos, window->mapToGlobal(pos), button, button, stateKey);
136                 break;
137             case MouseRelease:
138                 me = QMouseEvent(QEvent::MouseButtonRelease, pos, window->mapToGlobal(pos), button, 0, stateKey);
139                 break;
140             case MouseDoubleClick:
141                 me = QMouseEvent(QEvent::MouseButtonDblClick, pos, window->mapToGlobal(pos), button, button, stateKey);
142                 break;
143             case MouseMove:
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);
146                 break;
147             default:
148                 QTEST_ASSERT(false);
149         }
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());
156         }
157     }
158
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)
162     {
163         QTEST_ASSERT(window);
164         QTEST_ASSERT(item);
165         if (delay == -1 || delay < QTest::defaultMouseDelay())
166             delay = QTest::defaultMouseDelay();
167         if (delay > 0)
168             QTest::qWait(delay);
169
170         QPoint pos;
171 #ifdef QUICK_TEST_SCENEGRAPH
172         QQuickItem *sgitem = qobject_cast<QQuickItem *>(item);
173         if (sgitem) {
174             pos = sgitem->mapToScene(_pos).toPoint();
175         } else
176 #endif
177         {
178             qWarning("No suitable QtQuick1 implementation is available!");
179         }
180         QTEST_ASSERT(buttons == Qt::NoButton || buttons & Qt::MouseButtonMask);
181         QTEST_ASSERT(stateKey == 0 || stateKey & Qt::KeyboardModifierMask);
182
183         stateKey &= static_cast<unsigned int>(Qt::KeyboardModifierMask);
184         QWheelEvent we(pos, window->mapToGlobal(pos), delta, buttons, stateKey, orientation);
185
186         QSpontaneKeyEvent::setSpontaneous(&we); // hmmmm
187         if (!qApp->notify(window, &we))
188             QTest::qWarn("Wheel event not accepted by receiving window");
189     }
190 };
191
192 bool QuickTestEvent::mousePress
193     (QObject *item, qreal x, qreal y, int button,
194      int modifiers, int delay)
195 {
196     QWindow *view = eventWindow();
197     if (!view)
198         return false;
199     QtQuickTest::mouseEvent(QtQuickTest::MousePress, view, item,
200                             Qt::MouseButton(button),
201                             Qt::KeyboardModifiers(modifiers),
202                             QPointF(x, y), delay);
203     return true;
204 }
205
206 bool QuickTestEvent::mouseWheel(
207     QObject *item, qreal x, qreal y, int buttons,
208     int modifiers, int delta, int delay, int orientation)
209 {
210     QWindow *view = eventWindow();
211     if (!view)
212         return false;
213     QtQuickTest::mouseWheel(view, item, Qt::MouseButtons(buttons),
214                             Qt::KeyboardModifiers(modifiers),
215                             QPointF(x, y), delta, delay, Qt::Orientation(orientation));
216     return true;
217 }
218
219 bool QuickTestEvent::mouseRelease
220     (QObject *item, qreal x, qreal y, int button,
221      int modifiers, int delay)
222 {
223     QWindow *view = eventWindow();
224     if (!view)
225         return false;
226     QtQuickTest::mouseEvent(QtQuickTest::MouseRelease, view, item,
227                             Qt::MouseButton(button),
228                             Qt::KeyboardModifiers(modifiers),
229                             QPointF(x, y), delay);
230     return true;
231 }
232
233 bool QuickTestEvent::mouseClick
234     (QObject *item, qreal x, qreal y, int button,
235      int modifiers, int delay)
236 {
237     QWindow *view = eventWindow();
238     if (!view)
239         return false;
240     QtQuickTest::mouseEvent(QtQuickTest::MouseClick, view, item,
241                             Qt::MouseButton(button),
242                             Qt::KeyboardModifiers(modifiers),
243                             QPointF(x, y), delay);
244     return true;
245 }
246
247 bool QuickTestEvent::mouseDoubleClick
248     (QObject *item, qreal x, qreal y, int button,
249      int modifiers, int delay)
250 {
251     QWindow *view = eventWindow();
252     if (!view)
253         return false;
254     QtQuickTest::mouseEvent(QtQuickTest::MouseDoubleClick, view, item,
255                             Qt::MouseButton(button),
256                             Qt::KeyboardModifiers(modifiers),
257                             QPointF(x, y), delay);
258     return true;
259 }
260
261 bool QuickTestEvent::mouseMove
262     (QObject *item, qreal x, qreal y, int delay, int buttons)
263 {
264     QWindow *view = eventWindow();
265     if (!view)
266         return false;
267     QtQuickTest::mouseEvent(QtQuickTest::MouseMove, view, item,
268                             Qt::MouseButton(buttons), Qt::NoModifier,
269                             QPointF(x, y), delay);
270     return true;
271 }
272
273 QWindow *QuickTestEvent::eventWindow()
274 {
275 #ifdef QUICK_TEST_SCENEGRAPH
276     QQuickItem *sgitem = qobject_cast<QQuickItem *>(parent());
277     if (sgitem)
278         return sgitem->canvas();
279 #endif
280     return 0;
281     /*
282     QQuickItem *item = qobject_cast<QQuickItem *>(parent());
283     if (!item)
284         return 0;
285     QGraphicsScene *s = item->scene();
286     if (!s)
287         return 0;
288     QList<QGraphicsView *> views = s->views();
289     if (views.isEmpty())
290         return 0;
291     return views.at(0)->windowHandle();
292     */
293 }
294
295 QT_END_NAMESPACE