Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,6 @@ ai_assistant_hub/preferences/always_scroll_to_bottom=false

[rendering]

textures/canvas_textures/default_texture_filter=0
renderer/rendering_method="gl_compatibility"
renderer/rendering_method.mobile="gl_compatibility"
175 changes: 175 additions & 0 deletions test/gut/test_input_remap_button.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
## test_input_remap_button.gd
## GUT unit tests for input_remap_button.gd.
## Covers IRB-01 to IRB-08 from test plan.
## Test Plan:
## https://github.com/ikostan/SkyLockAssault/issues/347

extends "res://addons/gut/test.gd"

var InputRemapButton: = preload("res://scripts/input_remap_button.gd") # Adjust path as needed.
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

var button: InputRemapButton
const TEST_ACTION: String = "test_action"


## Per-test setup: Reset InputMap for test action, instantiate button.
## :rtype: void
func before_each() -> void:
if InputMap.has_action(TEST_ACTION):
InputMap.erase_action(TEST_ACTION)
InputMap.add_action(TEST_ACTION)
button = InputRemapButton.new()
button.action = TEST_ACTION
button.current_device = InputRemapButton.DeviceType.KEYBOARD
add_child_autofree(button)


## Per-test cleanup: Erase test action.
## :rtype: void
func after_each() -> void:
if InputMap.has_action(TEST_ACTION):
InputMap.erase_action(TEST_ACTION)
await get_tree().process_frame


## IRB-01 | Remap keyboard event | current_device = KEYBOARD; action exists with prior events | Instantiate, simulate _input with Key, inspect InputMap | Only keyboard event added; old erased; button label updated; remap logged.
## :rtype: void
func test_irb_01() -> void:
# Add prior events
var prior_key: InputEventKey = InputEventKey.new()
prior_key.physical_keycode = KEY_A
InputMap.action_add_event(TEST_ACTION, prior_key)
var prior_gamepad: InputEventJoypadButton = InputEventJoypadButton.new()
prior_gamepad.button_index = JOY_BUTTON_A
InputMap.action_add_event(TEST_ACTION, prior_gamepad)
# Start remapping
button.button_pressed = true
button._on_pressed()
assert_true(button.listening)
# Simulate input
var new_key: InputEventKey = InputEventKey.new()
new_key.physical_keycode = KEY_B
new_key.pressed = true
button._input(new_key)
# Assert
var events: Array = InputMap.action_get_events(TEST_ACTION)
assert_eq(events.size(), 2)
assert_true(events.any(func(ev: InputEvent) -> bool: return ev is InputEventKey and ev.physical_keycode == KEY_B))
assert_true(events.any(func(ev: InputEvent) -> bool: return ev is InputEventJoypadButton and ev.button_index == JOY_BUTTON_A))
assert_eq(button.text, OS.get_keycode_string(KEY_B))
assert_false(button.listening)


## IRB-02 | Remap gamepad event | current_device = GAMEPAD; action exists | Simulate _input with InputEventJoypadButton, inspect InputMap | Only gamepad event added; non-matching events ignored; label updated correctly.
## :rtype: void
func test_irb_02() -> void:
button.current_device = InputRemapButton.DeviceType.GAMEPAD
# Add prior events
var prior_key: InputEventKey = InputEventKey.new()
prior_key.physical_keycode = KEY_A
InputMap.action_add_event(TEST_ACTION, prior_key)
# Start remapping
button.button_pressed = true
button._on_pressed()
assert_true(button.listening)
# Simulate input
var new_gamepad: InputEventJoypadButton = InputEventJoypadButton.new()
new_gamepad.button_index = JOY_BUTTON_B
new_gamepad.pressed = true
button._input(new_gamepad)
# Assert
var events: Array = InputMap.action_get_events(TEST_ACTION)
assert_eq(events.size(), 2)
assert_true(events.any(func(ev: InputEvent) -> bool: return ev is InputEventJoypadButton and ev.button_index == JOY_BUTTON_B))
assert_true(events.any(func(ev: InputEvent) -> bool: return ev is InputEventKey and ev.physical_keycode == KEY_A))
assert_eq(button.text, "B")
assert_false(button.listening)


