-
Notifications
You must be signed in to change notification settings - Fork 339
Expand file tree
/
Copy pathDeviceSimulatorTests.cs
More file actions
153 lines (123 loc) · 5.45 KB
/
Copy pathDeviceSimulatorTests.cs
File metadata and controls
153 lines (123 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#if UNITY_EDITOR
using System.Collections;
using System.Reflection;
using NUnit.Framework;
using UnityEditor.DeviceSimulation;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Editor;
using UnityEngine.InputSystem.EnhancedTouch;
using UnityEngine.TestTools;
using Touch = UnityEngine.InputSystem.EnhancedTouch.Touch;
using TouchPhase = UnityEngine.InputSystem.TouchPhase;
public class DeviceSimulatorTests : InputTestFixture
{
[UnityTest]
[Category("Device Simulator")]
public IEnumerator InputEventsArePropagated()
{
EnhancedTouchSupport.Enable();
var plugin = new InputSystemPlugin();
plugin.OnCreate();
yield return null;
plugin.OnTouchEvent(CreateTouch(0, new Vector2(5, 5), UnityEditor.DeviceSimulation.TouchPhase.Began));
yield return null;
var activeTouches = Touch.activeTouches;
Assert.Greater(activeTouches.Count, 0);
Assert.AreEqual(new Vector2(5, 5), Touch.activeTouches[0].screenPosition);
Assert.AreEqual(TouchPhase.Began, Touch.activeTouches[0].phase);
yield return null;
Assert.AreEqual(new Vector2(5, 5), Touch.activeTouches[0].screenPosition);
Assert.AreEqual(TouchPhase.Stationary, Touch.activeTouches[0].phase);
plugin.OnTouchEvent(CreateTouch(0, new Vector2(10, 10), UnityEditor.DeviceSimulation.TouchPhase.Moved));
yield return null;
Assert.AreEqual(new Vector2(10, 10), Touch.activeTouches[0].screenPosition);
Assert.AreEqual(TouchPhase.Moved, Touch.activeTouches[0].phase);
plugin.OnTouchEvent(CreateTouch(0, new Vector2(5, 5), UnityEditor.DeviceSimulation.TouchPhase.Ended));
yield return null;
Assert.AreEqual(new Vector2(5, 5), Touch.activeTouches[0].screenPosition);
Assert.AreEqual(TouchPhase.Ended, Touch.activeTouches[0].phase);
yield return null;
Assert.AreEqual(Touch.activeTouches.Count, 0);
plugin.OnDestroy();
EnhancedTouchSupport.Disable();
}
[Test]
[Category("Device Simulator")]
public void TouchscreenAddedAndRemoved()
{
var plugin = new InputSystemPlugin();
plugin.OnCreate();
var touchscreen = plugin.SimulatorTouchscreen;
Assert.IsTrue(touchscreen.added);
plugin.OnDestroy();
Assert.IsFalse(touchscreen.added);
}
[Test]
[Category("Device Simulator")]
public void ConflictingDevicesAreNotDisabledOnCreate()
{
runtime.ReportNewInputDevice<Mouse>();
InputSystem.Update();
var mouse = Mouse.current;
Assert.That(mouse.native, Is.True);
var plugin = new InputSystemPlugin();
plugin.OnCreate();
// Conflicting devices are only disabled once the Simulator gains focus, not on create.
Assert.That(mouse.enabled, Is.True);
plugin.OnDestroy();
}
[Test]
[Category("Device Simulator")]
public void ConflictingDeviceAddedWhileSimulatorFocused_IsDisabledThenReenabledOnDestroy()
{
var plugin = new InputSystemPlugin();
plugin.OnCreate();
// Simulate the Simulator window being the focused window (bypasses the panel-based OnUpdate).
SetPrivateField(plugin, "m_ConflictingDevicesDisabled", true);
runtime.ReportNewInputDevice<Mouse>();
InputSystem.Update();
var mouse = Mouse.current;
Assert.That(mouse.native, Is.True);
Assert.That(mouse.enabled, Is.False); // disabled via the OnDeviceChange gate
plugin.OnDestroy();
Assert.That(mouse.enabled, Is.True); // ReenableConflictingDevices restores it
}
[Test]
[Category("Device Simulator")]
public void ConflictingDeviceAddedWhileSimulatorNotFocused_StaysEnabled()
{
var plugin = new InputSystemPlugin();
plugin.OnCreate();
// m_ConflictingDevicesDisabled defaults to false (Simulator not focused).
runtime.ReportNewInputDevice<Mouse>();
InputSystem.Update();
var mouse = Mouse.current;
Assert.That(mouse.enabled, Is.True);
plugin.OnDestroy();
}
private static void SetPrivateField(object target, string name, object value)
{
var field = target.GetType().GetField(name, BindingFlags.Instance | BindingFlags.NonPublic);
Assert.NotNull(field, $"Field '{name}' not found on {target.GetType().Name}");
field.SetValue(target, value);
}
private TouchEvent CreateTouch(int touchId, Vector2 position, UnityEditor.DeviceSimulation.TouchPhase phase)
{
var touch = new TouchEvent();
var type = typeof(TouchEvent);
object touchObject = touch;
var touchIdAutoBackingField = type.GetField($"<{nameof(TouchEvent.touchId)}>k__BackingField", BindingFlags.Instance | BindingFlags.NonPublic);
var positionAutoBackingField = type.GetField($"<{nameof(TouchEvent.position)}>k__BackingField", BindingFlags.Instance | BindingFlags.NonPublic);
var phaseAutoBackingField = type.GetField($"<{nameof(TouchEvent.phase)}>k__BackingField", BindingFlags.Instance | BindingFlags.NonPublic);
Assert.NotNull(touchIdAutoBackingField);
Assert.NotNull(positionAutoBackingField);
Assert.NotNull(phaseAutoBackingField);
touchIdAutoBackingField.SetValue(touchObject, touchId);
positionAutoBackingField.SetValue(touchObject, position);
phaseAutoBackingField.SetValue(touchObject, phase);
touch = (TouchEvent)touchObject;
return touch;
}
}
#endif