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
import SwiftUI
import UIKit
import XCTest
@testable import OldFella
@MainActor
final class ThreadScrollViewTests: XCTestCase {
func testPrependedAncestorKeepsPositionAfterScrolling() async throws {
try await assertReadingPosition(initialHeight: 0, updatedHeight: 200)
}
func testAncestorHeightChangeKeepsPositionAfterScrolling() async throws {
try await assertReadingPosition(initialHeight: 200, updatedHeight: 400)
}
func testAncestorRemovalKeepsPositionAfterScrolling() async throws {
try await assertReadingPosition(initialHeight: 200, updatedHeight: 0)
}
func testAncestorShrinkKeepsBottomPosition() async throws {
try await assertReadingPosition(initialHeight: 400, updatedHeight: 200, atBottom: true)
}
private func assertReadingPosition(
initialHeight: CGFloat, updatedHeight: CGFloat, atBottom: Bool = false
) async throws {
let marker = UIView()
let host = UIHostingController(rootView: FixtureView(ancestorHeight: initialHeight, marker: marker))
let scene = try XCTUnwrap(UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }.first)
let window = UIWindow(windowScene: scene)
window.frame = CGRect(x: 0, y: 0, width: 390, height: 844)
window.rootViewController = host
window.isHidden = false
defer { window.isHidden = true }
host.view.layoutIfNeeded()
try await Task.sleep(for: .milliseconds(100))
var ancestor = marker.superview
while ancestor != nil && !(ancestor is UIScrollView) { ancestor = ancestor?.superview }
let scroll = try XCTUnwrap(ancestor as? UIScrollView)
let offset = atBottom
? scroll.contentSize.height - scroll.bounds.height + scroll.adjustedContentInset.bottom
: initialHeight + 100 - scroll.adjustedContentInset.top
scroll.setContentOffset(CGPoint(x: 0, y: offset), animated: false)
try await Task.sleep(for: .milliseconds(100))
let initialTop = marker.convert(.zero, to: window).y
let initialContentHeight = scroll.contentSize.height
host.rootView = FixtureView(ancestorHeight: updatedHeight, marker: marker)
host.view.setNeedsLayout()
host.view.layoutIfNeeded()
try await Task.sleep(for: .milliseconds(100))
XCTAssertEqual(scroll.contentSize.height - initialContentHeight, updatedHeight - initialHeight, accuracy: 1)
XCTAssertEqual(marker.convert(.zero, to: window).y, initialTop, accuracy: 1)
}
func testThreadContentBelongsToThePresentingControllerHierarchy() async throws {
let probe = UIView()
let host = UIHostingController(
rootView: ThreadScrollView(anchorID: "post") { _ in
VStack {
ControllerProbe(view: probe)
.frame(height: 100)
.background(alignment: .top) { ThreadScrollAnchor(id: "post") }
}
})
let scene = try XCTUnwrap(UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }.first)
let window = UIWindow(windowScene: scene)
window.frame = CGRect(x: 0, y: 0, width: 390, height: 844)
window.rootViewController = host
window.isHidden = false
defer { window.isHidden = true }
host.view.layoutIfNeeded()
try await Task.sleep(for: .milliseconds(100))
var responder: UIResponder? = probe
while let current = responder, !(current is UIViewController) {
responder = current.next
}
let contentController = try XCTUnwrap(responder as? UIViewController)
XCTAssertNotNil(contentController.view.window)
var ancestor: UIViewController? = contentController
while let parent = ancestor?.parent { ancestor = parent }
XCTAssertTrue(ancestor === host, "Thread content must share the controller hierarchy that presents sheets.")
}
private struct ControllerProbe: UIViewRepresentable {
let view: UIView
func makeUIView(context: Context) -> UIView { view }
func updateUIView(_ uiView: UIView, context: Context) {}
}
private struct FixtureView: View {
let ancestorHeight: CGFloat
let marker: UIView
var body: some View {
ThreadScrollView(anchorID: "post") { height in
VStack(spacing: 0) {
VStack {
if ancestorHeight > 0 {
Color.clear.frame(height: ancestorHeight).id("ancestor")
}
}
VStack(spacing: 0) {
ControllerProbe(view: marker).frame(height: 200)
.background(alignment: .top) { ThreadScrollAnchor(id: "post") }
Color.clear.frame(height: 1200).id("reply")
}
.frame(minHeight: height, alignment: .top)
}
}
}
}
}