SelectableTests.cs 16.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
using System.Reflection;
using System.Collections;
using NUnit.Framework;
using UnityEngine.EventSystems;
using UnityEngine.TestTools;

namespace UnityEngine.UI.Tests
{
    [TestFixture]
    class SelectableTests
    {
        private class SelectableTest : Selectable
        {
            public bool isStateNormal { get { return currentSelectionState == SelectionState.Normal; } }
            public bool isStateHighlighted { get { return currentSelectionState == SelectionState.Highlighted; } }
            public bool isStateSelected { get { return currentSelectionState == SelectionState.Selected; } }
            public bool isStatePressed { get { return currentSelectionState == SelectionState.Pressed; } }
            public bool isStateDisabled { get { return currentSelectionState == SelectionState.Disabled; } }

            public Selectable GetSelectableAtIndex(int index)
            {
                return s_Selectables[index];
            }

            public int GetSelectableCurrentIndex()
            {
                return m_CurrentIndex;
            }
        }

        private SelectableTest selectable;

        private CanvasGroup CreateAndParentGroupTo(string name, GameObject child)
        {
            GameObject canvasRoot = new GameObject("Canvas", typeof(RectTransform), typeof(Canvas));
            GameObject groupGO = new GameObject(name, typeof(RectTransform), typeof(CanvasGroup));
            groupGO.transform.SetParent(canvasRoot.transform);
            child.transform.SetParent(groupGO.transform);
            return groupGO.GetComponent<CanvasGroup>();
        }

        [SetUp]
        public void TestSetup()
        {
            EventSystem.current = new GameObject("EventSystem", typeof(EventSystem)).GetComponent<EventSystem>();
            GameObject canvasRoot = new GameObject("Canvas", typeof(RectTransform), typeof(Canvas));
            GameObject SelectableGO = new GameObject("Selectable", typeof(RectTransform), typeof(CanvasRenderer));

            SelectableGO.transform.SetParent(canvasRoot.transform);
            selectable = SelectableGO.AddComponent<SelectableTest>();
            selectable.targetGraphic = selectable.gameObject.AddComponent<ConcreteGraphic>();
        }

        [TearDown]
        public void TearDown()
        {
            EventSystem.current = null;
        }

        [Test] // regression test 1160054
        public void SelectableArrayRemovesReferenceUponDisable()
        {
            int originalSelectableCount = Selectable.allSelectableCount;

            selectable.enabled = false;

            Assert.AreEqual(originalSelectableCount - 1, Selectable.allSelectableCount, "We have more then originalSelectableCount - 1 selectable objects.");
            //ensure the item as the last index is nulled out as it replaced the item that was disabled.
            Assert.IsNull(selectable.GetSelectableAtIndex(Selectable.allSelectableCount));

            selectable.enabled = true;
        }

        #region Selected object

        [Test]
        public void SettingCurrentSelectedSelectableNonInteractableShouldNullifyCurrentSelected()
        {
            EventSystem.current.SetSelectedGameObject(selectable.gameObject);
            selectable.interactable = false;

            // it should be unselected now that it is not interactable anymore
            Assert.IsNull(EventSystem.current.currentSelectedGameObject);
        }

        [Test]
        public void PointerEnterDownShouldMakeItSelectedGameObject()
        {
            Assert.IsNull(EventSystem.current.currentSelectedGameObject);
            selectable.InvokeOnPointerEnter(new PointerEventData(EventSystem.current));
            selectable.InvokeOnPointerDown(new PointerEventData(EventSystem.current));
            Assert.AreEqual(selectable.gameObject, EventSystem.current.currentSelectedGameObject);
        }

        [Test]
        public void OnSelectShouldSetSelectedState()
        {
            Assert.True(selectable.isStateNormal);
            selectable.OnSelect(new BaseEventData(EventSystem.current));
            Assert.True(selectable.isStateSelected);
        }

