oficina / docs Public

Clone
git clone https://amadan.net/oficina/docs

Plain git — no account needed to clone.

Download

Download this file

1#if os(macOS)
2import AppKit
3import XCTest
4@testable import DocsKit
5
6@MainActor final class DocumentPageEditingTests: XCTestCase {
7 func testOptimisticAndMarkedEditsInvalidatePageOffsets() throws {
8 let nodes = (0..<100).map { index in
9 CRDTNode(id: "p\(index)", type: "paragraph", children: [CRDTNode(id: "t\(index)", type: "text", runs: [CRDTRun(insert: "A paragraph kept together when pages are recalculated.")])])
10 }
11 let view = DocsPagedTextView(frame: CGRect(x: 0, y: 0, width: 636, height: 800))
12 let storage = try XCTUnwrap(view.textStorage)
13 storage.setAttributedString(NativeTextProjection.render(CRDTProjection(children: nodes, canUndo: false, canRedo: false)))
14 view.paged = true
15 view.layout()
16 XCTAssertFalse(view.pagination.breakStarts.isEmpty)
17 let initial = view.pagination.breakStarts
18 let insertion = String(repeating: "Inserted text before the boundary. ", count: 20)
19 storage.replaceCharacters(in: NSRange(location: 0, length: 0), with: insertion)
20 assertBlockStarts(view, storage)
21 view.layout()
22 XCTAssertNotEqual(initial, view.pagination.breakStarts)
23 assertBlockStarts(view, storage)
24 view.setSelectedRange(NSRange(location: 0, length: 0))
25 view.setMarkedText("日本語", selectedRange: NSRange(location: 3, length: 0), replacementRange: NSRange(location: 0, length: 0))
26 assertBlockStarts(view, storage)
27 view.layout()
28 assertBlockStarts(view, storage)
29 view.unmarkText()
30 storage.replaceCharacters(in: NSRange(location: 0, length: 3 + insertion.utf16.count), with: "")
31 assertBlockStarts(view, storage)
32 view.layout()
33 XCTAssertEqual(view.pagination.breakStarts, initial)
34 XCTAssertEqual(storage.string, nodes.map(\.plainText).joined(separator: "\n") + "\n")
35 }
36
37 func testMetadataReconciliationNeverExposesUnpaginatedCaretGeometry() throws {
38 let nodes = (0..<1000).map { index in
39 CRDTNode(id: "p\(index)", type: "paragraph", children: [CRDTNode(id: "t\(index)", type: "text", runs: [CRDTRun(insert: "A paragraph kept together when pages are recalculated.")])])
40 }
41 let view = DocsPagedTextView(frame: CGRect(x: 0, y: 0, width: 636, height: 800))
42 let storage = try XCTUnwrap(view.textStorage)
43 storage.setAttributedString(NativeTextProjection.render(CRDTProjection(children: nodes, canUndo: false, canRedo: false)))
44 view.paged = true
45 view.layout()
46 let manager = try XCTUnwrap(view.layoutManager)
47 let container = try XCTUnwrap(view.textContainer as? DocsPageTextContainer)
48 XCTAssertGreaterThanOrEqual(view.pagination.breakStarts.count, 2)
49 let selected = storage.length - 3
50 view.setSelectedRange(NSRange(location: selected, length: 0))
51 func caretY() -> CGFloat {
52 manager.ensureLayout(for: container)
53 return manager.lineFragmentRect(forGlyphAt: manager.glyphIndexForCharacter(at: selected), effectiveRange: nil).minY
54 }
55 let before = caretY()
56 let start = ContinuousClock.now
57 for generation in 0..<10 {
58 storage.addAttribute(NSAttributedString.Key("MetadataOnly"), value: generation, range: NSRange(location: 0, length: storage.length))
59 XCTAssertEqual(caretY(), before, accuracy: 0.01, "AppKit can query the caret before the next view layout")
60 }
61 print("Ten metadata updates in a 1,000-paragraph document:", start.duration(to: .now))
62 view.layout()
63 XCTAssertEqual(caretY(), before, accuracy: 0.01)
64 XCTAssertEqual(view.selectedRange(), NSRange(location: selected, length: 0))
65 }
66
67 func testCaretBackgroundErasureStaysInsideRequestedRectangle() throws {
68 let view = DocsPagedTextView(frame: CGRect(x: 0, y: 0, width: 636, height: 800))
69 view.layout()
70 let bitmap = try XCTUnwrap(NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: 100, pixelsHigh: 100,
71 bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false,
72 colorSpaceName: .deviceRGB, bytesPerRow: 0, bitsPerPixel: 0))
73 let context = try XCTUnwrap(NSGraphicsContext(bitmapImageRep: bitmap))
74 NSGraphicsContext.saveGraphicsState()
75 defer { NSGraphicsContext.restoreGraphicsState() }
76 NSGraphicsContext.current = context
77 NSColor.red.setFill()
78 CGRect(x: 0, y: 0, width: 100, height: 100).fill()
79 view.drawBackground(in: CGRect(x: 40, y: 40, width: 20, height: 20))
80 XCTAssertEqual(try XCTUnwrap(bitmap.colorAt(x: 20, y: 20)).greenComponent, 0, accuracy: 0.01,
81 "Erasing one caret must not paint over neighboring glyphs")
82 XCTAssertGreaterThan(try XCTUnwrap(bitmap.colorAt(x: 50, y: 50)).greenComponent, 0.9)
83 }
84
85 func testTypingKeepsCaretInsidePageBoundsWithStableOrigin() throws {
86 let nodes = (0..<60).map { index in
87 CRDTNode(id: "p\(index)", type: "paragraph", children: [CRDTNode(id: "t\(index)", type: "text", runs: [CRDTRun(insert: String(repeating: "A paragraph that wraps across lines. ", count: 8))])])
88 }
89 let view = DocsTextView(frame: CGRect(x: 0, y: 0, width: 900, height: 800))
90 view.textStorage!.setAttributedString(NativeTextProjection.render(CRDTProjection(children: nodes, canUndo: false, canRedo: false)))
91 view.paged = true
92 view.layout()
93 view.setSelectedRange(NSRange(location: view.string.utf16.count - 1, length: 0))
94 for text in ["\n", "a", "\n", "b"] {
95 view.insertText(text, replacementRange: view.selectedRange())
96 view.layoutManager!.ensureLayout(for: view.textContainer!)
97 let glyph = view.layoutManager!.glyphIndexForCharacter(at: view.selectedRange().location)
98 let line = view.layoutManager!.lineFragmentRect(forGlyphAt: glyph, effectiveRange: nil)
99 XCTAssertEqual(view.textContainerOrigin, .zero)
100 XCTAssertGreaterThanOrEqual(view.bounds.maxY, line.maxY)
101 }
102 }
103
104 private func assertBlockStarts(_ view: DocsPagedTextView, _ storage: NSTextStorage) {
105 for location in view.pagination.breakStarts.keys {
106 var range = NSRange()
107 XCTAssertNotNil(storage.attribute(.docsTopBlock, at: location, effectiveRange: &range))
108 XCTAssertEqual(range.location, location, "A separator must never move into a paragraph")
109 }
110 }
111}
112#endif
113