Ei kuvausta
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HeatmapTests.cs 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using NUnit.Framework;
  3. using UnityEngine;
  4. namespace UnityEditor.ShaderGraph.UnitTests
  5. {
  6. class HeatmapTests
  7. {
  8. class TestNode : AbstractMaterialNode { }
  9. class TemporarySubGraph : IDisposable
  10. {
  11. SubGraphAsset m_SubGraph;
  12. public SubGraphNode Node { get; }
  13. public TemporarySubGraph()
  14. {
  15. m_SubGraph = ScriptableObject.CreateInstance<SubGraphAsset>();
  16. AssetDatabase.CreateAsset(m_SubGraph, AssetDatabase.GenerateUniqueAssetPath("Assets/HeatmapTests_TemporarySubGraph.asset"));
  17. Node = new SubGraphNode {asset = m_SubGraph};
  18. Assert.IsFalse(string.IsNullOrEmpty(Node.subGraphGuid), "Temporary subgraph was not created correctly.");
  19. }
  20. public void Dispose()
  21. {
  22. AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(m_SubGraph));
  23. }
  24. }
  25. [Test]
  26. public void TestHeatmapEntries_TryGetCategory_PresentEntry_ReturnsValue()
  27. {
  28. var he = new HeatmapEntries();
  29. he.Entries.Add(new HeatmapEntry("Test", 123));
  30. Assert.IsTrue(he.TryGetCategory("Test", out var category));
  31. Assert.AreEqual(123, category);
  32. }
  33. [Test]
  34. public void TestHeatmapEntries_TryGetCategory_MissingEntry_ReturnsZero()
  35. {
  36. var he = new HeatmapEntries();
  37. Assert.IsFalse(he.TryGetCategory("Test", out var category));
  38. Assert.AreEqual(0, category);
  39. }
  40. [Test]
  41. public void TestHeatmapEntries_TryGetCategory_MissingEntryNonEmpty_ReturnsZero()
  42. {
  43. var he = new HeatmapEntries();
  44. he.Entries.Add(new HeatmapEntry("Test", 123));
  45. Assert.IsFalse(he.TryGetCategory("Test2", out var category));
  46. Assert.AreEqual(0, category);
  47. }
  48. [Test]
  49. public void TestShaderGraphHeatmapValues_GetHeatmapKey_BuiltInNode_IsTypeName()
  50. {
  51. var key = ShaderGraphHeatmapValues.GetHeatmapKey(new TestNode());
  52. Assert.AreEqual("TestNode", key);
  53. }
  54. [Test]
  55. public void TestShaderGraphHeatmapValues_GetHeatmapKey_SubGraph_IsSubGraphGuid()
  56. {
  57. using var sub = new TemporarySubGraph();
  58. var key = ShaderGraphHeatmapValues.GetHeatmapKey(sub.Node);
  59. Assert.AreEqual(sub.Node.subGraphGuid, key);
  60. }
  61. [Test]
  62. public void TestShaderGraphHeatmapValues_TryGetCategory_PresentBuiltInNode_ReturnsValue()
  63. {
  64. var heatmapValues = ScriptableObject.CreateInstance<ShaderGraphHeatmapValues>();
  65. heatmapValues.m_Colors = new[] {Color.white};
  66. heatmapValues.Nodes.Entries.Add(new HeatmapEntry("TestNode", 0));
  67. Assert.IsTrue(heatmapValues.TryGetCategoryColor(new TestNode(), out var category));
  68. Assert.AreEqual(Color.white, category);
  69. }
  70. [Test]
  71. public void TestShaderGraphHeatmapValues_TryGetCategory_PresentSubGraph_ReturnsValue()
  72. {
  73. using var sub = new TemporarySubGraph();
  74. var heatmapValues = ScriptableObject.CreateInstance<ShaderGraphHeatmapValues>();
  75. heatmapValues.m_Colors = new[] {Color.white};
  76. heatmapValues.Subgraphs.Entries.Add(new HeatmapEntry(sub.Node.subGraphGuid, 0));
  77. Assert.IsTrue(heatmapValues.TryGetCategoryColor(sub.Node, out var category));
  78. Assert.AreEqual(Color.white, category);
  79. }
  80. [Test]
  81. public void TestShaderGraphHeatmapValues_TryGetCategory_MissingNode_ReturnsDefault()
  82. {
  83. var heatmapValues = ScriptableObject.CreateInstance<ShaderGraphHeatmapValues>();
  84. heatmapValues.m_Colors = new[] {Color.white};
  85. Assert.IsFalse(heatmapValues.TryGetCategoryColor(new TestNode(), out var category));
  86. Assert.AreEqual(default(Color), category);
  87. }
  88. [Test]
  89. public void TestShaderGraphHeatmapValues_TryGetCategory_MissingSubGraph_ReturnsDefault()
  90. {
  91. var heatmapValues = ScriptableObject.CreateInstance<ShaderGraphHeatmapValues>();
  92. heatmapValues.m_Colors = new[] {Color.white};
  93. using var sub = new TemporarySubGraph();
  94. Assert.IsFalse(heatmapValues.TryGetCategoryColor(sub.Node, out var category));
  95. Assert.AreEqual(default(Color), category);
  96. }
  97. [Test]
  98. public void TestShaderGraphHeatmapValues_TryGetCategory_ValueIsClampedToColorCount()
  99. {
  100. var heatmapValues = ScriptableObject.CreateInstance<ShaderGraphHeatmapValues>();
  101. heatmapValues.m_Colors = new[] { Color.red, Color.green, Color.blue };
  102. using var s1 = new TemporarySubGraph();
  103. using var s2 = new TemporarySubGraph();
  104. using var s3 = new TemporarySubGraph();
  105. heatmapValues.Subgraphs.Entries.Add(new HeatmapEntry(s1.Node.subGraphGuid, -100));
  106. heatmapValues.Subgraphs.Entries.Add(new HeatmapEntry(s2.Node.subGraphGuid, 1));
  107. heatmapValues.Subgraphs.Entries.Add(new HeatmapEntry(s3.Node.subGraphGuid, 100));
  108. Assert.IsTrue(heatmapValues.TryGetCategoryColor(s1.Node, out var category));
  109. Assert.AreEqual(Color.red, category);
  110. Assert.IsTrue(heatmapValues.TryGetCategoryColor(s2.Node, out category));
  111. Assert.AreEqual(Color.green, category);
  112. Assert.IsTrue(heatmapValues.TryGetCategoryColor(s3.Node, out category));
  113. Assert.AreEqual(Color.blue, category);
  114. }
  115. }
  116. }