Scene.cpp 12 KB

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