InputModuleTests.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. [SetUp]
  15. public void TestSetup()
  16. {
  17. // Camera | Canvas (Image) | Event System
  18. m_Canvas = new GameObject("Canvas").AddComponent<Canvas>();
  19. m_Canvas.renderMode = RenderMode.ScreenSpaceOverlay;
  20. m_Canvas.gameObject.AddComponent<GraphicRaycaster>();
  21. m_Image = new GameObject("Image").AddComponent<Image>();
  22. m_Image.gameObject.transform.SetParent(m_Canvas.transform);
  23. RectTransform imageRectTransform = m_Image.GetComponent<RectTransform>();
  24. imageRectTransform.sizeDelta = new Vector2(400f, 400f);
  25. imageRectTransform.localPosition = Vector3.zero;
  26. GameObject go = new GameObject("Event System");
  27. m_EventSystem = go.AddComponent<EventSystem>();
  28. m_EventSystem.pixelDragThreshold = 1;
  29. m_StandaloneInputModule = go.AddComponent<StandaloneInputModule>();
  30. m_FakeBaseInput = go.AddComponent<FakeBaseInput>();
  31. // Override input with FakeBaseInput so we can send fake mouse/keyboards button presses and touches
  32. m_StandaloneInputModule.inputOverride = m_FakeBaseInput;
  33. Cursor.lockState = CursorLockMode.None;
  34. }
  35. [UnityTest]
  36. public IEnumerator DragCallbacksDoGetCalled()
  37. {
  38. // While left mouse button is pressed and the mouse is moving, OnBeginDrag and OnDrag callbacks should be called
  39. // Then when the left mouse button is released, OnEndDrag callback should be called
  40. // Add script to EventSystem to update the mouse position
  41. m_EventSystem.gameObject.AddComponent<MouseUpdate>();
  42. // Add script to Image which implements OnBeginDrag, OnDrag & OnEndDrag callbacks
  43. DragCallbackCheck callbackCheck = m_Image.gameObject.AddComponent<DragCallbackCheck>();
  44. // Setting required input.mousePresent to fake mouse presence
  45. m_FakeBaseInput.MousePresent = true;
  46. var canvasRT = m_Canvas.gameObject.transform as RectTransform;
  47. m_FakeBaseInput.MousePosition = new Vector2(Screen.width / 2, Screen.height / 2);
  48. yield return null;
  49. // Left mouse button down simulation
  50. m_FakeBaseInput.MouseButtonDown[0] = true;
  51. yield return null;
  52. // Left mouse button down flag needs to reset in the next frame
  53. m_FakeBaseInput.MouseButtonDown[0] = false;
  54. yield return null;
  55. // Left mouse button up simulation
  56. m_FakeBaseInput.MouseButtonUp[0] = true;
  57. yield return null;
  58. // Left mouse button up flag needs to reset in the next frame
  59. m_FakeBaseInput.MouseButtonUp[0] = false;
  60. yield return null;
  61. Assert.IsTrue(callbackCheck.onBeginDragCalled, "OnBeginDrag not called");
  62. Assert.IsTrue(callbackCheck.onDragCalled, "OnDragCalled not called");
  63. Assert.IsTrue(callbackCheck.onEndDragCalled, "OnEndDragCalled not called");
  64. Assert.IsTrue(callbackCheck.onDropCalled, "OnDrop not called");
  65. }
  66. [UnityTest]
  67. public IEnumerator MouseOutsideMaskRectTransform_WhileInsidePaddedArea_PerformsClick()
  68. {
  69. var mask = new GameObject("Panel").AddComponent<RectMask2D>();
  70. mask.gameObject.transform.SetParent(m_Canvas.transform);
  71. RectTransform panelRectTransform = mask.GetComponent<RectTransform>();
  72. panelRectTransform.sizeDelta = new Vector2(100, 100f);
  73. panelRectTransform.localPosition = Vector3.zero;
  74. m_Image.gameObject.transform.SetParent(mask.transform, true);
  75. mask.padding = new Vector4(-30, -30, -30, -30);
  76. PointerClickCallbackCheck callbackCheck = m_Image.gameObject.AddComponent<PointerClickCallbackCheck>();
  77. var canvasRT = m_Canvas.gameObject.transform as RectTransform;
  78. var screenMiddle = new Vector2(Screen.width / 2, Screen.height / 2);
  79. m_FakeBaseInput.MousePresent = true;
  80. m_FakeBaseInput.MousePosition = screenMiddle;
  81. yield return null;
  82. // Click the center of the screen should hit the middle of the image.
  83. m_FakeBaseInput.MouseButtonDown[0] = true;
  84. yield return null;
  85. m_FakeBaseInput.MouseButtonDown[0] = false;
  86. yield return null;
  87. m_FakeBaseInput.MouseButtonUp[0] = true;
  88. yield return null;
  89. m_FakeBaseInput.MouseButtonUp[0] = false;
  90. yield return null;
  91. Assert.IsTrue(callbackCheck.pointerDown);
  92. //Reset the callbackcheck and click outside the mask but still in the image.
  93. callbackCheck.pointerDown = false;
  94. m_FakeBaseInput.MousePosition = new Vector2(screenMiddle.x - 60, screenMiddle.y);
  95. yield return null;
  96. m_FakeBaseInput.MouseButtonDown[0] = true;
  97. yield return null;
  98. m_FakeBaseInput.MouseButtonDown[0] = false;
  99. yield return null;
  100. m_FakeBaseInput.MouseButtonUp[0] = true;
  101. yield return null;
  102. m_FakeBaseInput.MouseButtonUp[0] = false;
  103. yield return null;
  104. Assert.IsTrue(callbackCheck.pointerDown);
  105. //Reset the callbackcheck and click outside the mask and outside in the image.
  106. callbackCheck.pointerDown = false;
  107. m_FakeBaseInput.MousePosition = new Vector2(screenMiddle.x - 100, screenMiddle.y);
  108. yield return null;
  109. m_FakeBaseInput.MouseButtonDown[0] = true;
  110. yield return null;
  111. m_FakeBaseInput.MouseButtonDown[0] = false;
  112. yield return null;
  113. m_FakeBaseInput.MouseButtonUp[0] = true;
  114. yield return null;
  115. m_FakeBaseInput.MouseButtonUp[0] = false;
  116. yield return null;
  117. Assert.IsFalse(callbackCheck.pointerDown);
  118. }
  119. [TearDown]
  120. public void TearDown()
  121. {
  122. GameObject.DestroyImmediate(m_EventSystem.gameObject);
  123. GameObject.DestroyImmediate(m_Canvas.gameObject);
  124. }
  125. }