Skip to content
Permalink
Browse files

[labeling] Refactor unit tests so all render check tests can be inher…

…ited by all output test classes (canvas, composer, server)

- Add openInBrowserTab function to utilities
  • Loading branch information
dakcarto committed Aug 17, 2013
1 parent 4408a8b commit 6a9280735638c1050b1920a15c3760ecfd9a08cd
@@ -24,7 +24,6 @@
import datetime
import glob
import StringIO
import subprocess
import tempfile
from PyQt4.QtCore import *
from PyQt4.QtGui import *
@@ -47,7 +46,8 @@
TestCase,
unittest,
expectedFailure,
unitTestDataPath
unitTestDataPath,
openInBrowserTab
)

QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()
@@ -140,13 +140,20 @@ def loadFeatureLayer(cls, table):
cls._MapRegistry.addMapLayer(vlayer)
cls._MapRenderer.setLayerSet([vlayer.id()])

# zoom to area of interest, which matches output aspect ratio
uri.setDataSource('', 'aoi', 'geometry')
aoilayer = QgsVectorLayer(uri.uri(), table, 'spatialite')
cls._MapRenderer.setExtent(aoilayer.extent())
# zoom to aoi
cls._MapRenderer.setExtent(cls.aoiExtent())
cls._Canvas.zoomToFullExtent()
return vlayer

@classmethod
def aoiExtent(cls):
"""Area of interest extent, which matches output aspect ratio"""
uri = QgsDataSourceURI()
uri.setDatabase(cls._PalFeaturesDb)
uri.setDataSource('', 'aoi', 'geometry')
aoilayer = QgsVectorLayer(uri.uri(), 'aoi', 'spatialite')
return aoilayer.extent()

def configTest(self, prefix, abbr):
"""Call in setUp() function of test subclass"""
self._TestGroupPrefix = prefix
@@ -219,7 +226,7 @@ def renderCheck(self, mismatch=0):
return res, msg


class TestQgsPalLabelingBase(TestQgsPalLabeling):
class TestPALConfig(TestQgsPalLabeling):

@classmethod
def setUpClass(cls):
@@ -300,28 +307,18 @@ def runSuite(module, tests):
report += '</body></html>'

tmp = tempfile.NamedTemporaryFile(suffix=".html", delete=False)
palreport = tmp.name
tmp.write(report)
tmp.close()

if sys.platform[:3] in ('win', 'dar'):
import webbrowser
webbrowser.open_new_tab("file://{0}".format(palreport))
else:
# some Linux OS pause execution on webbrowser open, so background it
cmd = 'import webbrowser;' \
'webbrowser.open_new_tab("file://{0}")'.format(palreport)
p = subprocess.Popen([sys.executable, "-c", cmd],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT).pid
openInBrowserTab('file://' + tmp.name)

return res


if __name__ == '__main__':
# NOTE: unless PAL_SUITE env var is set
# all test class methods will be run
b = 'TestQgsPalLabelingBase.'
tests = [b + 'test_write_read_settings']
res = runSuite(sys.modules[__name__], tests)
# NOTE: unless PAL_SUITE env var is set all test class methods will be run
# ex: 'TestGroup(Point|Line|Curved|Polygon|Feature).test_method'
suite = [
'TestPALConfig.test_write_read_settings'
]
res = runSuite(sys.modules[__name__], suite)
sys.exit(not res.wasSuccessful())
@@ -33,11 +33,10 @@
)

from test_qgspallabeling_base import TestQgsPalLabeling, runSuite
from test_qgspallabeling_tests import TestPointBase


class TestQgsPalLabelingPoint(TestQgsPalLabeling):
"""Most layer-level labeling properties shared across feature types are
tested in this class"""
class TestCanvasPoint(TestQgsPalLabeling, TestPointBase):

@classmethod
def setUpClass(cls):
@@ -53,57 +52,17 @@ def tearDown(self):
"""Run after each test."""
pass

def test_default_label(self):
# Verify basic default label placement and text size in points
def checkTest(self):
self.lyr.writeToLayer(self.layer)
self.saveContolImage()
self.assertTrue(*self.renderCheck())

