DeletePrefab_UnderImmediateInstance.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 DeletePrefab_UnderImmediateInstance():
  7. """
  8. Test description:
  9. - Deletes the "Tire_Prefab" instance of the "First_Car".
  10. - Checks that the "Tire_Prefab" is deleted only in "First_Car".
  11. - Checks undo/redo correctness.
  12. - Checks that the deleted "Tire_Prefab" instance (and "Tire_Entity") should re-appear
  13. after focusing on its owning instance (i.e. "First_Car").
  14. Hierarchy:
  15. - Level <-- focus on this prefab
  16. - First_Car
  17. - Tire_Prefab <-- delete this instance as override
  18. - Tire_Entity
  19. - Second_Car
  20. - Tire_Prefab <-- this instance is unchanged
  21. - Tire_Entity
  22. """
  23. from editor_python_test_tools.editor_entity_utils import EditorEntity
  24. from editor_python_test_tools.prefab_utils import Prefab
  25. from editor_python_test_tools.wait_utils import PrefabWaiter
  26. import azlmbr.legacy.general as general
  27. import Prefab.tests.PrefabTestUtils as prefab_test_utils
  28. CAR_PREFAB_FILE_NAME = Path(__file__).stem + "_" + "car_prefab"
  29. WHEEL_PREFAB_FILE_NAME = Path(__file__).stem + "_" + "wheel_prefab"
  30. TIRE_PREFAB_FILE_NAME = Path(__file__).stem + "_" + "tire_prefab"
  31. FIRST_CAR_NAME = "First_Car"
  32. SECOND_CAR_NAME = "Second_Car"
  33. TIRE_PREFAB_NAME = "Tire_Prefab"
  34. TIRE_ENTITY_NAME = "Tire_Entity"
  35. prefab_test_utils.open_base_tests_level()
  36. # Create a new entity at the root level.
  37. tire_entity = EditorEntity.create_editor_entity_at((100.0, 100.0, 100.0), TIRE_ENTITY_NAME)
  38. assert tire_entity.id.IsValid(), f"Failed to create new entity: {TIRE_ENTITY_NAME}."
  39. # Create a tire prefab from the tire entity.
  40. tire_prefab_entities = [tire_entity]
  41. tire_prefab, tire_instance = Prefab.create_prefab(tire_prefab_entities, TIRE_PREFAB_FILE_NAME, TIRE_PREFAB_NAME)
  42. assert tire_instance.is_valid(), f"Failed to instantiate instance: {TIRE_PREFAB_NAME}."
  43. # Create a car prefab and the first car instance from the tire entity.
  44. car_prefab_entities = [tire_instance.container_entity]
  45. car_prefab, car_instance_1 = Prefab.create_prefab(car_prefab_entities, CAR_PREFAB_FILE_NAME, FIRST_CAR_NAME)
  46. assert car_instance_1.is_valid(), f"Failed to instantiate instance: {FIRST_CAR_NAME}."
  47. # Create the second car instance.
  48. car_instance_2 = car_prefab.instantiate(name=SECOND_CAR_NAME)
  49. assert car_instance_2.is_valid(), f"Failed to instantiate instance: {SECOND_CAR_NAME}."
  50. # Delete the tire instance the first car. Note: Level prefab is currently being focused by default during deletion.
  51. tire_container_entities_in_car_instance_1 = car_instance_1.get_direct_child_entities()
  52. tire_container_entity_in_car_instance_1 = tire_container_entities_in_car_instance_1[0]
  53. tire_container_entity_in_car_instance_1.delete()
  54. # Wait till prefab propagation finishes before validating deletion.
  55. PrefabWaiter.wait_for_propagation()
  56. # Validate after instance deletion.
  57. prefab_test_utils.validate_count_for_named_editor_entity(TIRE_PREFAB_NAME, 1)
  58. prefab_test_utils.validate_count_for_named_editor_entity(TIRE_ENTITY_NAME, 1)
  59. prefab_test_utils.check_entity_children_count(car_instance_1.container_entity.id, 0)
  60. prefab_test_utils.check_entity_children_count(car_instance_2.container_entity.id, 1)
  61. # Validate undo on instance deletion.
  62. general.undo()
  63. PrefabWaiter.wait_for_propagation()
  64. prefab_test_utils.validate_count_for_named_editor_entity(TIRE_PREFAB_NAME, 2)
  65. prefab_test_utils.validate_count_for_named_editor_entity(TIRE_ENTITY_NAME, 2)
  66. prefab_test_utils.check_entity_children_count(car_instance_1.container_entity.id, 1)
  67. prefab_test_utils.check_entity_children_count(car_instance_2.container_entity.id, 1)
  68. # Validate redo on instance deletion.
  69. general.redo()
  70. PrefabWaiter.wait_for_propagation()
  71. prefab_test_utils.validate_count_for_named_editor_entity(TIRE_PREFAB_NAME, 1)
  72. prefab_test_utils.validate_count_for_named_editor_entity(TIRE_ENTITY_NAME, 1)
  73. prefab_test_utils.check_entity_children_count(car_instance_1.container_entity.id, 0)
  74. prefab_test_utils.check_entity_children_count(car_instance_2.container_entity.id, 1)
  75. # Focus on the first car instance. Deleted tire instance should appear in Prefab Edit Mode.
  76. car_instance_1.container_entity.focus_on_owning_prefab()
  77. PrefabWaiter.wait_for_propagation() # propagate source template changes after focusing on
  78. prefab_test_utils.check_entity_children_count(car_instance_1.container_entity.id, 1)
  79. # Note: This should be 2 because the tire entity (and instance) inside the second car is not deleted.
  80. prefab_test_utils.validate_count_for_named_editor_entity(TIRE_PREFAB_NAME, 2)
  81. prefab_test_utils.validate_count_for_named_editor_entity(TIRE_ENTITY_NAME, 2)
  82. if __name__ == "__main__":
  83. from editor_python_test_tools.utils import Report
  84. Report.start_test(DeletePrefab_UnderImmediateInstance)