Skip to content

Commit

Permalink
[labeling] Make test font available for app, tests, server without re…
Browse files Browse the repository at this point in the history
…quiring font install

- Load test font from testdata.qrc if app or server built with ENABLE_TESTS
- Add global loading of test font in utilities.py
  • Loading branch information
dakcarto committed Aug 21, 2013
1 parent a634f18 commit e75022e
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 21 deletions.
11 changes: 9 additions & 2 deletions src/app/CMakeLists.txt
Expand Up @@ -366,6 +366,13 @@ SET(IMAGE_RCCS ../../images/images.qrc)

QT4_ADD_RESOURCES(IMAGE_RCC_SRCS ${IMAGE_RCCS})

SET(TEST_RCC_SRCS)
IF (ENABLE_TESTS)
# add test resources, e.g. standard test font
SET(TEST_RCCS ../../tests/testdata/testdata.qrc)
QT4_ADD_RESOURCES(TEST_RCC_SRCS ${TEST_RCCS})
ENDIF (ENABLE_TESTS)

QT4_WRAP_CPP(QGIS_APP_MOC_SRCS ${QGIS_APP_MOC_HDRS})

IF (WIN32)
Expand Down Expand Up @@ -444,9 +451,9 @@ ENDIF (POSTGRES_FOUND)

#############
IF (ANDROID)
ADD_LIBRARY(${QGIS_APP_NAME} SHARED ${QGIS_APP_SRCS} ${QGIS_APP_MOC_SRCS} ${QGIS_APP_HDRS} ${QGIS_APP_MOC_HDRS} ${IMAGE_RCC_SRCS})
ADD_LIBRARY(${QGIS_APP_NAME} SHARED ${QGIS_APP_SRCS} ${QGIS_APP_MOC_SRCS} ${QGIS_APP_HDRS} ${QGIS_APP_MOC_HDRS} ${IMAGE_RCC_SRCS} ${TEST_RCC_SRCS})
ELSE (ANDROID)
ADD_EXECUTABLE(${QGIS_APP_NAME} MACOSX_BUNDLE WIN32 ${QGIS_APP_SRCS} ${QGIS_APP_MOC_SRCS} ${IMAGE_RCC_SRCS})
ADD_EXECUTABLE(${QGIS_APP_NAME} MACOSX_BUNDLE WIN32 ${QGIS_APP_SRCS} ${QGIS_APP_MOC_SRCS} ${IMAGE_RCC_SRCS} ${TEST_RCC_SRCS})
ENDIF (ANDROID)

TARGET_LINK_LIBRARIES(${QGIS_APP_NAME}
Expand Down
9 changes: 9 additions & 0 deletions src/app/main.cpp
Expand Up @@ -21,6 +21,7 @@
#include <QFile>
#include <QFileInfo>
#include <QFont>
#include <QFontDatabase>
#include <QPixmap>
#include <QLocale>
#include <QSettings>
Expand Down Expand Up @@ -810,6 +811,14 @@ int main( int argc, char *argv[] )
}
}

// load standard test font from testdata.qrc (for unit tests)
QFile testFont( ":/testdata/font/FreeSansQGIS.ttf" );
if ( testFont.open( QIODevice::ReadOnly ) )
{
int fontID = QFontDatabase::addApplicationFontFromData( testFont.readAll() );
QgsDebugMsg( QString( "Test font %1loaded from testdata.qrc" ).arg( fontID != -1 ? "" : "NOT " ) );
} // else app wasn't built with ENABLE_TESTS

// Set the application style. If it's not set QT will use the platform style except on Windows
// as it looks really ugly so we use QPlastiqueStyle.
QString style = mySettings.value( "/qgis/style" ).toString();
Expand Down
8 changes: 8 additions & 0 deletions src/mapserver/CMakeLists.txt
Expand Up @@ -66,11 +66,19 @@ QT4_WRAP_CPP (qgis_mapserv_MOC_SRCS ${qgis_mapserv_MOC_HDRS})

QT4_ADD_RESOURCES(qgis_mapserv_RCC_SRCS ${qgis_mapserv_RCCS})

SET(qgis_mapserv_TESTRCC_SRCS)
IF (ENABLE_TESTS)
# add test resources, e.g. standard test font
SET(qgis_mapserv_TESTRCCS ../../tests/testdata/testdata.qrc)
QT4_ADD_RESOURCES(qgis_mapserv_TESTRCC_SRCS ${qgis_mapserv_TESTRCCS})
ENDIF(ENABLE_TESTS)

ADD_EXECUTABLE(qgis_mapserv.fcgi
${qgis_mapserv_SRCS}
${qgis_mapserv_MOC_SRCS}
${qgis_mapserv_RCC_SRCS}
${qgis_mapserv_UIS_H}
${qgis_mapserv_TESTRCC_SRCS}
)

