| 1 | #if os(macOS) |
| 2 | import AppKit |
| 3 | import SwiftUI |
| 4 | import XCTest |
| 5 | @testable import DocsKit |
| 6 | |
| 7 | /// The text view's render guards, now on `DocumentSurface`: a publication |
| 8 | /// never overwrites typing the model has not answered, nor marked text, |
| 9 | /// nor a generation that is not current. |
| 10 | final class NativeRenderTests: XCTestCase { |
| 11 | private func one(_ text: String, marks: [String: JSONValue]? = nil) -> CRDTProjection { |
| 12 | SurfaceFixture.projection([SurfaceFixture.paragraph("p", text, marks: marks)]) |
| 13 | } |
| 14 | @MainActor func testDelayedCandidateCannotOverwriteAcceptedNativeTyping() { |
| 15 | let state = SurfaceState() |
| 16 | let text = state.text |
| 17 | state.publish(one("a😀b"), generation: 1, dirty: nil) |
| 18 | XCTAssertEqual(text.string, "a😀b\n") |
| 19 | state.allowed = 2 |
| 20 | text.insertText("X", replacementRange: NSRange(location: 4, length: 0)) |
| 21 | XCTAssertTrue(state.pending) |
| 22 | XCTAssertEqual(text.string, "a😀bX\n") |
| 23 | XCTAssertEqual(state.commands.last?["generation"], .number(1)) |
| 24 | state.publish(one("older server view"), generation: 2, dirty: ["p"]) |
| 25 | XCTAssertEqual(text.string, "a😀bX\n") |
| 26 | XCTAssertEqual(state.acknowledgements, [1]) |
| 27 | state.pending = false; state.allowed = 3 |
| 28 | state.surface.flushIfDeferred() |
| 29 | XCTAssertEqual(text.string, "a😀bX\n") |
| 30 | state.selection = NSRange(location: 6, length: 0) |
| 31 | state.publish(one("a😀bX!"), generation: 3, dirty: ["p"]) |
| 32 | XCTAssertEqual(text.string, "a😀bX!\n") |
| 33 | XCTAssertEqual(text.selectedRange(), NSRange(location: 6, length: 0)) |
| 34 | XCTAssertEqual(state.acknowledgements, [1, 3]) |
| 35 | } |
| 36 | @MainActor func testMarkedTextSurvivesPeerPresentationAndCommitsOnce() { |
| 37 | let state = SurfaceState() |
| 38 | let text = state.text |
| 39 | state.publish(one("a😀b"), generation: 1, dirty: nil) |
| 40 | text.setMarkedText("に", selectedRange: NSRange(location: 1, length: 0), replacementRange: NSRange(location: 1, length: 2)) |
| 41 | XCTAssertTrue(text.hasMarkedText()) |
| 42 | XCTAssertTrue(state.commands.isEmpty) |
| 43 | state.allowed = 2 |
| 44 | state.publish(one("peer update"), generation: 2, dirty: ["p"]) |
| 45 | XCTAssertEqual(text.string, "aにb\n") |
| 46 | XCTAssertEqual(state.acknowledgements, [1]) |
| 47 | text.unmarkText() |
| 48 | XCTAssertEqual(state.commands.count, 1) |
| 49 | XCTAssertEqual(state.commands.first?["generation"], .number(1)) |
| 50 | XCTAssertEqual(state.commands.first?["at"], .number(1)) |
| 51 | XCTAssertEqual(state.commands.first?["length"], .number(2)) |
| 52 | XCTAssertEqual(state.commands.first?["text"], .string("に")) |
| 53 | } |
| 54 | |
| 55 | @MainActor func testAttributeOnlyRefreshPreservesNativeCaret() { |
| 56 | let state = SurfaceState() |
| 57 | let text = state.text |
| 58 | state.publish(one("abcdef"), generation: 1, dirty: nil) |
| 59 | text.setSelectedRange(NSRange(location: 4, length: 0)) |
| 60 | state.selection = NSRange(location: 0, length: 0) |
| 61 | let before = text.textStorage?.attribute(.font, at: 0, effectiveRange: nil) as? NSFont |
| 62 | state.publish(one("abcdef", marks: ["strong": .object([:])]), generation: 1, dirty: ["p"]) |
| 63 | XCTAssertEqual(text.selectedRange(), NSRange(location: 4, length: 0)) |
| 64 | XCTAssertNotEqual(text.textStorage?.attribute(.font, at: 0, effectiveRange: nil) as? NSFont, before) |
| 65 | } |
| 66 | } |
| 67 | #endif |
| 68 | |