< Summary

Class:Tests.UIContainerStackTests
Assembly:UIContainerRectTests
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/UI/UIContainer/Tests/UIContainerStackTests.cs
Covered lines:128
Uncovered lines:0
Coverable lines:128
Total lines:413
Line coverage:100% (128 of 128)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
PropertiesAreAppliedCorrectly()0%550100%
MissingValuesGetDefaultedOnUpdate()0%440100%
AddedCorrectlyOnInvisibleParent()0%330100%
NormalizedSize()0%550100%
TestChildrenAreHandledCorrectly()0%770100%
VerticalStackIsAppliedCorrectly()0%880100%
HorizontalStackIsAppliedCorrectly()0%990100%
AdaptSizeIsAppliedCorrectly()0%10100100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/UI/UIContainer/Tests/UIContainerStackTests.cs

#LineLine coverage
 1using DCL.Components;
 2using DCL.Helpers;
 3using DCL.Models;
 4using NUnit.Framework;
 5using System.Collections;
 6using UnityEngine;
 7using UnityEngine.TestTools;
 8using UnityEngine.UI;
 9
 10namespace Tests
 11{
 12    public class UIContainerStackTests : UITestsBase
 13    {
 14        [UnityTest]
 15        public IEnumerator PropertiesAreAppliedCorrectly()
 16        {
 17            // Create UIScreenSpaceShape
 118            UIScreenSpace screenSpaceShape =
 19                TestHelpers.SharedComponentCreate<UIScreenSpace, UIScreenSpace.Model>(scene,
 20                    CLASS_ID.UI_SCREEN_SPACE_SHAPE);
 121            yield return screenSpaceShape.routine;
 22
 23            // Create UIContainerStack
 124            UIContainerStack uiContainerStack =
 25                TestHelpers.SharedComponentCreate<UIContainerStack, UIContainerStack.Model>(scene,
 26                    CLASS_ID.UI_CONTAINER_STACK);
 127            yield return uiContainerStack.routine;
 28
 129            UnityEngine.UI.Image image = uiContainerStack.referencesContainer.image;
 30
 31            // Check default properties are applied correctly
 132            Assert.IsTrue(image.color == new Color(0f, 0f, 0f, 0f));
 133            Assert.IsTrue(uiContainerStack.referencesContainer.canvasGroup.blocksRaycasts);
 134            Assert.AreEqual(100f, uiContainerStack.childHookRectTransform.rect.width);
 135            Assert.AreEqual(50f, uiContainerStack.childHookRectTransform.rect.height);
 136            Assert.AreEqual(Vector3.zero, uiContainerStack.childHookRectTransform.localPosition);
 37
 138            yield return TestHelpers.SharedComponentUpdate(uiContainerStack,
 39                new UIContainerStack.Model
 40                {
 41                    parentComponent = screenSpaceShape.id,
 42                    color = new Color(0.2f, 0.7f, 0.05f, 1f),
 43                    isPointerBlocker = true,
 44                    width = new UIValue(275f),
 45                    height = new UIValue(130f),
 46                    positionX = new UIValue(-30f),
 47                    positionY = new UIValue(-15f),
 48                    hAlign = "right",
 49                    vAlign = "bottom"
 50                });
 51
 52            // Check updated properties are applied correctly
 153            Assert.IsTrue(uiContainerStack.referencesContainer.transform.parent ==
 54                          screenSpaceShape.childHookRectTransform);
 155            Assert.IsTrue(image.color == new Color(0.2f, 0.7f, 0.05f, 1f));
 156            Assert.IsTrue(image.raycastTarget);
 157            Assert.AreEqual(275f, uiContainerStack.childHookRectTransform.rect.width);
 158            Assert.AreEqual(130f, uiContainerStack.childHookRectTransform.rect.height);
 159            Assert.IsTrue(uiContainerStack.referencesContainer.layoutGroup.childAlignment == TextAnchor.LowerRight);
 60
 161            Vector2 alignedPosition = CalculateAlignedAnchoredPosition(screenSpaceShape.childHookRectTransform.rect,
 62                uiContainerStack.childHookRectTransform.rect, "bottom", "right");
 163            alignedPosition += new Vector2(-30, -15); // apply offset position
 64
 165            Assert.AreEqual(alignedPosition.ToString(),
 66                uiContainerStack.childHookRectTransform.anchoredPosition.ToString());
 67
 168            screenSpaceShape.Dispose();
 169        }
 70
 71        [UnityTest]
 72        public IEnumerator MissingValuesGetDefaultedOnUpdate()
 73        {
 74            // Create UIScreenSpaceShape
 175            UIScreenSpace screenSpaceShape =
 76                TestHelpers.SharedComponentCreate<UIScreenSpace, UIScreenSpace.Model>(scene,
 77                    CLASS_ID.UI_SCREEN_SPACE_SHAPE);
 178            yield return screenSpaceShape.routine;
 79
 180            yield return TestHelpers.TestSharedComponentDefaultsOnUpdate<UIContainerStack.Model, UIContainerStack>(
 81                scene, CLASS_ID.UI_CONTAINER_STACK);
 182        }
 83
 84        [UnityTest]
 285        public IEnumerator AddedCorrectlyOnInvisibleParent() { yield return TestHelpers.TestUIElementAddedCorrectlyOnInv
 86
 87        [UnityTest]
 88        public IEnumerator NormalizedSize()
 89        {
 90            // Create UIScreenSpaceShape
 191            UIScreenSpace screenSpaceShape =
 92                TestHelpers.SharedComponentCreate<UIScreenSpace, UIScreenSpace.Model>(scene,
 93                    CLASS_ID.UI_SCREEN_SPACE_SHAPE);
 194            yield return screenSpaceShape.routine;
 95
 96            // Create UIContainerStack
 197            UIContainerStack uiContainerStack =
 98                TestHelpers.SharedComponentCreate<UIContainerStack, UIContainerStack.Model>(scene,
 99                    CLASS_ID.UI_CONTAINER_STACK,
 100                    new UIContainerStack.Model
 101                    {
 102                        parentComponent = screenSpaceShape.id,
 103                        width = new UIValue(50, UIValue.Unit.PERCENT),
 104                        height = new UIValue(30, UIValue.Unit.PERCENT)
 105                    });
 1106            yield return uiContainerStack.routine;
 107
 1108            UnityEngine.UI.Image image = uiContainerStack.childHookRectTransform.GetComponent<UnityEngine.UI.Image>();
 109
 110            // Check updated properties are applied correctly
 1111            Assert.AreEqual(screenSpaceShape.childHookRectTransform.rect.width * 0.5f,
 112                uiContainerStack.childHookRectTransform.rect.width, 0.01f);
 1113            Assert.AreEqual(screenSpaceShape.childHookRectTransform.rect.height * 0.3f,
 114                uiContainerStack.childHookRectTransform.rect.height, 0.01f);
 115
 1116            screenSpaceShape.Dispose();
 1117            yield return null;
 1118        }
 119
 120        [UnityTest]
 121        public IEnumerator TestChildrenAreHandledCorrectly()
 122        {
 123            // Create UIScreenSpaceShape
 1124            UIScreenSpace screenSpaceShape =
 125                TestHelpers.SharedComponentCreate<UIScreenSpace, UIScreenSpace.Model>(scene,
 126                    CLASS_ID.UI_SCREEN_SPACE_SHAPE);
 1127            yield return screenSpaceShape.routine;
 128
 129            // Create UIContainerStack
 1130            UIContainerStack uiContainerStack =
 131                TestHelpers.SharedComponentCreate<UIContainerStack, UIContainerStack.Model>(scene,
 132                    CLASS_ID.UI_CONTAINER_STACK);
 1133            yield return uiContainerStack.routine;
 134
 135
 136            // Create 1st child object
 1137            UIContainerRect childComponent1 = TestHelpers.SharedComponentCreate<UIContainerRect, UIContainerRect.Model>(
 138                scene, CLASS_ID.UI_CONTAINER_RECT,
 139                new UIContainerRect.Model()
 140                {
 141                    parentComponent = uiContainerStack.id
 142                });
 143
 1144            yield return childComponent1.routine;
 145
 1146            Assert.IsTrue(uiContainerStack.stackContainers.ContainsKey(childComponent1.id));
 1147            Assert.IsTrue(childComponent1.referencesContainer.transform.parent.gameObject ==
 148                          uiContainerStack.stackContainers[childComponent1.id]);
 149
 150            // Create 2nd child object
 1151            UIImage childComponent2 = TestHelpers.SharedComponentCreate<UIImage, UIImage.Model>(scene,
 152                CLASS_ID.UI_IMAGE_SHAPE,
 153                new UIImage.Model
 154                {
 155                    parentComponent = uiContainerStack.id,
 156                });
 157
 1158            yield return childComponent2.routine;
 159
 1160            Assert.IsTrue(uiContainerStack.stackContainers.ContainsKey(childComponent2.id));
 1161            Assert.IsTrue(childComponent2.referencesContainer.transform.parent.gameObject ==
 162                          uiContainerStack.stackContainers[childComponent2.id]);
 163
 1164            screenSpaceShape.Dispose();
 1165            yield return null;
 1166        }
 167
 168        [UnityTest]
 169        public IEnumerator VerticalStackIsAppliedCorrectly()
 170        {
 171            // Create UIScreenSpaceShape
 1172            UIScreenSpace screenSpaceShape =
 173                TestHelpers.SharedComponentCreate<UIScreenSpace, UIScreenSpace.Model>(scene,
 174                    CLASS_ID.UI_SCREEN_SPACE_SHAPE);
 1175            yield return screenSpaceShape.routine;
 176
 177            // Create UIContainerStack
 1178            UIContainerStack uiContainerStack =
 179                TestHelpers.SharedComponentCreate<UIContainerStack, UIContainerStack.Model>(scene,
 180                    CLASS_ID.UI_CONTAINER_STACK,
 181                    new UIContainerStack.Model
 182                    {
 183                        width = new UIValue(500f),
 184                        height = new UIValue(300f)
 185                    });
 186
 1187            yield return uiContainerStack.routine;
 188
 189            // Check container stack was initialized correctly
 1190            Assert.IsTrue(uiContainerStack != null);
 191
 1192            var layoutGroup = uiContainerStack.referencesContainer.GetComponentInChildren<VerticalLayoutGroup>();
 1193            Assert.IsTrue(layoutGroup != null);
 1194            Assert.IsFalse(layoutGroup.childControlHeight);
 1195            Assert.IsFalse(layoutGroup.childControlWidth);
 1196            Assert.IsFalse(layoutGroup.childForceExpandWidth);
 1197            Assert.IsFalse(layoutGroup.childForceExpandHeight);
 198
 199            // Create 1st child object
 1200            UIContainerRect childComponent1 = TestHelpers.SharedComponentCreate<UIContainerRect, UIContainerRect.Model>(
 201                scene, CLASS_ID.UI_CONTAINER_RECT,
 202                new UIContainerRect.Model
 203                {
 204                    parentComponent = uiContainerStack.id,
 205                    width = new UIValue(130f),
 206                    height = new UIValue(70f)
 207                });
 208
 1209            yield return childComponent1.routine;
 210
 211            // Create 2nd child object
 1212            UIImage childComponent2 = TestHelpers.SharedComponentCreate<UIImage, UIImage.Model>(scene,
 213                CLASS_ID.UI_IMAGE_SHAPE,
 214                new UIImage.Model
 215                {
 216                    parentComponent = uiContainerStack.id,
 217                    width = new UIValue(75f),
 218                    height = new UIValue(35f)
 219                });
 220
 1221            yield return childComponent2.routine;
 222
 223            // Create 3rd child object
 1224            UIInputText childComponent3 = TestHelpers.SharedComponentCreate<UIInputText, UIInputText.Model>(scene,
 225                CLASS_ID.UI_INPUT_TEXT_SHAPE,
 226                new UIInputText.Model
 227                {
 228                    parentComponent = uiContainerStack.id,
 229                    width = new UIValue(150f),
 230                    height = new UIValue(50f)
 231                });
 232
 1233            yield return childComponent3.routine;
 234
 1235            RectTransform child1RT = childComponent1.referencesContainer.transform.parent as RectTransform;
 1236            RectTransform child2RT = childComponent2.referencesContainer.transform.parent as RectTransform;
 1237            RectTransform child3RT = childComponent3.referencesContainer.transform.parent as RectTransform;
 238
 1239            Assert.AreEqual(new Vector2(65, -35).ToString(), child1RT.anchoredPosition.ToString());
 1240            Assert.AreEqual(new Vector2(37.5f, -87.5f).ToString(), child2RT.anchoredPosition.ToString());
 1241            Assert.AreEqual(new Vector2(75, -130).ToString(), child3RT.anchoredPosition.ToString());
 242
 1243            Assert.AreEqual(new Vector2(130, 70).ToString(), child1RT.sizeDelta.ToString());
 1244            Assert.AreEqual(new Vector2(75, 35).ToString(), child2RT.sizeDelta.ToString());
 1245            Assert.AreEqual(new Vector2(150, 50).ToString(), child3RT.sizeDelta.ToString());
 246
 1247            screenSpaceShape.Dispose();
 1248            yield return null;
 1249        }
 250
 251        [UnityTest]
 252        public IEnumerator HorizontalStackIsAppliedCorrectly()
 253        {
 254            // Create UIScreenSpaceShape
 1255            UIScreenSpace screenSpaceShape =
 256                TestHelpers.SharedComponentCreate<UIScreenSpace, UIScreenSpace.Model>(scene,
 257                    CLASS_ID.UI_SCREEN_SPACE_SHAPE);
 1258            yield return screenSpaceShape.routine;
 259
 260            // Create UIContainerStack
 1261            UIContainerStack uiContainerStack =
 262                TestHelpers.SharedComponentCreate<UIContainerStack, UIContainerStack.Model>(scene,
 263                    CLASS_ID.UI_CONTAINER_STACK);
 1264            yield return uiContainerStack.routine;
 265
 1266            yield return TestHelpers.SharedComponentUpdate(uiContainerStack,
 267                new UIContainerStack.Model
 268                {
 269                    width = new UIValue(500f),
 270                    height = new UIValue(300f),
 271                    stackOrientation = UIContainerStack.StackOrientation.HORIZONTAL
 272                });
 273
 274            // Check container stack was initialized correctly
 1275            Assert.IsTrue(uiContainerStack != null);
 276
 1277            var layoutGroup = uiContainerStack.referencesContainer.GetComponentInChildren<HorizontalLayoutGroup>();
 1278            Assert.IsTrue(layoutGroup != null);
 1279            Assert.IsFalse(layoutGroup.childControlHeight);
 1280            Assert.IsFalse(layoutGroup.childControlWidth);
 1281            Assert.IsFalse(layoutGroup.childForceExpandWidth);
 1282            Assert.IsFalse(layoutGroup.childForceExpandHeight);
 283
 284            // Create 1st child object
 1285            UIContainerRect childComponent1 = TestHelpers.SharedComponentCreate<UIContainerRect, UIContainerRect.Model>(
 286                scene, CLASS_ID.UI_CONTAINER_RECT,
 287                new UIContainerRect.Model
 288                {
 289                    parentComponent = uiContainerStack.id,
 290                    width = new UIValue(130f),
 291                    height = new UIValue(70f)
 292                });
 293
 1294            yield return childComponent1.routine;
 295
 296            // Create 2nd child object
 1297            UIImage childComponent2 = TestHelpers.SharedComponentCreate<UIImage, UIImage.Model>(scene,
 298                CLASS_ID.UI_IMAGE_SHAPE,
 299                new UIImage.Model
 300                {
 301                    parentComponent = uiContainerStack.id,
 302                    width = new UIValue(75f),
 303                    height = new UIValue(35f)
 304                });
 1305            yield return childComponent2.routine;
 306
 307            /// Create 3rd child object
 1308            UIInputText childComponent3 = TestHelpers.SharedComponentCreate<UIInputText, UIInputText.Model>(scene,
 309                CLASS_ID.UI_INPUT_TEXT_SHAPE,
 310                new UIInputText.Model
 311                {
 312                    parentComponent = uiContainerStack.id,
 313                    width = new UIValue(150f),
 314                    height = new UIValue(50f)
 315                });
 316
 1317            yield return childComponent3.routine;
 318
 1319            RectTransform child1RT = childComponent1.referencesContainer.transform.parent as RectTransform;
 1320            RectTransform child2RT = childComponent2.referencesContainer.transform.parent as RectTransform;
 1321            RectTransform child3RT = childComponent3.referencesContainer.transform.parent as RectTransform;
 322
 1323            Assert.AreEqual(new Vector2(65, -35).ToString(), child1RT.anchoredPosition.ToString());
 1324            Assert.AreEqual(new Vector2(167.5f, -17.5f).ToString(), child2RT.anchoredPosition.ToString());
 1325            Assert.AreEqual(new Vector2(280, -25).ToString(), child3RT.anchoredPosition.ToString());
 326
 1327            Assert.AreEqual(new Vector2(130, 70).ToString(), child1RT.sizeDelta.ToString());
 1328            Assert.AreEqual(new Vector2(75, 35).ToString(), child2RT.sizeDelta.ToString());
 1329            Assert.AreEqual(new Vector2(150, 50).ToString(), child3RT.sizeDelta.ToString());
 330
 1331            screenSpaceShape.Dispose();
 1332            yield return null;
 1333        }
 334
 335        [UnityTest]
 336        public IEnumerator AdaptSizeIsAppliedCorrectly()
 337        {
 338            // Create UIScreenSpaceShape
 1339            UIScreenSpace screenSpaceShape =
 340                TestHelpers.SharedComponentCreate<UIScreenSpace, UIScreenSpace.Model>(scene,
 341                    CLASS_ID.UI_SCREEN_SPACE_SHAPE);
 1342            yield return screenSpaceShape.routine;
 343
 344            // Create UIContainerStack
 1345            UIContainerStack uiContainerStack =
 346                TestHelpers.SharedComponentCreate<UIContainerStack, UIContainerStack.Model>(scene,
 347                    CLASS_ID.UI_CONTAINER_STACK,
 348                    new UIContainerStack.Model
 349                    {
 350                        width = new UIValue(500f),
 351                        height = new UIValue(300f)
 352                    });
 353
 1354            yield return uiContainerStack.routine;
 355
 356            // Create 1st child object
 1357            UIContainerRect childComponent1 = TestHelpers.SharedComponentCreate<UIContainerRect, UIContainerRect.Model>(
 358                scene, CLASS_ID.UI_CONTAINER_RECT,
 359                new UIContainerRect.Model
 360                {
 361                    parentComponent = uiContainerStack.id,
 362                    width = new UIValue(130f),
 363                    height = new UIValue(70f)
 364                });
 365
 1366            yield return childComponent1.routine;
 367
 368            // Create 2nd child object
 1369            UIImage childComponent2 = TestHelpers.SharedComponentCreate<UIImage, UIImage.Model>(scene,
 370                CLASS_ID.UI_IMAGE_SHAPE,
 371                new UIImage.Model
 372                {
 373                    parentComponent = uiContainerStack.id,
 374                    width = new UIValue(75f),
 375                    height = new UIValue(35f)
 376                });
 1377            yield return childComponent2.routine;
 378
 379            // Create 3rd child object
 1380            UIInputText childComponent3 = TestHelpers.SharedComponentCreate<UIInputText, UIInputText.Model>(scene,
 381                CLASS_ID.UI_INPUT_TEXT_SHAPE,
 382                new UIInputText.Model
 383                {
 384                    parentComponent = uiContainerStack.id,
 385                    width = new UIValue(150f),
 386                    height = new UIValue(50f)
 387                });
 1388            yield return childComponent3.routine;
 389
 390
 1391            yield return TestHelpers.SharedComponentUpdate(uiContainerStack,
 392                new UIContainerStack.Model
 393                {
 394                    adaptHeight = true,
 395                    adaptWidth = true,
 396                });
 397
 1398            yield return null;
 399
 400            // Check stacked components position
 1401            Assert.AreEqual(150f, uiContainerStack.childHookRectTransform.rect.width, 0.01f);
 1402            Assert.AreEqual(
 403                childComponent1.referencesContainer.rectTransform.rect.height + childComponent2.referencesContainer
 404                                                                                               .rectTransform.rect.heigh
 405                                                                              + childComponent3.referencesContainer
 406                                                                                               .rectTransform.rect.heigh
 407                uiContainerStack.childHookRectTransform.rect.height, 0.01f);
 408
 1409            screenSpaceShape.Dispose();
 1410            yield return null;
 1411        }
 412    }
 413}