|
| 1 | +// |
| 2 | +// NSViewFocusTests.swift |
| 3 | +// TableProTests |
| 4 | +// |
| 5 | + |
| 6 | +import AppKit |
| 7 | +import Testing |
| 8 | +@testable import TablePro |
| 9 | + |
| 10 | +@Suite("NSView+Focus") |
| 11 | +struct NSViewFocusTests { |
| 12 | + @Test("Returns nil for empty container view") |
| 13 | + func emptyView() { |
| 14 | + let container = NSView(frame: .zero) |
| 15 | + #expect(container.firstEditableTextField() == nil) |
| 16 | + } |
| 17 | + |
| 18 | + @Test("Finds direct editable text field") |
| 19 | + func directTextField() { |
| 20 | + let textField = NSTextField(frame: .zero) |
| 21 | + #expect(textField.firstEditableTextField() === textField) |
| 22 | + } |
| 23 | + |
| 24 | + @Test("Finds editable text field in subviews") |
| 25 | + func nestedTextField() { |
| 26 | + let container = NSView(frame: .zero) |
| 27 | + let child = NSView(frame: .zero) |
| 28 | + let textField = NSTextField(frame: .zero) |
| 29 | + child.addSubview(textField) |
| 30 | + container.addSubview(child) |
| 31 | + #expect(container.firstEditableTextField() === textField) |
| 32 | + } |
| 33 | + |
| 34 | + @Test("Skips non-editable text field") |
| 35 | + func skipsNonEditable() { |
| 36 | + let container = NSView(frame: .zero) |
| 37 | + let label = NSTextField(labelWithString: "Label") |
| 38 | + container.addSubview(label) |
| 39 | + #expect(container.firstEditableTextField() == nil) |
| 40 | + } |
| 41 | + |
| 42 | + @Test("Returns first editable text field in depth-first order") |
| 43 | + func depthFirstOrder() { |
| 44 | + let container = NSView(frame: .zero) |
| 45 | + let first = NSTextField(frame: .zero) |
| 46 | + let second = NSTextField(frame: .zero) |
| 47 | + container.addSubview(first) |
| 48 | + container.addSubview(second) |
| 49 | + |
| 50 | + let found = container.firstEditableTextField() |
| 51 | + #expect(found === first) |
| 52 | + } |
| 53 | + |
| 54 | + @Test("Finds editable text field among mixed subviews") |
| 55 | + func mixedSubviews() { |
| 56 | + let container = NSView(frame: .zero) |
| 57 | + let button = NSButton(frame: .zero) |
| 58 | + let label = NSTextField(labelWithString: "Label") |
| 59 | + let editable = NSTextField(frame: .zero) |
| 60 | + |
| 61 | + container.addSubview(button) |
| 62 | + container.addSubview(label) |
| 63 | + container.addSubview(editable) |
| 64 | + |
| 65 | + #expect(container.firstEditableTextField() === editable) |
| 66 | + } |
| 67 | + |
| 68 | + @Test("Returns nil when only non-editable text fields exist") |
| 69 | + func onlyLabels() { |
| 70 | + let container = NSView(frame: .zero) |
| 71 | + let label1 = NSTextField(labelWithString: "A") |
| 72 | + let label2 = NSTextField(labelWithString: "B") |
| 73 | + container.addSubview(label1) |
| 74 | + container.addSubview(label2) |
| 75 | + #expect(container.firstEditableTextField() == nil) |
| 76 | + } |
| 77 | + |
| 78 | + @Test("Finds deeply nested text field") |
| 79 | + func deeplyNested() { |
| 80 | + let root = NSView(frame: .zero) |
| 81 | + var current = root |
| 82 | + for _ in 0 ..< 5 { |
| 83 | + let child = NSView(frame: .zero) |
| 84 | + current.addSubview(child) |
| 85 | + current = child |
| 86 | + } |
| 87 | + let textField = NSTextField(frame: .zero) |
| 88 | + current.addSubview(textField) |
| 89 | + |
| 90 | + #expect(root.firstEditableTextField() === textField) |
| 91 | + } |
| 92 | +} |
0 commit comments