Showing with 139 additions and 4 deletions.
  1. +2 −1 tests/src/python/CMakeLists.txt
  2. +4 −3 tests/src/python/test_qgscomposerhtml.py
  3. +98 −0 tests/src/python/test_qgscomposition.py
  4. +35 −0 tests/testdata/template-for-substitution.qpt
3 changes: 2 additions & 1 deletion tests/src/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ ADD_PYTHON_TEST(PyQgsLogger test_qgslogger.py)
ADD_PYTHON_TEST(PyQgsCoordinateTransform test_qgscoordinatetransform.py)
ADD_PYTHON_TEST(PyQgsRectangle test_qgsrectangle.py)
ADD_PYTHON_TEST(PyQgsSpatialIndex test_qgsspatialindex.py)
#ADD_PYTHON_TEST(PyQgsComposerHtml test_qgscomposerhtml.py)
ADD_PYTHON_TEST(PyQgsComposerHtml test_qgscomposerhtml.py)
ADD_PYTHON_TEST(PyQgsComposition test_qgscomposition.py)
#ADD_PYTHON_TEST(PyQgsComposerMap test_qgscomposermap.py)
7 changes: 4 additions & 3 deletions tests/src/python/test_qgscomposerhtml.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
TEST_DATA_DIR = unitTestDataPath()


class TestQgsComposerMap(unittest.TestCase):
class TestQgsComposerHtml(unittest.TestCase):

def setUp(self):
"""Run before each test."""
Expand All @@ -69,7 +69,8 @@ def htmlUrl(self):
myUrl = QUrl(QString("file:///%1").arg(myPath))
return myUrl

