| | 1 | | using DCL; |
| | 2 | | using System.Collections; |
| | 3 | | using UnityEngine.Assertions; |
| | 4 | | using UnityEngine.TestTools; |
| | 5 | |
|
| | 6 | | namespace AssetPromiseKeeper_Mock_Tests |
| | 7 | | { |
| | 8 | | public class AnyAssetPromiseShould |
| | 9 | | { |
| | 10 | | [UnityTest] |
| | 11 | | public IEnumerator CallSuccessCallbackOnlyWhenNeeded() |
| | 12 | | { |
| 1 | 13 | | var library = new AssetLibrary_Mock(); |
| 1 | 14 | | var keeper = new AssetPromiseKeeper_Mock(library); |
| | 15 | |
|
| 1 | 16 | | AssetPromise_Mock prom = new AssetPromise_Mock(); |
| | 17 | |
|
| 1 | 18 | | bool successCalled = false; |
| | 19 | |
|
| 1 | 20 | | prom.idGenerator = "1"; |
| | 21 | |
|
| 1 | 22 | | System.Action<Asset_Mock> testEvent = (x) => |
| | 23 | | { |
| 1 | 24 | | successCalled = true; |
| 1 | 25 | | }; |
| | 26 | |
|
| 1 | 27 | | prom.OnSuccessEvent += testEvent; |
| | 28 | |
|
| 1 | 29 | | prom.SetLibrary_Test(library); |
| 1 | 30 | | prom.Load_Test(); |
| | 31 | |
|
| 1 | 32 | | yield return prom; |
| | 33 | |
|
| 1 | 34 | | Assert.IsTrue(successCalled == true, "Success callback wasn't called!"); |
| | 35 | |
|
| 1 | 36 | | successCalled = false; |
| 1 | 37 | | prom.Load_Test(); |
| 1 | 38 | | Assert.IsTrue(successCalled == false, "Success callback was called when it shouldn't and the event didn't cl |
| | 39 | |
|
| 1 | 40 | | successCalled = false; |
| 1 | 41 | | prom.OnSuccessEvent += testEvent; |
| 1 | 42 | | prom.Load_Test(); |
| | 43 | |
|
| 1 | 44 | | Assert.IsTrue(successCalled == false, "Success callback was called when it shouldn't!"); |
| 1 | 45 | | } |
| | 46 | |
|
| | 47 | | [UnityTest] |
| | 48 | | public IEnumerator CallFailedCallbackOnlyWhenNeeded() |
| | 49 | | { |
| 1 | 50 | | var library = new AssetLibrary_Mock(); |
| 1 | 51 | | var keeper = new AssetPromiseKeeper_Mock(library); |
| | 52 | |
|
| 1 | 53 | | AssetPromise_Mock prom = new AssetPromise_Mock(); |
| | 54 | |
|
| 1 | 55 | | bool failCalled = false; |
| | 56 | |
|
| 1 | 57 | | System.Action<Asset_Mock> testEvent = (x) => |
| | 58 | | { |
| 0 | 59 | | failCalled = true; |
| 0 | 60 | | }; |
| | 61 | |
|
| 1 | 62 | | prom.idGenerator = "1"; |
| 1 | 63 | | prom.OnFailEvent += testEvent; |
| | 64 | |
|
| 1 | 65 | | prom.SetLibrary_Test(library); |
| 1 | 66 | | prom.Load_Test(); |
| | 67 | |
|
| 1 | 68 | | yield return prom; |
| | 69 | |
|
| 1 | 70 | | Assert.IsTrue(prom.state == AssetPromiseState.FINISHED); |
| | 71 | |
|
| | 72 | | //NOTE(Brian): Test that nothing should happen if called multiple times |
| 1 | 73 | | prom.Unload_Test(); |
| | 74 | |
|
| 1 | 75 | | Assert.IsTrue(!failCalled, "Fail callback was called when it shouldn't!"); |
| 1 | 76 | | Assert.IsTrue(prom.state == AssetPromiseState.IDLE_AND_EMPTY, "Status should be NOT_GIVEN"); |
| | 77 | |
|
| 1 | 78 | | failCalled = false; |
| 1 | 79 | | prom.OnFailEvent += testEvent; |
| | 80 | |
|
| 1 | 81 | | prom.Unload_Test(); |
| 1 | 82 | | Assert.IsTrue(!failCalled, "Fail callback was called when it shouldn't!"); |
| 1 | 83 | | Assert.IsTrue(prom.state == AssetPromiseState.IDLE_AND_EMPTY, "Status should be NOT_GIVEN"); |
| | 84 | |
|
| 1 | 85 | | Assert.IsTrue(prom.GetAsset_Test() == null, "Asset should be null when promise is cancelled!"); |
| 1 | 86 | | } |
| | 87 | |
|
| | 88 | | [UnityTest] |
| | 89 | | public IEnumerator DontCallFailedCallbackAfterIsAlreadyUnloaded() |
| | 90 | | { |
| 1 | 91 | | var library = new AssetLibrary_Mock(); |
| 1 | 92 | | var keeper = new AssetPromiseKeeper_Mock(library); |
| | 93 | |
|
| 1 | 94 | | AssetPromise_Mock prom = new AssetPromise_Mock(); |
| 1 | 95 | | bool failCalled = false; |
| | 96 | |
|
| 1 | 97 | | System.Action<Asset_Mock> testEvent = (x) => |
| | 98 | | { |
| 0 | 99 | | failCalled = true; |
| 0 | 100 | | }; |
| | 101 | |
|
| 1 | 102 | | prom.idGenerator = "1"; |
| 1 | 103 | | prom.OnFailEvent += testEvent; |
| | 104 | |
|
| 1 | 105 | | prom.SetLibrary_Test(library); |
| 1 | 106 | | prom.Load_Test(); |
| | 107 | |
|
| 1 | 108 | | Assert.IsTrue(prom.state == AssetPromiseState.LOADING); |
| 1 | 109 | | Assert.IsTrue(prom.GetAsset_Test() != null, "Asset shouldn't be null when loading!"); |
| | 110 | |
|
| | 111 | | //NOTE(Brian): Test that nothing should happen if called multiple times |
| 1 | 112 | | prom.Unload_Test(); |
| | 113 | |
|
| 1 | 114 | | Assert.IsTrue(!failCalled, "Fail callback was called when it shouldnt'!"); |
| | 115 | |
|
| 1 | 116 | | failCalled = false; |
| 1 | 117 | | prom.Unload_Test(); |
| 1 | 118 | | Assert.IsTrue(!failCalled, "Fail callback was called when it shouldn't and fail event didn't clear!"); |
| | 119 | |
|
| 1 | 120 | | failCalled = false; |
| 1 | 121 | | prom.OnFailEvent += testEvent; |
| | 122 | |
|
| 1 | 123 | | prom.Unload_Test(); |
| 1 | 124 | | Assert.IsTrue(!failCalled, "Fail callback was called twice when it shouldn't!"); |
| | 125 | |
|
| | 126 | | //NOTE(Brian): Test that nothing should happen if called multiple times |
| 1 | 127 | | prom.Unload_Test(); |
| 1 | 128 | | prom.Unload_Test(); |
| 1 | 129 | | prom.Unload_Test(); |
| | 130 | |
|
| 1 | 131 | | Assert.IsTrue(prom.GetAsset_Test() == null, "Asset should be null when promise is cancelled!"); |
| 1 | 132 | | yield break; |
| | 133 | | } |
| | 134 | | } |
| | 135 | | } |