Warning, file /plasma/kwin/autotests/test_gestures.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     KWin - the KDE window manager
0003     This file is part of the KDE project.
0004 
0005     SPDX-FileCopyrightText: 2017 Martin Gräßlin <mgraesslin@kde.org>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 #include "gestures.h"
0010 
0011 #include <QSignalSpy>
0012 #include <QTest>
0013 #include <QtWidgets/qaction.h>
0014 #include <iostream>
0015 
0016 using namespace KWin;
0017 
0018 class GestureTest : public QObject
0019 {
0020     Q_OBJECT
0021 private Q_SLOTS:
0022     void testSwipeMinFinger_data();
0023     void testSwipeMinFinger();
0024     void testPinchMinFinger_data();
0025     void testPinchMinFinger();
0026 
0027     void testSwipeMaxFinger_data();
0028     void testSwipeMaxFinger();
0029     void testPinchMaxFinger_data();
0030     void testPinchMaxFinger();
0031 
0032     void testSwipeDirection_data();
0033     void testSwipeDirection();
0034     void testPinchDirection_data();
0035     void testPinchDirection();
0036 
0037     // swipe only
0038     void testMinimumX_data();
0039     void testMinimumX();
0040     void testMinimumY_data();
0041     void testMinimumY();
0042     void testMaximumX_data();
0043     void testMaximumX();
0044     void testMaximumY_data();
0045     void testMaximumY();
0046     void testStartGeometry();
0047 
0048     // swipe and pinch
0049     void testSetMinimumDelta();
0050     void testMinimumDeltaReached_data();
0051     void testMinimumDeltaReached();
0052     void testMinimumScaleDelta();
0053     void testUnregisterSwipeCancels();
0054     void testUnregisterPinchCancels();
0055     void testDeleteSwipeCancels();
0056     void testSwipeCancel_data();
0057     void testSwipeCancel();
0058     void testSwipeUpdateTrigger_data();
0059     void testSwipeUpdateTrigger();
0060 
0061     // both
0062     void testSwipeMinFingerStart_data();
0063     void testSwipeMinFingerStart();
0064     void testSwipeMaxFingerStart_data();
0065     void testSwipeMaxFingerStart();
0066     void testNotEmitCallbacksBeforeDirectionDecided();
0067 
0068     // swipe only
0069     void testSwipeGeometryStart_data();
0070     void testSwipeGeometryStart();
0071 };
0072 
0073 void GestureTest::testSwipeMinFinger_data()
0074 {
0075     QTest::addColumn<uint>("count");
0076     QTest::addColumn<uint>("expectedCount");
0077 
0078     QTest::newRow("0") << 0u << 0u;
0079     QTest::newRow("1") << 1u << 1u;
0080     QTest::newRow("10") << 10u << 10u;
0081 }
0082 
0083 void GestureTest::testSwipeMinFinger()
0084 {
0085     SwipeGesture swipeGesture;
0086     QCOMPARE(swipeGesture.minimumFingerCountIsRelevant(), false);
0087     QCOMPARE(swipeGesture.minimumFingerCount(), 0u);
0088     QFETCH(uint, count);
0089     swipeGesture.setMinimumFingerCount(count);
0090     QCOMPARE(swipeGesture.minimumFingerCountIsRelevant(), true);
0091     QTEST(swipeGesture.minimumFingerCount(), "expectedCount");
0092     swipeGesture.setMinimumFingerCount(0);
0093     QCOMPARE(swipeGesture.minimumFingerCountIsRelevant(), true);
0094     QCOMPARE(swipeGesture.minimumFingerCount(), 0u);
0095 }
0096 
0097 void GestureTest::testPinchMinFinger_data()
0098 {
0099     QTest::addColumn<uint>("count");
0100     QTest::addColumn<uint>("expectedCount");
0101 
0102     QTest::newRow("0") << 0u << 0u;
0103     QTest::newRow("1") << 1u << 1u;
0104     QTest::newRow("10") << 10u << 10u;
0105 }
0106 
0107 void GestureTest::testPinchMinFinger()
0108 {
0109     PinchGesture pinchGesture;
0110     QCOMPARE(pinchGesture.minimumFingerCountIsRelevant(), false);
0111     QCOMPARE(pinchGesture.minimumFingerCount(), 0u);
0112     QFETCH(uint, count);
0113     pinchGesture.setMinimumFingerCount(count);
0114     QCOMPARE(pinchGesture.minimumFingerCountIsRelevant(), true);
0115     QTEST(pinchGesture.minimumFingerCount(), "expectedCount");
0116     pinchGesture.setMinimumFingerCount(0);
0117     QCOMPARE(pinchGesture.minimumFingerCountIsRelevant(), true);
0118     QCOMPARE(pinchGesture.minimumFingerCount(), 0u);
0119 }
0120 
0121 void GestureTest::testSwipeMaxFinger_data()
0122 {
0123     QTest::addColumn<uint>("count");
0124     QTest::addColumn<uint>("expectedCount");
0125 
0126     QTest::newRow("0") << 0u << 0u;
0127     QTest::newRow("1") << 1u << 1u;
0128     QTest::newRow("10") << 10u << 10u;
0129 }
0130 
0131 void GestureTest::testSwipeMaxFinger()
0132 {
0133     SwipeGesture gesture;
0134     QCOMPARE(gesture.maximumFingerCountIsRelevant(), false);
0135     QCOMPARE(gesture.maximumFingerCount(), 0u);
0136     QFETCH(uint, count);
0137     gesture.setMaximumFingerCount(count);
0138     QCOMPARE(gesture.maximumFingerCountIsRelevant(), true);
0139     QTEST(gesture.maximumFingerCount(), "expectedCount");
0140     gesture.setMaximumFingerCount(0);
0141     QCOMPARE(gesture.maximumFingerCountIsRelevant(), true);
0142     QCOMPARE(gesture.maximumFingerCount(), 0u);
0143 }
0144 
0145 void GestureTest::testPinchMaxFinger_data()
0146 {
0147     QTest::addColumn<uint>("count");
0148     QTest::addColumn<uint>("expectedCount");
0149 
0150     QTest::newRow("0") << 0u << 0u;
0151     QTest::newRow("1") << 1u << 1u;
0152     QTest::newRow("10") << 10u << 10u;
0153 }
0154 
0155 void GestureTest::testPinchMaxFinger()
0156 {
0157     PinchGesture gesture;
0158     QCOMPARE(gesture.maximumFingerCountIsRelevant(), false);
0159     QCOMPARE(gesture.maximumFingerCount(), 0u);
0160     QFETCH(uint, count);
0161     gesture.setMaximumFingerCount(count);
0162     QCOMPARE(gesture.maximumFingerCountIsRelevant(), true);
0163     QTEST(gesture.maximumFingerCount(), "expectedCount");
0164     gesture.setMaximumFingerCount(0);
0165     QCOMPARE(gesture.maximumFingerCountIsRelevant(), true);
0166     QCOMPARE(gesture.maximumFingerCount(), 0u);
0167 }
0168 
0169 void GestureTest::testSwipeDirection_data()
0170 {
0171     QTest::addColumn<KWin::SwipeGesture::Direction>("swipe_direction");
0172 
0173     QTest::newRow("Up") << KWin::SwipeGesture::Direction::Up;
0174     QTest::newRow("Left") << KWin::SwipeGesture::Direction::Left;
0175     QTest::newRow("Right") << KWin::SwipeGesture::Direction::Right;
0176     QTest::newRow("Down") << KWin::SwipeGesture::Direction::Down;
0177 }
0178 
0179 void GestureTest::testSwipeDirection()
0180 {
0181     SwipeGesture gesture;
0182     QCOMPARE(gesture.direction(), SwipeGesture::Direction::Down);
0183     QFETCH(KWin::SwipeGesture::Direction, swipe_direction);
0184     gesture.setDirection(swipe_direction);
0185     QCOMPARE(gesture.direction(), swipe_direction);
0186     // back to down
0187     gesture.setDirection(SwipeGesture::Direction::Down);
0188     QCOMPARE(gesture.direction(), SwipeGesture::Direction::Down);
0189 }
0190 
0191 void GestureTest::testPinchDirection_data()
0192 {
0193     QTest::addColumn<KWin::PinchGesture::Direction>("pinch_direction");
0194 
0195     QTest::newRow("Contracting") << KWin::PinchGesture::Direction::Contracting;
0196     QTest::newRow("Expanding") << KWin::PinchGesture::Direction::Expanding;
0197 }
0198 
0199 void GestureTest::testPinchDirection()
0200 {
0201     PinchGesture gesture;
0202     QCOMPARE(gesture.direction(), PinchGesture::Direction::Expanding);
0203     QFETCH(KWin::PinchGesture::Direction, pinch_direction);
0204     gesture.setDirection(pinch_direction);
0205     QCOMPARE(gesture.direction(), pinch_direction);
0206     // back to down
0207     gesture.setDirection(PinchGesture::Direction::Expanding);
0208     QCOMPARE(gesture.direction(), PinchGesture::Direction::Expanding);
0209 }
0210 
0211 void GestureTest::testMinimumX_data()
0212 {
0213     QTest::addColumn<int>("min");
0214 
0215     QTest::newRow("0") << 0;
0216     QTest::newRow("-1") << -1;
0217     QTest::newRow("1") << 1;
0218 }
0219 
0220 void GestureTest::testMinimumX()
0221 {
0222     SwipeGesture gesture;
0223     QCOMPARE(gesture.minimumX(), 0);
0224     QCOMPARE(gesture.minimumXIsRelevant(), false);
0225     QFETCH(int, min);
0226     gesture.setMinimumX(min);
0227     QCOMPARE(gesture.minimumX(), min);
0228     QCOMPARE(gesture.minimumXIsRelevant(), true);
0229 }
0230 
0231 void GestureTest::testMinimumY_data()
0232 {
0233     QTest::addColumn<int>("min");
0234 
0235     QTest::newRow("0") << 0;
0236     QTest::newRow("-1") << -1;
0237     QTest::newRow("1") << 1;
0238 }
0239 
0240 void GestureTest::testMinimumY()
0241 {
0242     SwipeGesture gesture;
0243     QCOMPARE(gesture.minimumY(), 0);
0244     QCOMPARE(gesture.minimumYIsRelevant(), false);
0245     QFETCH(int, min);
0246     gesture.setMinimumY(min);
0247     QCOMPARE(gesture.minimumY(), min);
0248     QCOMPARE(gesture.minimumYIsRelevant(), true);
0249 }
0250 
0251 void GestureTest::testMaximumX_data()
0252 {
0253     QTest::addColumn<int>("max");
0254 
0255     QTest::newRow("0") << 0;
0256     QTest::newRow("-1") << -1;
0257     QTest::newRow("1") << 1;
0258 }
0259 
0260 void GestureTest::testMaximumX()
0261 {
0262     SwipeGesture gesture;
0263     QCOMPARE(gesture.maximumX(), 0);
0264     QCOMPARE(gesture.maximumXIsRelevant(), false);
0265     QFETCH(int, max);
0266     gesture.setMaximumX(max);
0267     QCOMPARE(gesture.maximumX(), max);
0268     QCOMPARE(gesture.maximumXIsRelevant(), true);
0269 }
0270 
0271 void GestureTest::testMaximumY_data()
0272 {
0273     QTest::addColumn<int>("max");
0274 
0275     QTest::newRow("0") << 0;
0276     QTest::newRow("-1") << -1;
0277     QTest::newRow("1") << 1;
0278 }
0279 
0280 void GestureTest::testMaximumY()
0281 {
0282     SwipeGesture gesture;
0283     QCOMPARE(gesture.maximumY(), 0);
0284     QCOMPARE(gesture.maximumYIsRelevant(), false);
0285     QFETCH(int, max);
0286     gesture.setMaximumY(max);
0287     QCOMPARE(gesture.maximumY(), max);
0288     QCOMPARE(gesture.maximumYIsRelevant(), true);
0289 }
0290 
0291 void GestureTest::testStartGeometry()
0292 {
0293     SwipeGesture gesture;
0294     gesture.setStartGeometry(QRect(1, 2, 20, 30));
0295     QCOMPARE(gesture.minimumXIsRelevant(), true);
0296     QCOMPARE(gesture.minimumYIsRelevant(), true);
0297     QCOMPARE(gesture.maximumXIsRelevant(), true);
0298     QCOMPARE(gesture.maximumYIsRelevant(), true);
0299     QCOMPARE(gesture.minimumX(), 1);
0300     QCOMPARE(gesture.minimumY(), 2);
0301     QCOMPARE(gesture.maximumX(), 21);
0302     QCOMPARE(gesture.maximumY(), 32);
0303 }
0304 
0305 void GestureTest::testSetMinimumDelta()
0306 {
0307     SwipeGesture swipeGesture;
0308     QCOMPARE(swipeGesture.isMinimumDeltaRelevant(), false);
0309     QCOMPARE(swipeGesture.minimumDelta(), QPointF());
0310     QCOMPARE(swipeGesture.minimumDeltaReached(QPointF()), true);
0311     swipeGesture.setMinimumDelta(QPointF(2, 3));
0312     QCOMPARE(swipeGesture.isMinimumDeltaRelevant(), true);
0313     QCOMPARE(swipeGesture.minimumDelta(), QPointF(2, 3));
0314     QCOMPARE(swipeGesture.minimumDeltaReached(QPointF()), false);
0315     QCOMPARE(swipeGesture.minimumDeltaReached(QPointF(2, 3)), true);
0316 
0317     PinchGesture pinchGesture;
0318     QCOMPARE(pinchGesture.isMinimumScaleDeltaRelevant(), false);
0319     QCOMPARE(pinchGesture.minimumScaleDelta(), DEFAULT_UNIT_SCALE_DELTA);
0320     QCOMPARE(pinchGesture.minimumScaleDeltaReached(1.25), true);
0321     pinchGesture.setMinimumScaleDelta(.5);
0322     QCOMPARE(pinchGesture.isMinimumScaleDeltaRelevant(), true);
0323     QCOMPARE(pinchGesture.minimumScaleDelta(), .5);
0324     QCOMPARE(pinchGesture.minimumScaleDeltaReached(1.24), false);
0325     QCOMPARE(pinchGesture.minimumScaleDeltaReached(1.5), true);
0326 }
0327 
0328 void GestureTest::testMinimumDeltaReached_data()
0329 {
0330     QTest::addColumn<KWin::SwipeGesture::Direction>("direction");
0331     QTest::addColumn<QPointF>("minimumDelta");
0332     QTest::addColumn<QPointF>("delta");
0333     QTest::addColumn<bool>("reached");
0334     QTest::addColumn<qreal>("progress");
0335 
0336     QTest::newRow("Up (more)") << KWin::SwipeGesture::Direction::Up << QPointF(0, -30) << QPointF(0, -40) << true << 1.0;
0337     QTest::newRow("Up (exact)") << KWin::SwipeGesture::Direction::Up << QPointF(0, -30) << QPointF(0, -30) << true << 1.0;
0338     QTest::newRow("Up (less)") << KWin::SwipeGesture::Direction::Up << QPointF(0, -30) << QPointF(0, -29) << false << 29.0 / 30.0;
0339     QTest::newRow("Left (more)") << KWin::SwipeGesture::Direction::Left << QPointF(-30, -30) << QPointF(-40, 20) << true << 1.0;
0340     QTest::newRow("Left (exact)") << KWin::SwipeGesture::Direction::Left << QPointF(-30, -40) << QPointF(-30, 0) << true << 1.0;
0341     QTest::newRow("Left (less)") << KWin::SwipeGesture::Direction::Left << QPointF(-30, -30) << QPointF(-29, 0) << false << 29.0 / 30.0;
0342     QTest::newRow("Right (more)") << KWin::SwipeGesture::Direction::Right << QPointF(30, -30) << QPointF(40, 20) << true << 1.0;
0343     QTest::newRow("Right (exact)") << KWin::SwipeGesture::Direction::Right << QPointF(30, -40) << QPointF(30, 0) << true << 1.0;
0344     QTest::newRow("Right (less)") << KWin::SwipeGesture::Direction::Right << QPointF(30, -30) << QPointF(29, 0) << false << 29.0 / 30.0;
0345     QTest::newRow("Down (more)") << KWin::SwipeGesture::Direction::Down << QPointF(0, 30) << QPointF(0, 40) << true << 1.0;
0346     QTest::newRow("Down (exact)") << KWin::SwipeGesture::Direction::Down << QPointF(0, 30) << QPointF(0, 30) << true << 1.0;
0347     QTest::newRow("Down (less)") << KWin::SwipeGesture::Direction::Down << QPointF(0, 30) << QPointF(0, 29) << false << 29.0 / 30.0;
0348 }
0349 
0350 void GestureTest::testMinimumDeltaReached()
0351 {
0352     GestureRecognizer recognizer;
0353 
0354     // swipe gesture
0355     SwipeGesture gesture;
0356     QFETCH(SwipeGesture::Direction, direction);
0357     gesture.setDirection(direction);
0358     QFETCH(QPointF, minimumDelta);
0359     gesture.setMinimumDelta(minimumDelta);
0360     QFETCH(QPointF, delta);
0361     QFETCH(bool, reached);
0362     QCOMPARE(gesture.minimumDeltaReached(delta), reached);
0363 
0364     recognizer.registerSwipeGesture(&gesture);
0365 
0366     QSignalSpy startedSpy(&gesture, &SwipeGesture::started);
0367     QSignalSpy triggeredSpy(&gesture, &SwipeGesture::triggered);
0368     QSignalSpy cancelledSpy(&gesture, &SwipeGesture::cancelled);
0369     QSignalSpy progressSpy(&gesture, &SwipeGesture::progress);
0370 
0371     recognizer.startSwipeGesture(1);
0372     QCOMPARE(startedSpy.count(), 1);
0373     QCOMPARE(triggeredSpy.count(), 0);
0374     QCOMPARE(cancelledSpy.count(), 0);
0375     QCOMPARE(progressSpy.count(), 0);
0376 
0377     recognizer.updateSwipeGesture(delta);
0378     QCOMPARE(startedSpy.count(), 1);
0379     QCOMPARE(triggeredSpy.count(), 0);
0380     QCOMPARE(cancelledSpy.count(), 0);
0381     QCOMPARE(progressSpy.count(), 1);
0382     QTEST(progressSpy.first().first().value<qreal>(), "progress");
0383 
0384     recognizer.endSwipeGesture();
0385     QCOMPARE(startedSpy.count(), 1);
0386     QCOMPARE(progressSpy.count(), 1);
0387     QCOMPARE(triggeredSpy.isEmpty(), !reached);
0388     QCOMPARE(cancelledSpy.isEmpty(), reached);
0389 }
0390 
0391 void GestureTest::testMinimumScaleDelta()
0392 {
0393     // pinch gesture
0394     PinchGesture gesture;
0395     gesture.setDirection(PinchGesture::Direction::Contracting);
0396     gesture.setMinimumScaleDelta(.5);
0397     gesture.setMinimumFingerCount(3);
0398     gesture.setMaximumFingerCount(4);
0399 
0400     QCOMPARE(gesture.minimumScaleDeltaReached(1.25), false);
0401     QCOMPARE(gesture.minimumScaleDeltaReached(1.5), true);
0402 
0403     GestureRecognizer recognizer;
0404     recognizer.registerPinchGesture(&gesture);
0405 
0406     QSignalSpy startedSpy(&gesture, &PinchGesture::started);
0407     QSignalSpy triggeredSpy(&gesture, &PinchGesture::triggered);
0408     QSignalSpy cancelledSpy(&gesture, &PinchGesture::cancelled);
0409     QSignalSpy progressSpy(&gesture, &PinchGesture::progress);
0410 
0411     recognizer.startPinchGesture(4);
0412     QCOMPARE(startedSpy.count(), 1);
0413     QCOMPARE(triggeredSpy.count(), 0);
0414     QCOMPARE(cancelledSpy.count(), 0);
0415     QCOMPARE(progressSpy.count(), 0);
0416 }
0417 
0418 void GestureTest::testUnregisterSwipeCancels()
0419 {
0420     GestureRecognizer recognizer;
0421     std::unique_ptr<SwipeGesture> gesture(new SwipeGesture);
0422     QSignalSpy startedSpy(gesture.get(), &SwipeGesture::started);
0423     QSignalSpy cancelledSpy(gesture.get(), &SwipeGesture::cancelled);
0424 
0425     recognizer.registerSwipeGesture(gesture.get());
0426     recognizer.startSwipeGesture(1);
0427     QCOMPARE(startedSpy.count(), 1);
0428     QCOMPARE(cancelledSpy.count(), 0);
0429     recognizer.unregisterSwipeGesture(gesture.get());
0430     QCOMPARE(cancelledSpy.count(), 1);
0431 
0432     // delete the gesture should not trigger cancel
0433     gesture.reset();
0434     QCOMPARE(cancelledSpy.count(), 1);
0435 }
0436 
0437 void GestureTest::testUnregisterPinchCancels()
0438 {
0439     GestureRecognizer recognizer;
0440     std::unique_ptr<PinchGesture> gesture(new PinchGesture);
0441     QSignalSpy startedSpy(gesture.get(), &PinchGesture::started);
0442     QSignalSpy cancelledSpy(gesture.get(), &PinchGesture::cancelled);
0443 
0444     recognizer.registerPinchGesture(gesture.get());
0445     recognizer.startPinchGesture(1);
0446     QCOMPARE(startedSpy.count(), 1);
0447     QCOMPARE(cancelledSpy.count(), 0);
0448     recognizer.unregisterPinchGesture(gesture.get());
0449     QCOMPARE(cancelledSpy.count(), 1);
0450 
0451     // delete the gesture should not trigger cancel
0452     gesture.reset();
0453     QCOMPARE(cancelledSpy.count(), 1);
0454 }
0455 
0456 void GestureTest::testDeleteSwipeCancels()
0457 {
0458     GestureRecognizer recognizer;
0459     std::unique_ptr<SwipeGesture> gesture(new SwipeGesture);
0460     QSignalSpy startedSpy(gesture.get(), &SwipeGesture::started);
0461     QSignalSpy cancelledSpy(gesture.get(), &SwipeGesture::cancelled);
0462 
0463     recognizer.registerSwipeGesture(gesture.get());
0464     recognizer.startSwipeGesture(1);
0465     QCOMPARE(startedSpy.count(), 1);
0466     QCOMPARE(cancelledSpy.count(), 0);
0467     gesture.reset();
0468     QCOMPARE(cancelledSpy.count(), 1);
0469 }
0470 
0471 void GestureTest::testSwipeCancel_data()
0472 {
0473     QTest::addColumn<KWin::SwipeGesture::Direction>("direction");
0474 
0475     QTest::newRow("Up") << KWin::SwipeGesture::Direction::Up;
0476     QTest::newRow("Left") << KWin::SwipeGesture::Direction::Left;
0477     QTest::newRow("Right") << KWin::SwipeGesture::Direction::Right;
0478     QTest::newRow("Down") << KWin::SwipeGesture::Direction::Down;
0479 }
0480 
0481 void GestureTest::testSwipeCancel()
0482 {
0483     GestureRecognizer recognizer;
0484     std::unique_ptr<SwipeGesture> gesture(new SwipeGesture);
0485     QFETCH(SwipeGesture::Direction, direction);
0486     gesture->setDirection(direction);
0487     QSignalSpy startedSpy(gesture.get(), &SwipeGesture::started);
0488     QSignalSpy cancelledSpy(gesture.get(), &SwipeGesture::cancelled);
0489     QSignalSpy triggeredSpy(gesture.get(), &SwipeGesture::triggered);
0490 
0491     recognizer.registerSwipeGesture(gesture.get());
0492     recognizer.startSwipeGesture(1);
0493     QCOMPARE(startedSpy.count(), 1);
0494     QCOMPARE(cancelledSpy.count(), 0);
0495     recognizer.cancelSwipeGesture();
0496     QCOMPARE(cancelledSpy.count(), 1);
0497     QCOMPARE(triggeredSpy.count(), 0);
0498 }
0499 
0500 void GestureTest::testSwipeUpdateTrigger_data()
0501 {
0502     QTest::addColumn<KWin::SwipeGesture::Direction>("direction");
0503     QTest::addColumn<QPointF>("delta");
0504 
0505     QTest::newRow("Up") << KWin::SwipeGesture::Direction::Up << QPointF(2, -3);
0506     QTest::newRow("Left") << KWin::SwipeGesture::Direction::Left << QPointF(-3, 1);
0507     QTest::newRow("Right") << KWin::SwipeGesture::Direction::Right << QPointF(20, -19);
0508     QTest::newRow("Down") << KWin::SwipeGesture::Direction::Down << QPointF(0, 50);
0509 }
0510 
0511 void GestureTest::testSwipeUpdateTrigger()
0512 {
0513     GestureRecognizer recognizer;
0514     SwipeGesture gesture;
0515     QFETCH(SwipeGesture::Direction, direction);
0516     gesture.setDirection(direction);
0517 
0518     QSignalSpy triggeredSpy(&gesture, &SwipeGesture::triggered);
0519     QSignalSpy cancelledSpy(&gesture, &SwipeGesture::cancelled);
0520 
0521     recognizer.registerSwipeGesture(&gesture);
0522 
0523     recognizer.startSwipeGesture(1);
0524     QFETCH(QPointF, delta);
0525     recognizer.updateSwipeGesture(delta);
0526     QCOMPARE(cancelledSpy.count(), 0);
0527     QCOMPARE(triggeredSpy.count(), 0);
0528 
0529     recognizer.endSwipeGesture();
0530     QCOMPARE(cancelledSpy.count(), 0);
0531     QCOMPARE(triggeredSpy.count(), 1);
0532 }
0533 
0534 void GestureTest::testSwipeMinFingerStart_data()
0535 {
0536     QTest::addColumn<uint>("min");
0537     QTest::addColumn<uint>("count");
0538     QTest::addColumn<bool>("started");
0539 
0540     QTest::newRow("same") << 1u << 1u << true;
0541     QTest::newRow("less") << 2u << 1u << false;
0542     QTest::newRow("more") << 1u << 2u << true;
0543 }
0544 
0545 void GestureTest::testSwipeMinFingerStart()
0546 {
0547     GestureRecognizer recognizer;
0548     SwipeGesture gesture;
0549     QFETCH(uint, min);
0550     gesture.setMinimumFingerCount(min);
0551 
0552     QSignalSpy startedSpy(&gesture, &SwipeGesture::started);
0553 
0554     recognizer.registerSwipeGesture(&gesture);
0555     QFETCH(uint, count);
0556     recognizer.startSwipeGesture(count);
0557     QTEST(!startedSpy.isEmpty(), "started");
0558 }
0559 
0560 void GestureTest::testSwipeMaxFingerStart_data()
0561 {
0562     QTest::addColumn<uint>("max");
0563     QTest::addColumn<uint>("count");
0564     QTest::addColumn<bool>("started");
0565 
0566     QTest::newRow("same") << 1u << 1u << true;
0567     QTest::newRow("less") << 2u << 1u << true;
0568     QTest::newRow("more") << 1u << 2u << false;
0569 }
0570 
0571 void GestureTest::testSwipeMaxFingerStart()
0572 {
0573     GestureRecognizer recognizer;
0574     SwipeGesture gesture;
0575     QFETCH(uint, max);
0576     gesture.setMaximumFingerCount(max);
0577 
0578     QSignalSpy startedSpy(&gesture, &SwipeGesture::started);
0579 
0580     recognizer.registerSwipeGesture(&gesture);
0581     QFETCH(uint, count);
0582     recognizer.startSwipeGesture(count);
0583     QTEST(!startedSpy.isEmpty(), "started");
0584 }
0585 
0586 void GestureTest::testNotEmitCallbacksBeforeDirectionDecided()
0587 {
0588     GestureRecognizer recognizer;
0589     SwipeGesture up;
0590     SwipeGesture down;
0591     SwipeGesture right;
0592     PinchGesture expand;
0593     PinchGesture contract;
0594     up.setDirection(SwipeGesture::Direction::Up);
0595     down.setDirection(SwipeGesture::Direction::Down);
0596     right.setDirection(SwipeGesture::Direction::Right);
0597     expand.setDirection(PinchGesture::Direction::Expanding);
0598     contract.setDirection(PinchGesture::Direction::Contracting);
0599     recognizer.registerSwipeGesture(&up);
0600     recognizer.registerSwipeGesture(&down);
0601     recognizer.registerSwipeGesture(&right);
0602     recognizer.registerPinchGesture(&expand);
0603     recognizer.registerPinchGesture(&contract);
0604 
0605     QSignalSpy upSpy(&up, &SwipeGesture::progress);
0606     QSignalSpy downSpy(&down, &SwipeGesture::progress);
0607     QSignalSpy rightSpy(&right, &SwipeGesture::progress);
0608     QSignalSpy expandSpy(&expand, &PinchGesture::progress);
0609     QSignalSpy contractSpy(&contract, &PinchGesture::progress);
0610 
0611     // don't release callback until we know the direction of swipe gesture
0612     recognizer.startSwipeGesture(4);
0613     QCOMPARE(upSpy.count(), 0);
0614     QCOMPARE(downSpy.count(), 0);
0615     QCOMPARE(rightSpy.count(), 0);
0616 
0617     // up (negative y)
0618     recognizer.updateSwipeGesture(QPointF(0, -1.5));
0619     QCOMPARE(upSpy.count(), 1);
0620     QCOMPARE(downSpy.count(), 0);
0621     QCOMPARE(rightSpy.count(), 0);
0622 
0623     // down (positive y)
0624     // recognizer.updateSwipeGesture(QPointF(0, 0));
0625     recognizer.updateSwipeGesture(QPointF(0, 3));
0626     QCOMPARE(upSpy.count(), 1);
0627     QCOMPARE(downSpy.count(), 1);
0628     QCOMPARE(rightSpy.count(), 0);
0629 
0630     // right
0631     recognizer.cancelSwipeGesture();
0632     recognizer.startSwipeGesture(4);
0633     recognizer.updateSwipeGesture(QPointF(1, 0));
0634     QCOMPARE(upSpy.count(), 1);
0635     QCOMPARE(downSpy.count(), 1);
0636     QCOMPARE(rightSpy.count(), 1);
0637 
0638     recognizer.cancelSwipeGesture();
0639 
0640     // same test for pinch gestures
0641     recognizer.startPinchGesture(4);
0642     QCOMPARE(expandSpy.count(), 0);
0643     QCOMPARE(contractSpy.count(), 0);
0644 
0645     // contracting
0646     recognizer.updatePinchGesture(.5, 0, QPointF(0, 0));
0647     QCOMPARE(expandSpy.count(), 0);
0648     QCOMPARE(contractSpy.count(), 1);
0649 
0650     // expanding
0651     recognizer.updatePinchGesture(1.5, 0, QPointF(0, 0));
0652     QCOMPARE(expandSpy.count(), 1);
0653     QCOMPARE(contractSpy.count(), 1);
0654 }
0655 
0656 void GestureTest::testSwipeGeometryStart_data()
0657 {
0658     QTest::addColumn<QRect>("geometry");
0659     QTest::addColumn<QPointF>("startPos");
0660     QTest::addColumn<bool>("started");
0661 
0662     QTest::newRow("top left") << QRect(0, 0, 10, 20) << QPointF(0, 0) << true;
0663     QTest::newRow("top right") << QRect(0, 0, 10, 20) << QPointF(10, 0) << true;
0664     QTest::newRow("bottom left") << QRect(0, 0, 10, 20) << QPointF(0, 20) << true;
0665     QTest::newRow("bottom right") << QRect(0, 0, 10, 20) << QPointF(10, 20) << true;
0666     QTest::newRow("x too small") << QRect(10, 20, 30, 40) << QPointF(9, 25) << false;
0667     QTest::newRow("y too small") << QRect(10, 20, 30, 40) << QPointF(25, 19) << false;
0668     QTest::newRow("x too large") << QRect(10, 20, 30, 40) << QPointF(41, 25) << false;
0669     QTest::newRow("y too large") << QRect(10, 20, 30, 40) << QPointF(25, 61) << false;
0670     QTest::newRow("inside") << QRect(10, 20, 30, 40) << QPointF(25, 25) << true;
0671 }
0672 
0673 void GestureTest::testSwipeGeometryStart()
0674 {
0675     GestureRecognizer recognizer;
0676     SwipeGesture gesture;
0677     QFETCH(QRect, geometry);
0678     gesture.setStartGeometry(geometry);
0679 
0680     QSignalSpy startedSpy(&gesture, &SwipeGesture::started);
0681 
0682     recognizer.registerSwipeGesture(&gesture);
0683     QFETCH(QPointF, startPos);
0684     recognizer.startSwipeGesture(startPos);
0685     QTEST(!startedSpy.isEmpty(), "started");
0686 }
0687 
0688 QTEST_MAIN(GestureTest)
0689 #include "test_gestures.moc"