UITransitionTests.cs 5.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using DevilLink2.UI;
  2. using NUnit.Framework;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using UnityEngine.UI;
  6. namespace DevilLink2.Tests
  7. {
  8. public sealed class UITransitionTests
  9. {
  10. private GameObject root;
  11. [SetUp] public void Setup() { root = new GameObject("TransitionTest", typeof(RectTransform)); }
  12. [TearDown] public void Cleanup() { Object.DestroyImmediate(root); }
  13. [Test] public void RepeatedOpenCloseReusesScreenAndCanvasGroup()
  14. {
  15. var screen = root.AddComponent<UIScreen>();
  16. for (int i = 0; i < 5; i++) { screen.SetVisible(true); screen.SetVisible(true); screen.SetVisible(false); }
  17. Assert.That(root.GetComponents<UITransition>().Length, Is.EqualTo(1));
  18. Assert.That(root.GetComponents<CanvasGroup>().Length, Is.EqualTo(1));
  19. Assert.That(screen.IsVisible, Is.False); Assert.That(root.activeSelf, Is.False);
  20. Assert.That(root.GetComponent<CanvasGroup>().blocksRaycasts, Is.False);
  21. }
  22. [Test] public void StaticVisibilityPreservesRectAndNormalizesCardScale()
  23. {
  24. var card = new GameObject("Card", typeof(RectTransform)); card.transform.SetParent(root.transform, false);
  25. card.transform.localScale = new Vector3(.9f, .9f, 1);
  26. var rect = (RectTransform)card.transform; rect.sizeDelta = new Vector2(330, 400); rect.anchoredPosition = new Vector2(12, -20);
  27. UITransition.Show(root, true); UITransition.Show(root, false); UITransition.Show(root, true);
  28. Assert.That(rect.sizeDelta, Is.EqualTo(new Vector2(330, 400)));
  29. Assert.That(rect.anchoredPosition, Is.EqualTo(new Vector2(12, -20)));
  30. Assert.That(rect.localScale, Is.EqualTo(Vector3.one));
  31. Assert.That(root.GetComponent<UITransition>().IsTransitioning, Is.False);
  32. }
  33. [Test] public void ModalInputLockSurvivesRepeatedShow()
  34. {
  35. UITransition.Show(root, true); UITransition.SetInput(root, false); UITransition.Show(root, true);
  36. Assert.That(root.GetComponent<CanvasGroup>().interactable, Is.False);
  37. UITransition.SetInput(root, true); Assert.That(root.GetComponent<CanvasGroup>().interactable, Is.True);
  38. }
  39. [Test] public void NestedPanelLogicalVisibilityAndExternalDisableRecover()
  40. {
  41. UITransition.Show(root, true); root.SetActive(false); UITransition.Show(root, true);
  42. Assert.That(root.activeSelf, Is.True); Assert.That(UITransition.IsVisible(root), Is.True);
  43. UITransition.Show(root, false); Assert.That(UITransition.IsVisible(root), Is.False);
  44. }
  45. [Test] public void FastButtonActivationInvokesExistingListenerOnce()
  46. {
  47. var button = root.AddComponent<UIFeedbackButton>(); int clicked = 0; button.onClick.AddListener(() => clicked++);
  48. Assert.That(button.TryActivate(1), Is.True); Assert.That(button.TryActivate(1.01), Is.False);
  49. Assert.That(clicked, Is.EqualTo(1)); Assert.That(button.TryActivate(1.2), Is.True); Assert.That(clicked, Is.EqualTo(2));
  50. }
  51. [Test] public void DisabledButtonNeitherActivatesNorShrinks()
  52. {
  53. var button = root.AddComponent<UIFeedbackButton>(); int clicked = 0; button.onClick.AddListener(() => clicked++);
  54. button.interactable = false;
  55. button.OnPointerDown(new PointerEventData(null) { button = PointerEventData.InputButton.Left });
  56. Assert.That(button.TryActivate(1), Is.False); Assert.That(clicked, Is.Zero);
  57. Assert.That(root.transform.localScale, Is.EqualTo(Vector3.one));
  58. }
  59. [Test] public void PressReleaseAndDisableRestoreScaleWithoutAddingObjects()
  60. {
  61. var button = root.AddComponent<UIFeedbackButton>();
  62. var pointer = new PointerEventData(null) { button = PointerEventData.InputButton.Left };
  63. button.OnPointerDown(pointer); Assert.That(root.transform.localScale.x, Is.EqualTo(.98f));
  64. button.OnPointerUp(pointer); Assert.That(root.transform.localScale, Is.EqualTo(Vector3.one));
  65. button.OnPointerDown(pointer); root.SetActive(false);
  66. typeof(UIFeedbackButton).GetMethod("OnDisable",System.Reflection.BindingFlags.NonPublic|System.Reflection.BindingFlags.Instance|System.Reflection.BindingFlags.DeclaredOnly,
  67. null,System.Type.EmptyTypes,null).Invoke(button,null);
  68. Assert.That(root.transform.localScale, Is.EqualTo(Vector3.one)); Assert.That(root.transform.childCount, Is.Zero);
  69. }
  70. [Test] public void ParentModalInputLockBlocksPointerAndSubmitActivation()
  71. {
  72. UITransition.Show(root, true);
  73. var child = new GameObject("Button", typeof(RectTransform), typeof(UIFeedbackButton)); child.transform.SetParent(root.transform, false);
  74. var button = child.GetComponent<UIFeedbackButton>(); int clicked = 0; button.onClick.AddListener(() => clicked++);
  75. UITransition.SetInput(root, false); Assert.That(button.TryActivate(1), Is.False);
  76. UITransition.SetInput(root, true); Assert.That(button.TryActivate(2), Is.True); Assert.That(clicked, Is.EqualTo(1));
  77. }
  78. [Test] public void OldTransitionCompletionCannotHideReopenedPanel()
  79. {
  80. UITransition.Show(root,true); var effect=root.GetComponent<UITransition>();
  81. var generation=typeof(UITransition).GetField("generation",System.Reflection.BindingFlags.NonPublic|System.Reflection.BindingFlags.Instance);
  82. int old=(int)generation.GetValue(effect);
  83. UITransition.Show(root,false); UITransition.Show(root,true);
  84. typeof(UITransition).GetMethod("Finish",System.Reflection.BindingFlags.NonPublic|System.Reflection.BindingFlags.Instance,
  85. null,new[]{typeof(int)},null).Invoke(effect,new object[]{old});
  86. Assert.That(root.activeSelf,Is.True); Assert.That(effect.IsShown,Is.True);
  87. Assert.That(root.GetComponent<CanvasGroup>().alpha,Is.EqualTo(1));
  88. }
  89. }
  90. }