83 lines
2.6 KiB
GDScript
83 lines
2.6 KiB
GDScript
extends SceneTree
|
|
|
|
var failures: Array[String] = []
|
|
var intents: Array = []
|
|
|
|
|
|
func _init() -> void:
|
|
_run.call_deferred()
|
|
|
|
|
|
func _run() -> void:
|
|
var component: Node = load("res://scenes/components/input_component.gd").new()
|
|
root.add_child(component)
|
|
await process_frame
|
|
if not component.has_signal("intent_created"):
|
|
failures.append("InputComponent should expose intent_created")
|
|
else:
|
|
component.connect("intent_created", _on_intent_created)
|
|
|
|
var normal := InputEventKey.new()
|
|
normal.pressed = true
|
|
normal.keycode = KEY_A
|
|
normal.physical_keycode = KEY_A
|
|
var handled: bool = component.call("handle_input_event", normal)
|
|
_expect_bool(handled, true, "A press should be handled")
|
|
_expect_int(intents.size(), 1, "A press should emit one intent")
|
|
if intents.size() == 1:
|
|
_expect_string(str(intents[0].get("symbol")), "A", "intent symbol")
|
|
_expect_string(str(intents[0].get("rhythm_action")), "a", "intent rhythm action")
|
|
_expect_string(str(intents[0].get("event_type")), "pressed", "intent event type")
|
|
_expect_bool(float(intents[0].get("timestamp_ms")) > 0.0, true, "intent timestamp")
|
|
|
|
var echo := InputEventKey.new()
|
|
echo.pressed = true
|
|
echo.echo = true
|
|
echo.keycode = KEY_A
|
|
echo.physical_keycode = KEY_A
|
|
handled = component.call("handle_input_event", echo)
|
|
_expect_bool(handled, false, "echo press should not be handled")
|
|
_expect_int(intents.size(), 1, "echo press should not emit another intent")
|
|
|
|
var release := InputEventKey.new()
|
|
release.pressed = false
|
|
release.keycode = KEY_A
|
|
release.physical_keycode = KEY_A
|
|
handled = component.call("handle_input_event", release)
|
|
_expect_bool(handled, true, "A release should be handled")
|
|
_expect_int(intents.size(), 2, "A release should emit one release intent")
|
|
if intents.size() == 2:
|
|
_expect_string(str(intents[1].get("event_type")), "released", "release event type")
|
|
|
|
component.free()
|
|
_finish()
|
|
|
|
|
|
func _on_intent_created(intent) -> void:
|
|
intents.append(intent)
|
|
|
|
|
|
func _expect_bool(actual: bool, expected: bool, label: String) -> void:
|
|
if actual != expected:
|
|
failures.append("%s: expected %s, got %s" % [label, expected, actual])
|
|
|
|
|
|
func _expect_int(actual: int, expected: int, label: String) -> void:
|
|
if actual != expected:
|
|
failures.append("%s: expected %d, got %d" % [label, expected, actual])
|
|
|
|
|
|
func _expect_string(actual: String, expected: String, label: String) -> void:
|
|
if actual != expected:
|
|
failures.append("%s: expected %s, got %s" % [label, expected, actual])
|
|
|
|
|
|
func _finish() -> void:
|
|
if failures.is_empty():
|
|
print("PASS input component intents")
|
|
quit(0)
|
|
else:
|
|
for failure: String in failures:
|
|
push_error(failure)
|
|
quit(1)
|