2021-11-12 13:44:48 +01:00
|
|
|
using System;
|
2021-10-11 12:34:18 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Controller.Entities.Movies;
|
|
|
|
using MediaBrowser.Controller.Entities.TV;
|
|
|
|
using MediaBrowser.Controller.MediaEncoding;
|
|
|
|
using MediaBrowser.Model.Drawing;
|
|
|
|
using MediaBrowser.Model.Dto;
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
using MediaBrowser.Providers.MediaInfo;
|
|
|
|
using Moq;
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
namespace Jellyfin.Providers.Tests.MediaInfo
|
|
|
|
{
|
|
|
|
public class EmbeddedImageProviderTests
|
|
|
|
{
|
|
|
|
[Theory]
|
2021-11-12 13:44:48 +01:00
|
|
|
[InlineData(typeof(AudioBook))]
|
|
|
|
[InlineData(typeof(BoxSet))]
|
|
|
|
[InlineData(typeof(Series))]
|
|
|
|
[InlineData(typeof(Season))]
|
|
|
|
[InlineData(typeof(Episode), ImageType.Primary)]
|
|
|
|
[InlineData(typeof(Movie), ImageType.Logo, ImageType.Backdrop, ImageType.Primary)]
|
|
|
|
public void GetSupportedImages_AnyBaseItem_ReturnsExpected(Type type, params ImageType[] expected)
|
2021-10-11 12:34:18 +02:00
|
|
|
{
|
2021-11-12 13:44:48 +01:00
|
|
|
BaseItem item = (BaseItem)Activator.CreateInstance(type)!;
|
|
|
|
var embeddedImageProvider = new EmbeddedImageProvider(Mock.Of<IMediaEncoder>());
|
2021-10-11 12:34:18 +02:00
|
|
|
var actual = embeddedImageProvider.GetSupportedImages(item);
|
|
|
|
Assert.Equal(expected.OrderBy(i => i.ToString()), actual.OrderBy(i => i.ToString()));
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
2021-11-12 13:44:48 +01:00
|
|
|
public async void GetImage_NoStreams_ReturnsNoImage()
|
2021-10-11 12:34:18 +02:00
|
|
|
{
|
2021-11-12 13:44:48 +01:00
|
|
|
var embeddedImageProvider = new EmbeddedImageProvider(null);
|
2021-10-11 12:34:18 +02:00
|
|
|
|
2021-10-11 21:25:12 +02:00
|
|
|
var input = GetMovie(new List<MediaAttachment>(), new List<MediaStream>());
|
2021-10-11 12:34:18 +02:00
|
|
|
|
2021-10-11 21:25:12 +02:00
|
|
|
var actual = await embeddedImageProvider.GetImage(input, ImageType.Primary, CancellationToken.None);
|
2021-10-11 12:34:18 +02:00
|
|
|
Assert.NotNull(actual);
|
|
|
|
Assert.False(actual.HasImage);
|
|
|
|
}
|
|
|
|
|
2021-11-12 13:44:48 +01:00
|
|
|
[Theory]
|
|
|
|
[InlineData("unmatched", null, 1, ImageType.Primary, null)] // doesn't default on no match
|
|
|
|
[InlineData("clearlogo.png", null, 1, ImageType.Logo, ImageFormat.Png)] // extract extension from name
|
|
|
|
[InlineData("backdrop", "image/bmp", 2, ImageType.Backdrop, ImageFormat.Bmp)] // extract extension from mimetype
|
|
|
|
[InlineData("poster", null, 3, ImageType.Primary, ImageFormat.Jpg)] // default extension to jpg
|
|
|
|
public async void GetImage_Attachment_ReturnsCorrectSelection(string filename, string mimetype, int targetIndex, ImageType type, ImageFormat? format)
|
2021-10-11 12:34:18 +02:00
|
|
|
{
|
2021-11-12 13:44:48 +01:00
|
|
|
var attachments = new List<MediaAttachment>();
|
|
|
|
string pathPrefix = "path";
|
|
|
|
for (int i = 1; i <= targetIndex; i++)
|
|
|
|
{
|
|
|
|
var name = i == targetIndex ? filename : "unmatched";
|
2021-11-12 16:11:15 +01:00
|
|
|
attachments.Add(new ()
|
2021-11-12 13:44:48 +01:00
|
|
|
{
|
|
|
|
FileName = name,
|
|
|
|
MimeType = mimetype,
|
|
|
|
Index = i
|
|
|
|
});
|
|
|
|
}
|
2021-10-11 12:34:18 +02:00
|
|
|
|
|
|
|
var mediaEncoder = new Mock<IMediaEncoder>(MockBehavior.Strict);
|
2021-11-12 16:11:15 +01:00
|
|
|
mediaEncoder.Setup(encoder => encoder.ExtractVideoImage(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<MediaSourceInfo>(), It.IsAny<MediaStream>(), It.IsAny<int>(), It.IsAny<ImageFormat>(), It.IsAny<CancellationToken>()))
|
|
|
|
.Returns<string, string, MediaSourceInfo, MediaStream, int, ImageFormat, CancellationToken>((_, _, _, _, index, ext, _) => Task.FromResult(pathPrefix + index + "." + ext));
|
2021-11-12 13:44:48 +01:00
|
|
|
var embeddedImageProvider = new EmbeddedImageProvider(mediaEncoder.Object);
|
2021-10-11 12:34:18 +02:00
|
|
|
|
2021-11-12 13:44:48 +01:00
|
|
|
var input = GetMovie(attachments, new List<MediaStream>());
|
2021-10-11 12:34:18 +02:00
|
|
|
|
2021-11-12 13:44:48 +01:00
|
|
|
var actual = await embeddedImageProvider.GetImage(input, type, CancellationToken.None);
|
2021-10-11 21:25:12 +02:00
|
|
|
Assert.NotNull(actual);
|
2021-11-12 13:44:48 +01:00
|
|
|
if (format == null)
|
|
|
|
{
|
|
|
|
Assert.False(actual.HasImage);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Assert.True(actual.HasImage);
|
2021-11-12 16:11:15 +01:00
|
|
|
Assert.Equal(pathPrefix + targetIndex + "." + format, actual.Path, StringComparer.OrdinalIgnoreCase);
|
2021-11-12 13:44:48 +01:00
|
|
|
Assert.Equal(format, actual.Format);
|
|
|
|
}
|
2021-10-11 21:25:12 +02:00
|
|
|
}
|
|
|
|
|
2021-11-12 13:44:48 +01:00
|
|
|
[Theory]
|
|
|
|
[InlineData(null, 1, ImageType.Backdrop, false)] // no label, can only find primary
|
|
|
|
[InlineData(null, 1, ImageType.Primary, true)] // no label, finds primary
|
|
|
|
[InlineData("backdrop", 2, ImageType.Backdrop, true)] // uses label to find index 2, not just pulling first stream
|
|
|
|
[InlineData("cover", 2, ImageType.Primary, true)] // uses label to find index 2, not just pulling first stream
|
|
|
|
public async void GetImage_Embedded_ReturnsCorrectSelection(string label, int targetIndex, ImageType type, bool hasImage)
|
2021-10-11 12:34:18 +02:00
|
|
|
{
|
2021-11-12 13:44:48 +01:00
|
|
|
var streams = new List<MediaStream>();
|
|
|
|
for (int i = 1; i <= targetIndex; i++)
|
|
|
|
{
|
|
|
|
var comment = i == targetIndex ? label : "unmatched";
|
2021-11-12 16:11:15 +01:00
|
|
|
streams.Add(new ()
|
2021-11-12 13:44:48 +01:00
|
|
|
{
|
|
|
|
Type = MediaStreamType.EmbeddedImage,
|
|
|
|
Index = i,
|
|
|
|
Comment = comment
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
var pathPrefix = "path";
|
2021-10-11 12:34:18 +02:00
|
|
|
var mediaEncoder = new Mock<IMediaEncoder>(MockBehavior.Strict);
|
2021-11-12 16:11:15 +01:00
|
|
|
mediaEncoder.Setup(encoder => encoder.ExtractVideoImage(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<MediaSourceInfo>(), It.IsAny<MediaStream>(), It.IsAny<int>(), It.IsAny<ImageFormat>(), It.IsAny<CancellationToken>()))
|
|
|
|
.Returns<string, string, MediaSourceInfo, MediaStream, int, ImageFormat, CancellationToken>((_, _, _, stream, index, ext, _) =>
|
2021-11-12 13:44:48 +01:00
|
|
|
{
|
|
|
|
Assert.Equal(streams[index - 1], stream);
|
2021-11-12 16:11:15 +01:00
|
|
|
return Task.FromResult(pathPrefix + index + "." + ext);
|
2021-11-12 13:44:48 +01:00
|
|
|
});
|
|
|
|
var embeddedImageProvider = new EmbeddedImageProvider(mediaEncoder.Object);
|
2021-10-11 12:34:18 +02:00
|
|
|
|
2021-11-12 13:44:48 +01:00
|
|
|
var input = GetMovie(new List<MediaAttachment>(), streams);
|
2021-10-11 12:34:18 +02:00
|
|
|
|
2021-11-12 13:44:48 +01:00
|
|
|
var actual = await embeddedImageProvider.GetImage(input, type, CancellationToken.None);
|
2021-10-11 12:34:18 +02:00
|
|
|
Assert.NotNull(actual);
|
2021-11-12 13:44:48 +01:00
|
|
|
Assert.Equal(hasImage, actual.HasImage);
|
|
|
|
if (hasImage)
|
|
|
|
{
|
2021-11-12 16:11:15 +01:00
|
|
|
Assert.Equal(pathPrefix + targetIndex + ".jpg", actual.Path, StringComparer.OrdinalIgnoreCase);
|
2021-11-12 13:44:48 +01:00
|
|
|
Assert.Equal(ImageFormat.Jpg, actual.Format);
|
|
|
|
}
|
2021-10-11 21:25:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private static Movie GetMovie(List<MediaAttachment> mediaAttachments, List<MediaStream> mediaStreams)
|
|
|
|
{
|
|
|
|
// Mocking IMediaSourceManager GetMediaAttachments and GetMediaStreams instead of mocking Movie works, but
|
|
|
|
// has concurrency problems between this and VideoImageProviderTests due to BaseItem.MediaSourceManager
|
|
|
|
// being static
|
|
|
|
var movie = new Mock<Movie>();
|
|
|
|
|
|
|
|
movie.Setup(item => item.GetMediaSources(It.IsAny<bool>()))
|
|
|
|
.Returns(new List<MediaSourceInfo> { new () { MediaAttachments = mediaAttachments } } );
|
|
|
|
movie.Setup(item => item.GetMediaStreams())
|
|
|
|
.Returns(mediaStreams);
|
|
|
|
|
|
|
|
return movie.Object;
|
|
|
|
}
|
2021-10-11 12:34:18 +02:00
|
|
|
}
|
|
|
|
}
|