1
0

FontManagerImplTests.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using System.Linq;
  3. using Avalonia.Media;
  4. using Avalonia.UnitTests;
  5. using SkiaSharp;
  6. using Xunit;
  7. namespace Avalonia.Skia.UnitTests.Media
  8. {
  9. public class FontManagerImplTests
  10. {
  11. private static string s_fontUri = "resm:Avalonia.Skia.UnitTests.Assets?assembly=Avalonia.Skia.UnitTests#Noto Mono";
  12. [Fact]
  13. public void Should_Create_Typeface_From_Fallback()
  14. {
  15. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface.With(fontManagerImpl: new FontManagerImpl())))
  16. {
  17. var fontManager = FontManager.Current;
  18. var glyphTypeface = new Typeface(new FontFamily("A, B, " + fontManager.DefaultFontFamilyName)).GlyphTypeface;
  19. Assert.Equal(SKTypeface.Default.FamilyName, glyphTypeface.FamilyName);
  20. }
  21. }
  22. [Fact]
  23. public void Should_Create_Typeface_From_Fallback_Bold()
  24. {
  25. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface.With(fontManagerImpl: new FontManagerImpl())))
  26. {
  27. var glyphTypeface = new Typeface(new FontFamily($"A, B, Arial"), weight: FontWeight.Bold).GlyphTypeface;
  28. Assert.True((int)glyphTypeface.Weight >= 600);
  29. }
  30. }
  31. [Fact]
  32. public void Should_Throw_InvalidOperationException_For_Invalid_FamilyName()
  33. {
  34. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface.With(fontManagerImpl: new FontManagerImpl())))
  35. {
  36. Assert.Throws<InvalidOperationException>(() =>
  37. {
  38. var glyphTypeface = new Typeface(new FontFamily("Unknown")).GlyphTypeface;
  39. });
  40. }
  41. }
  42. [Fact]
  43. public void Should_Load_Typeface_From_Resource()
  44. {
  45. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface.With(fontManagerImpl: new FontManagerImpl())))
  46. {
  47. var glyphTypeface = new Typeface(s_fontUri).GlyphTypeface;
  48. Assert.Equal("Noto Mono", glyphTypeface.FamilyName);
  49. }
  50. }
  51. [Fact]
  52. public void Should_Load_Nearest_Matching_Font()
  53. {
  54. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface.With(fontManagerImpl: new FontManagerImpl())))
  55. {
  56. var glyphTypeface = new Typeface(s_fontUri, FontStyle.Italic, FontWeight.Black).GlyphTypeface;
  57. Assert.Equal("Noto Mono", glyphTypeface.FamilyName);
  58. }
  59. }
  60. [Fact]
  61. public void Should_Throw_For_Invalid_Custom_Font()
  62. {
  63. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface.With(fontManagerImpl: new FontManagerImpl())))
  64. {
  65. Assert.Throws<InvalidOperationException>(() => new Typeface("resm:Avalonia.Skia.UnitTests.Assets?assembly=Avalonia.Skia.UnitTests#Unknown").GlyphTypeface);
  66. }
  67. }
  68. }
  69. }