|
| 1 | +// |
| 2 | +// PrecedingKeyDownClaimTests.swift |
| 3 | +// TableProEditorKitTests |
| 4 | +// |
| 5 | + |
| 6 | +import AppKit |
| 7 | +import Carbon.HIToolbox |
| 8 | +@testable import TableProEditorKit |
| 9 | +import TableProTextEngine |
| 10 | +import Testing |
| 11 | + |
| 12 | +@MainActor |
| 13 | +private final class RecordingCoordinator: TextViewCoordinator { |
| 14 | + private(set) var seenKeyCodes: [Int] = [] |
| 15 | + |
| 16 | + func prepareCoordinator(controller: TextViewController) { } |
| 17 | + |
| 18 | + func textViewShouldClaimKeyDown(controller: TextViewController, event: NSEvent) -> NSEvent? { |
| 19 | + seenKeyCodes.append(Int(event.keyCode)) |
| 20 | + return event |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +/// Serialized because the claim is one static for every editor in the process. |
| 25 | +@Suite("The app-wide claim runs ahead of every editor link", .serialized) |
| 26 | +@MainActor |
| 27 | +internal struct PrecedingKeyDownClaimTests { |
| 28 | + @Test("A claimed key reaches neither the coordinators nor the text") |
| 29 | + func claimedKeyStopsTheChain() throws { |
| 30 | + TextViewController.precedingKeyDownClaim = { $0.keyCode == UInt16(kVK_Tab) } |
| 31 | + defer { TextViewController.precedingKeyDownClaim = nil } |
| 32 | + let (window, editor) = Mock.focusedTextViewController(string: "SELECT 1\nFROM t") |
| 33 | + editor.setCursorPositions([CursorPosition(range: NSRange(location: 0, length: 12))]) |
| 34 | + let coordinator = RecordingCoordinator() |
| 35 | + editor.textCoordinators = [WeakCoordinator(coordinator)] |
| 36 | + let tab = try #require(Mock.keyDown(keyCode: kVK_Tab, characters: "\t", in: window)) |
| 37 | + |
| 38 | + #expect(editor.claimKeyDown(tab, textViewHasFocus: true, findPanelHasFocus: false) == nil) |
| 39 | + #expect(coordinator.seenKeyCodes.isEmpty) |
| 40 | + #expect(editor.textView.string == "SELECT 1\nFROM t") |
| 41 | + } |
| 42 | + |
| 43 | + /// The find field holds focus instead of the text view, and its own Escape closes the panel. A |
| 44 | + /// Control-Tab held open must still get that Escape first. |
| 45 | + @Test("A claimed Escape does not close a focused find panel") |
| 46 | + func claimedEscapeBeatsTheFindPanel() throws { |
| 47 | + TextViewController.precedingKeyDownClaim = { $0.keyCode == UInt16(kVK_Escape) } |
| 48 | + defer { TextViewController.precedingKeyDownClaim = nil } |
| 49 | + let (window, editor) = Mock.focusedTextViewController(string: "SELECT ") |
| 50 | + let finder = try #require(editor.findViewController) |
| 51 | + finder.showFindPanel(animated: false) |
| 52 | + defer { finder.hideFindPanel(animated: false) } |
| 53 | + let escape = try #require(Mock.keyDown(keyCode: kVK_Escape, characters: "\u{1b}", in: window)) |
| 54 | + |
| 55 | + #expect(editor.claimKeyDown(escape, textViewHasFocus: false, findPanelHasFocus: true) == nil) |
| 56 | + #expect(finder.viewModel.isShowingFindPanel) |
| 57 | + } |
| 58 | + |
| 59 | + @Test("An unclaimed key goes down the chain as before") |
| 60 | + func unclaimedKeyContinues() throws { |
| 61 | + TextViewController.precedingKeyDownClaim = { _ in false } |
| 62 | + defer { TextViewController.precedingKeyDownClaim = nil } |
| 63 | + let (window, editor) = Mock.focusedTextViewController(string: "SELECT ") |
| 64 | + let coordinator = RecordingCoordinator() |
| 65 | + editor.textCoordinators = [WeakCoordinator(coordinator)] |
| 66 | + let escape = try #require(Mock.keyDown(keyCode: kVK_Escape, characters: "\u{1b}", in: window)) |
| 67 | + |
| 68 | + _ = editor.claimKeyDown(escape, textViewHasFocus: true, findPanelHasFocus: false) |
| 69 | + |
| 70 | + #expect(coordinator.seenKeyCodes == [kVK_Escape]) |
| 71 | + } |
| 72 | +} |
0 commit comments