< Summary

Class:DCL.GlobalUsersPositionMarkerTests
Assembly:GlobalUsersPositionMarkerTests
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/MapRenderer/GlobalUsersPositionMarker/Tests/GlobalUsersPositionMarkerTests.cs
Covered lines:64
Uncovered lines:0
Coverable lines:64
Total lines:157
Line coverage:100% (64 of 64)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SceneFilterShouldReturnTheCorrectAmount()0%330100%
SceneFetcherShouldSetIntervalsCorrectly()0%110100%
PlayerPositionShouldSetCorrectly()0%110100%
MarkersShouldSetCorrectly()0%220100%
CreateSceneInfo(...)0%110100%
GetActiveGameObjectsInParent(...)0%440100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/MapRenderer/GlobalUsersPositionMarker/Tests/GlobalUsersPositionMarkerTests.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using NUnit.Framework;
 4using UnityEngine;
 5using UnityEngine.TestTools;
 6using Assert = UnityEngine.Assertions.Assert;
 7
 8namespace 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
 119            List<HotScenesController.HotSceneInfo> initialScenes = new List<HotScenesController.HotSceneInfo>();
 120            List<HotScenesController.HotSceneInfo> finalScenes = new List<HotScenesController.HotSceneInfo>();
 21
 47622            for (int i = 0; i < FINAL_SCENES_AMOUNT; i++)
 23            {
 23724                var scene = CreateSceneInfo(Vector2Int.zero);
 23725                if (i < INITIAL_SCENES_AMOUNT)
 26                {
 2027                    initialScenes.Add(scene);
 28                }
 29
 23730                finalScenes.Add(scene);
 31            }
 32
 133            ScenesFilter filter = new ScenesFilter();
 134            Assert.AreEqual(INITIAL_SCENES_AMOUNT, filter.Filter(initialScenes, MAX_MARKERS).Count);
 135            Assert.AreEqual(MAX_MARKERS, filter.Filter(finalScenes, MAX_MARKERS).Count);
 136        }
 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
 145            FetchScenesHandler handler = new FetchScenesHandler(INITIAL_INTERVAL, FOREGROUND_INTERVAL, BACKROUND_INTERVA
 146            Assert.AreEqual(INITIAL_INTERVAL, handler.updateInterval);
 47
 148            handler.SetUpdateMode(MapGlobalUsersPositionMarkerController.UpdateMode.FOREGROUND);
 149            Assert.AreEqual(INITIAL_INTERVAL, handler.updateInterval, "It should be using INITIAL_INTERVAL interval wait
 50
 151            handler.isFirstFetch = false;
 152            handler.SetUpdateMode(MapGlobalUsersPositionMarkerController.UpdateMode.FOREGROUND);
 153            Assert.AreEqual(FOREGROUND_INTERVAL, handler.updateInterval);
 54
 155            handler.SetUpdateMode(MapGlobalUsersPositionMarkerController.UpdateMode.BACKGROUND);
 156            Assert.AreEqual(BACKROUND_INTERVAL, handler.updateInterval);
 157        }
 58
 59        [Test]
 60        public void PlayerPositionShouldSetCorrectly()
 61        {
 162            Vector2Int FIRST_POSITION = new Vector2Int(0, 0);
 163            Vector2Int SECOND_POSITION = new Vector2Int(70, -135);
 164            Vector2Int THIRD_POSITION = new Vector2Int(-34, -495);
 65
 166            UserPositionHandler handler = new UserPositionHandler();
 67
 168            CommonScriptableObjects.playerCoords.Set(FIRST_POSITION);
 169            Assert.AreEqual(FIRST_POSITION, handler.playerCoords);
 70
 171            CommonScriptableObjects.playerCoords.Set(SECOND_POSITION);
 172            Assert.AreEqual(SECOND_POSITION, handler.playerCoords);
 73
 174            CommonScriptableObjects.playerCoords.Set(THIRD_POSITION);
 175            Assert.AreEqual(THIRD_POSITION, handler.playerCoords);
 76
 177            handler.Dispose();
 178        }
 79
 80        [Test]
 81        public void MarkersShouldSetCorrectly()
 82        {
 83            const int MAX_MARKERS = 5;
 84            const int EXCLUSION_AREA = 1;
 85
 186            var markerPrefab = new GameObject().AddComponent<UserMarkerObject>();
 187            var overlay = new GameObject();
 88
 689            Func<float, float, Vector3> coordToMapPosFunc = (x, y) => { return new Vector3(x, y, 0); };
 90
 91            // create scenes to test
 192            var scenes = new List<HotScenesController.HotSceneInfo>();
 193            scenes.Add(CreateSceneInfo(new Vector2Int(0, 0)));
 194            scenes.Add(CreateSceneInfo(new Vector2Int(3, 4)));
 195            scenes.Add(CreateSceneInfo(new Vector2Int(-4, -4)));
 96
 97            // create handler
 198            MarkersHandler handler = new MarkersHandler(markerPrefab, overlay.transform, MAX_MARKERS, coordToMapPosFunc)
 199            Assert.AreEqual(MAX_MARKERS, handler.availableMarkers.Count);
 1100            Assert.AreEqual(0, handler.usedMarkers.Count);
 101
 102            // set exclusion area and set markers for scenes
 1103            handler.SetExclusionArea(Vector2Int.zero, EXCLUSION_AREA);
 1104            handler.SetMarkers(scenes);
 105
 106            // check pools count and marker hidden by exclusion area
 1107            Assert.AreEqual(MAX_MARKERS - scenes.Count, handler.availableMarkers.Count);
 1108            Assert.AreEqual(scenes.Count, handler.usedMarkers.Count);
 1109            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
 1112            handler.SetExclusionArea(new Vector2Int(6, 6), EXCLUSION_AREA);
 1113            Assert.AreEqual(scenes.Count, GetActiveGameObjectsInParent(overlay.transform), "All markers should be visibl
 114
 115            // remove a scene and check pools count
 1116            scenes.RemoveAt(0);
 1117            handler.SetMarkers(scenes);
 1118            Assert.AreEqual(MAX_MARKERS - scenes.Count, handler.availableMarkers.Count);
 1119            Assert.AreEqual(scenes.Count, handler.usedMarkers.Count);
 120
 1121            handler.Dispose();
 1122            UnityEngine.Object.Destroy(markerPrefab);
 1123            UnityEngine.Object.Destroy(overlay);
 1124        }
 125
 126        private HotScenesController.HotSceneInfo CreateSceneInfo(Vector2Int coords)
 127        {
 240128            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        {
 2145            int result = 0;
 24146            foreach (Transform t in parent)
 147            {
 10148                if (t.gameObject.activeSelf)
 149                {
 5150                    result++;
 151                }
 152            }
 153
 2154            return result;
 155        }
 156    }
 157}