< Summary

Class:AssetPromiseKeeper_AssetBundle_GameObject_Tests.BlockedAndMasterPromisesShould
Assembly:AssetPromiseKeeper_AssetBundle_GameObject_Tests
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/AssetManager/AssetBundles/AB_GameObject/Tests/BlockedAndMasterPromisesShould.cs
Covered lines:71
Uncovered lines:0
Coverable lines:71
Total lines:131
Line coverage:100% (71 of 71)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
CreatePromise(...)0%220100%
TearDown()0%330100%
SucceedWhenMastersParentIsDestroyed()0%550100%
FailCorrectlyWhenGivenWrongURL()0%550100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/AssetManager/AssetBundles/AB_GameObject/Tests/BlockedAndMasterPromisesShould.cs

#LineLine coverage
 1using AssetPromiseKeeper_Tests;
 2using DCL;
 3using DCL.Helpers;
 4using System.Collections;
 5using UnityEngine;
 6using UnityEngine.Assertions;
 7using UnityEngine.TestTools;
 8
 9namespace AssetPromiseKeeper_AssetBundle_GameObject_Tests
 10{
 11    public class BlockedAndMasterPromisesShould : TestsBase_APK<AssetPromiseKeeper_AB_GameObject,
 12                                                AssetPromise_AB_GameObject,
 13                                                Asset_AB_GameObject,
 14                                                AssetLibrary_AB_GameObject>
 15    {
 16        protected AssetPromise_AB_GameObject CreatePromise(string hash = null)
 17        {
 618            string contentUrl = TestAssetsUtils.GetPath() + "/AssetBundles/";
 619            hash = hash ?? "QmNS4K7GaH63T9rhAfkrra7ADLXSEeco8FTGknkPnAVmKM";
 620            var prom = new AssetPromise_AB_GameObject(contentUrl, hash);
 621            return prom;
 22        }
 23
 24        protected override IEnumerator TearDown()
 25        {
 226            yield return base.TearDown();
 27
 28            // AssetPromise_AB_GameObject promises are wired to AssetPromiseKeeper_AB.
 29            // TODO(Brian): Pass the APK instance by ref to properly test this without this line.
 230            AssetPromiseKeeper_AB.i.Cleanup();
 231        }
 32
 33        /// <summary>
 34        /// If this test fails, you should ensure that OnSilentForget is implemented properly.
 35        /// OnSilentForget should unparent and hide the container to ensure blocked promises finish correctly.
 36        /// </summary>
 37        [UnityTest]
 38        public IEnumerator SucceedWhenMastersParentIsDestroyed()
 39        {
 140            GameObject parent = new GameObject("parent");
 41
 142            var prom = CreatePromise();
 143            prom.settings.parent = parent.transform;
 44
 145            var prom2 = CreatePromise();
 146            bool failEventCalled2 = false;
 147            prom2.OnFailEvent += (x) => { failEventCalled2 = true; };
 48
 149            var prom3 = CreatePromise();
 150            bool failEventCalled3 = false;
 151            prom3.OnFailEvent += (x) => { failEventCalled3 = true; };
 52
 153            keeper.Keep(prom);
 154            keeper.Keep(prom2);
 155            keeper.Keep(prom3);
 56
 157            keeper.Forget(prom);
 58
 159            Assert.AreEqual(3, keeper.waitingPromisesCount);
 60
 161            Object.Destroy(parent);
 62
 163            yield return prom;
 164            yield return prom2;
 165            yield return prom3;
 66
 167            Assert.AreEqual(AssetPromiseState.FINISHED, prom.state);
 168            Assert.AreEqual(AssetPromiseState.FINISHED, prom2.state);
 169            Assert.AreEqual(AssetPromiseState.FINISHED, prom3.state);
 70
 171            Assert.IsFalse(failEventCalled2);
 172            Assert.IsFalse(failEventCalled3);
 73
 174            Assert.IsTrue(prom.asset != null);
 175            Assert.IsTrue(prom2.asset != null);
 176            Assert.IsTrue(prom3.asset != null);
 77
 178            Assert.IsTrue(keeper.library.Contains(prom.asset));
 179            Assert.AreEqual(1, keeper.library.masterAssets.Count);
 180        }
 81
 82        [UnityTest]
 83        public IEnumerator FailCorrectlyWhenGivenWrongURL()
 84        {
 185            string invalidHash = "Qm_InVaLiD_hAsH";
 86
 187            var prom = CreatePromise(invalidHash);
 188            Asset_AB_GameObject asset = null;
 189            bool failEventCalled1 = false;
 190            prom.OnSuccessEvent += (x) => { asset = x; };
 391            prom.OnFailEvent += (x) => { failEventCalled1 = true; };
 92
 193            var prom2 = CreatePromise(invalidHash);
 194            Asset_AB_GameObject asset2 = null;
 195            bool failEventCalled2 = false;
 196            prom2.OnSuccessEvent += (x) => { asset2 = x; };
 397            prom2.OnFailEvent += (x) => { failEventCalled2 = true; };
 98
 199            var prom3 = CreatePromise(invalidHash);
 1100            Asset_AB_GameObject asset3 = null;
 1101            bool failEventCalled3 = false;
 1102            prom3.OnSuccessEvent += (x) => { asset3 = x; };
 3103            prom3.OnFailEvent += (x) => { failEventCalled3 = true; };
 104
 1105            keeper.Keep(prom);
 1106            keeper.Keep(prom2);
 1107            keeper.Keep(prom3);
 108
 1109            Assert.AreEqual(3, keeper.waitingPromisesCount);
 110
 1111            yield return prom;
 1112            yield return prom2;
 1113            yield return prom3;
 114
 1115            Assert.AreNotEqual(AssetPromiseState.FINISHED, prom.state);
 1116            Assert.AreNotEqual(AssetPromiseState.FINISHED, prom2.state);
 1117            Assert.AreNotEqual(AssetPromiseState.FINISHED, prom3.state);
 118
 1119            Assert.IsTrue(failEventCalled1);
 1120            Assert.IsTrue(failEventCalled2);
 1121            Assert.IsTrue(failEventCalled3);
 122
 1123            Assert.IsFalse(asset != null);
 1124            Assert.IsFalse(asset2 != null);
 1125            Assert.IsFalse(asset3 != null);
 126
 1127            Assert.IsFalse(keeper.library.Contains(asset));
 1128            Assert.AreNotEqual(1, keeper.library.masterAssets.Count);
 1129        }
 130    }
 131}