TestBase.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System.IO;
  2. using System.Runtime.CompilerServices;
  3. using Avalonia.Controls;
  4. using Avalonia.Media.Imaging;
  5. using Avalonia.Rendering;
  6. using SixLabors.ImageSharp;
  7. using Xunit;
  8. using Avalonia.Platform;
  9. using System.Threading.Tasks;
  10. using System;
  11. using System.Collections.Concurrent;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Reactive.Disposables;
  15. using System.Threading;
  16. using Avalonia.Controls.Platform.Surfaces;
  17. using Avalonia.Media;
  18. using Avalonia.Rendering.Composition;
  19. using Avalonia.Threading;
  20. using Avalonia.UnitTests;
  21. using Avalonia.Utilities;
  22. using SixLabors.ImageSharp.PixelFormats;
  23. using Image = SixLabors.ImageSharp.Image;
  24. #if AVALONIA_SKIA
  25. using Avalonia.Skia;
  26. #else
  27. using Avalonia.Direct2D1;
  28. #endif
  29. #if AVALONIA_SKIA
  30. namespace Avalonia.Skia.RenderTests
  31. #else
  32. namespace Avalonia.Direct2D1.RenderTests
  33. #endif
  34. {
  35. public class TestBase : IDisposable
  36. {
  37. #if AVALONIA_SKIA
  38. private static string s_fontUri = "resm:Avalonia.Skia.RenderTests.Assets?assembly=Avalonia.Skia.RenderTests#Noto Mono";
  39. #else
  40. private static string s_fontUri = "resm:Avalonia.Direct2D1.RenderTests.Assets?assembly=Avalonia.Direct2D1.RenderTests#Noto Mono";
  41. #endif
  42. public static FontFamily TestFontFamily = new FontFamily(s_fontUri);
  43. #if AVALONIA_SKIA3
  44. // TODO: investigate why output is different.
  45. // Most likely we need to use new SKSamplingOptions API, as old filters are broken with SKBitmap.
  46. private const double AllowedError = 0.15;
  47. #else
  48. private const double AllowedError = 0.022;
  49. #endif
  50. public TestBase(string outputPath)
  51. {
  52. outputPath = outputPath.Replace('\\', Path.DirectorySeparatorChar);
  53. var testPath = GetTestsDirectory();
  54. var testFiles = Path.Combine(testPath, "TestFiles");
  55. #if AVALONIA_SKIA
  56. var platform = "Skia";
  57. #else
  58. var platform = "Direct2D1";
  59. #endif
  60. OutputPath = Path.Combine(testFiles, platform, outputPath);
  61. TestRenderHelper.BeginTest();
  62. }
  63. public string OutputPath
  64. {
  65. get;
  66. }
  67. protected async Task RenderToFile(Control target, [CallerMemberName] string testName = "", double dpi = 96)
  68. {
  69. if (!Directory.Exists(OutputPath))
  70. {
  71. Directory.CreateDirectory(OutputPath);
  72. }
  73. var immediatePath = Path.Combine(OutputPath, testName + ".immediate.out.png");
  74. var compositedPath = Path.Combine(OutputPath, testName + ".composited.out.png");
  75. await TestRenderHelper.RenderToFile(target, immediatePath, true, dpi);
  76. await TestRenderHelper.RenderToFile(target, compositedPath, false, dpi);
  77. }
  78. protected void CompareImages([CallerMemberName] string testName = "",
  79. bool skipImmediate = false, bool skipCompositor = false)
  80. {
  81. var expectedPath = Path.Combine(OutputPath, testName + ".expected.png");
  82. var immediatePath = Path.Combine(OutputPath, testName + ".immediate.out.png");
  83. var compositedPath = Path.Combine(OutputPath, testName + ".composited.out.png");
  84. using (var expected = Image.Load<Rgba32>(expectedPath))
  85. using (var immediate = skipImmediate ? null: Image.Load<Rgba32>(immediatePath))
  86. using (var composited = skipCompositor ? null : Image.Load<Rgba32>(compositedPath))
  87. {
  88. if (!skipImmediate)
  89. {
  90. var immediateError = TestRenderHelper.CompareImages(immediate!, expected);
  91. if (immediateError > AllowedError)
  92. {
  93. Assert.True(false, immediatePath + ": Error = " + immediateError);
  94. }
  95. }
  96. if (!skipCompositor)
  97. {
  98. var compositedError = TestRenderHelper.CompareImages(composited!, expected);
  99. if (compositedError > AllowedError)
  100. {
  101. Assert.True(false, compositedPath + ": Error = " + compositedError);
  102. }
  103. }
  104. }
  105. }
  106. protected void CompareImagesNoRenderer([CallerMemberName] string testName = "", string expectedName = null)
  107. {
  108. var expectedPath = Path.Combine(OutputPath, (expectedName ?? testName) + ".expected.png");
  109. var actualPath = Path.Combine(OutputPath, testName + ".out.png");
  110. TestRenderHelper.AssertCompareImages(actualPath, expectedPath);
  111. }
  112. private static string GetTestsDirectory() => TestRenderHelper.GetTestsDirectory();
  113. public void Dispose() => TestRenderHelper.EndTest();
  114. }
  115. }