AlertSoundTests.swift (2503B)
1 // SPDX-License-Identifier: GPL-2.0-only 2 import AVFoundation 3 import RemoraDeco 4 import Testing 5 @testable import OpenMerou 6 7 /// Which sound plays when: the rules, and the bundled tones. (That the sound is really heard with the 8 /// ring/silent switch on silent can only be checked on an iPhone.) 9 struct AlertSoundTests { 10 @Test func leakAlarmSoundsUntilSilenced() { 11 #expect(AlertPolicy.leakSound(leaking: true, silenced: false) == .alarm) 12 #expect(AlertPolicy.leakSound(leaking: true, silenced: true) == nil) 13 #expect(AlertPolicy.leakSound(leaking: false, silenced: false) == nil) 14 } 15 16 @Test func everyCueHasItsSound() { 17 func sound(_ cue: DiveCue) -> AlertSound? { AlertPolicy.sound(for: cue, leaking: false, alarmSilenced: false) } 18 #expect(sound(.alarm) == .alarm) 19 #expect(sound(.depthLost) == .alarm) 20 #expect(sound(.warning) == .notice) 21 #expect(sound(.safetyStopStarted) == .notice) 22 #expect(sound(.safetyStopDone) == .notice) 23 #expect(sound(.diveSaved) == nil) 24 } 25 26 @Test func firstButtonPressSilencesTheAlarmButNotTheNotices() { 27 func sound(_ cue: DiveCue) -> AlertSound? { AlertPolicy.sound(for: cue, leaking: false, alarmSilenced: true) } 28 #expect(sound(.alarm) == nil) 29 #expect(sound(.depthLost) == nil) 30 #expect(sound(.warning) == .notice) 31 #expect(sound(.safetyStopStarted) == .notice) 32 } 33 34 @Test func leakAlarmHasPriorityOverEveryDiveSound() { 35 for cue in DiveCue.allCases { 36 #expect(AlertPolicy.sound(for: cue, leaking: true, alarmSilenced: false) == nil) 37 } 38 } 39 40 @Test func tonesAreBundledAndPlayable() throws { 41 for sound in AlertSound.allCases { 42 let url = try #require(sound.url) 43 let player = try AVAudioPlayer(contentsOf: url) 44 #expect(player.duration > 0.3 && player.duration < 1) // the leak alarm repeats every second 45 } 46 } 47 48 /// The category that ignores the ring/silent switch and still lets the camera record sound. 49 @MainActor @Test func audioSessionIsPlayAndRecordOnTheSpeaker() { 50 #expect(AlertPlayer.category == .playAndRecord) 51 #expect(AlertPlayer.options.contains(.defaultToSpeaker)) 52 } 53 54 @MainActor @Test func playerRemembersWhatItWasAskedToPlay() { 55 let player = AlertPlayer() 56 player.prepare() 57 #expect(player.lastPlayed == nil) 58 player.play(.notice) 59 #expect(player.lastPlayed == .notice) 60 } 61 }