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
@testable import Grain
import SwiftUI
import UIKit
import XCTest
/// The app drops to UIKit for three gestures — drag-to-dismiss on a story,
/// long-press-to-reorder in the grid, and the photo strip's horizontal pan —
/// because SwiftUI's equivalents fight the surrounding scroll views. What makes
/// them coexist is entirely in their delegates, which is exactly the part a
/// render test never reaches.
@MainActor
final class GestureCoordinatorTests: GrainTestCase {
/// Records callbacks a coordinator fires.
private final class Calls {
var phases: [String] = []
var translations: [CGSize] = []
}
/// A view sitting in a real view-controller hierarchy, which is what the
/// installers walk the responder chain to find.
private func hostedView() -> (view: UIView, controller: UIViewController, window: UIWindow) {
let controller = UIViewController()
let anchor = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
controller.view.addSubview(anchor)
let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 402, height: 874))
window.rootViewController = controller
window.isHidden = false
return (anchor, controller, window)
}
private func teardown(_ window: UIWindow) {
window.isHidden = true
window.rootViewController = nil
}
// MARK: - Drag to dismiss
private func makeDismissCoordinator(
handle: FadeDismissHandle,
calls: Calls,
onDismiss: @escaping () -> Void = {}
) -> DragToDismissInstaller.Coordinator {
DragToDismissInstaller.Coordinator(
handle: handle,
onDismiss: { calls.phases.append("dismiss"); onDismiss() },
onDragStart: { calls.phases.append("dragStart") },
onDragCancel: { calls.phases.append("dragCancel") },
onSwipeLeft: { calls.phases.append("swipeLeft") },
onSwipeRight: { calls.phases.append("swipeRight") },
onHorizontalDragStart: { calls.phases.append($0 ? "forward" : "back") },
onSwipeDragging: { calls.translations.append(CGSize(width: $0, height: 0)) },
onHorizontalDragCancel: { calls.phases.append("horizontalCancel") }
)
}
/// The pan is installed on the hosting controller's view, not on the tiny
/// anchor, so a drag anywhere on the story dismisses it.
func testInstallingPutsThePanOnTheHostingControllersView() {
let hosted = hostedView()
defer { teardown(hosted.window) }
let handle = FadeDismissHandle()
let coordinator = makeDismissCoordinator(handle: handle, calls: Calls())
coordinator.anchorView = hosted.view
coordinator.installGestureIfNeeded()
let installed = hosted.controller.view.gestureRecognizers ?? []
XCTAssertTrue(installed.contains { $0 is UIPanGestureRecognizer })
XCTAssertTrue(installed.contains { $0.delegate === coordinator })
}
/// `updateUIView` runs on every SwiftUI update, so installing has to be
/// idempotent or the story ends up with a stack of pan recognizers.
func testInstallingTwiceOnlyAddsOnePan() {
let hosted = hostedView()
defer { teardown(hosted.window) }
let coordinator = makeDismissCoordinator(handle: FadeDismissHandle(), calls: Calls())
coordinator.anchorView = hosted.view
coordinator.installGestureIfNeeded()
coordinator.installGestureIfNeeded()
coordinator.installGestureIfNeeded()
let pans = (hosted.controller.view.gestureRecognizers ?? []).filter { $0 is UIPanGestureRecognizer }
XCTAssertEqual(pans.count, 1)
}
/// Before SwiftUI has put the view in a hierarchy there is nothing to
/// install onto, and that has to be survivable rather than a trap.
func testInstallingWithNothingToAttachToIsHarmless() {
let coordinator = makeDismissCoordinator(handle: FadeDismissHandle(), calls: Calls())
coordinator.installGestureIfNeeded()
coordinator.anchorView = UIView()
coordinator.installGestureIfNeeded()
XCTAssertNil(coordinator.anchorView?.gestureRecognizers)
}
/// The comment sheet takes over touches while it's up, so the story's pan
/// has to be switched off rather than competing with it.
func testDisablingTurnsTheInstalledPanOff() {
let hosted = hostedView()
defer { teardown(hosted.window) }
let coordinator = makeDismissCoordinator(handle: FadeDismissHandle(), calls: Calls())
coordinator.anchorView = hosted.view
coordinator.installGestureIfNeeded()
let pan = (hosted.controller.view.gestureRecognizers ?? []).first { $0 is UIPanGestureRecognizer }
coordinator.setEnabled(false)
XCTAssertEqual(pan?.isEnabled, false)
coordinator.setEnabled(true)
XCTAssertEqual(pan?.isEnabled, true)
}
func testDisablingBeforeAnythingIsInstalledIsHarmless() {
let coordinator = makeDismissCoordinator(handle: FadeDismissHandle(), calls: Calls())
coordinator.setEnabled(false)
coordinator.setEnabled(true)
XCTAssertNotNil(coordinator)
}
/// SwiftUI taps inside the story have to keep working during the drag, but
/// a second pan — the comment sheet's scroll, say — must not.
func testTapsRunAlongsideTheDismissPanButOtherPansDoNot() {
let coordinator = makeDismissCoordinator(handle: FadeDismissHandle(), calls: Calls())
XCTAssertTrue(
coordinator.gestureRecognizer(UIPanGestureRecognizer(), shouldRecognizeSimultaneouslyWith: UITapGestureRecognizer())
)
XCTAssertFalse(
coordinator.gestureRecognizer(UIPanGestureRecognizer(), shouldRecognizeSimultaneouslyWith: UIPanGestureRecognizer())
)
}
// MARK: - Fade dismiss handle
/// Installing the gesture is also what wires the handle to the dismiss
/// callback, so the story viewer can fade out programmatically when the
/// last story ends — and the dismiss lands after the fade, not before,
/// otherwise the story pops off screen instead of fading.
func testTheHandleFadesTheViewThenDismisses() {
let hosted = hostedView()
defer { teardown(hosted.window) }
let handle = FadeDismissHandle()
let dismissed = expectation(description: "dismiss ran after the fade")
let coordinator = makeDismissCoordinator(
handle: handle, calls: Calls(), onDismiss: { dismissed.fulfill() }
)
coordinator.anchorView = hosted.view
coordinator.installGestureIfNeeded()
handle.fadeDismiss()
wait(for: [dismissed], timeout: 5)
XCTAssertEqual(hosted.controller.view.alpha, 0, "The dismiss lands at the end of the fade, not before it")
}
/// Nothing wired up at all — the story viewer builds the handle before it
/// has anywhere to send the dismissal.
func testAnUnwiredHandleIsHarmless() {
FadeDismissHandle().fadeDismiss()
}
// MARK: - Reorder recognizer
private func makeReorderCoordinator(isEnabled: Bool, calls: Calls) -> ReorderRecognizer.Coordinator {
ReorderRecognizer.Coordinator(isEnabled: isEnabled) { phase, translation in
calls.phases.append(String(describing: phase))
calls.translations.append(translation)
}
}
/// Arming fires the moment a finger lands, before the hold completes, so
/// the parent can lock the Form's scroll and not lose the vertical
/// component of the drag.
func testTouchingDownArmsTheReorderBeforeTheHoldCompletes() {
let calls = Calls()
let coordinator = makeReorderCoordinator(isEnabled: true, calls: calls)
let shouldBegin = coordinator.gestureRecognizerShouldBegin(UILongPressGestureRecognizer())
XCTAssertTrue(shouldBegin)
XCTAssertEqual(calls.phases, ["arming"])
}
/// The recognizer is gated on editor mode; in preview or captions mode it
/// must never fire, and must not announce arming either.
func testADisabledReorderNeverBeginsOrArms() {
let calls = Calls()
let coordinator = makeReorderCoordinator(isEnabled: false, calls: calls)
let shouldBegin = coordinator.gestureRecognizerShouldBegin(UILongPressGestureRecognizer())
XCTAssertFalse(shouldBegin)
XCTAssertTrue(calls.phases.isEmpty)
}
/// The gate is re-read on every SwiftUI update rather than captured once.
func testTheReorderGateCanBeFlippedAfterTheFact() {
let calls = Calls()
let coordinator = makeReorderCoordinator(isEnabled: false, calls: calls)
XCTAssertFalse(coordinator.gestureRecognizerShouldBegin(UILongPressGestureRecognizer()))
coordinator.isEnabled = true
XCTAssertTrue(coordinator.gestureRecognizerShouldBegin(UILongPressGestureRecognizer()))
}
/// Scroll pans and SwiftUI taps have to keep firing alongside a reorder —
/// this is the hook the Representable protocol doesn't offer, and the whole
/// reason the gesture is UIKit-backed.
func testAReorderRunsAlongsideEveryOtherGesture() {
let coordinator = makeReorderCoordinator(isEnabled: true, calls: Calls())
for other in [UIPanGestureRecognizer(), UITapGestureRecognizer(), UILongPressGestureRecognizer()] {
XCTAssertTrue(
coordinator.gestureRecognizer(UILongPressGestureRecognizer(), shouldRecognizeSimultaneouslyWith: other)
)
}
}
func testTheReorderCoordinatorStartsWithNoDragInFlight() {
XCTAssertNil(makeReorderCoordinator(isEnabled: true, calls: Calls()).startLocation)
}
// MARK: - Strip pan recognizer
/// A recognizer that has never been attached has no coordinate space to
/// report velocity in, so give it one.
private func attachedPan() -> UIPanGestureRecognizer {
let pan = UIPanGestureRecognizer()
UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100)).addGestureRecognizer(pan)
return pan
}
private func makeStripCoordinator(isEnabled: Bool, calls: Calls) -> StripPanRecognizer.Coordinator {
StripPanRecognizer.Coordinator(
isEnabled: isEnabled,
onChanged: { calls.translations.append(CGSize(width: $0, height: 0)) },
onEnded: { translation, _ in calls.translations.append(CGSize(width: translation, height: 0)) }
)
}
/// The strip scrolls sideways inside a vertically-scrolling Form, so a
/// mostly-vertical drag has to be left to the Form.
func testTheStripOnlyClaimsMostlyHorizontalDrags() {
let coordinator = makeStripCoordinator(isEnabled: true, calls: Calls())
// A resting finger reports zero velocity, which is not horizontal.
XCTAssertFalse(coordinator.gestureRecognizerShouldBegin(attachedPan()))
}
/// In grid or captions mode the strip isn't on screen and its recognizer
/// has to be inert.
func testADisabledStripNeverBegins() {
let coordinator = makeStripCoordinator(isEnabled: false, calls: Calls())
XCTAssertFalse(coordinator.gestureRecognizerShouldBegin(attachedPan()))
}
/// Anything that isn't a pan is let through untouched.
func testANonPanGestureIsNotTheStripsToJudge() {
let coordinator = makeStripCoordinator(isEnabled: true, calls: Calls())
XCTAssertTrue(coordinator.gestureRecognizerShouldBegin(UITapGestureRecognizer()))
}
/// Once the strip's pan is running, the Form's vertical scroll must not run
/// with it — otherwise the strip and the page move together.
func testTheStripRefusesToShareWithTheEnclosingScroll() {
let coordinator = makeStripCoordinator(isEnabled: true, calls: Calls())
XCTAssertFalse(
coordinator.gestureRecognizer(attachedPan(), shouldRecognizeSimultaneouslyWith: attachedPan())
)
}
}