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/.
12 from org
.libreoffice
.unotest
import UnoInProcess
13 from com
.sun
.star
.container
import NoSuchElementException
14 from com
.sun
.star
.beans
import UnknownPropertyException
15 from com
.sun
.star
.lang
import IndexOutOfBoundsException
16 from com
.sun
.star
.lang
import IllegalArgumentException
19 class CheckStyle(unittest
.TestCase
):
23 cls
._uno
= UnoInProcess()
27 def tearDownClass(cls
):
30 def test_StyleFamilies(self
):
31 xDoc
= CheckStyle
._uno
.openEmptyWriterDoc()
32 xStyleFamilies
= xDoc
.StyleFamilies
33 self
.assertEqual(xStyleFamilies
.ImplementationName
, "SwXStyleFamilies")
34 self
.assertEqual(len(xStyleFamilies
.SupportedServiceNames
), 1)
36 for servicename
in xStyleFamilies
.SupportedServiceNames
:
37 self
.assertIn(servicename
, ["com.sun.star.style.StyleFamilies"])
38 self
.assertTrue(xStyleFamilies
.supportsService(servicename
))
39 self
.assertFalse(xStyleFamilies
.supportsService("foobarbaz"))
40 self
.assertTrue(xStyleFamilies
.hasElements())
41 self
.assertRegex(str(xStyleFamilies
.ElementType
), "com\.sun\.star\.container\.XNameContainer")
42 self
.assertEqual(len(xStyleFamilies
.ElementNames
), 7)
44 for sFamilyname
in xStyleFamilies
.ElementNames
:
45 self
.assertIn(sFamilyname
,
46 ["CharacterStyles", "ParagraphStyles", "PageStyles", "FrameStyles", "NumberingStyles", "TableStyles", "CellStyles"])
48 with self
.assertRaises(NoSuchElementException
):
49 xStyleFamilies
.getByName("foobarbaz")
52 def __test_StyleFamily(self
, xFamily
, vExpectedNames
, sElementImplName
):
53 self
.assertEqual(xFamily
.ImplementationName
, "XStyleFamily")
54 self
.assertEqual(len(xFamily
.SupportedServiceNames
), 1)
56 for sServicename
in xFamily
.SupportedServiceNames
:
57 self
.assertIn(sServicename
, ["com.sun.star.style.StyleFamily"])
58 self
.assertTrue(xFamily
.supportsService(sServicename
))
59 self
.assertFalse(xFamily
.supportsService("foobarbaz"))
60 self
.assertTrue(xFamily
.hasElements())
61 self
.assertRegex(str(xFamily
.ElementType
), "com\.sun\.star\.style\.XStyle")
63 with self
.assertRaises(NoSuchElementException
):
64 xFamily
.getByName("foobarbaz")
66 with self
.assertRaises(IndexOutOfBoundsException
):
67 xFamily
.getByIndex(-1)
69 for sStylename
in xFamily
.ElementNames
:
70 self
.assertTrue(xFamily
.hasByName(sStylename
))
71 self
.assertEqual(xFamily
[sStylename
].ImplementationName
, sElementImplName
)
72 self
.assertFalse(xFamily
[sStylename
].isUserDefined())
75 vNames
= list(xFamily
.ElementNames
)
77 self
.assertListEqual(vNames
, vExpectedNames
)
79 def __test_StyleFamilyIndex(self
, xFamily
, vExpectedNames
, sElementImplName
):
80 self
.assertEqual(xFamily
.Count
, len(vExpectedNames
))
82 for nIndex
in range(xFamily
.Count
):
83 xStyle
= xFamily
.getByIndex(nIndex
)
84 self
.assertEqual(xStyle
.ImplementationName
, sElementImplName
)
85 self
.assertIn(xStyle
.Name
, vExpectedNames
)
86 self
.assertFalse(xStyle
.isUserDefined())
88 def __test_StyleFamilyInsert(self
, xDoc
, xFamily
, vExpectedNames
, sRightStyle
, sWrongStyle
):
89 xRightStyle
= xDoc
.createInstance(sRightStyle
)
90 xRightStyle
.Name
= "RightStyleOld"
91 xWrongStyle
= xDoc
.createInstance(sWrongStyle
)
92 xWrongStyle
.Name
= "WrongtStyleOld"
93 xFamily
.insertByName("RightStyle", xRightStyle
)
94 self
.assertEqual(xRightStyle
.Name
, "RightStyle")
95 self
.assertTrue(xRightStyle
.isUserDefined())
96 self
.assertEqual(xFamily
[xRightStyle
.Name
], xRightStyle
)
97 self
.assertNotEqual(xFamily
[xRightStyle
.Name
], xWrongStyle
)
98 self
.assertTrue(xFamily
[xRightStyle
.Name
].isUserDefined())
99 xRightStyle2
= xDoc
.createInstance(sRightStyle
)
100 xRightStyle2
.Name
= "RightStyle2Old"
101 xFamily
.replaceByName("RightStyle", xRightStyle2
)
102 self
.assertEqual(xRightStyle2
.Name
, "RightStyle")
103 self
.assertEqual(xFamily
[xRightStyle2
.Name
], xRightStyle2
)
104 xFamily
.removeByName(xRightStyle2
.Name
)
106 with self
.assertRaises(NoSuchElementException
):
107 nope
= xFamily
.getByName("RightStyleOld")
108 with self
.assertRaises(NoSuchElementException
):
109 nope
= xFamily
.getByName("RightStyle")
111 with self
.assertRaises(NoSuchElementException
):
112 nope
= xFamily
.getByName("RightStyle2Old")
114 with self
.assertRaises(NoSuchElementException
):
115 nope
= xFamily
.getByName("RightStyle2")
117 with self
.assertRaises(IllegalArgumentException
):
118 xFamily
.insertByName("WrongStyle", xWrongStyle
)
120 with self
.assertRaises(NoSuchElementException
):
121 nope
= xFamily
.getByName("WrongStyle")
123 def test_CharacterFamily(self
):
124 xDoc
= CheckStyle
._uno
.openEmptyWriterDoc()
125 xCharStyles
= xDoc
.StyleFamilies
["CharacterStyles"]
126 vEmptyDocStyles
= ['Default Style', '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']
127 self
.__test
_StyleFamily
(xCharStyles
, vEmptyDocStyles
, "SwXStyle")
128 self
.__test
_StyleFamilyIndex
(xCharStyles
, vEmptyDocStyles
, "SwXStyle")
129 self
.__test
_StyleFamilyInsert
(xDoc
, xCharStyles
, vEmptyDocStyles
, "com.sun.star.style.CharacterStyle", "com.sun.star.style.ParagraphStyle")
132 def test_ParagraphFamily(self
):
133 xDoc
= CheckStyle
._uno
.openEmptyWriterDoc()
134 xParaStyles
= xDoc
.StyleFamilies
["ParagraphStyles"]
135 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', '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', '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']
136 self
.__test
_StyleFamily
(xParaStyles
, vEmptyDocStyles
, "SwXStyle")
137 self
.__test
_StyleFamilyIndex
(xParaStyles
, vEmptyDocStyles
, "SwXStyle")
138 self
.__test
_StyleFamilyInsert
(xDoc
, xParaStyles
, vEmptyDocStyles
, "com.sun.star.style.ParagraphStyle", "com.sun.star.style.CharacterStyle")
141 def test_PageFamily(self
):
142 xDoc
= CheckStyle
._uno
.openEmptyWriterDoc()
143 xPageStyles
= xDoc
.StyleFamilies
["PageStyles"]
144 vEmptyDocStyles
= ['Standard', 'First Page', 'Left Page', 'Right Page', 'Envelope', 'Index', 'HTML', 'Footnote', 'Endnote', 'Landscape']
145 self
.__test
_StyleFamily
(xPageStyles
, vEmptyDocStyles
, "SwXStyle")
146 self
.__test
_StyleFamilyIndex
(xPageStyles
, vEmptyDocStyles
, "SwXStyle")
147 self
.__test
_StyleFamilyInsert
(xDoc
, xPageStyles
, vEmptyDocStyles
, "com.sun.star.style.PageStyle", "com.sun.star.style.CharacterStyle")
150 def test_FrameFamily(self
):
151 xDoc
= CheckStyle
._uno
.openEmptyWriterDoc()
152 xFrameStyles
= xDoc
.StyleFamilies
["FrameStyles"]
153 vEmptyDocStyles
= ['Formula', 'Frame', 'Graphics', 'Labels', 'Marginalia', 'OLE', 'Watermark']
154 self
.__test
_StyleFamily
(xFrameStyles
, vEmptyDocStyles
, "SwXStyle")
155 self
.__test
_StyleFamilyIndex
(xFrameStyles
, vEmptyDocStyles
, "SwXStyle")
156 self
.__test
_StyleFamilyInsert
(xDoc
, xFrameStyles
, vEmptyDocStyles
, "com.sun.star.style.FrameStyle", "com.sun.star.style.CharacterStyle")
159 def test_NumberingFamily(self
):
160 xDoc
= CheckStyle
._uno
.openEmptyWriterDoc()
161 xNumberingStyles
= xDoc
.StyleFamilies
["NumberingStyles"]
162 vEmptyDocStyles
= ['List 1', 'List 2', 'List 3', 'List 4', 'List 5', 'Numbering 123', 'Numbering ABC', 'Numbering abc', 'Numbering IVX', 'Numbering ivx']
163 self
.__test
_StyleFamily
(xNumberingStyles
, vEmptyDocStyles
, "SwXStyle")
164 self
.__test
_StyleFamilyIndex
(xNumberingStyles
, vEmptyDocStyles
, "SwXStyle")
165 self
.__test
_StyleFamilyInsert
(xDoc
, xNumberingStyles
, vEmptyDocStyles
, "com.sun.star.style.NumberingStyle", "com.sun.star.style.CharacterStyle")
168 def test_TableFamily(self
):
169 xDoc
= CheckStyle
._uno
.openEmptyWriterDoc()
170 xTableStyles
= xDoc
.StyleFamilies
["TableStyles"]
171 vEmptyDocStyles
= ['Default Style']
172 self
.__test
_StyleFamily
(xTableStyles
, vEmptyDocStyles
, "SwXTextTableStyle")
173 self
.__test
_StyleFamilyIndex
(xTableStyles
, vEmptyDocStyles
, "SwXTextTableStyle")
174 self
.__test
_StyleFamilyInsert
(xDoc
, xTableStyles
, vEmptyDocStyles
, "com.sun.star.style.TableStyle", "com.sun.star.style.CharacterStyle")
175 for sStyleName
in vEmptyDocStyles
:
176 self
.assertIsNotNone(xTableStyles
.getByName(sStyleName
))
177 #check SwXTextCellStyles
178 vCellStyles
= ["first-row", "last-row", "first-column", "last-column", "body", "even-rows", "odd-rows", "even-columns", "odd-columns", "background"]
179 xDefaultTableStyle
= xTableStyles
.getByIndex(0)
180 for sCellStyle
in vCellStyles
:
181 xCellStyle
= xDefaultTableStyle
.getByName(sCellStyle
)
182 self
.assertIsNotNone(xCellStyle
.getPropertyValue("BackColor"))
183 with self
.assertRaises(UnknownPropertyException
):
184 xCellStyle
.getPropertyValue("foobarbaz")
187 def test_tableStyleIsInUse(self
):
188 xDoc
= CheckStyle
._uno
.openEmptyWriterDoc()
189 xBodyText
= xDoc
.getText()
190 xCursor
= xBodyText
.createTextCursor()
191 xTable
= xDoc
.createInstance("com.sun.star.text.TextTable")
192 xTable
.initialize(1, 1)
193 xBodyText
.insertTextContent(xCursor
, xTable
, True)
194 xDefaultTableStyle
= xDoc
.StyleFamilies
.getByName("TableStyles").getByName("Default Style")
195 xDefaultCellStyle
= xDoc
.StyleFamilies
.getByName("CellStyles").getByName("Default Style.1")
196 self
.assertFalse(xDefaultTableStyle
.isInUse())
197 self
.assertFalse(xDefaultCellStyle
.isInUse())
198 xTable
.setPropertyValue("TableTemplateName", "Default Style")
199 self
.assertTrue(xDefaultTableStyle
.isInUse())
200 self
.assertTrue(xDefaultCellStyle
.isInUse())
201 xTable
.setPropertyValue("TableTemplateName", "")
202 self
.assertFalse(xDefaultTableStyle
.isInUse())
203 self
.assertFalse(xDefaultCellStyle
.isInUse())
204 xTableStyle
= xDoc
.createInstance("com.sun.star.style.TableStyle")
205 self
.assertFalse(xTableStyle
.isInUse())
206 xDoc
.StyleFamilies
.getByName("TableStyles").insertByName("Test Table Style", xTableStyle
)
207 self
.assertFalse(xTableStyle
.isInUse())
208 xTable
.setPropertyValue("TableTemplateName", "Test Table Style")
209 self
.assertTrue(xTableStyle
.isInUse())
210 xTable
.setPropertyValue("TableTemplateName", "")
211 self
.assertFalse(xTableStyle
.isInUse())
214 def test_CellFamily(self
):
215 xDoc
= CheckStyle
._uno
.openEmptyWriterDoc()
216 xCellStyles
= xDoc
.StyleFamilies
["CellStyles"]
217 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']
218 self
.__test
_StyleFamily
(xCellStyles
, vEmptyDocStyles
, "SwXTextCellStyle")
219 self
.__test
_StyleFamilyIndex
(xCellStyles
, vEmptyDocStyles
, "SwXTextCellStyle")
220 self
.__test
_StyleFamilyInsert
(xDoc
, xCellStyles
, vEmptyDocStyles
, "com.sun.star.style.CellStyle", "com.sun.star.style.CharacterStyle")
221 xTableStyle
= xDoc
.createInstance("com.sun.star.style.TableStyle")
222 xCellStyle
= xTableStyle
.getByName("first-row")
223 xDoc
.StyleFamilies
.getByName("TableStyles").insertByName("Test Table Style", xTableStyle
)
224 self
.assertEqual(xTableStyle
.getByName("first-row"), xCellStyle
)
225 self
.assertEqual(xTableStyle
.getByName("first-row"), xDoc
.StyleFamilies
.getByName("TableStyles").getByName("Test Table Style").getByName("first-row"))
227 with self
.assertRaises(NoSuchElementException
):
228 xTableStyle
.replaceByName("foobarbaz", xCellStyle
)
229 xCellStyle
= xDoc
.createInstance("com.sun.star.style.CellStyle")
230 with self
.assertRaises(IllegalArgumentException
): #replace with not inserted cell style
231 xTableStyle
.replaceByName("first-row", xCellStyle
)
232 xTableStyle2
= xDoc
.createInstance("com.sun.star.style.TableStyle")
233 with self
.assertRaises(IllegalArgumentException
): #replace with other family style
234 xTableStyle
.replaceByName("first-row", xTableStyle2
)
235 #replace with already assigned cell style
236 xTableStyle
.replaceByName("first-row", xTableStyle2
.getByName("first-row"))
237 self
.assertEqual(xTableStyle
.getByName("first-row"), xTableStyle2
.getByName("first-row"))
238 xDoc
.StyleFamilies
.getByName("CellStyles").insertByName("Test Cell Style", xCellStyle
)
239 xTableStyle
.replaceByName("first-row", xCellStyle
)
240 self
.assertEqual(xTableStyle
.getByName("first-row"), xCellStyle
)
243 if __name__
== '__main__':
246 # vim: set shiftwidth=4 softtabstop=4 expandtab: