< Summary

Class:Tests.CharacterControllerTests
Assembly:CharacterControllerTests
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/CharacterController/Tests/CharacterControllerTests.cs
Covered lines:21
Uncovered lines:111
Coverable lines:132
Total lines:338
Line coverage:15.9% (21 of 132)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
CharacterTeleportReposition()0%330100%
InitCharacterPosition()0%330100%
InitCharacterPosition()0%4.054085.71%
WaitUntilGrounded()0%30500%
CharacterAdjustPosition()0%550100%
CharacterIsNotParentedOnWorldReposition()0%20400%
Character_UpdateSOPosition()0%330100%
Character_UpdateSORotation()0%12300%
CharacterSupportsMovingPlatforms()0%1101000%
CharacterSupportsRotatingPlatforms()0%1321100%
CharacterIsReleasedOnEntityRemoval()0%42600%
CharacterIsReleasedOnPlatformCollisionToggle()0%42600%
CharacterIsReleasedOnShapeRemoval()0%30500%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/CharacterController/Tests/CharacterControllerTests.cs

#LineLine coverage
 1using DCL.Components;
 2using DCL.Configuration;
 3using DCL.Helpers;
 4using Newtonsoft.Json;
 5using NUnit.Framework;
 6using System.Collections;
 7using UnityEngine;
 8using UnityEngine.TestTools;
 9
 10namespace Tests
 11{
 12    public class CharacterControllerTests : IntegrationTestSuite_Legacy
 13    {
 14        [UnityTest]
 215        public IEnumerator CharacterTeleportReposition() { yield return InitCharacterPosition(10, 2, 10); }
 16
 417        public IEnumerator InitCharacterPosition(float x, float y, float z, bool pauseGravity = true) { yield return Ini
 18
 19        public IEnumerator InitCharacterPosition(Vector3 position, bool pauseGravity = true)
 20        {
 521            if (pauseGravity)
 522                DCLCharacterController.i.PauseGravity();
 23            else
 024                DCLCharacterController.i.ResumeGravity();
 25
 526            DCLCharacterController.i.Teleport(JsonUtility.ToJson(position));
 27
 528            UnityEngine.Assertions.Assert.AreApproximatelyEqual(0, Vector3.Distance(DCLCharacterController.i.characterPo
 29
 530            yield return null;
 531        }
 32
 33        public IEnumerator WaitUntilGrounded()
 34        {
 35            // Let the character *fall* onto the ground shape
 036            yield return new WaitUntil(() => DCLCharacterController.i.isGrounded);
 037            yield return null;
 038        }
 39
 40        [UnityTest]
 41        public IEnumerator CharacterAdjustPosition()
 42        {
 143            UnityEngine.Assertions.Assert.AreApproximatelyEqual(0,
 44                Vector3.Distance(Vector3.zero,
 45                    CommonScriptableObjects.worldOffset), 0.05f);
 46
 147            Vector3 originalCharacterPosition = new Vector3
 48            {
 49                x = 50f,
 50                y = 2f,
 51                z = 0f
 52            };
 53
 154            yield return InitCharacterPosition(originalCharacterPosition, true);
 55
 156            var pos2 = new Vector3
 57            {
 58                x = 50f + PlayerSettings.WORLD_REPOSITION_MINIMUM_DISTANCE,
 59                y = 2f,
 60                z = 50f + PlayerSettings.WORLD_REPOSITION_MINIMUM_DISTANCE
 61            };
 62
 163            yield return InitCharacterPosition(pos2, true);
 164            UnityEngine.Assertions.Assert.AreApproximatelyEqual(0, Vector3.Distance(new Vector3(50f, 2f, 50f), DCLCharac
 65
 166            var pos3 = new Vector3
 67            {
 68                x = -50f - PlayerSettings.WORLD_REPOSITION_MINIMUM_DISTANCE,
 69                y = 2f,
 70                z = -50f - PlayerSettings.WORLD_REPOSITION_MINIMUM_DISTANCE
 71            };
 72
 173            yield return InitCharacterPosition(pos3, true);
 174            Assert.AreEqual(new Vector3(-50f, 2f, -50f), DCLCharacterController.i.transform.position);
 175        }
 76
 77        [UnityTest]
 78        [Explicit]
 79        [Category("Explicit")]
 80        public IEnumerator CharacterIsNotParentedOnWorldReposition()
 81        {
 82            // We use a shape that represents a static ground and has collisions
 083            TestHelpers.InstantiateEntityWithShape(scene, "groundShape", DCL.Models.CLASS_ID.PLANE_SHAPE, Vector3.zero);
 084            var shapeEntity = scene.entities["groundShape"];
 85
 86            // Reposition ground shape to be on the world-reposition-limit
 087            TestHelpers.SetEntityTransform(scene, shapeEntity,
 88                new DCLTransform.Model
 89                {
 90                    position = new Vector3(PlayerSettings.WORLD_REPOSITION_MINIMUM_DISTANCE, 1f, PlayerSettings.WORLD_RE
 91                    rotation = Quaternion.Euler(90f, 0f, 0f),
 92                    scale = new Vector3(20, 20, 1)
 93                });
 94
 95            // Place character on the ground shape and check if it's detected as ground
 096            DCLCharacterController.i.Teleport(JsonConvert.SerializeObject(new
 97            {
 98                x = PlayerSettings.WORLD_REPOSITION_MINIMUM_DISTANCE - 2f,
 99                y = 3f,
 100                z = PlayerSettings.WORLD_REPOSITION_MINIMUM_DISTANCE - 2f
 101            }));
 102
 0103            yield return WaitUntilGrounded();
 104
 0105            Assert.IsTrue(DCLCharacterController.i.groundTransform == shapeEntity.meshRootGameObject.transform);
 106
 107            // Place the character barely passing the limits to trigger the world repositioning
 0108            DCLCharacterController.i.Teleport(JsonConvert.SerializeObject(new
 109            {
 110                x = PlayerSettings.WORLD_REPOSITION_MINIMUM_DISTANCE + 1f,
 111                y = DCLCharacterController.i.transform.position.y,
 112                z = PlayerSettings.WORLD_REPOSITION_MINIMUM_DISTANCE + 1f
 113            }));
 114
 0115            yield return null;
 116
 117            // check if the character got repositioned correctly
 0118            Assert.AreEqual(new Vector3(1f, DCLCharacterController.i.transform.position.y, 1f), DCLCharacterController.i
 119
 120            // check it's not parented but still has the same ground
 0121            Assert.IsTrue(DCLCharacterController.i.groundTransform == shapeEntity.meshRootGameObject.transform);
 0122            Assert.IsTrue(DCLCharacterController.i.transform.parent == null);
 0123        }
 124
 125        [UnityTest]
 126        public IEnumerator Character_UpdateSOPosition()
 127        {
 1128            yield return InitCharacterPosition(50, 2, 0);
 1129            Assert.AreEqual(new Vector3(50f, 2f, 0f), CommonScriptableObjects.playerUnityPosition.Get());
 1130        }
 131
 132        [UnityTest]
 133        [Explicit]
 134        [Category("Explicit")]
 135        public IEnumerator Character_UpdateSORotation()
 136        {
 0137            DCLCharacterController.i.PauseGravity();
 138
 0139            var newEulerAngle = 10f;
 0140            CommonScriptableObjects.characterForward.Set(Quaternion.Euler(new Vector3(0, newEulerAngle, 0)) * Vector3.fo
 0141            Cursor.lockState = CursorLockMode.Locked;
 0142            yield return new WaitForSeconds(0.1f);
 143
 0144            Assert.AreEqual(DCLCharacterController.i.transform.eulerAngles, CommonScriptableObjects.playerUnityEulerAngl
 0145            DCLCharacterController.i.ResumeGravity();
 0146        }
 147
 148        [UnityTest]
 149        [NUnit.Framework.Explicit("This test started failing on the CI out of the blue. Will be re-enabled after impleme
 150        [Category("Explicit")]
 151        public IEnumerator CharacterSupportsMovingPlatforms()
 152        {
 0153            Vector3 originalCharacterPosition = new Vector3
 154            {
 155                x = 2f,
 156                y = 3f,
 157                z = 8f
 158            };
 159
 0160            yield return InitCharacterPosition(originalCharacterPosition);
 161
 0162            string platformEntityId = "movingPlatform";
 0163            TestHelpers.InstantiateEntityWithShape(scene, platformEntityId, DCL.Models.CLASS_ID.BOX_SHAPE, new Vector3(2
 164
 0165            Transform platformTransform = scene.entities[platformEntityId].gameObject.transform;
 0166            platformTransform.localScale = new Vector3(2f, 0.5f, 2f);
 167
 0168            yield return null;
 0169            Assert.IsTrue(Vector3.Distance(platformTransform.position, new Vector3(2f, 1f, 8f)) < 0.1f);
 170
 171            // enable character gravity
 0172            DCLCharacterController.i.ResumeGravity();
 173
 0174            yield return WaitUntilGrounded();
 175
 0176            Assert.IsFalse(DCLCharacterController.i.isOnMovingPlatform, "isOnMovingPlatform should be true only if the p
 177
 178            // Lerp the platform's position
 0179            float lerpTime = 0f;
 0180            float lerpSpeed = 2f;
 0181            Vector3 originalPosition = platformTransform.position;
 0182            Vector3 targetPosition = new Vector3(10f, 1f, 8f);
 183
 0184            bool checkedParent = false;
 185
 0186            while (lerpTime < 1f)
 187            {
 0188                yield return null;
 0189                lerpTime += Time.deltaTime * lerpSpeed;
 190
 0191                if (lerpTime > 1f)
 0192                    lerpTime = 1f;
 193
 0194                platformTransform.position = Vector3.Lerp(originalPosition, targetPosition, lerpTime);
 195
 0196                DCLCharacterController.i.LateUpdate();
 197
 0198                if (!checkedParent && lerpTime >= 0.25f)
 199                {
 0200                    Assert.IsTrue(DCLCharacterController.i.isOnMovingPlatform, "isOnMovingPlatform should be true when t
 0201                    checkedParent = true;
 202                }
 203            }
 204
 205            // check positions
 0206            Assert.IsTrue(Vector3.Distance(platformTransform.position, targetPosition) < 0.1f);
 207
 0208            float dist1 = Vector3.Distance(originalCharacterPosition, DCLCharacterController.i.transform.position);
 0209            float dist2 = Vector3.Distance(originalPosition, targetPosition);
 210
 0211            UnityEngine.Assertions.Assert.AreApproximatelyEqual(dist1, dist2, 1f);
 0212        }
 213
 214        [UnityTest]
 215        [NUnit.Framework.Explicit("This test started failing on the CI out of the blue. Will be re-enabled after impleme
 216        [Category("Explicit")]
 217        public IEnumerator CharacterSupportsRotatingPlatforms()
 218        {
 0219            Vector3 originalCharacterPosition = new Vector3
 220            {
 221                x = 5f,
 222                y = 3f,
 223                z = 5f
 224            };
 225
 0226            yield return InitCharacterPosition(originalCharacterPosition);
 227
 0228            string platformEntityId = "rotatingPlatform";
 0229            TestHelpers.InstantiateEntityWithShape(scene, platformEntityId, DCL.Models.CLASS_ID.BOX_SHAPE, new Vector3(8
 230
 0231            Transform platformTransform = scene.entities[platformEntityId].gameObject.transform;
 0232            platformTransform.localScale = new Vector3(8f, 0.5f, 8f);
 233
 0234            yield return null;
 0235            Assert.IsTrue(Vector3.Distance(platformTransform.position, new Vector3(8f, 1f, 8f)) < 0.1f);
 236
 237            // enable character gravity
 0238            DCLCharacterController.i.ResumeGravity();
 239
 0240            yield return WaitUntilGrounded();
 241
 0242            Assert.IsFalse(DCLCharacterController.i.isOnMovingPlatform, "isOnMovingPlatform should be true only if the p
 243
 244            // Lerp the platform's rotation
 0245            float lerpTime = 0f;
 0246            float lerpSpeed = 1f;
 0247            Quaternion initialRotation = Quaternion.identity;
 0248            Quaternion targetRotation = Quaternion.Euler(0, 180f, 0f);
 249
 0250            bool checkedParent = false;
 0251            while (lerpTime < 1f)
 252            {
 0253                yield return null;
 0254                lerpTime += Time.deltaTime * lerpSpeed;
 255
 0256                if (lerpTime > 1f)
 0257                    lerpTime = 1f;
 258
 0259                platformTransform.rotation = Quaternion.Lerp(initialRotation, targetRotation, lerpTime);
 260
 0261                if (!checkedParent && lerpTime >= 0.5f)
 262                {
 0263                    Assert.IsTrue(DCLCharacterController.i.isOnMovingPlatform, "isOnMovingPlatform should be true when t
 264
 0265                    checkedParent = true;
 266                }
 267            }
 268
 269            // check positions
 0270            Assert.IsTrue(Vector3.Distance(platformTransform.rotation.eulerAngles, targetRotation.eulerAngles) < 0.1f);
 271
 0272            UnityEngine.Assertions.Assert.AreApproximatelyEqual(DCLCharacterController.i.transform.position.x, 11f, 1f);
 0273            UnityEngine.Assertions.Assert.AreApproximatelyEqual(DCLCharacterController.i.transform.position.z, 11f, 1f);
 274
 275            // remove platform and check character parent
 0276            TestHelpers.RemoveSceneEntity(scene, platformEntityId);
 0277            yield return null;
 278
 0279            Assert.IsFalse(DCLCharacterController.i.isOnMovingPlatform, "isOnMovingPlatform should be false as there's n
 0280        }
 281
 282        [UnityTest]
 283        [NUnit.Framework.Explicit("This test started failing on the CI out of the blue. Will be re-enabled after impleme
 284        [Category("Explicit")]
 285        public IEnumerator CharacterIsReleasedOnEntityRemoval()
 286        {
 0287            yield return CharacterSupportsMovingPlatforms();
 288
 289            // remove platform and check character parent
 0290            string platformEntityId = "movingPlatform";
 0291            TestHelpers.RemoveSceneEntity(scene, platformEntityId);
 0292            yield return null;
 0293            yield return null;
 0294            yield return null;
 295
 0296            Assert.IsNull(DCLCharacterController.i.transform.parent, "The character shouldn't be parented as there's no 
 0297        }
 298
 299        [UnityTest]
 300        [NUnit.Framework.Explicit("This test started failing on the CI out of the blue. Will be re-enabled after impleme
 301        [Category("Explicit")]
 302        public IEnumerator CharacterIsReleasedOnPlatformCollisionToggle()
 303        {
 0304            yield return CharacterSupportsMovingPlatforms();
 305
 306            // Disable shape colliders
 0307            string platformEntityId = "movingPlatform";
 0308            var shapeComponent = scene.entities[platformEntityId].GetSharedComponent(typeof(BaseShape));
 0309            yield return TestHelpers.SharedComponentUpdate(shapeComponent, new BaseShape.Model()
 310            {
 311                withCollisions = false
 312            });
 313
 0314            yield return null;
 0315            yield return null;
 316
 0317            Assert.IsNull(DCLCharacterController.i.transform.parent, "The character shouldn't be parented as the shape c
 0318        }
 319
 320        [UnityTest]
 321        [NUnit.Framework.Explicit("This test started failing on the CI out of the blue. Will be re-enabled after impleme
 322        [Category("Explicit")]
 323        public IEnumerator CharacterIsReleasedOnShapeRemoval()
 324        {
 0325            yield return CharacterSupportsMovingPlatforms();
 326
 327            // remove shape component
 0328            string platformEntityId = "movingPlatform";
 0329            var shapeComponent = scene.entities[platformEntityId].GetSharedComponent(typeof(BaseShape));
 0330            TestHelpers.DetachSharedComponent(scene, platformEntityId, shapeComponent.id);
 331
 0332            yield return null;
 0333            yield return null;
 334
 0335            Assert.IsFalse(DCLCharacterController.i.isOnMovingPlatform, "isOnMovingPlatform should be false when the sha
 0336        }
 337    }
 338}