< Summary

Class:Tests.UIScrollRectTests
Assembly:UIScrollRectTests
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/UI/UIScrollRect/Tests/UIScrollRectTests.cs
Covered lines:46
Uncovered lines:12
Coverable lines:58
Total lines:194
Line coverage:79.3% (46 of 58)
Covered branches:0
Total branches:0

Metrics

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

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/UI/UIScrollRect/Tests/UIScrollRectTests.cs

#LineLine coverage
 1using DCL.Components;
 2using DCL.Helpers;
 3using DCL.Models;
 4using NUnit.Framework;
 5using System.Collections;
 6using UnityEngine;
 7using UnityEngine.TestTools;
 8
 9namespace Tests
 10{
 11    public class UIScrollRectTests : UITestsBase
 12    {
 13        [UnityTest]
 14        public IEnumerator TestPropertiesAreAppliedCorrectly()
 15        {
 16            // Create UIScreenSpaceShape
 117            UIScreenSpace screenSpaceShape =
 18                TestHelpers.SharedComponentCreate<UIScreenSpace, UIScreenSpace.Model>(scene,
 19                    CLASS_ID.UI_SCREEN_SPACE_SHAPE);
 120            yield return screenSpaceShape.routine;
 21
 122            UIScrollRect scrRect =
 23                TestHelpers.SharedComponentCreate<UIScrollRect, UIScrollRect.Model>(scene, CLASS_ID.UI_SLIDER_SHAPE);
 124            yield return scrRect.routine;
 25
 26            // Force a new update to pass the first apply
 127            yield return TestHelpers.SharedComponentUpdate(scrRect, new UIScrollRect.Model
 28            {
 29                name = "newName"
 30            });
 31
 132            var refC = scrRect.referencesContainer;
 33
 134            Assert.IsTrue(refC.canvasGroup.blocksRaycasts);
 35            //Canvas group is disabled on first apply
 136            Assert.AreEqual(1, refC.canvasGroup.alpha);
 37
 38            // Apply padding
 139            Assert.AreEqual(0, refC.paddingLayoutGroup.padding.bottom);
 140            Assert.AreEqual(0, refC.paddingLayoutGroup.padding.top);
 141            Assert.AreEqual(0, refC.paddingLayoutGroup.padding.left);
 142            Assert.AreEqual(0, refC.paddingLayoutGroup.padding.right);
 43
 144            Assert.IsFalse(refC.scrollRect.horizontal);
 145            Assert.IsTrue(refC.scrollRect.vertical);
 46
 147            Assert.AreEqual(0, refC.HScrollbar.value);
 148            Assert.AreEqual(0, refC.VScrollbar.value);
 49
 50            // Update UIScrollRect properties
 151            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
 165            Assert.IsTrue(scrRect.referencesContainer.transform.parent == screenSpaceShape.childHookRectTransform);
 166            Assert.IsFalse(scrRect.referencesContainer.canvasGroup.blocksRaycasts);
 167            Assert.AreEqual(275f, scrRect.childHookRectTransform.rect.width);
 168            Assert.AreEqual(130f, scrRect.childHookRectTransform.rect.height);
 169            Assert.IsTrue(scrRect.referencesContainer.layoutGroup.childAlignment == TextAnchor.LowerRight);
 70
 171            Vector2 alignedPosition = CalculateAlignedAnchoredPosition(screenSpaceShape.childHookRectTransform.rect,
 72                scrRect.childHookRectTransform.rect, "bottom", "right");
 173            alignedPosition += new Vector2(-30, -15); // apply offset position
 74
 175            Assert.AreEqual(alignedPosition.ToString(),
 76                scrRect.referencesContainer.layoutElementRT.anchoredPosition.ToString());
 77
 178            screenSpaceShape.Dispose();
 179        }
 80
 81        [UnityTest]
 82        public IEnumerator TestOnClickEvent()
 83        {
 84            // Create UIScreenSpaceShape
 185            UIScreenSpace screenSpaceShape =
 86                TestHelpers.SharedComponentCreate<UIScreenSpace, UIScreenSpace.Model>(scene,
 87                    CLASS_ID.UI_SCREEN_SPACE_SHAPE);
 188            yield return screenSpaceShape.routine;
 89
 190            UIScrollRect scrRect =
 91                TestHelpers.SharedComponentCreate<UIScrollRect, UIScrollRect.Model>(scene, CLASS_ID.UI_SLIDER_SHAPE);
 192            yield return scrRect.routine;
 93
 94            // Force a new update to pass the first apply
 195            yield return TestHelpers.SharedComponentUpdate(scrRect, new UIScrollRect.Model
 96            {
 97                name = "newName"
 98            });
 99
 100            // Update UIScrollRect properties
 1101            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
 1117            bool eventResult = false;
 118
 1119            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
 1126                    eventResult = res;
 1127                });
 128
 1129            Assert.IsTrue(eventResult);
 130
 1131            screenSpaceShape.Dispose();
 1132        }
 133
 134        [UnityTest]
 135        public IEnumerator TestMissingValuesGetDefaultedOnUpdate()
 136        {
 137            //// Create UIScreenSpaceShape
 1138            UIScreenSpace screenSpaceShape =
 139                TestHelpers.SharedComponentCreate<UIScreenSpace, UIScreenSpace.Model>(scene,
 140                    CLASS_ID.UI_SCREEN_SPACE_SHAPE);
 141
 1142            yield return screenSpaceShape.routine;
 143
 1144            Assert.IsFalse(screenSpaceShape == null);
 145
 1146            yield return TestHelpers.TestSharedComponentDefaultsOnUpdate<UIScrollRect.Model, UIScrollRect>(scene,
 147                CLASS_ID.UI_SLIDER_SHAPE);
 1148        }
 149
 150        [UnityTest]
 2151        public IEnumerator AddedCorrectlyOnInvisibleParent() { yield return TestHelpers.TestUIElementAddedCorrectlyOnInv
 152
 153        [UnityTest]
 154        [Explicit]
 155        public IEnumerator TestNormalizedSize()
 156        {
 157            // Create UIScreenSpaceShape
 0158            UIScreenSpace screenSpaceShape =
 159                TestHelpers.SharedComponentCreate<UIScreenSpace, UIScreenSpace.Model>(scene,
 160                    CLASS_ID.UI_SCREEN_SPACE_SHAPE);
 161
 0162            yield return screenSpaceShape.routine;
 163
 164            // Create UIContainerRectShape
 0165            UIContainerRect uiContainerRectShape = TestHelpers.SharedComponentCreate<UIContainerRect, UIContainerRect.Mo
 166                CLASS_ID.UI_CONTAINER_RECT);
 167
 0168            yield return uiContainerRectShape.routine;
 169
 170            // Update UIContainerRectShape properties
 0171            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
 0179            yield return uiContainerRectShape.routine;
 180
 0181            UnityEngine.UI.Image image =
 182                uiContainerRectShape.childHookRectTransform.GetComponent<UnityEngine.UI.Image>();
 183
 184            // Check updated properties are applied correctly
 0185            Assert.AreEqual(screenSpaceShape.childHookRectTransform.rect.width * 0.5f,
 186                uiContainerRectShape.childHookRectTransform.rect.width, 0.01f);
 0187            Assert.AreEqual(screenSpaceShape.childHookRectTransform.rect.height * 0.3f,
 188                uiContainerRectShape.childHookRectTransform.rect.height, 0.01f);
 189
 0190            yield return new WaitForAllMessagesProcessed();
 0191            screenSpaceShape.Dispose();
 0192        }
 193    }
 194}