Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question How to automate mouse testing in UTF?

Discussion in 'Input System' started by jakesee, Oct 9, 2023.

  1. jakesee

    jakesee

    Joined:
    Jan 7, 2020
    Posts:
    81
    I am trying to simulate mouse down-drag-up in Unity Testing Framework, Play Mode Test, but the Mouse.current.delta seems to reset unexpectedly. I am using Input System 1.6.3.

    Code (CSharp):
    1.  
    2.  
    3.     public IEnumerator Tween(float duration, Action<float> action)
    4.     {
    5.         float timePassed = 0f;
    6.         while (timePassed < duration)
    7.         {
    8.             yield return new WaitForEndOfFrame();
    9.             timePassed += Time.deltaTime;
    10.             action(timePassed / duration);
    11.         }
    12.         action(1f);
    13.     }
    14.  
    15.     [UnityTest]
    16.     public IEnumerator CanSelectAndClearItems()
    17.     {
    18.         yield return new WaitWhile(() => !sceneLoaded);
    19.         Init();
    20.  
    21.        Vector2 destination = new Vector2(Screen.width, Screen.height);
    22.         Vector2 initialPos = Mouse.current.position.ReadValue();
    23.         yield return Wait.Tween(5f, (t) => {
    24.             Vector2 next = Vector2.Lerp(initialPos, destination, t);
    25.             Move(Mouse.current.position, next);
    26.             // assert passes as expected
    27.             Assert.Greater(Mouse.current.delta.ReadValue().sqrMagnitude, 0f, "mouse should be moving");
    28.         });
    29.  
    30.     }
    In my test scene, I have a Player:

    Code (CSharp):
    1. class Player : Monobehaviour {
    2. void Update() {
    3.  
    4.             if(Mouse.current.delta.ReadValue().sqrMagnitude > 0) {
    5.                 // this does not get called, because delta is always 0 for some reason
    6.                 Debug.Log($"Mouse Position {Mouse.current.position.ReadValue()}");
    7.             }
    8. }
    9. }
    I do not want to use the InputActions, if possible. The InputActions are unnecessarily convoluted to set up especially my use case limits to only mouse and keyboard.

    What is the expected behavior for Mouse.current.delta?

    Another thing I observe is that my physical mouse and keyboard seems to be active during the running of the Play Mode Tests, and hence I suspect it contributes to unexpected values during the simulation. Is it possible to disable my physical mouse and keyboard at the beginning of the TestCase and only use the simulated/programmatic control?

    EDIT: For now, I resolve the issue by manually tracking the delta. I would still like to know how delta is supposed to work properly.
     
    Last edited: Oct 9, 2023