Skip to content
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
3 changes: 2 additions & 1 deletion Assets/Tests/Editor/StaticFacadeStateGuardTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ public sealed class StaticFacadeStateGuardTests
"Packages/src/Editor/FirstPartyTools/SimulateMouseInput/MouseInputMainThreadCleanup.cs",
"Packages/src/Editor/FirstPartyTools/SimulateMouseInput/MouseInputPressActionExecutor.cs",
"Packages/src/Editor/FirstPartyTools/SimulateMouseInput/MouseInputMotionActionExecutor.cs",
"Packages/src/Editor/FirstPartyTools/SimulateKeyboard/SimulateKeyboardUseCase.cs"
"Packages/src/Editor/FirstPartyTools/SimulateKeyboard/SimulateKeyboardUseCase.cs",
"Packages/src/Editor/FirstPartyTools/SimulateKeyboard/KeyboardInputMainThreadCleanup.cs"
};

private static readonly Dictionary<string, string[]> OverloadGuardMethodsByPath = new Dictionary<string, string[]>
Expand Down
130 changes: 130 additions & 0 deletions Assets/Tests/PlayMode/KeyboardInputSimulationResponseFactoryTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#if ULOOP_HAS_INPUT_SYSTEM
#nullable enable
using System;
using NUnit.Framework;

using io.github.hatayama.UnityCliLoop.FirstPartyTools;
using io.github.hatayama.UnityCliLoop.Runtime;
using io.github.hatayama.UnityCliLoop.ToolContracts;

