editor_test_helper.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. #
  2. # All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
  3. # its licensors.
  4. #
  5. # For complete copyright and license terms please see the LICENSE at the root of this
  6. # distribution (the "License"). All use of this software is governed by the License,
  7. # or, if provided, by the license below or the license accompanying this file. Do not
  8. # remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
  9. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. #
  11. # NOTE: This code is used for tests in several feature areas. If changes are made to this file, please verify all
  12. # dependent tests continue to run without issue.
  13. #
  14. import sys
  15. import time
  16. from typing import Sequence
  17. from .report import Report
  18. # Lumberyard specific imports
  19. import azlmbr.legacy.general as general
  20. import azlmbr.legacy.settings as settings
  21. class EditorTestHelper:
  22. def __init__(self, log_prefix: str, args: Sequence[str] = None) -> None:
  23. self.log_prefix = log_prefix + ": "
  24. self.test_success = True
  25. # If the idle loop has already been enabled at test init time, the Editor is already running.
  26. # If that's the case, we'll skip the "exit_no_prompt" at the end.
  27. self.editor_already_running = general.is_idle_enabled()
  28. self.args = {}
  29. if args:
  30. # Get the level name and heightmap name from command-line args
  31. if len(sys.argv) == (len(args) + 1):
  32. for arg_index in range(len(args)):
  33. self.args[args[arg_index]] = sys.argv[arg_index + 1]
  34. else:
  35. self.test_success = False
  36. self.log(f"Expected command-line args: {args}")
  37. self.log(f"Check that cfg_args were passed into the test class")
  38. # Test Setup
  39. # Set helpers
  40. # Set viewport size
  41. # Turn off display mode, antialiasing
  42. # set log prefix, log test started
  43. def setup(self) -> None:
  44. self.log("test started")
  45. def after_level_load(self, bypass_viewport_resize: bool = False) -> bool:
  46. success = True
  47. # Enable the Editor to start running its idle loop.
  48. # This is needed for Python scripts passed into the Editor startup. Since they're executed
  49. # during the startup flow, they run before idle processing starts. Without this, the engine loop
  50. # won't run during idle_wait, which will prevent our test level from working.
  51. general.idle_enable(True)
  52. # Give everything a second to initialize
  53. general.idle_wait(1.0)
  54. general.update_viewport()
  55. general.idle_wait(0.5) # half a second is more than enough for updating the viewport.
  56. self.original_settings = settings.get_misc_editor_settings()
  57. self.helpers_visible = general.is_helpers_shown()
  58. self.viewport_size = general.get_viewport_size()
  59. self.viewport_layout = general.get_view_pane_layout()
  60. # Turn off the helper gizmos if visible
  61. if self.helpers_visible:
  62. general.toggle_helpers()
  63. general.idle_wait(1.0)
  64. # Close the Error Report window so it doesn't interfere with testing hierarchies and focus
  65. if general.is_pane_visible("Error Report"):
  66. general.close_pane("Error Report")
  67. if general.is_pane_visible("Error Log"):
  68. general.close_pane("Error Log")
  69. general.idle_wait(1.0)
  70. if not bypass_viewport_resize:
  71. # Set Editor viewport to a well-defined size
  72. screen_width = 1600
  73. screen_height = 900
  74. general.set_viewport_expansion_policy("FixedSize")
  75. general.set_viewport_size(screen_width, screen_height)
  76. general.update_viewport()
  77. general.idle_wait(1.0)
  78. new_viewport_size = general.get_viewport_size()
  79. new_viewport_width = int(new_viewport_size.x)
  80. new_viewport_height = int(new_viewport_size.y)
  81. if (new_viewport_width != screen_width) or (new_viewport_height != screen_height):
  82. self.log(
  83. f"set_viewport_size failed - expected ({screen_width},{screen_height}), got ({new_viewport_width},{new_viewport_height})"
  84. )
  85. self.test_success = False
  86. success = False
  87. # Turn off any display info like FPS, as that will mess up our image comparisons
  88. # Turn off antialiasing as well
  89. general.run_console("r_displayInfo=0")
  90. general.run_console("r_antialiasingmode=0")
  91. general.idle_wait(1.0)
  92. return success
  93. # Test Teardown
  94. # Restore everything from above
  95. # log test results, exit editor
  96. def teardown(self) -> None:
  97. # Restore the original Editor settings
  98. settings.set_misc_editor_settings(self.original_settings)
  99. # If the helper gizmos were on at the start, restore them
  100. if self.helpers_visible:
  101. general.toggle_helpers()
  102. # Set the viewport back to whatever size it was at the start and restore the pane layout
  103. general.set_viewport_size(int(self.viewport_size.x), int(self.viewport_size.y))
  104. general.set_viewport_expansion_policy("AutoExpand")
  105. general.set_view_pane_layout(self.viewport_layout)
  106. general.update_viewport()
  107. self.log("test finished")
  108. if self.test_success:
  109. self.log("result=SUCCESS")
  110. general.set_result_to_success()
  111. else:
  112. self.log("result=FAILURE")
  113. general.set_result_to_failure()
  114. if not self.editor_already_running:
  115. general.exit_no_prompt()
  116. def run_test(self) -> None:
  117. self.log("run")
  118. def run(self) -> None:
  119. self.setup()
  120. # Only run the actual test if we didn't have setup issues
  121. if self.test_success:
  122. self.run_test()
  123. self.teardown()
  124. def get_arg(self, arg_name: str) -> str:
  125. if arg_name in self.args:
  126. return self.args[arg_name]
  127. return ""
  128. # general logger that adds prefix?
  129. def log(self, log_line: str) -> None:
  130. Report.info(self.log_prefix + log_line)
  131. # isclose: Compares two floating-point values for "nearly-equal"
  132. def isclose(self, a: float, b: float, rel_tol: float = 1e-9, abs_tol: float = 0.0) -> bool:
  133. return abs(a - b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)
  134. # Create a new empty level
  135. def create_level(
  136. self,
  137. level_name: str,
  138. heightmap_resolution: int = 1024,
  139. heightmap_meters_per_pixel: int = 1,
  140. terrain_texture_resolution: int = 4096,
  141. use_terrain: bool = False,
  142. bypass_viewport_resize: bool = False,
  143. ) -> bool:
  144. self.log(f"Creating level {level_name}")
  145. result = general.create_level_no_prompt(
  146. level_name, heightmap_resolution, heightmap_meters_per_pixel, terrain_texture_resolution, use_terrain
  147. )
  148. # Result codes are ECreateLevelResult defined in CryEdit.h
  149. if result == 1:
  150. self.log(f"{level_name} level already exists")
  151. elif result == 2:
  152. self.log("Failed to create directory")
  153. elif result == 3:
  154. self.log("Directory length is too long")
  155. elif result != 0:
  156. self.log("Unknown error, failed to create level")
  157. else:
  158. self.log(f"{level_name} level created successfully")
  159. # If the editor is already running, allow "level already exists" to count as success
  160. if (result == 0) or (self.editor_already_running and (result == 1)):
  161. # For successful level creation, call the post-load step.
  162. if self.after_level_load(bypass_viewport_resize):
  163. result = 0
  164. else:
  165. result = -1
  166. return result == 0
  167. def open_level(self, level_name: str, bypass_viewport_resize: bool = False) -> bool:
  168. # Open the level non-interactively
  169. if self.editor_already_running and (general.get_current_level_name() == level_name):
  170. self.log(f"Level {level_name} already open")
  171. result = True
  172. else:
  173. self.log(f"Opening level {level_name}")
  174. result = general.open_level_no_prompt(level_name)
  175. result = result and self.after_level_load(bypass_viewport_resize)
  176. if result:
  177. self.log(f"Successfully opened {level_name}")
  178. else:
  179. self.log(f"Unknown error, {level_name} level failed to open")
  180. return result
  181. # Take Screenshot
  182. def take_viewport_screenshot(
  183. self, posX: float, posY: float, posZ: float, rotX: float, rotY: float, rotZ: float
  184. ) -> None:
  185. # Set our camera position / rotation and wait for the Editor to acknowledge it
  186. general.set_current_view_position(posX, posY, posZ)
  187. general.set_current_view_rotation(rotX, rotY, rotZ)
  188. general.idle_wait(1.0)
  189. # Request a screenshot and wait for the Editor to process it
  190. general.run_console("r_GetScreenShot=2")
  191. general.idle_wait(1.0)
  192. def enter_game_mode(self, success_message: str) -> None:
  193. """
  194. :param success_message: The str with the expected message for entering game mode.
  195. :return: None
  196. """
  197. Report.info("Entering game mode")
  198. general.enter_game_mode()
  199. general.idle_wait_frames(1)
  200. self.critical_result(success_message, general.is_in_game_mode())
  201. def exit_game_mode(self, success_message: str) -> None:
  202. """
  203. :param success_message: The str with the expected message for exiting game mode.
  204. :return: None
  205. """
  206. Report.info("Exiting game mode")
  207. general.exit_game_mode()
  208. general.idle_wait_frames(1)
  209. self.critical_result(success_message, not general.is_in_game_mode())
  210. def critical_result(self, success_message: str, condition: bool, fast_fail_message: str = None) -> None:
  211. """
  212. if condition is False we will fail fast
  213. :param success_message: messages to print based on the condition
  214. :param condition: success (True) or failure (False)
  215. :param fast_fail_message: [optional] message to include on fast fail
  216. """
  217. if not isinstance(condition, bool):
  218. raise TypeError("condition argument must be a bool")
  219. if not Report.result(success_message, condition):
  220. self.test_success = False
  221. self.fail_fast(fast_fail_message)
  222. def fail_fast(self, message: str = None) -> None:
  223. """
  224. A state has been reached where progressing in the test is not viable.
  225. raises FailFast
  226. :return: None
  227. """
  228. Report.info("Failing fast. Raising an exception and shutting down the editor.")
  229. if message:
  230. Report.info(f"Fail fast message: {message}")
  231. self.teardown()
  232. raise RuntimeError
  233. def wait_for_condition(self, function, timeout_in_seconds=1.0):
  234. # type: (function, float) -> bool
  235. """
  236. **** Will be replaced by a function of the same name exposed in the Engine*****
  237. a function to run until it returns True or timeout is reached
  238. the function can have no parameters and
  239. waiting idle__wait_* is handled here not in the function
  240. :param function: a function that returns a boolean indicating a desired condition is achieved
  241. :param timeout_in_seconds: when reached, function execution is abandoned and False is returned
  242. """
  243. with Timeout(timeout_in_seconds) as t:
  244. while True:
  245. try:
  246. general.idle_wait_frames(1)
  247. except Exception:
  248. print("WARNING: Couldn't wait for frame")
  249. if t.timed_out:
  250. return False
  251. ret = function()
  252. if not isinstance(ret, bool):
  253. raise TypeError("return value for wait_for_condition function must be a bool")
  254. if ret:
  255. return True
  256. class Timeout:
  257. # type: (float) -> None
  258. """
  259. contextual timeout
  260. :param seconds: float seconds to allow before timed_out is True
  261. """
  262. def __init__(self, seconds):
  263. self.seconds = seconds
  264. def __enter__(self):
  265. self.die_after = time.time() + self.seconds
  266. return self
  267. def __exit__(self, type, value, traceback):
  268. pass
  269. @property
  270. def timed_out(self):
  271. return time.time() > self.die_after