test_desktop_recovery.py 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. from types import SimpleNamespace
  2. from unittest.mock import Mock
  3. import pytest
  4. import script as script_module
  5. from module.device.method import desktop_impl as desktop
  6. from module.device.desktop_device import DesktopDevice
  7. from module.exception import GameNotRunningError, RequestHumanTakeover, TaskEnd
  8. from tasks.Restart.script_task import ScriptTask
  9. @pytest.mark.parametrize('just_launched,popup,expected', [
  10. (True, 0, False), (False, 0, True), (False, 99, False),
  11. ])
  12. def test_binding_distinguishes_new_client_from_existing_login(monkeypatch, just_launched, popup, expected):
  13. window = desktop.DesktopWindow()
  14. window.desktop_profile_dir = lambda: None
  15. window.find_desktop_login_popup = Mock(return_value=popup)
  16. monkeypatch.setattr(desktop, 'desktop_keep_screen_on', Mock())
  17. monkeypatch.setattr(desktop, 'desktop_write_instance_state', Mock())
  18. window.desktop_bind_pid(123, just_launched=just_launched)
  19. assert window._desktop_login_done is expected
  20. assert window._desktop_just_launched is just_launched
  21. assert window.find_desktop_login_popup.call_count == (0 if just_launched else 1)
  22. @pytest.mark.parametrize('logged_in', [False, True])
  23. def test_force_restart_reuses_owned_window_only_until_first_login(logged_in):
  24. class Client(desktop.DesktopWindow, DesktopDevice):
  25. pass
  26. client = Client()
  27. client.root_handle = '123'
  28. client._desktop_spawned_pids = {123}
  29. client._desktop_just_launched = True
  30. client._desktop_login_done = False
  31. client.desktop_window_exists = lambda: True
  32. client.desktop_stop_client = Mock(return_value=True)
  33. if logged_in:
  34. client.desktop_mark_logged_in()
  35. # Later loss of login must not restore the fresh-launch exemption.
  36. client.desktop_mark_logged_out()
  37. task = object.__new__(ScriptTask)
  38. task.device = client
  39. task._desktop_start_and_login = Mock()
  40. task._desktop_restart_and_login()
  41. assert client.desktop_stop_client.call_count == int(logged_in)
  42. task._desktop_start_and_login.assert_called_once_with()
  43. @pytest.mark.parametrize('exists,stop_ok', [(False, True), (True, True), (True, False)])
  44. def test_force_restart_existing_or_absent_window(exists, stop_ok):
  45. task = object.__new__(ScriptTask)
  46. task.device = SimpleNamespace(
  47. desktop_window_exists=lambda: exists, desktop_window_is_own_launch=lambda: False,
  48. desktop_stop_client=Mock(return_value=stop_ok),
  49. )
  50. task._desktop_start_and_login = Mock()
  51. if not stop_ok:
  52. with pytest.raises(RequestHumanTakeover):
  53. task._desktop_restart_and_login()
  54. task._desktop_start_and_login.assert_not_called()
  55. else:
  56. task._desktop_restart_and_login()
  57. task._desktop_start_and_login.assert_called_once_with()
  58. assert task.device.desktop_stop_client.call_count == int(exists)
  59. @pytest.mark.parametrize('command', ['Restart', 'RealmRaid'])
  60. def test_missing_prescreenshot_allows_only_restart_to_execute(monkeypatch, command):
  61. script = object.__new__(script_module.Script)
  62. script.instance_guard = None
  63. script._try_acquire_queue_token = Mock(return_value=True)
  64. script.device = SimpleNamespace(screenshot=Mock(side_effect=GameNotRunningError('closed')))
  65. script.config = SimpleNamespace(task_call=Mock())
  66. script.exception_handler = Mock()
  67. run = Mock(side_effect=TaskEnd)
  68. loader = Mock(return_value=SimpleNamespace(ScriptTask=lambda **kw: SimpleNamespace(run=run)))
  69. monkeypatch.setattr(script_module, 'load_module', loader)
  70. assert script.run(command) is True
  71. assert run.call_count == int(command == 'Restart')
  72. assert script.config.task_call.call_count == int(command != 'Restart')
  73. def test_enabled_restart_option_uses_window_recovery():
  74. task = object.__new__(ScriptTask)
  75. task.device = SimpleNamespace(is_desktop=True)
  76. task.config = SimpleNamespace(
  77. script=SimpleNamespace(desktop=SimpleNamespace(restart_when_window_exists=True)),
  78. restart=SimpleNamespace(harvest_config=SimpleNamespace(enable_ap=False)),
  79. )
  80. task._desktop_restart_and_login = Mock()
  81. task.set_next_run = Mock()
  82. task.app_restart()
  83. task._desktop_restart_and_login.assert_called_once_with()
  84. task.set_next_run.assert_called_once_with(task='Restart', success=True, finish=True, server=True)