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.CoreModels; namespace StrixMusic.Sdk.Tests.Plugins.Models { [TestClass] public class PlayablePluginBaseTests { 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, ImageCollection = 4, UrlCollection = 8, } [TestMethod, Timeout(1000)] public void NoPlugins() { var builder = new SdkModelPlugin(SdkTestPluginMetadata.Metadata).Playable; 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).Playable; 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(1000)] public void PluginFullyCustom() { // No plugins. var builder = new SdkModelPlugin(SdkTestPluginMetadata.Metadata).Playable; 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).Playable; var defaultImplementation = new Unimplemented(); builder.Add(x => new NoOverride(x) { InnerDownloadable = data.HasFlag(PossiblePlugins.Downloadable) ? new DownloadablePluginBaseTests.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.UrlCollection)) { expectedExceptionsWhenDisposing.Add(typeof(AccessedException)); Helpers.AssertAllMembersThrowOnAccess, UrlCollectionPluginBaseTests.Unimplemented>( finalImpl, customFilter: NoInnerOrSources, typesToExclude: typeof(IAsyncDisposable) ); } Helpers.AssertAllThrowsOnMemberAccess( finalImpl, customFilter: NoInnerOrSources, expectedExceptions: expectedExceptionsWhenDisposing.ToArray() ); } internal class FullyCustom : PlayablePluginBase { public FullyCustom(IPlayable inner) : base(new ModelPluginMetadata("", nameof(FullyCustom), "", new Version()), inner) { } internal static AccessedException AccessedException { get; } = new AccessedException(); public override DownloadInfo DownloadInfo => 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 int TotalImageCount => throw AccessedException; public override int TotalUrlCount => throw AccessedException; public override event EventHandler? DownloadInfoChanged { add => throw AccessedException; remove => 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 event EventHandler? ImagesCountChanged { add => throw AccessedException; remove => throw AccessedException; } public override event EventHandler? UrlsCountChanged { add => throw AccessedException; remove => throw AccessedException; } public override event CollectionChangedEventHandler? ImagesChanged { add => throw AccessedException; remove => throw AccessedException; } public override event CollectionChangedEventHandler? UrlsChanged { add => throw AccessedException; remove => throw AccessedException; } public override Task AddImageAsync(IImage image, int index, CancellationToken cancellationToken = default) => throw AccessedException; public override Task AddUrlAsync(IUrl url, int index, 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 Task ChangeNameAsync(string name, CancellationToken cancellationToken = default) => throw AccessedException; public override ValueTask DisposeAsync() => throw AccessedException; public override bool Equals(ICoreImageCollection? other) => throw AccessedException; public override bool Equals(ICoreUrlCollection? other) => throw AccessedException; public override IAsyncEnumerable GetImagesAsync(int limit, int offset, CancellationToken cancellationToken = default) => throw AccessedException; public override IAsyncEnumerable GetUrlsAsync(int limit, int offset, CancellationToken cancellationToken = default) => throw AccessedException; public override Task IsAddImageAvailableAsync(int index, CancellationToken cancellationToken = default) => throw AccessedException; public override Task IsAddUrlAvailableAsync(int index, CancellationToken cancellationToken = default) => throw AccessedException; public override Task IsRemoveImageAvailableAsync(int index, CancellationToken cancellationToken = default) => throw AccessedException; public override Task IsRemoveUrlAvailableAsync(int index, CancellationToken cancellationToken = default) => throw AccessedException; public override Task RemoveImageAsync(int index, CancellationToken cancellationToken = default) => throw AccessedException; public override Task RemoveUrlAsync(int index, CancellationToken cancellationToken = default) => throw AccessedException; public override Task StartDownloadOperationAsync(DownloadOperation operation, CancellationToken cancellationToken = default) => throw AccessedException; } internal class NoOverride : PlayablePluginBase { public NoOverride(IPlayable inner) : base(new ModelPluginMetadata("", nameof(NoOverride), "", new Version()), inner) { } } internal class NotBlockingDisposeAsync : PlayablePluginBase { public NotBlockingDisposeAsync() : base(new ModelPluginMetadata("", nameof(NotBlockingDisposeAsync), "", new Version()), new Unimplemented()) { } /// public override ValueTask DisposeAsync() => default; } internal class Unimplemented : IPlayable { internal static AccessedException AccessedException { get; } = new AccessedException(); public event EventHandler? SourcesChanged { add => throw AccessedException; remove => throw AccessedException; } public DownloadInfo DownloadInfo => 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 int TotalImageCount => throw AccessedException; public int TotalUrlCount => throw AccessedException; public IReadOnlyList Sources => throw AccessedException; IReadOnlyList IMerged.Sources => throw AccessedException; public event EventHandler? DownloadInfoChanged { add => throw AccessedException; remove => 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 event EventHandler? ImagesCountChanged { add => throw AccessedException; remove => throw AccessedException; } public event EventHandler? UrlsCountChanged { add => throw AccessedException; remove => throw AccessedException; } public event CollectionChangedEventHandler? ImagesChanged { add => throw AccessedException; remove => throw AccessedException; } public event CollectionChangedEventHandler? UrlsChanged { add => throw AccessedException; remove => throw AccessedException; } public Task AddImageAsync(IImage image, int index, CancellationToken cancellationToken = default) => throw AccessedException; public Task AddUrlAsync(IUrl url, int index, 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 Task ChangeNameAsync(string name, CancellationToken cancellationToken = default) => throw AccessedException; public ValueTask DisposeAsync() => throw AccessedException; public bool Equals(ICoreImageCollection? other) => throw AccessedException; public bool Equals(ICoreUrlCollection? other) => throw AccessedException; public IAsyncEnumerable GetImagesAsync(int limit, int offset, CancellationToken cancellationToken = default) => throw AccessedException; public IAsyncEnumerable GetUrlsAsync(int limit, int offset, CancellationToken cancellationToken = default) => throw AccessedException; public Task IsAddImageAvailableAsync(int index, CancellationToken cancellationToken = default) => throw AccessedException; public Task IsAddUrlAvailableAsync(int index, CancellationToken cancellationToken = default) => throw AccessedException; public Task IsRemoveImageAvailableAsync(int index, CancellationToken cancellationToken = default) => throw AccessedException; public Task IsRemoveUrlAvailableAsync(int index, CancellationToken cancellationToken = default) => throw AccessedException; public Task RemoveImageAsync(int index, CancellationToken cancellationToken = default) => throw AccessedException; public Task RemoveUrlAsync(int index, CancellationToken cancellationToken = default) => throw AccessedException; public Task StartDownloadOperationAsync(DownloadOperation operation, CancellationToken cancellationToken = default) => throw AccessedException; } } }