def testTable(self):
@expectedFailure
def XtestTable(self):
"""Test we can render a html table in a single frame."""
composerHtml = QgsComposerHtml(self.mComposition, False)
htmlFrame = QgsComposerFrame(self.mComposition,
Expand All @@ -86,7 +87,7 @@ def testTable(self):
assert myResult, myMessage

@expectedFailure
def testTableMultiFrame(self):
def XtestTableMultiFrame(self):
"""Test we can render to multiframes."""
composerHtml = QgsComposerHtml(self.mComposition, False)
htmlFrame = QgsComposerFrame(self.mComposition, composerHtml,
Expand Down
98 changes: 98 additions & 0 deletions tests/src/python/test_qgscomposition.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# -*- coding: utf-8 -*-
'''
test_qgscomposerhtml.py
--------------------------------------
Date : August 2012
Copyright : (C) 2012 by Dr. Horst Düster /
Dr. Marco Hugentobler
Tim Sutton
email : marco@sourcepole.ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
'''
import unittest
import sys
import os
from utilities import unitTestDataPath, getQgisTestApp
from PyQt4.QtCore import QUrl, QString, qDebug
from PyQt4.QtXml import QDomDocument
from qgis.core import (QgsComposition, QgsPoint)

from qgscompositionchecker import QgsCompositionChecker
# support python < 2.7 via unittest2
# needed for expected failure decorator
if sys.version_info[0:2] < (2,7):
try:
from unittest2 import TestCase, expectedFailure
except ImportError:
print "You need to install unittest2 to run the salt tests"
sys.exit(1)
else:
from unittest import TestCase, expectedFailure

QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()
TEST_DATA_DIR = unitTestDataPath()


class TestQgsComposition(unittest.TestCase):

def setUp(self):
"""Run before each test."""
pass

def tearDown(self):
"""Run after each test."""
pass

@expectedFailure
def testSubstitutionMap(self):
"""Test that we can use degree symbols in substitutions.
"""
# Create a point and convert it to text containing a degree symbol.
myPoint = QgsPoint(12.3, -33.33)
myCoordinates = myPoint.toDegreesMinutesSeconds(2)
myTokens = myCoordinates.split(',')
myLongitude = myTokens[0]
myLatitude = myTokens[1]
myText = 'Latitude: %s, Longitude: %s' % (myLatitude, myLongitude)

# Load the composition with the substitutions
myComposition = QgsComposition(CANVAS.mapRenderer())
mySubstitutionMap = {'replace-me': myText }
myFile = os.path.join(TEST_DATA_DIR, 'template-for-substitution.qpt')
myTemplateFile = file(myFile, 'rt')
myTemplateContent = myTemplateFile.read()
myTemplateFile.close()
myDocument = QDomDocument()
myDocument.setContent(myTemplateContent)
myComposition.loadFromTemplate(myDocument, mySubstitutionMap)

# We should be able to get map0
myMap = myComposition.getComposerMapById(0)
myMessage = ('Map 0 could not be found in template %s', myFile)
assert myMap is not None, myMessage

def testNoSubstitutionMap(self):
"""Test that we can get a map if we use no text substitutions."""
myComposition = QgsComposition(CANVAS.mapRenderer())
myFile = os.path.join(TEST_DATA_DIR, 'template-for-substitution.qpt')
myTemplateFile = file(myFile, 'rt')
myTemplateContent = myTemplateFile.read()
myTemplateFile.close()
myDocument = QDomDocument()
myDocument.setContent(myTemplateContent)
myComposition.loadFromTemplate(myDocument)

# We should be able to get map0
myMap = myComposition.getComposerMapById(0)
myMessage = ('Map 0 could not be found in template %s', myFile)
assert myMap is not None, myMessage

if __name__ == '__main__':
unittest.main()
35 changes: 35 additions & 0 deletions tests/testdata/template-for-substitution.qpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<Composer title="Composer 1" visible="1">
<Composition printResolution="300" paperWidth="297" snapGridOffsetX="0" snapGridResolution="0" snapGridOffsetY="0" snapping="0" numPages="1" paperHeight="210" printAsRaster="0">
<ComposerLabel valign="32" labelText="[replace-me] &lt;-- should be replaced by unit test" halign="1" margin="1">
<LabelFont description="Ubuntu,10,-1,5,50,0,0,0,0,0"/>
<FontColor red="0" blue="0" green="0"/>
<ComposerItem width="248.016" x="22.8186" y="29.6098" rotation="0" height="9.7794" frame="true" outlineWidth="0.3" zValue="2" lastValidViewScaleFactor="3.68121" id="" positionLock="false">
<FrameColor alpha="255" red="0" blue="0" green="0"/>
<BackgroundColor alpha="255" red="255" blue="255" green="255"/>
</ComposerItem>
</ComposerLabel>
<ComposerMap keepLayerSet="false" overviewFrameMap="-1" id="0" previewMode="Cache" drawCanvasItems="true">
<overviewFrame overviewFrameMap="-1">
<symbol outputUnit="MM" alpha="0.3" type="fill" name="">
<layer pass="0" class="SimpleFill" locked="0">
<prop k="color" v="255,0,0,76"/>
<prop k="color_border" v="0,0,0,76"/>
<prop k="offset" v="0,0"/>
<prop k="style" v="solid"/>
<prop k="style_border" v="no"/>
<prop k="width_border" v="0.26"/>
</layer>
</symbol>
</overviewFrame>
<Extent ymin="-1" xmin="-1.24602" ymax="0.347325" xmax="1.24602"/>
<LayerSet/>
<Grid gridStyle="0" crossLength="3" penColorRed="0" penColorBlue="0" intervalX="0" offsetX="0" intervalY="0" offsetY="0" gridFrameStyle="0" gridFrameWidth="2" show="0" penWidth="0" penColorGreen="0">
<Annotation rightDirection="0" bottomDirection="0" format="0" topPosition="1" precision="3" leftDirection="0" rightPosition="1" bottomPosition="1" frameDistance="1" show="0" font="Ubuntu,9,-1,5,50,0,0,0,0,0" leftPosition="1" topDirection="0"/>
</Grid>
<ComposerItem width="246" x="24.835" y="64.7284" rotation="0" height="133" frame="true" outlineWidth="0.3" zValue="1" lastValidViewScaleFactor="3.68121" id="map" positionLock="false">
<FrameColor alpha="255" red="0" blue="0" green="0"/>
<BackgroundColor alpha="255" red="255" blue="255" green="255"/>
</ComposerItem>
</ComposerMap>
</Composition>
</Composer>