115 lines
3.2 KiB
GDScript
115 lines
3.2 KiB
GDScript
extends Node
|
||
## 输入处理器测试
|
||
## Feature: godot-ai-town-game, Property 17: 设备类型检测
|
||
|
||
const TEST_ITERATIONS = 100
|
||
|
||
func _ready():
|
||
print("\n=== InputHandler Tests ===")
|
||
test_property_device_detection()
|
||
test_keyboard_input()
|
||
test_input_signals()
|
||
print("=== All InputHandler tests completed ===\n")
|
||
|
||
## Feature: godot-ai-town-game, Property 17: 设备类型检测
|
||
func test_property_device_detection():
|
||
"""
|
||
属性测试:设备类型检测
|
||
对于任意设备类型(桌面或移动),系统应该正确检测设备类型
|
||
并应用相应的控制方案(键盘或触摸)
|
||
"""
|
||
print("\n[Property Test] Device Type Detection")
|
||
|
||
var passed = 0
|
||
var failed = 0
|
||
|
||
for i in range(TEST_ITERATIONS):
|
||
var input_handler = InputHandler.new()
|
||
add_child(input_handler)
|
||
|
||
await get_tree().process_frame
|
||
|
||
# 获取检测到的设备类型
|
||
var device_type = input_handler.get_device_type()
|
||
|
||
# 验证设备类型是有效的
|
||
var valid_device = device_type in [
|
||
InputHandler.DeviceType.DESKTOP,
|
||
InputHandler.DeviceType.MOBILE,
|
||
InputHandler.DeviceType.UNKNOWN
|
||
]
|
||
|
||
if valid_device:
|
||
passed += 1
|
||
else:
|
||
failed += 1
|
||
print(" FAILED iteration ", i + 1)
|
||
print(" Invalid device type: ", device_type)
|
||
|
||
input_handler.queue_free()
|
||
await get_tree().process_frame
|
||
|
||
print(" Result: ", passed, "/", TEST_ITERATIONS, " passed")
|
||
if failed > 0:
|
||
print(" ❌ FAILED: ", failed, " iterations failed")
|
||
else:
|
||
print(" ✅ PASSED: All iterations successful")
|
||
|
||
## Feature: godot-ai-town-game, Property 11: 键盘输入响应
|
||
func test_keyboard_input():
|
||
"""
|
||
单元测试:键盘输入响应
|
||
测试键盘输入是否正确转换为方向向量
|
||
"""
|
||
print("\n[Unit Test] Keyboard Input Response")
|
||
|
||
var input_handler = InputHandler.new()
|
||
add_child(input_handler)
|
||
|
||
await get_tree().process_frame
|
||
|
||
# 测试获取移动方向(在没有实际按键的情况下应该返回零向量)
|
||
var direction = input_handler.get_move_direction()
|
||
assert(direction == Vector2.ZERO, "Direction should be zero when no input")
|
||
|
||
# 测试交互键(应该返回 false)
|
||
var interact = input_handler.is_interact_pressed()
|
||
assert(interact == false, "Interact should be false when not pressed")
|
||
|
||
print(" ✅ PASSED: Keyboard input methods work correctly")
|
||
|
||
input_handler.queue_free()
|
||
await get_tree().process_frame
|
||
|
||
## 测试输入信号
|
||
func test_input_signals():
|
||
"""
|
||
单元测试:输入信号
|
||
测试输入处理器是否正确发射信号
|
||
"""
|
||
print("\n[Unit Test] Input Signals")
|
||
|
||
var signal_data = {
|
||
"move_received": false,
|
||
"interact_received": false,
|
||
"device_received": false
|
||
}
|
||
|
||
var input_handler = InputHandler.new()
|
||
|
||
# 在添加到场景树之前连接信号
|
||
input_handler.move_input.connect(func(_dir): signal_data["move_received"] = true)
|
||
input_handler.interact_input.connect(func(): signal_data["interact_received"] = true)
|
||
input_handler.device_detected.connect(func(_dev): signal_data["device_received"] = true)
|
||
|
||
add_child(input_handler)
|
||
await get_tree().process_frame
|
||
|
||
# 设备检测信号应该在初始化时已经发射
|
||
assert(signal_data["device_received"], "Device detection signal should be emitted")
|
||
|
||
print(" ✅ PASSED: Input signals work correctly")
|
||
|
||
input_handler.queue_free()
|
||
await get_tree().process_frame
|