| 1 | /* |
| 2 | This file is part of the KDE project |
| 3 | SPDX-FileCopyrightText: 2021 Steffen Hartleib <[email protected]> |
| 4 | |
| 5 | SPDX-License-Identifier: LGPL-2.1-or-later |
| 6 | */ |
| 7 | |
| 8 | #include <ktwofingertap.h> |
| 9 | |
| 10 | #include <QMainWindow> |
| 11 | #include <QSignalSpy> |
| 12 | #include <QTest> |
| 13 | #include <QWidget> |
| 14 | |
| 15 | class KTwoFingerSwipeTest : public QObject |
| 16 | { |
| 17 | Q_OBJECT |
| 18 | |
| 19 | QPointingDevice *mDev = nullptr; |
| 20 | |
| 21 | QMainWindow mMainWindow; |
| 22 | QWidget mWidget; |
| 23 | KTwoFingerTapRecognizer *mTwoFingerRec = nullptr; |
| 24 | Qt::GestureType mKTwoFingerTapGesture; |
| 25 | QPoint mTouchPointPos = QPoint(100, 100); |
| 26 | QSignalSpy *mSpyTapFinished = nullptr; |
| 27 | QSignalSpy *mSpyTapStarted = nullptr; |
| 28 | QSignalSpy *mSpyTapUpdated = nullptr; |
| 29 | QSignalSpy *mSpyTapCanceled = nullptr; |
| 30 | |
| 31 | Q_SIGNALS: |
| 32 | void tapStarted(KTwoFingerTap *); |
| 33 | void tapFinished(KTwoFingerTap *); |
| 34 | void tapUpdated(KTwoFingerTap *); |
| 35 | void tapCanceled(KTwoFingerTap *); |
| 36 | |
| 37 | protected: |
| 38 | bool eventFilter(QObject *watched, QEvent *e) override |
| 39 | { |
| 40 | if (e->type() == QEvent::Gesture) { |
| 41 | QGestureEvent *gEvent = static_cast<QGestureEvent *>(e); |
| 42 | KTwoFingerTap *tap = static_cast<KTwoFingerTap *>(gEvent->gesture(mKTwoFingerTapGesture)); |
| 43 | if (tap) { |
| 44 | const Qt::GestureState state = tap->state(); |
| 45 | if (state == Qt::GestureFinished) { |
| 46 | Q_EMIT tapFinished(tap); |
| 47 | } else if (state == Qt::GestureStarted) { |
| 48 | Q_EMIT tapStarted(tap); |
| 49 | } else if (state == Qt::GestureUpdated) { |
| 50 | Q_EMIT tapUpdated(tap); |
| 51 | } else if (state == Qt::GestureCanceled) { |
| 52 | Q_EMIT tapCanceled(tap); |
| 53 | } |
| 54 | } |
| 55 | e->accept(); |
| 56 | return true; |
| 57 | } |
| 58 | return QObject::eventFilter(watched, e); |
| 59 | } |
| 60 | |
| 61 | protected Q_SLOTS: |
| 62 | void slotTapFinished(KTwoFingerTap *tap) |
| 63 | { |
| 64 | compareGesturePositions(tap); |
| 65 | } |
| 66 | void slotTapStarted(KTwoFingerTap *tap) |
| 67 | { |
| 68 | compareGesturePositions(tap); |
| 69 | } |
| 70 | void slotTapUpdated(KTwoFingerTap *tap) |
| 71 | { |
| 72 | compareGesturePositions(tap); |
| 73 | } |
| 74 | void slotTapCanceled(KTwoFingerTap *tap) |
| 75 | { |
| 76 | compareGesturePositions(tap); |
| 77 | } |
| 78 | |
| 79 | private: |
| 80 | void compareGesturePositions(KTwoFingerTap *tap) |
| 81 | { |
| 82 | QCOMPARE(tap->pos(), mTouchPointPos); |
| 83 | QCOMPARE(tap->screenPos(), mWidget.mapToGlobal(mTouchPointPos)); |
| 84 | QCOMPARE(tap->hotSpot(), mWidget.mapToGlobal(mTouchPointPos)); |
| 85 | QCOMPARE(tap->scenePos(), mWidget.mapToGlobal(mTouchPointPos)); |
| 86 | QVERIFY(tap->hasHotSpot()); |
| 87 | } |
| 88 | |
| 89 | void clearSignalSpys() |
| 90 | { |
| 91 | mSpyTapFinished->clear(); |
| 92 | mSpyTapCanceled->clear(); |
| 93 | mSpyTapUpdated->clear(); |
| 94 | mSpyTapStarted->clear(); |
| 95 | } |
| 96 | |
| 97 | private Q_SLOTS: |
| 98 | void cleanupTestCase() |
| 99 | { |
| 100 | delete mSpyTapFinished; |
| 101 | delete mSpyTapCanceled; |
| 102 | delete mSpyTapUpdated; |
| 103 | delete mSpyTapStarted; |
| 104 | delete mDev; |
| 105 | } |
| 106 | |
| 107 | void initTestCase() |
| 108 | { |
| 109 | mDev = QTest::createTouchDevice(); |
| 110 | |
| 111 | mMainWindow.setGeometry(0, 0, 500, 500); |
| 112 | mMainWindow.setCentralWidget(&mWidget); |
| 113 | mMainWindow.show(); |
| 114 | |
| 115 | mWidget.installEventFilter(this); |
| 116 | mWidget.setAttribute(Qt::WA_AcceptTouchEvents); |
| 117 | |
| 118 | QVERIFY(QTest::qWaitForWindowActive(&mMainWindow)); |
| 119 | QVERIFY(QTest::qWaitForWindowActive(&mWidget)); |
| 120 | |
| 121 | mTwoFingerRec = new KTwoFingerTapRecognizer(); |
| 122 | QVERIFY(mTwoFingerRec); |
| 123 | |
| 124 | mKTwoFingerTapGesture = QGestureRecognizer::registerRecognizer(mTwoFingerRec); |
| 125 | QVERIFY(mKTwoFingerTapGesture & Qt::CustomGesture); |
| 126 | |
| 127 | mWidget.grabGesture(mKTwoFingerTapGesture); |
| 128 | |
| 129 | connect(this, &KTwoFingerSwipeTest::tapFinished, this, &KTwoFingerSwipeTest::slotTapFinished); |
| 130 | connect(this, &KTwoFingerSwipeTest::tapStarted, this, &KTwoFingerSwipeTest::slotTapStarted); |
| 131 | connect(this, &KTwoFingerSwipeTest::tapUpdated, this, &KTwoFingerSwipeTest::slotTapUpdated); |
| 132 | connect(this, &KTwoFingerSwipeTest::tapCanceled, this, &KTwoFingerSwipeTest::slotTapCanceled); |
| 133 | |
| 134 | mSpyTapFinished = new QSignalSpy(this, &KTwoFingerSwipeTest::tapFinished); |
| 135 | QVERIFY(mSpyTapFinished->isValid()); |
| 136 | |
| 137 | mSpyTapStarted = new QSignalSpy(this, &KTwoFingerSwipeTest::tapStarted); |
| 138 | QVERIFY(mSpyTapStarted->isValid()); |
| 139 | |
| 140 | mSpyTapUpdated = new QSignalSpy(this, &KTwoFingerSwipeTest::tapUpdated); |
| 141 | QVERIFY(mSpyTapUpdated->isValid()); |
| 142 | |
| 143 | mSpyTapCanceled = new QSignalSpy(this, &KTwoFingerSwipeTest::tapCanceled); |
| 144 | QVERIFY(mSpyTapCanceled->isValid()); |
| 145 | |
| 146 | QTest::qWait(1000); |
| 147 | } |
| 148 | |
| 149 | void testChangeTapRadius() |
| 150 | { |
| 151 | QVERIFY(mTwoFingerRec->tapRadius() >= 0); |
| 152 | |
| 153 | mTwoFingerRec->setTapRadius(10); |
| 154 | QCOMPARE(mTwoFingerRec->tapRadius(), 10); |
| 155 | |
| 156 | mTwoFingerRec->setTapRadius(-100); |
| 157 | QCOMPARE(mTwoFingerRec->tapRadius(), 0); |
| 158 | |
| 159 | mTwoFingerRec->setTapRadius(40); |
| 160 | QCOMPARE(mTwoFingerRec->tapRadius(), 40); |
| 161 | } |
| 162 | |
| 163 | void testSuccessfulGesture() |
| 164 | { |
| 165 | // Test a successful gesture with two fingers, where the touch points wiggle only a little bit. |
| 166 | |
| 167 | clearSignalSpys(); |
| 168 | |
| 169 | int tapRadius = mTwoFingerRec->tapRadius(); |
| 170 | QPoint wiggleRoom = QPoint(tapRadius / 2, tapRadius / 2); |
| 171 | |
| 172 | QTest::touchEvent(&mWidget, mDev).press(0, mTouchPointPos, (QWidget *)nullptr); |
| 173 | |
| 174 | QTest::touchEvent(&mWidget, mDev).move(0, mTouchPointPos + wiggleRoom, (QWidget *)nullptr).press(1, mTouchPointPos, (QWidget *)nullptr); |
| 175 | |
| 176 | QTest::touchEvent(&mWidget, mDev).move(0, mTouchPointPos, (QWidget *)nullptr).move(1, mTouchPointPos + wiggleRoom, (QWidget *)nullptr); |
| 177 | |
| 178 | QTest::touchEvent(&mWidget, mDev).move(0, mTouchPointPos, (QWidget *)nullptr).release(1, mTouchPointPos, (QWidget *)nullptr); |
| 179 | |
| 180 | QTest::touchEvent(&mWidget, mDev).release(0, mTouchPointPos, (QWidget *)nullptr); |
| 181 | |
| 182 | if (mSpyTapStarted->count() == 0) { |
| 183 | QVERIFY(mSpyTapStarted->wait(1000)); |
| 184 | } |
| 185 | if (mSpyTapFinished->count() == 0) { |
| 186 | QVERIFY(mSpyTapFinished->wait(1000)); |
| 187 | } |
| 188 | |
| 189 | QCOMPARE(mSpyTapStarted->count(), 1); |
| 190 | QCOMPARE(mSpyTapFinished->count(), 1); |
| 191 | QCOMPARE(mSpyTapCanceled->count(), 0); |
| 192 | QVERIFY(mSpyTapUpdated->count() > 0); |
| 193 | } |
| 194 | |
| 195 | void testFailingGesture() |
| 196 | { |
| 197 | // Test a failing gesture with two fingers, where the touch points wiggle too much at beginning. |
| 198 | |
| 199 | clearSignalSpys(); |
| 200 | |
| 201 | int tapRadius = mTwoFingerRec->tapRadius(); |
| 202 | QPoint wiggleRoom = QPoint(tapRadius, tapRadius); |
| 203 | |
| 204 | QTest::touchEvent(&mWidget, mDev).press(0, mTouchPointPos, (QWidget *)nullptr); |
| 205 | |
| 206 | QTest::touchEvent(&mWidget, mDev).move(0, mTouchPointPos + wiggleRoom, (QWidget *)nullptr).press(1, mTouchPointPos, (QWidget *)nullptr); |
| 207 | |
| 208 | QTest::touchEvent(&mWidget, mDev).move(0, mTouchPointPos, (QWidget *)nullptr).move(1, mTouchPointPos + wiggleRoom, (QWidget *)nullptr); |
| 209 | |
| 210 | QTest::touchEvent(&mWidget, mDev).move(0, mTouchPointPos, (QWidget *)nullptr).release(1, mTouchPointPos, (QWidget *)nullptr); |
| 211 | |
| 212 | QTest::touchEvent(&mWidget, mDev).release(0, mTouchPointPos, (QWidget *)nullptr); |
| 213 | |
| 214 | QCOMPARE(mSpyTapStarted->count(), 0); |
| 215 | QCOMPARE(mSpyTapFinished->count(), 0); |
| 216 | QCOMPARE(mSpyTapCanceled->count(), 0); |
| 217 | QCOMPARE(mSpyTapUpdated->count(), 0); |
| 218 | } |
| 219 | |
| 220 | void testFailingGesture2() |
| 221 | { |
| 222 | // Test a failing gesture with two fingers, where the touch points wiggle too much in the middle. |
| 223 | clearSignalSpys(); |
| 224 | int tapRadius = mTwoFingerRec->tapRadius(); |
| 225 | QPoint wiggleRoom = QPoint(tapRadius, tapRadius); |
| 226 | |
| 227 | QTest::touchEvent(&mWidget, mDev).press(0, mTouchPointPos, (QWidget *)nullptr); |
| 228 | |
| 229 | QTest::touchEvent(&mWidget, mDev).move(0, mTouchPointPos, (QWidget *)nullptr).press(1, mTouchPointPos, (QWidget *)nullptr); |
| 230 | |
| 231 | QTest::touchEvent(&mWidget, mDev).move(0, mTouchPointPos, (QWidget *)nullptr).move(1, mTouchPointPos + wiggleRoom, (QWidget *)nullptr); |
| 232 | |
| 233 | QTest::touchEvent(&mWidget, mDev).move(0, mTouchPointPos, (QWidget *)nullptr).release(1, mTouchPointPos, (QWidget *)nullptr); |
| 234 | |
| 235 | QTest::touchEvent(&mWidget, mDev).release(0, mTouchPointPos, (QWidget *)nullptr); |
| 236 | |
| 237 | if (mSpyTapStarted->count() == 0) { |
| 238 | QVERIFY(mSpyTapStarted->wait(1000)); |
| 239 | } |
| 240 | QCOMPARE(mSpyTapStarted->count(), 1); |
| 241 | QCOMPARE(mSpyTapFinished->count(), 0); |
| 242 | QCOMPARE(mSpyTapCanceled->count(), 1); |
| 243 | QVERIFY(mSpyTapUpdated->count() >= 0); |
| 244 | } |
| 245 | |
| 246 | void testFailingGesture3() |
| 247 | { |
| 248 | // Test a failing gesture with two fingers, where the touch points wiggle too much in the middle, |
| 249 | // and the second finger press and release successively. |
| 250 | |
| 251 | clearSignalSpys(); |
| 252 | int tapRadius = mTwoFingerRec->tapRadius(); |
| 253 | QPoint wiggleRoom = QPoint(tapRadius, tapRadius); |
| 254 | |
| 255 | QTest::touchEvent(&mWidget, mDev).press(0, mTouchPointPos, (QWidget *)nullptr); |
| 256 | |
| 257 | QTest::touchEvent(&mWidget, mDev).move(0, mTouchPointPos, (QWidget *)nullptr).press(1, mTouchPointPos, (QWidget *)nullptr); |
| 258 | |
| 259 | QTest::touchEvent(&mWidget, mDev).move(0, mTouchPointPos, (QWidget *)nullptr).move(1, mTouchPointPos + wiggleRoom, (QWidget *)nullptr); |
| 260 | |
| 261 | QTest::touchEvent(&mWidget, mDev).move(0, mTouchPointPos, (QWidget *)nullptr).release(1, mTouchPointPos, (QWidget *)nullptr); |
| 262 | |
| 263 | QTest::touchEvent(&mWidget, mDev).move(0, mTouchPointPos, (QWidget *)nullptr).press(1, mTouchPointPos, (QWidget *)nullptr); |
| 264 | |
| 265 | QTest::touchEvent(&mWidget, mDev).move(0, mTouchPointPos, (QWidget *)nullptr).release(1, mTouchPointPos, (QWidget *)nullptr); |
| 266 | |
| 267 | QTest::touchEvent(&mWidget, mDev).release(0, mTouchPointPos, (QWidget *)nullptr); |
| 268 | |
| 269 | if (mSpyTapStarted->count() == 0) { |
| 270 | QVERIFY(mSpyTapStarted->wait(1000)); |
| 271 | } |
| 272 | QCOMPARE(mSpyTapStarted->count(), 1); |
| 273 | QCOMPARE(mSpyTapFinished->count(), 0); |
| 274 | QCOMPARE(mSpyTapCanceled->count(), 1); |
| 275 | QVERIFY(mSpyTapUpdated->count() >= 0); |
| 276 | } |
| 277 | |
| 278 | void testFailingGesture_threeFingers() |
| 279 | { |
| 280 | // the test a gesture with three fingers should be end with gesture canceled. |
| 281 | clearSignalSpys(); |
| 282 | int tapRadius = mTwoFingerRec->tapRadius(); |
| 283 | QPoint wiggleRoom = QPoint(tapRadius / 2, tapRadius / 2); |
| 284 | |
| 285 | QTest::touchEvent(&mWidget, mDev).press(0, mTouchPointPos, (QWidget *)nullptr); |
| 286 | |
| 287 | QTest::touchEvent(&mWidget, mDev).move(0, mTouchPointPos, (QWidget *)nullptr).press(1, mTouchPointPos, (QWidget *)nullptr); |
| 288 | |
| 289 | QTest::touchEvent(&mWidget, mDev) |
| 290 | .move(0, mTouchPointPos, (QWidget *)nullptr) |
| 291 | .move(1, mTouchPointPos + wiggleRoom, (QWidget *)nullptr) |
| 292 | .press(2, mTouchPointPos, (QWidget *)nullptr); |
| 293 | |
| 294 | QTest::touchEvent(&mWidget, mDev).move(0, mTouchPointPos, (QWidget *)nullptr).release(1, mTouchPointPos, (QWidget *)nullptr); |
| 295 | |
| 296 | QTest::touchEvent(&mWidget, mDev).release(0, mTouchPointPos, (QWidget *)nullptr); |
| 297 | |
| 298 | if (mSpyTapStarted->count() == 0) { |
| 299 | QVERIFY(mSpyTapStarted->wait(1000)); |
| 300 | } |
| 301 | QCOMPARE(mSpyTapStarted->count(), 1); |
| 302 | QCOMPARE(mSpyTapFinished->count(), 0); |
| 303 | QCOMPARE(mSpyTapCanceled->count(), 1); |
| 304 | QVERIFY(mSpyTapUpdated->count() >= 0); |
| 305 | } |
| 306 | |
| 307 | void testFailingGesture_threeFingers2() |
| 308 | { |
| 309 | clearSignalSpys(); |
| 310 | |
| 311 | QTest::touchEvent(&mWidget, mDev).press(0, mTouchPointPos, (QWidget *)nullptr); |
| 312 | |
| 313 | QTest::touchEvent(&mWidget, mDev).move(0, mTouchPointPos, (QWidget *)nullptr).press(1, mTouchPointPos, (QWidget *)nullptr); |
| 314 | |
| 315 | QTest::touchEvent(&mWidget, mDev) |
| 316 | .move(0, mTouchPointPos, (QWidget *)nullptr) |
| 317 | .move(1, mTouchPointPos, (QWidget *)nullptr) |
| 318 | .press(2, mTouchPointPos, (QWidget *)nullptr); |
| 319 | |
| 320 | QTest::touchEvent(&mWidget, mDev) |
| 321 | .move(0, mTouchPointPos, (QWidget *)nullptr) |
| 322 | .move(1, mTouchPointPos, (QWidget *)nullptr) |
| 323 | .release(3, mTouchPointPos, (QWidget *)nullptr); |
| 324 | |
| 325 | QTest::touchEvent(&mWidget, mDev).move(0, mTouchPointPos, (QWidget *)nullptr).release(1, mTouchPointPos, (QWidget *)nullptr); |
| 326 | |
| 327 | QTest::touchEvent(&mWidget, mDev).move(0, mTouchPointPos, (QWidget *)nullptr).press(1, mTouchPointPos, (QWidget *)nullptr); |
| 328 | |
| 329 | QTest::touchEvent(&mWidget, mDev).move(0, mTouchPointPos, (QWidget *)nullptr).release(1, mTouchPointPos, (QWidget *)nullptr); |
| 330 | |
| 331 | QTest::touchEvent(&mWidget, mDev).release(1, mTouchPointPos, (QWidget *)nullptr); |
| 332 | |
| 333 | if (mSpyTapStarted->count() == 0) { |
| 334 | QVERIFY(mSpyTapStarted->wait(1000)); |
| 335 | } |
| 336 | QCOMPARE(mSpyTapStarted->count(), 1); |
| 337 | QCOMPARE(mSpyTapFinished->count(), 0); |
| 338 | QCOMPARE(mSpyTapCanceled->count(), 1); |
| 339 | QVERIFY(mSpyTapUpdated->count() >= 0); |
| 340 | } |
| 341 | |
| 342 | void testFailingGesture_oneFinger() |
| 343 | { |
| 344 | // Test a failing gesture where we use only one finger. |
| 345 | clearSignalSpys(); |
| 346 | int tapRadius = mTwoFingerRec->tapRadius(); |
| 347 | QPoint wiggleRoom = QPoint(tapRadius / 2, tapRadius / 2); |
| 348 | |
| 349 | QTest::touchEvent(&mWidget, mDev).press(0, mTouchPointPos, (QWidget *)nullptr); |
| 350 | |
| 351 | QTest::touchEvent(&mWidget, mDev).move(0, mTouchPointPos + wiggleRoom, (QWidget *)nullptr); |
| 352 | |
| 353 | QTest::touchEvent(&mWidget, mDev).move(0, mTouchPointPos, (QWidget *)nullptr); |
| 354 | |
| 355 | QTest::touchEvent(&mWidget, mDev).release(0, mTouchPointPos, (QWidget *)nullptr); |
| 356 | |
| 357 | QCOMPARE(mSpyTapStarted->count(), 0); |
| 358 | QCOMPARE(mSpyTapFinished->count(), 0); |
| 359 | QCOMPARE(mSpyTapCanceled->count(), 0); |
| 360 | QCOMPARE(mSpyTapUpdated->count(), 0); |
| 361 | } |
| 362 | void testSomeRandomTaps() |
| 363 | { |
| 364 | for (int i = 0; i < 50; ++i) { |
| 365 | int v1 = rand() % 450; |
| 366 | int v2 = rand() % 450; |
| 367 | mTouchPointPos = QPoint(v1, v2); |
| 368 | testSuccessfulGesture(); |
| 369 | } |
| 370 | } |
| 371 | }; |
| 372 | |
| 373 | QTEST_MAIN(KTwoFingerSwipeTest) |
| 374 | |
| 375 | #include "ktwofingertaptest.moc" |