Skip to content

Commit 86d55b1

Browse files
Add progress with converting / refactoring tests to use TestCaseSource.
1 parent 29aaa21 commit 86d55b1

1 file changed

Lines changed: 35 additions & 115 deletions

File tree

Assets/Tests/InputSystem/CoreTests_ActionsPriority.cs

Lines changed: 35 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
using System;
2-
using System.Collections;
32
using System.Collections.Generic;
43
using NUnit.Framework;
54
using UnityEngine;
65
using UnityEngine.InputSystem;
76
using UnityEngine.InputSystem.Controls;
8-
using UnityEngine.TestTools;
97

108
internal static class PriorityTestExtensions
119
{
@@ -340,155 +338,77 @@ public void Actions_Priority_BothActionsWithDifferentPriorityFire_WhenThereIsNoC
340338
// Assert.That(action2.WasPerformedThisFrame(), Is.False);
341339
}
342340

343-
[UnityTest]
341+
[Test]
344342
[Category("Actions Priority")]
345-
public IEnumerator Actions_Priority_TwoNonConflictingShortcuts_ReversedPriorityOrder_BothStillPerform()
343+
[TestCaseSource(nameof(TwoInputActionNoConflictingBindingTestCases))]
344+
public void Actions_Priority_BothActionsWithDifferentPriorityFire_WhenThereIsNoConflictingBindingInverseOrder(TwoInputActionDataWrapper<InputAction, InputAction> twoInputActions)
346345
{
347-
string sharedModifier = "ctrl";
348-
string binding1 = "x";
349-
string binding2 = "c";
350-
351346
InputSystem.settings.shortcutKeysConsumeInput = true;
352347
var keyboard = InputSystem.AddDevice<Keyboard>();
353348

354-
var map = new InputActionMap("map");
355-
var action1 = map.AddAction("action1");
356-
action1.AddCompositeBinding(("OneModifier") + "(overrideModifiersNeedToBePressedFirst)")
357-
.With("Modifier", "<Keyboard>/" + sharedModifier)
358-
.With("Binding", "<Keyboard>/" + binding1);
359-
360-
var action2 = map.AddAction("action2");
361-
action2.AddCompositeBinding(("OneModifier") + "(overrideModifiersNeedToBePressedFirst)")
362-
.With("Modifier", "<Keyboard>/" + sharedModifier)
363-
.With("Binding", "<Keyboard>/" + binding2);
364-
365-
// Higher priority on the first action; letter keys still differ so both should run.
366-
action1.Priority = 10;
367-
action2.Priority = 0;
349+
var action1 = twoInputActions.Action1;
350+
var action2 = twoInputActions.Action2;
368351

369-
map.Enable();
352+
action1.Priority = 15;
353+
action2.Priority = 5;
370354

355+
action1.m_ActionMap.Enable();
356+
action2.m_ActionMap.Enable();
357+
//
371358
var action1WasPerformed = false;
372359
action1.performed += _ => action1WasPerformed = true;
373360

374361
Assert.That(action1.WasPerformedThisFrame(), Is.False);
375362
Assert.That(action2.WasPerformedThisFrame(), Is.False);
376363

377-
Press((ButtonControl)keyboard[sharedModifier], queueEventOnly: true);
378-
Press((ButtonControl)keyboard[binding1], queueEventOnly: true);
379-
Press((ButtonControl)keyboard[binding2], queueEventOnly: true);
380-
InputSystem.Update();
364+
PressBindingsForInputActions(keyboard, action1, action2);
381365

366+
// Different letter keys: no conflict on the same control, so both shortcuts can perform despite different priorities.
382367
Assert.That(action1WasPerformed, Is.True);
383368
Assert.That(action2.WasPerformedThisFrame(), Is.True);
384369

385-
yield return null;
386-
387-
Press((ButtonControl)keyboard[binding1], queueEventOnly: true);
388-
Press((ButtonControl)keyboard[binding2], queueEventOnly: true);
389-
390-
InputSystem.Update();
391-
392-
Assert.That(action1.WasPerformedThisFrame(), Is.False);
393-
Assert.That(action2.WasPerformedThisFrame(), Is.False);
370+
// TODO: Darren, trigger just the bindings again to be sure the shortcut doesn't trigger for a second time
371+
// Press((ButtonControl)keyboard[action1.GetBind], queueEventOnly: true);
372+
// Press((ButtonControl)keyboard[action2.controls[i].name], queueEventOnly: true);
373+
//
374+
// Assert.That(action1.WasPerformedThisFrame(), Is.False);
375+
// Assert.That(action2.WasPerformedThisFrame(), Is.False);
394376
}
395377

