Skip to content

Commit f3ed1ae

Browse files
added test for gamepad background behaviour
1 parent d7e0a17 commit f3ed1ae

1 file changed

Lines changed: 67 additions & 0 deletions

File tree

Assets/Tests/InputSystem/CoreTests_Devices.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5296,6 +5296,73 @@ void DeviceChangeCallback(InputDevice device, InputDeviceChange change)
52965296
#pragma warning restore CS0162
52975297
}
52985298

5299+
[Test]
5300+
[Category("Devices")]
5301+
[TestCase(true, true)]
5302+
[TestCase(true, false)]
5303+
public unsafe void Devices_GamepadCanSkipEventsWhileInBackground_WithResetAndDisableNonBackgroundDevices(bool runInBackground, bool runDeviceInBackground)
5304+
{
5305+
InputSystem.runInBackground = runInBackground;
5306+
InputSystem.settings.backgroundBehavior = InputSettings.BackgroundBehavior.ResetAndDisableNonBackgroundDevices;
5307+
5308+
var time = 0;
5309+
var gamepad = InputSystem.AddDevice<Gamepad>();
5310+
var pressAction = new InputAction("Press", binding: "<Gamepad>/buttonSouth");
5311+
var performedCount = 0;
5312+
var eventCount = 100;
5313+
5314+
long DeviceCallback(string device, InputDeviceCommand* command, bool canRunInBackground)
5315+
{
5316+
if (command->type == QueryCanRunInBackground.Type)
5317+
{
5318+
((QueryCanRunInBackground*)command)->canRunInBackground = canRunInBackground;
5319+
return InputDeviceCommand.GenericSuccess;
5320+
}
5321+
return InputDeviceCommand.GenericFailure;
5322+
}
5323+
5324+
runtime.SetDeviceCommandCallback(gamepad,
5325+
(id, command) =>
5326+
DeviceCallback("Gamepad", command, runDeviceInBackground));
5327+
5328+
pressAction.performed += ctx =>
5329+
{
5330+
performedCount++;
5331+
};
5332+
pressAction.Enable();
5333+
5334+
Assert.That(gamepad.enabled, Is.True);
5335+
5336+
// Lose focus
5337+
ScheduleFocusChangedEvent(applicationHasFocus: false);
5338+
InputSystem.Update(InputUpdateType.Dynamic);
5339+
5340+
Assert.That(gamepad.canRunInBackground, Is.EqualTo(runDeviceInBackground));
5341+
5342+
// Device should only be enabled if app runs in background and device can run in background
5343+
if (runInBackground && runDeviceInBackground)
5344+
Assert.That(gamepad.enabled, Is.True);
5345+
else
5346+
Assert.That(gamepad.enabled, Is.False);
5347+
5348+
// Simulate noisy controller sending many events while in background
5349+
// Queue all events first, then process them in one update
5350+
for (int i = 0; i < eventCount; i++)
5351+
{
5352+
Press(gamepad.buttonSouth, queueEventOnly: true, time: time++);
5353+
Release(gamepad.buttonSouth, queueEventOnly: true, time: time++);
5354+
}
5355+
5356+
// Process all queued events
5357+
InputSystem.Update(InputUpdateType.Dynamic);
5358+
5359+
// If device should be disabled, events should have been discarded
5360+
if (runInBackground && runDeviceInBackground)
5361+
Assert.That(performedCount, Is.EqualTo(eventCount)); // Device runs in background, events processed
5362+
else
5363+
Assert.That(performedCount, Is.EqualTo(0)); // Device disabled, events discarded
5364+
}
5365+
52995366
[Test]
53005367
[Category("Devices")]
53015368
// This test validates that when InputSettings.BackgroundBehavior.ResetAndDisableAllDevices is selected,

0 commit comments

Comments
 (0)