        [Test]
        public void OnDeselectShouldUnsetSelectedState()
        {
            Assert.True(selectable.isStateNormal);
            selectable.OnSelect(new BaseEventData(EventSystem.current));
            Assert.True(selectable.isStateSelected);
            selectable.OnDeselect(new BaseEventData(EventSystem.current));
            Assert.True(selectable.isStateNormal);
        }

        #endregion

        #region Interactable

        [Test]
        public void SettingCanvasGroupNotInteractableShouldMakeSelectableNotInteractable()
        {
            // Canvas Group on same object
            var group = selectable.gameObject.AddComponent<CanvasGroup>();
            Assert.IsTrue(selectable.IsInteractable());

            group.interactable = false;
            // actual call happens on the native side, cause by interactable = false
            selectable.InvokeOnCanvasGroupChanged();

            Assert.IsFalse(selectable.IsInteractable());
        }

        [Test]
        public void SettingParentCanvasGroupNotInteractableShouldMakeSelectableNotInteractable()
        {
            var canvasGroup = CreateAndParentGroupTo("CanvasGroup", selectable.gameObject);
            Assert.IsTrue(selectable.IsInteractable());

            canvasGroup.interactable = false;
            // actual call happens on the native side, cause by interactable = false
            selectable.InvokeOnCanvasGroupChanged();

            Assert.IsFalse(selectable.IsInteractable());
        }

        [Test]
        public void SettingParentParentCanvasGroupNotInteractableShouldMakeSelectableNotInteractable()
        {
            var canvasGroup1 = CreateAndParentGroupTo("CanvasGroup1", selectable.gameObject);
            var canvasGroup2 = CreateAndParentGroupTo("CanvasGroup2", canvasGroup1.gameObject);
            Assert.IsTrue(selectable.IsInteractable());

            canvasGroup2.interactable = false;
            // actual call happens on the native side, cause by interactable = false
            selectable.InvokeOnCanvasGroupChanged();

            Assert.IsFalse(selectable.IsInteractable());
        }

        [Test]
        public void SettingParentParentCanvasGroupInteractableShouldMakeSelectableInteractable()
        {
            var canvasGroup1 = CreateAndParentGroupTo("CanvasGroup1", selectable.gameObject);
            CreateAndParentGroupTo("CanvasGroup2", canvasGroup1.gameObject);
            Assert.IsTrue(selectable.IsInteractable());

            // actual call happens on the native side, cause by interactable
            selectable.InvokeOnCanvasGroupChanged();

            Assert.IsTrue(selectable.IsInteractable());
        }

        [Test]
        public void SettingParentParentCanvasGroupNotInteractableShouldNotMakeSelectableNotInteractableIfIgnoreParentGroups()
        {
            var canvasGroup1 = CreateAndParentGroupTo("CanvasGroup1", selectable.gameObject);
            canvasGroup1.ignoreParentGroups = true;
            var canvasGroup2 = CreateAndParentGroupTo("CanvasGroup2", canvasGroup1.gameObject);
            Assert.IsTrue(selectable.IsInteractable());

            canvasGroup2.interactable = false;
            // actual call happens on the native side, cause by interactable = false
            selectable.InvokeOnCanvasGroupChanged();

            Assert.IsTrue(selectable.IsInteractable());
        }

        [Test]// regression test 861736
        public void PointerEnterThenSetNotInteractableThenExitThenSetInteractableShouldSetStateToDefault()
        {
            Assert.True(selectable.isStateNormal);
            selectable.InvokeOnPointerEnter(null);
            Assert.True(selectable.isStateHighlighted);
            selectable.interactable = false;
            selectable.InvokeOnPointerExit(null);
            selectable.interactable = true;
            Assert.False(selectable.isStateHighlighted);
            Assert.True(selectable.isStateNormal);
        }

