using System.Linq; using NUnit.Framework; using Robust.Shared.GameObjects; using Robust.Shared.Map; using Robust.UnitTesting.Server; // ReSharper disable AccessToStaticMemberViaDerivedType namespace Robust.UnitTesting.Shared.Map; [TestFixture] internal sealed class MapPauseTests { private static ISimulation SimulationFactory() { var sim = RobustServerSimulation .NewSimulation() .InitializeInstance(); return sim; } /// /// When an entity is on a paused map, it does not get returned by an EntityQuery. /// [Test] public void Paused_NotIncluded_NotInQuery() { var sim = SimulationFactory(); var entMan = sim.Resolve(); var mapMan = sim.Resolve(); // arrange var mapId = mapMan.CreateMap(); mapMan.SetMapPaused(mapId, true); entMan.SpawnEntity(null, new MapCoordinates(0, 0, mapId)); var query = entMan.EntityQuery(false).ToList(); // 0 ents, map and the spawned one are not returned Assert.That(query.Count, Is.EqualTo(0)); } /// /// When an entity is on an unpaused map, it is returned by an EntityQuery. /// [Test] public void UnPaused_NotIncluded_InQuery() { var sim = SimulationFactory(); var entMan = sim.Resolve(); var mapMan = sim.Resolve(); // arrange var mapId = mapMan.CreateMap(); mapMan.SetMapPaused(mapId, false); var newEnt = entMan.SpawnEntity(null, new MapCoordinates(0, 0, mapId)); var query = entMan.EntityQuery(false).ToList(); // 2 ents, map and the spawned one Assert.That(query.Count, Is.EqualTo(2)); } /// /// When an entity is on a paused map, it is get returned by an EntityQuery when included. /// [Test] public void Paused_Included_InQuery() { var sim = SimulationFactory(); var entMan = sim.Resolve(); var mapMan = sim.Resolve(); // arrange var mapId = mapMan.CreateMap(); mapMan.SetMapPaused(mapId, true); entMan.SpawnEntity(null, new MapCoordinates(0, 0, mapId)); var query = entMan.EntityQuery(true).ToList(); // 2 ents, map and the spawned one are returned because includePaused Assert.That(query.Count, Is.EqualTo(2)); } /// /// A new child entity added to a paused map will be created paused. /// [Test] public void Paused_AddEntity_IsPaused() { var sim = SimulationFactory(); var entMan = sim.Resolve(); var mapMan = sim.Resolve(); // arrange var mapId = mapMan.CreateMap(); mapMan.SetMapPaused(mapId, true); var newEnt = entMan.SpawnEntity(null, new MapCoordinates(0, 0, mapId)); var metaData = entMan.GetComponent(newEnt); Assert.That(metaData.EntityPaused, Is.True); } /// /// A new child entity added to an unpaused map will be created unpaused. /// [Test] public void UnPaused_AddEntity_IsNotPaused() { var sim = SimulationFactory(); var entMan = sim.Resolve(); var mapMan = sim.Resolve(); // arrange var mapId = mapMan.CreateMap(); mapMan.SetMapPaused(mapId, false); var newEnt = entMan.SpawnEntity(null, new MapCoordinates(0, 0, mapId)); var metaData = entMan.GetComponent(newEnt); Assert.That(metaData.EntityPaused, Is.False); } /// /// When a new grid is added to a paused map, the grid becomes paused. /// [Test] public void Paused_AddGrid_GridPaused() { var sim = SimulationFactory(); var entMan = sim.Resolve(); var mapMan = sim.Resolve(); // arrange var mapId = mapMan.CreateMap(); mapMan.SetMapPaused(mapId, true); // act var newGrid = mapMan.CreateGrid(mapId); // assert var metaData = entMan.GetComponent(newGrid.Owner); Assert.That(metaData.EntityPaused, Is.True); } /// /// When a tree of entities are teleported from a paused map /// to an unpaused map, all of the entities in the tree are unpaused. /// [Test] public void Paused_TeleportBetweenMaps_Unpaused() { var sim = SimulationFactory(); var entMan = sim.Resolve(); var mapMan = sim.Resolve(); // arrange var map1 = mapMan.CreateMap(); mapMan.SetMapPaused(map1, true); var newEnt = entMan.SpawnEntity(null, new MapCoordinates(0, 0, map1)); var xform = entMan.GetComponent(newEnt); var map2 = mapMan.CreateMap(); mapMan.SetMapPaused(map2, false); // Act entMan.EntitySysManager.GetEntitySystem().SetParent(xform.Owner, mapMan.GetMapEntityId(map2)); var metaData = entMan.GetComponent(newEnt); Assert.That(metaData.EntityPaused, Is.False); } /// /// When a tree of entities are teleported from an unpaused map /// to a paused map, all of the entitites in the tree are paused. /// [Test] public void Unpaused_TeleportBetweenMaps_IsPaused() { var sim = SimulationFactory(); var entMan = sim.Resolve(); var mapMan = sim.Resolve(); // arrange var map1 = mapMan.CreateMap(); mapMan.SetMapPaused(map1, false); var newEnt = entMan.SpawnEntity(null, new MapCoordinates(0, 0, map1)); var xform = entMan.GetComponent(newEnt); var map2 = mapMan.CreateMap(); mapMan.SetMapPaused(map2, true); // Act entMan.EntitySysManager.GetEntitySystem().SetParent(xform.Owner, mapMan.GetMapEntityId(map2)); var metaData = entMan.GetComponent(newEnt); Assert.That(metaData.EntityPaused, Is.True); } /// /// When a paused map is unpaused, all of the entities on the map are unpaused. /// [Test] public void Paused_UnpauseMap_UnpausedEntities() { var sim = SimulationFactory(); var entMan = sim.Resolve(); var mapMan = sim.Resolve(); var mapId = mapMan.CreateMap(); mapMan.SetMapPaused(mapId, true); var newEnt = entMan.SpawnEntity(null, new MapCoordinates(0, 0, mapId)); mapMan.SetMapPaused(mapId, false); var metaData = entMan.GetComponent(newEnt); Assert.That(metaData.EntityPaused, Is.False); } /// /// When an unpaused map is paused, all of the entities on the map are paused. /// [Test] public void Unpaused_PauseMap_PausedEntities() { var sim = SimulationFactory(); var entMan = sim.Resolve(); var mapMan = sim.Resolve(); var mapId = mapMan.CreateMap(); mapMan.SetMapPaused(mapId, false); var newEnt = entMan.SpawnEntity(null, new MapCoordinates(0, 0, mapId)); mapMan.SetMapPaused(mapId, true); var metaData = entMan.GetComponent(newEnt); Assert.That(metaData.EntityPaused, Is.True); } /// /// An unallocated MapId is always unpaused. /// [Test] public void UnallocatedMap_IsUnPaused() { var sim = SimulationFactory(); var entMan = sim.Resolve(); var mapMan = (IMapManagerInternal)sim.Resolve(); // some random unallocated MapId var paused = mapMan.IsMapPaused(new MapId(12)); Assert.That(paused, Is.False); } /// /// Nullspace is always unpaused, and setting it is a no-op. /// [Test] public void Nullspace_Pause_IsUnPaused() { var sim = SimulationFactory(); var entMan = sim.Resolve(); var mapMan = (IMapManagerInternal)sim.Resolve(); mapMan.SetMapPaused(MapId.Nullspace, true); var paused = mapMan.IsMapPaused(MapId.Nullspace); Assert.That(paused, Is.False); } /// /// An allocated MapId without an allocated entity (Nullspace) is always unpaused. /// [Test] public void Nullspace_IsUnPaused() { var sim = SimulationFactory(); var entMan = sim.Resolve(); var mapMan = (IMapManagerInternal)sim.Resolve(); var paused = mapMan.IsMapPaused(MapId.Nullspace); Assert.That(paused, Is.False); } /// /// A freed MapId is always unpaused. /// [Test] public void Unpaused_Freed_IsUnPaused() { var sim = SimulationFactory(); var entMan = sim.Resolve(); var mapMan = (IMapManagerInternal)sim.Resolve(); var paused = mapMan.IsMapPaused(MapId.Nullspace); Assert.That(paused, Is.False); } }