Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / sw / qa / python / check_cross_references.py
blob2246118bed3139de5fa3bc82a55b0ead5f291022
1 '''
2 This file is part of the LibreOffice project.
4 This Source Code Form is subject to the terms of the Mozilla Public
5 License, v. 2.0. If a copy of the MPL was not distributed with this
6 file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 This file incorporates work covered by the following license notice:
10 Licensed to the Apache Software Foundation (ASF) under one or more
11 contributor license agreements. See the NOTICE file distributed
12 with this work for additional information regarding copyright
13 ownership. The ASF licenses this file to you under the Apache
14 License, Version 2.0 (the "License"); you may not use this file
15 except in compliance with the License. You may obtain a copy of
16 the License at http://www.apache.org/licenses/LICENSE-2.0 .
17 '''
18 import unittest
19 import unohelper
20 import os
21 from com.sun.star.lang import XMultiServiceFactory
22 from com.sun.star.text import XTextDocument
23 from com.sun.star.text import XTextField
24 from com.sun.star.container import XEnumeration
25 from com.sun.star.util import XRefreshable
26 from com.sun.star.container import XEnumerationAccess
27 from com.sun.star.beans import XPropertySet
28 from com.sun.star.text import XTextFieldsSupplier
29 from com.sun.star.container import XNamed
30 from com.sun.star.text.ReferenceFieldPart import (NUMBER, NUMBER_NO_CONTEXT, NUMBER_FULL_CONTEXT, TEXT)
31 from com.sun.star.text.ReferenceFieldSource import BOOKMARK
32 from org.libreoffice.unotest import UnoInProcess
35 class CheckCrossReferences(unittest.TestCase):
37 @classmethod
38 def setUpClass(cls):
39 cls._uno = UnoInProcess()
40 cls._uno.setUp()
41 cls.document = cls._uno.openWriterTemplateDoc("CheckCrossReferences.odt")
42 cls.xParaEnum = None
43 cls.xPortionEnum = None
44 cls.xFieldsRefresh = None
46 @classmethod
47 def tearDownClass(cls):
48 cls._uno.tearDown()
50 def getNextField(self):
51 while True:
52 while self.xPortionEnum is None:
53 if (not(self.xParaEnum.hasMoreElements())):
54 self.fail("Cannot retrieve next field.")
56 aPara = self.xParaEnum.nextElement()
57 self.xPortionEnum = aPara.createEnumeration()
59 if (self.xPortionEnum is None):
60 break
62 while self.xPortionEnum.hasMoreElements():
63 xPortionProps = self.xPortionEnum.nextElement()
64 sPortionType = str(xPortionProps.getPropertyValue("TextPortionType"))
65 if (sPortionType == "TextField"):
66 xField = xPortionProps.getPropertyValue("TextField")
67 self.assertTrue(xField, "Cannot retrieve next field")
68 return xField
70 self.xPortionEnum = None
71 return None # unreachable
73 def getFieldProps(self, xField):
74 xProps = xField
75 self.assertTrue(xProps, "Cannot retrieve field properties.")
76 return xProps
78 def checkField(self, xField, xProps, nFormat, aExpectedFieldResult):
79 # set requested format
80 xProps.setPropertyValue("ReferenceFieldPart", int(nFormat))
82 # refresh fields in order to get new format applied
83 self.xFieldsRefresh.refresh()
84 aFieldResult = xField.getPresentation(False)
85 self.assertEqual(aExpectedFieldResult, aFieldResult, "set reference field format doesn't result in correct field result")
87 def test_checkCrossReferences(self):
88 xParaEnumAccess = self.document.getText()
89 self.xParaEnum = xParaEnumAccess.createEnumeration()
91 # get field refresher
92 xFieldSupp = self.__class__.document
93 self.xFieldsRefresh = xFieldSupp.getTextFields()
95 # check first reference field
96 # strings for checking
97 FieldResult1 = "*i*"
98 FieldResult2 = "+b+*i*"
99 FieldResult3 = "-1-+b+*i*"
100 FieldResult4 = "1"
101 FieldResult5 = "1"
102 FieldResult6 = "A.1"
103 FieldResult7 = "2(a)"
104 FieldResult8 = "2(b)"
105 FieldResult9 = "2"
106 FieldResult10 = "1(a)"
107 FieldResult11 = "(b)"
108 FieldResult12 = "(a)"
110 # variables for current field
111 xField = self.getNextField()
112 xProps = self.getFieldProps(xField)
113 self.checkField(xField, xProps, NUMBER, FieldResult2)
114 self.checkField(xField, xProps, NUMBER_NO_CONTEXT, FieldResult1)
115 self.checkField(xField, xProps, NUMBER_FULL_CONTEXT, FieldResult3)
117 xField = self.getNextField()
118 xProps = self.getFieldProps(xField)
119 self.checkField(xField, xProps, NUMBER, FieldResult1)
120 self.checkField(xField, xProps, NUMBER_NO_CONTEXT, FieldResult1)
121 self.checkField(xField, xProps, NUMBER_FULL_CONTEXT, FieldResult3)
123 xField = self.getNextField()
124 xProps = self.getFieldProps(xField)
125 self.checkField(xField, xProps, NUMBER, FieldResult3)
126 self.checkField(xField, xProps, NUMBER_NO_CONTEXT, FieldResult1)
127 self.checkField(xField, xProps, NUMBER_FULL_CONTEXT, FieldResult3)
129 xField = self.getNextField()
130 xProps = self.getFieldProps(xField)
131 self.checkField(xField, xProps, NUMBER, FieldResult5)
132 self.checkField(xField, xProps, NUMBER_NO_CONTEXT, FieldResult4)
133 self.checkField(xField, xProps, NUMBER_FULL_CONTEXT, FieldResult6)
135 xField = self.getNextField()
136 xProps = self.getFieldProps(xField)
137 self.checkField(xField, xProps, NUMBER, FieldResult4)
138 self.checkField(xField, xProps, NUMBER_NO_CONTEXT, FieldResult4)
139 self.checkField(xField, xProps, NUMBER_FULL_CONTEXT, FieldResult6)
141 xField = self.getNextField()
142 xProps = self.getFieldProps(xField)
143 self.checkField(xField, xProps, NUMBER, FieldResult6)
144 self.checkField(xField, xProps, NUMBER_NO_CONTEXT, FieldResult4)
145 self.checkField(xField, xProps, NUMBER_FULL_CONTEXT, FieldResult6)
147 xField = self.getNextField()
148 xProps = self.getFieldProps(xField)
149 self.checkField(xField, xProps, NUMBER, FieldResult7)
150 self.checkField(xField, xProps, NUMBER_NO_CONTEXT, FieldResult12)
151 self.checkField(xField, xProps, NUMBER_FULL_CONTEXT, FieldResult7)
152 xField = self.getNextField()
153 xProps = self.getFieldProps(xField)
154 self.checkField(xField, xProps, NUMBER, FieldResult8)
155 self.checkField(xField, xProps, NUMBER_NO_CONTEXT, FieldResult11)
156 self.checkField(xField, xProps, NUMBER_FULL_CONTEXT, FieldResult8)
158 xField = self.getNextField()
159 xProps = self.getFieldProps(xField)
160 self.checkField(xField, xProps, NUMBER, FieldResult9)
161 self.checkField(xField, xProps, NUMBER_NO_CONTEXT, FieldResult9)
162 self.checkField(xField, xProps, NUMBER_FULL_CONTEXT, FieldResult9)
164 xField = self.getNextField()
165 xProps = self.getFieldProps(xField)
166 self.checkField(xField, xProps, NUMBER, FieldResult4)
167 self.checkField(xField, xProps, NUMBER_NO_CONTEXT, FieldResult4)
168 self.checkField(xField, xProps, NUMBER_FULL_CONTEXT, FieldResult4)
170 xField = self.getNextField()
171 xProps = self.getFieldProps(xField)
172 self.checkField(xField, xProps, NUMBER, FieldResult10)
173 self.checkField(xField, xProps, NUMBER_NO_CONTEXT, FieldResult12)
174 self.checkField(xField, xProps, NUMBER_FULL_CONTEXT, FieldResult10)
176 xField = self.getNextField()
177 xProps = self.getFieldProps(xField)
178 self.checkField(xField, xProps, NUMBER, FieldResult12)
179 self.checkField(xField, xProps, NUMBER_NO_CONTEXT, FieldResult12)
180 self.checkField(xField, xProps, NUMBER_FULL_CONTEXT, FieldResult7)
182 # insert a certain cross-reference bookmark and a reference field to this bookmark
183 # restart paragraph enumeration
184 xParaEnumAccess = self.__class__.document.getText()
185 self.xParaEnum = xParaEnumAccess.createEnumeration()
187 # iterate on the paragraphs to find certain paragraph to insert the bookmark
188 while self.xParaEnum.hasMoreElements():
189 xParaTextRange = self.xParaEnum.nextElement()
191 if xParaTextRange.getString() == "J":
192 break
193 else:
194 xParaTextRange = None
196 self.assertTrue(xParaTextRange, "Cannot find paragraph to insert cross-reference bookmark")
198 # insert bookmark
199 xFac = self.__class__.document
200 cBookmarkName = "__RefNumPara__47114711"
201 xBookmark = xFac.createInstance("com.sun.star.text.Bookmark")
203 if xBookmark is not None:
204 xName = xBookmark
205 xName.setName(cBookmarkName)
206 xBookmark.attach(xParaTextRange.getStart())
208 # insert reference field, which references the inserted bookmark
209 xNewField = xFac.createInstance("com.sun.star.text.TextField.GetReference")
211 if xNewField is not None:
212 xFieldProps = xNewField
213 xFieldProps.setPropertyValue("ReferenceFieldPart", int(TEXT))
214 xFieldProps.setPropertyValue("ReferenceFieldSource", int(BOOKMARK))
215 xFieldProps.setPropertyValue("SourceName", cBookmarkName)
216 xFieldTextRange = self.xParaEnum.nextElement()
217 xNewField.attach(xFieldTextRange.getEnd())
218 self.xFieldsRefresh.refresh()
220 # check inserted reference field
221 xField = xNewField
222 self.assertEqual("J", xField.getPresentation(False), "inserted reference field doesn't has correct field result")
224 xParaTextRange.getStart().setString("Hallo new bookmark: ")
225 self.xFieldsRefresh.refresh()
226 self.assertEqual("Hallo new bookmark: J", xField.getPresentation(False), "inserted reference field doesn't has correct field result")
229 if __name__ == "__main__":
230 unittest.main()