adding PlayerTimeline tests

This commit is contained in:
andy 2021-10-04 17:21:48 +01:00
parent 02af7a5e21
commit bb6ad45a98
3 changed files with 143 additions and 11 deletions

View File

@ -0,0 +1,120 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;
using Moq;
using FluentAssertions;
using SpotifyAPI.Web;
using Selector;
namespace Selector.Tests
{
public class PlayerTimelineTests
{
public static IEnumerable<object[]> CountData =>
new List<object[]>
{
new object[] {
new CurrentlyPlayingContext[]
{
Helper.CurrentPlayback(Helper.FullTrack("uri1"))
}
},
new object[] {
new CurrentlyPlayingContext[]
{
Helper.CurrentPlayback(Helper.FullTrack("uri1")),
Helper.CurrentPlayback(Helper.FullTrack("uri2")),
Helper.CurrentPlayback(Helper.FullTrack("uri3")),
}
},
};
[Theory]
[MemberData(nameof(CountData))]
public void Count(CurrentlyPlayingContext[] currentlyPlaying)
{
var timeline = new PlayerTimeline();
foreach(var i in currentlyPlaying)
{
timeline.Add(i);
}
timeline.Count.Should().Be(currentlyPlaying.Length);
}
[Fact]
public void Clear()
{
var timeline = new PlayerTimeline();
var tracks = new CurrentlyPlayingContext[]
{
Helper.CurrentPlayback(Helper.FullTrack("uri1")),
Helper.CurrentPlayback(Helper.FullTrack("uri2")),
Helper.CurrentPlayback(Helper.FullTrack("uri3")),
};
foreach (var i in tracks)
{
timeline.Add(i);
}
timeline.Clear();
timeline.Count.Should().Be(0);
}
[Fact]
public void Sort()
{
var timeline = new PlayerTimeline(){
SortOnBackDate = false
};
var earlier = Helper.CurrentPlayback(Helper.FullTrack("uri1"));
var earlierDate = DateTime.Now;
var later = Helper.CurrentPlayback(Helper.FullTrack("uri2"));
var laterDate = DateTime.Now.AddDays(2);
timeline.Add(later, laterDate);
timeline.Add(earlier, earlierDate);
timeline.Select(i => i.Item).Should().Equal(later, earlier);
timeline.Sort();
timeline.Select(i => i.Item).Should().Equal(earlier, later);
}
[Fact]
public void Sort3()
{
var timeline = new PlayerTimeline()
{
SortOnBackDate = false
};
var earlier = Helper.CurrentPlayback(Helper.FullTrack("uri1"));
var earlierDate = DateTime.Now;
var middle= Helper.CurrentPlayback(Helper.FullTrack("uri3"));
var middleDate = DateTime.Now.AddDays(1);
var later = Helper.CurrentPlayback(Helper.FullTrack("uri2"));
var laterDate = DateTime.Now.AddDays(2);
timeline.Add(later, laterDate);
timeline.Add(earlier, earlierDate);
timeline.Add(middle, middleDate);
timeline.Select(i => i.Item).Should().Equal(later, earlier, middle);
timeline.Sort();
timeline.Select(i => i.Item).Should().Equal(earlier, middle, later);
}
}
}

View File

@ -1,16 +1,19 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using SpotifyAPI.Web;
namespace Selector
{
public class PlayerTimeline : ITimeline<CurrentlyPlayingContext>
public class PlayerTimeline
: ITimeline<CurrentlyPlayingContext>,
IEnumerable<TimelineItem<CurrentlyPlayingContext>>
{
private List<TimelineItem<CurrentlyPlayingContext>> recentlyPlayed = new List<TimelineItem<CurrentlyPlayingContext>>();
public IEqual EqualityChecker { get; set; }
public bool SortOnBackDate { get; set; }
public bool SortOnBackDate { get; set; } = true;
public int Count { get => recentlyPlayed.Count; }
public void Add(CurrentlyPlayingContext item) => Add(item, DateHelper.FromUnixMilli(item.Timestamp));
@ -42,7 +45,8 @@ namespace Selector
public CurrentlyPlayingContext Get(DateTime at)
=> GetTimelineItem(at)?.Item;
public TimelineItem<CurrentlyPlayingContext> GetTimelineItem(DateTime at)
=> recentlyPlayed.Where(i => i.Time <= at).LastOrDefault();
=> recentlyPlayed
.Where(i => i.Time <= at).LastOrDefault();
public CurrentlyPlayingContext Get(FullTrack track)
=> GetAll(track)
@ -105,7 +109,8 @@ namespace Selector
.Select(t => t.Item);
private IEnumerable<TimelineItem<CurrentlyPlayingContext>> GetAllTimelineItems(Device device)
=> recentlyPlayed.Where(i => EqualityChecker.IsEqual(i.Item.Device, device));
=> recentlyPlayed
.Where(i => EqualityChecker.IsEqual(i.Item.Device, device));
public CurrentlyPlayingContext Get(Context context)
=> GetAll(context)
@ -116,7 +121,10 @@ namespace Selector
.Select(t => t.Item);
private IEnumerable<TimelineItem<CurrentlyPlayingContext>> GetAllTimelineItems(Context context)
=> recentlyPlayed.Where(i => EqualityChecker.IsEqual(i.Item.Context, context));
=> recentlyPlayed
.Where(i => EqualityChecker.IsEqual(i.Item.Context, context));
public IEnumerator<TimelineItem<CurrentlyPlayingContext>> GetEnumerator() => recentlyPlayed.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
}

View File

@ -59,12 +59,14 @@ namespace Selector
else
{
// STARTED PLAYBACK
if(previous is null && (Live.Item is FullTrack || Live.Item is FullEpisode))
if(previous is null
&& (Live.Item is FullTrack || Live.Item is FullEpisode))
{
OnPlayingChange(ListeningChangeEventArgs.From(previous, Live));
}
// STOPPED PLAYBACK
else if((previous.Item is FullTrack || previous.Item is FullEpisode) && Live is null)
else if((previous.Item is FullTrack || previous.Item is FullEpisode)
&& Live is null)
{
OnPlayingChange(ListeningChangeEventArgs.From(previous, Live));
}
@ -72,7 +74,8 @@ namespace Selector
else {
// MUSIC
if(previous.Item is FullTrack previousTrack && Live.Item is FullTrack currentTrack)
if(previous.Item is FullTrack previousTrack
&& Live.Item is FullTrack currentTrack)
{
if(!eq.IsEqual(previousTrack, currentTrack)) {
@ -88,14 +91,15 @@ namespace Selector
}
}
// CHANGED CONTENT
else if(previous.Item is FullTrack && Live.Item is FullEpisode
|| previous.Item is FullEpisode && Live.Item is FullTrack)
else if((previous.Item is FullTrack && Live.Item is FullEpisode)
|| (previous.Item is FullEpisode && Live.Item is FullTrack))
{
OnContentChange(ListeningChangeEventArgs.From(previous, Live));
OnItemChange(ListeningChangeEventArgs.From(previous, Live));
}
// PODCASTS
else if(previous.Item is FullEpisode previousEp && Live.Item is FullEpisode currentEp)
else if(previous.Item is FullEpisode previousEp
&& Live.Item is FullEpisode currentEp)
{
if(!eq.IsEqual(previousEp, currentEp)) {
OnItemChange(ListeningChangeEventArgs.From(previous, Live));