Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Simulate using mouse input doesn't working

Discussion in 'Testing & Automation' started by Kalita2127, Jan 10, 2023.

  1. Kalita2127

    Kalita2127

    Joined:
    Dec 6, 2014
    Posts:
    265
    I have a simple script to simulate left button mouse input like this:
    Code (CSharp):
    1.  
    2.             _input.Move(Mouse.current.position, eventButton.GetComponent<RectTransform>().RectTransformToScreenSpace().position, queueEventOnly: true);
    3.             InputSystem.Update();
    4.  
    5.             Debug.Log("Mouse pos room test: " + Mouse.current.position.ReadValue());
    6.  
    7.             _input.Click(Mouse.current.leftButton);
    8.             InputSystem.Update();
    but for unknown reason the button won't get clicked. Is there anything that I missed in that script?
     
  2. noeddie

    noeddie

    Joined:
    Jul 15, 2017
    Posts:
    3
    Hey @Ali_Akbar_Montazeri !

    Could you give more information about what are you trying to test?

    I have been studying Integration tests in Unity in the last 2 weeks, maybe I can help you to setup a better workflow.

    But for your question:

    Click 
    method call under the hood the
    PressAndRelease 
    method and probably you will need to use InputActionTrace to check if the mouse click was executed:

    ref.: https://github.com/Unity-Technologi...sts/TestFixture/InputTestFixture.cs#L459-L462

    Code (CSharp):
    1. [UnityTest]
    2.     public IEnumerator NewTestScriptWithEnumeratorPasses()
    3.     {
    4.         var pressAction = new InputAction("Press", binding: "<Mouse>/leftButton");
    5.         pressAction.Enable();
    6.  
    7.         using (var trace = new InputActionTrace(pressAction))
    8.         {
    9.             Click(_mouse.leftButton, time: 0.2);
    10.             InputSystem.Update();
    11.  
    12.             Assert.That(
    13.                 trace,
    14.                 Started(pressAction, _mouse.leftButton, 1, time: 0.2)
    15.                     .AndThen(Performed(pressAction, _mouse.leftButton, 1, time: 0.2))
    16.                     .AndThen(Canceled(pressAction, _mouse.leftButton, 0, time: 0.2))
    17.             );
    18.             trace.Clear();
    19.         }
    20.  
    21.         yield return null;
    22.     }
    But I don't think this is the way to test behavior in your game.
     
  3. Kalita2127

    Kalita2127

    Joined:
    Dec 6, 2014
    Posts:
    265
    It's kinda sucks that the button won't get clicked that way. So I've discussed with my team and found that
    ExecuteEvents.Execute
    is what we needed. It really does the click on the button