설명 없음
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.

ButtonTests.cs 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System.Collections;
  2. using System.IO;
  3. using NUnit.Framework;
  4. using UnityEditor;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.TestTools;
  7. using UnityEngine.UI;
  8. using UnityEngine;
  9. public class ButtonTests : IPrebuildSetup
  10. {
  11. GameObject m_PrefabRoot;
  12. const string kPrefabPath = "Assets/Resources/ButtonPrefab.prefab";
  13. public void Setup()
  14. {
  15. #if UNITY_EDITOR
  16. var rootGO = new GameObject("rootGo");
  17. var canvasGO = new GameObject("Canvas", typeof(Canvas));
  18. canvasGO.transform.SetParent(rootGO.transform);
  19. var canvas = canvasGO.GetComponent<Canvas>();
  20. canvas.referencePixelsPerUnit = 100;
  21. GameObject eventSystemGO = new GameObject("EventSystem", typeof(EventSystem));
  22. eventSystemGO.transform.SetParent(rootGO.transform);
  23. GameObject TestButtonGO = new GameObject("TestButton", typeof(RectTransform), typeof(TestButton));
  24. TestButtonGO.transform.SetParent(canvasGO.transform);
  25. if (!Directory.Exists("Assets/Resources/"))
  26. Directory.CreateDirectory("Assets/Resources/");
  27. PrefabUtility.SaveAsPrefabAsset(rootGO, kPrefabPath);
  28. GameObject.DestroyImmediate(rootGO);
  29. #endif
  30. }
  31. [SetUp]
  32. public void TestSetup()
  33. {
  34. m_PrefabRoot = Object.Instantiate(Resources.Load("ButtonPrefab")) as GameObject;
  35. }
  36. [TearDown]
  37. public void TearDown()
  38. {
  39. GameObject.DestroyImmediate(m_PrefabRoot);
  40. }
  41. [OneTimeTearDown]
  42. public void OneTimeTearDown()
  43. {
  44. #if UNITY_EDITOR
  45. AssetDatabase.DeleteAsset(kPrefabPath);
  46. #endif
  47. }
  48. [Test]
  49. public void PressShouldCallClickHandler()
  50. {
  51. Button button = m_PrefabRoot.GetComponentInChildren<Button>();
  52. bool called = false;
  53. button.onClick.AddListener(() => { called = true; });
  54. button.OnPointerClick(new PointerEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>()) { button = PointerEventData.InputButton.Left });
  55. Assert.True(called);
  56. }
  57. [Test]
  58. public void PressInactiveShouldNotCallClickHandler()
  59. {
  60. Button button = m_PrefabRoot.GetComponentInChildren<Button>();
  61. bool called = false;
  62. button.enabled = false;
  63. button.onClick.AddListener(() => { called = true; });
  64. button.OnPointerClick(new PointerEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>()) { button = PointerEventData.InputButton.Left });
  65. Assert.False(called);
  66. }
  67. [Test]
  68. public void PressNotInteractableShouldNotCallClickHandler()
  69. {
  70. Button button = m_PrefabRoot.GetComponentInChildren<Button>();
  71. bool called = false;
  72. button.interactable = false;
  73. button.onClick.AddListener(() => { called = true; });
  74. button.OnPointerClick(new PointerEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>()) { button = PointerEventData.InputButton.Left });
  75. Assert.False(called);
  76. }
  77. [Test]
  78. public void SelectShouldHoldThePreviousStateAfterDisablingAndEnabling()
  79. {
  80. TestButton button = m_PrefabRoot.GetComponentInChildren<TestButton>();
  81. button.onClick.AddListener(() => {
  82. button.Select();
  83. button.enabled = false;
  84. });
  85. button.OnPointerClick(new PointerEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>()) { button = PointerEventData.InputButton.Left });
  86. Assert.False(button.enabled, "Expected button to not be enabled");
  87. button.enabled = true;
  88. Assert.True(button.isStateSelected, "Expected selected state to be true");
  89. }
  90. [Test]
  91. public void SubmitShouldCallClickHandler()
  92. {
  93. Button button = m_PrefabRoot.GetComponentInChildren<Button>();
  94. bool called = false;
  95. button.onClick.AddListener(() => { called = true; });
  96. button.OnSubmit(null);
  97. Assert.True(called);
  98. }
  99. [Test]
  100. public void SubmitInactiveShouldNotCallClickHandler()
  101. {
  102. Button button = m_PrefabRoot.GetComponentInChildren<Button>();
  103. bool called = false;
  104. button.enabled = false;
  105. button.onClick.AddListener(() => { called = true; });
  106. button.OnSubmit(new PointerEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>()) { button = PointerEventData.InputButton.Left });
  107. Assert.False(called);
  108. }
  109. [Test]
  110. public void SubmitNotInteractableShouldNotCallClickHandler()
  111. {
  112. Button button = m_PrefabRoot.GetComponentInChildren<Button>();
  113. bool called = false;
  114. button.interactable = false;
  115. button.onClick.AddListener(() => { called = true; });
  116. button.OnSubmit(new PointerEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>()) { button = PointerEventData.InputButton.Left });
  117. Assert.False(called);
  118. }
  119. [UnityTest]
  120. public IEnumerator SubmitShouldTransitionToPressedStateAndBackToNormal()
  121. {
  122. TestButton button = m_PrefabRoot.GetComponentInChildren<TestButton>();
  123. Assert.True(button.IsTransitionToNormal(0));
  124. button.OnSubmit(null);
  125. Assert.True(button.isStateNormal);
  126. Assert.True(button.IsTransitionToPressed(1));
  127. yield return new WaitWhile(() => button.StateTransitionCount == 2);
  128. // 3rd transition back to normal should have started
  129. Assert.True(button.IsTransitionToNormal(2));
  130. yield return null;
  131. Assert.True(button.isStateNormal);
  132. }
  133. }