Converted common Qt test classes

This commit is contained in:
Andrew Murray 2020-03-27 20:30:00 +11:00
parent 748739c992
commit a8637449b9
7 changed files with 120 additions and 107 deletions

View File

@ -3,7 +3,6 @@ import pytest
from PIL import FontFile, Image, ImageDraw, ImageFont, PcfFontFile
from .helper import (
PillowTestCase,
assert_image_equal,
assert_image_similar,
skip_unless_feature,
@ -16,6 +15,7 @@ message = "hello, world"
pytestmark = skip_unless_feature("zlib")
def save_font(request, tmp_path):
with open(fontname, "rb") as test_file:
font = PcfFontFile.PcfFontFile(test_file)
@ -30,6 +30,7 @@ def save_font(request, tmp_path):
os.remove(tempname[:-4] + ".pbm")
except OSError:
pass # report?
request.addfinalizer(delete_tempfile)
font.save(tempname)
@ -42,14 +43,17 @@ def save_font(request, tmp_path):
assert f_loaded.read() == f_target.read()
return tempname
def test_sanity(request, tmp_path):
save_font(request, tmp_path)
def test_invalid_file():
with open("Tests/images/flower.jpg", "rb") as fp:
with pytest.raises(SyntaxError):
PcfFontFile.PcfFontFile(fp)
def test_draw(request, tmp_path):
tempname = save_font(request, tmp_path)
font = ImageFont.load(tempname)
@ -59,6 +63,7 @@ def test_draw(request, tmp_path):
with Image.open("Tests/images/test_draw_pbm_target.png") as target:
assert_image_similar(im, target, 0)
def test_textsize(request, tmp_path):
tempname = save_font(request, tmp_path)
font = ImageFont.load(tempname)
@ -70,6 +75,7 @@ def test_textsize(request, tmp_path):
msg = message[: l + 1]
assert font.getsize(msg) == (len(msg) * 10, 20)
def _test_high_characters(request, tmp_path, message):
tempname = save_font(request, tmp_path)
font = ImageFont.load(tempname)
@ -79,6 +85,7 @@ def _test_high_characters(request, tmp_path, message):
with Image.open("Tests/images/high_ascii_chars.png") as target:
assert_image_similar(im, target, 0)
def test_high_characters(request, tmp_path):
message = "".join(chr(i + 1) for i in range(140, 232))
_test_high_characters(request, tmp_path, message)

View File

@ -1,12 +1,13 @@
import pytest
from PIL import Image, ImageQt
from .helper import PillowTestCase, assert_image_equal, hopper
from .helper import assert_image_equal, hopper
from .test_imageqt import skip_if_qt_is_not_installed
pytestmark = skip_if_qt_is_not_installed()
@pytest.fixture
def test_images():
ims = [
@ -20,6 +21,7 @@ def test_images():
for im in ims.values():
im.close()
def roundtrip(expected):
# PIL -> Qt
intermediate = expected.toqimage()
@ -31,22 +33,27 @@ def roundtrip(expected):
else:
assert_image_equal(result, expected.convert("RGB"))
def test_sanity_1(test_images):
for im in test_images:
roundtrip(im.convert("1"))
def test_sanity_rgb(test_images):
for im in test_images:
roundtrip(im.convert("RGB"))
def test_sanity_rgba(test_images):
for im in test_images:
roundtrip(im.convert("RGBA"))
def test_sanity_l(test_images):
for im in test_images:
roundtrip(im.convert("L"))
def test_sanity_p(test_images):
for im in test_images:
roundtrip(im.convert("P"))

View File

@ -1,8 +1,6 @@
import pytest
from PIL import Image, ImageFilter
from .helper import PillowTestCase
@pytest.fixture
def test_images():

View File

@ -1,7 +1,7 @@
import pytest
from PIL import ImageQt
from .helper import PillowTestCase, hopper
from .helper import hopper
if ImageQt.qt_is_installed:
from PIL.ImageQt import qRgba
@ -9,6 +9,23 @@ if ImageQt.qt_is_installed:
def skip_if_qt_is_not_installed():
pass
@pytest.fixture
def qpixmap_app():
try:
if ImageQt.qt_version == "5":
from PyQt5.QtGui import QGuiApplication
elif ImageQt.qt_version == "side2":
from PySide2.QtGui import QGuiApplication
except ImportError:
pytest.skip("QGuiApplication not installed")
return
app = QGuiApplication([])
try:
yield
finally:
app.quit()
else:
@ -16,34 +33,10 @@ else:
return pytest.mark.skip(reason="Qt bindings are not installed")
class PillowQtTestCase:
def setUp(self):
skip_if_qt_is_not_installed(self)
def tearDown(self):
pass
pytestmark = skip_if_qt_is_not_installed()
class PillowQPixmapTestCase(PillowQtTestCase):
def setUp(self):
super().setUp()
try:
if ImageQt.qt_version == "5":
from PyQt5.QtGui import QGuiApplication
elif ImageQt.qt_version == "side2":
from PySide2.QtGui import QGuiApplication
except ImportError:
self.skipTest("QGuiApplication not installed")
self.app = QGuiApplication([])
def tearDown(self):
super().tearDown()
self.app.quit()
class TestImageQt(PillowQtTestCase, PillowTestCase):
def test_rgb(self):
def test_rgb():
# from https://doc.qt.io/archives/qt-4.8/qcolor.html
# typedef QRgb
# An ARGB quadruplet on the format #AARRGGBB,
@ -67,6 +60,7 @@ class TestImageQt(PillowQtTestCase, PillowTestCase):
checkrgb(0, 255, 0)
checkrgb(0, 0, 255)
def test_image(self):
def test_image():
for mode in ("1", "RGB", "RGBA", "L", "P"):
ImageQt.ImageQt(hopper(mode))

View File

@ -1,15 +1,18 @@
import pytest
from PIL import ImageQt
from .helper import PillowTestCase, assert_image_equal, hopper
from .test_imageqt import PillowQPixmapTestCase
from .helper import assert_image_equal, hopper
from .test_imageqt import qpixmap_app, skip_if_qt_is_not_installed
pytestmark = skip_if_qt_is_not_installed()
class TestFromQPixmap(PillowQPixmapTestCase, PillowTestCase):
def roundtrip(self, expected):
def roundtrip(expected):
result = ImageQt.fromqpixmap(ImageQt.toqpixmap(expected))
# Qt saves all pixmaps as rgb
assert_image_equal(result, expected.convert("RGB"))
def test_sanity(self):
def test_sanity(qpixmap_app):
for mode in ("1", "RGB", "RGBA", "L", "P"):
self.roundtrip(hopper(mode))
roundtrip(hopper(mode))

View File

@ -1,7 +1,9 @@
from PIL import Image, ImageQt
from .helper import PillowTestCase, assert_image_equal, hopper
from .test_imageqt import PillowQtTestCase
from .helper import assert_image_equal, hopper
from .test_imageqt import skip_if_qt_is_not_installed
pytestmark = skip_if_qt_is_not_installed()
if ImageQt.qt_is_installed:
from PIL.ImageQt import QImage
@ -14,8 +16,7 @@ if ImageQt.qt_is_installed:
from PySide2.QtWidgets import QWidget, QHBoxLayout, QLabel, QApplication
class TestToQImage(PillowQtTestCase, PillowTestCase):
def test_sanity(self):
def test_sanity(tmp_path):
for mode in ("RGB", "RGBA", "L", "P", "1"):
src = hopper(mode)
data = ImageQt.toqimage(src)
@ -39,14 +40,15 @@ class TestToQImage(PillowQtTestCase, PillowTestCase):
continue
# Test saving the file
tempfile = self.tempfile("temp_{}.png".format(mode))
tempfile = str(tmp_path / "temp_{}.png".format(mode))
data.save(tempfile)
# Check that it actually worked.
with Image.open(tempfile) as reloaded:
assert_image_equal(reloaded, src)
def test_segfault(self):
def test_segfault():
app = QApplication([])
ex = Example()
assert app # Silence warning

View File

@ -1,14 +1,16 @@
import pytest
from PIL import ImageQt
from .helper import PillowTestCase, hopper
from .test_imageqt import PillowQPixmapTestCase
from .helper import hopper
from .test_imageqt import qpixmap_app, skip_if_qt_is_not_installed
if ImageQt.qt_is_installed:
from PIL.ImageQt import QPixmap
pytestmark = skip_if_qt_is_not_installed()
class TestToQPixmap(PillowQPixmapTestCase, PillowTestCase):
def test_sanity(self):
def test_sanity(qpixmap, tmp_path):
for mode in ("1", "RGB", "RGBA", "L", "P"):
data = ImageQt.toqpixmap(hopper(mode))
@ -16,5 +18,5 @@ class TestToQPixmap(PillowQPixmapTestCase, PillowTestCase):
assert not data.isNull()
# Test saving the file
tempfile = self.tempfile("temp_{}.png".format(mode))
tempfile = str(tmp_path / "temp_{}.png".format(mode))
data.save(tempfile)