-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGestureRecognizerTests.swift
More file actions
1698 lines (1357 loc) · 65 KB
/
GestureRecognizerTests.swift
File metadata and controls
1698 lines (1357 loc) · 65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import CoreGraphics
import XCTest
@testable import MiddleDragCore
final class GestureRecognizerTests: XCTestCase {
var recognizer: GestureRecognizer!
var mockDelegate: MockGestureRecognizerDelegate!
override func setUp() {
super.setUp()
recognizer = GestureRecognizer()
mockDelegate = MockGestureRecognizerDelegate()
recognizer.delegate = mockDelegate
}
override func tearDown() {
recognizer = nil
mockDelegate = nil
super.tearDown()
}
// MARK: - Helper Methods
/// Creates a valid MTTouch struct with the given position and properties
private func createTouch(
x: Float, y: Float, zTotal: Float = 0.5, state: UInt32 = 4
) -> MTTouch {
let position = MTPoint(x: x, y: y)
let velocity = MTPoint(x: 0, y: 0)
let normalizedVector = MTVector(position: position, velocity: velocity)
let absoluteVector = MTVector(position: position, velocity: velocity)
return MTTouch(
frame: 0,
timestamp: 0,
pathIndex: 0,
state: state,
fingerID: 0,
handID: 0,
normalizedVector: normalizedVector,
zTotal: zTotal,
field9: 0,
angle: 0,
majorAxis: 0.1,
minorAxis: 0.1,
absoluteVector: absoluteVector,
field14: 0,
field15: 0,
zDensity: 0
)
}
/// Creates touch data in memory and returns pointer + cleanup closure
private func createTouchData(touches: [MTTouch]) -> (UnsafeMutableRawPointer, Int, () -> Void) {
let count = touches.count
guard count > 0 else {
// Non-null placeholder; processTouches won't dereference when count == 0.
return unsafe (UnsafeMutableRawPointer(bitPattern: 1)!, 0, {})
}
let pointer = UnsafeMutablePointer<MTTouch>.allocate(capacity: count)
for (index, touch) in touches.enumerated() {
unsafe pointer[index] = touch
}
let rawPointer = UnsafeMutableRawPointer(pointer)
let cleanup = { unsafe pointer.deallocate() }
return unsafe (rawPointer, count, cleanup)
}
// MARK: - Modifier Key Requirement Tests
func testModifierKeyRequirement_CancelsGestureWhenModifierNotHeld() {
// Configure to require shift key
recognizer.configuration.requireModifierKey = true
recognizer.configuration.modifierKeyType = .shift
// Create 3 valid touches
let touches = [
createTouch(x: 0.3, y: 0.5),
createTouch(x: 0.5, y: 0.5),
createTouch(x: 0.7, y: 0.5),
]
let (pointer, count, cleanup) = unsafe createTouchData(touches: touches)
defer { cleanup() }
// Process with shift held - gesture should start
unsafe recognizer.processTouches(pointer, count: count, timestamp: 0.0, modifierFlags: .maskShift)
XCTAssertTrue(mockDelegate.didStartCalled, "Gesture should start when modifier is held")
// Reset delegate tracking
mockDelegate.reset()
// Now process without shift held - should cancel
unsafe recognizer.processTouches(pointer, count: count, timestamp: 0.1, modifierFlags: [])
XCTAssertTrue(
mockDelegate.didCancelCalled || mockDelegate.didCancelDraggingCalled,
"Gesture should be cancelled when modifier is released")
}
func testModifierKeyRequirement_AllowsGestureWhenModifierHeld() {
recognizer.configuration.requireModifierKey = true
recognizer.configuration.modifierKeyType = .option
let touches = [
createTouch(x: 0.3, y: 0.5),
createTouch(x: 0.5, y: 0.5),
createTouch(x: 0.7, y: 0.5),
]
let (pointer, count, cleanup) = unsafe createTouchData(touches: touches)
defer { cleanup() }
// Process with option key held (maskAlternate)
unsafe recognizer.processTouches(
pointer, count: count, timestamp: 0.0, modifierFlags: .maskAlternate)
XCTAssertTrue(
mockDelegate.didStartCalled, "Gesture should start when correct modifier is held")
}
func testModifierKeyRequirement_AllModifierTypes() {
let modifierTests: [(ModifierKeyType, CGEventFlags)] = [
(.shift, .maskShift),
(.control, .maskControl),
(.option, .maskAlternate),
(.command, .maskCommand),
]
for (modifierType, eventFlag) in modifierTests {
// Reset for each test
recognizer.reset()
mockDelegate.reset()
recognizer.configuration.requireModifierKey = true
recognizer.configuration.modifierKeyType = modifierType
let touches = [
createTouch(x: 0.3, y: 0.5),
createTouch(x: 0.5, y: 0.5),
createTouch(x: 0.7, y: 0.5),
]
let (pointer, count, cleanup) = unsafe createTouchData(touches: touches)
defer { cleanup() }
unsafe recognizer.processTouches(
pointer, count: count, timestamp: 0.0, modifierFlags: eventFlag)
XCTAssertTrue(
mockDelegate.didStartCalled,
"Gesture should start with \(modifierType) modifier held")
}
}
func testModifierKeyRequirement_DisabledAllowsAllGestures() {
recognizer.configuration.requireModifierKey = false // Disabled
let touches = [
createTouch(x: 0.3, y: 0.5),
createTouch(x: 0.5, y: 0.5),
createTouch(x: 0.7, y: 0.5),
]
let (pointer, count, cleanup) = unsafe createTouchData(touches: touches)
defer { cleanup() }
// Process with no modifiers
unsafe recognizer.processTouches(pointer, count: count, timestamp: 0.0, modifierFlags: [])
XCTAssertTrue(
mockDelegate.didStartCalled,
"Gesture should start without modifier when requirement is disabled")
}
// MARK: - Exclusion Zone Tests
func testExclusionZone_FiltersTouchesInExclusionZone() {
recognizer.configuration.exclusionZoneEnabled = true
recognizer.configuration.exclusionZoneSize = 0.2 // Bottom 20%
// All 3 touches are in the exclusion zone (y < 0.2)
let touches = [
createTouch(x: 0.3, y: 0.1), // y=0.1 < 0.2, filtered
createTouch(x: 0.5, y: 0.15), // y=0.15 < 0.2, filtered
createTouch(x: 0.7, y: 0.05), // y=0.05 < 0.2, filtered
]
let (pointer, count, cleanup) = unsafe createTouchData(touches: touches)
defer { cleanup() }
unsafe recognizer.processTouches(pointer, count: count, timestamp: 0.0, modifierFlags: [])
// All touches filtered = no valid 3-finger gesture
XCTAssertFalse(
mockDelegate.didStartCalled, "Gesture should not start when all touches are filtered")
}
func testExclusionZone_AcceptsTouchesAboveZone() {
recognizer.configuration.exclusionZoneEnabled = true
recognizer.configuration.exclusionZoneSize = 0.2
// All 3 touches are above the exclusion zone (y >= 0.2)
let touches = [
createTouch(x: 0.3, y: 0.5),
createTouch(x: 0.5, y: 0.6),
createTouch(x: 0.7, y: 0.7),
]
let (pointer, count, cleanup) = unsafe createTouchData(touches: touches)
defer { cleanup() }
unsafe recognizer.processTouches(pointer, count: count, timestamp: 0.0, modifierFlags: [])
XCTAssertTrue(
mockDelegate.didStartCalled,
"Gesture should start when touches are above exclusion zone"
)
}
func testExclusionZone_DisabledAcceptsAllTouches() {
recognizer.configuration.exclusionZoneEnabled = false // Disabled
// Touches in what would be the exclusion zone
let touches = [
createTouch(x: 0.3, y: 0.1),
createTouch(x: 0.5, y: 0.1),
createTouch(x: 0.7, y: 0.1),
]
let (pointer, count, cleanup) = unsafe createTouchData(touches: touches)
defer { cleanup() }
unsafe recognizer.processTouches(pointer, count: count, timestamp: 0.0, modifierFlags: [])
XCTAssertTrue(
mockDelegate.didStartCalled,
"Gesture should start with low Y touches when exclusion zone is disabled")
}
// MARK: - Contact Size Filter Tests
func testContactSizeFilter_FiltersLargeContacts() {
recognizer.configuration.contactSizeFilterEnabled = true
recognizer.configuration.maxContactSize = 1.0
// All contacts are too large (zTotal > 1.0)
let touches = [
createTouch(x: 0.3, y: 0.5, zTotal: 2.0), // Too large
createTouch(x: 0.5, y: 0.5, zTotal: 1.5), // Too large
createTouch(x: 0.7, y: 0.5, zTotal: 3.0), // Too large
]
let (pointer, count, cleanup) = unsafe createTouchData(touches: touches)
defer { cleanup() }
unsafe recognizer.processTouches(pointer, count: count, timestamp: 0.0, modifierFlags: [])
XCTAssertFalse(
mockDelegate.didStartCalled,
"Gesture should not start when all contacts are too large (palm rejection)")
}
func testContactSizeFilter_AcceptsNormalContacts() {
recognizer.configuration.contactSizeFilterEnabled = true
recognizer.configuration.maxContactSize = 1.5
// All contacts are within acceptable range
let touches = [
createTouch(x: 0.3, y: 0.5, zTotal: 0.5),
createTouch(x: 0.5, y: 0.5, zTotal: 0.8),
createTouch(x: 0.7, y: 0.5, zTotal: 1.0),
]
let (pointer, count, cleanup) = unsafe createTouchData(touches: touches)
defer { cleanup() }
unsafe recognizer.processTouches(pointer, count: count, timestamp: 0.0, modifierFlags: [])
XCTAssertTrue(
mockDelegate.didStartCalled,
"Gesture should start when all contacts are within size limit")
}
func testContactSizeFilter_DisabledAcceptsAllContacts() {
recognizer.configuration.contactSizeFilterEnabled = false // Disabled
// Very large contacts that would normally be filtered
let touches = [
createTouch(x: 0.3, y: 0.5, zTotal: 10.0),
createTouch(x: 0.5, y: 0.5, zTotal: 15.0),
createTouch(x: 0.7, y: 0.5, zTotal: 20.0),
]
let (pointer, count, cleanup) = unsafe createTouchData(touches: touches)
defer { cleanup() }
unsafe recognizer.processTouches(pointer, count: count, timestamp: 0.0, modifierFlags: [])
XCTAssertTrue(
mockDelegate.didStartCalled,
"Gesture should start with large contacts when filter is disabled")
}
// MARK: - Partial Filter Tests (Some touches filtered, some pass)
func testExclusionZone_PartialFiltering_FiveTouchesThreePass() {
recognizer.configuration.exclusionZoneEnabled = true
recognizer.configuration.exclusionZoneSize = 0.2 // Bottom 20%
// 5 touches: 2 in exclusion zone, 3 above - should activate gesture
let touches = [
createTouch(x: 0.2, y: 0.1), // Filtered (y < 0.2)
createTouch(x: 0.3, y: 0.5), // Passes
createTouch(x: 0.5, y: 0.6), // Passes
createTouch(x: 0.7, y: 0.5), // Passes
createTouch(x: 0.8, y: 0.05), // Filtered (y < 0.2)
]
let (pointer, count, cleanup) = unsafe createTouchData(touches: touches)
defer { cleanup() }
unsafe recognizer.processTouches(pointer, count: count, timestamp: 0.0, modifierFlags: [])
XCTAssertTrue(
mockDelegate.didStartCalled,
"Gesture should start when 5 touches detected but only 3 pass exclusion zone filter")
}
func testContactSizeFilter_PartialFiltering_FiveTouchesThreePass() {
recognizer.configuration.contactSizeFilterEnabled = true
recognizer.configuration.maxContactSize = 1.5
// 5 touches: 2 too large (palm), 3 normal - should activate gesture
let touches = [
createTouch(x: 0.2, y: 0.5, zTotal: 3.0), // Filtered (too large)
createTouch(x: 0.3, y: 0.5, zTotal: 0.5), // Passes
createTouch(x: 0.5, y: 0.5, zTotal: 0.8), // Passes
createTouch(x: 0.7, y: 0.5, zTotal: 1.0), // Passes
createTouch(x: 0.8, y: 0.5, zTotal: 5.0), // Filtered (too large)
]
let (pointer, count, cleanup) = unsafe createTouchData(touches: touches)
defer { cleanup() }
unsafe recognizer.processTouches(pointer, count: count, timestamp: 0.0, modifierFlags: [])
XCTAssertTrue(
mockDelegate.didStartCalled,
"Gesture should start when 5 touches detected but only 3 pass contact size filter")
}
func testCombinedFilters_PartialFiltering_SixTouchesThreePass() {
recognizer.configuration.exclusionZoneEnabled = true
recognizer.configuration.exclusionZoneSize = 0.2
recognizer.configuration.contactSizeFilterEnabled = true
recognizer.configuration.maxContactSize = 1.5
// 6 touches: 2 in exclusion zone, 1 too large, 3 pass both - should activate
let touches = [
createTouch(x: 0.1, y: 0.1, zTotal: 0.5), // Filtered (exclusion zone)
createTouch(x: 0.2, y: 0.5, zTotal: 3.0), // Filtered (too large)
createTouch(x: 0.3, y: 0.5, zTotal: 0.5), // Passes both
createTouch(x: 0.5, y: 0.6, zTotal: 0.8), // Passes both
createTouch(x: 0.7, y: 0.5, zTotal: 1.0), // Passes both
createTouch(x: 0.9, y: 0.05, zTotal: 0.5), // Filtered (exclusion zone)
]
let (pointer, count, cleanup) = unsafe createTouchData(touches: touches)
defer { cleanup() }
unsafe recognizer.processTouches(pointer, count: count, timestamp: 0.0, modifierFlags: [])
XCTAssertTrue(
mockDelegate.didStartCalled,
"Gesture should start when 6 touches detected but only 3 pass all filters")
}
func testPartialFiltering_InsufficientRemainingTouches() {
recognizer.configuration.exclusionZoneEnabled = true
recognizer.configuration.exclusionZoneSize = 0.2
// 4 touches: 2 filtered, only 2 remain - should NOT activate (need exactly 3)
let touches = [
createTouch(x: 0.2, y: 0.1), // Filtered (exclusion zone)
createTouch(x: 0.3, y: 0.5), // Passes
createTouch(x: 0.7, y: 0.5), // Passes
createTouch(x: 0.8, y: 0.05), // Filtered (exclusion zone)
]
let (pointer, count, cleanup) = unsafe createTouchData(touches: touches)
defer { cleanup() }
unsafe recognizer.processTouches(pointer, count: count, timestamp: 0.0, modifierFlags: [])
XCTAssertFalse(
mockDelegate.didStartCalled,
"Gesture should not start when only 2 touches remain after filtering")
}
// MARK: - Combined Filter Tests
func testCombinedFilters_AllFiltersWorking() {
recognizer.configuration.exclusionZoneEnabled = true
recognizer.configuration.exclusionZoneSize = 0.2
recognizer.configuration.contactSizeFilterEnabled = true
recognizer.configuration.maxContactSize = 1.5
recognizer.configuration.requireModifierKey = true
recognizer.configuration.modifierKeyType = .shift
// Valid touches: above exclusion zone, normal size
let touches = [
createTouch(x: 0.3, y: 0.5, zTotal: 0.5),
createTouch(x: 0.5, y: 0.5, zTotal: 0.8),
createTouch(x: 0.7, y: 0.5, zTotal: 1.0),
]
let (pointer, count, cleanup) = unsafe createTouchData(touches: touches)
defer { cleanup() }
// Process with shift held
unsafe recognizer.processTouches(pointer, count: count, timestamp: 0.0, modifierFlags: .maskShift)
XCTAssertTrue(
mockDelegate.didStartCalled, "Gesture should start when all filters pass")
}
// MARK: - State Transition Tests
func testInitialStateIsIdle() {
XCTAssertEqual(recognizer.state, .idle, "Initial state should be idle")
}
func testStateTransitionToTap() {
let touches = [
createTouch(x: 0.3, y: 0.5),
createTouch(x: 0.5, y: 0.5),
createTouch(x: 0.7, y: 0.5),
]
let (pointer, count, cleanup) = unsafe createTouchData(touches: touches)
defer { cleanup() }
// Start gesture
unsafe recognizer.processTouches(pointer, count: count, timestamp: 0.0, modifierFlags: [])
XCTAssertEqual(
recognizer.state, .possibleTap, "State should be possibleTap after initial touch")
}
func testStateTransitionToDragging() {
// Set low move threshold for easier testing
recognizer.configuration.moveThreshold = 0.01
let touches1 = [
createTouch(x: 0.3, y: 0.5),
createTouch(x: 0.5, y: 0.5),
createTouch(x: 0.7, y: 0.5),
]
let (pointer1, count1, cleanup1) = unsafe createTouchData(touches: touches1)
defer { cleanup1() }
// Start gesture
unsafe recognizer.processTouches(pointer1, count: count1, timestamp: 0.0, modifierFlags: [])
XCTAssertEqual(recognizer.state, .possibleTap)
// Move fingers (small delta, well within 0.15 jump threshold)
let touches2 = [
createTouch(x: 0.32, y: 0.52),
createTouch(x: 0.52, y: 0.52),
createTouch(x: 0.72, y: 0.52),
]
let (pointer2, count2, cleanup2) = unsafe createTouchData(touches: touches2)
defer { cleanup2() }
unsafe recognizer.processTouches(pointer2, count: count2, timestamp: 0.1, modifierFlags: [])
XCTAssertEqual(
recognizer.state, .dragging, "State should transition to dragging after movement")
}
func testStateTransitionFromDragToIdle() {
recognizer.configuration.moveThreshold = 0.01
// Start with 3 fingers
let touches = [
createTouch(x: 0.3, y: 0.5),
createTouch(x: 0.5, y: 0.5),
createTouch(x: 0.7, y: 0.5),
]
let (pointer, count, cleanup) = unsafe createTouchData(touches: touches)
defer { cleanup() }
unsafe recognizer.processTouches(pointer, count: count, timestamp: 0.0, modifierFlags: [])
// Move to trigger drag (small delta, well within 0.15 jump threshold)
let touches2 = [
createTouch(x: 0.32, y: 0.52),
createTouch(x: 0.52, y: 0.52),
createTouch(x: 0.72, y: 0.52),
]
let (pointer2, count2, cleanup2) = unsafe createTouchData(touches: touches2)
defer { cleanup2() }
unsafe recognizer.processTouches(pointer2, count: count2, timestamp: 0.1, modifierFlags: [])
XCTAssertEqual(recognizer.state, .dragging)
// Lift all fingers (empty touch array) - need 2 frames for stable state
let emptyTouches: [MTTouch] = []
let (emptyPointer, _, emptyCleanup) = unsafe createTouchData(touches: emptyTouches)
defer { emptyCleanup() }
unsafe recognizer.processTouches(emptyPointer, count: 0, timestamp: 0.2, modifierFlags: [])
unsafe recognizer.processTouches(emptyPointer, count: 0, timestamp: 0.3, modifierFlags: [])
XCTAssertEqual(recognizer.state, .idle, "State should return to idle after lifting fingers")
}
// MARK: - Tap Gesture Tests
func testTapDetectedWithQuickRelease() {
recognizer.configuration.tapThreshold = 0.3 // 300ms
let touches = [
createTouch(x: 0.3, y: 0.5),
createTouch(x: 0.5, y: 0.5),
createTouch(x: 0.7, y: 0.5),
]
let (pointer, count, cleanup) = unsafe createTouchData(touches: touches)
defer { cleanup() }
// Start gesture
unsafe recognizer.processTouches(pointer, count: count, timestamp: 0.0, modifierFlags: [])
// Lift fingers quickly (before tap threshold)
let emptyTouches: [MTTouch] = []
let (emptyPointer, _, emptyCleanup) = unsafe createTouchData(touches: emptyTouches)
defer { emptyCleanup() }
// Two frames to trigger stable state
unsafe recognizer.processTouches(emptyPointer, count: 0, timestamp: 0.1, modifierFlags: [])
unsafe recognizer.processTouches(emptyPointer, count: 0, timestamp: 0.15, modifierFlags: [])
XCTAssertTrue(mockDelegate.didTapCalled, "Tap should be detected for quick release")
}
func testTapNotDetectedWhenHeldTooLong() {
recognizer.configuration.tapThreshold = 0.2 // 200ms
let touches = [
createTouch(x: 0.3, y: 0.5),
createTouch(x: 0.5, y: 0.5),
createTouch(x: 0.7, y: 0.5),
]
let (pointer, count, cleanup) = unsafe createTouchData(touches: touches)
defer { cleanup() }
// Start gesture
unsafe recognizer.processTouches(pointer, count: count, timestamp: 0.0, modifierFlags: [])
// Lift fingers after tap threshold exceeded
let emptyTouches: [MTTouch] = []
let (emptyPointer, _, emptyCleanup) = unsafe createTouchData(touches: emptyTouches)
defer { emptyCleanup() }
unsafe recognizer.processTouches(emptyPointer, count: 0, timestamp: 0.5, modifierFlags: [])
unsafe recognizer.processTouches(emptyPointer, count: 0, timestamp: 0.6, modifierFlags: [])
XCTAssertFalse(mockDelegate.didTapCalled, "Tap should not be detected when held too long")
}
func testTapNotDetectedWhenHeldBeyondMaxDuration() {
// Configure tap threshold high, but max hold duration low
recognizer.configuration.tapThreshold = 1.0 // 1 second
recognizer.configuration.maxTapHoldDuration = 0.3 // 300ms max hold
let touches = [
createTouch(x: 0.3, y: 0.5),
createTouch(x: 0.5, y: 0.5),
createTouch(x: 0.7, y: 0.5),
]
let (pointer, count, cleanup) = unsafe createTouchData(touches: touches)
defer { cleanup() }
// Start gesture
unsafe recognizer.processTouches(pointer, count: count, timestamp: 0.0, modifierFlags: [])
// Lift fingers after max hold duration exceeded but within tap threshold
let emptyTouches: [MTTouch] = []
let (emptyPointer, _, emptyCleanup) = unsafe createTouchData(touches: emptyTouches)
defer { emptyCleanup() }
// 0.5 seconds is within tap threshold (1.0s) but exceeds max hold (0.3s)
unsafe recognizer.processTouches(emptyPointer, count: 0, timestamp: 0.5, modifierFlags: [])
unsafe recognizer.processTouches(emptyPointer, count: 0, timestamp: 0.6, modifierFlags: [])
XCTAssertFalse(
mockDelegate.didTapCalled,
"Tap should not be detected when held beyond max hold duration")
}
func testTapDetectedWhenWithinMaxDuration() {
// Configure both tap threshold and max hold duration
recognizer.configuration.tapThreshold = 0.5 // 500ms
recognizer.configuration.maxTapHoldDuration = 0.5 // 500ms max hold
let touches = [
createTouch(x: 0.3, y: 0.5),
createTouch(x: 0.5, y: 0.5),
createTouch(x: 0.7, y: 0.5),
]
let (pointer, count, cleanup) = unsafe createTouchData(touches: touches)
defer { cleanup() }
// Start gesture
unsafe recognizer.processTouches(pointer, count: count, timestamp: 0.0, modifierFlags: [])
// Lift fingers within both thresholds
let emptyTouches: [MTTouch] = []
let (emptyPointer, _, emptyCleanup) = unsafe createTouchData(touches: emptyTouches)
defer { emptyCleanup() }
// 0.1 seconds is within both tap threshold and max hold
unsafe recognizer.processTouches(emptyPointer, count: 0, timestamp: 0.1, modifierFlags: [])
unsafe recognizer.processTouches(emptyPointer, count: 0, timestamp: 0.15, modifierFlags: [])
XCTAssertTrue(
mockDelegate.didTapCalled, "Tap should be detected when within max hold duration")
}
func testTapNotDetectedWhenMovingTooMuch() {
recognizer.configuration.tapThreshold = 0.5
recognizer.configuration.moveThreshold = 0.01 // Very low threshold
let touches = [
createTouch(x: 0.3, y: 0.5),
createTouch(x: 0.5, y: 0.5),
createTouch(x: 0.7, y: 0.5),
]
let (pointer, count, cleanup) = unsafe createTouchData(touches: touches)
defer { cleanup() }
// Start gesture
unsafe recognizer.processTouches(pointer, count: count, timestamp: 0.0, modifierFlags: [])
// Move with small delta (exceeds move threshold of 0.01, well within 0.15 jump threshold)
// Centroid move: from 0.5 to 0.52 = 0.02
let touches2 = [
createTouch(x: 0.32, y: 0.52),
createTouch(x: 0.52, y: 0.52),
createTouch(x: 0.72, y: 0.52),
]
let (pointer2, count2, cleanup2) = unsafe createTouchData(touches: touches2)
defer { cleanup2() }
unsafe recognizer.processTouches(pointer2, count: count2, timestamp: 0.1, modifierFlags: [])
// Verify we're in dragging state
XCTAssertEqual(recognizer.state, .dragging, "Should have transitioned to dragging")
// Lift fingers
let emptyTouches: [MTTouch] = []
let (emptyPointer, _, emptyCleanup) = unsafe createTouchData(touches: emptyTouches)
defer { emptyCleanup() }
unsafe recognizer.processTouches(emptyPointer, count: 0, timestamp: 0.2, modifierFlags: [])
unsafe recognizer.processTouches(emptyPointer, count: 0, timestamp: 0.25, modifierFlags: [])
// Tap should NOT be called because we transitioned to drag
XCTAssertFalse(
mockDelegate.didTapCalled, "Tap should not be detected when movement exceeds threshold")
XCTAssertTrue(mockDelegate.didBeginDraggingCalled, "Should have called begin dragging")
}
// MARK: - Drag Gesture Tests
func testDragBeginsAfterMovementThreshold() {
recognizer.configuration.moveThreshold = 0.02
let touches = [
createTouch(x: 0.3, y: 0.5),
createTouch(x: 0.5, y: 0.5),
createTouch(x: 0.7, y: 0.5),
]
let (pointer, count, cleanup) = unsafe createTouchData(touches: touches)
defer { cleanup() }
unsafe recognizer.processTouches(pointer, count: count, timestamp: 0.0, modifierFlags: [])
XCTAssertFalse(mockDelegate.didBeginDraggingCalled)
// Move past threshold
let touches2 = [
createTouch(x: 0.32, y: 0.52),
createTouch(x: 0.52, y: 0.52),
createTouch(x: 0.72, y: 0.52),
]
let (pointer2, count2, cleanup2) = unsafe createTouchData(touches: touches2)
defer { cleanup2() }
unsafe recognizer.processTouches(pointer2, count: count2, timestamp: 0.1, modifierFlags: [])
XCTAssertTrue(
mockDelegate.didBeginDraggingCalled, "Drag should begin after movement threshold")
}
func testDragDoesNotBeginAfterTimeThresholdWithoutMovement() {
// With the new behavior, resting fingers should NOT trigger a drag
// Drag only starts when there is actual movement
recognizer.configuration.tapThreshold = 0.2
recognizer.configuration.moveThreshold = 1.0 // Very high to prevent movement-based trigger
let touches = [
createTouch(x: 0.5, y: 0.5),
createTouch(x: 0.51, y: 0.5),
createTouch(x: 0.52, y: 0.5),
]
let (pointer, count, cleanup) = unsafe createTouchData(touches: touches)
defer { cleanup() }
unsafe recognizer.processTouches(pointer, count: count, timestamp: 0.0, modifierFlags: [])
// Same position but after time threshold - should NOT start drag
unsafe recognizer.processTouches(pointer, count: count, timestamp: 0.3, modifierFlags: [])
XCTAssertFalse(
mockDelegate.didBeginDraggingCalled,
"Drag should NOT begin just from time elapsed without movement")
}
func testDragUpdatesWithMovement() {
recognizer.configuration.moveThreshold = 0.01
let touches = [
createTouch(x: 0.3, y: 0.5),
createTouch(x: 0.5, y: 0.5),
createTouch(x: 0.7, y: 0.5),
]
let (pointer, count, cleanup) = unsafe createTouchData(touches: touches)
defer { cleanup() }
unsafe recognizer.processTouches(pointer, count: count, timestamp: 0.0, modifierFlags: [])
// First movement to trigger drag
let touches2 = [
createTouch(x: 0.32, y: 0.52),
createTouch(x: 0.52, y: 0.52),
createTouch(x: 0.72, y: 0.52),
]
let (pointer2, count2, cleanup2) = unsafe createTouchData(touches: touches2)
defer { cleanup2() }
unsafe recognizer.processTouches(pointer2, count: count2, timestamp: 0.1, modifierFlags: [])
XCTAssertTrue(mockDelegate.didBeginDraggingCalled)
// Additional movement - should trigger update (small delta 0.01 to avoid large jump rejection)
let touches3 = [
createTouch(x: 0.33, y: 0.53),
createTouch(x: 0.53, y: 0.53),
createTouch(x: 0.73, y: 0.53),
]
let (pointer3, count3, cleanup3) = unsafe createTouchData(touches: touches3)
defer { cleanup3() }
unsafe recognizer.processTouches(pointer3, count: count3, timestamp: 0.2, modifierFlags: [])
XCTAssertTrue(
mockDelegate.didUpdateDraggingCalled, "Drag updates should be sent during movement")
XCTAssertNotNil(mockDelegate.lastGestureData, "Gesture data should be provided")
}
func testDragEndsWithFingerLiftoff() {
recognizer.configuration.moveThreshold = 0.01
let touches = [
createTouch(x: 0.3, y: 0.5),
createTouch(x: 0.5, y: 0.5),
createTouch(x: 0.7, y: 0.5),
]
let (pointer, count, cleanup) = unsafe createTouchData(touches: touches)
defer { cleanup() }
unsafe recognizer.processTouches(pointer, count: count, timestamp: 0.0, modifierFlags: [])
// Move to start dragging
let touches2 = [
createTouch(x: 0.32, y: 0.52),
createTouch(x: 0.52, y: 0.52),
createTouch(x: 0.72, y: 0.52),
]
let (pointer2, count2, cleanup2) = unsafe createTouchData(touches: touches2)
defer { cleanup2() }
unsafe recognizer.processTouches(pointer2, count: count2, timestamp: 0.1, modifierFlags: [])
XCTAssertTrue(mockDelegate.didBeginDraggingCalled)
// Lift fingers
let emptyTouches: [MTTouch] = []
let (emptyPointer, _, emptyCleanup) = unsafe createTouchData(touches: emptyTouches)
defer { emptyCleanup() }
unsafe recognizer.processTouches(emptyPointer, count: 0, timestamp: 0.2, modifierFlags: [])
unsafe recognizer.processTouches(emptyPointer, count: 0, timestamp: 0.3, modifierFlags: [])
XCTAssertTrue(mockDelegate.didEndDraggingCalled, "Drag should end when fingers lift")
}
// MARK: - Cancellation and Cooldown Tests
func testFourFingersCancelActiveGesture() {
let touches = [
createTouch(x: 0.3, y: 0.5),
createTouch(x: 0.5, y: 0.5),
createTouch(x: 0.7, y: 0.5),
]
let (pointer, count, cleanup) = unsafe createTouchData(touches: touches)
defer { cleanup() }
unsafe recognizer.processTouches(pointer, count: count, timestamp: 0.0, modifierFlags: [])
XCTAssertTrue(mockDelegate.didStartCalled)
// Add 4th finger
let touches4 = [
createTouch(x: 0.2, y: 0.5),
createTouch(x: 0.4, y: 0.5),
createTouch(x: 0.6, y: 0.5),
createTouch(x: 0.8, y: 0.5),
]
let (pointer4, count4, cleanup4) = unsafe createTouchData(touches: touches4)
defer { cleanup4() }
unsafe recognizer.processTouches(pointer4, count: count4, timestamp: 0.1, modifierFlags: [])
XCTAssertTrue(
mockDelegate.didCancelCalled || mockDelegate.didCancelDraggingCalled,
"4 fingers should cancel active gesture")
XCTAssertEqual(recognizer.state, .idle, "State should be idle after cancel")
}
func testFourFingersCancelDragging() {
recognizer.configuration.moveThreshold = 0.01
let touches = [
createTouch(x: 0.3, y: 0.5),
createTouch(x: 0.5, y: 0.5),
createTouch(x: 0.7, y: 0.5),
]
let (pointer, count, cleanup) = unsafe createTouchData(touches: touches)
defer { cleanup() }
unsafe recognizer.processTouches(pointer, count: count, timestamp: 0.0, modifierFlags: [])
// Start dragging
let touches2 = [
createTouch(x: 0.32, y: 0.52),
createTouch(x: 0.52, y: 0.52),
createTouch(x: 0.72, y: 0.52),
]
let (pointer2, count2, cleanup2) = unsafe createTouchData(touches: touches2)
defer { cleanup2() }
unsafe recognizer.processTouches(pointer2, count: count2, timestamp: 0.1, modifierFlags: [])
XCTAssertEqual(recognizer.state, .dragging)
// Add 4th finger
let touches4 = [
createTouch(x: 0.2, y: 0.5),
createTouch(x: 0.4, y: 0.5),
createTouch(x: 0.6, y: 0.5),
createTouch(x: 0.8, y: 0.5),
]
let (pointer4, count4, cleanup4) = unsafe createTouchData(touches: touches4)
defer { cleanup4() }
unsafe recognizer.processTouches(pointer4, count: count4, timestamp: 0.2, modifierFlags: [])
XCTAssertTrue(mockDelegate.didCancelDraggingCalled, "4 fingers should cancel dragging")
}
func testCooldownDuringActiveDragPreventsImmediateRestart() {
// This test verifies that the cooldown mechanism works during an ACTIVE drag
// When 4 fingers are detected during dragging, it cancels and sets cooldown
// The cooldown should NOT block when going back to 3 fingers from idle state
// (that's intentional - see comment in code "so user can start a new gesture")
recognizer.configuration.moveThreshold = 0.01
let touches3 = [
createTouch(x: 0.3, y: 0.5),
createTouch(x: 0.5, y: 0.5),
createTouch(x: 0.7, y: 0.5),
]
let (pointer3, count3, cleanup3) = unsafe createTouchData(touches: touches3)
defer { cleanup3() }
unsafe recognizer.processTouches(pointer3, count: count3, timestamp: 0.0, modifierFlags: [])
// Move to start dragging
let touches3b = [
createTouch(x: 0.32, y: 0.52),
createTouch(x: 0.52, y: 0.52),
createTouch(x: 0.72, y: 0.52),
]
let (pointer3b, count3b, cleanup3b) = unsafe createTouchData(touches: touches3b)
defer { cleanup3b() }
unsafe recognizer.processTouches(pointer3b, count: count3b, timestamp: 0.1, modifierFlags: [])
XCTAssertEqual(recognizer.state, .dragging)
mockDelegate.reset()
// 4 fingers trigger cancellation
let touches4 = [
createTouch(x: 0.2, y: 0.5),
createTouch(x: 0.4, y: 0.5),
createTouch(x: 0.6, y: 0.5),
createTouch(x: 0.8, y: 0.5),
]
let (pointer4, count4, cleanup4) = unsafe createTouchData(touches: touches4)
defer { cleanup4() }
unsafe recognizer.processTouches(pointer4, count: count4, timestamp: 0.2, modifierFlags: [])
XCTAssertTrue(mockDelegate.didCancelDraggingCalled, "4 fingers should cancel dragging")
XCTAssertEqual(recognizer.state, .idle, "State should be idle after cancel")
// Now try 3 fingers again - since state is idle, cooldown should clear
// and gesture should be allowed (this is the intended behavior)
mockDelegate.reset()
unsafe recognizer.processTouches(pointer3, count: count3, timestamp: 0.3, modifierFlags: [])
// Gesture SHOULD start because cooldown clears when (state == .idle && fingerCount == 3)
XCTAssertTrue(
mockDelegate.didStartCalled, "Gesture should start from idle state even after cancel")
}
func testCooldownClearsWhenFingersDropBelowThree() {
let touches3 = [
createTouch(x: 0.3, y: 0.5),
createTouch(x: 0.5, y: 0.5),
createTouch(x: 0.7, y: 0.5),
]
let (pointer3, count3, cleanup3) = unsafe createTouchData(touches: touches3)
defer { cleanup3() }
unsafe recognizer.processTouches(pointer3, count: count3, timestamp: 0.0, modifierFlags: [])
// 4 fingers trigger cancellation
let touches4 = [
createTouch(x: 0.2, y: 0.5),
createTouch(x: 0.4, y: 0.5),
createTouch(x: 0.6, y: 0.5),
createTouch(x: 0.8, y: 0.5),
]
let (pointer4, count4, cleanup4) = unsafe createTouchData(touches: touches4)
defer { cleanup4() }
unsafe recognizer.processTouches(pointer4, count: count4, timestamp: 0.1, modifierFlags: [])
// Drop to 2 fingers to clear cooldown
let touches2 = [
createTouch(x: 0.4, y: 0.5),
createTouch(x: 0.6, y: 0.5),
]
let (pointer2, count2, cleanup2) = unsafe createTouchData(touches: touches2)
defer { cleanup2() }
unsafe recognizer.processTouches(pointer2, count: count2, timestamp: 0.2, modifierFlags: [])
// Now 3 fingers should work again
mockDelegate.reset()
unsafe recognizer.processTouches(pointer3, count: count3, timestamp: 0.3, modifierFlags: [])
XCTAssertTrue(mockDelegate.didStartCalled, "Gesture should start after cooldown clears")
}
// MARK: - Edge Case Tests
func testResetClearsAllState() {
let touches = [
createTouch(x: 0.3, y: 0.5),
createTouch(x: 0.5, y: 0.5),
createTouch(x: 0.7, y: 0.5),
]
let (pointer, count, cleanup) = unsafe createTouchData(touches: touches)
defer { cleanup() }
unsafe recognizer.processTouches(pointer, count: count, timestamp: 0.0, modifierFlags: [])
XCTAssertNotEqual(recognizer.state, .idle)
recognizer.reset()
XCTAssertEqual(recognizer.state, .idle, "State should be idle after reset")
}
func testTouchStateFiltering_OnlyAcceptsState3And4() {
// State 5 = lifting, should be ignored
let touches = [
createTouch(x: 0.3, y: 0.5, zTotal: 0.5, state: 5), // Lifting - ignored
createTouch(x: 0.5, y: 0.5, zTotal: 0.5, state: 4), // Active - counted
createTouch(x: 0.7, y: 0.5, zTotal: 0.5, state: 4), // Active - counted
]
let (pointer, count, cleanup) = unsafe createTouchData(touches: touches)
defer { cleanup() }
unsafe recognizer.processTouches(pointer, count: count, timestamp: 0.0, modifierFlags: [])
// Only 2 valid touches, should not start gesture
XCTAssertFalse(mockDelegate.didStartCalled, "Should not start with only 2 valid touches")
}
func testTouchStateFiltering_State3Accepted() {
// State 3 = touching down
let touches = [