A game about forced loneliness, made by TACStudios
at master 119 lines 4.5 kB view raw
1using System.Collections.Generic; 2using NUnit.Framework; 3using UnityEngine; 4using UnityEditor.Graphing; 5using UnityEditor.ShaderGraph.Internal; 6 7namespace UnityEditor.ShaderGraph.UnitTests 8{ 9 [TestFixture] 10 class BlockNodeTests 11 { 12 static BlockFieldDescriptor s_DescriptorA = new BlockFieldDescriptor("Test", "BlockA", string.Empty, new FloatControl(0.5f), ShaderStage.Fragment, true); 13 static BlockFieldDescriptor s_DescriptorB = new BlockFieldDescriptor("Test", "BlockB", string.Empty, new NormalControl(CoordinateSpace.World), ShaderStage.Fragment, true); 14 15 static Vector3MaterialSlot s_MaterialSlot = new Vector3MaterialSlot(0, "Test", "BlockB", SlotType.Input, Vector3.one); 16 static CustomSlotBlockFieldDescriptor s_CustomSlotDescriptor = new CustomSlotBlockFieldDescriptor("Test", "CustomBlock", string.Empty, 17 () => { return new Vector3MaterialSlot(0, "Test", "BlockB", SlotType.Input, Vector3.one); }); 18 19 [OneTimeSetUp] 20 public void RunBeforeAnyTests() 21 { 22 Debug.unityLogger.logHandler = new ConsoleLogHandler(); 23 } 24 25 [Test] 26 public void CanGatherBlockDescriptors() 27 { 28 GraphData graph = new GraphData(); 29 graph.AddContexts(); 30 31 Assert.IsNotNull(graph.blockFieldDescriptors); 32 Assert.AreNotEqual(0, graph.blockFieldDescriptors.Count); 33 } 34 35 [Test] 36 public void CanInitializeBlockNode() 37 { 38 var node = new BlockNode(); 39 node.Init(s_DescriptorA); 40 41 Assert.IsNotNull(node.descriptor); 42 Assert.AreEqual(s_DescriptorA, node.descriptor); 43 Assert.AreEqual("Test.BlockA", $"{node.descriptor.tag}.{node.descriptor.name}"); 44 } 45 46 [Test] 47 public void CanCreateSlotFromBlockDescriptor() 48 { 49 var node = new BlockNode(); 50 node.Init(s_DescriptorA); 51 List<MaterialSlot> slots = new List<MaterialSlot>(); 52 node.GetSlots(slots); 53 54 Assert.IsNotNull(slots); 55 Assert.AreEqual(1, slots.Count); 56 57 var vector3Slot = slots[0] as Vector1MaterialSlot; 58 Assert.IsNotNull(vector3Slot); 59 Assert.AreEqual(0, vector3Slot.id); 60 Assert.AreEqual(s_DescriptorA.displayName, vector3Slot.RawDisplayName()); 61 Assert.AreEqual(s_DescriptorA.name, vector3Slot.shaderOutputName); 62 Assert.AreEqual(SlotType.Input, vector3Slot.slotType); 63 Assert.AreEqual(((FloatControl)s_DescriptorA.control).value, vector3Slot.value); 64 Assert.AreEqual(s_DescriptorA.shaderStage.GetShaderStageCapability(), vector3Slot.stageCapability); 65 } 66 67 [Test] 68 public void CanCreateSlotFromCustomSlotBlockDescriptor() 69 { 70 var node = new BlockNode(); 71 node.Init(s_CustomSlotDescriptor); 72 List<MaterialSlot> slots = new List<MaterialSlot>(); 73 node.GetSlots(slots); 74 75 Assert.IsNotNull(slots); 76 Assert.AreEqual(1, slots.Count); 77 Assert.AreNotEqual(s_MaterialSlot, slots[0]); //We actually WANT to create a new slot in this case 78 Assert.AreEqual(s_MaterialSlot.displayName, slots[0].displayName); 79 Assert.AreEqual(s_MaterialSlot.valueType, slots[0].valueType); 80 Assert.AreEqual(s_MaterialSlot.value, ((Vector3MaterialSlot)slots[0]).value); 81 } 82 83 [Test] 84 public void CanGetRequirementsFromBlockNode() 85 { 86 var node = new BlockNode(); 87 node.Init(s_DescriptorB); 88 89 var iMayRequireNormals = node as IMayRequireNormal; 90 Assert.IsNotNull(iMayRequireNormals); 91 92 var neededCoordinateSpace = iMayRequireNormals.RequiresNormal(ShaderStageCapability.Fragment); 93 Assert.AreEqual(NeededCoordinateSpace.World, neededCoordinateSpace); 94 } 95 96 [Test] 97 public void CanSerializeDescriptor() 98 { 99 var node = new BlockNode(); 100 node.Init(s_DescriptorA); 101 node.OnBeforeSerialize(); 102 103 Assert.AreEqual("Test.BlockA", node.serializedDescriptor); 104 } 105 106 [Test] 107 public void CanGetBlockIndex() 108 { 109 GraphData graph = new GraphData(); 110 graph.AddContexts(); 111 112 var node = new BlockNode(); 113 node.Init(s_DescriptorA); 114 graph.AddBlock(node, graph.fragmentContext, 0); 115 116 Assert.AreEqual(0, node.index); 117 } 118 } 119}