testing feature injector and factories
This commit is contained in:
parent
d139dce719
commit
c6439db00b
102
Selector.Tests/Consumer/AudioInjector.cs
Normal file
102
Selector.Tests/Consumer/AudioInjector.cs
Normal file
@ -0,0 +1,102 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
using Moq;
|
||||
using FluentAssertions;
|
||||
using SpotifyAPI.Web;
|
||||
|
||||
using Selector;
|
||||
|
||||
namespace Selector.Tests
|
||||
{
|
||||
public class AudioInjectorTests
|
||||
{
|
||||
[Fact]
|
||||
public void Subscribe()
|
||||
{
|
||||
var watcherMock = new Mock<IPlayerWatcher>();
|
||||
var spotifyMock = new Mock<ITracksClient>();
|
||||
|
||||
var featureInjector = new AudioFeatureInjector(watcherMock.Object, spotifyMock.Object);
|
||||
|
||||
featureInjector.Subscribe();
|
||||
|
||||
watcherMock.VerifyAdd(m => m.ItemChange += It.IsAny<EventHandler<ListeningChangeEventArgs>>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Unsubscribe()
|
||||
{
|
||||
var watcherMock = new Mock<IPlayerWatcher>();
|
||||
var spotifyMock = new Mock<ITracksClient>();
|
||||
|
||||
var featureInjector = new AudioFeatureInjector(watcherMock.Object, spotifyMock.Object);
|
||||
|
||||
featureInjector.Unsubscribe();
|
||||
|
||||
watcherMock.VerifyRemove(m => m.ItemChange -= It.IsAny<EventHandler<ListeningChangeEventArgs>>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SubscribeFuncArg()
|
||||
{
|
||||
var watcherMock = new Mock<IPlayerWatcher>();
|
||||
var watcherFuncArgMock = new Mock<IPlayerWatcher>();
|
||||
var spotifyMock = new Mock<ITracksClient>();
|
||||
|
||||
var featureInjector = new AudioFeatureInjector(watcherMock.Object, spotifyMock.Object);
|
||||
|
||||
featureInjector.Subscribe(watcherFuncArgMock.Object);
|
||||
|
||||
watcherFuncArgMock.VerifyAdd(m => m.ItemChange += It.IsAny<EventHandler<ListeningChangeEventArgs>>());
|
||||
watcherMock.VerifyNoOtherCalls();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UnsubscribeFuncArg()
|
||||
{
|
||||
var watcherMock = new Mock<IPlayerWatcher>();
|
||||
var watcherFuncArgMock = new Mock<IPlayerWatcher>();
|
||||
var spotifyMock = new Mock<ITracksClient>();
|
||||
|
||||
var featureInjector = new AudioFeatureInjector(watcherMock.Object, spotifyMock.Object);
|
||||
|
||||
featureInjector.Unsubscribe(watcherFuncArgMock.Object);
|
||||
|
||||
watcherFuncArgMock.VerifyRemove(m => m.ItemChange -= It.IsAny<EventHandler<ListeningChangeEventArgs>>());
|
||||
watcherMock.VerifyNoOtherCalls();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void Callback()
|
||||
{
|
||||
var watcherMock = new Mock<IPlayerWatcher>();
|
||||
var spotifyMock = new Mock<ITracksClient>();
|
||||
var timelineMock = new Mock<AnalysedTrackTimeline>();
|
||||
var eventArgsMock = new Mock<ListeningChangeEventArgs>();
|
||||
var playingMock = new Mock<CurrentlyPlayingContext>();
|
||||
var trackMock = new Mock<FullTrack>();
|
||||
var featureMock = new Mock<TrackAudioFeatures>();
|
||||
|
||||
eventArgsMock.Object.Current = playingMock.Object;
|
||||
playingMock.Object.Item = trackMock.Object;
|
||||
|
||||
spotifyMock.Setup(m => m.GetAudioFeatures(It.IsAny<string>()).Result).Returns(() => featureMock.Object);
|
||||
|
||||
var featureInjector = new AudioFeatureInjector(watcherMock.Object, spotifyMock.Object)
|
||||
{
|
||||
Timeline = timelineMock.Object
|
||||
};
|
||||
|
||||
await featureInjector.AsyncCallback(eventArgsMock.Object);
|
||||
|
||||
spotifyMock.Verify(m => m.GetAudioFeatures(It.IsAny<string>()));
|
||||
spotifyMock.VerifyNoOtherCalls();
|
||||
|
||||
timelineMock.Verify(m => m.Add(It.IsAny<AnalysedTrack>(), It.IsAny<DateTime>()), Times.Once);
|
||||
timelineMock.VerifyNoOtherCalls();
|
||||
}
|
||||
}
|
||||
}
|
31
Selector.Tests/Consumer/AudioInjectorFactory.cs
Normal file
31
Selector.Tests/Consumer/AudioInjectorFactory.cs
Normal file
@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Xunit;
|
||||
using Moq;
|
||||
using FluentAssertions;
|
||||
using SpotifyAPI.Web;
|
||||
|
||||
using Selector;
|
||||
|
||||
namespace Selector.Tests
|
||||
{
|
||||
public class AudioInjectorFactoryTests
|
||||
{
|
||||
[Fact]
|
||||
public void Get()
|
||||
{
|
||||
var logMock = new Mock<ILoggerFactory>();
|
||||
var configFactoryMock = new Mock<ISpotifyConfigFactory>();
|
||||
|
||||
var factory = new AudioFeatureInjectorFactory(logMock.Object);
|
||||
|
||||
var consumer = factory.Get(configFactoryMock.Object);
|
||||
|
||||
configFactoryMock.Verify(m => m.GetConfig(), Times.Once);
|
||||
consumer.Should().NotBeNull();
|
||||
}
|
||||
}
|
||||
}
|
50
Selector.Tests/Spotify/Providers.cs
Normal file
50
Selector.Tests/Spotify/Providers.cs
Normal file
@ -0,0 +1,50 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Xunit;
|
||||
using Moq;
|
||||
using FluentAssertions;
|
||||
using SpotifyAPI.Web;
|
||||
|
||||
using Selector;
|
||||
|
||||
namespace Selector.Tests
|
||||
{
|
||||
public class RefreshTokenFactoryProviderTests
|
||||
{
|
||||
[Fact]
|
||||
public void Init()
|
||||
{
|
||||
var provider = new RefreshTokenFactoryProvider();
|
||||
|
||||
provider.Initialised.Should().BeFalse();
|
||||
|
||||
provider.Initialise("a", "b");
|
||||
|
||||
provider.Initialised.Should().BeTrue();
|
||||
|
||||
provider.Initialise("a", "");
|
||||
|
||||
provider.Initialised.Should().BeFalse();
|
||||
|
||||
provider.Initialise(null, "b");
|
||||
|
||||
provider.Initialised.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Get()
|
||||
{
|
||||
var provider = new RefreshTokenFactoryProvider();
|
||||
|
||||
provider.Initialise("a", "b");
|
||||
|
||||
var consumer = provider.GetFactory("a");
|
||||
|
||||
consumer.Should().NotBeNull();
|
||||
consumer.Result.Should().NotBeNull();
|
||||
}
|
||||
}
|
||||
}
|
@ -38,7 +38,7 @@ namespace Selector
|
||||
Task.Run(() => { return AsyncCallback(e); }, CancelToken);
|
||||
}
|
||||
|
||||
private async Task AsyncCallback(ListeningChangeEventArgs e)
|
||||
public async Task AsyncCallback(ListeningChangeEventArgs e)
|
||||
{
|
||||
if (e.Current.Item is FullTrack track)
|
||||
{
|
||||
|
@ -9,11 +9,11 @@ namespace Selector
|
||||
{
|
||||
public static class SpotifyExtensions
|
||||
{
|
||||
public static string DisplayString(this FullTrack track) => $"{track.Name} / {track.Album.Name} / {track.Artists.DisplayString()}";
|
||||
public static string DisplayString(this SimpleAlbum album) => $"{album.Name} / {album.Artists.DisplayString()}";
|
||||
public static string DisplayString(this FullTrack track) => $"{track.Name} / {track.Album?.Name} / {track.Artists?.DisplayString()}";
|
||||
public static string DisplayString(this SimpleAlbum album) => $"{album.Name} / {album.Artists?.DisplayString()}";
|
||||
public static string DisplayString(this SimpleArtist artist) => artist.Name;
|
||||
|
||||
public static string DisplayString(this FullEpisode ep) => $"{ep.Name} / {ep.Show.DisplayString()}";
|
||||
public static string DisplayString(this FullEpisode ep) => $"{ep.Name} / {ep.Show?.DisplayString()}";
|
||||
public static string DisplayString(this SimpleShow show) => $"{show.Name} / {show.Publisher}";
|
||||
|
||||
|
||||
@ -21,11 +21,11 @@ namespace Selector
|
||||
|
||||
if (currentPlaying.Item is FullTrack track)
|
||||
{
|
||||
return $"{currentPlaying.IsPlaying}, {track.DisplayString()}, {currentPlaying.Device.DisplayString()}";
|
||||
return $"{currentPlaying.IsPlaying}, {track.DisplayString()}, {currentPlaying.Device?.DisplayString()}";
|
||||
}
|
||||
else if (currentPlaying.Item is FullEpisode episode)
|
||||
{
|
||||
return $"{currentPlaying.IsPlaying}, {episode.DisplayString()}, {currentPlaying.Device.DisplayString()}";
|
||||
return $"{currentPlaying.IsPlaying}, {episode.DisplayString()}, {currentPlaying.Device?.DisplayString()}";
|
||||
}
|
||||
else
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user