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