|
1 | 1 | using System; |
| 2 | +using System.Collections; |
2 | 3 | using System.Collections.Generic; |
3 | 4 | using NUnit.Framework; |
4 | 5 | using UnityEngine; |
5 | 6 | using UnityEngine.InputSystem; |
6 | 7 | using UnityEngine.InputSystem.Controls; |
| 8 | +using UnityEngine.TestTools; |
7 | 9 |
|
8 | 10 | internal static class PriorityTestExtensions |
9 | 11 | { |
@@ -66,11 +68,6 @@ private static IEnumerable<TwoInputActionDataWrapper<InputAction, InputAction>> |
66 | 68 | Action1 = map.SetupTestAction("ctrl", "shift", "v"), |
67 | 69 | Action2 = map.SetupTestAction("shift", "v") |
68 | 70 | }; |
69 | | - // yield return new TwoInputActionDataWrapper<InputAction, InputAction> |
70 | | - // { |
71 | | - // Action1 = map.SetupTestAction("ctrl", "enter", "w"), |
72 | | - // Action2 = map.SetupTestAction("alt", "shift", "w") |
73 | | - // }; |
74 | 71 | } |
75 | 72 |
|
76 | 73 | public class TwoInputActionDataWrapper<TInputAction1, TInputAction2> |
@@ -408,14 +405,6 @@ public void AltShiftW_Only_Triggers_TeamChat(ThreeInputActionDataWrapper<InputAc |
408 | 405 |
|
409 | 406 |
|
410 | 407 | Assert.That(threeInputActions.Action1.WasPerformedThisFrame(), Is.True); |
411 | | - |
412 | | - //Assert.IsTrue(threeInputActions.Action1.WasPerformedThisFrame(), "Team chat should be activated by Alt+Shift+W."); |
413 | | - // Assert.IsFalse(moveAction.IsPressed(), "Move should not be activated when Team chat takes priority."); |
414 | | - // Assert.IsFalse(runFastAction.IsPressed(), "Run Fast should not be activated when Team chat takes priority."); |
415 | | - |
416 | | - // Release(keyboard.wKey); |
417 | | - // Release(keyboard.leftShiftKey); |
418 | | - // Release(keyboard.altKey); |
419 | 408 | } |
420 | 409 |
|
421 | 410 | [Test] |
@@ -539,4 +528,102 @@ public unsafe void Actions_Priority_ControlGrouping_WritesPerControlSlotPriority |
539 | 528 | Assert.That(state.memory.controlGroupingAndPriority[pLow], Is.EqualTo(4)); |
540 | 529 | Assert.That(state.memory.controlGroupingAndPriority[pHigh], Is.EqualTo(11)); |
541 | 530 | } |
| 531 | + |
| 532 | + /// <summary> |
| 533 | + /// Shift+B with <c>hold(duration=2)</c> reaches <see cref="InputActionPhase.Performed"/> after two seconds of |
| 534 | + /// continuous hold (real time on the test runtime clock). |
| 535 | + /// </summary> |
| 536 | + [UnityTest] |
| 537 | + [Category("Actions Priority")] |
| 538 | + public IEnumerator Actions_Priority_BothActionsArePerformed_WhenAHoldAndBasicActionHaveDifferentTiming() |
| 539 | + { |
| 540 | + var keyboard = InputSystem.AddDevice<Keyboard>(); |
| 541 | + using var map = new InputActionMap("HoldChord"); |
| 542 | + |
| 543 | + var plainB = map.AddAction("PlainB", InputActionType.Button, "<Keyboard>/b"); |
| 544 | + var shiftBHold = map.AddAction("ShiftBHold", InputActionType.Button, binding: null, interactions: "hold(duration=2)"); |
| 545 | + shiftBHold.AddCompositeBinding("OneModifier(modifiersOrder=2)") |
| 546 | + .With("modifier", "<Keyboard>/shift") |
| 547 | + .With("binding", "<Keyboard>/b"); |
| 548 | + plainB.Priority = 0; |
| 549 | + shiftBHold.Priority = 1; |
| 550 | + |
| 551 | + var plainBPerformed = false; |
| 552 | + plainB.performed += _ => plainBPerformed = true; |
| 553 | + |
| 554 | + map.Enable(); |
| 555 | + |
| 556 | + var t0 = currentTime; |
| 557 | + Press(keyboard.leftShiftKey); |
| 558 | + Press(keyboard.bKey); |
| 559 | + InputSystem.Update(); |
| 560 | + yield return null; |
| 561 | + |
| 562 | + Assert.AreNotEqual( |
| 563 | + InputActionPhase.Performed, |
| 564 | + shiftBHold.phase, |
| 565 | + "Hold should not be Performed until the hold duration elapses."); |
| 566 | + |
| 567 | + currentTime = t0 + 2.1; |
| 568 | + InputSystem.Update(); |
| 569 | + yield return null; |
| 570 | + |
| 571 | + Assert.IsTrue(plainBPerformed); |
| 572 | + Assert.IsTrue( |
| 573 | + shiftBHold.phase == InputActionPhase.Performed, |
| 574 | + "Hold should complete to Performed after the hold duration with keys still down."); |
| 575 | + |
| 576 | + Release(keyboard.bKey); |
| 577 | + Release(keyboard.leftShiftKey); |
| 578 | + map.Disable(); |
| 579 | + } |
| 580 | + |
| 581 | + /// <summary> |
| 582 | + /// Shift+B with <c>hold(duration=2)</c> reaches <see cref="InputActionPhase.Performed"/> after two seconds of |
| 583 | + /// continuous hold (real time on the test runtime clock). |
| 584 | + /// </summary> |
| 585 | + [UnityTest] |
| 586 | + [Category("Actions Priority")] |
| 587 | + public IEnumerator Actions_Priority_OnlyOneHoldActionIsPerformed_WhenOnePriorityIsHigher() |
| 588 | + { |
| 589 | + var keyboard = InputSystem.AddDevice<Keyboard>(); |
| 590 | + using var map = new InputActionMap("HoldChord"); |
| 591 | + |
| 592 | + var plainB = map.AddAction("PlainB", InputActionType.Button, "<Keyboard>/b", interactions: "hold(duration=2)"); |
| 593 | + var shiftBHold = map.AddAction("ShiftBHold", InputActionType.Button, binding: null, interactions: "hold(duration=2)"); |
| 594 | + shiftBHold.AddCompositeBinding("OneModifier(modifiersOrder=2)") |
| 595 | + .With("modifier", "<Keyboard>/shift") |
| 596 | + .With("binding", "<Keyboard>/b"); |
| 597 | + plainB.Priority = 0; |
| 598 | + shiftBHold.Priority = 1; |
| 599 | + |
| 600 | + var plainBPerformed = false; |
| 601 | + plainB.performed += _ => plainBPerformed = true; |
| 602 | + |
| 603 | + map.Enable(); |
| 604 | + |
| 605 | + var t0 = currentTime; |
| 606 | + Press(keyboard.leftShiftKey); |
| 607 | + Press(keyboard.bKey); |
| 608 | + InputSystem.Update(); |
| 609 | + yield return null; |
| 610 | + |
| 611 | + Assert.AreNotEqual( |
| 612 | + InputActionPhase.Performed, |
| 613 | + shiftBHold.phase, |
| 614 | + "Hold should not be Performed until the hold duration elapses."); |
| 615 | + |
| 616 | + currentTime = t0 + 2.1; |
| 617 | + InputSystem.Update(); |
| 618 | + yield return null; |
| 619 | + |
| 620 | + Assert.IsTrue(plainBPerformed); |
| 621 | + Assert.IsTrue( |
| 622 | + shiftBHold.phase == InputActionPhase.Performed, |
| 623 | + "Hold should complete to Performed after the hold duration with keys still down."); |
| 624 | + |
| 625 | + Release(keyboard.bKey); |
| 626 | + Release(keyboard.leftShiftKey); |
| 627 | + map.Disable(); |
| 628 | + } |
542 | 629 | } |
0 commit comments