Testing a left mouse button down at a certain position

Hi,

I’m currently writing a Unit Test where I have this action:


It’s just a Vector2Control with a Composite with One Modifier, where the Modifier is the left mouse button and the binding is the mouse position.

Then, I’ve the following test for that:

public class InputReaderSOTests : InputTestFixture
{
  [Test]
  public void DoesExecuteClickEventWhenEnabled()
  {
    var mouse = InputSystem.AddDevice<Mouse>();
     
    var inputReader = ScriptableObject.CreateInstance<InputReaderSO>();
    inputReader.EnablePlayerControls();

    var clickedVector = Vector2.zero;
    inputReader.Click += position => clickedVector = position;
     
    // Works
    /*InputSystem.QueueStateEvent(mouse, new MouseState() { position = new Vector2(1337, 1337)}.WithButton(MouseButton.Left ));
    InputSystem.Update();*/
     
    // Does not work
    /*Set(mouse.position, new Vector2(1337, 1337), queueEventOnly: false);
    Click(mouse.leftButton);*/
     
    clickedVector.Should().Be(new Vector2(1337, 1337));
  }
}

In the documentation there’s a entry about using Set/Click/Press etc. for setting the state.

As you can see in the example, I set the mouse.position and then I click the left mouse button. However, the test reports that the mouse was not clicked on that position.

However, if I use the other method above using InputSystem.QueueStateEvent and InputSystem.Update it works as expected.

I feel that it also should work with set Set and Click methods. Any idea what I’m missing?

Thanks!

Dang. Some minutes later I found it. :slight_smile:

Instead of using Set, I have to use Move:

Move(mouse.position, new Vector2(1337, 1337), queueEventOnly: true);
Click(mouse.leftButton);

Then it works as expected, nice!

are you using this in a EditTest? How to move a mouse without Screen? :slight_smile: