File size: 2,771 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
#if MLA_INPUT_TESTS
using NUnit.Framework;
using Unity.Barracuda;
using Unity.MLAgents.Actuators;
using Unity.MLAgents.Extensions.Input;
using Unity.MLAgents.Policies;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.LowLevel;

namespace Unity.MLAgents.Extensions.Tests.Runtime.Input
{
    class TestAdaptor : IRLActionInputAdaptor
    {
        public bool eventWritten;
        public bool writtenToHeuristic;

        public ActionSpec GetActionSpecForInputAction(InputAction action)
        {
            return ActionSpec.MakeContinuous(1);
        }

        public void WriteToInputEventForAction(InputEventPtr eventPtr, InputAction action, InputControl control, ActionSpec actionSpec, in ActionBuffers actionBuffers)
        {
            eventWritten = true;
        }

        public void WriteToHeuristic(InputAction action, in ActionBuffers actionBuffers)
        {
            writtenToHeuristic = true;
        }

        public void Reset()
        {
            eventWritten = false;
            writtenToHeuristic = false;
        }
    }

    [TestFixture]
    public class InputActionActuatorTests
    {
        BehaviorParameters m_BehaviorParameters;
        InputActionActuator m_Actuator;
        TestAdaptor m_Adaptor;

        [SetUp]
        public void Setup()
        {
            var go = new GameObject();
            m_BehaviorParameters = go.AddComponent<BehaviorParameters>();
            var action = new InputAction("action");
            m_Adaptor = new TestAdaptor();
            m_Actuator = new InputActionActuator(null, m_BehaviorParameters, action, m_Adaptor, new InputActuatorEventContext(1, InputSystem.AddDevice<Gamepad>()));
        }

        [Test]
        public void TestOnActionReceived()
        {
            m_BehaviorParameters.BehaviorType = BehaviorType.HeuristicOnly;
            m_Actuator.OnActionReceived(new ActionBuffers());
            m_Actuator.Heuristic(new ActionBuffers());
            Assert.IsFalse(m_Adaptor.eventWritten);
            Assert.IsTrue(m_Adaptor.writtenToHeuristic);
            m_Adaptor.Reset();

            m_BehaviorParameters.BehaviorType = BehaviorType.Default;
            m_Actuator.OnActionReceived(new ActionBuffers());
            Assert.IsFalse(m_Adaptor.eventWritten);
            m_Adaptor.Reset();

            m_BehaviorParameters.Model = ScriptableObject.CreateInstance<NNModel>();
            m_Actuator.OnActionReceived(new ActionBuffers());
            Assert.IsTrue(m_Adaptor.eventWritten);
            m_Adaptor.Reset();

            Assert.AreEqual(m_Actuator.Name, "InputActionActuator-action");
            m_Actuator.ResetData();
            m_Actuator.WriteDiscreteActionMask(null);
        }
    }
}
#endif // MLA_INPUT_TESTS