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. Dismiss Notice

Resolved Testing a left mouse button down at a certain position

Discussion in 'Input System' started by ManuelRauber, May 8, 2022.

  1. ManuelRauber

    ManuelRauber

    Joined:
    Apr 3, 2015
    Posts:
    114
    Hi,

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

    upload_2022-5-8_13-56-53.png
    upload_2022-5-8_13-57-9.png

    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:

    Code (CSharp):
    1. public class InputReaderSOTests : InputTestFixture
    2. {
    3.   [Test]
    4.   public void DoesExecuteClickEventWhenEnabled()
    5.   {
    6.     var mouse = InputSystem.AddDevice<Mouse>();
    7.      
    8.     var inputReader = ScriptableObject.CreateInstance<InputReaderSO>();
    9.     inputReader.EnablePlayerControls();
    10.  
    11.     var clickedVector = Vector2.zero;
    12.     inputReader.Click += position => clickedVector = position;
    13.      
    14.     // Works
    15.     /*InputSystem.QueueStateEvent(mouse, new MouseState() { position = new Vector2(1337, 1337)}.WithButton(MouseButton.Left ));
    16.     InputSystem.Update();*/
    17.      
    18.     // Does not work
    19.     /*Set(mouse.position, new Vector2(1337, 1337), queueEventOnly: false);
    20.     Click(mouse.leftButton);*/
    21.      
    22.     clickedVector.Should().Be(new Vector2(1337, 1337));
    23.   }
    24. }
    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!
     

    Attached Files:

  2. ManuelRauber

    ManuelRauber

    Joined:
    Apr 3, 2015
    Posts:
    114
    Dang. Some minutes later I found it. :)

    Instead of using Set, I have to use Move:

    Code (CSharp):
    1. Move(mouse.position, new Vector2(1337, 1337), queueEventOnly: true);
    2. Click(mouse.leftButton);
    Then it works as expected, nice!
     
  3. stephyswe

    stephyswe

    Joined:
    Aug 23, 2017
    Posts:
    2
    are you using this in a EditTest? How to move a mouse without Screen? :)