|
| 1 | +import Virtualization |
| 2 | +import XCTest |
| 3 | +@testable import tart |
| 4 | + |
| 5 | +final class InputDeviceConfigurationTests: XCTestCase { |
| 6 | + func testLinuxUSBInputsCanBeDisabled() { |
| 7 | + let configuration = VZVirtualMachineConfiguration() |
| 8 | + |
| 9 | + VM.configureInputDevices(configuration, platform: Linux()) |
| 10 | + XCTAssertEqual(configuration.keyboards.count, 1) |
| 11 | + XCTAssertTrue(configuration.keyboards.contains { $0 is VZUSBKeyboardConfiguration }) |
| 12 | + XCTAssertEqual(configuration.pointingDevices.count, 1) |
| 13 | + XCTAssertTrue(configuration.pointingDevices.contains { $0 is VZUSBScreenCoordinatePointingDeviceConfiguration }) |
| 14 | + |
| 15 | + VM.configureInputDevices(configuration, platform: Linux(), noUSBAccessories: true) |
| 16 | + XCTAssertTrue(configuration.keyboards.isEmpty) |
| 17 | + XCTAssertTrue(configuration.pointingDevices.isEmpty) |
| 18 | + |
| 19 | + VM.configureInputDevices(configuration, platform: Linux(), noUSBAccessories: true, noTrackpad: true) |
| 20 | + XCTAssertTrue(configuration.keyboards.isEmpty) |
| 21 | + XCTAssertTrue(configuration.pointingDevices.isEmpty) |
| 22 | + } |
| 23 | + |
| 24 | + #if arch(arm64) |
| 25 | + func testMacOS13RetainsItsNativeTrackpad() { |
| 26 | + let platform = MacInputPlatform(nativeKeyboard: false) |
| 27 | + let configuration = VZVirtualMachineConfiguration() |
| 28 | + |
| 29 | + VM.configureInputDevices(configuration, platform: platform) |
| 30 | + XCTAssertEqual(configuration.keyboards.count, 1) |
| 31 | + XCTAssertEqual(configuration.pointingDevices.count, 2) |
| 32 | + |
| 33 | + VM.configureInputDevices(configuration, platform: platform, noUSBAccessories: true) |
| 34 | + XCTAssertTrue(configuration.keyboards.isEmpty) |
| 35 | + XCTAssertEqual(configuration.pointingDevices.count, 1) |
| 36 | + XCTAssertTrue(configuration.pointingDevices.contains { $0 is VZMacTrackpadConfiguration }) |
| 37 | + } |
| 38 | + |
| 39 | + func testMacOS14RetainsBothNativeInputs() throws { |
| 40 | + guard #available(macOS 14, *) else { |
| 41 | + throw XCTSkip("Mac keyboards require macOS 14") |
| 42 | + } |
| 43 | + |
| 44 | + let configuration = VZVirtualMachineConfiguration() |
| 45 | + VM.configureInputDevices(configuration, platform: MacInputPlatform(nativeKeyboard: true), noUSBAccessories: true) |
| 46 | + |
| 47 | + XCTAssertEqual(configuration.keyboards.count, 1) |
| 48 | + XCTAssertTrue(configuration.keyboards.contains { $0 is VZMacKeyboardConfiguration }) |
| 49 | + XCTAssertEqual(configuration.pointingDevices.count, 1) |
| 50 | + XCTAssertTrue(configuration.pointingDevices.contains { $0 is VZMacTrackpadConfiguration }) |
| 51 | + } |
| 52 | + |
| 53 | + func testInputFlagsStillSelectTheExpectedDevices() throws { |
| 54 | + guard #available(macOS 14, *) else { |
| 55 | + throw XCTSkip("Mac keyboards require macOS 14") |
| 56 | + } |
| 57 | + |
| 58 | + let platform = MacInputPlatform(nativeKeyboard: true) |
| 59 | + for noUSBAccessories in [false, true] { |
| 60 | + for noKeyboard in [false, true] { |
| 61 | + for noPointer in [false, true] { |
| 62 | + for noTrackpad in [false, true] { |
| 63 | + let configuration = VZVirtualMachineConfiguration() |
| 64 | + VM.configureInputDevices( |
| 65 | + configuration, |
| 66 | + platform: platform, |
| 67 | + noUSBAccessories: noUSBAccessories, |
| 68 | + noTrackpad: noTrackpad, |
| 69 | + noPointer: noPointer, |
| 70 | + noKeyboard: noKeyboard |
| 71 | + ) |
| 72 | + |
| 73 | + XCTAssertEqual(configuration.keyboards.contains { $0 is VZUSBKeyboardConfiguration }, !noUSBAccessories && !noKeyboard) |
| 74 | + XCTAssertEqual(configuration.keyboards.contains { $0 is VZMacKeyboardConfiguration }, !noKeyboard) |
| 75 | + XCTAssertEqual(configuration.pointingDevices.contains { $0 is VZUSBScreenCoordinatePointingDeviceConfiguration }, !noUSBAccessories && !noPointer) |
| 76 | + XCTAssertEqual(configuration.pointingDevices.contains { $0 is VZMacTrackpadConfiguration }, !noPointer && !noTrackpad) |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + func testSuspendableFallbackCannotReintroduceUSBInputs() { |
| 84 | + let configuration = VZVirtualMachineConfiguration() |
| 85 | + let platform = MacInputPlatform(nativeKeyboard: false) |
| 86 | + VM.configureInputDevices(configuration, platform: platform, suspendable: true) |
| 87 | + XCTAssertEqual(configuration.keyboards.count, 1) |
| 88 | + XCTAssertEqual(configuration.pointingDevices.count, 2) |
| 89 | + |
| 90 | + VM.configureInputDevices( |
| 91 | + configuration, |
| 92 | + platform: platform, |
| 93 | + suspendable: true, |
| 94 | + noUSBAccessories: true |
| 95 | + ) |
| 96 | + |
| 97 | + XCTAssertTrue(configuration.keyboards.isEmpty) |
| 98 | + XCTAssertEqual(configuration.pointingDevices.count, 1) |
| 99 | + XCTAssertTrue(configuration.pointingDevices.contains { $0 is VZMacTrackpadConfiguration }) |
| 100 | + } |
| 101 | + #endif |
| 102 | +} |
| 103 | + |
| 104 | +#if arch(arm64) |
| 105 | + // Model macOS 13 and 14 input availability without requiring a second host. |
| 106 | + private struct MacInputPlatform: PlatformSuspendable { |
| 107 | + var nativeKeyboard: Bool |
| 108 | + |
| 109 | + func os() -> OS { .darwin } |
| 110 | + |
| 111 | + func bootLoader(nvramURL: URL) throws -> VZBootLoader { |
| 112 | + try Linux().bootLoader(nvramURL: nvramURL) |
| 113 | + } |
| 114 | + |
| 115 | + func platform(nvramURL: URL, needsNestedVirtualization: Bool) throws -> VZPlatformConfiguration { |
| 116 | + try Linux().platform(nvramURL: nvramURL, needsNestedVirtualization: needsNestedVirtualization) |
| 117 | + } |
| 118 | + |
| 119 | + func graphicsDevice(vmConfig: VMConfig) -> VZGraphicsDeviceConfiguration { |
| 120 | + Linux().graphicsDevice(vmConfig: vmConfig) |
| 121 | + } |
| 122 | + |
| 123 | + func keyboards(noUSB: Bool) -> [VZKeyboardConfiguration] { |
| 124 | + var devices: [VZKeyboardConfiguration] = noUSB ? [] : [VZUSBKeyboardConfiguration()] |
| 125 | + if nativeKeyboard, #available(macOS 14, *) { |
| 126 | + devices.append(VZMacKeyboardConfiguration()) |
| 127 | + } |
| 128 | + return devices |
| 129 | + } |
| 130 | + |
| 131 | + func pointingDevices(noUSB: Bool) -> [VZPointingDeviceConfiguration] { |
| 132 | + var devices: [VZPointingDeviceConfiguration] = noUSB ? [] : [VZUSBScreenCoordinatePointingDeviceConfiguration()] |
| 133 | + devices.append(VZMacTrackpadConfiguration()) |
| 134 | + return devices |
| 135 | + } |
| 136 | + |
| 137 | + func pointingDevicesSimplified(noUSB: Bool) -> [VZPointingDeviceConfiguration] { |
| 138 | + noUSB ? [] : [VZUSBScreenCoordinatePointingDeviceConfiguration()] |
| 139 | + } |
| 140 | + |
| 141 | + func keyboardsSuspendable(noUSB: Bool) -> [VZKeyboardConfiguration] { |
| 142 | + if nativeKeyboard, #available(macOS 14, *) { |
| 143 | + return [VZMacKeyboardConfiguration()] |
| 144 | + } |
| 145 | + return keyboards(noUSB: noUSB) |
| 146 | + } |
| 147 | + |
| 148 | + func pointingDevicesSuspendable(noUSB: Bool) -> [VZPointingDeviceConfiguration] { |
| 149 | + nativeKeyboard ? [VZMacTrackpadConfiguration()] : pointingDevices(noUSB: noUSB) |
| 150 | + } |
| 151 | + } |
| 152 | +#endif |
0 commit comments