Selector/Selector.Tests/Consumer/AudioInjector.cs

132 lines
4.7 KiB
C#
Raw Normal View History

2021-10-15 23:38:24 +01:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
using Moq;
using FluentAssertions;
using SpotifyAPI.Web;
using Selector;
2023-01-06 21:22:08 +00:00
using System.Threading;
2021-10-15 23:38:24 +01:00
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]
2022-06-18 11:02:02 +01:00
public async void CallbackNoId()
2021-10-15 23:38:24 +01:00
{
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;
2023-01-06 21:22:08 +00:00
spotifyMock.Setup(m => m.GetAudioFeatures(It.IsAny<string>(), It.IsAny<CancellationToken>()).Result).Returns(() => featureMock.Object);
2021-10-15 23:38:24 +01:00
var featureInjector = new AudioFeatureInjector(watcherMock.Object, spotifyMock.Object)
{
Timeline = timelineMock.Object
};
await featureInjector.AsyncCallback(eventArgsMock.Object);
2022-06-18 11:02:02 +01:00
spotifyMock.VerifyNoOtherCalls();
timelineMock.VerifyNoOtherCalls();
}
[Fact]
public async void CallbackWithId()
{
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;
trackMock.Object.Id = "Fake-Id";
2023-01-06 21:22:08 +00:00
spotifyMock.Setup(m => m.GetAudioFeatures(It.IsAny<string>(), It.IsAny<CancellationToken>()).Result).Returns(() => featureMock.Object);
2022-06-18 11:02:02 +01:00
var featureInjector = new AudioFeatureInjector(watcherMock.Object, spotifyMock.Object)
{
Timeline = timelineMock.Object
};
await featureInjector.AsyncCallback(eventArgsMock.Object);
2023-01-06 21:22:08 +00:00
spotifyMock.Verify(m => m.GetAudioFeatures(It.IsAny<string>(), It.IsAny<CancellationToken>()));
2021-10-15 23:38:24 +01:00
spotifyMock.VerifyNoOtherCalls();
timelineMock.Verify(m => m.Add(It.IsAny<AnalysedTrack>(), It.IsAny<DateTime>()), Times.Once);
timelineMock.VerifyNoOtherCalls();
}
}
}