From 916706f63ed04deeaf1fbc288d575d9f89161a4a Mon Sep 17 00:00:00 2001 From: Larry Shaffer Date: Wed, 19 Feb 2014 13:25:27 -0700 Subject: [PATCH] Update tests to work with new test font functions in QgsFontUtils - Cleanup code style --- src/core/qgsfontutils.cpp | 10 ++--- tests/src/python/test_qgsfontutils.py | 44 ++++++++++++------- tests/src/python/test_qgspallabeling_base.py | 14 +++--- tests/src/python/test_qgspallabeling_tests.py | 4 +- tests/src/python/utilities.py | 38 +++++++++++----- 5 files changed, 66 insertions(+), 44 deletions(-) diff --git a/src/core/qgsfontutils.cpp b/src/core/qgsfontutils.cpp index 2c4bb22f998..8a54fae7c96 100644 --- a/src/core/qgsfontutils.cpp +++ b/src/core/qgsfontutils.cpp @@ -224,7 +224,7 @@ bool QgsFontUtils::loadStandardTestFonts( QStringList loadstyles ) { QString fontstyle( f.key() ); QString fontpath( f.value() ); - if ( ! ( loadstyles.contains( fontstyle ) || loadstyles.contains( "All" ) ) ) + if ( !( loadstyles.contains( fontstyle ) || loadstyles.contains( "All" ) ) ) { continue; } @@ -248,8 +248,8 @@ bool QgsFontUtils::loadStandardTestFonts( QStringList loadstyles ) int fontID = QFontDatabase::addApplicationFont( fontPath ); loaded = ( fontID != -1 ); fontsLoaded = ( fontsLoaded || loaded ); - QgsDebugMsg( QString( "Test font '%1' %2 from filesystem") - .arg( familyStyle ).arg( loaded ? "loaded" : "FAILED to load" ) ); + QgsDebugMsg( QString( "Test font '%1' %2 from filesystem" ) + .arg( familyStyle ).arg( loaded ? "loaded" : "FAILED to load" ) ); } else { @@ -260,8 +260,8 @@ bool QgsFontUtils::loadStandardTestFonts( QStringList loadstyles ) loaded = ( fontID != -1 ); fontsLoaded = ( fontsLoaded || loaded ); } - QgsDebugMsg( QString( "Test font '%1' %2 from testdata.qrc") - .arg( familyStyle ).arg( loaded ? "loaded" : "FAILED to load" ) ); + QgsDebugMsg( QString( "Test font '%1' %2 from testdata.qrc" ) + .arg( familyStyle ).arg( loaded ? "loaded" : "FAILED to load" ) ); } } } diff --git a/tests/src/python/test_qgsfontutils.py b/tests/src/python/test_qgsfontutils.py index 5c2fbbfb8fb..c21c18773fd 100644 --- a/tests/src/python/test_qgsfontutils.py +++ b/tests/src/python/test_qgsfontutils.py @@ -27,6 +27,8 @@ from utilities import ( unittest, expectedFailure, unitTestDataPath, + getTestFontFamily, + loadTestFonts ) QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() @@ -36,31 +38,41 @@ class TestQgsFontUtils(TestCase): @classmethod def setUpClass(cls): - cls._family = 'QGIS Vera Sans' - cls._fontdb = QFontDatabase() - """:type : QFontDatabase""" + cls._family = getTestFontFamily() + cls._has_style = QgsFontUtils.fontFamilyHasStyle - def test_loading_specific_test_font(self): - QgsFontUtils.loadStandardTestFonts(['Roman']) - msg = self._family + ' Roman test font styles could not be loaded' - assert self._has_style(self._family, 'Roman'), msg + def test_loading_base_test_fonts(self): + loadTestFonts() - def test_loading_all_test_fonts(self): + def test_loading_every_test_font(self): QgsFontUtils.loadStandardTestFonts(['All']) # styles = '' - # for style in self._fontdb.styles(self._family): + # for style in QFontDatabase().styles(self._family): # styles += ' ' + style # print self._family + ' styles:' + styles + + res = ( + self._has_style(self._family, 'Roman') + and self._has_style(self._family, 'Oblique') + and self._has_style(self._family, 'Bold') + and self._has_style(self._family, 'Bold Oblique') + ) msg = self._family + ' test font styles could not be loaded' - res = (self._has_style(self._family, 'Roman') - and self._has_style(self._family, 'Oblique') - and self._has_style(self._family, 'Bold') - and self._has_style(self._family, 'Bold Oblique')) assert res, msg - def _has_style(self, family, style): - return (family in self._fontdb.families() - and style in self._fontdb.styles(family)) + def test_get_specific_test_font(self): + # default returned is Roman at 12 pt + f = QgsFontUtils.getStandardTestFont('Bold Oblique', 14) + """:type: QFont""" + res = ( + f.family() == self._family + and f.bold() + and f.italic() + and f.pointSize() == 14 + ) + msg = self._family + ' test font Bold Oblique at 14 pt not retrieved' + assert res, msg + if __name__ == '__main__': unittest.main() diff --git a/tests/src/python/test_qgspallabeling_base.py b/tests/src/python/test_qgspallabeling_base.py index 1ef6610b246..edb73e5b462 100644 --- a/tests/src/python/test_qgspallabeling_base.py +++ b/tests/src/python/test_qgspallabeling_base.py @@ -49,12 +49,13 @@ from utilities import ( unittest, expectedFailure, unitTestDataPath, - loadTestFont, + loadTestFonts, + getTestFont, openInBrowserTab ) QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() -TESTFONT = loadTestFont() +FONTSLOADED = loadTestFonts() PALREPORT = 'PAL_REPORT' in os.environ PALREPORTS = {} @@ -65,10 +66,11 @@ class TestQgsPalLabeling(TestCase): _TestDataDir = unitTestDataPath() _PalDataDir = os.path.join(_TestDataDir, 'labeling') _PalFeaturesDb = os.path.join(_PalDataDir, 'pal_features_v3.sqlite') - _TestFont = TESTFONT + _TestFont = getTestFont() # Roman at 12 pt _MapRegistry = None _MapRenderer = None _Canvas = None + _PalEngine = None @classmethod def setUpClass(cls): @@ -83,10 +85,6 @@ class TestQgsPalLabeling(TestCase): res = 'spatialite' in QgsProviderRegistry.instance().providerList() assert res, msg - # load the FreeSansQGIS labeling test font - msg = '\nCould not load test font, SKIPPING TEST SUITE' - assert TESTFONT is not None, msg - cls._TestFunction = '' cls._TestGroup = '' cls._TestGroupPrefix = '' @@ -182,7 +180,7 @@ class TestQgsPalLabeling(TestCase): font = self.getTestFont() font.setPointSize(48) lyr.textFont = font - lyr.textNamedStyle = 'Medium' + lyr.textNamedStyle = 'Roman' return lyr @staticmethod diff --git a/tests/src/python/test_qgspallabeling_tests.py b/tests/src/python/test_qgspallabeling_tests.py index 12ee03ba36d..1cd0d70fbe6 100644 --- a/tests/src/python/test_qgspallabeling_tests.py +++ b/tests/src/python/test_qgspallabeling_tests.py @@ -23,15 +23,13 @@ from qgis.core import ( QgsPalLayerSettings, ) -from utilities import loadTestFont - class TestPointBase(object): def __init__(self): """Dummy assignments, intended to be overriden in subclasses""" self.lyr = QgsPalLayerSettings() - self._TestFont = loadTestFont() + self._TestFont = QApplication.font() # will become a standard test font def checkTest(self, **kwargs): """Intended to be overriden in subclasses""" diff --git a/tests/src/python/utilities.py b/tests/src/python/utilities.py index 0f31b19f922..c0c6612b30a 100644 --- a/tests/src/python/utilities.py +++ b/tests/src/python/utilities.py @@ -17,7 +17,8 @@ import qgis from PyQt4 import QtGui, QtCore from qgis.core import (QgsApplication, QgsCoordinateReferenceSystem, - QgsVectorFileWriter) + QgsVectorFileWriter, + QgsFontUtils) from qgis.gui import QgsMapCanvas from qgis_interface import QgisInterface import hashlib @@ -48,7 +49,7 @@ IFACE = None GEOCRS = 4326 # constant for EPSG:GEOCRS Geographic CRS id GOOGLECRS = 900913 # constant for EPSG:GOOGLECRS Google Mercator id -TESTFONT = None +FONTSLOADED = False def assertHashesForFile(theHashes, theFilename): @@ -216,18 +217,31 @@ def compareWkt(a, b, tol=0.000001): return True -def loadTestFont(): - # load the FreeSansQGIS test font - global TESTFONT # pylint: disable=W0603 +def getTestFontFamily(): + return QgsFontUtils.standardTestFontFamily() - if TESTFONT is None: - fontid = QtGui.QFontDatabase().addApplicationFont( - os.path.join(unitTestDataPath('font'), - 'QGIS-Vera', 'QGIS-Vera.ttf')) - if fontid != -1: - TESTFONT = QtGui.QFont('QGIS Vera Sans') - return TESTFONT +def getTestFont(style='Roman', size=12): + """Only Roman and Bold are loaded by default + Others available: Oblique, Bold Oblique + """ + if not FONTSLOADED: + loadTestFonts() + return QgsFontUtils.getStandardTestFont(style, size) + + +def loadTestFonts(): + if QGISAPP is None: + getQgisTestApp() + + global FONTSLOADED # pylint: disable=W0603 + if FONTSLOADED is False: + QgsFontUtils.loadStandardTestFonts(['Roman', 'Bold']) + msg = getTestFontFamily() + ' base test font styles could not be loaded' + res = (QgsFontUtils.fontFamilyHasStyle(getTestFontFamily(), 'Roman') + and QgsFontUtils.fontFamilyHasStyle(getTestFontFamily(), 'Bold')) + assert res, msg + FONTSLOADED = True def openInBrowserTab(url):