RenderTests.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using Avalonia.Headless.XUnit;
  2. using ChunkyImageLib;
  3. using ChunkyImageLib.DataHolders;
  4. using Drawie.Backend.Core;
  5. using Drawie.Backend.Core.Bridge;
  6. using Drawie.Backend.Core.ColorsImpl;
  7. using Drawie.Numerics;
  8. using PixiEditor.Models.IO;
  9. using Xunit.Abstractions;
  10. using Color = Drawie.Backend.Core.ColorsImpl.Color;
  11. namespace PixiEditor.Tests;
  12. public class RenderTests : FullPixiEditorTest
  13. {
  14. private readonly ITestOutputHelper _testOutputHelper;
  15. public RenderTests(ITestOutputHelper testOutputHelper)
  16. {
  17. _testOutputHelper = testOutputHelper;
  18. }
  19. [AvaloniaTheory]
  20. [InlineData("Fibi")]
  21. [InlineData("Pond")]
  22. [InlineData("SmlPxlCircShadWithMask")]
  23. [InlineData("SmallPixelArtCircleShadow")]
  24. [InlineData("SmlPxlCircShadWithMaskClipped")]
  25. [InlineData("SmlPxlCircShadWithMaskClippedInFolder")]
  26. [InlineData("VectorRectangleClippedToCircle")]
  27. [InlineData("VectorRectangleClippedToCircleShadowFilter")]
  28. [InlineData("VectorRectangleClippedToCircleMasked")]
  29. public void TestThatPixiFilesRenderTheSameResultAsSavedPng(string fileName)
  30. {
  31. if (!DrawingBackendApi.Current.IsHardwareAccelerated)
  32. {
  33. _testOutputHelper.WriteLine("Skipping the test because hardware acceleration is not enabled.");
  34. return;
  35. }
  36. string pixiFile = Path.Combine("TestFiles", "RenderTests", fileName + ".pixi");
  37. string pngFile = Path.Combine("TestFiles", "RenderTests", fileName + ".png");
  38. var document = Importer.ImportDocument(pixiFile);
  39. Assert.NotNull(pngFile);
  40. var result = document.TryRenderWholeImage(0);
  41. Assert.True(result is { IsT1: true, AsT1: not null }); // Check if rendering was successful
  42. using var image = result.AsT1;
  43. using var toCompareTo = Importer.ImportImage(pngFile, document.SizeBindable);
  44. Assert.NotNull(toCompareTo);
  45. Assert.True(PixelCompare(image, toCompareTo));
  46. }
  47. [AvaloniaTheory]
  48. [InlineData("SingleLayer")]
  49. [InlineData("SingleLayerWithMask")]
  50. [InlineData("LayerWithMaskClipped")]
  51. [InlineData("LayerWithMaskClippedHighDpiPresent")]
  52. [InlineData("LayerWithMaskClippedInFolder")]
  53. [InlineData("LayerWithMaskClippedInFolderWithMask")]
  54. public void TestThatHalfResolutionScalesRenderCorrectly(string pixiName)
  55. {
  56. string pixiFile = Path.Combine("TestFiles", "ResolutionTests", pixiName + ".pixi");
  57. var document = Importer.ImportDocument(pixiFile);
  58. using Surface output = Surface.ForDisplay(document.SizeBindable);
  59. document.SceneRenderer.RenderScene(output.DrawingSurface, ChunkResolution.Half);
  60. Color expectedColor = Colors.Yellow;
  61. Assert.True(AllPixelsAreColor(output, expectedColor));
  62. }
  63. private static bool PixelCompare(Surface image, Surface compareTo)
  64. {
  65. if (image.Size != compareTo.Size)
  66. {
  67. return false;
  68. }
  69. using Surface compareSurface1 = new Surface(image.Size);
  70. using Surface compareSurface2 = new Surface(image.Size);
  71. compareSurface1.DrawingSurface.Canvas.DrawSurface(image.DrawingSurface, 0, 0);
  72. compareSurface2.DrawingSurface.Canvas.DrawSurface(compareTo.DrawingSurface, 0, 0);
  73. var imageData1 = compareSurface1.PeekPixels();
  74. var imageData2 = compareSurface2.PeekPixels();
  75. if (imageData1.Width != imageData2.Width || imageData1.Height != imageData2.Height)
  76. {
  77. return false;
  78. }
  79. for (int y = 0; y < imageData1.Height; y++)
  80. {
  81. for (int x = 0; x < imageData1.Width; x++)
  82. {
  83. var pixel1 = imageData1.GetPixelColor(x, y);
  84. var pixel2 = imageData2.GetPixelColor(x, y);
  85. if (pixel1 != pixel2)
  86. {
  87. return false;
  88. }
  89. }
  90. }
  91. return true;
  92. }
  93. private static bool AllPixelsAreColor(Surface image, Color color)
  94. {
  95. var imageData = image.PeekPixels();
  96. for (int y = 0; y < imageData.Height; y++)
  97. {
  98. for (int x = 0; x < imageData.Width; x++)
  99. {
  100. var pixel = imageData.GetPixelColor(x, y);
  101. if (pixel != color)
  102. {
  103. return false;
  104. }
  105. }
  106. }
  107. return true;
  108. }
  109. }