77using UnityEngine . TestTools ;
88using Is = UnityEngine . TestTools . Constraints . Is ;
99using UnityEngineInternal . Input ;
10- using Touch = UnityEngine . InputSystem . EnhancedTouch . Touch ;
1110
1211
1312partial class CoreTests
1413{
14+ // resets MouseEvents script to default state
15+ public override void TearDown ( )
16+ {
17+ var mouse = InputSystem . AddDevice < Mouse > ( ) ;
18+ var pen = InputSystem . AddDevice < Pen > ( ) ;
19+ SetMouse ( mouse , Vector2 . zero , 0f ) ;
20+ SetPen ( pen , Vector2 . zero , 0f ) ;
21+ base . TearDown ( ) ;
22+ }
23+
24+ #region Mouse
1525 [ UnityTest ]
1626 [ Category ( "MouseEvents" ) ]
1727 public IEnumerator MouseEvents_CanReceiveOnMouseDown ( )
@@ -25,14 +35,122 @@ public IEnumerator MouseEvents_CanReceiveOnMouseDown()
2535 camera . transform . position = new Vector3 ( 0 , 0 , - 2f ) ;
2636 camera . tag = "MainCamera" ;
2737 var vec = Camera . main . WorldToScreenPoint ( gameObject . transform . position ) ;
28- SetMouse ( mouse , new Vector2 ( vec . x , vec . y ) ) ;
38+ SetMouse ( mouse , new Vector2 ( vec . x , vec . y ) , 1f ) ;
2939
3040 yield return null ;
3141
3242 Assert . That ( gameObject . transform . position , Is . EqualTo ( new Vector3 ( 0 , 0 , 1 ) ) , "No MouseDown event received." ) ;
3343 }
3444
35- unsafe void SetMouse ( Mouse mouse , Vector2 pos , float pressed = 1f )
45+ [ UnityTest ]
46+ [ Category ( "MouseEvents" ) ]
47+ public IEnumerator MouseEvents_CanReceiveOnMouseUp ( )
48+ {
49+ var mouse = InputSystem . AddDevice < Mouse > ( ) ;
50+
51+ var gameObject = GameObject . CreatePrimitive ( PrimitiveType . Cube ) ;
52+ gameObject . AddComponent < OnMouseEventsTest > ( ) ;
53+ gameObject . transform . position = Vector3 . zero ;
54+ var camera = new GameObject ( "MainCamera" ) . AddComponent < Camera > ( ) ;
55+ camera . transform . position = new Vector3 ( 0 , 0 , - 2f ) ;
56+ camera . tag = "MainCamera" ;
57+ var vec = Camera . main . WorldToScreenPoint ( gameObject . transform . position ) ;
58+ SetMouse ( mouse , new Vector2 ( vec . x , vec . y ) , 1f ) ;
59+ yield return null ;
60+ SetMouse ( mouse , new Vector2 ( vec . x , vec . y ) , 0f ) ;
61+ yield return null ;
62+ Assert . That ( gameObject . transform . position , Is . EqualTo ( new Vector3 ( 0 , 0 , 2 ) ) , "No MouseUp event received." ) ;
63+ }
64+
65+ [ UnityTest ]
66+ [ Category ( "MouseEvents" ) ]
67+ public IEnumerator MouseEvents_CanReceiveOnMouseUpAsButton ( )
68+ {
69+ var mouse = InputSystem . AddDevice < Mouse > ( ) ;
70+
71+ var gameObject = GameObject . CreatePrimitive ( PrimitiveType . Cube ) ;
72+ gameObject . AddComponent < OnMousEventTestTwo > ( ) ;
73+ gameObject . transform . position = Vector3 . zero ;
74+ var camera = new GameObject ( "MainCamera" ) . AddComponent < Camera > ( ) ;
75+ camera . transform . position = new Vector3 ( 0 , 0 , - 2f ) ;
76+ camera . tag = "MainCamera" ;
77+ var vec = Camera . main . WorldToScreenPoint ( gameObject . transform . position ) ;
78+ SetMouse ( mouse , new Vector2 ( vec . x , vec . y ) , 1f ) ;
79+ yield return null ;
80+ SetMouse ( mouse , new Vector2 ( vec . x , vec . y ) , 0f ) ;
81+ yield return null ;
82+ Assert . That ( gameObject . transform . position , Is . EqualTo ( new Vector3 ( 0 , 0 , 1 ) ) , "No MouseUpAsButton event received." ) ;
83+ }
84+
85+ [ UnityTest ]
86+ [ Category ( "MouseEvents" ) ]
87+ public IEnumerator MouseEvents_CanReceiveOnMouseDrag ( )
88+ {
89+ var mouse = InputSystem . AddDevice < Mouse > ( ) ;
90+
91+ var gameObject = GameObject . CreatePrimitive ( PrimitiveType . Cube ) ;
92+ gameObject . AddComponent < OnMouseEventsTest > ( ) ;
93+ gameObject . transform . position = Vector3 . zero ;
94+ var camera = new GameObject ( "MainCamera" ) . AddComponent < Camera > ( ) ;
95+ camera . transform . position = new Vector3 ( 0 , 0 , - 2f ) ;
96+ camera . tag = "MainCamera" ;
97+ var vec = Camera . main . WorldToScreenPoint ( gameObject . transform . position ) ;
98+ SetMouse ( mouse , new Vector2 ( vec . x , vec . y ) , 0f ) ;
99+ yield return null ;
100+ SetMouse ( mouse , new Vector2 ( vec . x , vec . y ) , 1f ) ;
101+ yield return null ;
102+ SetMouse ( mouse , new Vector2 ( vec . x , vec . y ) , 1f ) ;
103+ yield return null ;
104+ SetMouse ( mouse , new Vector2 ( vec . x + 1f , vec . y ) , 1f ) ;
105+ yield return null ;
106+ Assert . That ( gameObject . transform . position , Is . EqualTo ( new Vector3 ( 0 , 0 , 3 ) ) , "No MouseDrag event received." ) ;
107+ }
108+
109+ [ UnityTest ]
110+ [ Category ( "MouseEvents" ) ]
111+ public IEnumerator MouseEvents_CanReceiveOnMouseEnterAndMouseExit ( )
112+ {
113+ var mouse = InputSystem . AddDevice < Mouse > ( ) ;
114+
115+ var gameObject = GameObject . CreatePrimitive ( PrimitiveType . Cube ) ;
116+ gameObject . AddComponent < OnMouseEventsTest > ( ) ;
117+ gameObject . transform . position = Vector3 . zero ;
118+ var camera = new GameObject ( "MainCamera" ) . AddComponent < Camera > ( ) ;
119+ camera . transform . position = new Vector3 ( 0 , 0 , - 2f ) ;
120+ camera . tag = "MainCamera" ;
121+ var vec = Camera . main . WorldToScreenPoint ( gameObject . transform . position ) ;
122+ SetMouse ( mouse , new Vector2 ( 0 , 0 ) , 0f ) ;
123+ yield return null ;
124+ SetMouse ( mouse , new Vector2 ( vec . x , vec . y ) , 0f ) ;
125+ yield return null ;
126+ Assert . That ( gameObject . GetComponent < Renderer > ( ) . material . color , Is . EqualTo ( Color . green ) , "No MouseEnter event received." ) ;
127+
128+ SetMouse ( mouse , new Vector2 ( 0 , 0 ) , 0f ) ;
129+ yield return null ;
130+ Assert . That ( gameObject . GetComponent < Renderer > ( ) . material . color , Is . EqualTo ( Color . red ) , "No MouseExit event received." ) ;
131+ }
132+
133+ [ UnityTest ]
134+ [ Category ( "MouseEvents" ) ]
135+ public IEnumerator MouseEvents_CanReceiveOnMouseOver ( )
136+ {
137+ var mouse = InputSystem . AddDevice < Mouse > ( ) ;
138+
139+ var gameObject = GameObject . CreatePrimitive ( PrimitiveType . Cube ) ;
140+ gameObject . AddComponent < OnMousEventTestTwo > ( ) ;
141+ gameObject . transform . position = Vector3 . zero ;
142+ var camera = new GameObject ( "MainCamera" ) . AddComponent < Camera > ( ) ;
143+ camera . transform . position = new Vector3 ( 0 , 0 , - 2f ) ;
144+ camera . tag = "MainCamera" ;
145+ var vec = Camera . main . WorldToScreenPoint ( gameObject . transform . position ) ;
146+ SetMouse ( mouse , new Vector2 ( vec . x , vec . y ) , 0f ) ;
147+ yield return null ;
148+ Assert . That ( gameObject . GetComponent < Renderer > ( ) . material . color , Is . EqualTo ( Color . blue ) , "No MouseOver event received." ) ;
149+ }
150+
151+ // we need to use the NativeInputRuntime to queue the events in order to get the input events in native
152+ // code, where the mouse events are processed.
153+ unsafe void SetMouse ( Mouse mouse , Vector2 pos , float pressed )
36154 {
37155 using ( StateEvent . From ( mouse , out var eventPtr ) )
38156 {
@@ -42,12 +160,195 @@ unsafe void SetMouse(Mouse mouse, Vector2 pos, float pressed = 1f)
42160 NativeInputRuntime . instance . QueueEvent ( eventPtr ) ;
43161 }
44162 }
163+
164+ #endregion
165+
166+ #region Pen
167+
168+ [ UnityTest ]
169+ [ Category ( "MouseEvents" ) ]
170+ public IEnumerator PenEvents_CanReceiveOnMouseDown ( )
171+ {
172+ var pen = InputSystem . AddDevice < Pen > ( ) ;
173+
174+ var gameObject = GameObject . CreatePrimitive ( PrimitiveType . Cube ) ;
175+ gameObject . AddComponent < OnMouseEventsTest > ( ) ;
176+ gameObject . transform . position = Vector3 . zero ;
177+ var camera = new GameObject ( "MainCamera" ) . AddComponent < Camera > ( ) ;
178+ camera . transform . position = new Vector3 ( 0 , 0 , - 2f ) ;
179+ camera . tag = "MainCamera" ;
180+ var vec = Camera . main . WorldToScreenPoint ( gameObject . transform . position ) ;
181+ SetPen ( pen , new Vector2 ( vec . x , vec . y ) , 1f ) ;
182+
183+ yield return null ;
184+
185+ Assert . That ( gameObject . transform . position , Is . EqualTo ( new Vector3 ( 0 , 0 , 1 ) ) , "No MouseDown event received." ) ;
186+ }
187+
188+ [ UnityTest ]
189+ [ Category ( "MouseEvents" ) ]
190+ public IEnumerator PenEvents_CanReceiveOnMouseUp ( )
191+ {
192+ var pen = InputSystem . AddDevice < Pen > ( ) ;
193+
194+ var gameObject = GameObject . CreatePrimitive ( PrimitiveType . Cube ) ;
195+ gameObject . AddComponent < OnMouseEventsTest > ( ) ;
196+ gameObject . transform . position = Vector3 . zero ;
197+ var camera = new GameObject ( "MainCamera" ) . AddComponent < Camera > ( ) ;
198+ camera . transform . position = new Vector3 ( 0 , 0 , - 2f ) ;
199+ camera . tag = "MainCamera" ;
200+ var vec = Camera . main . WorldToScreenPoint ( gameObject . transform . position ) ;
201+ SetPen ( pen , new Vector2 ( vec . x , vec . y ) , 1f ) ;
202+ yield return null ;
203+ SetPen ( pen , new Vector2 ( vec . x , vec . y ) , 0f ) ;
204+ yield return null ;
205+ Assert . That ( gameObject . transform . position , Is . EqualTo ( new Vector3 ( 0 , 0 , 2 ) ) , "No MouseUp event received." ) ;
206+ }
207+
208+ [ UnityTest ]
209+ [ Category ( "MouseEvents" ) ]
210+ public IEnumerator PenEvents_CanReceiveOnMouseUpAsButton ( )
211+ {
212+ var pen = InputSystem . AddDevice < Pen > ( ) ;
213+
214+ var gameObject = GameObject . CreatePrimitive ( PrimitiveType . Cube ) ;
215+ gameObject . AddComponent < OnMousEventTestTwo > ( ) ;
216+ gameObject . transform . position = Vector3 . zero ;
217+ var camera = new GameObject ( "MainCamera" ) . AddComponent < Camera > ( ) ;
218+ camera . transform . position = new Vector3 ( 0 , 0 , - 2f ) ;
219+ camera . tag = "MainCamera" ;
220+ var vec = Camera . main . WorldToScreenPoint ( gameObject . transform . position ) ;
221+ SetPen ( pen , new Vector2 ( vec . x , vec . y ) , 1f ) ;
222+ yield return null ;
223+ SetPen ( pen , new Vector2 ( vec . x , vec . y ) , 0f ) ;
224+ yield return null ;
225+ Assert . That ( gameObject . transform . position , Is . EqualTo ( new Vector3 ( 0 , 0 , 1 ) ) , "No MouseUpAsButton event received." ) ;
226+ }
227+
228+ [ UnityTest ]
229+ [ Category ( "MouseEvents" ) ]
230+ public IEnumerator PenEvents_CanReceiveOnMouseDrag ( )
231+ {
232+ var pen = InputSystem . AddDevice < Pen > ( ) ;
233+
234+ var gameObject = GameObject . CreatePrimitive ( PrimitiveType . Cube ) ;
235+ gameObject . AddComponent < OnMousEventTestTwo > ( ) ;
236+ gameObject . transform . position = Vector3 . zero ;
237+ var camera = new GameObject ( "MainCamera" ) . AddComponent < Camera > ( ) ;
238+ camera . transform . position = new Vector3 ( 0 , 0 , - 2f ) ;
239+ camera . tag = "MainCamera" ;
240+ var vec = Camera . main . WorldToScreenPoint ( gameObject . transform . position ) ;
241+ SetPen ( pen , new Vector2 ( vec . x , vec . y ) , 0f ) ;
242+ yield return null ;
243+ SetPen ( pen , new Vector2 ( vec . x , vec . y ) , 1f ) ;
244+ yield return null ;
245+ SetPen ( pen , new Vector2 ( vec . x , vec . y ) , 1f ) ;
246+ yield return null ;
247+ SetPen ( pen , new Vector2 ( vec . x + 1f , vec . y ) , 1f ) ;
248+ yield return null ;
249+ Assert . That ( gameObject . transform . position , Is . EqualTo ( new Vector3 ( 0 , 0 , 3 ) ) , "No MouseDrag event received." ) ;
250+ }
251+
252+ [ UnityTest ]
253+ [ Category ( "MouseEvents" ) ]
254+ public IEnumerator PenEvents_CanReceiveOnMouseEnterAndMouseExit ( )
255+ {
256+ var pen = InputSystem . AddDevice < Pen > ( ) ;
257+
258+ var gameObject = GameObject . CreatePrimitive ( PrimitiveType . Cube ) ;
259+ gameObject . AddComponent < OnMouseEventsTest > ( ) ;
260+ gameObject . transform . position = Vector3 . zero ;
261+ var camera = new GameObject ( "MainCamera" ) . AddComponent < Camera > ( ) ;
262+ camera . transform . position = new Vector3 ( 0 , 0 , - 2f ) ;
263+ camera . tag = "MainCamera" ;
264+ var vec = Camera . main . WorldToScreenPoint ( gameObject . transform . position ) ;
265+ SetPen ( pen , new Vector2 ( 0 , 0 ) , 0f ) ;
266+ yield return null ;
267+ SetPen ( pen , new Vector2 ( vec . x , vec . y ) , 0f ) ;
268+ yield return null ;
269+ Assert . That ( gameObject . GetComponent < Renderer > ( ) . material . color , Is . EqualTo ( Color . green ) , "No MouseEnter event received." ) ;
270+
271+ SetPen ( pen , new Vector2 ( 0 , 0 ) , 0f ) ;
272+ yield return null ;
273+ Assert . That ( gameObject . GetComponent < Renderer > ( ) . material . color , Is . EqualTo ( Color . red ) , "No MouseExit event received." ) ;
274+ }
275+
276+ [ UnityTest ]
277+ [ Category ( "MouseEvents" ) ]
278+ public IEnumerator PenEvents_CanReceiveOnMouseOver ( )
279+ {
280+ var pen = InputSystem . AddDevice < Pen > ( ) ;
281+
282+ var gameObject = GameObject . CreatePrimitive ( PrimitiveType . Cube ) ;
283+ gameObject . AddComponent < OnMousEventTestTwo > ( ) ;
284+ gameObject . transform . position = Vector3 . zero ;
285+ var camera = new GameObject ( "MainCamera" ) . AddComponent < Camera > ( ) ;
286+ camera . transform . position = new Vector3 ( 0 , 0 , - 2f ) ;
287+ camera . tag = "MainCamera" ;
288+ var vec = Camera . main . WorldToScreenPoint ( gameObject . transform . position ) ;
289+ SetPen ( pen , new Vector2 ( vec . x , vec . y ) , 0f ) ;
290+ yield return null ;
291+ Assert . That ( gameObject . GetComponent < Renderer > ( ) . material . color , Is . EqualTo ( Color . blue ) , "No MouseOver event received." ) ;
292+ }
293+
294+ // we need to use the NativeInputRuntime to queue the events in order to get the input events in native
295+ // code, where the pen events are processed.
296+ unsafe void SetPen ( Pen pen , Vector2 pos , float pressed )
297+ {
298+ using ( StateEvent . From ( pen , out var eventPtr ) )
299+ {
300+ eventPtr . time = InputState . currentTime ;
301+ pen . position . WriteValueIntoEvent ( pos , eventPtr ) ;
302+ pen . tip . WriteValueIntoEvent ( pressed , eventPtr ) ;
303+ NativeInputRuntime . instance . QueueEvent ( eventPtr ) ;
304+ }
305+ }
306+
307+ #endregion
308+ }
309+
310+ internal class OnMouseEventsTest : MonoBehaviour
311+ {
312+ private void OnMouseDown ( )
313+ {
314+ gameObject . transform . position = new Vector3 ( 0 , 0 , 1 ) ;
315+ }
316+
317+ private void OnMouseUp ( )
318+ {
319+ gameObject . transform . position = new Vector3 ( 0 , 0 , 2 ) ;
320+ }
321+
322+ private void OnMouseDrag ( )
323+ {
324+ gameObject . transform . position = new Vector3 ( 0 , 0 , 3 ) ;
325+ }
326+
327+ private void OnMouseEnter ( )
328+ {
329+ gameObject . GetComponent < Renderer > ( ) . material . color = Color . green ;
330+ }
331+
332+ private void OnMouseExit ( )
333+ {
334+ gameObject . GetComponent < Renderer > ( ) . material . color = Color . red ;
335+ }
45336}
46337
47- public class OnMouseEventsTest : MonoBehaviour
338+ internal class OnMousEventTestTwo : MonoBehaviour
48339{
49- void OnMouseDown ( )
340+ private void OnMouseUpAsButton ( )
50341 {
51342 gameObject . transform . position = new Vector3 ( 0 , 0 , 1 ) ;
52343 }
344+
345+ private void OnMouseOver ( )
346+ {
347+ gameObject . GetComponent < Renderer > ( ) . material . color = Color . blue ;
348+ }
349+
350+ private void OnMouseDrag ( )
351+ {
352+ gameObject . transform . position = new Vector3 ( 0 , 0 , 3 ) ;
353+ }
53354}
0 commit comments