using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.VisualStudio.TestTools.UnitTesting; using OwlCore; using OwlCore.Events; using StrixMusic.Sdk.AppModels; using StrixMusic.Sdk.Tests.Mock.AppModels; namespace StrixMusic.Sdk.Tests; public static class VerifyModelObjectTree { public static void VerifyReturns(IStrixDataRoot item) { Assert.IsInstanceOfType(item.Discoverables, typeof(T)); Assert.IsInstanceOfType(item.RecentlyPlayed, typeof(T)); Assert.IsInstanceOfType(item.Pins, typeof(T)); Assert.IsInstanceOfType(item.Search, typeof(T)); Assert.IsInstanceOfType(item.Library, typeof(T)); } public async static Task VerifyReturnsAsync(IPlayableCollectionGroup item) { await VerifyReturnsAsync((IAlbumCollection)item); await VerifyReturnsAsync((IArtistCollection)item); await VerifyReturnsAsync((IPlaylistCollection)item); await VerifyReturnsAsync((ITrackCollection)item); await VerifyReturnsAsync((IImageCollection)item); await VerifyReturnsAsync((IUrlCollection)item); item.ChildItemsChanged += OnChildItemsChanged; IReadOnlyList>? added = null; IReadOnlyList>? removed = null; await item.AddChildAsync(new MockPlayableCollectionGroup(), 0); Assert.IsNotNull(added); Assert.AreEqual(1, added.Count); Assert.IsInstanceOfType(added[0].Data, typeof(T)); Assert.IsInstanceOfType(added[0].Data, typeof(IPlayableCollectionGroup)); added = null; await item.RemoveChildAsync(0); Assert.IsNotNull(removed); Assert.AreEqual(1, removed.Count); Assert.IsInstanceOfType(removed[0].Data, typeof(T)); Assert.IsInstanceOfType(removed[0].Data, typeof(IPlayableCollectionGroup)); removed = null; item.ChildItemsChanged -= OnChildItemsChanged; void OnChildItemsChanged(object sender, IReadOnlyList> addedItems, IReadOnlyList> removedItems) { if (addedItems.Count > 0) added = addedItems; if (removedItems.Count > 0) removed = removedItems; } } public async static Task VerifyReturnsAsync(ILibrary item) { await VerifyReturnsAsync((IPlayableCollectionGroup)item); } public async static Task VerifyReturnsAsync(IDiscoverables item) { await VerifyReturnsAsync((IPlayableCollectionGroup)item); } public async static Task VerifyReturnsAsync(IRecentlyPlayed item) { await VerifyReturnsAsync((IPlayableCollectionGroup)item); } public async static Task VerifyReturnsAsync(ISearch item) { var results = await item.GetSearchResultsAsync(string.Empty); await VerifyReturnsAsync(results); Assert.IsNotNull(item.SearchHistory); await VerifyReturnsAsync(item.SearchHistory); } public async static Task VerifyReturnsAsync(ISearchHistory item) { await VerifyReturnsAsync((IPlayableCollectionGroup)item); } public async static Task VerifyReturnsAsync(ISearchResults item) { await VerifyReturnsAsync((IPlayableCollectionGroup)item); } public async static Task VerifyReturnsAsync(IArtist item) { await VerifyReturnsAsync((IGenreCollection)item); await VerifyReturnsAsync((IImageCollection)item); await VerifyReturnsAsync((IUrlCollection)item); await VerifyReturnsAsync((ITrackCollection)item); await VerifyReturnsAsync((IAlbumCollection)item); Assert.IsNotNull(item.RelatedItems); Assert.IsInstanceOfType(item.RelatedItems, typeof(T)); } public async static Task VerifyReturnsAsync(IAlbum item) { await VerifyReturnsAsync((IGenreCollection)item); await VerifyReturnsAsync((IImageCollection)item); await VerifyReturnsAsync((IUrlCollection)item); await VerifyReturnsAsync((ITrackCollection)item); await VerifyReturnsAsync((IArtistCollection)item); Assert.IsNotNull(item.RelatedItems); Assert.IsInstanceOfType(item.RelatedItems, typeof(T)); } public async static Task VerifyReturnsAsync(IPlaylist item) { await VerifyReturnsAsync((IImageCollection)item); await VerifyReturnsAsync((IUrlCollection)item); await VerifyReturnsAsync((ITrackCollection)item); Assert.IsNotNull(item.RelatedItems); Assert.IsInstanceOfType(item.RelatedItems, typeof(T)); } public async static Task VerifyReturnsAsync(ITrack item) { await VerifyReturnsAsync((IGenreCollection)item); await VerifyReturnsAsync((IImageCollection)item); await VerifyReturnsAsync((IUrlCollection)item); await VerifyReturnsAsync((IArtistCollection)item); Assert.IsNotNull(item.RelatedItems); Assert.IsInstanceOfType(item.RelatedItems, typeof(T)); } public async static Task VerifyReturnsAsync(IAlbumCollection item) { await VerifyReturnsAsync((IImageCollection)item); await VerifyReturnsAsync((IUrlCollection)item); item.AlbumItemsChanged += OnAlbumItemsChanged; IReadOnlyList>? added = null; IReadOnlyList>? removed = null; await item.AddAlbumItemAsync(new MockAlbum(), 0); Assert.IsNotNull(added); Assert.AreEqual(1, added.Count); Assert.IsInstanceOfType(added[0].Data, typeof(T)); Assert.IsInstanceOfType(added[0].Data, typeof(IAlbum)); added = null; var albumItems = await item.GetAlbumItemsAsync(1, 0).ToListAsync(); Assert.IsInstanceOfType(albumItems[0], typeof(T)); Assert.IsInstanceOfType(albumItems[0], typeof(IAlbum)); await item.RemoveAlbumItemAsync(0); Assert.IsNotNull(removed); Assert.AreEqual(1, removed.Count); Assert.IsInstanceOfType(removed[0].Data, typeof(T)); Assert.IsInstanceOfType(removed[0].Data, typeof(IAlbum)); removed = null; await item.AddAlbumItemAsync(new MockAlbumCollection(), 0); Assert.IsNotNull(added); Assert.AreEqual(1, added.Count); Assert.IsInstanceOfType(added[0].Data, typeof(T)); Assert.IsInstanceOfType(added[0].Data, typeof(IAlbumCollection)); added = null; var albumCollection = await item.GetAlbumItemsAsync(1, 0).ToListAsync(); Assert.IsInstanceOfType(albumCollection[0], typeof(T)); Assert.IsInstanceOfType(albumCollection[0], typeof(IAlbumCollection)); await item.RemoveAlbumItemAsync(0); Assert.IsNotNull(removed); Assert.AreEqual(1, removed.Count); Assert.IsInstanceOfType(removed[0].Data, typeof(T)); Assert.IsInstanceOfType(removed[0].Data, typeof(IAlbumCollection)); removed = null; item.AlbumItemsChanged -= OnAlbumItemsChanged; void OnAlbumItemsChanged(object sender, IReadOnlyList> addedItems, IReadOnlyList> removedItems) { if (addedItems.Count > 0) added = addedItems; if (removedItems.Count > 0) removed = removedItems; } } public async static Task VerifyReturnsAsync(IArtistCollection item) { await VerifyReturnsAsync((IImageCollection)item); await VerifyReturnsAsync((IUrlCollection)item); item.ArtistItemsChanged += OnArtistItemsChanged; IReadOnlyList>? added = null; IReadOnlyList>? removed = null; await item.AddArtistItemAsync(new MockArtist(), 0); Assert.IsNotNull(added); Assert.AreEqual(1, added.Count); Assert.IsInstanceOfType(added[0].Data, typeof(T)); Assert.IsInstanceOfType(added[0].Data, typeof(IArtist)); added = null; var artistItems = await item.GetArtistItemsAsync(1, 0).ToListAsync(); Assert.IsInstanceOfType(artistItems[0], typeof(T)); Assert.IsInstanceOfType(artistItems[0], typeof(IArtist)); await item.RemoveArtistItemAsync(0); Assert.IsNotNull(removed); Assert.AreEqual(1, removed.Count); Assert.IsInstanceOfType(removed[0].Data, typeof(T)); Assert.IsInstanceOfType(removed[0].Data, typeof(IArtist)); removed = null; await item.AddArtistItemAsync(new MockArtistCollection(), 0); Assert.IsNotNull(added); Assert.AreEqual(1, added.Count); Assert.IsInstanceOfType(added[0].Data, typeof(T)); Assert.IsInstanceOfType(added[0].Data, typeof(IArtistCollection)); added = null; var artistCollection = await item.GetArtistItemsAsync(1, 0).ToListAsync(); Assert.IsInstanceOfType(artistCollection[0], typeof(T)); Assert.IsInstanceOfType(artistCollection[0], typeof(IArtistCollection)); await item.RemoveArtistItemAsync(0); Assert.IsNotNull(removed); Assert.AreEqual(1, removed.Count); Assert.IsInstanceOfType(removed[0].Data, typeof(T)); Assert.IsInstanceOfType(removed[0].Data, typeof(IArtistCollection)); removed = null; item.ArtistItemsChanged -= OnArtistItemsChanged; void OnArtistItemsChanged(object sender, IReadOnlyList> addedItems, IReadOnlyList> removedItems) { if (addedItems.Count > 0) added = addedItems; if (removedItems.Count > 0) removed = removedItems; } } public async static Task VerifyReturnsAsync(ITrackCollection item) { item.TracksChanged += OnChanged; IReadOnlyList>? added = null; IReadOnlyList>? removed = null; await item.AddTrackAsync(new MockTrack(), 0); Assert.IsNotNull(added); Assert.AreEqual(1, added.Count); Assert.IsInstanceOfType(added[0].Data, typeof(T)); Assert.IsInstanceOfType(added[0].Data, typeof(ITrack)); added = null; var trackCollection = await item.GetTracksAsync(1, 0).ToListAsync(); Assert.IsInstanceOfType(trackCollection[0], typeof(T)); Assert.IsInstanceOfType(trackCollection[0], typeof(ITrack)); await item.RemoveTrackAsync(0); Assert.IsNotNull(removed); Assert.AreEqual(1, removed.Count); Assert.IsInstanceOfType(removed[0].Data, typeof(T)); Assert.IsInstanceOfType(removed[0].Data, typeof(ITrack)); removed = null; item.TracksChanged -= OnChanged; void OnChanged(object sender, IReadOnlyList> addedItems, IReadOnlyList> removedItems) { if (addedItems.Count > 0) added = addedItems; if (removedItems.Count > 0) removed = removedItems; } } public async static Task VerifyReturnsAsync(IPlaylistCollection item) { await VerifyReturnsAsync((IImageCollection)item); await VerifyReturnsAsync((IUrlCollection)item); item.PlaylistItemsChanged += OnPlaylistItemsChanged; IReadOnlyList>? added = null; IReadOnlyList>? removed = null; await item.AddPlaylistItemAsync(new MockPlaylist(), 0); Assert.IsNotNull(added); Assert.AreEqual(1, added.Count); Assert.IsInstanceOfType(added[0].Data, typeof(T)); Assert.IsInstanceOfType(added[0].Data, typeof(IPlaylist)); added = null; var playlistItems = await item.GetPlaylistItemsAsync(1, 0).ToListAsync();; Assert.IsInstanceOfType(playlistItems[0], typeof(T)); Assert.IsInstanceOfType(playlistItems[0], typeof(IPlaylist)); await item.RemovePlaylistItemAsync(0); Assert.IsNotNull(removed); Assert.AreEqual(1, removed.Count); Assert.IsInstanceOfType(removed[0].Data, typeof(T)); Assert.IsInstanceOfType(removed[0].Data, typeof(IPlaylist)); removed = null; await item.AddPlaylistItemAsync(new MockPlaylistCollection(), 0); Assert.IsNotNull(added); Assert.AreEqual(1, added.Count); Assert.IsInstanceOfType(added[0].Data, typeof(T)); Assert.IsInstanceOfType(added[0].Data, typeof(IPlaylistCollection)); added = null; var playlistCollection = await item.GetPlaylistItemsAsync(1, 0).ToListAsync(); Assert.IsInstanceOfType(playlistCollection[0], typeof(T)); Assert.IsInstanceOfType(playlistCollection[0], typeof(IPlaylistCollection)); await item.RemovePlaylistItemAsync(0); Assert.IsNotNull(removed); Assert.AreEqual(1, removed.Count); Assert.IsInstanceOfType(removed[0].Data, typeof(T)); Assert.IsInstanceOfType(removed[0].Data, typeof(IPlaylistCollection)); removed = null; item.PlaylistItemsChanged -= OnPlaylistItemsChanged; void OnPlaylistItemsChanged(object sender, IReadOnlyList> addedItems, IReadOnlyList> removedItems) { if (addedItems.Count > 0) added = addedItems; if (removedItems.Count > 0) removed = removedItems; } } public async static Task VerifyReturnsAsync(IUrlCollection item) { item.UrlsChanged += OnChanged; IReadOnlyList>? added = null; IReadOnlyList>? removed = null; await item.AddUrlAsync(new MockUrl(), 0); Assert.IsNotNull(added); Assert.AreEqual(1, added.Count); Assert.IsInstanceOfType(added[0].Data, typeof(T)); Assert.IsInstanceOfType(added[0].Data, typeof(IUrl)); added = null; var urlCollection = await item.GetUrlsAsync(1, 0).ToListAsync(); Assert.IsInstanceOfType(urlCollection[0], typeof(T)); Assert.IsInstanceOfType(urlCollection[0], typeof(IUrl)); await item.RemoveUrlAsync(0); Assert.IsNotNull(removed); Assert.AreEqual(1, removed.Count); Assert.IsInstanceOfType(removed[0].Data, typeof(T)); Assert.IsInstanceOfType(removed[0].Data, typeof(IUrl)); removed = null; item.UrlsChanged -= OnChanged; void OnChanged(object sender, IReadOnlyList> addedItems, IReadOnlyList> removedItems) { if (addedItems.Count > 0) added = addedItems; if (removedItems.Count > 0) removed = removedItems; } } public async static Task VerifyReturnsAsync(IImageCollection item) { item.ImagesChanged += OnChanged; IReadOnlyList>? added = null; IReadOnlyList>? removed = null; await item.AddImageAsync(new MockImage(), 0); Assert.IsNotNull(added); Assert.AreEqual(1, added.Count); Assert.IsInstanceOfType(added[0].Data, typeof(T)); Assert.IsInstanceOfType(added[0].Data, typeof(IImage)); added = null; var imageCollection = await item.GetImagesAsync(1, 0).ToListAsync(); Assert.IsInstanceOfType(imageCollection[0], typeof(T)); Assert.IsInstanceOfType(imageCollection[0], typeof(IImage)); await item.RemoveImageAsync(0); Assert.IsNotNull(removed); Assert.AreEqual(1, removed.Count); Assert.IsInstanceOfType(removed[0].Data, typeof(T)); Assert.IsInstanceOfType(removed[0].Data, typeof(IImage)); removed = null; item.ImagesChanged -= OnChanged; void OnChanged(object sender, IReadOnlyList> addedItems, IReadOnlyList> removedItems) { if (addedItems.Count > 0) added = addedItems; if (removedItems.Count > 0) removed = removedItems; } } public async static Task VerifyReturnsAsync(IGenreCollection item) { item.GenresChanged += OnChanged; IReadOnlyList>? added = null; IReadOnlyList>? removed = null; await item.AddGenreAsync(new MockGenre(), 0); Assert.IsNotNull(added); Assert.AreEqual(1, added.Count); Assert.IsInstanceOfType(added[0].Data, typeof(T)); Assert.IsInstanceOfType(added[0].Data, typeof(IGenre)); added = null; var genreCollection = await item.GetGenresAsync(1, 0).ToListAsync(); Assert.IsInstanceOfType(genreCollection[0], typeof(T)); Assert.IsInstanceOfType(genreCollection[0], typeof(IGenre)); await item.RemoveGenreAsync(0); Assert.IsNotNull(removed); Assert.AreEqual(1, removed.Count); Assert.IsInstanceOfType(removed[0].Data, typeof(T)); Assert.IsInstanceOfType(removed[0].Data, typeof(IGenre)); removed = null; item.GenresChanged -= OnChanged; void OnChanged(object sender, IReadOnlyList> addedItems, IReadOnlyList> removedItems) { if (addedItems.Count > 0) added = addedItems; if (removedItems.Count > 0) removed = removedItems; } } }