< Summary

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

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
BillboardTests()0%110100%
AddBillboardComponent()0%550100%
CheckLookAtPlayer()0%880100%
CheckLookAtPlayerWhileTransformMoves()0%770100%
CreateComponent()0%440100%
GetLookAtVector(...)0%770100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Billboard/Tests/BillboardTests.cs

#LineLine coverage
 1using DCL;
 2using DCL.Helpers;
 3using System.Collections;
 4using UnityEngine;
 5using UnityEngine.Assertions;
 6using UnityEngine.TestTools;
 7
 8namespace Tests
 9{
 10    public class BillboardTests : IntegrationTestSuite_Legacy
 11    {
 12        Billboard billboard;
 113        string entityId = "e1";
 14
 15        [UnityTest]
 16        public IEnumerator AddBillboardComponent()
 17        {
 118            yield return CreateComponent(x: false, y: true, z: false);
 19
 120            Assert.IsFalse(billboard.GetModel().x, "Wrong model data! x should be false.");
 121            Assert.IsTrue(billboard.GetModel().y, "Wrong model data! y should be true.");
 122            Assert.IsFalse(billboard.GetModel().z, "Wrong model data! z should be false");
 23
 124            yield return null;
 25
 126            yield return null;
 127        }
 28
 29        [UnityTest]
 30        public IEnumerator CheckLookAtPlayer()
 31        {
 132            DCLCharacterController.i.PauseGravity();
 33
 134            yield return CreateComponent(x: true, y: true, z: true);
 35
 136            yield return billboard.routine;
 37
 138            Transform entityTransform = scene.entities[entityId].gameObject.transform;
 139            Vector3 lookAt = GetLookAtVector(billboard.GetModel(), entityTransform);
 40
 141            yield return null;
 142            Assert.AreApproximatelyEqual(lookAt.x, entityTransform.forward.x, "billboard entity forward vector should be
 143            Assert.AreApproximatelyEqual(lookAt.y, entityTransform.forward.y, "billboard entity forward vector should be
 144            Assert.AreApproximatelyEqual(lookAt.z, entityTransform.forward.z, "billboard entity forward vector should be
 45
 146            var billboardModel = new Billboard.Model()
 47            {
 48                x = true,
 49                y = false,
 50                z = true
 51            };
 52
 153            yield return TestHelpers.EntityComponentUpdate<Billboard, Billboard.Model>(billboard, billboardModel);
 54
 155            lookAt = GetLookAtVector(billboardModel, entityTransform);
 156            Assert.IsTrue(entityTransform.forward == lookAt, "billboard entity forward vector should be the same as the 
 57
 158            billboardModel = new Billboard.Model()
 59            {
 60                x = false,
 61                y = false,
 62                z = false
 63            };
 64
 165            yield return TestHelpers.EntityComponentUpdate<Billboard, Billboard.Model>(billboard, billboardModel);
 66
 167            lookAt = GetLookAtVector(billboardModel, entityTransform);
 168            Assert.IsTrue(entityTransform.forward == lookAt, "billboard entity forward vector should be the same as the 
 69
 170            yield return null;
 171        }
 72
 73        [UnityTest]
 74        public IEnumerator CheckLookAtPlayerWhileTransformMoves()
 75        {
 176            DCLCharacterController.i.PauseGravity();
 77
 178            yield return CreateComponent(x: true, y: true, z: true);
 79
 180            yield return billboard.routine;
 81
 182            var entity = scene.entities[entityId];
 183            Transform entityTransform = entity.gameObject.transform;
 184            Vector3 lookAt = GetLookAtVector(billboard.GetModel(), entityTransform);
 85
 186            Assert.AreApproximatelyEqual(lookAt.x, entityTransform.forward.x, "billboard entity forward vector should be
 187            Assert.AreApproximatelyEqual(lookAt.y, entityTransform.forward.y, "billboard entity forward vector should be
 188            Assert.AreApproximatelyEqual(lookAt.z, entityTransform.forward.z, "billboard entity forward vector should be
 89
 190            var billboardModel = new Billboard.Model();
 91
 192            yield return TestHelpers.EntityComponentUpdate<Billboard, Billboard.Model>(billboard, billboardModel);
 93
 194            lookAt = GetLookAtVector(billboardModel, entityTransform);
 195            Assert.IsTrue(entityTransform.forward == lookAt, "billboard entity forward vector should be the same as the 
 96
 97            // We simulate a "system" moving the billboard object
 198            Vector3 position = Vector3.one;
 1299            for (int i = 0; i < 5; i++)
 100            {
 5101                position.y += i;
 5102                entityTransform.position = position;
 5103                Assert.IsTrue(entityTransform.forward == lookAt, "billboard entity forward vector should be the same as 
 104            }
 105
 1106            yield return null;
 1107        }
 108
 109        IEnumerator CreateComponent(bool x, bool y, bool z)
 110        {
 3111            var entity = TestHelpers.CreateSceneEntity(scene, entityId);
 3112            TestHelpers.SetEntityTransform(scene, entity, Vector3.one, Quaternion.identity, Vector3.one);
 3113            yield return null;
 114
 3115            var billboardModel = new Billboard.Model()
 116            {
 117                x = x,
 118                y = y,
 119                z = z
 120            };
 121
 3122            billboard =
 123                TestHelpers.EntityComponentCreate<Billboard, Billboard.Model>(scene, scene.entities[entityId],
 124                    billboardModel);
 125
 3126            Assert.IsTrue(billboard != null, "Component creation fail!");
 127
 3128            yield return billboard.routine;
 3129        }
 130
 131        Vector3 GetLookAtVector(Billboard.Model model, Transform entityTransform)
 132        {
 5133            Vector3 lookAtDir = CommonScriptableObjects.cameraPosition - entityTransform.position;
 134
 135            // Note (Zak): This check is here to avoid normalizing twice if not needed
 5136            if (!(model.x && model.y && model.z))
 137            {
 2138                lookAtDir.Normalize();
 139
 140                // Note (Zak): Model x,y,z are axis that we want to enable/disable
 141                // while lookAtDir x,y,z are the components of the look-at vector
 2142                if (!model.x || model.z)
 2143                    lookAtDir.y = entityTransform.forward.y;
 2144                if (!model.y)
 2145                    lookAtDir.x = entityTransform.forward.x;
 146            }
 147
 5148            return lookAtDir.normalized;
 149        }
 150    }
 151}