Skip to content

Audio Manager Testing

Jay Thakkar edited this page Sep 11, 2024 · 4 revisions

Overview

The AudioManager is responsible for controlling all audio-related functionalities in the game. This includes playing, stopping, and managing sound and music assets, as well as controlling their volume and muting/unmuting them. The AudioManager interacts with the ResourceService to retrieve the necessary audio assets and handle them according to user preferences (e.g., volume adjustments, muting).

Testing (jUnit) for AudioManager

Purpose of Tests

The tests aim to ensure that the core functionalities of the AudioManager work as expected, including playing/stopping music and sounds, adjusting volumes (with quadratic scaling), and handling mute/unmute operations. The tests use Mockito to mock Sound and Music objects and their interactions with the ResourceService.

Test Descriptions

shouldPlaySound

  • Purpose: This test verifies that a sound is correctly played when requested by AudioManager.

  • What it does: Mocks the Sound object retrieved from ResourceService, and calls the playSound method with a sample sound path. Verifies that the sound is played with the correct volume.

  • Why This Test: Ensures that sounds are played correctly using the AudioManager and that the system interacts properly with the ResourceService and the sound asset.

shouldStopSound

  • Purpose: This test checks whether a sound can be stopped after being played.

  • What it does: Mocks the Sound object, simulates playing a sound, then calls AudioManager.stopSound() with a sound path. It verifies that the correct sound instance is stopped using its sound ID.

  • Why This Test: Ensures that sound effects can be halted mid-play, which is important for dynamic game behavior. For instance, sound effects may need to stop when switching scenes or triggering certain in-game events.

shouldPlayMusic

  • Purpose: Verifies that a music track is played with the correct settings, including volume and looping, when requested by AudioManager.

  • What it does: Mocks the Music object retrieved from ResourceService, then calls the playMusic method with a specified path and looping option. It verifies that the music is set to the correct volume, set to loop if necessary, and then played.

  • Why This Test: Ensures that background music or any continuous music track starts correctly with the desired volume and looping settings. This is vital for creating a smooth auditory environment in the game.

shouldStopMusic

  • Purpose: This test checks that a playing music track can be stopped by the AudioManager.

  • What it does: Mocks the Music object, simulates playing a track, and calls AudioManager.stopMusic(). It verifies that the music stops correctly.

  • Why This Test: Ensures that music tracks can be stopped, which is essential for handling transitions between game scenes or when stopping audio altogether (e.g., during a pause or game over).

shouldSetMusicVolume

  • Purpose:Ensures that the volume of the currently playing music can be adjusted by the AudioManager using quadratic scaling.

  • What it does: Mocks the Music object, simulates playing a music track, then calls AudioManager.setMusicVolume() with a specific volume level. It verifies that the volume is scaled quadratically and applied to the music.

  • Why This Test: Ensures that players can control the volume of background music, and verifies that the quadratic scaling (which smooths the volume progression) is working correctly. This improves user experience with more intuitive volume control.

shouldSetSoundVolume

  • Purpose: Verifies that the volume of playing sounds can be adjusted dynamically by the AudioManager using quadratic scaling.

  • What it does: Mocks the Sound object, simulates playing a sound, and then calls AudioManager.setSoundVolume() with a specific volume level. It checks that the quadratic scaling is correctly applied to the sound's volume.

  • Why This Test: Ensures that sound effects are affected by volume adjustments during gameplay, and verifies that the quadratic scaling is applied, making the volume change smoother for a more natural listening experience.

shouldMuteAndUnmuteAudio

  • Purpose: Tests that the AudioManager can mute and unmute both music and sounds, ensuring that the volume behaves as expected when toggled.

  • What it does: Mocks both Music and Sound objects, simulates playing both, then calls AudioManager.muteAudio() to mute and AudioManager.unmuteAudio() to unmute. Verifies that the volumes are set to zero when muted and revert to their previous levels when unmuted.

  • Why This Test: Ensures that the mute functionality works correctly for both music and sound effects. This is important for players who need to quickly mute/unmute the game audio.

shouldScaleVolume

  • Purpose: Verifies that the AudioManager applies quadratic scaling when setting music and sound volumes.

  • What it does: Sets music and sound volumes using AudioManager.setMusicVolume() and AudioManager.setSoundVolume(). It then verifies that the volumes are correctly scaled **quadratically **(i.e., volume^2).

  • Why This Test: Ensures that the quadratic scaling for volume adjustments works correctly, providing a more natural change in perceived loudness as players adjust volume settings.

shouldReturnCorrectVolumeWhenMuted

  • Purpose: Verifies that after muting and unmuting, the music and sound volumes are restored to their previous levels.

  • What it does: Sets the music and sound volumes, mutes the audio using AudioManager.muteAudio(), and checks that the volume is set to 0 while muted. After unmuting with AudioManager.unmuteAudio(), it verifies that the volume returns to the pre-mute levels.

  • Why This Test: Ensures that the system remembers the previous volume levels when audio is muted and correctly restores them when unmuted, which is important for a smooth audio control experience.

Clone this wiki locally