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) } } } } }