Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question New Input System doesn't work

Discussion in 'Testing & Automation' started by Kalita2127, Dec 21, 2022.

  1. Kalita2127

    Kalita2127

    Joined:
    Dec 6, 2014
    Posts:
    279
    I tried to do simple input testing with
    InputTestFixture
    like in this doc but it seems like it's not working. Here is my code:
    Code (CSharp):
    1.  
    2.             var keyboard = InputSystem.AddDevice<Keyboard>();
    3.  
    4.             var shiftAction = new InputAction("apaya", binding: "<Keyboard>/shift", interactions: "Hold");
    5.             shiftAction.Enable();
    6.  
    7.             _input.Press(keyboard.shiftKey);
    8.  
    9.             using (var trace = new InputActionTrace())
    10.             {
    11.                 trace.SubscribeTo(shiftAction);
    12.                 _input.currentTime = 0.1f;
    13.  
    14.                 var actions = trace.ToArray();
    15.  
    16.                 Debug.Log("kon" + actions.Length); // returns 0
    17.             }
    18.  
    19.             float timeOut = 0f;
    20.             while (!keyboard.shiftKey.isPressed && timeOut < 4f)
    21.             {
    22.                 yield return null;
    23.                 timeOut += Time.deltaTime;
    24.             }
    25.             Assert.IsTrue(keyboard.shiftKey.isPressed); // this returns false
    26.  
    did I do something wrong?
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,533
    Seems like not working => means what exactly?
     
  3. Kalita2127

    Kalita2127

    Joined:
    Dec 6, 2014
    Posts:
    279
    I have no idea what's behind the Press function but I'm expecting the
    var actions
    length to be 1 or
    isPressed
    to be true
     
  4. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,367
    Can you check if your "keyboard" reference is identical to the value of
    Keyboard.current
    ? I've never had to "add" a keyboard, and I think you're adding a second one which is likely not in use.
     
    Kalita2127 likes this.
  5. Kalita2127

    Kalita2127

    Joined:
    Dec 6, 2014
    Posts:
    279
    I did
    Debug.Log($"compare: {keyboard.deviceId} with: {Keyboard.current.deviceId}");
    and it's just the same.
    Anyway what's the use of
    currentTime
    ?
     
    Last edited: Dec 21, 2022
  6. Kalita2127

    Kalita2127

    Joined:
    Dec 6, 2014
    Posts:
    279
    I think I accidentally solved the issue and I have no idea why. This script works.
    Code (CSharp):
    1.  
    2.         var keyboard = InputSystem.AddDevice<Keyboard>();
    3.         var action1 = new InputAction("action1", binding: "<Keyboard>/shift", interactions: "Hold");
    4.  
    5.         action1.Enable();
    6.         Press(keyboard.shiftKey);
    7.  
    8.         using (var trace = new InputActionTrace())
    9.         {
    10.             trace.SubscribeTo(action1);
    11.             //currentTime = 0.1f;
    12.  
    13.             action1.Disable();
    14.             var actions = trace.ToArray();
    15.  
    16.             Debug.Log("1. " + keyboard.shiftKey.isPressed); // returns true
    17.  
    18.             Assert.That(actions.Length, Is.EqualTo(1)); // returns 1
    19.         }
    Anyone can explain?
     
  7. Kalita2127

    Kalita2127

    Joined:
    Dec 6, 2014
    Posts:
    279
    So I found that it worked if I did
    Code (CSharp):
    1. [TestFixture]
    2. public class DummyTest : InputTestFixture
    but not if I instantiate it like
    Code (CSharp):
    1. [TestFixture]
    2. class MyTestFixture
    3. {
    4.     private InputTestFixture input = new InputTestFixture();
    5. }
     
    Last edited: Dec 21, 2022
  8. Kalita2127

    Kalita2127

    Joined:
    Dec 6, 2014
    Posts:
    279
    Still don't get it :confused:
     
  9. Kalita2127

    Kalita2127

    Joined:
    Dec 6, 2014
    Posts:
    279
    Another issue was if it's
    IEnumerator
    it doesn't work.

    Code (CSharp):
    1.  
    2.     [UnityTest]
    3.     public IEnumerator Keyboard_ShiftClicked()
    4.     {
    5.         var keyboard = InputSystem.AddDevice<Keyboard>();
    6.  
    7.         var action1 = new InputAction("action1", binding: "<Keyboard>/shift", interactions: "Hold");
    8.  
    9.         action1.Enable();
    10.         Press(keyboard.shiftKey);
    11.  
    12.  
    13.         using (var trace = new InputActionTrace())
    14.         {
    15.             trace.SubscribeTo(action1);
    16.             //currentTime = 0.1f;
    17.  
    18.             action1.Disable();
    19.             var actions = trace.ToArray();
    20.  
    21.             Debug.Log("1. " + keyboard.shiftKey.isPressed); // returns false
    22.  
    23.             Assert.That(actions.Length, Is.EqualTo(1)); // returns 0
    24.         }
    25.         yield return null;
    26.     }
    while this works
    Code (CSharp):
    1.     [Test]
    2.     public void Keyboard_ShiftClicked()
    3.     {
    4.         var keyboard = InputSystem.AddDevice<Keyboard>();
    5.  
    6.         var action1 = new InputAction("action1", binding: "<Keyboard>/shift", interactions: "Hold");
    7.  
    8.         action1.Enable();
    9.         Press(keyboard.shiftKey);
    10.  
    11.         using (var trace = new InputActionTrace())
    12.         {
    13.             trace.SubscribeTo(action1);
    14.             //currentTime = 0.1f;
    15.  
    16.             action1.Disable();
    17.             var actions = trace.ToArray();
    18.  
    19.             Debug.Log("1. " + keyboard.shiftKey.isPressed); // returns true
    20.  
    21.             Assert.That(actions.Length, Is.EqualTo(1)); // returns 1
    22.         }
    23.     }
     
  10. Kalita2127

    Kalita2127

    Joined:
    Dec 6, 2014
    Posts:
    279
    with async
    Code (CSharp):
    1.     [Test]
    2.     public async void Keyboard_ShiftClicked()
    3.     {
    4.         await UniTask.Delay(3000);
    5.         var keyboard = InputSystem.AddDevice<Keyboard>();
    6.  
    7.         var action1 = new InputAction("action1", binding: "<Keyboard>/shift", interactions: "Hold");
    8.  
    9.         action1.Enable();
    10.         Press(keyboard.shiftKey);
    11.  
    12.         using (var trace = new InputActionTrace())
    13.         {
    14.             trace.SubscribeTo(action1);
    15.             //currentTime = 0.1f;
    16.  
    17.             action1.Disable();
    18.             var actions = trace.ToArray();
    19.  
    20.             Debug.Log("1. " + keyboard.shiftKey.isPressed); // returns true
    21.  
    22.             Assert.That(actions.Length, Is.EqualTo(1)); // AssertionException: Expected: 1 But was:  0
    23.  
    24.         }
    25.  
    26.     }
    why this test has no solid result?
     
  11. ProtoTerminator

    ProtoTerminator

    Joined:
    Nov 19, 2013
    Posts:
    583
    async void
    is fire-and-forget, so the test runner will think the test is already complete after the first await. You need to use
    async Task
    for the test runner to wait for the entire async method to complete. (And you need to use experimental v2 to get that to work.)

    I don't know anything about your other questions, sorry.
     
  12. noeddie

    noeddie

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

    Make sure to call
    InputSystem.Update()
    after the
    Press(keyboard.shiftKey);
    command.

    Something like this:

    Code (CSharp):
    1. using System.Collections;
    2. using NUnit.Framework;
    3. using UnityEngine.InputSystem;
    4. using UnityEngine.TestTools;
    5.  
    6. public class NewTestScript : InputTestFixture
    7. {
    8.     Keyboard keyboard;
    9.  
    10.     [UnityTest]
    11.     public IEnumerator NewTestScriptWithEnumeratorPasses()
    12.     {
    13.         Press(keyboard.shiftKey);
    14.         InputSystem.Update();
    15.  
    16.         Assert.That(keyboard.shiftKey.isPressed, Is.True);
    17.  
    18.         yield return null;
    19.     }
    20.  
    21.     public override void Setup()
    22.     {
    23.         base.Setup();
    24.         keyboard = InputSystem.AddDevice<Keyboard>();
    25.     }
    26.  
    27.     public override void TearDown()
    28.     {
    29.         base.TearDown();
    30.     }
    31. }
    32.  
    If you would like to test the
    Hold
    interaction:

    Code (CSharp):
    1. [UnityTest]
    2.     public IEnumerator NewTestScript1WithEnumeratorPasses()
    3.     {
    4.         var action = new InputAction("test", binding: "<Keyboard>/shift", interactions: "Hold");
    5.         action.Enable();
    6.  
    7.         using (var trace = new InputActionTrace(action))
    8.         {
    9.             Press(keyboard.shiftKey);
    10.             InputSystem.Update();
    11.  
    12.             Assert.That(trace, Started<HoldInteraction>(action, keyboard.shiftKey));
    13.  
    14.             trace.Clear();
    15.         }
    16.  
    17.         yield return null;
    18.     }
     
    Last edited: Dec 30, 2022
  13. Kalita2127

    Kalita2127

    Joined:
    Dec 6, 2014
    Posts:
    279
    Thank you so much guys for your help! I got it working with the
    (Whatever_device).current
    combined with
    InputSystem.Update
    after each interactions.
    Anyway happy new years! :D