Skip to content

Commit 3806968

Browse files
committed
Add RenameAction test
1 parent 790d9cc commit 3806968

File tree

2 files changed

+72
-3
lines changed

2 files changed

+72
-3
lines changed

Assets/Tests/InputSystem.Editor/InputActionsEditorTests.cs

Lines changed: 63 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()
@@ -55,6 +58,19 @@ IEnumerator WaitForActionMapRename(int index, bool isActive, double timeoutSecs
5558
}, $"WaitForActionMapRename {index} {isActive}", timeoutSecs);
5659
}
5760

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+
}
73+
5874
#endregion
5975

6076
[Test]
@@ -173,5 +189,51 @@ 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+
yield return WaitForActionRename(1, isActive: true);
220+
221+
// Rename the action
222+
SimulateTypingText("New Name");
223+
224+
// Wait for rename to end
225+
yield return WaitForActionRename(1, isActive: false);
226+
227+
// Check on the UI side
228+
actionContainer = m_Window.rootVisualElement.Q("actions-container");
229+
Assume.That(actionContainer, Is.Not.Null);
230+
actionItem = actionContainer.Query<InputActionsTreeViewItem>().ToList();
231+
Assert.That(actionItem, Is.Not.Null);
232+
Assert.That(actionItem.Count, Is.EqualTo(2));
233+
Assert.That(actionItem[1].Q<Label>("name").text, Is.EqualTo("New Name"));
234+
235+
// Check on the asset side
236+
Assert.That(m_Window.currentAssetInEditor.actionMaps[0].actions[1].name, Is.EqualTo("New Name"));
237+
}
176238
}
177239
#endif

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ internal class InputActionsTreeViewItem : VisualElement
1717
private const string kRenameTextField = "rename-text-field";
1818
public event EventCallback<string> EditTextFinished;
1919

20+
// for testing purposes to know if the item is focused to accept input
21+
internal bool IsFocused { get; private set; } = false;
22+
2023
private bool m_IsEditing;
2124
private static InputActionsTreeViewItem s_EditingItem = null;
2225

@@ -34,9 +37,13 @@ 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 =>
43+
{
44+
OnEditTextFinished();
45+
IsFocused = false;
46+
});
4047
}
4148

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

0 commit comments

Comments
 (0)