Updated tests for audio player.

This commit is contained in:
2025-02-24 02:45:47 -05:00
parent 3c2c39e659
commit 960c6b9ec7
2 changed files with 92 additions and 54 deletions

View File

@@ -8,12 +8,32 @@ using Shouldly;
namespace Harmonia.Tests;
public class TestAudioPlayer(IAudioEngine audioEngine, IPlaylistRepository playlistRepository)
: AudioPlayer(audioEngine, playlistRepository)
{
internal void SetPlaylist(Playlist playlist)
{
Playlist = playlist;
}
internal int GetPlayingSongIndex()
{
if (Playlist == null)
return -1;
if (PlayingSong == null)
return -1;
return Playlist.Songs.IndexOf(PlayingSong);
}
}
public class AudioPlayerTests
{
private readonly IAudioEngine _audioEngine;
private readonly IPlaylistRepository _playlistRepository;
private readonly PlaylistSong[] _playlistSongs;
private readonly IAudioPlayer _audioPlayer;
private readonly TestAudioPlayer _audioPlayer;
public AudioPlayerTests()
{
@@ -50,57 +70,76 @@ public class AudioPlayerTests
_playlistRepository.Get().Returns([playlist]);
//_playlistRepository.GetPlaylist(_playlistSongs[0]).Returns(playlist);
_audioPlayer = new AudioPlayer(_audioEngine, _playlistRepository);
_audioPlayer = new TestAudioPlayer(_audioEngine, _playlistRepository);
_audioPlayer.SetPlaylist(playlist);
//await _audioPlayer.LoadAsync(_playlistSongs[0], PlaybackMode.LoadOnly);
}
[Fact]
public async Task Load_Song_Success()
[Theory]
[InlineData(0, true)]
[InlineData(1, false)]
[InlineData(2, true)]
[InlineData(3, false)]
[InlineData(4, false)]
[InlineData(5, true)]
[InlineData(6, true)]
[InlineData(7, false)]
public async Task Load_Song(int index, bool expectedResult)
{
(await _audioPlayer.LoadAsync(_playlistSongs[0], PlaybackMode.LoadAndPlay)).ShouldBe(true);
bool result = await _audioPlayer.LoadAsync(index);
result.ShouldBe(expectedResult);
}
[Fact]
public async Task Load_Song_Failure()
[Theory]
[InlineData(0, 2)]
[InlineData(2, 5)]
[InlineData(5, 6)]
[InlineData(6, 0)]
public async Task Load_Next_Song(int index, int expectedResult)
{
(await _audioPlayer.LoadAsync(_playlistSongs[1], PlaybackMode.LoadAndPlay)).ShouldBe(false);
}
[Fact]
public async Task Load_Song_Failure_With_Exception()
{
(await _audioPlayer.LoadAsync(_playlistSongs[3], PlaybackMode.LoadAndPlay)).ShouldBe(false);
}
[Fact]
public async Task Load_Next_Song_Success()
{
await _audioPlayer.LoadAsync(_playlistSongs[5], PlaybackMode.LoadAndPlay);
await _audioPlayer.LoadAsync(index);
await _audioPlayer.NextAsync();
_audioPlayer.PlayingSong.ShouldBe(_playlistSongs[6]);
int currentIndex = _audioPlayer.GetPlayingSongIndex();
currentIndex.ShouldBe(expectedResult);
}
[Fact]
public async Task Load_Next_Song_Failure()
[Theory]
[InlineData(0, 0, RepeatState.Off, 0)]
[InlineData(0, 6, RepeatState.RepeatAll, 0)]
[InlineData(6, 5, RepeatState.Off, 0)]
[InlineData(6, 6, RepeatState.Off, 6)]
public async Task Load_Previous_Song(int index, int expectedResult, RepeatState repeatState = RepeatState.Off, double position = 0)
{
await _audioPlayer.LoadAsync(_playlistSongs[0], PlaybackMode.LoadAndPlay);
await _audioPlayer.NextAsync();
await _audioPlayer.LoadAsync(index, PlaybackMode.LoadAndPlay);
_audioPlayer.RepeatState = repeatState;
_audioPlayer.Position = position;
_audioPlayer.PlayingSong.ShouldBe(_playlistSongs[2]);
await _audioPlayer.PreviousAsync();
int currentIndex = _audioPlayer.GetPlayingSongIndex();
currentIndex.ShouldBe(expectedResult);
}
[Fact]
public async Task Load_Next_Song_From_Last_Song()
[Theory]
[InlineData(0, 2, RepeatState.Off)]
[InlineData(0, 0, RepeatState.RepeatOne)]
public async Task Song_Finshed(int index, int expectedResult, RepeatState repeatState = RepeatState.Off)
{
await _audioPlayer.LoadAsync(_playlistSongs[6], PlaybackMode.LoadAndPlay);
await _audioPlayer.NextAsync();
await _audioPlayer.LoadAsync(index, PlaybackMode.LoadAndPlay);
_audioPlayer.RepeatState = repeatState;
_audioPlayer.PlayingSong.ShouldBe(_playlistSongs[0]);
}
TaskCompletionSource<bool> taskCompletionSource = new();
[Fact]
public void Random_Test()
{
_audioPlayer.IsRandom.ShouldBeFalse();
_audioEngine.StreamFinished += (sender, eventArgs) => { taskCompletionSource.SetResult(true); };
_audioEngine.StreamFinished += Raise.EventWith<EventArgs>(_audioEngine, new());
await taskCompletionSource.Task;
int currentIndex = _audioPlayer.GetPlayingSongIndex();
currentIndex.ShouldBe(expectedResult);
}
}