2 # -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
4 # This file is part of the LibreOffice project.
6 # This Source Code Form is subject to the terms of the Mozilla Public
7 # License, v. 2.0. If a copy of the MPL was not distributed with this
8 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
11 from org
.libreoffice
.unotest
import UnoInProcess
12 from com
.sun
.star
.container
import NoSuchElementException
13 from com
.sun
.star
.beans
import UnknownPropertyException
14 from com
.sun
.star
.lang
import IndexOutOfBoundsException
15 from com
.sun
.star
.lang
import IllegalArgumentException
18 class CheckStyle(unittest
.TestCase
):
22 cls
._uno
= UnoInProcess()
26 def tearDownClass(cls
):
29 def test_StyleFamilies(self
):
30 xDoc
= CheckStyle
._uno
.openEmptyWriterDoc()
31 xStyleFamilies
= xDoc
.StyleFamilies
32 self
.assertEqual(xStyleFamilies
.ImplementationName
, "SwXStyleFamilies")
33 self
.assertEqual(len(xStyleFamilies
.SupportedServiceNames
), 1)
35 for servicename
in xStyleFamilies
.SupportedServiceNames
:
36 self
.assertIn(servicename
, ["com.sun.star.style.StyleFamilies"])
37 self
.assertTrue(xStyleFamilies
.supportsService(servicename
))
38 self
.assertFalse(xStyleFamilies
.supportsService("foobarbaz"))
39 self
.assertTrue(xStyleFamilies
.hasElements())
40 self
.assertRegex(str(xStyleFamilies
.ElementType
), r
"com\.sun\.star\.container\.XNameContainer")
41 self
.assertEqual(len(xStyleFamilies
.ElementNames
), 7)
43 for sFamilyname
in xStyleFamilies
.ElementNames
:
44 self
.assertIn(sFamilyname
,
45 ["CharacterStyles", "ParagraphStyles", "PageStyles", "FrameStyles", "NumberingStyles", "TableStyles", "CellStyles"])
47 with self
.assertRaises(NoSuchElementException
):
48 xStyleFamilies
.getByName("foobarbaz")
51 def __test_StyleFamily(self
, xFamily
, vExpectedNames
, sElementImplName
):
52 self
.assertEqual(xFamily
.ImplementationName
, "XStyleFamily")
53 self
.assertEqual(len(xFamily
.SupportedServiceNames
), 1)
55 for sServicename
in xFamily
.SupportedServiceNames
:
56 self
.assertIn(sServicename
, ["com.sun.star.style.StyleFamily"])
57 self
.assertTrue(xFamily
.supportsService(sServicename
))
58 self
.assertFalse(xFamily
.supportsService("foobarbaz"))
59 self
.assertTrue(xFamily
.hasElements())
60 self
.assertRegex(str(xFamily
.ElementType
), r
"com\.sun\.star\.style\.XStyle")
62 with self
.assertRaises(NoSuchElementException
):
63 xFamily
.getByName("foobarbaz")
65 with self
.assertRaises(IndexOutOfBoundsException
):
66 xFamily
.getByIndex(-1)
68 for sStylename
in xFamily
.ElementNames
:
69 self
.assertTrue(xFamily
.hasByName(sStylename
))
70 self
.assertEqual(xFamily
[sStylename
].ImplementationName
, sElementImplName
)
71 self
.assertFalse(xFamily
[sStylename
].isUserDefined())
74 vNames
= list(xFamily
.ElementNames
)
76 self
.assertListEqual(vNames
, vExpectedNames
)
78 def __test_StyleFamilyIndex(self
, xFamily
, vExpectedNames
, sElementImplName
):
79 self
.assertEqual(len(xFamily
), len(vExpectedNames
))
81 for nIndex
in range(len(xFamily
)):
82 xStyle
= xFamily
[nIndex
]
83 self
.assertEqual(xStyle
.ImplementationName
, sElementImplName
)
84 self
.assertIn(xStyle
.Name
, vExpectedNames
)
85 self
.assertFalse(xStyle
.isUserDefined())
87 def __test_StyleFamilyInsert(self
, xDoc
, xFamily
, vExpectedNames
, sRightStyle
, sWrongStyle
):
88 xRightStyle
= xDoc
.createInstance(sRightStyle
)
89 xRightStyle
.Name
= "RightStyleOld"
90 xWrongStyle
= xDoc
.createInstance(sWrongStyle
)
91 xWrongStyle
.Name
= "WrongtStyleOld"
92 xFamily
.insertByName("RightStyle", xRightStyle
)
93 self
.assertEqual(xRightStyle
.Name
, "RightStyle")
94 self
.assertTrue(xRightStyle
.isUserDefined())
95 self
.assertEqual(xFamily
[xRightStyle
.Name
], xRightStyle
)
96 self
.assertNotEqual(xFamily
[xRightStyle
.Name
], xWrongStyle
)
97 self
.assertTrue(xFamily
[xRightStyle
.Name
].isUserDefined())
98 xRightStyle2
= xDoc
.createInstance(sRightStyle
)
99 xRightStyle2
.Name
= "RightStyle2Old"
100 xFamily
.replaceByName("RightStyle", xRightStyle2
)
101 self
.assertEqual(xRightStyle2
.Name
, "RightStyle")
102 self
.assertEqual(xFamily
[xRightStyle2
.Name
], xRightStyle2
)
103 xFamily
.removeByName(xRightStyle2
.Name
)
105 with self
.assertRaises(NoSuchElementException
):
106 xFamily
.getByName("RightStyleOld")
107 with self
.assertRaises(NoSuchElementException
):
108 xFamily
.getByName("RightStyle")
110 with self
.assertRaises(NoSuchElementException
):
111 xFamily
.getByName("RightStyle2Old")
113 with self
.assertRaises(NoSuchElementException
):
114 xFamily
.getByName("RightStyle2")
116 with self
.assertRaises(IllegalArgumentException
):
117 xFamily
.insertByName("WrongStyle", xWrongStyle
)
119 with self
.assertRaises(NoSuchElementException
):
120 xFamily
.getByName("WrongStyle")
122 def test_CharacterFamily(self
):
123 xDoc
= CheckStyle
._uno
.openEmptyWriterDoc()
124 xCharStyles
= xDoc
.StyleFamilies
["CharacterStyles"]
125 vEmptyDocStyles
= ['Standard', 'Footnote Symbol', 'Page Number', 'Caption characters', 'Drop Caps', 'Numbering Symbols', 'Bullet Symbols', 'Internet link', 'Visited Internet Link', 'Placeholder', 'Index Link', 'Endnote Symbol', 'Line numbering', 'Main index entry', 'Footnote anchor', 'Endnote anchor', 'Rubies', 'Vertical Numbering Symbols', 'Emphasis', 'Citation', 'Strong Emphasis', 'Source Text', 'Example', 'User Entry', 'Variable', 'Definition', 'Teletype']
126 self
.__test
_StyleFamily
(xCharStyles
, vEmptyDocStyles
, "SwXStyle")
127 self
.__test
_StyleFamilyIndex
(xCharStyles
, vEmptyDocStyles
, "SwXStyle")
128 self
.__test
_StyleFamilyInsert
(xDoc
, xCharStyles
, vEmptyDocStyles
, "com.sun.star.style.CharacterStyle", "com.sun.star.style.ParagraphStyle")
131 def test_ParagraphFamily(self
):
132 xDoc
= CheckStyle
._uno
.openEmptyWriterDoc()
133 xParaStyles
= xDoc
.StyleFamilies
["ParagraphStyles"]
134 vEmptyDocStyles
= ['Standard', 'Heading', 'Text body', 'List', 'Caption', 'Index', 'First line indent', 'Hanging indent', 'Text body indent', 'Salutation', 'Signature', 'List Indent', 'Marginalia', 'Heading 1', 'Heading 2', 'Heading 3', 'Heading 4', 'Heading 5', 'Heading 6', 'Heading 7', 'Heading 8', 'Heading 9', 'Heading 10', 'Title', 'Subtitle', 'Appendix', 'Numbering 1 Start', 'Numbering 1', 'Numbering 1 End', 'Numbering 1 Cont.', 'Numbering 2 Start', 'Numbering 2', 'Numbering 2 End', 'Numbering 2 Cont.', 'Numbering 3 Start', 'Numbering 3', 'Numbering 3 End', 'Numbering 3 Cont.', 'Numbering 4 Start', 'Numbering 4', 'Numbering 4 End', 'Numbering 4 Cont.', 'Numbering 5 Start', 'Numbering 5', 'Numbering 5 End', 'Numbering 5 Cont.', 'List 1 Start', 'List 1', 'List 1 End', 'List 1 Cont.', 'List 2 Start', 'List 2', 'List 2 End', 'List 2 Cont.', 'List 3 Start', 'List 3', 'List 3 End', 'List 3 Cont.', 'List 4 Start', 'List 4', 'List 4 End', 'List 4 Cont.', 'List 5 Start', 'List 5', 'List 5 End', 'List 5 Cont.', 'Index Heading', 'Index 1', 'Index 2', 'Index 3', 'Index Separator', 'Contents Heading', 'Contents 1', 'Contents 2', 'Contents 3', 'Contents 4', 'Contents 5', 'User Index Heading', 'User Index 1', 'User Index 2', 'User Index 3', 'User Index 4', 'User Index 5', 'Contents 6', 'Contents 7', 'Contents 8', 'Contents 9', 'Contents 10', 'Figure Index Heading', 'Figure Index 1', 'Object index heading', 'Object index 1', 'Table index heading', 'Table index 1', 'Bibliography Heading', 'Bibliography 1', 'User Index 6', 'User Index 7', 'User Index 8', 'User Index 9', 'User Index 10', 'Header and Footer','Header', 'Header left', 'Header right', 'Footer', 'Footer left', 'Footer right', 'Table Contents', 'Table Heading', 'Illustration', 'Table', 'Text','Figure', 'Frame contents', 'Footnote', 'Addressee', 'Sender', 'Endnote', 'Drawing', 'Quotations', 'Preformatted Text', 'Horizontal Line', 'List Contents', 'List Heading']
135 self
.__test
_StyleFamily
(xParaStyles
, vEmptyDocStyles
, "SwXStyle")
136 self
.__test
_StyleFamilyIndex
(xParaStyles
, vEmptyDocStyles
, "SwXStyle")
137 self
.__test
_StyleFamilyInsert
(xDoc
, xParaStyles
, vEmptyDocStyles
, "com.sun.star.style.ParagraphStyle", "com.sun.star.style.CharacterStyle")
140 def test_PageFamily(self
):
141 xDoc
= CheckStyle
._uno
.openEmptyWriterDoc()
142 xPageStyles
= xDoc
.StyleFamilies
["PageStyles"]
143 vEmptyDocStyles
= ['Standard', 'First Page', 'Left Page', 'Right Page', 'Envelope', 'Index', 'HTML', 'Footnote', 'Endnote', 'Landscape']
144 self
.__test
_StyleFamily
(xPageStyles
, vEmptyDocStyles
, "SwXStyle")
145 self
.__test
_StyleFamilyIndex
(xPageStyles
, vEmptyDocStyles
, "SwXStyle")
146 self
.__test
_StyleFamilyInsert
(xDoc
, xPageStyles
, vEmptyDocStyles
, "com.sun.star.style.PageStyle", "com.sun.star.style.CharacterStyle")
149 def test_FrameFamily(self
):
150 xDoc
= CheckStyle
._uno
.openEmptyWriterDoc()
151 xFrameStyles
= xDoc
.StyleFamilies
["FrameStyles"]
152 vEmptyDocStyles
= ['Formula', 'Frame', 'Graphics', 'Labels', 'Marginalia', 'OLE', 'Watermark']
153 self
.__test
_StyleFamily
(xFrameStyles
, vEmptyDocStyles
, "SwXStyle")
154 self
.__test
_StyleFamilyIndex
(xFrameStyles
, vEmptyDocStyles
, "SwXStyle")
155 self
.__test
_StyleFamilyInsert
(xDoc
, xFrameStyles
, vEmptyDocStyles
, "com.sun.star.style.FrameStyle", "com.sun.star.style.CharacterStyle")
158 def test_NumberingFamily(self
):
159 xDoc
= CheckStyle
._uno
.openEmptyWriterDoc()
160 xNumberingStyles
= xDoc
.StyleFamilies
["NumberingStyles"]
161 vEmptyDocStyles
= ['No List','List 1', 'List 2', 'List 3', 'List 4', 'List 5', 'Numbering 123', 'Numbering ABC', 'Numbering abc', 'Numbering IVX', 'Numbering ivx']
162 self
.__test
_StyleFamily
(xNumberingStyles
, vEmptyDocStyles
, "SwXStyle")
163 self
.__test
_StyleFamilyIndex
(xNumberingStyles
, vEmptyDocStyles
, "SwXStyle")
164 self
.__test
_StyleFamilyInsert
(xDoc
, xNumberingStyles
, vEmptyDocStyles
, "com.sun.star.style.NumberingStyle", "com.sun.star.style.CharacterStyle")
167 def test_TableFamily(self
):
168 xDoc
= CheckStyle
._uno
.openEmptyWriterDoc()
169 xTableStyles
= xDoc
.StyleFamilies
["TableStyles"]
170 vEmptyDocStyles
= ['Default Style']
171 self
.__test
_StyleFamily
(xTableStyles
, vEmptyDocStyles
, "SwXTextTableStyle")
172 self
.__test
_StyleFamilyIndex
(xTableStyles
, vEmptyDocStyles
, "SwXTextTableStyle")
173 self
.__test
_StyleFamilyInsert
(xDoc
, xTableStyles
, vEmptyDocStyles
, "com.sun.star.style.TableStyle", "com.sun.star.style.CharacterStyle")
174 for sStyleName
in vEmptyDocStyles
:
175 self
.assertIsNotNone(xTableStyles
.getByName(sStyleName
))
176 #check SwXTextCellStyles
177 vCellStyles
= ["first-row", "last-row", "first-column", "last-column", "body", "even-rows", "odd-rows", "even-columns", "odd-columns", "background"]
178 xDefaultTableStyle
= xTableStyles
[0]
179 for sCellStyle
in vCellStyles
:
180 xCellStyle
= xDefaultTableStyle
.getByName(sCellStyle
)
181 self
.assertIsNotNone(xCellStyle
.getPropertyValue("BackColor"))
182 with self
.assertRaises(UnknownPropertyException
):
183 xCellStyle
.getPropertyValue("foobarbaz")
186 def test_tableStyleIsInUse(self
):
187 xDoc
= CheckStyle
._uno
.openEmptyWriterDoc()
188 xBodyText
= xDoc
.getText()
189 xCursor
= xBodyText
.createTextCursor()
190 xTable
= xDoc
.createInstance("com.sun.star.text.TextTable")
191 xTable
.initialize(1, 1)
192 xBodyText
.insertTextContent(xCursor
, xTable
, True)
193 xDefaultTableStyle
= xDoc
.StyleFamilies
.getByName("TableStyles").getByName("Default Style")
194 xDefaultCellStyle
= xDoc
.StyleFamilies
.getByName("CellStyles").getByName("Default Style.1")
195 self
.assertFalse(xDefaultTableStyle
.isInUse())
196 self
.assertFalse(xDefaultCellStyle
.isInUse())
197 xTable
.setPropertyValue("TableTemplateName", "Default Style")
198 self
.assertTrue(xDefaultTableStyle
.isInUse())
199 self
.assertTrue(xDefaultCellStyle
.isInUse())
200 xTable
.setPropertyValue("TableTemplateName", "")
201 self
.assertFalse(xDefaultTableStyle
.isInUse())
202 self
.assertFalse(xDefaultCellStyle
.isInUse())
203 xTableStyle
= xDoc
.createInstance("com.sun.star.style.TableStyle")
204 self
.assertFalse(xTableStyle
.isInUse())
205 xDoc
.StyleFamilies
.getByName("TableStyles").insertByName("Test Table Style", xTableStyle
)
206 self
.assertFalse(xTableStyle
.isInUse())
207 xTable
.setPropertyValue("TableTemplateName", "Test Table Style")
208 self
.assertTrue(xTableStyle
.isInUse())
209 xTable
.setPropertyValue("TableTemplateName", "")
210 self
.assertFalse(xTableStyle
.isInUse())
213 def test_CellFamily(self
):
214 xDoc
= CheckStyle
._uno
.openEmptyWriterDoc()
215 xCellStyles
= xDoc
.StyleFamilies
["CellStyles"]
216 vEmptyDocStyles
= ['Default Style.1', 'Default Style.2', 'Default Style.3', 'Default Style.4', 'Default Style.5', 'Default Style.6', 'Default Style.7', 'Default Style.8', 'Default Style.9', 'Default Style.10', 'Default Style.11', 'Default Style.12', 'Default Style.13', 'Default Style.14', 'Default Style.15', 'Default Style.16']
217 self
.__test
_StyleFamily
(xCellStyles
, vEmptyDocStyles
, "SwXTextCellStyle")
218 self
.__test
_StyleFamilyIndex
(xCellStyles
, vEmptyDocStyles
, "SwXTextCellStyle")
219 self
.__test
_StyleFamilyInsert
(xDoc
, xCellStyles
, vEmptyDocStyles
, "com.sun.star.style.CellStyle", "com.sun.star.style.CharacterStyle")
220 xTableStyle
= xDoc
.createInstance("com.sun.star.style.TableStyle")
221 xCellStyle
= xTableStyle
.getByName("first-row")
222 xDoc
.StyleFamilies
.getByName("TableStyles").insertByName("Test Table Style", xTableStyle
)
223 self
.assertEqual(xTableStyle
.getByName("first-row"), xCellStyle
)
224 self
.assertEqual(xTableStyle
.getByName("first-row"), xDoc
.StyleFamilies
.getByName("TableStyles").getByName("Test Table Style").getByName("first-row"))
226 with self
.assertRaises(NoSuchElementException
):
227 xTableStyle
.replaceByName("foobarbaz", xCellStyle
)
228 xCellStyle
= xDoc
.createInstance("com.sun.star.style.CellStyle")
229 with self
.assertRaises(IllegalArgumentException
): #replace with not inserted cell style
230 xTableStyle
.replaceByName("first-row", xCellStyle
)
231 xTableStyle2
= xDoc
.createInstance("com.sun.star.style.TableStyle")
232 with self
.assertRaises(IllegalArgumentException
): #replace with other family style
233 xTableStyle
.replaceByName("first-row", xTableStyle2
)
234 #replace with already assigned cell style
235 xTableStyle
.replaceByName("first-row", xTableStyle2
.getByName("first-row"))
236 self
.assertEqual(xTableStyle
.getByName("first-row"), xTableStyle2
.getByName("first-row"))
237 xDoc
.StyleFamilies
.getByName("CellStyles").insertByName("Test Cell Style", xCellStyle
)
238 xTableStyle
.replaceByName("first-row", xCellStyle
)
239 self
.assertEqual(xTableStyle
.getByName("first-row"), xCellStyle
)
242 if __name__
== '__main__':
245 # vim: set shiftwidth=4 softtabstop=4 expandtab: