Doc: Enabling Qt QML linking to Qt Quick.
[profile/ivi/qtdeclarative.git] / src / qmltest / quicktestevent.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.  For licensing terms and
14 ** conditions see http://qt.digia.com/licensing.  For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights.  These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file.  Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
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 #include <QtQuick/qquickitem.h>
46 #include <QtQuick/qquickwindow.h>
47
48 QT_BEGIN_NAMESPACE
49
50 QuickTestEvent::QuickTestEvent(QObject *parent)
51     : QObject(parent)
52 {
53 }
54
55 QuickTestEvent::~QuickTestEvent()
56 {
57 }
58
59 bool QuickTestEvent::keyPress(int key, int modifiers, int delay)
60 {
61     QWindow *window = eventWindow();
62     if (!window)
63         return false;
64     QTest::keyPress(window, Qt::Key(key), Qt::KeyboardModifiers(modifiers), delay);
65     return true;
66 }
67
68 bool QuickTestEvent::keyRelease(int key, int modifiers, int delay)
69 {
70     QWindow *window = eventWindow();
71     if (!window)
72         return false;
73     QTest::keyRelease(window, Qt::Key(key), Qt::KeyboardModifiers(modifiers), delay);
74     return true;
75 }
76
77 bool QuickTestEvent::keyClick(int key, int modifiers, int delay)
78 {
79     QWindow *window = eventWindow();
80     if (!window)
81         return false;
82     QTest::keyClick(window, Qt::Key(key), Qt::KeyboardModifiers(modifiers), delay);
83     return true;
84 }
85
86 namespace QTest {
87     extern int Q_TESTLIB_EXPORT defaultMouseDelay();
88 };
89
90 namespace QtQuickTest
91 {
92     enum MouseAction { MousePress, MouseRelease, MouseClick, MouseDoubleClick, MouseMove };
93
94     static void mouseEvent(MouseAction action, QWindow *window,
95                            QObject *item, Qt::MouseButton button,
96                            Qt::KeyboardModifiers stateKey, QPointF _pos, int delay=-1)
97     {
98         QTEST_ASSERT(window);
99         QTEST_ASSERT(item);
100
101         if (delay == -1 || delay < QTest::defaultMouseDelay())
102             delay = QTest::defaultMouseDelay();
103         if (delay > 0)
104             QTest::qWait(delay);
105
106         if (action == MouseClick) {
107             mouseEvent(MousePress, window, item, button, stateKey, _pos);
108             mouseEvent(MouseRelease, window, item, button, stateKey, _pos);
109             return;
110         }
111
112         QPoint pos;
113         QQuickItem *sgitem = qobject_cast<QQuickItem *>(item);
114         if (sgitem)
115             pos = sgitem->mapToScene(_pos).toPoint();
116         QTEST_ASSERT(button == Qt::NoButton || button & Qt::MouseButtonMask);
117         QTEST_ASSERT(stateKey == 0 || stateKey & Qt::KeyboardModifierMask);
118
119         stateKey &= static_cast<unsigned int>(Qt::KeyboardModifierMask);
120
121         QMouseEvent me(QEvent::User, QPoint(), Qt::LeftButton, button, stateKey);
122         switch (action)
123         {
124             case MousePress:
125                 me = QMouseEvent(QEvent::MouseButtonPress, pos, window->mapToGlobal(pos), button, button, stateKey);
126                 break;
127             case MouseRelease:
128                 me = QMouseEvent(QEvent::MouseButtonRelease, pos, window->mapToGlobal(pos), button, 0, stateKey);
129                 break;
130             case MouseDoubleClick:
131                 me = QMouseEvent(QEvent::MouseButtonDblClick, pos, window->mapToGlobal(pos), button, button, stateKey);
132                 break;
133             case MouseMove:
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);
136                 break;
137             default:
138                 QTEST_ASSERT(false);
139         }
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)])).toLatin1().data());
146         }
147     }
148
149     static void mouseWheel(QWindow* window, QObject* item, Qt::MouseButtons buttons,
150                                 Qt::KeyboardModifiers stateKey,
151                                 QPointF _pos, int xDelta, int yDelta, int delay = -1)
152     {
153         QTEST_ASSERT(window);
154         QTEST_ASSERT(item);
155         if (delay == -1 || delay < QTest::defaultMouseDelay())
156             delay = QTest::defaultMouseDelay();
157         if (delay > 0)
158             QTest::qWait(delay);
159
160         QPoint pos;
161         QQuickItem *sgitem = qobject_cast<QQuickItem *>(item);
162         if (sgitem)
163             pos = sgitem->mapToScene(_pos).toPoint();
164
165         QTEST_ASSERT(buttons == Qt::NoButton || buttons & Qt::MouseButtonMask);
166         QTEST_ASSERT(stateKey == 0 || stateKey & Qt::KeyboardModifierMask);
167
168         stateKey &= static_cast<unsigned int>(Qt::KeyboardModifierMask);
169         QWheelEvent we(pos, window->mapToGlobal(pos), QPoint(0, 0), QPoint(xDelta, yDelta), 0, Qt::Vertical, buttons, stateKey);
170
171         QSpontaneKeyEvent::setSpontaneous(&we); // hmmmm
172         if (!qApp->notify(window, &we))
173             QTest::qWarn("Wheel event not accepted by receiving window");
174     }
175 };
176
177 bool QuickTestEvent::mousePress
178     (QObject *item, qreal x, qreal y, int button,
179      int modifiers, int delay)
180 {
181     QWindow *view = eventWindow();
182     if (!view)
183         return false;
184     QtQuickTest::mouseEvent(QtQuickTest::MousePress, view, item,
185                             Qt::MouseButton(button),
186                             Qt::KeyboardModifiers(modifiers),
187                             QPointF(x, y), delay);
188     return true;
189 }
190
191 bool QuickTestEvent::mouseWheel(
192     QObject *item, qreal x, qreal y, int buttons,
193     int modifiers, int xDelta, int yDelta, int delay)
194 {
195     QWindow *view = eventWindow();
196     if (!view)
197         return false;
198     QtQuickTest::mouseWheel(view, item, Qt::MouseButtons(buttons),
199                             Qt::KeyboardModifiers(modifiers),
200                             QPointF(x, y), xDelta, yDelta, delay);
201     return true;
202 }
203
204 bool QuickTestEvent::mouseRelease
205     (QObject *item, qreal x, qreal y, int button,
206      int modifiers, int delay)
207 {
208     QWindow *view = eventWindow();
209     if (!view)
210         return false;
211     QtQuickTest::mouseEvent(QtQuickTest::MouseRelease, view, item,
212                             Qt::MouseButton(button),
213                             Qt::KeyboardModifiers(modifiers),
214                             QPointF(x, y), delay);
215     return true;
216 }
217
218 bool QuickTestEvent::mouseClick
219     (QObject *item, qreal x, qreal y, int button,
220      int modifiers, int delay)
221 {
222     QWindow *view = eventWindow();
223     if (!view)
224         return false;
225     QtQuickTest::mouseEvent(QtQuickTest::MouseClick, view, item,
226                             Qt::MouseButton(button),
227                             Qt::KeyboardModifiers(modifiers),
228                             QPointF(x, y), delay);
229     return true;
230 }
231
232 bool QuickTestEvent::mouseDoubleClick
233     (QObject *item, qreal x, qreal y, int button,
234      int modifiers, int delay)
235 {
236     QWindow *view = eventWindow();
237     if (!view)
238         return false;
239     QtQuickTest::mouseEvent(QtQuickTest::MouseDoubleClick, view, item,
240                             Qt::MouseButton(button),
241                             Qt::KeyboardModifiers(modifiers),
242                             QPointF(x, y), delay);
243     return true;
244 }
245
246 bool QuickTestEvent::mouseMove
247     (QObject *item, qreal x, qreal y, int delay, int buttons)
248 {
249     QWindow *view = eventWindow();
250     if (!view)
251         return false;
252     QtQuickTest::mouseEvent(QtQuickTest::MouseMove, view, item,
253                             Qt::MouseButton(buttons), Qt::NoModifier,
254                             QPointF(x, y), delay);
255     return true;
256 }
257
258 QWindow *QuickTestEvent::eventWindow()
259 {
260     QQuickItem *sgitem = qobject_cast<QQuickItem *>(parent());
261     if (sgitem)
262         return sgitem->window();
263     return 0;
264 }
265
266 QT_END_NAMESPACE