Skip to content

Commit

Permalink
Split layout map overview tests into their own file, add masks
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 2, 2019
1 parent 9d800d4 commit 0f7d8c0
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 83 deletions.
1 change: 1 addition & 0 deletions tests/src/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ ADD_PYTHON_TEST(PyQgsLayoutLabel test_qgslayoutlabel.py)
ADD_PYTHON_TEST(PyQgsLayoutLegend test_qgslayoutlegend.py)
ADD_PYTHON_TEST(PyQgsLayoutMap test_qgslayoutmap.py)
ADD_PYTHON_TEST(PyQgsLayoutMapGrid test_qgslayoutmapgrid.py)
ADD_PYTHON_TEST(PyQgsLayoutMapOverview test_qgslayoutmapoverview.py)
ADD_PYTHON_TEST(PyQgsLayoutPage test_qgslayoutpage.py)
ADD_PYTHON_TEST(PyQgsLayoutPicture test_qgslayoutpicture.py)
ADD_PYTHON_TEST(PyQgsLayoutPolygon test_qgslayoutpolygon.py)
Expand Down
84 changes: 1 addition & 83 deletions tests/src/python/test_qgslayoutmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@
QgsUnitTypes,
QgsLayoutObject,
QgsProperty,
QgsReadWriteContext,
QgsPrintLayout)
QgsReadWriteContext)

from qgis.testing import start_app, unittest
from utilities import unitTestDataPath
Expand Down Expand Up @@ -97,87 +96,6 @@ def __init__(self, methodName):
self.map.setLayers([self.raster_layer])
self.layout.addLayoutItem(self.map)

def testOverviewMap(self):
overviewMap = QgsLayoutItemMap(self.layout)
overviewMap.attemptSetSceneRect(QRectF(20, 130, 70, 70))
overviewMap.setFrameEnabled(True)
overviewMap.setLayers([self.raster_layer])
self.layout.addLayoutItem(overviewMap)
# zoom in
myRectangle = QgsRectangle(96, -152, 160, -120)
self.map.setExtent(myRectangle)
myRectangle2 = QgsRectangle(0, -256, 256, 0)
overviewMap.setExtent(myRectangle2)
overviewMap.overview().setLinkedMap(self.map)
checker = QgsLayoutChecker('composermap_overview', self.layout)
checker.setColorTolerance(6)
checker.setControlPathPrefix("composer_mapoverview")
myTestResult, myMessage = checker.testLayout()
self.report += checker.report()
self.layout.removeLayoutItem(overviewMap)
assert myTestResult, myMessage

def testOverviewMapBlend(self):
overviewMap = QgsLayoutItemMap(self.layout)
overviewMap.attemptSetSceneRect(QRectF(20, 130, 70, 70))
overviewMap.setFrameEnabled(True)
overviewMap.setLayers([self.raster_layer])
self.layout.addLayoutItem(overviewMap)
# zoom in
myRectangle = QgsRectangle(96, -152, 160, -120)
self.map.setExtent(myRectangle)
myRectangle2 = QgsRectangle(0, -256, 256, 0)
overviewMap.setExtent(myRectangle2)
overviewMap.overview().setLinkedMap(self.map)
overviewMap.overview().setBlendMode(QPainter.CompositionMode_Multiply)
checker = QgsLayoutChecker('composermap_overview_blending', self.layout)
checker.setControlPathPrefix("composer_mapoverview")
myTestResult, myMessage = checker.testLayout()
self.report += checker.report()
self.layout.removeLayoutItem(overviewMap)
assert myTestResult, myMessage

