| | 1 | | using System; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using System.Collections; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Linq; |
| | 7 | | using UnityEngine; |
| | 8 | | using UnityEngine.Assertions; |
| | 9 | | using UnityEngine.TestTools; |
| | 10 | | using Object = UnityEngine.Object; |
| | 11 | |
|
| | 12 | | namespace AssetPromiseKeeper_GLTF_Tests |
| | 13 | | { |
| | 14 | | public class AnyAssetPromiseShould : IntegrationTestSuite_Legacy |
| | 15 | | { |
| | 16 | | [UnityTest] |
| | 17 | | public IEnumerator BeSetupCorrectlyAfterLoad() |
| | 18 | | { |
| 1 | 19 | | var keeper = new AssetPromiseKeeper_GLTF(); |
| | 20 | |
|
| 1 | 21 | | string url = TestAssetsUtils.GetPath() + "/GLB/Lantern/Lantern.glb"; |
| 1 | 22 | | AssetPromise_GLTF prom = new AssetPromise_GLTF(scene.contentProvider, url); |
| 1 | 23 | | Asset_GLTF loadedAsset = null; |
| | 24 | |
|
| | 25 | |
|
| 1 | 26 | | prom.OnSuccessEvent += |
| 2 | 27 | | (x) => { loadedAsset = x; } |
| | 28 | | ; |
| | 29 | |
|
| 1 | 30 | | Vector3 initialPos = Vector3.one; |
| 1 | 31 | | Quaternion initialRot = Quaternion.LookRotation(Vector3.right, Vector3.up); |
| 1 | 32 | | Vector3 initialScale = Vector3.one * 2; |
| | 33 | |
|
| 1 | 34 | | prom.settings.initialLocalPosition = initialPos; |
| 1 | 35 | | prom.settings.initialLocalRotation = initialRot; |
| 1 | 36 | | prom.settings.initialLocalScale = initialScale; |
| | 37 | |
|
| 1 | 38 | | keeper.Keep(prom); |
| | 39 | |
|
| 1 | 40 | | yield return prom; |
| | 41 | |
|
| 1 | 42 | | Assert.IsTrue(PoolManager.i.ContainsPool(loadedAsset.id), "Not in pool after loaded!"); |
| | 43 | |
|
| 1 | 44 | | Pool pool = PoolManager.i.GetPool(loadedAsset.id); |
| | 45 | |
|
| 1 | 46 | | Assert.AreEqual(0, pool.unusedObjectsCount, "incorrect inactive objects in pool"); |
| 1 | 47 | | Assert.AreEqual(1, pool.usedObjectsCount, "incorrect active objects in pool"); |
| 1 | 48 | | Assert.IsTrue(pool.original != loadedAsset.container, "In pool, the original gameObject must NOT be the load |
| | 49 | |
|
| | 50 | | //NOTE(Brian): If the following asserts fail, check that ApplySettings_LoadStart() is called from AssetPromi |
| 1 | 51 | | Assert.AreEqual(initialPos.ToString(), loadedAsset.container.transform.localPosition.ToString(), "initial po |
| 1 | 52 | | Assert.AreEqual(initialRot.ToString(), loadedAsset.container.transform.localRotation.ToString(), "initial ro |
| 1 | 53 | | Assert.AreEqual(initialScale.ToString(), loadedAsset.container.transform.localScale.ToString(), "initial sca |
| | 54 | |
|
| 1 | 55 | | Assert.IsTrue(loadedAsset != null); |
| 1 | 56 | | Assert.IsTrue(keeper.library.Contains(loadedAsset)); |
| 1 | 57 | | Assert.AreEqual(1, keeper.library.masterAssets.Count); |
| 1 | 58 | | keeper.Cleanup(); |
| 1 | 59 | | } |
| | 60 | |
|
| | 61 | | [UnityTest] |
| | 62 | | public IEnumerator ForceNewInstanceIsOff() |
| | 63 | | { |
| 1 | 64 | | var keeper = new AssetPromiseKeeper_GLTF(); |
| | 65 | |
|
| 1 | 66 | | string url = TestAssetsUtils.GetPath() + "/GLB/Lantern/Lantern.glb"; |
| 1 | 67 | | AssetPromise_GLTF prom = new AssetPromise_GLTF(scene.contentProvider, url); |
| 1 | 68 | | prom.settings.forceNewInstance = false; |
| 1 | 69 | | keeper.Keep(prom); |
| 1 | 70 | | yield return prom; |
| | 71 | |
|
| 1 | 72 | | var poolableObjectComponent = PoolManager.i.GetPoolable(prom.asset.container); |
| 1 | 73 | | Assert.IsNotNull(poolableObjectComponent); |
| 1 | 74 | | keeper.Cleanup(); |
| 1 | 75 | | } |
| | 76 | |
|
| | 77 | | [UnityTest] |
| | 78 | | public IEnumerator ForceNewInstanceIsOffMultipleTimes() |
| | 79 | | { |
| 1 | 80 | | var keeper = new AssetPromiseKeeper_GLTF(); |
| | 81 | |
|
| 1 | 82 | | var poolableComponents = new List<PoolableObject>(); |
| | 83 | |
|
| 1 | 84 | | string url = TestAssetsUtils.GetPath() + "/GLB/Lantern/Lantern.glb"; |
| 22 | 85 | | for (int i = 0; i < 10; i++) |
| | 86 | | { |
| 10 | 87 | | AssetPromise_GLTF prom = new AssetPromise_GLTF(scene.contentProvider, url); |
| 10 | 88 | | prom.settings.forceNewInstance = false; |
| 10 | 89 | | keeper.Keep(prom); |
| 10 | 90 | | yield return prom; |
| 10 | 91 | | poolableComponents.Add(PoolManager.i.GetPoolable(prom.asset.container)); |
| 10 | 92 | | keeper.Forget(prom); |
| 10 | 93 | | } |
| | 94 | |
|
| 11 | 95 | | Assert.IsTrue(poolableComponents.TrueForAll(x => x != null)); |
| 1 | 96 | | keeper.Cleanup(); |
| 1 | 97 | | } |
| | 98 | |
|
| | 99 | | [UnityTest] |
| | 100 | | public IEnumerator ForceNewInstanceIsOn() |
| | 101 | | { |
| 1 | 102 | | var keeper = new AssetPromiseKeeper_GLTF(); |
| | 103 | |
|
| 1 | 104 | | string url = TestAssetsUtils.GetPath() + "/GLB/Lantern/Lantern.glb"; |
| 1 | 105 | | AssetPromise_GLTF prom = new AssetPromise_GLTF(scene.contentProvider, url); |
| 1 | 106 | | prom.settings.forceNewInstance = true; |
| | 107 | |
|
| 1 | 108 | | keeper.Keep(prom); |
| 1 | 109 | | yield return prom; |
| | 110 | |
|
| 1 | 111 | | var poolableObjectComponent = PoolManager.i.GetPoolable(prom.asset.container); |
| 1 | 112 | | Assert.IsNull(poolableObjectComponent); |
| | 113 | |
|
| 1 | 114 | | if (prom.asset.container != null) |
| 1 | 115 | | Object.Destroy(prom.asset.container); |
| | 116 | |
|
| 1 | 117 | | keeper.Cleanup(); |
| 1 | 118 | | } |
| | 119 | |
|
| | 120 | | [UnityTest] |
| | 121 | | public IEnumerator ForceNewInstanceIsOnMultipleTimes() |
| | 122 | | { |
| 1 | 123 | | var keeper = new AssetPromiseKeeper_GLTF(); |
| | 124 | |
|
| 1 | 125 | | var poolableComponents = new List<PoolableObject>(); |
| | 126 | |
|
| 1 | 127 | | string url = TestAssetsUtils.GetPath() + "/GLB/Lantern/Lantern.glb"; |
| | 128 | |
|
| 22 | 129 | | for (int i = 0; i < 10; i++) |
| | 130 | | { |
| 10 | 131 | | AssetPromise_GLTF prom = new AssetPromise_GLTF(scene.contentProvider, url); |
| 10 | 132 | | prom.settings.forceNewInstance = true; |
| 10 | 133 | | keeper.Keep(prom); |
| 10 | 134 | | yield return prom; |
| 10 | 135 | | poolableComponents.Add(PoolManager.i.GetPoolable(prom.asset.container)); |
| 10 | 136 | | keeper.Forget(prom); |
| 10 | 137 | | } |
| | 138 | |
|
| 11 | 139 | | Assert.IsTrue(poolableComponents.TrueForAll(x => x == null)); |
| | 140 | |
|
| 1 | 141 | | keeper.Cleanup(); |
| 1 | 142 | | } |
| | 143 | |
|
| | 144 | | [UnityTest] |
| | 145 | | public IEnumerator NotTryToLoadAfterForget() |
| | 146 | | { |
| 1 | 147 | | var keeper = new AssetPromiseKeeper_GLTF(); |
| | 148 | |
|
| 1 | 149 | | var promises = new List<AssetPromise_GLTF>(); |
| 1 | 150 | | var forgottenPromises = new Dictionary<int, bool>(); |
| 1 | 151 | | bool waitMasterPromise = true; |
| | 152 | |
|
| 1 | 153 | | string url = TestAssetsUtils.GetPath() + "/GLB/Trunk/Trunk.glb"; |
| | 154 | |
|
| 1 | 155 | | AssetPromise_GLTF masterPromise = new AssetPromise_GLTF(scene.contentProvider, url); |
| 2 | 156 | | masterPromise.OnPreFinishEvent += promise => waitMasterPromise = false; |
| 1 | 157 | | keeper.Keep(masterPromise); |
| | 158 | |
|
| 22 | 159 | | for (int i = 0; i < 10; i++) |
| | 160 | | { |
| 10 | 161 | | AssetPromise_GLTF prom = new AssetPromise_GLTF(scene.contentProvider, url); |
| | 162 | |
|
| 10 | 163 | | promises.Add(prom); |
| | 164 | |
|
| 10 | 165 | | int promiseHash = prom.GetHashCode(); |
| 10 | 166 | | forgottenPromises.Add(promiseHash, false); |
| | 167 | |
|
| 10 | 168 | | prom.OnSuccessEvent += (asset) => |
| | 169 | | { |
| 0 | 170 | | Assert.IsFalse(forgottenPromises[promiseHash], "Success on forgotten promise shouldn't be called"); |
| 0 | 171 | | }; |
| 10 | 172 | | prom.OnFailEvent += (asset) => |
| | 173 | | { |
| 0 | 174 | | Assert.IsFalse(forgottenPromises[promiseHash], "Fail on forgotten promise shouldn't be called"); |
| 0 | 175 | | }; |
| 10 | 176 | | keeper.Keep(prom); |
| | 177 | | } |
| | 178 | |
|
| 1 | 179 | | keeper.Forget(masterPromise); |
| | 180 | |
|
| 166 | 181 | | yield return new WaitWhile(() => waitMasterPromise); |
| 1 | 182 | | yield return null; |
| | 183 | |
|
| 22 | 184 | | for (int i = 0; i < promises.Count; i++) |
| | 185 | | { |
| 10 | 186 | | var prom = promises[i]; |
| 10 | 187 | | forgottenPromises[prom.GetHashCode()] = true; |
| 10 | 188 | | keeper.Forget(prom); |
| | 189 | | } |
| | 190 | |
|
| 1 | 191 | | keeper.Cleanup(); |
| 1 | 192 | | } |
| | 193 | | } |
| | 194 | | } |