-
Notifications
You must be signed in to change notification settings - Fork 338
FIX: uum 144337 inputfixture teardown fails to reset input system #2437
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Darren-Kelly-Unity
wants to merge
5
commits into
develop
Choose a base branch
from
bugfix/UUM-144337-inputfixture-teardown-fails-to-reset-input-system
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ce9904b
Add test and fix for multiple touchscreen input bug.
Darren-Kelly-Unity 1adb456
Add fix for regression reported by u-pr.
Darren-Kelly-Unity b9b9359
Add base test and fix for incorrect teardown / reset.
Darren-Kelly-Unity 0caac9f
Add fix suggested by u-pr.
Darren-Kelly-Unity 7fa3703
Add fix for test issues.
Darren-Kelly-Unity File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
Assets/Tests/InputSystem.Editor/InputTestFixtureTeardownTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| using NUnit.Framework; | ||
| using UnityEngine; | ||
| using UnityEngine.InputSystem; | ||
|
|
||
| /// <summary> | ||
| /// Regression tests for IN-107889: InputTestFixture.TearDown() fails to reset Input System state | ||
| /// between consecutive scene-based tests. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Bug: When an <see cref="InputActionMap"/> belonging to the project-wide <see cref="InputSystem.actions"/> | ||
| /// asset is enabled before a test's <see cref="InputTestFixture.Setup"/> runs, the TearDown/Restore | ||
| /// cycle leaves the action map disconnected from its saved <see cref="InputActionState"/>. | ||
| /// | ||
| /// Root cause: | ||
| /// 1. <c>TestHook_DisableActions()</c> calls <c>Disable()</c> + <c>OnSetupChanged()</c> on the | ||
| /// project-wide asset's maps. <c>Disable()</c> modifies the action state memory (phases → Disabled) | ||
| /// on the <em>same</em> InputActionState objects that were just saved in the snapshot, because | ||
| /// <c>Disable()</c> is called after <c>SaveAndResetState()</c> but references the same managed objects. | ||
| /// 2. <c>OnSetupChanged()</c> sets <c>map.m_State = null</c>, disconnecting the map from its state. | ||
| /// 3. <c>Restore()</c> restores <c>s_GlobalState</c> (the registry) but does NOT restore the per-map | ||
| /// back-references (<c>InputActionMap.m_State</c>, <c>m_MapIndexInState</c>) or the action phase | ||
| /// memory. | ||
| /// | ||
| /// The fix: | ||
| /// 1. <c>TestHook_DisableActions()</c> should disconnect maps without modifying the saved state's memory, | ||
| /// so the saved snapshot retains the correct enabled phases. | ||
| /// 2. <c>Restore()</c> should re-link the back-references after <c>RestoreSavedState()</c> and | ||
| /// recompute <c>m_EnabledActionsCount</c> from the restored action phases. | ||
| /// </remarks> | ||
| internal class InputTestFixtureTeardownTests : InputTestFixture | ||
| { | ||
| // Simulates a scene with a PlayerInput component using project-wide actions. | ||
| // Created once before any [SetUp] runs, like a scene's OnEnable(). | ||
| private InputActionAsset m_PreTestAsset; | ||
| private InputActionMap m_PreTestMap; | ||
| private InputAction m_PreTestAction; | ||
|
|
||
| [OneTimeSetUp] | ||
| public void SimulateSceneLoad() | ||
| { | ||
| // Create an action asset simulating project-wide actions that a scene's | ||
| // PlayerInput component would use. | ||
| m_PreTestAsset = ScriptableObject.CreateInstance<InputActionAsset>(); | ||
| m_PreTestAsset.hideFlags = HideFlags.HideAndDontSave; | ||
| m_PreTestMap = m_PreTestAsset.AddActionMap("Player"); | ||
| m_PreTestAction = m_PreTestMap.AddAction("Fire", InputActionType.Button); | ||
|
|
||
| // Enable the map - creates an InputActionState and registers it in s_GlobalState. | ||
| // This simulates PlayerInput.ActivateInput() running before the test starts. | ||
| m_PreTestMap.Enable(); | ||
|
|
||
| // Register as project-wide actions on the real manager. | ||
| // This causes TestHook_DisableActions() to treat our asset like project-wide actions | ||
| // (i.e. call Disable() + OnSetupChanged() on it during each Setup()). | ||
| InputSystem.s_Manager.actions = m_PreTestAsset; | ||
| } | ||
|
|
||
| [OneTimeTearDown] | ||
| public void VerifyPreTestStatePreserved() | ||
| { | ||
| // After all test TearDowns, the action map should still be linked to its saved | ||
| // InputActionState and the enabled count should reflect the pre-test enabled state. | ||
| // | ||
| // With the bug: map.m_State is null and map.enabled is false because: | ||
| // - TestHook_DisableActions() called Disable() on the map (clearing enabled state | ||
| // in the shared state memory) then OnSetupChanged() (setting m_State = null) | ||
| // - Restore() restores s_GlobalState but not map.m_State or m_EnabledActionsCount | ||
| // | ||
| // With the fix: map.m_State is properly re-linked and map.enabled is true because: | ||
| // - TestHook_DisableActions() no longer modifies the saved state's memory | ||
| // - Restore() calls RelinkRestoredStates() to restore back-references and | ||
| // recompute m_EnabledActionsCount from the action phase memory | ||
|
|
||
| Assert.That(m_PreTestMap.m_State, Is.Not.Null, | ||
| "Action map should be linked to its saved InputActionState after Restore(). " + | ||
| "m_State was cleared by OnSetupChanged() during TestHook_DisableActions() " + | ||
| "and was not re-linked by Restore()."); | ||
|
|
||
| Assert.That(m_PreTestMap.enabled, Is.True, | ||
| "Action map should be enabled after Restore(): it was enabled before any test ran " + | ||
| "and should be restored to that enabled state after TearDown()."); | ||
|
|
||
| // Clean up | ||
| InputSystem.s_Manager.actions = null; | ||
| Object.DestroyImmediate(m_PreTestAsset); | ||
| } | ||
|
|
||
| [Test] | ||
| [Order(1)] | ||
| public void TearDown_FirstTest_ProjectWideActionsAreReenabledForTest() | ||
| { | ||
| // During this test, project-wide actions may have been re-enabled by TestHook_EnableActions | ||
| // (or left disabled if TestHook_EnableActions is a no-op for the test manager). | ||
| // We're just running to trigger a Setup/TearDown cycle. | ||
| Assert.Pass("First test ran successfully (triggering Setup/TearDown cycle)"); | ||
| } | ||
|
|
||
| [Test] | ||
| [Order(2)] | ||
| public void TearDown_SecondTest_StateRemainsCorrectAfterSecondCycle() | ||
| { | ||
| // After the first test's TearDown() + this Setup(), verify setup completes without errors. | ||
| // The [OneTimeTearDown] contains the actual assertion for the post-Restore() state. | ||
| Assert.Pass("Second test ran successfully (triggering second Setup/TearDown cycle)"); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| using NUnit.Framework; | ||
| using UnityEngine; | ||
| using UnityEngine.InputSystem; | ||
| using UnityEngine.InputSystem.LowLevel; | ||
| using TouchPhase = UnityEngine.InputSystem.TouchPhase; | ||
|
|
||
| // Tests covering touch tracking across multiple physical touchscreen monitors. | ||
| // Regression coverage for IN-108611: touches from one screen incorrectly matching | ||
| // ongoing touches from another screen when both screens report the same touchId. | ||
| [TestFixture] | ||
| internal class TouchscreenMultiDisplayTests : CoreTestsFixture | ||
| { | ||
| // When two physical touchscreens both have an active touch with the same touchId, | ||
| // a Move event from screen 2 must update screen 2's touch slot, not screen 1's. | ||
| // | ||
| // Failure mode (pre-fix): OnStateEvent matches ongoing touches by touchId alone, | ||
| // so screen 2's Move event finds screen 1's slot first (both have touchId=1) and | ||
| // incorrectly updates it, leaving screen 1's position changed and screen 2 stale. | ||
| [Test] | ||
| [Category("Devices")] | ||
| public void Devices_TouchMoveOnSecondDisplay_DoesNotUpdateTouchOnFirstDisplay() | ||
| { | ||
| var device = InputSystem.AddDevice<Touchscreen>(); | ||
|
|
||
| // Finger down on display 0 (touchId=1). | ||
| InputSystem.QueueStateEvent(device, new TouchState | ||
| { | ||
| phase = TouchPhase.Began, | ||
| touchId = 1, | ||
| position = new Vector2(100, 100), | ||
| displayIndex = 0, | ||
| }); | ||
|
|
||
| // Finger down on display 1 — same touchId, different screen. | ||
| InputSystem.QueueStateEvent(device, new TouchState | ||
| { | ||
| phase = TouchPhase.Began, | ||
| touchId = 1, | ||
| position = new Vector2(200, 200), | ||
| displayIndex = 1, | ||
| }); | ||
|
|
||
| InputSystem.Update(); | ||
|
|
||
| // Both touches should be allocated to separate slots. | ||
| Assert.That(device.touches[0].phase.ReadValue(), Is.EqualTo(TouchPhase.Began)); | ||
| Assert.That(device.touches[1].phase.ReadValue(), Is.EqualTo(TouchPhase.Began)); | ||
|
|
||
| var display0SlotIndex = -1; | ||
| var display1SlotIndex = -1; | ||
| for (var i = 0; i < 2; i++) | ||
| { | ||
| var displayIdx = device.touches[i].displayIndex.ReadValue(); | ||
| if (displayIdx == 0) display0SlotIndex = i; | ||
| else if (displayIdx == 1) display1SlotIndex = i; | ||
| } | ||
|
|
||
| Assert.That(display0SlotIndex, Is.Not.EqualTo(-1), "No touch slot found for display 0"); | ||
| Assert.That(display1SlotIndex, Is.Not.EqualTo(-1), "No touch slot found for display 1"); | ||
|
|
||
| var display0PositionBefore = device.touches[display0SlotIndex].position.ReadValue(); | ||
|
|
||
| // Swipe on display 1 (same touchId=1 as display 0's held touch). | ||
| InputSystem.QueueStateEvent(device, new TouchState | ||
| { | ||
| phase = TouchPhase.Moved, | ||
| touchId = 1, | ||
| position = new Vector2(300, 300), | ||
| displayIndex = 1, | ||
| }); | ||
|
|
||
| InputSystem.Update(); | ||
|
|
||
| // Display 0's touch must be unchanged. | ||
| Assert.That(device.touches[display0SlotIndex].position.ReadValue(), Is.EqualTo(display0PositionBefore), | ||
| "Touch on display 0 was incorrectly updated by a Move event from display 1"); | ||
|
|
||
| // Display 1's touch must reflect the new position. | ||
| Assert.That(device.touches[display1SlotIndex].position.ReadValue(), Is.EqualTo(new Vector2(300, 300)), | ||
| "Touch on display 1 was not updated by its own Move event"); | ||
|
|
||
| Assert.That(device.touches[display1SlotIndex].phase.ReadValue(), Is.EqualTo(TouchPhase.Moved)); | ||
| } | ||
| } |
2 changes: 2 additions & 0 deletions
2
Assets/Tests/InputSystem/TouchscreenMultiDisplayTests.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since
TestHook_DisableActions()now explicitly nulls outm_SharedStateForAllMapson the project-wide action asset,RelinkRestoredStates()needs to restore it here.Without this, the restored action maps will successfully reconnect their
m_State, but their parentInputActionAssetwill be left with a nullm_SharedStateForAllMaps. This breaks asset-level reactivity: methods likeInputActionAsset.bindingMaskandInputActionAsset.devicessetters rely onReResolveIfNecessary(), which returns early and does nothing ifm_SharedStateForAllMaps == null. As a result, subsequent tests that try to apply overrides to the project-wide asset would see those changes silently ignored.You can safely restore it using the map's asset reference:
Suggested replacement omitted because it is too large for an inline review comment.
🤖 Helpful? 👍/👎 by bug_hunter_agent