Skip to content

Commit 6a92807

Browse files
committed
[labeling] Refactor unit tests so all render check tests can be inherited by all output test classes (canvas, composer, server)
- Add openInBrowserTab function to utilities
1 parent 4408a8b commit 6a92807

4 files changed

+104
-74
lines changed

tests/src/python/test_qgspallabeling_base.py

+21-24
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import datetime
2525
import glob
2626
import StringIO
27-
import subprocess
2827
import tempfile
2928
from PyQt4.QtCore import *
3029
from PyQt4.QtGui import *
@@ -47,7 +46,8 @@
4746
TestCase,
4847
unittest,
4948
expectedFailure,
50-
unitTestDataPath
49+
unitTestDataPath,
50+
openInBrowserTab
5151
)
5252

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

143-
# zoom to area of interest, which matches output aspect ratio
144-
uri.setDataSource('', 'aoi', 'geometry')
145-
aoilayer = QgsVectorLayer(uri.uri(), table, 'spatialite')
146-
cls._MapRenderer.setExtent(aoilayer.extent())
143+
# zoom to aoi
144+
cls._MapRenderer.setExtent(cls.aoiExtent())
147145
cls._Canvas.zoomToFullExtent()
148146
return vlayer
149147

148+
@classmethod
149+
def aoiExtent(cls):
150+
"""Area of interest extent, which matches output aspect ratio"""
151+
uri = QgsDataSourceURI()
152+
uri.setDatabase(cls._PalFeaturesDb)
153+
uri.setDataSource('', 'aoi', 'geometry')
154+
aoilayer = QgsVectorLayer(uri.uri(), 'aoi', 'spatialite')
155+
return aoilayer.extent()
156+
150157
def configTest(self, prefix, abbr):
151158
"""Call in setUp() function of test subclass"""
152159
self._TestGroupPrefix = prefix
@@ -219,7 +226,7 @@ def renderCheck(self, mismatch=0):
219226
return res, msg
220227

221228

222-
class TestQgsPalLabelingBase(TestQgsPalLabeling):
229+
class TestPALConfig(TestQgsPalLabeling):
223230

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

302309
tmp = tempfile.NamedTemporaryFile(suffix=".html", delete=False)
303-
palreport = tmp.name
304310
tmp.write(report)
305311
tmp.close()
306-
307-
if sys.platform[:3] in ('win', 'dar'):
308-
import webbrowser
309-
webbrowser.open_new_tab("file://{0}".format(palreport))
310-
else:
311-
# some Linux OS pause execution on webbrowser open, so background it
312-
cmd = 'import webbrowser;' \
313-
'webbrowser.open_new_tab("file://{0}")'.format(palreport)
314-
p = subprocess.Popen([sys.executable, "-c", cmd],
315-
stdout=subprocess.PIPE,
316-
stderr=subprocess.STDOUT).pid
312+
openInBrowserTab('file://' + tmp.name)
317313

318314
return res
319315

320316

321317
if __name__ == '__main__':
322-
# NOTE: unless PAL_SUITE env var is set
323-
# all test class methods will be run
324-
b = 'TestQgsPalLabelingBase.'
325-
tests = [b + 'test_write_read_settings']
326-
res = runSuite(sys.modules[__name__], tests)
318+
# NOTE: unless PAL_SUITE env var is set all test class methods will be run
319+
# ex: 'TestGroup(Point|Line|Curved|Polygon|Feature).test_method'
320+
suite = [
321+
'TestPALConfig.test_write_read_settings'
322+
]
323+
res = runSuite(sys.modules[__name__], suite)
327324
sys.exit(not res.wasSuccessful())

tests/src/python/test_qgspallabeling_canvas.py

+9-50
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,10 @@
3333
)
3434

3535
from test_qgspallabeling_base import TestQgsPalLabeling, runSuite
36+
from test_qgspallabeling_tests import TestPointBase
3637

3738

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

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

56-
def test_default_label(self):
57-
# Verify basic default label placement and text size in points
55+
def checkTest(self):
5856
self.lyr.writeToLayer(self.layer)
5957
self.saveContolImage()
6058
self.assertTrue(*self.renderCheck())
6159

