Merge branch 'v8'
[profile/ivi/qtdeclarative.git] / src / qmltest / quicktestevent.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2010 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 <QtDeclarative/qdeclarativeitem.h>
46 #include <QtDeclarative/qdeclarativeview.h>
47 #if defined(QML_VERSION) && QML_VERSION >= 0x020000
48 #include <QtDeclarative/qsgitem.h>
49 #include <QtDeclarative/qsgcanvas.h>
50 #define QUICK_TEST_SCENEGRAPH 1
51 #endif
52 #include <QtGui/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     QWidget *widget = eventWidget();
68     if (!widget)
69         return false;
70     QTest::keyPress(widget, Qt::Key(key), Qt::KeyboardModifiers(modifiers), delay);
71     return true;
72 }
73
74 bool QuickTestEvent::keyRelease(int key, int modifiers, int delay)
75 {
76     QWidget *widget = eventWidget();
77     if (!widget)
78         return false;
79     QTest::keyRelease(widget, Qt::Key(key), Qt::KeyboardModifiers(modifiers), delay);
80     return true;
81 }
82
83 bool QuickTestEvent::keyClick(int key, int modifiers, int delay)
84 {
85     QWidget *widget = eventWidget();
86     if (!widget)
87         return false;
88     QTest::keyClick(widget, 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, QWidget *widget,
101                            QObject *item, Qt::MouseButton button,
102                            Qt::KeyboardModifiers stateKey, QPointF _pos, int delay=-1)
103     {
104         QTEST_ASSERT(widget);
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, widget, item, button, stateKey, _pos);
114             mouseEvent(MouseRelease, widget, item, button, stateKey, _pos);
115             return;
116         }
117
118         QPoint pos;
119         QDeclarativeView *view = qobject_cast<QDeclarativeView *>(widget);
120         QWidget *eventWidget = widget;
121 #ifdef QUICK_TEST_SCENEGRAPH
122         QSGItem *sgitem = qobject_cast<QSGItem *>(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             eventWidget = view->viewport();
135         }
136
137         QTEST_ASSERT(button == Qt::NoButton || button & Qt::MouseButtonMask);
138         QTEST_ASSERT(stateKey == 0 || stateKey & Qt::KeyboardModifierMask);
139
140         stateKey &= static_cast<unsigned int>(Qt::KeyboardModifierMask);
141
142         QMouseEvent me(QEvent::User, QPoint(), Qt::LeftButton, button, stateKey);
143         switch (action)
144         {
145             case MousePress:
146                 me = QMouseEvent(QEvent::MouseButtonPress, pos, widget->mapToGlobal(pos), button, button, stateKey);
147                 break;
148             case MouseRelease:
149                 me = QMouseEvent(QEvent::MouseButtonRelease, pos, widget->mapToGlobal(pos), button, 0, stateKey);
150                 break;
151             case MouseDoubleClick:
152                 me = QMouseEvent(QEvent::MouseButtonDblClick, pos, widget->mapToGlobal(pos), button, button, stateKey);
153                 break;
154             case MouseMove:
155                 QCursor::setPos(widget->mapToGlobal(pos));
156                 qApp->processEvents();
157                 return;
158             default:
159                 QTEST_ASSERT(false);
160         }
161         QSpontaneKeyEvent::setSpontaneous(&me);
162         if (!qApp->notify(eventWidget, &me)) {
163             static const char *mouseActionNames[] =
164                 { "MousePress", "MouseRelease", "MouseClick", "MouseDoubleClick", "MouseMove" };
165             QString warning = QString::fromLatin1("Mouse event \"%1\" not accepted by receiving widget");
166             QTest::qWarn(warning.arg(QString::fromLatin1(mouseActionNames[static_cast<int>(action)])).toAscii().data());
167         }
168     }
169 };
170
171 bool QuickTestEvent::mousePress
172     (QObject *item, qreal x, qreal y, int button,
173      int modifiers, int delay)
174 {
175     QWidget *view = eventWidget();
176     if (!view)
177         return false;
178     QtQuickTest::mouseEvent(QtQuickTest::MousePress, view, item,
179                             Qt::MouseButton(button),
180                             Qt::KeyboardModifiers(modifiers),
181                             QPointF(x, y), delay);
182     return true;
183 }
184
185 bool QuickTestEvent::mouseRelease
186     (QObject *item, qreal x, qreal y, int button,
187      int modifiers, int delay)
188 {
189     QWidget *view = eventWidget();
190     if (!view)
191         return false;
192     QtQuickTest::mouseEvent(QtQuickTest::MouseRelease, view, item,
193                             Qt::MouseButton(button),
194                             Qt::KeyboardModifiers(modifiers),
195                             QPointF(x, y), delay);
196     return true;
197 }
198
199 bool QuickTestEvent::mouseClick
200     (QObject *item, qreal x, qreal y, int button,
201      int modifiers, int delay)
202 {
203     QWidget *view = eventWidget();
204     if (!view)
205         return false;
206     QtQuickTest::mouseEvent(QtQuickTest::MouseClick, view, item,
207                             Qt::MouseButton(button),
208                             Qt::KeyboardModifiers(modifiers),
209                             QPointF(x, y), delay);
210     return true;
211 }
212
213 bool QuickTestEvent::mouseDoubleClick
214     (QObject *item, qreal x, qreal y, int button,
215      int modifiers, int delay)
216 {
217     QWidget *view = eventWidget();
218     if (!view)
219         return false;
220     QtQuickTest::mouseEvent(QtQuickTest::MouseDoubleClick, view, item,
221                             Qt::MouseButton(button),
222                             Qt::KeyboardModifiers(modifiers),
223                             QPointF(x, y), delay);
224     return true;
225 }
226
227 bool QuickTestEvent::mouseMove
228     (QObject *item, qreal x, qreal y, int delay)
229 {
230     QWidget *view = eventWidget();
231     if (!view)
232         return false;
233     QtQuickTest::mouseEvent(QtQuickTest::MouseMove, view, item,
234                             Qt::NoButton, Qt::NoModifier,
235                             QPointF(x, y), delay);
236     return true;
237 }
238
239 QWidget *QuickTestEvent::eventWidget()
240 {
241 #ifdef QUICK_TEST_SCENEGRAPH
242     QSGItem *sgitem = qobject_cast<QSGItem *>(parent());
243     if (sgitem)
244         return sgitem->canvas();
245 #endif
246     QDeclarativeItem *item = qobject_cast<QDeclarativeItem *>(parent());
247     if (!item)
248         return 0;
249     QGraphicsScene *s = item->scene();
250     if (!s)
251         return 0;
252     QList<QGraphicsView *> views = s->views();
253     if (views.isEmpty())
254         return 0;
255     return views.at(0);
256 }
257
258 QT_END_NAMESPACE