< Summary

Class:Tests.AnimatorTests
Assembly:AnimatorTests
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Animator/Tests/AnimatorTests.cs
Covered lines:96
Uncovered lines:20
Coverable lines:116
Total lines:372
Line coverage:82.7% (96 of 116)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
CreateAnimationComponent()0%440100%
DCLAnimatorResetAnimation()0%11110100%
DCLAnimatorResetAllAnimations()0%10100100%
AnimationComponentMissingValuesGetDefaultedOnUpdate()0%330100%
UpdateAnimationComponent()0%550100%
AnimationStartsAutomaticallyWithNoDCLAnimator()0%20400%
NonSkeletalAnimationsSupport()0%550100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Animator/Tests/AnimatorTests.cs

#LineLine coverage
 1using DCL.Components;
 2using DCL.Helpers;
 3using DCL.Models;
 4using Newtonsoft.Json;
 5using NUnit.Framework;
 6using System.Collections;
 7using System.Linq;
 8using UnityEngine;
 9using UnityEngine.TestTools;
 10
 11namespace Tests
 12{
 13    public class AnimatorTests : IntegrationTestSuite_Legacy
 14    {
 15        [UnityTest]
 16        public IEnumerator CreateAnimationComponent()
 17        {
 118            var entity = TestHelpers.CreateSceneEntity(scene);
 19
 120            Assert.IsTrue(entity.gameObject.GetComponentInChildren<UnityGLTF.InstantiatedGLTFObject>() == null,
 21                "Since the shape hasn't been updated yet, the 'GLTFScene' child object shouldn't exist");
 22
 123            TestHelpers.CreateAndSetShape(scene, entity.entityId, DCL.Models.CLASS_ID.GLTF_SHAPE,
 24                JsonConvert.SerializeObject(new
 25                {
 26                    src = TestAssetsUtils.GetPath() + "/GLB/CesiumMan/CesiumMan.glb"
 27                }));
 28
 129            DCLAnimator.Model animatorModel = new DCLAnimator.Model
 30            {
 31                states = new DCLAnimator.Model.DCLAnimationState[]
 32                {
 33                    new DCLAnimator.Model.DCLAnimationState
 34                    {
 35                        name = "clip01",
 36                        clip = "animation:0",
 37                        playing = true,
 38                        weight = 1,
 39                        speed = 1
 40                    }
 41                }
 42            };
 43
 144            DCLAnimator animator =
 45                TestHelpers.EntityComponentCreate<DCLAnimator, DCLAnimator.Model>(scene, entity, animatorModel);
 46
 147            LoadWrapper gltfShape = GLTFShape.GetLoaderForEntity(entity);
 948            yield return new WaitUntil(() => gltfShape.alreadyLoaded == true);
 49
 150            Assert.IsTrue(entity.gameObject.GetComponentInChildren<Animation>() != null,
 51                "'GLTFScene' child object with 'Animator' component should exist if the GLTF was loaded correctly.");
 152            Assert.IsTrue(entity.gameObject.GetComponentInChildren<DCLAnimator>() != null,
 53                "'GLTFScene' child object with 'DCLAnimator' component should exist if the GLTF was loaded correctly.");
 54
 155            yield return animator.routine;
 56
 157            animator = entity.gameObject.GetComponentInChildren<DCLAnimator>();
 58
 159            Assert.IsTrue(animator.GetStateByString("clip01") != null, "dclAnimator.GetStateByString fail!");
 160            Assert.IsTrue(animator.GetModel().states[0].clip != null, "dclAnimator clipReference is null!");
 161        }
 62
 63        [UnityTest]
 64        public IEnumerator DCLAnimatorResetAnimation()
 65        {
 166            GLTFShape gltfShape = TestHelpers.CreateEntityWithGLTFShape(scene, Vector3.zero,
 67                new LoadableShape.Model
 68                {
 69                    src = TestAssetsUtils.GetPath() + "/GLB/Shark/shark_anim.gltf"
 70                });
 171            var entity = gltfShape.attachedEntities.First();
 72
 173            DCLAnimator.Model animatorModel = new DCLAnimator.Model
 74            {
 75                states = new[]
 76                {
 77                    new DCLAnimator.Model.DCLAnimationState
 78                    {
 79                        name = "Bite",
 80                        clip = "shark_skeleton_bite",
 81                        playing = true,
 82                        weight = 1,
 83                        speed = 1
 84                    },
 85                    new DCLAnimator.Model.DCLAnimationState
 86                    {
 87                        name = "Swim",
 88                        clip = "shark_skeleton_swim",
 89                        playing = true,
 90                        weight = 1,
 91                        speed = 1
 92                    }
 93                }
 94            };
 95
 196            DCLAnimator animator =
 97                TestHelpers.EntityComponentCreate<DCLAnimator, DCLAnimator.Model>(scene, entity, animatorModel);
 198            LoadWrapper gltfLoader = GLTFShape.GetLoaderForEntity(entity);
 999            yield return new WaitUntil(() => gltfLoader.alreadyLoaded);
 100
 1101            yield return animator.routine;
 102
 1103            animator.animComponent.cullingType = AnimationCullingType.AlwaysAnimate;
 104
 1105            yield return null;
 106
 1107            Animation animation = entity.gameObject.GetComponentInChildren<Animation>();
 6108            foreach (AnimationState animState in animation)
 109            {
 2110                Assert.AreNotEqual(0f, animState.time);
 111            }
 112
 1113            animatorModel.states[1].shouldReset = true;
 114
 1115            yield return TestHelpers.EntityComponentUpdate(animator, animatorModel);
 116
 1117            animator.ResetAnimation(animator.GetStateByString("Swim"));
 6118            foreach (AnimationState animState in animation)
 119            {
 2120                if (animator.GetStateByString("Swim").clipReference.name == animState.clip.name)
 121                {
 1122                    Assert.AreEqual(0f, animState.time);
 1123                }
 124                else
 125                {
 1126                    Assert.AreNotEqual(0f, animState.time);
 127                }
 128            }
 1129        }
 130
 131        [UnityTest]
 132        public IEnumerator DCLAnimatorResetAllAnimations()
 133        {
 1134            var gltfShape = TestHelpers.CreateEntityWithGLTFShape(scene, Vector3.zero,
 135                new LoadableShape.Model
 136                {
 137                    src = TestAssetsUtils.GetPath() + "/GLB/Shark/shark_anim.gltf"
 138                });
 1139            var entity = gltfShape.attachedEntities.First();
 140
 1141            DCLAnimator.Model animatorModel = new DCLAnimator.Model
 142            {
 143                states = new[]
 144                {
 145                    new DCLAnimator.Model.DCLAnimationState
 146                    {
 147                        name = "Bite",
 148                        clip = "shark_skeleton_bite",
 149                        playing = true,
 150                        weight = 1,
 151                        speed = 1
 152                    },
 153                    new DCLAnimator.Model.DCLAnimationState
 154                    {
 155                        name = "Swim",
 156                        clip = "shark_skeleton_swim",
 157                        playing = true,
 158                        weight = 1,
 159                        speed = 1
 160                    }
 161                }
 162            };
 163
 1164            DCLAnimator animator =
 165                TestHelpers.EntityComponentCreate<DCLAnimator, DCLAnimator.Model>(scene, entity, animatorModel);
 1166            LoadWrapper gltfLoader = GLTFShape.GetLoaderForEntity(entity);
 9167            yield return new WaitUntil(() => gltfLoader.alreadyLoaded);
 168
 1169            yield return animator.routine;
 170
 1171            animator.animComponent.cullingType = AnimationCullingType.AlwaysAnimate;
 172
 1173            yield return null;
 174
 1175            Animation animation = entity.gameObject.GetComponentInChildren<Animation>();
 176
 6177            foreach (AnimationState animState in animation)
 178            {
 2179                Assert.AreNotEqual(0f, animState.time);
 180            }
 181
 1182            animatorModel.states[0].shouldReset = true;
 1183            animatorModel.states[1].shouldReset = true;
 184
 1185            yield return TestHelpers.EntityComponentUpdate(animator, animatorModel);
 186
 6187            foreach (AnimationState animState in animation)
 188            {
 2189                Assert.AreEqual(0f, animState.time);
 190            }
 1191        }
 192
 193        [UnityTest]
 2194        public IEnumerator AnimationComponentMissingValuesGetDefaultedOnUpdate() { yield return TestHelpers.TestEntityCo
 195
 196        [UnityTest]
 197        public IEnumerator UpdateAnimationComponent()
 198        {
 1199            var entity = TestHelpers.CreateSceneEntity(scene);
 200
 1201            Assert.IsTrue(entity.gameObject.GetComponentInChildren<UnityGLTF.InstantiatedGLTFObject>() == null,
 202                "Since the shape hasn't been updated yet, the 'GLTFScene' child object shouldn't exist");
 203
 1204            TestHelpers.CreateAndSetShape(scene, entity.entityId, DCL.Models.CLASS_ID.GLTF_SHAPE,
 205                JsonConvert.SerializeObject(new
 206                {
 207                    src = TestAssetsUtils.GetPath() + "/GLB/CesiumMan/CesiumMan.glb"
 208                }));
 209
 1210            string clipName = "animation:0";
 1211            DCLAnimator.Model animatorModel = new DCLAnimator.Model
 212            {
 213                states = new DCLAnimator.Model.DCLAnimationState[]
 214                {
 215                    new DCLAnimator.Model.DCLAnimationState
 216                    {
 217                        name = "clip01",
 218                        clip = clipName,
 219                        playing = true,
 220                        weight = 1,
 221                        speed = 1,
 222                        looping = false
 223                    }
 224                }
 225            };
 226
 1227            DCLAnimator animator = TestHelpers.EntityComponentCreate<DCLAnimator, DCLAnimator.Model>(scene, entity, anim
 228
 1229            LoadWrapper gltfShape = GLTFShape.GetLoaderForEntity(entity);
 8230            yield return new WaitUntil(() => gltfShape.alreadyLoaded == true);
 231
 1232            Assert.IsTrue(animator.animComponent.isPlaying);
 1233            Assert.AreEqual(animator.animComponent.clip.name, clipName);
 1234            Assert.IsFalse(animator.animComponent.clip.wrapMode == WrapMode.Loop);
 235
 1236            yield return null;
 237
 238            // update component properties
 1239            animatorModel.states[0].playing = false;
 1240            animatorModel.states[0].looping = true;
 1241            yield return TestHelpers.EntityComponentUpdate(animator, animatorModel);
 242
 1243            Assert.IsFalse(animator.animComponent.isPlaying);
 1244            Assert.IsTrue(animator.animComponent.clip.wrapMode == WrapMode.Loop);
 1245        }
 246
 247        [UnityTest]
 248        [Explicit]
 249        [Category("Explicit")]
 250        public IEnumerator AnimationStartsAutomaticallyWithNoDCLAnimator()
 251        {
 252            // GLTFShape without DCLAnimator
 0253            var entity = TestHelpers.CreateSceneEntity(scene);
 254
 0255            Assert.IsTrue(entity.gameObject.GetComponentInChildren<UnityGLTF.InstantiatedGLTFObject>() == null,
 256                "Since the shape hasn't been updated yet, the 'GLTFScene' child object shouldn't exist");
 257
 0258            TestHelpers.CreateAndSetShape(scene, entity.entityId, DCL.Models.CLASS_ID.GLTF_SHAPE,
 259                JsonConvert.SerializeObject(new
 260                {
 261                    src = TestAssetsUtils.GetPath() + "/GLB/CesiumMan/CesiumMan.glb"
 262                }));
 263
 0264            LoadWrapper gltfShape = GLTFShape.GetLoaderForEntity(entity);
 0265            yield return new WaitUntil(() => gltfShape.alreadyLoaded == true);
 266
 0267            Animation animation = entity.meshRootGameObject.GetComponentInChildren<Animation>();
 268
 0269            Assert.IsTrue(animation != null);
 0270            Assert.IsTrue(animation.isPlaying);
 271
 272            // GLTFShape with DCLAnimator
 0273            var entity2 = TestHelpers.CreateSceneEntity(scene);
 274
 0275            Assert.IsTrue(entity2.gameObject.GetComponentInChildren<UnityGLTF.InstantiatedGLTFObject>() == null,
 276                "Since the shape hasn't been updated yet, the 'GLTFScene' child object shouldn't exist");
 277
 0278            TestHelpers.CreateAndSetShape(scene, entity2.entityId, DCL.Models.CLASS_ID.GLTF_SHAPE,
 279                JsonConvert.SerializeObject(new
 280                {
 281                    src = TestAssetsUtils.GetPath() + "/GLB/CesiumMan/CesiumMan.glb"
 282                }));
 283
 0284            string clipName = "animation:0";
 0285            DCLAnimator.Model animatorModel = new DCLAnimator.Model
 286            {
 287                states = new DCLAnimator.Model.DCLAnimationState[]
 288                {
 289                    new DCLAnimator.Model.DCLAnimationState
 290                    {
 291                        name = "clip01",
 292                        clip = clipName,
 293                        playing = false,
 294                        weight = 1,
 295                        speed = 1,
 296                        looping = false
 297                    }
 298                }
 299            };
 300
 0301            DCLAnimator animator = TestHelpers.EntityComponentCreate<DCLAnimator, DCLAnimator.Model>(scene, entity, anim
 302
 0303            LoadWrapper gltfShape2 = GLTFShape.GetLoaderForEntity(entity);
 0304            yield return new WaitUntil(() => gltfShape2.alreadyLoaded == true);
 305
 0306            Assert.IsTrue(animator.animComponent != null);
 0307            Assert.AreEqual(animator.animComponent.clip.name, clipName);
 0308            Assert.IsFalse(animator.animComponent.isPlaying);
 0309        }
 310
 311        [UnityTest]
 312        public IEnumerator NonSkeletalAnimationsSupport()
 313        {
 1314            var entity = TestHelpers.CreateSceneEntity(scene);
 315
 1316            TestHelpers.SetEntityTransform(scene, entity, new Vector3(8, 2, 8), Quaternion.identity, Vector3.one);
 317
 1318            Assert.IsTrue(entity.gameObject.GetComponentInChildren<UnityGLTF.InstantiatedGLTFObject>() == null,
 319                "Since the shape hasn't been updated yet, the 'GLTFScene' child object shouldn't exist");
 320
 1321            TestHelpers.CreateAndSetShape(scene, entity.entityId, DCL.Models.CLASS_ID.GLTF_SHAPE,
 322                JsonConvert.SerializeObject(new
 323                {
 324                    src = TestAssetsUtils.GetPath() + "/GLB/non-skeletal-3-transformations.glb"
 325                }));
 326
 1327            string clipName = "All";
 1328            DCLAnimator.Model animatorModel = new DCLAnimator.Model
 329            {
 330                states = new DCLAnimator.Model.DCLAnimationState[]
 331                {
 332                    new DCLAnimator.Model.DCLAnimationState
 333                    {
 334                        name = "clip01",
 335                        clip = clipName,
 336                        playing = false,
 337                        weight = 1,
 338                        speed = 1,
 339                        looping = false
 340                    }
 341                }
 342            };
 343
 1344            DCLAnimator animator = TestHelpers.EntityComponentCreate<DCLAnimator, DCLAnimator.Model>(scene, entity, anim
 345
 1346            LoadWrapper gltfShape = GLTFShape.GetLoaderForEntity(entity);
 5347            yield return new WaitUntil(() => gltfShape.alreadyLoaded == true);
 348
 1349            animator.animComponent.cullingType = AnimationCullingType.AlwaysAnimate;
 350
 1351            Assert.IsTrue(!animator.animComponent.isPlaying);
 1352            Assert.AreEqual(animator.animComponent.clip.name, clipName);
 1353            Assert.IsFalse(animator.animComponent.clip.wrapMode == WrapMode.Loop);
 354
 1355            Transform animatedGameObject = animator.animComponent.transform.GetChild(0);
 356
 1357            Vector3 originalScale = animatedGameObject.transform.localScale;
 1358            Vector3 originalPos = animatedGameObject.transform.localPosition;
 1359            Quaternion originalRot = animatedGameObject.transform.localRotation;
 360
 361            // start animation
 1362            animatorModel.states[0].playing = true;
 1363            yield return TestHelpers.EntityComponentUpdate(animator, animatorModel);
 364
 1365            yield return new WaitForSeconds(0.1f);
 366
 1367            Assert.IsFalse(animatedGameObject.localScale == originalScale);
 1368            Assert.IsFalse(animatedGameObject.localPosition == originalPos);
 1369            Assert.IsFalse(animatedGameObject.localRotation == originalRot);
 1370        }
 371    }
 372}