namespace io.github.hatayama.UnityCliLoop.Tests.PlayMode
{
/// <summary>
/// Characterizes wire-visible keyboard input response construction before factory extraction.
/// </summary>
[TestFixture]
public sealed class KeyboardInputSimulationResponseFactoryTests
{
private readonly DateTime _nowUtc = new(2026, 7, 9, 0, 0, 0, DateTimeKind.Utc);

[SetUp]
public void SetUp()
{
UloopPausePointRegistry.ConfigureForTests(new FakePauseController(), () => _nowUtc);
}

[TearDown]
public void TearDown()
{
UloopPausePointRegistry.ResetForTests();
}

/// <summary>
/// Verifies interrupted press responses preserve edge observations and project every Pause Point hit.
/// </summary>
[TestCase(true)]
[TestCase(false)]
public void InterruptedKeyResult_WithMultiplePausePointHits_MapsPauseEvidenceAndPressEdge(
bool pressEdgeObserved)
{
UloopPausePointRegistry.Enable("first-hit", 30);
UloopPausePointRegistry.Enable("latest-hit", 30);
UloopPausePoint.Pause("first-hit");
UloopPausePoint.Pause("latest-hit");

SimulateKeyboardResponse response = KeyboardInputSimulationResponseFactory.InterruptedKeyResult(
UnityCliLoopKeyboardAction.Press,
"Space",
pressEdgeObserved);

Assert.That(response.Success, Is.True);
Assert.That(
response.Message,
Is.EqualTo(
"Keyboard input stopped because Unity paused during Pause Point inspection. Key 'Space' was released from Unity CLI Loop bookkeeping."));
Assert.That(response.Action, Is.EqualTo(UnityCliLoopKeyboardAction.Press.ToString()));
Assert.That(response.KeyName, Is.EqualTo("Space"));
Assert.That(response.InterruptedByPausePoint, Is.True);
Assert.That(response.PressEdgeObserved, Is.EqualTo(pressEdgeObserved));
Assert.That(response.PausePointId, Is.EqualTo("latest-hit"));
Assert.That(response.PausePointHitCount, Is.EqualTo(1));
Assert.That(response.PausePointHits, Has.Count.EqualTo(2));
Assert.That(response.PausePointHits![0].Id, Is.EqualTo("first-hit"));
Assert.That(response.PausePointHits[1].Id, Is.EqualTo("latest-hit"));
}

/// <summary>
/// Verifies interrupted key-up responses preserve the absent press edge and project a single Pause Point hit.
/// </summary>
[Test]
public void InterruptedKeyResult_ForKeyUp_MapsNullPressEdgeAndSinglePausePointHit()
{
UloopPausePointRegistry.Enable("key-up-hit", 30);
UloopPausePoint.Pause("key-up-hit");

SimulateKeyboardResponse response = KeyboardInputSimulationResponseFactory.InterruptedKeyResult(
UnityCliLoopKeyboardAction.KeyUp,
"Enter",
null);

Assert.That(response.Success, Is.True);
Assert.That(response.Action, Is.EqualTo(UnityCliLoopKeyboardAction.KeyUp.ToString()));
Assert.That(response.KeyName, Is.EqualTo("Enter"));
Assert.That(response.InterruptedByPausePoint, Is.True);
Assert.That(response.PressEdgeObserved, Is.Null);
Assert.That(response.PausePointId, Is.EqualTo("key-up-hit"));
Assert.That(response.PausePointHitCount, Is.EqualTo(1));
Assert.That(response.PausePointHits, Has.Count.EqualTo(1));
Assert.That(response.PausePointHits![0].Id, Is.EqualTo("key-up-hit"));
}

/// <summary>
/// Verifies timed-out key responses preserve the action, key name, and timeout failure fields.
/// </summary>
[Test]
public void TimedOutKeyResult_WithActionAndKeyName_MapsFailureResponse()
{
SimulateKeyboardResponse response = KeyboardInputSimulationResponseFactory.TimedOutKeyResult(
UnityCliLoopKeyboardAction.KeyDown,
"W");

Assert.That(response.Success, Is.False);
Assert.That(
response.Message,
Is.EqualTo(
"Keyboard input timed out while waiting for Unity Editor update. Key 'W' cleanup is queued for the next Editor tick."));
Assert.That(response.Action, Is.EqualTo(UnityCliLoopKeyboardAction.KeyDown.ToString()));
Assert.That(response.KeyName, Is.EqualTo("W"));
Assert.That(response.InterruptedByPausePoint, Is.False);
Assert.That(response.PressEdgeObserved, Is.Null);
Assert.That(response.PausePointId, Is.Null);
Assert.That(response.PausePointHitCount, Is.Null);
Assert.That(response.PausePointHits, Is.Null);
}

/// <summary>
/// Test double that records Pause Point state without pausing the Unity Editor.
/// </summary>
private sealed class FakePauseController : IUloopPausePointPauseController
{
public bool IsPlaying => true;
public bool IsPaused { get; private set; }

public void Pause()
{
IsPaused = true;
}
}
}
}
#endif

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
#if ULOOP_HAS_INPUT_SYSTEM
#nullable enable
using System.Threading;
using System.Threading.Tasks;
using UnityEditor;
using UnityEngine;
using UnityEngine.InputSystem;

using io.github.hatayama.UnityCliLoop.Runtime;
using io.github.hatayama.UnityCliLoop.ToolContracts;

