SpawnableRemoveEditorInfoTests.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 <Prefab/SpawnableRemoveEditorInfoTestFixture.h>
  9. namespace UnitTest
  10. {
  11. using SpawnableRemoveEditorInfoTests = SpawnableRemoveEditorInfoTestFixture;
  12. TEST_F(SpawnableRemoveEditorInfoTests, SpawnableRemoveEditorInfo_OnlyRuntimeEntityExported)
  13. {
  14. // Create one entity that's flagged as Editor-Only, and one that's enabled for runtime.
  15. CreateSourceEntity("EditorOnly", true);
  16. CreateSourceEntity("EditorAndRuntime", false);
  17. ConvertRuntimePrefab();
  18. // Only the runtime entity exists in the converted Prefab DOM.
  19. EXPECT_FALSE(GetRuntimeEntity("EditorOnly"));
  20. EXPECT_TRUE(GetRuntimeEntity("EditorAndRuntime"));
  21. }
  22. TEST_F(SpawnableRemoveEditorInfoTests, SpawnableRemoveEditorInfo_RuntimeComponentExportedSuccessfully)
  23. {
  24. // Create a component with RuntimeExportCallback and successfully exports itself.
  25. CreateSourceTestExportRuntimeEntity("EntityWithRuntimeComponent", true, true);
  26. ConvertRuntimePrefab();
  27. // Expected result: processed entity contains the component.
  28. AZ::Entity* entity = GetRuntimeEntity("EntityWithRuntimeComponent");
  29. ASSERT_TRUE(entity);
  30. EXPECT_TRUE(entity->FindComponent<TestExportRuntimeComponentWithCallback>());
  31. }
  32. TEST_F(SpawnableRemoveEditorInfoTests, RuntimeExportCallback_RuntimeComponentExportRemoved)
  33. {
  34. // Create a component with RuntimeExportCallback and successfully removes itself from exporting.
  35. CreateSourceTestExportRuntimeEntity("EntityWithRuntimeComponent", false, true);
  36. ConvertRuntimePrefab();
  37. // Expected result: processed entity does NOT contain the component.
  38. AZ::Entity* entity = GetRuntimeEntity("EntityWithRuntimeComponent");
  39. ASSERT_TRUE(entity);
  40. EXPECT_FALSE(entity->FindComponent<TestExportRuntimeComponentWithCallback>());
  41. }
  42. TEST_F(SpawnableRemoveEditorInfoTests, RuntimeExportCallback_RuntimeComponentExportUnhandled)
  43. {
  44. // Create a component with RuntimeExportCallback, returns a pointer to itself, but says it wasn't handled.
  45. CreateSourceTestExportRuntimeEntity("EntityWithRuntimeComponent", true, false);
  46. ConvertRuntimePrefab();
  47. // Expected result: processed entity contains the component, because the default behavior is "clone/add" for
  48. // runtime components.
  49. AZ::Entity* entity = GetRuntimeEntity("EntityWithRuntimeComponent");
  50. ASSERT_TRUE(entity);
  51. EXPECT_TRUE(entity->FindComponent<TestExportRuntimeComponentWithCallback>());
  52. }
  53. TEST_F(SpawnableRemoveEditorInfoTests, RuntimeExportCallback_RuntimeComponentExportRemovedAndUnhandled)
  54. {
  55. // Create a component with RuntimeExportCallback and removes itself from exporting, but says it wasn't handled.
  56. CreateSourceTestExportRuntimeEntity("EntityWithRuntimeComponent", false, false);
  57. ConvertRuntimePrefab();
  58. // Expected result: processed entity contains the component, because by saying it wasn't handled, it
  59. // should fall back on the default behavior of "clone/add" for runtime components.
  60. AZ::Entity* entity = GetRuntimeEntity("EntityWithRuntimeComponent");
  61. ASSERT_TRUE(entity);
  62. EXPECT_TRUE(entity->FindComponent<TestExportRuntimeComponentWithCallback>());
  63. }
  64. TEST_F(SpawnableRemoveEditorInfoTests, RuntimeExportCallback_EditorComponentExportedSuccessfully)
  65. {
  66. // Create an editor component that has a RuntimeExportCallback and successfully exports itself.
  67. CreateSourceTestExportEditorEntity(
  68. "EntityWithEditorComponent",
  69. TestExportEditorComponent::ExportComponentType::ExportRuntimeComponentWithoutCallBack,
  70. true);
  71. ConvertRuntimePrefab();
  72. // Expected result: processed entity contains the runtime component, exported from RuntimeExportCallback.
  73. AZ::Entity* entity = GetRuntimeEntity("EntityWithEditorComponent");
  74. ASSERT_TRUE(entity);
  75. EXPECT_FALSE(entity->FindComponent<TestExportEditorComponent>());
  76. EXPECT_FALSE(entity->FindComponent<TestExportRuntimeComponentWithCallback>());
  77. EXPECT_TRUE(entity->FindComponent<TestExportRuntimeComponentWithoutCallback>());
  78. }
  79. TEST_F(SpawnableRemoveEditorInfoTests, RuntimeExportCallback_EditorComponentExportRemoved)
  80. {
  81. // Create an editor component that has a RuntimeExportCallback and successfully removes itself from exporting.
  82. CreateSourceTestExportEditorEntity(
  83. "EntityWithEditorComponent",
  84. TestExportEditorComponent::ExportComponentType::ExportNullComponent,
  85. true);
  86. ConvertRuntimePrefab();
  87. // Expected result: processed entity does NOT contain either component.
  88. AZ::Entity* entity = GetRuntimeEntity("EntityWithEditorComponent");
  89. ASSERT_TRUE(entity);
  90. EXPECT_FALSE(entity->FindComponent<TestExportEditorComponent>());
  91. EXPECT_FALSE(entity->FindComponent<TestExportRuntimeComponentWithCallback>());
  92. EXPECT_FALSE(entity->FindComponent<TestExportRuntimeComponentWithoutCallback>());
  93. }
  94. TEST_F(SpawnableRemoveEditorInfoTests, RuntimeExportCallback_EditorComponentExportUnhandledFallBackToBuildGameEntity)
  95. {
  96. // Create an editor component that has a RuntimeExportCallback, returns a pointer to itself, but says it wasn't handled.
  97. CreateSourceTestExportEditorEntity(
  98. "EntityWithEditorComponent",
  99. TestExportEditorComponent::ExportComponentType::ExportEditorComponent,
  100. false);
  101. ConvertRuntimePrefab();
  102. // Expected result: processed entity contains the runtime component, because the fallback to BuildGameEntity()
  103. // produced a runtime component.
  104. AZ::Entity* entity = GetRuntimeEntity("EntityWithEditorComponent");
  105. ASSERT_TRUE(entity);
  106. EXPECT_FALSE(entity->FindComponent<TestExportEditorComponent>());
  107. EXPECT_TRUE(entity->FindComponent<TestExportRuntimeComponentWithCallback>());
  108. EXPECT_FALSE(entity->FindComponent<TestExportRuntimeComponentWithoutCallback>());
  109. }
  110. TEST_F(SpawnableRemoveEditorInfoTests, RuntimeExportCallback_EditorComponentExportRemovedAndUnhandledFallBackToBuildGameEntity)
  111. {
  112. // Create an editor component that has a RuntimeExportCallback and removes itself from exporting, but says it wasn't handled.
  113. CreateSourceTestExportEditorEntity(
  114. "EntityWithEditorComponent",
  115. TestExportEditorComponent::ExportComponentType::ExportNullComponent,
  116. false);
  117. ConvertRuntimePrefab();
  118. // Expected result: processed entity contains the runtime component, because the fallback to BuildGameEntity()
  119. // produced a runtime component.
  120. AZ::Entity* entity = GetRuntimeEntity("EntityWithEditorComponent");
  121. ASSERT_TRUE(entity);
  122. EXPECT_FALSE(entity->FindComponent<TestExportEditorComponent>());
  123. EXPECT_TRUE(entity->FindComponent<TestExportRuntimeComponentWithCallback>());
  124. EXPECT_FALSE(entity->FindComponent<TestExportRuntimeComponentWithoutCallback>());
  125. }
  126. TEST_F(SpawnableRemoveEditorInfoTests, RuntimeExportCallback_EditorComponentFailsToExportItself)
  127. {
  128. // Create an editor component that has a RuntimeExportCallback and removes itself from exporting, but says it wasn't handled.
  129. CreateSourceTestExportEditorEntity(
  130. "EntityWithEditorComponent",
  131. TestExportEditorComponent::ExportComponentType::ExportEditorComponent,
  132. true);
  133. // We expect the exporting to fail, since an editor component is being exported as a game component.
  134. ConvertRuntimePrefab(false);
  135. }
  136. }