TestBase.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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.Skia;
  20. using Avalonia.Threading;
  21. using Avalonia.UnitTests;
  22. using Avalonia.Utilities;
  23. using SixLabors.ImageSharp.PixelFormats;
  24. using Image = SixLabors.ImageSharp.Image;
  25. namespace Avalonia.Skia.RenderTests
  26. {
  27. public class TestBase : IDisposable
  28. {
  29. private static string s_fontUri = "resm:Avalonia.Skia.RenderTests.Assets?assembly=Avalonia.Skia.RenderTests#Noto Mono";
  30. public static FontFamily TestFontFamily = new FontFamily(s_fontUri);
  31. private const double AllowedError = 0.022;
  32. public TestBase(string outputPath)
  33. {
  34. outputPath = outputPath.Replace('\\', Path.DirectorySeparatorChar);
  35. var testPath = GetTestsDirectory();
  36. var testFiles = Path.Combine(testPath, "TestFiles");
  37. OutputPath = Path.Combine(testFiles, "Skia", outputPath);
  38. TestRenderHelper.BeginTest();
  39. }
  40. public string OutputPath
  41. {
  42. get;
  43. }
  44. protected async Task RenderToFile(Control target, [CallerMemberName] string testName = "", double dpi = 96)
  45. {
  46. if (!Directory.Exists(OutputPath))
  47. {
  48. Directory.CreateDirectory(OutputPath);
  49. }
  50. var immediatePath = Path.Combine(OutputPath, testName + ".immediate.out.png");
  51. var compositedPath = Path.Combine(OutputPath, testName + ".composited.out.png");
  52. await TestRenderHelper.RenderToFile(target, immediatePath, true, dpi);
  53. await TestRenderHelper.RenderToFile(target, compositedPath, false, dpi);
  54. }
  55. protected void CompareImages([CallerMemberName] string testName = "",
  56. bool skipImmediate = false, bool skipCompositor = false)
  57. {
  58. var expectedPath = Path.Combine(OutputPath, testName + ".expected.png");
  59. var immediatePath = Path.Combine(OutputPath, testName + ".immediate.out.png");
  60. var compositedPath = Path.Combine(OutputPath, testName + ".composited.out.png");
  61. using (var expected = Image.Load<Rgba32>(expectedPath))
  62. using (var immediate = skipImmediate ? null: Image.Load<Rgba32>(immediatePath))
  63. using (var composited = skipCompositor ? null : Image.Load<Rgba32>(compositedPath))
  64. {
  65. if (!skipImmediate)
  66. {
  67. var immediateError = TestRenderHelper.CompareImages(immediate!, expected);
  68. if (immediateError > AllowedError)
  69. {
  70. Assert.Fail(immediatePath + ": Error = " + immediateError);
  71. }
  72. }
  73. if (!skipCompositor)
  74. {
  75. var compositedError = TestRenderHelper.CompareImages(composited!, expected);
  76. if (compositedError > AllowedError)
  77. {
  78. Assert.Fail(compositedPath + ": Error = " + compositedError);
  79. }
  80. }
  81. }
  82. }
  83. protected void CompareImagesNoRenderer([CallerMemberName] string testName = "", string expectedName = null)
  84. {
  85. var expectedPath = Path.Combine(OutputPath, (expectedName ?? testName) + ".expected.png");
  86. var actualPath = Path.Combine(OutputPath, testName + ".out.png");
  87. TestRenderHelper.AssertCompareImages(actualPath, expectedPath);
  88. }
  89. private static string GetTestsDirectory() => TestRenderHelper.GetTestsDirectory();
  90. public void Dispose() => TestRenderHelper.EndTest();
  91. }
  92. }