tests/auto/gestures/tst_gestures.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 
       
    43 #include <QtTest/QtTest>
       
    44 #include "../../shared/util.h"
       
    45 
       
    46 #include <qevent.h>
       
    47 #include <qwidget.h>
       
    48 #include <qlayout.h>
       
    49 #include <qgesture.h>
       
    50 #include <qgesturerecognizer.h>
       
    51 #include <qgraphicsitem.h>
       
    52 #include <qgraphicsview.h>
       
    53 
       
    54 #include <qdebug.h>
       
    55 
       
    56 //TESTED_CLASS=
       
    57 //TESTED_FILES=
       
    58 
       
    59 static QPointF mapToGlobal(const QPointF &pt, QGraphicsItem *item, QGraphicsView *view)
       
    60 {
       
    61     return view->mapToGlobal(view->mapFromScene(item->mapToScene(pt)));
       
    62 }
       
    63 
       
    64 class CustomGesture : public QGesture
       
    65 {
       
    66     Q_OBJECT
       
    67 public:
       
    68     static Qt::GestureType GestureType;
       
    69 
       
    70     CustomGesture(QObject *parent = 0)
       
    71         : QGesture(parent), serial(0)
       
    72     {
       
    73     }
       
    74 
       
    75     int serial;
       
    76 
       
    77     static const int SerialMaybeThreshold;
       
    78     static const int SerialStartedThreshold;
       
    79     static const int SerialFinishedThreshold;
       
    80 };
       
    81 Qt::GestureType CustomGesture::GestureType = Qt::CustomGesture;
       
    82 const int CustomGesture::SerialMaybeThreshold = 1;
       
    83 const int CustomGesture::SerialStartedThreshold = 3;
       
    84 const int CustomGesture::SerialFinishedThreshold = 6;
       
    85 
       
    86 class CustomEvent : public QEvent
       
    87 {
       
    88 public:
       
    89     static int EventType;
       
    90 
       
    91     CustomEvent(int serial_ = 0)
       
    92         : QEvent(QEvent::Type(CustomEvent::EventType)),
       
    93           serial(serial_), hasHotSpot(false)
       
    94     {
       
    95     }
       
    96 
       
    97     int serial;
       
    98     QPointF hotSpot;
       
    99     bool hasHotSpot;
       
   100 };
       
   101 int CustomEvent::EventType = 0;
       
   102 
       
   103 class CustomGestureRecognizer : public QGestureRecognizer
       
   104 {
       
   105 public:
       
   106     CustomGestureRecognizer()
       
   107     {
       
   108         if (!CustomEvent::EventType)
       
   109             CustomEvent::EventType = QEvent::registerEventType();
       
   110     }
       
   111 
       
   112     QGesture* createGesture(QObject *)
       
   113     {
       
   114         return new CustomGesture;
       
   115     }
       
   116 
       
   117     QGestureRecognizer::Result filterEvent(QGesture *state, QObject*, QEvent *event)
       
   118     {
       
   119         if (event->type() == CustomEvent::EventType) {
       
   120             QGestureRecognizer::Result result = QGestureRecognizer::ConsumeEventHint;
       
   121             CustomGesture *g = static_cast<CustomGesture*>(state);
       
   122             CustomEvent *e = static_cast<CustomEvent*>(event);
       
   123             g->serial = e->serial;
       
   124             if (e->hasHotSpot)
       
   125                 g->setHotSpot(e->hotSpot);
       
   126             if (g->serial >= CustomGesture::SerialFinishedThreshold)
       
   127                 result |= QGestureRecognizer::GestureFinished;
       
   128             else if (g->serial >= CustomGesture::SerialStartedThreshold)
       
   129                 result |= QGestureRecognizer::GestureTriggered;
       
   130             else if (g->serial >= CustomGesture::SerialMaybeThreshold)
       
   131                 result |= QGestureRecognizer::MaybeGesture;
       
   132             else
       
   133                 result = QGestureRecognizer::NotGesture;
       
   134             return result;
       
   135         }
       
   136         return QGestureRecognizer::Ignore;
       
   137     }
       
   138 
       
   139     void reset(QGesture *state)
       
   140     {
       
   141         CustomGesture *g = static_cast<CustomGesture*>(state);
       
   142         g->serial = 0;
       
   143         QGestureRecognizer::reset(state);
       
   144     }
       
   145 };
       
   146 
       
   147 // same as CustomGestureRecognizer but triggers early without the maybe state
       
   148 class CustomContinuousGestureRecognizer : public QGestureRecognizer
       
   149 {
       
   150 public:
       
   151     CustomContinuousGestureRecognizer()
       
   152     {
       
   153         if (!CustomEvent::EventType)
       
   154             CustomEvent::EventType = QEvent::registerEventType();
       
   155     }
       
   156 
       
   157     QGesture* createGesture(QObject *)
       
   158     {
       
   159         return new CustomGesture;
       
   160     }
       
   161 
       
   162     QGestureRecognizer::Result filterEvent(QGesture *state, QObject*, QEvent *event)
       
   163     {
       
   164         if (event->type() == CustomEvent::EventType) {
       
   165             QGestureRecognizer::Result result = QGestureRecognizer::ConsumeEventHint;
       
   166             CustomGesture *g = static_cast<CustomGesture*>(state);
       
   167             CustomEvent *e = static_cast<CustomEvent*>(event);
       
   168             g->serial = e->serial;
       
   169             if (e->hasHotSpot)
       
   170                 g->setHotSpot(e->hotSpot);
       
   171             if (g->serial >= CustomGesture::SerialFinishedThreshold)
       
   172                 result |= QGestureRecognizer::GestureFinished;
       
   173             else if (g->serial >= CustomGesture::SerialMaybeThreshold)
       
   174                 result |= QGestureRecognizer::GestureTriggered;
       
   175             else
       
   176                 result = QGestureRecognizer::NotGesture;
       
   177             return result;
       
   178         }
       
   179         return QGestureRecognizer::Ignore;
       
   180     }
       
   181 
       
   182     void reset(QGesture *state)
       
   183     {
       
   184         CustomGesture *g = static_cast<CustomGesture*>(state);
       
   185         g->serial = 0;
       
   186         QGestureRecognizer::reset(state);
       
   187     }
       
   188 };
       
   189 
       
   190 class GestureWidget : public QWidget
       
   191 {
       
   192     Q_OBJECT
       
   193 public:
       
   194     GestureWidget(const char *name = 0, QWidget *parent = 0)
       
   195         : QWidget(parent)
       
   196     {
       
   197         if (name)
       
   198             setObjectName(QLatin1String(name));
       
   199         reset();
       
   200         acceptGestureOverride = false;
       
   201     }
       
   202     void reset()
       
   203     {
       
   204         customEventsReceived = 0;
       
   205         gestureEventsReceived = 0;
       
   206         gestureOverrideEventsReceived = 0;
       
   207         events.clear();
       
   208         overrideEvents.clear();
       
   209         ignoredGestures.clear();
       
   210     }
       
   211 
       
   212     int customEventsReceived;
       
   213     int gestureEventsReceived;
       
   214     int gestureOverrideEventsReceived;
       
   215     struct Events
       
   216     {
       
   217         QList<Qt::GestureType> all;
       
   218         QList<Qt::GestureType> started;
       
   219         QList<Qt::GestureType> updated;
       
   220         QList<Qt::GestureType> finished;
       
   221         QList<Qt::GestureType> canceled;
       
   222 
       
   223         void clear()
       
   224         {
       
   225             all.clear();
       
   226             started.clear();
       
   227             updated.clear();
       
   228             finished.clear();
       
   229             canceled.clear();
       
   230         }
       
   231     } events, overrideEvents;
       
   232 
       
   233     bool acceptGestureOverride;
       
   234     QSet<Qt::GestureType> ignoredGestures;
       
   235 
       
   236 protected:
       
   237     bool event(QEvent *event)
       
   238     {
       
   239         Events *eventsPtr = 0;
       
   240         if (event->type() == QEvent::Gesture) {
       
   241             QGestureEvent *e = static_cast<QGestureEvent*>(event);
       
   242             ++gestureEventsReceived;
       
   243             eventsPtr = &events;
       
   244             foreach(Qt::GestureType type, ignoredGestures)
       
   245                 e->ignore(e->gesture(type));
       
   246         } else if (event->type() == QEvent::GestureOverride) {
       
   247             ++gestureOverrideEventsReceived;
       
   248             eventsPtr = &overrideEvents;
       
   249             if (acceptGestureOverride)
       
   250                 event->accept();
       
   251         }
       
   252         if (eventsPtr) {
       
   253             QGestureEvent *e = static_cast<QGestureEvent*>(event);
       
   254             QList<QGesture*> gestures = e->allGestures();
       
   255             foreach(QGesture *g, gestures) {
       
   256                 eventsPtr->all << g->gestureType();
       
   257                 switch(g->state()) {
       
   258                 case Qt::GestureStarted:
       
   259                     eventsPtr->started << g->gestureType();
       
   260                     break;
       
   261                 case Qt::GestureUpdated:
       
   262                     eventsPtr->updated << g->gestureType();
       
   263                     break;
       
   264                 case Qt::GestureFinished:
       
   265                     eventsPtr->finished << g->gestureType();
       
   266                     break;
       
   267                 case Qt::GestureCanceled:
       
   268                     eventsPtr->canceled << g->gestureType();
       
   269                     break;
       
   270                 default:
       
   271                     Q_ASSERT(false);
       
   272                 }
       
   273             }
       
   274         } else if (event->type() == CustomEvent::EventType) {
       
   275             ++customEventsReceived;
       
   276         } else {
       
   277             return QWidget::event(event);
       
   278         }
       
   279         return true;
       
   280     }
       
   281 };
       
   282 
       
   283 static void sendCustomGesture(CustomEvent *event, QObject *object, QGraphicsScene *scene = 0)
       
   284 {
       
   285     for (int i = CustomGesture::SerialMaybeThreshold;
       
   286          i <= CustomGesture::SerialFinishedThreshold; ++i) {
       
   287         event->serial = i;
       
   288         if (scene)
       
   289             scene->sendEvent(qobject_cast<QGraphicsObject *>(object), event);
       
   290         else
       
   291             QApplication::sendEvent(object, event);
       
   292     }
       
   293 }
       
   294 
       
   295 class tst_Gestures : public QObject
       
   296 {
       
   297 Q_OBJECT
       
   298 
       
   299 public:
       
   300     tst_Gestures();
       
   301     virtual ~tst_Gestures();
       
   302 
       
   303 public slots:
       
   304     void initTestCase();
       
   305     void cleanupTestCase();
       
   306     void init();
       
   307     void cleanup();
       
   308 
       
   309 private slots:
       
   310     void customGesture();
       
   311     void autoCancelingGestures();
       
   312     void gestureOverChild();
       
   313     void multipleWidgetOnlyGestureInTree();
       
   314     void conflictingGestures();
       
   315     void finishedWithoutStarted();
       
   316     void unknownGesture();
       
   317     void graphicsItemGesture();
       
   318     void graphicsItemTreeGesture();
       
   319     void explicitGraphicsObjectTarget();
       
   320     void gestureOverChildGraphicsItem();
       
   321     void twoGesturesOnDifferentLevel();
       
   322     void multipleGesturesInTree();
       
   323     void multipleGesturesInComplexTree();
       
   324     void testMapToScene();
       
   325 };
       
   326 
       
   327 tst_Gestures::tst_Gestures()
       
   328 {
       
   329 }
       
   330 
       
   331 tst_Gestures::~tst_Gestures()
       
   332 {
       
   333 }
       
   334 
       
   335 void tst_Gestures::initTestCase()
       
   336 {
       
   337     CustomGesture::GestureType = qApp->registerGestureRecognizer(new CustomGestureRecognizer);
       
   338     QVERIFY(CustomGesture::GestureType != Qt::GestureType(0));
       
   339     QVERIFY(CustomGesture::GestureType != Qt::CustomGesture);
       
   340 }
       
   341 
       
   342 void tst_Gestures::cleanupTestCase()
       
   343 {
       
   344 }
       
   345 
       
   346 void tst_Gestures::init()
       
   347 {
       
   348 }
       
   349 
       
   350 void tst_Gestures::cleanup()
       
   351 {
       
   352 }
       
   353 
       
   354 void tst_Gestures::customGesture()
       
   355 {
       
   356     GestureWidget widget;
       
   357     widget.grabGesture(CustomGesture::GestureType, Qt::WidgetGesture);
       
   358     CustomEvent event;
       
   359     sendCustomGesture(&event, &widget);
       
   360 
       
   361     static const int TotalGestureEventsCount = CustomGesture::SerialFinishedThreshold - CustomGesture::SerialStartedThreshold + 1;
       
   362     static const int TotalCustomEventsCount = CustomGesture::SerialFinishedThreshold - CustomGesture::SerialMaybeThreshold + 1;
       
   363     QCOMPARE(widget.customEventsReceived, TotalCustomEventsCount);
       
   364     QCOMPARE(widget.gestureEventsReceived, TotalGestureEventsCount);
       
   365     QCOMPARE(widget.gestureOverrideEventsReceived, 0);
       
   366     QCOMPARE(widget.events.all.size(), TotalGestureEventsCount);
       
   367     for(int i = 0; i < widget.events.all.size(); ++i)
       
   368         QCOMPARE(widget.events.all.at(i), CustomGesture::GestureType);
       
   369     QCOMPARE(widget.events.started.size(), 1);
       
   370     QCOMPARE(widget.events.updated.size(), TotalGestureEventsCount - 2);
       
   371     QCOMPARE(widget.events.finished.size(), 1);
       
   372     QCOMPARE(widget.events.canceled.size(), 0);
       
   373 }
       
   374 
       
   375 void tst_Gestures::autoCancelingGestures()
       
   376 {
       
   377     GestureWidget widget;
       
   378     widget.grabGesture(CustomGesture::GestureType, Qt::WidgetGesture);
       
   379     // send partial gesture. The gesture will be in the "maybe" state, but will
       
   380     // never get enough events to fire, so Qt will have to kill it.
       
   381     CustomEvent ev;
       
   382     for (int i = CustomGesture::SerialMaybeThreshold;
       
   383          i < CustomGesture::SerialStartedThreshold; ++i) {
       
   384         ev.serial = i;
       
   385         QApplication::sendEvent(&widget, &ev);
       
   386     }
       
   387     // wait long enough so the gesture manager will cancel the gesture
       
   388     QTest::qWait(5000);
       
   389     QCOMPARE(widget.customEventsReceived, CustomGesture::SerialStartedThreshold - CustomGesture::SerialMaybeThreshold);
       
   390     QCOMPARE(widget.gestureEventsReceived, 0);
       
   391     QCOMPARE(widget.gestureOverrideEventsReceived, 0);
       
   392     QCOMPARE(widget.events.all.size(), 0);
       
   393 }
       
   394 
       
   395 void tst_Gestures::gestureOverChild()
       
   396 {
       
   397     GestureWidget widget("widget");
       
   398     QVBoxLayout *l = new QVBoxLayout(&widget);
       
   399     GestureWidget *child = new GestureWidget("child");
       
   400     l->addWidget(child);
       
   401 
       
   402     widget.grabGesture(CustomGesture::GestureType, Qt::WidgetGesture);
       
   403 
       
   404     CustomEvent event;
       
   405     sendCustomGesture(&event, child);
       
   406 
       
   407     static const int TotalGestureEventsCount = CustomGesture::SerialFinishedThreshold - CustomGesture::SerialStartedThreshold + 1;
       
   408     static const int TotalCustomEventsCount = CustomGesture::SerialFinishedThreshold - CustomGesture::SerialMaybeThreshold + 1;
       
   409 
       
   410     QCOMPARE(child->customEventsReceived, TotalCustomEventsCount);
       
   411     QCOMPARE(widget.customEventsReceived, 0);
       
   412     QCOMPARE(child->gestureEventsReceived, 0);
       
   413     QCOMPARE(child->gestureOverrideEventsReceived, 0);
       
   414     QCOMPARE(widget.gestureEventsReceived, 0);
       
   415     QCOMPARE(widget.gestureOverrideEventsReceived, 0);
       
   416 
       
   417     // enable gestures over the children
       
   418     widget.grabGesture(CustomGesture::GestureType, Qt::WidgetWithChildrenGesture);
       
   419 
       
   420     widget.reset();
       
   421     child->reset();
       
   422 
       
   423     sendCustomGesture(&event, child);
       
   424 
       
   425     QCOMPARE(child->customEventsReceived, TotalCustomEventsCount);
       
   426     QCOMPARE(widget.customEventsReceived, 0);
       
   427 
       
   428     QCOMPARE(child->gestureEventsReceived, 0);
       
   429     QCOMPARE(child->gestureOverrideEventsReceived, 0);
       
   430     QCOMPARE(widget.gestureEventsReceived, TotalGestureEventsCount);
       
   431     QCOMPARE(widget.gestureOverrideEventsReceived, 0);
       
   432     for(int i = 0; i < widget.events.all.size(); ++i)
       
   433         QCOMPARE(widget.events.all.at(i), CustomGesture::GestureType);
       
   434     QCOMPARE(widget.events.started.size(), 1);
       
   435     QCOMPARE(widget.events.updated.size(), TotalGestureEventsCount - 2);
       
   436     QCOMPARE(widget.events.finished.size(), 1);
       
   437     QCOMPARE(widget.events.canceled.size(), 0);
       
   438 }
       
   439 
       
   440 void tst_Gestures::multipleWidgetOnlyGestureInTree()
       
   441 {
       
   442     GestureWidget parent("parent");
       
   443     QVBoxLayout *l = new QVBoxLayout(&parent);
       
   444     GestureWidget *child = new GestureWidget("child");
       
   445     l->addWidget(child);
       
   446 
       
   447     parent.grabGesture(CustomGesture::GestureType, Qt::WidgetGesture);
       
   448     child->grabGesture(CustomGesture::GestureType, Qt::WidgetGesture);
       
   449 
       
   450     static const int TotalGestureEventsCount = CustomGesture::SerialFinishedThreshold - CustomGesture::SerialStartedThreshold + 1;
       
   451     static const int TotalCustomEventsCount = CustomGesture::SerialFinishedThreshold - CustomGesture::SerialMaybeThreshold + 1;
       
   452 
       
   453     // sending events to the child and making sure there is no conflict
       
   454     CustomEvent event;
       
   455     sendCustomGesture(&event, child);
       
   456 
       
   457     QCOMPARE(child->customEventsReceived, TotalCustomEventsCount);
       
   458     QCOMPARE(parent.customEventsReceived, 0);
       
   459     QCOMPARE(child->gestureEventsReceived, TotalGestureEventsCount);
       
   460     QCOMPARE(child->gestureOverrideEventsReceived, 0);
       
   461     QCOMPARE(parent.gestureEventsReceived, 0);
       
   462     QCOMPARE(parent.gestureOverrideEventsReceived, 0);
       
   463 
       
   464     parent.reset();
       
   465     child->reset();
       
   466 
       
   467     // same for the parent widget
       
   468     sendCustomGesture(&event, &parent);
       
   469 
       
   470     QCOMPARE(child->customEventsReceived, 0);
       
   471     QCOMPARE(parent.customEventsReceived, TotalCustomEventsCount);
       
   472     QCOMPARE(child->gestureEventsReceived, 0);
       
   473     QCOMPARE(child->gestureOverrideEventsReceived, 0);
       
   474     QCOMPARE(parent.gestureEventsReceived, TotalGestureEventsCount);
       
   475     QCOMPARE(parent.gestureOverrideEventsReceived, 0);
       
   476 }
       
   477 
       
   478 void tst_Gestures::conflictingGestures()
       
   479 {
       
   480     GestureWidget parent("parent");
       
   481     QVBoxLayout *l = new QVBoxLayout(&parent);
       
   482     GestureWidget *child = new GestureWidget("child");
       
   483     l->addWidget(child);
       
   484 
       
   485     parent.grabGesture(CustomGesture::GestureType, Qt::WidgetWithChildrenGesture);
       
   486     child->grabGesture(CustomGesture::GestureType, Qt::WidgetWithChildrenGesture);
       
   487 
       
   488     static const int TotalGestureEventsCount = CustomGesture::SerialFinishedThreshold - CustomGesture::SerialStartedThreshold + 1;
       
   489 
       
   490     // child accepts the override, parent will not receive anything
       
   491     parent.acceptGestureOverride = false;
       
   492     child->acceptGestureOverride = true;
       
   493 
       
   494     // sending events to the child and making sure there is no conflict
       
   495     CustomEvent event;
       
   496     sendCustomGesture(&event, child);
       
   497 
       
   498     QCOMPARE(child->gestureOverrideEventsReceived, 1);
       
   499     QCOMPARE(child->gestureEventsReceived, TotalGestureEventsCount);
       
   500     QCOMPARE(parent.gestureOverrideEventsReceived, 0);
       
   501     QCOMPARE(parent.gestureEventsReceived, 0);
       
   502 
       
   503     parent.reset();
       
   504     child->reset();
       
   505 
       
   506     // parent accepts the override
       
   507     parent.acceptGestureOverride = true;
       
   508     child->acceptGestureOverride = false;
       
   509 
       
   510     // sending events to the child and making sure there is no conflict
       
   511     sendCustomGesture(&event, child);
       
   512 
       
   513     QCOMPARE(child->gestureOverrideEventsReceived, 1);
       
   514     QCOMPARE(child->gestureEventsReceived, 0);
       
   515     QCOMPARE(parent.gestureOverrideEventsReceived, 1);
       
   516     QCOMPARE(parent.gestureEventsReceived, TotalGestureEventsCount);
       
   517 
       
   518     parent.reset();
       
   519     child->reset();
       
   520 
       
   521     // nobody accepts the override, we will send normal events to the closest context (to the child)
       
   522     parent.acceptGestureOverride = false;
       
   523     child->acceptGestureOverride = false;
       
   524     child->ignoredGestures << CustomGesture::GestureType;
       
   525 
       
   526     // sending events to the child and making sure there is no conflict
       
   527     sendCustomGesture(&event, child);
       
   528 
       
   529     QCOMPARE(child->gestureOverrideEventsReceived, 1);
       
   530     QCOMPARE(child->gestureEventsReceived, TotalGestureEventsCount);
       
   531     QCOMPARE(parent.gestureOverrideEventsReceived, 1);
       
   532     QCOMPARE(parent.gestureEventsReceived, TotalGestureEventsCount);
       
   533 
       
   534     parent.reset();
       
   535     child->reset();
       
   536 
       
   537     Qt::GestureType ContinuousGesture = qApp->registerGestureRecognizer(new CustomContinuousGestureRecognizer);
       
   538     static const int ContinuousGestureEventsCount = CustomGesture::SerialFinishedThreshold - CustomGesture::SerialMaybeThreshold + 1;
       
   539     child->grabGesture(ContinuousGesture);
       
   540     // child accepts override. And it also receives another custom gesture.
       
   541     parent.acceptGestureOverride = false;
       
   542     child->acceptGestureOverride = true;
       
   543     sendCustomGesture(&event, child);
       
   544 
       
   545     QCOMPARE(child->gestureOverrideEventsReceived, 1);
       
   546     QVERIFY(child->gestureEventsReceived > TotalGestureEventsCount);
       
   547     QCOMPARE(child->events.all.count(), TotalGestureEventsCount + ContinuousGestureEventsCount);
       
   548     QCOMPARE(parent.gestureOverrideEventsReceived, 0);
       
   549     QCOMPARE(parent.gestureEventsReceived, 0);
       
   550 }
       
   551 
       
   552 void tst_Gestures::finishedWithoutStarted()
       
   553 {
       
   554     GestureWidget widget;
       
   555     widget.grabGesture(CustomGesture::GestureType, Qt::WidgetGesture);
       
   556 
       
   557     // the gesture will claim it finished, but it was never started.
       
   558     CustomEvent ev;
       
   559     ev.serial = CustomGesture::SerialFinishedThreshold;
       
   560     QApplication::sendEvent(&widget, &ev);
       
   561 
       
   562     QCOMPARE(widget.customEventsReceived, 1);
       
   563     QCOMPARE(widget.gestureEventsReceived, 2);
       
   564     QCOMPARE(widget.gestureOverrideEventsReceived, 0);
       
   565     QCOMPARE(widget.events.all.size(), 2);
       
   566     QCOMPARE(widget.events.started.size(), 1);
       
   567     QCOMPARE(widget.events.updated.size(), 0);
       
   568     QCOMPARE(widget.events.finished.size(), 1);
       
   569     QCOMPARE(widget.events.canceled.size(), 0);
       
   570 }
       
   571 
       
   572 void tst_Gestures::unknownGesture()
       
   573 {
       
   574     GestureWidget widget;
       
   575     widget.grabGesture(CustomGesture::GestureType, Qt::WidgetGesture);
       
   576     widget.grabGesture(Qt::CustomGesture, Qt::WidgetGesture);
       
   577     widget.grabGesture(Qt::GestureType(Qt::PanGesture+512), Qt::WidgetGesture);
       
   578 
       
   579     CustomEvent event;
       
   580     sendCustomGesture(&event, &widget);
       
   581 
       
   582     static const int TotalGestureEventsCount = CustomGesture::SerialFinishedThreshold - CustomGesture::SerialStartedThreshold + 1;
       
   583 
       
   584     QCOMPARE(widget.gestureEventsReceived, TotalGestureEventsCount);
       
   585 }
       
   586 
       
   587 static const QColor InstanceColors[] = {
       
   588     Qt::blue, Qt::red, Qt::green, Qt::gray, Qt::yellow
       
   589 };
       
   590 
       
   591 class GestureItem : public QGraphicsObject
       
   592 {
       
   593     static int InstanceCount;
       
   594 
       
   595 public:
       
   596     GestureItem(const char *name = 0)
       
   597     {
       
   598         instanceNumber = InstanceCount++;
       
   599         if (name)
       
   600             setObjectName(QLatin1String(name));
       
   601         size = QRectF(0, 0, 100, 100);
       
   602         customEventsReceived = 0;
       
   603         gestureEventsReceived = 0;
       
   604         gestureOverrideEventsReceived = 0;
       
   605         events.clear();
       
   606         overrideEvents.clear();
       
   607         acceptGestureOverride = false;
       
   608     }
       
   609     ~GestureItem()
       
   610     {
       
   611         --InstanceCount;
       
   612     }
       
   613 
       
   614     int customEventsReceived;
       
   615     int gestureEventsReceived;
       
   616     int gestureOverrideEventsReceived;
       
   617     struct Events
       
   618     {
       
   619         QList<Qt::GestureType> all;
       
   620         QList<Qt::GestureType> started;
       
   621         QList<Qt::GestureType> updated;
       
   622         QList<Qt::GestureType> finished;
       
   623         QList<Qt::GestureType> canceled;
       
   624 
       
   625         void clear()
       
   626         {
       
   627             all.clear();
       
   628             started.clear();
       
   629             updated.clear();
       
   630             finished.clear();
       
   631             canceled.clear();
       
   632         }
       
   633     } events, overrideEvents;
       
   634 
       
   635     bool acceptGestureOverride;
       
   636     QSet<Qt::GestureType> ignoredGestures;
       
   637 
       
   638     QRectF size;
       
   639     int instanceNumber;
       
   640 
       
   641     void reset()
       
   642     {
       
   643         customEventsReceived = 0;
       
   644         gestureEventsReceived = 0;
       
   645         gestureOverrideEventsReceived = 0;
       
   646         events.clear();
       
   647         overrideEvents.clear();
       
   648         ignoredGestures.clear();
       
   649     }
       
   650 
       
   651 protected:
       
   652     QRectF boundingRect() const
       
   653     {
       
   654         return size;
       
   655     }
       
   656     void paint(QPainter *p, const QStyleOptionGraphicsItem *, QWidget *)
       
   657     {
       
   658         QColor color = InstanceColors[instanceNumber % (sizeof(InstanceColors)/sizeof(InstanceColors[0]))];
       
   659         p->fillRect(boundingRect(), color);
       
   660     }
       
   661 
       
   662     bool event(QEvent *event)
       
   663     {
       
   664         Events *eventsPtr = 0;
       
   665         if (event->type() == QEvent::Gesture) {
       
   666             ++gestureEventsReceived;
       
   667             eventsPtr = &events;
       
   668             QGestureEvent *e = static_cast<QGestureEvent *>(event);
       
   669             foreach(Qt::GestureType type, ignoredGestures)
       
   670                 e->ignore(e->gesture(type));
       
   671         } else if (event->type() == QEvent::GestureOverride) {
       
   672             ++gestureOverrideEventsReceived;
       
   673             eventsPtr = &overrideEvents;
       
   674             if (acceptGestureOverride)
       
   675                 event->accept();
       
   676         }
       
   677         if (eventsPtr) {
       
   678             QGestureEvent *e = static_cast<QGestureEvent*>(event);
       
   679             QList<QGesture*> gestures = e->allGestures();
       
   680             foreach(QGesture *g, gestures) {
       
   681                 eventsPtr->all << g->gestureType();
       
   682                 switch(g->state()) {
       
   683                 case Qt::GestureStarted:
       
   684                     eventsPtr->started << g->gestureType();
       
   685                     break;
       
   686                 case Qt::GestureUpdated:
       
   687                     eventsPtr->updated << g->gestureType();
       
   688                     break;
       
   689                 case Qt::GestureFinished:
       
   690                     eventsPtr->finished << g->gestureType();
       
   691                     break;
       
   692                 case Qt::GestureCanceled:
       
   693                     eventsPtr->canceled << g->gestureType();
       
   694                     break;
       
   695                 default:
       
   696                     Q_ASSERT(false);
       
   697                 }
       
   698             }
       
   699         } else if (event->type() == CustomEvent::EventType) {
       
   700             ++customEventsReceived;
       
   701         } else {
       
   702             return QGraphicsObject::event(event);
       
   703         }
       
   704         return true;
       
   705     }
       
   706 };
       
   707 int GestureItem::InstanceCount = 0;
       
   708 
       
   709 void tst_Gestures::graphicsItemGesture()
       
   710 {
       
   711     QGraphicsScene scene;
       
   712     QGraphicsView view(&scene);
       
   713 
       
   714     GestureItem *item = new GestureItem("item");
       
   715     scene.addItem(item);
       
   716     item->setPos(100, 100);
       
   717 
       
   718     view.show();
       
   719     QTest::qWaitForWindowShown(&view);
       
   720     view.ensureVisible(scene.sceneRect());
       
   721 
       
   722     view.viewport()->grabGesture(CustomGesture::GestureType, Qt::WidgetGesture);
       
   723     item->grabGesture(CustomGesture::GestureType);
       
   724 
       
   725     static const int TotalGestureEventsCount = CustomGesture::SerialFinishedThreshold - CustomGesture::SerialStartedThreshold + 1;
       
   726     static const int TotalCustomEventsCount = CustomGesture::SerialFinishedThreshold - CustomGesture::SerialMaybeThreshold + 1;
       
   727 
       
   728     CustomEvent event;
       
   729     // gesture without hotspot should not be delivered to items in the view
       
   730     QTest::ignoreMessage(QtWarningMsg, "QGestureManager::deliverEvent: could not find the target for gesture");
       
   731     QTest::ignoreMessage(QtWarningMsg, "QGestureManager::deliverEvent: could not find the target for gesture");
       
   732     QTest::ignoreMessage(QtWarningMsg, "QGestureManager::deliverEvent: could not find the target for gesture");
       
   733     QTest::ignoreMessage(QtWarningMsg, "QGestureManager::deliverEvent: could not find the target for gesture");
       
   734     sendCustomGesture(&event, item, &scene);
       
   735 
       
   736     QCOMPARE(item->customEventsReceived, TotalCustomEventsCount);
       
   737     QCOMPARE(item->gestureEventsReceived, 0);
       
   738     QCOMPARE(item->gestureOverrideEventsReceived, 0);
       
   739 
       
   740     item->reset();
       
   741 
       
   742     // make sure the event is properly delivered if only the hotspot is set.
       
   743     event.hotSpot = mapToGlobal(QPointF(10, 10), item, &view);
       
   744     event.hasHotSpot = true;
       
   745     sendCustomGesture(&event, item, &scene);
       
   746 
       
   747     QCOMPARE(item->customEventsReceived, TotalCustomEventsCount);
       
   748     QCOMPARE(item->gestureEventsReceived, TotalGestureEventsCount);
       
   749     QCOMPARE(item->gestureOverrideEventsReceived, 0);
       
   750     QCOMPARE(item->events.all.size(), TotalGestureEventsCount);
       
   751     for(int i = 0; i < item->events.all.size(); ++i)
       
   752         QCOMPARE(item->events.all.at(i), CustomGesture::GestureType);
       
   753     QCOMPARE(item->events.started.size(), 1);
       
   754     QCOMPARE(item->events.updated.size(), TotalGestureEventsCount - 2);
       
   755     QCOMPARE(item->events.finished.size(), 1);
       
   756     QCOMPARE(item->events.canceled.size(), 0);
       
   757 
       
   758     item->reset();
       
   759 
       
   760     // send gesture to the item which ignores it.
       
   761     item->ignoredGestures << CustomGesture::GestureType;
       
   762 
       
   763     event.hotSpot = mapToGlobal(QPointF(10, 10), item, &view);
       
   764     event.hasHotSpot = true;
       
   765     sendCustomGesture(&event, item, &scene);
       
   766     QCOMPARE(item->customEventsReceived, TotalCustomEventsCount);
       
   767     QCOMPARE(item->gestureEventsReceived, TotalGestureEventsCount);
       
   768     QCOMPARE(item->gestureOverrideEventsReceived, 0);
       
   769 }
       
   770 
       
   771 void tst_Gestures::graphicsItemTreeGesture()
       
   772 {
       
   773     QGraphicsScene scene;
       
   774     QGraphicsView view(&scene);
       
   775 
       
   776     GestureItem *item1 = new GestureItem("item1");
       
   777     item1->setPos(100, 100);
       
   778     item1->size = QRectF(0, 0, 350, 200);
       
   779     scene.addItem(item1);
       
   780 
       
   781     GestureItem *item1_child1 = new GestureItem("item1_child1");
       
   782     item1_child1->setPos(50, 50);
       
   783     item1_child1->size = QRectF(0, 0, 100, 100);
       
   784     item1_child1->setParentItem(item1);
       
   785 
       
   786     GestureItem *item1_child2 = new GestureItem("item1_child2");
       
   787     item1_child2->size = QRectF(0, 0, 100, 100);
       
   788     item1_child2->setPos(200, 50);
       
   789     item1_child2->setParentItem(item1);
       
   790 
       
   791     view.show();
       
   792     QTest::qWaitForWindowShown(&view);
       
   793     view.ensureVisible(scene.sceneRect());
       
   794 
       
   795     view.viewport()->grabGesture(CustomGesture::GestureType, Qt::WidgetGesture);
       
   796     item1->grabGesture(CustomGesture::GestureType);
       
   797 
       
   798     static const int TotalGestureEventsCount = CustomGesture::SerialFinishedThreshold - CustomGesture::SerialStartedThreshold + 1;
       
   799 
       
   800     CustomEvent event;
       
   801     event.hotSpot = mapToGlobal(QPointF(10, 10), item1_child1, &view);
       
   802     event.hasHotSpot = true;
       
   803 
       
   804     item1->ignoredGestures << CustomGesture::GestureType;
       
   805     sendCustomGesture(&event, item1_child1, &scene);
       
   806     QCOMPARE(item1_child1->gestureOverrideEventsReceived, 0);
       
   807     QCOMPARE(item1_child1->gestureEventsReceived, 0);
       
   808     QCOMPARE(item1_child2->gestureEventsReceived, 0);
       
   809     QCOMPARE(item1_child2->gestureOverrideEventsReceived, 0);
       
   810     QCOMPARE(item1->gestureOverrideEventsReceived, 0);
       
   811     QCOMPARE(item1->gestureEventsReceived, TotalGestureEventsCount);
       
   812 
       
   813     item1->reset(); item1_child1->reset(); item1_child2->reset();
       
   814 
       
   815     item1_child1->grabGesture(CustomGesture::GestureType);
       
   816 
       
   817     item1->ignoredGestures << CustomGesture::GestureType;
       
   818     item1_child1->ignoredGestures << CustomGesture::GestureType;
       
   819     sendCustomGesture(&event, item1_child1, &scene);
       
   820     QCOMPARE(item1_child1->gestureOverrideEventsReceived, 1);
       
   821     QCOMPARE(item1_child1->gestureEventsReceived, TotalGestureEventsCount);
       
   822     QCOMPARE(item1_child2->gestureEventsReceived, 0);
       
   823     QCOMPARE(item1_child2->gestureOverrideEventsReceived, 0);
       
   824     QCOMPARE(item1->gestureOverrideEventsReceived, 1);
       
   825     QCOMPARE(item1->gestureEventsReceived, TotalGestureEventsCount);
       
   826 }
       
   827 
       
   828 void tst_Gestures::explicitGraphicsObjectTarget()
       
   829 {
       
   830     QGraphicsScene scene;
       
   831     QGraphicsView view(&scene);
       
   832 
       
   833     GestureItem *item1 = new GestureItem("item1");
       
   834     scene.addItem(item1);
       
   835     item1->setPos(100, 100);
       
   836     item1->setZValue(1);
       
   837 
       
   838     GestureItem *item2 = new GestureItem("item2");
       
   839     scene.addItem(item2);
       
   840     item2->setPos(100, 100);
       
   841     item2->setZValue(5);
       
   842 
       
   843     GestureItem *item2_child1 = new GestureItem("item2_child1");
       
   844     scene.addItem(item2_child1);
       
   845     item2_child1->setParentItem(item2);
       
   846     item2_child1->setPos(10, 10);
       
   847 
       
   848     view.show();
       
   849     QTest::qWaitForWindowShown(&view);
       
   850     view.ensureVisible(scene.sceneRect());
       
   851 
       
   852     view.viewport()->grabGesture(CustomGesture::GestureType, Qt::WidgetGesture);
       
   853     item1->grabGesture(CustomGesture::GestureType, Qt::ItemGesture);
       
   854     item2->grabGesture(CustomGesture::GestureType, Qt::ItemGesture);
       
   855     item2_child1->grabGesture(CustomGesture::GestureType, Qt::ItemGesture);
       
   856 
       
   857     static const int TotalGestureEventsCount = CustomGesture::SerialFinishedThreshold - CustomGesture::SerialStartedThreshold + 1;
       
   858 
       
   859     // sending events to item1, but the hotSpot is set to item2
       
   860     CustomEvent event;
       
   861     event.hotSpot = mapToGlobal(QPointF(15, 15), item2, &view);
       
   862     event.hasHotSpot = true;
       
   863 
       
   864     sendCustomGesture(&event, item1, &scene);
       
   865 
       
   866     QCOMPARE(item1->gestureEventsReceived, 0);
       
   867     QCOMPARE(item1->gestureOverrideEventsReceived, 1);
       
   868     QCOMPARE(item2_child1->gestureEventsReceived, TotalGestureEventsCount);
       
   869     QCOMPARE(item2_child1->gestureOverrideEventsReceived, 1);
       
   870     QCOMPARE(item2_child1->events.all.size(), TotalGestureEventsCount);
       
   871     for(int i = 0; i < item2_child1->events.all.size(); ++i)
       
   872         QCOMPARE(item2_child1->events.all.at(i), CustomGesture::GestureType);
       
   873     QCOMPARE(item2_child1->events.started.size(), 1);
       
   874     QCOMPARE(item2_child1->events.updated.size(), TotalGestureEventsCount - 2);
       
   875     QCOMPARE(item2_child1->events.finished.size(), 1);
       
   876     QCOMPARE(item2_child1->events.canceled.size(), 0);
       
   877     QCOMPARE(item2->gestureEventsReceived, 0);
       
   878     QCOMPARE(item2->gestureOverrideEventsReceived, 1);
       
   879 }
       
   880 
       
   881 void tst_Gestures::gestureOverChildGraphicsItem()
       
   882 {
       
   883     QGraphicsScene scene;
       
   884     QGraphicsView view(&scene);
       
   885 
       
   886     GestureItem *item0 = new GestureItem("item0");
       
   887     scene.addItem(item0);
       
   888     item0->setPos(0, 0);
       
   889     item0->grabGesture(CustomGesture::GestureType);
       
   890     item0->setZValue(1);
       
   891 
       
   892     GestureItem *item1 = new GestureItem("item1");
       
   893     scene.addItem(item1);
       
   894     item1->setPos(100, 100);
       
   895     item1->setZValue(5);
       
   896 
       
   897     GestureItem *item2 = new GestureItem("item2");
       
   898     scene.addItem(item2);
       
   899     item2->setPos(100, 100);
       
   900     item2->setZValue(10);
       
   901 
       
   902     GestureItem *item2_child1 = new GestureItem("item2_child1");
       
   903     scene.addItem(item2_child1);
       
   904     item2_child1->setParentItem(item2);
       
   905     item2_child1->setPos(0, 0);
       
   906 
       
   907     view.show();
       
   908     QTest::qWaitForWindowShown(&view);
       
   909     view.ensureVisible(scene.sceneRect());
       
   910 
       
   911     view.viewport()->grabGesture(CustomGesture::GestureType, Qt::WidgetGesture);
       
   912     item1->grabGesture(CustomGesture::GestureType);
       
   913 
       
   914     static const int TotalGestureEventsCount = CustomGesture::SerialFinishedThreshold - CustomGesture::SerialStartedThreshold + 1;
       
   915     static const int TotalCustomEventsCount = CustomGesture::SerialFinishedThreshold - CustomGesture::SerialMaybeThreshold + 1;
       
   916 
       
   917     CustomEvent event;
       
   918     event.hotSpot = mapToGlobal(QPointF(10, 10), item2_child1, &view);
       
   919     event.hasHotSpot = true;
       
   920     sendCustomGesture(&event, item0, &scene);
       
   921 
       
   922     QCOMPARE(item0->customEventsReceived, TotalCustomEventsCount);
       
   923     QCOMPARE(item2_child1->gestureEventsReceived, 0);
       
   924     QCOMPARE(item2_child1->gestureOverrideEventsReceived, 0);
       
   925     QCOMPARE(item2->gestureEventsReceived, 0);
       
   926     QCOMPARE(item2->gestureOverrideEventsReceived, 0);
       
   927     QCOMPARE(item1->gestureEventsReceived, TotalGestureEventsCount);
       
   928     QCOMPARE(item1->gestureOverrideEventsReceived, 0);
       
   929 
       
   930     item0->reset(); item1->reset(); item2->reset(); item2_child1->reset();
       
   931     item2->grabGesture(CustomGesture::GestureType);
       
   932     item2->ignoredGestures << CustomGesture::GestureType;
       
   933 
       
   934     event.hotSpot = mapToGlobal(QPointF(10, 10), item2_child1, &view);
       
   935     event.hasHotSpot = true;
       
   936     sendCustomGesture(&event, item0, &scene);
       
   937 
       
   938     QCOMPARE(item0->customEventsReceived, TotalCustomEventsCount);
       
   939     QCOMPARE(item2_child1->gestureEventsReceived, 0);
       
   940     QCOMPARE(item2_child1->gestureOverrideEventsReceived, 0);
       
   941     QCOMPARE(item2->gestureEventsReceived, TotalGestureEventsCount);
       
   942     QCOMPARE(item2->gestureOverrideEventsReceived, 1);
       
   943     QCOMPARE(item1->gestureEventsReceived, TotalGestureEventsCount);
       
   944     QCOMPARE(item1->gestureOverrideEventsReceived, 1);
       
   945 }
       
   946 
       
   947 void tst_Gestures::twoGesturesOnDifferentLevel()
       
   948 {
       
   949     GestureWidget parent("parent");
       
   950     QVBoxLayout *l = new QVBoxLayout(&parent);
       
   951     GestureWidget *child = new GestureWidget("child");
       
   952     l->addWidget(child);
       
   953 
       
   954     Qt::GestureType SecondGesture = qApp->registerGestureRecognizer(new CustomGestureRecognizer);
       
   955 
       
   956     parent.grabGesture(CustomGesture::GestureType, Qt::WidgetWithChildrenGesture);
       
   957     child->grabGesture(SecondGesture, Qt::WidgetWithChildrenGesture);
       
   958 
       
   959     CustomEvent event;
       
   960     // sending events that form a gesture to one widget, but they will be
       
   961     // filtered by two different gesture recognizers and will generate two
       
   962     // QGesture objects. Check that those gesture objects are delivered to
       
   963     // different widgets properly.
       
   964     sendCustomGesture(&event, child);
       
   965 
       
   966     static const int TotalGestureEventsCount = CustomGesture::SerialFinishedThreshold - CustomGesture::SerialStartedThreshold + 1;
       
   967     static const int TotalCustomEventsCount = CustomGesture::SerialFinishedThreshold - CustomGesture::SerialMaybeThreshold + 1;
       
   968 
       
   969     QCOMPARE(child->customEventsReceived, TotalCustomEventsCount);
       
   970     QCOMPARE(child->gestureEventsReceived, TotalGestureEventsCount);
       
   971     QCOMPARE(child->gestureOverrideEventsReceived, 0);
       
   972     QCOMPARE(child->events.all.size(), TotalGestureEventsCount);
       
   973     for(int i = 0; i < child->events.all.size(); ++i)
       
   974         QCOMPARE(child->events.all.at(i), SecondGesture);
       
   975 
       
   976     QCOMPARE(parent.gestureEventsReceived, TotalGestureEventsCount);
       
   977     QCOMPARE(parent.gestureOverrideEventsReceived, 0);
       
   978     QCOMPARE(parent.events.all.size(), TotalGestureEventsCount);
       
   979     for(int i = 0; i < child->events.all.size(); ++i)
       
   980         QCOMPARE(parent.events.all.at(i), CustomGesture::GestureType);
       
   981 }
       
   982 
       
   983 void tst_Gestures::multipleGesturesInTree()
       
   984 {
       
   985     GestureWidget a("A");
       
   986     GestureWidget *A = &a;
       
   987     GestureWidget *B = new GestureWidget("B", A);
       
   988     GestureWidget *C = new GestureWidget("C", B);
       
   989     GestureWidget *D = new GestureWidget("D", C);
       
   990 
       
   991     Qt::GestureType FirstGesture  = CustomGesture::GestureType;
       
   992     Qt::GestureType SecondGesture = qApp->registerGestureRecognizer(new CustomGestureRecognizer);
       
   993     Qt::GestureType ThirdGesture  = qApp->registerGestureRecognizer(new CustomGestureRecognizer);
       
   994 
       
   995     A->grabGesture(FirstGesture, Qt::WidgetWithChildrenGesture);   // A [1   3]
       
   996     A->grabGesture(ThirdGesture, Qt::WidgetWithChildrenGesture);   // |
       
   997     B->grabGesture(SecondGesture, Qt::WidgetWithChildrenGesture);  // B [  2 3]
       
   998     B->grabGesture(ThirdGesture, Qt::WidgetWithChildrenGesture);   // |
       
   999     C->grabGesture(FirstGesture, Qt::WidgetWithChildrenGesture);   // C [1 2 3]
       
  1000     C->grabGesture(SecondGesture, Qt::WidgetWithChildrenGesture);  // |
       
  1001     C->grabGesture(ThirdGesture, Qt::WidgetWithChildrenGesture);   // D [1   3]
       
  1002     D->grabGesture(FirstGesture, Qt::WidgetWithChildrenGesture);
       
  1003     D->grabGesture(ThirdGesture, Qt::WidgetWithChildrenGesture);
       
  1004 
       
  1005     // make sure all widgets ignore events, so they get propagated.
       
  1006     A->ignoredGestures << FirstGesture << ThirdGesture;
       
  1007     B->ignoredGestures << SecondGesture << ThirdGesture;
       
  1008     C->ignoredGestures << FirstGesture << SecondGesture << ThirdGesture;
       
  1009     D->ignoredGestures << FirstGesture << ThirdGesture;
       
  1010 
       
  1011     CustomEvent event;
       
  1012     sendCustomGesture(&event, D);
       
  1013 
       
  1014     static const int TotalGestureEventsCount = CustomGesture::SerialFinishedThreshold - CustomGesture::SerialStartedThreshold + 1;
       
  1015 
       
  1016     // gesture override events
       
  1017     QCOMPARE(D->overrideEvents.all.count(FirstGesture), 1);
       
  1018     QCOMPARE(D->overrideEvents.all.count(SecondGesture), 0);
       
  1019     QCOMPARE(D->overrideEvents.all.count(ThirdGesture), 1);
       
  1020 
       
  1021     QCOMPARE(C->overrideEvents.all.count(FirstGesture), 1);
       
  1022     QCOMPARE(C->overrideEvents.all.count(SecondGesture), 1);
       
  1023     QCOMPARE(C->overrideEvents.all.count(ThirdGesture), 1);
       
  1024 
       
  1025     QCOMPARE(B->overrideEvents.all.count(FirstGesture), 0);
       
  1026     QCOMPARE(B->overrideEvents.all.count(SecondGesture), 1);
       
  1027     QCOMPARE(B->overrideEvents.all.count(ThirdGesture), 1);
       
  1028 
       
  1029     QCOMPARE(A->overrideEvents.all.count(FirstGesture), 1);
       
  1030     QCOMPARE(A->overrideEvents.all.count(SecondGesture), 0);
       
  1031     QCOMPARE(A->overrideEvents.all.count(ThirdGesture), 1);
       
  1032 
       
  1033     // normal gesture events
       
  1034     QCOMPARE(D->events.all.count(FirstGesture), TotalGestureEventsCount);
       
  1035     QCOMPARE(D->events.all.count(SecondGesture), 0);
       
  1036     QCOMPARE(D->events.all.count(ThirdGesture), TotalGestureEventsCount);
       
  1037 
       
  1038     QCOMPARE(C->events.all.count(FirstGesture), TotalGestureEventsCount);
       
  1039     QCOMPARE(C->events.all.count(SecondGesture), TotalGestureEventsCount);
       
  1040     QCOMPARE(C->events.all.count(ThirdGesture), TotalGestureEventsCount);
       
  1041 
       
  1042     QCOMPARE(B->events.all.count(FirstGesture), 0);
       
  1043     QCOMPARE(B->events.all.count(SecondGesture), TotalGestureEventsCount);
       
  1044     QCOMPARE(B->events.all.count(ThirdGesture), TotalGestureEventsCount);
       
  1045 
       
  1046     QCOMPARE(A->events.all.count(FirstGesture), TotalGestureEventsCount);
       
  1047     QCOMPARE(A->events.all.count(SecondGesture), 0);
       
  1048     QCOMPARE(A->events.all.count(ThirdGesture), TotalGestureEventsCount);
       
  1049 }
       
  1050 
       
  1051 void tst_Gestures::multipleGesturesInComplexTree()
       
  1052 {
       
  1053     GestureWidget a("A");
       
  1054     GestureWidget *A = &a;
       
  1055     GestureWidget *B = new GestureWidget("B", A);
       
  1056     GestureWidget *C = new GestureWidget("C", B);
       
  1057     GestureWidget *D = new GestureWidget("D", C);
       
  1058 
       
  1059     Qt::GestureType FirstGesture   = CustomGesture::GestureType;
       
  1060     Qt::GestureType SecondGesture  = qApp->registerGestureRecognizer(new CustomGestureRecognizer);
       
  1061     Qt::GestureType ThirdGesture   = qApp->registerGestureRecognizer(new CustomGestureRecognizer);
       
  1062     Qt::GestureType FourthGesture  = qApp->registerGestureRecognizer(new CustomGestureRecognizer);
       
  1063     Qt::GestureType FifthGesture   = qApp->registerGestureRecognizer(new CustomGestureRecognizer);
       
  1064     Qt::GestureType SixthGesture   = qApp->registerGestureRecognizer(new CustomGestureRecognizer);
       
  1065     Qt::GestureType SeventhGesture = qApp->registerGestureRecognizer(new CustomGestureRecognizer);
       
  1066 
       
  1067     A->grabGesture(FirstGesture, Qt::WidgetWithChildrenGesture);   // A [1,3,4]
       
  1068     A->grabGesture(ThirdGesture, Qt::WidgetWithChildrenGesture);   // |
       
  1069     A->grabGesture(FourthGesture, Qt::WidgetWithChildrenGesture);  // B [2,3,5]
       
  1070     B->grabGesture(SecondGesture, Qt::WidgetWithChildrenGesture);  // |
       
  1071     B->grabGesture(ThirdGesture, Qt::WidgetWithChildrenGesture);   // C [1,2,3,6]
       
  1072     B->grabGesture(FifthGesture, Qt::WidgetWithChildrenGesture);   // |
       
  1073     C->grabGesture(FirstGesture, Qt::WidgetWithChildrenGesture);   // D [1,3,7]
       
  1074     C->grabGesture(SecondGesture, Qt::WidgetWithChildrenGesture);
       
  1075     C->grabGesture(ThirdGesture, Qt::WidgetWithChildrenGesture);
       
  1076     C->grabGesture(SixthGesture, Qt::WidgetWithChildrenGesture);
       
  1077     D->grabGesture(FirstGesture, Qt::WidgetWithChildrenGesture);
       
  1078     D->grabGesture(ThirdGesture, Qt::WidgetWithChildrenGesture);
       
  1079     D->grabGesture(SeventhGesture, Qt::WidgetWithChildrenGesture);
       
  1080 
       
  1081     // make sure all widgets ignore events, so they get propagated.
       
  1082     QSet<Qt::GestureType> allGestureTypes;
       
  1083     allGestureTypes << FirstGesture << SecondGesture << ThirdGesture
       
  1084             << FourthGesture << FifthGesture << SixthGesture << SeventhGesture;
       
  1085     A->ignoredGestures = B->ignoredGestures = allGestureTypes;
       
  1086     C->ignoredGestures = D->ignoredGestures = allGestureTypes;
       
  1087 
       
  1088     CustomEvent event;
       
  1089     sendCustomGesture(&event, D);
       
  1090 
       
  1091     static const int TotalGestureEventsCount = CustomGesture::SerialFinishedThreshold - CustomGesture::SerialStartedThreshold + 1;
       
  1092 
       
  1093     // gesture override events
       
  1094     QCOMPARE(D->overrideEvents.all.count(FirstGesture), 1);
       
  1095     QCOMPARE(D->overrideEvents.all.count(SecondGesture), 0);
       
  1096     QCOMPARE(D->overrideEvents.all.count(ThirdGesture), 1);
       
  1097 
       
  1098     QCOMPARE(C->overrideEvents.all.count(FirstGesture), 1);
       
  1099     QCOMPARE(C->overrideEvents.all.count(SecondGesture), 1);
       
  1100     QCOMPARE(C->overrideEvents.all.count(ThirdGesture), 1);
       
  1101 
       
  1102     QCOMPARE(B->overrideEvents.all.count(FirstGesture), 0);
       
  1103     QCOMPARE(B->overrideEvents.all.count(SecondGesture), 1);
       
  1104     QCOMPARE(B->overrideEvents.all.count(ThirdGesture), 1);
       
  1105 
       
  1106     QCOMPARE(A->overrideEvents.all.count(FirstGesture), 1);
       
  1107     QCOMPARE(A->overrideEvents.all.count(SecondGesture), 0);
       
  1108     QCOMPARE(A->overrideEvents.all.count(ThirdGesture), 1);
       
  1109 
       
  1110     // normal gesture events
       
  1111     QCOMPARE(D->events.all.count(FirstGesture), TotalGestureEventsCount);
       
  1112     QCOMPARE(D->events.all.count(SecondGesture), 0);
       
  1113     QCOMPARE(D->events.all.count(ThirdGesture), TotalGestureEventsCount);
       
  1114     QCOMPARE(D->events.all.count(FourthGesture), 0);
       
  1115     QCOMPARE(D->events.all.count(FifthGesture), 0);
       
  1116     QCOMPARE(D->events.all.count(SixthGesture), 0);
       
  1117     QCOMPARE(D->events.all.count(SeventhGesture), TotalGestureEventsCount);
       
  1118 
       
  1119     QCOMPARE(C->events.all.count(FirstGesture), TotalGestureEventsCount);
       
  1120     QCOMPARE(C->events.all.count(SecondGesture), TotalGestureEventsCount);
       
  1121     QCOMPARE(C->events.all.count(ThirdGesture), TotalGestureEventsCount);
       
  1122     QCOMPARE(C->events.all.count(FourthGesture), 0);
       
  1123     QCOMPARE(C->events.all.count(FifthGesture), 0);
       
  1124     QCOMPARE(C->events.all.count(SixthGesture), TotalGestureEventsCount);
       
  1125     QCOMPARE(C->events.all.count(SeventhGesture), 0);
       
  1126 
       
  1127     QCOMPARE(B->events.all.count(FirstGesture), 0);
       
  1128     QCOMPARE(B->events.all.count(SecondGesture), TotalGestureEventsCount);
       
  1129     QCOMPARE(B->events.all.count(ThirdGesture), TotalGestureEventsCount);
       
  1130     QCOMPARE(B->events.all.count(FourthGesture), 0);
       
  1131     QCOMPARE(B->events.all.count(FifthGesture), TotalGestureEventsCount);
       
  1132     QCOMPARE(B->events.all.count(SixthGesture), 0);
       
  1133     QCOMPARE(B->events.all.count(SeventhGesture), 0);
       
  1134 
       
  1135     QCOMPARE(A->events.all.count(FirstGesture), TotalGestureEventsCount);
       
  1136     QCOMPARE(A->events.all.count(SecondGesture), 0);
       
  1137     QCOMPARE(A->events.all.count(ThirdGesture), TotalGestureEventsCount);
       
  1138     QCOMPARE(A->events.all.count(FourthGesture), TotalGestureEventsCount);
       
  1139     QCOMPARE(A->events.all.count(FifthGesture), 0);
       
  1140     QCOMPARE(A->events.all.count(SixthGesture), 0);
       
  1141     QCOMPARE(A->events.all.count(SeventhGesture), 0);
       
  1142 }
       
  1143 
       
  1144 void tst_Gestures::testMapToScene()
       
  1145 {
       
  1146     QGesture gesture;
       
  1147     QList<QGesture*> list;
       
  1148     list << &gesture;
       
  1149     QGestureEvent event(list);
       
  1150     QCOMPARE(event.mapToScene(gesture.hotSpot()), QPointF()); // not set, can't do much
       
  1151 
       
  1152     QGraphicsScene scene;
       
  1153     QGraphicsView view(&scene);
       
  1154 
       
  1155     GestureItem *item0 = new GestureItem;
       
  1156     scene.addItem(item0);
       
  1157     item0->setPos(14, 16);
       
  1158 
       
  1159     view.show(); // need to show to give it a global coordinate
       
  1160     QTest::qWaitForWindowShown(&view);
       
  1161     view.ensureVisible(scene.sceneRect());
       
  1162 
       
  1163     QPoint origin = view.mapToGlobal(QPoint());
       
  1164     event.setWidget(view.viewport());
       
  1165 
       
  1166     QCOMPARE(event.mapToScene(origin + QPoint(100, 200)), view.mapToScene(QPoint(100, 200)));
       
  1167 }
       
  1168 
       
  1169 QTEST_MAIN(tst_Gestures)
       
  1170 #include "tst_gestures.moc"