Selector/Selector.Tests/Watcher/PlayerWatcher.cs

242 lines
12 KiB
C#
Raw Normal View History

2021-09-25 21:00:58 +01:00
using System;
using System.Collections.Generic;
using Xunit;
using Moq;
using FluentAssertions;
using SpotifyAPI.Web;
using System.Threading;
2021-10-07 22:09:10 +01:00
using System.Threading.Tasks;
using Xunit.Sdk;
2021-09-25 21:00:58 +01:00
namespace Selector.Tests
{
public class PlayerWatcherTests
{
2021-09-30 18:07:35 +01:00
public static IEnumerable<object[]> NowPlayingData =>
2021-09-26 12:11:44 +01:00
new List<object[]>
{
2021-09-28 21:14:52 +01:00
new object[] { new List<CurrentlyPlayingContext>(){
Helper.CurrentPlayback(Helper.FullTrack("track1", "album1", "artist1")),
Helper.CurrentPlayback(Helper.FullTrack("track2", "album2", "artist2")),
Helper.CurrentPlayback(Helper.FullTrack("track3", "album3", "artist3")),
2021-09-30 18:07:35 +01:00
}
2021-09-26 12:11:44 +01:00
}
};
2021-09-25 21:00:58 +01:00
2021-09-26 12:11:44 +01:00
[Theory]
[MemberData(nameof(NowPlayingData))]
2021-09-28 21:14:52 +01:00
public async void NowPlaying(List<CurrentlyPlayingContext> playing)
2021-09-26 12:11:44 +01:00
{
2021-09-28 21:14:52 +01:00
var playingQueue = new Queue<CurrentlyPlayingContext>(playing);
2021-09-25 21:00:58 +01:00
2021-09-26 12:11:44 +01:00
var spotMock = new Mock<IPlayerClient>();
2021-10-01 22:01:28 +01:00
var eq = new UriEqual();
2021-09-25 21:00:58 +01:00
2023-01-06 21:22:08 +00:00
spotMock.Setup(s => s.GetCurrentPlayback(It.IsAny<CancellationToken>()).Result).Returns(playingQueue.Dequeue);
2021-09-25 21:00:58 +01:00
var watcher = new PlayerWatcher(spotMock.Object, eq);
2021-09-25 21:00:58 +01:00
2021-09-30 18:07:35 +01:00
for (var i = 0; i < playing.Count; i++)
2021-09-26 12:11:44 +01:00
{
await watcher.WatchOne();
watcher.Live.Should().Be(playing[i]);
2021-09-26 12:11:44 +01:00
}
}
2021-09-30 18:07:35 +01:00
public static IEnumerable<object[]> EventsData =>
2021-09-26 12:11:44 +01:00
new List<object[]>
{
// NO CHANGING
2021-09-28 21:14:52 +01:00
new object[] { new List<CurrentlyPlayingContext>(){
2021-10-01 22:01:28 +01:00
Helper.CurrentPlayback(Helper.FullTrack("nochange", "album1", "artist1"), isPlaying: true, context: "context1"),
Helper.CurrentPlayback(Helper.FullTrack("nochange", "album1", "artist1"), isPlaying: true, context: "context1"),
Helper.CurrentPlayback(Helper.FullTrack("nochange", "album1", "artist1"), isPlaying: true, context: "context1"),
2021-09-26 12:11:44 +01:00
},
// to raise
2021-12-18 22:19:11 +00:00
new List<string>(){ "ItemChange", "ContextChange", "PlayingChange", "DeviceChange", "VolumeChange" },
2021-09-26 12:11:44 +01:00
// to not raise
2021-12-18 22:19:11 +00:00
new List<string>(){ "AlbumChange", "ArtistChange" }
2021-09-26 12:11:44 +01:00
},
// TRACK CHANGE
2021-09-28 21:14:52 +01:00
new object[] { new List<CurrentlyPlayingContext>(){
2021-10-01 22:01:28 +01:00
Helper.CurrentPlayback(Helper.FullTrack("trackchange1", "album1", "artist1")),
Helper.CurrentPlayback(Helper.FullTrack("trackchange2", "album1", "artist1"))
2021-09-26 12:11:44 +01:00
},
// to raise
2021-12-18 22:19:11 +00:00
new List<string>(){ "ContextChange", "PlayingChange", "ItemChange", "DeviceChange", "VolumeChange" },
2021-09-26 12:11:44 +01:00
// to not raise
2021-12-18 22:19:11 +00:00
new List<string>(){ "AlbumChange", "ArtistChange" }
2021-09-26 12:11:44 +01:00
},
// ALBUM CHANGE
2021-09-28 21:14:52 +01:00
new object[] { new List<CurrentlyPlayingContext>(){
2021-10-01 22:01:28 +01:00
Helper.CurrentPlayback(Helper.FullTrack("albumchange", "album1", "artist1")),
Helper.CurrentPlayback(Helper.FullTrack("albumchange", "album2", "artist1"))
2021-09-26 12:11:44 +01:00
},
// to raise
2021-12-18 22:19:11 +00:00
new List<string>(){ "ContextChange", "PlayingChange", "ItemChange", "AlbumChange", "DeviceChange", "VolumeChange" },
2021-09-26 12:11:44 +01:00
// to not raise
2021-12-18 22:19:11 +00:00
new List<string>(){ "ArtistChange" }
2021-09-26 12:11:44 +01:00
},
// ARTIST CHANGE
2021-09-28 21:14:52 +01:00
new object[] { new List<CurrentlyPlayingContext>(){
2021-10-01 22:01:28 +01:00
Helper.CurrentPlayback(Helper.FullTrack("artistchange", "album1", "artist1")),
Helper.CurrentPlayback(Helper.FullTrack("artistchange", "album1", "artist2"))
2021-09-26 12:11:44 +01:00
},
// to raise
2021-12-18 22:19:11 +00:00
new List<string>(){ "ContextChange", "PlayingChange", "ItemChange", "ArtistChange", "DeviceChange", "VolumeChange" },
2021-09-26 12:11:44 +01:00
// to not raise
2021-12-18 22:19:11 +00:00
new List<string>(){ "AlbumChange" }
2021-09-26 12:11:44 +01:00
},
// CONTEXT CHANGE
2021-09-28 21:14:52 +01:00
new object[] { new List<CurrentlyPlayingContext>(){
2021-10-01 22:01:28 +01:00
Helper.CurrentPlayback(Helper.FullTrack("contextchange", "album1", "artist1"), context: "context1"),
Helper.CurrentPlayback(Helper.FullTrack("contextchange", "album1", "artist1"), context: "context2")
2021-09-26 12:11:44 +01:00
},
// to raise
2021-12-18 22:19:11 +00:00
new List<string>(){ "PlayingChange", "ItemChange", "ContextChange", "DeviceChange", "VolumeChange" },
2021-09-26 12:11:44 +01:00
// to not raise
2021-12-18 22:19:11 +00:00
new List<string>(){ "AlbumChange", "ArtistChange" }
2021-09-26 12:11:44 +01:00
},
// PLAYING CHANGE
2021-09-28 21:14:52 +01:00
new object[] { new List<CurrentlyPlayingContext>(){
2021-10-01 22:01:28 +01:00
Helper.CurrentPlayback(Helper.FullTrack("playingchange1", "album1", "artist1"), isPlaying: true, context: "context1"),
Helper.CurrentPlayback(Helper.FullTrack("playingchange1", "album1", "artist1"), isPlaying: false, context: "context1")
2021-09-26 12:11:44 +01:00
},
// to raise
2021-12-18 22:19:11 +00:00
new List<string>(){ "ContextChange", "ItemChange", "PlayingChange", "DeviceChange", "VolumeChange" },
2021-09-26 12:11:44 +01:00
// to not raise
2021-12-18 22:19:11 +00:00
new List<string>(){ "AlbumChange", "ArtistChange" }
},
// PLAYING CHANGE
2021-09-28 21:14:52 +01:00
new object[] { new List<CurrentlyPlayingContext>(){
2021-10-01 22:01:28 +01:00
Helper.CurrentPlayback(Helper.FullTrack("playingchange2", "album1", "artist1"), isPlaying: false, context: "context1"),
Helper.CurrentPlayback(Helper.FullTrack("playingchange2", "album1", "artist1"), isPlaying: true, context: "context1")
},
// to raise
2021-12-18 22:19:11 +00:00
new List<string>(){ "ContextChange", "ItemChange", "PlayingChange", "DeviceChange", "VolumeChange" },
// to not raise
2021-12-18 22:19:11 +00:00
new List<string>(){ "AlbumChange", "ArtistChange" }
},
// CONTENT CHANGE
2021-09-28 21:14:52 +01:00
new object[] { new List<CurrentlyPlayingContext>(){
2021-10-01 22:01:28 +01:00
Helper.CurrentPlayback(Helper.FullTrack("contentchange1", "album1", "artist1"), isPlaying: true, context: "context1"),
Helper.CurrentPlayback(Helper.FullEpisode("contentchange1", "show1", "pub1"), isPlaying: true, context: "context2")
},
// to raise
2021-12-18 22:19:11 +00:00
new List<string>(){ "PlayingChange", "ContentChange", "ContextChange", "ItemChange", "DeviceChange", "VolumeChange" },
// to not raise
2021-12-18 22:19:11 +00:00
new List<string>(){ "AlbumChange", "ArtistChange" }
},
// CONTENT CHANGE
2021-09-28 21:14:52 +01:00
new object[] { new List<CurrentlyPlayingContext>(){
2021-10-01 22:01:28 +01:00
Helper.CurrentPlayback(Helper.FullEpisode("contentchange1", "show1", "pub1"), isPlaying: true, context: "context2"),
Helper.CurrentPlayback(Helper.FullTrack("contentchange1", "album1", "artist1"), isPlaying: true, context: "context1")
},
// to raise
2021-12-18 22:19:11 +00:00
new List<string>(){ "PlayingChange", "ContentChange", "ContextChange", "ItemChange", "DeviceChange", "VolumeChange" },
// to not raise
2021-12-18 22:19:11 +00:00
new List<string>(){ "AlbumChange", "ArtistChange" }
2021-09-28 21:14:52 +01:00
},
// DEVICE CHANGE
new object[] { new List<CurrentlyPlayingContext>(){
2021-10-01 22:01:28 +01:00
Helper.CurrentPlayback(Helper.FullTrack("devicechange", "album1", "artist1"), device: Helper.Device("dev1")),
Helper.CurrentPlayback(Helper.FullTrack("devicechange", "album1", "artist1"), device: Helper.Device("dev2"))
2021-09-28 21:14:52 +01:00
},
// to raise
2021-12-18 22:19:11 +00:00
new List<string>(){ "ContextChange", "PlayingChange", "ItemChange", "VolumeChange", "DeviceChange" },
2021-09-28 21:14:52 +01:00
// to not raise
2021-12-18 22:19:11 +00:00
new List<string>(){ "AlbumChange", "ArtistChange", "ContentChange" }
2021-09-28 21:14:52 +01:00
},
// VOLUME CHANGE
new object[] { new List<CurrentlyPlayingContext>(){
2021-10-01 22:01:28 +01:00
Helper.CurrentPlayback(Helper.FullTrack("volumechange", "album1", "artist1"), device: Helper.Device("dev1", volume: 50)),
Helper.CurrentPlayback(Helper.FullTrack("volumechange", "album1", "artist1"), device: Helper.Device("dev1", volume: 60))
2021-09-28 21:14:52 +01:00
},
// to raise
2021-12-18 22:19:11 +00:00
new List<string>(){ "ContextChange", "PlayingChange", "ItemChange", "VolumeChange", "DeviceChange" },
2021-09-28 21:14:52 +01:00
// to not raise
2021-12-18 22:19:11 +00:00
new List<string>(){ "AlbumChange", "ArtistChange", "ContentChange" }
2021-09-28 21:14:52 +01:00
},
// // STARTED PLAYBACK
// new object[] { new List<CurrentlyPlayingContext>(){
// null,
// Helper.CurrentPlayback(Helper.FullTrack("track1", "album1", "artist1"), isPlaying: true, context: "context1")
// },
// // to raise
// new List<string>(){ "PlayingChange" },
// // to not raise
2021-09-30 18:07:35 +01:00
// new List<string>(){ "AlbumChange", "ArtistChange", "ContentChange", "ContextChange", "ItemChange", "DeviceChange", "VolumeChange" }
2021-09-28 21:14:52 +01:00
// },
// // STARTED PLAYBACK
// new object[] { new List<CurrentlyPlayingContext>(){
// Helper.CurrentPlayback(Helper.FullTrack("track1", "album1", "artist1"), isPlaying: true, context: "context1"),
// null
// },
// // to raise
// new List<string>(){ "PlayingChange" },
// // to not raise
2021-09-30 18:07:35 +01:00
// new List<string>(){ "AlbumChange", "ArtistChange", "ContentChange", "ContextChange", "ItemChange", "DeviceChange", "VolumeChange" }
2021-09-28 21:14:52 +01:00
// }
2021-09-26 12:11:44 +01:00
};
[Theory]
[MemberData(nameof(EventsData))]
2021-09-28 21:14:52 +01:00
public async void Events(List<CurrentlyPlayingContext> playing, List<string> toRaise, List<string> toNotRaise)
2021-09-26 12:11:44 +01:00
{
2021-09-28 21:14:52 +01:00
var playingQueue = new Queue<CurrentlyPlayingContext>(playing);
2021-09-26 12:11:44 +01:00
var spotMock = new Mock<IPlayerClient>();
2021-10-01 22:01:28 +01:00
var eq = new UriEqual();
2021-09-26 12:11:44 +01:00
2021-09-28 21:14:52 +01:00
spotMock.Setup(
2023-01-06 21:22:08 +00:00
s => s.GetCurrentPlayback(It.IsAny<CancellationToken>()).Result
2021-09-28 21:14:52 +01:00
).Returns(playingQueue.Dequeue);
2021-09-26 12:11:44 +01:00
var watcher = new PlayerWatcher(spotMock.Object, eq);
2021-09-26 12:11:44 +01:00
using var monitoredWatcher = watcher.Monitor();
2021-09-30 18:07:35 +01:00
for (var i = 0; i < playing.Count; i++)
2021-09-26 12:11:44 +01:00
{
await watcher.WatchOne();
}
2021-09-28 21:14:52 +01:00
toRaise.ForEach(r => monitoredWatcher.Should().Raise(r).WithSender(watcher));
toNotRaise.ForEach(r => monitoredWatcher.Should().NotRaise(r));
2021-09-26 12:11:44 +01:00
}
2021-10-07 22:09:10 +01:00
[Theory]
[InlineData(1000, 3500, 4)]
[InlineData(500, 3800, 8)]
[InlineData(100, 250, 3)]
2021-10-07 22:09:10 +01:00
public async void Watch(int pollPeriod, int execTime, int numberOfCalls)
{
var spotMock = new Mock<IPlayerClient>();
var eq = new UriEqual();
var watch = new PlayerWatcher(spotMock.Object, eq)
{
PollPeriod = pollPeriod
};
var tokenSource = new CancellationTokenSource();
var task = watch.Watch(tokenSource.Token);
await Task.Delay(execTime);
tokenSource.Cancel();
2023-01-06 21:22:08 +00:00
spotMock.Verify(s => s.GetCurrentPlayback(It.IsAny<CancellationToken>()), Times.Exactly(numberOfCalls));
2021-10-07 22:09:10 +01:00
}
// [Fact]
// public async void Auth()
// {
// var spot = new SpotifyClient("");
2021-10-07 22:09:10 +01:00
// var eq = new UriEqual();
// var watch = new PlayerWatcher(spot.Player, eq);
// var token = new CancellationTokenSource();
// await watch.Watch(token.Token);
// }
2021-09-25 21:00:58 +01:00
}
}