Added audio player logic and tests.
This commit is contained in:
106
Harmonia.Tests/AudioPlayerTests.cs
Normal file
106
Harmonia.Tests/AudioPlayerTests.cs
Normal file
@@ -0,0 +1,106 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -1,33 +1,37 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsPackable>false</IsPackable>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.4">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
|
||||
<PackageReference Include="NSubstitute" Version="5.3.0" />
|
||||
<PackageReference Include="Shouldly" Version="4.3.0" />
|
||||
<PackageReference Include="xunit" Version="2.9.3" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.2">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
<UseMicrosoftTestingPlatformRunner>true</UseMicrosoftTestingPlatformRunner>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Harmonia.Core\Harmonia.Core.csproj" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.4">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
|
||||
<PackageReference Include="NSubstitute" Version="5.3.0" />
|
||||
<PackageReference Include="Shouldly" Version="4.3.0" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.2">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="xunit.v3" Version="1.1.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="Xunit" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Harmonia.Core\Harmonia.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="Xunit" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user