Lua_Spawnables_DespawnOnEntityDeactivate.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 Lua_Spawnables_DespawnOnEntityDeactivate():
  7. """
  8. This test validates an edge case that could result in an Editor crash: https://github.com/o3de/o3de/pull/9535
  9. """
  10. import azlmbr.entity as entity
  11. import azlmbr.bus as bus
  12. import azlmbr.legacy.general as general
  13. from editor_python_test_tools.utils import TestHelper as helper
  14. from editor_python_test_tools.editor_entity_utils import EditorEntity
  15. def validate_entities_in_edit_mode(condition):
  16. search_filter = entity.SearchFilter()
  17. search_filter.names = ["Lua_Spawner"]
  18. spawner_entity = entity.SearchBus(bus.Broadcast, 'SearchEntities', search_filter)[0]
  19. search_filter.names = ["PinkFlower"]
  20. flower_entity = entity.SearchBus(bus.Broadcast, 'SearchEntities', search_filter)
  21. assert spawner_entity is not None, f"Failed to find Spawner entity {condition}"
  22. assert len(flower_entity) == 0, f"Unexpectedly found PinkFlower entity {condition}"
  23. helper.init_idle()
  24. helper.open_level("Prefab", "Lua_Spawnables_DespawnOnEntityDeactivate")
  25. # Search for expected entities in edit mode
  26. validate_entities_in_edit_mode("before entering Game Mode")
  27. # Enter Game Mode and search for entities at runtime
  28. general.enter_game_mode()
  29. helper.wait_for_condition(lambda: EditorEntity(general.find_game_entity("Lua_Spawner")) is not None, 3.0)
  30. spawner_entity = EditorEntity(general.find_game_entity("Lua_Spawner"))
  31. helper.wait_for_condition(lambda: EditorEntity(general.find_game_entity("PinkFlower")) is not None, 3.0)
  32. flower_entity = EditorEntity(general.find_game_entity("PinkFlower"))
  33. assert spawner_entity, "Failed to find Spawner entity at runtime"
  34. assert flower_entity, "Failed to find PinkFlower entity at runtime"
  35. # Exit Game Mode and verify despawn of all SpawnablesTestEntity instance
  36. general.exit_game_mode()
  37. game_mode_exited = helper.wait_for_condition(lambda: not general.is_in_game_mode(), 5.0)
  38. assert game_mode_exited, "Lua script failed to exit Game Mode"
  39. validate_entities_in_edit_mode("after exiting Game Mode")
  40. if __name__ == "__main__":
  41. from editor_python_test_tools.utils import Report
  42. Report.start_test(Lua_Spawnables_DespawnOnEntityDeactivate)