| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using NUnit.Framework; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.TestTools; |
| | 6 | | using Assert = UnityEngine.Assertions.Assert; |
| | 7 | |
|
| | 8 | | namespace DCL |
| | 9 | | { |
| | 10 | | public class GlobalUsersPositionMarkerTests |
| | 11 | | { |
| | 12 | | [Test] |
| | 13 | | public void SceneFilterShouldReturnTheCorrectAmount() |
| | 14 | | { |
| | 15 | | const int MAX_MARKERS = 200; |
| | 16 | | const int INITIAL_SCENES_AMOUNT = 20; // Smaller than MAX_MARKERS |
| | 17 | | const int FINAL_SCENES_AMOUNT = 237; // Greater than MAX_MARKERS |
| | 18 | |
|
| 1 | 19 | | List<HotScenesController.HotSceneInfo> initialScenes = new List<HotScenesController.HotSceneInfo>(); |
| 1 | 20 | | List<HotScenesController.HotSceneInfo> finalScenes = new List<HotScenesController.HotSceneInfo>(); |
| | 21 | |
|
| 476 | 22 | | for (int i = 0; i < FINAL_SCENES_AMOUNT; i++) |
| | 23 | | { |
| 237 | 24 | | var scene = CreateSceneInfo(Vector2Int.zero); |
| 237 | 25 | | if (i < INITIAL_SCENES_AMOUNT) |
| | 26 | | { |
| 20 | 27 | | initialScenes.Add(scene); |
| | 28 | | } |
| | 29 | |
|
| 237 | 30 | | finalScenes.Add(scene); |
| | 31 | | } |
| | 32 | |
|
| 1 | 33 | | ScenesFilter filter = new ScenesFilter(); |
| 1 | 34 | | Assert.AreEqual(INITIAL_SCENES_AMOUNT, filter.Filter(initialScenes, MAX_MARKERS).Count); |
| 1 | 35 | | Assert.AreEqual(MAX_MARKERS, filter.Filter(finalScenes, MAX_MARKERS).Count); |
| 1 | 36 | | } |
| | 37 | |
|
| | 38 | | [Test] |
| | 39 | | public void SceneFetcherShouldSetIntervalsCorrectly() |
| | 40 | | { |
| | 41 | | const float INITIAL_INTERVAL = 1; |
| | 42 | | const float FOREGROUND_INTERVAL = 1.5f; |
| | 43 | | const float BACKROUND_INTERVAL = 3.5f; |
| | 44 | |
|
| 1 | 45 | | FetchScenesHandler handler = new FetchScenesHandler(INITIAL_INTERVAL, FOREGROUND_INTERVAL, BACKROUND_INTERVA |
| 1 | 46 | | Assert.AreEqual(INITIAL_INTERVAL, handler.updateInterval); |
| | 47 | |
|
| 1 | 48 | | handler.SetUpdateMode(MapGlobalUsersPositionMarkerController.UpdateMode.FOREGROUND); |
| 1 | 49 | | Assert.AreEqual(INITIAL_INTERVAL, handler.updateInterval, "It should be using INITIAL_INTERVAL interval wait |
| | 50 | |
|
| 1 | 51 | | handler.isFirstFetch = false; |
| 1 | 52 | | handler.SetUpdateMode(MapGlobalUsersPositionMarkerController.UpdateMode.FOREGROUND); |
| 1 | 53 | | Assert.AreEqual(FOREGROUND_INTERVAL, handler.updateInterval); |
| | 54 | |
|
| 1 | 55 | | handler.SetUpdateMode(MapGlobalUsersPositionMarkerController.UpdateMode.BACKGROUND); |
| 1 | 56 | | Assert.AreEqual(BACKROUND_INTERVAL, handler.updateInterval); |
| 1 | 57 | | } |
| | 58 | |
|
| | 59 | | [Test] |
| | 60 | | public void PlayerPositionShouldSetCorrectly() |
| | 61 | | { |
| 1 | 62 | | Vector2Int FIRST_POSITION = new Vector2Int(0, 0); |
| 1 | 63 | | Vector2Int SECOND_POSITION = new Vector2Int(70, -135); |
| 1 | 64 | | Vector2Int THIRD_POSITION = new Vector2Int(-34, -495); |
| | 65 | |
|
| 1 | 66 | | UserPositionHandler handler = new UserPositionHandler(); |
| | 67 | |
|
| 1 | 68 | | CommonScriptableObjects.playerCoords.Set(FIRST_POSITION); |
| 1 | 69 | | Assert.AreEqual(FIRST_POSITION, handler.playerCoords); |
| | 70 | |
|
| 1 | 71 | | CommonScriptableObjects.playerCoords.Set(SECOND_POSITION); |
| 1 | 72 | | Assert.AreEqual(SECOND_POSITION, handler.playerCoords); |
| | 73 | |
|
| 1 | 74 | | CommonScriptableObjects.playerCoords.Set(THIRD_POSITION); |
| 1 | 75 | | Assert.AreEqual(THIRD_POSITION, handler.playerCoords); |
| | 76 | |
|
| 1 | 77 | | handler.Dispose(); |
| 1 | 78 | | } |
| | 79 | |
|
| | 80 | | [Test] |
| | 81 | | public void MarkersShouldSetCorrectly() |
| | 82 | | { |
| | 83 | | const int MAX_MARKERS = 5; |
| | 84 | | const int EXCLUSION_AREA = 1; |
| | 85 | |
|
| 1 | 86 | | var markerPrefab = new GameObject().AddComponent<UserMarkerObject>(); |
| 1 | 87 | | var overlay = new GameObject(); |
| | 88 | |
|
| 6 | 89 | | Func<float, float, Vector3> coordToMapPosFunc = (x, y) => { return new Vector3(x, y, 0); }; |
| | 90 | |
|
| | 91 | | // create scenes to test |
| 1 | 92 | | var scenes = new List<HotScenesController.HotSceneInfo>(); |
| 1 | 93 | | scenes.Add(CreateSceneInfo(new Vector2Int(0, 0))); |
| 1 | 94 | | scenes.Add(CreateSceneInfo(new Vector2Int(3, 4))); |
| 1 | 95 | | scenes.Add(CreateSceneInfo(new Vector2Int(-4, -4))); |
| | 96 | |
|
| | 97 | | // create handler |
| 1 | 98 | | MarkersHandler handler = new MarkersHandler(markerPrefab, overlay.transform, MAX_MARKERS, coordToMapPosFunc) |
| 1 | 99 | | Assert.AreEqual(MAX_MARKERS, handler.availableMarkers.Count); |
| 1 | 100 | | Assert.AreEqual(0, handler.usedMarkers.Count); |
| | 101 | |
|
| | 102 | | // set exclusion area and set markers for scenes |
| 1 | 103 | | handler.SetExclusionArea(Vector2Int.zero, EXCLUSION_AREA); |
| 1 | 104 | | handler.SetMarkers(scenes); |
| | 105 | |
|
| | 106 | | // check pools count and marker hidden by exclusion area |
| 1 | 107 | | Assert.AreEqual(MAX_MARKERS - scenes.Count, handler.availableMarkers.Count); |
| 1 | 108 | | Assert.AreEqual(scenes.Count, handler.usedMarkers.Count); |
| 1 | 109 | | Assert.AreEqual(scenes.Count - 1, GetActiveGameObjectsInParent(overlay.transform), "A marker should be hidde |
| | 110 | |
|
| | 111 | | // move exclusion area and check markers hidden by exclusion area |
| 1 | 112 | | handler.SetExclusionArea(new Vector2Int(6, 6), EXCLUSION_AREA); |
| 1 | 113 | | Assert.AreEqual(scenes.Count, GetActiveGameObjectsInParent(overlay.transform), "All markers should be visibl |
| | 114 | |
|
| | 115 | | // remove a scene and check pools count |
| 1 | 116 | | scenes.RemoveAt(0); |
| 1 | 117 | | handler.SetMarkers(scenes); |
| 1 | 118 | | Assert.AreEqual(MAX_MARKERS - scenes.Count, handler.availableMarkers.Count); |
| 1 | 119 | | Assert.AreEqual(scenes.Count, handler.usedMarkers.Count); |
| | 120 | |
|
| 1 | 121 | | handler.Dispose(); |
| 1 | 122 | | UnityEngine.Object.Destroy(markerPrefab); |
| 1 | 123 | | UnityEngine.Object.Destroy(overlay); |
| 1 | 124 | | } |
| | 125 | |
|
| | 126 | | private HotScenesController.HotSceneInfo CreateSceneInfo(Vector2Int coords) |
| | 127 | | { |
| 240 | 128 | | return new HotScenesController.HotSceneInfo() |
| | 129 | | { |
| | 130 | | baseCoords = coords, |
| | 131 | | realms = new HotScenesController.HotSceneInfo.Realm[] |
| | 132 | | { |
| | 133 | | new HotScenesController.HotSceneInfo.Realm() |
| | 134 | | { |
| | 135 | | userParcels = new Vector2Int[] { coords }, |
| | 136 | | usersCount = 1 |
| | 137 | | } |
| | 138 | | }, |
| | 139 | | usersTotalCount = 1 |
| | 140 | | }; |
| | 141 | | } |
| | 142 | |
|
| | 143 | | private int GetActiveGameObjectsInParent(Transform parent) |
| | 144 | | { |
| 2 | 145 | | int result = 0; |
| 24 | 146 | | foreach (Transform t in parent) |
| | 147 | | { |
| 10 | 148 | | if (t.gameObject.activeSelf) |
| | 149 | | { |
| 5 | 150 | | result++; |
| | 151 | | } |
| | 152 | | } |
| | 153 | |
|
| 2 | 154 | | return result; |
| | 155 | | } |
| | 156 | | } |
| | 157 | | } |