NodeSystemTests.cs 5.1 KB

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