Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand unit test covering of base layout item blend mode handling #54843

Merged
merged 6 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions python/testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ def image_check(cls,
image: QImage,
control_name=None,
color_tolerance: int = 2,
allowed_mismatch: int = 20) -> bool:
allowed_mismatch: int = 20,
size_tolerance: Optional[int] = None,
expect_fail: bool = False) -> bool:
temp_dir = QDir.tempPath() + '/'
file_name = temp_dir + name + ".png"
image.save(file_name, "PNG")
Expand All @@ -125,8 +127,13 @@ def image_check(cls,
checker.setControlName(control_name or "expected_" + reference_image)
checker.setRenderedImage(file_name)
checker.setColorTolerance(color_tolerance)
checker.setExpectFail(expect_fail)
if size_tolerance is not None:
checker.setSizeTolerance(size_tolerance, size_tolerance)

result = checker.runTest(name, allowed_mismatch)
if not result:
if (not expect_fail and not result) or \
(expect_fail and result):
cls.report += f"<h2>Render {name}</h2>\n"
cls.report += checker.report()

Expand Down
5 changes: 3 additions & 2 deletions src/core/layout/qgslayoutitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,11 @@ QgsLayoutItem::QgsLayoutItem( QgsLayout *layout, bool manageZValue )
mEffect = new QgsLayoutEffect();
if ( mLayout )
{
mEffect->setEnabled( mLayout->renderContext().flags() & QgsLayoutRenderContext::FlagUseAdvancedEffects );
mEffect->setEnabled( ( mLayout->renderContext().flags() & QgsLayoutRenderContext::FlagUseAdvancedEffects )
&& !( mLayout->renderContext().flags() & QgsLayoutRenderContext::FlagForceVectorOutput ) );
connect( &mLayout->renderContext(), &QgsLayoutRenderContext::flagsChanged, this, [ = ]( QgsLayoutRenderContext::Flags flags )
{
mEffect->setEnabled( flags & QgsLayoutRenderContext::FlagUseAdvancedEffects );
mEffect->setEnabled( ( flags & QgsLayoutRenderContext::FlagUseAdvancedEffects ) && !( flags & QgsLayoutRenderContext::FlagForceVectorOutput ) );
} );
}
setGraphicsEffect( mEffect.data() );
Expand Down
1 change: 1 addition & 0 deletions src/core/qgsmultirenderchecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ bool QgsMultiRenderChecker::runTest( const QString &testName, unsigned int misma
checker.setControlPathSuffix( suffix );
checker.setControlName( mControlName );
checker.setMapSettings( mMapSettings );
checker.setExpectFail( mExpectFail );

if ( !mRenderedImage.isNull() )
{
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsrenderchecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void QgsRenderChecker::setControlImagePath( const QString &path )

QString QgsRenderChecker::report( bool ignoreSuccess ) const
{
return ( ignoreSuccess && mResult ) ? QString() : mReport;
return ( ( ignoreSuccess && mResult ) || ( mExpectFail && !mResult ) ) ? QString() : mReport;
}

void QgsRenderChecker::setControlName( const QString &name )
Expand Down
8 changes: 0 additions & 8 deletions tests/src/python/test_qgsannotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,6 @@ class TestQgsAnnotation(QgisTestCase):
def control_path_prefix(cls):
return "annotations"

def setUp(self):
self.report = "<h1>Python QgsAnnotation Tests</h1>\n"

def tearDown(self):
report_file_path = f"{QDir.tempPath()}/qgistest.html"
with open(report_file_path, 'a') as report_file:
report_file.write(self.report)

def testTextAnnotation(self):
""" test rendering a text annotation"""
a = QgsTextAnnotation()
Expand Down
35 changes: 6 additions & 29 deletions tests/src/python/test_qgsannotationlayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
__copyright__ = 'Copyright 2020, The QGIS Project'

import qgis # NOQA
from qgis.PyQt.QtCore import QDir, QSize, QTemporaryDir
from qgis.PyQt.QtCore import QSize, QTemporaryDir
from qgis.PyQt.QtGui import QColor, QImage, QPainter
from qgis.PyQt.QtXml import QDomDocument
from qgis.core import (
Expand All @@ -38,7 +38,6 @@
QgsProject,
QgsReadWriteContext,
QgsRectangle,
QgsRenderChecker,
QgsRenderContext,
QgsVertexId,
)
Expand All @@ -54,16 +53,8 @@
class TestQgsAnnotationLayer(QgisTestCase):

@classmethod
def setUpClass(cls):
super().setUpClass()
cls.report = "<h1>Python QgsAnnotationLayer Tests</h1>\n"

@classmethod
def tearDownClass(cls):
report_file_path = f"{QDir.tempPath()}/qgistest.html"
with open(report_file_path, 'a') as report_file:
report_file.write(cls.report)
super().tearDownClass()
def control_path_prefix(cls):
return 'annotation_layer'

def testItems(self):
layer = QgsAnnotationLayer('test', QgsAnnotationLayer.LayerOptions(QgsProject.instance().transformContext()))
Expand Down Expand Up @@ -357,7 +348,7 @@ def testRenderLayer(self):
finally:
painter.end()

self.assertTrue(self.imageCheck('layer_render', 'layer_render', image))
self.assertTrue(self.image_check('layer_render', 'layer_render', image))

# also check details of rendered items
item_details = renderer.takeRenderedItemDetails()
Expand Down Expand Up @@ -417,7 +408,7 @@ def testRenderWithTransform(self):
finally:
painter.end()

self.assertTrue(self.imageCheck('layer_render_transform', 'layer_render_transform', image))
self.assertTrue(self.image_check('layer_render_transform', 'layer_render_transform', image))

# also check details of rendered items
item_details = renderer.takeRenderedItemDetails()
Expand Down Expand Up @@ -482,7 +473,7 @@ def testRenderLayerWithReferenceScale(self):
finally:
painter.end()

self.assertTrue(self.imageCheck('layer_render_reference_scale', 'layer_render_reference_scale', image))
self.assertTrue(self.image_check('layer_render_reference_scale', 'layer_render_reference_scale', image))

# also check details of rendered items
item_details = renderer.takeRenderedItemDetails()
Expand Down Expand Up @@ -607,20 +598,6 @@ def test_render_via_job_with_transform(self):
self.assertTrue(compareWkt(result, expected, tol=1000), "mismatch Expected:\n{}\nGot:\n{}\n".format(expected,
result))

def imageCheck(self, name, reference_image, image):
TestQgsAnnotationLayer.report += f"<h2>Render {name}</h2>\n"
temp_dir = QDir.tempPath() + '/'
file_name = temp_dir + 'patch_' + name + ".png"
image.save(file_name, "PNG")
checker = QgsRenderChecker()
checker.setControlPathPrefix("annotation_layer")
checker.setControlName("expected_" + reference_image)
checker.setRenderedImage(file_name)
checker.setColorTolerance(2)
result = checker.compareImages(name, 20)
TestQgsAnnotationLayer.report += checker.report()
return result


if __name__ == '__main__':
unittest.main()
35 changes: 6 additions & 29 deletions tests/src/python/test_qgsannotationlineitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
__copyright__ = 'Copyright 2020, The QGIS Project'

import qgis # NOQA
from qgis.PyQt.QtCore import QDir, QSize
from qgis.PyQt.QtCore import QSize
from qgis.PyQt.QtGui import QColor, QImage, QPainter
from qgis.PyQt.QtXml import QDomDocument
from qgis.core import (
Expand All @@ -34,7 +34,6 @@
QgsProject,
QgsReadWriteContext,
QgsRectangle,
QgsRenderChecker,
QgsRenderContext,
QgsVertexId,
)
Expand All @@ -50,16 +49,8 @@
class TestQgsAnnotationLineItem(QgisTestCase):

@classmethod
def setUpClass(cls):
super().setUpClass()
cls.report = "<h1>Python QgsAnnotationLineItem Tests</h1>\n"

@classmethod
def tearDownClass(cls):
report_file_path = f"{QDir.tempPath()}/qgistest.html"
with open(report_file_path, 'a') as report_file:
report_file.write(cls.report)
super().tearDownClass()
def control_path_prefix(cls):
return "annotation_layer"

def testBasic(self):
item = QgsAnnotationLineItem(QgsLineString([QgsPoint(12, 13), QgsPoint(14, 13), QgsPoint(14, 15)]))
Expand Down Expand Up @@ -194,7 +185,7 @@ def testRenderLineString(self):
finally:
painter.end()

self.assertTrue(self.imageCheck('linestring_item', 'linestring_item', image))
self.assertTrue(self.image_check('linestring_item', 'linestring_item', image))

def testRenderCurve(self):
item = QgsAnnotationLineItem(QgsCircularString(QgsPoint(12, 13.2), QgsPoint(14, 13.4), QgsPoint(14, 15)))
Expand All @@ -220,7 +211,7 @@ def testRenderCurve(self):
finally:
painter.end()

self.assertTrue(self.imageCheck('line_circularstring', 'line_circularstring', image))
self.assertTrue(self.image_check('line_circularstring', 'line_circularstring', image))

def testRenderWithTransform(self):
item = QgsAnnotationLineItem(QgsLineString([QgsPoint(11, 13), QgsPoint(12, 13), QgsPoint(12, 15)]))
Expand All @@ -247,21 +238,7 @@ def testRenderWithTransform(self):
finally:
painter.end()

self.assertTrue(self.imageCheck('linestring_item_transform', 'linestring_item_transform', image))

def imageCheck(self, name, reference_image, image):
TestQgsAnnotationLineItem.report += f"<h2>Render {name}</h2>\n"
temp_dir = QDir.tempPath() + '/'
file_name = temp_dir + 'patch_' + name + ".png"
image.save(file_name, "PNG")
checker = QgsRenderChecker()
checker.setControlPathPrefix("annotation_layer")
checker.setControlName("expected_" + reference_image)
checker.setRenderedImage(file_name)
checker.setColorTolerance(2)
result = checker.compareImages(name, 20)
TestQgsAnnotationLineItem.report += checker.report()
return result
self.assertTrue(self.image_check('linestring_item_transform', 'linestring_item_transform', image))


if __name__ == '__main__':
Expand Down
41 changes: 9 additions & 32 deletions tests/src/python/test_qgsannotationlinetextitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
__copyright__ = 'Copyright 2020, The QGIS Project'

import qgis # NOQA
from qgis.PyQt.QtCore import QDir, QSize, Qt
from qgis.PyQt.QtCore import QSize
from qgis.PyQt.QtGui import QColor, QImage, QPainter
from qgis.PyQt.QtXml import QDomDocument
from qgis.core import (
Expand All @@ -31,7 +31,6 @@
QgsProject,
QgsReadWriteContext,
QgsRectangle,
QgsMultiRenderChecker,
QgsRenderContext,
QgsTextFormat,
QgsVertexId,
Expand All @@ -50,16 +49,8 @@
class TestQgsAnnotationLineTextItem(QgisTestCase):

@classmethod
def setUpClass(cls):
super().setUpClass()
cls.report = "<h1>Python QgsAnnotationLineTextItem Tests</h1>\n"

@classmethod
def tearDownClass(cls):
report_file_path = f"{QDir.tempPath()}/qgistest.html"
with open(report_file_path, 'a') as report_file:
report_file.write(cls.report)
super().tearDownClass()
def control_path_prefix(cls):
return "annotation_layer"

def testBasic(self):
item = QgsAnnotationLineTextItem('my text', QgsLineString(((12, 13), (13, 13.1), (14, 13))))
Expand Down Expand Up @@ -242,7 +233,7 @@ def testRenderLine(self):
finally:
painter.end()

self.assertTrue(self.imageCheck('linetext_item', 'linetext_item', image))
self.assertTrue(self.image_check('linetext_item', 'linetext_item', image))

def testRenderLineOffsetPositive(self):
item = QgsAnnotationLineTextItem('my text', QgsLineString(((12, 13), (13, 13.1), (14, 12))))
Expand Down Expand Up @@ -276,7 +267,7 @@ def testRenderLineOffsetPositive(self):
finally:
painter.end()

self.assertTrue(self.imageCheck('linetext_item_offset_positive', 'linetext_item_offset_positive', image))
self.assertTrue(self.image_check('linetext_item_offset_positive', 'linetext_item_offset_positive', image))

def testRenderLineOffsetNegative(self):
item = QgsAnnotationLineTextItem('my text', QgsLineString(((12, 13), (13, 13.1), (14, 12))))
Expand Down Expand Up @@ -310,7 +301,7 @@ def testRenderLineOffsetNegative(self):
finally:
painter.end()

self.assertTrue(self.imageCheck('linetext_item_offset_negative', 'linetext_item_offset_negative', image))
self.assertTrue(self.image_check('linetext_item_offset_negative', 'linetext_item_offset_negative', image))

def testRenderLineTruncate(self):
item = QgsAnnotationLineTextItem('my text', QgsLineString(((12, 13), (13, 13.1), (14, 12))))
Expand Down Expand Up @@ -342,7 +333,7 @@ def testRenderLineTruncate(self):
finally:
painter.end()

self.assertTrue(self.imageCheck('linetext_item_truncate', 'linetext_item_truncate', image))
self.assertTrue(self.image_check('linetext_item_truncate', 'linetext_item_truncate', image))

def testRenderLineTextExpression(self):
item = QgsAnnotationLineTextItem('[% 1 + 1.5 %]', QgsLineString(((12, 13), (13, 13.1), (14, 12))))
Expand Down Expand Up @@ -374,7 +365,7 @@ def testRenderLineTextExpression(self):
finally:
painter.end()

self.assertTrue(self.imageCheck('linetext_item_expression', 'linetext_item_expression', image))
self.assertTrue(self.image_check('linetext_item_expression', 'linetext_item_expression', image))

def testRenderWithTransform(self):
item = QgsAnnotationLineTextItem('my text', QgsLineString(
Expand Down Expand Up @@ -408,21 +399,7 @@ def testRenderWithTransform(self):
finally:
painter.end()

self.assertTrue(self.imageCheck('linetext_item_transform', 'linetext_item_transform', image))

def imageCheck(self, name, reference_image, image):
TestQgsAnnotationLineTextItem.report += f"<h2>Render {name}</h2>\n"
temp_dir = QDir.tempPath() + '/'
file_name = temp_dir + 'annotation_' + name + ".png"
image.save(file_name, "PNG")
checker = QgsMultiRenderChecker()
checker.setControlPathPrefix("annotation_layer")
checker.setControlName("expected_" + reference_image)
checker.setRenderedImage(file_name)
checker.setColorTolerance(2)
result = checker.runTest(name, 20)
TestQgsAnnotationLineTextItem.report += checker.report()
return result
self.assertTrue(self.image_check('linetext_item_transform', 'linetext_item_transform', image))


if __name__ == '__main__':
Expand Down
Loading
Loading