LocalizationManagerTests.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using System;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. using Emby.Server.Implementations.Localization;
  5. using MediaBrowser.Controller.Configuration;
  6. using MediaBrowser.Model.Configuration;
  7. using Microsoft.Extensions.Logging.Abstractions;
  8. using Moq;
  9. using Xunit;
  10. namespace Jellyfin.Server.Implementations.Tests.Localization
  11. {
  12. public class LocalizationManagerTests
  13. {
  14. [Fact]
  15. public void GetCountries_All_Success()
  16. {
  17. var localizationManager = Setup(new ServerConfiguration
  18. {
  19. UICulture = "de-DE"
  20. });
  21. var countries = localizationManager.GetCountries().ToList();
  22. Assert.Equal(139, countries.Count);
  23. var germany = countries.FirstOrDefault(x => x.Name.Equals("DE", StringComparison.Ordinal));
  24. Assert.NotNull(germany);
  25. Assert.Equal("Germany", germany!.DisplayName);
  26. Assert.Equal("DEU", germany.ThreeLetterISORegionName);
  27. Assert.Equal("DE", germany.TwoLetterISORegionName);
  28. }
  29. [Fact]
  30. public async Task GetCultures_All_Success()
  31. {
  32. var localizationManager = Setup(new ServerConfiguration
  33. {
  34. UICulture = "de-DE"
  35. });
  36. await localizationManager.LoadAll();
  37. var cultures = localizationManager.GetCultures().ToList();
  38. Assert.Equal(191, cultures.Count);
  39. var germany = cultures.FirstOrDefault(x => x.TwoLetterISOLanguageName.Equals("de", StringComparison.Ordinal));
  40. Assert.NotNull(germany);
  41. Assert.Equal("ger", germany!.ThreeLetterISOLanguageName);
  42. Assert.Equal("German", germany.DisplayName);
  43. Assert.Equal("German", germany.Name);
  44. Assert.Contains("deu", germany.ThreeLetterISOLanguageNames);
  45. Assert.Contains("ger", germany.ThreeLetterISOLanguageNames);
  46. }
  47. [Theory]
  48. [InlineData("de")]
  49. [InlineData("ger")]
  50. [InlineData("german")]
  51. public async Task FindLanguageInfo_Valid_Success(string identifier)
  52. {
  53. var localizationManager = Setup(new ServerConfiguration
  54. {
  55. UICulture = "de-DE"
  56. });
  57. await localizationManager.LoadAll();
  58. var germany = localizationManager.FindLanguageInfo(identifier);
  59. Assert.NotNull(germany);
  60. Assert.Equal("ger", germany!.ThreeLetterISOLanguageName);
  61. Assert.Equal("German", germany.DisplayName);
  62. Assert.Equal("German", germany.Name);
  63. Assert.Contains("deu", germany.ThreeLetterISOLanguageNames);
  64. Assert.Contains("ger", germany.ThreeLetterISOLanguageNames);
  65. }
  66. [Fact]
  67. public async Task GetParentalRatings_Default_Success()
  68. {
  69. var localizationManager = Setup(new ServerConfiguration
  70. {
  71. UICulture = "de-DE"
  72. });
  73. await localizationManager.LoadAll();
  74. var ratings = localizationManager.GetParentalRatings().ToList();
  75. Assert.Equal(56, ratings.Count);
  76. var tvma = ratings.FirstOrDefault(x => x.Name.Equals("TV-MA", StringComparison.Ordinal));
  77. Assert.NotNull(tvma);
  78. Assert.Equal(17, tvma!.Value);
  79. }
  80. [Fact]
  81. public async Task GetParentalRatings_ConfiguredCountryCode_Success()
  82. {
  83. var localizationManager = Setup(new ServerConfiguration()
  84. {
  85. MetadataCountryCode = "DE"
  86. });
  87. await localizationManager.LoadAll();
  88. var ratings = localizationManager.GetParentalRatings().ToList();
  89. Assert.Equal(24, ratings.Count);
  90. var fsk = ratings.FirstOrDefault(x => x.Name.Equals("FSK-12", StringComparison.Ordinal));
  91. Assert.NotNull(fsk);
  92. Assert.Equal(12, fsk!.Value);
  93. }
  94. [Theory]
  95. [InlineData("CA-R", "CA", 18)]
  96. [InlineData("FSK-16", "DE", 16)]
  97. [InlineData("FSK-18", "DE", 18)]
  98. [InlineData("FSK-18", "US", 18)]
  99. [InlineData("TV-MA", "US", 17)]
  100. [InlineData("XXX", "asdf", 1000)]
  101. [InlineData("Germany: FSK-18", "DE", 18)]
  102. [InlineData("Rated : R", "US", 17)]
  103. [InlineData("Rated: R", "US", 17)]
  104. [InlineData("Rated R", "US", 17)]
  105. [InlineData(" PG-13 ", "US", 13)]
  106. public async Task GetRatingLevel_GivenValidString_Success(string value, string countryCode, int expectedLevel)
  107. {
  108. var localizationManager = Setup(new ServerConfiguration()
  109. {
  110. MetadataCountryCode = countryCode
  111. });
  112. await localizationManager.LoadAll();
  113. var level = localizationManager.GetRatingLevel(value);
  114. Assert.NotNull(level);
  115. Assert.Equal(expectedLevel, level!);
  116. }
  117. [Theory]
  118. [InlineData("0", 0)]
  119. [InlineData("1", 1)]
  120. [InlineData("6", 6)]
  121. [InlineData("12", 12)]
  122. [InlineData("42", 42)]
  123. [InlineData("9999", 9999)]
  124. public async Task GetRatingLevel_GivenValidAge_Success(string value, int expectedLevel)
  125. {
  126. var localizationManager = Setup(new ServerConfiguration { MetadataCountryCode = "nl" });
  127. await localizationManager.LoadAll();
  128. var level = localizationManager.GetRatingLevel(value);
  129. Assert.NotNull(level);
  130. Assert.Equal(expectedLevel, level);
  131. }
  132. [Fact]
  133. public async Task GetRatingLevel_GivenUnratedString_Success()
  134. {
  135. var localizationManager = Setup(new ServerConfiguration()
  136. {
  137. UICulture = "de-DE"
  138. });
  139. await localizationManager.LoadAll();
  140. Assert.Null(localizationManager.GetRatingLevel("NR"));
  141. Assert.Null(localizationManager.GetRatingLevel("unrated"));
  142. Assert.Null(localizationManager.GetRatingLevel("Not Rated"));
  143. Assert.Null(localizationManager.GetRatingLevel("n/a"));
  144. }
  145. [Theory]
  146. [InlineData("-NO RATING SHOWN-")]
  147. [InlineData(":NO RATING SHOWN:")]
  148. public async Task GetRatingLevel_Split_Success(string value)
  149. {
  150. var localizationManager = Setup(new ServerConfiguration()
  151. {
  152. UICulture = "en-US"
  153. });
  154. await localizationManager.LoadAll();
  155. Assert.Null(localizationManager.GetRatingLevel(value));
  156. }
  157. [Theory]
  158. [InlineData("Default", "Default")]
  159. [InlineData("HeaderLiveTV", "Live TV")]
  160. public void GetLocalizedString_Valid_Success(string key, string expected)
  161. {
  162. var localizationManager = Setup(new ServerConfiguration()
  163. {
  164. UICulture = "en-US"
  165. });
  166. var translated = localizationManager.GetLocalizedString(key);
  167. Assert.NotNull(translated);
  168. Assert.Equal(expected, translated);
  169. }
  170. [Fact]
  171. public void GetLocalizedString_Invalid_Success()
  172. {
  173. var localizationManager = Setup(new ServerConfiguration()
  174. {
  175. UICulture = "en-US"
  176. });
  177. var key = "SuperInvalidTranslationKeyThatWillNeverBeAdded";
  178. var translated = localizationManager.GetLocalizedString(key);
  179. Assert.NotNull(translated);
  180. Assert.Equal(key, translated);
  181. }
  182. private LocalizationManager Setup(ServerConfiguration config)
  183. {
  184. var mockConfiguration = new Mock<IServerConfigurationManager>();
  185. mockConfiguration.SetupGet(x => x.Configuration).Returns(config);
  186. return new LocalizationManager(mockConfiguration.Object, new NullLogger<LocalizationManager>());
  187. }
  188. }
  189. }