ModalButtonStateTests.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System.Reflection;
  2. using DevilLink2.UI;
  3. using NUnit.Framework;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.UI;
  7. namespace DevilLink2.Tests
  8. {
  9. public sealed class ModalButtonStateTests
  10. {
  11. private GameObject root;
  12. private UIFeedbackButton button;
  13. private Sprite normal, disabled;
  14. private Texture2D texture;
  15. private static bool PointerDown(Selectable value) => (bool)typeof(Selectable)
  16. .GetProperty("isPointerDown", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(value);
  17. [SetUp] public void Setup()
  18. {
  19. root = new GameObject("ModalTest", typeof(RectTransform));
  20. UITransition.Show(root, true);
  21. var child = new GameObject("Button", typeof(RectTransform), typeof(Image)); child.transform.SetParent(root.transform, false);
  22. button = child.AddComponent<UIFeedbackButton>(); button.targetGraphic = child.GetComponent<Image>();
  23. button.PreserveAppearanceDuringModalLock = true;
  24. texture = new Texture2D(2, 2);
  25. normal = Sprite.Create(texture, new Rect(0, 0, 2, 2), Vector2.one * .5f);
  26. disabled = Sprite.Create(texture, new Rect(0, 0, 1, 1), Vector2.one * .5f);
  27. button.image.sprite = normal; button.transition = Selectable.Transition.SpriteSwap;
  28. button.spriteState = new SpriteState { disabledSprite = disabled, highlightedSprite = normal, selectedSprite = normal };
  29. }
  30. [TearDown] public void Cleanup()
  31. { Object.DestroyImmediate(root); Object.DestroyImmediate(normal); Object.DestroyImmediate(disabled); Object.DestroyImmediate(texture); }
  32. [Test] public void RejectedPointerDownDoesNotLatchSelectablePress()
  33. {
  34. UITransition.SetInput(root, false);
  35. button.OnPointerDown(new PointerEventData(null) { button = PointerEventData.InputButton.Left });
  36. Assert.That(PointerDown(button), Is.False);
  37. }
  38. [Test] public void ModalLockPreservesNormalSpriteButStillBlocksActivation()
  39. {
  40. UITransition.SetInput(root, false);
  41. button.SendMessage("OnCanvasGroupChanged", SendMessageOptions.RequireReceiver);
  42. Assert.That(button.IsInteractable(), Is.False);
  43. Assert.That(button.image.overrideSprite, Is.SameAs(normal));
  44. Assert.That(button.TryActivate(1), Is.False);
  45. }
  46. [Test] public void RealDisabledStateRetainsDisabledSprite()
  47. {
  48. button.interactable = false;
  49. Assert.That(button.image.overrideSprite, Is.SameAs(disabled));
  50. Assert.That(button.TryActivate(1), Is.False);
  51. }
  52. [Test] public void ModalLockCancelsAcceptedPressWithoutChangingNavigation()
  53. {
  54. var navigation = button.navigation;
  55. button.OnPointerDown(new PointerEventData(null) { button = PointerEventData.InputButton.Left });
  56. Assert.That(PointerDown(button), Is.True);
  57. UITransition.SetInput(root, false);
  58. button.SendMessage("OnCanvasGroupChanged", SendMessageOptions.RequireReceiver);
  59. Assert.That(PointerDown(button), Is.False);
  60. Assert.That(button.transform.localScale, Is.EqualTo(Vector3.one));
  61. Assert.That(button.navigation.mode, Is.EqualTo(navigation.mode));
  62. }
  63. }
  64. }