3
0

DeleteEntity_UnderNestedInstance.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. def DeleteEntity_UnderNestedInstance():
  7. """
  8. Test description:
  9. - Focuses on the "First_Car" instance.
  10. - Deletes the "Tire_Entity" of the "Front_Wheel" of "First_Car".
  11. - Checks that there are 2 "Tire_Entity" and the deleted
  12. entities are only under the "Front_Wheel" instances in both cars.
  13. - Checks undo/redo correctness.
  14. - Checks that the deleted "Tire_Entity" should re-appear after focusing on
  15. its owning instance (i.e. "Front_Wheel") in "First_Car".
  16. Hierarchy:
  17. - Level
  18. - First_Car <-- focus on this prefab
  19. - Front_Wheel
  20. - Tire_Entity <-- delete this entity as override
  21. - Back_Wheel
  22. - Tire_Entity
  23. - Second_Car
  24. - Front_Wheel
  25. - Tire_Entity <-- this should be deleted as well after propagation
  26. - Back_Wheel
  27. - Tire_Entity
  28. """
  29. from editor_python_test_tools.editor_entity_utils import EditorEntity
  30. from editor_python_test_tools.prefab_utils import Prefab
  31. from editor_python_test_tools.wait_utils import PrefabWaiter
  32. import azlmbr.legacy.general as general
  33. import Prefab.tests.PrefabTestUtils as prefab_test_utils
  34. CAR_PREFAB_FILE_NAME = Path(__file__).stem + "_" + "car_prefab"
  35. WHEEL_PREFAB_FILE_NAME = Path(__file__).stem + "_" + "wheel_prefab"
  36. FIRST_CAR_NAME = "First_Car"
  37. SECOND_CAR_NAME = "Second_Car"
  38. FRONT_WHEEL_NAME = "Front_Wheel"
  39. BACK_WHEEL_NAME = "Back_Wheel"
  40. TIRE_ENTITY_NAME = "Tire_Entity"
  41. prefab_test_utils.open_base_tests_level()
  42. # Create a new entity at the root level.
  43. tire_entity = EditorEntity.create_editor_entity_at((100.0, 100.0, 100.0), TIRE_ENTITY_NAME)
  44. assert tire_entity.id.IsValid(), f"Failed to create new entity: {TIRE_ENTITY_NAME}."
  45. # Create a wheel prefab and front wheel instance from the tire entity.
  46. wheel_prefab_entities = [tire_entity]
  47. wheel_prefab, front_wheel_instance = Prefab.create_prefab(wheel_prefab_entities, WHEEL_PREFAB_FILE_NAME, \
  48. FRONT_WHEEL_NAME)
  49. assert front_wheel_instance.is_valid(), f"Failed to instantiate instance: {FRONT_WHEEL_NAME}."
  50. # Create a back wheel instance.
  51. back_wheel_instance = wheel_prefab.instantiate(name=BACK_WHEEL_NAME)
  52. assert back_wheel_instance.is_valid(), f"Failed to instantiate instance: {BACK_WHEEL_NAME}."
  53. # Create a car prefab and the first car instance from two wheels.
  54. car_prefab_entities = [front_wheel_instance.container_entity, back_wheel_instance.container_entity]
  55. car_prefab, car_instance_1 = Prefab.create_prefab(car_prefab_entities, CAR_PREFAB_FILE_NAME, \
  56. FIRST_CAR_NAME)
  57. assert car_instance_1.is_valid(), f"Failed to instantiate instance: {FIRST_CAR_NAME}."
  58. # Create a second car instance.
  59. car_instance_2 = car_prefab.instantiate(name=SECOND_CAR_NAME)
  60. assert car_instance_2.is_valid(), f"Failed to instantiate instance: {SECOND_CAR_NAME}."
  61. # Focus on the first car instance.
  62. car_instance_1.container_entity.focus_on_owning_prefab()
  63. # Delete the tire entity in the front wheel instance in the first car instance.
  64. wheel_container_entities_in_car_instance_1 = car_instance_1.get_direct_child_entities()
  65. filtered_wheel_list = list(filter(lambda wheel_container_entity: \
  66. wheel_container_entity.get_name() == FRONT_WHEEL_NAME, \
  67. wheel_container_entities_in_car_instance_1))
  68. assert len(filtered_wheel_list) == 1, f"Found {len(filtered_wheel_list)} {FRONT_WHEEL_NAME}. " \
  69. f"Expected 1 {FRONT_WHEEL_NAME} in {FIRST_CAR_NAME} to delete."
  70. tire_entity_in_front_wheel_in_car_instance_1 = filtered_wheel_list[0].get_children()[0]
  71. tire_entity_in_front_wheel_in_car_instance_1.delete()
  72. # Wait till prefab propagation finishes before validating deletion.
  73. PrefabWaiter.wait_for_propagation()
  74. # Validate after entity deletion.
  75. prefab_test_utils.validate_count_for_named_editor_entity(TIRE_ENTITY_NAME, 2)
  76. prefab_test_utils.validate_child_count_for_named_editor_entity(FRONT_WHEEL_NAME, 0)
  77. prefab_test_utils.validate_child_count_for_named_editor_entity(BACK_WHEEL_NAME, 1)
  78. # Validate undo on entity deletion.
  79. general.undo()
  80. PrefabWaiter.wait_for_propagation()
  81. prefab_test_utils.validate_count_for_named_editor_entity(TIRE_ENTITY_NAME, 4)
  82. prefab_test_utils.validate_child_count_for_named_editor_entity(FRONT_WHEEL_NAME, 1)
  83. prefab_test_utils.validate_child_count_for_named_editor_entity(BACK_WHEEL_NAME, 1)
  84. # Validate redo on entity deletion.
  85. general.redo()
  86. PrefabWaiter.wait_for_propagation()
  87. prefab_test_utils.validate_count_for_named_editor_entity(TIRE_ENTITY_NAME, 2)
  88. prefab_test_utils.validate_child_count_for_named_editor_entity(FRONT_WHEEL_NAME, 0)
  89. prefab_test_utils.validate_child_count_for_named_editor_entity(BACK_WHEEL_NAME, 1)
  90. # Focus on first wheel instance in first car. Deleted tire entity should appear in Prefab Edit Mode.
  91. front_wheel_container_entities = EditorEntity.find_editor_entities([FRONT_WHEEL_NAME])
  92. filtered_front_wheel_list = list(filter(lambda front_wheel: \
  93. front_wheel.get_parent_id() == car_instance_1.container_entity.id, \
  94. front_wheel_container_entities))
  95. assert len(filtered_front_wheel_list) == 1, f"Found {len(filtered_front_wheel_list)} {FRONT_WHEEL_NAME}. " \
  96. f"Expected 1 {FRONT_WHEEL_NAME} in {FIRST_CAR_NAME} to focus on."
  97. front_wheel_in_car_instance_1 = filtered_front_wheel_list[0]
  98. front_wheel_in_car_instance_1.focus_on_owning_prefab()
  99. PrefabWaiter.wait_for_propagation() # propagate source template changes after focusing on
  100. prefab_test_utils.check_entity_children_count(front_wheel_in_car_instance_1.id, 1)
  101. # Note: This should be 3 because the tire entity inside the front wheel under the second car is still deleted.
  102. prefab_test_utils.validate_count_for_named_editor_entity(TIRE_ENTITY_NAME, 3)
  103. if __name__ == "__main__":
  104. from editor_python_test_tools.utils import Report
  105. Report.start_test(DeleteEntity_UnderNestedInstance)