< Summary

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

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SetUp()0%330100%
NotDestroySharedTextureWhenDisposed()0%550100%
BeCreatedProperly()0%440100%
BeUpdatedProperly()0%550100%
BeSharedProperly()0%220100%
AffectDifferentEntitiesCorrectly()0%330100%
WorkCorrectlyWhenAttachedBeforeShape()0%550100%
DefaultMissingValuesPropertyOnUpdate()0%440100%
GetReplacedWhenAnotherMaterialIsAttached()0%330100%

File(s)

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

#LineLine coverage
 1using DCL;
 2using DCL.Components;
 3using DCL.Helpers;
 4using DCL.Models;
 5using System.Collections;
 6using System.Linq;
 7using UnityEngine;
 8using UnityEngine.Assertions;
 9using UnityEngine.TestTools;
 10
 11public class PBRMaterialShould : IntegrationTestSuite_Legacy
 12{
 13    [UnitySetUp]
 14    protected override IEnumerator SetUp()
 15    {
 816        yield return base.SetUp();
 817        Environment.i.world.sceneBoundsChecker.Stop();
 818    }
 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        PBRMaterial mat = TestHelpers.CreateEntityWithPBRMaterial(scene,
 29            new PBRMaterial.Model
 30            {
 31                albedoTexture = texture.id,
 32                metallic = 0,
 33                roughness = 1,
 34            },
 35            out IDCLEntity entity1);
 36
 137        yield return mat.routine;
 38
 139        PBRMaterial mat2 = TestHelpers.CreateEntityWithPBRMaterial(scene,
 40            new PBRMaterial.Model
 41            {
 42                albedoTexture = texture.id,
 43                metallic = 0,
 44                roughness = 1,
 45            },
 46            out IDCLEntity entity2);
 47
 148        yield return mat2.routine;
 49
 150        TestHelpers.SharedComponentDispose(mat);
 151        Assert.IsTrue(texture.texture != null, "Texture should persist because is used by the other material!!");
 152    }
 53
 54    [UnityTest]
 55    public IEnumerator BeCreatedProperly()
 56    {
 157        DCLTexture texture =
 58            TestHelpers.CreateDCLTexture(scene, TestAssetsUtils.GetPath() + "/Images/atlas.png");
 59
 160        yield return texture.routine;
 61
 162        PBRMaterial matPBR = TestHelpers.CreateEntityWithPBRMaterial(scene,
 63            new PBRMaterial.Model
 64            {
 65                albedoTexture = texture.id,
 66                metallic = 0,
 67                roughness = 1,
 68            },
 69            out IDCLEntity entity);
 70
 171        yield return matPBR.routine;
 72
 173        Assert.IsTrue(entity.meshRootGameObject != null,
 74            "Every entity with a shape should have the mandatory 'Mesh' object as a child");
 75
 176        var meshRenderer = entity.meshRootGameObject.GetComponent<MeshRenderer>();
 177        Assert.IsTrue(meshRenderer != null, "MeshRenderer must exist");
 78
 179        var assignedMaterial = meshRenderer.sharedMaterial;
 180        Assert.IsTrue(meshRenderer != null, "MeshRenderer.sharedMaterial must be the same as assignedMaterial");
 181        Assert.AreEqual(assignedMaterial, matPBR.material, "Assigned material");
 82
 183        var loadedTexture = meshRenderer.sharedMaterial.GetTexture("_BaseMap");
 184        Assert.IsTrue(loadedTexture != null, "Texture must be loaded");
 185        Assert.AreEqual(texture.texture, loadedTexture, "Texture data must be correct");
 186    }
 87
 88    [UnityTest]
 89    public IEnumerator BeUpdatedProperly()
 90    {
 191        string entityId = "1";
 192        string materialID = "a-material";
 93
 94        // Instantiate entity with default PBR Material
 195        TestHelpers.InstantiateEntityWithMaterial(scene, entityId, Vector3.zero,
 96            new PBRMaterial.Model(), materialID);
 97
 198        var materialComponent = scene.disposableComponents[materialID] as DCL.Components.PBRMaterial;
 99
 1100        yield return materialComponent.routine;
 101
 1102        Assert.IsTrue(materialComponent is DCL.Components.PBRMaterial, "material is PBRMaterial");
 103
 104        // Check if material initialized correctly
 105        {
 1106            Assert.IsTrue(scene.entities[entityId].meshRootGameObject != null,
 107                "Every entity with a shape should have the mandatory 'Mesh' object as a child");
 108
 1109            var meshRenderer = scene.entities[entityId].meshRootGameObject.GetComponent<MeshRenderer>();
 110
 1111            Assert.IsTrue(meshRenderer != null, "MeshRenderer must exist");
 112
 1113            var assignedMaterial = meshRenderer.sharedMaterial;
 1114            Assert.IsTrue(meshRenderer != null, "MeshRenderer.sharedMaterial must be the same as assignedMaterial");
 115
 1116            Assert.AreEqual(assignedMaterial, materialComponent.material, "Assigned material");
 117        }
 118
 119        // Check default properties
 120        {
 121            // Texture
 1122            Assert.IsTrue(materialComponent.material.GetTexture("_BaseMap") == null);
 123
 124            // Colors
 1125            Assert.AreEqual("FFFFFF", ColorUtility.ToHtmlStringRGB(materialComponent.material.GetColor("_BaseColor")));
 1126            Assert.AreEqual("000000",
 127                ColorUtility.ToHtmlStringRGB(materialComponent.material.GetColor("_EmissionColor")));
 1128            Assert.AreEqual("FFFFFF",
 129                ColorUtility.ToHtmlStringRGB(materialComponent.material.GetColor("_SpecColor")));
 130
 131            // Other properties
 1132            Assert.AreApproximatelyEqual(0.5f, materialComponent.material.GetFloat("_Metallic"));
 1133            Assert.AreApproximatelyEqual(0.5f, materialComponent.material.GetFloat("_Smoothness"));
 1134            Assert.AreApproximatelyEqual(1.0f, materialComponent.material.GetFloat("_EnvironmentReflections"));
 1135            Assert.AreApproximatelyEqual(1.0f, materialComponent.material.GetFloat("_SpecularHighlights"));
 1136            Assert.AreApproximatelyEqual(.0f, materialComponent.material.GetFloat("_AlphaClip"));
 137        }
 138
 139        // Update material
 1140        DCLTexture texture =
 141            TestHelpers.CreateDCLTexture(scene, TestAssetsUtils.GetPath() + "/Images/atlas.png");
 142
 1143        yield return texture.routine;
 144
 145        Color color1, color2, color3;
 146
 1147        ColorUtility.TryParseHtmlString("#99deff", out color1);
 1148        ColorUtility.TryParseHtmlString("#42f4aa", out color2);
 1149        ColorUtility.TryParseHtmlString("#601121", out color3);
 150
 1151        scene.SharedComponentUpdate(materialID, JsonUtility.ToJson(new DCL.Components.PBRMaterial.Model
 152        {
 153            albedoTexture = texture.id,
 154            albedoColor = color1,
 155            emissiveColor = color2,
 156            emissiveIntensity = 1,
 157            reflectivityColor = color3,
 158            metallic = 0.37f,
 159            roughness = 0.9f,
 160            microSurface = 0.4f,
 161            specularIntensity = 2f,
 162            transparencyMode = 2,
 163        }));
 164
 1165        yield return materialComponent.routine;
 166
 167        // Check updated properties
 168        {
 169            // Texture
 1170            Assert.IsTrue(materialComponent.material.GetTexture("_BaseMap") != null, "texture is null!");
 171
 172            // Colors
 1173            Assert.AreEqual("99DEFF", ColorUtility.ToHtmlStringRGB(materialComponent.material.GetColor("_BaseColor")));
 1174            Assert.AreEqual("42F4AA",
 175                ColorUtility.ToHtmlStringRGB(materialComponent.material.GetColor("_EmissionColor")));
 1176            Assert.AreEqual("601121",
 177                ColorUtility.ToHtmlStringRGB(materialComponent.material.GetColor("_SpecColor")));
 178
 179            // Other properties
 1180            Assert.AreApproximatelyEqual(0.37f, materialComponent.material.GetFloat("_Metallic"));
 1181            Assert.AreApproximatelyEqual(0.1f, materialComponent.material.GetFloat("_Smoothness"));
 1182            Assert.AreApproximatelyEqual(0.4f, materialComponent.material.GetFloat("_EnvironmentReflections"));
 1183            Assert.AreApproximatelyEqual(2.0f, materialComponent.material.GetFloat("_SpecularHighlights"));
 1184            Assert.AreApproximatelyEqual(.0f, materialComponent.material.GetFloat("_AlphaClip"));
 1185            Assert.AreEqual((int) UnityEngine.Rendering.BlendMode.SrcAlpha,
 186                materialComponent.material.GetInt("_SrcBlend"));
 1187            Assert.AreEqual((int) UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha,
 188                materialComponent.material.GetInt("_DstBlend"));
 1189            Assert.AreEqual(0, materialComponent.material.GetInt("_ZWrite"));
 190        }
 1191    }
 192
 193    [UnityTest]
 194    public IEnumerator BeSharedProperly()
 195    {
 196        // Create first entity with material
 1197        string firstEntityID = "1";
 1198        string firstMaterialID = "a-material";
 199
 1200        TestHelpers.InstantiateEntityWithMaterial(scene, firstEntityID, Vector3.zero,
 201            new DCL.Components.PBRMaterial.Model
 202            {
 203                metallic = 0.3f,
 204            }, firstMaterialID);
 205
 1206        Assert.IsTrue(scene.entities[firstEntityID].meshRootGameObject != null,
 207            "Every entity with a shape should have the mandatory 'Mesh' object as a child");
 208
 209        // Create second entity with material
 1210        string secondEntityID = "2";
 1211        string secondMaterialID = "b-material";
 212
 1213        TestHelpers.InstantiateEntityWithMaterial(scene, secondEntityID, Vector3.zero,
 214            new DCL.Components.PBRMaterial.Model
 215            {
 216                metallic = 0.66f,
 217            }, secondMaterialID);
 218
 1219        Assert.IsTrue(scene.entities[secondEntityID].meshRootGameObject != null,
 220            "Every entity with a shape should have the mandatory 'Mesh' object as a child");
 221
 222        // Create third entity and assign 1st material
 1223        string thirdEntityID = "3";
 224
 1225        TestHelpers.InstantiateEntityWithShape(scene, thirdEntityID, DCL.Models.CLASS_ID.BOX_SHAPE, Vector3.zero);
 1226        scene.SharedComponentAttach(
 227            thirdEntityID,
 228            firstMaterialID
 229        );
 230
 1231        Assert.IsTrue(scene.entities[thirdEntityID].meshRootGameObject != null,
 232            "Every entity with a shape should have the mandatory 'Mesh' object as a child");
 233
 234        // Check renderers material references
 1235        var firstRenderer = scene.entities[firstEntityID].meshRootGameObject.GetComponent<MeshRenderer>();
 1236        var secondRenderer = scene.entities[secondEntityID].meshRootGameObject.GetComponent<MeshRenderer>();
 1237        var thirdRenderer = scene.entities[thirdEntityID].meshRootGameObject.GetComponent<MeshRenderer>();
 1238        Assert.IsTrue(firstRenderer.sharedMaterial != secondRenderer.sharedMaterial,
 239            "1st and 2nd entities should have different materials");
 1240        Assert.IsTrue(firstRenderer.sharedMaterial == thirdRenderer.sharedMaterial,
 241            "1st and 3rd entities should have the same material");
 242
 1243        yield break;
 244    }
 245
 246    [UnityTest]
 247    public IEnumerator AffectDifferentEntitiesCorrectly()
 248    {
 249        // Create first entity with material
 1250        PBRMaterial material1 = TestHelpers.CreateEntityWithPBRMaterial(scene,
 251            new PBRMaterial.Model
 252            {
 253                metallic = 0.3f,
 254            }, out IDCLEntity entity1);
 255
 1256        Assert.IsTrue(entity1.meshRootGameObject != null,
 257            "Every entity with a shape should have the mandatory 'Mesh' object as a child");
 258
 259        // Create second entity with material
 1260        PBRMaterial material2 = TestHelpers.CreateEntityWithPBRMaterial(scene,
 261            new PBRMaterial.Model
 262            {
 263                metallic = 0.66f,
 264            }, out IDCLEntity entity2);
 265
 1266        Assert.IsTrue(entity2.meshRootGameObject != null,
 267            "Every entity with a shape should have the mandatory 'Mesh' object as a child");
 268
 269        // Create third entity and assign 1st material
 1270        var boxShape = TestHelpers.CreateEntityWithBoxShape(scene, Vector3.zero);
 1271        var entity3 = boxShape.attachedEntities.First();
 272
 1273        scene.SharedComponentAttach(
 274            entity3.entityId,
 275            material1.id
 276        );
 277
 1278        Assert.IsTrue(entity3.meshRootGameObject != null,
 279            "Every entity with a shape should have the mandatory 'Mesh' object as a child");
 280
 281        // Check renderers material references
 1282        var firstRenderer = entity1.meshRootGameObject.GetComponent<MeshRenderer>();
 1283        var secondRenderer = entity2.meshRootGameObject.GetComponent<MeshRenderer>();
 1284        var thirdRenderer = entity3.meshRootGameObject.GetComponent<MeshRenderer>();
 285
 1286        Assert.IsTrue(firstRenderer.sharedMaterial != secondRenderer.sharedMaterial,
 287            "1st and 2nd entities should have different materials");
 1288        Assert.IsTrue(firstRenderer.sharedMaterial == thirdRenderer.sharedMaterial,
 289            "1st and 3rd entities should have the same material");
 290
 291        // Check material properties before updating them
 1292        Assert.AreApproximatelyEqual(0.3f, firstRenderer.sharedMaterial.GetFloat("_Metallic"));
 1293        Assert.AreApproximatelyEqual(0.66f, secondRenderer.sharedMaterial.GetFloat("_Metallic"));
 294
 295        // Update material properties
 1296        scene.SharedComponentUpdate(material1.id, JsonUtility.ToJson(new PBRMaterial.Model
 297        {
 298            metallic = 0.95f
 299        }));
 300
 1301        Assert.IsTrue(firstRenderer.sharedMaterial != null);
 302
 1303        yield return material1.routine;
 304
 1305        Assert.IsTrue(firstRenderer.sharedMaterial != null);
 306        // Check material properties after updating them
 1307        Assert.AreApproximatelyEqual(0.95f, firstRenderer.sharedMaterial.GetFloat("_Metallic"));
 1308        Assert.AreApproximatelyEqual(0.66f, secondRenderer.sharedMaterial.GetFloat("_Metallic"));
 1309    }
 310
 311    [UnityTest]
 312    public IEnumerator WorkCorrectlyWhenAttachedBeforeShape()
 313    {
 1314        IDCLEntity entity = TestHelpers.CreateSceneEntity(scene);
 315
 1316        DCLTexture dclTexture = TestHelpers.CreateDCLTexture(
 317            scene,
 318            TestAssetsUtils.GetPath() + "/Images/atlas.png",
 319            DCLTexture.BabylonWrapMode.CLAMP,
 320            FilterMode.Bilinear);
 321
 1322        yield return dclTexture.routine;
 323
 1324        PBRMaterial mat = TestHelpers.SharedComponentCreate<PBRMaterial, PBRMaterial.Model>(scene,
 325            CLASS_ID.PBR_MATERIAL,
 326            new PBRMaterial.Model
 327            {
 328                albedoTexture = dclTexture.id,
 329                metallic = 0,
 330                roughness = 1,
 331            }
 332        );
 333
 1334        yield return mat.routine;
 335
 1336        TestHelpers.SharedComponentAttach(mat, entity);
 337
 1338        SphereShape shape = TestHelpers.SharedComponentCreate<SphereShape, SphereShape.Model>(scene,
 339            CLASS_ID.SPHERE_SHAPE,
 340            new SphereShape.Model { });
 341
 1342        yield return shape.routine;
 343
 1344        TestHelpers.SharedComponentAttach(shape, entity);
 345
 1346        Assert.IsTrue(entity.meshRootGameObject != null);
 1347        Assert.IsTrue(entity.meshRootGameObject.GetComponent<MeshRenderer>() != null);
 1348        Assert.AreEqual(entity.meshRootGameObject.GetComponent<MeshRenderer>().sharedMaterial, mat.material);
 1349    }
 350
 351    [UnityTest]
 352    public IEnumerator DefaultMissingValuesPropertyOnUpdate()
 353    {
 354        Color color1;
 1355        ColorUtility.TryParseHtmlString("#808080", out color1);
 356
 357        // 1. Create component with non-default configs
 1358        PBRMaterial PBRMaterialComponent = TestHelpers.SharedComponentCreate<PBRMaterial, PBRMaterial.Model>(scene,
 359            CLASS_ID.PBR_MATERIAL,
 360            new PBRMaterial.Model
 361            {
 362                albedoColor = color1,
 363                metallic = 0.3f,
 364                directIntensity = 0.1f,
 365                specularIntensity = 3f
 366            });
 367
 1368        yield return PBRMaterialComponent.routine;
 369
 370        // 2. Check configured values
 1371        Assert.AreEqual(color1, PBRMaterialComponent.GetModel().albedoColor);
 1372        Assert.AreEqual(0.3f, PBRMaterialComponent.GetModel().metallic);
 1373        Assert.AreEqual(0.1f, PBRMaterialComponent.GetModel().directIntensity);
 1374        Assert.AreEqual(3f, PBRMaterialComponent.GetModel().specularIntensity);
 375
 376        // 3. Update component with missing values
 1377        scene.SharedComponentUpdate(PBRMaterialComponent.id, JsonUtility.ToJson(new PBRMaterial.Model { }));
 378
 1379        yield return PBRMaterialComponent.routine;
 380
 381        // 4. Check defaulted values
 1382        Assert.AreEqual(Color.white, PBRMaterialComponent.GetModel().albedoColor);
 1383        Assert.AreEqual(0.5f, PBRMaterialComponent.GetModel().metallic);
 1384        Assert.AreEqual(1, PBRMaterialComponent.GetModel().directIntensity);
 1385        Assert.AreEqual(1f, PBRMaterialComponent.GetModel().specularIntensity);
 1386    }
 387
 388    [UnityTest]
 389    public IEnumerator GetReplacedWhenAnotherMaterialIsAttached()
 390    {
 1391        yield return TestHelpers.TestAttachedSharedComponentOfSameTypeIsReplaced<PBRMaterial.Model, PBRMaterial>(
 392            scene, CLASS_ID.PBR_MATERIAL);
 1393    }
 394}