EmptyInstanceSpawnerTests.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include "VegetationTest.h"
  9. #include "VegetationMocks.h"
  10. #include <AzCore/Component/Entity.h>
  11. #include <AzTest/AzTest.h>
  12. #include <AzCore/UnitTest/TestTypes.h>
  13. #include <Vegetation/EmptyInstanceSpawner.h>
  14. #include <Vegetation/Ebuses/DescriptorNotificationBus.h>
  15. namespace UnitTest
  16. {
  17. // Mock VegetationSystemComponent is needed to reflect only the EmptyInstanceSpawner.
  18. class MockEmptyInstanceVegetationSystemComponent
  19. : public AZ::Component
  20. {
  21. public:
  22. AZ_COMPONENT(MockEmptyInstanceVegetationSystemComponent, "{B2AF429A-4E3A-4A59-A425-5A191733D24A}", AZ::Component);
  23. void Activate() override {}
  24. void Deactivate() override {}
  25. static void Reflect(AZ::ReflectContext* reflect)
  26. {
  27. Vegetation::InstanceSpawner::Reflect(reflect);
  28. Vegetation::EmptyInstanceSpawner::Reflect(reflect);
  29. }
  30. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  31. {
  32. provided.push_back(AZ_CRC_CE("VegetationSystemService"));
  33. }
  34. };
  35. class EmptyInstanceSpawnerTests
  36. : public VegetationComponentTests
  37. , public Vegetation::DescriptorNotificationBus::Handler
  38. {
  39. public:
  40. void RegisterComponentDescriptors() override
  41. {
  42. m_app.RegisterComponentDescriptor(MockEmptyInstanceVegetationSystemComponent::CreateDescriptor());
  43. }
  44. void OnDescriptorAssetsLoaded() override { m_numOnLoadedCalls++; }
  45. int m_numOnLoadedCalls = 0;
  46. };
  47. TEST_F(EmptyInstanceSpawnerTests, BasicInitializationTest)
  48. {
  49. // Basic test to make sure we can construct / destroy without errors.
  50. Vegetation::EmptyInstanceSpawner instanceSpawner;
  51. }
  52. TEST_F(EmptyInstanceSpawnerTests, SpawnersAlwaysEqual)
  53. {
  54. // Two different instances of the EmptyInstanceSpawner should always be considered
  55. // data-equivalent.
  56. Vegetation::EmptyInstanceSpawner instanceSpawner1;
  57. Vegetation::EmptyInstanceSpawner instanceSpawner2;
  58. EXPECT_TRUE(instanceSpawner1 == instanceSpawner2);
  59. }
  60. TEST_F(EmptyInstanceSpawnerTests, LoadAndUnloadAssets)
  61. {
  62. // The spawner should successfully pretend to load/unload assets without errors.
  63. // ("Pretend" because an EmptyInstanceSpawner has no assets)
  64. Vegetation::EmptyInstanceSpawner instanceSpawner;
  65. Vegetation::DescriptorNotificationBus::Handler::BusConnect(&instanceSpawner);
  66. instanceSpawner.LoadAssets();
  67. // We expect this to be called immediately during LoadAssets for EmptyInstanceSpawner, so there's
  68. // no need to wait before checking it.
  69. EXPECT_TRUE(m_numOnLoadedCalls == 1);
  70. instanceSpawner.UnloadAssets();
  71. Vegetation::DescriptorNotificationBus::Handler::BusDisconnect();
  72. }
  73. TEST_F(EmptyInstanceSpawnerTests, CreateAndDestroyInstance)
  74. {
  75. // The spawner should successfully "create" and "destroy" an instance without errors.
  76. Vegetation::EmptyInstanceSpawner instanceSpawner;
  77. Vegetation::InstanceData instanceData;
  78. Vegetation::InstancePtr instance = instanceSpawner.CreateInstance(instanceData);
  79. EXPECT_TRUE(instance);
  80. instanceSpawner.DestroyInstance(0, instance);
  81. }
  82. TEST_F(EmptyInstanceSpawnerTests, SpawnerRegisteredWithDescriptor)
  83. {
  84. // Validate that the Descriptor successfully gets EmptyInstanceSpawner registered with it,
  85. // as long as InstanceSpawner and EmptyInstanceSpawner have been reflected.
  86. MockEmptyInstanceVegetationSystemComponent* component = nullptr;
  87. auto entity = CreateEntity(&component);
  88. Vegetation::Descriptor descriptor;
  89. descriptor.RefreshSpawnerTypeList();
  90. auto spawnerTypes = descriptor.GetSpawnerTypeList();
  91. EXPECT_TRUE(spawnerTypes.size() == 1);
  92. EXPECT_TRUE(spawnerTypes[0].first == Vegetation::EmptyInstanceSpawner::RTTI_Type());
  93. }
  94. TEST_F(EmptyInstanceSpawnerTests, DescriptorCreatesCorrectSpawner)
  95. {
  96. // Validate that the Descriptor successfully creates a new EmptyInstanceSpawner if we change
  97. // the spawner type on the Descriptor.
  98. MockEmptyInstanceVegetationSystemComponent* component = nullptr;
  99. auto entity = CreateEntity(&component);
  100. // We expect the Descriptor to start off with a Legacy Vegetation spawner, but then should correctly get an
  101. // EmptyInstanceSpawner after we change spawnerType.
  102. Vegetation::Descriptor descriptor;
  103. EXPECT_TRUE(azrtti_typeid(*(descriptor.GetInstanceSpawner())) != Vegetation::EmptyInstanceSpawner::RTTI_Type());
  104. descriptor.m_spawnerType = Vegetation::EmptyInstanceSpawner::RTTI_Type();
  105. descriptor.RefreshSpawnerTypeList();
  106. descriptor.SpawnerTypeChanged();
  107. EXPECT_TRUE(azrtti_typeid(*(descriptor.GetInstanceSpawner())) == Vegetation::EmptyInstanceSpawner::RTTI_Type());
  108. }
  109. }