| | 1 | | using DCL.Components; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using DCL.Models; |
| | 4 | | using NUnit.Framework; |
| | 5 | | using System.Collections; |
| | 6 | | using UnityEngine; |
| | 7 | | using UnityEngine.TestTools; |
| | 8 | |
|
| | 9 | | namespace Tests |
| | 10 | | { |
| | 11 | | public class UIScrollRectTests : UITestsBase |
| | 12 | | { |
| | 13 | | [UnityTest] |
| | 14 | | public IEnumerator TestPropertiesAreAppliedCorrectly() |
| | 15 | | { |
| | 16 | | // Create UIScreenSpaceShape |
| 1 | 17 | | UIScreenSpace screenSpaceShape = |
| | 18 | | TestHelpers.SharedComponentCreate<UIScreenSpace, UIScreenSpace.Model>(scene, |
| | 19 | | CLASS_ID.UI_SCREEN_SPACE_SHAPE); |
| 1 | 20 | | yield return screenSpaceShape.routine; |
| | 21 | |
|
| 1 | 22 | | UIScrollRect scrRect = |
| | 23 | | TestHelpers.SharedComponentCreate<UIScrollRect, UIScrollRect.Model>(scene, CLASS_ID.UI_SLIDER_SHAPE); |
| 1 | 24 | | yield return scrRect.routine; |
| | 25 | |
|
| | 26 | | // Force a new update to pass the first apply |
| 1 | 27 | | yield return TestHelpers.SharedComponentUpdate(scrRect, new UIScrollRect.Model |
| | 28 | | { |
| | 29 | | name = "newName" |
| | 30 | | }); |
| | 31 | |
|
| 1 | 32 | | var refC = scrRect.referencesContainer; |
| | 33 | |
|
| 1 | 34 | | Assert.IsTrue(refC.canvasGroup.blocksRaycasts); |
| | 35 | | //Canvas group is disabled on first apply |
| 1 | 36 | | Assert.AreEqual(1, refC.canvasGroup.alpha); |
| | 37 | |
|
| | 38 | | // Apply padding |
| 1 | 39 | | Assert.AreEqual(0, refC.paddingLayoutGroup.padding.bottom); |
| 1 | 40 | | Assert.AreEqual(0, refC.paddingLayoutGroup.padding.top); |
| 1 | 41 | | Assert.AreEqual(0, refC.paddingLayoutGroup.padding.left); |
| 1 | 42 | | Assert.AreEqual(0, refC.paddingLayoutGroup.padding.right); |
| | 43 | |
|
| 1 | 44 | | Assert.IsFalse(refC.scrollRect.horizontal); |
| 1 | 45 | | Assert.IsTrue(refC.scrollRect.vertical); |
| | 46 | |
|
| 1 | 47 | | Assert.AreEqual(0, refC.HScrollbar.value); |
| 1 | 48 | | Assert.AreEqual(0, refC.VScrollbar.value); |
| | 49 | |
|
| | 50 | | // Update UIScrollRect properties |
| 1 | 51 | | yield return TestHelpers.SharedComponentUpdate(scrRect, |
| | 52 | | new UIScrollRect.Model |
| | 53 | | { |
| | 54 | | parentComponent = screenSpaceShape.id, |
| | 55 | | isPointerBlocker = false, |
| | 56 | | width = new UIValue(275f), |
| | 57 | | height = new UIValue(130f), |
| | 58 | | positionX = new UIValue(-30f), |
| | 59 | | positionY = new UIValue(-15f), |
| | 60 | | hAlign = "right", |
| | 61 | | vAlign = "bottom" |
| | 62 | | }); |
| | 63 | |
|
| | 64 | | // Check updated properties are applied correctly |
| 1 | 65 | | Assert.IsTrue(scrRect.referencesContainer.transform.parent == screenSpaceShape.childHookRectTransform); |
| 1 | 66 | | Assert.IsFalse(scrRect.referencesContainer.canvasGroup.blocksRaycasts); |
| 1 | 67 | | Assert.AreEqual(275f, scrRect.childHookRectTransform.rect.width); |
| 1 | 68 | | Assert.AreEqual(130f, scrRect.childHookRectTransform.rect.height); |
| 1 | 69 | | Assert.IsTrue(scrRect.referencesContainer.layoutGroup.childAlignment == TextAnchor.LowerRight); |
| | 70 | |
|
| 1 | 71 | | Vector2 alignedPosition = CalculateAlignedAnchoredPosition(screenSpaceShape.childHookRectTransform.rect, |
| | 72 | | scrRect.childHookRectTransform.rect, "bottom", "right"); |
| 1 | 73 | | alignedPosition += new Vector2(-30, -15); // apply offset position |
| | 74 | |
|
| 1 | 75 | | Assert.AreEqual(alignedPosition.ToString(), |
| | 76 | | scrRect.referencesContainer.layoutElementRT.anchoredPosition.ToString()); |
| | 77 | |
|
| 1 | 78 | | screenSpaceShape.Dispose(); |
| 1 | 79 | | } |
| | 80 | |
|
| | 81 | | [UnityTest] |
| | 82 | | public IEnumerator TestOnClickEvent() |
| | 83 | | { |
| | 84 | | // Create UIScreenSpaceShape |
| 1 | 85 | | UIScreenSpace screenSpaceShape = |
| | 86 | | TestHelpers.SharedComponentCreate<UIScreenSpace, UIScreenSpace.Model>(scene, |
| | 87 | | CLASS_ID.UI_SCREEN_SPACE_SHAPE); |
| 1 | 88 | | yield return screenSpaceShape.routine; |
| | 89 | |
|
| 1 | 90 | | UIScrollRect scrRect = |
| | 91 | | TestHelpers.SharedComponentCreate<UIScrollRect, UIScrollRect.Model>(scene, CLASS_ID.UI_SLIDER_SHAPE); |
| 1 | 92 | | yield return scrRect.routine; |
| | 93 | |
|
| | 94 | | // Force a new update to pass the first apply |
| 1 | 95 | | yield return TestHelpers.SharedComponentUpdate(scrRect, new UIScrollRect.Model |
| | 96 | | { |
| | 97 | | name = "newName" |
| | 98 | | }); |
| | 99 | |
|
| | 100 | | // Update UIScrollRect properties |
| 1 | 101 | | yield return TestHelpers.SharedComponentUpdate(scrRect, |
| | 102 | | new UIScrollRect.Model |
| | 103 | | { |
| | 104 | | parentComponent = screenSpaceShape.id, |
| | 105 | | isPointerBlocker = false, |
| | 106 | | width = new UIValue(275f), |
| | 107 | | height = new UIValue(130f), |
| | 108 | | positionX = new UIValue(-30f), |
| | 109 | | positionY = new UIValue(-15f), |
| | 110 | | hAlign = "right", |
| | 111 | | vAlign = "bottom", |
| | 112 | | onClick = "UUIDFakeEventId" |
| | 113 | | }); |
| | 114 | |
|
| | 115 | | //------------------------------------------------------------------------ |
| | 116 | | // Test click events |
| 1 | 117 | | bool eventResult = false; |
| | 118 | |
|
| 1 | 119 | | yield return TestHelpers.TestUIClickEventPropagation( |
| | 120 | | scene.sceneData.id, |
| | 121 | | scrRect.model.onClick, |
| | 122 | | scrRect.referencesContainer.childHookRectTransform, |
| | 123 | | (bool res) => |
| | 124 | | { |
| | 125 | | // Check image object clicking triggers the correct event |
| 1 | 126 | | eventResult = res; |
| 1 | 127 | | }); |
| | 128 | |
|
| 1 | 129 | | Assert.IsTrue(eventResult); |
| | 130 | |
|
| 1 | 131 | | screenSpaceShape.Dispose(); |
| 1 | 132 | | } |
| | 133 | |
|
| | 134 | | [UnityTest] |
| | 135 | | public IEnumerator TestMissingValuesGetDefaultedOnUpdate() |
| | 136 | | { |
| | 137 | | //// Create UIScreenSpaceShape |
| 1 | 138 | | UIScreenSpace screenSpaceShape = |
| | 139 | | TestHelpers.SharedComponentCreate<UIScreenSpace, UIScreenSpace.Model>(scene, |
| | 140 | | CLASS_ID.UI_SCREEN_SPACE_SHAPE); |
| | 141 | |
|
| 1 | 142 | | yield return screenSpaceShape.routine; |
| | 143 | |
|
| 1 | 144 | | Assert.IsFalse(screenSpaceShape == null); |
| | 145 | |
|
| 1 | 146 | | yield return TestHelpers.TestSharedComponentDefaultsOnUpdate<UIScrollRect.Model, UIScrollRect>(scene, |
| | 147 | | CLASS_ID.UI_SLIDER_SHAPE); |
| 1 | 148 | | } |
| | 149 | |
|
| | 150 | | [UnityTest] |
| 2 | 151 | | public IEnumerator AddedCorrectlyOnInvisibleParent() { yield return TestHelpers.TestUIElementAddedCorrectlyOnInv |
| | 152 | |
|
| | 153 | | [UnityTest] |
| | 154 | | [Explicit] |
| | 155 | | public IEnumerator TestNormalizedSize() |
| | 156 | | { |
| | 157 | | // Create UIScreenSpaceShape |
| 0 | 158 | | UIScreenSpace screenSpaceShape = |
| | 159 | | TestHelpers.SharedComponentCreate<UIScreenSpace, UIScreenSpace.Model>(scene, |
| | 160 | | CLASS_ID.UI_SCREEN_SPACE_SHAPE); |
| | 161 | |
|
| 0 | 162 | | yield return screenSpaceShape.routine; |
| | 163 | |
|
| | 164 | | // Create UIContainerRectShape |
| 0 | 165 | | UIContainerRect uiContainerRectShape = TestHelpers.SharedComponentCreate<UIContainerRect, UIContainerRect.Mo |
| | 166 | | CLASS_ID.UI_CONTAINER_RECT); |
| | 167 | |
|
| 0 | 168 | | yield return uiContainerRectShape.routine; |
| | 169 | |
|
| | 170 | | // Update UIContainerRectShape properties |
| 0 | 171 | | yield return TestHelpers.SharedComponentUpdate(uiContainerRectShape, |
| | 172 | | new UIContainerRect.Model |
| | 173 | | { |
| | 174 | | parentComponent = screenSpaceShape.id, |
| | 175 | | width = new UIValue(50, UIValue.Unit.PERCENT), |
| | 176 | | height = new UIValue(30, UIValue.Unit.PERCENT) |
| | 177 | | }); |
| | 178 | |
|
| 0 | 179 | | yield return uiContainerRectShape.routine; |
| | 180 | |
|
| 0 | 181 | | UnityEngine.UI.Image image = |
| | 182 | | uiContainerRectShape.childHookRectTransform.GetComponent<UnityEngine.UI.Image>(); |
| | 183 | |
|
| | 184 | | // Check updated properties are applied correctly |
| 0 | 185 | | Assert.AreEqual(screenSpaceShape.childHookRectTransform.rect.width * 0.5f, |
| | 186 | | uiContainerRectShape.childHookRectTransform.rect.width, 0.01f); |
| 0 | 187 | | Assert.AreEqual(screenSpaceShape.childHookRectTransform.rect.height * 0.3f, |
| | 188 | | uiContainerRectShape.childHookRectTransform.rect.height, 0.01f); |
| | 189 | |
|
| 0 | 190 | | yield return new WaitForAllMessagesProcessed(); |
| 0 | 191 | | screenSpaceShape.Dispose(); |
| 0 | 192 | | } |
| | 193 | | } |
| | 194 | | } |