Pillow/Tests/test_imageqt.py
Jon Dufresne d50445ff30 Introduce isort to automate import ordering and formatting
Similar to the recent adoption of Black. isort is a Python utility to
sort imports alphabetically and automatically separate into sections. By
using isort, contributors can quickly and automatically conform to the
projects style without thinking. Just let the tool do it.

Uses the configuration recommended by the Black to avoid conflicts of
style.

Rewrite TestImageQt.test_deprecated to no rely on import order.
2019-07-06 16:11:35 -07:00

96 lines
2.7 KiB
Python

import sys
import warnings
from PIL import ImageQt
from .helper import PillowTestCase, hopper
if sys.version_info.major >= 3:
from importlib import reload
if ImageQt.qt_is_installed:
from PIL.ImageQt import qRgba
def skip_if_qt_is_not_installed(_):
pass
else:
def skip_if_qt_is_not_installed(test_case):
test_case.skipTest("Qt bindings are not installed")
class PillowQtTestCase(object):
def setUp(self):
skip_if_qt_is_not_installed(self)
def tearDown(self):
pass
class PillowQPixmapTestCase(PillowQtTestCase):
def setUp(self):
PillowQtTestCase.setUp(self)
try:
if ImageQt.qt_version == "5":
from PyQt5.QtGui import QGuiApplication
elif ImageQt.qt_version == "4":
from PyQt4.QtGui import QGuiApplication
elif ImageQt.qt_version == "side":
from PySide.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):
PillowQtTestCase.tearDown(self)
self.app.quit()
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 == "4":
from PyQt4.QtGui import qRgb
elif ImageQt.qt_version == "side":
from PySide.QtGui import qRgb
elif ImageQt.qt_version == "side2":
from PySide2.QtGui import qRgb
self.assertEqual(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
self.assertEqual(val >> 16, r)
self.assertEqual(((val >> 8) % 2 ** 8), g)
self.assertEqual(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_deprecated(self):
with warnings.catch_warnings(record=True) as w:
reload(ImageQt)
if ImageQt.qt_version in ["4", "side"]:
self.assertEqual(len(w), 1)
self.assertTrue(issubclass(w[-1].category, DeprecationWarning))
else:
# No warning.
self.assertEqual(w, [])