Skip to content

Commit

Permalink
Update tests to work with new test font functions in QgsFontUtils
Browse files Browse the repository at this point in the history
- Cleanup code style
  • Loading branch information
dakcarto committed Feb 19, 2014
1 parent 523fd44 commit 916706f
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 44 deletions.
10 changes: 5 additions & 5 deletions src/core/qgsfontutils.cpp
Expand Up @@ -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;
}
Expand All @@ -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
{
Expand All @@ -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" ) );
}
}
}
Expand Down
44 changes: 28 additions & 16 deletions tests/src/python/test_qgsfontutils.py
Expand Up @@ -27,6 +27,8 @@
unittest,
expectedFailure,
unitTestDataPath,
getTestFontFamily,
loadTestFonts
)

QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()
Expand All @@ -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()
14 changes: 6 additions & 8 deletions tests/src/python/test_qgspallabeling_base.py
Expand Up @@ -49,12 +49,13 @@
unittest,
expectedFailure,
unitTestDataPath,
loadTestFont,
loadTestFonts,
getTestFont,
openInBrowserTab
)

QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()
TESTFONT = loadTestFont()
FONTSLOADED = loadTestFonts()

PALREPORT = 'PAL_REPORT' in os.environ
PALREPORTS = {}
Expand All @@ -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):
Expand All @@ -83,10 +85,6 @@ def setUpClass(cls):
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 = ''
Expand Down Expand Up @@ -182,7 +180,7 @@ def defaultSettings(self):
font = self.getTestFont()
font.setPointSize(48)
lyr.textFont = font
lyr.textNamedStyle = 'Medium'
lyr.textNamedStyle = 'Roman'
return lyr

@staticmethod
Expand Down
4 changes: 1 addition & 3 deletions tests/src/python/test_qgspallabeling_tests.py
Expand Up @@ -23,15 +23,13 @@
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"""
Expand Down
38 changes: 26 additions & 12 deletions tests/src/python/utilities.py
Expand Up @@ -17,7 +17,8 @@
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
Expand Down Expand Up @@ -48,7 +49,7 @@
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):
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 916706f

Please sign in to comment.