Simulate using mouse input doesn't working

I have a simple script to simulate left button mouse input like this:

            _input.Move(Mouse.current.position, eventButton.GetComponent<RectTransform>().RectTransformToScreenSpace().position, queueEventOnly: true);
            InputSystem.Update();

            Debug.Log("Mouse pos room test: " + Mouse.current.position.ReadValue());

            _input.Click(Mouse.current.leftButton);
            InputSystem.Update();

but for unknown reason the button won’t get clicked. Is there anything that I missed in that script?

Hey @Kalita2127 !

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.: InputSystem/Packages/com.unity.inputsystem/Tests/TestFixture/InputTestFixture.cs at develop · Unity-Technologies/InputSystem · GitHub

[UnityTest]
    public IEnumerator NewTestScriptWithEnumeratorPasses()
    {
        var pressAction = new InputAction("Press", binding: "<Mouse>/leftButton");
        pressAction.Enable();

        using (var trace = new InputActionTrace(pressAction))
        {
            Click(_mouse.leftButton, time: 0.2);
            InputSystem.Update();

            Assert.That(
                trace,
                Started(pressAction, _mouse.leftButton, 1, time: 0.2)
                    .AndThen(Performed(pressAction, _mouse.leftButton, 1, time: 0.2))
                    .AndThen(Canceled(pressAction, _mouse.leftButton, 0, time: 0.2))
            );
            trace.Clear();
        }

        yield return null;
    }

But I don’t think this is the way to test behavior in your game.

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