|
| 1 | +// UITK TreeView is not supported in earlier versions |
| 2 | +// Therefore the UITK version of the InputActionAsset Editor is not available on earlier Editor versions either. |
| 3 | +#if UNITY_EDITOR && UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS && UNITY_6000_0_OR_NEWER |
| 4 | + |
| 5 | +using NUnit.Framework; |
| 6 | +using System; |
| 7 | +using System.Collections; |
| 8 | +using UnityEditor; |
| 9 | +using UnityEngine; |
| 10 | +using UnityEngine.InputSystem; |
| 11 | +using UnityEngine.InputSystem.Editor; |
| 12 | +using UnityEngine.TestTools; |
| 13 | +using UnityEngine.UIElements; |
| 14 | + |
| 15 | +internal class InputActionsEditorTests : UIToolkitBaseTestWindow<InputActionsEditorWindow> |
| 16 | +{ |
| 17 | + #region setup and teardown |
| 18 | + InputActionAsset m_Asset; |
| 19 | + |
| 20 | + public override void OneTimeSetUp() |
| 21 | + { |
| 22 | + base.OneTimeSetUp(); |
| 23 | + m_Asset = AssetDatabaseUtils.CreateAsset<InputActionAsset>(); |
| 24 | + m_Asset.AddActionMap("First Name"); |
| 25 | + m_Asset.AddActionMap("Second Name"); |
| 26 | + m_Asset.AddActionMap("Third Name"); |
| 27 | + } |
| 28 | + |
| 29 | + public override void OneTimeTearDown() |
| 30 | + { |
| 31 | + AssetDatabaseUtils.Restore(); |
| 32 | + base.OneTimeTearDown(); |
| 33 | + } |
| 34 | + |
| 35 | + public override IEnumerator UnitySetup() |
| 36 | + { |
| 37 | + m_Window = InputActionsEditorWindow.OpenEditor(m_Asset); |
| 38 | + yield return base.UnitySetup(); |
| 39 | + } |
| 40 | + |
| 41 | + #endregion |
| 42 | + |
| 43 | + #region Helper methods |
| 44 | + |
| 45 | + IEnumerator WaitForActionMapRename(int index, bool isActive, double timeoutSecs = 5.0) |
| 46 | + { |
| 47 | + return WaitUntil(() => |
| 48 | + { |
| 49 | + var actionMapItems = m_Window.rootVisualElement.Q("action-maps-container").Query<InputActionMapsTreeViewItem>().ToList(); |
| 50 | + if (actionMapItems.Count > index && actionMapItems[index].IsFocused == isActive) |
| 51 | + { |
| 52 | + return true; |
| 53 | + } |
| 54 | + return false; |
| 55 | + }, $"WaitForActionMapRename {index} {isActive}", timeoutSecs); |
| 56 | + } |
| 57 | + |
| 58 | + #endregion |
| 59 | + |
| 60 | + [Test] |
| 61 | + public void CanListActionMaps() |
| 62 | + { |
| 63 | + var actionMapsContainer = m_Window.rootVisualElement.Q("action-maps-container"); |
| 64 | + Assert.That(actionMapsContainer, Is.Not.Null); |
| 65 | + var actionMapItem = actionMapsContainer.Query<InputActionMapsTreeViewItem>().ToList(); |
| 66 | + Assert.That(actionMapItem, Is.Not.Null); |
| 67 | + Assert.That(actionMapItem.Count, Is.EqualTo(3)); |
| 68 | + Assert.That(m_Window.currentAssetInEditor.actionMaps[0].name, Is.EqualTo("First Name")); |
| 69 | + Assert.That(m_Window.currentAssetInEditor.actionMaps[1].name, Is.EqualTo("Second Name")); |
| 70 | + Assert.That(m_Window.currentAssetInEditor.actionMaps[2].name, Is.EqualTo("Third Name")); |
| 71 | + } |
| 72 | + |
| 73 | + [UnityTest] |
| 74 | + public IEnumerator CanCreateActionMap() |
| 75 | + { |
| 76 | + var button = m_Window.rootVisualElement.Q<Button>("add-new-action-map-button"); |
| 77 | + Assume.That(button, Is.Not.Null); |
| 78 | + SimulateClickOn(button); |
| 79 | + |
| 80 | + // Wait for the focus to move out the button and start new ActionMaps editon |
| 81 | + yield return WaitForActionMapRename(3, isActive: true); |
| 82 | + |
| 83 | + // Rename the new action map |
| 84 | + SimulateTypingText("New Name"); |
| 85 | + |
| 86 | + // wait for the edition to end |
| 87 | + yield return WaitForActionMapRename(3, isActive: false); |
| 88 | + |
| 89 | + // Check on the UI side |
| 90 | + var actionMapsContainer = m_Window.rootVisualElement.Q("action-maps-container"); |
| 91 | + var actionMapItem = actionMapsContainer.Query<InputActionMapsTreeViewItem>().ToList(); |
| 92 | + Assert.That(actionMapItem, Is.Not.Null); |
| 93 | + Assert.That(actionMapItem.Count, Is.EqualTo(4)); |
| 94 | + Assert.That(actionMapItem[3].Q<Label>("name").text, Is.EqualTo("New Name")); |
| 95 | + |
| 96 | + // Check on the asset side |
| 97 | + Assert.That(m_Window.currentAssetInEditor.actionMaps.Count, Is.EqualTo(4)); |
| 98 | + Assert.That(m_Window.currentAssetInEditor.actionMaps[3].name, Is.EqualTo("New Name")); |
| 99 | + } |
| 100 | + |
| 101 | + [UnityTest] |
| 102 | + public IEnumerator CanRenameActionMap() |
| 103 | + { |
| 104 | + var actionMapsContainer = m_Window.rootVisualElement.Q("action-maps-container"); |
| 105 | + var actionMapItem = actionMapsContainer.Query<InputActionMapsTreeViewItem>().ToList(); |
| 106 | + Assume.That(actionMapItem[1].Q<Label>("name").text, Is.EqualTo("Second Name")); |
| 107 | + |
| 108 | + // for the selection the prevent some instabilities with current ui intregration |
| 109 | + m_Window.rootVisualElement.Q<ListView>("action-maps-list-view").Focus(); |
| 110 | + m_Window.rootVisualElement.Q<ListView>("action-maps-list-view").selectedIndex = 1; |
| 111 | + |
| 112 | + yield return WaitForNotDirty(); |
| 113 | + yield return WaitForFocus(m_Window.rootVisualElement.Q("action-maps-list-view")); |
| 114 | + |
| 115 | + // refetch the action map item since the ui may have refreshed. |
| 116 | + actionMapItem = actionMapsContainer.Query<InputActionMapsTreeViewItem>().ToList(); |
| 117 | + |
| 118 | + // clic twice to start the rename |
| 119 | + SimulateClickOn(actionMapItem[1]); |
| 120 | + // in case of the item is already focused, don't click again |
| 121 | + if (!actionMapItem[1].IsFocused) |
| 122 | + { |
| 123 | + SimulateClickOn(actionMapItem[1]); |
| 124 | + } |
| 125 | + |
| 126 | + yield return WaitForActionMapRename(1, isActive: true); |
| 127 | + |
| 128 | + // Rename the new action map |
| 129 | + SimulateTypingText("New Name"); |
| 130 | + |
| 131 | + // wait for the edition to end |
| 132 | + yield return WaitForActionMapRename(1, isActive: false); |
| 133 | + |
| 134 | + // Check on the UI side |
| 135 | + actionMapsContainer = m_Window.rootVisualElement.Q("action-maps-container"); |
| 136 | + Assume.That(actionMapsContainer, Is.Not.Null); |
| 137 | + actionMapItem = actionMapsContainer.Query<InputActionMapsTreeViewItem>().ToList(); |
| 138 | + Assert.That(actionMapItem, Is.Not.Null); |
| 139 | + Assert.That(actionMapItem.Count, Is.EqualTo(3)); |
| 140 | + Assert.That(actionMapItem[1].Q<Label>("name").text, Is.EqualTo("New Name")); |
| 141 | + |
| 142 | + // Check on the asset side |
| 143 | + Assert.That(m_Window.currentAssetInEditor.actionMaps.Count, Is.EqualTo(3)); |
| 144 | + Assert.That(m_Window.currentAssetInEditor.actionMaps[1].name, Is.EqualTo("New Name")); |
| 145 | + } |
| 146 | + |
| 147 | + [UnityTest] |
| 148 | + public IEnumerator CanDeleteActionMap() |
| 149 | + { |
| 150 | + var actionMapsContainer = m_Window.rootVisualElement.Q("action-maps-container"); |
| 151 | + var actionMapItem = actionMapsContainer.Query<InputActionMapsTreeViewItem>().ToList(); |
| 152 | + Assume.That(actionMapItem[1].Q<Label>("name").text, Is.EqualTo("Second Name")); |
| 153 | + |
| 154 | + // Select the second element |
| 155 | + SimulateClickOn(actionMapItem[1]); |
| 156 | + |
| 157 | + yield return WaitForFocus(m_Window.rootVisualElement.Q("action-maps-list-view")); |
| 158 | + |
| 159 | + SimulateDeleteCommand(); |
| 160 | + |
| 161 | + yield return WaitUntil(() => actionMapsContainer.Query<InputActionMapsTreeViewItem>().ToList().Count == 2, "wait for element to be deleted"); |
| 162 | + |
| 163 | + // Check on the UI side |
| 164 | + actionMapItem = actionMapsContainer.Query<InputActionMapsTreeViewItem>().ToList(); |
| 165 | + Assert.That(actionMapItem.Count, Is.EqualTo(2)); |
| 166 | + Assert.That(actionMapItem[0].Q<Label>("name").text, Is.EqualTo("First Name")); |
| 167 | + Assert.That(actionMapItem[1].Q<Label>("name").text, Is.EqualTo("Third Name")); |
| 168 | + |
| 169 | + // Check on the asset side |
| 170 | + Assert.That(m_Window.currentAssetInEditor.actionMaps.Count, Is.EqualTo(2)); |
| 171 | + Assert.That(m_Window.currentAssetInEditor.actionMaps[0].name, Is.EqualTo("First Name")); |
| 172 | + Assert.That(m_Window.currentAssetInEditor.actionMaps[1].name, Is.EqualTo("Third Name")); |
| 173 | + } |
| 174 | +} |
| 175 | +#endif |
0 commit comments