< Summary

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

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SetUp()0%330100%
NotDestroySharedTextureWhenDisposed()0%550100%
WorkCorrectlyWhenAttachedBeforeShape()0%440100%
GetReplacedWhenAnotherMaterialIsAttached()0%330100%
BeDetachedCorrectly()0%330100%
BeDetachedOnDispose()0%330100%
EntityBasicMaterialUpdate()0%440100%
DefaultMissingValuesPropertyOnUpdate()0%440100%
ProcessCastShadowProperty_True()0%330100%
ProcessCastShadowProperty_False()0%330100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Materials/Tests/BasicMaterialShould.cs

#LineLine coverage
 1using System.Collections;
 2using DCL;
 3using DCL.Components;
 4using DCL.Helpers;
 5using DCL.Models;
 6using UnityEngine;
 7using UnityEngine.Rendering;
 8using UnityEngine.TestTools;
 9using Assert = UnityEngine.Assertions.Assert;
 10
 11public class BasicMaterialShould : IntegrationTestSuite_Legacy
 12{
 13    [UnitySetUp]
 14    protected override IEnumerator SetUp()
 15    {
 916        yield return base.SetUp();
 917        Environment.i.world.sceneBoundsChecker.Stop();
 918    }
 19
 20    [UnityTest]
 21    public IEnumerator NotDestroySharedTextureWhenDisposed()
 22    {
 123        DCLTexture texture =
 24            TestHelpers.CreateDCLTexture(scene, TestAssetsUtils.GetPath() + "/Images/atlas.png");
 25
 126        yield return texture.routine;
 27
 128        BasicMaterial mat = TestHelpers.CreateEntityWithBasicMaterial(scene,
 29            new BasicMaterial.Model
 30            {
 31                texture = texture.id,
 32                alphaTest = 1,
 33            },
 34            out IDCLEntity entity1);
 35
 136        yield return mat.routine;
 37
 138        BasicMaterial mat2 = TestHelpers.CreateEntityWithBasicMaterial(scene,
 39            new BasicMaterial.Model
 40            {
 41                texture = texture.id,
 42                alphaTest = 1,
 43            },
 44            out IDCLEntity entity2);
 45
 146        yield return mat2.routine;
 47
 148        TestHelpers.SharedComponentDispose(mat);
 149        Assert.IsTrue(texture.texture != null, "Texture should persist because is used by the other material!!");
 150    }
 51
 52    [UnityTest]
 53    public IEnumerator WorkCorrectlyWhenAttachedBeforeShape()
 54    {
 155        IDCLEntity entity = TestHelpers.CreateSceneEntity(scene);
 56
 157        DCLTexture dclTexture = TestHelpers.CreateDCLTexture(
 58            scene,
 59            TestAssetsUtils.GetPath() + "/Images/atlas.png",
 60            DCLTexture.BabylonWrapMode.CLAMP,
 61            FilterMode.Bilinear);
 62
 163        yield return dclTexture.routine;
 64
 165        BasicMaterial mat = TestHelpers.SharedComponentCreate<BasicMaterial, BasicMaterial.Model>
 66        (scene, CLASS_ID.BASIC_MATERIAL,
 67            new BasicMaterial.Model
 68            {
 69                texture = dclTexture.id,
 70                alphaTest = 0.5f
 71            });
 72
 173        yield return mat.routine;
 74
 175        TestHelpers.SharedComponentAttach(mat, entity);
 76
 177        SphereShape shape = TestHelpers.SharedComponentCreate<SphereShape, SphereShape.Model>(scene,
 78            CLASS_ID.SPHERE_SHAPE,
 79            new SphereShape.Model { });
 80
 181        TestHelpers.SharedComponentAttach(shape, entity);
 82
 183        Assert.IsTrue(entity.meshRootGameObject != null);
 184        Assert.IsTrue(entity.meshRootGameObject.GetComponent<MeshRenderer>() != null);
 185        Assert.AreEqual(entity.meshRootGameObject.GetComponent<MeshRenderer>().sharedMaterial, mat.material);
 186    }
 87
 88    [UnityTest]
 89    public IEnumerator GetReplacedWhenAnotherMaterialIsAttached()
 90    {
 191        yield return
 92            TestHelpers.TestAttachedSharedComponentOfSameTypeIsReplaced<BasicMaterial.Model, BasicMaterial>(scene,
 93                CLASS_ID.BASIC_MATERIAL);
 194    }
 95
 96    [UnityTest]
 97    public IEnumerator BeDetachedCorrectly()
 98    {
 199        string entityId = "1";
 1100        string materialID = "a-material";
 101
 1102        TestHelpers.InstantiateEntityWithMaterial(scene, entityId, Vector3.zero,
 103            new BasicMaterial.Model(), materialID);
 104
 1105        Assert.IsTrue(scene.entities[entityId].meshRootGameObject != null,
 106            "Every entity with a shape should have the mandatory 'Mesh' object as a child");
 107
 1108        var meshRenderer = scene.entities[entityId].meshRootGameObject.GetComponent<MeshRenderer>();
 1109        var materialComponent = scene.disposableComponents[materialID] as BasicMaterial;
 110
 1111        yield return materialComponent.routine;
 112
 113        // Check if material initialized correctly
 114        {
 1115            Assert.IsTrue(meshRenderer != null, "MeshRenderer must exist");
 116
 1117            Assert.AreEqual(meshRenderer.sharedMaterial, materialComponent.material, "Assigned material");
 118        }
 119
 120        // Remove material
 1121        materialComponent.DetachFrom(scene.entities[entityId]);
 122
 123        // Check if material was removed correctly
 1124        Assert.IsTrue(meshRenderer.sharedMaterial == null,
 125            "Assigned material should be null as it has been removed");
 1126    }
 127
 128    [UnityTest]
 129    public IEnumerator BeDetachedOnDispose()
 130    {
 1131        string firstEntityId = "1";
 1132        string secondEntityId = "2";
 1133        string materialID = "a-material";
 134
 135        // Instantiate entity with material
 1136        TestHelpers.InstantiateEntityWithMaterial(scene, firstEntityId, Vector3.zero,
 137            new BasicMaterial.Model(), materialID);
 138
 1139        Assert.IsTrue(scene.entities[firstEntityId].meshRootGameObject != null,
 140            "Every entity with a shape should have the mandatory 'Mesh' object as a child");
 141
 142        // Create 2nd entity and attach same material to it
 1143        TestHelpers.InstantiateEntityWithShape(scene, secondEntityId, CLASS_ID.BOX_SHAPE, Vector3.zero);
 1144        scene.SharedComponentAttach(
 145            secondEntityId,
 146            materialID
 147        );
 148
 1149        Assert.IsTrue(scene.entities[secondEntityId].meshRootGameObject != null,
 150            "Every entity with a shape should have the mandatory 'Mesh' object as a child");
 151
 1152        var firstMeshRenderer = scene.entities[firstEntityId].meshRootGameObject.GetComponent<MeshRenderer>();
 1153        var secondMeshRenderer = scene.entities[secondEntityId].meshRootGameObject.GetComponent<MeshRenderer>();
 1154        var materialComponent = scene.disposableComponents[materialID] as DCL.Components.BasicMaterial;
 155
 1156        yield return materialComponent.routine;
 157
 158        // Check if material attached correctly
 159        {
 1160            Assert.IsTrue(firstMeshRenderer != null, "MeshRenderer must exist");
 1161            Assert.AreEqual(firstMeshRenderer.sharedMaterial, materialComponent.material, "Assigned material");
 162
 1163            Assert.IsTrue(secondMeshRenderer != null, "MeshRenderer must exist");
 1164            Assert.AreEqual(secondMeshRenderer.sharedMaterial, materialComponent.material, "Assigned material");
 165        }
 166
 167        // Dispose material
 1168        scene.SharedComponentDispose(materialID);
 169
 170        // Check if material detached correctly
 1171        Assert.IsTrue(firstMeshRenderer.sharedMaterial == null, "MeshRenderer must exist");
 1172        Assert.IsTrue(secondMeshRenderer.sharedMaterial == null, "MeshRenderer must exist");
 1173    }
 174
 175    [UnityTest]
 176    public IEnumerator EntityBasicMaterialUpdate()
 177    {
 1178        string entityId = "1";
 1179        string materialID = "a-material";
 180
 1181        Assert.IsFalse(scene.disposableComponents.ContainsKey(materialID));
 182
 183        // Instantiate entity with default material
 1184        TestHelpers.InstantiateEntityWithMaterial(scene, entityId, new Vector3(8, 1, 8),
 185            new BasicMaterial.Model(), materialID);
 186
 1187        var meshObject = scene.entities[entityId].meshRootGameObject;
 1188        Assert.IsTrue(meshObject != null,
 189            "Every entity with a shape should have the mandatory 'Mesh' object as a child");
 190
 1191        var meshRenderer = meshObject.GetComponent<MeshRenderer>();
 1192        var materialComponent = scene.disposableComponents[materialID] as BasicMaterial;
 193
 1194        yield return materialComponent.routine;
 195
 196        // Check if material initialized correctly
 197        {
 1198            Assert.IsTrue(meshRenderer != null, "MeshRenderer must exist");
 199
 1200            var assignedMaterial = meshRenderer.sharedMaterial;
 1201            Assert.IsTrue(meshRenderer != null, "MeshRenderer.sharedMaterial must be the same as assignedMaterial");
 202
 1203            Assert.AreEqual(assignedMaterial, materialComponent.material, "Assigned material");
 204        }
 205
 206        // Check default properties
 207        {
 1208            Assert.IsTrue(materialComponent.material.GetTexture("_BaseMap") == null);
 1209            Assert.AreApproximatelyEqual(1.0f, materialComponent.material.GetFloat("_AlphaClip"));
 210        }
 211
 1212        DCLTexture dclTexture = TestHelpers.CreateDCLTexture(
 213            scene,
 214            TestAssetsUtils.GetPath() + "/Images/atlas.png",
 215            DCLTexture.BabylonWrapMode.MIRROR,
 216            FilterMode.Bilinear);
 217
 218        // Update material
 1219        scene.SharedComponentUpdate(materialID, JsonUtility.ToJson(new BasicMaterial.Model
 220        {
 221            texture = dclTexture.id,
 222            alphaTest = 0.5f,
 223        }));
 224
 1225        yield return materialComponent.routine;
 226
 227        // Check updated properties
 228        {
 1229            Texture mainTex = materialComponent.material.GetTexture("_BaseMap");
 1230            Assert.IsTrue(mainTex != null);
 1231            Assert.AreApproximatelyEqual(0.5f, materialComponent.material.GetFloat("_Cutoff"));
 1232            Assert.AreApproximatelyEqual(1.0f, materialComponent.material.GetFloat("_AlphaClip"));
 1233            Assert.AreEqual(TextureWrapMode.Mirror, mainTex.wrapMode);
 1234            Assert.AreEqual(FilterMode.Bilinear, mainTex.filterMode);
 235        }
 1236    }
 237
 238    [UnityTest]
 239    public IEnumerator DefaultMissingValuesPropertyOnUpdate()
 240    {
 241        // 1. Create component with non-default configs
 1242        BasicMaterial basicMaterialComponent =
 243            TestHelpers.SharedComponentCreate<BasicMaterial, BasicMaterial.Model>(scene, CLASS_ID.BASIC_MATERIAL,
 244                new BasicMaterial.Model
 245                {
 246                    alphaTest = 1f
 247                });
 248
 1249        yield return basicMaterialComponent.routine;
 250
 251        // 2. Check configured values
 1252        Assert.AreEqual(1f, basicMaterialComponent.GetModel().alphaTest);
 253
 254        // 3. Update component with missing values
 255
 1256        scene.SharedComponentUpdate(basicMaterialComponent.id, JsonUtility.ToJson(new BasicMaterial.Model { }));
 257
 1258        yield return basicMaterialComponent.routine;
 259
 260        // 4. Check defaulted values
 1261        Assert.AreEqual(0.5f, basicMaterialComponent.GetModel().alphaTest);
 1262    }
 263
 264    [UnityTest]
 265    public IEnumerator ProcessCastShadowProperty_True()
 266    {
 1267        BasicMaterial basicMaterialComponent = TestHelpers.CreateEntityWithBasicMaterial(scene, new BasicMaterial.Model
 268        {
 269            alphaTest = 1f,
 270            castShadows = true
 271        }, out IDCLEntity entity);
 1272        yield return basicMaterialComponent.routine;
 273
 1274        Assert.AreEqual(true, basicMaterialComponent.GetModel().castShadows);
 1275        Assert.AreEqual(ShadowCastingMode.On, entity.meshRootGameObject.GetComponent<MeshRenderer>().shadowCastingMode);
 1276    }
 277
 278    [UnityTest]
 279    public IEnumerator ProcessCastShadowProperty_False()
 280    {
 1281        BasicMaterial basicMaterialComponent = TestHelpers.CreateEntityWithBasicMaterial(scene, new BasicMaterial.Model
 282        {
 283            alphaTest = 1f,
 284            castShadows = false
 285        }, out IDCLEntity entity);
 1286        yield return basicMaterialComponent.routine;
 287
 1288        Assert.AreEqual(false, basicMaterialComponent.GetModel().castShadows);
 1289        Assert.AreEqual(ShadowCastingMode.Off, entity.meshRootGameObject.GetComponent<MeshRenderer>().shadowCastingMode)
 1290    }
 291}