2022-01-05 00:19:23 -08:00
|
|
|
using System.IO;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Server.Alert;
|
2020-11-09 20:22:19 -08:00
|
|
|
using Content.Shared.Alert;
|
|
|
|
|
using NUnit.Framework;
|
2021-02-11 01:13:03 -08:00
|
|
|
using Robust.Shared.GameObjects;
|
2023-07-13 20:20:46 +10:00
|
|
|
using Robust.Shared.GameStates;
|
2020-11-09 20:22:19 -08:00
|
|
|
using Robust.Shared.IoC;
|
|
|
|
|
using Robust.Shared.Prototypes;
|
2021-03-05 01:08:38 +01:00
|
|
|
using Robust.Shared.Serialization.Manager;
|
2020-11-09 20:22:19 -08:00
|
|
|
|
2022-01-05 00:19:23 -08:00
|
|
|
namespace Content.Tests.Shared.Alert
|
2020-11-09 20:22:19 -08:00
|
|
|
{
|
|
|
|
|
[TestFixture]
|
2022-01-05 00:19:23 -08:00
|
|
|
[TestOf(typeof(AlertsComponent))]
|
2022-02-16 00:23:23 -07:00
|
|
|
public sealed class ServerAlertsComponentTests : ContentUnitTest
|
2020-11-09 20:22:19 -08:00
|
|
|
{
|
|
|
|
|
const string PROTOTYPES = @"
|
|
|
|
|
- type: alert
|
2022-04-03 02:01:22 +02:00
|
|
|
id: LowPressure
|
2020-11-09 20:22:19 -08:00
|
|
|
category: Pressure
|
|
|
|
|
icon: /Textures/Interface/Alerts/Pressure/lowpressure.png
|
|
|
|
|
|
|
|
|
|
- type: alert
|
2022-04-03 02:01:22 +02:00
|
|
|
id: HighPressure
|
2020-11-09 20:22:19 -08:00
|
|
|
category: Pressure
|
|
|
|
|
icon: /Textures/Interface/Alerts/Pressure/highpressure.png
|
|
|
|
|
";
|
|
|
|
|
|
|
|
|
|
[Test]
|
2022-01-05 00:19:23 -08:00
|
|
|
[Ignore("There is no way to load extra Systems in a unit test, fixing RobustUnitTest is out of scope.")]
|
2020-11-09 20:22:19 -08:00
|
|
|
public void ShowAlerts()
|
|
|
|
|
{
|
|
|
|
|
// this is kind of unnecessary because there's integration test coverage of Alert components
|
|
|
|
|
// but wanted to keep it anyway to see what's possible w.r.t. testing components
|
|
|
|
|
// in a unit test
|
|
|
|
|
|
2023-07-13 20:20:46 +10:00
|
|
|
var entManager = IoCManager.Resolve<IEntityManager>();
|
2021-03-05 01:08:38 +01:00
|
|
|
IoCManager.Resolve<ISerializationManager>().Initialize();
|
2020-11-09 20:22:19 -08:00
|
|
|
var prototypeManager = IoCManager.Resolve<IPrototypeManager>();
|
2021-09-04 14:13:34 +10:00
|
|
|
prototypeManager.Initialize();
|
2020-11-09 20:22:19 -08:00
|
|
|
var factory = IoCManager.Resolve<IComponentFactory>();
|
2022-01-05 00:19:23 -08:00
|
|
|
factory.RegisterClass<AlertsComponent>();
|
2020-11-09 20:22:19 -08:00
|
|
|
prototypeManager.LoadFromStream(new StringReader(PROTOTYPES));
|
2022-04-03 02:01:22 +02:00
|
|
|
prototypeManager.ResolveResults();
|
2020-11-09 20:22:19 -08:00
|
|
|
|
2023-07-13 20:20:46 +10:00
|
|
|
var entSys = entManager.EntitySysManager;
|
2022-01-05 00:19:23 -08:00
|
|
|
entSys.LoadExtraSystemType<ServerAlertsSystem>();
|
2020-11-09 20:22:19 -08:00
|
|
|
|
2022-01-05 00:19:23 -08:00
|
|
|
var alertsComponent = new AlertsComponent();
|
2020-11-09 20:22:19 -08:00
|
|
|
alertsComponent = IoCManager.InjectDependencies(alertsComponent);
|
|
|
|
|
|
2022-01-05 00:19:23 -08:00
|
|
|
Assert.That(EntitySystem.Get<AlertsSystem>().TryGet(AlertType.LowPressure, out var lowpressure));
|
|
|
|
|
Assert.That(EntitySystem.Get<AlertsSystem>().TryGet(AlertType.HighPressure, out var highpressure));
|
2020-11-09 20:22:19 -08:00
|
|
|
|
2022-01-05 00:19:23 -08:00
|
|
|
EntitySystem.Get<AlertsSystem>().ShowAlert(alertsComponent.Owner, AlertType.LowPressure, null, null);
|
2023-07-13 20:20:46 +10:00
|
|
|
|
|
|
|
|
var getty = new ComponentGetState();
|
|
|
|
|
entManager.EventBus.RaiseComponentEvent(alertsComponent, getty);
|
|
|
|
|
|
2023-09-28 16:20:29 -07:00
|
|
|
var alertState = (AlertsComponent.AlertsComponent_AutoState) getty.State!;
|
2020-11-09 20:22:19 -08:00
|
|
|
Assert.NotNull(alertState);
|
2020-12-13 14:28:20 -08:00
|
|
|
Assert.That(alertState.Alerts.Count, Is.EqualTo(1));
|
|
|
|
|
Assert.That(alertState.Alerts.ContainsKey(lowpressure.AlertKey));
|
2020-11-09 20:22:19 -08:00
|
|
|
|
2022-01-05 00:19:23 -08:00
|
|
|
EntitySystem.Get<AlertsSystem>().ShowAlert(alertsComponent.Owner, AlertType.HighPressure, null, null);
|
2023-07-13 20:20:46 +10:00
|
|
|
|
|
|
|
|
// Lazy
|
|
|
|
|
entManager.EventBus.RaiseComponentEvent(alertsComponent, getty);
|
2023-09-28 16:20:29 -07:00
|
|
|
alertState = (AlertsComponent.AlertsComponent_AutoState) getty.State!;
|
2020-12-13 14:28:20 -08:00
|
|
|
Assert.That(alertState.Alerts.Count, Is.EqualTo(1));
|
|
|
|
|
Assert.That(alertState.Alerts.ContainsKey(highpressure.AlertKey));
|
2020-11-09 20:22:19 -08:00
|
|
|
|
2022-01-05 00:19:23 -08:00
|
|
|
EntitySystem.Get<AlertsSystem>().ClearAlertCategory(alertsComponent.Owner, AlertCategory.Pressure);
|
2023-07-13 20:20:46 +10:00
|
|
|
|
|
|
|
|
entManager.EventBus.RaiseComponentEvent(alertsComponent, getty);
|
2023-09-28 16:20:29 -07:00
|
|
|
alertState = (AlertsComponent.AlertsComponent_AutoState) getty.State!;
|
2020-12-13 14:28:20 -08:00
|
|
|
Assert.That(alertState.Alerts.Count, Is.EqualTo(0));
|
2020-11-09 20:22:19 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|