396-
[UnityTest]
378+
[Test]
397379
[Category("Actions Priority")]
398-
public IEnumerator Actions_Priority_TwoNonConflictingShortcuts_EqualHighPriority_BothPerform()
380+
[TestCaseSource(nameof(TwoInputActionNoConflictingBindingTestCases))]
381+
public void Actions_Priority_BothActionsWithEqualPriorityFire_WhenThereIsNoConflictingBinding(TwoInputActionDataWrapper<InputAction, InputAction> twoInputActions)
399382
{
400-
string sharedModifier = "ctrl";
401-
string binding1 = "x";
402-
string binding2 = "c";
403-
404383
InputSystem.settings.shortcutKeysConsumeInput = true;
405384
var keyboard = InputSystem.AddDevice<Keyboard>();
406385

407-
var map = new InputActionMap("map");
408-
var action1 = map.AddAction("action1");
409-
action1.AddCompositeBinding(("OneModifier") + "(overrideModifiersNeedToBePressedFirst)")
410-
.With("Modifier", "<Keyboard>/" + sharedModifier)
411-
.With("Binding", "<Keyboard>/" + binding1);
412-
413-
var action2 = map.AddAction("action2");
414-
action2.AddCompositeBinding(("OneModifier") + "(overrideModifiersNeedToBePressedFirst)")
415-
.With("Modifier", "<Keyboard>/" + sharedModifier)
416-
.With("Binding", "<Keyboard>/" + binding2);
386+
var action1 = twoInputActions.Action1;
387+
var action2 = twoInputActions.Action2;
417388

418-
action1.Priority = 500;
419-
action2.Priority = 500;
389+
action1.Priority = 5;
390+
action2.Priority = 5;
420391

421-
map.Enable();
392+
action1.m_ActionMap.Enable();
393+
action2.m_ActionMap.Enable();
422394

423395
var action1WasPerformed = false;
424396
action1.performed += _ => action1WasPerformed = true;
425397

426-
Press((ButtonControl)keyboard[sharedModifier], queueEventOnly: true);
427-
Press((ButtonControl)keyboard[binding1], queueEventOnly: true);
428-
Press((ButtonControl)keyboard[binding2], queueEventOnly: true);
429-
InputSystem.Update();
430-
431-
Assert.That(action1WasPerformed, Is.True);
432-
Assert.That(action2.WasPerformedThisFrame(), Is.True);
433-
434-
yield return null;
435-
436-
Press((ButtonControl)keyboard[binding1], queueEventOnly: true);
437-
Press((ButtonControl)keyboard[binding2], queueEventOnly: true);
438-
439-
InputSystem.Update();
440-
441398
Assert.That(action1.WasPerformedThisFrame(), Is.False);
442399
Assert.That(action2.WasPerformedThisFrame(), Is.False);
443-
}
444-
445-
[UnityTest]
446-
[Category("Actions Priority")]
447-
public IEnumerator Actions_Priority_TwoNonConflictingShortcuts_ReverseActionDeclarationOrder_BothPerform()
448-
{
449-
string sharedModifier = "ctrl";
450-
string binding1 = "x";
451-
string binding2 = "c";
452-
453-
InputSystem.settings.shortcutKeysConsumeInput = true;
454-
var keyboard = InputSystem.AddDevice<Keyboard>();
455-
456-
var map = new InputActionMap("map");
457-
// Add the second shortcut's action first so registration order differs from the original test.
458-
var action2 = map.AddAction("action2");
459-
action2.AddCompositeBinding(("OneModifier") + "(overrideModifiersNeedToBePressedFirst)")
460-
.With("Modifier", "<Keyboard>/" + sharedModifier)
461-
.With("Binding", "<Keyboard>/" + binding2);
462-
463-
var action1 = map.AddAction("action1");
464-
action1.AddCompositeBinding(("OneModifier") + "(overrideModifiersNeedToBePressedFirst)")
465-
.With("Modifier", "<Keyboard>/" + sharedModifier)
466-
.With("Binding", "<Keyboard>/" + binding1);
467-
468-
action1.Priority = 0;
469-
action2.Priority = 1;
470400

471-
map.Enable();
472-
473-
var action1WasPerformed = false;
474-
action1.performed += _ => action1WasPerformed = true;
475-
476-
Press((ButtonControl)keyboard[sharedModifier], queueEventOnly: true);
477-
Press((ButtonControl)keyboard[binding1], queueEventOnly: true);
478-
Press((ButtonControl)keyboard[binding2], queueEventOnly: true);
479-
InputSystem.Update();
401+
PressBindingsForInputActions(keyboard, action1, action2);
480402

403+
// Different letter keys: no conflict on the same control, so both shortcuts can perform despite different priorities.
481404
Assert.That(action1WasPerformed, Is.True);
482405
Assert.That(action2.WasPerformedThisFrame(), Is.True);
483406

484-
yield return null;
485-
486-
Press((ButtonControl)keyboard[binding1], queueEventOnly: true);
487-
Press((ButtonControl)keyboard[binding2], queueEventOnly: true);
488-
489-
InputSystem.Update();
490-
491-
Assert.That(action1.WasPerformedThisFrame(), Is.False);
492-
Assert.That(action2.WasPerformedThisFrame(), Is.False);
407+
// TODO: Darren, trigger just the bindings again to be sure the shortcut doesn't trigger for a second time
408+
// Press((ButtonControl)keyboard[action1.GetBind], queueEventOnly: true);
409+
// Press((ButtonControl)keyboard[action2.controls[i].name], queueEventOnly: true);
410+
//
411+
// Assert.That(action1.WasPerformedThisFrame(), Is.False);
412+
// Assert.That(action2.WasPerformedThisFrame(), Is.False);
493413
}
494414
}

0 commit comments

Comments
 (0)