using System.Reflection; using DevilLink2.UI; using NUnit.Framework; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; namespace DevilLink2.Tests { public sealed class ModalButtonStateTests { private GameObject root; private UIFeedbackButton button; private Sprite normal, disabled; private Texture2D texture; private static bool PointerDown(Selectable value) => (bool)typeof(Selectable) .GetProperty("isPointerDown", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(value); [SetUp] public void Setup() { root = new GameObject("ModalTest", typeof(RectTransform)); UITransition.Show(root, true); var child = new GameObject("Button", typeof(RectTransform), typeof(Image)); child.transform.SetParent(root.transform, false); button = child.AddComponent(); button.targetGraphic = child.GetComponent(); button.PreserveAppearanceDuringModalLock = true; texture = new Texture2D(2, 2); normal = Sprite.Create(texture, new Rect(0, 0, 2, 2), Vector2.one * .5f); disabled = Sprite.Create(texture, new Rect(0, 0, 1, 1), Vector2.one * .5f); button.image.sprite = normal; button.transition = Selectable.Transition.SpriteSwap; button.spriteState = new SpriteState { disabledSprite = disabled, highlightedSprite = normal, selectedSprite = normal }; } [TearDown] public void Cleanup() { Object.DestroyImmediate(root); Object.DestroyImmediate(normal); Object.DestroyImmediate(disabled); Object.DestroyImmediate(texture); } [Test] public void RejectedPointerDownDoesNotLatchSelectablePress() { UITransition.SetInput(root, false); button.OnPointerDown(new PointerEventData(null) { button = PointerEventData.InputButton.Left }); Assert.That(PointerDown(button), Is.False); } [Test] public void ModalLockPreservesNormalSpriteButStillBlocksActivation() { UITransition.SetInput(root, false); button.SendMessage("OnCanvasGroupChanged", SendMessageOptions.RequireReceiver); Assert.That(button.IsInteractable(), Is.False); Assert.That(button.image.overrideSprite, Is.SameAs(normal)); Assert.That(button.TryActivate(1), Is.False); } [Test] public void RealDisabledStateRetainsDisabledSprite() { button.interactable = false; Assert.That(button.image.overrideSprite, Is.SameAs(disabled)); Assert.That(button.TryActivate(1), Is.False); } [Test] public void ModalLockCancelsAcceptedPressWithoutChangingNavigation() { var navigation = button.navigation; button.OnPointerDown(new PointerEventData(null) { button = PointerEventData.InputButton.Left }); Assert.That(PointerDown(button), Is.True); UITransition.SetInput(root, false); button.SendMessage("OnCanvasGroupChanged", SendMessageOptions.RequireReceiver); Assert.That(PointerDown(button), Is.False); Assert.That(button.transform.localScale, Is.EqualTo(Vector3.one)); Assert.That(button.navigation.mode, Is.EqualTo(navigation.mode)); } } }