using DevilLink2.UI; using NUnit.Framework; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; namespace DevilLink2.Tests { public sealed class UITransitionTests { private GameObject root; [SetUp] public void Setup() { root = new GameObject("TransitionTest", typeof(RectTransform)); } [TearDown] public void Cleanup() { Object.DestroyImmediate(root); } [Test] public void RepeatedOpenCloseReusesScreenAndCanvasGroup() { var screen = root.AddComponent(); for (int i = 0; i < 5; i++) { screen.SetVisible(true); screen.SetVisible(true); screen.SetVisible(false); } Assert.That(root.GetComponents().Length, Is.EqualTo(1)); Assert.That(root.GetComponents().Length, Is.EqualTo(1)); Assert.That(screen.IsVisible, Is.False); Assert.That(root.activeSelf, Is.False); Assert.That(root.GetComponent().blocksRaycasts, Is.False); } [Test] public void StaticVisibilityPreservesRectAndNormalizesCardScale() { var card = new GameObject("Card", typeof(RectTransform)); card.transform.SetParent(root.transform, false); card.transform.localScale = new Vector3(.9f, .9f, 1); var rect = (RectTransform)card.transform; rect.sizeDelta = new Vector2(330, 400); rect.anchoredPosition = new Vector2(12, -20); UITransition.Show(root, true); UITransition.Show(root, false); UITransition.Show(root, true); Assert.That(rect.sizeDelta, Is.EqualTo(new Vector2(330, 400))); Assert.That(rect.anchoredPosition, Is.EqualTo(new Vector2(12, -20))); Assert.That(rect.localScale, Is.EqualTo(Vector3.one)); Assert.That(root.GetComponent().IsTransitioning, Is.False); } [Test] public void ModalInputLockSurvivesRepeatedShow() { UITransition.Show(root, true); UITransition.SetInput(root, false); UITransition.Show(root, true); Assert.That(root.GetComponent().interactable, Is.False); UITransition.SetInput(root, true); Assert.That(root.GetComponent().interactable, Is.True); } [Test] public void NestedPanelLogicalVisibilityAndExternalDisableRecover() { UITransition.Show(root, true); root.SetActive(false); UITransition.Show(root, true); Assert.That(root.activeSelf, Is.True); Assert.That(UITransition.IsVisible(root), Is.True); UITransition.Show(root, false); Assert.That(UITransition.IsVisible(root), Is.False); } [Test] public void FastButtonActivationInvokesExistingListenerOnce() { var button = root.AddComponent(); int clicked = 0; button.onClick.AddListener(() => clicked++); Assert.That(button.TryActivate(1), Is.True); Assert.That(button.TryActivate(1.01), Is.False); Assert.That(clicked, Is.EqualTo(1)); Assert.That(button.TryActivate(1.2), Is.True); Assert.That(clicked, Is.EqualTo(2)); } [Test] public void DisabledButtonNeitherActivatesNorShrinks() { var button = root.AddComponent(); int clicked = 0; button.onClick.AddListener(() => clicked++); button.interactable = false; button.OnPointerDown(new PointerEventData(null) { button = PointerEventData.InputButton.Left }); Assert.That(button.TryActivate(1), Is.False); Assert.That(clicked, Is.Zero); Assert.That(root.transform.localScale, Is.EqualTo(Vector3.one)); } [Test] public void PressReleaseAndDisableRestoreScaleWithoutAddingObjects() { var button = root.AddComponent(); var pointer = new PointerEventData(null) { button = PointerEventData.InputButton.Left }; button.OnPointerDown(pointer); Assert.That(root.transform.localScale.x, Is.EqualTo(.98f)); button.OnPointerUp(pointer); Assert.That(root.transform.localScale, Is.EqualTo(Vector3.one)); button.OnPointerDown(pointer); root.SetActive(false); typeof(UIFeedbackButton).GetMethod("OnDisable",System.Reflection.BindingFlags.NonPublic|System.Reflection.BindingFlags.Instance|System.Reflection.BindingFlags.DeclaredOnly, null,System.Type.EmptyTypes,null).Invoke(button,null); Assert.That(root.transform.localScale, Is.EqualTo(Vector3.one)); Assert.That(root.transform.childCount, Is.Zero); } [Test] public void ParentModalInputLockBlocksPointerAndSubmitActivation() { UITransition.Show(root, true); var child = new GameObject("Button", typeof(RectTransform), typeof(UIFeedbackButton)); child.transform.SetParent(root.transform, false); var button = child.GetComponent(); int clicked = 0; button.onClick.AddListener(() => clicked++); UITransition.SetInput(root, false); Assert.That(button.TryActivate(1), Is.False); UITransition.SetInput(root, true); Assert.That(button.TryActivate(2), Is.True); Assert.That(clicked, Is.EqualTo(1)); } [Test] public void OldTransitionCompletionCannotHideReopenedPanel() { UITransition.Show(root,true); var effect=root.GetComponent(); var generation=typeof(UITransition).GetField("generation",System.Reflection.BindingFlags.NonPublic|System.Reflection.BindingFlags.Instance); int old=(int)generation.GetValue(effect); UITransition.Show(root,false); UITransition.Show(root,true); typeof(UITransition).GetMethod("Finish",System.Reflection.BindingFlags.NonPublic|System.Reflection.BindingFlags.Instance, null,new[]{typeof(int)},null).Invoke(effect,new object[]{old}); Assert.That(root.activeSelf,Is.True); Assert.That(effect.IsShown,Is.True); Assert.That(root.GetComponent().alpha,Is.EqualTo(1)); } } }