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 .
19 from com
.sun
.star
.text
.ReferenceFieldPart
import (NUMBER
, NUMBER_NO_CONTEXT
, NUMBER_FULL_CONTEXT
, TEXT
)
20 from com
.sun
.star
.text
.ReferenceFieldSource
import BOOKMARK
21 from org
.libreoffice
.unotest
import UnoInProcess
24 class CheckCrossReferences(unittest
.TestCase
):
28 cls
._uno
= UnoInProcess()
30 cls
.document
= cls
._uno
.openDocFromTDOC("CheckCrossReferences.odt")
32 cls
.xPortionEnum
= None
33 cls
.xFieldsRefresh
= None
36 def tearDownClass(cls
):
38 # HACK in case cls.document holds a UNO proxy to an SwXTextDocument (whose dtor calls
39 # Application::GetSolarMutex via sw::UnoImplPtrDeleter), which would potentially only be
40 # garbage-collected after VCL has already been deinitialized:
43 def getNextField(self
):
45 while self
.xPortionEnum
is None:
46 if (not(self
.xParaEnum
.hasMoreElements())):
47 self
.fail("Cannot retrieve next field.")
49 aPara
= self
.xParaEnum
.nextElement()
50 self
.xPortionEnum
= aPara
.createEnumeration()
52 if (self
.xPortionEnum
is None):
55 for xPortionProps
in self
.xPortionEnum
:
56 sPortionType
= str(xPortionProps
.getPropertyValue("TextPortionType"))
57 if (sPortionType
== "TextField"):
58 xField
= xPortionProps
.getPropertyValue("TextField")
59 self
.assertTrue(xField
, "Cannot retrieve next field")
62 self
.xPortionEnum
= None
63 return None # unreachable
65 def getFieldProps(self
, xField
):
67 self
.assertTrue(xProps
, "Cannot retrieve field properties.")
70 def checkField(self
, xField
, xProps
, nFormat
, aExpectedFieldResult
):
71 # set requested format
72 xProps
.setPropertyValue("ReferenceFieldPart", int(nFormat
))
74 # refresh fields in order to get new format applied
75 self
.xFieldsRefresh
.refresh()
76 aFieldResult
= xField
.getPresentation(False)
77 self
.assertEqual(aExpectedFieldResult
, aFieldResult
, "set reference field format doesn't result in correct field result")
79 def test_checkCrossReferences(self
):
80 xParaEnumAccess
= self
.document
.getText()
81 self
.xParaEnum
= xParaEnumAccess
.createEnumeration()
84 xFieldSupp
= self
.__class
__.document
85 self
.xFieldsRefresh
= xFieldSupp
.getTextFields()
87 # check first reference field
88 # strings for checking
90 FieldResult2
= "+b+*i*"
91 FieldResult3
= "-1-+b+*i*"
95 FieldResult7
= " 2.(a)"
96 FieldResult8
= " 2.(b)"
98 FieldResult10
= " 1.(a)"
100 FieldResult12
= "(a)"
103 # variables for current field
104 xField
= self
.getNextField()
105 xProps
= self
.getFieldProps(xField
)
106 self
.checkField(xField
, xProps
, NUMBER
, FieldResult2
)
107 self
.checkField(xField
, xProps
, NUMBER_NO_CONTEXT
, FieldResult1
)
108 self
.checkField(xField
, xProps
, NUMBER_FULL_CONTEXT
, FieldResult3
)
110 xField
= self
.getNextField()
111 xProps
= self
.getFieldProps(xField
)
112 self
.checkField(xField
, xProps
, NUMBER
, FieldResult1
)
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
, FieldResult3
)
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
, FieldResult5
)
125 self
.checkField(xField
, xProps
, NUMBER_NO_CONTEXT
, FieldResult4
)
126 self
.checkField(xField
, xProps
, NUMBER_FULL_CONTEXT
, FieldResult6
)
128 xField
= self
.getNextField()
129 xProps
= self
.getFieldProps(xField
)
130 self
.checkField(xField
, xProps
, NUMBER
, FieldResult4
)
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
, FieldResult6
)
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
, FieldResult7
)
143 self
.checkField(xField
, xProps
, NUMBER_NO_CONTEXT
, FieldResult12
)
144 self
.checkField(xField
, xProps
, NUMBER_FULL_CONTEXT
, FieldResult7
)
145 xField
= self
.getNextField()
146 xProps
= self
.getFieldProps(xField
)
147 self
.checkField(xField
, xProps
, NUMBER
, FieldResult8
)
148 self
.checkField(xField
, xProps
, NUMBER_NO_CONTEXT
, FieldResult11
)
149 self
.checkField(xField
, xProps
, NUMBER_FULL_CONTEXT
, FieldResult8
)
151 xField
= self
.getNextField()
152 xProps
= self
.getFieldProps(xField
)
153 self
.checkField(xField
, xProps
, NUMBER
, FieldResult9
)
154 self
.checkField(xField
, xProps
, NUMBER_NO_CONTEXT
, FieldResult9
)
155 self
.checkField(xField
, xProps
, NUMBER_FULL_CONTEXT
, FieldResult9
)
157 xField
= self
.getNextField()
158 xProps
= self
.getFieldProps(xField
)
159 self
.checkField(xField
, xProps
, NUMBER
, FieldResult13
)
160 self
.checkField(xField
, xProps
, NUMBER_NO_CONTEXT
, FieldResult13
)
161 self
.checkField(xField
, xProps
, NUMBER_FULL_CONTEXT
, FieldResult13
)
163 xField
= self
.getNextField()
164 xProps
= self
.getFieldProps(xField
)
165 self
.checkField(xField
, xProps
, NUMBER
, FieldResult10
)
166 self
.checkField(xField
, xProps
, NUMBER_NO_CONTEXT
, FieldResult12
)
167 self
.checkField(xField
, xProps
, NUMBER_FULL_CONTEXT
, FieldResult10
)
169 xField
= self
.getNextField()
170 xProps
= self
.getFieldProps(xField
)
171 self
.checkField(xField
, xProps
, NUMBER
, FieldResult12
)
172 self
.checkField(xField
, xProps
, NUMBER_NO_CONTEXT
, FieldResult12
)
173 self
.checkField(xField
, xProps
, NUMBER_FULL_CONTEXT
, FieldResult7
)
175 # insert a certain cross-reference bookmark and a reference field to this bookmark
176 # restart paragraph enumeration
177 xParaEnumAccess
= self
.__class
__.document
.getText()
178 self
.xParaEnum
= xParaEnumAccess
.createEnumeration()
180 # iterate on the paragraphs to find certain paragraph to insert the bookmark
181 for xParaTextRange
in self
.xParaEnum
:
183 if xParaTextRange
.getString() == "J":
186 xParaTextRange
= None
188 self
.assertTrue(xParaTextRange
, "Cannot find paragraph to insert cross-reference bookmark")
191 xFac
= self
.__class
__.document
192 cBookmarkName
= "__RefNumPara__47114711"
193 xBookmark
= xFac
.createInstance("com.sun.star.text.Bookmark")
195 if xBookmark
is not None:
197 xName
.setName(cBookmarkName
)
198 xBookmark
.attach(xParaTextRange
.getStart())
200 # insert reference field, which references the inserted bookmark
201 xNewField
= xFac
.createInstance("com.sun.star.text.TextField.GetReference")
203 if xNewField
is not None:
204 xFieldProps
= xNewField
205 xFieldProps
.setPropertyValue("ReferenceFieldPart", int(TEXT
))
206 xFieldProps
.setPropertyValue("ReferenceFieldSource", int(BOOKMARK
))
207 xFieldProps
.setPropertyValue("SourceName", cBookmarkName
)
208 xFieldTextRange
= self
.xParaEnum
.nextElement()
209 xNewField
.attach(xFieldTextRange
.getEnd())
210 self
.xFieldsRefresh
.refresh()
212 # check inserted reference field
214 self
.assertEqual("J", xField
.getPresentation(False), "inserted reference field doesn't has correct field result")
216 xParaTextRange
.getStart().setString("Hallo new bookmark: ")
217 self
.xFieldsRefresh
.refresh()
218 self
.assertEqual("Hallo new bookmark: J", xField
.getPresentation(False), "inserted reference field doesn't has correct field result")
221 if __name__
== "__main__":