File size: 4,806 Bytes
05c9ac2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#if MLA_INPUT_TESTS
using System;
using System.Linq;
using NUnit.Framework;
using Unity.MLAgents.Actuators;
using Unity.MLAgents.Extensions.Input;
using Unity.MLAgents.Policies;
using UnityEngine;
using UnityEngine.InputSystem;

namespace Unity.MLAgents.Extensions.Tests.Runtime.Input
{
    class TestProvider : MonoBehaviour, IInputActionAssetProvider
    {
        public InputActionAsset asset;
        public IInputActionCollection2 collection;

        public (InputActionAsset, IInputActionCollection2) GetInputActionAsset()
        {
            return (asset, collection);
        }
    }
    public class InputActuatorComponentTests : InputTestFixture
    {
        InputActionAsset m_Asset;
        GameObject m_GameObject;
        PlayerInput m_PlayerInput;
        BehaviorParameters m_BehaviorParameters;
        InputActuatorComponent m_ActuatorComponent;
        TestPushBlockActions m_Actions;
        TestProvider m_Provider;

        public override void Setup()
        {
            base.Setup();
            m_Actions = new TestPushBlockActions();
            m_Asset = m_Actions.asset;
            m_GameObject = new GameObject();
            m_PlayerInput = m_GameObject.AddComponent<PlayerInput>();
            m_Provider = m_GameObject.AddComponent<TestProvider>();
            m_Provider.asset = m_Asset;
            m_Provider.collection = m_Actions;
            m_ActuatorComponent = m_GameObject.AddComponent<InputActuatorComponent>();
            m_BehaviorParameters = m_GameObject.AddComponent<BehaviorParameters>();
            m_BehaviorParameters.BehaviorName = "InputTest";
            m_BehaviorParameters.BehaviorType = BehaviorType.Default;
        }

        public override void TearDown()
        {
            m_ActuatorComponent.CleanupActionAsset();
            var objects = GameObject.FindObjectsOfType<GameObject>();
            foreach (var o in objects)
            {
                UnityEngine.Object.DestroyImmediate(o);
            }
            base.TearDown();
        }

        [Test]
        public void InputActuatorComponentTestCreateActuators()
        {
            // Use the Assert class to test conditions.
            m_PlayerInput.actions = m_Asset;
            m_PlayerInput.defaultActionMap = m_Asset.actionMaps[0].name;
            var actuators = m_ActuatorComponent.CreateActuators();
            Assert.IsTrue(actuators.Length == 2);
            Assert.IsTrue(actuators[0].ActionSpec.Equals(ActionSpec.MakeContinuous(2)));
            Assert.IsTrue(actuators[1].ActionSpec.NumDiscreteActions == 1);

            var actuatorComponentActionSpec = m_ActuatorComponent.ActionSpec;
            Assert.IsTrue(actuatorComponentActionSpec.BranchSizes.SequenceEqual(new[] {2}));
            Assert.IsTrue(actuatorComponentActionSpec.NumContinuousActions == 2);
        }

        [Test]
        public void InputActuatorComponentTestGenerateActuatorsFromAsset()
        {
            // Use the Assert class to test conditions.
            m_PlayerInput.actions = m_Asset;
            m_PlayerInput.defaultActionMap = m_Asset.actionMaps[0].name;
            var inputActionMap = m_Asset.FindActionMap(m_PlayerInput.defaultActionMap);
            InputActuatorComponent.RegisterLayoutBuilder(
                inputActionMap,
                "TestLayout");

            var device = InputSystem.AddDevice("TestLayout");

            var actuators = InputActuatorComponent.CreateActuatorsFromMap(inputActionMap, m_BehaviorParameters, device, new InputActuatorEventContext());
            Assert.IsTrue(actuators.Length == 2);
            Assert.IsTrue(actuators[0].ActionSpec.Equals(ActionSpec.MakeContinuous(2)));
            Assert.IsTrue(actuators[1].ActionSpec.NumDiscreteActions == 1);
        }

        [Test]
        public void InputActuatorComponentTestCreateDevice()
        {
            // Use the Assert class to test conditions.
            m_PlayerInput.actions = m_Asset;
            m_PlayerInput.defaultActionMap = m_Asset.actionMaps[0].name;

            // need to call this to load the layout in the input system
            InputActuatorComponent.RegisterLayoutBuilder(
                m_Asset.FindActionMap(m_PlayerInput.defaultActionMap),
                "TestLayout");

            InputSystem.LoadLayout("TestLayout");
            var device = InputSystem.AddDevice("TestLayout");
            Assert.AreEqual("TestLayout", device.layout);
            Assert.IsTrue(device.children.Count == 2);
            Assert.AreEqual(device.children[0].path, $"{device.path}{InputControlPath.Separator}movement");
            Assert.AreEqual(device.children[1].path, $"{device.path}{InputControlPath.Separator}jump");
            Assert.NotNull(InputSystem.LoadLayout("TestLayout"));
        }
    }
}
#endif // MLA_INPUT_TESTS