Skip to content
Permalink
Browse files

Unit tests for prefix/suffix

  • Loading branch information
nyalldawson committed Dec 18, 2020
1 parent c8b71ef commit 55dbd9c4746375891a907631378ebc9862ec46de
@@ -69,6 +69,10 @@ def test_settings(self):
self.assertEqual(settings.minimumLabel(), 'min')
settings.setMaximumLabel('max')
self.assertEqual(settings.maximumLabel(), 'max')
settings.setPrefix('pref')
self.assertEqual(settings.prefix(), 'pref')
settings.setSuffix('suff')
self.assertEqual(settings.suffix(), 'suff')

self.assertIsNotNone(settings.numericFormat())
settings.setNumericFormat(QgsBearingNumericFormat())
@@ -79,6 +83,8 @@ def test_settings(self):
self.assertEqual(settings2.minimumLabel(), 'min')
self.assertEqual(settings2.maximumLabel(), 'max')
self.assertIsInstance(settings2.numericFormat(), QgsBearingNumericFormat)
self.assertEqual(settings2.prefix(), 'pref')
self.assertEqual(settings2.suffix(), 'suff')

doc = QDomDocument("testdoc")
elem = doc.createElement('test')
@@ -90,6 +96,8 @@ def test_settings(self):
self.assertEqual(settings3.minimumLabel(), 'min')
self.assertEqual(settings3.maximumLabel(), 'max')
self.assertIsInstance(settings3.numericFormat(), QgsBearingNumericFormat)
self.assertEqual(settings3.prefix(), 'pref')
self.assertEqual(settings3.suffix(), 'suff')

def test_basic(self):
r = QgsGradientColorRamp(QColor(200, 0, 0, 100), QColor(0, 200, 0, 200))
@@ -154,6 +162,29 @@ def test_icon_with_settings(self):

self.assertTrue(self.imageCheck('color_ramp_legend_node_settings_icon', 'color_ramp_legend_node_settings_icon', im, 10))

def test_icon_prefix_suffix(self):
r = QgsGradientColorRamp(QColor(200, 0, 0, 100), QColor(0, 200, 0, 200))

# need a layer in order to make legend nodes
layer = QgsVectorLayer('dummy', 'test', 'memory')
layer_tree_layer = QgsLayerTreeLayer(layer)

settings = QgsColorRampLegendNodeSettings()
settings.setPrefix('pref ')
settings.setSuffix(' suff')

node = TestColorRampLegend(layer_tree_layer, r, settings, 5, 10)

pixmap = node.data(Qt.DecorationRole)

im = QImage(pixmap.size(), QImage.Format_ARGB32)
im.fill(QColor(255, 255, 255))
p = QPainter(im)
p.drawPixmap(0, 0, pixmap)
p.end()

self.assertTrue(self.imageCheck('color_ramp_legend_node_prefix_suffix_icon', 'color_ramp_legend_node_prefix_suffix_icon', im, 10))

def test_draw(self):
r = QgsGradientColorRamp(QColor(200, 0, 0, 100), QColor(0, 200, 0, 200))

@@ -247,6 +278,52 @@ def test_draw_settings(self):

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

def test_draw_prefix_suffix(self):
r = QgsGradientColorRamp(QColor(200, 0, 0, 100), QColor(0, 200, 0, 200))

# need a layer in order to make legend nodes
layer = QgsVectorLayer('dummy', 'test', 'memory')
layer_tree_layer = QgsLayerTreeLayer(layer)

settings = QgsColorRampLegendNodeSettings()
settings.setPrefix('pref ')
settings.setSuffix(' suff')
node = QgsColorRampLegendNode(layer_tree_layer, r, settings, 5, 10)

ls = QgsLegendSettings()
item_style = ls.style(QgsLegendStyle.SymbolLabel)
item_style.setFont(QgsFontUtils.getStandardTestFont('Bold', 18))
ls.setStyle(QgsLegendStyle.SymbolLabel, item_style)

item_context = QgsLayerTreeModelLegendNode.ItemContext()

image = QImage(400, 250, QImage.Format_ARGB32)
image.fill(QColor(255, 255, 255))

p = QPainter(image)

ms = QgsMapSettings()
ms.setExtent(QgsRectangle(1, 10, 1, 10))
ms.setOutputSize(image.size())
context = QgsRenderContext.fromMapSettings(ms)
context.setPainter(p)
context.setScaleFactor(150 / 25.4) # 150 DPI

p.scale(context.scaleFactor(), context.scaleFactor())

item_context.context = context
item_context.painter = p
item_context.top = 1
item_context.columnLeft = 3
item_context.columnRight = 30
item_context.patchSize = QSizeF(12, 40)

symbol_size = node.drawSymbol(ls, item_context, 0)
node.drawSymbolText(ls, item_context, symbol_size)
p.end()

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

def imageCheck(self, name, reference_image, image, size_tolerance=0):
TestQgsColorRampLegendNode.report += "<h2>Render {}</h2>\n".format(name)
temp_dir = QDir.tempPath() + '/'
Binary file not shown.
Binary file not shown.

0 comments on commit 55dbd9c

Please sign in to comment.
You can’t perform that action at this time.