Remove copy of tests/shared/util.h.
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / qsgcanvas / tst_qsgcanvas.cpp
index 0791884..d8135ed 100644 (file)
 #include <qtest.h>
 #include <QDebug>
 #include <QTouchEvent>
-
-#include "qsgitem.h"
-#include "qsgcanvas.h"
-#include "private/qsgrectangle_p.h"
-#include "../../../shared/util.h"
+#include <QtDeclarative/QSGItem>
+#include <QtDeclarative/QSGCanvas>
+#include <QtDeclarative/private/qsgrectangle_p.h>
+#include <QtGui/QWindowSystemInterface>
 
 struct TouchEventData {
     QEvent::Type type;
     QWidget *widget;
+    QWindow *window;
     Qt::TouchPointStates states;
     QList<QTouchEvent::TouchPoint> touchPoints;
 };
@@ -60,6 +60,7 @@ static QTouchEvent::TouchPoint makeTouchPoint(QSGItem *item, const QPointF &p, c
     QPointF last = lastPoint.isNull() ? p : lastPoint;
 
     QTouchEvent::TouchPoint tp;
+
     tp.setPos(p);
     tp.setLastPos(last);
     tp.setScenePos(item->mapToScene(p));
@@ -71,7 +72,7 @@ static QTouchEvent::TouchPoint makeTouchPoint(QSGItem *item, const QPointF &p, c
 
 static TouchEventData makeTouchData(QEvent::Type type, QWidget *w, Qt::TouchPointStates states, const QList<QTouchEvent::TouchPoint> &touchPoints)
 {
-    TouchEventData d = { type, w, states, touchPoints };
+    TouchEventData d = { type, w, 0, states, touchPoints };
     return d;
 }
 
@@ -81,6 +82,17 @@ static TouchEventData makeTouchData(QEvent::Type type, QWidget *w, Qt::TouchPoin
     points << touchPoint;
     return makeTouchData(type, w, states, points);
 }
+static TouchEventData makeTouchData(QEvent::Type type, QWindow *w, Qt::TouchPointStates states, const QList<QTouchEvent::TouchPoint>& touchPoints)
+{
+    TouchEventData d = { type, 0, w, states, touchPoints };
+    return d;
+}
+static TouchEventData makeTouchData(QEvent::Type type, QWindow *w, Qt::TouchPointStates states, const QTouchEvent::TouchPoint &touchPoint)
+{
+    QList<QTouchEvent::TouchPoint> points;
+    points << touchPoint;
+    return makeTouchData(type, w, states, points);
+}
 
 #define COMPARE_TOUCH_POINTS(tp1, tp2) \
 { \
@@ -108,9 +120,11 @@ class TestTouchItem : public QSGRectangle
     Q_OBJECT
 public:
     TestTouchItem(QSGItem *parent = 0)
-        : QSGRectangle(parent), acceptEvents(true)
+        : QSGRectangle(parent), acceptEvents(true), mousePressId(0)
     {
         border()->setWidth(1);
+        setAcceptedMouseButtons(Qt::LeftButton);
+        setFiltersChildMouseEvents(true);
     }
 
     void reset() {
@@ -118,11 +132,12 @@ public:
         setEnabled(true);
         setOpacity(1.0);
 
-        lastEvent = makeTouchData(QEvent::None, 0, 0, QList<QTouchEvent::TouchPoint>());
+        lastEvent = makeTouchData(QEvent::None, canvas(), 0, QList<QTouchEvent::TouchPoint>());//CHECK_VALID
     }
 
     bool acceptEvents;
     TouchEventData lastEvent;
+    int mousePressId;
 
 protected:
     virtual void touchEvent(QTouchEvent *event) {
@@ -133,8 +148,20 @@ protected:
         lastEvent = makeTouchData(event->type(), event->widget(), event->touchPointStates(), event->touchPoints());
         event->accept();
     }
+
+    virtual void mousePressEvent(QMouseEvent *event) {
+        mousePressId = ++mousePressNum;
+    }
+
+    bool childMouseEventFilter(QSGItem *, QEvent *) {
+        mousePressId = ++mousePressNum;
+        return false;
+    }
+
+    static int mousePressNum;
 };
 
+int TestTouchItem::mousePressNum = 0;
 
 class ConstantUpdateItem : public QSGItem
 {
@@ -168,6 +195,7 @@ private slots:
     void touchEvent_propagation_data();
 
     void clearCanvas();
+    void mouseFiltering();
 };
 
 tst_qsgcanvas::tst_qsgcanvas()
@@ -195,7 +223,7 @@ void tst_qsgcanvas::touchEvent_basic()
 {
     QSGCanvas *canvas = new QSGCanvas;
     canvas->resize(250, 250);
-    canvas->window()->move(100, 100);
+    canvas->move(100, 100);
     canvas->show();
 
     TestTouchItem *bottomItem = new TestTouchItem(canvas->rootItem());
@@ -215,16 +243,21 @@ void tst_qsgcanvas::touchEvent_basic()
     QPointF pos(10, 10);
 
     // press single point
-    QTest::touchEvent(canvas).press(0, topItem->mapToScene(pos).toPoint());
+    QTest::touchEvent(canvas).press(0, topItem->mapToScene(pos).toPoint(),canvas);
+    QTest::qWait(50);
+
     QCOMPARE(topItem->lastEvent.touchPoints.count(), 1);
+
     QVERIFY(middleItem->lastEvent.touchPoints.isEmpty());
     QVERIFY(bottomItem->lastEvent.touchPoints.isEmpty());
+    TouchEventData d = makeTouchData(QEvent::TouchBegin, canvas, Qt::TouchPointPressed, makeTouchPoint(topItem,pos));
     COMPARE_TOUCH_DATA(topItem->lastEvent, makeTouchData(QEvent::TouchBegin, canvas, Qt::TouchPointPressed, makeTouchPoint(topItem, pos)));
     topItem->reset();
 
     // press multiple points
-    QTest::touchEvent(canvas).press(0, topItem->mapToScene(pos).toPoint())
-                             .press(1, bottomItem->mapToScene(pos).toPoint());
+    QTest::touchEvent(canvas).press(0, topItem->mapToScene(pos).toPoint(),canvas)
+            .press(1, bottomItem->mapToScene(pos).toPoint(), canvas);
+    QTest::qWait(50);
     QCOMPARE(topItem->lastEvent.touchPoints.count(), 1);
     QVERIFY(middleItem->lastEvent.touchPoints.isEmpty());
     QCOMPARE(bottomItem->lastEvent.touchPoints.count(), 1);
@@ -234,25 +267,31 @@ void tst_qsgcanvas::touchEvent_basic()
     bottomItem->reset();
 
     // touch point on top item moves to bottom item, but top item should still receive the event
-    QTest::touchEvent(canvas).press(0, topItem->mapToScene(pos).toPoint());
-    QTest::touchEvent(canvas).move(0, bottomItem->mapToScene(pos).toPoint());
+    QTest::touchEvent(canvas).press(0, topItem->mapToScene(pos).toPoint(), canvas);
+    QTest::qWait(50);
+    QTest::touchEvent(canvas).move(0, bottomItem->mapToScene(pos).toPoint(), canvas);
+    QTest::qWait(50);
     QCOMPARE(topItem->lastEvent.touchPoints.count(), 1);
     COMPARE_TOUCH_DATA(topItem->lastEvent, makeTouchData(QEvent::TouchUpdate, canvas, Qt::TouchPointMoved,
             makeTouchPoint(topItem, topItem->mapFromItem(bottomItem, pos), pos)));
     topItem->reset();
 
     // touch point on bottom item moves to top item, but bottom item should still receive the event
-    QTest::touchEvent(canvas).press(0, bottomItem->mapToScene(pos).toPoint());
-    QTest::touchEvent(canvas).move(0, topItem->mapToScene(pos).toPoint());
+    QTest::touchEvent(canvas).press(0, bottomItem->mapToScene(pos).toPoint(), canvas);
+    QTest::qWait(50);
+    QTest::touchEvent(canvas).move(0, topItem->mapToScene(pos).toPoint(), canvas);
+    QTest::qWait(50);
     QCOMPARE(bottomItem->lastEvent.touchPoints.count(), 1);
     COMPARE_TOUCH_DATA(bottomItem->lastEvent, makeTouchData(QEvent::TouchUpdate, canvas, Qt::TouchPointMoved,
             makeTouchPoint(bottomItem, bottomItem->mapFromItem(topItem, pos), pos)));
     bottomItem->reset();
 
     // a single stationary press on an item shouldn't cause an event
-    QTest::touchEvent(canvas).press(0, topItem->mapToScene(pos).toPoint());
+    QTest::touchEvent(canvas).press(0, topItem->mapToScene(pos).toPoint(), canvas);
+    QTest::qWait(50);
     QTest::touchEvent(canvas).stationary(0)
-                             .press(1, bottomItem->mapToScene(pos).toPoint());
+            .press(1, bottomItem->mapToScene(pos).toPoint(), canvas);
+    QTest::qWait(50);
     QCOMPARE(topItem->lastEvent.touchPoints.count(), 1);    // received press only, not stationary
     QVERIFY(middleItem->lastEvent.touchPoints.isEmpty());
     QCOMPARE(bottomItem->lastEvent.touchPoints.count(), 1);
@@ -262,19 +301,24 @@ void tst_qsgcanvas::touchEvent_basic()
     bottomItem->reset();
 
     // move touch point from top item to bottom, and release
-    QTest::touchEvent(canvas).press(0, topItem->mapToScene(pos).toPoint());
-    QTest::touchEvent(canvas).release(0, bottomItem->mapToScene(pos).toPoint());
+    QTest::touchEvent(canvas).press(0, topItem->mapToScene(pos).toPoint(),canvas);
+    QTest::qWait(50);
+    QTest::touchEvent(canvas).release(0, bottomItem->mapToScene(pos).toPoint(),canvas);
+    QTest::qWait(50);
     QCOMPARE(topItem->lastEvent.touchPoints.count(), 1);
     COMPARE_TOUCH_DATA(topItem->lastEvent, makeTouchData(QEvent::TouchEnd, canvas, Qt::TouchPointReleased,
             makeTouchPoint(topItem, topItem->mapFromItem(bottomItem, pos), pos)));
     topItem->reset();
 
     // release while another point is pressed
-    QTest::touchEvent(canvas).press(0, topItem->mapToScene(pos).toPoint())
-                             .press(1, bottomItem->mapToScene(pos).toPoint());
-    QTest::touchEvent(canvas).move(0, bottomItem->mapToScene(pos).toPoint());
-    QTest::touchEvent(canvas).release(0, bottomItem->mapToScene(pos).toPoint())
+    QTest::touchEvent(canvas).press(0, topItem->mapToScene(pos).toPoint(),canvas)
+            .press(1, bottomItem->mapToScene(pos).toPoint(), canvas);
+    QTest::qWait(50);
+    QTest::touchEvent(canvas).move(0, bottomItem->mapToScene(pos).toPoint(), canvas);
+    QTest::qWait(50);
+    QTest::touchEvent(canvas).release(0, bottomItem->mapToScene(pos).toPoint(), canvas)
                              .stationary(1);
+    QTest::qWait(50);
     QCOMPARE(topItem->lastEvent.touchPoints.count(), 1);
     QVERIFY(middleItem->lastEvent.touchPoints.isEmpty());
     QCOMPARE(bottomItem->lastEvent.touchPoints.count(), 1);
@@ -298,7 +342,7 @@ void tst_qsgcanvas::touchEvent_propagation()
 
     QSGCanvas *canvas = new QSGCanvas;
     canvas->resize(250, 250);
-    canvas->window()->move(100, 100);
+    canvas->move(100, 100);
     canvas->show();
 
     TestTouchItem *bottomItem = new TestTouchItem(canvas->rootItem());
@@ -326,7 +370,8 @@ void tst_qsgcanvas::touchEvent_propagation()
     topItem->setOpacity(itemOpacity);
 
     // single touch to top item, should be received by middle item
-    QTest::touchEvent(canvas).press(0, pointInTopItem);
+    QTest::touchEvent(canvas).press(0, pointInTopItem, canvas);
+    QTest::qWait(50);
     QVERIFY(topItem->lastEvent.touchPoints.isEmpty());
     QCOMPARE(middleItem->lastEvent.touchPoints.count(), 1);
     QVERIFY(bottomItem->lastEvent.touchPoints.isEmpty());
@@ -334,13 +379,14 @@ void tst_qsgcanvas::touchEvent_propagation()
             makeTouchPoint(middleItem, middleItem->mapFromItem(topItem, pos))));
 
     // touch top and middle items, middle item should get both events
-    QTest::touchEvent(canvas).press(0, pointInTopItem)
-                             .press(1, pointInMiddleItem);
+    QTest::touchEvent(canvas).press(0, pointInTopItem, canvas)
+            .press(1, pointInMiddleItem, canvas);
+    QTest::qWait(50);
     QVERIFY(topItem->lastEvent.touchPoints.isEmpty());
     QCOMPARE(middleItem->lastEvent.touchPoints.count(), 2);
     QVERIFY(bottomItem->lastEvent.touchPoints.isEmpty());
     COMPARE_TOUCH_DATA(middleItem->lastEvent, makeTouchData(QEvent::TouchBegin, canvas, Qt::TouchPointPressed,
-            (QList<QTouchEvent::TouchPoint>() << makeTouchPoint(middleItem, middleItem->mapFromItem(topItem, pos))
+           (QList<QTouchEvent::TouchPoint>() << makeTouchPoint(middleItem, middleItem->mapFromItem(topItem, pos))
                                               << makeTouchPoint(middleItem, pos) )));
     middleItem->reset();
 
@@ -350,8 +396,9 @@ void tst_qsgcanvas::touchEvent_propagation()
     middleItem->setOpacity(itemOpacity);
 
     // touch top and middle items, bottom item should get all events
-    QTest::touchEvent(canvas).press(0, pointInTopItem)
-                             .press(1, pointInMiddleItem);
+    QTest::touchEvent(canvas).press(0, pointInTopItem, canvas)
+            .press(1, pointInMiddleItem, canvas);
+    QTest::qWait(50);
     QVERIFY(topItem->lastEvent.touchPoints.isEmpty());
     QVERIFY(middleItem->lastEvent.touchPoints.isEmpty());
     QCOMPARE(bottomItem->lastEvent.touchPoints.count(), 2);
@@ -366,9 +413,10 @@ void tst_qsgcanvas::touchEvent_propagation()
     bottomItem->setOpacity(itemOpacity);
 
     // no events should be received
-    QTest::touchEvent(canvas).press(0, pointInTopItem)
-                             .press(1, pointInMiddleItem)
-                             .press(2, pointInBottomItem);
+    QTest::touchEvent(canvas).press(0, pointInTopItem, canvas)
+            .press(1, pointInMiddleItem, canvas)
+            .press(2, pointInBottomItem, canvas);
+    QTest::qWait(50);
     QVERIFY(topItem->lastEvent.touchPoints.isEmpty());
     QVERIFY(middleItem->lastEvent.touchPoints.isEmpty());
     QVERIFY(bottomItem->lastEvent.touchPoints.isEmpty());
@@ -381,7 +429,8 @@ void tst_qsgcanvas::touchEvent_propagation()
     middleItem->acceptEvents = acceptEvents;
     middleItem->setEnabled(enableItem);
     middleItem->setOpacity(itemOpacity);
-    QTest::touchEvent(canvas).press(0, pointInTopItem);
+    QTest::touchEvent(canvas).press(0, pointInTopItem, canvas);
+    QTest::qWait(50);
     if (!enableItem || itemOpacity == 0) {
         // middle item is disabled or has 0 opacity, bottom item receives the event
         QVERIFY(topItem->lastEvent.touchPoints.isEmpty());
@@ -430,6 +479,41 @@ void tst_qsgcanvas::clearCanvas()
     delete item;
 }
 
+void tst_qsgcanvas::mouseFiltering()
+{
+    QSGCanvas *canvas = new QSGCanvas;
+    canvas->resize(250, 250);
+    canvas->move(100, 100);
+    canvas->show();
+
+    TestTouchItem *bottomItem = new TestTouchItem(canvas->rootItem());
+    bottomItem->setObjectName("Bottom Item");
+    bottomItem->setSize(QSizeF(150, 150));
+
+    TestTouchItem *middleItem = new TestTouchItem(bottomItem);
+    middleItem->setObjectName("Middle Item");
+    middleItem->setPos(QPointF(50, 50));
+    middleItem->setSize(QSizeF(150, 150));
+
+    TestTouchItem *topItem = new TestTouchItem(middleItem);
+    topItem->setObjectName("Top Item");
+    topItem->setPos(QPointF(50, 50));
+    topItem->setSize(QSizeF(150, 150));
+
+    QPoint pos(100, 100);
+
+    QTest::mousePress(canvas, Qt::LeftButton, 0, pos);
+    QTest::qWait(50);
+
+    // Mouse filtering propagates down the stack, so the
+    // correct order is
+    // 1. middleItem filters event
+    // 2. bottomItem filters event
+    // 3. topItem receives event
+    QCOMPARE(middleItem->mousePressId, 1);
+    QCOMPARE(bottomItem->mousePressId, 2);
+    QCOMPARE(topItem->mousePressId, 3);
+}
 
 
 QTEST_MAIN(tst_qsgcanvas)