diff --git a/Assets/Tests/InputSystem.Editor/CustomProcessorEnumTest.cs b/Assets/Tests/InputSystem.Editor/CustomProcessorEnumTest.cs new file mode 100644 index 0000000000..0a57f1ebed --- /dev/null +++ b/Assets/Tests/InputSystem.Editor/CustomProcessorEnumTest.cs @@ -0,0 +1,112 @@ +#if UNITY_EDITOR && UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS && UNITY_6000_0_OR_NEWER + +using System; +using NUnit.Framework; +using System.Collections; +using System.Linq; +using UnityEditor; +using UnityEngine; +using UnityEngine.InputSystem; +using UnityEngine.InputSystem.Editor; +using UnityEngine.TestTools; +using UnityEngine.UIElements; + +internal enum SomeEnum +{ + OptionA = 10, + OptionB = 20 +} + +#if UNITY_EDITOR +[InitializeOnLoad] +#endif +internal class CustomProcessor : InputProcessor +{ + public SomeEnum SomeEnum; + +#if UNITY_EDITOR + static CustomProcessor() + { + Initialize(); + } + +#endif + + [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] + private static void Initialize() + { + InputSystem.RegisterProcessor(); + } + + public override float Process(float value, InputControl control) + { + return value; + } +} + +internal class CustomProcessorEnumTest : UIToolkitBaseTestWindow +{ + InputActionAsset m_Asset; + + public override void OneTimeSetUp() + { + base.OneTimeSetUp(); + m_Asset = AssetDatabaseUtils.CreateAsset(); + + var actionMap = m_Asset.AddActionMap("Action Map"); + + actionMap.AddAction("Action", InputActionType.Value, processors: "Custom(SomeEnum=10)"); + } + + public override void OneTimeTearDown() + { + AssetDatabaseUtils.Restore(); + base.OneTimeTearDown(); + } + + public override IEnumerator UnitySetup() + { + m_Window = InputActionsEditorWindow.OpenEditor(m_Asset); + yield return base.UnitySetup(); + } + + [UnityTest] + public IEnumerator ProcessorEnum_ShouldSerializeByValue_WhenSerializedToAsset() + { + // Serialize current asset to JSON, and check that initial JSON contains default enum value for OptionA + var json = m_Window.currentAssetInEditor.ToJson(); + + Assert.That(json.Contains("Custom(SomeEnum=10)"), Is.True, + "Serialized JSON does not contain the expected custom processor string for OptionA."); + + // Query the dropdown with exactly two enum choices and check that the drop down is present in the UI + var dropdownList = m_Window.rootVisualElement.Query().Where(d => d.choices.Count == 2).ToList(); + Assume.That(dropdownList.Count > 0, Is.True, "Enum parameter dropdown not found in the UI."); + + // Determine the new value to be set in the dropdown, focus the dropdown before dispatching the change + var dropdown = dropdownList.First(); + var newValue = dropdown.choices[1]; + dropdown.Focus(); + dropdown.value = newValue; + + // Create and send a change event from OptionA to OptionB + var changeEvent = ChangeEvent.GetPooled(SomeEnum.OptionA, SomeEnum.OptionB); + changeEvent.target = dropdown; + dropdown.SendEvent(changeEvent); + + // Find the save button in the window, focus and click the save button to persist the changes + var saveButton = m_Window.rootVisualElement.Q