62-
def test_text_size_map_unit(self):
63-
# Verify label text size in map units
64-
self.lyr.fontSizeInMapUnits = True
65-
tmpFont = QFont(self._TestFont)
66-
tmpFont.setPointSizeF(0.25)
67-
self.lyr.textFont = tmpFont
68-
self.lyr.writeToLayer(self.layer)
69-
self.saveContolImage()
70-
self.assertTrue(*self.renderCheck())
71-
72-
def test_text_color(self):
73-
# Verify label color change
74-
self.lyr.textColor = Qt.blue
75-
self.lyr.writeToLayer(self.layer)
76-
self.saveContolImage()
77-
self.assertTrue(*self.renderCheck())
78-
79-
80-
# class TestQgsPalLabelingLine(TestQgsPalLabeling):
81-
# """Layer-level property tests for line features"""
82-
#
83-
# @classmethod
84-
# def setUpClass(cls):
85-
# TestQgsPalLabeling.setUpClass()
86-
# cls.layer = TestQgsPalLabeling.loadFeatureLayer('line')
87-
#
88-
# def setUp(self):
89-
# """Run before each test."""
90-
# self.configTest('pal_canvas', 'sl')
91-
# self.lyr = self.defaultSettings()
92-
#
93-
# def tearDown(self):
94-
# """Run after each test."""
95-
# pass
96-
9760

9861
if __name__ == '__main__':
99-
# NOTE: unless PAL_SUITE env var is set
100-
# all test class methods will be run
101-
sp = 'TestQgsPalLabelingPoint.'
102-
sl = 'TestQgsPalLabelingLine.'
103-
sc = 'TestQgsPalLabelingCurved.'
104-
sg = 'TestQgsPalLabelingPolygon.'
105-
mf = 'TestQgsPalLabelingMultiFeature.'
106-
107-
tests = [sp + 'test_default_label']
108-
res = runSuite(sys.modules[__name__], tests)
62+
# NOTE: unless PAL_SUITE env var is set all test class methods will be run
63+
# ex: 'TestGroup(Point|Line|Curved|Polygon|Feature).test_method'
64+
suite = [
65+
'TestCanvasPoint.test_text_size_map_unit'
66+
]
67+
res = runSuite(sys.modules[__name__], suite)
10968
sys.exit(not res.wasSuccessful())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# -*- coding: utf-8 -*-
2+
"""QGIS Unit tests for QgsPalLabeling: base suite of render check tests
3+
4+
Class is meant to be inherited by classes that test different labeling outputs
5+
6+
.. note:: This program is free software; you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation; either version 2 of the License, or
9+
(at your option) any later version.
10+
"""
11+
__author__ = 'Larry Shaffer'
12+
__date__ = '07/16/2013'
13+
__copyright__ = 'Copyright 2013, The QGIS Project'
14+
# This will get replaced with a git SHA1 when you do a git archive
15+
__revision__ = '$Format:%H$'
16+
17+
import os
18+
import sys
19+
from PyQt4.QtCore import *
20+
from PyQt4.QtGui import *
21+
22+
from qgis.core import (
23+
QgsPalLayerSettings,
24+
)
25+
26+
27+
class TestPointBase(object):
28+
29+
def __init__(self):
30+
"""Dummy assignments, intended to be overriden in subclasses"""
31+
self.lyr = QgsPalLayerSettings()
32+
self._TestFont = QFont()
33+
34+
def checkTest(self):
35+
"""Intended to be overriden in subclasses"""
36+
pass
37+
38+
def test_default_label(self):
39+
# Default label placement, with text size in points
40+
self.checkTest()
41+
42+
def test_text_size_map_unit(self):
43+
# Label text size in map units
44+
self.lyr.fontSizeInMapUnits = True
45+
tmpFont = QFont(self._TestFont)
46+
tmpFont.setPointSizeF(0.25)
47+
self.lyr.textFont = tmpFont
48+
self.checkTest()
49+
50+
def test_text_color(self):
51+
# Label color change
52+
self.lyr.textColor = Qt.blue
53+
self.checkTest()
54+
55+
56+
if __name__ == '__main__':
57+
pass

tests/src/python/utilities.py

+17
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
import re
2525
from itertools import izip
2626

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

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

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

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

209214
return True
215+
216+
217+
def openInBrowserTab(url):
218+
if sys.platform[:3] in ('win', 'dar'):
219+
webbrowser.open_new_tab(url)
220+
else:
221+
# some Linux OS pause execution on webbrowser open, so background it
222+
cmd = 'import webbrowser;' \
223+
'webbrowser.open_new_tab({0})'.format(url)
224+
p = subprocess.Popen([sys.executable, "-c", cmd],
225+
stdout=subprocess.PIPE,
226+
stderr=subprocess.STDOUT).pid

0 commit comments

Comments
 (0)