1 /****************************************************************************
3 ** Copyright (C) 2011 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 ****************************************************************************/
44 #include <QTouchEvent>
45 #include <QtDeclarative/QSGItem>
46 #include <QtDeclarative/QSGCanvas>
47 #include <QtDeclarative/private/qsgrectangle_p.h>
48 #include "../../../shared/util.h"
49 #include <QtGui/QWindowSystemInterface>
51 struct TouchEventData {
55 Qt::TouchPointStates states;
56 QList<QTouchEvent::TouchPoint> touchPoints;
59 static QTouchEvent::TouchPoint makeTouchPoint(QSGItem *item, const QPointF &p, const QPointF &lastPoint = QPointF())
61 QPointF last = lastPoint.isNull() ? p : lastPoint;
63 QTouchEvent::TouchPoint tp;
67 tp.setScenePos(item->mapToScene(p));
68 tp.setLastScenePos(item->mapToScene(last));
69 tp.setScreenPos(item->canvas()->mapToGlobal(tp.scenePos().toPoint()));
70 tp.setLastScreenPos(item->canvas()->mapToGlobal(tp.lastScenePos().toPoint()));
74 static TouchEventData makeTouchData(QEvent::Type type, QWidget *w, Qt::TouchPointStates states, const QList<QTouchEvent::TouchPoint> &touchPoints)
76 TouchEventData d = { type, w, 0, states, touchPoints };
80 static TouchEventData makeTouchData(QEvent::Type type, QWidget *w, Qt::TouchPointStates states, const QTouchEvent::TouchPoint &touchPoint)
82 QList<QTouchEvent::TouchPoint> points;
84 return makeTouchData(type, w, states, points);
86 static TouchEventData makeTouchData(QEvent::Type type, QWindow *w, Qt::TouchPointStates states, const QList<QTouchEvent::TouchPoint>& touchPoints)
88 TouchEventData d = { type, 0, w, states, touchPoints };
91 static TouchEventData makeTouchData(QEvent::Type type, QWindow *w, Qt::TouchPointStates states, const QTouchEvent::TouchPoint &touchPoint)
93 QList<QTouchEvent::TouchPoint> points;
95 return makeTouchData(type, w, states, points);
98 #define COMPARE_TOUCH_POINTS(tp1, tp2) \
100 QCOMPARE(tp1.pos(), tp2.pos()); \
101 QCOMPARE(tp1.lastPos(), tp2.lastPos()); \
102 QCOMPARE(tp1.scenePos(), tp2.scenePos()); \
103 QCOMPARE(tp1.lastScenePos(), tp2.lastScenePos()); \
104 QCOMPARE(tp1.screenPos(), tp2.screenPos()); \
105 QCOMPARE(tp1.lastScreenPos(), tp2.lastScreenPos()); \
108 #define COMPARE_TOUCH_DATA(d1, d2) \
110 QCOMPARE((int)d1.type, (int)d2.type); \
111 QCOMPARE(d1.widget, d2.widget); \
112 QCOMPARE((int)d1.states, (int)d2.states); \
113 QCOMPARE(d1.touchPoints.count(), d2.touchPoints.count()); \
114 for (int i=0; i<d1.touchPoints.count(); i++) { \
115 COMPARE_TOUCH_POINTS(d1.touchPoints[i], d2.touchPoints[i]); \
119 class TestTouchItem : public QSGRectangle
123 TestTouchItem(QSGItem *parent = 0)
124 : QSGRectangle(parent), acceptEvents(true), mousePressId(0)
126 border()->setWidth(1);
127 setAcceptedMouseButtons(Qt::LeftButton);
128 setFiltersChildMouseEvents(true);
136 lastEvent = makeTouchData(QEvent::None, canvas(), 0, QList<QTouchEvent::TouchPoint>());//CHECK_VALID
140 TouchEventData lastEvent;
144 virtual void touchEvent(QTouchEvent *event) {
149 lastEvent = makeTouchData(event->type(), event->widget(), event->touchPointStates(), event->touchPoints());
153 virtual void mousePressEvent(QMouseEvent *event) {
154 mousePressId = ++mousePressNum;
157 bool childMouseEventFilter(QSGItem *, QEvent *) {
158 mousePressId = ++mousePressNum;
162 static int mousePressNum;
165 int TestTouchItem::mousePressNum = 0;
167 class ConstantUpdateItem : public QSGItem
171 ConstantUpdateItem(QSGItem *parent = 0) : QSGItem(parent), iterations(0) {setFlag(ItemHasContents);}
175 QSGNode* updatePaintNode(QSGNode *, UpdatePaintNodeData *){
182 class tst_qsgcanvas : public QObject
190 void cleanupTestCase();
192 void constantUpdates();
194 void touchEvent_basic();
195 void touchEvent_propagation();
196 void touchEvent_propagation_data();
199 void mouseFiltering();
202 tst_qsgcanvas::tst_qsgcanvas()
206 void tst_qsgcanvas::initTestCase()
210 void tst_qsgcanvas::cleanupTestCase()
214 //If the item calls update inside updatePaintNode, it should schedule another update
215 void tst_qsgcanvas::constantUpdates()
218 ConstantUpdateItem item(canvas.rootItem());
220 QTRY_VERIFY(item.iterations > 60);
223 void tst_qsgcanvas::touchEvent_basic()
225 QSGCanvas *canvas = new QSGCanvas;
226 canvas->resize(250, 250);
227 canvas->move(100, 100);
230 TestTouchItem *bottomItem = new TestTouchItem(canvas->rootItem());
231 bottomItem->setObjectName("Bottom Item");
232 bottomItem->setSize(QSizeF(150, 150));
234 TestTouchItem *middleItem = new TestTouchItem(bottomItem);
235 middleItem->setObjectName("Middle Item");
236 middleItem->setPos(QPointF(50, 50));
237 middleItem->setSize(QSizeF(150, 150));
239 TestTouchItem *topItem = new TestTouchItem(middleItem);
240 topItem->setObjectName("Top Item");
241 topItem->setPos(QPointF(50, 50));
242 topItem->setSize(QSizeF(150, 150));
246 // press single point
247 QTest::touchEvent(canvas).press(0, topItem->mapToScene(pos).toPoint(),canvas);
250 QCOMPARE(topItem->lastEvent.touchPoints.count(), 1);
252 QVERIFY(middleItem->lastEvent.touchPoints.isEmpty());
253 QVERIFY(bottomItem->lastEvent.touchPoints.isEmpty());
254 TouchEventData d = makeTouchData(QEvent::TouchBegin, canvas, Qt::TouchPointPressed, makeTouchPoint(topItem,pos));
255 COMPARE_TOUCH_DATA(topItem->lastEvent, makeTouchData(QEvent::TouchBegin, canvas, Qt::TouchPointPressed, makeTouchPoint(topItem, pos)));
258 // press multiple points
259 QTest::touchEvent(canvas).press(0, topItem->mapToScene(pos).toPoint(),canvas)
260 .press(1, bottomItem->mapToScene(pos).toPoint(), canvas);
262 QCOMPARE(topItem->lastEvent.touchPoints.count(), 1);
263 QVERIFY(middleItem->lastEvent.touchPoints.isEmpty());
264 QCOMPARE(bottomItem->lastEvent.touchPoints.count(), 1);
265 COMPARE_TOUCH_DATA(topItem->lastEvent, makeTouchData(QEvent::TouchBegin, canvas, Qt::TouchPointPressed, makeTouchPoint(topItem, pos)));
266 COMPARE_TOUCH_DATA(bottomItem->lastEvent, makeTouchData(QEvent::TouchBegin, canvas, Qt::TouchPointPressed, makeTouchPoint(bottomItem, pos)));
270 // touch point on top item moves to bottom item, but top item should still receive the event
271 QTest::touchEvent(canvas).press(0, topItem->mapToScene(pos).toPoint(), canvas);
273 QTest::touchEvent(canvas).move(0, bottomItem->mapToScene(pos).toPoint(), canvas);
275 QCOMPARE(topItem->lastEvent.touchPoints.count(), 1);
276 COMPARE_TOUCH_DATA(topItem->lastEvent, makeTouchData(QEvent::TouchUpdate, canvas, Qt::TouchPointMoved,
277 makeTouchPoint(topItem, topItem->mapFromItem(bottomItem, pos), pos)));
280 // touch point on bottom item moves to top item, but bottom item should still receive the event
281 QTest::touchEvent(canvas).press(0, bottomItem->mapToScene(pos).toPoint(), canvas);
283 QTest::touchEvent(canvas).move(0, topItem->mapToScene(pos).toPoint(), canvas);
285 QCOMPARE(bottomItem->lastEvent.touchPoints.count(), 1);
286 COMPARE_TOUCH_DATA(bottomItem->lastEvent, makeTouchData(QEvent::TouchUpdate, canvas, Qt::TouchPointMoved,
287 makeTouchPoint(bottomItem, bottomItem->mapFromItem(topItem, pos), pos)));
290 // a single stationary press on an item shouldn't cause an event
291 QTest::touchEvent(canvas).press(0, topItem->mapToScene(pos).toPoint(), canvas);
293 QTest::touchEvent(canvas).stationary(0)
294 .press(1, bottomItem->mapToScene(pos).toPoint(), canvas);
296 QCOMPARE(topItem->lastEvent.touchPoints.count(), 1); // received press only, not stationary
297 QVERIFY(middleItem->lastEvent.touchPoints.isEmpty());
298 QCOMPARE(bottomItem->lastEvent.touchPoints.count(), 1);
299 COMPARE_TOUCH_DATA(topItem->lastEvent, makeTouchData(QEvent::TouchBegin, canvas, Qt::TouchPointPressed, makeTouchPoint(topItem, pos)));
300 COMPARE_TOUCH_DATA(bottomItem->lastEvent, makeTouchData(QEvent::TouchBegin, canvas, Qt::TouchPointPressed, makeTouchPoint(bottomItem, pos)));
304 // move touch point from top item to bottom, and release
305 QTest::touchEvent(canvas).press(0, topItem->mapToScene(pos).toPoint(),canvas);
307 QTest::touchEvent(canvas).release(0, bottomItem->mapToScene(pos).toPoint(),canvas);
309 QCOMPARE(topItem->lastEvent.touchPoints.count(), 1);
310 COMPARE_TOUCH_DATA(topItem->lastEvent, makeTouchData(QEvent::TouchEnd, canvas, Qt::TouchPointReleased,
311 makeTouchPoint(topItem, topItem->mapFromItem(bottomItem, pos), pos)));
314 // release while another point is pressed
315 QTest::touchEvent(canvas).press(0, topItem->mapToScene(pos).toPoint(),canvas)
316 .press(1, bottomItem->mapToScene(pos).toPoint(), canvas);
318 QTest::touchEvent(canvas).move(0, bottomItem->mapToScene(pos).toPoint(), canvas);
320 QTest::touchEvent(canvas).release(0, bottomItem->mapToScene(pos).toPoint(), canvas)
323 QCOMPARE(topItem->lastEvent.touchPoints.count(), 1);
324 QVERIFY(middleItem->lastEvent.touchPoints.isEmpty());
325 QCOMPARE(bottomItem->lastEvent.touchPoints.count(), 1);
326 COMPARE_TOUCH_DATA(topItem->lastEvent, makeTouchData(QEvent::TouchEnd, canvas, Qt::TouchPointReleased,
327 makeTouchPoint(topItem, topItem->mapFromItem(bottomItem, pos))));
328 COMPARE_TOUCH_DATA(bottomItem->lastEvent, makeTouchData(QEvent::TouchBegin, canvas, Qt::TouchPointPressed, makeTouchPoint(bottomItem, pos)));
338 void tst_qsgcanvas::touchEvent_propagation()
340 QFETCH(bool, acceptEvents);
341 QFETCH(bool, enableItem);
342 QFETCH(qreal, itemOpacity);
344 QSGCanvas *canvas = new QSGCanvas;
345 canvas->resize(250, 250);
346 canvas->move(100, 100);
349 TestTouchItem *bottomItem = new TestTouchItem(canvas->rootItem());
350 bottomItem->setObjectName("Bottom Item");
351 bottomItem->setSize(QSizeF(150, 150));
353 TestTouchItem *middleItem = new TestTouchItem(bottomItem);
354 middleItem->setObjectName("Middle Item");
355 middleItem->setPos(QPointF(50, 50));
356 middleItem->setSize(QSizeF(150, 150));
358 TestTouchItem *topItem = new TestTouchItem(middleItem);
359 topItem->setObjectName("Top Item");
360 topItem->setPos(QPointF(50, 50));
361 topItem->setSize(QSizeF(150, 150));
364 QPoint pointInBottomItem = bottomItem->mapToScene(pos).toPoint(); // (10, 10)
365 QPoint pointInMiddleItem = middleItem->mapToScene(pos).toPoint(); // (60, 60) overlaps with bottomItem
366 QPoint pointInTopItem = topItem->mapToScene(pos).toPoint(); // (110, 110) overlaps with bottom & top items
369 topItem->acceptEvents = acceptEvents;
370 topItem->setEnabled(enableItem);
371 topItem->setOpacity(itemOpacity);
373 // single touch to top item, should be received by middle item
374 QTest::touchEvent(canvas).press(0, pointInTopItem, canvas);
376 QVERIFY(topItem->lastEvent.touchPoints.isEmpty());
377 QCOMPARE(middleItem->lastEvent.touchPoints.count(), 1);
378 QVERIFY(bottomItem->lastEvent.touchPoints.isEmpty());
379 COMPARE_TOUCH_DATA(middleItem->lastEvent, makeTouchData(QEvent::TouchBegin, canvas, Qt::TouchPointPressed,
380 makeTouchPoint(middleItem, middleItem->mapFromItem(topItem, pos))));
382 // touch top and middle items, middle item should get both events
383 QTest::touchEvent(canvas).press(0, pointInTopItem, canvas)
384 .press(1, pointInMiddleItem, canvas);
386 QVERIFY(topItem->lastEvent.touchPoints.isEmpty());
387 QCOMPARE(middleItem->lastEvent.touchPoints.count(), 2);
388 QVERIFY(bottomItem->lastEvent.touchPoints.isEmpty());
389 COMPARE_TOUCH_DATA(middleItem->lastEvent, makeTouchData(QEvent::TouchBegin, canvas, Qt::TouchPointPressed,
390 (QList<QTouchEvent::TouchPoint>() << makeTouchPoint(middleItem, middleItem->mapFromItem(topItem, pos))
391 << makeTouchPoint(middleItem, pos) )));
394 // disable middleItem as well
395 middleItem->acceptEvents = acceptEvents;
396 middleItem->setEnabled(enableItem);
397 middleItem->setOpacity(itemOpacity);
399 // touch top and middle items, bottom item should get all events
400 QTest::touchEvent(canvas).press(0, pointInTopItem, canvas)
401 .press(1, pointInMiddleItem, canvas);
403 QVERIFY(topItem->lastEvent.touchPoints.isEmpty());
404 QVERIFY(middleItem->lastEvent.touchPoints.isEmpty());
405 QCOMPARE(bottomItem->lastEvent.touchPoints.count(), 2);
406 COMPARE_TOUCH_DATA(bottomItem->lastEvent, makeTouchData(QEvent::TouchBegin, canvas, Qt::TouchPointPressed,
407 (QList<QTouchEvent::TouchPoint>() << makeTouchPoint(bottomItem, bottomItem->mapFromItem(topItem, pos))
408 << makeTouchPoint(bottomItem, bottomItem->mapFromItem(middleItem, pos)) )));
411 // disable bottom item as well
412 bottomItem->acceptEvents = acceptEvents;
413 bottomItem->setEnabled(enableItem);
414 bottomItem->setOpacity(itemOpacity);
416 // no events should be received
417 QTest::touchEvent(canvas).press(0, pointInTopItem, canvas)
418 .press(1, pointInMiddleItem, canvas)
419 .press(2, pointInBottomItem, canvas);
421 QVERIFY(topItem->lastEvent.touchPoints.isEmpty());
422 QVERIFY(middleItem->lastEvent.touchPoints.isEmpty());
423 QVERIFY(bottomItem->lastEvent.touchPoints.isEmpty());
429 // disable middle item, touch on top item
430 middleItem->acceptEvents = acceptEvents;
431 middleItem->setEnabled(enableItem);
432 middleItem->setOpacity(itemOpacity);
433 QTest::touchEvent(canvas).press(0, pointInTopItem, canvas);
435 if (!enableItem || itemOpacity == 0) {
436 // middle item is disabled or has 0 opacity, bottom item receives the event
437 QVERIFY(topItem->lastEvent.touchPoints.isEmpty());
438 QVERIFY(middleItem->lastEvent.touchPoints.isEmpty());
439 QCOMPARE(bottomItem->lastEvent.touchPoints.count(), 1);
440 COMPARE_TOUCH_DATA(bottomItem->lastEvent, makeTouchData(QEvent::TouchBegin, canvas, Qt::TouchPointPressed,
441 makeTouchPoint(bottomItem, bottomItem->mapFromItem(topItem, pos))));
443 // middle item ignores event, sends it to the top item (top-most child)
444 QCOMPARE(topItem->lastEvent.touchPoints.count(), 1);
445 QVERIFY(middleItem->lastEvent.touchPoints.isEmpty());
446 QVERIFY(bottomItem->lastEvent.touchPoints.isEmpty());
447 COMPARE_TOUCH_DATA(topItem->lastEvent, makeTouchData(QEvent::TouchBegin, canvas, Qt::TouchPointPressed,
448 makeTouchPoint(topItem, pos)));
457 void tst_qsgcanvas::touchEvent_propagation_data()
459 QTest::addColumn<bool>("acceptEvents");
460 QTest::addColumn<bool>("enableItem");
461 QTest::addColumn<qreal>("itemOpacity");
463 QTest::newRow("disable events") << false << true << 1.0;
464 QTest::newRow("disable item") << true << false << 1.0;
465 QTest::newRow("opacity of 0") << true << true << 0.0;
468 void tst_qsgcanvas::clearCanvas()
470 QSGCanvas *canvas = new QSGCanvas;
471 QSGItem *item = new QSGItem;
472 item->setParentItem(canvas->rootItem());
474 QVERIFY(item->canvas() == canvas);
478 QVERIFY(item->canvas() == 0);
483 void tst_qsgcanvas::mouseFiltering()
485 QSGCanvas *canvas = new QSGCanvas;
486 canvas->resize(250, 250);
487 canvas->move(100, 100);
490 TestTouchItem *bottomItem = new TestTouchItem(canvas->rootItem());
491 bottomItem->setObjectName("Bottom Item");
492 bottomItem->setSize(QSizeF(150, 150));
494 TestTouchItem *middleItem = new TestTouchItem(bottomItem);
495 middleItem->setObjectName("Middle Item");
496 middleItem->setPos(QPointF(50, 50));
497 middleItem->setSize(QSizeF(150, 150));
499 TestTouchItem *topItem = new TestTouchItem(middleItem);
500 topItem->setObjectName("Top Item");
501 topItem->setPos(QPointF(50, 50));
502 topItem->setSize(QSizeF(150, 150));
504 QPoint pos(100, 100);
506 QTest::mousePress(canvas, Qt::LeftButton, 0, pos);
509 // Mouse filtering propagates down the stack, so the
511 // 1. middleItem filters event
512 // 2. bottomItem filters event
513 // 3. topItem receives event
514 QCOMPARE(middleItem->mousePressId, 1);
515 QCOMPARE(bottomItem->mousePressId, 2);
516 QCOMPARE(topItem->mousePressId, 3);
520 QTEST_MAIN(tst_qsgcanvas)
522 #include "tst_qsgcanvas.moc"