1919using UnityEngine . Scripting ;
2020using UnityEngine . TestTools ;
2121using UnityEngine . TestTools . Utils ;
22+ using UnityEngineInternal . Input ;
2223using Gyroscope = UnityEngine . InputSystem . Gyroscope ;
2324using UnityEngine . TestTools . Constraints ;
2425using Is = NUnit . Framework . Is ;
@@ -1522,7 +1523,7 @@ public void Devices_CanReconnectDevice_WhenDisconnectedWhileAppIsOutOfFocus()
15221523 Assert . That ( device , Is . Not . Null ) ;
15231524
15241525 // Loose focus.
1525- runtime . PlayerFocusLost ( ) ;
1526+ ScheduleFocusChangedEvent ( applicationHasFocus : false ) ;
15261527 InputSystem . Update ( ) ;
15271528
15281529 // Disconnect.
@@ -1534,7 +1535,7 @@ public void Devices_CanReconnectDevice_WhenDisconnectedWhileAppIsOutOfFocus()
15341535 Assert . That ( InputSystem . devices , Is . Empty ) ;
15351536
15361537 // Regain focus.
1537- runtime . PlayerFocusGained ( ) ;
1538+ ScheduleFocusChangedEvent ( applicationHasFocus : true ) ;
15381539 InputSystem . Update ( ) ;
15391540
15401541 var newDeviceId = runtime . ReportNewInputDevice ( deviceDesc ) ;
@@ -4604,7 +4605,13 @@ void DeviceChangeCallback(InputDevice device, InputDeviceChange change)
46044605 InputSystem . onDeviceChange += DeviceChangeCallback ;
46054606
46064607 var eventCount = 0 ;
4607- InputSystem . onEvent += ( eventPtr , _ ) => ++ eventCount ;
4608+ InputSystem . onEvent += ( eventPtr , _ ) =>
4609+ {
4610+ // Focus events will always be processed no matter the state
4611+ // Since the test relies on counting events based on state, dont count focus events
4612+ if ( eventPtr . data ->type != ( FourCC ) FocusConstants . kEventType )
4613+ ++ eventCount ;
4614+ } ;
46084615
46094616 Assert . That ( trackedDevice . enabled , Is . True ) ;
46104617 Assert . That ( mouse . enabled , Is . True ) ;
@@ -4647,7 +4654,8 @@ void DeviceChangeCallback(InputDevice device, InputDeviceChange change)
46474654 }
46484655
46494656 // Lose focus.
4650- runtime . PlayerFocusLost ( ) ;
4657+ ScheduleFocusChangedEvent ( applicationHasFocus : false ) ;
4658+ InputSystem . Update ( InputUpdateType . Dynamic ) ;
46514659
46524660 Assert . That ( sensor . enabled , Is . False ) ;
46534661 Assert . That ( disabledDevice . enabled , Is . False ) ;
@@ -5068,7 +5076,8 @@ void DeviceChangeCallback(InputDevice device, InputDeviceChange change)
50685076 commands . Clear ( ) ;
50695077
50705078 // Regain focus.
5071- runtime . PlayerFocusGained ( ) ;
5079+ ScheduleFocusChangedEvent ( applicationHasFocus : true ) ;
5080+ InputSystem . Update ( InputUpdateType . Dynamic ) ;
50725081
50735082 Assert . That ( sensor . enabled , Is . False ) ;
50745083 Assert . That ( disabledDevice . enabled , Is . False ) ;
@@ -5275,13 +5284,10 @@ void DeviceChangeCallback(InputDevice device, InputDeviceChange change)
52755284 "Sync Gamepad" , "Sync Joystick" ,
52765285 "Sync TrackedDevice" , "Sync TrackedDevice2" ,
52775286 "Sync Mouse" , "Sync Mouse2" , "Sync Mouse3" ,
5278- "Sync Keyboard" , "Reset Joystick"
5279- } ) ) ;
5280- // Enabled devices that don't support syncs get reset.
5281- Assert . That ( changes , Is . EquivalentTo ( new [ ]
5282- {
5283- "SoftReset Mouse1" , "SoftReset Mouse3" , "HardReset Joystick" , "SoftReset TrackedDevice2"
5287+ "Sync Keyboard"
52845288 } ) ) ;
5289+ // Enabled devices that don't support syncs dont get reset for Ignore Focus as we do not want to cancel any actions.
5290+ Assert . That ( changes , Is . Empty ) ;
52855291 break ;
52865292 }
52875293 }
@@ -5318,7 +5324,13 @@ public void Devices_CanSkipProcessingEventsWhileInBackground()
53185324 Assert . That ( performedCount , Is . EqualTo ( 1 ) ) ;
53195325
53205326 // Lose focus
5321- runtime . PlayerFocusLost ( ) ;
5327+ ScheduleFocusChangedEvent ( applicationHasFocus : false ) ;
5328+ #if UNITY_INPUTSYSTEM_SUPPORTS_FOCUS_EVENTS
5329+ // in the new system, we have to process the focus event to update the state of the devices.
5330+ // In the old system, this wouldn't work and would make the test fal
5331+ InputSystem . Update ( ) ;
5332+ #endif
5333+
53225334 Assert . That ( gamepad . enabled , Is . False ) ;
53235335
53245336 // Queue an event while in the background. We don't want to see this event to be processed once focus
@@ -5329,7 +5341,7 @@ public void Devices_CanSkipProcessingEventsWhileInBackground()
53295341 InputSystem . Update ( ) ;
53305342
53315343 // Gain focus
5332- runtime . PlayerFocusGained ( ) ;
5344+ ScheduleFocusChangedEvent ( applicationHasFocus : true ) ;
53335345
53345346 // Run update to try process events accordingly once focus is gained
53355347 InputSystem . Update ( ) ;
0 commit comments