| | 1 | | using DCL; |
| | 2 | | using NUnit.Framework; |
| | 3 | | using System.Collections; |
| | 4 | | using DCL.Components; |
| | 5 | | using DCL.Controllers; |
| | 6 | | using DCL.Helpers; |
| | 7 | | using DCL.Models; |
| | 8 | | using Newtonsoft.Json; |
| | 9 | | using UnityEngine; |
| | 10 | | using UnityEngine.TestTools; |
| | 11 | |
|
| | 12 | | namespace Tests |
| | 13 | | { |
| | 14 | | public class PoolManagerTests : IntegrationTestSuite_Legacy |
| | 15 | | { |
| | 16 | | public class PooledObjectInstantiator : IPooledObjectInstantiator |
| | 17 | | { |
| 5 | 18 | | public GameObject Instantiate(GameObject go) { return GameObject.Instantiate(go); } |
| | 19 | |
|
| 0 | 20 | | public bool IsValid(GameObject original) { return original != null; } |
| | 21 | | } |
| | 22 | |
|
| | 23 | | [Test] |
| | 24 | | public void PoolManagerShouldHandleNullArgsGracefully() |
| | 25 | | { |
| 1 | 26 | | var thisShouldBeNull = PoolManager.i.GetPoolable(null); |
| 1 | 27 | | Assert.IsTrue(thisShouldBeNull == null); |
| 1 | 28 | | } |
| | 29 | |
|
| | 30 | | [UnityTest] |
| | 31 | | public IEnumerator PooledGameObjectDestroyed() |
| | 32 | | { |
| 1 | 33 | | PooledObjectInstantiator instantiator = new PooledObjectInstantiator(); |
| | 34 | |
|
| 1 | 35 | | GameObject original = new GameObject("Original"); |
| | 36 | |
|
| 1 | 37 | | object id = "testId"; |
| | 38 | |
|
| 1 | 39 | | Pool pool = PoolManager.i.AddPool(id, original, instantiator); |
| 1 | 40 | | Assert.IsNotNull(pool, "Pool instance shouldn't be null."); |
| | 41 | |
|
| 1 | 42 | | PoolableObject po1 = PoolManager.i.Get(id); |
| 1 | 43 | | Assert.IsNotNull(po1, "Poolable object instance shouldn't be null."); |
| 1 | 44 | | po1.gameObject.SetActive(true); |
| | 45 | |
|
| 1 | 46 | | Assert.IsTrue(po1.gameObject.activeSelf, "Poolable object should be alive."); |
| | 47 | |
|
| 1 | 48 | | PoolableObject po2 = PoolManager.i.Get(id); |
| 1 | 49 | | Assert.IsNotNull(po2, "Poolable object instance shouldn't be null."); |
| 1 | 50 | | po2.gameObject.SetActive(true); |
| | 51 | |
|
| 1 | 52 | | Assert.IsTrue(po2.gameObject.activeSelf, "Poolable object should be alive."); |
| | 53 | |
|
| 1 | 54 | | pool = PoolManager.i.GetPool(id); |
| 1 | 55 | | Assert.IsNotNull(pool, "Pool instance shouldn't be null."); |
| | 56 | |
|
| 1 | 57 | | Assert.AreEqual(2, pool.usedObjectsCount, "Alive objects count should be 2"); |
| 1 | 58 | | Assert.AreEqual(0, pool.unusedObjectsCount, "Inactive objects count should be 0"); |
| | 59 | |
|
| 1 | 60 | | po1.Release(); |
| 1 | 61 | | yield return null; |
| | 62 | |
|
| 1 | 63 | | Assert.AreEqual(1, pool.usedObjectsCount, "Alive objects count should be 1"); |
| 1 | 64 | | Assert.AreEqual(1, pool.unusedObjectsCount, "Inactive objects count should be 1"); |
| | 65 | |
|
| 1 | 66 | | PoolManager.i.Cleanup(); |
| | 67 | |
|
| 1 | 68 | | Assert.AreEqual(0, pool.usedObjectsCount, "Alive objects count should be 0"); |
| 1 | 69 | | Assert.AreEqual(0, pool.unusedObjectsCount, "Inactive objects count should be 0"); |
| 1 | 70 | | } |
| | 71 | |
|
| | 72 | | [Test] |
| | 73 | | public void GetPoolableObject() |
| | 74 | | { |
| 1 | 75 | | PooledObjectInstantiator instantiator = new PooledObjectInstantiator(); |
| | 76 | |
|
| 1 | 77 | | GameObject original = new GameObject("Original"); |
| | 78 | |
|
| 1 | 79 | | object id = "testId"; |
| | 80 | |
|
| 1 | 81 | | Pool pool = PoolManager.i.AddPool(id, original, instantiator); |
| 1 | 82 | | Assert.IsNotNull(pool, "Pool instance shouldn't be null."); |
| | 83 | |
|
| 1 | 84 | | PoolableObject po1 = PoolManager.i.Get(id); |
| 1 | 85 | | Assert.IsNotNull(po1, "Poolable object instance shouldn't be null."); |
| | 86 | |
|
| 1 | 87 | | PoolableObject po2 = PoolManager.i.Get(id); |
| 1 | 88 | | Assert.IsNotNull(po2, "Poolable object instance shouldn't be null."); |
| | 89 | |
|
| 1 | 90 | | PoolableObject po3 = PoolManager.i.Get(id); |
| 1 | 91 | | Assert.IsNotNull(po3, "Poolable object instance shouldn't be null."); |
| | 92 | |
|
| 1 | 93 | | Assert.AreEqual(3, pool.usedObjectsCount, "Alive objects count should be 3"); |
| 1 | 94 | | Assert.AreEqual(0, pool.unusedObjectsCount, "Inactive objects count should be 0"); |
| | 95 | |
|
| 1 | 96 | | po3.Release(); |
| | 97 | |
|
| 1 | 98 | | Assert.AreEqual(2, pool.usedObjectsCount, "Alive objects count should be 2"); |
| 1 | 99 | | Assert.AreEqual(1, pool.unusedObjectsCount, "Inactive objects count should be 1"); |
| | 100 | |
|
| 1 | 101 | | PoolManager.i.ReleaseAllFromPool(id); |
| | 102 | |
|
| 1 | 103 | | Assert.AreEqual(0, pool.usedObjectsCount, "Alive objects count should be 0"); |
| 1 | 104 | | Assert.AreEqual(3, pool.unusedObjectsCount, "Inactive objects count should be 3"); |
| | 105 | |
|
| 1 | 106 | | PoolManager.i.RemovePool(id); |
| | 107 | |
|
| 1 | 108 | | Assert.IsFalse(PoolManager.i.ContainsPool(id), "Pool shouldn't exist after disposal"); |
| 1 | 109 | | } |
| | 110 | | } |
| | 111 | | } |