Pillow/Tests/test_imageshow.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

122 lines
2.8 KiB
Python
Raw Normal View History

from __future__ import annotations
2024-01-20 14:23:03 +03:00
import os
2024-02-20 07:41:20 +03:00
from typing import Any
2020-01-27 14:46:52 +03:00
import pytest
from PIL import Image, ImageShow
2020-01-01 15:02:27 +03:00
from .helper import hopper, is_win32, on_ci
2020-01-27 14:46:52 +03:00
def test_sanity() -> None:
2020-01-27 14:46:52 +03:00
dir(Image)
dir(ImageShow)
def test_register() -> None:
# Test registering a viewer that is an instance
class TestViewer(ImageShow.Viewer):
pass
ImageShow.register(TestViewer())
2020-01-27 14:46:52 +03:00
# Restore original state
ImageShow._viewers.pop()
2014-06-10 13:10:47 +04:00
@pytest.mark.parametrize(
2020-08-31 00:37:17 +03:00
"order",
[-1, 0],
)
2024-02-20 07:41:20 +03:00
def test_viewer_show(order: int) -> None:
2020-01-27 14:46:52 +03:00
class TestViewer(ImageShow.Viewer):
2024-02-20 07:41:20 +03:00
def show_image(self, image: Image.Image, **options: Any) -> bool:
2020-01-27 14:46:52 +03:00
self.methodCalled = True
return True
2017-09-01 13:36:51 +03:00
2020-01-27 14:46:52 +03:00
viewer = TestViewer()
ImageShow.register(viewer, order)
2018-11-03 05:13:04 +03:00
2020-01-27 14:46:52 +03:00
for mode in ("1", "I;16", "LA", "RGB", "RGBA"):
viewer.methodCalled = False
with hopper(mode) as im:
2020-01-27 14:46:52 +03:00
assert ImageShow.show(im)
assert viewer.methodCalled
2017-09-01 13:36:51 +03:00
2020-01-27 14:46:52 +03:00
# Restore original state
ImageShow._viewers.pop(0)
2019-06-13 18:54:46 +03:00
2017-09-01 13:36:51 +03:00
2020-01-27 14:46:52 +03:00
@pytest.mark.skipif(
2020-08-31 00:37:17 +03:00
not on_ci() or is_win32(),
reason="Only run on CIs; hangs on Windows CIs",
2020-01-27 14:46:52 +03:00
)
2022-10-03 08:57:42 +03:00
@pytest.mark.parametrize("mode", ("1", "I;16", "LA", "RGB", "RGBA"))
2024-02-20 07:41:20 +03:00
def test_show(mode: str) -> None:
2022-10-03 08:57:42 +03:00
im = hopper(mode)
assert ImageShow.show(im)
2017-09-01 13:36:51 +03:00
2018-11-03 05:13:04 +03:00
def test_show_without_viewers() -> None:
2022-02-12 01:07:17 +03:00
viewers = ImageShow._viewers
ImageShow._viewers = []
2023-03-11 14:39:11 +03:00
with hopper() as im:
assert not ImageShow.show(im)
2022-02-12 01:07:17 +03:00
ImageShow._viewers = viewers
@pytest.mark.parametrize(
"viewer",
(
ImageShow.Viewer(),
ImageShow.WindowsViewer(),
ImageShow.MacViewer(),
ImageShow.XDGViewer(),
ImageShow.DisplayViewer(),
ImageShow.GmDisplayViewer(),
ImageShow.EogViewer(),
ImageShow.XVViewer(),
ImageShow.IPythonViewer(),
),
)
def test_show_file(viewer: ImageShow.Viewer) -> None:
assert not os.path.exists("missing.png")
with pytest.raises(FileNotFoundError):
viewer.show_file("missing.png")
def test_viewer() -> None:
2020-01-27 14:46:52 +03:00
viewer = ImageShow.Viewer()
2019-09-21 15:28:05 +03:00
2024-03-02 05:12:17 +03:00
im = Image.new("L", (1, 1))
assert viewer.get_format(im) is None
2017-03-01 12:20:18 +03:00
2020-01-27 14:46:52 +03:00
with pytest.raises(NotImplementedError):
2024-03-02 05:12:17 +03:00
viewer.get_command("")
2017-03-01 12:20:18 +03:00
2022-10-03 08:57:42 +03:00
@pytest.mark.parametrize("viewer", ImageShow._viewers)
2024-02-20 07:41:20 +03:00
def test_viewers(viewer: ImageShow.Viewer) -> None:
2022-10-03 08:57:42 +03:00
try:
viewer.get_command("test.jpg")
except NotImplementedError:
pass
2021-03-04 00:55:24 +03:00
def test_ipythonviewer() -> None:
2021-03-04 00:55:24 +03:00
pytest.importorskip("IPython", reason="IPython not installed")
for viewer in ImageShow._viewers:
if isinstance(viewer, ImageShow.IPythonViewer):
test_viewer = viewer
break
else:
2024-09-14 13:59:02 +03:00
pytest.fail("IPythonViewer not found")
2021-03-04 00:55:24 +03:00
2024-10-07 08:50:05 +03:00
with hopper() as im:
assert test_viewer.show(im) == 1