Skip to content

Commit efd0854

Browse files
authored
tests: enhance test_get_pixels.py, and try to fix a random failure at the same time (#252)
1 parent d1e3baa commit efd0854

File tree

4 files changed

+31
-31
lines changed

4 files changed

+31
-31
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ See Git checking messages for full history.
77
- Windows: refactored how internal handles are stored (fixes #198)
88
- Windows: removed side effects when leaving the context manager, resources are all freed
99
- CI: run tests via xvfb-run on GitHub Actions (#248)
10-
- tests: Use `PyVirtualDisplay` instead of `xvfbwrapper` (#249)
10+
- tests: enhance ``test_get_pixels.py``, and try to fix a random failure at the same time (related to #251)
11+
- tests: use `PyVirtualDisplay` instead of `xvfbwrapper` (#249)
1112
- :heart: contributors: @mgorny, @CTPaHHuK-HEbA
1213

1314
## 8.0.3 (2023/04/15)

src/tests/conftest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""
55
import glob
66
import os
7+
import platform
78
from hashlib import md5
89
from pathlib import Path
910
from zipfile import ZipFile
@@ -55,6 +56,10 @@ def raw() -> bytes:
5556
@pytest.fixture(scope="session")
5657
def pixel_ratio() -> int:
5758
"""Get the pixel, used to adapt test checks."""
59+
60+
if platform.system().lower() != "darwin":
61+
return 1
62+
5863
# Grab a 1x1 screenshot
5964
region = {"top": 0, "left": 0, "width": 1, "height": 1}
6065

src/tests/test_bgra_to_rgb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def test_bad_length():
1414
image.rgb
1515

1616

17-
def test_good_types(raw):
17+
def test_good_types(raw: bytes):
1818
image = ScreenShot.from_size(bytearray(raw), 1024, 768)
1919
assert isinstance(image.raw, bytearray)
2020
assert isinstance(image.rgb, bytes)

src/tests/test_get_pixels.py

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
This is part of the MSS Python's module.
33
Source: https://github.com/BoboTiG/python-mss
44
"""
5+
6+
import itertools
57
import os
68

79
import pytest
@@ -21,35 +23,27 @@ def test_grab_monitor():
2123

2224

2325
def test_grab_part_of_screen(pixel_ratio):
24-
monitor = {"top": 160, "left": 160, "width": 160, "height": 160}
25-
with mss(display=os.getenv("DISPLAY")) as sct:
26-
image = sct.grab(monitor)
27-
assert isinstance(image, ScreenShot)
28-
assert isinstance(image.raw, bytearray)
29-
assert isinstance(image.rgb, bytes)
30-
assert image.top == 160
31-
assert image.left == 160
32-
assert image.width == 160 * pixel_ratio
33-
assert image.height == 160 * pixel_ratio
34-
35-
36-
def test_grab_part_of_screen_rounded(pixel_ratio):
37-
monitor = {"top": 160, "left": 160, "width": 161, "height": 159}
3826
with mss(display=os.getenv("DISPLAY")) as sct:
39-
image = sct.grab(monitor)
40-
assert isinstance(image, ScreenShot)
41-
assert isinstance(image.raw, bytearray)
42-
assert isinstance(image.rgb, bytes)
43-
assert image.top == 160
44-
assert image.left == 160
45-
assert image.width == 161 * pixel_ratio
46-
assert image.height == 159 * pixel_ratio
47-
48-
49-
def test_grab_individual_pixels():
50-
monitor = {"top": 160, "left": 160, "width": 222, "height": 42}
51-
with mss(display=os.getenv("DISPLAY")) as sct:
52-
image = sct.grab(monitor)
53-
assert isinstance(image.pixel(0, 0), tuple)
27+
for width, height in itertools.product(range(1, 42), range(1, 42)):
28+
monitor = {"top": 160, "left": 160, "width": width, "height": height}
29+
image = sct.grab(monitor)
30+
31+
assert image.top == 160
32+
assert image.left == 160
33+
assert image.width == width * pixel_ratio
34+
assert image.height == height * pixel_ratio
35+
36+
37+
def test_get_pixel(raw: bytes):
38+
image = ScreenShot.from_size(bytearray(raw), 1024, 768)
39+
assert image.width == 1024
40+
assert image.height == 768
41+
assert len(image.pixels) == 768
42+
assert len(image.pixels[0]) == 1024
43+
44+
assert image.pixel(0, 0) == (135, 152, 192)
45+
assert image.pixel(image.width // 2, image.height // 2) == (0, 0, 0)
46+
assert image.pixel(image.width - 1, image.height - 1) == (135, 152, 192)
47+
5448
with pytest.raises(ScreenShotError):
5549
image.pixel(image.width + 1, 12)

0 commit comments

Comments
 (0)