< Summary

Class:DCL.ABConverter.Tests.ABConverterCoreShould
Assembly:ABConverterTests
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/ABConverter/Tests/ABConverterCoreShould.cs
Covered lines:0
Uncovered lines:215
Coverable lines:215
Total lines:430
Line coverage:0% (0 of 215)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ABConverterCoreShould()0%2100%
SetUp()0%6200%
TearDown()0%2100%
PopulateLowercaseMappingsCorrectly()0%2100%
InitializeDirectoryPathsCorrectly()0%2100%
InjectTexturesCorrectly()0%2100%
InjectBuffersCorrectly()0%2100%
DumpGLTFSucceedsCorrectly()0%2100%
DumpGLTFFailsCorrectly()0%2100%
DumpImportableAssetsCorrectly()0%2100%
DumpRawAssetsCorrectly()0%2100%
DownloadAssetCorrectly()0%2100%
ConvertAssetsWithExternalTextures()0%20400%
NotGenerateColorMapsWithDXTnm()0%12300%
NotFailIfExternalTexturesAreMissing()0%12300%
ResetCacheAndWorkingFolders()0%20400%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/ABConverter/Tests/ABConverterCoreShould.cs

#LineLine coverage
 1using DCL;
 2using NUnit.Framework;
 3using System.Collections;
 4using System.Collections.Generic;
 5using System.IO;
 6using System.Text;
 7using System.Text.RegularExpressions;
 8using DCL.Helpers;
 9using UnityEditor;
 10using UnityEngine;
 11using UnityEngine.Events;
 12using UnityEngine.TestTools;
 13using UnityEngine.UI;
 14using UnityGLTF.Cache;
 15
 16namespace DCL.ABConverter.Tests
 17{
 18    public class ABConverterShould
 19    {
 20        [UnityTest]
 21        public IEnumerator DumpAreaCorrectly()
 22        {
 23            //TODO(Brian): Implement later
 24            yield break;
 25        }
 26
 27        [UnityTest]
 28        public IEnumerator DumpSceneCorrectly()
 29        {
 30            //TODO(Brian): Implement later
 31            yield break;
 32        }
 33    }
 34
 35    public class ABConverterCoreShould
 36    {
 37        private ABConverter.Core core;
 38        private ABConverter.Environment env;
 39
 040        string basePath = @"C:\test-path\";
 041        string hash1 = "QmHash1", hash2 = "QmHash2", hash3 = "QmHash3", hash4 = "QmHash4";
 042        string baseUrl = @"https://peer.decentraland.org/lambdas/contentv2/contents/";
 043        private string contentData1 = "TestContent1 - guid: 14e357df3f3b75940b5d59e1035255b1\n";
 044        private string contentData2 = "TestContent2 - guid: 14e357df3f3b75940b5d59e1035255b2\n";
 045        private string contentData3 = "TestContent3 - guid: 14e357df3f3b75940b5d59e1035255b3\n";
 46
 47        [SetUp]
 48        public void SetUp()
 49        {
 050            ResetCacheAndWorkingFolders();
 51
 052            var settings = new ABConverter.Client.Settings(ContentServerUtils.ApiTLD.ZONE);
 053            settings.deleteDownloadPathAfterFinished = false;
 54
 055            env = ABConverter.Environment.CreateWithMockImplementations();
 056            core = new ABConverter.Core(env, settings);
 57
 058            if (env.webRequest is Mocked.WebRequest mockedReq)
 59            {
 060                mockedReq.mockedContent.Add($"{baseUrl}{hash1}", contentData1);
 061                mockedReq.mockedContent.Add($"{baseUrl}{hash2}", contentData2);
 062                mockedReq.mockedContent.Add($"{baseUrl}{hash3}", contentData3);
 63            }
 064        }
 65
 66        [TearDown]
 067        public void TearDown() { ResetCacheAndWorkingFolders(); }
 68
 69        [Test]
 70        public void PopulateLowercaseMappingsCorrectly()
 71        {
 072            var pairs = new List<ContentServerUtils.MappingPair>();
 73
 074            pairs.Add(new ContentServerUtils.MappingPair() { file = "foo", hash = "tEsT1" });
 075            pairs.Add(new ContentServerUtils.MappingPair() { file = "foo", hash = "Test2" });
 076            pairs.Add(new ContentServerUtils.MappingPair() { file = "foo", hash = "tesT3" });
 077            pairs.Add(new ContentServerUtils.MappingPair() { file = "foo", hash = "teSt4" });
 78
 079            core.PopulateLowercaseMappings(pairs.ToArray());
 80
 081            Assert.IsTrue(core.hashLowercaseToHashProper.ContainsKey("test1"));
 082            Assert.IsTrue(core.hashLowercaseToHashProper.ContainsKey("test2"));
 083            Assert.IsTrue(core.hashLowercaseToHashProper.ContainsKey("test3"));
 084            Assert.IsTrue(core.hashLowercaseToHashProper.ContainsKey("test4"));
 85
 086            Assert.AreEqual("tEsT1", core.hashLowercaseToHashProper["test1"]);
 087            Assert.AreEqual("Test2", core.hashLowercaseToHashProper["test2"]);
 088            Assert.AreEqual("tesT3", core.hashLowercaseToHashProper["test3"]);
 089            Assert.AreEqual("teSt4", core.hashLowercaseToHashProper["test4"]);
 090        }
 91
 92        [Test]
 93        public void InitializeDirectoryPathsCorrectly()
 94        {
 095            var settings = new ABConverter.Client.Settings(ContentServerUtils.ApiTLD.ZONE);
 096            settings.deleteDownloadPathAfterFinished = false;
 97
 098            env = ABConverter.Environment.CreateWithDefaultImplementations();
 099            core = new ABConverter.Core(env, settings);
 100
 0101            core.InitializeDirectoryPaths(false);
 102
 0103            Assert.IsFalse(string.IsNullOrEmpty(core.settings.finalAssetBundlePath));
 0104            Assert.IsFalse(string.IsNullOrEmpty(core.finalDownloadedPath));
 105
 0106            Assert.IsTrue(env.directory.Exists(core.settings.finalAssetBundlePath));
 0107            Assert.IsTrue(env.directory.Exists(core.finalDownloadedPath));
 108
 0109            string file1 = core.settings.finalAssetBundlePath + "test.txt";
 0110            string file2 = core.finalDownloadedPath + "test.txt";
 111
 0112            env.file.WriteAllText(file1, "test");
 0113            env.file.WriteAllText(file2, "test");
 114
 0115            core.InitializeDirectoryPaths(true);
 116
 0117            Assert.IsFalse(env.file.Exists(file1));
 0118            Assert.IsFalse(env.file.Exists(file2));
 0119        }
 120
 121        [Test]
 122        public void InjectTexturesCorrectly()
 123        {
 0124            AssetPath gltfPath = new AssetPath(core.finalDownloadedPath, "MyHash", "model/myModel.gltf");
 0125            AssetPath texturePath = new AssetPath(core.finalDownloadedPath, "MyHash2", "model/texture.png");
 0126            AssetPath texturePath2 = new AssetPath(core.finalDownloadedPath, "MyHash3", "model/invalid-texture.png");
 127
 0128            PersistentAssetCache.ImageCacheByUri.Clear();
 0129            PersistentAssetCache.StreamCacheByUri.Clear();
 130
 0131            core.RetrieveAndInjectTexture(gltfPath, texturePath);
 132
 0133            string content1 = "Test";
 0134            env.file.WriteAllText(texturePath.finalPath, content1);
 135
 0136            core.RetrieveAndInjectTexture(gltfPath, texturePath);
 0137            core.RetrieveAndInjectTexture(gltfPath, texturePath2);
 138
 0139            string id1 = $"texture.png@{gltfPath.finalPath}";
 0140            string id2 = $"invalid-texture.png@{gltfPath.finalPath}";
 141
 142            //NOTE(Brian): Check if streams exists and are added correctly
 0143            Assert.IsTrue(PersistentAssetCache.HasImage(id1), $"id1:{id1} doesn't exist?");
 0144            Assert.IsFalse(PersistentAssetCache.HasImage(id2), $"Second file with {id2} shouldn't be injected because it
 145
 0146            Assert.IsNotNull(PersistentAssetCache.GetImage(id1), "First image don't exist!");
 147
 148            //NOTE(Brian): Read image and validate content
 0149            var image1 = PersistentAssetCache.GetImage(id1);
 0150        }
 151
 152        [Test]
 153        public void InjectBuffersCorrectly()
 154        {
 0155            AssetPath gltfPath = new AssetPath(core.finalDownloadedPath, "MyHash", "models/myModel.gltf");
 0156            AssetPath bufferPath = new AssetPath(core.finalDownloadedPath, "MyHash2", "models/anims/anim1.bin");
 0157            AssetPath bufferPath2 = new AssetPath(core.finalDownloadedPath, "MyHash3", "models/misc.bin");
 0158            AssetPath bufferPath3 = new AssetPath(core.finalDownloadedPath, "MyHash4", "missing-file.bin");
 159
 0160            PersistentAssetCache.ImageCacheByUri.Clear();
 0161            PersistentAssetCache.StreamCacheByUri.Clear();
 162
 0163            string content1 = "Test";
 0164            string content2 = "Test2";
 165
 0166            env.file.WriteAllText(bufferPath.finalPath, content1);
 0167            env.file.WriteAllText(bufferPath2.finalPath, content2);
 168
 0169            core.RetrieveAndInjectBuffer(gltfPath, bufferPath);
 0170            core.RetrieveAndInjectBuffer(gltfPath, bufferPath2);
 0171            core.RetrieveAndInjectBuffer(gltfPath, bufferPath3);
 172
 0173            char ds = Path.DirectorySeparatorChar;
 174
 0175            string id1 = $"anims{ds}anim1.bin@{gltfPath.finalPath}";
 0176            string id2 = $"misc.bin@{gltfPath.finalPath}";
 0177            string id3 = $"..{ds}missing-file.bin@{gltfPath.finalPath}";
 178
 179            //NOTE(Brian): Check if streams exists and are added correctly
 0180            Assert.IsTrue(PersistentAssetCache.HasBuffer(id1));
 0181            Assert.IsTrue(PersistentAssetCache.HasBuffer(id2));
 0182            Assert.IsFalse(PersistentAssetCache.HasBuffer(id3), "Third file shouldn't be injected because it doesn't exi
 183
 0184            Assert.IsNotNull(PersistentAssetCache.GetBuffer(id1), "First stream don't exist!");
 0185            Assert.IsNotNull(PersistentAssetCache.GetBuffer(id2), "Second stream don't exist!");
 186
 187            //NOTE(Brian): Read stream and validate content
 0188            var buffer1 = PersistentAssetCache.GetBuffer(id1);
 0189            var buffer2 = PersistentAssetCache.GetBuffer(id2);
 190
 0191            byte[] chars = new byte[100];
 0192            buffer1.stream.Read(chars, 0, 100);
 0193            string bufferText = UTF8Encoding.UTF8.GetString(chars).TrimEnd('\0');
 0194            Assert.AreEqual(content1, bufferText, "First stream has invalid content!");
 195
 0196            buffer2.stream.Read(chars, 0, 100);
 0197            bufferText = UTF8Encoding.UTF8.GetString(chars).TrimEnd('\0');
 0198            Assert.AreEqual(content2, bufferText, "Second stream has invalid content!");
 199
 0200            buffer1.stream.Dispose();
 0201            buffer2.stream.Dispose();
 0202        }
 203
 204        [Test]
 205        public void DumpGLTFSucceedsCorrectly()
 206        {
 0207            List<AssetPath> texturePaths = new List<AssetPath>();
 0208            List<AssetPath> bufferPaths = new List<AssetPath>();
 209
 0210            AssetPath gltfPath = new AssetPath(basePath, hash1, "test.gltf");
 211
 0212            var output = core.DumpGltf(gltfPath, texturePaths, bufferPaths);
 213
 0214            Assert.IsNotNull(output);
 0215        }
 216
 217        [Test]
 218        public void DumpGLTFFailsCorrectly()
 219        {
 0220            List<AssetPath> texturePaths = new List<AssetPath>();
 0221            List<AssetPath> bufferPaths = new List<AssetPath>();
 222
 0223            AssetPath gltfPath = new AssetPath(basePath, "QmNonExistentHash", "test.gltf");
 224
 0225            var output = core.DumpGltf(gltfPath, texturePaths, bufferPaths);
 226
 0227            LogAssert.Expect(LogType.Error, new Regex("^.*?Download failed!"));
 0228            Assert.IsNull(output);
 0229        }
 230
 231        [Test]
 232        public void DumpImportableAssetsCorrectly()
 233        {
 0234            List<AssetPath> paths = new List<AssetPath>();
 235
 0236            string[] files = { "file1.png", "file2.png", "file3.png", "file4.png" };
 237
 0238            paths.Add(new AssetPath(basePath, hash1, files[0]));
 0239            paths.Add(new AssetPath(basePath, hash2, files[1]));
 0240            paths.Add(new AssetPath(basePath, hash3, files[2]));
 0241            paths.Add(new AssetPath(basePath, hash4, files[3]));
 242
 0243            string targetGuid1 = ABConverter.Utils.CidToGuid(hash1);
 0244            string targetGuid2 = ABConverter.Utils.CidToGuid(hash2);
 0245            string targetGuid3 = ABConverter.Utils.CidToGuid(hash3);
 246
 0247            var textures = core.DumpImportableAssets(paths);
 248
 0249            LogAssert.Expect(LogType.Error, new Regex(@"^.*?Download failed"));
 0250            LogAssert.Expect(LogType.Error, new Regex(@"^.*?QmHash4"));
 251
 0252            Assert.AreEqual(3, textures.Count);
 253
 254            //NOTE(Brian): textures exist?
 0255            Assert.IsTrue(env.file.Exists(paths[0].finalPath));
 0256            Assert.IsTrue(env.file.Exists(paths[1].finalPath));
 0257            Assert.IsTrue(env.file.Exists(paths[2].finalPath));
 0258            Assert.IsFalse(env.file.Exists(paths[3].finalPath));
 259
 260            //NOTE(Brian): textures .meta exist?
 0261            Assert.IsTrue(env.file.Exists(paths[0].finalMetaPath));
 0262            Assert.IsTrue(env.file.Exists(paths[1].finalMetaPath));
 0263            Assert.IsTrue(env.file.Exists(paths[2].finalMetaPath));
 0264            Assert.IsFalse(env.file.Exists(paths[3].finalMetaPath));
 265
 266            //NOTE(Brian): textures .meta guid is changed?
 0267            Assert.IsTrue(env.file.ReadAllText(paths[0].finalMetaPath).Contains(targetGuid1));
 0268            Assert.IsTrue(env.file.ReadAllText(paths[1].finalMetaPath).Contains(targetGuid2));
 0269            Assert.IsTrue(env.file.ReadAllText(paths[2].finalMetaPath).Contains(targetGuid3));
 0270        }
 271
 272        [Test]
 273        public void DumpRawAssetsCorrectly()
 274        {
 0275            List<AssetPath> paths = new List<AssetPath>();
 276
 0277            string[] files = { "file1.bin", "file2.bin", "file3.bin", "file4.bin" };
 278
 0279            paths.Add(new AssetPath(basePath, hash1, files[0]));
 0280            paths.Add(new AssetPath(basePath, hash2, files[1]));
 0281            paths.Add(new AssetPath(basePath, hash3, files[2]));
 0282            paths.Add(new AssetPath(basePath, hash4, files[3]));
 283
 0284            var buffers = core.DumpRawAssets(paths);
 285
 0286            LogAssert.Expect(LogType.Error, new Regex(@"^.*?Download failed"));
 0287            LogAssert.Expect(LogType.Error, new Regex(@"^.*?QmHash4"));
 288
 0289            Assert.AreEqual(3, buffers.Count);
 290
 291            //NOTE(Brian): textures exist?
 0292            Assert.IsTrue(env.file.Exists(paths[0].finalPath));
 0293            Assert.IsTrue(env.file.Exists(paths[1].finalPath));
 0294            Assert.IsTrue(env.file.Exists(paths[2].finalPath));
 0295            Assert.IsFalse(env.file.Exists(paths[3].finalPath));
 296
 297            //NOTE(Brian): textures .meta exist?
 0298            Assert.IsTrue(env.file.Exists(paths[0].finalMetaPath));
 0299            Assert.IsTrue(env.file.Exists(paths[1].finalMetaPath));
 0300            Assert.IsTrue(env.file.Exists(paths[2].finalMetaPath));
 0301            Assert.IsFalse(env.file.Exists(paths[3].finalMetaPath));
 0302        }
 303
 304        [Test]
 305        public void DownloadAssetCorrectly()
 306        {
 0307            AssetPath path = new AssetPath(
 308                basePath: basePath,
 309                hash: hash1,
 310                file: "texture.png"
 311            );
 312
 0313            string output = core.DownloadAsset(path);
 314
 0315            UnityEngine.Assertions.Assert.IsTrue(env.file.Exists(path.finalPath));
 0316            UnityEngine.Assertions.Assert.IsTrue(env.file.Exists(path.finalMetaPath));
 0317            UnityEngine.Assertions.Assert.AreEqual(contentData1, env.file.ReadAllText(output));
 0318        }
 319
 320        [UnityTest]
 321        public IEnumerator ConvertAssetsWithExternalTextures()
 322        {
 0323            ContentServerUtils.MappingPair[] input =
 324            {
 325                new ContentServerUtils.MappingPair { file = "SimpleCubeWithSharedNormal.gltf", hash = "SimpleCubeWithSha
 326                new ContentServerUtils.MappingPair { file = "SimpleCubeWithSharedNormal.bin", hash = "SimpleCubeWithShar
 327                new ContentServerUtils.MappingPair { file = "Textures/Test.png", hash = "Test.png" }
 328            };
 329
 0330            core.settings.baseUrl = TestAssetsUtils.GetPath() + "/GLTF/SimpleCube/";
 331
 0332            env = ABConverter.Environment.CreateWithDefaultImplementations();
 0333            core = new ABConverter.Core(env, core.settings);
 334
 0335            core.Convert(input);
 336
 0337            yield return new WaitUntil(() => core.state.step == ABConverter.Core.State.Step.FINISHED);
 338
 0339            Assert.IsTrue(core.state.lastErrorCode == ABConverter.Core.ErrorCodes.SUCCESS);
 340
 0341            AssetBundle abDependency = AssetBundle.LoadFromFile(ABConverter.Config.ASSET_BUNDLES_PATH_ROOT + "/Test.png"
 0342            abDependency.LoadAllAssets();
 343
 0344            AssetBundle abMain = AssetBundle.LoadFromFile(ABConverter.Config.ASSET_BUNDLES_PATH_ROOT + "/SimpleCubeWithS
 0345            Material[] mats = abMain.LoadAllAssets<Material>();
 346
 0347            bool hasMap = false;
 348
 0349            foreach (var mat in mats)
 350            {
 0351                hasMap = mat.GetTexture("_BaseMap") != null;
 352            }
 353
 0354            abMain.Unload(true);
 0355            abDependency.Unload(true);
 356
 0357            Assert.IsTrue(hasMap, "Dependency has NOT been generated correctly!");
 0358        }
 359
 360        [UnityTest]
 361        public IEnumerator NotGenerateColorMapsWithDXTnm()
 362        {
 0363            ContentServerUtils.MappingPair[] input =
 364            {
 365                new ContentServerUtils.MappingPair { file = "SimpleCubeWithSharedNormal.gltf", hash = "SimpleCubeWithSha
 366                new ContentServerUtils.MappingPair { file = "SimpleCubeWithSharedNormal.bin", hash = "SimpleCubeWithShar
 367                new ContentServerUtils.MappingPair { file = "Textures/Test.png", hash = "Test.png" }
 368            };
 369
 0370            core.settings.baseUrl = TestAssetsUtils.GetPath() + "/GLTF/SimpleCube/";
 0371            core.settings.verbose = true;
 0372            core.settings.dumpOnly = true;
 0373            core.settings.deleteDownloadPathAfterFinished = false;
 374
 0375            env = ABConverter.Environment.CreateWithDefaultImplementations();
 0376            core = new ABConverter.Core(env, core.settings);
 377
 0378            core.Convert(input);
 379
 0380            yield return new WaitUntil(() => core.state.step == ABConverter.Core.State.Step.FINISHED);
 381
 0382            Assert.IsTrue(core.state.lastErrorCode == ABConverter.Core.ErrorCodes.SUCCESS);
 383
 0384            string importerPath = $"{core.finalDownloadedPath}Test.png{ABConverter.Config.DASH}Test.png.png";
 0385            TextureImporter importer = env.assetDatabase.GetImporterAtPath(importerPath) as TextureImporter;
 386
 0387            Assert.IsTrue(importer != null, "Texture importer is null!");
 0388            Assert.IsTrue(TextureImporterType.NormalMap != importer.textureType, "Texture is used for color! It shouldn'
 0389        }
 390
 391        [UnityTest]
 392        public IEnumerator NotFailIfExternalTexturesAreMissing()
 393        {
 0394            ContentServerUtils.MappingPair[] input =
 395            {
 396                new ContentServerUtils.MappingPair { file = "SimpleCubeWithSharedNormal.gltf", hash = "SimpleCubeWithSha
 397                new ContentServerUtils.MappingPair { file = "SimpleCubeWithSharedNormal.bin", hash = "SimpleCubeWithShar
 398            };
 399
 0400            core.settings.baseUrl = TestAssetsUtils.GetPath() + "/GLTF/SimpleCube/";
 401
 0402            env = ABConverter.Environment.CreateWithDefaultImplementations();
 0403            core = new ABConverter.Core(env, core.settings);
 404
 0405            core.Convert(input);
 406
 0407            yield return new WaitUntil(() => core.state.step == ABConverter.Core.State.Step.FINISHED);
 408
 0409            Assert.IsTrue(core.state.lastErrorCode == ABConverter.Core.ErrorCodes.SUCCESS);
 0410            LogAssert.Expect(LogType.Error, new Regex(@"^.*?Buffer file not found"));
 0411            LogAssert.Expect(LogType.Error, new Regex(@"^.*?Buffer file not found"));
 0412        }
 413
 414        void ResetCacheAndWorkingFolders()
 415        {
 0416            Caching.ClearCache();
 417
 0418            if (Directory.Exists(ABConverter.Config.ASSET_BUNDLES_PATH_ROOT))
 0419                Directory.Delete(ABConverter.Config.ASSET_BUNDLES_PATH_ROOT, true);
 420
 0421            if (Directory.Exists(ABConverter.Config.DOWNLOADED_PATH_ROOT))
 0422                Directory.Delete(ABConverter.Config.DOWNLOADED_PATH_ROOT, true);
 423
 0424            if (File.Exists(ABConverter.Config.DOWNLOADED_PATH_ROOT + ".meta"))
 0425                File.Delete(ABConverter.Config.DOWNLOADED_PATH_ROOT + ".meta");
 426
 0427            UnityEditor.AssetDatabase.Refresh();
 0428        }
 429    }
 430}