No Description
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.

InputModuleTests.cs 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. using System.Collections;
  2. using NUnit.Framework;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using UnityEngine.TestTools;
  6. using UnityEngine.UI;
  7. public class InputModuleTests
  8. {
  9. EventSystem m_EventSystem;
  10. FakeBaseInput m_FakeBaseInput;
  11. StandaloneInputModule m_StandaloneInputModule;
  12. Canvas m_Canvas;
  13. Image m_Image;
  14. Image m_NestedImage;
  15. [SetUp]
  16. public void TestSetup()
  17. {
  18. // Camera | Canvas (Image) | Event System
  19. m_Canvas = new GameObject("Canvas").AddComponent<Canvas>();
  20. m_Canvas.renderMode = RenderMode.ScreenSpaceOverlay;
  21. m_Canvas.gameObject.AddComponent<GraphicRaycaster>();
  22. m_Image = new GameObject("Image").AddComponent<Image>();
  23. m_Image.gameObject.transform.SetParent(m_Canvas.transform);
  24. RectTransform imageRectTransform = m_Image.GetComponent<RectTransform>();
  25. imageRectTransform.sizeDelta = new Vector2(400f, 400f);
  26. imageRectTransform.localPosition = Vector3.zero;
  27. m_NestedImage = new GameObject("NestedImage").AddComponent<Image>();
  28. m_NestedImage.gameObject.transform.SetParent(m_Image.transform);
  29. RectTransform nestedImageRectTransform = m_NestedImage.GetComponent<RectTransform>();
  30. nestedImageRectTransform.sizeDelta = new Vector2(200f, 200f);
  31. nestedImageRectTransform.localPosition = Vector3.zero;
  32. GameObject go = new GameObject("Event System");
  33. m_EventSystem = go.AddComponent<EventSystem>();
  34. m_EventSystem.pixelDragThreshold = 1;
  35. m_StandaloneInputModule = go.AddComponent<StandaloneInputModule>();
  36. m_FakeBaseInput = go.AddComponent<FakeBaseInput>();
  37. // Override input with FakeBaseInput so we can send fake mouse/keyboards button presses and touches
  38. m_StandaloneInputModule.inputOverride = m_FakeBaseInput;
  39. Cursor.lockState = CursorLockMode.None;
  40. }
  41. [UnityTest]
  42. public IEnumerator DragCallbacksDoGetCalled()
  43. {
  44. // While left mouse button is pressed and the mouse is moving, OnBeginDrag and OnDrag callbacks should be called
  45. // Then when the left mouse button is released, OnEndDrag callback should be called
  46. // Add script to EventSystem to update the mouse position
  47. m_EventSystem.gameObject.AddComponent<MouseUpdate>();
  48. // Add script to Image which implements OnBeginDrag, OnDrag & OnEndDrag callbacks
  49. DragCallbackCheck callbackCheck = m_Image.gameObject.AddComponent<DragCallbackCheck>();
  50. // Setting required input.mousePresent to fake mouse presence
  51. m_FakeBaseInput.MousePresent = true;
  52. var canvasRT = m_Canvas.gameObject.transform as RectTransform;
  53. m_FakeBaseInput.MousePosition = new Vector2(Screen.width / 2, Screen.height / 2);
  54. yield return null;
  55. // Left mouse button down simulation
  56. m_FakeBaseInput.MouseButtonDown[0] = true;
  57. yield return null;
  58. // Left mouse button down flag needs to reset in the next frame
  59. m_FakeBaseInput.MouseButtonDown[0] = false;
  60. yield return null;
  61. // Left mouse button up simulation
  62. m_FakeBaseInput.MouseButtonUp[0] = true;
  63. yield return null;
  64. // Left mouse button up flag needs to reset in the next frame
  65. m_FakeBaseInput.MouseButtonUp[0] = false;
  66. yield return null;
  67. Assert.IsTrue(callbackCheck.onBeginDragCalled, "OnBeginDrag not called");
  68. Assert.IsTrue(callbackCheck.onDragCalled, "OnDragCalled not called");
  69. Assert.IsTrue(callbackCheck.onEndDragCalled, "OnEndDragCalled not called");
  70. Assert.IsTrue(callbackCheck.onDropCalled, "OnDrop not called");
  71. }
  72. [UnityTest]
  73. public IEnumerator MouseOutsideMaskRectTransform_WhileInsidePaddedArea_PerformsClick()
  74. {
  75. var mask = new GameObject("Panel").AddComponent<RectMask2D>();
  76. mask.gameObject.transform.SetParent(m_Canvas.transform);
  77. RectTransform panelRectTransform = mask.GetComponent<RectTransform>();
  78. panelRectTransform.sizeDelta = new Vector2(100, 100f);
  79. panelRectTransform.localPosition = Vector3.zero;
  80. m_Image.gameObject.transform.SetParent(mask.transform, true);
  81. mask.padding = new Vector4(-30, -30, -30, -30);
  82. PointerClickCallbackCheck callbackCheck = m_Image.gameObject.AddComponent<PointerClickCallbackCheck>();
  83. var canvasRT = m_Canvas.gameObject.transform as RectTransform;
  84. var screenMiddle = new Vector2(Screen.width / 2, Screen.height / 2);
  85. m_FakeBaseInput.MousePresent = true;
  86. m_FakeBaseInput.MousePosition = screenMiddle;
  87. yield return null;
  88. // Click the center of the screen should hit the middle of the image.
  89. m_FakeBaseInput.MouseButtonDown[0] = true;
  90. yield return null;
  91. m_FakeBaseInput.MouseButtonDown[0] = false;
  92. yield return null;
  93. m_FakeBaseInput.MouseButtonUp[0] = true;
  94. yield return null;
  95. m_FakeBaseInput.MouseButtonUp[0] = false;
  96. yield return null;
  97. Assert.IsTrue(callbackCheck.pointerDown);
  98. //Reset the callbackcheck and click outside the mask but still in the image.
  99. callbackCheck.pointerDown = false;
  100. m_FakeBaseInput.MousePosition = new Vector2(screenMiddle.x - 60, screenMiddle.y);
  101. yield return null;
  102. m_FakeBaseInput.MouseButtonDown[0] = true;
  103. yield return null;
  104. m_FakeBaseInput.MouseButtonDown[0] = false;
  105. yield return null;
  106. m_FakeBaseInput.MouseButtonUp[0] = true;
  107. yield return null;
  108. m_FakeBaseInput.MouseButtonUp[0] = false;
  109. yield return null;
  110. Assert.IsTrue(callbackCheck.pointerDown);
  111. //Reset the callbackcheck and click outside the mask and outside in the image.
  112. callbackCheck.pointerDown = false;
  113. m_FakeBaseInput.MousePosition = new Vector2(screenMiddle.x - 100, screenMiddle.y);
  114. yield return null;
  115. m_FakeBaseInput.MouseButtonDown[0] = true;
  116. yield return null;
  117. m_FakeBaseInput.MouseButtonDown[0] = false;
  118. yield return null;
  119. m_FakeBaseInput.MouseButtonUp[0] = true;
  120. yield return null;
  121. m_FakeBaseInput.MouseButtonUp[0] = false;
  122. yield return null;
  123. Assert.IsFalse(callbackCheck.pointerDown);
  124. }
  125. [UnityTest]
  126. public IEnumerator PointerEnterChildShouldNotFullyExit_NotSendPointerEventToParent()
  127. {
  128. m_StandaloneInputModule.sendPointerHoverToParent = false;
  129. PointerExitCallbackCheck callbackCheck = m_Image.gameObject.AddComponent<PointerExitCallbackCheck>();
  130. m_NestedImage.gameObject.AddComponent<PointerExitCallbackCheck>();
  131. var screenMiddle = new Vector2(Screen.width / 2, Screen.height / 2);
  132. m_FakeBaseInput.MousePresent = true;
  133. m_FakeBaseInput.MousePosition = screenMiddle - new Vector2(150, 150);
  134. yield return null;
  135. m_FakeBaseInput.MousePosition = screenMiddle;
  136. yield return null;
  137. Assert.IsTrue(callbackCheck.pointerData.fullyExited == false);
  138. }
  139. [UnityTest]
  140. public IEnumerator PointerEnterChildShouldNotExit_SendPointerEventToParent()
  141. {
  142. m_StandaloneInputModule.sendPointerHoverToParent = true;
  143. PointerExitCallbackCheck callbackCheck = m_Image.gameObject.AddComponent<PointerExitCallbackCheck>();
  144. m_NestedImage.gameObject.AddComponent<PointerExitCallbackCheck>();
  145. var screenMiddle = new Vector2(Screen.width / 2, Screen.height / 2);
  146. m_FakeBaseInput.MousePresent = true;
  147. m_FakeBaseInput.MousePosition = screenMiddle - new Vector2(150, 150);
  148. yield return null;
  149. m_FakeBaseInput.MousePosition = screenMiddle;
  150. yield return null;
  151. Assert.IsTrue(callbackCheck.pointerData == null);
  152. }
  153. [UnityTest]
  154. public IEnumerator PointerEnterChildShouldNotReenter()
  155. {
  156. PointerEnterCallbackCheck callbackCheck = m_NestedImage.gameObject.AddComponent<PointerEnterCallbackCheck>();
  157. var screenMiddle = new Vector2(Screen.width / 2, Screen.height / 2);
  158. m_FakeBaseInput.MousePresent = true;
  159. m_FakeBaseInput.MousePosition = screenMiddle - new Vector2(150, 150);
  160. yield return null;
  161. m_FakeBaseInput.MousePosition = screenMiddle;
  162. yield return null;
  163. Assert.IsTrue(callbackCheck.pointerData.reentered == false);
  164. }
  165. [UnityTest]
  166. public IEnumerator PointerExitChildShouldReenter_NotSendPointerEventToParent()
  167. {
  168. m_StandaloneInputModule.sendPointerHoverToParent = false;
  169. PointerEnterCallbackCheck callbackCheck = m_Image.gameObject.AddComponent<PointerEnterCallbackCheck>();
  170. var screenMiddle = new Vector2(Screen.width / 2, Screen.height / 2);
  171. m_FakeBaseInput.MousePresent = true;
  172. m_FakeBaseInput.MousePosition = screenMiddle - new Vector2(150, 150);
  173. yield return null;
  174. m_FakeBaseInput.MousePosition = screenMiddle;
  175. yield return null;
  176. m_FakeBaseInput.MousePosition = screenMiddle - new Vector2(150, 150);
  177. yield return null;
  178. Assert.IsTrue(callbackCheck.pointerData.reentered == true);
  179. }
  180. [UnityTest]
  181. public IEnumerator PointerExitChildShouldNotSendEnter_SendPointerEventToParent()
  182. {
  183. m_StandaloneInputModule.sendPointerHoverToParent = true;
  184. m_NestedImage.gameObject.AddComponent<PointerEnterCallbackCheck>();
  185. var screenMiddle = new Vector2(Screen.width / 2, Screen.height / 2);
  186. m_FakeBaseInput.MousePresent = true;
  187. m_FakeBaseInput.MousePosition = screenMiddle;
  188. yield return null;
  189. PointerEnterCallbackCheck callbackCheck = m_Image.gameObject.AddComponent<PointerEnterCallbackCheck>();
  190. m_FakeBaseInput.MousePosition = screenMiddle - new Vector2(150, 150);
  191. yield return null;
  192. Assert.IsTrue(callbackCheck.pointerData == null);
  193. }
  194. [UnityTest]
  195. public IEnumerator PointerExitChildShouldFullyExit()
  196. {
  197. PointerExitCallbackCheck callbackCheck = m_NestedImage.gameObject.AddComponent<PointerExitCallbackCheck>();
  198. var screenMiddle = new Vector2(Screen.width / 2, Screen.height / 2);
  199. m_FakeBaseInput.MousePresent = true;
  200. m_FakeBaseInput.MousePosition = screenMiddle - new Vector2(150, 150);
  201. yield return null;
  202. m_FakeBaseInput.MousePosition = screenMiddle;
  203. yield return null;
  204. m_FakeBaseInput.MousePosition = screenMiddle - new Vector2(150, 150);
  205. yield return null;
  206. Assert.IsTrue(callbackCheck.pointerData.fullyExited == true);
  207. }
  208. [TearDown]
  209. public void TearDown()
  210. {
  211. GameObject.DestroyImmediate(m_EventSystem.gameObject);
  212. GameObject.DestroyImmediate(m_Canvas.gameObject);
  213. }
  214. }