Files
vampire-like/Assets/Plugins/InputModule/Tests/PlayMode/InputModule/InputModuleComponentRebindTests.cs
T

216 lines
8.1 KiB
C#

using System.Collections;
using NUnit.Framework;
using SepCore.InputModule;
using SepCore.InputModule.Runtime;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.TestTools;
namespace SepCore.InputModule.Tests
{
public class InputModuleComponentRebindTests : InputTestFixture
{
private GameObject _gameObject;
private InputModuleComponent _component;
public override void Setup()
{
base.Setup();
_gameObject = new GameObject("InputModuleRebindTest");
_component = InputModuleTestFactory.CreateComponent(_gameObject);
_component.OnInit();
}
public override void TearDown()
{
Object.DestroyImmediate(_gameObject);
base.TearDown();
}
[Test]
public void StartRebinding_ValidBinding_ReturnsTrueAndSetsIsRebinding()
{
bool started = _component.StartRebinding(InputContextId.UI, InputActionId.Confirm, 0);
Assert.That(started, Is.True);
Assert.That(_component.IsRebinding, Is.True);
_component.CancelRebinding();
}
[Test]
public void StartRebinding_AlreadyRebinding_ReturnsFalse()
{
_component.StartRebinding(InputContextId.UI, InputActionId.Confirm, 0);
bool started = _component.StartRebinding(InputContextId.UI, InputActionId.Confirm, 1);
Assert.That(started, Is.False);
_component.CancelRebinding();
}
[Test]
public void StartRebinding_GlobalAction_ReturnsTrueAndSetsIsRebinding()
{
bool started = _component.StartRebinding(InputContextId.Global, InputActionId.Pause, 0);
Assert.That(started, Is.True);
Assert.That(_component.IsRebinding, Is.True);
_component.CancelRebinding();
}
[Test]
public void StartRebinding_InvalidBindingIndex_ReturnsFalse()
{
bool started = _component.StartRebinding(InputContextId.UI, InputActionId.Confirm, 99);
Assert.That(started, Is.False);
}
[Test]
public void CancelRebinding_InvokesRebindCompletedWithCanceled()
{
RebindResult result = default;
_component.RebindCompleted += r => result = r;
_component.StartRebinding(InputContextId.UI, InputActionId.Confirm, 0);
_component.CancelRebinding();
Assert.That(result.Status, Is.EqualTo(RebindStatus.Canceled));
Assert.That(_component.IsRebinding, Is.False);
}
[Test]
public void RebindTimeout_InvokesRebindCompletedWithTimeout()
{
RebindResult result = default;
_component.RebindCompleted += r => result = r;
_component.StartRebinding(InputContextId.UI, InputActionId.Confirm, 0, timeoutSeconds: 0.05f);
currentTime += 0.1;
InputSystem.Update();
Assert.That(result.Status, Is.EqualTo(RebindStatus.Timeout));
Assert.That(_component.IsRebinding, Is.False);
}
[UnityTest]
public IEnumerator Rebind_WithExpectedDeviceKind_AcceptsMatchingDevice()
{
RebindResult result = default;
_component.RebindCompleted += r => result = r;
Keyboard keyboard = InputSystem.AddDevice<Keyboard>();
_component.StartRebinding(InputContextId.UI, InputActionId.Confirm, 0, expectedDeviceKind: InputDeviceKind.KeyboardMouse);
Press(keyboard.spaceKey);
yield return null;
Assert.That(result.Status, Is.EqualTo(RebindStatus.Succeeded));
Assert.That(result.Path, Is.EqualTo("<Keyboard>/space"));
}
[UnityTest]
public IEnumerator Rebind_WithExpectedDeviceKind_RejectsWrongDevice()
{
RebindResult result = default;
_component.RebindCompleted += r => result = r;
Keyboard keyboard = InputSystem.AddDevice<Keyboard>();
_component.StartRebinding(InputContextId.UI, InputActionId.Confirm, 0, expectedDeviceKind: InputDeviceKind.Gamepad);
Press(keyboard.spaceKey);
yield return null;
Assert.That(result.Status, Is.EqualTo(RebindStatus.Canceled));
}
[UnityTest]
public IEnumerator Rebind_WithConflict_RejectsAndRestoresPreviousPath()
{
InputAction pauseAction = _component.RuntimeActions.FindAction($"{nameof(InputContextId.Global)}/{nameof(InputActionId.Pause)}");
pauseAction.ApplyBindingOverride("<Keyboard>/space");
RebindResult result = default;
_component.RebindCompleted += r => result = r;
Keyboard keyboard = InputSystem.AddDevice<Keyboard>();
_component.StartRebinding(InputContextId.UI, InputActionId.Confirm, 0);
Press(keyboard.spaceKey);
yield return null;
Assert.That(result.Status, Is.EqualTo(RebindStatus.ConflictDetected));
Assert.That(result.PreviousPath, Is.EqualTo("<Keyboard>/enter"));
InputAction confirmAction = _component.RuntimeActions.FindAction($"{nameof(InputContextId.UI)}/{nameof(InputActionId.Confirm)}");
Assert.That(confirmAction.bindings[0].effectivePath, Is.EqualTo("<Keyboard>/enter"));
}
[UnityTest]
public IEnumerator Rebind_Completes_RestoresPreviousContext()
{
_component.SetContext(InputContextId.GameplayExplore);
Assert.That(_component.CurrentContext, Is.EqualTo(InputContextId.GameplayExplore));
Keyboard keyboard = InputSystem.AddDevice<Keyboard>();
_component.StartRebinding(InputContextId.UI, InputActionId.Confirm, 0);
Press(keyboard.spaceKey);
yield return null;
Assert.That(_component.CurrentContext, Is.EqualTo(InputContextId.GameplayExplore));
}
[Test]
public void ResetBindingToDefault_RemovesOverride()
{
InputAction confirmAction = _component.RuntimeActions.FindAction($"{nameof(InputContextId.UI)}/{nameof(InputActionId.Confirm)}");
confirmAction.ApplyBindingOverride(0, "<Keyboard>/space");
_component.ResetBindingToDefault(InputContextId.UI, InputActionId.Confirm, 0);
Assert.That(confirmAction.bindings[0].overridePath, Is.Null);
}
[Test]
public void ResetActionToDefaults_RemovesAllOverridesForAction()
{
InputAction confirmAction = _component.RuntimeActions.FindAction($"{nameof(InputContextId.UI)}/{nameof(InputActionId.Confirm)}");
confirmAction.ApplyBindingOverride(0, "<Keyboard>/space");
confirmAction.ApplyBindingOverride(1, "<Keyboard>/f");
_component.ResetActionToDefaults(InputContextId.UI, InputActionId.Confirm);
Assert.That(confirmAction.bindings[0].overridePath, Is.Null);
Assert.That(confirmAction.bindings[1].overridePath, Is.Null);
}
[Test]
public void ResetAllBindingsToDefaults_RemovesAllOverrides()
{
InputAction confirmAction = _component.RuntimeActions.FindAction($"{nameof(InputContextId.UI)}/{nameof(InputActionId.Confirm)}");
confirmAction.ApplyBindingOverride(0, "<Keyboard>/space");
_component.ResetAllBindingsToDefaults();
Assert.That(confirmAction.bindings[0].overridePath, Is.Null);
}
[Test]
public void ApplyBindingSnapshot_AfterReset_RestoresOverrides()
{
InputAction confirmAction = _component.RuntimeActions.FindAction($"{nameof(InputContextId.UI)}/{nameof(InputActionId.Confirm)}");
confirmAction.ApplyBindingOverride(0, "<Keyboard>/space");
string overridesJson = _component.RuntimeActions.SaveBindingOverridesAsJson();
InputBindingSnapshot snapshot = new InputBindingSnapshot(overridesJson);
_component.ResetAllBindingsToDefaults();
Assert.That(confirmAction.bindings[0].overridePath, Is.Null);
_component.ApplyBindingSnapshot(snapshot);
Assert.That(confirmAction.bindings[0].effectivePath, Is.EqualTo("<Keyboard>/space"));
}
}
}