Skip to content

Commit 09d6044

Browse files
committed
Add RenameAction test
1 parent 60e8a24 commit 09d6044

File tree

2 files changed

+72
-3
lines changed

2 files changed

+72
-3
lines changed

Assets/Tests/InputSystem.Editor/InputActionsEditorTests.cs

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@ public override void OneTimeSetUp()
2121
{
2222
base.OneTimeSetUp();
2323
m_Asset = AssetDatabaseUtils.CreateAsset<InputActionAsset>();
24-
m_Asset.AddActionMap("First Name");
24+
var actionMap = m_Asset.AddActionMap("First Name");
2525
m_Asset.AddActionMap("Second Name");
2626
m_Asset.AddActionMap("Third Name");
27+
28+
actionMap.AddAction("Action One");
29+
actionMap.AddAction("Action Two");
2730
}
2831

2932
public override void OneTimeTearDown()
@@ -54,6 +57,19 @@ IEnumerator WaitForActionMapRename(int index, bool isActive, double timeoutSecs
5457
return false;
5558
}, $"WaitForActionMapRename {index} {isActive}", timeoutSecs);
5659
}
60+
61+
IEnumerator WaitForActionRename(int index, bool isActive, double timeoutSecs = 5.0)
62+
{
63+
return WaitUntil(() =>
64+
{
65+
var actionItems = m_Window.rootVisualElement.Q("actions-container").Query<InputActionsTreeViewItem>().ToList();
66+
if (actionItems.Count > index && actionItems[index].IsFocused == isActive)
67+
{
68+
return true;
69+
}
70+
return false;
71+
}, $"WaitForActionRename {index} {isActive}", timeoutSecs);
72+
}
5773

5874
#endregion
5975

@@ -173,5 +189,55 @@ public IEnumerator CanDeleteActionMap()
173189
Assert.That(m_Window.currentAssetInEditor.actionMaps[0].name, Is.EqualTo("First Name"));
174190
Assert.That(m_Window.currentAssetInEditor.actionMaps[1].name, Is.EqualTo("Third Name"));
175191
}
192+
193+
[UnityTest]
194+
public IEnumerator CanRenameAction()
195+
{
196+
var actionContainer = m_Window.rootVisualElement.Q("actions-container");
197+
var actionItem = actionContainer.Query<InputActionsTreeViewItem>().ToList();
198+
Assume.That(actionItem[1].Q<Label>("name").text, Is.EqualTo("Action Two"));
199+
200+
m_Window.rootVisualElement.Q<TreeView>("actions-tree-view").Focus();
201+
m_Window.rootVisualElement.Q<TreeView>("actions-tree-view").selectedIndex = 1;
202+
203+
// Selection change triggers a state change, wait for the scheduler to process the frame
204+
yield return WaitForSchedulerLoop();
205+
yield return WaitForNotDirty();
206+
yield return WaitForFocus(m_Window.rootVisualElement.Q<TreeView>("actions-tree-view"));
207+
208+
// Re-fetch the actions since the UI may have refreshed.
209+
actionItem = actionContainer.Query<InputActionsTreeViewItem>().ToList();
210+
211+
// Click twice to start the rename
212+
SimulateClickOn(actionItem[1]);
213+
// If the item is already focused, don't click again
214+
if (!actionItem[1].IsFocused)
215+
{
216+
SimulateClickOn(actionItem[1]);
217+
}
218+
219+
// Get the element that has focus before rename
220+
var focusedElement = m_Window.rootVisualElement.Q<TreeView>("actions-tree-view").focusController.focusedElement;
221+
222+
yield return WaitForActionRename(1, isActive: true);
223+
224+
// Rename the action
225+
SimulateTypingText("New Name");
226+
227+
// Wait for rename to end
228+
yield return WaitForActionRename(1, isActive: false);
229+
230+
// Check on the UI side
231+
actionContainer = m_Window.rootVisualElement.Q("actions-container");
232+
Assume.That(actionContainer, Is.Not.Null);
233+
actionItem = actionContainer.Query<InputActionsTreeViewItem>().ToList();
234+
Assert.That(actionItem, Is.Not.Null);
235+
Assert.That(actionItem.Count, Is.EqualTo(2));
236+
Assert.That(actionItem[1].Q<Label>("name").text, Is.EqualTo("New Name"));
237+
Assert.That(m_Window.rootVisualElement.Q<TreeView>("actions-tree-view").focusController.focusedElement, Is.EqualTo(focusedElement));
238+
239+
// Check on the asset side
240+
Assert.That(m_Window.currentAssetInEditor.actionMaps[0].actions[1].name, Is.EqualTo("New Name"));
241+
}
176242
}
177243
#endif

Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Views/InputActionsTreeViewItem.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ internal class InputActionsTreeViewItem : VisualElement
1616

1717
private const string kRenameTextField = "rename-text-field";
1818
public event EventCallback<string> EditTextFinished;
19+
20+
// for testing purposes to know if the item is focused to accept input
21+
internal bool IsFocused { get; private set; } = false;
1922

2023
private bool m_IsEditing;
2124
private static InputActionsTreeViewItem s_EditingItem = null;
@@ -34,9 +37,9 @@ public InputActionsTreeViewItem()
3437
renameTextfield.selectAllOnFocus = true;
3538
renameTextfield.selectAllOnMouseUp = false;
3639

37-
3840
RegisterCallback<MouseDownEvent>(OnMouseDownEventForRename);
39-
renameTextfield.RegisterCallback<FocusOutEvent>(e => OnEditTextFinished());
41+
renameTextfield.RegisterCallback<FocusInEvent>(e => IsFocused = true);
42+
renameTextfield.RegisterCallback<FocusOutEvent>(e => { OnEditTextFinished(); IsFocused = false; });
4043
}
4144

4245
public Label label => this.Q<Label>();

0 commit comments

Comments
 (0)