2022-02-07 18:36:07 +01:00
|
|
|
using System;
|
2022-02-06 23:26:25 +01:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Emby.Naming.Common;
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
using MediaBrowser.Controller.MediaEncoding;
|
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
using MediaBrowser.Model.Globalization;
|
|
|
|
using MediaBrowser.Providers.MediaInfo;
|
|
|
|
using Moq;
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
namespace Jellyfin.Providers.Tests.MediaInfo
|
|
|
|
{
|
|
|
|
public class SubtitleResolverTests
|
|
|
|
{
|
2022-02-07 18:36:07 +01:00
|
|
|
private const string VideoDirectoryPath = "Test Data/Video";
|
|
|
|
private const string MetadataDirectoryPath = "Test Data/Metadata";
|
2022-02-17 09:03:08 +01:00
|
|
|
private readonly SubtitleResolver _subtitleResolver;
|
2022-02-06 23:26:25 +01:00
|
|
|
|
|
|
|
public SubtitleResolverTests()
|
|
|
|
{
|
2022-02-17 09:03:08 +01:00
|
|
|
var englishCultureDto = new CultureDto("English", "English", "en", new[] { "eng" });
|
|
|
|
var frenchCultureDto = new CultureDto("French", "French", "fr", new[] { "fre", "fra" });
|
2022-02-06 23:26:25 +01:00
|
|
|
|
|
|
|
var localizationManager = new Mock<ILocalizationManager>(MockBehavior.Loose);
|
|
|
|
localizationManager.Setup(lm => lm.FindLanguageInfo(It.IsRegex(@"en.*", RegexOptions.IgnoreCase)))
|
|
|
|
.Returns(englishCultureDto);
|
|
|
|
localizationManager.Setup(lm => lm.FindLanguageInfo(It.IsRegex(@"fr.*", RegexOptions.IgnoreCase)))
|
|
|
|
.Returns(frenchCultureDto);
|
|
|
|
|
|
|
|
var mediaEncoder = new Mock<IMediaEncoder>(MockBehavior.Strict);
|
|
|
|
mediaEncoder.Setup(me => me.GetMediaInfo(It.IsAny<MediaInfoRequest>(), It.IsAny<CancellationToken>()))
|
|
|
|
.Returns<MediaInfoRequest, CancellationToken>((_, _) => Task.FromResult(new MediaBrowser.Model.MediaInfo.MediaInfo
|
|
|
|
{
|
|
|
|
MediaStreams = new List<MediaStream>
|
|
|
|
{
|
|
|
|
new()
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
2022-02-17 09:03:08 +01:00
|
|
|
_subtitleResolver = new SubtitleResolver(localizationManager.Object, mediaEncoder.Object, new NamingOptions());
|
2022-02-06 23:26:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
2022-02-17 09:03:08 +01:00
|
|
|
public async void AddExternalStreamsAsync_GivenMixedFilenames_ReturnsValidSubtitles()
|
2022-02-06 23:26:25 +01:00
|
|
|
{
|
|
|
|
var startIndex = 0;
|
|
|
|
var index = startIndex;
|
|
|
|
var files = new[]
|
|
|
|
{
|
2022-02-07 18:36:07 +01:00
|
|
|
VideoDirectoryPath + "/MyVideo.en.srt",
|
|
|
|
VideoDirectoryPath + "/MyVideo.en.forced.default.sub",
|
|
|
|
VideoDirectoryPath + "/My.Video.mp3",
|
|
|
|
VideoDirectoryPath + "/My.Video.png",
|
|
|
|
VideoDirectoryPath + "/My.Video.srt",
|
|
|
|
VideoDirectoryPath + "/My.Video.txt",
|
|
|
|
VideoDirectoryPath + "/My.Video.vtt",
|
|
|
|
VideoDirectoryPath + "/My.Video.ass",
|
|
|
|
VideoDirectoryPath + "/My.Video.sub",
|
|
|
|
VideoDirectoryPath + "/My.Video.ssa",
|
|
|
|
VideoDirectoryPath + "/My.Video.smi",
|
|
|
|
VideoDirectoryPath + "/My.Video.sami",
|
|
|
|
VideoDirectoryPath + "/My.Video.mks",
|
|
|
|
VideoDirectoryPath + "/My.Video.en.srt",
|
|
|
|
VideoDirectoryPath + "/My.Video.default.en.srt",
|
|
|
|
VideoDirectoryPath + "/My.Video.default.forced.en.srt",
|
|
|
|
VideoDirectoryPath + "/My.Video.en.default.forced.srt",
|
|
|
|
VideoDirectoryPath + "/My.Video.en.With Additional Garbage.sub",
|
|
|
|
VideoDirectoryPath + "/My.Video.With Additional Garbage.English.sub",
|
|
|
|
VideoDirectoryPath + "/My.Video.With.Additional.Garbage.en.srt",
|
|
|
|
VideoDirectoryPath + "/Some.Other.Video.srt"
|
|
|
|
};
|
|
|
|
var metadataFiles = new[]
|
|
|
|
{
|
|
|
|
MetadataDirectoryPath + "/My.Video.en.srt"
|
2022-02-06 23:26:25 +01:00
|
|
|
};
|
|
|
|
var expectedResult = new[]
|
|
|
|
{
|
2022-02-07 18:36:07 +01:00
|
|
|
CreateMediaStream(VideoDirectoryPath + "/MyVideo.en.srt", "srt", "eng", null, index++),
|
|
|
|
CreateMediaStream(VideoDirectoryPath + "/MyVideo.en.forced.default.sub", "sub", "eng", null, index++, isDefault: true, isForced: true),
|
|
|
|
CreateMediaStream(VideoDirectoryPath + "/My.Video.srt", "srt", null, null, index++),
|
|
|
|
CreateMediaStream(VideoDirectoryPath + "/My.Video.vtt", "vtt", null, null, index++),
|
|
|
|
CreateMediaStream(VideoDirectoryPath + "/My.Video.ass", "ass", null, null, index++),
|
|
|
|
CreateMediaStream(VideoDirectoryPath + "/My.Video.sub", "sub", null, null, index++),
|
|
|
|
CreateMediaStream(VideoDirectoryPath + "/My.Video.ssa", "ssa", null, null, index++),
|
|
|
|
CreateMediaStream(VideoDirectoryPath + "/My.Video.smi", "smi", null, null, index++),
|
|
|
|
CreateMediaStream(VideoDirectoryPath + "/My.Video.sami", "sami", null, null, index++),
|
|
|
|
CreateMediaStream(VideoDirectoryPath + "/My.Video.mks", "mks", null, null, index++),
|
|
|
|
CreateMediaStream(VideoDirectoryPath + "/My.Video.en.srt", "srt", "eng", null, index++),
|
|
|
|
CreateMediaStream(VideoDirectoryPath + "/My.Video.default.en.srt", "srt", "eng", null, index++, isDefault: true),
|
|
|
|
CreateMediaStream(VideoDirectoryPath + "/My.Video.default.forced.en.srt", "srt", "eng", null, index++, isForced: true, isDefault: true),
|
|
|
|
CreateMediaStream(VideoDirectoryPath + "/My.Video.en.default.forced.srt", "srt", "eng", null, index++, isForced: true, isDefault: true),
|
|
|
|
CreateMediaStream(VideoDirectoryPath + "/My.Video.en.With Additional Garbage.sub", "sub", "eng", "With Additional Garbage", index++),
|
|
|
|
CreateMediaStream(VideoDirectoryPath + "/My.Video.With Additional Garbage.English.sub", "sub", "eng", "With Additional Garbage", index++),
|
|
|
|
CreateMediaStream(VideoDirectoryPath + "/My.Video.With.Additional.Garbage.en.srt", "srt", "eng", "With.Additional.Garbage", index++),
|
|
|
|
CreateMediaStream(MetadataDirectoryPath + "/My.Video.en.srt", "srt", "eng", null, index)
|
2022-02-06 23:26:25 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
BaseItem.MediaSourceManager = Mock.Of<IMediaSourceManager>();
|
2022-02-07 18:36:07 +01:00
|
|
|
|
|
|
|
var video = new Mock<Video>();
|
|
|
|
video.CallBase = true;
|
|
|
|
video.Setup(moq => moq.Path).Returns(VideoDirectoryPath + "/My.Video.mkv");
|
|
|
|
video.Setup(moq => moq.GetInternalMetadataPath()).Returns(MetadataDirectoryPath);
|
2022-02-06 23:26:25 +01:00
|
|
|
|
|
|
|
var directoryService = new Mock<IDirectoryService>(MockBehavior.Strict);
|
|
|
|
directoryService.Setup(ds => ds.GetFilePaths(It.IsRegex(@"Test Data[/\\]Video"), It.IsAny<bool>(), It.IsAny<bool>()))
|
|
|
|
.Returns(files);
|
2022-02-07 18:36:07 +01:00
|
|
|
directoryService.Setup(ds => ds.GetFilePaths(It.IsRegex(@"Test Data[/\\]Metadata"), It.IsAny<bool>(), It.IsAny<bool>()))
|
|
|
|
.Returns(metadataFiles);
|
2022-02-06 23:26:25 +01:00
|
|
|
|
2022-02-17 09:03:08 +01:00
|
|
|
var streams = await _subtitleResolver.GetExternalStreamsAsync(video.Object, startIndex, directoryService.Object, false, CancellationToken.None);
|
2022-02-06 23:26:25 +01:00
|
|
|
|
|
|
|
Assert.Equal(expectedResult.Length, streams.Count);
|
|
|
|
for (var i = 0; i < expectedResult.Length; i++)
|
|
|
|
{
|
|
|
|
var expected = expectedResult[i];
|
|
|
|
var actual = streams[i];
|
|
|
|
|
|
|
|
Assert.Equal(expected.Index, actual.Index);
|
|
|
|
Assert.Equal(expected.Type, actual.Type);
|
|
|
|
Assert.Equal(expected.IsExternal, actual.IsExternal);
|
|
|
|
Assert.Equal(expected.Path, actual.Path);
|
|
|
|
Assert.Equal(expected.IsDefault, actual.IsDefault);
|
|
|
|
Assert.Equal(expected.IsForced, actual.IsForced);
|
|
|
|
Assert.Equal(expected.Language, actual.Language);
|
|
|
|
Assert.Equal(expected.Title, actual.Title);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Theory]
|
2022-02-07 18:36:07 +01:00
|
|
|
[InlineData("MyVideo.en.srt", "srt", "eng", null, false, false)]
|
|
|
|
[InlineData("MyVideo.en.forced.default.srt", "srt", "eng", null, true, true)]
|
2022-02-06 23:26:25 +01:00
|
|
|
[InlineData("My.Video.srt", "srt", null, null, false, false)]
|
|
|
|
[InlineData("My.Video.foreign.srt", "srt", null, null, true, false)]
|
|
|
|
[InlineData("My.Video.default.srt", "srt", null, null, false, true)]
|
|
|
|
[InlineData("My.Video.forced.default.srt", "srt", null, null, true, true)]
|
|
|
|
[InlineData("My.Video.en.srt", "srt", "eng", null, false, false)]
|
|
|
|
[InlineData("My.Video.fr.en.srt", "srt", "eng", "fr", false, false)]
|
|
|
|
[InlineData("My.Video.en.fr.srt", "srt", "fre", "en", false, false)]
|
|
|
|
[InlineData("My.Video.default.en.srt", "srt", "eng", null, false, true)]
|
|
|
|
[InlineData("My.Video.default.forced.en.srt", "srt", "eng", null, true, true)]
|
|
|
|
[InlineData("My.Video.en.default.forced.srt", "srt", "eng", null, true, true)]
|
|
|
|
[InlineData("My.Video.Track Label.srt", "srt", null, "Track Label", false, false)]
|
2022-02-07 18:36:07 +01:00
|
|
|
[InlineData("My.Video.Track.Label.srt", "srt", null, "Track.Label", false, false)]
|
2022-02-06 23:26:25 +01:00
|
|
|
[InlineData("My.Video.Track Label.en.default.forced.srt", "srt", "eng", "Track Label", true, true)]
|
|
|
|
[InlineData("My.Video.en.default.forced.Track Label.srt", "srt", "eng", "Track Label", true, true)]
|
2022-02-17 09:03:08 +01:00
|
|
|
public async void AddExternalStreamsAsync_GivenSingleFile_ReturnsExpectedSubtitle(string file, string codec, string? language, string? title, bool isForced, bool isDefault)
|
2022-02-06 23:26:25 +01:00
|
|
|
{
|
|
|
|
BaseItem.MediaSourceManager = Mock.Of<IMediaSourceManager>();
|
2022-02-07 18:36:07 +01:00
|
|
|
|
|
|
|
var video = new Mock<Video>();
|
|
|
|
video.CallBase = true;
|
|
|
|
video.Setup(moq => moq.Path).Returns(VideoDirectoryPath + "/My.Video.mkv");
|
|
|
|
video.Setup(moq => moq.GetInternalMetadataPath()).Returns(MetadataDirectoryPath);
|
2022-02-06 23:26:25 +01:00
|
|
|
|
|
|
|
var directoryService = new Mock<IDirectoryService>(MockBehavior.Strict);
|
|
|
|
directoryService.Setup(ds => ds.GetFilePaths(It.IsRegex(@"Test Data[/\\]Video"), It.IsAny<bool>(), It.IsAny<bool>()))
|
2022-02-07 18:36:07 +01:00
|
|
|
.Returns(new[] { VideoDirectoryPath + "/" + file });
|
|
|
|
directoryService.Setup(ds => ds.GetFilePaths(It.IsRegex(@"Test Data[/\\]Metadata"), It.IsAny<bool>(), It.IsAny<bool>()))
|
|
|
|
.Returns(Array.Empty<string>());
|
2022-02-06 23:26:25 +01:00
|
|
|
|
2022-02-17 09:03:08 +01:00
|
|
|
var streams = await _subtitleResolver.GetExternalStreamsAsync(video.Object, 0, directoryService.Object, false, CancellationToken.None);
|
2022-02-06 23:26:25 +01:00
|
|
|
|
|
|
|
Assert.Single(streams);
|
|
|
|
var actual = streams[0];
|
|
|
|
|
2022-02-07 18:36:07 +01:00
|
|
|
var expected = CreateMediaStream(VideoDirectoryPath + "/" + file, codec, language, title, 0, isForced, isDefault);
|
2022-02-06 23:26:25 +01:00
|
|
|
Assert.Equal(expected.Index, actual.Index);
|
|
|
|
Assert.Equal(expected.Type, actual.Type);
|
|
|
|
Assert.Equal(expected.IsExternal, actual.IsExternal);
|
|
|
|
Assert.Equal(expected.Path, actual.Path);
|
|
|
|
Assert.Equal(expected.IsDefault, actual.IsDefault);
|
|
|
|
Assert.Equal(expected.IsForced, actual.IsForced);
|
|
|
|
Assert.Equal(expected.Language, actual.Language);
|
|
|
|
Assert.Equal(expected.Title, actual.Title);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static MediaStream CreateMediaStream(string path, string codec, string? language, string? title, int index, bool isForced = false, bool isDefault = false)
|
|
|
|
{
|
|
|
|
return new()
|
|
|
|
{
|
|
|
|
Index = index,
|
|
|
|
Codec = codec,
|
|
|
|
Type = MediaStreamType.Subtitle,
|
|
|
|
IsExternal = true,
|
|
|
|
Path = path,
|
|
|
|
IsDefault = isDefault,
|
|
|
|
IsForced = isForced,
|
|
|
|
Language = language,
|
|
|
|
Title = title
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|