test_area_navigation.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. """Replay battle return transitions without a running game or screenshot fixtures."""
  2. from copy import deepcopy
  3. from types import SimpleNamespace
  4. from unittest.mock import patch
  5. import pytest
  6. from module.base import timer
  7. from module.exception import GameStuckError
  8. from tasks.AreaBoss.config_boss import AreaBossFloor
  9. from tasks.AreaBoss.script_task import ScriptTask
  10. class AreaNavigationReplay(ScriptTask):
  11. def __init__(self, monkeypatch, *, screen='map', rebound=False,
  12. ignored_closes=0, background_visible=False, win=True,
  13. transitions=None, close_delay=0):
  14. # Keep the real navigation, recognition dispatch and click cooldowns.
  15. self.screen = screen
  16. self.rebound = rebound
  17. self.ignored_closes = ignored_closes
  18. self.background_visible = background_visible
  19. self.transitions = dict(transitions or {})
  20. self.close_delay = close_delay
  21. self.frames = 0
  22. self.now = 100.0
  23. self.interval_timer = {}
  24. self.clicks = []
  25. self.config = SimpleNamespace(area_boss=SimpleNamespace(
  26. boss=SimpleNamespace(reward_floor=AreaBossFloor.DEFAULT)))
  27. self.device = SimpleNamespace(image=frozenset(), click=self.record_click)
  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 finish_first_battle(self):
  38. with patch.object(self, 'open_boss_detail', return_value=True):
  39. return self.boss_fight(self.I_BATTLE_1, fileter_open=False)
  40. def screenshot(self):
  41. self.frames += 1
  42. assert self.frames <= 200, 'Navigation did not finish or respect its timeout'
  43. self.now += 0.25
  44. if self.frames in self.transitions:
  45. self.screen = self.transitions.pop(self.frames)
  46. if self.rebound and self.frames == 3:
  47. self.screen = 'detail'
  48. self.rebound = False
  49. visible = {
  50. 'map': {'I_FILTER', 'I_UI_BACK_YELLOW'},
  51. 'detail': {'I_AB_CLOSE_RED', 'I_FIRE'},
  52. 'detail_loading': {'I_AB_CLOSE_RED'},
  53. 'detail_with_filter': {'I_AB_CLOSE_RED', 'I_AB_FILTER_OPENED'},
  54. 'filter': {'I_AB_FILTER_OPENED'},
  55. 'main': {'I_CHECK_MAIN'},
  56. 'unknown': set(),
  57. }[self.screen].copy()
  58. if self.screen in ('detail', 'detail_loading') and self.background_visible:
  59. visible.update({'I_FILTER', 'I_UI_BACK_YELLOW'})
  60. self.device.image = frozenset(visible)
  61. def record_click(self, x, y, control_name):
  62. self.clicks.append((control_name, self.now))
  63. if control_name == self.I_AB_CLOSE_RED.name:
  64. assert self.screen in ('detail', 'detail_with_filter')
  65. if self.ignored_closes:
  66. self.ignored_closes -= 1
  67. elif self.screen == 'detail_with_filter':
  68. # The first tap outside the list can dismiss just the list.
  69. self.screen = 'detail'
  70. elif self.close_delay:
  71. self.screen = 'unknown'
  72. self.transitions[self.frames + self.close_delay] = 'map'
  73. else:
  74. self.screen = 'map'
  75. elif control_name == self.I_FILTER.name:
  76. assert self.screen == 'map', 'Clicked filter behind the boss detail'
  77. if not self.rebound:
  78. self.screen = 'filter'
  79. elif control_name == self.I_UI_BACK_YELLOW.name:
  80. assert self.screen == 'map', 'Clicked back behind the boss detail'
  81. if not self.rebound:
  82. self.screen = 'main'
  83. elif control_name == self.I_BATTLE_2.name:
  84. assert self.screen == 'filter', 'Selected a boss while the previous detail was open'
  85. # The list can still be folding away as the selected detail appears.
  86. self.screen = 'detail_with_filter'
  87. self.transitions[self.frames + 3] = 'detail'
  88. else:
  89. pytest.fail(f'Unexpected click: {control_name}')
  90. @property
  91. def clicked_names(self):
  92. return [name for name, _ in self.clicks]
  93. @pytest.mark.parametrize('navigate, destination, navigation_asset', [
  94. ('open_filter', 'filter', 'I_FILTER'),
  95. ('go_back', 'main', 'I_UI_BACK_YELLOW'),
  96. ])
  97. @pytest.mark.parametrize('win', [True, False])
  98. def test_battle_return_recovers_detail_reappearing_after_map(
  99. monkeypatch, navigate, destination, navigation_asset, win):
  100. task = AreaNavigationReplay(monkeypatch, rebound=True, win=win,
  101. ignored_closes=3, background_visible=True)
  102. assert task.finish_first_battle() is win
  103. assert task.clicked_names == [task.I_AB_CLOSE_RED.name] * 4
  104. getattr(task, navigate)()
  105. assert task.screen == destination
  106. button = getattr(task, navigation_asset).name
  107. assert task.clicked_names == [task.I_AB_CLOSE_RED.name] * 4 + [button]
  108. close_times = [at for name, at in task.clicks if name == task.I_AB_CLOSE_RED.name]
  109. assert all(later - earlier >= 1 for earlier, later in zip(close_times, close_times[1:]))
  110. def test_battle_return_closes_detail_under_filter_before_selecting_second_boss(monkeypatch):
  111. task = AreaNavigationReplay(monkeypatch, screen='map', transitions={3: 'detail_with_filter'})
  112. close = task.I_AB_CLOSE_RED.name
  113. assert task.finish_first_battle()
  114. assert task.screen == 'map'
  115. assert task.clicked_names == [close, close]
  116. assert task.boss_fight(task.I_BATTLE_2)
  117. assert task.clicked_names == [close, close, task.I_FILTER.name, task.I_BATTLE_2.name, close]
  118. def test_second_boss_selection_recovers_filter_already_over_detail(monkeypatch):
  119. task = AreaNavigationReplay(monkeypatch, screen='detail_with_filter')
  120. task.screenshot()
  121. assert task.boss_fight(task.I_BATTLE_2)
  122. close = task.I_AB_CLOSE_RED.name
  123. assert task.clicked_names == [close, close, task.I_FILTER.name, task.I_BATTLE_2.name, close]
  124. def test_reward_boss_fights_already_selected_detail(monkeypatch):
  125. task = AreaNavigationReplay(monkeypatch, screen='detail')
  126. assert task.boss_fight(task.C_AB_BOSS_REWARD_PHOTO_1, fileter_open=False)
  127. assert task.clicked_names == [task.I_AB_CLOSE_RED.name]
  128. def test_unavailable_boss_retries_only_on_clear_list(monkeypatch):
  129. task = AreaNavigationReplay(monkeypatch, screen='filter')
  130. def ignore_selection(x, y, control_name):
  131. assert task.screen == 'filter'
  132. task.clicks.append((control_name, task.now))
  133. task.device.click = ignore_selection
  134. assert not task.boss_fight(task.I_BATTLE_2)
  135. assert task.clicked_names == [task.I_BATTLE_2.name] * 3
  136. assert all(later - earlier >= 3 for (_, earlier), (_, later) in zip(task.clicks, task.clicks[1:]))
  137. @pytest.mark.parametrize('navigate, destination', [
  138. ('open_filter', 'filter'), ('go_back', 'main'),
  139. ])
  140. @pytest.mark.parametrize('screen', ['map', 'detail'])
  141. def test_battle_return_still_accepts_direct_map_and_normal_detail(
  142. monkeypatch, navigate, destination, screen):
  143. task = AreaNavigationReplay(monkeypatch, screen=screen)
  144. assert task.finish_first_battle() is True
  145. getattr(task, navigate)()
  146. assert task.screen == destination
  147. assert task.clicked_names.count(task.I_AB_CLOSE_RED.name) == (screen == 'detail')
  148. @pytest.mark.parametrize('navigate, screen', [
  149. ('open_filter', 'filter'), ('go_back', 'main'),
  150. ])
  151. def test_arrived_navigation_target_needs_no_click(monkeypatch, navigate, screen):
  152. task = AreaNavigationReplay(monkeypatch, screen=screen)
  153. getattr(task, navigate)()
  154. assert task.screen == screen
  155. assert task.clicks == []
  156. @pytest.mark.parametrize('navigate', ['open_filter', 'go_back'])
  157. @pytest.mark.parametrize('screen', ['unknown', 'detail'])
  158. def test_navigation_times_out_when_page_never_changes(monkeypatch, navigate, screen):
  159. task = AreaNavigationReplay(monkeypatch, screen=screen, ignored_closes=100)
  160. with pytest.raises(GameStuckError):
  161. getattr(task, navigate)()
  162. assert task.now <= 125
  163. assert task.screen == screen
  164. assert all(name == task.I_AB_CLOSE_RED.name for name in task.clicked_names)
  165. @pytest.mark.parametrize('screen', ['map', 'filter'])
  166. @pytest.mark.parametrize('win', [True, False])
  167. def test_battle_return_waits_through_unknown_frames(monkeypatch, screen, win):
  168. task = AreaNavigationReplay(monkeypatch, screen='unknown', win=win,
  169. transitions={3: screen})
  170. assert task.finish_first_battle() is win
  171. assert task.screen == screen
  172. assert task.clicks == []
  173. def test_battle_return_waits_for_map_after_detail_close_animation(monkeypatch):
  174. task = AreaNavigationReplay(monkeypatch, screen='detail', close_delay=3)
  175. assert task.finish_first_battle() is True
  176. assert task.screen == 'map'
  177. assert task.clicked_names == [task.I_AB_CLOSE_RED.name]
  178. @pytest.mark.parametrize('screen', ['unknown', 'detail'])
  179. def test_battle_return_times_out_without_a_confirmed_page(monkeypatch, screen):
  180. task = AreaNavigationReplay(monkeypatch, screen=screen, ignored_closes=100)
  181. with pytest.raises(GameStuckError):
  182. task.finish_first_battle()
  183. assert task.now <= 125
  184. assert task.screen == screen
  185. assert all(name == task.I_AB_CLOSE_RED.name for name in task.clicked_names)
  186. @pytest.mark.parametrize('navigate, destination', [
  187. ('boss_fight', 'map'), ('open_filter', 'filter'), ('go_back', 'main'),
  188. ])
  189. def test_detail_loading_blocks_background_controls_until_ready(
  190. monkeypatch, navigate, destination):
  191. task = AreaNavigationReplay(monkeypatch, screen='detail_loading',
  192. transitions={5: 'detail'}, background_visible=True)
  193. if navigate == 'boss_fight':
  194. assert task.finish_first_battle() is True
  195. else:
  196. getattr(task, navigate)()
  197. assert task.screen == destination
  198. assert task.clicked_names[0] == task.I_AB_CLOSE_RED.name
  199. assert task.clicks[0][1] >= 101.25