< Summary

Class:GLTFShape_Tests
Assembly:LoadableShapesTests
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/LoadableShapes/Tests/GLTFShape_Tests.cs
Covered lines:96
Uncovered lines:32
Coverable lines:128
Total lines:303
Line coverage:75% (96 of 128)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ShapeUpdate()0%330100%
CustomContentProvider()0%22095.83%
PreExistentShapeUpdate()0%440100%
PreExistentShapeImmediateUpdate()0%440100%
AttachedGetsReplacedOnNewAttachment()0%440100%
CollisionProperty()0%42600%
VisibleProperty()0%42600%
OnReadyBeforeLoading()0%110100%
OnReadyWaitLoading()0%330100%
OnReadyWithoutAttachInstantlyCalled()0%2100%
OnReadyAfterLoadingInstantlyCalled()0%330100%
OnDestroyWhileLoading()0%660100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/LoadableShapes/Tests/GLTFShape_Tests.cs

#LineLine coverage
 1using DCL;
 2using DCL.Components;
 3using DCL.Helpers;
 4using DCL.Models;
 5using Newtonsoft.Json;
 6using NUnit.Framework;
 7using System.Collections;
 8using UnityEngine;
 9using UnityEngine.TestTools;
 10using UnityGLTF;
 11
 12public class GLTFShape_Tests : IntegrationTestSuite_Legacy
 13{
 14    [UnityTest]
 15    public IEnumerator ShapeUpdate()
 16    {
 117        string entityId = "1";
 118        TestHelpers.CreateSceneEntity(scene, entityId);
 19
 120        Assert.IsTrue(
 21            scene.entities[entityId].gameObject.GetComponentInChildren<UnityGLTF.InstantiatedGLTFObject>() == null,
 22            "Since the shape hasn't been updated yet, the 'GLTFScene' child object shouldn't exist");
 23
 124        TestHelpers.CreateAndSetShape(scene, entityId, DCL.Models.CLASS_ID.GLTF_SHAPE, JsonConvert.SerializeObject(
 25            new
 26            {
 27                src = TestAssetsUtils.GetPath() + "/GLB/Trunk/Trunk.glb"
 28            }));
 29
 130        LoadWrapper gltfShape = GLTFShape.GetLoaderForEntity(scene.entities[entityId]);
 731        yield return new WaitUntil(() => gltfShape.alreadyLoaded);
 32
 133        Assert.IsTrue(
 34            scene.entities[entityId].gameObject.GetComponentInChildren<UnityGLTF.InstantiatedGLTFObject>() != null,
 35            "'GLTFScene' child object with 'InstantiatedGLTF' component should exist if the GLTF was loaded correctly");
 136    }
 37
 38    [Test]
 39    public void CustomContentProvider()
 40    {
 141        string entityId = "1";
 142        TestHelpers.CreateSceneEntity(scene, entityId);
 43
 144        string mockupAssetId = "cdd5a4ea94388dd21babdecd26dd560f739dce2fbb8c99cc10a45bb8306b6076";
 145        string mockupKey = "key";
 146        string mockupValue = "Value";
 47
 148        SceneAssetPack sceneAssetPack = new SceneAssetPack();
 149        sceneAssetPack.assets = new System.Collections.Generic.List<SceneObject>();
 150        sceneAssetPack.id = "mockupId";
 51
 152        SceneObject sceneObject = new SceneObject();
 153        sceneObject.id = mockupAssetId;
 154        sceneObject.contents = new System.Collections.Generic.Dictionary<string, string>();
 155        sceneObject.contents.Add(mockupKey, mockupValue);
 56
 157        sceneAssetPack.assets.Add(sceneObject);
 58
 159        AssetCatalogBridge.i.AddSceneAssetPackToCatalog(sceneAssetPack);
 60
 161        TestHelpers.CreateAndSetShape(scene, entityId, DCL.Models.CLASS_ID.GLTF_SHAPE, JsonConvert.SerializeObject(
 62            new
 63            {
 64                assetId = mockupAssetId,
 65                src = TestAssetsUtils.GetPath() + "/GLB/Lantern/Lantern.glb"
 66            }));
 67
 68
 169        LoadWrapper gltfShape = GLTFShape.GetLoaderForEntity(scene.entities[entityId]);
 70
 171        if (!(gltfShape is LoadWrapper_GLTF))
 072            Assert.Fail();
 73
 74
 175        LoadWrapper_GLTF gltfWrapper = (LoadWrapper_GLTF) gltfShape;
 176        ContentProvider customContentProvider = AssetCatalogBridge.i.GetContentProviderForAssetIdInSceneObjectCatalog(mo
 177        Assert.AreEqual(customContentProvider.baseUrl, gltfWrapper.customContentProvider.baseUrl);
 178        Assert.AreEqual(mockupKey, gltfWrapper.customContentProvider.contents[0].file);
 179        Assert.AreEqual(mockupValue, gltfWrapper.customContentProvider.contents[0].hash);
 180    }
 81
 82    [UnityTest]
 83    public IEnumerator PreExistentShapeUpdate()
 84    {
 185        string entityId = "1";
 186        TestHelpers.CreateSceneEntity(scene, entityId);
 87
 188        var componentId = TestHelpers.CreateAndSetShape(scene, entityId, DCL.Models.CLASS_ID.GLTF_SHAPE, JsonConvert.Ser
 89            new
 90            {
 91                src = TestAssetsUtils.GetPath() + "/GLB/Trunk/Trunk.glb"
 92            }));
 93
 194        LoadWrapper gltfShape = GLTFShape.GetLoaderForEntity(scene.entities[entityId]);
 795        yield return new WaitUntil(() => gltfShape.alreadyLoaded);
 96
 97        {
 198            var gltfObject = scene.entities[entityId].gameObject.GetComponentInChildren<InstantiatedGLTFObject>();
 99
 1100            Assert.IsTrue(gltfObject != null, "InstantiatedGLTFObject is null in first object!");
 1101            Assert.IsTrue(gltfObject.transform.Find("TreeStump_01") != null, "Can't find \"TreeStump_01!\"");
 102        }
 103
 1104        TestHelpers.UpdateShape(scene, componentId, JsonConvert.SerializeObject(
 105            new
 106            {
 107                src = TestAssetsUtils.GetPath() + "/GLB/PalmTree_01.glb"
 108            }));
 109
 1110        gltfShape = GLTFShape.GetLoaderForEntity(scene.entities[entityId]);
 7111        yield return new WaitUntil(() => gltfShape.alreadyLoaded);
 112
 113        {
 1114            var gltfObject = scene.entities[entityId].gameObject.GetComponentInChildren<InstantiatedGLTFObject>();
 115
 1116            Assert.IsTrue(gltfObject != null, "InstantiatedGLTFObject is null in second object!");
 1117            Assert.IsTrue(gltfObject.transform.Find("PalmTree_01") != null,
 118                "Can't find \"PalmTree_01\"!");
 119        }
 1120    }
 121
 122    [UnityTest]
 123    public IEnumerator PreExistentShapeImmediateUpdate()
 124    {
 1125        string entityId = "1";
 1126        TestHelpers.CreateSceneEntity(scene, entityId);
 127
 1128        var componentId = TestHelpers.CreateAndSetShape(scene, entityId, DCL.Models.CLASS_ID.GLTF_SHAPE, JsonConvert.Ser
 129            new
 130            {
 131                src = TestAssetsUtils.GetPath() + "/GLB/Trunk/Trunk.glb"
 132            }));
 133
 1134        LoadWrapper gltfShape = GLTFShape.GetLoaderForEntity(scene.entities[entityId]);
 7135        yield return new WaitUntil(() => gltfShape.alreadyLoaded);
 136
 1137        TestHelpers.UpdateShape(scene, componentId, JsonConvert.SerializeObject(
 138            new
 139            {
 140                src = TestAssetsUtils.GetPath() + "/GLB/PalmTree_01.glb"
 141            }));
 142
 1143        gltfShape = GLTFShape.GetLoaderForEntity(scene.entities[entityId]);
 7144        yield return new WaitUntil(() => gltfShape.alreadyLoaded);
 145
 1146        Assert.AreEqual(1,
 147            scene.entities[entityId].gameObject.GetComponentsInChildren<InstantiatedGLTFObject>().Length,
 148            "Only 1 'InstantiatedGLTFObject' should remain once the GLTF shape has been updated");
 1149    }
 150
 151    [UnityTest]
 152    public IEnumerator AttachedGetsReplacedOnNewAttachment()
 153    {
 1154        var entity = TestHelpers.CreateSceneEntity(scene);
 155
 156        // set first GLTF
 1157        string gltfId1 = TestHelpers.CreateAndSetShape(scene, entity.entityId, DCL.Models.CLASS_ID.GLTF_SHAPE,
 158            JsonConvert.SerializeObject(new
 159            {
 160                src = TestAssetsUtils.GetPath() + "/GLB/PalmTree_01.glb"
 161            }));
 1162        var gltf1 = scene.GetSharedComponent(gltfId1);
 163
 1164        LoadWrapper gltfLoader = GLTFShape.GetLoaderForEntity(entity);
 7165        yield return new WaitUntil(() => gltfLoader.alreadyLoaded);
 166
 1167        Assert.AreEqual(gltf1, entity.GetSharedComponent(typeof(BaseShape)));
 168
 169        // set second GLTF
 1170        string gltfId2 = TestHelpers.CreateAndSetShape(scene, entity.entityId, DCL.Models.CLASS_ID.GLTF_SHAPE,
 171            JsonConvert.SerializeObject(new
 172            {
 173                src = TestAssetsUtils.GetPath() + "/GLB/Trunk/Trunk.glb"
 174            }));
 175
 1176        gltfLoader = GLTFShape.GetLoaderForEntity(entity);
 7177        yield return new WaitUntil(() => gltfLoader.alreadyLoaded);
 178
 1179        Assert.AreEqual(scene.GetSharedComponent(gltfId2), entity.GetSharedComponent(typeof(BaseShape)));
 1180        Assert.IsFalse(gltf1.GetAttachedEntities().Contains(entity));
 1181    }
 182
 183    // NOTE: Since every deployed scene carries compiled core code of the sdk at the moment of deploying, most of them h
 184    // different than the new default value, that's why we are forcing the colliders to be ON on EVERY GLTF until that g
 185    [UnityTest]
 186    [Explicit]
 187    [Category("Explicit")]
 188    public IEnumerator CollisionProperty()
 189    {
 0190        string entityId = "entityId";
 0191        TestHelpers.CreateSceneEntity(scene, entityId);
 0192        var entity = scene.entities[entityId];
 0193        yield return null;
 194
 195        // Create shape component
 0196        var shapeModel = new LoadableShape<LoadWrapper_GLTF, LoadableShape.Model>.Model();
 0197        shapeModel.src = TestAssetsUtils.GetPath() + "/GLB/PalmTree_01.glb";
 198
 0199        var shapeComponent = TestHelpers.SharedComponentCreate<LoadableShape<LoadWrapper_GLTF, LoadableShape.Model>, Loa
 0200        yield return shapeComponent.routine;
 201
 0202        TestHelpers.SharedComponentAttach(shapeComponent, entity);
 203
 0204        var shapeLoader = GLTFShape.GetLoaderForEntity(entity);
 0205        yield return new WaitUntil(() => shapeLoader.alreadyLoaded);
 206
 0207        yield return TestHelpers.TestShapeCollision(shapeComponent, shapeModel, entity);
 0208    }
 209
 210    [UnityTest]
 211    [Explicit]
 212    [Category("Explicit")]
 213    public IEnumerator VisibleProperty()
 214    {
 0215        string entityId = "entityId";
 0216        TestHelpers.CreateSceneEntity(scene, entityId);
 0217        var entity = scene.entities[entityId];
 0218        yield return null;
 219
 220        // Create shape component
 0221        var shapeModel = new LoadableShape<LoadWrapper_GLTF, LoadableShape.Model>.Model();
 0222        shapeModel.src = TestAssetsUtils.GetPath() + "/GLB/PalmTree_01.glb";
 223
 0224        var shapeComponent = TestHelpers.SharedComponentCreate<LoadableShape<LoadWrapper_GLTF, LoadableShape.Model>, Loa
 0225        yield return shapeComponent.routine;
 226
 0227        TestHelpers.SharedComponentAttach(shapeComponent, entity);
 228
 0229        var shapeLoader = GLTFShape.GetLoaderForEntity(entity);
 0230        yield return new WaitUntil(() => shapeLoader.alreadyLoaded);
 231
 0232        yield return TestHelpers.TestShapeVisibility(shapeComponent, shapeModel, entity);
 0233    }
 234
 235    [Test]
 236    public void OnReadyBeforeLoading()
 237    {
 1238        GLTFShape gltfShape = TestHelpers.CreateEntityWithGLTFShape(scene, Vector3.zero, TestAssetsUtils.GetPath() + "/G
 239
 1240        bool isOnReady = false;
 1241        gltfShape.CallWhenReady((x) => { isOnReady = true; });
 242
 1243        Assert.IsFalse(isOnReady);
 1244    }
 245
 246    [UnityTest]
 247    public IEnumerator OnReadyWaitLoading()
 248    {
 1249        GLTFShape gltfShape = TestHelpers.CreateEntityWithGLTFShape(scene, Vector3.zero, TestAssetsUtils.GetPath() + "/G
 250
 1251        bool isOnReady = false;
 3252        gltfShape.CallWhenReady((x) => { isOnReady = true; });
 1253        yield return TestHelpers.WaitForGLTFLoad(entity);
 254
 1255        Assert.IsTrue(isOnReady);
 1256    }
 257
 258    [Test]
 259    [Explicit]
 260    [Category("Explicit")]
 261    public void OnReadyWithoutAttachInstantlyCalled()
 262    {
 0263        GLTFShape gltfShape = TestHelpers.SharedComponentCreate<GLTFShape, GLTFShape.Model>(scene, CLASS_ID.GLTF_SHAPE, 
 264        {
 265            src = TestAssetsUtils.GetPath() + "/GLB/Trevor/Trevor.glb"
 266        });
 267
 0268        bool isOnReady = false;
 0269        gltfShape.CallWhenReady((x) => { isOnReady = true; });
 270
 0271        Assert.IsTrue(isOnReady);
 0272    }
 273
 274    [UnityTest]
 275    public IEnumerator OnReadyAfterLoadingInstantlyCalled()
 276    {
 1277        GLTFShape gltfShape = TestHelpers.CreateEntityWithGLTFShape(scene, Vector3.zero, TestAssetsUtils.GetPath() + "/G
 1278        yield return TestHelpers.WaitForGLTFLoad(entity);
 279
 1280        bool isOnReady = false;
 3281        gltfShape.CallWhenReady((x) => { isOnReady = true; });
 1282        Assert.IsTrue(isOnReady);
 1283    }
 284
 285    [UnityTest]
 286    public IEnumerator OnDestroyWhileLoading()
 287    {
 1288        GLTFShape gltfShape = TestHelpers.CreateEntityWithGLTFShape(scene, Vector3.zero, TestAssetsUtils.GetPath() + "/G
 1289        GLTFShape gltfShape2 = TestHelpers.CreateEntityWithGLTFShape(scene, Vector3.zero, TestAssetsUtils.GetPath() + "/
 1290        GLTFShape gltfShape3 = TestHelpers.CreateEntityWithGLTFShape(scene, Vector3.zero, TestAssetsUtils.GetPath() + "/
 291
 1292        TestHelpers.SetEntityParent(scene, entity2, entity);
 1293        TestHelpers.SetEntityParent(scene, entity3, entity);
 294
 1295        yield return null;
 1296        yield return null;
 1297        yield return null;
 298
 1299        Object.Destroy(entity.gameObject);
 300
 1301        yield return null;
 1302    }
 303}