        [Test]// regression test 861736
        public void PointerEnterThenSetNotInteractableThenSetInteractableShouldStayHighlighted()
        {
            Assert.True(selectable.isStateNormal);
            selectable.InvokeOnPointerEnter(null);
            Assert.True(selectable.isStateHighlighted);
            selectable.interactable = false;
            selectable.interactable = true;
            Assert.True(selectable.isStateHighlighted);
        }

        #endregion

        #region Tweening

        [UnityTest]
        public IEnumerator SettingNotInteractableShouldTweenToDisabledColor()
        {
            var canvasRenderer = selectable.gameObject.GetComponent<CanvasRenderer>();
            selectable.InvokeOnEnable();
            canvasRenderer.SetColor(selectable.colors.normalColor);

            selectable.interactable = false;

            yield return new WaitForSeconds(1);

            Assert.AreEqual(selectable.colors.disabledColor, canvasRenderer.GetColor());

            selectable.interactable = true;

            yield return new WaitForSeconds(1);

            Assert.AreEqual(selectable.colors.normalColor, canvasRenderer.GetColor());
        }

        [UnityTest][Ignore("Fails")] // regression test 742140
        public IEnumerator SettingNotInteractableThenInteractableShouldNotTweenToDisabledColor()
        {
            var canvasRenderer = selectable.gameObject.GetComponent<CanvasRenderer>();
            selectable.enabled = false;
            selectable.enabled = true;
            canvasRenderer.SetColor(selectable.colors.normalColor);

            selectable.interactable = false;
            selectable.interactable = true;
            Color c = canvasRenderer.GetColor();

            for (int i = 0; i < 30; i++)
            {
                yield return null;
                Color c2 = canvasRenderer.GetColor();
                Assert.AreNotEqual(c2, c);
            }
            Assert.AreEqual(selectable.colors.normalColor, canvasRenderer.GetColor());
        }

        [UnityTest]
        public IEnumerator SettingInteractableToFalseTrueFalseShouldTweenToDisabledColor()
        {
            var canvasRenderer = selectable.gameObject.GetComponent<CanvasRenderer>();
            selectable.InvokeOnEnable();
            canvasRenderer.SetColor(selectable.colors.normalColor);

            selectable.interactable = false;
            selectable.interactable = true;
            selectable.interactable = false;

            yield return new WaitForSeconds(1);

            Assert.AreEqual(selectable.colors.disabledColor, canvasRenderer.GetColor());
        }

#if PACKAGE_ANIMATION
        [Test]
        public void TriggerAnimationWithNoAnimator()
        {
            Assert.Null(selectable.animator);
            Assert.DoesNotThrow(() => selectable.InvokeTriggerAnimation("asdasd"));
        }

        [Test]
        public void TriggerAnimationWithDisabledAnimator()
        {
            var an = selectable.gameObject.AddComponent<Animator>();
            an.enabled = false;
            Assert.NotNull(selectable.animator);
            Assert.DoesNotThrow(() => selectable.InvokeTriggerAnimation("asdasd"));
        }

        [Test]
        public void TriggerAnimationAnimatorWithNoRuntimeController()
        {
            var an = selectable.gameObject.AddComponent<Animator>();
            an.runtimeAnimatorController = null;
            Assert.NotNull(selectable.animator);
            Assert.DoesNotThrow(() => selectable.InvokeTriggerAnimation("asdasd"));
        }

#endif
        #endregion

        #region Selection state and pointer

        [Test]
        public void SelectShouldSetSelectedObject()
        {
            Assert.Null(EventSystem.current.currentSelectedGameObject);
            selectable.Select();
            Assert.AreEqual(selectable.gameObject, EventSystem.current.currentSelectedGameObject);
        }

        [Test]
        public void SelectWhenAlreadySelectingShouldNotSetSelectedObject()
        {
            Assert.Null(EventSystem.current.currentSelectedGameObject);
            var fieldInfo = typeof(EventSystem).GetField("m_SelectionGuard", BindingFlags.NonPublic | BindingFlags.Instance);
            fieldInfo
                .SetValue(EventSystem.current, true);
            selectable.Select();
            Assert.Null(EventSystem.current.currentSelectedGameObject);
        }

