|
| 1 | +// |
| 2 | +// AccessibilityTests.swift |
| 3 | +// CodeEditTextView |
| 4 | +// |
| 5 | +// Created by Khan Winter on 7/17/25. |
| 6 | +// |
| 7 | + |
| 8 | +import Testing |
| 9 | +import AppKit |
| 10 | +@testable import CodeEditTextView |
| 11 | + |
| 12 | +@MainActor |
| 13 | +@Suite |
| 14 | +struct AccessibilityTests { |
| 15 | + let textView: TextView |
| 16 | + let sampleText = "Line 1\nLine 2\nLine 3" |
| 17 | + |
| 18 | + init() { |
| 19 | + textView = TextView(string: sampleText) |
| 20 | + textView.frame = NSRect(x: 0, y: 0, width: 1000, height: 1000) |
| 21 | + textView.updateFrameIfNeeded() |
| 22 | + } |
| 23 | + |
| 24 | + // MARK: - Basic Accessibility Properties |
| 25 | + |
| 26 | + @Test |
| 27 | + func isAccessibilityElement() { |
| 28 | + #expect(textView.isAccessibilityElement()) |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + func isAccessibilityEnabled() { |
| 33 | + #expect(textView.isAccessibilityEnabled()) |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + func accessibilityLabel() { |
| 38 | + #expect(textView.accessibilityLabel() == "Text Editor") |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + func accessibilityRole() { |
| 43 | + #expect(textView.accessibilityRole() == .textArea) |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + func accessibilityValue() { |
| 48 | + #expect(textView.accessibilityValue() as? String == sampleText) |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + func setAccessibilityValue() { |
| 53 | + let newValue = "New content" |
| 54 | + textView.setAccessibilityValue(newValue) |
| 55 | + #expect(textView.string == newValue) |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + func setAccessibilityValueInvalidType() { |
| 60 | + let originalString = textView.string |
| 61 | + textView.setAccessibilityValue(42) |
| 62 | + #expect(textView.string == originalString) |
| 63 | + } |
| 64 | + |
| 65 | + // MARK: - Character and String Access |
| 66 | + |
| 67 | + @Test |
| 68 | + func accessibilityNumberOfCharacters() { |
| 69 | + #expect(textView.accessibilityNumberOfCharacters() == sampleText.count) |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + func accessibilityStringForRange() { |
| 74 | + let range = NSRange(location: 0, length: 6) |
| 75 | + let result = textView.accessibilityString(for: range) |
| 76 | + #expect(result == "Line 1") |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + func accessibilityStringForInvalidRange() { |
| 81 | + let range = NSRange(location: 100, length: 5) |
| 82 | + let result = textView.accessibilityString(for: range) |
| 83 | + #expect(result == nil) |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + func accessibilityRangeForCharacterIndex() { |
| 88 | + let range = textView.accessibilityRange(for: 0) |
| 89 | + #expect(range.location == 0) |
| 90 | + #expect(range.length == 1) |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + func accessibilityRangeForInvalidIndex() { |
| 95 | + let range = textView.accessibilityRange(for: 1000) |
| 96 | + #expect(range == .notFound) |
| 97 | + } |
| 98 | + |
| 99 | + // MARK: - Selection Tests |
| 100 | + |
| 101 | + @Test |
| 102 | + func accessibilitySelectedTextNoSelections() { |
| 103 | + textView.selectionManager.setSelectedRanges([]) |
| 104 | + #expect(textView.accessibilitySelectedText() == nil) |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + func accessibilitySelectedTextEmpty() { |
| 109 | + textView.selectionManager.setSelectedRange(.zero) |
| 110 | + #expect(textView.accessibilitySelectedText() == "") |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + func accessibilitySelectedText() { |
| 115 | + let range = NSRange(location: 0, length: 6) |
| 116 | + textView.selectionManager.setSelectedRange(range) |
| 117 | + #expect(textView.accessibilitySelectedText() == "Line 1") |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + func accessibilitySelectedTextRange() { |
| 122 | + let range = NSRange(location: 2, length: 4) |
| 123 | + textView.selectionManager.setSelectedRange(range) |
| 124 | + let selectedRange = textView.accessibilitySelectedTextRange() |
| 125 | + #expect(selectedRange.location == 2) |
| 126 | + #expect(selectedRange.length == 4) |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + func accessibilitySelectedTextRangeEmpty() { |
| 131 | + textView.selectionManager.setSelectedRange(.zero) |
| 132 | + let selectedRange = textView.accessibilitySelectedTextRange() |
| 133 | + #expect(selectedRange == .zero) |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + func setAccessibilitySelectedTextRange() { |
| 138 | + let range = NSRange(location: 7, length: 6) |
| 139 | + textView.setAccessibilitySelectedTextRange(range) |
| 140 | + #expect(textView.accessibilitySelectedTextRange() == range) |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + func accessibilitySelectedTextRanges() { |
| 145 | + let ranges = [ |
| 146 | + NSRange(location: 0, length: 4), |
| 147 | + NSRange(location: 7, length: 6) |
| 148 | + ] |
| 149 | + textView.selectionManager.setSelectedRanges(ranges) |
| 150 | + let selectedRanges = textView.accessibilitySelectedTextRanges()?.compactMap { $0 as? NSRange } |
| 151 | + #expect(selectedRanges?.count == 2) |
| 152 | + #expect(selectedRanges?.contains(ranges[0]) == true) |
| 153 | + #expect(selectedRanges?.contains(ranges[1]) == true) |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + func setAccessibilitySelectedTextRanges() { |
| 158 | + let ranges = [ |
| 159 | + NSRange(location: 0, length: 4) as NSValue, |
| 160 | + NSRange(location: 7, length: 6) as NSValue |
| 161 | + ] |
| 162 | + textView.setAccessibilitySelectedTextRanges(ranges) |
| 163 | + let selectedRanges = textView.accessibilitySelectedTextRanges() |
| 164 | + #expect(selectedRanges?.count == 2) |
| 165 | + } |
| 166 | + |
| 167 | + @Test |
| 168 | + func setAccessibilitySelectedTextRangesNil() { |
| 169 | + textView.setAccessibilitySelectedTextRanges(nil) |
| 170 | + let selectedRanges = textView.accessibilitySelectedTextRanges() |
| 171 | + #expect(selectedRanges?.isEmpty == true) |
| 172 | + } |
| 173 | + |
| 174 | + // MARK: - Line Navigation Tests |
| 175 | + |
| 176 | + @Test |
| 177 | + func accessibilityLineForIndex() { |
| 178 | + let lineIndex = textView.accessibilityLine(for: 0) |
| 179 | + #expect(lineIndex == 0) |
| 180 | + } |
| 181 | + |
| 182 | + @Test |
| 183 | + func accessibilityLineForIndexSecondLine() { |
| 184 | + let lineIndex = textView.accessibilityLine(for: 7) |
| 185 | + #expect(lineIndex == 1) |
| 186 | + } |
| 187 | + |
| 188 | + @Test |
| 189 | + func accessibilityLineForEndOfDocument() { |
| 190 | + let lineIndex = textView.accessibilityLine(for: textView.documentRange.max) |
| 191 | + #expect(lineIndex == 2) |
| 192 | + } |
| 193 | + |
| 194 | + @Test |
| 195 | + func accessibilityLineForInvalidIndex() { |
| 196 | + let lineIndex = textView.accessibilityLine(for: 1000) |
| 197 | + #expect(lineIndex == -1) |
| 198 | + } |
| 199 | + |
| 200 | + @Test |
| 201 | + func accessibilityRangeForLine() { |
| 202 | + let range = textView.accessibilityRange(forLine: 0) |
| 203 | + #expect(range.location == 0) |
| 204 | + #expect(range.length == 7) |
| 205 | + } |
| 206 | + |
| 207 | + @Test |
| 208 | + func accessibilityRangeForLineSecondLine() { |
| 209 | + let range = textView.accessibilityRange(forLine: 1) |
| 210 | + #expect(range.location == 7) |
| 211 | + #expect(range.length == 7) |
| 212 | + } |
| 213 | + |
| 214 | + @Test |
| 215 | + func accessibilityRangeForInvalidLine() { |
| 216 | + let range = textView.accessibilityRange(forLine: 100) |
| 217 | + #expect(range == .zero) |
| 218 | + } |
| 219 | + |
| 220 | + @Test |
| 221 | + func accessibilityRangeForNegativeLine() { |
| 222 | + let range = textView.accessibilityRange(forLine: -1) |
| 223 | + #expect(range == .zero) |
| 224 | + } |
| 225 | + |
| 226 | + @Test |
| 227 | + func accessibilityInsertionPointLineNumber() { |
| 228 | + textView.selectionManager.setSelectedRange(NSRange(location: 7, length: 0)) |
| 229 | + let lineNumber = textView.accessibilityInsertionPointLineNumber() |
| 230 | + #expect(lineNumber == 1) |
| 231 | + } |
| 232 | + |
| 233 | + @Test |
| 234 | + func accessibilityInsertionPointLineNumberEmptySelection() { |
| 235 | + textView.selectionManager.setSelectedRange(.zero) |
| 236 | + let lineNumber = textView.accessibilityInsertionPointLineNumber() |
| 237 | + #expect(lineNumber == 0) |
| 238 | + } |
| 239 | + |
| 240 | + @Test |
| 241 | + func accessibilityInsertionPointLineNumberNoSelection() { |
| 242 | + textView.selectionManager.setSelectedRanges([]) |
| 243 | + let lineNumber = textView.accessibilityInsertionPointLineNumber() |
| 244 | + #expect(lineNumber == -1) |
| 245 | + } |
| 246 | + |
| 247 | + // MARK: - Visible Range Tests |
| 248 | + |
| 249 | + @Test |
| 250 | + func accessibilityVisibleCharacterRange() { |
| 251 | + let visibleRange = textView.accessibilityVisibleCharacterRange() |
| 252 | + #expect(visibleRange != .notFound) |
| 253 | + } |
| 254 | + |
| 255 | + @Test |
| 256 | + func accessibilityVisibleCharacterRangeNoVisibleText() { |
| 257 | + let emptyTextView = TextView(string: "") |
| 258 | + let visibleRange = emptyTextView.accessibilityVisibleCharacterRange() |
| 259 | + #expect(visibleRange == .zero) |
| 260 | + } |
| 261 | + |
| 262 | + // MARK: - Point and Frame Tests |
| 263 | + |
| 264 | + @Test |
| 265 | + func accessibilityRangeForPoint() { |
| 266 | + let point = NSPoint(x: 10, y: 10) |
| 267 | + let range = textView.accessibilityRange(for: point) |
| 268 | + #expect(range.length == 0) |
| 269 | + } |
| 270 | + |
| 271 | + @Test |
| 272 | + func accessibilityRangeForInvalidPoint() { |
| 273 | + let point = NSPoint(x: -100, y: -100) |
| 274 | + let range = textView.accessibilityRange(for: point) |
| 275 | + #expect(range == .zero) |
| 276 | + } |
| 277 | + |
| 278 | + @Test |
| 279 | + func accessibilityFrameForRange() { |
| 280 | + let range = NSRange(location: 0, length: 6) |
| 281 | + let frame = textView.accessibilityFrame(for: range) |
| 282 | + #expect(frame.size.width > 0) |
| 283 | + #expect(frame.size.height > 0) |
| 284 | + } |
| 285 | + |
| 286 | + @Test |
| 287 | + func accessibilityFrameForEmptyRange() { |
| 288 | + let range = NSRange(location: 0, length: 0) |
| 289 | + let frame = textView.accessibilityFrame(for: range) |
| 290 | + #expect(frame.size.width >= 0) |
| 291 | + #expect(frame.size.height >= 0) |
| 292 | + } |
| 293 | + |
| 294 | + @Test |
| 295 | + func isAccessibilityFocusedWhenNotFirstResponder() { |
| 296 | + textView.window?.makeFirstResponder(nil) |
| 297 | + #expect(!textView.isAccessibilityFocused()) |
| 298 | + } |
| 299 | +} |
0 commit comments