Tests/OrgEditorAppKitTests/MultiViewTests.swift

58f3b73e8ff337e5a15111fbb32c0d57cf2346da
orgstar/Tests/OrgEditorAppKitTests/MultiViewTests.swift history · blame · raw

110 lines · 4481 bytes

11 symbols in this file
  1import AppKit
  2import OrgCore
  3import OrgDocument
  4import OrgPresentation
  5import Testing
  6@testable import OrgEditorAppKit
  7
  8/// One editor, several text views on its storage: text, undo, folds and narrowing are the
  9/// editor's; the caret and the focus are each view's.
 10@MainActor
 11struct MultiViewTests {
 12    @MainActor struct Two {
 13        let editor: OrgEditor
 14        let second: NSTextView
 15        let windows: [NSWindow]
 16
 17        init(_ text: String) {
 18            _ = NSApplication.shared
 19            let editor = OrgEditor(document: DocumentState(bytes: Array(text.utf8)))
 20            let second = editor.addView()
 21            let windows = [editor.textView, second].map { view in
 22                let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 600, height: 800), styleMask: [.titled], backing: .buffered, defer: false)
 23                window.contentView = editor.makeScrollView(for: view)
 24                return window
 25            }
 26            windows[0].makeFirstResponder(editor.textView)
 27            editor.finishStyling()
 28            for view in editor.textViews { view.textLayoutManager!.ensureLayout(for: view.textLayoutManager!.documentRange) }
 29            self.editor = editor
 30            self.second = second
 31            self.windows = windows
 32        }
 33
 34        func lines(_ view: NSTextView) -> Int {
 35            let layout = view.textLayoutManager!
 36            layout.ensureLayout(for: layout.documentRange)
 37            var count = 0
 38            layout.enumerateTextLayoutFragments(from: layout.documentRange.location, options: [.ensuresLayout]) { _ in count += 1; return true }
 39            return count
 40        }
 41    }
 42
 43    @Test func typingInOneViewShowsInTheOtherAndInTheDocument() {
 44        let two = Two("* a\nbody\n")
 45        two.editor.textView.insertText("X", replacementRange: NSRange(location: 0, length: 0))
 46        #expect(two.second.string == "X* a\nbody\n")
 47        #expect(two.editor.document.text == "X* a\nbody\n")
 48    }
 49
 50    @Test func undoIsShared() {
 51        let two = Two("* a\nbody\n")
 52        two.editor.textView.insertText("X", replacementRange: NSRange(location: 0, length: 0))
 53        two.second.undoManager?.undo()
 54        #expect(two.second.string == "* a\nbody\n")
 55        #expect(two.editor.document.text == "* a\nbody\n")
 56    }
 57
 58    @Test func foldsAreTheEditors() {
 59        let two = Two("* a\nbody\n* b\n")
 60        let before = two.lines(two.second)
 61        two.editor.setCaret(0)
 62        _ = two.editor.cycleAtCaret()
 63        #expect(two.lines(two.second) < before)
 64        #expect(two.lines(two.editor.textView) == two.lines(two.second))
 65    }
 66
 67    @Test func commandsActOnTheFocusedView() {
 68        let two = Two("* a\nbody\n* b\nmore\n")
 69        two.windows[1].makeFirstResponder(two.second)
 70        #expect(two.editor.textView === two.second)
 71        two.second.setSelectedRange(NSRange(location: 9, length: 0))
 72        _ = two.editor.cycleAtCaret()
 73        #expect(two.editor.foldState.folds == [9])
 74    }
 75
 76    @Test func aDiskChangeMapsEveryViewsCaret() {
 77        let two = Two("* a\nbody\n")
 78        two.second.setSelectedRange(NSRange(location: 5, length: 0))
 79        _ = two.editor.diskChanged(to: Array("XY* a\nbody\n".utf8))
 80        #expect(two.second.selectedRange() == NSRange(location: 7, length: 0))
 81        #expect(two.second.string == "XY* a\nbody\n")
 82    }
 83
 84    @Test func narrowingMovesEveryViewsCaretIntoTheRegion() {
 85        let two = Two("* a\nbody\n* b\nmore\n")
 86        two.second.setSelectedRange(NSRange(location: 0, length: 0))
 87        two.editor.setCaret(9)
 88        two.editor.run("org.narrow.subtree")
 89        #expect(two.editor.narrowed?.lowerBound == 9)
 90        #expect(two.second.selectedRange().location >= 9)
 91    }
 92
 93    @Test func aViewTakingFocusLeavesHiddenText() {
 94        let two = Two("* a\nbody\n* b\nmore\n")
 95        two.second.setSelectedRange(NSRange(location: 5, length: 0))
 96        two.editor.setCaret(0)
 97        _ = two.editor.cycleAtCaret()
 98        two.windows[1].makeFirstResponder(two.second)
 99        #expect(two.editor.hidden.containing(two.second.selectedRange().location) == nil)
100    }
101
102    @Test func removingAViewDetachesItsLayoutManager() {
103        let two = Two("* a\n")
104        let storage = two.editor.textView.textLayoutManager!.textContentManager!
105        #expect(storage.textLayoutManagers.count == 2)
106        two.editor.removeView(two.second)
107        #expect(storage.textLayoutManagers.count == 1)
108        #expect(two.editor.textViews.count == 1)
109    }
110}