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,57 +33,34 @@ 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")
def test_rgb():
# from https://doc.qt.io/archives/qt-4.8/qcolor.html
# typedef QRgb
# An ARGB quadruplet on the format #AARRGGBB,
# equivalent to an unsigned int.
if ImageQt.qt_version == "5":
from PyQt5.QtGui import qRgb
elif ImageQt.qt_version == "side2":
from PySide2.QtGui import qRgb
self.app = QGuiApplication([])
assert qRgb(0, 0, 0) == qRgba(0, 0, 0, 255)
def tearDown(self):
super().tearDown()
self.app.quit()
def checkrgb(r, g, b):
val = ImageQt.rgb(r, g, b)
val = val % 2 ** 24 # drop the alpha
assert val >> 16 == r
assert ((val >> 8) % 2 ** 8) == g
assert val % 2 ** 8 == b
checkrgb(0, 0, 0)
checkrgb(255, 0, 0)
checkrgb(0, 255, 0)
checkrgb(0, 0, 255)
class TestImageQt(PillowQtTestCase, PillowTestCase):
def test_rgb(self):
# from https://doc.qt.io/archives/qt-4.8/qcolor.html
# typedef QRgb
# An ARGB quadruplet on the format #AARRGGBB,
# equivalent to an unsigned int.
if ImageQt.qt_version == "5":
from PyQt5.QtGui import qRgb
elif ImageQt.qt_version == "side2":
from PySide2.QtGui import qRgb
assert qRgb(0, 0, 0) == qRgba(0, 0, 0, 255)
def checkrgb(r, g, b):
val = ImageQt.rgb(r, g, b)
val = val % 2 ** 24 # drop the alpha
assert val >> 16 == r
assert ((val >> 8) % 2 ** 8) == g
assert val % 2 ** 8 == b
checkrgb(0, 0, 0)
checkrgb(255, 0, 0)
checkrgb(0, 255, 0)
checkrgb(0, 0, 255)
def test_image(self):
for mode in ("1", "RGB", "RGBA", "L", "P"):
ImageQt.ImageQt(hopper(mode))
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):
result = ImageQt.fromqpixmap(ImageQt.toqpixmap(expected))
# Qt saves all pixmaps as rgb
assert_image_equal(result, expected.convert("RGB"))
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):
for mode in ("1", "RGB", "RGBA", "L", "P"):
self.roundtrip(hopper(mode))
def test_sanity(qpixmap_app):
for mode in ("1", "RGB", "RGBA", "L", "P"):
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,43 +16,43 @@ if ImageQt.qt_is_installed:
from PySide2.QtWidgets import QWidget, QHBoxLayout, QLabel, QApplication
class TestToQImage(PillowQtTestCase, PillowTestCase):
def test_sanity(self):
for mode in ("RGB", "RGBA", "L", "P", "1"):
src = hopper(mode)
data = ImageQt.toqimage(src)
def test_sanity(tmp_path):
for mode in ("RGB", "RGBA", "L", "P", "1"):
src = hopper(mode)
data = ImageQt.toqimage(src)
assert isinstance(data, QImage)
assert not data.isNull()
assert isinstance(data, QImage)
assert not data.isNull()
# reload directly from the qimage
rt = ImageQt.fromqimage(data)
if mode in ("L", "P", "1"):
assert_image_equal(rt, src.convert("RGB"))
else:
assert_image_equal(rt, src)
# reload directly from the qimage
rt = ImageQt.fromqimage(data)
if mode in ("L", "P", "1"):
assert_image_equal(rt, src.convert("RGB"))
else:
assert_image_equal(rt, src)
if mode == "1":
# BW appears to not save correctly on QT4 and QT5
# kicks out errors on console:
# libpng warning: Invalid color type/bit depth combination
# in IHDR
# libpng error: Invalid IHDR data
continue
if mode == "1":
# BW appears to not save correctly on QT4 and QT5
# kicks out errors on console:
# libpng warning: Invalid color type/bit depth combination
# in IHDR
# libpng error: Invalid IHDR data
continue
# Test saving the file
tempfile = self.tempfile("temp_{}.png".format(mode))
data.save(tempfile)
# Test saving the file
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)
# Check that it actually worked.
with Image.open(tempfile) as reloaded:
assert_image_equal(reloaded, src)
def test_segfault(self):
app = QApplication([])
ex = Example()
assert app # Silence warning
assert ex # Silence warning
def test_segfault():
app = QApplication([])
ex = Example()
assert app # Silence warning
assert ex # Silence warning
if ImageQt.qt_is_installed:

View File

@ -1,20 +1,22 @@
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):
for mode in ("1", "RGB", "RGBA", "L", "P"):
data = ImageQt.toqpixmap(hopper(mode))
assert isinstance(data, QPixmap)
assert not data.isNull()
def test_sanity(qpixmap, tmp_path):
for mode in ("1", "RGB", "RGBA", "L", "P"):
data = ImageQt.toqpixmap(hopper(mode))
# Test saving the file
tempfile = self.tempfile("temp_{}.png".format(mode))
data.save(tempfile)
assert isinstance(data, QPixmap)
assert not data.isNull()
# Test saving the file
tempfile = str(tmp_path / "temp_{}.png".format(mode))
data.save(tempfile)