|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using NUnit.Framework; |
| 4 | +using UnityEditor; |
| 5 | +using UnityEngine; |
| 6 | +using UnityEngine.InputSystem; |
| 7 | +using UnityEngine.TestTools; |
| 8 | +using Object = UnityEngine.Object; |
| 9 | + |
| 10 | +// Enters real play mode so that the engine runs every RuntimeInitializeOnLoadMethod in its own order. |
| 11 | +// This deliberately does not derive from InputTestFixture, which replaces the runtime and global state. |
| 12 | +internal class InputActionStatePlayModeTests |
| 13 | +{ |
| 14 | + const string k_TestCategory = "Editor"; |
| 15 | + const string k_ArmedKey = "InputSystem.Tests.InputActionStatePlayModeTests.Armed"; |
| 16 | + const string k_EnabledAfterSceneLoadKey = "InputSystem.Tests.InputActionStatePlayModeTests.EnabledAfterSceneLoad"; |
| 17 | + const string k_SavedOptionsKey = "InputSystem.Tests.InputActionStatePlayModeTests.SavedOptions"; |
| 18 | + const string k_SavedOptionsEnabledKey = "InputSystem.Tests.InputActionStatePlayModeTests.SavedOptionsEnabled"; |
| 19 | + |
| 20 | + static readonly bool[] k_DisableDomainReloadValues = { false, true }; |
| 21 | + |
| 22 | + static InputAction s_EarlyAction; |
| 23 | + |
| 24 | + [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)] |
| 25 | + static void EnableActionBeforeSceneLoad() |
| 26 | + { |
| 27 | + if (!SessionState.GetBool(k_ArmedKey, false)) |
| 28 | + return; |
| 29 | + |
| 30 | + s_EarlyAction = new InputAction(binding: "<Keyboard>/space"); |
| 31 | + s_EarlyAction.Enable(); |
| 32 | + } |
| 33 | + |
| 34 | + [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)] |
| 35 | + static void RecordActionAfterSceneLoad() |
| 36 | + { |
| 37 | + if (!SessionState.GetBool(k_ArmedKey, false)) |
| 38 | + return; |
| 39 | + |
| 40 | + SessionState.SetBool(k_EnabledAfterSceneLoadKey, s_EarlyAction != null && s_EarlyAction.enabled); |
| 41 | + } |
| 42 | + |
| 43 | + [SetUp] |
| 44 | + public void SetUp() |
| 45 | + { |
| 46 | + // Each domain reload during the test runs SetUp again, so only record the options once. |
| 47 | + if (SessionState.GetInt(k_SavedOptionsKey, -1) != -1) |
| 48 | + return; |
| 49 | + |
| 50 | + // Unity 6000.0 reports no options while they are disabled, so enable them to read the stored ones. |
| 51 | + var optionsEnabled = EditorSettings.enterPlayModeOptionsEnabled; |
| 52 | + EditorSettings.enterPlayModeOptionsEnabled = true; |
| 53 | + SessionState.SetInt(k_SavedOptionsKey, (int)EditorSettings.enterPlayModeOptions); |
| 54 | + SessionState.SetBool(k_SavedOptionsEnabledKey, optionsEnabled); |
| 55 | + EditorSettings.enterPlayModeOptionsEnabled = optionsEnabled; |
| 56 | + } |
| 57 | + |
| 58 | + [TearDown] |
| 59 | + public void TearDown() |
| 60 | + { |
| 61 | + if (Application.isPlaying) |
| 62 | + return; |
| 63 | + |
| 64 | + EditorSettings.enterPlayModeOptionsEnabled = true; |
| 65 | + EditorSettings.enterPlayModeOptions = (EnterPlayModeOptions)SessionState.GetInt(k_SavedOptionsKey, 0); |
| 66 | + EditorSettings.enterPlayModeOptionsEnabled = SessionState.GetBool(k_SavedOptionsEnabledKey, false); |
| 67 | + |
| 68 | + // Entering play mode saves the modified settings to disk, so save the restored ones too. |
| 69 | + AssetDatabase.SaveAssets(); |
| 70 | + SessionState.EraseInt(k_SavedOptionsKey); |
| 71 | + SessionState.EraseBool(k_SavedOptionsEnabledKey); |
| 72 | + SessionState.EraseBool(k_ArmedKey); |
| 73 | + SessionState.EraseBool(k_EnabledAfterSceneLoadKey); |
| 74 | + |
| 75 | + s_EarlyAction?.Dispose(); |
| 76 | + s_EarlyAction = null; |
| 77 | + } |
| 78 | + |
| 79 | + static void SetDomainReloadDisabled(bool disabled) |
| 80 | + { |
| 81 | + // Unity 6000.0 ignores enterPlayModeOptions unless the options are enabled. |
| 82 | + EditorSettings.enterPlayModeOptionsEnabled = true; |
| 83 | + EditorSettings.enterPlayModeOptions = disabled |
| 84 | + ? EnterPlayModeOptions.DisableDomainReload | EnterPlayModeOptions.DisableSceneReload |
| 85 | + : EnterPlayModeOptions.None; |
| 86 | + } |
| 87 | + |
| 88 | + static bool HasOnActionChangeCallback(Action<object, InputActionChange> callback) |
| 89 | + { |
| 90 | + var callbacks = InputActionState.s_GlobalState.onActionChange; |
| 91 | + for (var i = 0; i < callbacks.length; ++i) |
| 92 | + { |
| 93 | + if (callbacks[i] == callback) |
| 94 | + return true; |
| 95 | + } |
| 96 | + return false; |
| 97 | + } |
| 98 | + |
| 99 | + [UnityTest] |
| 100 | + [Category(k_TestCategory)] |
| 101 | + [Description("https://jira.unity3d.com/browse/UUM-151403")] |
| 102 | + public IEnumerator Actions_EnabledBeforeSceneLoad_StayEnabled_WhenEnteringPlayMode( |
| 103 | + [ValueSource(nameof(k_DisableDomainReloadValues))] bool disableDomainReload) |
| 104 | + { |
| 105 | + SetDomainReloadDisabled(disableDomainReload); |
| 106 | + SessionState.SetBool(k_ArmedKey, true); |
| 107 | + |
| 108 | + yield return new EnterPlayMode(); |
| 109 | + |
| 110 | + SessionState.SetBool(k_ArmedKey, false); |
| 111 | + Assert.That(SessionState.GetBool(k_EnabledAfterSceneLoadKey, false), Is.True); |
| 112 | + |
| 113 | + yield return new ExitPlayMode(); |
| 114 | + |
| 115 | + // Without a domain reload the action object survives, but exiting play mode must still clean up its state. |
| 116 | + if (disableDomainReload) |
| 117 | + Assert.That(s_EarlyAction.enabled, Is.False); |
| 118 | + } |
| 119 | + |
| 120 | + [UnityTest] |
| 121 | + [Category(k_TestCategory)] |
| 122 | + [Description("https://jira.unity3d.com/browse/UUM-151403")] |
| 123 | + public IEnumerator Actions_OnActionChangeCallbacksAddedInPlayMode_AreRemovedOnExit_WhenDomainReloadIsDisabled() |
| 124 | + { |
| 125 | + SetDomainReloadDisabled(true); |
| 126 | + |
| 127 | + Action<object, InputActionChange> editModeCallback = (obj, change) => {}; |
| 128 | + Action<object, InputActionChange> playModeCallback = (obj, change) => {}; |
| 129 | + var survivingListener = ScriptableObject.CreateInstance<ActionChangeListener>(); |
| 130 | + survivingListener.hideFlags = HideFlags.HideAndDontSave; |
| 131 | + var destroyedListener = ScriptableObject.CreateInstance<ActionChangeListener>(); |
| 132 | + destroyedListener.hideFlags = HideFlags.HideAndDontSave; |
| 133 | + Action<object, InputActionChange> survivingListenerCallback = survivingListener.OnActionChange; |
| 134 | + Action<object, InputActionChange> destroyedListenerCallback = destroyedListener.OnActionChange; |
| 135 | + InputSystem.onActionChange += editModeCallback; |
| 136 | + try |
| 137 | + { |
| 138 | + yield return new EnterPlayMode(); |
| 139 | + |
| 140 | + Assert.That(HasOnActionChangeCallback(editModeCallback), Is.True); |
| 141 | + InputSystem.onActionChange += playModeCallback; |
| 142 | + |
| 143 | + // Like an editor window opened during play mode, which stays open after it. |
| 144 | + InputSystem.onActionChange += survivingListenerCallback; |
| 145 | + |
| 146 | + // Like a component of a play mode GameObject, which is destroyed on exit. |
| 147 | + InputSystem.onActionChange += destroyedListenerCallback; |
| 148 | + Object.DestroyImmediate(destroyedListener); |
| 149 | + |
| 150 | + yield return new ExitPlayMode(); |
| 151 | + |
| 152 | + Assert.That(HasOnActionChangeCallback(editModeCallback), Is.True); |
| 153 | + Assert.That(HasOnActionChangeCallback(playModeCallback), Is.False); |
| 154 | + Assert.That(HasOnActionChangeCallback(survivingListenerCallback), Is.True); |
| 155 | + Assert.That(HasOnActionChangeCallback(destroyedListenerCallback), Is.False); |
| 156 | + } |
| 157 | + finally |
| 158 | + { |
| 159 | + InputSystem.onActionChange -= editModeCallback; |
| 160 | + InputSystem.onActionChange -= playModeCallback; |
| 161 | + InputSystem.onActionChange -= survivingListenerCallback; |
| 162 | + InputSystem.onActionChange -= destroyedListenerCallback; |
| 163 | + Object.DestroyImmediate(survivingListener); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + class ActionChangeListener : ScriptableObject |
| 168 | + { |
| 169 | + public void OnActionChange(object actionOrMap, InputActionChange change) |
| 170 | + { |
| 171 | + } |
| 172 | + } |
| 173 | +} |
0 commit comments