ScriptCanvasComponent_OnEntityActivatedDeactivated_PrintMessage.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. import os
  7. import editor_python_test_tools.pyside_utils as pyside_utils
  8. import editor_python_test_tools.hydra_editor_utils as hydra
  9. from editor_python_test_tools.utils import Report, Tracer
  10. from editor_python_test_tools.utils import TestHelper as helper
  11. import scripting_utils.scripting_tools as scripting_tools
  12. import azlmbr.legacy.general as general
  13. import azlmbr.paths as paths
  14. from scripting_utils.scripting_constants import (WAIT_TIME_3, BASE_LEVEL_NAME)
  15. EXPECTED_LINES = ["Activator Script: Activated", "Deactivator Script: Deactivated"]
  16. controller_dict = {
  17. "name": "Controller",
  18. "status": "active",
  19. "path": os.path.join(paths.projectroot, "ScriptCanvas", "OnEntityActivatedScripts", "controller.scriptcanvas"),
  20. }
  21. activated_dict = {
  22. "name": "ActivationTest",
  23. "status": "inactive",
  24. "path": os.path.join(paths.projectroot, "ScriptCanvas", "OnEntityActivatedScripts", "activator.scriptcanvas"),
  25. }
  26. deactivated_dict = {
  27. "name": "DeactivationTest",
  28. "status": "active",
  29. "path": os.path.join(paths.projectroot, "ScriptCanvas", "OnEntityActivatedScripts", "deactivator.scriptcanvas"),
  30. }
  31. ENTITY_TO_ACTIVATE_PATH = "Configuration|Properties|Variables|EntityToActivate|Datum|Datum|value|EntityToActivate"
  32. ENTITY_TO_DEACTIVATE_PATH = "Configuration|Properties|Variables|EntityToDeactivate|Datum|Datum|value|EntityToDeactivate"
  33. WAIT_ONE_SECOND = 1.0
  34. # fmt: off
  35. class Tests():
  36. controller_exists = ("Successfully found controller entity", "Failed to find controller entity")
  37. activated_exists = ("Successfully found activated entity", "Failed to find activated entity")
  38. deactivated_exists = ("Successfully found deactivated entity", "Failed to find deactivated entity")
  39. start_states_correct = ("Start states set up successfully", "Start states set up incorrectly")
  40. game_mode_entered = ("Successfully entered game mode", "Failed to enter game mode")
  41. lines_found = ("Successfully found expected prints", "Failed to find expected prints")
  42. game_mode_exited = ("Successfully exited game mode", "Failed to exit game mode")
  43. # fmt: on
  44. class ScriptCanvasComponent_OnEntityActivatedDeactivated_PrintMessage():
  45. """
  46. Summary:
  47. Verify that the On Entity Activated/On Entity Deactivated nodes are working as expected
  48. Expected Behavior:
  49. Upon entering game mode, the Controller entity will wait 1 second and then activate the ActivationTest
  50. entity. The script attached to ActivationTest will print out a message on activation. The Controller
  51. will also deactivate the DeactivationTest entity, which should print a message.
  52. Test Steps:
  53. 1) Create temp level
  54. 2) Create all the entities we need for the test
  55. 3) Validate the entities were created and configured
  56. 4) Start the Tracer
  57. 5) Enter Game Mode
  58. 6) Wait one second for graph timers then exit game mode
  59. 7) Validate Print message
  60. Note:
  61. - This test file must be called from the Open 3D Engine Editor command terminal
  62. - Any passed and failed tests are written to the Editor.log file.
  63. Parsing the file or running a log_monitor are required to observe the test results.
  64. :return: None
  65. """
  66. def __init__(self):
  67. self.editor_main_window = None
  68. def setup_level_entities(self):
  69. activated_entity = scripting_tools.create_entity_with_sc_component_asset(activated_dict["name"], activated_dict["path"])
  70. scripting_tools.change_entity_start_status(activated_dict["name"], activated_dict["status"])
  71. helper.wait_for_condition(lambda: activated_entity is not None, WAIT_TIME_3)
  72. deactivated_entity = scripting_tools.create_entity_with_sc_component_asset(deactivated_dict["name"], deactivated_dict["path"])
  73. scripting_tools.change_entity_start_status(deactivated_dict["name"], deactivated_dict["status"])
  74. helper.wait_for_condition(lambda: deactivated_entity is not None, WAIT_TIME_3)
  75. self.setup_controller_entity(controller_dict)
  76. def setup_controller_entity(self, entity_dict: dict):
  77. """
  78. create entity using hydra and editor entity library but also drill into its exposed variables and set values.
  79. """
  80. entity = scripting_tools.create_entity_with_sc_component_asset(entity_dict["name"], entity_dict["path"])
  81. scripting_tools.change_entity_start_status(entity_dict["name"], entity_dict["status"])
  82. scripting_tools.change_entity_sc_variable_entity(entity_dict["name"], activated_dict["name"],
  83. ENTITY_TO_ACTIVATE_PATH)
  84. scripting_tools.change_entity_sc_variable_entity(entity_dict["name"], deactivated_dict["name"],
  85. ENTITY_TO_DEACTIVATE_PATH)
  86. helper.wait_for_condition(lambda: entity is not None, WAIT_TIME_3)
  87. def validate_entity_state(self, entity_tuple, test):
  88. """
  89. Function to make sure the entities created for this test were properly added to the level
  90. """
  91. entity = scripting_tools.validate_entity_exists_by_name(entity_tuple["name"], test)
  92. state_correct = scripting_tools.validate_entity_start_state_by_name(entity_tuple["name"], entity_tuple["status"])
  93. return state_correct
  94. def validate_test_entities(self):
  95. entities_valid = (self.validate_entity_state(activated_dict, Tests.activated_exists) and
  96. self.validate_entity_state(deactivated_dict, Tests.deactivated_exists) and
  97. self.validate_entity_state(controller_dict, Tests.controller_exists))
  98. Report.critical_result(Tests.start_states_correct, entities_valid)
  99. @pyside_utils.wrap_async
  100. async def run_test(self):
  101. # 1) Create temp level
  102. general.idle_enable(True)
  103. hydra.open_base_level()
  104. helper.wait_for_condition(lambda: general.get_current_level_name() == BASE_LEVEL_NAME, WAIT_TIME_3)
  105. general.close_pane("Error Report")
  106. # 2) create all the entities we need for the test
  107. self.setup_level_entities()
  108. # 3) Validate the entities were created and configured
  109. self.validate_test_entities()
  110. # 4) Start the Tracer
  111. with Tracer() as section_tracer:
  112. # 5) Enter Game Mode
  113. helper.enter_game_mode(Tests.game_mode_entered)
  114. # 6 Wait one second for graph timers then exit game mode
  115. general.idle_wait(WAIT_ONE_SECOND)
  116. helper.exit_game_mode(Tests.game_mode_exited)
  117. # 7) Validate Print message
  118. found_expected_lines = scripting_tools.located_expected_tracer_lines(self, section_tracer, EXPECTED_LINES)
  119. helper.wait_for_condition(lambda: found_expected_lines is not None, WAIT_TIME_3)
  120. Report.result(Tests.lines_found, found_expected_lines)
  121. test = ScriptCanvasComponent_OnEntityActivatedDeactivated_PrintMessage()
  122. test.run_test()