110 lines
4.0 KiB
Markdown
110 lines
4.0 KiB
Markdown
---
|
|
uid: input-system-write-tests
|
|
---
|
|
|
|
# Write tests
|
|
|
|
When writing a test, use [`InputSystem.AddDevice<T>()`](xref:UnityEngine.InputSystem.InputSystem) to add new Devices.
|
|
|
|
```CSharp
|
|
[Test]
|
|
public void PlayerInput_CanInstantiatePlayer_WithSpecificControlScheme()
|
|
{
|
|
InputSystem.AddDevice<Gamepad>();
|
|
var keyboard = InputSystem.AddDevice<Keyboard>();
|
|
var mouse = InputSystem.AddDevice<Mouse>();
|
|
|
|
var prefab = new GameObject();
|
|
prefab.SetActive(false);
|
|
var prefabPlayerInput = prefab.AddComponent<PlayerInput>();
|
|
prefabPlayerInput.actions = InputActionAsset.FromJson(kActions);
|
|
|
|
var player = PlayerInput.Instantiate(prefab, controlScheme: "Keyboard&Mouse");
|
|
|
|
Assert.That(player.devices, Is.EquivalentTo(new InputDevice[] { keyboard, mouse }));
|
|
Assert.That(player.controlScheme, Is.EqualTo("Keyboard&Mouse"));
|
|
}
|
|
```
|
|
|
|
To feed input, the easiest way is to use the [`Press(button)`](xref:UnityEngine.InputSystem.InputTestFixture), [`Release(button)`](xref:UnityEngine.InputSystem.InputTestFixture), [`PressAndRelease(button)`](xref:UnityEngine.InputSystem.InputTestFixture), `Set(control,value)`, and [`Trigger(action)`](xref:UnityEngine.InputSystem.InputTestFixture) helper methods provided by [`InputTestFixture`](xref:UnityEngine.InputSystem.InputTestFixture).
|
|
|
|
```CSharp
|
|
[Test]
|
|
public void Actions_WhenDisabled_CancelAllStartedInteractions()
|
|
{
|
|
var gamepad = InputSystem.AddDevice<Gamepad>();
|
|
|
|
var action1 = new InputAction("action1", binding: "<Gamepad>/buttonSouth", interactions: "Hold");
|
|
var action2 = new InputAction("action2", binding: "<Gamepad>/leftStick");
|
|
|
|
action1.Enable();
|
|
action2.Enable();
|
|
|
|
Press(gamepad.buttonSouth);
|
|
Set(gamepad.leftStick, new Vector2(0.123f, 0.234f));
|
|
|
|
using (var trace = new InputActionTrace())
|
|
{
|
|
trace.SubscribeTo(action1);
|
|
trace.SubscribeTo(action2);
|
|
|
|
runtime.currentTime = 0.234f;
|
|
runtime.advanceTimeEachDynamicUpdate = 0;
|
|
|
|
action1.Disable();
|
|
action2.Disable();
|
|
|
|
var actions = trace.ToArray();
|
|
|
|
Assert.That(actions.Length, Is.EqualTo(2));
|
|
//...
|
|
}
|
|
}
|
|
```
|
|
|
|
Alternatively, you can use code to feed arbitrary input events into the system, and run arbitrary input updates:
|
|
|
|
```CSharp
|
|
[Test]
|
|
public void PlayerInput_JoiningPlayerThroughButtonPress_WillFailIfDeviceIsNotUsableWithPlayerActions()
|
|
{
|
|
var playerPrefab = new GameObject();
|
|
playerPrefab.SetActive(false);
|
|
playerPrefab.AddComponent<PlayerInput>();
|
|
playerPrefab.GetComponent<PlayerInput>().actions = InputActionAsset.FromJson(kActions);
|
|
|
|
var manager = new GameObject();
|
|
var listener = manager.AddComponent<MessageListener>();
|
|
var managerComponent = manager.AddComponent<PlayerInputManager>();
|
|
managerComponent.joinBehavior = PlayerJoinBehavior.JoinPlayersWhenButtonIsPressed;
|
|
managerComponent.playerPrefab = playerPrefab;
|
|
|
|
// Create a Device based on the HID layout with a single button control.
|
|
const string kLayout = @"
|
|
{
|
|
""name"" : ""TestDevice"",
|
|
""extend"" : ""HID"",
|
|
""controls"" : [
|
|
{ ""name"" : ""button"", ""layout"" : ""Button"" }
|
|
]
|
|
}
|
|
";
|
|
|
|
InputSystem.RegisterLayout(kLayout);
|
|
var device = InputSystem.AddDevice("TestDevice");
|
|
|
|
using (StateEvent.From(device, out var eventPtr))
|
|
{
|
|
((ButtonControl)device["button"]).WriteValueIntoEvent(1f, eventPtr);
|
|
InputSystem.QueueEvent(eventPtr);
|
|
InputSystem.Update();
|
|
}
|
|
|
|
Assert.That(listener.messages, Is.Empty);
|
|
Assert.That(PlayerInput.all, Is.Empty);
|
|
}
|
|
```
|
|
|
|
> [!NOTE]
|
|
> For reference, you can find the tests for the Input System itself in its [GitHub repository](https://github.com/Unity-Technologies/InputSystem/tree/stable/Assets/Tests/InputSystem).
|