test_hero_test_skill_fallback.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. """Exercise HeroTest selection loops without a Config, Device or running game."""
  2. import itertools
  3. import unittest
  4. from types import SimpleNamespace
  5. from unittest.mock import patch
  6. from module.atom.click import RuleClick
  7. from tasks.HeroTest.config import SkillMode
  8. from tasks.HeroTest.script_task import ScriptTask
  9. class ScriptedHeroTest(ScriptTask):
  10. def __init__(self, *, mode=SkillMode.PVE, panel_visible=True, close_on_frame=None):
  11. # Skip BaseTask.__init__, which requires a real Config and Device.
  12. self.conf = SimpleNamespace(herotest=SimpleNamespace(skill_mode=mode))
  13. self.panel_visible = panel_visible
  14. self.close_on_frame = close_on_frame
  15. self.frames = 0
  16. self.visible_skills = []
  17. self.attempted_skills = []
  18. self.events = []
  19. def screenshot(self):
  20. self.frames += 1
  21. if self.frames > 3:
  22. raise AssertionError('Skill selection kept retrying after the timeout')
  23. if self.frames == self.close_on_frame:
  24. self.panel_visible = False
  25. def wait_until_appear(self, button, **kwargs):
  26. return self.appear(button)
  27. def appear(self, button, **kwargs):
  28. return button is self.I_BCMJ_SKILL_ADD_CONFIRM and self.panel_visible
  29. def appear_then_click(self, button, **kwargs):
  30. if button is self.I_BCMJ_SKILL_ADD_CONFIRM:
  31. if not self.panel_visible:
  32. return False
  33. self.events.append(('confirm', button))
  34. self.panel_visible = False
  35. return True
  36. self.attempted_skills.append(button)
  37. if any(button is skill for skill in self.visible_skills):
  38. self.events.append(('preferred', button))
  39. return True
  40. return False
  41. def click(self, button, **kwargs):
  42. self.events.append(('fallback', button))
  43. return True
  44. def ui_click_until_disappear(self, button, **kwargs):
  45. self.appear_then_click(button)
  46. class HeroTestSkillFallbackTests(unittest.TestCase):
  47. def setUp(self):
  48. timer_patch = patch('tasks.HeroTest.script_task.Timer')
  49. timer = timer_patch.start()
  50. self.addCleanup(timer_patch.stop)
  51. # Allow one normal recognition attempt, then keep reporting a timeout.
  52. self.expired = timer.return_value.start.return_value.reached_and_reset
  53. def run_selection(self, task, method):
  54. self.expired.side_effect = itertools.chain([False], itertools.repeat(True))
  55. return getattr(task, method)()
  56. def assert_fallback_then_confirm(self, task, method):
  57. self.assertTrue(self.run_selection(task, method))
  58. self.assertEqual([event for event, _ in task.events], ['fallback', 'confirm'])
  59. self.assertIsInstance(task.events[0][1], RuleClick)
  60. self.assertEqual(task.frames, 2)
  61. def test_hero1_unrecognized_skills_fall_back_then_confirm(self):
  62. self.assert_fallback_then_confirm(ScriptedHeroTest(), 'hero1_skill_wait')
  63. def test_hero2_unrecognized_skills_fall_back_then_confirm(self):
  64. self.assert_fallback_then_confirm(ScriptedHeroTest(), 'hero2_skill_wait')
  65. def test_empty_hero2_pvp_priority_list_does_not_retry_forever(self):
  66. task = ScriptedHeroTest(mode=SkillMode.PVP)
  67. self.assert_fallback_then_confirm(task, 'hero2_skill_wait')
  68. def test_hero1_recognized_skills_keep_their_priority(self):
  69. task = ScriptedHeroTest()
  70. task.visible_skills = [task.I_BCMJ_SKILL_ADD2, task.I_BCMJ_BLESS]
  71. self.assertTrue(self.run_selection(task, 'hero1_skill_wait'))
  72. self.assertEqual([event for event, _ in task.events], ['preferred', 'confirm'])
  73. self.assertIs(task.events[0][1], task.I_BCMJ_SKILL_ADD2)
  74. self.assertEqual(len(task.attempted_skills), 2)
  75. self.assertEqual(task.frames, 1)
  76. def test_hero2_recognized_skills_keep_their_priority(self):
  77. task = ScriptedHeroTest()
  78. task.visible_skills = [task.I_HERO2_SKILL2, task.I_HERO2_SKILL3]
  79. self.assertTrue(self.run_selection(task, 'hero2_skill_wait'))
  80. self.assertEqual([event for event, _ in task.events], ['preferred', 'confirm'])
  81. self.assertIs(task.events[0][1], task.I_HERO2_SKILL2)
  82. self.assertEqual(len(task.attempted_skills), 2)
  83. self.assertEqual(task.frames, 1)
  84. def test_no_panel_does_not_click(self):
  85. for method in ('hero1_skill_wait', 'hero2_skill_wait'):
  86. with self.subTest(method=method):
  87. task = ScriptedHeroTest(panel_visible=False)
  88. self.assertFalse(self.run_selection(task, method))
  89. self.assertEqual(task.events, [])
  90. self.assertEqual(task.frames, 0)
  91. def test_panel_closed_at_timeout_does_not_click_randomly(self):
  92. for method in ('hero1_skill_wait', 'hero2_skill_wait'):
  93. with self.subTest(method=method):
  94. task = ScriptedHeroTest(close_on_frame=2)
  95. self.run_selection(task, method)
  96. self.assertEqual(task.events, [])
  97. self.assertEqual(task.frames, 2)
  98. if __name__ == '__main__':
  99. unittest.main()