ECSatize AlertsSystem (#5559)

This commit is contained in:
Acruid
2022-01-05 00:19:23 -08:00
committed by GitHub
parent 36d4de5e61
commit 5b1cd2dd96
59 changed files with 1069 additions and 1038 deletions

View File

@@ -1,14 +1,17 @@
using System.IO;
using Content.Shared.Alert;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Prototypes;
using Robust.Shared.Reflection;
using Robust.Shared.Serialization.Manager;
using Robust.Shared.Utility;
namespace Content.Tests.Shared.Alert
{
[TestFixture, TestOf(typeof(AlertManager))]
[TestFixture, TestOf(typeof(AlertsSystem))]
public class AlertManagerTests : ContentUnitTest
{
const string PROTOTYPES = @"
@@ -24,23 +27,26 @@ namespace Content.Tests.Shared.Alert
";
[Test]
[Ignore("There is no way to load extra Systems in a unit test, fixing RobustUnitTest is out of scope.")]
public void TestAlertManager()
{
IoCManager.Resolve<ISerializationManager>().Initialize();
var reflection = IoCManager.Resolve<IReflectionManager>();
reflection.LoadAssemblies();
var prototypeManager = IoCManager.Resolve<IPrototypeManager>();
prototypeManager.Initialize();
prototypeManager.LoadFromStream(new StringReader(PROTOTYPES));
var alertManager = IoCManager.Resolve<AlertManager>();
alertManager.Initialize();
Assert.That(alertManager.TryGet(AlertType.LowPressure, out var lowPressure));
Assert.That(EntitySystem.Get<AlertsSystem>().TryGet(AlertType.LowPressure, out var lowPressure));
Assert.That(lowPressure.Icon, Is.EqualTo(new SpriteSpecifier.Texture(new ResourcePath("/Textures/Interface/Alerts/Pressure/lowpressure.png"))));
Assert.That(alertManager.TryGet(AlertType.HighPressure, out var highPressure));
Assert.That(EntitySystem.Get<AlertsSystem>().TryGet(AlertType.HighPressure, out var highPressure));
Assert.That(highPressure.Icon, Is.EqualTo(new SpriteSpecifier.Texture(new ResourcePath("/Textures/Interface/Alerts/Pressure/highpressure.png"))));
Assert.That(alertManager.TryGet(AlertType.LowPressure, out lowPressure));
Assert.That(EntitySystem.Get<AlertsSystem>().TryGet(AlertType.LowPressure, out lowPressure));
Assert.That(lowPressure.Icon, Is.EqualTo(new SpriteSpecifier.Texture(new ResourcePath("/Textures/Interface/Alerts/Pressure/lowpressure.png"))));
Assert.That(alertManager.TryGet(AlertType.HighPressure, out highPressure));
Assert.That(EntitySystem.Get<AlertsSystem>().TryGet(AlertType.HighPressure, out highPressure));
Assert.That(highPressure.Icon, Is.EqualTo(new SpriteSpecifier.Texture(new ResourcePath("/Textures/Interface/Alerts/Pressure/highpressure.png"))));
}
}

View File

@@ -1,4 +1,5 @@
using System.IO;
using System;
using System.IO;
using Content.Server.Alert;
using Content.Shared.Alert;
using NUnit.Framework;
@@ -7,10 +8,10 @@ using Robust.Shared.IoC;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.Manager;
namespace Content.Tests.Server.GameObjects.Components.Mobs
namespace Content.Tests.Shared.Alert
{
[TestFixture]
[TestOf(typeof(ServerAlertsComponent))]
[TestOf(typeof(AlertsComponent))]
public class ServerAlertsComponentTests : ContentUnitTest
{
const string PROTOTYPES = @"
@@ -28,6 +29,7 @@ namespace Content.Tests.Server.GameObjects.Components.Mobs
";
[Test]
[Ignore("There is no way to load extra Systems in a unit test, fixing RobustUnitTest is out of scope.")]
public void ShowAlerts()
{
// this is kind of unnecessary because there's integration test coverage of Alert components
@@ -38,31 +40,31 @@ namespace Content.Tests.Server.GameObjects.Components.Mobs
var prototypeManager = IoCManager.Resolve<IPrototypeManager>();
prototypeManager.Initialize();
var factory = IoCManager.Resolve<IComponentFactory>();
factory.RegisterClass<ServerAlertsComponent>();
factory.RegisterClass<AlertsComponent>();
prototypeManager.LoadFromStream(new StringReader(PROTOTYPES));
prototypeManager.Resync();
var alertManager = IoCManager.Resolve<AlertManager>();
alertManager.Initialize();
var entSys = IoCManager.Resolve<IEntitySystemManager>();
entSys.LoadExtraSystemType<ServerAlertsSystem>();
var alertsComponent = new ServerAlertsComponent();
var alertsComponent = new AlertsComponent();
alertsComponent = IoCManager.InjectDependencies(alertsComponent);
Assert.That(alertManager.TryGet(AlertType.LowPressure, out var lowpressure));
Assert.That(alertManager.TryGet(AlertType.HighPressure, out var highpressure));
Assert.That(EntitySystem.Get<AlertsSystem>().TryGet(AlertType.LowPressure, out var lowpressure));
Assert.That(EntitySystem.Get<AlertsSystem>().TryGet(AlertType.HighPressure, out var highpressure));
alertsComponent.ShowAlert(AlertType.LowPressure);
EntitySystem.Get<AlertsSystem>().ShowAlert(alertsComponent.Owner, AlertType.LowPressure, null, null);
var alertState = alertsComponent.GetComponentState() as AlertsComponentState;
Assert.NotNull(alertState);
Assert.That(alertState.Alerts.Count, Is.EqualTo(1));
Assert.That(alertState.Alerts.ContainsKey(lowpressure.AlertKey));
alertsComponent.ShowAlert(AlertType.HighPressure);
EntitySystem.Get<AlertsSystem>().ShowAlert(alertsComponent.Owner, AlertType.HighPressure, null, null);
alertState = alertsComponent.GetComponentState() as AlertsComponentState;
Assert.That(alertState.Alerts.Count, Is.EqualTo(1));
Assert.That(alertState.Alerts.ContainsKey(highpressure.AlertKey));
alertsComponent.ClearAlertCategory(AlertCategory.Pressure);
EntitySystem.Get<AlertsSystem>().ClearAlertCategory(alertsComponent.Owner, AlertCategory.Pressure);
alertState = alertsComponent.GetComponentState() as AlertsComponentState;
Assert.That(alertState.Alerts.Count, Is.EqualTo(0));
}