Skip to content

Commit

Permalink
feat: Add action just pressed tests
Browse files Browse the repository at this point in the history
Also changes the after_each method to wait for the InputSender to
release and clear all actions. There were some presses/releases
being leaked to the next test if we don't wait for an extra frame.
  • Loading branch information
Edearth committed Jul 5, 2024
1 parent ce0abd0 commit 766eb92
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 additions & 5 deletions test/unit/test_bugs/test_i578.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@ extends GutTest

class InputSingletonTracker:
extends Node
var input_frames = []
var pressed_frames = []
var just_pressed_count = 0
var just_released_count = 0

var _frame_counter = 0

func _process(delta):
_frame_counter += 1
if Input.is_anything_pressed():
input_frames.append(_frame_counter)

if(Input.is_action_just_pressed("jump")):
just_pressed_count += 1

if(Input.is_action_just_released("jump")):
just_released_count += 1

if Input.is_action_pressed("jump"):
pressed_frames.append(_frame_counter)

class TestInputSingleton:
extends "res://addons/gut/test.gd"
Expand All @@ -23,6 +32,9 @@ class TestInputSingleton:

func after_each():
_sender.release_all()
# Wait for key release to be processed. Otherwise the key release is
# leaked to the next test and it detects an extra key release.
await wait_frames(1)
_sender.clear()

func test_raw_input_press():
Expand All @@ -32,12 +44,31 @@ class TestInputSingleton:
await wait_frames(10)
Input.action_release("jump")

assert_gt(r.input_frames.size(), 1, 'input size')
assert_gt(r.pressed_frames.size(), 1, 'input size')

func test_input_sender_press():
var r = add_child_autofree(InputSingletonTracker.new())

_sender.action_down("jump").hold_for('10f')
await wait_for_signal(_sender.idle, 5)

assert_gt(r.input_frames.size(), 1, 'input size')
print(r.pressed_frames.size())
assert_gt(r.pressed_frames.size(), 1, 'input size')

func test_input_sender_just_pressed():
var r = add_child_autofree(InputSingletonTracker.new())

_sender.action_down("jump").hold_for("20f")
await wait_frames(5)

assert_eq(r.just_pressed_count, 1, 'just pressed once')
assert_eq(r.just_released_count, 0, 'not released yet')

func test_input_sender_just_released():
var r = add_child_autofree(InputSingletonTracker.new())

_sender.action_down("jump").hold_for('5f')
await wait_for_signal(_sender.idle, 10)

assert_eq(r.just_pressed_count, 1, 'just pressed once')
assert_eq(r.just_released_count, 1, 'released key once')

0 comments on commit 766eb92

Please sign in to comment.