Skip to content

Commit

Permalink
Merge pull request #1 from wlsnmrk/synchronicity
Browse files Browse the repository at this point in the history
feat: fire input action events, synchronize methods
  • Loading branch information
jolexxa authored Jan 22, 2024
2 parents 448006b + 6a952c1 commit d10266f
Show file tree
Hide file tree
Showing 30 changed files with 383 additions and 335 deletions.
99 changes: 99 additions & 0 deletions GodotTestDriver.Tests/ActionsControlExtensionsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
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();
}
}
3 changes: 3 additions & 0 deletions GodotTestDriver.Tests/ActionsControlExtensionsTest.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[gd_scene format=3 uid="uid://busk27caw7oi8"]

[node name="ActionsControlExtensionsTest" type="Node"]
13 changes: 6 additions & 7 deletions GodotTestDriver.Tests/ButtonDriverTest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace Chickensoft.GodotTestDriver.Tests;

using System;
using System.Threading.Tasks;
using Chickensoft.GoDotTest;
using Godot;
using GodotTestDriver.Drivers;
Expand All @@ -23,34 +22,34 @@ public ButtonDriverTest(Node testScene) : base(testScene)
}

[Test]
public async Task ClickingWorks()
public void ClickingWorks()
{
// WHEN
// i click the button
await _button.ClickCenter();
_button.ClickCenter();
// the label text changes.
_label.Text.ShouldBe("did work");
// and the panel disappears
_panel.IsVisible.ShouldBeFalse();
}

[Test]
public async Task ClickingDisabledButtonThrowsException()
public void ClickingDisabledButtonThrowsException()
{
// SETUP
_button.PresentRoot.Disabled = true;
// WHEN
// i click the button then an exception is thrown
await Should.ThrowAsync<InvalidOperationException>(async () => await _button.ClickCenter());
Should.Throw<InvalidOperationException>(() => _button.ClickCenter());
}

[Test]
public async Task ClickingHiddenButtonThrowsException()
public void ClickingHiddenButtonThrowsException()
{
// SETUP
_button.PresentRoot.Visible = false;
// WHEN
// i click the button then an exception is thrown
await Should.ThrowAsync<InvalidOperationException>(async () => await _button.ClickCenter());
Should.Throw<InvalidOperationException>(() => _button.ClickCenter());
}
}
8 changes: 5 additions & 3 deletions GodotTestDriver.Tests/Camera2DDriverTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

namespace Chickensoft.GodotTestDriver.Tests;

using System.Threading.Tasks;
Expand Down Expand Up @@ -37,10 +36,13 @@ public async Task Camera2DCanMove()
{
// WHEN
// I move camera to off-center sprite
await _camera2D.MoveIntoView(_offCenterSprite.GlobalPosition, 2);
var moveTask = _camera2D.MoveIntoView(_offCenterSprite.GlobalPosition, 2);

// THEN
// the off center sprite is not visible
// the camera has successfully moved to the new position
(await moveTask).ShouldBeTrue();

// and off center sprite is visible
_offCenterSprite.IsFullyInView.ShouldBeTrue();
}
}
7 changes: 3 additions & 4 deletions GodotTestDriver.Tests/CheckBoxDriverTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace Chickensoft.GodotTestDriver.Tests;

using System.Threading.Tasks;
using Chickensoft.GoDotTest;
using Godot;
using GodotTestDriver.Drivers;
Expand All @@ -18,19 +17,19 @@ public CheckBoxDriverTest(Node testScene) : base(testScene)
}

[Test]
public async Task ClickingChecksAndUnchecks()
public void ClickingChecksAndUnchecks()
{
// WHEN
// i click the checkbox
await _checkBox.ClickCenter();
_checkBox.ClickCenter();

// THEN
// the checkbox is checked
_checkBox.IsChecked.ShouldBeTrue();

// WHEN
// i click the checkbox again
await _checkBox.ClickCenter();
_checkBox.ClickCenter();

// THEN
// the checkbox is unchecked
Expand Down
21 changes: 10 additions & 11 deletions GodotTestDriver.Tests/GraphEditDriverTest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace Chickensoft.GodotTestDriver.Tests;
namespace Chickensoft.GodotTestDriver.Tests;

using System.Linq;
using System.Threading.Tasks;
using Chickensoft.GoDotTest;
using Godot;
using GodotTestDriver.Drivers;
Expand Down Expand Up @@ -51,15 +50,15 @@ public void InspectionWorks()
}

[Test]
public async Task DraggingNodesWorks()
public void DraggingNodesWorks()
{
// SETUP
var firstNode = _graphEdit.Nodes.First();

var firstNodeOffset = firstNode.Offset;
// WHEN
// i drag the first node
await firstNode.DragByOwnSize(2, 0);
firstNode.DragByOwnSize(2, 0);

// THEN
// the offset has changed by 2x it's width
Expand All @@ -71,50 +70,50 @@ public async Task DraggingNodesWorks()
}

[Test]
public async Task DraggingConnectionsWorks()
public void DraggingConnectionsWorks()
{
// SETUP
var firstNode = _graphEdit.Nodes.First();
var secondNode = _graphEdit.Nodes.Last();

// WHEN
// i drag a connection from the first node to the second node
await firstNode.DragConnection(Port.Output(0), secondNode, Port.Input(0));
firstNode.DragConnection(Port.Output(0), secondNode, Port.Input(0));

// THEN
// the connection works
_graphEdit.HasConnection(firstNode, Port.Output(0), secondNode, Port.Input(0)).ShouldBeTrue();
}

[Test]
public async Task DraggingConnectionsInReverseWorks()
public void DraggingConnectionsInReverseWorks()
{
// SETUP
var firstNode = _graphEdit.Nodes.First();
var secondNode = _graphEdit.Nodes.Last();

// WHEN
// i drag a connection from the second node to the first node
await secondNode.DragConnection(Port.Input(0), firstNode, Port.Output(0));
secondNode.DragConnection(Port.Input(0), firstNode, Port.Output(0));

// THEN
// the connection works
_graphEdit.HasConnection(firstNode, Port.Output(0), secondNode, Port.Input(0)).ShouldBeTrue();
}

[Test]
public async Task DisconnectingWorks()
public void DisconnectingWorks()
{
// SETUP
var firstNode = _graphEdit.Nodes.First();
var secondNode = _graphEdit.Nodes.Last();

// make a connection
await firstNode.DragConnection(Port.Output(0), secondNode, Port.Input(0));
firstNode.DragConnection(Port.Output(0), secondNode, Port.Input(0));

// WHEN
// i disconnect the connection, by dragging it from the second node to the first node
await secondNode.DragConnection(Port.Input(0), firstNode, Port.Output(0));
secondNode.DragConnection(Port.Input(0), firstNode, Port.Output(0));

// THEN
// the connection is gone
Expand Down
Loading

0 comments on commit d10266f

Please sign in to comment.