SerializationTests.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using Avalonia.Headless.XUnit;
  2. using Drawie.Backend.Core;
  3. using Drawie.Backend.Core.Bridge;
  4. using Drawie.Backend.Core.ColorsImpl;
  5. using Drawie.Backend.Core.ColorsImpl.Paintables;
  6. using Drawie.Backend.Core.Surfaces.ImageData;
  7. using Drawie.Backend.Core.Surfaces.PaintImpl;
  8. using Drawie.Numerics;
  9. using Drawie.Skia;
  10. using DrawiEngine;
  11. using PixiEditor.ChangeableDocument.Changeables.Interfaces;
  12. using PixiEditor.ChangeableDocument.Changes.NodeGraph;
  13. using PixiEditor.Models.IO;
  14. using PixiEditor.Models.Serialization;
  15. using PixiEditor.Models.Serialization.Factories;
  16. using PixiEditor.Models.Serialization.Factories.Paintables;
  17. using PixiEditor.Parser.Skia.Encoders;
  18. using PixiEditor.ViewModels.Document;
  19. namespace PixiEditor.Tests;
  20. public class SerializationTests : PixiEditorTest
  21. {
  22. [Fact]
  23. public void TestThatAllPaintablesHaveFactories()
  24. {
  25. var allDrawiePaintableTypes = typeof(Paintable).Assembly.GetTypes()
  26. .Where(x => x.IsAssignableTo(typeof(Paintable))
  27. && x is { IsAbstract: false, IsInterface: false }).ToList();
  28. var pixiEditorAssemblyPaintables = typeof(SerializationFactory).Assembly.GetTypes()
  29. .Where(x => x.IsAssignableTo(typeof(Paintable))
  30. && x is { IsAbstract: false, IsInterface: false }).ToList();
  31. var allPaintables = allDrawiePaintableTypes.Concat(pixiEditorAssemblyPaintables).Distinct().ToList();
  32. var allFoundFactories = typeof(SerializationFactory).Assembly.GetTypes()
  33. .Where(x => x.IsAssignableTo(typeof(IPaintableSerializationFactory))
  34. && x is { IsAbstract: false, IsInterface: false }).ToList();
  35. List<SerializationFactory> factories = new();
  36. QoiEncoder encoder = new QoiEncoder();
  37. SerializationConfig config = new SerializationConfig(encoder, ColorSpace.CreateSrgbLinear());
  38. foreach (var factoryType in allFoundFactories)
  39. {
  40. var factory = (SerializationFactory)Activator.CreateInstance(factoryType);
  41. factories.Add(factory);
  42. }
  43. foreach (var type in allPaintables)
  44. {
  45. var factory = factories.FirstOrDefault(x => x.OriginalType == type);
  46. Assert.NotNull(factory);
  47. }
  48. }
  49. [Fact]
  50. public void TestTexturePaintableFactory()
  51. {
  52. Texture texture = new Texture(new VecI(32, 32));
  53. texture.DrawingSurface.Canvas.DrawCircle(16, 16, 16, new Paint() { Color = Colors.Red, BlendMode = Drawie.Backend.Core.Surfaces.BlendMode.Src });
  54. TexturePaintable paintable = new TexturePaintable(texture);
  55. TexturePaintableSerializationFactory factory = new TexturePaintableSerializationFactory();
  56. factory.Config = new SerializationConfig(new QoiEncoder(), ColorSpace.CreateSrgbLinear());
  57. var serialized = factory.Serialize(paintable);
  58. var deserialized = (TexturePaintable)factory.Deserialize(serialized, default);
  59. Assert.NotNull(deserialized);
  60. var deserializedImage = deserialized.Image;
  61. Assert.NotNull(deserializedImage);
  62. Assert.Equal(texture.Size, deserializedImage.Size);
  63. for (int y = 0; y < texture.Size.Y; y++)
  64. {
  65. for (int x = 0; x < texture.Size.X; x++)
  66. {
  67. Color originalPixel = texture.GetSrgbPixel(new VecI(x, y));
  68. Color deserializedPixel = deserializedImage.GetSrgbPixel(new VecI(x, y));
  69. Assert.Equal(originalPixel, deserializedPixel);
  70. }
  71. }
  72. }
  73. [AvaloniaTheory]
  74. [InlineData("Fibi")]
  75. [InlineData("Pond")]
  76. [InlineData("SmlPxlCircShadWithMask")]
  77. [InlineData("SmallPixelArtCircleShadow")]
  78. [InlineData("SmlPxlCircShadWithMaskClipped")]
  79. [InlineData("SmlPxlCircShadWithMaskClippedInFolder")]
  80. [InlineData("VectorRectangleClippedToCircle")]
  81. [InlineData("VectorRectangleClippedToCircleShadowFilter")]
  82. [InlineData("VectorRectangleClippedToCircleMasked")]
  83. public void TestThatDeserializationOfSampleFilesDoesntThrow(string fileName)
  84. {
  85. string pixiFile = Path.Combine("TestFiles", "RenderTests", fileName + ".pixi");
  86. var document = Importer.ImportDocument(pixiFile);
  87. Assert.NotNull(document);
  88. }
  89. }