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

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

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

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

201 lines
6.8 KiB
C#

// Copyright (c) 2025-2026 GeWuYou
// SPDX-License-Identifier: Apache-2.0
using GFramework.Game.Abstractions.Input;
using GFramework.Godot.Input;
namespace GFramework.Godot.Tests.Input;
/// <summary>
/// 验证 Godot 输入绑定存储在纯托管后端上的动作快照、导入与冲突交换语义。
/// </summary>
[TestFixture]
public sealed class GodotInputBindingStoreTests
{
/// <summary>
/// 验证导出快照会反映后端提供的框架绑定描述。
/// </summary>
[Test]
public void ExportSnapshot_Should_ReturnBackendBindings()
{
var backend = new FakeInputMapBackend(
new InputBindingSnapshot(
[
new InputActionBinding(
"ui_accept",
[
new InputBindingDescriptor(
InputDeviceKind.KeyboardMouse,
InputBindingKind.Key,
"key:13",
"Enter")
])
]));
var store = new GodotInputBindingStore(backend);
var snapshot = store.ExportSnapshot();
var acceptBindings = snapshot.Actions.Single(
action => string.Equals(action.ActionName, "ui_accept", StringComparison.Ordinal));
Assert.That(acceptBindings.Bindings[0].Code, Is.EqualTo("key:13"));
}
/// <summary>
/// 验证导入快照后会把新绑定回写到后端,并能重新导出。
/// </summary>
[Test]
public void ImportSnapshot_Should_UpdateBackendBindings()
{
var backend = new FakeInputMapBackend(
new InputBindingSnapshot(
[
new InputActionBinding(
"ui_accept",
[
new InputBindingDescriptor(
InputDeviceKind.KeyboardMouse,
InputBindingKind.Key,
"key:13",
"Enter")
])
]));
var store = new GodotInputBindingStore(backend);
store.ImportSnapshot(
new InputBindingSnapshot(
[
new InputActionBinding(
"ui_accept",
[
new InputBindingDescriptor(
InputDeviceKind.KeyboardMouse,
InputBindingKind.Key,
"key:32",
"Space")
])
]));
var snapshot = store.ExportSnapshot();
var acceptBindings = snapshot.Actions.Single(
action => string.Equals(action.ActionName, "ui_accept", StringComparison.Ordinal));
Assert.That(acceptBindings.Bindings[0].Code, Is.EqualTo("key:32"));
}
/// <summary>
/// 验证从纯托管绑定设置主绑定时,会保留 `Game` 层冲突交换语义。
/// </summary>
[Test]
public void SetPrimaryBinding_WhenBindingTaken_SwapsBackendBindings()
{
var backend = new FakeInputMapBackend(
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")
])
]));
var store = new GodotInputBindingStore(backend);
store.SetPrimaryBinding(
"move_left",
new InputBindingDescriptor(
InputDeviceKind.KeyboardMouse,
InputBindingKind.Key,
"key:68",
"D"));
var snapshot = store.ExportSnapshot();
var moveLeft = snapshot.Actions.Single(
action => string.Equals(action.ActionName, "move_left", StringComparison.Ordinal));
var moveRight = snapshot.Actions.Single(
action => string.Equals(action.ActionName, "move_right", StringComparison.Ordinal));
Assert.Multiple(() =>
{
Assert.That(moveLeft.Bindings[0].Code, Is.EqualTo("key:68"));
Assert.That(moveRight.Bindings[0].Code, Is.EqualTo("key:65"));
});
}
/// <summary>
/// 测试用的纯托管 InputMap 后端。
/// </summary>
private sealed class FakeInputMapBackend : IGodotInputMapBackend
{
private readonly Dictionary<string, List<InputBindingDescriptor>> _defaults;
private readonly Dictionary<string, List<InputBindingDescriptor>> _current;
/// <summary>
/// 初始化测试后端。
/// </summary>
/// <param name="snapshot">初始快照。</param>
public FakeInputMapBackend(InputBindingSnapshot snapshot)
{
_defaults = snapshot.Actions.ToDictionary(
static action => action.ActionName,
static action => action.Bindings.ToList(),
StringComparer.Ordinal);
_current = snapshot.Actions.ToDictionary(
static action => action.ActionName,
static action => action.Bindings.ToList(),
StringComparer.Ordinal);
}
/// <inheritdoc />
public IReadOnlyList<string> GetActionNames()
{
return [.._current.Keys.OrderBy(static key => key, StringComparer.Ordinal)];
}
/// <inheritdoc />
public IReadOnlyList<InputBindingDescriptor> GetBindings(string actionName)
{
return _current.TryGetValue(actionName, out var bindings) ? [..bindings] : Array.Empty<InputBindingDescriptor>();
}
/// <inheritdoc />
public void SetBindings(string actionName, IReadOnlyList<InputBindingDescriptor> bindings)
{
_current[actionName] = [..bindings];
}
/// <inheritdoc />
public void ResetAction(string actionName)
{
if (_defaults.TryGetValue(actionName, out var bindings))
{
_current[actionName] = [..bindings];
return;
}
_current.Remove(actionName);
}
/// <inheritdoc />
public void ResetAll()
{
_current.Clear();
foreach (var pair in _defaults)
{
_current[pair.Key] = [..pair.Value];
}
}
}
}