ScriptCanvasComponent_OnEntityActivatedDeactivated_PrintMessage.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. game_mode_entered = ("Successfully entered game mode", "Failed to enter game mode")
  9. lines_found = ("Successfully found expected prints", "Failed to find expected prints")
  10. game_mode_exited = ("Successfully exited game mode", "Failed to exit game mode")
  11. # fmt: on
  12. class testEntity:
  13. def __init__(self, name: str, status: str, file: str):
  14. import os
  15. import azlmbr.paths as paths
  16. self.name = name
  17. self.entity_state = status
  18. self.file_path = os.path.join(paths.projectroot, "ScriptCanvas", "OnEntityActivatedScripts", file)
  19. def setup_level_entities(entities: [testEntity]) -> None:
  20. """
  21. Helper function for setting up the 3 entities and their script canvas components needed for this test. Entity name,
  22. activation status and path to sc file are stored in testEntity objects defined above.
  23. returns None
  24. """
  25. import azlmbr.math as math
  26. import scripting_utils.scripting_tools as scripting_tools
  27. from editor_python_test_tools.editor_entity_utils import EditorEntity
  28. from editor_python_test_tools.editor_component.editor_script_canvas import ScriptCanvasComponent
  29. from editor_python_test_tools.editor_component.editor_script_canvas import VariableState
  30. from editor_python_test_tools.utils import TestHelper as TestHelper
  31. from scripting_utils.scripting_constants import (WAIT_TIME_3)
  32. position = math.Vector3(512.0, 512.0, 32.0)
  33. for entity in entities:
  34. #create entity, give it a script canvas component and set its active status
  35. editor_entity = EditorEntity.create_editor_entity_at(position, entity.name)
  36. scriptcanvas_component = ScriptCanvasComponent(editor_entity)
  37. scriptcanvas_component.set_component_graph_file_from_path(entity.file_path)
  38. scripting_tools.change_entity_start_status(entity.name, entity.entity_state)
  39. TestHelper.wait_for_condition(lambda: editor_entity is not None, WAIT_TIME_3)
  40. # the controller entity needs extra steps to be set up properly
  41. if entity.name == "Controller":
  42. # get the ids of the two other entities we created
  43. activated_entity_id = EditorEntity.find_editor_entity("ActivationTest").id
  44. deactivated_entity_id = EditorEntity.find_editor_entity("DeactivationTest").id
  45. # set the two variables on the sc component to point to the activated and deactivated entities
  46. activated_entity_var = "EntityToActivate"
  47. deactivated_entity_var = "EntityToDeactivate"
  48. scriptcanvas_component.set_variable_value(activated_entity_var, VariableState.VARIABLE, activated_entity_id)
  49. scriptcanvas_component.set_variable_value(deactivated_entity_var, VariableState.VARIABLE,
  50. deactivated_entity_id)
  51. def ScriptCanvasComponent_OnEntityActivatedDeactivated_PrintMessage():
  52. """
  53. Summary:
  54. Verify that the On Entity Activated/On Entity Deactivated nodes are working as expected
  55. Expected Behavior:
  56. Upon entering game mode, the Controller entity will wait 1 second and then activate the ActivationTest
  57. entity. The script attached to ActivationTest will print out a message on activation. The Controller
  58. will also deactivate the DeactivationTest entity, which should print a message.
  59. Test Steps:
  60. 1) Create temp level
  61. 2) Create all the entities we need for the test
  62. 3) Start the Tracer
  63. 4) Enter Game Mode
  64. 5) Wait one second for graph timers then exit game mode
  65. 6) Validate Print message
  66. Note:
  67. - Any passed and failed tests are written to the Editor.log file.
  68. :return: None
  69. """
  70. import scripting_utils.scripting_tools as scripting_tools
  71. from editor_python_test_tools.utils import TestHelper
  72. from editor_python_test_tools.utils import Report, Tracer
  73. import azlmbr.legacy.general as general
  74. from scripting_utils.scripting_constants import (WAIT_TIME_3, WAIT_TIME_1)
  75. test_entities = [
  76. testEntity("ActivationTest", "inactive", "activator.scriptcanvas"),
  77. testEntity("DeactivationTest", "active", "deactivator.scriptcanvas"),
  78. testEntity("Controller", "active", "controller.scriptcanvas"),
  79. ]
  80. EXPECTED_LINES = ["Activator Script: Activated", "Deactivator Script: Deactivated"]
  81. # Preconditions
  82. general.idle_enable(True)
  83. # 1) Create temp level
  84. TestHelper.open_level("", "Base")
  85. # 2) create all the entities we need for the test
  86. setup_level_entities(test_entities)
  87. # 3) Start the Tracer
  88. with Tracer() as section_tracer:
  89. # 4) Enter Game Mode
  90. TestHelper.enter_game_mode(Tests.game_mode_entered)
  91. # 5) Wait one second for graph timers then exit game mode
  92. general.idle_wait(WAIT_TIME_1)
  93. TestHelper.exit_game_mode(Tests.game_mode_exited)
  94. # 6) Validate Print message
  95. found_expected_lines = scripting_tools.located_expected_tracer_lines(section_tracer, EXPECTED_LINES)
  96. TestHelper.wait_for_condition(lambda: found_expected_lines is not None, WAIT_TIME_3)
  97. Report.result(Tests.lines_found, found_expected_lines)
  98. if __name__ == "__main__":
  99. import ImportPathHelper as imports
  100. imports.init()
  101. from utils import Report
  102. Report.start_test(ScriptCanvasComponent_OnEntityActivatedDeactivated_PrintMessage)