AudioResolverTests.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 AudioResolverTests
  19. {
  20. private const string DirectoryPath = "Test Data/Video";
  21. private readonly AudioResolver _audioResolver;
  22. public AudioResolverTests()
  23. {
  24. var englishCultureDto = new CultureDto
  25. {
  26. Name = "English",
  27. DisplayName = "English",
  28. ThreeLetterISOLanguageNames = new[] { "eng" },
  29. TwoLetterISOLanguageName = "en"
  30. };
  31. var localizationManager = new Mock<ILocalizationManager>(MockBehavior.Loose);
  32. localizationManager.Setup(lm => lm.FindLanguageInfo(It.IsRegex(@"en.*", RegexOptions.IgnoreCase)))
  33. .Returns(englishCultureDto);
  34. var mediaEncoder = new Mock<IMediaEncoder>(MockBehavior.Strict);
  35. mediaEncoder.Setup(me => me.GetMediaInfo(It.IsAny<MediaInfoRequest>(), It.IsAny<CancellationToken>()))
  36. .Returns<MediaInfoRequest, CancellationToken>((_, _) => Task.FromResult(new MediaBrowser.Model.MediaInfo.MediaInfo
  37. {
  38. MediaStreams = new List<MediaStream>
  39. {
  40. new()
  41. }
  42. }));
  43. _audioResolver = new AudioResolver(localizationManager.Object, mediaEncoder.Object, new NamingOptions());
  44. }
  45. [Fact]
  46. public async void AddExternalAudioStreams_GivenMixedFilenames_ReturnsValidSubtitles()
  47. {
  48. var startIndex = 0;
  49. var index = startIndex;
  50. var files = new[]
  51. {
  52. DirectoryPath + "/My.Video.mp3",
  53. // DirectoryPath + "/Some.Other.Video.mp3", // TODO should not be picked up
  54. DirectoryPath + "/My.Video.png",
  55. DirectoryPath + "/My.Video.srt",
  56. DirectoryPath + "/My.Video.txt",
  57. DirectoryPath + "/My.Video.vtt",
  58. DirectoryPath + "/My.Video.ass",
  59. DirectoryPath + "/My.Video.sub",
  60. DirectoryPath + "/My.Video.ssa",
  61. DirectoryPath + "/My.Video.smi",
  62. DirectoryPath + "/My.Video.sami",
  63. DirectoryPath + "/My.Video.en.mp3",
  64. DirectoryPath + "/My.Video.Label.mp3",
  65. DirectoryPath + "/My.Video.With.Additional.Garbage.en.mp3",
  66. // DirectoryPath + "/My.Video With Additional Garbage.mp3" // TODO no "." after "My.Video", previously would be picked up
  67. };
  68. var expectedResult = new[]
  69. {
  70. CreateMediaStream(DirectoryPath + "/My.Video.mp3", null, null, index++),
  71. CreateMediaStream(DirectoryPath + "/My.Video.en.mp3", "eng", null, index++),
  72. CreateMediaStream(DirectoryPath + "/My.Video.Label.mp3", null, "Label", index++),
  73. CreateMediaStream(DirectoryPath + "/My.Video.With.Additional.Garbage.en.mp3", "eng", "Garbage", index) // TODO only "Garbage" is picked up as title, none of the other extra text
  74. };
  75. BaseItem.MediaSourceManager = Mock.Of<IMediaSourceManager>();
  76. var video = new Movie
  77. {
  78. // Must be valid for video.IsFileProtocol check
  79. Path = DirectoryPath + "/My.Video.mkv"
  80. };
  81. var directoryService = new Mock<IDirectoryService>(MockBehavior.Strict);
  82. directoryService.Setup(ds => ds.GetFilePaths(It.IsRegex(@"Test Data[/\\]Video"), It.IsAny<bool>(), It.IsAny<bool>()))
  83. .Returns(files);
  84. var asyncStreams = _audioResolver.GetExternalAudioStreams(video, startIndex, directoryService.Object, false, CancellationToken.None).ConfigureAwait(false);
  85. var streams = new List<MediaStream>();
  86. await foreach (var stream in asyncStreams)
  87. {
  88. streams.Add(stream);
  89. }
  90. Assert.Equal(expectedResult.Length, streams.Count);
  91. for (var i = 0; i < expectedResult.Length; i++)
  92. {
  93. var expected = expectedResult[i];
  94. var actual = streams[i];
  95. Assert.Equal(expected.Index, actual.Index);
  96. Assert.Equal(expected.Type, actual.Type);
  97. Assert.Equal(expected.IsExternal, actual.IsExternal);
  98. Assert.Equal(expected.Path, actual.Path);
  99. Assert.Equal(expected.Language, actual.Language);
  100. Assert.Equal(expected.Title, actual.Title);
  101. }
  102. }
  103. [Theory]
  104. [InlineData("My.Video.mp3", null, null, false, false)]
  105. [InlineData("My.Video.English.mp3", "eng", null, false, false)]
  106. [InlineData("My.Video.Title.mp3", null, "Title", false, false)]
  107. [InlineData("My.Video.forced.English.mp3", "eng", null, true, false)]
  108. [InlineData("My.Video.default.English.mp3", "eng", null, false, true)]
  109. [InlineData("My.Video.English.forced.default.Title.mp3", "eng", "Title", true, true)]
  110. public async void GetExternalAudioStreams_GivenSingleFile_ReturnsExpectedStream(string file, string? language, string? title, bool isForced, bool isDefault)
  111. {
  112. BaseItem.MediaSourceManager = Mock.Of<IMediaSourceManager>();
  113. var video = new Movie
  114. {
  115. // Must be valid for video.IsFileProtocol check
  116. Path = DirectoryPath + "/My.Video.mkv"
  117. };
  118. var directoryService = new Mock<IDirectoryService>(MockBehavior.Strict);
  119. directoryService.Setup(ds => ds.GetFilePaths(It.IsRegex(@"Test Data[/\\]Video"), It.IsAny<bool>(), It.IsAny<bool>()))
  120. .Returns(new[] { DirectoryPath + "/" + file });
  121. var asyncStreams = _audioResolver.GetExternalAudioStreams(video, 0, directoryService.Object, false, CancellationToken.None).ConfigureAwait(false);
  122. var streams = new List<MediaStream>();
  123. await foreach (var stream in asyncStreams)
  124. {
  125. streams.Add(stream);
  126. }
  127. Assert.Single(streams);
  128. var actual = streams[0];
  129. var expected = CreateMediaStream(DirectoryPath + "/" + file, language, title, 0, isForced, isDefault);
  130. Assert.Equal(expected.Index, actual.Index);
  131. Assert.Equal(expected.Type, actual.Type);
  132. Assert.Equal(expected.IsExternal, actual.IsExternal);
  133. Assert.Equal(expected.Path, actual.Path);
  134. Assert.Equal(expected.Language, actual.Language);
  135. Assert.Equal(expected.Title, actual.Title);
  136. Assert.Equal(expected.IsDefault, actual.IsDefault);
  137. Assert.Equal(expected.IsForced, actual.IsForced);
  138. }
  139. private static MediaStream CreateMediaStream(string path, string? language, string? title, int index, bool isForced = false, bool isDefault = false)
  140. {
  141. return new()
  142. {
  143. Index = index,
  144. Type = MediaStreamType.Audio,
  145. IsExternal = true,
  146. Path = path,
  147. Language = language,
  148. Title = title,
  149. IsForced = isForced,
  150. IsDefault = isDefault
  151. };
  152. }
  153. }
  154. }