|
1 | | -#if UNITY_EDITOR && UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS && UNITY_6000_0_OR_NEWER |
| 1 | +#if UNITY_EDITOR && UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS |
2 | 2 |
|
3 | 3 | using System; |
4 | 4 | using NUnit.Framework; |
|
11 | 11 | using UnityEngine.TestTools; |
12 | 12 | using UnityEngine.UIElements; |
13 | 13 |
|
14 | | -public enum SomeEnum |
| 14 | +internal enum SomeEnum |
15 | 15 | { |
16 | 16 | OptionA = 10, |
17 | | - OptionB = 20, |
18 | | - OptionC = 50, |
19 | | - OptionD = 100 |
| 17 | + OptionB = 20 |
20 | 18 | } |
21 | 19 |
|
22 | 20 | #if UNITY_EDITOR |
23 | 21 | [InitializeOnLoad] |
24 | 22 | #endif |
25 | | -public class CustomProcessor : InputProcessor<float> |
| 23 | +internal class CustomProcessor : InputProcessor<float> |
26 | 24 | { |
27 | 25 | public SomeEnum SomeEnum; |
28 | 26 |
|
@@ -72,38 +70,39 @@ public override IEnumerator UnitySetup() |
72 | 70 | } |
73 | 71 |
|
74 | 72 | [UnityTest] |
75 | | - public IEnumerator CustomProcessorEnum_SerialiseTheValue_InAsset() |
| 73 | + public IEnumerator ProcessorEnum_ShouldSerializeByValue_WhenSerializedToAsset() |
76 | 74 | { |
| 75 | + // Serialize current asset to JSON, and check that initial JSON contains default enum value for OptionA |
77 | 76 | var json = m_Window.currentAssetInEditor.ToJson(); |
78 | 77 |
|
79 | 78 | Assert.That(json.Contains("Custom(SomeEnum=10)"), Is.True, |
80 | 79 | "Serialized JSON does not contain the expected custom processor string for OptionA."); |
81 | | - |
82 | | - var dropdownList = m_Window.rootVisualElement.Query<DropdownField>().Where(d => d.choices != null && d.choices.Contains("OptionA") && d.choices.Contains("OptionB")).ToList(); |
| 80 | + |
| 81 | + // Query the dropdown with exactly two enum choices and check that the drop down is present in the UI |
| 82 | + var dropdownList = m_Window.rootVisualElement.Query<DropdownField>().Where(d => d.choices.Count == 2).ToList(); |
83 | 83 | Assume.That(dropdownList.Count > 0, Is.True, "Enum parameter dropdown not found in the UI."); |
84 | 84 |
|
| 85 | + // Determine the new value to be set in the dropdown, focus the dropdown before dispatching the change |
85 | 86 | var dropdown = dropdownList.First(); |
86 | | - |
87 | | - var newIndex = dropdown.choices.IndexOf("OptionB"); |
88 | | - var oldValue = dropdown.value; |
89 | | - var newValue = dropdown.choices[newIndex]; |
90 | | - |
| 87 | + var newValue = dropdown.choices[1]; |
91 | 88 | dropdown.Focus(); |
92 | 89 | dropdown.value = newValue; |
93 | 90 |
|
94 | | - var changeEvent = ChangeEvent<Enum>.GetPooled(SomeEnum.OptionA, SomeEnum.OptionC); |
| 91 | + // Create and send a change event from OptionA to OptionB |
| 92 | + var changeEvent = ChangeEvent<Enum>.GetPooled(SomeEnum.OptionA, SomeEnum.OptionB); |
95 | 93 | changeEvent.target = dropdown; |
96 | 94 | dropdown.SendEvent(changeEvent); |
97 | 95 |
|
| 96 | + // Find the save button in the window, focus and click the save button to persist the changes |
98 | 97 | var saveButton = m_Window.rootVisualElement.Q<Button>("save-asset-toolbar-button"); |
99 | 98 | Assume.That(saveButton, Is.Not.Null, "Save Asset button not found in the UI."); |
100 | 99 | saveButton.Focus(); |
101 | 100 | SimulateClickOn(saveButton); |
102 | 101 |
|
103 | 102 | Assert.That(dropdown.value, Is.EqualTo(newValue)); |
104 | 103 |
|
| 104 | + // Verify that the updated JSON contains the new enum value for OpitonB |
105 | 105 | var updatedJson = m_Window.currentAssetInEditor.ToJson(); |
106 | | - |
107 | 106 | Assert.That(updatedJson.Contains("Custom(SomeEnum=20)"), Is.True, "Serialized JSON does not contain the updated custom processor string for OptionB."); |
108 | 107 |
|
109 | 108 | yield return null; |
|
0 commit comments