Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add controller input extensions #3

Merged
merged 8 commits into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ indent_style = space
indent_size = 4
insert_final_newline = true
trim_trailing_whitespace = true
end_of_line = lf

##########################################
# File Extension Settings
Expand Down
Original file line number Diff line number Diff line change
@@ -1,99 +1,110 @@
namespace Chickensoft.GodotTestDriver.Tests;

using Chickensoft.GoDotTest;
using Godot;
using GodotTestDriver.Input;
using JetBrains.Annotations;
using Shouldly;

[UsedImplicitly]
public partial class ActionsControlExtensionsTest : DriverTest
{
private partial class ActionInputEventTestNode : Node
{
public bool HasInputEventFired { get; set; }
public bool WasInputPressed { get; set; }
public StringName InputEventName { get; set; } = string.Empty;

public override void _Input(InputEvent @event)
{
if (@event.IsAction(InputEventName))
{
HasInputEventFired = true;
WasInputPressed = @event.IsActionPressed(InputEventName);
}
}
}

private const string TestAction = "test_action";

public ActionsControlExtensionsTest(Node testScene) : base(testScene)
{
}

[Test]
public void StartActionSetsGlobalActionPressed()
{
Input.IsActionPressed(TestAction).ShouldBeFalse();
RootNode.StartAction(TestAction);
Input.IsActionPressed(TestAction).ShouldBeTrue();
RootNode.EndAction(TestAction);
}

[Test]
public void EndActionUnsetsGlobalActionPressed()
{
RootNode.StartAction(TestAction);
RootNode.EndAction(TestAction);
Input.IsActionPressed(TestAction).ShouldBeFalse();
}

[Test]
public void StartActionSetsGlobalActionJustPressed()
{
RootNode.StartAction(TestAction);
Input.IsActionJustPressed(TestAction).ShouldBeTrue();
RootNode.EndAction(TestAction);
}

[Test]
public void EndActionSetsGlobalActionJustReleased()
{
RootNode.StartAction(TestAction);
RootNode.EndAction(TestAction);
Input.IsActionJustReleased(TestAction).ShouldBeTrue();
}

[Test]
public void StartActionSendsInputEvent()
{
var inputTestNode = new ActionInputEventTestNode
{
InputEventName = TestAction
};
RootNode.AddChild(inputTestNode);
inputTestNode.HasInputEventFired.ShouldBeFalse();
inputTestNode.StartAction(TestAction);
inputTestNode.HasInputEventFired.ShouldBeTrue();
inputTestNode.WasInputPressed.ShouldBeTrue();
inputTestNode.EndAction(TestAction);
RootNode.RemoveChild(inputTestNode); // Remove immediately since we won't wait a frame for the free
inputTestNode.QueueFree();
}

[Test]
public void EndActionSendsInputEvent()
{
var inputTestNode = new ActionInputEventTestNode
{
InputEventName = TestAction
};
RootNode.AddChild(inputTestNode);
inputTestNode.HasInputEventFired.ShouldBeFalse();
inputTestNode.EndAction(TestAction);
inputTestNode.HasInputEventFired.ShouldBeTrue();
inputTestNode.WasInputPressed.ShouldBeFalse();
RootNode.RemoveChild(inputTestNode); // Remove immediately since we won't wait a frame for the free
inputTestNode.QueueFree();
}
}
namespace Chickensoft.GodotTestDriver.Tests;

using Chickensoft.GoDotTest;
using Godot;
using GodotTestDriver.Input;
using JetBrains.Annotations;
using Shouldly;

[UsedImplicitly]
public partial class ActionsInputExtensionsTest : DriverTest
{
private partial class ActionInputEventTestNode : Node
{
public bool HasInputEventFired { get; set; }
public bool WasInputPressed { get; set; }
public StringName InputEventName { get; set; } = string.Empty;

public override void _Input(InputEvent @event)
{
if (@event.IsAction(InputEventName))
{
HasInputEventFired = true;
WasInputPressed = @event.IsActionPressed(InputEventName);
}
}
}

private const string TestAction = "test_action";

public ActionsInputExtensionsTest(Node testScene) : base(testScene)
{
}

[Test]
public void StartActionSetsGlobalActionPressed()
{
Input.IsActionPressed(TestAction).ShouldBeFalse();
RootNode.StartAction(TestAction);
Input.IsActionPressed(TestAction).ShouldBeTrue();
RootNode.EndAction(TestAction);
}

[Test]
public void StartActionClampsStrengthBetweenZeroAndOne()
{
RootNode.StartAction(TestAction, -1);
Input.GetActionStrength(TestAction).ShouldBe(0);
RootNode.EndAction(TestAction);
RootNode.StartAction(TestAction, 2);
Input.GetActionStrength(TestAction).ShouldBe(1);
RootNode.EndAction(TestAction);
}

[Test]
public void EndActionUnsetsGlobalActionPressed()
{
RootNode.StartAction(TestAction);
RootNode.EndAction(TestAction);
Input.IsActionPressed(TestAction).ShouldBeFalse();
}

[Test]
public void StartActionSetsGlobalActionJustPressed()
{
RootNode.StartAction(TestAction);
Input.IsActionJustPressed(TestAction).ShouldBeTrue();
RootNode.EndAction(TestAction);
}

[Test]
public void EndActionSetsGlobalActionJustReleased()
{
RootNode.StartAction(TestAction);
RootNode.EndAction(TestAction);
Input.IsActionJustReleased(TestAction).ShouldBeTrue();
}

[Test]
public void StartActionSendsInputEvent()
{
var inputTestNode = new ActionInputEventTestNode
{
InputEventName = TestAction
};
RootNode.AddChild(inputTestNode);
inputTestNode.HasInputEventFired.ShouldBeFalse();
inputTestNode.StartAction(TestAction);
inputTestNode.HasInputEventFired.ShouldBeTrue();
inputTestNode.WasInputPressed.ShouldBeTrue();
inputTestNode.EndAction(TestAction);
RootNode.RemoveChild(inputTestNode); // Remove immediately since we won't wait a frame for the free
inputTestNode.QueueFree();
}

[Test]
public void EndActionSendsInputEvent()
{
var inputTestNode = new ActionInputEventTestNode
{
InputEventName = TestAction
};
RootNode.AddChild(inputTestNode);
inputTestNode.HasInputEventFired.ShouldBeFalse();
inputTestNode.EndAction(TestAction);
inputTestNode.HasInputEventFired.ShouldBeTrue();
inputTestNode.WasInputPressed.ShouldBeFalse();
RootNode.RemoveChild(inputTestNode); // Remove immediately since we won't wait a frame for the free
inputTestNode.QueueFree();
}
}
Loading
Loading