SubtitleResolverTests.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using System.Collections.Generic;
  2. using System.Text.RegularExpressions;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using Emby.Naming.Common;
  6. using MediaBrowser.Controller.Entities;
  7. using MediaBrowser.Controller.Entities.Movies;
  8. using MediaBrowser.Controller.Library;
  9. using MediaBrowser.Controller.MediaEncoding;
  10. using MediaBrowser.Controller.Providers;
  11. using MediaBrowser.Model.Entities;
  12. using MediaBrowser.Model.Globalization;
  13. using MediaBrowser.Providers.MediaInfo;
  14. using Moq;
  15. using Xunit;
  16. namespace Jellyfin.Providers.Tests.MediaInfo
  17. {
  18. public class SubtitleResolverTests
  19. {
  20. private const string DirectoryPath = "Test Data/Video";
  21. private readonly SubtitleResolver _subtitleResolver;
  22. public SubtitleResolverTests()
  23. {
  24. var englishCultureDto = new CultureDto
  25. {
  26. Name = "English",
  27. DisplayName = "English",
  28. ThreeLetterISOLanguageNames = new[] { "eng" },
  29. TwoLetterISOLanguageName = "en"
  30. };
  31. var frenchCultureDto = new CultureDto
  32. {
  33. Name = "French",
  34. DisplayName = "French",
  35. ThreeLetterISOLanguageNames = new[] { "fre", "fra" },
  36. TwoLetterISOLanguageName = "fr"
  37. };
  38. var localizationManager = new Mock<ILocalizationManager>(MockBehavior.Loose);
  39. localizationManager.Setup(lm => lm.FindLanguageInfo(It.IsRegex(@"en.*", RegexOptions.IgnoreCase)))
  40. .Returns(englishCultureDto);
  41. localizationManager.Setup(lm => lm.FindLanguageInfo(It.IsRegex(@"fr.*", RegexOptions.IgnoreCase)))
  42. .Returns(frenchCultureDto);
  43. var mediaEncoder = new Mock<IMediaEncoder>(MockBehavior.Strict);
  44. mediaEncoder.Setup(me => me.GetMediaInfo(It.IsAny<MediaInfoRequest>(), It.IsAny<CancellationToken>()))
  45. .Returns<MediaInfoRequest, CancellationToken>((_, _) => Task.FromResult(new MediaBrowser.Model.MediaInfo.MediaInfo
  46. {
  47. MediaStreams = new List<MediaStream>
  48. {
  49. new()
  50. }
  51. }));
  52. _subtitleResolver = new SubtitleResolver(localizationManager.Object, mediaEncoder.Object, new NamingOptions());
  53. }
  54. [Fact]
  55. public async void AddExternalSubtitleStreams_GivenMixedFilenames_ReturnsValidSubtitles()
  56. {
  57. var startIndex = 0;
  58. var index = startIndex;
  59. var files = new[]
  60. {
  61. DirectoryPath + "/My.Video.mp3",
  62. DirectoryPath + "/My.Video.png",
  63. DirectoryPath + "/My.Video.srt",
  64. // DirectoryPath + "/Some.Other.Video.srt", // TODO should not be picked up
  65. DirectoryPath + "/My.Video.txt",
  66. DirectoryPath + "/My.Video.vtt",
  67. DirectoryPath + "/My.Video.ass",
  68. DirectoryPath + "/My.Video.sub",
  69. DirectoryPath + "/My.Video.ssa",
  70. DirectoryPath + "/My.Video.smi",
  71. DirectoryPath + "/My.Video.sami",
  72. DirectoryPath + "/My.Video.en.srt",
  73. DirectoryPath + "/My.Video.default.en.srt",
  74. DirectoryPath + "/My.Video.default.forced.en.srt",
  75. DirectoryPath + "/My.Video.en.default.forced.srt",
  76. DirectoryPath + "/My.Video.With.Additional.Garbage.en.srt",
  77. // DirectoryPath + "/My.Video With Additional Garbage.srt" // TODO no "." after "My.Video", previously would be picked up
  78. };
  79. var expectedResult = new[]
  80. {
  81. CreateMediaStream(DirectoryPath + "/My.Video.srt", "srt", null, null, index++),
  82. CreateMediaStream(DirectoryPath + "/My.Video.vtt", "vtt", null, null, index++),
  83. CreateMediaStream(DirectoryPath + "/My.Video.ass", "ass", null, null, index++),
  84. CreateMediaStream(DirectoryPath + "/My.Video.sub", "sub", null, null, index++),
  85. CreateMediaStream(DirectoryPath + "/My.Video.ssa", "ssa", null, null, index++),
  86. CreateMediaStream(DirectoryPath + "/My.Video.smi", "smi", null, null, index++),
  87. CreateMediaStream(DirectoryPath + "/My.Video.sami", "sami", null, null, index++),
  88. CreateMediaStream(DirectoryPath + "/My.Video.en.srt", "srt", "eng", null, index++),
  89. CreateMediaStream(DirectoryPath + "/My.Video.default.en.srt", "srt", "eng", null, index++, isDefault: true),
  90. CreateMediaStream(DirectoryPath + "/My.Video.default.forced.en.srt", "srt", "eng", null, index++, isForced: true, isDefault: true),
  91. CreateMediaStream(DirectoryPath + "/My.Video.en.default.forced.srt", "srt", "eng", null, index++, isForced: true, isDefault: true),
  92. CreateMediaStream(DirectoryPath + "/My.Video.With.Additional.Garbage.en.srt", "srt", "eng", "Garbage", index) // TODO only "Garbage" is picked up as title, none of the other extra text
  93. };
  94. BaseItem.MediaSourceManager = Mock.Of<IMediaSourceManager>();
  95. var video = new Movie
  96. {
  97. // Must be valid for video.IsFileProtocol check
  98. Path = DirectoryPath + "/My.Video.mkv"
  99. };
  100. var directoryService = new Mock<IDirectoryService>(MockBehavior.Strict);
  101. directoryService.Setup(ds => ds.GetFilePaths(It.IsRegex(@"Test Data[/\\]Video"), It.IsAny<bool>(), It.IsAny<bool>()))
  102. .Returns(files);
  103. var asyncStreams = _subtitleResolver.GetExternalSubtitleStreams(video, startIndex, directoryService.Object, false, CancellationToken.None).ConfigureAwait(false);
  104. var streams = new List<MediaStream>();
  105. await foreach (var stream in asyncStreams)
  106. {
  107. streams.Add(stream);
  108. }
  109. Assert.Equal(expectedResult.Length, streams.Count);
  110. for (var i = 0; i < expectedResult.Length; i++)
  111. {
  112. var expected = expectedResult[i];
  113. var actual = streams[i];
  114. Assert.Equal(expected.Index, actual.Index);
  115. // Assert.Equal(expected.Codec, actual.Codec); TODO should codec still be set to file extension?
  116. Assert.Equal(expected.Type, actual.Type);
  117. Assert.Equal(expected.IsExternal, actual.IsExternal);
  118. Assert.Equal(expected.Path, actual.Path);
  119. Assert.Equal(expected.IsDefault, actual.IsDefault);
  120. Assert.Equal(expected.IsForced, actual.IsForced);
  121. Assert.Equal(expected.Language, actual.Language);
  122. Assert.Equal(expected.Title, actual.Title);
  123. }
  124. }
  125. [Theory]
  126. [InlineData("My Video.srt", "srt", null, null, false, false)]
  127. [InlineData("My Video.ass", "ass", null, null, false, false)]
  128. [InlineData("my video.srt", "srt", null, null, false, false)]
  129. [InlineData("My Vidèo.srt", "srt", null, null, false, false)]
  130. [InlineData("My. Video.srt", "srt", null, null, false, false)]
  131. [InlineData("My.Video.srt", "srt", null, null, false, false)]
  132. [InlineData("My.Video.foreign.srt", "srt", null, null, true, false)]
  133. [InlineData("My Video.forced.srt", "srt", null, null, true, false)]
  134. [InlineData("My.Video.default.srt", "srt", null, null, false, true)]
  135. [InlineData("My.Video.forced.default.srt", "srt", null, null, true, true)]
  136. [InlineData("My.Video.en.srt", "srt", "eng", null, false, false)]
  137. [InlineData("My.Video.fr.en.srt", "srt", "eng", "fr", false, false)]
  138. [InlineData("My.Video.en.fr.srt", "srt", "fre", "en", false, false)]
  139. [InlineData("My.Video.default.en.srt", "srt", "eng", null, false, true)]
  140. [InlineData("My.Video.default.forced.en.srt", "srt", "eng", null, true, true)]
  141. [InlineData("My.Video.en.default.forced.srt", "srt", "eng", null, true, true)]
  142. [InlineData("My.Video.Track Label.srt", "srt", null, "Track Label", false, false)]
  143. // [InlineData("My.Video.Track.Label.srt", "srt", null, "Track.Label", false, false)] // TODO fails - only "Label" is picked up for title, not "Track.Label"
  144. // [InlineData("MyVideo.Track Label.srt", "srt", null, "Track Label", false, false)] // TODO fails - fuzzy match doesn't pick up on end of matching segment being shorter?
  145. [InlineData("My.Video.Track Label.en.default.forced.srt", "srt", "eng", "Track Label", true, true)]
  146. [InlineData("My.Video.en.default.forced.Track Label.srt", "srt", "eng", "Track Label", true, true)]
  147. public async void AddExternalSubtitleStreams_GivenSingleFile_ReturnsExpectedSubtitle(string file, string codec, string? language, string? title, bool isForced, bool isDefault)
  148. {
  149. BaseItem.MediaSourceManager = Mock.Of<IMediaSourceManager>();
  150. var video = new Movie
  151. {
  152. // Must be valid for video.IsFileProtocol check
  153. Path = DirectoryPath + "/My.Video.mkv"
  154. };
  155. var directoryService = new Mock<IDirectoryService>(MockBehavior.Strict);
  156. directoryService.Setup(ds => ds.GetFilePaths(It.IsRegex(@"Test Data[/\\]Video"), It.IsAny<bool>(), It.IsAny<bool>()))
  157. .Returns(new[] { DirectoryPath + "/" + file });
  158. var asyncStreams = _subtitleResolver.GetExternalSubtitleStreams(video, 0, directoryService.Object, false, CancellationToken.None).ConfigureAwait(false);
  159. var streams = new List<MediaStream>();
  160. await foreach (var stream in asyncStreams)
  161. {
  162. streams.Add(stream);
  163. }
  164. Assert.Single(streams);
  165. var actual = streams[0];
  166. var expected = CreateMediaStream(DirectoryPath + "/" + file, codec, language, title, 0, isForced, isDefault);
  167. Assert.Equal(expected.Index, actual.Index);
  168. // Assert.Equal(expected.Codec, actual.Codec); TODO should codec still be set to file extension?
  169. Assert.Equal(expected.Type, actual.Type);
  170. Assert.Equal(expected.IsExternal, actual.IsExternal);
  171. Assert.Equal(expected.Path, actual.Path);
  172. Assert.Equal(expected.IsDefault, actual.IsDefault);
  173. Assert.Equal(expected.IsForced, actual.IsForced);
  174. Assert.Equal(expected.Language, actual.Language);
  175. Assert.Equal(expected.Title, actual.Title);
  176. }
  177. private static MediaStream CreateMediaStream(string path, string codec, string? language, string? title, int index, bool isForced = false, bool isDefault = false)
  178. {
  179. return new()
  180. {
  181. Index = index,
  182. Codec = codec,
  183. Type = MediaStreamType.Subtitle,
  184. IsExternal = true,
  185. Path = path,
  186. IsDefault = isDefault,
  187. IsForced = isForced,
  188. Language = language,
  189. Title = title
  190. };
  191. }
  192. }
  193. }