test_click.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import pytest
  2. from module.atom.click import RuleClick, RuleClickExclude
  3. def test_exclude_click_string_contains_excluded_click_names():
  4. first = RuleClick((0, 0, 1, 1), (100, 100, 20, 20), name='first')
  5. second = RuleClick((0, 0, 1, 1), (200, 200, 20, 20), name='second')
  6. click = RuleClickExclude([first, second], strategy='complement', distribution='uniform')
  7. assert 'first' in str(click)
  8. assert 'second' in str(click)
  9. assert repr(click) == str(click)
  10. @pytest.mark.parametrize('strategy', ['rejection', 'complement'])
  11. @pytest.mark.parametrize('distribution', ['uniform', 'normal'])
  12. def test_exclude_click_strategies_and_distributions(strategy, distribution):
  13. excluded = RuleClick((0, 0, 1, 1), (100, 100, 200, 150))
  14. click = RuleClickExclude(excluded, strategy=strategy, distribution=distribution)
  15. assert isinstance(click, RuleClick)
  16. for _ in range(200):
  17. x, y = click.coord()
  18. assert 0 <= x < 1280
  19. assert 0 <= y < 720
  20. assert not (100 <= x < 300 and 100 <= y < 250)
  21. def test_exclude_click_uses_union_of_overlapping_roi_back_regions():
  22. clicks = [
  23. RuleClick((0, 0, 1, 1), (100, 100, 200, 150)),
  24. RuleClick((0, 0, 1, 1), (250, 150, 200, 150)),
  25. ]
  26. click = RuleClickExclude(clicks, strategy='complement', distribution='uniform')
  27. for _ in range(200):
  28. x, y = click.coord()
  29. assert not (
  30. (100 <= x < 300 and 100 <= y < 250)
  31. or (250 <= x < 450 and 150 <= y < 300)
  32. )
  33. def test_exclude_click_clips_roi_back_to_screen():
  34. excluded = RuleClick((0, 0, 1, 1), (-100, -100, 200, 200))
  35. click = RuleClickExclude(excluded, strategy='complement', distribution='uniform')
  36. for _ in range(100):
  37. x, y = click.coord()
  38. assert x >= 100 or y >= 100
  39. def test_rejection_sampling_falls_back_to_complement(monkeypatch):
  40. excluded = RuleClick((0, 0, 1, 1), (0, 0, 1279, 720))
  41. click = RuleClickExclude(excluded, strategy='rejection', distribution='uniform', max_attempts=1)
  42. sample_point = click._sample_point
  43. monkeypatch.setattr(
  44. click,
  45. '_sample_point',
  46. lambda rect, distribution: (1, 1)
  47. if rect == (0, 0, 1280, 720) else sample_point(rect, distribution),
  48. )
  49. x, y = click.coord()
  50. assert x == 1279
  51. assert 0 <= y < 720
  52. def test_strategy_and_distribution_are_selected_once_at_initialization(monkeypatch):
  53. choices = iter(['rejection', 'normal'])
  54. monkeypatch.setattr('module.atom.click.random.choice', lambda values: next(choices))
  55. click = RuleClickExclude([])
  56. assert click.strategy == 'rejection'
  57. assert click.distribution == 'normal'
  58. def test_excluded_regions_covering_screen_are_rejected():
  59. excluded = RuleClick((0, 0, 1, 1), (0, 0, 1280, 720))
  60. with pytest.raises(ValueError, match='cover the whole screen'):
  61. RuleClickExclude(excluded)