using Microsoft.VisualStudio.TestTools.UnitTesting; using OwlCore.Events; using StrixMusic.Sdk.MediaPlayback; using StrixMusic.Sdk.Plugins.Model; using System; using System.Collections.Generic; using System.Reflection; using System.Threading; using System.Threading.Tasks; using StrixMusic.Sdk.AdapterModels; using StrixMusic.Sdk.AppModels; using StrixMusic.Sdk.BaseModels; using StrixMusic.Sdk.CoreModels; namespace StrixMusic.Sdk.Tests.Plugins.Models { [TestClass] public class PlaylistPluginBaseTests { private static bool NoInner(MemberInfo x) => !x.Name.Contains("Inner"); private static bool NoInnerOrSources(MemberInfo x) => NoInner(x) && !x.Name.ToLower().Contains("sources"); [Flags] public enum PossiblePlugins { None = 0, Playable = 1, Downloadable = 2, TrackCollection = 4, ImageCollection = 8, UrlCollection = 16, } [TestMethod, Timeout(1000)] public void NoPlugins() { var builder = new SdkModelPlugin(SdkTestPluginMetadata.Metadata).Playlist; var finalTestClass = new Unimplemented(); var emptyChain = builder.Execute(finalTestClass); Assert.AreSame(emptyChain, finalTestClass); Helpers.AssertAllThrowsOnMemberAccess>(finalTestClass); Helpers.AssertAllThrowsOnMemberAccess>(emptyChain); } [TestMethod, Timeout(1000)] public void PluginNoOverride() { // No plugins. var builder = new SdkModelPlugin(SdkTestPluginMetadata.Metadata).Playlist; var finalTestClass = new Unimplemented(); var emptyChain = builder.Execute(finalTestClass); Assert.AreSame(emptyChain, finalTestClass); Helpers.AssertAllThrowsOnMemberAccess>(finalTestClass); Helpers.AssertAllThrowsOnMemberAccess>(emptyChain); // No overrides. builder.Add(x => new NoOverride(x)); var noOverride = builder.Execute(finalTestClass); Assert.AreNotSame(noOverride, emptyChain); Assert.AreNotSame(noOverride, finalTestClass); Helpers.AssertAllMembersThrowOnAccess, NoOverride>( noOverride, customFilter: NoInner ); } [TestMethod, Timeout(5000)] public void PluginFullyCustom() { // No plugins. var builder = new SdkModelPlugin(SdkTestPluginMetadata.Metadata).Playlist; var finalTestClass = new Unimplemented(); var emptyChain = builder.Execute(finalTestClass); Assert.AreSame(emptyChain, finalTestClass); Helpers.AssertAllThrowsOnMemberAccess>(finalTestClass); Helpers.AssertAllThrowsOnMemberAccess>(emptyChain); // No overrides. builder.Add(x => new NoOverride(x)); var noOverride = builder.Execute(finalTestClass); Assert.AreNotSame(noOverride, emptyChain); Assert.AreNotSame(noOverride, finalTestClass); Helpers.AssertAllThrowsOnMemberAccess>(noOverride, customFilter: NoInner); // Fully custom builder.Add(x => new FullyCustom(x)); var allCustom = builder.Execute(finalTestClass); Assert.AreNotSame(noOverride, emptyChain); Assert.AreNotSame(noOverride, finalTestClass); Helpers.AssertAllThrowsOnMemberAccess>( allCustom, customFilter: NoInnerOrSources ); } [TestMethod, Timeout(5000)] [AllEnumFlagCombinations(typeof(PossiblePlugins))] public void PluginFullyCustomWith_AllCombinations(PossiblePlugins data) { var builder = new SdkModelPlugin(SdkTestPluginMetadata.Metadata).Playlist; var defaultImplementation = new Unimplemented(); builder.Add(x => new NoOverride(x) { InnerDownloadable = data.HasFlag(PossiblePlugins.Downloadable) ? new DownloadablePluginBaseTests.Unimplemented() : x, InnerPlayable = data.HasFlag(PossiblePlugins.Playable) ? new PlayablePluginBaseTests.Unimplemented() : x, InnerTrackCollection = data.HasFlag(PossiblePlugins.TrackCollection) ? new TrackCollectionPluginBaseTests.Unimplemented() : x, InnerImageCollection = data.HasFlag(PossiblePlugins.ImageCollection) ? new ImageCollectionPluginBaseTests.Unimplemented() : x, InnerUrlCollection = data.HasFlag(PossiblePlugins.UrlCollection) ? new UrlCollectionPluginBaseTests.Unimplemented() : x, } ); var finalImpl = builder.Execute(defaultImplementation); Assert.AreNotSame(finalImpl, defaultImplementation); Assert.IsInstanceOfType(finalImpl, typeof(NoOverride)); var expectedExceptionsWhenDisposing = new List { typeof(AccessedException), }; if (data.HasFlag(PossiblePlugins.Downloadable)) { expectedExceptionsWhenDisposing.Add(typeof(AccessedException)); Helpers.AssertAllMembersThrowOnAccess, DownloadablePluginBaseTests.Unimplemented>( finalImpl, customFilter: NoInnerOrSources, typesToExclude: typeof(IAsyncDisposable) ); } if (data.HasFlag(PossiblePlugins.Playable)) { expectedExceptionsWhenDisposing.Add(typeof(AccessedException)); Helpers.AssertAllMembersThrowOnAccess, PlayablePluginBaseTests.Unimplemented>( finalImpl, customFilter: NoInnerOrSources, typesToExclude: new[] { typeof(IAsyncDisposable), typeof(DownloadablePluginBaseTests.Unimplemented), typeof(ImageCollectionPluginBaseTests.Unimplemented), typeof(UrlCollectionPluginBaseTests.Unimplemented) } ); } if (data.HasFlag(PossiblePlugins.ImageCollection)) { expectedExceptionsWhenDisposing.Add(typeof(AccessedException)); Helpers.AssertAllMembersThrowOnAccess, ImageCollectionPluginBaseTests.Unimplemented>( finalImpl, customFilter: NoInnerOrSources, typesToExclude: typeof(IAsyncDisposable) ); } if (data.HasFlag(PossiblePlugins.TrackCollection)) { expectedExceptionsWhenDisposing.Add(typeof(AccessedException)); Helpers.AssertAllMembersThrowOnAccess, TrackCollectionPluginBaseTests.Unimplemented>( finalImpl, customFilter: NoInnerOrSources, typesToExclude: new[] { typeof(IAsyncDisposable), typeof(DownloadablePluginBaseTests.Unimplemented), typeof(ImageCollectionPluginBaseTests.Unimplemented), typeof(UrlCollectionPluginBaseTests.Unimplemented), typeof(PlayablePluginBaseTests.Unimplemented), typeof(IPlayableCollectionItem), } ); } if (data.HasFlag(PossiblePlugins.UrlCollection)) { expectedExceptionsWhenDisposing.Add(typeof(AccessedException)); Helpers.AssertAllMembersThrowOnAccess, UrlCollectionPluginBaseTests.Unimplemented>( finalImpl, customFilter: NoInnerOrSources, typesToExclude: typeof(IAsyncDisposable) ); } Helpers.AssertAllThrowsOnMemberAccess( finalImpl, customFilter: NoInnerOrSources, expectedExceptions: expectedExceptionsWhenDisposing.ToArray() ); } [TestMethod, Timeout(5000)] [AllEnumFlagCombinations(typeof(PossiblePlugins))] public async Task DisposeAsync_AllCombinations(PossiblePlugins data) { var builder = new SdkModelPlugin(SdkTestPluginMetadata.Metadata).Playlist; var defaultImplementation = new NotBlockingDisposeAsync(); builder.Add(x => new NoOverride(x) { InnerDownloadable = data.HasFlag(PossiblePlugins.Downloadable) ? new DownloadablePluginBaseTests.NotBlockingDisposeAsync() : x, InnerPlayable = data.HasFlag(PossiblePlugins.Playable) ? new PlayablePluginBaseTests.NotBlockingDisposeAsync() : x, InnerTrackCollection = data.HasFlag(PossiblePlugins.TrackCollection) ? new TrackCollectionPluginBaseTests.NotBlockingDisposeAsync() : x, InnerImageCollection = data.HasFlag(PossiblePlugins.ImageCollection) ? new ImageCollectionPluginBaseTests.NotBlockingDisposeAsync() : x, InnerUrlCollection = data.HasFlag(PossiblePlugins.UrlCollection) ? new UrlCollectionPluginBaseTests.NotBlockingDisposeAsync() : x, }); var finalImpl = builder.Execute(defaultImplementation); Assert.AreNotSame(finalImpl, defaultImplementation); Assert.IsInstanceOfType(finalImpl, typeof(NoOverride)); await finalImpl.DisposeAsync(); } internal class FullyCustom : PlaylistPluginBase { public FullyCustom(IPlaylist inner) : base(new ModelPluginMetadata("", nameof(FullyCustom), "", new Version()), inner) { } internal static AccessedException AccessedException { get; } = new(); public override ValueTask DisposeAsync() => throw AccessedException; public override Task IsAddImageAvailableAsync(int index, CancellationToken cancellationToken = default) => throw AccessedException; public override Task IsRemoveImageAvailableAsync(int index, CancellationToken cancellationToken = default) => throw AccessedException; public override Task RemoveImageAsync(int index, CancellationToken cancellationToken = default) => throw AccessedException; public override int TotalImageCount => throw AccessedException; public override event EventHandler? ImagesCountChanged { add => throw AccessedException; remove => throw AccessedException; } public override int TotalUrlCount => throw AccessedException; public override Task RemoveUrlAsync(int index, CancellationToken cancellationToken = default) => throw AccessedException; public override Task IsAddUrlAvailableAsync(int index, CancellationToken cancellationToken = default) => throw AccessedException; public override Task IsRemoveUrlAvailableAsync(int index, CancellationToken cancellationToken = default) => throw AccessedException; public override event EventHandler? UrlsCountChanged { add => throw AccessedException; remove => throw AccessedException; } public override string Id => throw AccessedException; public override string Name => throw AccessedException; public override string? Description => throw AccessedException; public override DateTime? LastPlayed => throw AccessedException; public override PlaybackState PlaybackState => throw AccessedException; public override TimeSpan Duration => throw AccessedException; public override bool IsChangeNameAsyncAvailable => throw AccessedException; public override bool IsChangeDescriptionAsyncAvailable => throw AccessedException; public override bool IsChangeDurationAsyncAvailable => throw AccessedException; public override Task ChangeNameAsync(string name, CancellationToken cancellationToken = default) => throw AccessedException; public override Task ChangeDescriptionAsync(string? description, CancellationToken cancellationToken = default) => throw AccessedException; public override Task ChangeDurationAsync(TimeSpan duration, CancellationToken cancellationToken = default) => throw AccessedException; public override event EventHandler? PlaybackStateChanged { add => throw AccessedException; remove => throw AccessedException; } public override event EventHandler? NameChanged { add => throw AccessedException; remove => throw AccessedException; } public override event EventHandler? DescriptionChanged { add => throw AccessedException; remove => throw AccessedException; } public override event EventHandler? DurationChanged { add => throw AccessedException; remove => throw AccessedException; } public override event EventHandler? LastPlayedChanged { add => throw AccessedException; remove => throw AccessedException; } public override event EventHandler? IsChangeNameAsyncAvailableChanged { add => throw AccessedException; remove => throw AccessedException; } public override event EventHandler? IsChangeDescriptionAsyncAvailableChanged { add => throw AccessedException; remove => throw AccessedException; } public override event EventHandler? IsChangeDurationAsyncAvailableChanged { add => throw AccessedException; remove => throw AccessedException; } public override DateTime? AddedAt => throw AccessedException; public override int TotalTrackCount => throw AccessedException; public override bool IsPlayTrackCollectionAsyncAvailable => throw AccessedException; public override bool IsPauseTrackCollectionAsyncAvailable => throw AccessedException; public override Task PlayTrackCollectionAsync(CancellationToken cancellationToken = default) => throw AccessedException; public override Task PauseTrackCollectionAsync(CancellationToken cancellationToken = default) => throw AccessedException; public override Task RemoveTrackAsync(int index, CancellationToken cancellationToken = default) => throw AccessedException; public override Task IsAddTrackAvailableAsync(int index, CancellationToken cancellationToken = default) => throw AccessedException; public override Task IsRemoveTrackAvailableAsync(int index, CancellationToken cancellationToken = default) => throw AccessedException; public override event EventHandler? IsPlayTrackCollectionAsyncAvailableChanged { add => throw AccessedException; remove => throw AccessedException; } public override event EventHandler? IsPauseTrackCollectionAsyncAvailableChanged { add => throw AccessedException; remove => throw AccessedException; } public override event EventHandler? TracksCountChanged { add => throw AccessedException; remove => throw AccessedException; } public override bool Equals(ICoreImageCollection? other) => throw AccessedException; public override IAsyncEnumerable GetImagesAsync(int limit, int offset, CancellationToken cancellationToken = default) => throw AccessedException; public override Task AddImageAsync(IImage image, int index, CancellationToken cancellationToken = default) => throw AccessedException; public override event CollectionChangedEventHandler? ImagesChanged { add => throw AccessedException; remove => throw AccessedException; } public override bool Equals(ICoreUrlCollection? other) => throw AccessedException; public override IAsyncEnumerable GetUrlsAsync(int limit, int offset, CancellationToken cancellationToken = default) => throw AccessedException; public override Task AddUrlAsync(IUrl url, int index, CancellationToken cancellationToken = default) => throw AccessedException; public override event CollectionChangedEventHandler? UrlsChanged { add => throw AccessedException; remove => throw AccessedException; } public override DownloadInfo DownloadInfo => throw AccessedException; public override Task StartDownloadOperationAsync(DownloadOperation operation, CancellationToken cancellationToken = default) => throw AccessedException; public override event EventHandler? DownloadInfoChanged { add => throw AccessedException; remove => throw AccessedException; } public override bool Equals(ICoreTrackCollection? other) => throw AccessedException; public override Task PlayTrackCollectionAsync(ITrack track, CancellationToken cancellationToken = default) => throw AccessedException; public override IAsyncEnumerable GetTracksAsync(int limit, int offset, CancellationToken cancellationToken = default) => throw AccessedException; public override Task AddTrackAsync(ITrack track, int index, CancellationToken cancellationToken = default) => throw AccessedException; public override event CollectionChangedEventHandler? TracksChanged { add => throw AccessedException; remove => throw AccessedException; } public override bool Equals(ICorePlaylistCollectionItem? other) => throw AccessedException; public override bool Equals(ICorePlaylist? other) => throw AccessedException; public override IUserProfile? Owner => throw AccessedException; public override IPlayableCollectionGroup? RelatedItems => throw AccessedException; } internal class NoOverride : PlaylistPluginBase { public NoOverride(IPlaylist inner) : base(new ModelPluginMetadata("", nameof(NoOverride), "", new Version()), inner) { } } internal class NotBlockingDisposeAsync : PlaylistPluginBase { public NotBlockingDisposeAsync() : base(new ModelPluginMetadata("", nameof(NotBlockingDisposeAsync), "", new Version()), new Unimplemented()) { } /// public override ValueTask DisposeAsync() => default; } internal class Unimplemented : IPlaylist { internal static AccessedException AccessedException { get; } = new(); public event EventHandler? SourcesChanged { add => throw AccessedException; remove => throw AccessedException; } public ValueTask DisposeAsync() => throw AccessedException; public Task IsAddImageAvailableAsync(int index, CancellationToken cancellationToken = default) => throw AccessedException; public Task IsRemoveImageAvailableAsync(int index, CancellationToken cancellationToken = default) => throw AccessedException; public Task RemoveImageAsync(int index, CancellationToken cancellationToken = default) => throw AccessedException; public int TotalImageCount => throw AccessedException; public event EventHandler? ImagesCountChanged { add => throw AccessedException; remove => throw AccessedException; } public int TotalUrlCount => throw AccessedException; public Task RemoveUrlAsync(int index, CancellationToken cancellationToken = default) => throw AccessedException; public Task IsAddUrlAvailableAsync(int index, CancellationToken cancellationToken = default) => throw AccessedException; public Task IsRemoveUrlAvailableAsync(int index, CancellationToken cancellationToken = default) => throw AccessedException; public event EventHandler? UrlsCountChanged { add => throw AccessedException; remove => throw AccessedException; } public string Id => throw AccessedException; public string Name => throw AccessedException; public string? Description => throw AccessedException; public DateTime? LastPlayed => throw AccessedException; public PlaybackState PlaybackState => throw AccessedException; public TimeSpan Duration => throw AccessedException; public bool IsChangeNameAsyncAvailable => throw AccessedException; public bool IsChangeDescriptionAsyncAvailable => throw AccessedException; public bool IsChangeDurationAsyncAvailable => throw AccessedException; public Task ChangeNameAsync(string name, CancellationToken cancellationToken = default) => throw AccessedException; public Task ChangeDescriptionAsync(string? description, CancellationToken cancellationToken = default) => throw AccessedException; public Task ChangeDurationAsync(TimeSpan duration, CancellationToken cancellationToken = default) => throw AccessedException; public event EventHandler? PlaybackStateChanged { add => throw AccessedException; remove => throw AccessedException; } public event EventHandler? NameChanged { add => throw AccessedException; remove => throw AccessedException; } public event EventHandler? DescriptionChanged { add => throw AccessedException; remove => throw AccessedException; } public event EventHandler? DurationChanged { add => throw AccessedException; remove => throw AccessedException; } public event EventHandler? LastPlayedChanged { add => throw AccessedException; remove => throw AccessedException; } public event EventHandler? IsChangeNameAsyncAvailableChanged { add => throw AccessedException; remove => throw AccessedException; } public event EventHandler? IsChangeDescriptionAsyncAvailableChanged { add => throw AccessedException; remove => throw AccessedException; } public event EventHandler? IsChangeDurationAsyncAvailableChanged { add => throw AccessedException; remove => throw AccessedException; } public DateTime? AddedAt => throw AccessedException; public int TotalTrackCount => throw AccessedException; public bool IsPlayTrackCollectionAsyncAvailable => throw AccessedException; public bool IsPauseTrackCollectionAsyncAvailable => throw AccessedException; public Task PlayTrackCollectionAsync(CancellationToken cancellationToken = default) => throw AccessedException; public Task PauseTrackCollectionAsync(CancellationToken cancellationToken = default) => throw AccessedException; public Task RemoveTrackAsync(int index, CancellationToken cancellationToken = default) => throw AccessedException; public Task IsAddTrackAvailableAsync(int index, CancellationToken cancellationToken = default) => throw AccessedException; public Task IsRemoveTrackAvailableAsync(int index, CancellationToken cancellationToken = default) => throw AccessedException; public event EventHandler? IsPlayTrackCollectionAsyncAvailableChanged { add => throw AccessedException; remove => throw AccessedException; } public event EventHandler? IsPauseTrackCollectionAsyncAvailableChanged { add => throw AccessedException; remove => throw AccessedException; } public event EventHandler? TracksCountChanged { add => throw AccessedException; remove => throw AccessedException; } public bool Equals(ICoreImageCollection? other) => throw AccessedException; IReadOnlyList IMerged.Sources => throw AccessedException; IReadOnlyList IMerged.Sources => throw AccessedException; IReadOnlyList IMerged.Sources => throw AccessedException; IReadOnlyList IMerged.Sources => throw AccessedException; IReadOnlyList IMerged.Sources => throw AccessedException; public IAsyncEnumerable GetImagesAsync(int limit, int offset, CancellationToken cancellationToken = default) => throw AccessedException; public Task AddImageAsync(IImage image, int index, CancellationToken cancellationToken = default) => throw AccessedException; public event CollectionChangedEventHandler? ImagesChanged { add => throw AccessedException; remove => throw AccessedException; } public bool Equals(ICoreUrlCollection? other) => throw AccessedException; public IAsyncEnumerable GetUrlsAsync(int limit, int offset, CancellationToken cancellationToken = default) => throw AccessedException; public Task AddUrlAsync(IUrl url, int index, CancellationToken cancellationToken = default) => throw AccessedException; public event CollectionChangedEventHandler? UrlsChanged { add => throw AccessedException; remove => throw AccessedException; } public DownloadInfo DownloadInfo => throw AccessedException; public Task StartDownloadOperationAsync(DownloadOperation operation, CancellationToken cancellationToken = default) => throw AccessedException; public event EventHandler? DownloadInfoChanged { add => throw AccessedException; remove => throw AccessedException; } public bool Equals(ICoreTrackCollection? other) => throw AccessedException; public Task PlayTrackCollectionAsync(ITrack track, CancellationToken cancellationToken = default) => throw AccessedException; public IAsyncEnumerable GetTracksAsync(int limit, int offset, CancellationToken cancellationToken = default) => throw AccessedException; public Task AddTrackAsync(ITrack track, int index, CancellationToken cancellationToken = default) => throw AccessedException; public event CollectionChangedEventHandler? TracksChanged { add => throw AccessedException; remove => throw AccessedException; } public bool Equals(ICorePlaylistCollectionItem? other) => throw AccessedException; public bool Equals(ICorePlaylist? other) => throw AccessedException; public IUserProfile? Owner => throw AccessedException; public IPlayableCollectionGroup? RelatedItems => throw AccessedException; } } }