def test_text_size_map_unit(self):
# Verify label text size in map units
self.lyr.fontSizeInMapUnits = True
tmpFont = QFont(self._TestFont)
tmpFont.setPointSizeF(0.25)
self.lyr.textFont = tmpFont
self.lyr.writeToLayer(self.layer)
self.saveContolImage()
self.assertTrue(*self.renderCheck())

def test_text_color(self):
# Verify label color change
self.lyr.textColor = Qt.blue
self.lyr.writeToLayer(self.layer)
self.saveContolImage()
self.assertTrue(*self.renderCheck())


# class TestQgsPalLabelingLine(TestQgsPalLabeling):
# """Layer-level property tests for line features"""
#
# @classmethod
# def setUpClass(cls):
# TestQgsPalLabeling.setUpClass()
# cls.layer = TestQgsPalLabeling.loadFeatureLayer('line')
#
# def setUp(self):
# """Run before each test."""
# self.configTest('pal_canvas', 'sl')
# self.lyr = self.defaultSettings()
#
# def tearDown(self):
# """Run after each test."""
# pass


if __name__ == '__main__':
# NOTE: unless PAL_SUITE env var is set
# all test class methods will be run
sp = 'TestQgsPalLabelingPoint.'
sl = 'TestQgsPalLabelingLine.'
sc = 'TestQgsPalLabelingCurved.'
sg = 'TestQgsPalLabelingPolygon.'
mf = 'TestQgsPalLabelingMultiFeature.'

tests = [sp + 'test_default_label']
res = runSuite(sys.modules[__name__], tests)
# NOTE: unless PAL_SUITE env var is set all test class methods will be run
# ex: 'TestGroup(Point|Line|Curved|Polygon|Feature).test_method'
suite = [
'TestCanvasPoint.test_text_size_map_unit'
]
res = runSuite(sys.modules[__name__], suite)
sys.exit(not res.wasSuccessful())
@@ -0,0 +1,57 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsPalLabeling: base suite of render check tests
Class is meant to be inherited by classes that test different labeling outputs
.. 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__ = 'Larry Shaffer'
__date__ = '07/16/2013'
__copyright__ = 'Copyright 2013, The QGIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import os
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

from qgis.core import (
QgsPalLayerSettings,
)


class TestPointBase(object):

def __init__(self):
"""Dummy assignments, intended to be overriden in subclasses"""
self.lyr = QgsPalLayerSettings()
self._TestFont = QFont()

def checkTest(self):
"""Intended to be overriden in subclasses"""
pass

def test_default_label(self):
# Default label placement, with text size in points
self.checkTest()

def test_text_size_map_unit(self):
# Label text size in map units
self.lyr.fontSizeInMapUnits = True
tmpFont = QFont(self._TestFont)
tmpFont.setPointSizeF(0.25)
self.lyr.textFont = tmpFont
self.checkTest()

def test_text_color(self):
# Label color change
self.lyr.textColor = Qt.blue
self.checkTest()


if __name__ == '__main__':
pass
@@ -24,6 +24,9 @@
import re
from itertools import izip

import webbrowser
import subprocess

# Support python < 2.7 via unittest2 needed for expected failure decorator.
# Note that you should ignore unused import warnings here as these are imported
# from this module by other tests.
@@ -163,6 +166,7 @@ def setCanvasCrs(theEpsgId, theOtfpFlag=False):
# Reproject all layers to WGS84 geographic CRS
CANVAS.mapRenderer().setDestinationCrs(myCrs)


def writeShape(theMemoryLayer, theFileName):
myFileName = os.path.join(str(QtCore.QDir.tempPath()), theFileName)
print myFileName
@@ -187,6 +191,7 @@ def writeShape(theMemoryLayer, theFileName):
mySkipAttributesFlag)
assert myResult == QgsVectorFileWriter.NoError


def compareWkt(a, b, tol=0.000001):
r = re.compile( "-?\d+(?:\.\d+)?(?:[eE]\d+)?" )

@@ -207,3 +212,15 @@ def compareWkt(a, b, tol=0.000001):
return False

return True


def openInBrowserTab(url):
if sys.platform[:3] in ('win', 'dar'):
webbrowser.open_new_tab(url)
else:
# some Linux OS pause execution on webbrowser open, so background it
cmd = 'import webbrowser;' \
'webbrowser.open_new_tab({0})'.format(url)
p = subprocess.Popen([sys.executable, "-c", cmd],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT).pid

0 comments on commit 6a92807

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