< Summary

Class:Tests.MapRendererShould
Assembly:MapRendererTests
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/MapRenderer/Tests/MapRendererShould.cs
Covered lines:51
Uncovered lines:30
Coverable lines:81
Total lines:173
Line coverage:62.9% (51 of 81)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SetUp()0%5.015092.31%
TearDown()0%330100%
CenterAsIntended()0%2100%
PerformCullingAsIntended()0%6200%
DisplayParcelOfInterestIconsProperly()0%110100%
DisplayAndUpdateUserIconProperly()0%220100%
GetChunkStatesAsString()0%30500%

File(s)

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

#LineLine coverage
 1using DCL;
 2using System.Collections;
 3using System.Collections.Generic;
 4using NUnit.Framework;
 5using UnityEngine;
 6using UnityEngine.TestTools;
 7using Assert = UnityEngine.Assertions.Assert;
 8
 9namespace Tests
 10{
 11    public class MapRendererShould : IntegrationTestSuite_Legacy
 12    {
 13        private GameObject viewport;
 14
 15        [UnitySetUp]
 16        protected override IEnumerator SetUp()
 17        {
 218            yield return base.SetUp();
 19
 220            if (MapRenderer.i == null)
 021                Object.Instantiate(Resources.Load("Map Renderer"));
 22
 223            MapRenderer.i.atlas.mapChunkPrefab = (GameObject) Resources.Load("Map Chunk Mock");
 224            viewport = new GameObject("Viewport");
 225            var rt = viewport.AddComponent<RectTransform>();
 226            rt.sizeDelta = Vector2.one * 100;
 227            viewport.transform.SetParent(MapRenderer.i.atlas.transform, false);
 228            viewport.transform.localPosition = Vector3.zero;
 229            MapRenderer.i.atlas.viewport = rt;
 30
 231            MapRenderer.i.Initialize();
 32
 33            //NOTE(Brian): Needed to wait for Start() call in MapRenderer
 234            yield return null;
 235        }
 36
 37        protected override IEnumerator TearDown()
 38        {
 239            MapRenderer.i.Cleanup();
 240            UnityEngine.Object.Destroy(viewport);
 41
 242            yield return base.TearDown();
 243        }
 44
 45        [Test]
 46        [Category("Explicit")]
 47        [Explicit("For some reason this test fails when running after other test in this suite.")]
 48        public void CenterAsIntended()
 49        {
 050            Transform atlasContainerTransform = MapRenderer.i.atlas.container.transform;
 51
 052            CommonScriptableObjects.playerWorldPosition.Set(new Vector3(1, 1, 1));
 053            CommonScriptableObjects.playerWorldPosition.Set(new Vector3(0, 0, 0));
 054            Assert.AreApproximatelyEqual(-1500, atlasContainerTransform.position.x);
 055            Assert.AreApproximatelyEqual(-1500, atlasContainerTransform.position.y);
 56
 057            CommonScriptableObjects.playerWorldPosition.Set(new Vector3(100, 0, 100));
 058            Assert.AreApproximatelyEqual(-1562.5f, atlasContainerTransform.position.x);
 059            Assert.AreApproximatelyEqual(-1562.5f, atlasContainerTransform.position.y);
 60
 061            CommonScriptableObjects.playerWorldPosition.Set(new Vector3(-100, 0, -100));
 062            Assert.AreApproximatelyEqual(-1437.5f, atlasContainerTransform.position.x);
 063            Assert.AreApproximatelyEqual(-1437.5f, atlasContainerTransform.position.y);
 064        }
 65
 66        [UnityTest]
 67        [Category("Explicit")]
 68        [Explicit("For some reason this test fails when running after other test in this suite.")]
 69        public IEnumerator PerformCullingAsIntended()
 70        {
 071            CommonScriptableObjects.playerWorldPosition.Set(new Vector3(0, 0, 0));
 072            Assert.AreEqual("0000000000000000000000001000000000000000000000000", GetChunkStatesAsString());
 073            CommonScriptableObjects.playerWorldPosition.Set(new Vector3(1000, 0, 1000));
 074            Assert.AreEqual("0000000000000000000000000000000011000001100000000", GetChunkStatesAsString());
 075            CommonScriptableObjects.playerWorldPosition.Set(new Vector3(-1000, 0, -1000));
 076            Assert.AreEqual("0000000011000001100000000000000000000000000000000", GetChunkStatesAsString());
 077            yield break;
 78        }
 79
 80        [Test]
 81        public void DisplayParcelOfInterestIconsProperly()
 82        {
 183            var sceneInfo = new MinimapMetadata.MinimapSceneInfo();
 184            sceneInfo.name = "important scene";
 185            sceneInfo.isPOI = true;
 186            sceneInfo.parcels = new List<Vector2Int>()
 87            {
 88                new Vector2Int() { x = 0, y = 0 },
 89                new Vector2Int() { x = 0, y = 1 },
 90                new Vector2Int() { x = 1, y = 0 },
 91                new Vector2Int() { x = 1, y = 1 }
 92            };
 93
 194            MinimapMetadata.GetMetadata().AddSceneInfo(sceneInfo);
 95
 196            var sceneInfo2 = new MinimapMetadata.MinimapSceneInfo();
 197            sceneInfo2.name = "non-important scene";
 198            sceneInfo2.isPOI = false;
 199            sceneInfo2.parcels = new List<Vector2Int>()
 100            {
 101                new Vector2Int() { x = 5, y = 0 },
 102            };
 103
 1104            MinimapMetadata.GetMetadata().AddSceneInfo(sceneInfo2);
 105
 1106            MapSceneIcon[] icons = MapRenderer.i.GetComponentsInChildren<MapSceneIcon>();
 107
 1108            Assert.AreEqual(1, icons.Length, "Only 1 icon is marked as POI, but 2 icons were spawned");
 1109            Assert.AreEqual(sceneInfo.name, icons[0].title.text);
 1110            Assert.AreEqual(new Vector3(3010, 3010, 0), icons[0].transform.localPosition);
 1111        }
 112
 113        [UnityTest]
 114        public IEnumerator DisplayAndUpdateUserIconProperly()
 115        {
 1116            Vector3 initialPosition = new Vector3(100, 0, 50);
 1117            Vector3 modifiedPosition = new Vector3(150, 0, -30);
 118
 1119            var userInfo = new MinimapMetadata.MinimapUserInfo();
 1120            userInfo.userId = "testuser";
 1121            userInfo.worldPosition = initialPosition;
 122
 123            // Create an user icon
 1124            MinimapMetadata.GetMetadata().AddOrUpdateUserInfo(userInfo);
 125
 1126            MapSceneIcon[] icons = MapRenderer.i.GetComponentsInChildren<MapSceneIcon>();
 127
 1128            Assert.AreEqual(1, icons.Length, "There should be only 1 user icon");
 1129            Vector2 iconGridPosition = DCL.Helpers.Utils.WorldToGridPositionUnclamped(initialPosition);
 1130            Assert.AreEqual(DCL.Helpers.MapUtils.GetTileToLocalPosition(iconGridPosition.x, iconGridPosition.y), icons[0
 131
 132            // Modifify the position of the user icon
 1133            userInfo.worldPosition = modifiedPosition;
 134
 1135            MinimapMetadata.GetMetadata().AddOrUpdateUserInfo(userInfo);
 136
 1137            icons = MapRenderer.i.GetComponentsInChildren<MapSceneIcon>();
 138
 1139            Assert.AreEqual(1, icons.Length, "There should still be the same user icon");
 1140            iconGridPosition = DCL.Helpers.Utils.WorldToGridPositionUnclamped(modifiedPosition);
 1141            Assert.AreEqual(DCL.Helpers.MapUtils.GetTileToLocalPosition(iconGridPosition.x, iconGridPosition.y), icons[0
 142
 143            // Remove the user icon
 1144            MinimapMetadata.GetMetadata().RemoveUserInfo(userInfo.userId);
 145
 1146            icons = MapRenderer.i.GetComponentsInChildren<MapSceneIcon>();
 147
 1148            Assert.AreEqual(0, icons.Length, "There should not be any user icon");
 1149            yield break;
 150        }
 151
 152        public string GetChunkStatesAsString()
 153        {
 0154            string result = "";
 0155            for (int x = 0; x < 7; x++)
 156            {
 0157                for (int y = 0; y < 7; y++)
 158                {
 0159                    MapChunk chunk = MapRenderer.i.atlas.GetChunk(x, y);
 160
 0161                    if (chunk == null)
 0162                        result += "-";
 0163                    else if (chunk.targetImage.enabled)
 0164                        result += "1";
 165                    else
 0166                        result += "0";
 167                }
 168            }
 169
 0170            return result;
 171        }
 172    }
 173}