INCLUDE_DIRECTORIES(
Expand Down
12 changes: 12 additions & 0 deletions src/mapserver/qgis_map_serv.cpp
Expand Up @@ -240,9 +240,21 @@ int main( int argc, char * argv[] )
QgsMapRenderer* theMapRenderer = new QgsMapRenderer();
theMapRenderer->setLabelingEngine( new QgsPalLabeling() );

// load standard test font from testdata.qrc (for unit tests)
bool testFontLoaded = false;
QFile testFont( ":/testdata/font/FreeSansQGIS.ttf" );
if ( testFont.open( QIODevice::ReadOnly ) )
{
int fontID = QFontDatabase::addApplicationFontFromData( testFont.readAll() );
testFontLoaded = ( fontID != -1 );
} // else app wasn't built with ENABLE_TESTS or not GUI app

while ( fcgi_accept() >= 0 )
{
printRequestInfos(); //print request infos if in debug mode
#ifdef QGSMSDEBUG
QgsDebugMsg( QString( "Test font %1loaded from testdata.qrc" ).arg( testFontLoaded ? "" : "NOT " ) );
#endif

//use QgsGetRequestHandler in case of HTTP GET and QgsSOAPRequestHandler in case of HTTP POST
QgsRequestHandler* theRequestHandler = 0;
Expand Down
26 changes: 12 additions & 14 deletions tests/src/python/test_qgspallabeling_base.py
Expand Up @@ -47,10 +47,12 @@
unittest,
expectedFailure,
unitTestDataPath,
loadTestFont,
openInBrowserTab
)

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

PALREPORT = 'PAL_REPORT' in os.environ
PALREPORTS = {}
Expand All @@ -61,7 +63,7 @@ class TestQgsPalLabeling(TestCase):
_TestDataDir = unitTestDataPath()
_PalDataDir = os.path.join(_TestDataDir, 'labeling')
_PalFeaturesDb = os.path.join(_PalDataDir, 'pal_features_v3.sqlite')
_TestFontID = -1
_TestFont = TESTFONT
_MapRegistry = None
_MapRenderer = None
_Canvas = None
Expand All @@ -81,18 +83,8 @@ def setUpClass(cls):
assert res, msg

# load the FreeSansQGIS labeling test font
fontdb = QFontDatabase()
cls._TestFontID = fontdb.addApplicationFont(
os.path.join(cls._TestDataDir, 'font', 'FreeSansQGIS.ttf'))
msg = ('\nCould not store test font in font database, '
'SKIPPING TEST SUITE')
assert cls._TestFontID != -1, msg

cls._TestFont = fontdb.font('FreeSansQGIS', 'Medium', 48)
appfont = QApplication.font()
msg = ('\nCould not load test font from font database, '
'SKIPPING TEST SUITE')
assert cls._TestFont.toString() != appfont.toString(), msg
msg = '\nCould not load test font, SKIPPING TEST SUITE'
assert TESTFONT is not None, msg

cls._TestFunction = ''
cls._TestGroup = ''
Expand Down Expand Up @@ -128,6 +120,10 @@ def tearDownClass(cls):
def removeAllLayers(cls):
cls._MapRegistry.removeAllMapLayers()

@classmethod
def getTestFont(cls):
return QFont(cls._TestFont)

@classmethod
def loadFeatureLayer(cls, table):
uri = QgsDataSourceURI()
Expand Down Expand Up @@ -176,7 +172,9 @@ def defaultSettings(self):
lyr = QgsPalLayerSettings()
lyr.enabled = True
lyr.fieldName = 'text' # default in data sources
lyr.textFont = self._TestFont
font = self.getTestFont()
font.setPointSize(48)
lyr.textFont = font
lyr.textNamedStyle = 'Medium'
return lyr

Expand Down
10 changes: 5 additions & 5 deletions tests/src/python/test_qgspallabeling_tests.py
Expand Up @@ -29,9 +29,9 @@ class TestPointBase(object):
def __init__(self):
"""Dummy assignments, intended to be overriden in subclasses"""
self.lyr = QgsPalLayerSettings()
self._TestFont = QFont()
self._TestFont = QApplication.font()

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

Expand All @@ -42,9 +42,9 @@ def test_default_label(self):
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
font = QFont(self._TestFont)
font.setPointSizeF(500)
self.lyr.textFont = font
self.checkTest()

def test_text_color(self):
Expand Down
14 changes: 14 additions & 0 deletions tests/src/python/utilities.py
Expand Up @@ -48,6 +48,7 @@
GEOCRS = 4326 # constant for EPSG:GEOCRS Geographic CRS id
GOOGLECRS = 900913 # constant for EPSG:GOOGLECRS Google Mercator id

TESTFONT = None

def assertHashesForFile(theHashes, theFilename):
"""Assert that a files has matches one of a list of expected hashes"""
Expand Down Expand Up @@ -214,6 +215,19 @@ def compareWkt(a, b, tol=0.000001):
return True


def loadTestFont():
# load the FreeSansQGIS test font
global TESTFONT # pylint: disable=W0603

if TESTFONT is None:
fontid = QtGui.QFontDatabase.addApplicationFont(
os.path.join(unitTestDataPath('font'), 'FreeSansQGIS.ttf'))
if fontid != -1:
TESTFONT = QtGui.QFont('FreeSansQGIS')

return TESTFONT


def openInBrowserTab(url):
if sys.platform[:3] in ('win', 'dar'):
webbrowser.open_new_tab(url)
Expand Down
5 changes: 5 additions & 0 deletions tests/testdata/testdata.qrc
@@ -0,0 +1,5 @@
<RCC>
<qresource prefix="/testdata">
<file>font/FreeSansQGIS.ttf</file>
</qresource>
</RCC>

0 comments on commit e75022e

Please sign in to comment.