hydra_AtomEditorComponents_EntityReferenceAdded.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. SPDX-License-Identifier: Apache-2.0 OR MIT
  5. """
  6. class Tests:
  7. creation_undo = (
  8. "UNDO Entity creation success",
  9. "P0: UNDO Entity creation failed")
  10. creation_redo = (
  11. "REDO Entity creation success",
  12. "P0: REDO Entity creation failed")
  13. entity_reference_creation = (
  14. "Entity Reference Entity successfully created",
  15. "P0: Entity Reference Entity failed to be created")
  16. entity_reference_component = (
  17. "Entity has an Entity Reference component",
  18. "P0: Entity failed to find Entity Reference component")
  19. enter_game_mode = (
  20. "Entered game mode",
  21. "P0: Failed to enter game mode")
  22. exit_game_mode = (
  23. "Exited game mode",
  24. "P0: Couldn't exit game mode")
  25. is_visible = (
  26. "Entity is visible",
  27. "P0: Entity was not visible")
  28. is_hidden = (
  29. "Entity is hidden",
  30. "P0: Entity was not hidden")
  31. entity_deleted = (
  32. "Entity deleted",
  33. "P0: Entity was not deleted")
  34. deletion_undo = (
  35. "UNDO deletion success",
  36. "P0: UNDO deletion failed")
  37. deletion_redo = (
  38. "REDO deletion success",
  39. "P0: REDO deletion failed")
  40. entity_id_references_is_container = (
  41. "EntityIdReferences is a container property",
  42. "P1: EntityIdReferences is NOT a container property")
  43. container_append = (
  44. "EntityIdReferences append succeeded",
  45. "P1: EntityIdReferences append did not succeed")
  46. container_add = (
  47. "EntityIdReferences add succeeded",
  48. "P1: EntityIdReferences add did not succeed")
  49. container_update = (
  50. "EntityIdReferences update succeeded",
  51. "P1: EntityIdReferences update did not succeed")
  52. container_remove = (
  53. "EntityIdReferences remove succeeded",
  54. "P1: EntityIdReferences remove did not succeed")
  55. container_reset = (
  56. "EntityIdReferences reset succeeded",
  57. "P1: EntityIdReferences reset did not succeed")
  58. entity_reference_component_removed = (
  59. "Entity Reference component removed from entity",
  60. "P1: Entity Reference component NOT removed from entity")
  61. def AtomEditorComponents_EntityReference_AddedToEntity():
  62. """
  63. Summary:
  64. Tests the Entity Reference component can be added to an entity and has the expected functionality.
  65. Test setup:
  66. - Wait for Editor idle loop.
  67. - Open the "Base" level.
  68. Expected Behavior:
  69. The component can be added, used in game mode, hidden/shown, deleted, and has accurate required components.
  70. Creation and deletion undo/redo should also work.
  71. Test Steps:
  72. 1) Create an Entity Reference entity with no components.
  73. 2) Add Entity Reference component to Entity Reference entity.
  74. 3) UNDO the entity creation and component addition.
  75. 4) REDO the entity creation and component addition.
  76. 5) 'EntityIdReferences' is a container property
  77. 6) Append item to 'EntityIdReferences'
  78. 7) Add item to 'EntityIdReferences'
  79. 8) Update item in 'EntityIdReferences'
  80. 9) Remove item from 'EntityIdReferences'
  81. 10) Reset the container property then put one entity reference back for further tests
  82. 11) Remove component
  83. 12) UNDO component remove
  84. 13) Enter/Exit game mode.
  85. 14) Test IsHidden.
  86. 15) Test IsVisible.
  87. 16) Delete Entity Reference entity.
  88. 17) UNDO deletion.
  89. 18) REDO deletion.
  90. 19) Look for errors.
  91. :return: None
  92. """
  93. import azlmbr.legacy.general as general
  94. from editor_python_test_tools.editor_entity_utils import EditorEntity
  95. from editor_python_test_tools.utils import Report, Tracer, TestHelper
  96. from Atom.atom_utils.atom_constants import AtomComponentProperties
  97. with Tracer() as error_tracer:
  98. # Test setup begins.
  99. # Setup: Wait for Editor idle loop before executing Python hydra scripts then open "Base" level.
  100. TestHelper.init_idle()
  101. TestHelper.open_level("Graphics", "base_empty")
  102. # Test steps begin.
  103. # 1. Create an Entity Reference entity with no components.
  104. entity_reference_entity = EditorEntity.create_editor_entity(AtomComponentProperties.entity_reference())
  105. Report.critical_result(Tests.entity_reference_creation, entity_reference_entity.exists())
  106. # 2. Add Entity Reference component to Entity Reference entity.
  107. entity_reference_component = entity_reference_entity.add_component(
  108. AtomComponentProperties.entity_reference())
  109. Report.critical_result(
  110. Tests.entity_reference_component,
  111. entity_reference_entity.has_component(AtomComponentProperties.entity_reference()))
  112. # 3. UNDO the entity creation and component addition.
  113. # -> UNDO component addition.
  114. general.undo()
  115. # -> UNDO naming entity.
  116. general.undo()
  117. # -> UNDO selecting entity.
  118. general.undo()
  119. # -> UNDO entity creation.
  120. general.undo()
  121. general.idle_wait_frames(1)
  122. Report.result(Tests.creation_undo, not entity_reference_entity.exists())
  123. # 4. REDO the entity creation and component addition.
  124. # -> REDO entity creation.
  125. general.redo()
  126. # -> REDO selecting entity.
  127. general.redo()
  128. # -> REDO naming entity.
  129. general.redo()
  130. # -> REDO component addition.
  131. general.redo()
  132. general.idle_wait_frames(1)
  133. Report.result(Tests.creation_redo, entity_reference_entity.exists())
  134. # Entities for EntityIdReferences tests
  135. test_1 = EditorEntity.create_editor_entity('test_1')
  136. test_2 = EditorEntity.create_editor_entity('test_2')
  137. test_3 = EditorEntity.create_editor_entity('test_3')
  138. # 5. 'EntityIdReferences' is a container property
  139. Report.result(
  140. Tests.entity_id_references_is_container,
  141. entity_reference_component.is_property_container(
  142. AtomComponentProperties.entity_reference('EntityIdReferences')))
  143. # 6. Append item to 'EntityIdReferences'
  144. entity_reference_component.append_container_item(
  145. AtomComponentProperties.entity_reference('EntityIdReferences'), test_1.id)
  146. Report.result(
  147. Tests.container_append,
  148. entity_reference_component.get_container_item(
  149. AtomComponentProperties.entity_reference('EntityIdReferences'), 0) == test_1.id)
  150. # 7. Add item to 'EntityIdReferences'
  151. entity_reference_component.add_container_item(
  152. AtomComponentProperties.entity_reference('EntityIdReferences'), 1, test_1.id)
  153. Report.result(
  154. Tests.container_add,
  155. entity_reference_component.get_container_count(
  156. AtomComponentProperties.entity_reference('EntityIdReferences')) == 2)
  157. # 8. Update item in 'EntityIdReferences'
  158. entity_reference_component.update_container_item(
  159. AtomComponentProperties.entity_reference('EntityIdReferences'), 1, test_2.id)
  160. Report.result(
  161. Tests.container_update,
  162. entity_reference_component.get_container_item(
  163. AtomComponentProperties.entity_reference('EntityIdReferences'), 1) == test_2.id)
  164. # 9. Remove item from 'EntityIdReferences'
  165. entity_reference_component.append_container_item(
  166. AtomComponentProperties.entity_reference('EntityIdReferences'), test_3.id)
  167. count_before = entity_reference_component.get_container_count(
  168. AtomComponentProperties.entity_reference('EntityIdReferences'))
  169. entity_reference_component.remove_container_item(
  170. AtomComponentProperties.entity_reference('EntityIdReferences'), 1)
  171. count_after = entity_reference_component.get_container_count(
  172. AtomComponentProperties.entity_reference('EntityIdReferences'))
  173. Report.result(
  174. Tests.container_remove,
  175. ((count_before == 3) and (count_after == 2) and
  176. (entity_reference_component.get_container_item(
  177. AtomComponentProperties.entity_reference('EntityIdReferences'), 1) == test_3.id)))
  178. # 10. Reset the container property then put one entity reference back for further tests
  179. entity_reference_component.reset_container(AtomComponentProperties.entity_reference('EntityIdReferences'))
  180. general.idle_wait_frames(1)
  181. Report.result(
  182. Tests.container_reset,
  183. entity_reference_component.get_container_count(
  184. AtomComponentProperties.entity_reference('EntityIdReferences')) == 0)
  185. entity_reference_component.append_container_item(
  186. AtomComponentProperties.entity_reference('EntityIdReferences'), test_1.id)
  187. # 11. Remove component
  188. entity_reference_entity.remove_component(AtomComponentProperties.entity_reference())
  189. general.idle_wait_frames(1)
  190. Report.result(Tests.entity_reference_component_removed, not entity_reference_entity.has_component(
  191. AtomComponentProperties.entity_reference()))
  192. # 12. UNDO component remove
  193. general.undo()
  194. general.idle_wait_frames(1)
  195. Report.result(Tests.entity_reference_component, entity_reference_entity.has_component(
  196. AtomComponentProperties.entity_reference()))
  197. # 13. Enter/Exit game mode.
  198. TestHelper.enter_game_mode(Tests.enter_game_mode)
  199. general.idle_wait_frames(1)
  200. TestHelper.exit_game_mode(Tests.exit_game_mode)
  201. # 14. Test IsHidden.
  202. entity_reference_entity.set_visibility_state(False)
  203. Report.result(Tests.is_hidden, entity_reference_entity.is_hidden() is True)
  204. # 15. Test IsVisible.
  205. entity_reference_entity.set_visibility_state(True)
  206. general.idle_wait_frames(1)
  207. Report.result(Tests.is_visible, entity_reference_entity.is_visible() is True)
  208. # 16. Delete Entity Reference entity.
  209. entity_reference_entity.delete()
  210. Report.result(Tests.entity_deleted, not entity_reference_entity.exists())
  211. # 17. UNDO deletion.
  212. general.undo()
  213. general.idle_wait_frames(1)
  214. Report.result(Tests.deletion_undo, entity_reference_entity.exists())
  215. # 18. REDO deletion.
  216. general.redo()
  217. general.idle_wait_frames(1)
  218. Report.result(Tests.deletion_redo, not entity_reference_entity.exists())
  219. # 19. Look for errors and asserts.
  220. TestHelper.wait_for_condition(lambda: error_tracer.has_errors or error_tracer.has_asserts, 1.0)
  221. for error_info in error_tracer.errors:
  222. Report.info(f"Error: {error_info.filename} {error_info.function} | {error_info.message}")
  223. for assert_info in error_tracer.asserts:
  224. Report.info(f"Assert: {assert_info.filename} {assert_info.function} | {assert_info.message}")
  225. if __name__ == "__main__":
  226. from editor_python_test_tools.utils import Report
  227. Report.start_test(AtomEditorComponents_EntityReference_AddedToEntity)