import AVFoundation import XCTest @testable import CallsKit /// The Mic Mode row is only shown on iPhone, but what it says is plain /// logic over two values, and it is held here with a stand-in for /// AVCaptureDevice, whose modes a test cannot set. @MainActor final class MicrophoneModeTests: XCTestCase { final class FakeModes: MicrophoneModeSource { var preferred: AVCaptureDevice.MicrophoneMode = .standard var active: AVCaptureDevice.MicrophoneMode = .standard var changed: (@Sendable () -> Void)? var pickerShown = 0 func showPicker() { pickerShown += 1 } } func testTheModesAreCalledWhatControlCenterCallsThem() { XCTAssertEqual(MicrophoneModes.name(.standard), "Standard") XCTAssertEqual(MicrophoneModes.name(.voiceIsolation), "Voice Isolation") XCTAssertEqual(MicrophoneModes.name(.wideSpectrum), "Wide Spectrum") } func testAnOverriddenChoiceNamesBothAndLeadsWithTheTruth() { // Saying "Voice Isolation" while the route runs Standard would be a // claim about audio the call is not getting. XCTAssertEqual(MicrophoneModes.summary(preferred: .voiceIsolation, active: .voiceIsolation), "Voice Isolation") let overridden = MicrophoneModes.summary(preferred: .voiceIsolation, active: .standard) XCTAssertTrue(overridden.hasPrefix("Standard"), overridden) XCTAssertTrue(overridden.contains("Voice Isolation is chosen"), overridden) } func testTheRowFollowsTheSystemWhenItSaysSomethingChanged() async { let fake = FakeModes() let modes = MicrophoneModes(source: fake) XCTAssertEqual(modes.summary, "Standard") fake.preferred = .voiceIsolation fake.active = .voiceIsolation fake.changed?() // The change is delivered through a main-actor hop, as a KVO callback // from any thread would be. for _ in 0..<10 where modes.active != .voiceIsolation { await Task.yield() } XCTAssertEqual(modes.preferred, .voiceIsolation) XCTAssertEqual(modes.summary, "Voice Isolation") } func testChangingTheModeIsTheSystemPickersJob() { // There is no setter to call: the button only opens Apple's picker. let fake = FakeModes() let modes = MicrophoneModes(source: fake) modes.showPicker() XCTAssertEqual(fake.pickerShown, 1) XCTAssertEqual(modes.active, .standard) } }