Skip to content
Permalink
Browse files
Add unit tests
  • Loading branch information
nyalldawson committed Sep 6, 2021
1 parent 40bef07 commit 88158d80b9797ceca9bd3c736e7ded0f539fce08
Showing with 72 additions and 1 deletion.
  1. +72 −1 tests/src/python/test_qgsmapcanvas.py
@@ -27,7 +27,11 @@
QgsLineString,
QgsPoint,
QgsPointXY,
QgsApplication)
QgsApplication,
QgsAnnotationLayer,
QgsAnnotationLineItem,
QgsAnnotationMarkerItem
)
from qgis.gui import (QgsMapCanvas)

from qgis.PyQt.QtCore import (Qt,
@@ -565,6 +569,73 @@ def testLockedScale(self):
self.assertEqual(c2.center().y(), 46.25)
self.assertAlmostEqual(c2.magnificationFactor(), 4 / dpr, 0)

def test_rendered_items(self):
canvas = QgsMapCanvas()
canvas.setDestinationCrs(QgsCoordinateReferenceSystem(4326))
canvas.setFrameStyle(0)
canvas.resize(600, 400)
canvas.setCachingEnabled(True)
self.assertEqual(canvas.width(), 600)
self.assertEqual(canvas.height(), 400)

layer = QgsAnnotationLayer('test', QgsAnnotationLayer.LayerOptions(QgsProject.instance().transformContext()))
self.assertTrue(layer.isValid())
layer2 = QgsAnnotationLayer('test', QgsAnnotationLayer.LayerOptions(QgsProject.instance().transformContext()))
self.assertTrue(layer2.isValid())

item = QgsAnnotationPolygonItem(
QgsPolygon(QgsLineString([QgsPoint(11.5, 13), QgsPoint(12, 13), QgsPoint(12, 13.5), QgsPoint(11.5, 13)])))
item.setSymbol(
QgsFillSymbol.createSimple({'color': '200,100,100', 'outline_color': 'black', 'outline_width': '2'}))
item.setZIndex(1)
i1_id = layer.addItem(item)

item = QgsAnnotationLineItem(QgsLineString([QgsPoint(11, 13), QgsPoint(12, 13), QgsPoint(12, 15)]))
item.setZIndex(2)
i2_id = layer.addItem(item)

item = QgsAnnotationMarkerItem(QgsPoint(12, 13))
item.setZIndex(3)
i3_id = layer2.addItem(item)

layer.setCrs(QgsCoordinateReferenceSystem('EPSG:4326'))
layer2.setCrs(QgsCoordinateReferenceSystem('EPSG:4326'))

canvas.setLayers([layer, layer2])
canvas.setExtent(QgsRectangle(10, 10, 18, 18))
canvas.show()

# need to wait until first redraw can occur (note that we first need to wait till drawing starts!)
while not canvas.isDrawing():
app.processEvents()
canvas.waitWhileRendering()

results = canvas.renderedItemResults()
self.assertCountEqual([i.itemId() for i in results.renderedItems()], [i1_id, i2_id, i3_id])

# turn off a layer -- the other layer will be rendered direct from the cached version
canvas.setLayers([layer2])
while not canvas.isDrawing():
app.processEvents()
canvas.waitWhileRendering()

results = canvas.renderedItemResults()
# only layer2 items should be present in results -- but these MUST be present while layer2 is visible in the canvas,
# even though the most recent canvas redraw used a cached version of layer2 and didn't actually have to redraw the layer
self.assertEqual([i.itemId() for i in results.renderedItems()], [i3_id])

# turn layer 1 back on
canvas.setLayers([layer, layer2])
while not canvas.isDrawing():
app.processEvents()
canvas.waitWhileRendering()

results = canvas.renderedItemResults()
# both layer1 and layer2 items should be present in results -- even though NEITHER of these layers were re-rendered,
# and instead we used precached renders of both layers
self.assertCountEqual([i.itemId() for i in results.renderedItems()], [i1_id, i2_id, i3_id])



if __name__ == '__main__':
unittest.main()

0 comments on commit 88158d8

Please sign in to comment.