LocalizationManagerTests.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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(54, 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. public async Task GetRatingLevel_GivenValidString_Success(string value, string countryCode, int expectedLevel)
  103. {
  104. var localizationManager = Setup(new ServerConfiguration()
  105. {
  106. MetadataCountryCode = countryCode
  107. });
  108. await localizationManager.LoadAll();
  109. var level = localizationManager.GetRatingLevel(value);
  110. Assert.NotNull(level);
  111. Assert.Equal(expectedLevel, level!);
  112. }
  113. [Theory]
  114. [InlineData("0", 0)]
  115. [InlineData("1", 1)]
  116. [InlineData("6", 6)]
  117. [InlineData("12", 12)]
  118. [InlineData("42", 42)]
  119. [InlineData("9999", 9999)]
  120. public async Task GetRatingLevel_GivenValidAge_Success(string value, int expectedLevel)
  121. {
  122. var localizationManager = Setup(new ServerConfiguration { MetadataCountryCode = "nl" });
  123. await localizationManager.LoadAll();
  124. var level = localizationManager.GetRatingLevel(value);
  125. Assert.NotNull(level);
  126. Assert.Equal(expectedLevel, level);
  127. }
  128. [Fact]
  129. public async Task GetRatingLevel_GivenUnratedString_Success()
  130. {
  131. var localizationManager = Setup(new ServerConfiguration()
  132. {
  133. UICulture = "de-DE"
  134. });
  135. await localizationManager.LoadAll();
  136. Assert.Null(localizationManager.GetRatingLevel("NR"));
  137. Assert.Null(localizationManager.GetRatingLevel("unrated"));
  138. Assert.Null(localizationManager.GetRatingLevel("Not Rated"));
  139. Assert.Null(localizationManager.GetRatingLevel("n/a"));
  140. }
  141. [Theory]
  142. [InlineData("Default", "Default")]
  143. [InlineData("HeaderLiveTV", "Live TV")]
  144. public void GetLocalizedString_Valid_Success(string key, string expected)
  145. {
  146. var localizationManager = Setup(new ServerConfiguration()
  147. {
  148. UICulture = "en-US"
  149. });
  150. var translated = localizationManager.GetLocalizedString(key);
  151. Assert.NotNull(translated);
  152. Assert.Equal(expected, translated);
  153. }
  154. [Fact]
  155. public void GetLocalizedString_Invalid_Success()
  156. {
  157. var localizationManager = Setup(new ServerConfiguration()
  158. {
  159. UICulture = "en-US"
  160. });
  161. var key = "SuperInvalidTranslationKeyThatWillNeverBeAdded";
  162. var translated = localizationManager.GetLocalizedString(key);
  163. Assert.NotNull(translated);
  164. Assert.Equal(key, translated);
  165. }
  166. private LocalizationManager Setup(ServerConfiguration config)
  167. {
  168. var mockConfiguration = new Mock<IServerConfigurationManager>();
  169. mockConfiguration.SetupGet(x => x.Configuration).Returns(config);
  170. return new LocalizationManager(mockConfiguration.Object, new NullLogger<LocalizationManager>());
  171. }
  172. }
  173. }