NodeSystemTests.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System.Reflection;
  2. using Drawie.Backend.Core.Bridge;
  3. using Drawie.Interop.VulkanAvalonia;
  4. using Drawie.Skia;
  5. using PixiEditor.ChangeableDocument.Changeables.Graph;
  6. using PixiEditor.ChangeableDocument.Changeables.Graph.Nodes;
  7. using PixiEditor.ChangeableDocument.Changeables.Interfaces;
  8. using PixiEditor.ChangeableDocument.Changes.NodeGraph;
  9. using PixiEditor.Models.Serialization;
  10. using PixiEditor.Models.Serialization.Factories;
  11. using PixiEditor.Parser.Skia.Encoders;
  12. using Xunit.Abstractions;
  13. namespace PixiEditor.Backend.Tests;
  14. public class NodeSystemTests
  15. {
  16. private readonly ITestOutputHelper output;
  17. private Type[] knownNonSerializableTypes = new[]
  18. {
  19. typeof(Filter),
  20. typeof(Painter)
  21. };
  22. public NodeSystemTests(ITestOutputHelper output)
  23. {
  24. this.output = output;
  25. if (!DrawingBackendApi.HasBackend)
  26. DrawingBackendApi.SetupBackend(new SkiaDrawingBackend(), new AvaloniaRenderingDispatcher());
  27. }
  28. [Fact]
  29. public void TestThatCreateSimpleNodeDoesntThrow()
  30. {
  31. var allNodeTypes = typeof(Node).Assembly.GetTypes()
  32. .Where(x =>
  33. x.IsAssignableTo(typeof(Node))
  34. && x is { IsAbstract: false, IsInterface: false }
  35. && x.GetCustomAttribute<PairNodeAttribute>() == null).ToList();
  36. IReadOnlyDocument target = new MockDocument();
  37. foreach (var type in allNodeTypes)
  38. {
  39. var node = NodeOperations.CreateNode(type, target);
  40. Assert.NotNull(node);
  41. }
  42. }
  43. [Fact]
  44. public void TestThatCreatePairNodeDoesntThrow()
  45. {
  46. var allNodeTypes = typeof(Node).Assembly.GetTypes()
  47. .Where(x =>
  48. x.IsAssignableTo(typeof(Node))
  49. && x is { IsAbstract: false, IsInterface: false }
  50. && x.GetCustomAttribute<PairNodeAttribute>() != null).ToList();
  51. IReadOnlyDocument target = new MockDocument();
  52. Dictionary<Type, Type> pairs = new();
  53. for (var i = 0; i < allNodeTypes.Count; i++)
  54. {
  55. var type = allNodeTypes[i];
  56. var pairAttribute = type.GetCustomAttribute<PairNodeAttribute>();
  57. if (pairAttribute == null) continue;
  58. if (!pairAttribute.IsStartingType) continue;
  59. pairs[type] = pairAttribute.OtherType;
  60. }
  61. foreach (var type in pairs)
  62. {
  63. var startNode = NodeOperations.CreateNode(type.Key, target);
  64. var endNode = NodeOperations.CreateNode(type.Value, target);
  65. Assert.NotNull(startNode);
  66. Assert.NotNull(endNode);
  67. }
  68. }
  69. [Fact]
  70. public void TestThatSerializeNodeDoesntThrow()
  71. {
  72. var allNodeTypes = GetNodeTypesWithoutPairs();
  73. IReadOnlyDocument target = new MockDocument();
  74. foreach (var type in allNodeTypes)
  75. {
  76. var node = NodeOperations.CreateNode(type, target);
  77. Assert.NotNull(node);
  78. Dictionary<string, object> data = new Dictionary<string, object>();
  79. node.SerializeAdditionalData(data);
  80. Assert.NotNull(data);
  81. }
  82. }
  83. [Fact]
  84. private void TestThatAllInputsAreSerializableOrHaveFactories()
  85. {
  86. var allNodeTypes = GetNodeTypesWithoutPairs();
  87. var allFoundFactories = typeof(SerializationFactory).Assembly.GetTypes()
  88. .Where(x => x.IsAssignableTo(typeof(SerializationFactory))
  89. && x is { IsAbstract: false, IsInterface: false }).ToList();
  90. List<SerializationFactory> factories = new();
  91. QoiEncoder encoder = new QoiEncoder();
  92. SerializationConfig config = new SerializationConfig(encoder);
  93. foreach (var factoryType in allFoundFactories)
  94. {
  95. var factory = (SerializationFactory)Activator.CreateInstance(factoryType);
  96. factories.Add(factory);
  97. }
  98. IReadOnlyDocument target = new MockDocument();
  99. foreach (var type in allNodeTypes)
  100. {
  101. var node = NodeOperations.CreateNode(type, target);
  102. Assert.NotNull(node);
  103. foreach (var input in node.InputProperties)
  104. {
  105. if (knownNonSerializableTypes.Contains(input.ValueType)) continue;
  106. if(input.ValueType.IsAbstract) continue;
  107. if (input.ValueType.IsAssignableTo(typeof(Delegate))) continue;
  108. bool hasFactory = factories.Any(x => x.OriginalType == input.ValueType);
  109. Assert.True(
  110. input.ValueType.IsValueType || input.ValueType == typeof(string) || hasFactory,
  111. $"{input.ValueType} doesn't have a factory and is not serializable. Property: {input.InternalPropertyName}, NodeType: {node.GetType().Name}");
  112. }
  113. }
  114. }
  115. private static List<Type> GetNodeTypesWithoutPairs()
  116. {
  117. var allNodeTypes = typeof(Node).Assembly.GetTypes()
  118. .Where(x =>
  119. x.IsAssignableTo(typeof(Node))
  120. && x is { IsAbstract: false, IsInterface: false }
  121. && x.GetCustomAttribute<PairNodeAttribute>() == null).ToList();
  122. return allNodeTypes;
  123. }
  124. }