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
1 change: 1 addition & 0 deletions Assets/Tests/Editor/StaticFacadeStateGuardTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public sealed class StaticFacadeStateGuardTests
"Packages/src/Editor/Infrastructure/SkillSetup/ToolSkillSynchronizer.cs",
"Packages/src/Editor/Presentation/Setup/SetupWizardWindow.cs",
"Packages/src/Editor/FirstPartyTools/SimulateMouseInput/SimulateMouseInputUseCase.cs",
"Packages/src/Editor/FirstPartyTools/SimulateMouseInput/MouseInputMainThreadCleanup.cs",
"Packages/src/Editor/FirstPartyTools/SimulateKeyboard/SimulateKeyboardUseCase.cs"
};

Expand Down
110 changes: 110 additions & 0 deletions Assets/Tests/PlayMode/MouseInputSimulationResponseFactoryTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#if ULOOP_HAS_INPUT_SYSTEM
#nullable enable
using System;
using NUnit.Framework;
using UnityEngine;

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 mouse input response construction before factory extraction.
/// </summary>
[TestFixture]
public sealed class MouseInputSimulationResponseFactoryTests
{
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 button responses project every Pause Point hit and preserve button coordinates.
/// </summary>
[Test]
public void InterruptedButtonResult_WithMultiplePausePointHits_MapsPauseEvidenceAndButtonPosition()
{
UloopPausePointRegistry.Enable("first-hit", 30);
UloopPausePointRegistry.Enable("latest-hit", 30);
UloopPausePoint.Pause("first-hit");
UloopPausePoint.Pause("latest-hit");
Vector2 inputPosition = new(10.25f, 20.75f);

SimulateMouseInputResponse response = MouseInputSimulationResponseFactory.InterruptedButtonResult(
UnityCliLoopMouseInputAction.Click,
"Left",
inputPosition);

Assert.That(response.Success, Is.True);
Assert.That(
response.Message,
Is.EqualTo(
"Mouse input stopped because Unity paused during Pause Point inspection. Unity CLI Loop released its held input bookkeeping."));
Assert.That(response.Action, Is.EqualTo(UnityCliLoopMouseInputAction.Click.ToString()));
Assert.That(response.Button, Is.EqualTo("Left"));
Assert.That(response.PositionX, Is.EqualTo(inputPosition.x));
Assert.That(response.PositionY, Is.EqualTo(inputPosition.y));
Assert.That(response.InterruptedByPausePoint, Is.True);
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 timed-out button responses preserve the action, button, coordinates, and timeout message.
/// </summary>
[Test]
public void TimedOutButtonResult_WithButtonAndPosition_MapsFailureResponse()
{
Vector2 inputPosition = new(30.5f, 40.25f);

SimulateMouseInputResponse response = MouseInputSimulationResponseFactory.TimedOutButtonResult(
UnityCliLoopMouseInputAction.LongPress,
"Right",
inputPosition);

Assert.That(response.Success, Is.False);
Assert.That(
response.Message,
Is.EqualTo(
"Mouse input timed out while waiting for Unity Editor update. Cleanup is queued for the next Editor tick."));
Assert.That(response.Action, Is.EqualTo(UnityCliLoopMouseInputAction.LongPress.ToString()));
Assert.That(response.Button, Is.EqualTo("Right"));
Assert.That(response.PositionX, Is.EqualTo(inputPosition.x));
Assert.That(response.PositionY, Is.EqualTo(inputPosition.y));
Assert.That(response.InterruptedByPausePoint, Is.False);
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,141 @@
#if ULOOP_HAS_INPUT_SYSTEM
#nullable enable
using System.Threading;
using System.Threading.Tasks;
using UnityEditor;
using UnityEngine;
using UnityEngine.InputSystem;

using RuntimeMouseButton = io.github.hatayama.UnityCliLoop.Runtime.MouseButton;
using io.github.hatayama.UnityCliLoop.Runtime;
using io.github.hatayama.UnityCliLoop.ToolContracts;

namespace io.github.hatayama.UnityCliLoop.FirstPartyTools
{
/// <summary>
/// Restores mouse device and overlay state on the Unity main thread after simulation ends.
/// </summary>
internal static class MouseInputMainThreadCleanup
{
internal static async Task<InputSimulationWaitOutcome> ReleaseButtonIfPossible(
Mouse mouse,
RuntimeMouseButton button,
CancellationToken ct)
{
await InputSystemUpdateHelper.SwitchToMainThreadIfNeeded(ct);
if (!CanInjectMouseState(mouse))
{
return InputSimulationWaitOutcome.Completed;
}

if (EditorApplication.isPaused)
{
ReleaseButtonImmediately(mouse, button);
return InputSimulationWaitOutcome.Completed;
}

InputSimulationWaitOutcome releaseOutcome = await InputSystemUpdateHelper.ApplyOnNextConfiguredUpdate(
() => MouseInputState.SetButtonState(mouse, button, false),
ct).ConfigureAwait(false);
if (releaseOutcome == InputSimulationWaitOutcome.TimedOut)
{
ScheduleReleaseButtonImmediately(mouse, button);
}

return releaseOutcome;
}

private static void ScheduleReleaseButtonImmediately(Mouse mouse, RuntimeMouseButton button)
{
ReleaseButtonImmediatelyOnMainThreadAsync(mouse, button, CancellationToken.None).Forget();
}

private static async Task ReleaseButtonImmediatelyOnMainThreadAsync(
Mouse mouse,
RuntimeMouseButton button,
CancellationToken ct)
{
await InputSystemUpdateHelper.SwitchToMainThreadIfNeeded(ct);
ReleaseButtonImmediately(mouse, button);
}

private static void ReleaseButtonImmediately(Mouse mouse, RuntimeMouseButton button)
{
Debug.Assert(CanInjectMouseState(mouse), "mouse button can only be released while PlayMode has a mouse");
if (!CanInjectMouseState(mouse))
{
return;
}

MouseInputState.SetButtonState(mouse, button, false);
InputSystemUpdateHelper.RunExplicitUpdate(InputUpdateTypeResolver.Resolve());
}

internal static void ResetDeltaIfPossible(Mouse mouse)
{
if (!CanInjectMouseState(mouse))
{
return;
}

MouseInputState.InjectDelta(mouse, Vector2.zero);
if (EditorApplication.isPaused)
{
InputSystemUpdateHelper.RunExplicitUpdate(InputUpdateTypeResolver.Resolve());
}
}

internal static void ScheduleTimedOutButtonCleanup(
Mouse mouse,
RuntimeMouseButton button,
bool pressWasApplied)
{
CleanupTimedOutButtonAsync(mouse, button, pressWasApplied, CancellationToken.None).Forget();
}

private static async Task CleanupTimedOutButtonAsync(
Mouse mouse,
RuntimeMouseButton button,
bool pressWasApplied,
CancellationToken ct)
{
await InputSystemUpdateHelper.SwitchToMainThreadIfNeeded(ct);
if (pressWasApplied)
{
await ReleaseButtonIfPossible(mouse, button, ct).ConfigureAwait(false);
}

MouseInputState.SetButtonUp(button);
SimulateMouseInputOverlayState.SetButtonHeld(button, false);
}

internal static void ScheduleTimedOutMouseOverlayCleanup()
{
CleanupTimedOutMouseOverlayAsync(CancellationToken.None).Forget();
}

private static async Task CleanupTimedOutMouseOverlayAsync(CancellationToken ct)
{
await InputSystemUpdateHelper.SwitchToMainThreadIfNeeded(ct);
SimulateMouseInputOverlayState.Clear();
}

internal static void ScheduleTimedOutDeltaCleanup(Mouse mouse)
{
CleanupTimedOutDeltaAsync(mouse, CancellationToken.None).Forget();
}

private static async Task CleanupTimedOutDeltaAsync(Mouse mouse, CancellationToken ct)
{
await InputSystemUpdateHelper.SwitchToMainThreadIfNeeded(ct);
ResetDeltaIfPossible(mouse);
SimulateMouseInputOverlayState.Clear();
}

private static bool CanInjectMouseState(Mouse mouse)
{
return EditorApplication.isPlaying && mouse != null;
}
}
}
#endif

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

Loading
Loading