HumanInventoryUniformSlotsTest.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using Content.Shared.Inventory;
  2. using Robust.Shared.GameObjects;
  3. namespace Content.IntegrationTests.Tests
  4. {
  5. // Tests the behavior of InventoryComponent.
  6. // i.e. the interaction between uniforms and the pocket/ID slots.
  7. // and also how big items don't fit in pockets.
  8. [TestFixture]
  9. public sealed class HumanInventoryUniformSlotsTest
  10. {
  11. [TestPrototypes]
  12. private const string Prototypes = @"
  13. - type: entity
  14. name: HumanUniformDummy
  15. id: HumanUniformDummy
  16. components:
  17. - type: Inventory
  18. - type: ContainerContainer
  19. - type: entity
  20. name: UniformDummy
  21. id: UniformDummy
  22. components:
  23. - type: Clothing
  24. slots: [innerclothing]
  25. - type: Item
  26. size: Tiny
  27. - type: entity
  28. name: IDCardDummy
  29. id: IDCardDummy
  30. components:
  31. - type: Clothing
  32. slots:
  33. - idcard
  34. - type: Item
  35. size: Tiny
  36. - type: IdCard
  37. - type: entity
  38. name: FlashlightDummy
  39. id: FlashlightDummy
  40. components:
  41. - type: Item
  42. size: Tiny
  43. - type: entity
  44. name: ToolboxDummy
  45. id: ToolboxDummy
  46. components:
  47. - type: Item
  48. size: Huge
  49. ";
  50. [Test]
  51. public async Task Test()
  52. {
  53. await using var pair = await PoolManager.GetServerClient();
  54. var server = pair.Server;
  55. var testMap = await pair.CreateTestMap();
  56. var coordinates = testMap.GridCoords;
  57. EntityUid human = default;
  58. EntityUid uniform = default;
  59. EntityUid idCard = default;
  60. EntityUid pocketItem = default;
  61. InventorySystem invSystem = default!;
  62. var mapSystem = server.System<SharedMapSystem>();
  63. var entityMan = server.ResolveDependency<IEntityManager>();
  64. await server.WaitAssertion(() =>
  65. {
  66. invSystem = entityMan.System<InventorySystem>();
  67. human = entityMan.SpawnEntity("HumanUniformDummy", coordinates);
  68. uniform = entityMan.SpawnEntity("UniformDummy", coordinates);
  69. idCard = entityMan.SpawnEntity("IDCardDummy", coordinates);
  70. pocketItem = entityMan.SpawnEntity("FlashlightDummy", coordinates);
  71. var tooBigItem = entityMan.SpawnEntity("ToolboxDummy", coordinates);
  72. Assert.Multiple(() =>
  73. {
  74. Assert.That(invSystem.CanEquip(human, uniform, "jumpsuit", out _));
  75. // Can't equip any of these since no uniform!
  76. Assert.That(invSystem.CanEquip(human, idCard, "id", out _), Is.False);
  77. Assert.That(invSystem.CanEquip(human, pocketItem, "pocket1", out _), Is.False);
  78. Assert.That(invSystem.CanEquip(human, tooBigItem, "pocket2", out _), Is.False); // This one fails either way.
  79. });
  80. Assert.Multiple(() =>
  81. {
  82. Assert.That(invSystem.TryEquip(human, uniform, "jumpsuit"));
  83. Assert.That(invSystem.TryEquip(human, idCard, "id"));
  84. });
  85. #pragma warning disable NUnit2045
  86. Assert.That(invSystem.CanEquip(human, tooBigItem, "pocket1", out _), Is.False); // Still failing!
  87. Assert.That(invSystem.TryEquip(human, pocketItem, "pocket1"));
  88. #pragma warning restore NUnit2045
  89. Assert.Multiple(() =>
  90. {
  91. Assert.That(IsDescendant(idCard, human, entityMan));
  92. Assert.That(IsDescendant(pocketItem, human, entityMan));
  93. });
  94. // Now drop the jumpsuit.
  95. Assert.That(invSystem.TryUnequip(human, "jumpsuit"));
  96. });
  97. await server.WaitRunTicks(2);
  98. await server.WaitAssertion(() =>
  99. {
  100. Assert.Multiple(() =>
  101. {
  102. // Items have been dropped!
  103. Assert.That(IsDescendant(uniform, human, entityMan), Is.False);
  104. Assert.That(IsDescendant(idCard, human, entityMan), Is.False);
  105. Assert.That(IsDescendant(pocketItem, human, entityMan), Is.False);
  106. // Ensure everything null here.
  107. Assert.That(!invSystem.TryGetSlotEntity(human, "jumpsuit", out _));
  108. Assert.That(!invSystem.TryGetSlotEntity(human, "id", out _));
  109. Assert.That(!invSystem.TryGetSlotEntity(human, "pocket1", out _));
  110. });
  111. mapSystem.DeleteMap(testMap.MapId);
  112. });
  113. await pair.CleanReturnAsync();
  114. }
  115. private static bool IsDescendant(EntityUid descendant, EntityUid parent, IEntityManager entManager)
  116. {
  117. var xforms = entManager.GetEntityQuery<TransformComponent>();
  118. var tmpParent = xforms.GetComponent(descendant).ParentUid;
  119. while (tmpParent.IsValid())
  120. {
  121. if (tmpParent == parent)
  122. {
  123. return true;
  124. }
  125. tmpParent = xforms.GetComponent(tmpParent).ParentUid;
  126. }
  127. return false;
  128. }
  129. }
  130. }