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