import CoreGraphics
import Foundation
import Testing
@testable import ParcelPlatform
struct InputValueTests {
@Test func descriptionAndIdentity() {
let key = HotKey(keyCode: 12, modifiers: [.shift, .command, .control, .option, .function])
#expect(key.description == "control+option+command+shift+function+key:12")
#expect(HotKey(keyCode: 0).description == "key:0")
#expect(Set([key, key]).count == 1)
#expect(HotKey.Modifiers(rawValue: .max) == [.hyper, .function])
}
@Test(arguments: Array(UInt32(0)...31))
func modifierRoundTrips(raw: UInt32) {
let modifiers = HotKey.Modifiers(rawValue: raw)
#expect(HotKey.Modifiers(eventFlags: modifiers.eventFlags) == modifiers)
#expect(HotKey.Modifiers(carbonModifiers: modifiers.carbonModifiers) == modifiers)
}
@Test func modifierNoiseAndMenuFlags() {
#expect(HotKey.Modifiers(eventFlags: [.maskAlphaShift, .maskNumericPad]) == [])
#expect(HotKey.Modifiers(menuModifiers: 0) == .command)
#expect(HotKey.Modifiers(menuModifiers: 7) == .hyper)
#expect(HotKey.Modifiers(menuModifiers: 8) == [])
#expect(HotKey.Modifiers(menuModifiers: 15) == [.shift, .option, .control])
#expect(HotKey.Modifiers(carbonModifiers: 0x100) == .command)
#expect(HotKey.Modifiers(eventFlags: .init(rawValue: 0x100000)) == .command)
}
@Test func routing() {
for modifiers: HotKey.Modifiers in [[], .command, .option, [.option, .shift]] {
#expect(HotKeyRoute.preferred(for: HotKey(keyCode: 0, modifiers: modifiers)) == .carbon)
}
#expect(HotKeyRoute.preferred(for: HotKey(keyCode: 0, modifiers: .hyper)) == .eventTap)
#expect(HotKeyRoute.preferred(for: HotKey(keyCode: 0, modifiers: .function)) == .carbon)
#expect(HotKeyRoute.afterCarbonRegistration(status: 0) == .carbon)
#expect(HotKeyRoute.afterCarbonRegistration(status: -9868) == .eventTap)
#expect(HotKeyRoute.afterCarbonRegistration(status: -50) == .eventTap)
}
@Test func repeatsAndMatching() {
let key = HotKey(keyCode: 12, modifiers: .option)
var state = HotKeyState(bindings: [key: false])
#expect(!state.down(HotKey(keyCode: 12), isRepeat: false).consume)
#expect(state.down(key, isRepeat: false).press == HotKeyPress(hotKey: key, isRepeat: false))
#expect(state.down(key, isRepeat: false).press == nil)
#expect(state.down(key, isRepeat: true).consume)
#expect(state.up(keyCode: 12) == true)
#expect(state.up(keyCode: 12) == false)
state.bindings[key] = true
#expect(state.down(key, isRepeat: false).press?.isRepeat == false)
#expect(state.down(key, isRepeat: true).press?.isRepeat == true)
state.reset()
#expect(state.down(key, isRepeat: false).press?.isRepeat == false)
state.remove(key)
#expect(!state.down(key, isRepeat: false).consume)
}
}
struct SymbolicHotKeyTests {
static let fixture = Data(
"""
AppleSymbolicHotKeys
79enabledvalue
typestandardparameters
655351238650752
81enabledvalue
typestandardparameters
655351248650752
118enabledvalue
typestandardparameters
4918262144
119enabled
200enabledvalue
typestandardparameters
65535655350
201enabledvalue
typemouseparameters
1
""".utf8)
@Test func parseFixture() throws {
let entries = try SymbolicHotKeys.parse(Self.fixture)
#expect(entries.map(\.id) == [79, 81, 118, 119, 200, 201])
#expect(entries[0].hotKey == HotKey(keyCode: 123, modifiers: [.control, .function]))
#expect(!entries[1].isEnabled)
#expect(entries[2].hotKey == HotKey(keyCode: 18, modifiers: .control))
#expect(entries[3].hotKey == nil)
#expect(entries[4].hotKey == nil)
#expect(entries[5].hotKey == nil)
}
@Test func binaryFixture() throws {
let plist = try PropertyListSerialization.propertyList(from: Self.fixture, format: nil)
let binary = try PropertyListSerialization.data(fromPropertyList: plist, format: .binary, options: 0)
#expect(try SymbolicHotKeys.parse(binary) == SymbolicHotKeys.parse(Self.fixture))
}
@Test(arguments: ["bad", "-79", "9223372036854775808", ""])
func malformedIDsAreReportedAndSkipped(id: String) throws {
let text = String(decoding: Self.fixture, as: UTF8.self)
let data = Data(text.replacingOccurrences(of: "79", with: "\(id)").utf8)
var reported: [String] = []
let entries = try SymbolicHotKeys.parse(data) { reported.append($0) }
#expect(reported == [id])
#expect(entries == (try SymbolicHotKeys.parse(Self.fixture)).filter { $0.id != 79 })
}
@Test(arguments: [
"broken",
"",
"enabledtrue",
"enabledvaluebroken",
"enabledvalue",
"enabledvaluetype1",
"enabledvaluetypestandard",
])
func malformedEntryShapesDoNotHideConflictsOrGuessDefaults(entry: String) throws {
try checkMalformedEntry(entry)
}
@Test(arguments: [
"",
"65535123",
"6553512386507520",
"655351288650752",
"65535-28650752",
"65535655368650752",
"65535123-1",
"655351234294967296",
"655351238650752",
"65535123.58650752",
"655358650752",
"655351239223372036854775808",
])
func malformedParametersAreReportedAndSkipped(parameters: String) throws {
try checkMalformedEntry(
"""
enabledvalue
typestandardparameters
\(parameters)
""")
}
private func checkMalformedEntry(_ entry: String) throws {
let data = Data(
"""
AppleSymbolicHotKeys
79\(entry)
118enabledvalue
typestandardparameters
4918262144
""".utf8)
let expected = [SymbolicHotKey(id: 118, isEnabled: true, hotKey: HotKey(keyCode: 18, modifiers: .control))]
let plist = try PropertyListSerialization.propertyList(from: data, format: nil)
let binary = try PropertyListSerialization.data(fromPropertyList: plist, format: .binary, options: 0)
for encoded in [data, binary] {
var reported: [String] = []
let entries = try SymbolicHotKeys.parse(encoded) { reported.append($0) }
#expect(reported == ["79"])
#expect(entries == expected)
let key = HotKey(keyCode: 18, modifiers: .control)
#expect(
ShortcutConflicts.detect(hotKeys: [key], symbolicHotKeys: entries).map(\.owner)
== [.system(symbolicID: 118)])
#expect(
ShortcutConflicts.suggestions(
for: HotKey(keyCode: 19, modifiers: .control), candidateKeyCodes: [18, 20],
hotKeys: [], symbolicHotKeys: entries) == [HotKey(keyCode: 20, modifiers: .control)])
#expect(!SymbolicHotKeys.spaceShortcut(.moveLeft, in: entries).isEnabled)
#expect(SymbolicHotKeys.spaceShortcut(.moveLeft, in: entries).hotKey == nil)
}
}
@Test func multipleMalformedEntriesAreEachReportedOnce() throws {
let text = String(decoding: Self.fixture, as: UTF8.self)
let data = Data(
text.replacingOccurrences(of: "123", with: "128")
.replacingOccurrences(of: "81", with: "bad").utf8)
var reported: [String] = []
let entries = try SymbolicHotKeys.parse(data) { reported.append($0) }
#expect(reported == ["79", "bad"])
#expect(entries == (try SymbolicHotKeys.parse(Self.fixture)).filter { $0.id != 79 && $0.id != 81 })
#expect(try SymbolicHotKeys.parse(data) == entries)
}
@Test(arguments: [
"invalid",
"",
"",
"AppleSymbolicHotKeys",
"AppleSymbolicHotKeysbroken",
])
func malformedDocumentsStillThrow(text: String) {
var reported: [String] = []
#expect(throws: (any Error).self) {
try SymbolicHotKeys.parse(Data(text.utf8)) { reported.append($0) }
}
#expect(reported.isEmpty)
}
@Test func emptyAndEntirelyMalformedDocuments() throws {
let empty = Data("AppleSymbolicHotKeys".utf8)
var reported: [String] = []
#expect(try SymbolicHotKeys.parse(empty) { reported.append($0) }.isEmpty)
#expect(reported.isEmpty)
let malformed = Data(
"""
AppleSymbolicHotKeys
79enabledvalue
81broken
""".utf8)
#expect(try SymbolicHotKeys.parse(malformed) { reported.append($0) }.isEmpty)
#expect(reported == ["79", "81"])
}
@Test(arguments: ["macOS-15.7.7", "macOS-26.6.1"])
func realMacOSFixturesPreserveConflicts(name: String) throws {
let url = try #require(
Bundle.module.url(forResource: name, withExtension: "plist", subdirectory: "Fixtures/SymbolicHotKeys"))
let data = try Data(contentsOf: url)
var reported: [String] = []
let entries = try SymbolicHotKeys.parse(data) { reported.append($0) }
#expect(reported.isEmpty)
#expect(entries.count == (name == "macOS-15.7.7" ? 28 : 19))
let left = HotKey(keyCode: 123, modifiers: .control)
let right = HotKey(keyCode: 124, modifiers: .control)
#expect(
ShortcutConflicts.detect(hotKeys: [left, right], symbolicHotKeys: entries).map(\.owner)
== [.system(symbolicID: 79), .system(symbolicID: 81)])
#expect(SymbolicHotKeys.spaceShortcut(.moveLeft, in: entries).hotKey?.keyCode == 123)
#expect(SymbolicHotKeys.spaceShortcut(.moveRight, in: entries).hotKey?.keyCode == 124)
#expect(!SymbolicHotKeys.spaceShortcut(.desktop(1), in: entries).isEnabled)
let plist = try PropertyListSerialization.propertyList(from: data, format: nil)
let binary = try PropertyListSerialization.data(fromPropertyList: plist, format: .binary, options: 0)
#expect(try SymbolicHotKeys.parse(binary) == entries)
let corrupted = Data(
String(decoding: data, as: UTF8.self)
.replacingOccurrences(of: "79", with: "bad").utf8)
let surviving = try SymbolicHotKeys.parse(corrupted) { reported.append($0) }
#expect(reported == ["bad"])
#expect(surviving == entries.filter { $0.id != 79 })
#expect(
ShortcutConflicts.detect(hotKeys: [left, right], symbolicHotKeys: surviving).map(\.owner)
== [.system(symbolicID: 81)])
}
@Test(arguments: [Int64(-1), 65535])
func unassignedKeyCodesDoNotGuessDefaults(code: Int64) throws {
let data = Data(
"""
AppleSymbolicHotKeys
79enabledvalue
typestandardparameters
65535\(code)0
""".utf8)
var reported: [String] = []
#expect(
try SymbolicHotKeys.parse(data) { reported.append($0) }
== [SymbolicHotKey(id: 79, isEnabled: true, hotKey: nil)])
#expect(reported.isEmpty)
}
// macOS 26 writes Space shortcuts still on their default chord as `enabled` with no value.
@Test func enabledEntriesWithoutAValueUseTheMacOSDefault() throws {
let data = Data(
"""
AppleSymbolicHotKeys
79enabled
82enabled
119enabled
300enabled
""".utf8)
let entries = try SymbolicHotKeys.parse(data)
#expect(entries.allSatisfy { $0.isEnabled })
#expect(entries[0].hotKey == HotKey(keyCode: 123, modifiers: [.control, .function]))
#expect(entries[1].hotKey == HotKey(keyCode: 124, modifiers: [.control, .shift, .function]))
#expect(entries[2].hotKey == HotKey(keyCode: 19, modifiers: .control))
#expect(entries[3].hotKey == nil)
let left = SymbolicHotKeys.spaceShortcut(.moveLeft, in: entries)
#expect(left.isEnabled && left.hotKey?.keyCode == 123)
}
@Test(arguments: [
(79, UInt16(123), UInt64(8_650_752)), (80, 123, 8_781_824),
(81, 124, 8_650_752), (82, 124, 8_781_824),
])
func defaultArrowsMatchWrittenValuesAndHardwareFlags(id: Int, code: UInt16, flags: UInt64) throws {
let omitted = Data(
"""
AppleSymbolicHotKeys
\(id)enabled
""".utf8)
let written = Data(
"""
AppleSymbolicHotKeys
\(id)enabledvalue
typestandardparameters
65535\(code)\(flags)
""".utf8)
let defaults = try SymbolicHotKeys.parse(omitted)
#expect(defaults == (try SymbolicHotKeys.parse(written)))
let hotKey = try #require(defaults.first?.hotKey)
let posted = try HotKeyPoster.events(for: hotKey)
for event in posted {
#expect(event.flags == CGEventFlags(rawValue: flags).union(.maskNumericPad))
#expect(event.getIntegerValueField(.keyboardEventKeycode) == Int64(code))
}
let binding = HotKey(keyCode: code, modifiers: hotKey.modifiers.subtracting(.function))
#expect(
ShortcutConflicts.detect(hotKeys: [binding], symbolicHotKeys: defaults).map(\.owner)
== [.system(symbolicID: id)])
#expect(
ShortcutConflicts.suggestions(
for: binding, candidateKeyCodes: [code], hotKeys: [],
symbolicHotKeys: defaults
).isEmpty)
}
@Test(arguments: [UInt16(123), 124, 125, 126])
func physicalFunctionArrowBindingsRemainDistinct(code: UInt16) {
let ordinary = HotKey(keyCode: code, modifiers: .control)
let physicalFunction = HotKey(keyCode: code, modifiers: [.control, .function])
let system = [SymbolicHotKey(id: 79, isEnabled: true, hotKey: physicalFunction)]
#expect(
ShortcutConflicts.detect(hotKeys: [ordinary, physicalFunction], symbolicHotKeys: system)
.map(\.owner) == [.system(symbolicID: 79)])
let otherCode: UInt16 = code == 123 ? 124 : 123
let source = HotKey(keyCode: otherCode, modifiers: [.control, .function])
#expect(
ShortcutConflicts.suggestions(
for: source, candidateKeyCodes: [code], hotKeys: [],
symbolicHotKeys: system) == [physicalFunction])
}
@Test func nonArrowFunctionBindingsRemainDistinct() {
let ordinary = HotKey(keyCode: 18, modifiers: .control)
let function = HotKey(keyCode: 18, modifiers: [.control, .function])
#expect(
ShortcutConflicts.detect(
hotKeys: [ordinary],
symbolicHotKeys: [
SymbolicHotKey(id: 118, isEnabled: true, hotKey: function)
]
).isEmpty)
}
@Test func spaceShortcuts() throws {
let entries = try SymbolicHotKeys.parse(Self.fixture)
let left = SymbolicHotKeys.spaceShortcut(.moveLeft, in: entries)
#expect(left.isEnabled)
#expect(left.hotKey == HotKey(keyCode: 123, modifiers: [.control, .function]))
let right = SymbolicHotKeys.spaceShortcut(.moveRight, in: entries)
#expect(!right.isEnabled)
#expect(right.hotKey?.keyCode == 124)
#expect(SymbolicHotKeys.spaceShortcut(.desktop(1), in: entries).isEnabled)
#expect(!SymbolicHotKeys.spaceShortcut(.desktop(2), in: entries).isEnabled)
#expect(SymbolicHotKeys.spaceShortcut(.desktop(0), in: entries).hotKey == nil)
}
@Test func conflictsAndSuggestions() throws {
let entries = try SymbolicHotKeys.parse(Self.fixture)
let key = HotKey(keyCode: 18, modifiers: .control)
let menu = MenuShortcut(pid: 42, path: ["File", "Open"], hotKey: key)
let conflicts = ShortcutConflicts.detect(
hotKeys: [key, key, HotKey(keyCode: 124, modifiers: .control)],
symbolicHotKeys: entries, menuShortcuts: [menu])
#expect(
conflicts.map(\.owner) == [
.system(symbolicID: 118), .application(pid: 42, menuPath: ["File", "Open"]), .binding(index: 1),
])
let suggestions = ShortcutConflicts.suggestions(
for: key, candidateKeyCodes: [18, 19, 19, 20, 123, 124, 65535],
hotKeys: [HotKey(keyCode: 19, modifiers: .control)], symbolicHotKeys: entries, menuShortcuts: [menu])
#expect(suggestions == [HotKey(keyCode: 20, modifiers: .control), HotKey(keyCode: 124, modifiers: .control)])
#expect(
ShortcutConflicts.suggestions(
for: key, candidateKeyCodes: [20, 21], hotKeys: [], symbolicHotKeys: [], limit: 1
).count == 1)
#expect(
ShortcutConflicts.suggestions(
for: key, candidateKeyCodes: [20], hotKeys: [], symbolicHotKeys: [], limit: 0
).isEmpty)
}
}