Skip to content

Commit

Permalink
[labeling] Unit tests update
Browse files Browse the repository at this point in the history
- Fix unpacking of render check results (fix false positives)
- Push opening of report to background process for Linux
- Report all failures to one random temp HTML file
- Better HTML output for report
  • Loading branch information
dakcarto committed Aug 14, 2013
1 parent 0607202 commit fef6e2b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 32 deletions.
60 changes: 33 additions & 27 deletions tests/src/python/test_qgspallabeling_base.py
Expand Up @@ -21,10 +21,11 @@

import os
import sys
import datetime
import glob
import shutil
import StringIO
import webbrowser
import subprocess
import tempfile
from PyQt4.QtCore import *
from PyQt4.QtGui import *

Expand All @@ -51,13 +52,8 @@

QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()

PALREPORTDIR = ''
if 'PAL_REPORT' in os.environ:
PALREPORTDIR = os.path.join(str(QDir.tempPath()), 'pal_test_report')
# clear out old reports, if temp dir exists
if os.path.exists(PALREPORTDIR):
shutil.rmtree(PALREPORTDIR, True)
os.mkdir(PALREPORTDIR)
PALREPORT = 'PAL_REPORT' in os.environ
PALREPORTS = {}


class TestQgsPalLabeling(TestCase):
Expand All @@ -78,8 +74,6 @@ def setUpClass(cls):
cls._QgisApp, cls._Canvas, cls._Iface, cls._Parent = \
QGISAPP, CANVAS, IFACE, PARENT

cls._PalReportDir = PALREPORTDIR

# verify that spatialite provider is available
msg = ('\nSpatialite provider not found, '
'SKIPPING TEST SUITE')
Expand Down Expand Up @@ -116,8 +110,7 @@ def setUpClass(cls):
# default for labeling test data sources: WGS 84 / UTM zone 13N
crs.createFromSrid(32613)
cls._MapRenderer.setDestinationCrs(crs)
# TODO: match and store platform's native logical output dpi
cls._MapRenderer.setOutputSize(QSize(600, 400), 72)
# use platform's native logical output dpi for QgsMapRenderer on launch

cls._Pal = QgsPalLabeling()
cls._MapRenderer.setLabelingEngine(cls._Pal)
Expand Down Expand Up @@ -219,16 +212,9 @@ def renderCheck(self, mismatch=0):
chk.setControlName(self._Test)
chk.setMapRenderer(self._MapRenderer)
res = chk.runTest(self._Test, mismatch)
if self._PalReportDir and not res: # don't report ok checks
if PALREPORT and not res: # don't report ok checks
testname = self._TestGroup + ' . ' + self._Test
report = '<html>'
report += '<head><title>{0}</title></head>'.format(testname)
report += '<body>' + chk.report().toLocal8Bit() + '</body>'
report += '</html>'
f = QFile(os.path.join(self._PalReportDir, testname + '.html'))
if f.open(QIODevice.ReadWrite | QIODevice.Truncate):
f.write(report)
f.close()
PALREPORTS[testname] = str(chk.report().toLocal8Bit())
msg = '\nRender check failed for "{0}"'.format(self._Test)
return res, msg

Expand Down Expand Up @@ -304,10 +290,30 @@ def runSuite(module, tests):
print '\n' + out.getvalue()
out.close()

if PALREPORTDIR:
for report in os.listdir(PALREPORTDIR):
webbrowser.open_new_tab('file://' +
os.path.join(PALREPORTDIR, report))
if PALREPORTS:
teststamp = 'PAL Test Report: ' + \
datetime.datetime.now().strftime('%Y-%m-%d %X')
report = '<html><head><title>{0}</title></head><body>'.format(teststamp)
report += '\n<h2>Failed Tests: {0}</h2>'.format(len(PALREPORTS))
for k, v in PALREPORTS.iteritems():
report += '\n<h3>{0}</h3>\n{1}'.format(k, v)
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

return res

Expand All @@ -318,4 +324,4 @@ def runSuite(module, tests):
b = 'TestQgsPalLabelingBase.'
tests = [b + 'test_write_read_settings']
res = runSuite(sys.modules[__name__], tests)
sys.exit(int(not res.wasSuccessful()))
sys.exit(not res.wasSuccessful())
10 changes: 5 additions & 5 deletions tests/src/python/test_qgspallabeling_canvas.py
Expand Up @@ -57,7 +57,7 @@ def test_default_label(self):
# Verify basic default label placement and text size in points
self.lyr.writeToLayer(self.layer)
self.saveContolImage()
self.assertTrue(self.renderCheck())
self.assertTrue(*self.renderCheck())

def test_text_size_map_unit(self):
# Verify label text size in map units
Expand All @@ -67,14 +67,14 @@ def test_text_size_map_unit(self):
self.lyr.textFont = tmpFont
self.lyr.writeToLayer(self.layer)
self.saveContolImage()
self.assertTrue(self.renderCheck())
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())
self.assertTrue(*self.renderCheck())


# class TestQgsPalLabelingLine(TestQgsPalLabeling):
Expand Down Expand Up @@ -104,6 +104,6 @@ def test_text_color(self):
sg = 'TestQgsPalLabelingPolygon.'
mf = 'TestQgsPalLabelingMultiFeature.'

tests = [sp + 'test_text_size_map_unit']
tests = [sp + 'test_default_label']
res = runSuite(sys.modules[__name__], tests)
sys.exit(int(not res.wasSuccessful()))
sys.exit(not res.wasSuccessful())

0 comments on commit fef6e2b

Please sign in to comment.