2020-01-27 14:46:52 +03:00
|
|
|
import pytest
|
2020-08-07 13:28:33 +03:00
|
|
|
|
2019-07-06 23:40:53 +03:00
|
|
|
from PIL import Image, ImageShow
|
2012-10-16 00:26:38 +04:00
|
|
|
|
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():
|
|
|
|
dir(Image)
|
|
|
|
dir(ImageShow)
|
|
|
|
|
|
|
|
|
|
|
|
def test_register():
|
|
|
|
# Test registering a viewer that is not a class
|
|
|
|
ImageShow.register("not a class")
|
|
|
|
|
|
|
|
# Restore original state
|
|
|
|
ImageShow._viewers.pop()
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2020-06-18 14:40:38 +03:00
|
|
|
@pytest.mark.parametrize(
|
2020-08-31 00:37:17 +03:00
|
|
|
"order",
|
|
|
|
[-1, 0],
|
2020-06-18 14:40:38 +03:00
|
|
|
)
|
|
|
|
def test_viewer_show(order):
|
2020-01-27 14:46:52 +03:00
|
|
|
class TestViewer(ImageShow.Viewer):
|
|
|
|
def show_image(self, image, **options):
|
|
|
|
self.methodCalled = True
|
|
|
|
return True
|
2017-09-01 13:36:51 +03:00
|
|
|
|
2020-01-27 14:46:52 +03:00
|
|
|
viewer = TestViewer()
|
2020-06-18 14:40:38 +03:00
|
|
|
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"):
|
2020-06-18 14:40:38 +03:00
|
|
|
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"))
|
|
|
|
def test_show(mode):
|
|
|
|
im = hopper(mode)
|
|
|
|
assert ImageShow.show(im)
|
2017-09-01 13:36:51 +03:00
|
|
|
|
2018-11-03 05:13:04 +03:00
|
|
|
|
2022-02-12 01:07:17 +03:00
|
|
|
def test_show_without_viewers():
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2020-01-27 14:46:52 +03:00
|
|
|
def test_viewer():
|
|
|
|
viewer = ImageShow.Viewer()
|
2019-09-21 15:28:05 +03:00
|
|
|
|
2020-01-27 14:46:52 +03:00
|
|
|
assert viewer.get_format(None) is None
|
2017-03-01 12:20:18 +03:00
|
|
|
|
2020-01-27 14:46:52 +03:00
|
|
|
with pytest.raises(NotImplementedError):
|
|
|
|
viewer.get_command(None)
|
2017-03-01 12:20:18 +03:00
|
|
|
|
|
|
|
|
2022-10-03 08:57:42 +03:00
|
|
|
@pytest.mark.parametrize("viewer", ImageShow._viewers)
|
|
|
|
def test_viewers(viewer):
|
|
|
|
try:
|
|
|
|
viewer.get_command("test.jpg")
|
|
|
|
except NotImplementedError:
|
|
|
|
pass
|
2021-03-04 00:55:24 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_ipythonviewer():
|
|
|
|
pytest.importorskip("IPython", reason="IPython not installed")
|
|
|
|
for viewer in ImageShow._viewers:
|
|
|
|
if isinstance(viewer, ImageShow.IPythonViewer):
|
|
|
|
test_viewer = viewer
|
|
|
|
break
|
|
|
|
else:
|
2023-11-12 23:30:28 +03:00
|
|
|
pytest.fail()
|
2021-03-04 00:55:24 +03:00
|
|
|
|
|
|
|
im = hopper()
|
|
|
|
assert test_viewer.show(im) == 1
|