| 1 | #if os(macOS) |
| 2 | import AppKit |
| 3 | import SwiftUI |
| 4 | import XCTest |
| 5 | @testable import DocsKit |
| 6 | |
| 7 | final class NativeClipboardInputTests: XCTestCase { |
| 8 | @MainActor func testDelegateTextReplacementAlsoRunsStructuralInputRule() { |
| 9 | let state = SurfaceState() |
| 10 | state.markPendingOnEdit = false |
| 11 | state.publish(SurfaceFixture.projection([SurfaceFixture.paragraph("p", "-")]), generation: 1, dirty: nil) |
| 12 | let text = state.text |
| 13 | text.setSelectedRange(NSRange(location: 1, length: 0)) |
| 14 | let allowed = state.surface.textView(text, shouldChangeTextIn: NSRange(location: 1, length: 0), replacementString: " ") |
| 15 | XCTAssertFalse(allowed) |
| 16 | XCTAssertEqual(text.string, "\n") |
| 17 | XCTAssertEqual(state.commands.last?["op"], .string("display_autoformat")) |
| 18 | XCTAssertEqual(state.commands.last?["generation"], .number(1)) |
| 19 | } |
| 20 | @MainActor func testDelayedDuplicateRuleSelectionNotificationDoesNotCancelReversal() { |
| 21 | let state = SurfaceState() |
| 22 | state.markPendingOnEdit = false |
| 23 | state.publish(SurfaceFixture.projection([SurfaceFixture.paragraph("p", "-")]), generation: 1, dirty: nil) |
| 24 | let text = state.text |
| 25 | text.setSelectedRange(NSRange(location: 1, length: 0)) |
| 26 | state.selection = NSRange(location: 1, length: 0) |
| 27 | text.insertText(" ", replacementRange: NSRange(location: NSNotFound, length: 0)) |
| 28 | state.surface.textViewDidChangeSelection(Notification(name: NSTextView.didChangeSelectionNotification, object: text)) |
| 29 | state.surface.textViewDidChangeSelection(Notification(name: NSTextView.didChangeSelectionNotification, object: text)) |
| 30 | XCTAssertNotNil(text.lastInputRule) |
| 31 | text.deleteBackward(nil) |
| 32 | XCTAssertEqual(text.string, "- \n") |
| 33 | XCTAssertEqual(state.commands.last?["op"], .string("display_revert_rule")) |
| 34 | } |
| 35 | @MainActor func testDirectDelegateBackspaceReversesRuleBeforeGenericDeletion() { |
| 36 | let state = SurfaceState() |
| 37 | state.markPendingOnEdit = false |
| 38 | state.publish(SurfaceFixture.projection([SurfaceFixture.paragraph("p", "-")]), generation: 1, dirty: nil) |
| 39 | let text = state.text |
| 40 | text.setSelectedRange(NSRange(location: 1, length: 0)) |
| 41 | state.selection = NSRange(location: 1, length: 0) |
| 42 | text.insertText(" ", replacementRange: NSRange(location: NSNotFound, length: 0)) |
| 43 | XCTAssertNotNil(text.lastInputRule) |
| 44 | let allowed = state.surface.textView(text, shouldChangeTextIn: NSRange(location: 0, length: 0), replacementString: "") |
| 45 | XCTAssertFalse(allowed) |
| 46 | XCTAssertEqual(text.string, "- \n") |
| 47 | XCTAssertEqual(state.commands.last?["op"], .string("display_revert_rule")) |
| 48 | } |
| 49 | @MainActor func testRichPasteOptimisticMarksMatchImmediateTyping() throws { |
| 50 | let blocks: [JSONValue] = [.object(["type": .string("paragraph"), "children": .array([.object(["type": .string("text"), "runs": .array([.object(["insert": .string("bold😀"), "marks": .object(["strong": .object([:])])])])])])])] |
| 51 | let initial = NSAttributedString(string: "AB\n") |
| 52 | let insertion = try XCTUnwrap(DocumentClipboardInsertion.attributed(blocks: blocks, in: initial, range: NSRange(location: 1, length: 0))) |
| 53 | let optimistic = NSMutableAttributedString(attributedString: initial) |
| 54 | optimistic.replaceCharacters(in: NSRange(location: 1, length: 0), with: insertion) |
| 55 | XCTAssertEqual(optimistic.string, "Abold😀B\n") |
| 56 | let command = try XCTUnwrap(TextEditMapping.displayCommand(in: optimistic, range: NSRange(location: 7, length: 0), replacement: "!")) |
| 57 | XCTAssertEqual(command["at"], .number(7)) |
| 58 | XCTAssertEqual(command["marks"], .object(["strong": .object([:])])) |
| 59 | } |
| 60 | @MainActor func testNativeSpaceRuleAndImmediateBackspaceRestorePrefix() { |
| 61 | let text = DocsTextView(frame: CGRect(x: 0, y: 0, width: 500, height: 700)) |
| 62 | text.textStorage?.setAttributedString(NSAttributedString(string: "-\n")) |
| 63 | text.setSelectedRange(NSRange(location: 1, length: 0)) |
| 64 | var commands: [[String: JSONValue]] = [] |
| 65 | text.ruleCommand = { commands.append($0); return true } |
| 66 | text.insertText(" ", replacementRange: NSRange(location: NSNotFound, length: 0)) |
| 67 | XCTAssertEqual(text.string, "\n") |
| 68 | XCTAssertEqual(text.selectedRange().location, 0) |
| 69 | XCTAssertEqual(commands.last?["op"], .string("display_autoformat")) |
| 70 | text.deleteBackward(nil) |
| 71 | XCTAssertEqual(text.string, "- \n") |
| 72 | XCTAssertEqual(commands.last?["op"], .string("display_revert_rule")) |
| 73 | let terminatorStyle = text.attributedString().attribute(.paragraphStyle, at: 2, effectiveRange: nil) as? NSParagraphStyle |
| 74 | XCTAssertTrue(terminatorStyle?.textLists.isEmpty == true, "The restored paragraph terminator must not continue an old native list") |
| 75 | } |
| 76 | @MainActor func testImmediateSecondReturnLeavesEmptyNativeListWithoutAddingText() { |
| 77 | let text = DocsTextView(frame: CGRect(x: 0, y: 0, width: 500, height: 700)) |
| 78 | text.textStorage?.setAttributedString(NSAttributedString(string: "-\n")) |
| 79 | text.setSelectedRange(NSRange(location: 1, length: 0)) |
| 80 | var commands: [[String: JSONValue]] = [] |
| 81 | text.ruleCommand = { commands.append($0); return true } |
| 82 | text.insertText(" ", replacementRange: NSRange(location: NSNotFound, length: 0)) |
| 83 | text.insertText("\n", replacementRange: NSRange(location: NSNotFound, length: 0)) |
| 84 | XCTAssertEqual(text.string, "\n") |
| 85 | XCTAssertEqual(commands.last?["op"], .string("display_exit_list")) |
| 86 | } |
| 87 | } |
| 88 | #endif |
| 89 | |