## IRB-04 | Ignore wrong-device event during remap | current_device = KEYBOARD | Simulate _input with InputEventJoypadButton | Event ignored; no change to InputMap; label unchanged.
## :rtype: void
func test_irb_04() -> void:
# Add prior event
var prior_key: InputEventKey = InputEventKey.new()
prior_key.physical_keycode = KEY_A
InputMap.action_add_event(TEST_ACTION, prior_key)
# Start remapping
button.button_pressed = true
button._on_pressed()
assert_true(button.listening)
# Simulate wrong input
var gamepad_event: InputEventJoypadButton = InputEventJoypadButton.new()
gamepad_event.button_index = JOY_BUTTON_A
gamepad_event.pressed = true
button._input(gamepad_event)
# Assert
var events: Array = InputMap.action_get_events(TEST_ACTION)
assert_eq(events.size(), 1)
assert_true(events[0] is InputEventKey)
assert_eq(events[0].physical_keycode, KEY_A)
assert_eq(button.text, "Press a key or controller button/axis...")
assert_true(button.listening)


## IRB-05 | Get matching event for device | Action has mixed keyboard, mouse, and gamepad events | Call get_matching_event(DeviceType.KEYBOARD) | Correct keyboard or mouse event returned; null if none exist.
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
## :rtype: void
func test_irb_05() -> void:
# Add mixed events
var key_event: InputEventKey = InputEventKey.new()
key_event.physical_keycode = KEY_A
InputMap.action_add_event(TEST_ACTION, key_event)
var gamepad_event: InputEventJoypadButton = InputEventJoypadButton.new()
gamepad_event.button_index = JOY_BUTTON_A
InputMap.action_add_event(TEST_ACTION, gamepad_event)
# For keyboard
button.current_device = InputRemapButton.DeviceType.KEYBOARD
var matching: InputEvent = button.get_matching_event()
assert_true(matching is InputEventKey)
# For gamepad
button.current_device = InputRemapButton.DeviceType.GAMEPAD
matching = button.get_matching_event()
assert_true(matching is InputEventJoypadButton)
# None for keyboard after erase
InputMap.action_erase_events(TEST_ACTION)
button.current_device = InputRemapButton.DeviceType.KEYBOARD
matching = button.get_matching_event()
assert_null(matching)


## IRB-07 | Handle remap logging | Logging enabled | Perform successful remap, inspect log output | Log entry includes device type and new event details.
## :rtype: void
func test_irb_07() -> void:
# Note: Assuming Globals.log_info is called; use spy if available.
# For simplicity, perform remap (logging happens internally).
button.button_pressed = true
button._on_pressed()
var new_key: InputEventKey = InputEventKey.new()
new_key.physical_keycode = KEY_C
new_key.pressed = true
button._input(new_key)
# Assert remap happened, assume log is printed (no direct assert on print).
var events: Array = InputMap.action_get_events(TEST_ACTION)
assert_eq(events.size(), 1)
# If GUT supports output spying, add here.


## IRB-08 | Edge case: No existing events | Action exists but has no mapped events | Clear action events, remap a new valid event | New event added correctly for active device; no errors thrown.
## :rtype: void
func test_irb_08() -> void:
InputMap.action_erase_events(TEST_ACTION)
# Start remapping
button.button_pressed = true
button._on_pressed()
assert_true(button.listening)
# Simulate input
var new_key: InputEventKey = InputEventKey.new()
new_key.physical_keycode = KEY_D
new_key.pressed = true
button._input(new_key)
# Assert
var events: Array = InputMap.action_get_events(TEST_ACTION)
assert_eq(events.size(), 1)
assert_true(events[0] is InputEventKey)
assert_eq(events[0].physical_keycode, KEY_D)
assert_eq(button.text, OS.get_keycode_string(KEY_D))
assert_false(button.listening)
Loading