DetachPrefab_WithSingleEntity.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 DetachPrefab_WithSingleEntity():
  7. from pathlib import Path
  8. import pyside_utils
  9. @pyside_utils.wrap_async
  10. async def run_test():
  11. import azlmbr.bus as bus
  12. import azlmbr.editor as editor
  13. import azlmbr.legacy.general as general
  14. from editor_python_test_tools.editor_entity_utils import EditorEntity
  15. from editor_python_test_tools.prefab_utils import Prefab
  16. from editor_python_test_tools.wait_utils import PrefabWaiter
  17. import Prefab.tests.PrefabTestUtils as prefab_test_utils
  18. TEST_PREFAB_FILE_NAME = Path(__file__).stem + '_prefab'
  19. prefab_test_utils.open_base_tests_level()
  20. # Creates an entity at the root level
  21. test_entity = EditorEntity.create_editor_entity("Test")
  22. test_prefab_entities = [test_entity]
  23. # Creates a prefab from the test entity
  24. _, test_instance = Prefab.create_prefab(test_prefab_entities, TEST_PREFAB_FILE_NAME)
  25. # Get parent info for the test prefab to validate hierarchy remains unchanged after Undo/Redo
  26. test_instance_parent = test_instance.container_entity.get_parent_id()
  27. # Detaches the test prefab instance
  28. Prefab.detach_prefab(test_instance)
  29. # Test undo/redo on prefab detach
  30. general.undo()
  31. PrefabWaiter.wait_for_propagation()
  32. is_prefab = editor.EditorComponentAPIBus(bus.Broadcast, "HasComponentOfType", test_instance.container_entity.id,
  33. azlmbr.globals.property.EditorPrefabComponentTypeId)
  34. test_instance_parent_undo = test_instance.container_entity.get_parent_id()
  35. assert test_instance_parent == test_instance_parent_undo, "Undo operation unexpectedly changed entity hierarchy"
  36. assert is_prefab, "Undo operation failed. Entity is not recognized as a prefab."
  37. general.redo()
  38. PrefabWaiter.wait_for_propagation()
  39. former_container_entity = EditorEntity.find_editor_entity(TEST_PREFAB_FILE_NAME)
  40. test_entity = EditorEntity.find_editor_entity("Test")
  41. is_prefab = editor.EditorComponentAPIBus(bus.Broadcast, "HasComponentOfType", former_container_entity.id,
  42. azlmbr.globals.property.EditorPrefabComponentTypeId)
  43. assert test_entity.get_parent_id() == former_container_entity.id, \
  44. "Redo operation unexpectedly changed entity hierarchy"
  45. assert not is_prefab, "Redo operation failed. Entity is still recognized as a prefab."
  46. run_test()
  47. if __name__ == "__main__":
  48. from editor_python_test_tools.utils import Report
  49. Report.start_test(DetachPrefab_WithSingleEntity)