Tests/OrgEditorAppKitTests/ScrollStabilityTests.swift
97 lines · 3911 bytes
8 symbols in this file
1import AppKit
2import OrgCore
3import OrgDocument
4import OrgPresentation
5import Testing
6@testable import OrgEditorAppKit
7
8/// A long file with lines of very different lengths, as real notes have: TextKit's height
9/// estimates for text it hasn't laid out are far off for such a file, so laying the whole
10/// document out again moves what the window shows.
11@MainActor
12struct ScrollStabilityTests {
13 static func longDocument(headings: Int) -> String {
14 var seed: UInt64 = 0x9E37_79B9_7F4A_7C15
15 func next() -> Int {
16 seed = seed &* 6_364_136_223_846_793_005 &+ 1_442_695_040_888_963_407
17 return Int(truncatingIfNeeded: seed >> 33)
18 }
19 let words = ["meeting", "todo", "the", "a", "deadline", "note", "project", "review", "call", "later", "important", "x"]
20 var text = "#+STARTUP: indent\n"
21 for n in 0..<headings {
22 text += "\(n % 3 == 0 ? "**" : "*") Heading \(n)\n"
23 for _ in 0..<(1 + next() % 4) {
24 let count: Int
25 switch next() % 5 {
26 case 0: count = 60 + next() % 120
27 case 1: count = 1 + next() % 4
28 default: count = 3 + next() % 12
29 }
30 text += (0..<count).map { _ in words[next() % words.count] }.joined(separator: " ") + "\n"
31 }
32 }
33 return text
34 }
35
36 /// The window on screen, with layout left to the view, as a user's window is.
37 static func shownHarness(_ text: String) -> Harness {
38 NSApplication.shared.setActivationPolicy(.regular)
39 let h = Harness(DocumentState(bytes: Array(text.utf8)), layout: false)
40 h.window.makeKeyAndOrderFront(nil)
41 pump(h)
42 h.drainStyling()
43 pump(h)
44 return h
45 }
46
47 static func pump(_ h: Harness) {
48 RunLoop.main.run(until: Date().addingTimeInterval(0.05))
49 h.window.displayIfNeeded()
50 }
51
52 /// The number of the line at the top of the window.
53 static func topLine(_ h: Harness) -> Int {
54 let layout = h.editor.textLayoutManager
55 let content = layout.textContentManager!
56 let y = h.textView.enclosingScrollView!.documentVisibleRect.minY - h.textView.textContainerOrigin.y
57 guard let fragment = layout.textLayoutFragment(for: CGPoint(x: 0, y: max(0, y))) else { return -1 }
58 let offset = content.offset(from: content.documentRange.location, to: fragment.rangeInElement.location)
59 return (h.string as NSString).substring(to: offset).filter { $0 == "\n" }.count
60 }
61
62 @Test func foldingNearTheEndKeepsTheWindowWhereItWas() {
63 let h = Self.shownHarness(Self.longDocument(headings: 1500))
64 let heading = h.offset(of: "* Heading 1480\n")
65 h.editor.setCaret(heading)
66 h.textView.scrollRangeToVisible(NSRange(location: heading, length: 0))
67 Self.pump(h)
68 let before = Self.topLine(h)
69 #expect(before > 5000)
70
71 var state = h.editor.foldState
72 state.folds.insert(heading)
73 h.editor.setFolds(state)
74 Self.pump(h)
75 let after = Self.topLine(h)
76 #expect(abs(after - before) <= 2, "top line moved from \(before) to \(after)")
77 }
78
79 @Test func aThemeChangeNearTheEndKeepsTheWindowWhereItWas() {
80 let h = Self.shownHarness(Self.longDocument(headings: 1500))
81 let heading = h.offset(of: "* Heading 1480\n")
82 h.editor.setCaret(heading)
83 h.textView.scrollRangeToVisible(NSRange(location: heading, length: 0))
84 Self.pump(h)
85 let before = Self.topLine(h)
86 #expect(before > 5000)
87
88 var spec = ThemeSpec.default
89 spec.light["heading-1"] = RGBA(hex: "#ff0000")
90 h.editor.themeSpec = spec
91 Self.pump(h)
92 h.drainStyling()
93 Self.pump(h)
94 let after = Self.topLine(h)
95 #expect(abs(after - before) <= 2, "top line moved from \(before) to \(after)")
96 }
97}