NodeSystemTests.cs 5.4 KB

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