|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Linq; |
| 4 | +using NUnit.Framework; |
| 5 | +using UnityEditor; |
| 6 | +using UnityEditor.SceneManagement; |
| 7 | +using UnityEngine; |
| 8 | +using UnityEngine.InputSystem; |
| 9 | +using UnityEngine.InputSystem.Editor; |
| 10 | +using UnityEngine.SceneManagement; |
| 11 | +using UnityEngine.TestTools; |
| 12 | +using Object = UnityEngine.Object; |
| 13 | + |
| 14 | +namespace Tests.InputSystem.Editor |
| 15 | +{ |
| 16 | + /// <summary> |
| 17 | + /// Editor tests for <see cref="UnityEngine.InputSystem.InputActionReference"/>. |
| 18 | + /// </summary> |
| 19 | + /// <remarks> |
| 20 | + /// This test need fixed asset paths since mid-test domain reloads would otherwise discard data. |
| 21 | + /// |
| 22 | + /// Be aware that if you get failures in editor tests that switch between play mode and edit mode via coroutines |
| 23 | + /// you might get misleading stack traces that indicate errors in different places than they actually happen. |
| 24 | + /// At least this have been observered for exception stack traces. |
| 25 | + /// </remarks> |
| 26 | + internal class InputActionReferenceEditorTests |
| 27 | + { |
| 28 | + private Scene m_Scene; |
| 29 | + |
| 30 | + private const string assetPath = "Assets/__InputActionReferenceEditorTestsActions.inputactions"; |
| 31 | + private const string dummyPath = "Assets/__InputActionReferenceEditorTestsDummy.asset"; |
| 32 | + private const string scenePath = "Assets/__InputActionReferenceEditorTestsScene.unity"; |
| 33 | + |
| 34 | + private void CreateAsset() |
| 35 | + { |
| 36 | + var asset = ScriptableObject.CreateInstance<InputActionAsset>(); |
| 37 | + |
| 38 | + var map1 = new InputActionMap("map1"); |
| 39 | + map1.AddAction("action1"); |
| 40 | + map1.AddAction("action2"); |
| 41 | + asset.AddActionMap(map1); |
| 42 | + |
| 43 | + System.IO.File.WriteAllText(assetPath, asset.ToJson()); |
| 44 | + Object.DestroyImmediate(asset); |
| 45 | + AssetDatabase.ImportAsset(assetPath); |
| 46 | + } |
| 47 | + |
| 48 | + [SetUp] |
| 49 | + public void SetUp() |
| 50 | + { |
| 51 | + // This looks odd, but when we yield into play mode from our test coroutine we may get a domain reload |
| 52 | + // (depending on editor preferences) which will trigger another SetUp() mid-test. |
| 53 | + if (Application.isPlaying) |
| 54 | + return; |
| 55 | + |
| 56 | + m_Scene = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene); |
| 57 | + CreateAsset(); |
| 58 | + |
| 59 | + var go = new GameObject("Root"); |
| 60 | + var behaviour = go.AddComponent<InputActionBehaviour>(); |
| 61 | + var reference = InputActionImporter.LoadInputActionReferencesFromAsset(assetPath).First( |
| 62 | + r => "action1".Equals(r.action.name)); |
| 63 | + behaviour.referenceAsField = reference; |
| 64 | + behaviour.referenceAsReference = reference; |
| 65 | + |
| 66 | + TestUtils.SaveScene(m_Scene, scenePath); |
| 67 | + } |
| 68 | + |
| 69 | + [TearDown] |
| 70 | + public void TearDown() |
| 71 | + { |
| 72 | + // This looks odd, but when we yield into play mode from our test coroutine we may get a domain reload |
| 73 | + // (depending on editor preferences) which will trigger another TearDown() mid-test. |
| 74 | + if (Application.isPlaying) |
| 75 | + return; |
| 76 | + |
| 77 | + // Close scene |
| 78 | + EditorSceneManager.CloseScene(m_Scene, true); |
| 79 | + |
| 80 | + // Clean-up |
| 81 | + AssetDatabase.DeleteAsset(dummyPath); |
| 82 | + AssetDatabase.DeleteAsset(assetPath); |
| 83 | + AssetDatabase.DeleteAsset(scenePath); |
| 84 | + } |
| 85 | + |
| 86 | + private static InputActionBehaviour GetBehaviour() => Object.FindObjectOfType<InputActionBehaviour>(); |
| 87 | + private static InputActionAsset GetAsset() => AssetDatabase.LoadAssetAtPath<InputActionAsset>(assetPath); |
| 88 | + |
| 89 | + // For unclear reason, NUnit fails to assert throwing exceptions after transition into play-mode. |
| 90 | + // So until that can be sorted out, we do it manually (in the same way) ourselves. |
| 91 | + private static void AssertThrows<T>(Action action) where T : Exception |
| 92 | + { |
| 93 | + var exceptionThrown = false; |
| 94 | + try |
| 95 | + { |
| 96 | + action(); |
| 97 | + } |
| 98 | + catch (InvalidOperationException) |
| 99 | + { |
| 100 | + exceptionThrown = true; |
| 101 | + } |
| 102 | + Assert.IsTrue(exceptionThrown, $"Expected exception of type {typeof(T)} to be thrown but it was not."); |
| 103 | + } |
| 104 | + |
| 105 | + [UnityTest] |
| 106 | + [Description("https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-1584")] |
| 107 | + public IEnumerator ReferenceSetInPlaymodeShouldBeRestored_WhenExitingPlaymode() |
| 108 | + { |
| 109 | + // Edit-mode section |
| 110 | + { |
| 111 | + // Sanity check our initial edit-mode state |
| 112 | + var obj = GetBehaviour(); |
| 113 | + Assert.That(obj.referenceAsField.action, Is.SameAs(GetAsset().FindAction("map1/action1"))); |
| 114 | + Assert.That(obj.referenceAsReference.action, Is.SameAs(GetAsset().FindAction("map1/action1"))); |
| 115 | + |
| 116 | + // Enter play-mode (This will lead to domain reload by default). |
| 117 | + yield return new EnterPlayMode(); |
| 118 | + } |
| 119 | + |
| 120 | + // Play-mode section |
| 121 | + { |
| 122 | + var obj = GetBehaviour(); |
| 123 | + var editModeAction = GetAsset().FindAction("map1/action1"); |
| 124 | + var playModeAction = GetAsset().FindAction("map1/action2"); |
| 125 | + |
| 126 | + // Make sure our action reference is consistent in play-mode |
| 127 | + Assert.That(obj.referenceAsField.action, Is.SameAs(editModeAction)); |
| 128 | + |
| 129 | + // ISXB-1584: Attempting assignment of persisted input action reference in play-mode in editor. |
| 130 | + // Rationale: We cannot allow this since it would corrupt the source asset since changes applied to SO |
| 131 | + // mapped to an asset isn't reverted when exiting play-mode. |
| 132 | + // |
| 133 | + // Here we would like to do: |
| 134 | + // Assert.Throws<InvalidOperationException>(() => obj.reference.Set(null)); |
| 135 | + // |
| 136 | + // But we can't since it would fail with NullReferenceException. |
| 137 | + // It turns out that because of the domain reload / Unity’s internal serialization quirks, the obj is |
| 138 | + // sometimes null inside the lambda when NUnit captures it for execution. So to work around this we |
| 139 | + // instead do the same kind of check manually for now which doesn't seem to have this problem. |
| 140 | + // |
| 141 | + // It is odd since NUnit does basically does the same thing (apart from wrapping the lambda as a |
| 142 | + // TestDelegate). So the WHY for this problem remains unclear for now. |
| 143 | + AssertThrows<InvalidOperationException>(() => obj.referenceAsField.Set(playModeAction)); |
| 144 | + AssertThrows<InvalidOperationException>(() => obj.referenceAsReference.Set(editModeAction)); |
| 145 | + |
| 146 | + // Make sure there were no side-effects. |
| 147 | + Assert.That(obj.referenceAsField.action, Is.SameAs(editModeAction)); |
| 148 | + Assert.That(obj.referenceAsReference.action, Is.SameAs(editModeAction)); |
| 149 | + |
| 150 | + // Correct usage is to use a run-time assigned input action reference instead. It is up to the user |
| 151 | + // to decide whether this reference should additionally be persisted (which is possible by saving it to |
| 152 | + // and asset, or by using SerializeReference). |
| 153 | + obj.referenceAsField = InputActionReference.Create(playModeAction); |
| 154 | + obj.referenceAsReference = InputActionReference.Create(playModeAction); |
| 155 | + |
| 156 | + // Makes sure we have the expected reference. |
| 157 | + Assert.That(obj.referenceAsField.action, Is.SameAs(playModeAction)); |
| 158 | + Assert.That(obj.referenceAsReference.action, Is.SameAs(playModeAction)); |
| 159 | + |
| 160 | + // Exit play-mode (This will lead to domain reload by default). |
| 161 | + yield return new ExitPlayMode(); |
| 162 | + } |
| 163 | + |
| 164 | + // Edit-mode section |
| 165 | + { |
| 166 | + // Make sure our reference is back to its initial edit mode state |
| 167 | + var obj = GetBehaviour(); |
| 168 | + var editModeAction = GetAsset().FindAction("map1/action1"); |
| 169 | + Assert.That(obj.referenceAsField.action, Is.SameAs(editModeAction)); |
| 170 | + Assert.That(obj.referenceAsReference.action, Is.SameAs(editModeAction)); |
| 171 | + } |
| 172 | + |
| 173 | + yield return null; |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments