using System.Collections.Generic; using System.Reflection; using NUnit.Framework; using SepCore.InputModule; using SepCore.InputModule.Runtime; using UnityEngine; using UnityEngine.InputSystem; namespace SepCore.InputModule.Tests { public class InputModuleDebuggerReflectionTests { private GameObject _gameObject; private InputModuleComponent _component; [SetUp] public void SetUp() { _gameObject = new GameObject("InputModuleDebuggerReflectionTest"); _component = InputModuleTestFactory.CreateComponent(_gameObject); } [TearDown] public void TearDown() { Object.DestroyImmediate(_gameObject); } [Test] public void CanReadContextStackViaReflection() { _component.OnInit(); _component.SetContext(InputContextId.UI); _component.PushContext(InputContextId.Dialog); FieldInfo stackField = typeof(InputModuleComponent).GetField("_contextStack", BindingFlags.Instance | BindingFlags.NonPublic); Assert.That(stackField, Is.Not.Null, "_contextStack field should exist"); object stack = stackField.GetValue(_component); Assert.That(stack, Is.Not.Null); Assert.That(stack, Is.InstanceOf()); PropertyInfo countProp = typeof(InputContextStack).GetProperty("Count", BindingFlags.Instance | BindingFlags.Public); PropertyInfo itemsProp = typeof(InputContextStack).GetProperty("Items", BindingFlags.Instance | BindingFlags.Public); Assert.That(countProp, Is.Not.Null, "Count property should exist"); Assert.That(itemsProp, Is.Not.Null, "Items property should exist"); int count = (int)countProp.GetValue(stack); IReadOnlyList items = itemsProp.GetValue(stack) as IReadOnlyList; Assert.That(count, Is.EqualTo(2)); Assert.That(items, Is.Not.Null); Assert.That(items.Count, Is.EqualTo(2)); Assert.That(items[0], Is.EqualTo(InputContextId.UI)); Assert.That(items[1], Is.EqualTo(InputContextId.Dialog)); } [Test] public void CanReadActionMapsViaReflection() { _component.OnInit(); FieldInfo mapsField = typeof(InputModuleComponent).GetField("_actionMaps", BindingFlags.Instance | BindingFlags.NonPublic); Assert.That(mapsField, Is.Not.Null, "_actionMaps field should exist"); object mapsObj = mapsField.GetValue(_component); Assert.That(mapsObj, Is.Not.Null); Assert.That(mapsObj, Is.InstanceOf>()); Dictionary maps = (Dictionary)mapsObj; Assert.That(maps.Count, Is.EqualTo(5)); Assert.That(maps, Contains.Key(InputContextId.Global)); Assert.That(maps, Contains.Key(InputContextId.UI)); Assert.That(maps, Contains.Key(InputContextId.GameplayExplore)); Assert.That(maps, Contains.Key(InputContextId.Dialog)); Assert.That(maps, Contains.Key(InputContextId.Rebind)); } [Test] public void ActionMapsReflectEnableState() { _component.OnInit(); _component.SetContext(InputContextId.GameplayExplore); FieldInfo mapsField = typeof(InputModuleComponent).GetField("_actionMaps", BindingFlags.Instance | BindingFlags.NonPublic); Dictionary maps = (Dictionary)mapsField.GetValue(_component); Assert.That(maps[InputContextId.Global].enabled, Is.True); Assert.That(maps[InputContextId.GameplayExplore].enabled, Is.True); Assert.That(maps[InputContextId.UI].enabled, Is.False); _component.PushContext(InputContextId.Dialog); Assert.That(maps[InputContextId.GameplayExplore].enabled, Is.False); Assert.That(maps[InputContextId.Dialog].enabled, Is.True); } [Test] public void ContextStackReflectsPopAndClear() { _component.OnInit(); _component.SetContext(InputContextId.UI); _component.PushContext(InputContextId.Dialog); FieldInfo stackField = typeof(InputModuleComponent).GetField("_contextStack", BindingFlags.Instance | BindingFlags.NonPublic); PropertyInfo countProp = typeof(InputContextStack).GetProperty("Count", BindingFlags.Instance | BindingFlags.Public); PropertyInfo itemsProp = typeof(InputContextStack).GetProperty("Items", BindingFlags.Instance | BindingFlags.Public); object stack = stackField.GetValue(_component); Assert.That(countProp.GetValue(stack), Is.EqualTo(2)); _component.PopContext(); Assert.That(countProp.GetValue(stack), Is.EqualTo(1)); IReadOnlyList itemsAfterPop = itemsProp.GetValue(stack) as IReadOnlyList; Assert.That(itemsAfterPop[0], Is.EqualTo(InputContextId.UI)); _component.ClearContexts(); Assert.That(countProp.GetValue(stack), Is.EqualTo(0)); } } }