| | 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 | | using UnityEngine.UI; |
| | 9 | |
|
| | 10 | | namespace Tests |
| | 11 | | { |
| | 12 | | public class UIContainerRectTests : UITestsBase |
| | 13 | | { |
| | 14 | | [UnityTest] |
| | 15 | | public IEnumerator TestPropertiesAreAppliedCorrectly() |
| | 16 | | { |
| | 17 | | // Create UIScreenSpaceShape |
| 1 | 18 | | UIScreenSpace screenSpaceShape = |
| | 19 | | TestHelpers.SharedComponentCreate<UIScreenSpace, UIScreenSpace.Model>(scene, |
| | 20 | | CLASS_ID.UI_SCREEN_SPACE_SHAPE); |
| | 21 | |
|
| 1 | 22 | | yield return screenSpaceShape.routine; |
| | 23 | |
|
| | 24 | | // Create UIContainerRectShape |
| 1 | 25 | | UIContainerRect uiContainerRectShape = |
| | 26 | | TestHelpers.SharedComponentCreate<UIContainerRect, UIContainerRect.Model>(scene, |
| | 27 | | CLASS_ID.UI_CONTAINER_RECT); |
| | 28 | |
|
| 1 | 29 | | yield return uiContainerRectShape.routine; |
| | 30 | |
|
| 1 | 31 | | UnityEngine.UI.Image image = uiContainerRectShape.referencesContainer.image; |
| | 32 | |
|
| | 33 | | // Check default properties are applied correctly |
| 1 | 34 | | Assert.AreEqual(0f, uiContainerRectShape.referencesContainer.canvasGroup.alpha); |
| 1 | 35 | | Assert.IsTrue(image.GetComponent<Outline>() == null); |
| 1 | 36 | | Assert.IsTrue(image.color == new Color(0f, 0f, 0f, 0f)); |
| 1 | 37 | | Assert.IsTrue(uiContainerRectShape.referencesContainer.canvasGroup.blocksRaycasts); |
| 1 | 38 | | Assert.AreEqual(100f, uiContainerRectShape.childHookRectTransform.rect.width); |
| 1 | 39 | | Assert.AreEqual(50f, uiContainerRectShape.childHookRectTransform.rect.height); |
| 1 | 40 | | Assert.AreEqual(Vector3.zero, uiContainerRectShape.childHookRectTransform.localPosition); |
| | 41 | |
|
| | 42 | | // Update UIContainerRectShape properties |
| 1 | 43 | | uiContainerRectShape = scene.SharedComponentUpdate(uiContainerRectShape.id, JsonUtility.ToJson(new UIContain |
| | 44 | | { |
| | 45 | | parentComponent = screenSpaceShape.id, |
| | 46 | | thickness = 5, |
| | 47 | | color = new Color(0.2f, 0.7f, 0.05f, 1f), |
| | 48 | | isPointerBlocker = false, |
| | 49 | | width = new UIValue(275f), |
| | 50 | | height = new UIValue(130f), |
| | 51 | | positionX = new UIValue(-30f), |
| | 52 | | positionY = new UIValue(-15f), |
| | 53 | | hAlign = "right", |
| | 54 | | vAlign = "bottom" |
| | 55 | | })) as UIContainerRect; |
| | 56 | |
|
| 1 | 57 | | yield return uiContainerRectShape.routine; |
| | 58 | |
|
| | 59 | | // Check updated properties are applied correctly |
| 1 | 60 | | Assert.AreEqual(1f, uiContainerRectShape.referencesContainer.canvasGroup.alpha); |
| 1 | 61 | | Assert.IsTrue(uiContainerRectShape.referencesContainer.transform.parent == screenSpaceShape.childHookRectTra |
| 1 | 62 | | Assert.IsTrue(image.GetComponent<Outline>() != null); |
| 1 | 63 | | Assert.IsTrue(image.color == new Color(0.2f, 0.7f, 0.05f, 1f)); |
| 1 | 64 | | Assert.IsFalse(uiContainerRectShape.referencesContainer.canvasGroup.blocksRaycasts); |
| 1 | 65 | | Assert.AreEqual(275f, uiContainerRectShape.childHookRectTransform.rect.width); |
| 1 | 66 | | Assert.AreEqual(130f, uiContainerRectShape.childHookRectTransform.rect.height); |
| 1 | 67 | | Assert.IsTrue(uiContainerRectShape.referencesContainer.layoutGroup.childAlignment == TextAnchor.LowerRight); |
| | 68 | |
|
| 1 | 69 | | Vector2 alignedPosition = CalculateAlignedAnchoredPosition(screenSpaceShape.childHookRectTransform.rect, |
| | 70 | | uiContainerRectShape.childHookRectTransform.rect, "bottom", "right"); |
| 1 | 71 | | alignedPosition += new Vector2(-30, -15); // apply offset position |
| | 72 | |
|
| 1 | 73 | | Assert.AreEqual(alignedPosition.ToString(), |
| | 74 | | uiContainerRectShape.childHookRectTransform.anchoredPosition.ToString()); |
| | 75 | |
|
| 1 | 76 | | screenSpaceShape.Dispose(); |
| 1 | 77 | | } |
| | 78 | |
|
| | 79 | | [UnityTest] |
| | 80 | | public IEnumerator TestParenting() |
| | 81 | | { |
| | 82 | | // Create UIScreenSpaceShape |
| 1 | 83 | | UIScreenSpace screenSpaceShape = |
| | 84 | | TestHelpers.SharedComponentCreate<UIScreenSpace, UIScreenSpace.Model>(scene, |
| | 85 | | CLASS_ID.UI_SCREEN_SPACE_SHAPE); |
| 1 | 86 | | yield return screenSpaceShape.routine; |
| | 87 | |
|
| | 88 | | // Create UIContainerRectShape |
| 1 | 89 | | UIContainerRect uiContainerRectShape = |
| | 90 | | TestHelpers.SharedComponentCreate<UIContainerRect, UIContainerRect.Model>(scene, |
| | 91 | | CLASS_ID.UI_CONTAINER_RECT); |
| 1 | 92 | | yield return uiContainerRectShape.routine; |
| | 93 | |
|
| | 94 | | // Update UIContainerRectShape parent |
| 1 | 95 | | scene.SharedComponentUpdate(uiContainerRectShape.id, JsonUtility.ToJson(new UIContainerRect.Model |
| | 96 | | { |
| | 97 | | parentComponent = screenSpaceShape.id, |
| | 98 | | })); |
| 1 | 99 | | yield return uiContainerRectShape.routine; |
| | 100 | |
|
| | 101 | | // Check updated parent |
| 1 | 102 | | Assert.IsTrue(uiContainerRectShape.referencesContainer.transform.parent == |
| | 103 | | screenSpaceShape.childHookRectTransform); |
| | 104 | |
|
| | 105 | | // Create 2nd UIContainerRectShape |
| 1 | 106 | | UIContainerRect uiContainerRectShape2 = |
| | 107 | | TestHelpers.SharedComponentCreate<UIContainerRect, UIContainerRect.Model>(scene, |
| | 108 | | CLASS_ID.UI_CONTAINER_RECT); |
| 1 | 109 | | yield return uiContainerRectShape2.routine; |
| | 110 | |
|
| | 111 | | // Update UIContainerRectShape parent to the previous container |
| 1 | 112 | | scene.SharedComponentUpdate(uiContainerRectShape2.id, JsonUtility.ToJson(new UIContainerRect.Model |
| | 113 | | { |
| | 114 | | parentComponent = uiContainerRectShape.id, |
| | 115 | | })); |
| 1 | 116 | | yield return uiContainerRectShape2.routine; |
| | 117 | |
|
| | 118 | | // Check updated parent |
| 1 | 119 | | Assert.IsTrue(uiContainerRectShape2.referencesContainer.transform.parent == |
| | 120 | | uiContainerRectShape.childHookRectTransform); |
| | 121 | |
|
| 1 | 122 | | screenSpaceShape.Dispose(); |
| 1 | 123 | | } |
| | 124 | |
|
| | 125 | | [UnityTest] |
| | 126 | | public IEnumerator TestMissingValuesGetDefaultedOnUpdate() |
| | 127 | | { |
| | 128 | | //// Create UIScreenSpaceShape |
| 1 | 129 | | UIScreenSpace screenSpaceShape = |
| | 130 | | TestHelpers.SharedComponentCreate<UIScreenSpace, UIScreenSpace.Model>(scene, |
| | 131 | | CLASS_ID.UI_SCREEN_SPACE_SHAPE); |
| 1 | 132 | | yield return screenSpaceShape.routine; |
| | 133 | |
|
| 1 | 134 | | Assert.IsFalse(screenSpaceShape == null); |
| | 135 | |
|
| 1 | 136 | | yield return TestHelpers.TestSharedComponentDefaultsOnUpdate<UIContainerRect.Model, UIContainerRect>(scene, |
| | 137 | | CLASS_ID.UI_CONTAINER_RECT); |
| 1 | 138 | | } |
| | 139 | |
|
| | 140 | | [UnityTest] |
| 2 | 141 | | public IEnumerator AddedCorrectlyOnInvisibleParent() { yield return TestHelpers.TestUIElementAddedCorrectlyOnInv |
| | 142 | |
|
| | 143 | | [UnityTest] |
| | 144 | | public IEnumerator TestNormalizedSize() |
| | 145 | | { |
| | 146 | | // Create UIScreenSpaceShape |
| 1 | 147 | | UIScreenSpace screenSpaceShape = |
| | 148 | | TestHelpers.SharedComponentCreate<UIScreenSpace, UIScreenSpace.Model>(scene, |
| | 149 | | CLASS_ID.UI_SCREEN_SPACE_SHAPE); |
| 1 | 150 | | yield return screenSpaceShape.routine; |
| | 151 | |
|
| | 152 | | // Create UIContainerRectShape |
| 1 | 153 | | UIContainerRect uiContainerRectShape = |
| | 154 | | TestHelpers.SharedComponentCreate<UIContainerRect, UIContainerRect.Model>(scene, |
| | 155 | | CLASS_ID.UI_CONTAINER_RECT); |
| 1 | 156 | | yield return uiContainerRectShape.routine; |
| | 157 | |
|
| | 158 | | // Update UIContainerRectShape properties |
| 1 | 159 | | uiContainerRectShape = scene.SharedComponentUpdate(uiContainerRectShape.id, JsonUtility.ToJson(new UIContain |
| | 160 | | { |
| | 161 | | parentComponent = screenSpaceShape.id, |
| | 162 | | width = new UIValue(50, UIValue.Unit.PERCENT), |
| | 163 | | height = new UIValue(30, UIValue.Unit.PERCENT) |
| | 164 | | })) as UIContainerRect; |
| 1 | 165 | | yield return uiContainerRectShape.routine; |
| | 166 | |
|
| 1 | 167 | | UnityEngine.UI.Image image = |
| | 168 | | uiContainerRectShape.childHookRectTransform.GetComponent<UnityEngine.UI.Image>(); |
| | 169 | |
|
| | 170 | | // Check updated properties are applied correctly |
| 1 | 171 | | Assert.AreEqual(screenSpaceShape.childHookRectTransform.rect.width * 0.5f, |
| | 172 | | uiContainerRectShape.childHookRectTransform.rect.width, 0.01f); |
| 1 | 173 | | Assert.AreEqual(screenSpaceShape.childHookRectTransform.rect.height * 0.3f, |
| | 174 | | uiContainerRectShape.childHookRectTransform.rect.height, 0.01f); |
| | 175 | |
|
| 1 | 176 | | screenSpaceShape.Dispose(); |
| 1 | 177 | | } |
| | 178 | |
|
| | 179 | | [UnityTest] |
| | 180 | | public IEnumerator TestOnClickEvent() |
| | 181 | | { |
| | 182 | | // Create UIScreenSpaceShape |
| 1 | 183 | | UIScreenSpace screenSpaceShape = |
| | 184 | | TestHelpers.SharedComponentCreate<UIScreenSpace, UIScreenSpace.Model>(scene, |
| | 185 | | CLASS_ID.UI_SCREEN_SPACE_SHAPE); |
| | 186 | |
|
| 1 | 187 | | yield return screenSpaceShape.routine; |
| | 188 | |
|
| | 189 | | // Create UIContainerRectShape |
| 1 | 190 | | UIContainerRect uiContainerRectShape = |
| | 191 | | TestHelpers.SharedComponentCreate<UIContainerRect, UIContainerRect.Model>(scene, |
| | 192 | | CLASS_ID.UI_CONTAINER_RECT); |
| | 193 | |
|
| 1 | 194 | | yield return uiContainerRectShape.routine; |
| | 195 | |
|
| | 196 | | // Update UIContainerRectShape properties |
| 1 | 197 | | uiContainerRectShape = scene.SharedComponentUpdate(uiContainerRectShape.id, JsonUtility.ToJson(new UIContain |
| | 198 | | { |
| | 199 | | parentComponent = screenSpaceShape.id, |
| | 200 | | thickness = 5, |
| | 201 | | color = new Color(0.2f, 0.7f, 0.05f, 1f), |
| | 202 | | isPointerBlocker = false, |
| | 203 | | width = new UIValue(275f), |
| | 204 | | height = new UIValue(130f), |
| | 205 | | positionX = new UIValue(-30f), |
| | 206 | | positionY = new UIValue(-15f), |
| | 207 | | hAlign = "right", |
| | 208 | | vAlign = "bottom", |
| | 209 | | onClick = "UUIDFakeEventId" |
| | 210 | | })) as UIContainerRect; |
| | 211 | |
|
| 1 | 212 | | yield return uiContainerRectShape.routine; |
| | 213 | |
|
| | 214 | | //------------------------------------------------------------------------ |
| | 215 | | // Test click events |
| 1 | 216 | | bool eventResult = false; |
| | 217 | |
|
| 1 | 218 | | yield return TestHelpers.TestUIClickEventPropagation( |
| | 219 | | scene.sceneData.id, |
| | 220 | | uiContainerRectShape.model.onClick, |
| | 221 | | uiContainerRectShape.referencesContainer.childHookRectTransform, |
| | 222 | | (bool res) => |
| | 223 | | { |
| | 224 | | // Check image object clicking triggers the correct event |
| 1 | 225 | | eventResult = res; |
| 1 | 226 | | }); |
| | 227 | |
|
| 1 | 228 | | Assert.IsTrue(eventResult); |
| 1 | 229 | | } |
| | 230 | | } |
| | 231 | | } |