GFramework/GFramework.Game.Tests/Input/InputBindingStoreTests.cs
gewuyou ebbef321ad feat(input): 新增统一输入抽象与Godot集成
- 新增输入绑定 DTO、设备上下文和 UI 语义桥接契约。

- 实现 Game 默认输入绑定存储、动作映射和 UI 分发桥接。

- 落地 Godot InputMap 适配、测试覆盖与配套文档。

- 更新 ai-plan 恢复点、worktree 映射与采用入口。
2026-05-10 22:29:26 +08:00

77 lines
2.4 KiB
C#

// Copyright (c) 2025-2026 GeWuYou
// SPDX-License-Identifier: Apache-2.0
using GFramework.Game.Abstractions.Input;
using GFramework.Game.Input;
namespace GFramework.Game.Tests.Input;
/// <summary>
/// 验证默认输入绑定存储的重绑定、冲突交换与默认恢复行为。
/// </summary>
[TestFixture]
public sealed class InputBindingStoreTests
{
/// <summary>
/// 验证主绑定冲突时,会把原绑定交换回被占用动作。
/// </summary>
[Test]
public void SetPrimaryBinding_WhenBindingOwnedByAnotherAction_SwapsBindings()
{
var store = CreateStore();
var replacement = new InputBindingDescriptor(
InputDeviceKind.KeyboardMouse,
InputBindingKind.Key,
"key:68",
"D");
store.SetPrimaryBinding("move_left", replacement);
var moveLeft = store.GetBindings("move_left");
var moveRight = store.GetBindings("move_right");
Assert.Multiple(() =>
{
Assert.That(moveLeft.Bindings[0].Code, Is.EqualTo("key:68"));
Assert.That(moveRight.Bindings[0].Code, Is.EqualTo("key:65"));
});
}
/// <summary>
/// 验证重置全部绑定时,会回退到初始化默认快照。
/// </summary>
[Test]
public void ResetAll_Should_Restore_DefaultSnapshot()
{
var store = CreateStore();
store.SetPrimaryBinding(
"move_left",
new InputBindingDescriptor(InputDeviceKind.KeyboardMouse, InputBindingKind.Key, "key:81", "Q"));
store.ResetAll();
var snapshot = store.ExportSnapshot();
Assert.That(
snapshot.Actions.Single(action => string.Equals(action.ActionName, "move_left", StringComparison.Ordinal)).Bindings[0].Code,
Is.EqualTo("key:65"));
}
private static InputBindingStore CreateStore()
{
return new InputBindingStore(
new InputBindingSnapshot(
[
new InputActionBinding(
"move_left",
[
new InputBindingDescriptor(InputDeviceKind.KeyboardMouse, InputBindingKind.Key, "key:65", "A")
]),
new InputActionBinding(
"move_right",
[
new InputBindingDescriptor(InputDeviceKind.KeyboardMouse, InputBindingKind.Key, "key:68", "D")
])
]));
}
}