test_click.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import pytest
  2. from module.atom.click import RuleClick, RuleClickExclude
  3. def test_coord_in_excluded_selects_a_matching_click_and_uses_its_roi_front():
  4. first = RuleClick((10, 20, 5, 6), (100, 100, 20, 20), name='first')
  5. second = RuleClick((30, 40, 7, 8), (200, 200, 20, 20), name='second')
  6. click = RuleClickExclude([first, second], strategy='complement', distribution='uniform')
  7. for _ in range(100):
  8. x, y = click.coord_in_excluded(['second'])
  9. assert 30 <= x < 37
  10. assert 40 <= y < 48
  11. def test_coord_in_excluded_matches_asset_attribute_name():
  12. click = RuleClick((30, 40, 7, 8), (200, 200, 20, 20), name='end_1_1')
  13. exclude = RuleClickExclude([click], strategy='complement', distribution='uniform')
  14. x, y = exclude.coord_in_excluded(['C_END_1_1'])
  15. assert 30 <= x < 37
  16. assert 40 <= y < 48
  17. def test_coord_in_excluded_randomly_selects_from_multiple_matches(monkeypatch):
  18. first = RuleClick((10, 20, 5, 6), (100, 100, 20, 20), name='area')
  19. second = RuleClick((30, 40, 7, 8), (200, 200, 20, 20), name='area')
  20. click = RuleClickExclude([first, second], strategy='complement', distribution='uniform')
  21. monkeypatch.setattr('module.atom.click.random.choice', lambda values: values[1])
  22. x, y = click.coord_in_excluded(['area'])
  23. assert 30 <= x < 37
  24. assert 40 <= y < 48
  25. def test_coord_in_excluded_rejects_unknown_area():
  26. click = RuleClickExclude([], strategy='complement', distribution='uniform')
  27. with pytest.raises(ValueError, match='No excluded click matches'):
  28. click.coord_in_excluded(['unknown'])
  29. def test_exclude_click_string_contains_excluded_click_names():
  30. first = RuleClick((0, 0, 1, 1), (100, 100, 20, 20), name='first')
  31. second = RuleClick((0, 0, 1, 1), (200, 200, 20, 20), name='second')
  32. click = RuleClickExclude([first, second], strategy='complement', distribution='uniform')
  33. assert 'first' in str(click)
  34. assert 'second' in str(click)
  35. assert repr(click) == str(click)
  36. @pytest.mark.parametrize('strategy', ['rejection', 'complement'])
  37. @pytest.mark.parametrize('distribution', ['uniform', 'normal'])
  38. def test_exclude_click_strategies_and_distributions(strategy, distribution):
  39. excluded = RuleClick((0, 0, 1, 1), (100, 100, 200, 150))
  40. click = RuleClickExclude(excluded, strategy=strategy, distribution=distribution)
  41. assert isinstance(click, RuleClick)
  42. for _ in range(200):
  43. x, y = click.coord()
  44. assert 0 <= x < 1280
  45. assert 0 <= y < 720
  46. assert not (100 <= x < 300 and 100 <= y < 250)
  47. def test_exclude_click_uses_union_of_overlapping_roi_back_regions():
  48. clicks = [
  49. RuleClick((0, 0, 1, 1), (100, 100, 200, 150)),
  50. RuleClick((0, 0, 1, 1), (250, 150, 200, 150)),
  51. ]
  52. click = RuleClickExclude(clicks, strategy='complement', distribution='uniform')
  53. for _ in range(200):
  54. x, y = click.coord()
  55. assert not (
  56. (100 <= x < 300 and 100 <= y < 250)
  57. or (250 <= x < 450 and 150 <= y < 300)
  58. )
  59. def test_exclude_click_clips_roi_back_to_screen():
  60. excluded = RuleClick((0, 0, 1, 1), (-100, -100, 200, 200))
  61. click = RuleClickExclude(excluded, strategy='complement', distribution='uniform')
  62. for _ in range(100):
  63. x, y = click.coord()
  64. assert x >= 100 or y >= 100
  65. def test_rejection_sampling_falls_back_to_complement(monkeypatch):
  66. excluded = RuleClick((0, 0, 1, 1), (0, 0, 1279, 720))
  67. click = RuleClickExclude(excluded, strategy='rejection', distribution='uniform', max_attempts=1)
  68. sample_point = click._sample_point
  69. monkeypatch.setattr(
  70. click,
  71. '_sample_point',
  72. lambda rect, distribution: (1, 1)
  73. if rect == (0, 0, 1280, 720) else sample_point(rect, distribution),
  74. )
  75. x, y = click.coord()
  76. assert x == 1279
  77. assert 0 <= y < 720
  78. def test_strategy_and_distribution_are_selected_once_at_initialization(monkeypatch):
  79. choices = iter(['rejection', 'normal'])
  80. monkeypatch.setattr('module.atom.click.random.choice', lambda values: next(choices))
  81. click = RuleClickExclude([])
  82. assert click.strategy == 'rejection'
  83. assert click.distribution == 'normal'
  84. def test_excluded_regions_covering_screen_are_rejected():
  85. excluded = RuleClick((0, 0, 1, 1), (0, 0, 1280, 720))
  86. with pytest.raises(ValueError, match='cover the whole screen'):
  87. RuleClickExclude(excluded)