| | 1 | | using AssetPromiseKeeper_Tests; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using System.Collections; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.Assertions; |
| | 7 | | using UnityEngine.TestTools; |
| | 8 | |
|
| | 9 | | namespace 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 | | { |
| 6 | 18 | | string contentUrl = TestAssetsUtils.GetPath() + "/AssetBundles/"; |
| 6 | 19 | | hash = hash ?? "QmNS4K7GaH63T9rhAfkrra7ADLXSEeco8FTGknkPnAVmKM"; |
| 6 | 20 | | var prom = new AssetPromise_AB_GameObject(contentUrl, hash); |
| 6 | 21 | | return prom; |
| | 22 | | } |
| | 23 | |
|
| | 24 | | protected override IEnumerator TearDown() |
| | 25 | | { |
| 2 | 26 | | 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. |
| 2 | 30 | | AssetPromiseKeeper_AB.i.Cleanup(); |
| 2 | 31 | | } |
| | 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 | | { |
| 1 | 40 | | GameObject parent = new GameObject("parent"); |
| | 41 | |
|
| 1 | 42 | | var prom = CreatePromise(); |
| 1 | 43 | | prom.settings.parent = parent.transform; |
| | 44 | |
|
| 1 | 45 | | var prom2 = CreatePromise(); |
| 1 | 46 | | bool failEventCalled2 = false; |
| 1 | 47 | | prom2.OnFailEvent += (x) => { failEventCalled2 = true; }; |
| | 48 | |
|
| 1 | 49 | | var prom3 = CreatePromise(); |
| 1 | 50 | | bool failEventCalled3 = false; |
| 1 | 51 | | prom3.OnFailEvent += (x) => { failEventCalled3 = true; }; |
| | 52 | |
|
| 1 | 53 | | keeper.Keep(prom); |
| 1 | 54 | | keeper.Keep(prom2); |
| 1 | 55 | | keeper.Keep(prom3); |
| | 56 | |
|
| 1 | 57 | | keeper.Forget(prom); |
| | 58 | |
|
| 1 | 59 | | Assert.AreEqual(3, keeper.waitingPromisesCount); |
| | 60 | |
|
| 1 | 61 | | Object.Destroy(parent); |
| | 62 | |
|
| 1 | 63 | | yield return prom; |
| 1 | 64 | | yield return prom2; |
| 1 | 65 | | yield return prom3; |
| | 66 | |
|
| 1 | 67 | | Assert.AreEqual(AssetPromiseState.FINISHED, prom.state); |
| 1 | 68 | | Assert.AreEqual(AssetPromiseState.FINISHED, prom2.state); |
| 1 | 69 | | Assert.AreEqual(AssetPromiseState.FINISHED, prom3.state); |
| | 70 | |
|
| 1 | 71 | | Assert.IsFalse(failEventCalled2); |
| 1 | 72 | | Assert.IsFalse(failEventCalled3); |
| | 73 | |
|
| 1 | 74 | | Assert.IsTrue(prom.asset != null); |
| 1 | 75 | | Assert.IsTrue(prom2.asset != null); |
| 1 | 76 | | Assert.IsTrue(prom3.asset != null); |
| | 77 | |
|
| 1 | 78 | | Assert.IsTrue(keeper.library.Contains(prom.asset)); |
| 1 | 79 | | Assert.AreEqual(1, keeper.library.masterAssets.Count); |
| 1 | 80 | | } |
| | 81 | |
|
| | 82 | | [UnityTest] |
| | 83 | | public IEnumerator FailCorrectlyWhenGivenWrongURL() |
| | 84 | | { |
| 1 | 85 | | string invalidHash = "Qm_InVaLiD_hAsH"; |
| | 86 | |
|
| 1 | 87 | | var prom = CreatePromise(invalidHash); |
| 1 | 88 | | Asset_AB_GameObject asset = null; |
| 1 | 89 | | bool failEventCalled1 = false; |
| 1 | 90 | | prom.OnSuccessEvent += (x) => { asset = x; }; |
| 3 | 91 | | prom.OnFailEvent += (x) => { failEventCalled1 = true; }; |
| | 92 | |
|
| 1 | 93 | | var prom2 = CreatePromise(invalidHash); |
| 1 | 94 | | Asset_AB_GameObject asset2 = null; |
| 1 | 95 | | bool failEventCalled2 = false; |
| 1 | 96 | | prom2.OnSuccessEvent += (x) => { asset2 = x; }; |
| 3 | 97 | | prom2.OnFailEvent += (x) => { failEventCalled2 = true; }; |
| | 98 | |
|
| 1 | 99 | | var prom3 = CreatePromise(invalidHash); |
| 1 | 100 | | Asset_AB_GameObject asset3 = null; |
| 1 | 101 | | bool failEventCalled3 = false; |
| 1 | 102 | | prom3.OnSuccessEvent += (x) => { asset3 = x; }; |
| 3 | 103 | | prom3.OnFailEvent += (x) => { failEventCalled3 = true; }; |
| | 104 | |
|
| 1 | 105 | | keeper.Keep(prom); |
| 1 | 106 | | keeper.Keep(prom2); |
| 1 | 107 | | keeper.Keep(prom3); |
| | 108 | |
|
| 1 | 109 | | Assert.AreEqual(3, keeper.waitingPromisesCount); |
| | 110 | |
|
| 1 | 111 | | yield return prom; |
| 1 | 112 | | yield return prom2; |
| 1 | 113 | | yield return prom3; |
| | 114 | |
|
| 1 | 115 | | Assert.AreNotEqual(AssetPromiseState.FINISHED, prom.state); |
| 1 | 116 | | Assert.AreNotEqual(AssetPromiseState.FINISHED, prom2.state); |
| 1 | 117 | | Assert.AreNotEqual(AssetPromiseState.FINISHED, prom3.state); |
| | 118 | |
|
| 1 | 119 | | Assert.IsTrue(failEventCalled1); |
| 1 | 120 | | Assert.IsTrue(failEventCalled2); |
| 1 | 121 | | Assert.IsTrue(failEventCalled3); |
| | 122 | |
|
| 1 | 123 | | Assert.IsFalse(asset != null); |
| 1 | 124 | | Assert.IsFalse(asset2 != null); |
| 1 | 125 | | Assert.IsFalse(asset3 != null); |
| | 126 | |
|
| 1 | 127 | | Assert.IsFalse(keeper.library.Contains(asset)); |
| 1 | 128 | | Assert.AreNotEqual(1, keeper.library.masterAssets.Count); |
| 1 | 129 | | } |
| | 130 | | } |
| | 131 | | } |