ScriptCanvasComponent_OnEntityActivatedDeactivated_PrintMessage.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. # fmt: off
  7. class Tests():
  8. level_created = ("Successfully created temp level", "Failed to create temp level")
  9. controller_exists = ("Successfully found controller entity", "Failed to find controller entity")
  10. activated_exists = ("Successfully found activated entity", "Failed to find activated entity")
  11. deactivated_exists = ("Successfully found deactivated entity", "Failed to find deactivated entity")
  12. start_states_correct = ("Start states set up successfully", "Start states set up incorrectly")
  13. game_mode_entered = ("Successfully entered game mode" "Failed to enter game mode")
  14. lines_found = ("Successfully found expected prints", "Failed to find expected prints")
  15. game_mode_exited = ("Successfully exited game mode" "Failed to exit game mode")
  16. # fmt: on
  17. def ScriptCanvasComponent_OnEntityActivatedDeactivated_PrintMessage():
  18. """
  19. Summary:
  20. Verify that the On Entity Activated/On Entity Deactivated nodes are working as expected
  21. Expected Behavior:
  22. Upon entering game mode, the Controller entity will wait 1 second and then activate the ActivationTest
  23. entity. The script attached to ActivationTest will print out a message on activation. The Controller
  24. will also deactivate the DeactivationTest entity, which should print a message.
  25. Test Steps:
  26. 1) Create temp level
  27. 2) Setup the level
  28. 3) Validate the entities
  29. 4) Start the Tracer
  30. 5) Enter Game Mode
  31. 6) Validate Print message
  32. 7) Exit game mode
  33. Note:
  34. - This test file must be called from the Open 3D Engine Editor command terminal
  35. - Any passed and failed tests are written to the Editor.log file.
  36. Parsing the file or running a log_monitor are required to observe the test results.
  37. :return: None
  38. """
  39. import os
  40. from editor_entity_utils import EditorEntity as Entity
  41. from utils import Report
  42. from utils import TestHelper as helper
  43. from utils import Tracer
  44. import azlmbr.legacy.general as general
  45. EditorEntity = str
  46. LEVEL_NAME = "tmp_level"
  47. WAIT_TIME = 3.0 # SECONDS
  48. EXPECTED_LINES = ["Activator Script: Activated", "Deactivator Script: Deactivated"]
  49. controller_dict = {
  50. "name": "Controller",
  51. "status": "active",
  52. "path": os.path.join("ScriptCanvas", "OnEntityActivatedScripts", "controller.scriptcanvas"),
  53. }
  54. activated_dict = {
  55. "name": "ActivationTest",
  56. "status": "inactive",
  57. "path": os.path.join("ScriptCanvas", "OnEntityActivatedScripts", "activator.scriptcanvas"),
  58. }
  59. deactivated_dict = {
  60. "name": "DeactivationTest",
  61. "status": "active",
  62. "path": os.path.join("ScriptCanvas", "OnEntityActivatedScripts", "deactivator.scriptcanvas"),
  63. }
  64. def get_asset(asset_path):
  65. return azlmbr.asset.AssetCatalogRequestBus(
  66. azlmbr.bus.Broadcast, "GetAssetIdByPath", asset_path, azlmbr.math.Uuid(), False
  67. )
  68. def setup_level():
  69. def create_editor_entity(
  70. entity_dict: dict, entity_to_activate: EditorEntity = None, entity_to_deactivate: EditorEntity = None
  71. ) -> EditorEntity:
  72. entity = Entity.create_editor_entity(entity_dict["name"])
  73. entity.set_start_status(entity_dict["status"])
  74. sc_component = entity.add_component("Script Canvas")
  75. sc_component.set_component_property_value(
  76. "Script Canvas Asset|Script Canvas Asset", get_asset(entity_dict["path"])
  77. )
  78. if entity_dict["name"] == "Controller":
  79. sc_component.get_property_tree()
  80. sc_component.set_component_property_value(
  81. "Properties|Variables|EntityToActivate|Datum|Datum|value|EntityToActivate",
  82. entity_to_activate.id,
  83. )
  84. sc_component.set_component_property_value(
  85. "Properties|Variables|EntityToDeactivate|Datum|Datum|value|EntityToDeactivate",
  86. entity_to_deactivate.id,
  87. )
  88. return entity
  89. activated = create_editor_entity(activated_dict)
  90. deactivated = create_editor_entity(deactivated_dict)
  91. create_editor_entity(controller_dict, activated, deactivated)
  92. def validate_entity_exist(entity_name: str, test_tuple: tuple):
  93. """
  94. Validate the entity with the given name exists in the level
  95. :return: entity: editor entity object
  96. """
  97. entity = Entity.find_editor_entity(entity_name)
  98. Report.critical_result(test_tuple, entity.id.IsValid())
  99. return entity
  100. def validate_start_state(entity: EditorEntity, expected_state: str):
  101. """
  102. Validate that the starting state of the entity is correct, if it isn't then attempt to rectify and recheck.
  103. :return: bool: Whether state is set as expected
  104. """
  105. state_options = {
  106. "active": azlmbr.globals.property.EditorEntityStartStatus_StartActive,
  107. "inactive": azlmbr.globals.property.EditorEntityStartStatus_StartInactive,
  108. "editor": azlmbr.globals.property.EditorEntityStartStatus_EditorOnly,
  109. }
  110. if expected_state.lower() not in state_options.keys():
  111. raise ValueError(f"{expected_state} is an invalid option; valid options: active, inactive, or editor.")
  112. state = entity.get_start_status()
  113. if state != state_options[expected_state]:
  114. # If state fails to set, set_start_status will assert
  115. entity.set_start_status(expected_state)
  116. return True
  117. def validate_entities_in_level():
  118. controller = validate_entity_exist(controller_dict["name"], Tests.controller_exists)
  119. state1_correct = validate_start_state(controller, controller_dict["status"])
  120. act_tester = validate_entity_exist(activated_dict["name"], Tests.activated_exists)
  121. state2_correct = validate_start_state(act_tester, activated_dict["status"])
  122. deac_tester = validate_entity_exist(deactivated_dict["name"], Tests.deactivated_exists)
  123. state3_correct = validate_start_state(deac_tester, deactivated_dict["status"])
  124. all_states_correct = state1_correct and state2_correct and state3_correct
  125. Report.critical_result(Tests.start_states_correct, all_states_correct)
  126. def locate_expected_lines(line_list: list):
  127. found_lines = [printInfo.message.strip() for printInfo in section_tracer.prints]
  128. return all(line in found_lines for line in line_list)
  129. # 1) Create temp level
  130. general.idle_enable(True)
  131. result = general.create_level_no_prompt(LEVEL_NAME, 128, 1, 512, True)
  132. Report.critical_result(Tests.level_created, result == 0)
  133. helper.wait_for_condition(lambda: general.get_current_level_name() == LEVEL_NAME, WAIT_TIME)
  134. general.close_pane("Error Report")
  135. # 2) Setup the level
  136. setup_level()
  137. # 3) Validate the entities
  138. validate_entities_in_level()
  139. # 4) Start the Tracer
  140. with Tracer() as section_tracer:
  141. # 5) Enter Game Mode
  142. helper.enter_game_mode(Tests.game_mode_entered)
  143. # 6) Validate Print message
  144. helper.wait_for_condition(lambda: locate_expected_lines(EXPECTED_LINES), WAIT_TIME)
  145. Report.result(Tests.lines_found, locate_expected_lines(EXPECTED_LINES))
  146. # 7) Exit game mode
  147. helper.exit_game_mode(Tests.game_mode_exited)
  148. if __name__ == "__main__":
  149. import ImportPathHelper as imports
  150. imports.init()
  151. from utils import Report
  152. Report.start_test(ScriptCanvasComponent_OnEntityActivatedDeactivated_PrintMessage)