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
|
|
|
)
|
|
|
|
def test_show():
|
|
|
|
for mode in ("1", "I;16", "LA", "RGB", "RGBA"):
|
|
|
|
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 = []
|
|
|
|
|
|
|
|
im = hopper()
|
|
|
|
assert not ImageShow.show(im)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
2020-01-27 14:46:52 +03:00
|
|
|
def test_viewers():
|
|
|
|
for viewer in ImageShow._viewers:
|
2021-03-04 00:55:24 +03:00
|
|
|
try:
|
|
|
|
viewer.get_command("test.jpg")
|
|
|
|
except NotImplementedError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
assert False
|
|
|
|
|
|
|
|
im = hopper()
|
|
|
|
assert test_viewer.show(im) == 1
|
2022-01-15 08:08:37 +03:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
not on_ci() or is_win32(),
|
|
|
|
reason="Only run on CIs; hangs on Windows CIs",
|
|
|
|
)
|
2022-01-17 00:59:17 +03:00
|
|
|
def test_file_deprecated(tmp_path):
|
|
|
|
f = str(tmp_path / "temp.jpg")
|
2022-01-15 08:08:37 +03:00
|
|
|
for viewer in ImageShow._viewers:
|
2022-01-17 00:59:17 +03:00
|
|
|
hopper().save(f)
|
2022-01-15 08:08:37 +03:00
|
|
|
with pytest.warns(DeprecationWarning):
|
|
|
|
try:
|
2022-01-17 00:59:17 +03:00
|
|
|
viewer.show_file(file=f)
|
2022-01-15 08:08:37 +03:00
|
|
|
except NotImplementedError:
|
|
|
|
pass
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
viewer.show_file()
|