| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- 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<UIScreen>();
- for (int i = 0; i < 5; i++) { screen.SetVisible(true); screen.SetVisible(true); screen.SetVisible(false); }
- Assert.That(root.GetComponents<UITransition>().Length, Is.EqualTo(1));
- Assert.That(root.GetComponents<CanvasGroup>().Length, Is.EqualTo(1));
- Assert.That(screen.IsVisible, Is.False); Assert.That(root.activeSelf, Is.False);
- Assert.That(root.GetComponent<CanvasGroup>().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<UITransition>().IsTransitioning, Is.False);
- }
- [Test] public void ModalInputLockSurvivesRepeatedShow()
- {
- UITransition.Show(root, true); UITransition.SetInput(root, false); UITransition.Show(root, true);
- Assert.That(root.GetComponent<CanvasGroup>().interactable, Is.False);
- UITransition.SetInput(root, true); Assert.That(root.GetComponent<CanvasGroup>().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<UIFeedbackButton>(); 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<UIFeedbackButton>(); 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<UIFeedbackButton>();
- 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<UIFeedbackButton>(); 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<UITransition>();
- 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<CanvasGroup>().alpha,Is.EqualTo(1));
- }
- }
- }
|