Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

TranscriptViewportGeometryTests.swift

304 lines11.1 KB

1import CoreGraphics
2import Testing
3import UIKit
4@testable import T3Code
5​
6@Suite("Transcript viewport anchoring")
7struct TranscriptViewportGeometryTests {
8 @Test func bottomButtonUsesTheVisibleViewportIncludingKeyboardInsets() {
9 let geometry = TranscriptViewportGeometry(contentHeight: 1_200, viewportHeight: 400, topInset: 20, bottomInset: 100)
10 #expect(geometry.showsScrollToBottom(at: 780))
11 #expect(!geometry.showsScrollToBottom(at: 781))
12 #expect(!geometry.showsScrollToBottom(at: 900))
13 let short = TranscriptViewportGeometry(contentHeight: 100, viewportHeight: 400, topInset: 20, bottomInset: 0)
14 #expect(!short.showsScrollToBottom(at: -20))
15 }
16​
17 @Test @MainActor
18 func transcriptOpensAtBottomAndButtonRestoresFollowing() throws {
19 let layout = FixedTranscriptLayout()
20 let view = BottomAnchoredTranscriptCollectionView(frame: .zero, collectionViewLayout: layout)
21 let dataSource = FixedTranscriptDataSource()
22 defer { withExtendedLifetime(dataSource) {} }
23 view.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "message")
24 view.dataSource = dataSource
25 view.contentInsetAdjustmentBehavior = .never
26 let controller = UIViewController()
27 let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 390, height: 800))
28 window.rootViewController = controller
29 window.makeKeyAndVisible()
30 defer { window.isHidden = true }
31 controller.view.addSubview(view)
32 view.layoutIfNeeded()
33 // The first snapshot can arrive before the destination has its final size.
34 layout.height = 3_000
35 view.reloadData()
36 layout.invalidateLayout()
37 view.frame = CGRect(x: 0, y: 0, width: 390, height: 600)
38 view.layoutIfNeeded()
39 #expect(view.contentOffset.y == 2_400)
40 let button = try #require(view.subviews.compactMap { $0 as? UIButton }.first)
41 #expect(button.isHidden)
42​
43 view.maintainsBottomAnchor = false
44 view.contentOffset.y = 500
45 view.layoutIfNeeded()
46 #expect(!button.isHidden)
47 #expect(view.bounds.contains(button.frame))
48 button.sendActions(for: .touchUpInside)
49 #expect(view.contentOffset.y == 2_400)
50 #expect(view.maintainsBottomAnchor)
51 #expect(button.isHidden)
52​
53 layout.height = 3_200
54 layout.invalidateLayout()
55 view.layoutIfNeeded()
56 #expect(view.contentOffset.y == 2_600)
57 view.frame.size.height = 350
58 view.layoutIfNeeded()
59 #expect(view.contentOffset.y == 2_850)
60 #expect(button.isHidden)
61 }
62​
63 @Test
64 func firstLoadedTranscriptAnchorsToLatestMessage() {
65 let empty = TranscriptViewportGeometry(
66 contentHeight: 0,
67 viewportHeight: 700,
68 topInset: 0,
69 bottomInset: 0
70 )
71 let loaded = TranscriptViewportGeometry(
72 contentHeight: 1_200,
73 viewportHeight: 700,
74 topInset: 0,
75 bottomInset: 0
76 )
77​
78 #expect(
79 loaded.restoredBottomOffset(
80 after: empty,
81 maintainsBottomAnchor: true,
82 isInteracting: false
83 ) == 500
84 )
85 }
86​
87 @Test
88 func keyboardViewportChangeKeepsLatestMessageVisible() {
89 let beforeKeyboard = TranscriptViewportGeometry(
90 contentHeight: 1_200,
91 viewportHeight: 700,
92 topInset: 0,
93 bottomInset: 0
94 )
95 let afterKeyboard = TranscriptViewportGeometry(
96 contentHeight: 1_200,
97 viewportHeight: 400,
98 topInset: 0,
99 bottomInset: 0
100 )
101​
102 #expect(
103 afterKeyboard.restoredBottomOffset(
104 after: beforeKeyboard,
105 maintainsBottomAnchor: true,
106 isInteracting: false
107 ) == 800
108 )
109 }
110​
111 @Test
112 func readerPositionIsUntouchedAwayFromLatestMessage() {
113 let beforeKeyboard = TranscriptViewportGeometry(
114 contentHeight: 1_200,
115 viewportHeight: 700,
116 topInset: 0,
117 bottomInset: 0
118 )
119 let afterKeyboard = TranscriptViewportGeometry(
120 contentHeight: 1_200,
121 viewportHeight: 400,
122 topInset: 0,
123 bottomInset: 0
124 )
125​
126 #expect(
127 afterKeyboard.restoredBottomOffset(
128 after: beforeKeyboard,
129 maintainsBottomAnchor: false,
130 isInteracting: false
131 ) == nil
132 )
133 }
134​
135 @Test
136 func activeTranscriptGestureOwnsItsScrollPosition() {
137 let before = TranscriptViewportGeometry(
138 contentHeight: 1_200,
139 viewportHeight: 700,
140 topInset: 0,
141 bottomInset: 0
142 )
143 let after = TranscriptViewportGeometry(
144 contentHeight: 1_260,
145 viewportHeight: 700,
146 topInset: 0,
147 bottomInset: 0
148 )
149​
150 #expect(
151 after.restoredBottomOffset(
152 after: before,
153 maintainsBottomAnchor: true,
154 isInteracting: true
155 ) == nil
156 )
157 }
158​
159 @Test
160 func verticalPanFailsBeforeItCanCompeteWithTranscriptScrolling() {
161 #expect(!ThreadBackSwipeGesture.shouldBegin(with: CGPoint(x: 40, y: 120)))
162 #expect(!ThreadBackSwipeGesture.shouldBegin(with: CGPoint(x: -120, y: 0)))
163 }
164​
165 @Test
166 func horizontalPanCanLeaveTheThreadFromAnywhereOnTheSurface() {
167 #expect(ThreadBackSwipeGesture.shouldBegin(with: CGPoint(x: 120, y: 20)))
168 #expect(
169 ThreadBackSwipeGesture.shouldNavigateBack(
170 with: CGPoint(x: 96, y: 16)
171 )
172 )
173 }
174​
175 @Test
176 func slowHorizontalPanUsesTranslationWhenVelocityIsUnavailable() {
177 #expect(
178 ThreadBackSwipeGesture.shouldBegin(
179 with: .zero,
180 translation: CGPoint(x: 16, y: 2)
181 )
182 )
183 #expect(
184 !ThreadBackSwipeGesture.shouldBegin(
185 with: .zero,
186 translation: CGPoint(x: 4, y: 16)
187 )
188 )
189 }
190​
191 @Test
192 func shortOrDiagonalPanDoesNotLeaveTheThread() {
193 #expect(
194 !ThreadBackSwipeGesture.shouldNavigateBack(
195 with: CGPoint(x: 71, y: 0)
196 )
197 )
198 #expect(
199 !ThreadBackSwipeGesture.shouldNavigateBack(
200 with: CGPoint(x: 96, y: 80)
201 )
202 )
203 }
204​
205 @Test
206 @MainActor
207 func horizontalScrollContentSharesOnlyAtItsLeadingEdge() {
208 let transcript = UIScrollView(frame: CGRect(x: 0, y: 0, width: 120, height: 120))
209 transcript.contentSize = CGSize(width: 120, height: 480)
210 #expect(ThreadBackSwipeGesture.shouldAllowSimultaneousRecognition(with: transcript))
211​
212 let codeBlock = UIScrollView(frame: CGRect(x: 0, y: 0, width: 120, height: 120))
213 codeBlock.contentSize = CGSize(width: 480, height: 120)
214 codeBlock.alwaysBounceVertical = true
215 #expect(ThreadBackSwipeGesture.shouldAllowSimultaneousRecognition(with: codeBlock))
216 codeBlock.contentOffset = CGPoint(x: 100, y: 0)
217 #expect(!ThreadBackSwipeGesture.shouldAllowSimultaneousRecognition(with: codeBlock))
218 }
219​
220 @Test
221 @MainActor
222 func horizontalScrollAncestorsCanReceiveBackPanAtLeadingEdge() {
223 let host = UIView(frame: CGRect(x: 0, y: 0, width: 240, height: 240))
224 let codeBlock = UIScrollView(frame: host.bounds)
225 codeBlock.contentSize = CGSize(width: 480, height: 240)
226 let label = UILabel(frame: .zero)
227 codeBlock.addSubview(label)
228 host.addSubview(codeBlock)
229​
230 #expect(ThreadBackSwipeGesture.shouldReceiveTouch(in: label, host: host))
231 codeBlock.contentOffset = CGPoint(x: 100, y: 0)
232 #expect(!ThreadBackSwipeGesture.shouldReceiveTouch(in: label, host: host))
233 #expect(ThreadBackSwipeGesture.shouldReceiveTouch(in: host, host: host))
234​
235 let detachedHost = UIView(frame: host.bounds)
236 let detachedCodeBlock = UIScrollView(frame: detachedHost.bounds)
237 detachedCodeBlock.contentSize = CGSize(width: 480, height: 240)
238 let detachedLabel = UILabel(frame: .zero)
239 detachedCodeBlock.addSubview(detachedLabel)
240 detachedHost.addSubview(detachedCodeBlock)
241 #expect(!ThreadBackSwipeGesture.shouldReceiveTouch(in: detachedLabel, host: host))
242 }
243​
244 @Test
245 @MainActor
246 func activeTextInteractionsKeepHorizontalDrags() {
247 let host = UIView(frame: CGRect(x: 0, y: 0, width: 240, height: 240))
248 let textField = UITextField(frame: .zero)
249 let textView = UITextView(frame: .zero)
250 textView.text = "Selectable transcript text"
251 let textViewContent = UIView(frame: .zero)
252 textView.addSubview(textViewContent)
253 host.addSubview(textField)
254 host.addSubview(textView)
255​
256 #expect(!ThreadBackSwipeGesture.shouldReceiveTouch(in: textField, host: host))
257 textView.isEditable = false
258 #expect(ThreadBackSwipeGesture.shouldReceiveTouch(in: textView, host: host))
259 #expect(ThreadBackSwipeGesture.shouldReceiveTouch(in: textViewContent, host: host))
260​
261 textView.selectedRange = NSRange(location: 0, length: 1)
262 #expect(ThreadBackSwipeGesture.shouldReceiveTouch(in: textView, host: host))
263 #expect(ThreadBackSwipeGesture.shouldReceiveTouch(in: textViewContent, host: host))
264​
265 textView.selectedRange = NSRange(location: 0, length: 0)
266 textView.isEditable = true
267 #expect(!ThreadBackSwipeGesture.shouldReceiveTouch(in: textView, host: host))
268​
269 let window = UIWindow(frame: host.bounds)
270 let rootViewController = UIViewController()
271 window.rootViewController = rootViewController
272 rootViewController.view.addSubview(host)
273 window.makeKeyAndVisible()
274 textView.isEditable = false
275 #expect(textView.becomeFirstResponder())
276 #expect(!ThreadBackSwipeGesture.shouldReceiveTouch(in: textViewContent, host: host))
277 textView.resignFirstResponder()
278 window.isHidden = true
279​
280 #expect(ThreadBackSwipeGesture.shouldReceiveTouch(in: host, host: host))
281 }
283​
284@MainActor
285private final class FixedTranscriptLayout: UICollectionViewLayout {
286 var height: CGFloat = 0
287 override var collectionViewContentSize: CGSize { CGSize(width: 390, height: height) }
288 override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
289 let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)
290 attributes.frame = CGRect(x: 0, y: 0, width: 390, height: height)
291 return attributes
292 }
293 override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
294 [layoutAttributesForItem(at: IndexPath(item: 0, section: 0))!]
295 }
297​
298@MainActor
299private final class FixedTranscriptDataSource: NSObject, UICollectionViewDataSource {
300 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 1 }
301 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
302 collectionView.dequeueReusableCell(withReuseIdentifier: "message", for: indexPath)
303 }