| | 1 | | using AssetPromiseKeeper_Tests; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using System.Collections; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using UnityEngine; |
| | 7 | | using UnityEngine.Assertions; |
| | 8 | | using UnityEngine.TestTools; |
| | 9 | |
|
| | 10 | | namespace AssetPromiseKeeper_AssetBundle_GameObject_Tests |
| | 11 | | { |
| | 12 | | public class APK_AB_GameObject_Promise_Should : TestsBase_APK<AssetPromiseKeeper_AB_GameObject, |
| | 13 | | AssetPromise_AB_GameObject, |
| | 14 | | Asset_AB_GameObject, |
| | 15 | | AssetLibrary_AB_GameObject> |
| | 16 | | { |
| | 17 | | protected AssetPromise_AB_GameObject CreatePromise(string hash = null) |
| | 18 | | { |
| 24 | 19 | | string contentUrl = TestAssetsUtils.GetPath() + "/AssetBundles/"; |
| 24 | 20 | | hash = hash ?? "QmNS4K7GaH63T9rhAfkrra7ADLXSEeco8FTGknkPnAVmKM"; |
| 24 | 21 | | var prom = new AssetPromise_AB_GameObject(contentUrl, hash); |
| 24 | 22 | | return prom; |
| | 23 | | } |
| | 24 | |
|
| | 25 | |
|
| | 26 | | [UnityTest] |
| | 27 | | public IEnumerator BeSetupCorrectlyAfterLoad() |
| | 28 | | { |
| 1 | 29 | | var prom = CreatePromise(); |
| 1 | 30 | | Asset_AB_GameObject loadedAsset = null; |
| | 31 | |
|
| 1 | 32 | | prom.OnSuccessEvent += |
| | 33 | | (x) => |
| | 34 | | { |
| 1 | 35 | | Debug.Log("Success is called"); |
| 1 | 36 | | loadedAsset = x; |
| 1 | 37 | | } |
| | 38 | | ; |
| | 39 | |
|
| 1 | 40 | | prom.OnFailEvent += (x) => { Debug.Log("Fail is called"); }; |
| | 41 | |
|
| 1 | 42 | | Vector3 initialPos = Vector3.one; |
| 1 | 43 | | Quaternion initialRot = Quaternion.LookRotation(Vector3.right, Vector3.up); |
| 1 | 44 | | Vector3 initialScale = Vector3.one * 2; |
| | 45 | |
|
| 1 | 46 | | prom.settings.initialLocalPosition = initialPos; |
| 1 | 47 | | prom.settings.initialLocalRotation = initialRot; |
| 1 | 48 | | prom.settings.initialLocalScale = initialScale; |
| | 49 | |
|
| 1 | 50 | | keeper.Keep(prom); |
| | 51 | |
|
| 1 | 52 | | yield return prom; |
| | 53 | |
|
| 1 | 54 | | Assert.IsTrue(PoolManager.i.ContainsPool(loadedAsset.id), "Not in pool after loaded!"); |
| | 55 | |
|
| 1 | 56 | | Pool pool = PoolManager.i.GetPool(loadedAsset.id); |
| | 57 | |
|
| 1 | 58 | | Assert.AreEqual(0, pool.unusedObjectsCount, "incorrect inactive objects in pool"); |
| 1 | 59 | | Assert.AreEqual(1, pool.usedObjectsCount, "incorrect active objects in pool"); |
| 1 | 60 | | Assert.IsTrue(pool.original != loadedAsset.container, "In pool, the original gameObject must NOT be the load |
| | 61 | |
|
| | 62 | | //NOTE(Brian): If the following asserts fail, check that ApplySettings_LoadStart() is called from AssetPromi |
| 1 | 63 | | Assert.AreEqual(initialPos.ToString(), loadedAsset.container.transform.localPosition.ToString(), "initial po |
| 1 | 64 | | Assert.AreEqual(initialRot.ToString(), loadedAsset.container.transform.localRotation.ToString(), "initial ro |
| 1 | 65 | | Assert.AreEqual(initialScale.ToString(), loadedAsset.container.transform.localScale.ToString(), "initial sca |
| | 66 | |
|
| 1 | 67 | | Assert.IsTrue(loadedAsset != null); |
| 1 | 68 | | Assert.IsTrue(keeper.library.Contains(loadedAsset)); |
| 1 | 69 | | Assert.AreEqual(1, keeper.library.masterAssets.Count); |
| 1 | 70 | | } |
| | 71 | |
|
| | 72 | | [UnityTest] |
| | 73 | | public IEnumerator ForceNewInstanceIsOff() |
| | 74 | | { |
| 1 | 75 | | var prom = CreatePromise(); |
| 1 | 76 | | prom.settings.forceNewInstance = false; |
| 1 | 77 | | keeper.Keep(prom); |
| 1 | 78 | | yield return prom; |
| | 79 | |
|
| 1 | 80 | | var poolableObjectComponent = PoolManager.i.GetPoolable(prom.asset.container); |
| 1 | 81 | | Assert.IsNotNull(poolableObjectComponent); |
| 1 | 82 | | } |
| | 83 | |
|
| | 84 | | [UnityTest] |
| | 85 | | public IEnumerator ForceNewInstanceIsOffMultipleTimes() |
| | 86 | | { |
| 1 | 87 | | var poolableComponents = new List<PoolableObject>(); |
| | 88 | |
|
| 22 | 89 | | for (int i = 0; i < 10; i++) |
| | 90 | | { |
| 10 | 91 | | var prom = CreatePromise(); |
| 10 | 92 | | prom.settings.forceNewInstance = false; |
| 10 | 93 | | keeper.Keep(prom); |
| 10 | 94 | | yield return prom; |
| 10 | 95 | | poolableComponents.Add(PoolManager.i.GetPoolable(prom.asset.container)); |
| 10 | 96 | | keeper.Forget(prom); |
| 10 | 97 | | } |
| | 98 | |
|
| 11 | 99 | | Assert.IsTrue(poolableComponents.TrueForAll(x => x != null)); |
| 1 | 100 | | } |
| | 101 | |
|
| | 102 | | [UnityTest] |
| | 103 | | public IEnumerator ForceNewInstanceIsOn() |
| | 104 | | { |
| 1 | 105 | | var prom = CreatePromise(); |
| 1 | 106 | | prom.settings.forceNewInstance = true; |
| 1 | 107 | | keeper.Keep(prom); |
| 1 | 108 | | yield return prom; |
| | 109 | |
|
| 1 | 110 | | var poolableObjectComponent = PoolManager.i.GetPoolable(prom.asset.container); |
| 1 | 111 | | Assert.IsNull(poolableObjectComponent); |
| 1 | 112 | | } |
| | 113 | |
|
| | 114 | | [UnityTest] |
| | 115 | | public IEnumerator ForceNewInstanceIsOnMultipleTimes() |
| | 116 | | { |
| 1 | 117 | | var poolableComponents = new List<PoolableObject>(); |
| | 118 | |
|
| 22 | 119 | | for (int i = 0; i < 10; i++) |
| | 120 | | { |
| 10 | 121 | | var prom = CreatePromise(); |
| 10 | 122 | | prom.settings.forceNewInstance = true; |
| 10 | 123 | | keeper.Keep(prom); |
| 10 | 124 | | yield return prom; |
| 10 | 125 | | poolableComponents.Add(PoolManager.i.GetPoolable(prom.asset.container)); |
| 10 | 126 | | keeper.Forget(prom); |
| 10 | 127 | | } |
| | 128 | |
|
| 11 | 129 | | Assert.IsTrue(poolableComponents.TrueForAll(x => x == null)); |
| 1 | 130 | | } |
| | 131 | |
|
| | 132 | |
|
| | 133 | | [UnityTest] |
| | 134 | | public IEnumerator FailCorrectlyWhenLoadingABWithoutGameObjects() |
| | 135 | | { |
| 1 | 136 | | var prom = CreatePromise("Broken_paladin_AB"); |
| | 137 | |
|
| 1 | 138 | | bool failed = false; |
| 3 | 139 | | prom.OnFailEvent += (x) => { failed = true; }; |
| 1 | 140 | | keeper.Keep(prom); |
| | 141 | |
|
| 1 | 142 | | yield return prom; |
| | 143 | |
|
| 1 | 144 | | Assert.IsTrue(failed == true, "The broken paladin AssetBundle contains no GameObjects and should fail!"); |
| 1 | 145 | | } |
| | 146 | | } |
| | 147 | | } |