Skip to content

Commit

Permalink
Merge pull request #3 from wlsnmrk/controller-input-extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
jolexxa authored Apr 6, 2024
2 parents 589c5f7 + e01444d commit a2efb65
Show file tree
Hide file tree
Showing 11 changed files with 582 additions and 119 deletions.
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

0 comments on commit a2efb65

Please sign in to comment.