Scene.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 <AzCore/UnitTest/TestTypes.h>
  9. AZ_PUSH_DISABLE_WARNING(, "-Wdelete-non-virtual-dtor")
  10. #include <AzCore/Component/ComponentApplication.h>
  11. #include <AzCore/Component/Entity.h>
  12. #include <AzCore/Component/Component.h>
  13. #include <AzFramework/Scene/SceneSystemComponent.h>
  14. #include <AzFramework/Scene/Scene.h>
  15. #include <AzCore/Asset/AssetManagerComponent.h>
  16. #include <AzCore/Asset/AssetManager.h>
  17. #include <AzCore/IO/Streamer/StreamerComponent.h>
  18. #include <AzCore/Jobs/JobManagerComponent.h>
  19. #include <AzCore/Memory/PoolAllocator.h>
  20. #include <AzCore/Slice/SliceAssetHandler.h>
  21. #include <AzFramework/IO/LocalFileIO.h>
  22. #include <AzCore/Slice/SliceSystemComponent.h>
  23. using namespace AzFramework;
  24. // Test component that allows code to be injected into activate / deactivate for testing.
  25. namespace SceneUnitTest
  26. {
  27. class TestComponent;
  28. class TestComponentConfig : public AZ::ComponentConfig
  29. {
  30. public:
  31. AZ_CLASS_ALLOCATOR(TestComponentConfig, AZ::SystemAllocator)
  32. AZ_RTTI(TestComponentConfig, "{DCD12D72-3BFE-43A9-9679-66B745814CAF}", ComponentConfig);
  33. typedef void(*ActivateFunction)(TestComponent* component);
  34. ActivateFunction m_activateFunction = nullptr;
  35. typedef void(*DeactivateFunction)(TestComponent* component);
  36. DeactivateFunction m_deactivateFunction = nullptr;
  37. };
  38. static constexpr AZ::TypeId TestComponentTypeId{ "{DC096267-4815-47D1-BA23-A1CDF0D72D9D}" };
  39. class TestComponent : public AZ::Component
  40. {
  41. public:
  42. AZ_COMPONENT(TestComponent, TestComponentTypeId);
  43. static void Reflect(AZ::ReflectContext*) {};
  44. void Activate() override
  45. {
  46. if (m_config.m_activateFunction)
  47. {
  48. m_config.m_activateFunction(this);
  49. }
  50. }
  51. void Deactivate() override
  52. {
  53. if (m_config.m_deactivateFunction)
  54. {
  55. m_config.m_deactivateFunction(this);
  56. }
  57. }
  58. bool ReadInConfig(const AZ::ComponentConfig* baseConfig) override
  59. {
  60. if (auto config = azrtti_cast<const TestComponentConfig*>(baseConfig))
  61. {
  62. m_config = *config;
  63. return true;
  64. }
  65. return false;
  66. }
  67. bool WriteOutConfig(AZ::ComponentConfig* outBaseConfig) const override
  68. {
  69. if (auto outConfig = azrtti_cast<TestComponentConfig*>(outBaseConfig))
  70. {
  71. *outConfig = m_config;
  72. return true;
  73. }
  74. return false;
  75. }
  76. TestComponentConfig m_config;
  77. };
  78. // Fixture that creates a bare-bones app with only the system components necesary.
  79. class SceneTest
  80. : public UnitTest::LeakDetectionFixture
  81. {
  82. public:
  83. void SetUp() override
  84. {
  85. m_prevFileIO = AZ::IO::FileIOBase::GetInstance();
  86. AZ::IO::FileIOBase::SetInstance(&m_fileIO);
  87. m_app.RegisterComponentDescriptor(SceneSystemComponent::CreateDescriptor());
  88. m_app.RegisterComponentDescriptor(AZ::SliceSystemComponent::CreateDescriptor());
  89. m_app.RegisterComponentDescriptor(AZ::AssetManagerComponent::CreateDescriptor());
  90. m_app.RegisterComponentDescriptor(AZ::JobManagerComponent::CreateDescriptor());
  91. m_app.RegisterComponentDescriptor(AZ::StreamerComponent::CreateDescriptor());
  92. AZ::ComponentApplication::Descriptor desc;
  93. AZ::ComponentApplication::StartupParameters startupParameters;
  94. startupParameters.m_loadSettingsRegistry = false;
  95. m_systemEntity = m_app.Create(desc, startupParameters);
  96. m_systemEntity->Init();
  97. m_systemEntity->CreateComponent<SceneSystemComponent>();
  98. // Asset / slice system components needed by entity contexts
  99. m_systemEntity->CreateComponent<AZ::SliceSystemComponent>();
  100. m_systemEntity->CreateComponent<AZ::AssetManagerComponent>();
  101. m_systemEntity->CreateComponent<AZ::JobManagerComponent>();
  102. m_systemEntity->CreateComponent<AZ::StreamerComponent>();
  103. m_systemEntity->Activate();
  104. m_sceneSystem = AzFramework::SceneSystemInterface::Get();
  105. }
  106. void TearDown() override
  107. {
  108. m_app.Destroy();
  109. AZ::IO::FileIOBase::SetInstance(m_prevFileIO);
  110. }
  111. AZ::IO::LocalFileIO m_fileIO;
  112. AZ::IO::FileIOBase* m_prevFileIO;
  113. AZ::ComponentApplication m_app;
  114. AZ::Entity* m_systemEntity = nullptr;
  115. AzFramework::ISceneSystem* m_sceneSystem = nullptr;
  116. };
  117. TEST_F(SceneTest, CreateScene)
  118. {
  119. // A scene should be able to be created with a given name.
  120. AZ::Outcome<AZStd::shared_ptr<Scene>, AZStd::string> createSceneOutcome = m_sceneSystem->CreateScene("TestScene");
  121. EXPECT_TRUE(createSceneOutcome.IsSuccess()) << "Unable to create a scene.";
  122. // The scene pointer returned should be valid
  123. AZStd::shared_ptr<Scene> scene = createSceneOutcome.TakeValue();
  124. EXPECT_NE(scene, nullptr) << "Scene creation reported success, but no scene actually was actually returned.";
  125. // Attempting to create another scene with the same name should fail.
  126. createSceneOutcome = m_sceneSystem->CreateScene("TestScene");
  127. EXPECT_TRUE(!createSceneOutcome.IsSuccess()) << "Should not be able to create two scenes with the same name.";
  128. }
  129. TEST_F(SceneTest, GetScene)
  130. {
  131. constexpr AZStd::string_view sceneName = "TestScene";
  132. AZ::Outcome<AZStd::shared_ptr<Scene>, AZStd::string> createSceneOutcome = m_sceneSystem->CreateScene(sceneName);
  133. AZStd::shared_ptr<Scene> createdScene = createSceneOutcome.TakeValue();
  134. // Should be able to get a scene by name, and it should match the scene that was created.
  135. AZStd::shared_ptr<Scene> retrievedScene = m_sceneSystem->GetScene(sceneName);
  136. EXPECT_NE(retrievedScene, nullptr) << "Attempting to get scene by name resulted in nullptr.";
  137. EXPECT_EQ(retrievedScene, createdScene) << "Retrieved scene does not match created scene.";
  138. // An invalid name should return a null scene.
  139. AZStd::shared_ptr<Scene> nullScene = m_sceneSystem->GetScene("non-existant scene");
  140. EXPECT_EQ(nullScene, nullptr) << "Should not be able to retrieve a scene that wasn't created.";
  141. }
  142. TEST_F(SceneTest, RemoveScene)
  143. {
  144. constexpr AZStd::string_view sceneName = "TestScene";
  145. AZ::Outcome<AZStd::shared_ptr<Scene>, AZStd::string> createSceneOutcome = m_sceneSystem->CreateScene(sceneName);
  146. bool success = m_sceneSystem->RemoveScene(sceneName);
  147. EXPECT_TRUE(success) << "Failed to remove the scene that was just created.";
  148. success = m_sceneSystem->RemoveScene("non-existant scene");
  149. EXPECT_FALSE(success) << "Remove scene returned success for a non-existant scene.";
  150. }
  151. TEST_F(SceneTest, IterateActiveScenes)
  152. {
  153. constexpr size_t NumScenes = 5;
  154. AZStd::shared_ptr<Scene> scenes[NumScenes] = {nullptr};
  155. for (size_t i = 0; i < NumScenes; ++i)
  156. {
  157. AZStd::string sceneName = AZStd::string::format("scene %zu", i);
  158. AZ::Outcome<AZStd::shared_ptr<Scene>, AZStd::string> createSceneOutcome = m_sceneSystem->CreateScene(sceneName);
  159. scenes[i] = createSceneOutcome.TakeValue();
  160. }
  161. size_t index = 0;
  162. m_sceneSystem->IterateActiveScenes([&index, &scenes](const AZStd::shared_ptr<Scene>& scene)
  163. {
  164. AZStd::shared_ptr<Scene> newscene = scenes[index++];
  165. EXPECT_EQ(newscene, scene);
  166. return true;
  167. });
  168. }
  169. TEST_F(SceneTest, IterateZombieScenes)
  170. {
  171. constexpr size_t NumScenes = 5;
  172. AZStd::shared_ptr<Scene> scenes[NumScenes] = {nullptr};
  173. // Create zombies.
  174. for (size_t i = 0; i < NumScenes; ++i)
  175. {
  176. AZStd::string sceneName = AZStd::string::format("scene %zu", i);
  177. AZ::Outcome<AZStd::shared_ptr<Scene>, AZStd::string> createSceneOutcome = m_sceneSystem->CreateScene(sceneName);
  178. scenes[i] = createSceneOutcome.TakeValue();
  179. m_sceneSystem->RemoveScene(sceneName);
  180. }
  181. // Check to make sure there are no more active scenes.
  182. size_t index = 0;
  183. m_sceneSystem->IterateActiveScenes([&index](const AZStd::shared_ptr<Scene>&)
  184. {
  185. index++;
  186. return true;
  187. });
  188. EXPECT_EQ(0, index);
  189. // Check that the scenes are still returned as zombies.
  190. index = 0;
  191. m_sceneSystem->IterateZombieScenes([&index, &scenes](Scene& scene)
  192. {
  193. AZStd::shared_ptr<Scene> zombieScene = scenes[index++];
  194. EXPECT_EQ(zombieScene.get(), &scene);
  195. return true;
  196. });
  197. // Check that all scenes are removed when there are no more handles.
  198. for (size_t i = 0; i < NumScenes; ++i)
  199. {
  200. scenes[i].reset();
  201. }
  202. index = 0;
  203. m_sceneSystem->IterateZombieScenes([&index](Scene&) {
  204. index++;
  205. return true;
  206. });
  207. EXPECT_EQ(0, index);
  208. }
  209. // Test classes for use in the SceneSystem test. These can't be defined in the test itself due to some functions created by AZ_RTTI not having a body which breaks VS2015.
  210. class Foo1
  211. {
  212. public:
  213. AZ_RTTI(Foo1, "{9A6AA770-E2EA-4C5E-952A-341802E2DE58}");
  214. };
  215. class Foo2
  216. {
  217. public:
  218. AZ_RTTI(Foo2, "{916A2DB4-9C30-4B90-837E-2BC9855B474B}");
  219. };
  220. TEST_F(SceneTest, SceneSystem)
  221. {
  222. // Create the scene
  223. AZ::Outcome<AZStd::shared_ptr<Scene>, AZStd::string> createSceneOutcome = m_sceneSystem->CreateScene("TestScene");
  224. EXPECT_TRUE(createSceneOutcome.IsSuccess());
  225. AZStd::shared_ptr<Scene> scene = createSceneOutcome.TakeValue();
  226. // Set a class on the Scene
  227. Foo1* foo1a = new Foo1();
  228. EXPECT_TRUE(scene->SetSubsystem(foo1a));
  229. // Get that class back from the Scene
  230. EXPECT_EQ(foo1a, *scene->FindSubsystem<Foo1*>());
  231. // Try to set the same class type twice, this should fail.
  232. Foo1* foo1b = new Foo1();
  233. EXPECT_FALSE(scene->SetSubsystem(foo1b));
  234. delete foo1b;
  235. // Add a child scene
  236. createSceneOutcome = m_sceneSystem->CreateSceneWithParent("ChildScene", scene);
  237. EXPECT_TRUE(createSceneOutcome.IsSuccess());
  238. AZStd::shared_ptr<Scene> childScene = createSceneOutcome.TakeValue();
  239. // Get class back from parent scene.
  240. EXPECT_EQ(foo1a, *childScene->FindSubsystem<Foo1*>());
  241. // Find overloaded version of class on child scene.
  242. Foo1* foo1c = new Foo1();
  243. EXPECT_TRUE(childScene->SetSubsystem(foo1c));
  244. EXPECT_EQ(foo1c, *childScene->FindSubsystem<Foo1*>());
  245. // Unset system on child scene, using alternative unset function.
  246. EXPECT_TRUE(childScene->UnsetSubsystem(foo1c));
  247. delete foo1c;
  248. // Try to un-set a class that was never set, this should fail.
  249. EXPECT_FALSE(scene->UnsetSubsystem<Foo2>());
  250. // Unset the class that was previously set
  251. EXPECT_TRUE(scene->UnsetSubsystem<Foo1>());
  252. delete foo1a;
  253. // Make sure that the previously set class was really removed.
  254. EXPECT_EQ(nullptr, scene->FindSubsystem<Foo1*>());
  255. }
  256. } // UnitTest
  257. AZ_POP_DISABLE_WARNING