def testOverviewMapInvert(self):
overviewMap = QgsLayoutItemMap(self.layout)
overviewMap.attemptSetSceneRect(QRectF(20, 130, 70, 70))
overviewMap.setFrameEnabled(True)
overviewMap.setLayers([self.raster_layer])
self.layout.addLayoutItem(overviewMap)
# zoom in
myRectangle = QgsRectangle(96, -152, 160, -120)
self.map.setExtent(myRectangle)
myRectangle2 = QgsRectangle(0, -256, 256, 0)
overviewMap.setExtent(myRectangle2)
overviewMap.overview().setLinkedMap(self.map)
overviewMap.overview().setInverted(True)
checker = QgsLayoutChecker('composermap_overview_invert', self.layout)
checker.setControlPathPrefix("composer_mapoverview")
myTestResult, myMessage = checker.testLayout()
self.report += checker.report()
self.layout.removeLayoutItem(overviewMap)
assert myTestResult, myMessage

def testOverviewMapCenter(self):
overviewMap = QgsLayoutItemMap(self.layout)
overviewMap.attemptSetSceneRect(QRectF(20, 130, 70, 70))
overviewMap.setFrameEnabled(True)
overviewMap.setLayers([self.raster_layer])
self.layout.addLayoutItem(overviewMap)
# zoom in
myRectangle = QgsRectangle(192, -288, 320, -224)
self.map.setExtent(myRectangle)
myRectangle2 = QgsRectangle(0, -256, 256, 0)
overviewMap.setExtent(myRectangle2)
overviewMap.overview().setLinkedMap(self.map)
overviewMap.overview().setInverted(False)
overviewMap.overview().setCentered(True)
checker = QgsLayoutChecker('composermap_overview_center', self.layout)
checker.setControlPathPrefix("composer_mapoverview")
myTestResult, myMessage = checker.testLayout()
self.report += checker.report()
self.layout.removeLayoutItem(overviewMap)
assert myTestResult, myMessage

