|
1 | 1 | using System; |
2 | | -using System.Collections; |
3 | 2 | using System.Collections.Generic; |
4 | 3 | using NUnit.Framework; |
5 | 4 | using UnityEngine; |
6 | 5 | using UnityEngine.InputSystem; |
7 | 6 | using UnityEngine.InputSystem.Controls; |
8 | | -using UnityEngine.TestTools; |
9 | 7 |
|
10 | 8 | internal static class PriorityTestExtensions |
11 | 9 | { |
@@ -340,155 +338,77 @@ public void Actions_Priority_BothActionsWithDifferentPriorityFire_WhenThereIsNoC |
340 | 338 | // Assert.That(action2.WasPerformedThisFrame(), Is.False); |
341 | 339 | } |
342 | 340 |
|
343 | | - [UnityTest] |
| 341 | + [Test] |
344 | 342 | [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) |
346 | 345 | { |
347 | | - string sharedModifier = "ctrl"; |
348 | | - string binding1 = "x"; |
349 | | - string binding2 = "c"; |
350 | | - |
351 | 346 | InputSystem.settings.shortcutKeysConsumeInput = true; |
352 | 347 | var keyboard = InputSystem.AddDevice<Keyboard>(); |
353 | 348 |
|
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; |
368 | 351 |
|
369 | | - map.Enable(); |
| 352 | + action1.Priority = 15; |
| 353 | + action2.Priority = 5; |
370 | 354 |
|
| 355 | + action1.m_ActionMap.Enable(); |
| 356 | + action2.m_ActionMap.Enable(); |
| 357 | + // |
371 | 358 | var action1WasPerformed = false; |
372 | 359 | action1.performed += _ => action1WasPerformed = true; |
373 | 360 |
|
374 | 361 | Assert.That(action1.WasPerformedThisFrame(), Is.False); |
375 | 362 | Assert.That(action2.WasPerformedThisFrame(), Is.False); |
376 | 363 |
|
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); |
381 | 365 |
|
| 366 | + // Different letter keys: no conflict on the same control, so both shortcuts can perform despite different priorities. |
382 | 367 | Assert.That(action1WasPerformed, Is.True); |
383 | 368 | Assert.That(action2.WasPerformedThisFrame(), Is.True); |
384 | 369 |
|
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); |
394 | 376 | } |
395 | 377 |
|
396 | | - [UnityTest] |
| 378 | + [Test] |
397 | 379 | [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) |
399 | 382 | { |
400 | | - string sharedModifier = "ctrl"; |
401 | | - string binding1 = "x"; |
402 | | - string binding2 = "c"; |
403 | | - |
404 | 383 | InputSystem.settings.shortcutKeysConsumeInput = true; |
405 | 384 | var keyboard = InputSystem.AddDevice<Keyboard>(); |
406 | 385 |
|
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; |
417 | 388 |
|
418 | | - action1.Priority = 500; |
419 | | - action2.Priority = 500; |
| 389 | + action1.Priority = 5; |
| 390 | + action2.Priority = 5; |
420 | 391 |
|
421 | | - map.Enable(); |
| 392 | + action1.m_ActionMap.Enable(); |
| 393 | + action2.m_ActionMap.Enable(); |
422 | 394 |
|
423 | 395 | var action1WasPerformed = false; |
424 | 396 | action1.performed += _ => action1WasPerformed = true; |
425 | 397 |
|
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 | | - |
441 | 398 | Assert.That(action1.WasPerformedThisFrame(), Is.False); |
442 | 399 | 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; |
470 | 400 |
|
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); |
480 | 402 |
|
| 403 | + // Different letter keys: no conflict on the same control, so both shortcuts can perform despite different priorities. |
481 | 404 | Assert.That(action1WasPerformed, Is.True); |
482 | 405 | Assert.That(action2.WasPerformedThisFrame(), Is.True); |
483 | 406 |
|
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); |
493 | 413 | } |
494 | 414 | } |
0 commit comments