|
| 1 | +#if UNITY_2023_2_OR_NEWER // UnityEngine.InputForUI Module unavailable in earlier releases |
| 2 | +using System.Collections.Generic; |
| 3 | +using NUnit.Framework; |
| 4 | +using UnityEngine; |
| 5 | +using UnityEngine.InputForUI; |
| 6 | +using UnityEngine.InputSystem; |
| 7 | +using UnityEngine.InputSystem.Plugins.InputForUI; |
| 8 | +using Event = UnityEngine.InputForUI.Event; |
| 9 | +using EventProvider = UnityEngine.InputForUI.EventProvider; |
| 10 | + |
| 11 | +// These tests are not meant to test the InputForUI module itself, but rather the integration between the InputForUI |
| 12 | +// module and the InputSystem package. |
| 13 | +// Be aware that these tests don't account for events dispatched by the InputEventPartialProvider. Those events are |
| 14 | +// already tested in the Input Manager provider. |
| 15 | +// Also, the internals to test InputEventPartialProvider are not exposed publicly, so we can't test them here. |
| 16 | +public class InputForUITests : InputTestFixture |
| 17 | +{ |
| 18 | + readonly List<Event> m_InputForUIEvents = new List<Event>(); |
| 19 | + InputSystemProvider m_InputSystemProvider; |
| 20 | + |
| 21 | + [SetUp] |
| 22 | + public void SetUp() |
| 23 | + { |
| 24 | + base.Setup(); |
| 25 | + |
| 26 | + var defaultActions = new DefaultInputActions(); |
| 27 | + defaultActions.Enable(); |
| 28 | + |
| 29 | + m_InputSystemProvider = new InputSystemProvider(); |
| 30 | + EventProvider.SetMockProvider(m_InputSystemProvider); |
| 31 | + // Register at least one consumer so the mock update gets invoked |
| 32 | + EventProvider.Subscribe(InputForUIOnEvent); |
| 33 | + } |
| 34 | + |
| 35 | + [TearDown] |
| 36 | + public void TearDown() |
| 37 | + { |
| 38 | + EventProvider.Unsubscribe(InputForUIOnEvent); |
| 39 | + EventProvider.ClearMockProvider(); |
| 40 | + m_InputForUIEvents.Clear(); |
| 41 | + } |
| 42 | + |
| 43 | + private bool InputForUIOnEvent(in Event ev) |
| 44 | + { |
| 45 | + m_InputForUIEvents.Add(ev); |
| 46 | + return true; |
| 47 | + } |
| 48 | + |
| 49 | + [Test] |
| 50 | + [Category("InputForUI")] |
| 51 | + public void PointerEventsAreDispatchedFromMouse() |
| 52 | + { |
| 53 | + var mouse = InputSystem.AddDevice<Mouse>(); |
| 54 | + Update(); |
| 55 | + |
| 56 | + PressAndRelease(mouse.leftButton); |
| 57 | + |
| 58 | + Update(); |
| 59 | + |
| 60 | + Assert.IsTrue(m_InputForUIEvents.Count == 2); |
| 61 | + Assert.That(m_InputForUIEvents[0].type, Is.EqualTo(Event.Type.PointerEvent)); |
| 62 | + Assert.That(m_InputForUIEvents[0].asPointerEvent.type, Is.EqualTo(PointerEvent.Type.ButtonPressed)); |
| 63 | + Assert.That(m_InputForUIEvents[1].type, Is.EqualTo(Event.Type.PointerEvent)); |
| 64 | + Assert.That(m_InputForUIEvents[1].asPointerEvent.type, Is.EqualTo(PointerEvent.Type.ButtonReleased)); |
| 65 | + } |
| 66 | + |
| 67 | + [Test] |
| 68 | + [Category("InputForUI")] |
| 69 | + // Checks that mouse events are ignored when a touch is active. |
| 70 | + // This is to workaround the issue ISXB-269 on Windows. |
| 71 | + public void TouchIsPressedAndMouseEventsAreIgnored() |
| 72 | + { |
| 73 | + var touch = InputSystem.AddDevice<Touchscreen>(); |
| 74 | + var mouse = InputSystem.AddDevice<Mouse>(); |
| 75 | + // Set initial mouse position to (0.5, 0.5) so that we get a delta when the mouse is moved, to dispatch |
| 76 | + // a pointer move event |
| 77 | + Set(mouse.position, new Vector2(0.5f, 0.5f)); |
| 78 | + Update(); |
| 79 | + |
| 80 | + // Start touch and move mouse to the same position to replicate the issue of duplicated Mouse events for |
| 81 | + // Touch events on Windows. |
| 82 | + BeginTouch(1, new Vector2(100f, 0.5f)); |
| 83 | + Move(mouse.position, new Vector2(100f, 0.5f)); |
| 84 | + Update(); |
| 85 | + |
| 86 | + Assert.IsTrue(m_InputForUIEvents.Count == 1); |
| 87 | + Assert.That(m_InputForUIEvents[0] is Event |
| 88 | + { |
| 89 | + type: Event.Type.PointerEvent, |
| 90 | + asPointerEvent: { type: PointerEvent.Type.ButtonPressed, |
| 91 | + eventSource: EventSource.Touch } |
| 92 | + }); |
| 93 | + } |
| 94 | + |
| 95 | + [Test] |
| 96 | + [Category("InputForUI")] |
| 97 | + // Presses a gamepad left stick left and verifies that a navigation move event is dispatched |
| 98 | + public void NavigationMoveWorks() |
| 99 | + { |
| 100 | + var gamepad = InputSystem.AddDevice<Gamepad>(); |
| 101 | + Update(); |
| 102 | + Press(gamepad.leftStick.left); |
| 103 | + Update(); |
| 104 | + Release(gamepad.leftStick.left); |
| 105 | + Update(); |
| 106 | + |
| 107 | + Assert.IsTrue(m_InputForUIEvents.Count == 1); |
| 108 | + Assert.That(m_InputForUIEvents[0] is Event |
| 109 | + { |
| 110 | + type: Event.Type.NavigationEvent, |
| 111 | + asNavigationEvent: { type: NavigationEvent.Type.Move, |
| 112 | + direction: NavigationEvent.Direction.Left, |
| 113 | + eventSource: EventSource.Gamepad} |
| 114 | + }); |
| 115 | + } |
| 116 | + |
| 117 | + [Test] |
| 118 | + [Category("InputForUI")] |
| 119 | + public void SendWheelEvent() |
| 120 | + { |
| 121 | + var kScrollUGUIScaleFactor = 3.0f; // See InputSystemProvider OnScrollWheelPerformed() callback |
| 122 | + var mouse = InputSystem.AddDevice<Mouse>(); |
| 123 | + Update(); |
| 124 | + // Make the minimum step of scroll delta to be ±1.0f |
| 125 | + Set(mouse.scroll.y, -1f / kScrollUGUIScaleFactor); |
| 126 | + Update(); |
| 127 | + Assert.IsTrue(m_InputForUIEvents.Count == 1); |
| 128 | + Assert.That(m_InputForUIEvents[0].asPointerEvent.scroll, Is.EqualTo(new Vector2(0, 1))); |
| 129 | + } |
| 130 | + |
| 131 | + static void Update() |
| 132 | + { |
| 133 | + EventProvider.NotifyUpdate(); |
| 134 | + InputSystem.Update(); |
| 135 | + } |
| 136 | +} |
| 137 | +#endif |
0 commit comments