< Summary

Class:Tests.TextShapeTests
Assembly:TextShapeTests
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/TextShape/Tests/TextShapeTests.cs
Covered lines:69
Uncovered lines:0
Coverable lines:69
Total lines:183
Line coverage:100% (69 of 69)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
TestCreate()0%660100%
TMProConsistencyAsserts(...)0%110100%
TestMissingValuesGetDefaultedOnUpdate()0%440100%
OpacityIsProcessedCorrectly()0%330100%
VisibleTrueIsProcessedCorrectly()0%330100%
VisibleFalseIsProcessedCorrectly()0%330100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/TextShape/Tests/TextShapeTests.cs

#LineLine coverage
 1using DCL.Components;
 2using DCL.Helpers;
 3using DCL.Models;
 4using NUnit.Framework;
 5using System.Collections;
 6using TMPro;
 7using UnityEngine;
 8using UnityEngine.TestTools;
 9
 10namespace Tests
 11{
 12    public class TextShapeTests : IntegrationTestSuite_Legacy
 13    {
 14        [UnityTest]
 15        public IEnumerator TestCreate()
 16        {
 117            string entityId = "e1";
 18
 119            TestHelpers.CreateSceneEntity(scene, entityId);
 20
 121            yield return null;
 22
 123            var textShapeModel = new TextShape.Model()
 24            {
 25                value = "Hello world!",
 26
 27                color = Color.white,
 28                opacity = 0.5f,
 29                fontSize = 10,
 30                fontWeight = "",
 31
 32                width = 20,
 33                height = 20,
 34                adaptHeight = false,
 35                adaptWidth = false,
 36                paddingTop = 10,
 37                paddingRight = 0,
 38                paddingBottom = 10,
 39                paddingLeft = 0,
 40
 41                shadowBlur = 0,
 42                shadowOffsetX = 0,
 43                shadowOffsetY = 0,
 44                shadowColor = Color.white
 45            };
 46
 147            TextShape textShape =
 48                TestHelpers.EntityComponentCreate<TextShape, TextShape.Model>(scene, scene.entities[entityId],
 49                    textShapeModel);
 50
 151            yield return textShape.routine;
 52
 153            TextMeshPro tmpro = textShape.GetComponentInChildren<TextMeshPro>();
 54
 155            Assert.IsTrue(textShape != null, "Component creation fail!");
 156            Assert.IsTrue(tmpro != null, "TextMeshPro doesn't exists for TextShape!");
 157            Assert.IsTrue(textShape.text != null, "Unity Text component doesn't exists for TextShape!");
 58
 159            yield return null;
 60
 161            TMProConsistencyAsserts(tmpro, textShapeModel);
 62
 163            textShapeModel.paddingLeft = 5;
 164            textShapeModel.paddingRight = 15;
 165            textShapeModel.value = "Hello world again!";
 66
 167            TextShape textShape2 =
 68                TestHelpers.EntityComponentCreate<TextShape, TextShape.Model>(scene, scene.entities[entityId],
 69                    textShapeModel);
 70
 171            TMProConsistencyAsserts(tmpro, textShapeModel);
 72
 173            yield return null;
 174        }
 75
 76        void TMProConsistencyAsserts(TextMeshPro tmpro, TextShape.Model model)
 77        {
 278            Assert.AreEqual(model.paddingLeft, tmpro.margin[0], 0.01,
 79                string.Format("Left margin must be {0}", model.paddingLeft));
 280            Assert.AreEqual(model.paddingTop, tmpro.margin[1], 0.01,
 81                string.Format("Top margin must be {0}", model.paddingTop));
 282            Assert.AreEqual(model.paddingRight, tmpro.margin[2], 0.01,
 83                string.Format("Right margin must be {0}", model.paddingRight));
 284            Assert.AreEqual(model.paddingBottom, tmpro.margin[3], 0.01,
 85                string.Format("Bottom margin must be {0}", model.paddingBottom));
 86
 287            Assert.IsTrue(tmpro.text == model.value, "Text wasn't set correctly!");
 288        }
 89
 90        [UnityTest]
 91        public IEnumerator TestMissingValuesGetDefaultedOnUpdate()
 92        {
 193            string entityId = "1";
 194            TestHelpers.CreateSceneEntity(scene, entityId);
 95
 96            // 1. Create component with non-default configs
 197            TextShape.Model textShapeModel = new TextShape.Model
 98            {
 99                color = Color.green,
 100                width = 0.25f,
 101                lineCount = 3,
 102                fontAutoSize = true,
 103                shadowColor = Color.red
 104            };
 105
 1106            TextShape textShapeComponent =
 107                TestHelpers.EntityComponentCreate<TextShape, TextShape.Model>(scene, scene.entities[entityId],
 108                    textShapeModel);
 109
 1110            yield return textShapeComponent.routine;
 111
 112            // 2. Check configured values
 1113            Assert.AreEqual(Color.green, textShapeComponent.GetModel().color);
 1114            Assert.AreEqual(0.25f, textShapeComponent.GetModel().width);
 1115            Assert.AreEqual(3, textShapeComponent.GetModel().lineCount);
 1116            Assert.IsTrue(textShapeComponent.GetModel().fontAutoSize);
 1117            Assert.AreEqual(Color.red, textShapeComponent.GetModel().shadowColor);
 118
 119            // 3. Update component with missing values
 1120            scene.EntityComponentUpdate(scene.entities[entityId], CLASS_ID_COMPONENT.TEXT_SHAPE,
 121                JsonUtility.ToJson(new TextShape.Model { }));
 122
 1123            yield return textShapeComponent.routine;
 124
 125            // 4. Check defaulted values
 1126            Assert.AreEqual(Color.white, textShapeComponent.GetModel().color);
 1127            Assert.AreEqual(1f, textShapeComponent.GetModel().width);
 1128            Assert.AreEqual(0, textShapeComponent.GetModel().lineCount);
 1129            Assert.IsFalse(textShapeComponent.GetModel().fontAutoSize);
 1130            Assert.AreEqual(new Color(1, 1, 1), textShapeComponent.GetModel().shadowColor);
 1131        }
 132
 133        [UnityTest]
 134        [TestCase(0, ExpectedResult = null)]
 135        [TestCase(0.3f, ExpectedResult = null)]
 136        [TestCase(1, ExpectedResult = null)]
 137        public IEnumerator OpacityIsProcessedCorrectly(float opacity)
 138        {
 3139            IDCLEntity entity = TestHelpers.CreateSceneEntity(scene);
 3140            TextShape textShapeComponent = TestHelpers.EntityComponentCreate<TextShape, TextShape.Model>(scene, entity, 
 141
 3142            yield return textShapeComponent.routine;
 143
 3144            TextMeshPro tmpro = textShapeComponent.gameObject.GetComponentInChildren<TextMeshPro>();
 145
 3146            Assert.NotNull(tmpro);
 3147            Assert.AreEqual(opacity, textShapeComponent.GetModel().opacity);
 3148            Assert.AreEqual(tmpro.color.a, opacity);
 3149        }
 150
 151        [UnityTest]
 152        public IEnumerator VisibleTrueIsProcessedCorrectly()
 153        {
 1154            IDCLEntity entity = TestHelpers.CreateSceneEntity(scene);
 1155            TextShape textShapeComponent = TestHelpers.EntityComponentCreate<TextShape, TextShape.Model>(scene, entity, 
 156
 1157            yield return textShapeComponent.routine;
 158
 1159            TextMeshPro tmpro = textShapeComponent.gameObject.GetComponentInChildren<TextMeshPro>();
 160
 1161            Assert.NotNull(tmpro);
 1162            Assert.AreEqual(0.3f, textShapeComponent.GetModel().opacity);
 1163            Assert.IsTrue(textShapeComponent.GetModel().visible);
 1164            Assert.AreEqual(tmpro.color.a, 0.3f);
 1165        }
 166
 167        [UnityTest]
 168        public IEnumerator VisibleFalseIsProcessedCorrectly()
 169        {
 1170            IDCLEntity entity = TestHelpers.CreateSceneEntity(scene);
 1171            TextShape textShapeComponent = TestHelpers.EntityComponentCreate<TextShape, TextShape.Model>(scene, entity, 
 172
 1173            yield return textShapeComponent.routine;
 174
 1175            TextMeshPro tmpro = textShapeComponent.gameObject.GetComponentInChildren<TextMeshPro>();
 176
 1177            Assert.NotNull(tmpro);
 1178            Assert.AreEqual(0.3f, textShapeComponent.GetModel().opacity);
 1179            Assert.IsFalse(textShapeComponent.GetModel().visible);
 1180            Assert.AreEqual(tmpro.color.a, 0f);
 1181        }
 182    }
 183}