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
.text
.ControlCharacter
import PARAGRAPH_BREAK
16 class TestVarFields(unittest
.TestCase
):
20 cls
._uno
= UnoInProcess()
22 cls
._xDoc
= cls
._uno
.openEmptyWriterDoc()
25 def tearDownClass(cls
):
28 def test_var_fields(self
):
29 """Reproduce fdo#55814.
31 Note: this test was migrated from java (the steps numbering too)
32 sw/qa/complex/writer/VarFields.java
35 xDoc
= self
.__class
__._xDoc
36 xBodyText
= xDoc
.getText()
37 xCursor
= xBodyText
.createTextCursor()
38 # 0. create text field
39 xField
= xDoc
.createInstance("com.sun.star.text.textfield.SetExpression")
40 # 1. fill it with properties
41 self
.__class
__._uno
.setProperties(xField
,
44 "Hint": "trying to reproduce fdo#55814",
47 # 2. create master field
48 xMaster
= xDoc
.createInstance("com.sun.star.text.fieldmaster.SetExpression")
49 # 3. set name of the master field to "foo"
50 xMaster
.setPropertyValue("Name", "foo")
51 # 4. get Dependent Field
53 # 5. connect real field to the master
54 xField
.attachTextFieldMaster(xMaster
)
55 # 6. insert text field into the document
56 xBodyText
.insertTextContent(xCursor
, xField
, False)
57 # 7. retrieve paragraph cursor
58 xParagraphCursor
= xCursor
59 xParagraphCursor
.gotoEndOfParagraph(False) # not selectd
61 xBodyText
.insertControlCharacter(xCursor
, PARAGRAPH_BREAK
, False)
62 # 9. create new text section
63 xTextSection
= xDoc
.createInstance("com.sun.star.text.TextSection")
64 # 10. fill the properties of section
65 self
.__class
__._uno
.checkProperties(
67 {"Condition": "foo EQ 1",
72 # 11. Insert some text to be content on the section
73 xBodyText
.insertString(xCursor
,
74 "The quick brown fox jumps over the lazy dog",
77 xBodyText
.insertTextContent(xCursor
, xTextSection
, True)
78 # 12.1 insert new paragraph. Note: that's here the difference
79 xParagraphCursor
.gotoEndOfParagraph(False) # not select
80 # TODO: how to leave the section now?
81 xBodyText
.insertControlCharacter(xCursor
, PARAGRAPH_BREAK
, False)
82 xBodyText
.insertString(xCursor
, "new paragraph", False)
83 # 13. Access fields to refresh the document
84 xTextFields
= xDoc
.getTextFields()
85 # 14. refresh document to update the fields
87 # 15. retrieve the field
88 xFieldEnum
= xTextFields
.createEnumeration()
89 # Note: we have only one field here, that why nextElement() is just fine here
90 xField
= xFieldEnum
.nextElement()
92 read_content
= xField
.getPropertyValue("Content")
93 self
.assertEqual("0", read_content
)
94 read_content
= xField
.getPropertyValue("Value")
95 self
.assertEqual(0.0, read_content
)
96 # 16. change the value of the field from 0 to 1 and check
97 self
.__class
__._uno
.checkProperties(
104 # 17. refresh document to update the fields again
105 xTextFields
.refresh()
107 url
= os
.path
.join(os
.environ
["TestUserDir"], "VarFields.odt")
108 xDoc
.storeToURL(url
, tuple(list(range(0))))
109 # 19. retrieve the section
110 xSection
= xDoc
.getTextSections().getByIndex(0)
111 # 20. retrieve the condition property of that section
112 read_content
= xSection
.getPropertyValue("Condition")
115 # self.assertEqual("foo EQ 1", readContent)
117 self
.assertEqual("0", read_content
)
119 if __name__
== '__main__':