testmasterstyles.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Copyright (C) 2007-2010 Søren Roug, European Environment Agency
  4. #
  5. # This is free software. You may redistribute it under the terms
  6. # of the Apache license and the GNU General Public License Version
  7. # 2 or at your option any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public
  15. # License along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. #
  18. # Contributor(s):
  19. #
  20. import unittest
  21. from odf.opendocument import OpenDocumentPresentation, OpenDocumentText
  22. from odf import style, text
  23. from odf.table import Table, TableColumn, TableRow, TableCell
  24. from odf.element import IllegalChild
  25. from odf.namespaces import TEXTNS
  26. from elementparser import ElementParser
  27. class TestMasterStyles(unittest.TestCase):
  28. def assertContains(self, stack, needle):
  29. self.assertNotEqual(-1, stack.find(needle))
  30. def assertNotContains(self, stack, needle):
  31. self.assertEqual(-1, stack.find(needle))
  32. def testStyle(self):
  33. """ Create a presentation with a page layout called MyLayout
  34. Add a presentation style for the title
  35. Check that MyLayout is listed in styles.xml
  36. """
  37. presdoc = OpenDocumentPresentation()
  38. # We must describe the dimensions of the page
  39. pagelayout = style.PageLayout(name="MyLayout")
  40. presdoc.automaticstyles.addElement(pagelayout)
  41. pagelayout.addElement(style.PageLayoutProperties(margin="0cm", pagewidth="28cm", pageheight="21cm", printorientation="landscape"))
  42. # Every drawing page must have a master page assigned to it.
  43. masterpage = style.MasterPage(name="MyMaster", pagelayoutname=pagelayout)
  44. presdoc.masterstyles.addElement(masterpage)
  45. # Style for the title frame of the page
  46. # We set a centered 34pt font with yellowish background
  47. titlestyle = style.Style(name="MyMaster-title", family="presentation")
  48. titlestyle.addElement(style.ParagraphProperties(textalign="center"))
  49. titlestyle.addElement(style.TextProperties(fontsize="34pt"))
  50. titlestyle.addElement(style.GraphicProperties(fillcolor="#ffff99"))
  51. presdoc.styles.addElement(titlestyle)
  52. s = presdoc.stylesxml()
  53. self.assertContains(s, '<style:page-layout style:name="MyLayout"><style:page-layout-properties ')
  54. e = ElementParser(s,'style:page-layout-properties')
  55. self.assertEqual(e.element,'style:page-layout-properties')
  56. self.assertTrue(e.has_value("fo:margin","0cm"))
  57. self.assertTrue(e.has_value("fo:page-width","28cm"))
  58. self.assertTrue(e.has_value("fo:page-height","21cm"))
  59. self.assertTrue(e.has_value("style:print-orientation","landscape"))
  60. e = ElementParser(s,'style:style')
  61. self.assertTrue(e.has_value("style:name","MyMaster-title"))
  62. self.assertTrue(e.has_value("style:display-name","MyMaster-title"))
  63. self.assertTrue(e.has_value("style:family","presentation"))
  64. self.assertContains(s, '<style:paragraph-properties fo:text-align="center"/><style:text-properties fo:font-size="34pt"/><style:graphic-properties draw:fill-color="#ffff99"/></style:style></office:styles>')
  65. e = ElementParser(s,'style:master-page')
  66. self.assertTrue(e.has_value("style:name","MyMaster"))
  67. self.assertTrue(e.has_value("style:display-name","MyMaster"))
  68. self.assertTrue(e.has_value("style:page-layout-name","MyLayout"))
  69. def testMasterWithHeader(self):
  70. """ Create a text document with a page layout called "pagelayout"
  71. Add a master page
  72. Check that pagelayout is listed in styles.xml
  73. """
  74. textdoc = OpenDocumentText()
  75. pl = style.PageLayout(name="pagelayout")
  76. textdoc.automaticstyles.addElement(pl)
  77. mp = style.MasterPage(name="Standard", pagelayoutname=pl)
  78. textdoc.masterstyles.addElement(mp)
  79. h = style.Header()
  80. hp = text.P(text="header try")
  81. h.addElement(hp)
  82. mp.addElement(h)
  83. s = textdoc.stylesxml()
  84. self.assertContains(s, u'<office:automatic-styles><style:page-layout style:name="pagelayout"/></office:automatic-styles>')
  85. def testAutomaticStyles(self):
  86. """ Create a text document with a page layout called "pagelayout"
  87. Add a master page
  88. Add an automatic style for the heading
  89. Check that pagelayout is listed in styles.xml under automatic-styles
  90. Check that the heading style is NOT listed in styles.xml
  91. Check that the pagelayout is NOT listed in content.xml
  92. """
  93. textdoc = OpenDocumentText()
  94. parastyle = style.Style(name="Para", family="paragraph")
  95. parastyle.addElement(style.ParagraphProperties(numberlines="false", linenumber="0"))
  96. parastyle.addElement(style.TextProperties(fontsize="24pt", fontweight="bold"))
  97. textdoc.automaticstyles.addElement(parastyle)
  98. hpstyle = style.Style(name="HeaderPara", family="paragraph")
  99. hpstyle.addElement(style.ParagraphProperties(linenumber="0"))
  100. hpstyle.addElement(style.TextProperties(fontsize="18pt", fontstyle="italic"))
  101. textdoc.automaticstyles.addElement(hpstyle)
  102. pl = style.PageLayout(name="pagelayout")
  103. textdoc.automaticstyles.addElement(pl)
  104. mp = style.MasterPage(name="Standard", pagelayoutname=pl)
  105. textdoc.masterstyles.addElement(mp)
  106. h = style.Header()
  107. hp = text.P(text="header content", stylename=hpstyle)
  108. h.addElement(hp)
  109. mp.addElement(h)
  110. textdoc.text.addElement(text.P(text="Paragraph 1", stylename=parastyle))
  111. # Check styles.xml
  112. s = textdoc.stylesxml()
  113. self.assertContains(s, u'<style:page-layout style:name="pagelayout"/>')
  114. self.assertContains(s, u'style:name="HeaderPara"')
  115. self.assertNotContains(s, u'style:name="Para" ')
  116. # Check content.xml
  117. s = textdoc.contentxml() # contentxml is supposed to yed a byts
  118. self.assertNotContains(s, b'<style:page-layout style:name="pagelayout"/>')
  119. self.assertContains(s, b'style:name="Para"')
  120. if __name__ == '__main__':
  121. unittest.main()