Files
crystall-punk-14/Content.IntegrationTests/Tests/Interaction/Click/InteractionSystemTests.cs

415 lines
17 KiB
C#
Raw Permalink Normal View History

#nullable enable annotations
using System.Numerics;
Add test pooling (#4961) * Add test pooling * WIP test pooling changes * Fix Destructible tests * Don't pool unpooled or dummy ticker instances * Change ServerPathfindingDebugSystem to replace existing entries * Fix SaveLoadSaveTest comment * Don't pool StartTest * Comment out global setup * Fix puddle tests * Move SolarPanelComponent initialize to PowerSolarSystem OnMapInit * Update RobustToolbox * Finish fixing tests, make test threads background threads * Bring back pooling * Fix nullable * Update RobustToolbox * Set cvars on server return * Un-pool tests with custom cvars * Update RobustToolbox * Update RobustToolbox * Change where the main tile coordinates are * Remove DisposalUnitTest grid check * Fix test pooling being a fickle bitch * Fix EntitySystemExtensionsTest * Update RobustToolbox * Update RobustToolbox * Make nullable pool settings true * Update RobustToolbox * Wait other way around * We are unitystation now * Update RobustToolbox * Create global setup * Pool some more tests * Fix not properly disconnecting clients before restarting the round * Give more info on ran tests * Standardize default test cvars * Update RobustToolbox * Update RobustToolbox * Pool clients * Fix test order issue * Fix cvars in character creation test not being set properly * Update RobustToolbox * Update RobustToolbox * Rider shut * Update RobustToolbox * Format tests ran better * Update RobustToolbox * Reset RobustToolbox * Reset RobustToolbox harder * Fix one instance of test order causing destructible tests to fail
2021-11-06 11:49:59 +01:00
using Content.Server.Interaction;
using Content.Shared.Hands.Components;
2022-03-17 20:13:31 +13:00
using Content.Shared.Hands.EntitySystems;
Add test pooling (#4961) * Add test pooling * WIP test pooling changes * Fix Destructible tests * Don't pool unpooled or dummy ticker instances * Change ServerPathfindingDebugSystem to replace existing entries * Fix SaveLoadSaveTest comment * Don't pool StartTest * Comment out global setup * Fix puddle tests * Move SolarPanelComponent initialize to PowerSolarSystem OnMapInit * Update RobustToolbox * Finish fixing tests, make test threads background threads * Bring back pooling * Fix nullable * Update RobustToolbox * Set cvars on server return * Un-pool tests with custom cvars * Update RobustToolbox * Update RobustToolbox * Change where the main tile coordinates are * Remove DisposalUnitTest grid check * Fix test pooling being a fickle bitch * Fix EntitySystemExtensionsTest * Update RobustToolbox * Update RobustToolbox * Make nullable pool settings true * Update RobustToolbox * Wait other way around * We are unitystation now * Update RobustToolbox * Create global setup * Pool some more tests * Fix not properly disconnecting clients before restarting the round * Give more info on ran tests * Standardize default test cvars * Update RobustToolbox * Update RobustToolbox * Pool clients * Fix test order issue * Fix cvars in character creation test not being set properly * Update RobustToolbox * Update RobustToolbox * Rider shut * Update RobustToolbox * Format tests ran better * Update RobustToolbox * Reset RobustToolbox * Reset RobustToolbox harder * Fix one instance of test order causing destructible tests to fail
2021-11-06 11:49:59 +01:00
using Content.Shared.Interaction;
using Content.Shared.Interaction.Components;
2022-07-27 03:53:47 -07:00
using Content.Shared.Item;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Reflection;
namespace Content.IntegrationTests.Tests.Interaction.Click
{
[TestFixture]
[TestOf(typeof(InteractionSystem))]
public sealed class InteractionSystemTests
{
[TestPrototypes]
private const string Prototypes = @"
- type: entity
id: DummyDebugWall
components:
- type: Physics
bodyType: Dynamic
2021-12-01 18:32:37 +11:00
- type: Fixtures
fixtures:
2023-05-06 16:26:15 +10:00
fix1:
shape:
!type:PhysShapeAabb
bounds: ""-0.25,-0.25,0.25,0.25""
layer:
- MobMask
mask:
- MobMask
";
[Test]
public async Task InteractionTest()
{
2023-08-25 02:56:51 +02:00
await using var pair = await PoolManager.GetServerClient();
var server = pair.Server;
2021-12-05 18:09:01 +01:00
var sEntities = server.ResolveDependency<IEntityManager>();
var mapManager = server.ResolveDependency<IMapManager>();
2022-03-17 20:13:31 +13:00
var sysMan = server.ResolveDependency<IEntitySystemManager>();
var handSys = sysMan.GetEntitySystem<SharedHandsSystem>();
var map = await pair.CreateTestMap();
var mapId = map.MapId;
var coords = map.MapCoords;
await server.WaitIdleAsync();
2021-12-05 18:09:01 +01:00
EntityUid user = default;
EntityUid target = default;
EntityUid item = default;
await server.WaitAssertion(() =>
{
2021-12-05 18:09:01 +01:00
user = sEntities.SpawnEntity(null, coords);
2023-10-11 02:19:46 -07:00
sEntities.EnsureComponent<HandsComponent>(user);
sEntities.EnsureComponent<ComplexInteractionComponent>(user);
2022-03-17 20:13:31 +13:00
handSys.AddHand(user, "hand", HandLocation.Left);
2021-12-05 18:09:01 +01:00
target = sEntities.SpawnEntity(null, coords);
item = sEntities.SpawnEntity(null, coords);
2023-10-11 02:19:46 -07:00
sEntities.EnsureComponent<ItemComponent>(item);
});
await server.WaitRunTicks(1);
var entitySystemManager = server.ResolveDependency<IEntitySystemManager>();
InteractionSystem interactionSystem = default!;
TestInteractionSystem testInteractionSystem = default!;
Assert.Multiple(() =>
{
Assert.That(entitySystemManager.TryGetEntitySystem(out interactionSystem));
Assert.That(entitySystemManager.TryGetEntitySystem(out testInteractionSystem));
});
var interactUsing = false;
var interactHand = false;
await server.WaitAssertion(() =>
{
testInteractionSystem.InteractUsingEvent = (ev) => { Assert.That(ev.Target, Is.EqualTo(target)); interactUsing = true; };
testInteractionSystem.InteractHandEvent = (ev) => { Assert.That(ev.Target, Is.EqualTo(target)); interactHand = true; };
2021-12-05 18:09:01 +01:00
interactionSystem.UserInteraction(user, sEntities.GetComponent<TransformComponent>(target).Coordinates, target);
Assert.Multiple(() =>
{
Assert.That(interactUsing, Is.False);
Assert.That(interactHand);
});
2022-03-17 20:13:31 +13:00
Assert.That(handSys.TryPickup(user, item));
2021-12-05 18:09:01 +01:00
interactionSystem.UserInteraction(user, sEntities.GetComponent<TransformComponent>(target).Coordinates, target);
Assert.That(interactUsing);
});
2023-04-15 07:41:25 +12:00
testInteractionSystem.ClearHandlers();
2023-08-25 02:56:51 +02:00
await pair.CleanReturnAsync();
}
[Test]
public async Task InteractionObstructionTest()
{
2023-08-25 02:56:51 +02:00
await using var pair = await PoolManager.GetServerClient();
var server = pair.Server;
2021-12-05 18:09:01 +01:00
var sEntities = server.ResolveDependency<IEntityManager>();
var mapManager = server.ResolveDependency<IMapManager>();
2022-03-17 20:13:31 +13:00
var sysMan = server.ResolveDependency<IEntitySystemManager>();
var handSys = sysMan.GetEntitySystem<SharedHandsSystem>();
var map = await pair.CreateTestMap();
var mapId = map.MapId;
var coords = map.MapCoords;
await server.WaitIdleAsync();
2021-12-05 18:09:01 +01:00
EntityUid user = default;
EntityUid target = default;
EntityUid item = default;
EntityUid wall = default;
await server.WaitAssertion(() =>
{
2021-12-05 18:09:01 +01:00
user = sEntities.SpawnEntity(null, coords);
2023-10-11 02:19:46 -07:00
sEntities.EnsureComponent<HandsComponent>(user);
2022-03-17 20:13:31 +13:00
handSys.AddHand(user, "hand", HandLocation.Left);
target = sEntities.SpawnEntity(null, new MapCoordinates(new Vector2(1.9f, 0), mapId));
2021-12-05 18:09:01 +01:00
item = sEntities.SpawnEntity(null, coords);
2023-10-11 02:19:46 -07:00
sEntities.EnsureComponent<ItemComponent>(item);
wall = sEntities.SpawnEntity("DummyDebugWall", new MapCoordinates(new Vector2(1, 0), sEntities.GetComponent<TransformComponent>(user).MapID));
});
await server.WaitRunTicks(1);
var entitySystemManager = server.ResolveDependency<IEntitySystemManager>();
InteractionSystem interactionSystem = default!;
TestInteractionSystem testInteractionSystem = default!;
Assert.Multiple(() =>
{
Assert.That(entitySystemManager.TryGetEntitySystem(out interactionSystem));
Assert.That(entitySystemManager.TryGetEntitySystem(out testInteractionSystem));
});
var interactUsing = false;
var interactHand = false;
await server.WaitAssertion(() =>
{
testInteractionSystem.InteractUsingEvent = (ev) => { Assert.That(ev.Target, Is.EqualTo(target)); interactUsing = true; };
testInteractionSystem.InteractHandEvent = (ev) => { Assert.That(ev.Target, Is.EqualTo(target)); interactHand = true; };
2021-12-05 18:09:01 +01:00
interactionSystem.UserInteraction(user, sEntities.GetComponent<TransformComponent>(target).Coordinates, target);
Assert.Multiple(() =>
{
Assert.That(interactUsing, Is.False);
Assert.That(interactHand, Is.False);
});
2022-03-17 20:13:31 +13:00
Assert.That(handSys.TryPickup(user, item));
2021-12-05 18:09:01 +01:00
interactionSystem.UserInteraction(user, sEntities.GetComponent<TransformComponent>(target).Coordinates, target);
Assert.That(interactUsing, Is.False);
});
2023-04-15 07:41:25 +12:00
testInteractionSystem.ClearHandlers();
2023-08-25 02:56:51 +02:00
await pair.CleanReturnAsync();
}
[Test]
public async Task InteractionInRangeTest()
{
2023-08-25 02:56:51 +02:00
await using var pair = await PoolManager.GetServerClient();
var server = pair.Server;
2021-12-05 18:09:01 +01:00
var sEntities = server.ResolveDependency<IEntityManager>();
var mapManager = server.ResolveDependency<IMapManager>();
2022-03-17 20:13:31 +13:00
var sysMan = server.ResolveDependency<IEntitySystemManager>();
var handSys = sysMan.GetEntitySystem<SharedHandsSystem>();
var map = await pair.CreateTestMap();
var mapId = map.MapId;
var coords = map.MapCoords;
await server.WaitIdleAsync();
2021-12-05 18:09:01 +01:00
EntityUid user = default;
EntityUid target = default;
EntityUid item = default;
await server.WaitAssertion(() =>
{
2021-12-05 18:09:01 +01:00
user = sEntities.SpawnEntity(null, coords);
2023-10-11 02:19:46 -07:00
sEntities.EnsureComponent<HandsComponent>(user);
sEntities.EnsureComponent<ComplexInteractionComponent>(user);
2022-03-17 20:13:31 +13:00
handSys.AddHand(user, "hand", HandLocation.Left);
target = sEntities.SpawnEntity(null, new MapCoordinates(new Vector2(SharedInteractionSystem.InteractionRange - 0.1f, 0), mapId));
2021-12-05 18:09:01 +01:00
item = sEntities.SpawnEntity(null, coords);
2023-10-11 02:19:46 -07:00
sEntities.EnsureComponent<ItemComponent>(item);
});
await server.WaitRunTicks(1);
var entitySystemManager = server.ResolveDependency<IEntitySystemManager>();
InteractionSystem interactionSystem = default!;
TestInteractionSystem testInteractionSystem = default!;
Assert.Multiple(() =>
{
Assert.That(entitySystemManager.TryGetEntitySystem(out interactionSystem));
Assert.That(entitySystemManager.TryGetEntitySystem(out testInteractionSystem));
});
var interactUsing = false;
var interactHand = false;
await server.WaitAssertion(() =>
{
testInteractionSystem.InteractUsingEvent = (ev) => { Assert.That(ev.Target, Is.EqualTo(target)); interactUsing = true; };
testInteractionSystem.InteractHandEvent = (ev) => { Assert.That(ev.Target, Is.EqualTo(target)); interactHand = true; };
2021-12-05 18:09:01 +01:00
interactionSystem.UserInteraction(user, sEntities.GetComponent<TransformComponent>(target).Coordinates, target);
Assert.Multiple(() =>
{
Assert.That(interactUsing, Is.False);
Assert.That(interactHand);
});
2022-03-17 20:13:31 +13:00
Assert.That(handSys.TryPickup(user, item));
2021-12-05 18:09:01 +01:00
interactionSystem.UserInteraction(user, sEntities.GetComponent<TransformComponent>(target).Coordinates, target);
Assert.That(interactUsing);
});
2023-04-15 07:41:25 +12:00
testInteractionSystem.ClearHandlers();
2023-08-25 02:56:51 +02:00
await pair.CleanReturnAsync();
}
[Test]
public async Task InteractionOutOfRangeTest()
{
2023-08-25 02:56:51 +02:00
await using var pair = await PoolManager.GetServerClient();
var server = pair.Server;
2021-12-05 18:09:01 +01:00
var sEntities = server.ResolveDependency<IEntityManager>();
var mapManager = server.ResolveDependency<IMapManager>();
2022-03-17 20:13:31 +13:00
var sysMan = server.ResolveDependency<IEntitySystemManager>();
var handSys = sysMan.GetEntitySystem<SharedHandsSystem>();
var map = await pair.CreateTestMap();
var mapId = map.MapId;
var coords = map.MapCoords;
await server.WaitIdleAsync();
2021-12-05 18:09:01 +01:00
EntityUid user = default;
EntityUid target = default;
EntityUid item = default;
await server.WaitAssertion(() =>
{
2021-12-05 18:09:01 +01:00
user = sEntities.SpawnEntity(null, coords);
2023-10-11 02:19:46 -07:00
sEntities.EnsureComponent<HandsComponent>(user);
2022-03-17 20:13:31 +13:00
handSys.AddHand(user, "hand", HandLocation.Left);
target = sEntities.SpawnEntity(null, new MapCoordinates(new Vector2(SharedInteractionSystem.InteractionRange + 0.01f, 0), mapId));
2021-12-05 18:09:01 +01:00
item = sEntities.SpawnEntity(null, coords);
2023-10-11 02:19:46 -07:00
sEntities.EnsureComponent<ItemComponent>(item);
});
await server.WaitRunTicks(1);
var entitySystemManager = server.ResolveDependency<IEntitySystemManager>();
InteractionSystem interactionSystem = default!;
TestInteractionSystem testInteractionSystem = default!;
Assert.Multiple(() =>
{
Assert.That(entitySystemManager.TryGetEntitySystem(out interactionSystem));
Assert.That(entitySystemManager.TryGetEntitySystem(out testInteractionSystem));
});
var interactUsing = false;
var interactHand = false;
await server.WaitAssertion(() =>
{
testInteractionSystem.InteractUsingEvent = (ev) => { Assert.That(ev.Target, Is.EqualTo(target)); interactUsing = true; };
testInteractionSystem.InteractHandEvent = (ev) => { Assert.That(ev.Target, Is.EqualTo(target)); interactHand = true; };
2021-12-05 18:09:01 +01:00
interactionSystem.UserInteraction(user, sEntities.GetComponent<TransformComponent>(target).Coordinates, target);
Assert.Multiple(() =>
{
Assert.That(interactUsing, Is.False);
Assert.That(interactHand, Is.False);
});
2022-03-17 20:13:31 +13:00
Assert.That(handSys.TryPickup(user, item));
2021-12-05 18:09:01 +01:00
interactionSystem.UserInteraction(user, sEntities.GetComponent<TransformComponent>(target).Coordinates, target);
Assert.That(interactUsing, Is.False);
});
2023-04-15 07:41:25 +12:00
testInteractionSystem.ClearHandlers();
2023-08-25 02:56:51 +02:00
await pair.CleanReturnAsync();
}
[Test]
public async Task InsideContainerInteractionBlockTest()
{
2023-08-25 02:56:51 +02:00
await using var pair = await PoolManager.GetServerClient();
var server = pair.Server;
2021-12-05 18:09:01 +01:00
var sEntities = server.ResolveDependency<IEntityManager>();
var mapManager = server.ResolveDependency<IMapManager>();
2022-03-17 20:13:31 +13:00
var sysMan = server.ResolveDependency<IEntitySystemManager>();
var handSys = sysMan.GetEntitySystem<SharedHandsSystem>();
var conSystem = sysMan.GetEntitySystem<SharedContainerSystem>();
var map = await pair.CreateTestMap();
var mapId = map.MapId;
var coords = map.MapCoords;
await server.WaitIdleAsync();
2021-12-05 18:09:01 +01:00
EntityUid user = default;
EntityUid target = default;
EntityUid item = default;
EntityUid containerEntity = default;
BaseContainer container = null;
await server.WaitAssertion(() =>
{
2021-12-05 18:09:01 +01:00
user = sEntities.SpawnEntity(null, coords);
2023-10-11 02:19:46 -07:00
sEntities.EnsureComponent<HandsComponent>(user);
sEntities.EnsureComponent<ComplexInteractionComponent>(user);
2022-03-17 20:13:31 +13:00
handSys.AddHand(user, "hand", HandLocation.Left);
2021-12-05 18:09:01 +01:00
target = sEntities.SpawnEntity(null, coords);
item = sEntities.SpawnEntity(null, coords);
2023-10-11 02:19:46 -07:00
sEntities.EnsureComponent<ItemComponent>(item);
2021-12-05 18:09:01 +01:00
containerEntity = sEntities.SpawnEntity(null, coords);
container = conSystem.EnsureContainer<Container>(containerEntity, "InteractionTestContainer");
});
await server.WaitRunTicks(1);
var entitySystemManager = server.ResolveDependency<IEntitySystemManager>();
InteractionSystem interactionSystem = default!;
TestInteractionSystem testInteractionSystem = default!;
Assert.Multiple(() =>
{
Assert.That(entitySystemManager.TryGetEntitySystem(out interactionSystem));
Assert.That(entitySystemManager.TryGetEntitySystem(out testInteractionSystem));
});
await server.WaitIdleAsync();
var interactUsing = false;
var interactHand = false;
await server.WaitAssertion(() =>
{
#pragma warning disable NUnit2045 // Interdependent assertions.
2023-12-27 21:30:03 -08:00
Assert.That(conSystem.Insert(user, container));
Assert.That(sEntities.GetComponent<TransformComponent>(user).ParentUid, Is.EqualTo(containerEntity));
#pragma warning restore NUnit2045
testInteractionSystem.InteractUsingEvent = (ev) => { Assert.That(ev.Target, Is.EqualTo(containerEntity)); interactUsing = true; };
testInteractionSystem.InteractHandEvent = (ev) => { Assert.That(ev.Target, Is.EqualTo(containerEntity)); interactHand = true; };
2021-12-05 18:09:01 +01:00
interactionSystem.UserInteraction(user, sEntities.GetComponent<TransformComponent>(target).Coordinates, target);
Assert.Multiple(() =>
{
Assert.That(interactUsing, Is.False);
Assert.That(interactHand, Is.False);
});
2021-12-05 18:09:01 +01:00
interactionSystem.UserInteraction(user, sEntities.GetComponent<TransformComponent>(containerEntity).Coordinates, containerEntity);
Assert.Multiple(() =>
{
Assert.That(interactUsing, Is.False);
Assert.That(interactHand);
});
2022-03-17 20:13:31 +13:00
Assert.That(handSys.TryPickup(user, item));
2021-12-05 18:09:01 +01:00
interactionSystem.UserInteraction(user, sEntities.GetComponent<TransformComponent>(target).Coordinates, target);
Assert.That(interactUsing, Is.False);
2021-12-05 18:09:01 +01:00
interactionSystem.UserInteraction(user, sEntities.GetComponent<TransformComponent>(containerEntity).Coordinates, containerEntity);
Assert.That(interactUsing, Is.True);
});
2023-04-15 07:41:25 +12:00
testInteractionSystem.ClearHandlers();
2023-08-25 02:56:51 +02:00
await pair.CleanReturnAsync();
}
public sealed class TestInteractionSystem : EntitySystem
{
public EntityEventHandler<InteractUsingEvent>? InteractUsingEvent;
public EntityEventHandler<InteractHandEvent>? InteractHandEvent;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<InteractUsingEvent>((e) => InteractUsingEvent?.Invoke(e));
SubscribeLocalEvent<InteractHandEvent>((e) => InteractHandEvent?.Invoke(e));
}
2023-04-15 07:41:25 +12:00
public void ClearHandlers()
{
InteractUsingEvent = null;
InteractHandEvent = null;
}
}
}
}