|
1 | 1 | # -*- coding: utf-8 -*-
|
2 | 2 | '''
|
3 |
| -test_qgscomposerhtml.py |
| 3 | +test_qgscomposerhtml.py |
4 | 4 | --------------------------------------
|
5 | 5 | Date : August 2012
|
6 |
| - Copyright : (C) 2012 by Dr. Horst Düster / Dr. Marco Hugentobler |
| 6 | + Copyright : (C) 2012 by Dr. Horst Düster / |
| 7 | + Dr. Marco Hugentobler |
| 8 | + Tim Sutton |
7 | 9 | email : marco@sourcepole.ch
|
8 | 10 | ***************************************************************************
|
9 | 11 | * *
|
|
13 | 15 | * (at your option) any later version. *
|
14 | 16 | * *
|
15 | 17 | ***************************************************************************/
|
16 |
| -''' |
| 18 | +''' |
17 | 19 | import unittest
|
18 |
| -from utilities import * |
19 |
| -from PyQt4.QtCore import * |
20 |
| -from PyQt4.QtGui import * |
21 |
| -from qgis.core import * |
| 20 | +import os |
| 21 | +from utilities import unitTestDataPath, getQgisTestApp |
| 22 | +from PyQt4.QtCore import QUrl, QString, qDebug |
| 23 | +from qgis.core import (QgsComposition, |
| 24 | + QgsComposerHtml, |
| 25 | + QgsComposerFrame, |
| 26 | + QgsComposerMultiFrame) |
| 27 | + |
22 | 28 | from qgscompositionchecker import QgsCompositionChecker
|
23 | 29 |
|
24 | 30 | QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()
|
| 31 | +TEST_DATA_DIR = unitTestDataPath() |
25 | 32 |
|
26 | 33 | class TestQgsComposerMap(unittest.TestCase):
|
27 |
| - |
28 |
| - def testCase(self): |
29 |
| - self.mComposition = QgsComposition( None ) |
30 |
| - self.mComposition.setPaperSize( 297, 210 ) #A4 landscape |
31 |
| - self.table() |
32 |
| - self.tableMultiFrame() |
33 |
| - |
34 |
| - def table(self): |
35 |
| - TEST_DATA_DIR = unitTestDataPath() |
36 |
| - htmlItem = QgsComposerHtml( self.mComposition, False ) |
37 |
| - htmlFrame = QgsComposerFrame( self.mComposition, htmlItem, 0, 0, 100, 200 ) |
38 |
| - htmlFrame.setFrameEnabled( True ) |
39 |
| - htmlItem.addFrame( htmlFrame ) |
40 |
| - htmlItem.setUrl( QUrl( QString( "file:///%1" ).arg( QString( TEST_DATA_DIR ) + QDir.separator() + "html_table.html" ) ) ); |
41 |
| - checker = QgsCompositionChecker( ) |
42 |
| - result = checker.testComposition( "Composer html table", self.mComposition, QString( TEST_DATA_DIR + QDir.separator().toAscii() + "control_images" + QDir.separator().toAscii() + "expected_composerhtml" + QDir.separator().toAscii() + "composerhtml_table.png" ) ) |
43 |
| - self.mComposition.removeMultiFrame( htmlItem ) |
44 |
| - del htmlItem |
45 |
| - assert result == True |
46 |
| - |
47 |
| - def tableMultiFrame(self): |
48 |
| - TEST_DATA_DIR = unitTestDataPath() |
49 |
| - htmlItem = QgsComposerHtml( self.mComposition, False ) |
50 |
| - htmlFrame = QgsComposerFrame( self.mComposition, htmlItem, 10, 10, 100, 50 ) |
51 |
| - htmlItem.addFrame( htmlFrame ) |
52 |
| - htmlItem.setResizeMode( QgsComposerMultiFrame.RepeatUntilFinished ) |
53 |
| - |
54 |
| - htmlItem.setUrl( QUrl( QString( "file:///%1" ).arg( QString( TEST_DATA_DIR ) + QDir.separator() + "html_table.html" ) ) ) |
55 |
| - htmlItem.frame( 0 ).setFrameEnabled( True ) |
56 |
| - |
| 34 | + |
| 35 | + def setUp(self): |
| 36 | + """Run before each test.""" |
| 37 | + self.mComposition = QgsComposition(None) |
| 38 | + self.mComposition.setPaperSize(297, 210) #A4 landscape |
| 39 | + self.htmlItem = QgsComposerHtml(self.mComposition, False) |
| 40 | + |
| 41 | + def tearDown(self): |
| 42 | + """Run after each test.""" |
| 43 | + print "Tear down" |
| 44 | + if self.htmlItem: |
| 45 | + self.mComposition.removeMultiFrame(self.htmlItem) |
| 46 | + del self.htmlItem |
| 47 | + |
| 48 | + def controlImagePath(self, theImageName): |
| 49 | + """Helper to get the path to a control image.""" |
| 50 | + myPath = os.path.join(TEST_DATA_DIR, |
| 51 | + "control_images", |
| 52 | + "expected_composerhtml", |
| 53 | + theImageName) |
| 54 | + assert os.path.exists(myPath) |
| 55 | + return myPath |
| 56 | + |
| 57 | + def htmlUrl(self): |
| 58 | + """Helper to get the url of the html doc.""" |
| 59 | + myPath = os.path.join(TEST_DATA_DIR, "html_table.html") |
| 60 | + myUrl = QUrl(QString("file:///%1").arg(myPath)) |
| 61 | + return myUrl |
| 62 | + |
| 63 | + def testTable(self): |
| 64 | + """Test we can render a html table in a single frame.""" |
| 65 | + htmlFrame = QgsComposerFrame(self.mComposition, |
| 66 | + self.htmlItem, 0, 0, 100, 200) |
| 67 | + htmlFrame.setFrameEnabled(True) |
| 68 | + self.htmlItem.addFrame(htmlFrame) |
| 69 | + self.htmlItem.setUrl(self.htmlUrl()) |
| 70 | + checker = QgsCompositionChecker() |
| 71 | + myResult, myMessage = checker.testComposition( |
| 72 | + "Composer html table", |
| 73 | + self.mComposition, |
| 74 | + self.controlImagePath("composerhtml_table.png")) |
| 75 | + qDebug(myMessage) |
| 76 | + assert myResult, myMessage |
| 77 | + |
| 78 | + def testTableMultiFrame(self): |
| 79 | + """Test we can render to multiframes.""" |
| 80 | + htmlFrame = QgsComposerFrame(self.mComposition, self.htmlItem, |
| 81 | + 10, 10, 100, 50) |
| 82 | + self.htmlItem.addFrame(htmlFrame) |
| 83 | + self.htmlItem.setResizeMode(QgsComposerMultiFrame.RepeatUntilFinished) |
| 84 | + self.htmlItem.setUrl(self.htmlUrl()) |
| 85 | + self.htmlItem.frame(0).setFrameEnabled(True) |
| 86 | + |
57 | 87 | result = True
|
58 | 88 |
|
59 |
| - #page 1 |
60 |
| - checker1 = QgsCompositionChecker( ) |
61 |
| - if not checker1.testComposition( "Composer html table", self.mComposition, QString( QString( TEST_DATA_DIR ) + QDir.separator() + "control_images" + QDir.separator() + "expected_composerhtml" + QDir.separator() + "composerhtml_table_multiframe1.png" ), 0 ): |
62 |
| - result = False |
63 |
| - |
64 |
| - checker2 = QgsCompositionChecker( ) |
65 |
| - if not checker2.testComposition( "Composer html table", self.mComposition, QString( QString( TEST_DATA_DIR ) + QDir.separator() + "control_images" + QDir.separator() + "expected_composerhtml" + QDir.separator() + "composerhtml_table_multiframe2.png" ) , 1 ): |
66 |
| - result = False |
67 |
| - |
68 |
| - checker3 = QgsCompositionChecker( ) |
69 |
| - if not checker3.testComposition( "Composer html table", self.mComposition, QString( QString( TEST_DATA_DIR ) + QDir.separator() + "control_images" + QDir.separator() + "expected_composerhtml" + QDir.separator() + "composerhtml_table_multiframe3.png" ), 2 ): |
70 |
| - result = False |
71 |
| - |
72 |
| - self.mComposition.removeMultiFrame( htmlItem ) |
73 |
| - del htmlItem |
74 |
| - |
75 |
| - assert result == True |
76 |
| - |
| 89 | + myPage = 0 |
| 90 | + checker1 = QgsCompositionChecker() |
| 91 | + myControlImage = self.controlImagePath( |
| 92 | + "composerhtml_table_multiframe1.png") |
| 93 | + print "Checking page 1" |
| 94 | + myResult, myMessage = checker1.testComposition("Composer html table", |
| 95 | + self.mComposition, |
| 96 | + myControlImage, |
| 97 | + myPage) |
| 98 | + assert myResult, myMessage |
| 99 | + |
| 100 | + myPage = 1 |
| 101 | + checker2 = QgsCompositionChecker() |
| 102 | + myControlImage = self.controlImagePath( |
| 103 | + "composerhtml_table_multiframe2.png") |
| 104 | + myResult, myMessage = checker2.testComposition("Composer html table", |
| 105 | + self.mComposition, |
| 106 | + myControlImage, |
| 107 | + myPage) |
| 108 | + assert myResult, myMessage |
| 109 | + |
| 110 | + myPage = 2 |
| 111 | + checker3 = QgsCompositionChecker() |
| 112 | + myControlImage = self.controlImagePath( |
| 113 | + "composerhtml_table_multiframe3.png") |
| 114 | + myResult, myMessage = checker3.testComposition("Composer html table", |
| 115 | + self.mComposition, |
| 116 | + myControlImage, |
| 117 | + myPage) |
| 118 | + assert myResult, myMessage |
| 119 | + |
77 | 120 | if __name__ == '__main__':
|
78 | 121 | unittest.main()
|
0 commit comments