ItemSpriteTest.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #nullable enable
  2. using System.Collections.Generic;
  3. using Content.Shared.Item;
  4. using Robust.Client.GameObjects;
  5. using Robust.Shared.GameObjects;
  6. using Robust.Shared.Prototypes;
  7. namespace Content.IntegrationTests.Tests.Sprite;
  8. /// <summary>
  9. /// This test checks that all items have a visible sprite. The general rationale is that all items can be picked up
  10. /// by players, thus they need to be visible and have a sprite that can be rendered on screen and in their hands GUI.
  11. /// This has nothing to do with in-hand sprites.
  12. /// </summary>
  13. /// <remarks>
  14. /// If a prototype fails this test, its probably either because it:
  15. /// - Should be marked abstract
  16. /// - inherits from BaseItem despite not being an item
  17. /// - Shouldn't have an item component
  18. /// - Is missing the required sprite information.
  19. /// If none of the abveo are true, it might need to be added to the list of ignored components, see
  20. /// <see cref="Ignored"/>
  21. /// </remarks>
  22. [TestFixture]
  23. public sealed class PrototypeSaveTest
  24. {
  25. private static readonly HashSet<string> Ignored = new()
  26. {
  27. // The only prototypes that should get ignored are those that REQUIRE setup to get a sprite. At that point it is
  28. // the responsibility of the spawner to ensure that a valid sprite is set.
  29. "VirtualItem"
  30. };
  31. [Test]
  32. public async Task AllItemsHaveSpritesTest()
  33. {
  34. var settings = new PoolSettings() { Connected = true }; // client needs to be in-game
  35. await using var pair = await PoolManager.GetServerClient(settings);
  36. List<EntityPrototype> badPrototypes = [];
  37. await pair.Client.WaitPost(() =>
  38. {
  39. foreach (var (proto, _) in pair.GetPrototypesWithComponent<ItemComponent>(Ignored))
  40. {
  41. var dummy = pair.Client.EntMan.Spawn(proto.ID);
  42. pair.Client.EntMan.RunMapInit(dummy, pair.Client.MetaData(dummy));
  43. var spriteComponent = pair.Client.EntMan.GetComponentOrNull<SpriteComponent>(dummy);
  44. if (spriteComponent?.Icon == null)
  45. badPrototypes.Add(proto);
  46. pair.Client.EntMan.DeleteEntity(dummy);
  47. }
  48. });
  49. Assert.Multiple(() =>
  50. {
  51. foreach (var proto in badPrototypes)
  52. {
  53. Assert.Fail($"Item prototype has no sprite: {proto.ID}. It should probably either be marked as abstract, not be an item, or have a valid sprite");
  54. }
  55. });
  56. await pair.CleanReturnAsync();
  57. }
  58. }