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 .
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
):
39 cls
._uno
= UnoInProcess()
41 cls
.document
= cls
._uno
.openDocFromTDOC("CheckCrossReferences.odt")
43 cls
.xPortionEnum
= None
44 cls
.xFieldsRefresh
= None
47 def tearDownClass(cls
):
50 def getNextField(self
):
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):
62 for xPortionProps
in self
.xPortionEnum
:
63 sPortionType
= str(xPortionProps
.getPropertyValue("TextPortionType"))
64 if (sPortionType
== "TextField"):
65 xField
= xPortionProps
.getPropertyValue("TextField")
66 self
.assertTrue(xField
, "Cannot retrieve next field")
69 self
.xPortionEnum
= None
70 return None # unreachable
72 def getFieldProps(self
, xField
):
74 self
.assertTrue(xProps
, "Cannot retrieve field properties.")
77 def checkField(self
, xField
, xProps
, nFormat
, aExpectedFieldResult
):
78 # set requested format
79 xProps
.setPropertyValue("ReferenceFieldPart", int(nFormat
))
81 # refresh fields in order to get new format applied
82 self
.xFieldsRefresh
.refresh()
83 aFieldResult
= xField
.getPresentation(False)
84 self
.assertEqual(aExpectedFieldResult
, aFieldResult
, "set reference field format doesn't result in correct field result")
86 def test_checkCrossReferences(self
):
87 xParaEnumAccess
= self
.document
.getText()
88 self
.xParaEnum
= xParaEnumAccess
.createEnumeration()
91 xFieldSupp
= self
.__class
__.document
92 self
.xFieldsRefresh
= xFieldSupp
.getTextFields()
94 # check first reference field
95 # strings for checking
97 FieldResult2
= "+b+*i*"
98 FieldResult3
= "-1-+b+*i*"
102 FieldResult7
= "2(a)"
103 FieldResult8
= "2(b)"
105 FieldResult10
= "1(a)"
106 FieldResult11
= "(b)"
107 FieldResult12
= "(a)"
109 # variables for current field
110 xField
= self
.getNextField()
111 xProps
= self
.getFieldProps(xField
)
112 self
.checkField(xField
, xProps
, NUMBER
, FieldResult2
)
113 self
.checkField(xField
, xProps
, NUMBER_NO_CONTEXT
, FieldResult1
)
114 self
.checkField(xField
, xProps
, NUMBER_FULL_CONTEXT
, FieldResult3
)
116 xField
= self
.getNextField()
117 xProps
= self
.getFieldProps(xField
)
118 self
.checkField(xField
, xProps
, NUMBER
, FieldResult1
)
119 self
.checkField(xField
, xProps
, NUMBER_NO_CONTEXT
, FieldResult1
)
120 self
.checkField(xField
, xProps
, NUMBER_FULL_CONTEXT
, FieldResult3
)
122 xField
= self
.getNextField()
123 xProps
= self
.getFieldProps(xField
)
124 self
.checkField(xField
, xProps
, NUMBER
, FieldResult3
)
125 self
.checkField(xField
, xProps
, NUMBER_NO_CONTEXT
, FieldResult1
)
126 self
.checkField(xField
, xProps
, NUMBER_FULL_CONTEXT
, FieldResult3
)
128 xField
= self
.getNextField()
129 xProps
= self
.getFieldProps(xField
)
130 self
.checkField(xField
, xProps
, NUMBER
, FieldResult5
)
131 self
.checkField(xField
, xProps
, NUMBER_NO_CONTEXT
, FieldResult4
)
132 self
.checkField(xField
, xProps
, NUMBER_FULL_CONTEXT
, FieldResult6
)
134 xField
= self
.getNextField()
135 xProps
= self
.getFieldProps(xField
)
136 self
.checkField(xField
, xProps
, NUMBER
, FieldResult4
)
137 self
.checkField(xField
, xProps
, NUMBER_NO_CONTEXT
, FieldResult4
)
138 self
.checkField(xField
, xProps
, NUMBER_FULL_CONTEXT
, FieldResult6
)
140 xField
= self
.getNextField()
141 xProps
= self
.getFieldProps(xField
)
142 self
.checkField(xField
, xProps
, NUMBER
, FieldResult6
)
143 self
.checkField(xField
, xProps
, NUMBER_NO_CONTEXT
, FieldResult4
)
144 self
.checkField(xField
, xProps
, NUMBER_FULL_CONTEXT
, FieldResult6
)
146 xField
= self
.getNextField()
147 xProps
= self
.getFieldProps(xField
)
148 self
.checkField(xField
, xProps
, NUMBER
, FieldResult7
)
149 self
.checkField(xField
, xProps
, NUMBER_NO_CONTEXT
, FieldResult12
)
150 self
.checkField(xField
, xProps
, NUMBER_FULL_CONTEXT
, FieldResult7
)
151 xField
= self
.getNextField()
152 xProps
= self
.getFieldProps(xField
)
153 self
.checkField(xField
, xProps
, NUMBER
, FieldResult8
)
154 self
.checkField(xField
, xProps
, NUMBER_NO_CONTEXT
, FieldResult11
)
155 self
.checkField(xField
, xProps
, NUMBER_FULL_CONTEXT
, FieldResult8
)
157 xField
= self
.getNextField()
158 xProps
= self
.getFieldProps(xField
)
159 self
.checkField(xField
, xProps
, NUMBER
, FieldResult9
)
160 self
.checkField(xField
, xProps
, NUMBER_NO_CONTEXT
, FieldResult9
)
161 self
.checkField(xField
, xProps
, NUMBER_FULL_CONTEXT
, FieldResult9
)
163 xField
= self
.getNextField()
164 xProps
= self
.getFieldProps(xField
)
165 self
.checkField(xField
, xProps
, NUMBER
, FieldResult4
)
166 self
.checkField(xField
, xProps
, NUMBER_NO_CONTEXT
, FieldResult4
)
167 self
.checkField(xField
, xProps
, NUMBER_FULL_CONTEXT
, FieldResult4
)
169 xField
= self
.getNextField()
170 xProps
= self
.getFieldProps(xField
)
171 self
.checkField(xField
, xProps
, NUMBER
, FieldResult10
)
172 self
.checkField(xField
, xProps
, NUMBER_NO_CONTEXT
, FieldResult12
)
173 self
.checkField(xField
, xProps
, NUMBER_FULL_CONTEXT
, FieldResult10
)
175 xField
= self
.getNextField()
176 xProps
= self
.getFieldProps(xField
)
177 self
.checkField(xField
, xProps
, NUMBER
, FieldResult12
)
178 self
.checkField(xField
, xProps
, NUMBER_NO_CONTEXT
, FieldResult12
)
179 self
.checkField(xField
, xProps
, NUMBER_FULL_CONTEXT
, FieldResult7
)
181 # insert a certain cross-reference bookmark and a reference field to this bookmark
182 # restart paragraph enumeration
183 xParaEnumAccess
= self
.__class
__.document
.getText()
184 self
.xParaEnum
= xParaEnumAccess
.createEnumeration()
186 # iterate on the paragraphs to find certain paragraph to insert the bookmark
187 for xParaTextRange
in self
.xParaEnum
:
189 if xParaTextRange
.getString() == "J":
192 xParaTextRange
= None
194 self
.assertTrue(xParaTextRange
, "Cannot find paragraph to insert cross-reference bookmark")
197 xFac
= self
.__class
__.document
198 cBookmarkName
= "__RefNumPara__47114711"
199 xBookmark
= xFac
.createInstance("com.sun.star.text.Bookmark")
201 if xBookmark
is not None:
203 xName
.setName(cBookmarkName
)
204 xBookmark
.attach(xParaTextRange
.getStart())
206 # insert reference field, which references the inserted bookmark
207 xNewField
= xFac
.createInstance("com.sun.star.text.TextField.GetReference")
209 if xNewField
is not None:
210 xFieldProps
= xNewField
211 xFieldProps
.setPropertyValue("ReferenceFieldPart", int(TEXT
))
212 xFieldProps
.setPropertyValue("ReferenceFieldSource", int(BOOKMARK
))
213 xFieldProps
.setPropertyValue("SourceName", cBookmarkName
)
214 xFieldTextRange
= self
.xParaEnum
.nextElement()
215 xNewField
.attach(xFieldTextRange
.getEnd())
216 self
.xFieldsRefresh
.refresh()
218 # check inserted reference field
220 self
.assertEqual("J", xField
.getPresentation(False), "inserted reference field doesn't has correct field result")
222 xParaTextRange
.getStart().setString("Hallo new bookmark: ")
223 self
.xFieldsRefresh
.refresh()
224 self
.assertEqual("Hallo new bookmark: J", xField
.getPresentation(False), "inserted reference field doesn't has correct field result")
227 if __name__
== "__main__":