namespace io.github.hatayama.UnityCliLoop.FirstPartyTools
{
/// <summary>
/// Restores keyboard device and overlay state on the Unity main thread after simulation ends.
/// </summary>
internal static class KeyboardInputMainThreadCleanup
{
internal static async Task FinalizePressOverlay(CancellationToken ct)
{
await InputSystemUpdateHelper.SwitchToMainThreadIfNeeded(CancellationToken.None);
if (ct.IsCancellationRequested)
{
SimulateKeyboardOverlayState.ClearPress();
return;
}

SimulateKeyboardOverlayState.ReleasePress();
await EditorFrameWaiter.WaitFramesOrTimeoutAsync(
1,
UnityCliLoopConstants.EDITOR_FRAME_WAIT_TIMEOUT_MS,
CancellationToken.None).ConfigureAwait(false);
}

internal static async Task<InputSimulationWaitOutcome> RollbackHeldKey(
Keyboard keyboard,
Key key,
string keyName,
CancellationToken ct)
{
await InputSystemUpdateHelper.SwitchToMainThreadIfNeeded(ct);
InputSimulationWaitOutcome releaseOutcome =
await ReleaseKeyStateIfPossible(keyboard, key, ct).ConfigureAwait(false);
if (releaseOutcome == InputSimulationWaitOutcome.TimedOut)
{
ScheduleTimedOutHeldKeyCleanup(keyboard, key, keyName, false);
return releaseOutcome;
}

await InputSystemUpdateHelper.SwitchToMainThreadIfNeeded(ct);
KeyboardKeyState.SetKeyUp(key);
SimulateKeyboardOverlayState.RemoveHeldKey(keyName);
return releaseOutcome;
}

internal static async Task<InputSimulationWaitOutcome> ReleaseKeyStateIfPossible(
Keyboard keyboard,
Key key,
CancellationToken ct)
{
await InputSystemUpdateHelper.SwitchToMainThreadIfNeeded(ct);
if (!CanInjectKeyboardState(keyboard))
{
return InputSimulationWaitOutcome.Completed;
}

if (EditorApplication.isPaused)
{
ReleaseKeyStateImmediately(keyboard, key);
return InputSimulationWaitOutcome.Completed;
}

InputSimulationWaitOutcome releaseOutcome = await InputSystemUpdateHelper.ApplyOnNextConfiguredUpdate(
() => KeyboardKeyState.SetKeyState(keyboard, key, false),
ct).ConfigureAwait(false);
if (releaseOutcome == InputSimulationWaitOutcome.TimedOut)
{
ScheduleReleaseKeyStateImmediately(keyboard, key);
}

return releaseOutcome;
}

private static void ScheduleReleaseKeyStateImmediately(Keyboard keyboard, Key key)
{
ReleaseKeyStateImmediatelyOnMainThreadAsync(keyboard, key, CancellationToken.None).Forget();
}

private static async Task ReleaseKeyStateImmediatelyOnMainThreadAsync(
Keyboard keyboard,
Key key,
CancellationToken ct)
{
await InputSystemUpdateHelper.SwitchToMainThreadIfNeeded(ct);
ReleaseKeyStateImmediately(keyboard, key);
}

private static void ReleaseKeyStateImmediately(Keyboard keyboard, Key key)
{
Debug.Assert(CanInjectKeyboardState(keyboard), "keyboard state can only be released while PlayMode has a keyboard");
if (!CanInjectKeyboardState(keyboard))
{
return;
}

KeyboardKeyState.SetKeyState(keyboard, key, false);
InputSystemUpdateHelper.RunExplicitUpdate(InputUpdateTypeResolver.Resolve());
}

internal static void ScheduleTimedOutPressCleanup(Keyboard keyboard, Key key, bool pressWasApplied)
{
CleanupTimedOutPressAsync(keyboard, key, pressWasApplied, CancellationToken.None).Forget();
}

private static async Task CleanupTimedOutPressAsync(
Keyboard keyboard,
Key key,
bool pressWasApplied,
CancellationToken ct)
{
await InputSystemUpdateHelper.SwitchToMainThreadIfNeeded(ct);
if (pressWasApplied)
{
await ReleaseKeyStateIfPossible(keyboard, key, ct).ConfigureAwait(false);
}

KeyboardKeyState.UnregisterTransientKey(key);
SimulateKeyboardOverlayState.ClearPress();
}

internal static void ScheduleTimedOutHeldKeyCleanup(
Keyboard keyboard,
Key key,
string keyName,
bool keyWasApplied)
{
CleanupTimedOutHeldKeyAsync(keyboard, key, keyName, keyWasApplied, CancellationToken.None).Forget();
}

private static async Task CleanupTimedOutHeldKeyAsync(
Keyboard keyboard,
Key key,
string keyName,
bool keyWasApplied,
CancellationToken ct)
{
await InputSystemUpdateHelper.SwitchToMainThreadIfNeeded(ct);
if (keyWasApplied)
{
await ReleaseKeyStateIfPossible(keyboard, key, ct).ConfigureAwait(false);
}

KeyboardKeyState.SetKeyUp(key);
SimulateKeyboardOverlayState.RemoveHeldKey(keyName);
}

private static bool CanInjectKeyboardState(Keyboard keyboard)
{
return EditorApplication.isPlaying && keyboard != null;
}
}
}
#endif

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading