Без опису
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

PropertyDrawerTests.cs 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using NUnit.Framework;
  2. using System.Collections;
  3. using System.Linq;
  4. using UnityEditor;
  5. using UnityEditor.UIElements;
  6. using UnityEngine;
  7. using UnityEngine.TestTools;
  8. using UnityEngine.UI;
  9. using UnityEngine.UIElements;
  10. using UnityEngine.Search;
  11. using UnityEngine;
  12. public class PropertyDrawerTests
  13. {
  14. class PropertyDrawerTestsWindow : EditorWindow
  15. {
  16. public Navigation navigation;
  17. public SpriteState spriteState;
  18. public Dropdown.OptionDataList optionDataList;
  19. [SearchContext("t:gameObject is:gameObject", "gameObject")]
  20. public GameObject searchContext;
  21. public ExposedReference<GameObject> exposedReference;
  22. SerializedObject serializedObject;
  23. void CreateGUI()
  24. {
  25. serializedObject = new SerializedObject(this);
  26. Add(nameof(navigation));
  27. Add(nameof(spriteState));
  28. Add(nameof(optionDataList));
  29. Add(nameof(searchContext));
  30. Add(nameof(exposedReference));
  31. rootVisualElement.Bind(serializedObject);
  32. // Forces visual tree update
  33. Rebuild();
  34. }
  35. void Add(string propertyName)
  36. {
  37. rootVisualElement.Add(new PropertyField() { bindingPath = propertyName });
  38. }
  39. public SerializedProperty Property(string propertyName) => serializedObject.FindProperty(propertyName);
  40. public void Rebuild() => rootVisualElement.Bind(serializedObject);
  41. }
  42. static PropertyDrawerTestsWindow window;
  43. [OneTimeSetUp]
  44. [MenuItem("Tests/Open Property Drawer Test Window")]
  45. public static void OneTimeSetUp() => window = EditorWindow.GetWindow<PropertyDrawerTestsWindow>();
  46. [OneTimeTearDown]
  47. public void OneTimeTearDown() => window.Close();
  48. [UnityTest]
  49. public IEnumerator NavigationDrawer_IsVisible()
  50. {
  51. yield return null;
  52. Assert.IsNotNull(window.rootVisualElement.Query<VisualElement>("Navigation").Build().First());
  53. }
  54. [UnityTest]
  55. public IEnumerator SpriteStateDrawer_IsVisible()
  56. {
  57. yield return null;
  58. Assert.IsNotNull(window.rootVisualElement.Q("SpriteState"));
  59. }
  60. [UnityTest, Ignore("Disabled for Instability https://jira.unity3d.com/browse/UUM-11060")]
  61. public IEnumerator DropdownOptionDataListDrawer_IsVisible()
  62. {
  63. yield return null;
  64. Assert.IsNotNull(window.rootVisualElement.Q("DropdownOptionDataList"), $"Item is null. Root object count: {window.rootVisualElement.childCount}");
  65. }
  66. [UnityTest]
  67. public IEnumerator SearchContextDrawer_IsVisible()
  68. {
  69. yield return null;
  70. Assert.IsNotNull(window.rootVisualElement.Q("SearchContext"));
  71. }
  72. [UnityTest, Ignore("Disabled for Instability https://jira.unity3d.com/browse/UUM-11060")]
  73. public IEnumerator ExposedReferenceDrawer_IsVisible()
  74. {
  75. yield return null;
  76. Assert.IsNotNull(window.rootVisualElement.Q("ExposedReference"), $"Item is null. Root object count: {window.rootVisualElement.childCount}");
  77. }
  78. // Fake expected result in order to make TestCaseAttribute to work with UnityTest
  79. [UnityTest]
  80. [TestCase(new object[] { (int)Navigation.Mode.None, 0}, ExpectedResult = null)]
  81. [TestCase(new object[] { (int)Navigation.Mode.Horizontal, 1 }, ExpectedResult = null)]
  82. [TestCase(new object[] { (int)Navigation.Mode.Vertical, 1 }, ExpectedResult = null)]
  83. [TestCase(new object[] { (int)Navigation.Mode.Automatic, 0 }, ExpectedResult = null)]
  84. [TestCase(new object[] { (int)Navigation.Mode.Explicit, 4 }, ExpectedResult = null)]
  85. [TestCase(new object[] { (int)(Navigation.Mode.Explicit | Navigation.Mode.Horizontal), 0 }, ExpectedResult = null)]
  86. [TestCase(new object[] { (int)(Navigation.Mode.Automatic | Navigation.Mode.Explicit), 0 }, ExpectedResult = null)]
  87. public static IEnumerator NavigationDrawer_ShowsCorrectAdditionalControlCount(int mode, int expectedCount)
  88. {
  89. window.Property("navigation.m_Mode").enumValueFlag = mode;
  90. window.Rebuild();
  91. yield return null;
  92. var indent = window.rootVisualElement.Query<VisualElement>("Navigation").Build().First().Query<VisualElement>("Indent").Build().First();
  93. var visibleChildren = indent.Children().Count(child => child.resolvedStyle.display != DisplayStyle.None);
  94. Assert.AreEqual(expectedCount, visibleChildren, $"{expectedCount} additional Navigation object properties should be " +
  95. $"visible when 'Mode' is set to '{(Navigation.Mode)window.Property("navigation.m_Mode").enumValueFlag}'");
  96. }
  97. }