106 lines
3.2 KiB
C#
106 lines
3.2 KiB
C#
using Harmonia.Core.Engine;
|
|
using Harmonia.Core.Models;
|
|
using Harmonia.Core.Player;
|
|
using Harmonia.Core.Playlists;
|
|
using NSubstitute;
|
|
using NSubstitute.ExceptionExtensions;
|
|
using Shouldly;
|
|
|
|
namespace Harmonia.Tests;
|
|
|
|
public class AudioPlayerTests
|
|
{
|
|
private readonly IAudioEngine _audioEngine;
|
|
private readonly IPlaylistRepository _playlistRepository;
|
|
private readonly PlaylistSong[] _playlistSongs;
|
|
private readonly IAudioPlayer _audioPlayer;
|
|
|
|
public AudioPlayerTests()
|
|
{
|
|
_audioEngine = Substitute.For<IAudioEngine>();
|
|
_audioEngine.LoadAsync("Song1.mp3").Returns(true);
|
|
_audioEngine.LoadAsync("Song2.mp3").Returns(false);
|
|
_audioEngine.LoadAsync("Song3.mp3").Returns(true);
|
|
_audioEngine.LoadAsync("Song4.mp3").Throws(new FileNotFoundException());
|
|
_audioEngine.LoadAsync("Song5.mp3").Returns(false);
|
|
_audioEngine.LoadAsync("Song6.mp3").Returns(true);
|
|
_audioEngine.LoadAsync("Song7.mp3").Returns(true);
|
|
|
|
Song[] songs =
|
|
[
|
|
new Song() { FileName = "Song1.mp3" },
|
|
new Song() { FileName = "Song2.mp3" },
|
|
new Song() { FileName = "Song3.mp3" },
|
|
new Song() { FileName = "Song4.mp3" },
|
|
new Song() { FileName = "Song5.mp3" },
|
|
new Song() { FileName = "Song6.mp3" },
|
|
new Song() { FileName = "Song7.mp3" }
|
|
];
|
|
|
|
Playlist playlist = new()
|
|
{
|
|
Name = "Playlist1"
|
|
};
|
|
|
|
playlist.AddSongs(songs);
|
|
|
|
_playlistSongs = [.. playlist.Songs];
|
|
|
|
_playlistRepository = Substitute.For<IPlaylistRepository>();
|
|
_playlistRepository.Get().Returns([playlist]);
|
|
//_playlistRepository.GetPlaylist(_playlistSongs[0]).Returns(playlist);
|
|
|
|
_audioPlayer = new AudioPlayer(_audioEngine, _playlistRepository);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Load_Song_Success()
|
|
{
|
|
(await _audioPlayer.LoadAsync(_playlistSongs[0], PlaybackMode.LoadAndPlay)).ShouldBe(true);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Load_Song_Failure()
|
|
{
|
|
(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.NextAsync();
|
|
|
|
_audioPlayer.PlayingSong.ShouldBe(_playlistSongs[6]);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Load_Next_Song_Failure()
|
|
{
|
|
await _audioPlayer.LoadAsync(_playlistSongs[0], PlaybackMode.LoadAndPlay);
|
|
await _audioPlayer.NextAsync();
|
|
|
|
_audioPlayer.PlayingSong.ShouldBe(_playlistSongs[2]);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Load_Next_Song_From_Last_Song()
|
|
{
|
|
await _audioPlayer.LoadAsync(_playlistSongs[6], PlaybackMode.LoadAndPlay);
|
|
await _audioPlayer.NextAsync();
|
|
|
|
_audioPlayer.PlayingSong.ShouldBe(_playlistSongs[0]);
|
|
}
|
|
|
|
[Fact]
|
|
public void Random_Test()
|
|
{
|
|
_audioPlayer.IsRandom.ShouldBeFalse();
|
|
}
|
|
} |