jellyfin-server/tests/Jellyfin.Server.Implementations.Tests/Localization/LocalizationManagerTests.cs

181 lines
6.2 KiB
C#
Raw Normal View History

2021-08-10 11:37:33 +00:00
using System;
using System.Linq;
2021-05-10 15:58:21 +00:00
using System.Threading.Tasks;
using Emby.Server.Implementations.Localization;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Model.Configuration;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using Xunit;
namespace Jellyfin.Server.Implementations.Tests.Localization
{
public class LocalizationManagerTests
{
[Fact]
public void GetCountries_All_Success()
{
var localizationManager = Setup(new ServerConfiguration
{
UICulture = "de-DE"
});
var countries = localizationManager.GetCountries();
2021-05-10 15:58:21 +00:00
var countryInfos = countries.ToList();
Assert.Equal(139, countryInfos.Count);
2021-08-10 11:37:33 +00:00
var germany = countryInfos.FirstOrDefault(x => x.Name.Equals("DE", StringComparison.Ordinal));
2021-05-10 15:58:21 +00:00
Assert.NotNull(germany);
Assert.Equal("Germany", germany!.DisplayName);
2021-08-10 11:37:33 +00:00
Assert.Equal("DEU", germany.ThreeLetterISORegionName);
Assert.Equal("DE", germany.TwoLetterISORegionName);
2021-05-10 15:58:21 +00:00
}
[Fact]
public async Task GetCultures_All_Success()
{
var localizationManager = Setup(new ServerConfiguration
{
UICulture = "de-DE"
});
await localizationManager.LoadAll();
var cultures = localizationManager.GetCultures().ToList();
2021-05-10 15:58:21 +00:00
Assert.Equal(189, cultures.Count);
var germany = cultures.FirstOrDefault(x => x.TwoLetterISOLanguageName == "de");
Assert.NotNull(germany);
Assert.Equal("ger", germany!.ThreeLetterISOLanguageName);
2021-08-10 11:37:33 +00:00
Assert.Equal("German", germany.DisplayName);
Assert.Equal("German", germany.Name);
Assert.Contains("deu", germany.ThreeLetterISOLanguageNames);
Assert.Contains("ger", germany.ThreeLetterISOLanguageNames);
2021-05-10 15:58:21 +00:00
}
[Theory]
[InlineData("de")]
[InlineData("ger")]
[InlineData("german")]
public async Task FindLanguage_Valid_Success(string identifier)
{
var localizationManager = Setup(new ServerConfiguration
{
UICulture = "de-DE"
});
await localizationManager.LoadAll();
2021-05-10 15:58:21 +00:00
var germany = localizationManager.FindLanguageInfo(identifier);
2021-05-10 15:58:21 +00:00
Assert.NotNull(germany);
2021-08-10 11:37:33 +00:00
Assert.Equal("ger", germany.ThreeLetterISOLanguageName);
Assert.Equal("German", germany.DisplayName);
Assert.Equal("German", germany.Name);
Assert.Contains("deu", germany.ThreeLetterISOLanguageNames);
Assert.Contains("ger", germany.ThreeLetterISOLanguageNames);
2021-05-10 15:58:21 +00:00
}
[Fact]
public async Task ParentalRatings_Default_Success()
{
var localizationManager = Setup(new ServerConfiguration
{
UICulture = "de-DE"
});
await localizationManager.LoadAll();
var ratings = localizationManager.GetParentalRatings().ToList();
2021-05-10 15:58:21 +00:00
Assert.Equal(23, ratings.Count);
var tvma = ratings.FirstOrDefault(x => x.Name == "TV-MA");
Assert.NotNull(tvma);
Assert.Equal(9, tvma!.Value);
}
[Fact]
public async Task ParentalRatings_ConfiguredCountryCode_Success()
{
var localizationManager = Setup(new ServerConfiguration()
2021-05-10 15:58:21 +00:00
{
MetadataCountryCode = "DE"
});
await localizationManager.LoadAll();
var ratings = localizationManager.GetParentalRatings().ToList();
2021-05-10 15:58:21 +00:00
Assert.Equal(10, ratings.Count);
var fsk = ratings.FirstOrDefault(x => x.Name == "FSK-12");
Assert.NotNull(fsk);
Assert.Equal(7, fsk!.Value);
}
[Theory]
[InlineData("CA-R", "CA", 10)]
[InlineData("FSK-16", "DE", 8)]
[InlineData("FSK-18", "DE", 9)]
[InlineData("FSK-18", "US", 9)]
[InlineData("TV-MA", "US", 9)]
[InlineData("XXX", "asdf", 100)]
[InlineData("Germany: FSK-18", "DE", 9)]
public async Task GetRatingLevelFromString_Valid_Success(string value, string countryCode, int expectedLevel)
{
var localizationManager = Setup(new ServerConfiguration()
2021-05-10 15:58:21 +00:00
{
MetadataCountryCode = countryCode
});
await localizationManager.LoadAll();
var level = localizationManager.GetRatingLevel(value);
2021-05-10 15:58:21 +00:00
Assert.NotNull(level);
Assert.Equal(expectedLevel, level!);
}
[Fact]
public async Task GetRatingLevelFromString_Unrated_Success()
{
var localizationManager = Setup(new ServerConfiguration()
{
UICulture = "de-DE"
});
await localizationManager.LoadAll();
Assert.Null(localizationManager.GetRatingLevel("n/a"));
2021-05-10 15:58:21 +00:00
}
[Theory]
[InlineData("Default", "Default")]
[InlineData("HeaderLiveTV", "Live TV")]
public void GetLocalizedString_Valid_Success(string key, string expected)
{
var localizationManager = Setup(new ServerConfiguration()
2021-05-10 15:58:21 +00:00
{
UICulture = "en-US"
});
var translated = localizationManager.GetLocalizedString(key);
2021-05-10 15:58:21 +00:00
Assert.NotNull(translated);
Assert.Equal(expected, translated);
}
[Fact]
public void GetLocalizedString_Invalid_Success()
{
var localizationManager = Setup(new ServerConfiguration()
2021-05-10 15:58:21 +00:00
{
UICulture = "en-US"
});
var key = "SuperInvalidTranslationKeyThatWillNeverBeAdded";
var translated = localizationManager.GetLocalizedString(key);
2021-05-10 15:58:21 +00:00
Assert.NotNull(translated);
Assert.Equal(key, translated);
}
private LocalizationManager Setup(ServerConfiguration config)
2021-05-10 15:58:21 +00:00
{
var mockConfiguration = new Mock<IServerConfigurationManager>();
mockConfiguration.SetupGet(x => x.Configuration).Returns(config);
return new LocalizationManager(mockConfiguration.Object, new NullLogger<LocalizationManager>());
2021-05-10 15:58:21 +00:00
}
}
}