Skip to content

Commit 0047e16

Browse files
committed
test: Fix test input cases.
1 parent c40f0ce commit 0047e16

1 file changed

Lines changed: 31 additions & 15 deletions

File tree

Assets/Tests/InputSystem/Plugins/DeviceSimulatorTests.cs

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using UnityEngine.InputSystem;
99
using UnityEngine.InputSystem.Editor;
1010
using UnityEngine.InputSystem.EnhancedTouch;
11+
using UnityEngine.InputSystem.Layouts;
1112
using UnityEngine.TestTools;
1213
using Touch = UnityEngine.InputSystem.EnhancedTouch.Touch;
1314
using TouchPhase = UnityEngine.InputSystem.TouchPhase;
@@ -68,9 +69,7 @@ public void TouchscreenAddedAndRemoved()
6869
[Category("Device Simulator")]
6970
public void ConflictingDevicesAreNotDisabledOnCreate()
7071
{
71-
runtime.ReportNewInputDevice<Mouse>();
72-
InputSystem.Update();
73-
var mouse = Mouse.current;
72+
var mouse = AddNativeMouse();
7473
Assert.That(mouse.native, Is.True);
7574

7675
var plugin = new InputSystemPlugin();
@@ -89,12 +88,10 @@ public void ConflictingDeviceAddedWhileSimulatorFocused_IsDisabledThenReenabledO
8988
var plugin = new InputSystemPlugin();
9089
plugin.OnCreate();
9190

92-
// Simulate the Simulator window being the focused window (bypasses the panel-based OnUpdate).
93-
SetPrivateField(plugin, "m_ConflictingDevicesDisabled", true);
91+
// Simulate the Simulator window being focused (bypasses the panel-based OnUpdate).
92+
plugin.SetConflictingDevicesDisabled(true);
9493

95-
runtime.ReportNewInputDevice<Mouse>();
96-
InputSystem.Update();
97-
var mouse = Mouse.current;
94+
var mouse = AddNativeMouse();
9895

9996
Assert.That(mouse.native, Is.True);
10097
Assert.That(mouse.enabled, Is.False); // disabled via the OnDeviceChange gate
@@ -103,6 +100,24 @@ public void ConflictingDeviceAddedWhileSimulatorFocused_IsDisabledThenReenabledO
103100
Assert.That(mouse.enabled, Is.True); // ReenableConflictingDevices restores it
104101
}
105102

103+
[Test]
104+
[Category("Device Simulator")]
105+
public void ConflictingDevicesReenabledWhenSimulatorLosesFocus()
106+
{
107+
var mouse = AddNativeMouse();
108+
109+
var plugin = new InputSystemPlugin();
110+
plugin.OnCreate();
111+
112+
plugin.SetConflictingDevicesDisabled(true); // Simulator gained focus
113+
Assert.That(mouse.enabled, Is.False);
114+
115+
plugin.SetConflictingDevicesDisabled(false); // Simulator lost focus
116+
Assert.That(mouse.enabled, Is.True);
117+
118+
plugin.OnDestroy();
119+
}
120+
106121
[Test]
107122
[Category("Device Simulator")]
108123
public void ConflictingDeviceAddedWhileSimulatorNotFocused_StaysEnabled()
@@ -111,20 +126,21 @@ public void ConflictingDeviceAddedWhileSimulatorNotFocused_StaysEnabled()
111126
plugin.OnCreate();
112127
// m_ConflictingDevicesDisabled defaults to false (Simulator not focused).
113128

114-
runtime.ReportNewInputDevice<Mouse>();
115-
InputSystem.Update();
116-
var mouse = Mouse.current;
129+
var mouse = AddNativeMouse();
117130

118131
Assert.That(mouse.enabled, Is.True);
119132

120133
plugin.OnDestroy();
121134
}
122135

123-
private static void SetPrivateField(object target, string name, object value)
136+
// Reports a native Mouse through the test runtime (device.native == true, which the plugin's
137+
// disable logic requires) and returns the resolved device rather than relying on Mouse.current.
138+
private Mouse AddNativeMouse()
124139
{
125-
var field = target.GetType().GetField(name, BindingFlags.Instance | BindingFlags.NonPublic);
126-
Assert.NotNull(field, $"Field '{name}' not found on {target.GetType().Name}");
127-
field.SetValue(target, value);
140+
var deviceId = runtime.ReportNewInputDevice(
141+
new InputDeviceDescription { deviceClass = "Mouse", interfaceName = "Test" });
142+
InputSystem.Update();
143+
return (Mouse)InputSystem.GetDeviceById(deviceId);
128144
}
129145

130146
private TouchEvent CreateTouch(int touchId, Vector2 position, UnityEditor.DeviceSimulation.TouchPhase phase)

0 commit comments

Comments
 (0)