Skip to content

Commit 012b254

Browse files
committed
Improve testing coverage and comments
1 parent b994e4b commit 012b254

File tree

3 files changed

+49
-16
lines changed

3 files changed

+49
-16
lines changed

Assets/Tests/InputSystem/CoreTests_Editor.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2893,9 +2893,9 @@ public void Editor_WhenRunUpdatesInEditModeIsEnabled_InputActionsTriggerInEditMo
28932893
private static void DisableProjectWideActions()
28942894
{
28952895
#if UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS
2896-
// If the system has project-wide input actions they will also trigger enable/disable via
2897-
// play mode change triggers above. Hence we adjust extra variable to compensate of
2898-
// state allocated by project-wide actions.
2896+
// If the system has project-wide input actions they will start enabled once InputSystem.Reset() is called and
2897+
// be disabled entering EditMode. Hence, we adjust extra variable to compensate ofstate allocated by
2898+
// project-wide actions.
28992899
if (InputSystem.actions)
29002900
{
29012901
Assert.That(InputActionState.s_GlobalState.globalList.length, Is.EqualTo(1));
@@ -2918,6 +2918,14 @@ public void Editor_LeavingPlayMode_DestroysAllActionStates()
29182918

29192919
// Enter play mode.
29202920
InputSystem.OnPlayModeChange(PlayModeStateChange.ExitingEditMode);
2921+
2922+
#if UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS
2923+
// This simulates enabling project-wide actions, which is done before just before entering play mode,
2924+
// called from InputSystem.InitializeInEditor().
2925+
if (InputSystem.actions)
2926+
InputSystem.actions.Enable();
2927+
#endif
2928+
29212929
InputSystem.OnPlayModeChange(PlayModeStateChange.EnteredPlayMode);
29222930

29232931
DisableProjectWideActions();
@@ -2934,7 +2942,8 @@ public void Editor_LeavingPlayMode_DestroysAllActionStates()
29342942
InputSystem.OnPlayModeChange(PlayModeStateChange.EnteredEditMode);
29352943

29362944
Assert.That(InputActionState.s_GlobalState.globalList.length, Is.Zero);
2937-
Assert.That(InputSystem.s_Manager.m_StateChangeMonitors[0].listeners[0].control, Is.Null); // Won't get removed, just cleared.
2945+
// Won't get removed, just cleared.
2946+
Assert.That(InputSystem.s_Manager.m_StateChangeMonitors[0].listeners[0].control, Is.Null);
29382947
}
29392948

29402949
[Test]

Assets/Tests/InputSystem/CoreTests_ProjectWideActions.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,37 @@ public void ProjectWideActions_CanEnableCurrentActionMapOfPlayerInput(string act
196196
//NOTE: Asset actions are considered enabled even if a single action map is enabled
197197
Assert.That(playerInput.actions.enabled, Is.EqualTo(expectedResult));
198198
}
199+
200+
[Test]
201+
[Category("Editor")]
202+
public void ProjectWideActions_InitializeInEditorEnablesProjectWideActions()
203+
{
204+
if (InputSystem.actions != null)
205+
{
206+
// Asserts that project wide actions are enabled by default.
207+
// Before the test is run, InputSystem.Reset() is called which will enable them.
208+
// It can be interpreted as a mock of the behavior that happens when `InitializeInEditor()` is called.
209+
Assert.That(InputSystem.actions.enabled, Is.True);
210+
211+
// Calling exit play mode callbacks will disable them
212+
InputSystem.OnPlayModeChange(PlayModeStateChange.ExitingPlayMode);
213+
InputSystem.OnPlayModeChange(PlayModeStateChange.EnteredEditMode);
214+
215+
Assert.That(InputSystem.actions.enabled, Is.False);
216+
217+
// Calling enter play mode callbacks will not re-enable them per default. They are only
218+
// enabled when `InputSystem.InitializeInEditor()` is called, which happens before these callbacks.
219+
// Note: Project-wide actions are disabled at this point. These next lines are added to make sure we
220+
// establish behavior that project-wide actions should be enabled only once
221+
// `InputSystem.InitializeInEditor()` is called. Before this test was introduced, project-wide actions were
222+
// enabled after entering play mode again which would lead to a different behavior than Player
223+
// builds.
224+
InputSystem.OnPlayModeChange(PlayModeStateChange.ExitingEditMode);
225+
InputSystem.OnPlayModeChange(PlayModeStateChange.EnteredPlayMode);
226+
227+
Assert.That(InputSystem.actions.enabled, Is.False);
228+
}
229+
}
199230
}
200231

201232
#endif

Packages/com.unity.inputsystem/InputSystem/InputSystem.cs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3015,16 +3015,10 @@ internal static bool ShouldDrawWarningIconForBinding(string bindingPath)
30153015
#region Actions
30163016

30173017
#if UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS
3018-
// EnteredEditMode Occurs during the next update of the Editor application if it is in edit mode and was previously in play mode.
3019-
// ExitingEditMode Occurs when exiting edit mode, before the Editor is in play mode.
3020-
// EnteredPlayMode Occurs during the next update of the Editor application if it is in play mode and was previously in edit mode.
3021-
// ExitingPlayMode Occurs when exiting play mode, before the Editor is in edit mode.
3022-
//
3023-
// Using the EnteredEditMode / EnteredPlayMode states to transition the actions' enabled
3024-
// state ensures that the they are active in all of these MonoBehavior methods:
3025-
//
3026-
// Awake() / Start() / OnEnable() / OnDisable() / OnDestroy()
3027-
//
3018+
3019+
// This is called from InitializeInEditor() and InitializeInPlayer() to make sure
3020+
// project-wide actions are all active in they are active in all of these MonoBehavior methods:
3021+
// Awake() / Start() / OnEnable() / OnDisable() / OnDestroy()
30283022
private static void EnableActions()
30293023
{
30303024
#if UNITY_EDITOR
@@ -3908,8 +3902,7 @@ private static void Reset(bool enableRemoting = false, IInputRuntime runtime = n
39083902
InputUser.ResetGlobals();
39093903
EnhancedTouchSupport.Reset();
39103904

3911-
// // This is the point where we initialise project-wide actions for the Editor, Editor Tests and Player Tests.
3912-
// // Note this is too early for editor ! actions is not setup yet.
3905+
// This is the point where we initialise project-wide actions for the Editor Play-mode, Editor Tests and Player Tests.
39133906
#if UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS
39143907
EnableActions();
39153908
#endif

0 commit comments

Comments
 (0)