|
2 | 2 | Source: https://github.com/BoboTiG/python-mss. |
3 | 3 | """ |
4 | 4 |
|
5 | | -import hashlib |
6 | | -import zlib |
7 | | -from collections.abc import Callable |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +import io |
8 | 8 | from pathlib import Path |
| 9 | +from typing import TYPE_CHECKING |
9 | 10 |
|
10 | 11 | import pytest |
11 | 12 |
|
12 | | -from mss.base import MSSBase |
13 | 13 | from mss.tools import to_png |
14 | 14 |
|
| 15 | +if TYPE_CHECKING: |
| 16 | + from collections.abc import Callable |
| 17 | + |
| 18 | + from mss.base import MSSBase |
| 19 | + |
15 | 20 | WIDTH = 10 |
16 | 21 | HEIGHT = 10 |
17 | | -MD5SUM = "ee1b645cc989cbfc48e613b395a929d3d79a922b77b9b38e46647ff6f74acef5" |
| 22 | + |
| 23 | + |
| 24 | +def assert_is_valid_png(*, raw: bytes | None = None, file: Path | None = None) -> None: |
| 25 | + Image = pytest.importorskip("PIL.Image", reason="PIL module not available.") # noqa: N806 |
| 26 | + |
| 27 | + assert bool(Image.open(io.BytesIO(raw) if raw is not None else file).tobytes()) |
| 28 | + try: |
| 29 | + Image.open(io.BytesIO(raw) if raw is not None else file).verify() |
| 30 | + except Exception: # noqa: BLE001 |
| 31 | + pytest.fail(reason="invalid PNG data") |
18 | 32 |
|
19 | 33 |
|
20 | 34 | def test_bad_compression_level(mss_impl: Callable[..., MSSBase]) -> None: |
21 | | - with mss_impl(compression_level=42) as sct, pytest.raises(zlib.error): |
| 35 | + with mss_impl(compression_level=42) as sct, pytest.raises(Exception, match="Bad compression level"): |
22 | 36 | sct.shot() |
23 | 37 |
|
24 | 38 |
|
25 | | -def test_compression_level(mss_impl: Callable[..., MSSBase]) -> None: |
26 | | - data = b"rgb" * WIDTH * HEIGHT |
27 | | - output = Path(f"{WIDTH}x{HEIGHT}.png") |
28 | | - |
29 | | - with mss_impl() as sct: |
30 | | - to_png(data, (WIDTH, HEIGHT), level=sct.compression_level, output=output) |
31 | | - |
32 | | - assert hashlib.sha256(output.read_bytes()).hexdigest() == MD5SUM |
33 | | - |
34 | | - |
35 | | -@pytest.mark.parametrize( |
36 | | - ("level", "checksum"), |
37 | | - [ |
38 | | - (0, "547191069e78eef1c5899f12c256dd549b1338e67c5cd26a7cbd1fc5a71b83aa"), |
39 | | - (1, "841665ec73b641dfcafff5130b497f5c692ca121caeb06b1d002ad3de5c77321"), |
40 | | - (2, "b11107163207f68f36294deb3f8e6b6a5a11399a532917bdd59d1d5f1117d4d0"), |
41 | | - (3, "31278bad8c1c077c715ac4f3b497694a323a71a87c5ff8bdc7600a36bd8d8c96"), |
42 | | - (4, "8f7237e1394d9ddc71fcb1fa4a2c2953087562ef6eac85d32d8154b61b287fb0"), |
43 | | - (5, "83a55f161bad2d511b222dcd32059c9adf32c3238b65f9aa576f19bc0a6c8fec"), |
44 | | - (6, "ee1b645cc989cbfc48e613b395a929d3d79a922b77b9b38e46647ff6f74acef5"), |
45 | | - (7, "85f8d1b01cef926c111b194229bd6c01e2a65b18b4dd902293698e6de8f4e9ac"), |
46 | | - (8, "85f8d1b01cef926c111b194229bd6c01e2a65b18b4dd902293698e6de8f4e9ac"), |
47 | | - (9, "85f8d1b01cef926c111b194229bd6c01e2a65b18b4dd902293698e6de8f4e9ac"), |
48 | | - ], |
49 | | -) |
50 | | -def test_compression_levels(level: int, checksum: str) -> None: |
| 39 | +@pytest.mark.parametrize("level", range(10)) |
| 40 | +def test_compression_level(level: int) -> None: |
51 | 41 | data = b"rgb" * WIDTH * HEIGHT |
52 | 42 | raw = to_png(data, (WIDTH, HEIGHT), level=level) |
53 | 43 | assert isinstance(raw, bytes) |
54 | | - sha256 = hashlib.sha256(raw).hexdigest() |
55 | | - assert sha256 == checksum |
| 44 | + assert_is_valid_png(raw=raw) |
56 | 45 |
|
57 | 46 |
|
58 | 47 | def test_output_file() -> None: |
59 | 48 | data = b"rgb" * WIDTH * HEIGHT |
60 | 49 | output = Path(f"{WIDTH}x{HEIGHT}.png") |
61 | 50 | to_png(data, (WIDTH, HEIGHT), output=output) |
62 | | - |
63 | 51 | assert output.is_file() |
64 | | - assert hashlib.sha256(output.read_bytes()).hexdigest() == MD5SUM |
| 52 | + assert_is_valid_png(file=output) |
65 | 53 |
|
66 | 54 |
|
67 | 55 | def test_output_raw_bytes() -> None: |
68 | 56 | data = b"rgb" * WIDTH * HEIGHT |
69 | 57 | raw = to_png(data, (WIDTH, HEIGHT)) |
70 | 58 | assert isinstance(raw, bytes) |
71 | | - assert hashlib.sha256(raw).hexdigest() == MD5SUM |
| 59 | + assert_is_valid_png(raw=raw) |
0 commit comments