1 /****************************************************************************
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the test suite of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
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 <QtDeclarative/qsgitem.h>
49 #include <QtDeclarative/qsgcanvas.h>
50 #define QUICK_TEST_SCENEGRAPH 1
52 #include <QtWidgets/qgraphicsscene.h>
56 QuickTestEvent::QuickTestEvent(QObject *parent)
61 QuickTestEvent::~QuickTestEvent()
65 bool QuickTestEvent::keyPress(int key, int modifiers, int delay)
67 QWidget *widget = eventWidget();
70 QTest::keyPress(widget, Qt::Key(key), Qt::KeyboardModifiers(modifiers), delay);
74 bool QuickTestEvent::keyRelease(int key, int modifiers, int delay)
76 QWidget *widget = eventWidget();
79 QTest::keyRelease(widget, Qt::Key(key), Qt::KeyboardModifiers(modifiers), delay);
83 bool QuickTestEvent::keyClick(int key, int modifiers, int delay)
85 QWidget *widget = eventWidget();
88 QTest::keyClick(widget, Qt::Key(key), Qt::KeyboardModifiers(modifiers), delay);
93 extern int Q_TESTLIB_EXPORT defaultMouseDelay();
98 enum MouseAction { MousePress, MouseRelease, MouseClick, MouseDoubleClick, MouseMove };
100 static void mouseEvent(MouseAction action, QWidget *widget,
101 QObject *item, Qt::MouseButton button,
102 Qt::KeyboardModifiers stateKey, QPointF _pos, int delay=-1)
104 QTEST_ASSERT(widget);
107 if (delay == -1 || delay < QTest::defaultMouseDelay())
108 delay = QTest::defaultMouseDelay();
112 if (action == MouseClick) {
113 mouseEvent(MousePress, widget, item, button, stateKey, _pos);
114 mouseEvent(MouseRelease, widget, item, button, stateKey, _pos);
119 QDeclarativeView *view = qobject_cast<QDeclarativeView *>(widget);
120 QWidget *eventWidget = widget;
121 #ifdef QUICK_TEST_SCENEGRAPH
122 QSGItem *sgitem = qobject_cast<QSGItem *>(item);
124 pos = sgitem->mapToScene(_pos).toPoint();
128 QDeclarativeItem *ditem = qobject_cast<QDeclarativeItem *>(item);
130 qWarning("Mouse event target is not an Item");
133 pos = view->mapFromScene(ditem->mapToScene(_pos));
134 eventWidget = view->viewport();
137 QTEST_ASSERT(button == Qt::NoButton || button & Qt::MouseButtonMask);
138 QTEST_ASSERT(stateKey == 0 || stateKey & Qt::KeyboardModifierMask);
140 stateKey &= static_cast<unsigned int>(Qt::KeyboardModifierMask);
142 QMouseEvent me(QEvent::User, QPoint(), Qt::LeftButton, button, stateKey);
146 me = QMouseEvent(QEvent::MouseButtonPress, pos, widget->mapToGlobal(pos), button, button, stateKey);
149 me = QMouseEvent(QEvent::MouseButtonRelease, pos, widget->mapToGlobal(pos), button, 0, stateKey);
151 case MouseDoubleClick:
152 me = QMouseEvent(QEvent::MouseButtonDblClick, pos, widget->mapToGlobal(pos), button, button, stateKey);
155 QCursor::setPos(widget->mapToGlobal(pos));
156 qApp->processEvents();
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());
171 bool QuickTestEvent::mousePress
172 (QObject *item, qreal x, qreal y, int button,
173 int modifiers, int delay)
175 QWidget *view = eventWidget();
178 QtQuickTest::mouseEvent(QtQuickTest::MousePress, view, item,
179 Qt::MouseButton(button),
180 Qt::KeyboardModifiers(modifiers),
181 QPointF(x, y), delay);
185 bool QuickTestEvent::mouseRelease
186 (QObject *item, qreal x, qreal y, int button,
187 int modifiers, int delay)
189 QWidget *view = eventWidget();
192 QtQuickTest::mouseEvent(QtQuickTest::MouseRelease, view, item,
193 Qt::MouseButton(button),
194 Qt::KeyboardModifiers(modifiers),
195 QPointF(x, y), delay);
199 bool QuickTestEvent::mouseClick
200 (QObject *item, qreal x, qreal y, int button,
201 int modifiers, int delay)
203 QWidget *view = eventWidget();
206 QtQuickTest::mouseEvent(QtQuickTest::MouseClick, view, item,
207 Qt::MouseButton(button),
208 Qt::KeyboardModifiers(modifiers),
209 QPointF(x, y), delay);
213 bool QuickTestEvent::mouseDoubleClick
214 (QObject *item, qreal x, qreal y, int button,
215 int modifiers, int delay)
217 QWidget *view = eventWidget();
220 QtQuickTest::mouseEvent(QtQuickTest::MouseDoubleClick, view, item,
221 Qt::MouseButton(button),
222 Qt::KeyboardModifiers(modifiers),
223 QPointF(x, y), delay);
227 bool QuickTestEvent::mouseMove
228 (QObject *item, qreal x, qreal y, int delay)
230 QWidget *view = eventWidget();
233 QtQuickTest::mouseEvent(QtQuickTest::MouseMove, view, item,
234 Qt::NoButton, Qt::NoModifier,
235 QPointF(x, y), delay);
239 QWidget *QuickTestEvent::eventWidget()
241 #ifdef QUICK_TEST_SCENEGRAPH
242 QSGItem *sgitem = qobject_cast<QSGItem *>(parent());
244 return sgitem->canvas();
246 QDeclarativeItem *item = qobject_cast<QDeclarativeItem *>(parent());
249 QGraphicsScene *s = item->scene();
252 QList<QGraphicsView *> views = s->views();