Skip to content

Commit 458c308

Browse files
CHANGE: Add automated testing setup for InputForUI (#1680)
* Expose InputSystemProvider internals to InputForUI tests * Add basic setup to test the InputForUI EventProvider for 2023.2+. Depends on Unity 2023.2+ * Add more tests for scroll, navigation and touch events
1 parent c504cf5 commit 458c308

4 files changed

Lines changed: 142 additions & 1 deletion

File tree

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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 = 40f; // See InputSystemProvider OnScrollWheelPerformed() callback
122+
var mouse = InputSystem.AddDevice<Mouse>();
123+
Update();
124+
Set(mouse.scroll.y, -1f * kScrollUGUIScaleFactor);
125+
Update();
126+
Assert.IsTrue(m_InputForUIEvents.Count == 1);
127+
Assert.That(m_InputForUIEvents[0].asPointerEvent.scroll, Is.EqualTo(new Vector2(0, 1)));
128+
}
129+
130+
static void Update()
131+
{
132+
EventProvider.NotifyUpdate();
133+
InputSystem.Update();
134+
}
135+
}
136+
#endif

Assets/Tests/InputSystem/Plugins/InputForUITests.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Tests/InputSystem/Unity.InputSystem.Tests.asmdef

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"UnityEngine.TestRunner",
1010
"UnityEditor.TestRunner",
1111
"Unity.PerformanceTesting",
12-
"Unity.Coding.Editor"
12+
"Unity.Coding.Editor",
13+
"Unity.InputSystem.ForUI"
1314
],
1415
"includePlatforms": [],
1516
"excludePlatforms": [],

Packages/com.unity.inputsystem/InputSystem/Plugins/InputForUI/AssemblyInfo.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
using UnityEngine.Scripting;
33

44
[assembly: InternalsVisibleTo("UnityEngine.InputForUIVisualizer")]
5+
[assembly: InternalsVisibleTo("Unity.InputSystem.Tests")]
56
[assembly: AlwaysLinkAssembly]

0 commit comments

Comments
 (0)