UITestHelpers.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // This code snippet was provided originally by stanislav.osipov@unity3d.com from #ui-elements slack channel.
  2. using System;
  3. using System.Collections;
  4. using Unity.Cloud.Collaborate.Assets;
  5. using UnityEditor;
  6. using UnityEngine;
  7. using UnityEngine.UIElements;
  8. namespace Unity.Cloud.Collaborate.Tests
  9. {
  10. internal static class UiTestHelpers
  11. {
  12. #region Events​
  13. // In order for tests to run without an EditorWindow but still be able to send
  14. // events, we sometimes need to force the event type. IMGUI::GetEventType() (native) will
  15. // return the event type as Ignore if the proper views haven't yet been
  16. // initialized. This (falsely) breaks tests that rely on the event type. So for tests, we
  17. // just ensure the event type is what we originally set it to when we sent it.
  18. // This original type can be retrieved via Event.rawType.
  19. static Event CreateEvent(Event evt)
  20. {
  21. return evt; //UIElementsUtility.CreateEvent(evt, evt.rawType);
  22. }
  23. public static Event MakeEvent(EventType type)
  24. {
  25. var evt = new Event { type = type };
  26. return CreateEvent(evt);
  27. }
  28. public static Event MakeEvent(EventType type, Vector2 position)
  29. {
  30. var evt = new Event { type = type, mousePosition = position };
  31. return CreateEvent(evt);
  32. }
  33. public static Event MakeKeyEvent(KeyCode code, EventType type = EventType.KeyDown, EventModifiers modifiers = EventModifiers.None, char character = '\0')
  34. {
  35. var evt = new Event { type = type, keyCode = code, character = character, modifiers = modifiers };
  36. return CreateEvent(evt);
  37. }
  38. public static Event MakeMouseEvent(EventType type, Vector2 position, MouseButton button = MouseButton.LeftMouse, EventModifiers modifiers = EventModifiers.None, int clickCount = 1)
  39. {
  40. var evt = new Event { type = type, mousePosition = position, button = (int)button, modifiers = modifiers, clickCount = clickCount };
  41. return CreateEvent(evt);
  42. }
  43. public static Event MakeScrollWheelEvent(Vector2 delta, Vector2 position)
  44. {
  45. var evt = new Event
  46. {
  47. type = EventType.ScrollWheel,
  48. delta = delta,
  49. mousePosition = position
  50. };
  51. return CreateEvent(evt);
  52. }
  53. public static Event MakeCommandEvent(EventType type, string command)
  54. {
  55. var evt = new Event { type = type, commandName = command };
  56. return CreateEvent(evt);
  57. }
  58. #endregion
  59. #region EditorWindow API
  60. public static bool IsCompletelyVisible(EditorWindow window, VisualElement element)
  61. {
  62. if (element.ClassListContains(UiConstants.ussHidden))
  63. {
  64. return false;
  65. }
  66. var windowBounds = window.rootVisualElement.worldBound;
  67. var elementBounds = element.worldBound;
  68. return elementBounds.x >= windowBounds.x
  69. && elementBounds.y >= windowBounds.y
  70. && windowBounds.x + windowBounds.width >= elementBounds.x + elementBounds.width
  71. && windowBounds.y + windowBounds.height >= elementBounds.y + elementBounds.height;
  72. }
  73. public static bool IsPartiallyVisible(EditorWindow window, VisualElement element)
  74. {
  75. if (element.ClassListContains(UiConstants.ussHidden))
  76. {
  77. return false;
  78. }
  79. var windowBounds = window.rootVisualElement.worldBound;
  80. var elementBounds = element.worldBound;
  81. return !(elementBounds.x > windowBounds.x + windowBounds.width)
  82. && !(elementBounds.x + elementBounds.width < windowBounds.x)
  83. && !(elementBounds.y > windowBounds.y + windowBounds.height)
  84. && !(elementBounds.y + elementBounds.height < windowBounds.y);
  85. }
  86. public static void SendMouseDownEvent(EditorWindow window, VisualElement element)
  87. {
  88. var evt = MakeMouseEvent(EventType.MouseDown, element.worldBound.center);
  89. window.SendEvent(evt);
  90. }
  91. public static void SendClickEvent(EditorWindow window, VisualElement element)
  92. {
  93. var evt = MakeMouseEvent(EventType.MouseDown, element.worldBound.center);
  94. window.SendEvent(evt);
  95. evt = MakeMouseEvent(EventType.MouseUp, element.worldBound.center);
  96. window.SendEvent(evt);
  97. }
  98. public static void SimulateTyping(EditorWindow window, string text)
  99. {
  100. foreach (var character in text)
  101. {
  102. var evt = MakeKeyEvent(KeyCode.None, EventType.KeyDown, EventModifiers.None, character);
  103. window.SendEvent(evt);
  104. }
  105. }
  106. public static void SimulateTyping(EditorWindow window, char character, int repetitions)
  107. {
  108. for (var i = 0; i < repetitions; i++)
  109. {
  110. var evt = MakeKeyEvent(KeyCode.None, EventType.KeyDown, EventModifiers.None, character);
  111. window.SendEvent(evt);
  112. }
  113. }
  114. public static IEnumerator Pause(int frames)
  115. {
  116. for (var i = 0; i < frames; i++)
  117. {
  118. yield return null;
  119. }
  120. }
  121. #endregion
  122. }
  123. }