test_area_navigation.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. """Replay battle return transitions without a running game or screenshot fixtures."""
  2. from copy import deepcopy
  3. from types import SimpleNamespace
  4. import pytest
  5. from module.base import timer
  6. from module.exception import GameStuckError
  7. from tasks.AreaBoss.config_boss import AreaBossFloor
  8. from tasks.AreaBoss.script_task import ScriptTask
  9. class AreaNavigationReplay(ScriptTask):
  10. def __init__(self, monkeypatch, *, screen='map', rebound=False,
  11. ignored_closes=0, background_visible=False, win=True,
  12. transitions=None, close_delay=0):
  13. # Keep the real navigation, recognition dispatch and click cooldowns.
  14. self.screen = screen
  15. self.rebound = rebound
  16. self.ignored_closes = ignored_closes
  17. self.background_visible = background_visible
  18. self.transitions = dict(transitions or {})
  19. self.close_delay = close_delay
  20. self.frames = 0
  21. self.now = 100.0
  22. self.interval_timer = {}
  23. self.clicks = []
  24. self.config = SimpleNamespace(area_boss=SimpleNamespace(
  25. boss=SimpleNamespace(reward_floor=AreaBossFloor.DEFAULT)))
  26. self.device = SimpleNamespace(image=frozenset(), click=self.record_click)
  27. self.open_boss_detail = lambda *args: True
  28. self.start_fight = lambda: win
  29. for name in (
  30. 'I_AB_CLOSE_RED', 'I_FIRE', 'I_FILTER', 'I_AB_FILTER_OPENED',
  31. 'I_UI_BACK_YELLOW', 'I_CHECK_MAIN',
  32. ):
  33. asset = deepcopy(getattr(self, name))
  34. asset.match = lambda image, threshold=None, name=name: name in image
  35. setattr(self, name, asset)
  36. monkeypatch.setattr(timer, 'time', SimpleNamespace(time=lambda: self.now))
  37. def screenshot(self):
  38. self.frames += 1
  39. assert self.frames <= 200, 'Navigation did not finish or respect its timeout'
  40. self.now += 0.25
  41. if self.frames in self.transitions:
  42. self.screen = self.transitions.pop(self.frames)
  43. if self.rebound and self.frames == 3:
  44. self.screen = 'detail'
  45. self.rebound = False
  46. visible = {
  47. 'map': {'I_FILTER', 'I_UI_BACK_YELLOW'},
  48. 'detail': {'I_AB_CLOSE_RED', 'I_FIRE'},
  49. 'detail_loading': {'I_AB_CLOSE_RED'},
  50. 'filter': {'I_AB_FILTER_OPENED'},
  51. 'main': {'I_CHECK_MAIN'},
  52. 'unknown': set(),
  53. }[self.screen].copy()
  54. if self.screen in ('detail', 'detail_loading') and self.background_visible:
  55. visible.update({'I_FILTER', 'I_UI_BACK_YELLOW'})
  56. self.device.image = frozenset(visible)
  57. def record_click(self, x, y, control_name):
  58. self.clicks.append((control_name, self.now))
  59. if control_name == self.I_AB_CLOSE_RED.name:
  60. assert self.screen == 'detail'
  61. if self.ignored_closes:
  62. self.ignored_closes -= 1
  63. elif self.close_delay:
  64. self.screen = 'unknown'
  65. self.transitions[self.frames + self.close_delay] = 'map'
  66. else:
  67. self.screen = 'map'
  68. elif control_name == self.I_FILTER.name:
  69. assert self.screen == 'map', 'Clicked filter behind the boss detail'
  70. if not self.rebound:
  71. self.screen = 'filter'
  72. elif control_name == self.I_UI_BACK_YELLOW.name:
  73. assert self.screen == 'map', 'Clicked back behind the boss detail'
  74. if not self.rebound:
  75. self.screen = 'main'
  76. else:
  77. pytest.fail(f'Unexpected click: {control_name}')
  78. @property
  79. def clicked_names(self):
  80. return [name for name, _ in self.clicks]
  81. @pytest.mark.parametrize('navigate, destination, navigation_asset', [
  82. ('open_filter', 'filter', 'I_FILTER'),
  83. ('go_back', 'main', 'I_UI_BACK_YELLOW'),
  84. ])
  85. @pytest.mark.parametrize('win', [True, False])
  86. def test_battle_return_recovers_detail_reappearing_after_map(
  87. monkeypatch, navigate, destination, navigation_asset, win):
  88. task = AreaNavigationReplay(monkeypatch, rebound=True, win=win,
  89. ignored_closes=3, background_visible=True)
  90. assert task.boss_fight(task.I_BATTLE_1, fileter_open=False) is win
  91. getattr(task, navigate)()
  92. assert task.screen == destination
  93. button = getattr(task, navigation_asset).name
  94. # Stay over the detail long enough for the background button's cooldown
  95. # to expire, so a failed close attempt cannot fall through to that button.
  96. assert task.clicked_names == [button] + [task.I_AB_CLOSE_RED.name] * 4 + [button]
  97. close_times = [at for name, at in task.clicks if name == task.I_AB_CLOSE_RED.name]
  98. assert all(later - earlier >= 1 for earlier, later in zip(close_times, close_times[1:]))
  99. @pytest.mark.parametrize('navigate, destination', [
  100. ('open_filter', 'filter'), ('go_back', 'main'),
  101. ])
  102. @pytest.mark.parametrize('screen', ['map', 'detail'])
  103. def test_battle_return_still_accepts_direct_map_and_normal_detail(
  104. monkeypatch, navigate, destination, screen):
  105. task = AreaNavigationReplay(monkeypatch, screen=screen)
  106. assert task.boss_fight(task.I_BATTLE_1, fileter_open=False) is True
  107. getattr(task, navigate)()
  108. assert task.screen == destination
  109. assert task.clicked_names.count(task.I_AB_CLOSE_RED.name) == (screen == 'detail')
  110. @pytest.mark.parametrize('navigate, screen', [
  111. ('open_filter', 'filter'), ('go_back', 'main'),
  112. ])
  113. def test_arrived_navigation_target_needs_no_click(monkeypatch, navigate, screen):
  114. task = AreaNavigationReplay(monkeypatch, screen=screen)
  115. getattr(task, navigate)()
  116. assert task.screen == screen
  117. assert task.clicks == []
  118. @pytest.mark.parametrize('navigate', ['open_filter', 'go_back'])
  119. @pytest.mark.parametrize('screen', ['unknown', 'detail'])
  120. def test_navigation_times_out_when_page_never_changes(monkeypatch, navigate, screen):
  121. task = AreaNavigationReplay(monkeypatch, screen=screen, ignored_closes=100)
  122. with pytest.raises(GameStuckError):
  123. getattr(task, navigate)()
  124. assert task.now <= 125
  125. assert task.screen == screen
  126. assert all(name == task.I_AB_CLOSE_RED.name for name in task.clicked_names)
  127. @pytest.mark.parametrize('screen', ['map', 'filter'])
  128. @pytest.mark.parametrize('win', [True, False])
  129. def test_battle_return_waits_through_unknown_frames(monkeypatch, screen, win):
  130. task = AreaNavigationReplay(monkeypatch, screen='unknown', win=win,
  131. transitions={3: screen})
  132. assert task.boss_fight(task.I_BATTLE_1, fileter_open=False) is win
  133. assert task.screen == screen
  134. assert task.clicks == []
  135. def test_battle_return_waits_for_map_after_detail_close_animation(monkeypatch):
  136. task = AreaNavigationReplay(monkeypatch, screen='detail', close_delay=3)
  137. assert task.boss_fight(task.I_BATTLE_1, fileter_open=False) is True
  138. assert task.screen == 'map'
  139. assert task.clicked_names == [task.I_AB_CLOSE_RED.name]
  140. @pytest.mark.parametrize('screen', ['unknown', 'detail'])
  141. def test_battle_return_times_out_without_a_confirmed_page(monkeypatch, screen):
  142. task = AreaNavigationReplay(monkeypatch, screen=screen, ignored_closes=100)
  143. with pytest.raises(GameStuckError):
  144. task.boss_fight(task.I_BATTLE_1, fileter_open=False)
  145. assert task.now <= 125
  146. assert task.screen == screen
  147. assert all(name == task.I_AB_CLOSE_RED.name for name in task.clicked_names)
  148. @pytest.mark.parametrize('navigate, destination', [
  149. ('boss_fight', 'map'), ('open_filter', 'filter'), ('go_back', 'main'),
  150. ])
  151. def test_detail_loading_blocks_background_controls_until_ready(
  152. monkeypatch, navigate, destination):
  153. task = AreaNavigationReplay(monkeypatch, screen='detail_loading',
  154. transitions={5: 'detail'}, background_visible=True)
  155. if navigate == 'boss_fight':
  156. assert task.boss_fight(task.I_BATTLE_1, fileter_open=False) is True
  157. else:
  158. getattr(task, navigate)()
  159. assert task.screen == destination
  160. assert task.clicked_names[0] == task.I_AB_CLOSE_RED.name
  161. assert task.clicks[0][1] >= 101.25