| | 1 | | using System.Collections.Generic; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Controllers; |
| | 4 | | using NSubstitute; |
| | 5 | | using NSubstitute.ReceivedExtensions; |
| | 6 | | using NUnit.Framework; |
| | 7 | | using UnityEngine; |
| | 8 | |
|
| | 9 | | public class BlockersControllerShould |
| | 10 | | { |
| | 11 | | private ISceneHandler sceneHandler; |
| | 12 | | WorldBlockersController blockerController; |
| | 13 | | IBlockerInstanceHandler blockerInstanceHandler; |
| | 14 | | GameObject blockersParent; |
| | 15 | |
|
| | 16 | | [SetUp] |
| | 17 | | protected void SetUp() |
| | 18 | | { |
| 3 | 19 | | RenderProfileManifest.i.Initialize(); |
| | 20 | |
|
| 3 | 21 | | sceneHandler = Substitute.For<ISceneHandler>(); |
| | 22 | |
|
| 3 | 23 | | var allLoadedParcelCoords = new HashSet<Vector2Int>(); |
| 3 | 24 | | allLoadedParcelCoords.Add(new Vector2Int(0, 0)); |
| 3 | 25 | | allLoadedParcelCoords.Add(new Vector2Int(-1, 0)); |
| 3 | 26 | | allLoadedParcelCoords.Add(new Vector2Int(-1, 1)); |
| | 27 | |
|
| 3 | 28 | | sceneHandler.GetAllLoadedScenesCoords().Returns(allLoadedParcelCoords); |
| | 29 | |
|
| 3 | 30 | | var animationHandler = Substitute.For<IBlockerAnimationHandler>(); |
| | 31 | | //NOTE(Brian): Call OnFinish() when blockerAnimationHandler.FadeOut is called. |
| 3 | 32 | | animationHandler.FadeOut(Arg.Any<GameObject>(), Arg.Invoke()); |
| | 33 | |
|
| 3 | 34 | | var newBlockerInstanceHandler = new BlockerInstanceHandler(); |
| 3 | 35 | | newBlockerInstanceHandler.Initialize(animationHandler, null); |
| | 36 | |
|
| 3 | 37 | | blockerInstanceHandler = newBlockerInstanceHandler; |
| 3 | 38 | | blockersParent = new GameObject(); |
| 3 | 39 | | blockerInstanceHandler.SetParent(blockersParent.transform); |
| | 40 | |
|
| 3 | 41 | | blockerController = new WorldBlockersController(); |
| 3 | 42 | | blockerController.Initialize(sceneHandler, blockerInstanceHandler); |
| 3 | 43 | | } |
| | 44 | |
|
| | 45 | | [TearDown] |
| | 46 | | protected void TearDown() |
| | 47 | | { |
| 3 | 48 | | blockerController.Dispose(); |
| 3 | 49 | | Object.Destroy(blockersParent); |
| 3 | 50 | | } |
| | 51 | |
|
| | 52 | | [Test] |
| | 53 | | public void SetupBlockersOnlyWhenEnabled() |
| | 54 | | { |
| | 55 | | // Arrange |
| 1 | 56 | | blockerInstanceHandler = Substitute.For<IBlockerInstanceHandler>(); |
| 1 | 57 | | blockerInstanceHandler.GetBlockers().Returns(new Dictionary<Vector2Int, IPoolableObject>()); |
| | 58 | |
|
| 1 | 59 | | if (blockerController != null) |
| 1 | 60 | | blockerController.Dispose(); |
| | 61 | |
|
| 1 | 62 | | blockerController.Initialize(sceneHandler, blockerInstanceHandler); |
| | 63 | |
|
| | 64 | | // Act-assert #1: first blockers added should be shown |
| 1 | 65 | | blockerController.SetupWorldBlockers(); |
| 1 | 66 | | blockerInstanceHandler.ReceivedWithAnyArgs().ShowBlocker(default); |
| | 67 | |
|
| 1 | 68 | | blockerInstanceHandler.ClearReceivedCalls(); |
| | 69 | |
|
| | 70 | | // Act-assert #2: if disabled, blockers should be removed |
| 1 | 71 | | blockerController.SetEnabled(false); |
| 1 | 72 | | blockerInstanceHandler.Received(1).DestroyAllBlockers(); |
| | 73 | |
|
| 1 | 74 | | blockerInstanceHandler.ClearReceivedCalls(); |
| | 75 | |
|
| | 76 | | // Act-assert #3: if disabled, no blockers should be added nor removed |
| 1 | 77 | | blockerController.SetupWorldBlockers(); |
| 1 | 78 | | blockerInstanceHandler.DidNotReceiveWithAnyArgs().ShowBlocker(default); |
| 1 | 79 | | blockerInstanceHandler.DidNotReceiveWithAnyArgs().HideBlocker(default, default); |
| | 80 | |
|
| 1 | 81 | | blockerInstanceHandler.ClearReceivedCalls(); |
| | 82 | |
|
| | 83 | | // Act-assert #4: If enabled again, blockers should be added or removed as needed |
| 1 | 84 | | blockerController.SetEnabled(true); |
| 1 | 85 | | blockerController.SetupWorldBlockers(); |
| 1 | 86 | | blockerInstanceHandler.ReceivedWithAnyArgs().ShowBlocker(default); |
| | 87 | |
|
| 1 | 88 | | blockerController.Dispose(); |
| 1 | 89 | | } |
| | 90 | |
|
| | 91 | | [Test] |
| | 92 | | public void PutBlockersAroundExplorableArea() |
| | 93 | | { |
| 1 | 94 | | blockerController.SetupWorldBlockers(); |
| 1 | 95 | | var blockers = blockerInstanceHandler.GetBlockers(); |
| | 96 | |
|
| 1 | 97 | | Assert.AreEqual(12, blockers.Count); |
| 1 | 98 | | Assert.IsFalse(blockers.ContainsKey(new Vector2Int(0, 0))); |
| 1 | 99 | | Assert.IsFalse(blockers.ContainsKey(new Vector2Int(-1, 0))); |
| 1 | 100 | | Assert.IsFalse(blockers.ContainsKey(new Vector2Int(-1, 1))); |
| | 101 | |
|
| 1 | 102 | | Assert.IsTrue(blockers.ContainsKey(new Vector2Int(1, 0))); |
| 1 | 103 | | Assert.IsTrue(blockers.ContainsKey(new Vector2Int(0, 1))); |
| 1 | 104 | | Assert.IsTrue(blockers.ContainsKey(new Vector2Int(0, -1))); |
| 1 | 105 | | Assert.IsTrue(blockers.ContainsKey(new Vector2Int(1, 1))); |
| 1 | 106 | | Assert.IsTrue(blockers.ContainsKey(new Vector2Int(-1, -1))); |
| 1 | 107 | | Assert.IsTrue(blockers.ContainsKey(new Vector2Int(1, -1))); |
| 1 | 108 | | Assert.IsTrue(blockers.ContainsKey(new Vector2Int(-2, 0))); |
| 1 | 109 | | Assert.IsTrue(blockers.ContainsKey(new Vector2Int(-2, -1))); |
| 1 | 110 | | Assert.IsTrue(blockers.ContainsKey(new Vector2Int(-2, 1))); |
| 1 | 111 | | Assert.IsTrue(blockers.ContainsKey(new Vector2Int(-1, 2))); |
| 1 | 112 | | Assert.IsTrue(blockers.ContainsKey(new Vector2Int(0, 2))); |
| 1 | 113 | | Assert.IsTrue(blockers.ContainsKey(new Vector2Int(-2, 2))); |
| 1 | 114 | | } |
| | 115 | |
|
| | 116 | | [Test] |
| | 117 | | public void ClearOnlyChangedBlockers() |
| | 118 | | { |
| 1 | 119 | | blockerController.SetupWorldBlockers(); |
| 1 | 120 | | var blockers = blockerInstanceHandler.GetBlockers(); |
| | 121 | |
|
| | 122 | | // Save instance of some blockers that shouldn't change on the next scene load |
| 1 | 123 | | var blocker1 = blockers[new Vector2Int(-1, -1)].gameObject; |
| 1 | 124 | | var blocker2 = blockers[new Vector2Int(-2, -1)].gameObject; |
| 1 | 125 | | var blocker3 = blockers[new Vector2Int(-2, 0)].gameObject; |
| | 126 | |
|
| | 127 | | // check blocker that will get removed on next scene load |
| 1 | 128 | | Assert.IsTrue(blockers.ContainsKey(new Vector2Int(0, 1))); |
| | 129 | |
|
| | 130 | | // Load 2nd scene next to the first one |
| 1 | 131 | | var newTotalLoadedCoords = new HashSet<Vector2Int>(); |
| 1 | 132 | | newTotalLoadedCoords.Add(new Vector2Int(0, 1)); |
| 1 | 133 | | newTotalLoadedCoords.Add(new Vector2Int(1, 1)); |
| 1 | 134 | | newTotalLoadedCoords.Add(new Vector2Int(1, 2)); |
| 1 | 135 | | newTotalLoadedCoords.UnionWith(sceneHandler.GetAllLoadedScenesCoords()); |
| | 136 | |
|
| 1 | 137 | | sceneHandler.GetAllLoadedScenesCoords().Returns(newTotalLoadedCoords); |
| 1 | 138 | | blockerController.SetupWorldBlockers(); |
| | 139 | |
|
| 1 | 140 | | blockers = blockerInstanceHandler.GetBlockers(); |
| | 141 | |
|
| | 142 | | // Check some non-changed blockers: |
| 1 | 143 | | Assert.IsTrue(blockers[new Vector2Int(-1, -1)].gameObject == blocker1); |
| 1 | 144 | | Assert.IsTrue(blockers[new Vector2Int(-2, -1)].gameObject == blocker2); |
| 1 | 145 | | Assert.IsTrue(blockers[new Vector2Int(-2, 0)].gameObject == blocker3); |
| | 146 | |
|
| | 147 | | // Check removed blocker |
| 1 | 148 | | Assert.IsFalse(blockers.ContainsKey(new Vector2Int(0, 1))); |
| 1 | 149 | | } |
| | 150 | | } |