test_shared_guild_mask.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. """Offline tests for the shared courtyard guild entrance (no device required).
  2. Fixtures contain only the 120x100 guild search area at (495,590), cropped from
  3. user-provided new-theme open/closed screenshots after 1280x720 normalization.
  4. The original-template and altered-background cases are synthetic controls,
  5. not real screenshots of the default theme.
  6. """
  7. import hashlib
  8. import json
  9. import unittest
  10. from copy import deepcopy
  11. from pathlib import Path
  12. import cv2
  13. import numpy as np
  14. from module.atom.image import RuleImage, set_roi_match_padding
  15. from tasks.GameUi.assets import GameUiAssets
  16. from tasks.Component.Costume.config import ThemeType
  17. from tasks.Component.CostumeTheme.theme import apply_theme
  18. ROOT = Path(__file__).resolve().parents[3]
  19. FIXTURES = Path(__file__).resolve().parent / 'fixtures'
  20. OTHER_ENTRANCES = ('COLLECTION', 'TRAVEL', 'TEAM', 'MALL', 'DAILY', 'FRIENDS',
  21. 'ONMYODO', 'SHIKIGAMI_RECORDS')
  22. class SharedGuildMaskTests(unittest.TestCase):
  23. def setUp(self):
  24. apply_theme(ThemeType.COSTUME_THEME_DEFAULT)
  25. set_roi_match_padding(0)
  26. def tearDown(self):
  27. apply_theme(ThemeType.COSTUME_THEME_DEFAULT)
  28. set_roi_match_padding(0)
  29. def rule(self, method='Masked template matching'):
  30. source = GameUiAssets.I_MAIN_GOTO_GUILD
  31. return RuleImage(tuple(source.roi_front), source.roi_back, method,
  32. source.threshold, source.file)
  33. def frame(self, state):
  34. crop = cv2.cvtColor(cv2.imread(str(FIXTURES / ('guild_search_' + state + '.png'))),
  35. cv2.COLOR_BGR2RGB)
  36. frame = np.full((720, 1280, 3), 100, dtype=np.uint8)
  37. frame[590:690, 495:615] = crop
  38. return frame
  39. def test_shared_source_and_mask_contract(self):
  40. items = json.loads((ROOT / 'tasks/GameUi/page/image_main.json').read_text(encoding='utf-8'))
  41. item = next(x for x in items if x['itemName'] == 'main_goto_guild')
  42. rule = self.rule()
  43. self.assertEqual(item['method'], 'Masked template matching')
  44. self.assertEqual(GameUiAssets.I_MAIN_GOTO_GUILD.method, item['method'])
  45. self.assertEqual(rule.threshold, 0.7)
  46. self.assertEqual(rule.roi_back, (495, 590, 120, 100))
  47. self.assertEqual(rule.mask.shape, rule.image.shape[:2])
  48. self.assertEqual(set(np.unique(rule.mask)), {0, 255})
  49. self.assertGreater(np.count_nonzero(rule.mask), 200)
  50. self.assertLess(np.count_nonzero(rule.mask), rule.mask.size)
  51. self.assertEqual(hashlib.sha256((ROOT / rule.file.removeprefix('./')).read_bytes()).hexdigest(),
  52. 'b7cbc4f76c6eef129e955b9b3d89a376a721b9930e4295011e98363bdd80a5d9')
  53. def test_new_theme_open_hits_at_guild_not_another_icon(self):
  54. for pad in (0, 2):
  55. with self.subTest(padding=pad):
  56. set_roi_match_padding(pad)
  57. rule = self.rule()
  58. self.assertTrue(rule.match(self.frame('open')))
  59. self.assertGreater(rule.last_score, 0.9)
  60. self.assertEqual(rule.roi_front[:2], [556, 610])
  61. def test_plain_matching_reproduces_reported_failure(self):
  62. rule = self.rule('Template matching')
  63. self.assertFalse(rule.match(self.frame('open')))
  64. self.assertAlmostEqual(rule.last_score, 0.52842, places=4)
  65. def test_closed_scroll_is_not_a_guild_entrance(self):
  66. for pad in (0, 2):
  67. with self.subTest(padding=pad):
  68. set_roi_match_padding(pad)
  69. rule = self.rule()
  70. self.assertFalse(rule.match(self.frame('closed')))
  71. self.assertLess(rule.last_score, rule.threshold)
  72. def test_original_template_synthetic_control(self):
  73. rule = self.rule()
  74. frame = np.full((720, 1280, 3), 100, dtype=np.uint8)
  75. frame[610:632, 556:606] = rule.image
  76. self.assertTrue(rule.match(frame))
  77. self.assertGreater(rule.last_score, 0.999)
  78. self.assertEqual(rule.roi_front[:2], [556, 610])
  79. def test_ignored_background_changes_do_not_change_result(self):
  80. rng = np.random.default_rng(1862)
  81. for variant in ('black', 'white', 'noise'):
  82. with self.subTest(background=variant):
  83. rule = self.rule()
  84. icon = rule.image.copy()
  85. background = (rng.integers(0, 256, icon.shape, dtype=np.uint8)
  86. if variant == 'noise' else
  87. np.full_like(icon, 0 if variant == 'black' else 255))
  88. icon[rule.mask == 0] = background[rule.mask == 0]
  89. frame = np.full((720, 1280, 3), 100, dtype=np.uint8)
  90. frame[610:632, 556:606] = icon
  91. self.assertTrue(rule.match(frame))
  92. self.assertGreater(rule.last_score, 0.999)
  93. self.assertEqual(rule.roi_front[:2], [556, 610])
  94. def test_other_entrance_icons_do_not_match_guild(self):
  95. for name in OTHER_ENTRANCES:
  96. with self.subTest(icon=name):
  97. rule = self.rule()
  98. icon = deepcopy(getattr(GameUiAssets, 'I_MAIN_GOTO_' + name)).image
  99. h, w = icon.shape[:2]
  100. frame = np.full((720, 1280, 3), 100, dtype=np.uint8)
  101. frame[610:610+h, 535:535+w] = icon
  102. self.assertFalse(rule.match(frame))
  103. def test_theme_switch_keeps_shared_mask_and_navigation_reference(self):
  104. from tasks.GameUi.default_pages import page_main
  105. ref = GameUiAssets.I_MAIN_GOTO_GUILD
  106. transitions = [t for t in page_main.transitions if t.action is ref]
  107. self.assertTrue(transitions)
  108. for theme in (ThemeType.COSTUME_THEME_XINYU_MINGXIAO, ThemeType.COSTUME_THEME_DEFAULT):
  109. apply_theme(theme)
  110. self.assertIs(GameUiAssets.I_MAIN_GOTO_GUILD, ref)
  111. self.assertEqual(ref.method, 'Masked template matching')
  112. self.assertIsNotNone(ref.mask)
  113. self.assertTrue(all(t.action is ref for t in transitions))
  114. self.assertTrue(ref.match(self.frame('open')))
  115. if __name__ == '__main__':
  116. unittest.main()