test_friend_tab.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. from types import SimpleNamespace
  2. import numpy as np
  3. import pytest
  4. from module.atom import image
  5. from tasks.Component.GeneralInvite.config_invite import FindMode
  6. from tasks.Component.GeneralInvite.general_invite import GeneralInvite, RoomType
  7. @pytest.mark.parametrize('padding', [0, 2])
  8. def test_first_friend_tab_ignores_surrounding_colors(monkeypatch, padding):
  9. task = object.__new__(GeneralInvite)
  10. task.device = SimpleNamespace(image=np.zeros((720, 1280, 3), dtype=np.uint8))
  11. x, y, w, h = task.I_FLAG_1_ON.roi_back
  12. badge = task.device.image[y:y+h, x:x+w]
  13. badge[:] = (156, 110, 86)
  14. clicks = []
  15. frames = 0
  16. def screenshot():
  17. nonlocal frames
  18. frames += 1
  19. assert frames < 9
  20. def click(rule, interval):
  21. assert rule is task.I_FLAG_1_OFF
  22. clicks.append(rule)
  23. badge[:] = (227, 193, 153)
  24. return True
  25. task.screenshot = screenshot
  26. task.click = click
  27. task.appear = lambda rule: rule is task.I_LOAD_FRIEND
  28. task.detect_select = lambda name: True
  29. for i in range(1, 5):
  30. label = '好友' if i == 1 else ''
  31. setattr(task, f'O_F_LIST_{i}', SimpleNamespace(ocr=lambda frame, label=label: label))
  32. monkeypatch.setattr(image, 'ROI_MATCH_PAD', padding)
  33. monkeypatch.setattr('tasks.Component.GeneralInvite.general_invite.sleep', lambda _: None)
  34. assert task.invite_friend('friend')
  35. assert clicks == [task.I_FLAG_1_OFF]
  36. @pytest.mark.parametrize('mode', [FindMode.AUTO_FIND, FindMode.RECENT_FRIEND])
  37. def test_second_tab_uses_selected_template_inside_wider_search_area(monkeypatch, mode):
  38. task = object.__new__(GeneralInvite)
  39. frame = np.zeros((720, 1280, 3), dtype=np.uint8)
  40. task.device = SimpleNamespace(image=frame)
  41. for index, label in enumerate(('好友', '最近', '', ''), start=1):
  42. setattr(task, f'O_F_LIST_{index}', SimpleNamespace(ocr=lambda _, label=label: label))
  43. def show_selected(rule):
  44. x, y, w, h = rule.roi_front
  45. frame[y:y+h, x:x+w] = rule.image
  46. show_selected(task.I_FLAG_1_ON)
  47. clicks = []
  48. def click(rule, interval):
  49. assert rule is task.I_FLAG_2_OFF and not clicks
  50. clicks.append(rule)
  51. show_selected(task.I_FLAG_2_ON)
  52. return True
  53. task.appear = lambda rule: rule is task.I_LOAD_FRIEND
  54. task.appear_then_click = click
  55. task.screenshot = lambda: None
  56. task.detect_select = lambda name: bool(clicks)
  57. monkeypatch.setattr('tasks.Component.GeneralInvite.general_invite.sleep', lambda _: None)
  58. assert task.invite_friend('friend', mode)
  59. assert clicks == [task.I_FLAG_2_OFF]
  60. def test_full_three_player_room_finishes_before_opening_another_invitation():
  61. task = object.__new__(GeneralInvite)
  62. task.room_type = RoomType.NORMAL_3
  63. task.appear = lambda rule: rule is task.I_GI_EMOJI_1
  64. frames = iter([None])
  65. task.screenshot = lambda: next(frames)
  66. task.appear_then_click = lambda rule, interval: False
  67. assert task.invite_friend('friend')