< Summary

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

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
TestPropertiesAreAppliedCorrectly()0%550100%
TestParenting()0%770100%
TestMissingValuesGetDefaultedOnUpdate()0%440100%
AddedCorrectlyOnInvisibleParent()0%330100%
TestNormalizedSize()0%550100%
TestOnClickEvent()0%660100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/UI/UIContainer/Tests/UIContainerRectTests.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 UIContainerRectTests : UITestsBase
 13    {
 14        [UnityTest]
 15        public IEnumerator TestPropertiesAreAppliedCorrectly()
 16        {
 17            // Create UIScreenSpaceShape
 118            UIScreenSpace screenSpaceShape =
 19                TestHelpers.SharedComponentCreate<UIScreenSpace, UIScreenSpace.Model>(scene,
 20                    CLASS_ID.UI_SCREEN_SPACE_SHAPE);
 21
 122            yield return screenSpaceShape.routine;
 23
 24            // Create UIContainerRectShape
 125            UIContainerRect uiContainerRectShape =
 26                TestHelpers.SharedComponentCreate<UIContainerRect, UIContainerRect.Model>(scene,
 27                    CLASS_ID.UI_CONTAINER_RECT);
 28
 129            yield return uiContainerRectShape.routine;
 30
 131            UnityEngine.UI.Image image = uiContainerRectShape.referencesContainer.image;
 32
 33            // Check default properties are applied correctly
 134            Assert.AreEqual(0f, uiContainerRectShape.referencesContainer.canvasGroup.alpha);
 135            Assert.IsTrue(image.GetComponent<Outline>() == null);
 136            Assert.IsTrue(image.color == new Color(0f, 0f, 0f, 0f));
 137            Assert.IsTrue(uiContainerRectShape.referencesContainer.canvasGroup.blocksRaycasts);
 138            Assert.AreEqual(100f, uiContainerRectShape.childHookRectTransform.rect.width);
 139            Assert.AreEqual(50f, uiContainerRectShape.childHookRectTransform.rect.height);
 140            Assert.AreEqual(Vector3.zero, uiContainerRectShape.childHookRectTransform.localPosition);
 41
 42            // Update UIContainerRectShape properties
 143            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
 157            yield return uiContainerRectShape.routine;
 58
 59            // Check updated properties are applied correctly
 160            Assert.AreEqual(1f, uiContainerRectShape.referencesContainer.canvasGroup.alpha);
 161            Assert.IsTrue(uiContainerRectShape.referencesContainer.transform.parent == screenSpaceShape.childHookRectTra
 162            Assert.IsTrue(image.GetComponent<Outline>() != null);
 163            Assert.IsTrue(image.color == new Color(0.2f, 0.7f, 0.05f, 1f));
 164            Assert.IsFalse(uiContainerRectShape.referencesContainer.canvasGroup.blocksRaycasts);
 165            Assert.AreEqual(275f, uiContainerRectShape.childHookRectTransform.rect.width);
 166            Assert.AreEqual(130f, uiContainerRectShape.childHookRectTransform.rect.height);
 167            Assert.IsTrue(uiContainerRectShape.referencesContainer.layoutGroup.childAlignment == TextAnchor.LowerRight);
 68
 169            Vector2 alignedPosition = CalculateAlignedAnchoredPosition(screenSpaceShape.childHookRectTransform.rect,
 70                uiContainerRectShape.childHookRectTransform.rect, "bottom", "right");
 171            alignedPosition += new Vector2(-30, -15); // apply offset position
 72
 173            Assert.AreEqual(alignedPosition.ToString(),
 74                uiContainerRectShape.childHookRectTransform.anchoredPosition.ToString());
 75
 176            screenSpaceShape.Dispose();
 177        }
 78
 79        [UnityTest]
 80        public IEnumerator TestParenting()
 81        {
 82            // Create UIScreenSpaceShape
 183            UIScreenSpace screenSpaceShape =
 84                TestHelpers.SharedComponentCreate<UIScreenSpace, UIScreenSpace.Model>(scene,
 85                    CLASS_ID.UI_SCREEN_SPACE_SHAPE);
 186            yield return screenSpaceShape.routine;
 87
 88            // Create UIContainerRectShape
 189            UIContainerRect uiContainerRectShape =
 90                TestHelpers.SharedComponentCreate<UIContainerRect, UIContainerRect.Model>(scene,
 91                    CLASS_ID.UI_CONTAINER_RECT);
 192            yield return uiContainerRectShape.routine;
 93
 94            // Update UIContainerRectShape parent
 195            scene.SharedComponentUpdate(uiContainerRectShape.id, JsonUtility.ToJson(new UIContainerRect.Model
 96            {
 97                parentComponent = screenSpaceShape.id,
 98            }));
 199            yield return uiContainerRectShape.routine;
 100
 101            // Check updated parent
 1102            Assert.IsTrue(uiContainerRectShape.referencesContainer.transform.parent ==
 103                          screenSpaceShape.childHookRectTransform);
 104
 105            // Create 2nd UIContainerRectShape
 1106            UIContainerRect uiContainerRectShape2 =
 107                TestHelpers.SharedComponentCreate<UIContainerRect, UIContainerRect.Model>(scene,
 108                    CLASS_ID.UI_CONTAINER_RECT);
 1109            yield return uiContainerRectShape2.routine;
 110
 111            // Update UIContainerRectShape parent to the previous container
 1112            scene.SharedComponentUpdate(uiContainerRectShape2.id, JsonUtility.ToJson(new UIContainerRect.Model
 113            {
 114                parentComponent = uiContainerRectShape.id,
 115            }));
 1116            yield return uiContainerRectShape2.routine;
 117
 118            // Check updated parent
 1119            Assert.IsTrue(uiContainerRectShape2.referencesContainer.transform.parent ==
 120                          uiContainerRectShape.childHookRectTransform);
 121
 1122            screenSpaceShape.Dispose();
 1123        }
 124
 125        [UnityTest]
 126        public IEnumerator TestMissingValuesGetDefaultedOnUpdate()
 127        {
 128            //// Create UIScreenSpaceShape
 1129            UIScreenSpace screenSpaceShape =
 130                TestHelpers.SharedComponentCreate<UIScreenSpace, UIScreenSpace.Model>(scene,
 131                    CLASS_ID.UI_SCREEN_SPACE_SHAPE);
 1132            yield return screenSpaceShape.routine;
 133
 1134            Assert.IsFalse(screenSpaceShape == null);
 135
 1136            yield return TestHelpers.TestSharedComponentDefaultsOnUpdate<UIContainerRect.Model, UIContainerRect>(scene,
 137                CLASS_ID.UI_CONTAINER_RECT);
 1138        }
 139
 140        [UnityTest]
 2141        public IEnumerator AddedCorrectlyOnInvisibleParent() { yield return TestHelpers.TestUIElementAddedCorrectlyOnInv
 142
 143        [UnityTest]
 144        public IEnumerator TestNormalizedSize()
 145        {
 146            // Create UIScreenSpaceShape
 1147            UIScreenSpace screenSpaceShape =
 148                TestHelpers.SharedComponentCreate<UIScreenSpace, UIScreenSpace.Model>(scene,
 149                    CLASS_ID.UI_SCREEN_SPACE_SHAPE);
 1150            yield return screenSpaceShape.routine;
 151
 152            // Create UIContainerRectShape
 1153            UIContainerRect uiContainerRectShape =
 154                TestHelpers.SharedComponentCreate<UIContainerRect, UIContainerRect.Model>(scene,
 155                    CLASS_ID.UI_CONTAINER_RECT);
 1156            yield return uiContainerRectShape.routine;
 157
 158            // Update UIContainerRectShape properties
 1159            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;
 1165            yield return uiContainerRectShape.routine;
 166
 1167            UnityEngine.UI.Image image =
 168                uiContainerRectShape.childHookRectTransform.GetComponent<UnityEngine.UI.Image>();
 169
 170            // Check updated properties are applied correctly
 1171            Assert.AreEqual(screenSpaceShape.childHookRectTransform.rect.width * 0.5f,
 172                uiContainerRectShape.childHookRectTransform.rect.width, 0.01f);
 1173            Assert.AreEqual(screenSpaceShape.childHookRectTransform.rect.height * 0.3f,
 174                uiContainerRectShape.childHookRectTransform.rect.height, 0.01f);
 175
 1176            screenSpaceShape.Dispose();
 1177        }
 178
 179        [UnityTest]
 180        public IEnumerator TestOnClickEvent()
 181        {
 182            // Create UIScreenSpaceShape
 1183            UIScreenSpace screenSpaceShape =
 184                TestHelpers.SharedComponentCreate<UIScreenSpace, UIScreenSpace.Model>(scene,
 185                    CLASS_ID.UI_SCREEN_SPACE_SHAPE);
 186
 1187            yield return screenSpaceShape.routine;
 188
 189            // Create UIContainerRectShape
 1190            UIContainerRect uiContainerRectShape =
 191                TestHelpers.SharedComponentCreate<UIContainerRect, UIContainerRect.Model>(scene,
 192                    CLASS_ID.UI_CONTAINER_RECT);
 193
 1194            yield return uiContainerRectShape.routine;
 195
 196            // Update UIContainerRectShape properties
 1197            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
 1212            yield return uiContainerRectShape.routine;
 213
 214            //------------------------------------------------------------------------
 215            // Test click events
 1216            bool eventResult = false;
 217
 1218            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
 1225                    eventResult = res;
 1226                });
 227
 1228            Assert.IsTrue(eventResult);
 1229        }
 230    }
 231}