def testMapCrs(self):
# create layout with layout map
map_settings = QgsMapSettings()
Expand Down
163 changes: 163 additions & 0 deletions tests/src/python/test_qgslayoutmapoverview.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsLayoutItemMap.
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"""
__author__ = '(C) 2017 Nyall Dawson'
__date__ = '20/10/2017'
__copyright__ = 'Copyright 2017, The QGIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import qgis # NOQA

import os

from qgis.PyQt.QtCore import QFileInfo, QRectF, QDir
from qgis.PyQt.QtGui import QPainter

from qgis.core import (QgsLayoutItemMap,
QgsRectangle,
QgsRasterLayer,
QgsVectorLayer,
QgsLayout,
QgsProject,
QgsMultiBandColorRenderer)

from qgis.testing import start_app, unittest
from utilities import unitTestDataPath
from qgslayoutchecker import QgsLayoutChecker
from test_qgslayoutitem import LayoutItemTestCase

start_app()
TEST_DATA_DIR = unitTestDataPath()


class TestQgsLayoutMap(unittest.TestCase, LayoutItemTestCase):

@classmethod
def setUpClass(cls):
cls.item_class = QgsLayoutItemMap

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

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

def __init__(self, methodName):
"""Run once on class initialization."""
unittest.TestCase.__init__(self, methodName)
myPath = os.path.join(TEST_DATA_DIR, 'rgb256x256.png')
rasterFileInfo = QFileInfo(myPath)
self.raster_layer = QgsRasterLayer(rasterFileInfo.filePath(),
rasterFileInfo.completeBaseName())
rasterRenderer = QgsMultiBandColorRenderer(
self.raster_layer.dataProvider(), 1, 2, 3)
self.raster_layer.setRenderer(rasterRenderer)

myPath = os.path.join(TEST_DATA_DIR, 'points.shp')
vector_file_info = QFileInfo(myPath)
self.vector_layer = QgsVectorLayer(vector_file_info.filePath(),
vector_file_info.completeBaseName(), 'ogr')
assert self.vector_layer.isValid()
QgsProject.instance().addMapLayers([self.raster_layer, self.vector_layer])

# create layout with layout map
self.layout = QgsLayout(QgsProject.instance())
self.layout.initializeDefaults()
self.map = QgsLayoutItemMap(self.layout)
self.map.attemptSetSceneRect(QRectF(20, 20, 200, 100))
self.map.setFrameEnabled(True)
self.map.setLayers([self.raster_layer])
self.layout.addLayoutItem(self.map)

def testOverviewMap(self):
overviewMap = QgsLayoutItemMap(self.layout)
overviewMap.attemptSetSceneRect(QRectF(20, 130, 70, 70))
overviewMap.setFrameEnabled(True)
overviewMap.setLayers([self.raster_layer])
self.layout.addLayoutItem(overviewMap)
# zoom in
myRectangle = QgsRectangle(96, -152, 160, -120)
self.map.setExtent(myRectangle)
myRectangle2 = QgsRectangle(0, -256, 256, 0)
overviewMap.setExtent(myRectangle2)
overviewMap.overview().setLinkedMap(self.map)
checker = QgsLayoutChecker('composermap_overview', self.layout)
checker.setColorTolerance(6)
checker.setControlPathPrefix("composer_mapoverview")
myTestResult, myMessage = checker.testLayout()
self.report += checker.report()
self.layout.removeLayoutItem(overviewMap)
assert myTestResult, myMessage

def testOverviewMapBlend(self):
overviewMap = QgsLayoutItemMap(self.layout)
overviewMap.attemptSetSceneRect(QRectF(20, 130, 70, 70))
overviewMap.setFrameEnabled(True)
overviewMap.setLayers([self.raster_layer])
self.layout.addLayoutItem(overviewMap)
# zoom in
myRectangle = QgsRectangle(96, -152, 160, -120)
self.map.setExtent(myRectangle)
myRectangle2 = QgsRectangle(0, -256, 256, 0)
overviewMap.setExtent(myRectangle2)
overviewMap.overview().setLinkedMap(self.map)
overviewMap.overview().setBlendMode(QPainter.CompositionMode_Multiply)
checker = QgsLayoutChecker('composermap_overview_blending', self.layout)
checker.setControlPathPrefix("composer_mapoverview")
myTestResult, myMessage = checker.testLayout()
self.report += checker.report()
self.layout.removeLayoutItem(overviewMap)
assert myTestResult, myMessage

def testOverviewMapInvert(self):
overviewMap = QgsLayoutItemMap(self.layout)
overviewMap.attemptSetSceneRect(QRectF(20, 130, 70, 70))
overviewMap.setFrameEnabled(True)
overviewMap.setLayers([self.raster_layer])
self.layout.addLayoutItem(overviewMap)
# zoom in
myRectangle = QgsRectangle(96, -152, 160, -120)
self.map.setExtent(myRectangle)
myRectangle2 = QgsRectangle(0, -256, 256, 0)
overviewMap.setExtent(myRectangle2)
overviewMap.overview().setLinkedMap(self.map)
overviewMap.overview().setInverted(True)
checker = QgsLayoutChecker('composermap_overview_invert', self.layout)
checker.setControlPathPrefix("composer_mapoverview")
myTestResult, myMessage = checker.testLayout()
self.report += checker.report()
self.layout.removeLayoutItem(overviewMap)
assert myTestResult, myMessage

def testOverviewMapCenter(self):
overviewMap = QgsLayoutItemMap(self.layout)
overviewMap.attemptSetSceneRect(QRectF(20, 130, 70, 70))
overviewMap.setFrameEnabled(True)
overviewMap.setLayers([self.raster_layer])
self.layout.addLayoutItem(overviewMap)
# zoom in
myRectangle = QgsRectangle(192, -288, 320, -224)
self.map.setExtent(myRectangle)
myRectangle2 = QgsRectangle(0, -256, 256, 0)
overviewMap.setExtent(myRectangle2)
overviewMap.overview().setLinkedMap(self.map)
overviewMap.overview().setInverted(False)
overviewMap.overview().setCentered(True)
checker = QgsLayoutChecker('composermap_overview_center', self.layout)
checker.setControlPathPrefix("composer_mapoverview")
myTestResult, myMessage = checker.testLayout()
self.report += checker.report()
self.layout.removeLayoutItem(overviewMap)
assert myTestResult, myMessage


if __name__ == '__main__':
unittest.main()
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0f7d8c0

Please sign in to comment.