| 1 | import AVFoundation |
| 2 | import XCTest |
| 3 | |
| 4 | @testable import CallsKit |
| 5 | |
| 6 | /// The Mic Mode row is only shown on iPhone, but what it says is plain |
| 7 | /// logic over two values, and it is held here with a stand-in for |
| 8 | /// AVCaptureDevice, whose modes a test cannot set. |
| 9 | @MainActor final class MicrophoneModeTests: XCTestCase { |
| 10 | final class FakeModes: MicrophoneModeSource { |
| 11 | var preferred: AVCaptureDevice.MicrophoneMode = .standard |
| 12 | var active: AVCaptureDevice.MicrophoneMode = .standard |
| 13 | var changed: (@Sendable () -> Void)? |
| 14 | var pickerShown = 0 |
| 15 | func showPicker() { pickerShown += 1 } |
| 16 | } |
| 17 | |
| 18 | func testTheModesAreCalledWhatControlCenterCallsThem() { |
| 19 | XCTAssertEqual(MicrophoneModes.name(.standard), "Standard") |
| 20 | XCTAssertEqual(MicrophoneModes.name(.voiceIsolation), "Voice Isolation") |
| 21 | XCTAssertEqual(MicrophoneModes.name(.wideSpectrum), "Wide Spectrum") |
| 22 | } |
| 23 | |
| 24 | func testAnOverriddenChoiceNamesBothAndLeadsWithTheTruth() { |
| 25 | // Saying "Voice Isolation" while the route runs Standard would be a |
| 26 | // claim about audio the call is not getting. |
| 27 | XCTAssertEqual(MicrophoneModes.summary(preferred: .voiceIsolation, active: .voiceIsolation), "Voice Isolation") |
| 28 | let overridden = MicrophoneModes.summary(preferred: .voiceIsolation, active: .standard) |
| 29 | XCTAssertTrue(overridden.hasPrefix("Standard"), overridden) |
| 30 | XCTAssertTrue(overridden.contains("Voice Isolation is chosen"), overridden) |
| 31 | } |
| 32 | |
| 33 | func testTheRowFollowsTheSystemWhenItSaysSomethingChanged() async { |
| 34 | let fake = FakeModes() |
| 35 | let modes = MicrophoneModes(source: fake) |
| 36 | XCTAssertEqual(modes.summary, "Standard") |
| 37 | |
| 38 | fake.preferred = .voiceIsolation |
| 39 | fake.active = .voiceIsolation |
| 40 | fake.changed?() |
| 41 | // The change is delivered through a main-actor hop, as a KVO callback |
| 42 | // from any thread would be. |
| 43 | for _ in 0..<10 where modes.active != .voiceIsolation { await Task.yield() } |
| 44 | XCTAssertEqual(modes.preferred, .voiceIsolation) |
| 45 | XCTAssertEqual(modes.summary, "Voice Isolation") |
| 46 | } |
| 47 | |
| 48 | func testChangingTheModeIsTheSystemPickersJob() { |
| 49 | // There is no setter to call: the button only opens Apple's picker. |
| 50 | let fake = FakeModes() |
| 51 | let modes = MicrophoneModes(source: fake) |
| 52 | modes.showPicker() |
| 53 | XCTAssertEqual(fake.pickerShown, 1) |
| 54 | XCTAssertEqual(modes.active, .standard) |
| 55 | } |
| 56 | } |
| 57 | |