        [Test]
        public void PointerEnterShouldHighlight()
        {
            Assert.True(selectable.isStateNormal);
            selectable.InvokeOnPointerEnter(null);
            Assert.True(selectable.isStateHighlighted);
        }

        [Test]
        public void PointerEnterAndRightClickShouldHighlightNotPress()
        {
            Assert.True(selectable.isStateNormal);
            selectable.InvokeOnPointerEnter(null);
            selectable.InvokeOnPointerDown(new PointerEventData(EventSystem.current)
            {
                button = PointerEventData.InputButton.Right
            });
            Assert.True(selectable.isStateHighlighted);
        }

        [Test]
        public void PointerEnterAndRightClickShouldPress()
        {
            Assert.True(selectable.isStateNormal);
            selectable.InvokeOnPointerEnter(null);
            selectable.InvokeOnPointerDown(new PointerEventData(EventSystem.current));
            Assert.True(selectable.isStatePressed);
        }

        [Test]
        public void PointerEnterLeftClickExitShouldPress()
        {
            Assert.True(selectable.isStateNormal);
            selectable.InvokeOnPointerEnter(null);
            selectable.InvokeOnPointerDown(new PointerEventData(EventSystem.current));
            selectable.InvokeOnPointerExit(null);
            Assert.True(selectable.isStatePressed);
        }

        [Test]
        public void PointerEnterLeftClickExitReleaseShouldSelect()
        {
            Assert.True(selectable.isStateNormal);
            selectable.InvokeOnPointerEnter(null);
            selectable.InvokeOnPointerDown(new PointerEventData(EventSystem.current));
            selectable.InvokeOnPointerExit(null);
            selectable.InvokeOnPointerUp(new PointerEventData(EventSystem.current));
            Assert.True(selectable.isStateSelected);
        }

        [Test]
        public void PointerDownShouldSetSelectedObject()
        {
            Assert.Null(EventSystem.current.currentSelectedGameObject);
            selectable.InvokeOnPointerDown(new PointerEventData(EventSystem.current));
            Assert.AreEqual(selectable.gameObject, EventSystem.current.currentSelectedGameObject);
        }

        [Test]
        public void PointerLeftDownRightDownRightUpShouldNotChangeState()
        {
            Assert.True(selectable.isStateNormal);
            selectable.InvokeOnPointerEnter(null);
            selectable.InvokeOnPointerDown(new PointerEventData(EventSystem.current) { button = PointerEventData.InputButton.Left });
            selectable.InvokeOnPointerDown(new PointerEventData(EventSystem.current) { button = PointerEventData.InputButton.Right });
            selectable.InvokeOnPointerUp(new PointerEventData(EventSystem.current) { button = PointerEventData.InputButton.Right });
            Assert.True(selectable.isStatePressed);
        }

        [Test, Ignore("No disabled state assigned ? Investigate")]
        public void SettingNotInteractableShouldDisable()
        {
            Assert.True(selectable.isStateNormal);
            selectable.interactable = false;
            selectable.InvokeOnCanvasGroupChanged();
            Assert.True(selectable.isStateDisabled);
        }

        #endregion

        #region No event system

        [Test] // regression test 787563
        public void SettingInteractableWithNoEventSystemShouldNotCrash()
        {
            EventSystem.current = null;
            selectable.interactable = false;
        }

        [Test] // regression test 787563
        public void OnPointerDownWithNoEventSystemShouldNotCrash()
        {
            EventSystem.current = null;
            selectable.OnPointerDown(new PointerEventData(EventSystem.current) {button = PointerEventData.InputButton.Left});
        }

        [Test] // regression test 787563
        public void SelectWithNoEventSystemShouldNotCrash()
        {
            EventSystem.current = null;
            selectable.Select();
        }

        #endregion
    }
}